f9fa26d34387cc3ea6d6dec2d61ad99be3f9a2f4
[debian/sudo] / auth / pam.c
1 /*
2  * Copyright (c) 1999-2005, 2007-2009 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  *
16  * Sponsored in part by the Defense Advanced Research Projects
17  * Agency (DARPA) and Air Force Research Laboratory, Air Force
18  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
19  */
20
21 #include <config.h>
22
23 #include <sys/types.h>
24 #include <sys/param.h>
25 #include <stdio.h>
26 #ifdef STDC_HEADERS
27 # include <stdlib.h>
28 # include <stddef.h>
29 #else
30 # ifdef HAVE_STDLIB_H
31 #  include <stdlib.h>
32 # endif
33 #endif /* STDC_HEADERS */
34 #ifdef HAVE_STRING_H
35 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
36 #  include <memory.h>
37 # endif
38 # include <string.h>
39 #else
40 # ifdef HAVE_STRINGS_H
41 #  include <strings.h>
42 # endif
43 #endif /* HAVE_STRING_H */
44 #ifdef HAVE_UNISTD_H
45 # include <unistd.h>
46 #endif /* HAVE_UNISTD_H */
47 #include <pwd.h>
48 #include <errno.h>
49
50 #ifdef HAVE_PAM_PAM_APPL_H
51 # include <pam/pam_appl.h>
52 #else
53 # include <security/pam_appl.h>
54 #endif
55
56 #ifdef HAVE_DGETTEXT
57 # include <libintl.h>
58 # if defined(__LINUX_PAM__)
59 #  define PAM_TEXT_DOMAIN       "Linux-PAM"
60 # elif defined(__sun__)
61 #  define PAM_TEXT_DOMAIN       "SUNW_OST_SYSOSPAM"
62 # endif
63 #endif
64
65 #include "sudo.h"
66 #include "sudo_auth.h"
67
68 /* Only OpenPAM and Linux PAM use const qualifiers. */
69 #if defined(_OPENPAM) || defined(OPENPAM_VERSION) || \
70     defined(__LIBPAM_VERSION) || defined(__LINUX_PAM__)
71 # define PAM_CONST      const
72 #else
73 # define PAM_CONST
74 #endif
75
76 static int sudo_conv __P((int, PAM_CONST struct pam_message **,
77                           struct pam_response **, void *));
78 static char *def_prompt = "Password:";
79 static int gotintr;
80
81 #ifndef PAM_DATA_SILENT
82 #define PAM_DATA_SILENT 0
83 #endif
84
85 static pam_handle_t *pamh;      /* global due to pam_prep_user() */
86
87 int
88 pam_init(pw, promptp, auth)
89     struct passwd *pw;
90     char **promptp;
91     sudo_auth *auth;
92 {
93     static struct pam_conv pam_conv;
94     static int pam_status;
95
96     /* Initial PAM setup */
97     if (auth != NULL)
98         auth->data = (void *) &pam_status;
99     pam_conv.conv = sudo_conv;
100     pam_status = pam_start("sudo", pw->pw_name, &pam_conv, &pamh);
101     if (pam_status != PAM_SUCCESS) {
102         log_error(USE_ERRNO|NO_EXIT|NO_MAIL, "unable to initialize PAM");
103         return(AUTH_FATAL);
104     }
105
106     /*
107      * Set PAM_RUSER to the invoking user (the "from" user).
108      * We set PAM_RHOST to avoid a bug in Solaris 7 and below.
109      */
110     (void) pam_set_item(pamh, PAM_RUSER, user_name);
111     (void) pam_set_item(pamh, PAM_RHOST, user_host);
112
113     /*
114      * Some versions of pam_lastlog have a bug that
115      * will cause a crash if PAM_TTY is not set so if
116      * there is no tty, set PAM_TTY to the empty string.
117      */
118     if (user_ttypath == NULL)
119         (void) pam_set_item(pamh, PAM_TTY, "");
120     else
121         (void) pam_set_item(pamh, PAM_TTY, user_ttypath);
122
123     return(AUTH_SUCCESS);
124 }
125
126 int
127 pam_verify(pw, prompt, auth)
128     struct passwd *pw;
129     char *prompt;
130     sudo_auth *auth;
131 {
132     const char *s;
133     int *pam_status = (int *) auth->data;
134
135     def_prompt = prompt;        /* for sudo_conv */
136
137     /* PAM_SILENT prevents the authentication service from generating output. */
138     *pam_status = pam_authenticate(pamh, PAM_SILENT);
139     switch (*pam_status) {
140         case PAM_SUCCESS:
141             *pam_status = pam_acct_mgmt(pamh, PAM_SILENT);
142             switch (*pam_status) {
143                 case PAM_SUCCESS:
144                     return(AUTH_SUCCESS);
145                 case PAM_AUTH_ERR:
146                     log_error(NO_EXIT|NO_MAIL, "pam_acct_mgmt: %d",
147                         *pam_status);
148                     return(AUTH_FAILURE);
149                 case PAM_NEW_AUTHTOK_REQD:
150                     log_error(NO_EXIT|NO_MAIL, "%s, %s",
151                         "Account or password is expired",
152                         "reset your password and try again");
153                     *pam_status = pam_chauthtok(pamh,
154                         PAM_CHANGE_EXPIRED_AUTHTOK);
155                     if (*pam_status == PAM_SUCCESS)
156                         return(AUTH_SUCCESS);
157                     if ((s = pam_strerror(pamh, *pam_status)))
158                         log_error(NO_EXIT|NO_MAIL, "pam_chauthtok: %s", s);
159                     return(AUTH_FAILURE);
160                 case PAM_AUTHTOK_EXPIRED:
161                     log_error(NO_EXIT|NO_MAIL,
162                         "Password expired, contact your system administrator");
163                     return(AUTH_FATAL);
164                 case PAM_ACCT_EXPIRED:
165                     log_error(NO_EXIT|NO_MAIL, "%s %s",
166                         "Account expired or PAM config lacks an \"account\"",
167                         "section for sudo, contact your system administrator");
168                     return(AUTH_FATAL);
169             }
170             /* FALLTHROUGH */
171         case PAM_AUTH_ERR:
172             if (gotintr) {
173                 /* error or ^C from tgetpass() */
174                 return(AUTH_INTR);
175             }
176         case PAM_MAXTRIES:
177         case PAM_PERM_DENIED:
178             return(AUTH_FAILURE);
179         default:
180             if ((s = pam_strerror(pamh, *pam_status)))
181                 log_error(NO_EXIT|NO_MAIL, "pam_authenticate: %s", s);
182             return(AUTH_FATAL);
183     }
184 }
185
186 int
187 pam_cleanup(pw, auth)
188     struct passwd *pw;
189     sudo_auth *auth;
190 {
191     int *pam_status = (int *) auth->data;
192
193     /* If successful, we can't close the session until pam_prep_user() */
194     if (auth->status == AUTH_SUCCESS)
195         return(AUTH_SUCCESS);
196
197     *pam_status = pam_end(pamh, *pam_status | PAM_DATA_SILENT);
198     return(*pam_status == PAM_SUCCESS ? AUTH_SUCCESS : AUTH_FAILURE);
199 }
200
201 int
202 pam_prep_user(pw)
203     struct passwd *pw;
204 {
205     int eval;
206
207     if (pamh == NULL)
208         pam_init(pw, NULL, NULL);
209
210     /*
211      * Update PAM_USER to reference the user we are running the command
212      * as, as opposed to the user we authenticated as.
213      */
214     (void) pam_set_item(pamh, PAM_USER, pw->pw_name);
215
216     /*
217      * Set credentials (may include resource limits, device ownership, etc).
218      * We don't check the return value here because in Linux-PAM 0.75
219      * it returns the last saved return code, not the return code
220      * for the setcred module.  Because we haven't called pam_authenticate(),
221      * this is not set and so pam_setcred() returns PAM_PERM_DENIED.
222      * We can't call pam_acct_mgmt() with Linux-PAM for a similar reason.
223      */
224     (void) pam_setcred(pamh, PAM_ESTABLISH_CRED);
225
226 #ifndef NO_PAM_SESSION
227     /*
228      * To fully utilize PAM sessions we would need to keep a
229      * sudo process around until the command exits.  However, we
230      * can at least cause pam_limits to be run by opening and then
231      * immediately closing the session.
232      */
233     if ((eval = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
234         (void) pam_end(pamh, eval | PAM_DATA_SILENT);
235         return(AUTH_FAILURE);
236     }
237     (void) pam_close_session(pamh, 0);
238 #endif
239
240     if (pam_end(pamh, PAM_SUCCESS | PAM_DATA_SILENT) == PAM_SUCCESS)
241         return(AUTH_SUCCESS);
242     else
243         return(AUTH_FAILURE);
244 }
245
246 /*
247  * ``Conversation function'' for PAM.
248  * XXX - does not handle PAM_BINARY_PROMPT
249  */
250 static int
251 sudo_conv(num_msg, msg, response, appdata_ptr)
252     int num_msg;
253     PAM_CONST struct pam_message **msg;
254     struct pam_response **response;
255     void *appdata_ptr;
256 {
257     struct pam_response *pr;
258     PAM_CONST struct pam_message *pm;
259     const char *prompt;
260     char *pass;
261     int n, flags, std_prompt;
262
263     if ((*response = malloc(num_msg * sizeof(struct pam_response))) == NULL)
264         return(PAM_SYSTEM_ERR);
265     zero_bytes(*response, num_msg * sizeof(struct pam_response));
266
267     for (pr = *response, pm = *msg, n = num_msg; n--; pr++, pm++) {
268         flags = tgetpass_flags;
269         switch (pm->msg_style) {
270             case PAM_PROMPT_ECHO_ON:
271                 SET(flags, TGP_ECHO);
272             case PAM_PROMPT_ECHO_OFF:
273                 prompt = def_prompt;
274
275                 /* Is the sudo prompt standard? (If so, we'l just use PAM's) */
276                 std_prompt =  strncmp(def_prompt, "Password:", 9) == 0 &&
277                     (def_prompt[9] == '\0' ||
278                     (def_prompt[9] == ' ' && def_prompt[10] == '\0'));
279
280                 /* Only override PAM prompt if it matches /^Password: ?/ */
281 #if defined(PAM_TEXT_DOMAIN) && defined(HAVE_DGETTEXT)
282                 if (!def_passprompt_override && (std_prompt ||
283                     (strcmp(pm->msg, dgettext(PAM_TEXT_DOMAIN, "Password: ")) &&
284                     strcmp(pm->msg, dgettext(PAM_TEXT_DOMAIN, "Password:")))))
285                     prompt = pm->msg;
286 #else
287                 if (!def_passprompt_override && (std_prompt ||
288                     strncmp(pm->msg, "Password:", 9) || (pm->msg[9] != '\0'
289                     && (pm->msg[9] != ' ' || pm->msg[10] != '\0'))))
290                     prompt = pm->msg;
291 #endif
292                 /* Read the password unless interrupted. */
293                 pass = tgetpass(prompt, def_passwd_timeout * 60, flags);
294                 if (pass == NULL) {
295                     /* We got ^C instead of a password; abort quickly. */
296                     if (errno == EINTR)
297                         gotintr = 1;
298 #if defined(__darwin__) || defined(__APPLE__)
299                     pass = "";
300 #else
301                     goto err;
302 #endif
303                 }
304                 pr->resp = estrdup(pass);
305                 zero_bytes(pass, strlen(pass));
306                 break;
307             case PAM_TEXT_INFO:
308                 if (pm->msg)
309                     (void) puts(pm->msg);
310                 break;
311             case PAM_ERROR_MSG:
312                 if (pm->msg) {
313                     (void) fputs(pm->msg, stderr);
314                     (void) fputc('\n', stderr);
315                 }
316                 break;
317             default:
318                 goto err;
319         }
320     }
321
322     return(PAM_SUCCESS);
323
324 err:
325     /* Zero and free allocated memory and return an error. */
326     for (pr = *response, n = num_msg; n--; pr++) {
327         if (pr->resp != NULL) {
328             zero_bytes(pr->resp, strlen(pr->resp));
329             free(pr->resp);
330             pr->resp = NULL;
331         }
332     }
333     zero_bytes(*response, num_msg * sizeof(struct pam_response));
334     free(*response);
335     *response = NULL;
336     return(gotintr ? PAM_AUTH_ERR : PAM_CONV_ERR);
337 }