f4269b841bfd969161fd82ab540be57e4191c8c2
[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(__LIBPAM_VERSION) || defined(__LINUX_PAM__)
70 # define PAM_CONST      const
71 #else
72 # define PAM_CONST
73 #endif
74
75 static int sudo_conv __P((int, PAM_CONST struct pam_message **,
76                           struct pam_response **, void *));
77 static char *def_prompt = "Password:";
78 static int gotintr;
79
80 #ifndef PAM_DATA_SILENT
81 #define PAM_DATA_SILENT 0
82 #endif
83
84 static pam_handle_t *pamh;      /* global due to pam_prep_user() */
85
86 int
87 pam_init(pw, promptp, auth)
88     struct passwd *pw;
89     char **promptp;
90     sudo_auth *auth;
91 {
92     static struct pam_conv pam_conv;
93     static int pam_status;
94
95     /* Initial PAM setup */
96     if (auth != NULL)
97         auth->data = (void *) &pam_status;
98     pam_conv.conv = sudo_conv;
99     pam_status = pam_start("sudo", pw->pw_name, &pam_conv, &pamh);
100     if (pam_status != PAM_SUCCESS) {
101         log_error(USE_ERRNO|NO_EXIT|NO_MAIL, "unable to initialize PAM");
102         return(AUTH_FATAL);
103     }
104
105     /*
106      * Set PAM_RUSER to the invoking user (the "from" user).
107      * We set PAM_RHOST to avoid a bug in Solaris 7 and below.
108      */
109     (void) pam_set_item(pamh, PAM_RUSER, user_name);
110     (void) pam_set_item(pamh, PAM_RHOST, user_host);
111
112     /*
113      * Some versions of pam_lastlog have a bug that
114      * will cause a crash if PAM_TTY is not set so if
115      * there is no tty, set PAM_TTY to the empty string.
116      */
117     if (user_ttypath == NULL)
118         (void) pam_set_item(pamh, PAM_TTY, "");
119     else
120         (void) pam_set_item(pamh, PAM_TTY, user_ttypath);
121
122     return(AUTH_SUCCESS);
123 }
124
125 int
126 pam_verify(pw, prompt, auth)
127     struct passwd *pw;
128     char *prompt;
129     sudo_auth *auth;
130 {
131     const char *s;
132     int *pam_status = (int *) auth->data;
133
134     def_prompt = prompt;        /* for sudo_conv */
135
136     /* PAM_SILENT prevents the authentication service from generating output. */
137     *pam_status = pam_authenticate(pamh, PAM_SILENT);
138     switch (*pam_status) {
139         case PAM_SUCCESS:
140             *pam_status = pam_acct_mgmt(pamh, PAM_SILENT);
141             switch (*pam_status) {
142                 case PAM_SUCCESS:
143                     return(AUTH_SUCCESS);
144                 case PAM_AUTH_ERR:
145                     log_error(NO_EXIT|NO_MAIL, "pam_acct_mgmt: %d",
146                         *pam_status);
147                     return(AUTH_FAILURE);
148                 case PAM_NEW_AUTHTOK_REQD:
149                     log_error(NO_EXIT|NO_MAIL, "%s, %s",
150                         "Account or password is expired",
151                         "reset your password and try again");
152                     *pam_status = pam_chauthtok(pamh,
153                         PAM_CHANGE_EXPIRED_AUTHTOK);
154                     if (*pam_status == PAM_SUCCESS)
155                         return(AUTH_SUCCESS);
156                     if ((s = pam_strerror(pamh, *pam_status)))
157                         log_error(NO_EXIT|NO_MAIL, "pam_chauthtok: %s", s);
158                     return(AUTH_FAILURE);
159                 case PAM_AUTHTOK_EXPIRED:
160                     log_error(NO_EXIT|NO_MAIL,
161                         "Password expired, contact your system administrator");
162                     return(AUTH_FATAL);
163                 case PAM_ACCT_EXPIRED:
164                     log_error(NO_EXIT|NO_MAIL, "%s %s",
165                         "Account expired or PAM config lacks an \"account\"",
166                         "section for sudo, contact your system administrator");
167                     return(AUTH_FATAL);
168             }
169             /* FALLTHROUGH */
170         case PAM_AUTH_ERR:
171             if (gotintr) {
172                 /* error or ^C from tgetpass() */
173                 return(AUTH_INTR);
174             }
175         case PAM_MAXTRIES:
176         case PAM_PERM_DENIED:
177             return(AUTH_FAILURE);
178         default:
179             if ((s = pam_strerror(pamh, *pam_status)))
180                 log_error(NO_EXIT|NO_MAIL, "pam_authenticate: %s", s);
181             return(AUTH_FATAL);
182     }
183 }
184
185 int
186 pam_cleanup(pw, auth)
187     struct passwd *pw;
188     sudo_auth *auth;
189 {
190     int *pam_status = (int *) auth->data;
191
192     /* If successful, we can't close the session until pam_prep_user() */
193     if (auth->status == AUTH_SUCCESS)
194         return(AUTH_SUCCESS);
195
196     *pam_status = pam_end(pamh, *pam_status | PAM_DATA_SILENT);
197     return(*pam_status == PAM_SUCCESS ? AUTH_SUCCESS : AUTH_FAILURE);
198 }
199
200 int
201 pam_prep_user(pw)
202     struct passwd *pw;
203 {
204     int eval;
205
206     if (pamh == NULL)
207         pam_init(pw, NULL, NULL);
208
209     /*
210      * Update PAM_USER to reference the user we are running the command
211      * as, as opposed to the user we authenticated as.
212      */
213     (void) pam_set_item(pamh, PAM_USER, pw->pw_name);
214
215     /*
216      * Set credentials (may include resource limits, device ownership, etc).
217      * We don't check the return value here because in Linux-PAM 0.75
218      * it returns the last saved return code, not the return code
219      * for the setcred module.  Because we haven't called pam_authenticate(),
220      * this is not set and so pam_setcred() returns PAM_PERM_DENIED.
221      * We can't call pam_acct_mgmt() with Linux-PAM for a similar reason.
222      */
223     (void) pam_setcred(pamh, PAM_ESTABLISH_CRED);
224
225 #ifndef NO_PAM_SESSION
226     /*
227      * To fully utilize PAM sessions we would need to keep a
228      * sudo process around until the command exits.  However, we
229      * can at least cause pam_limits to be run by opening and then
230      * immediately closing the session.
231      */
232     if ((eval = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
233         (void) pam_end(pamh, eval | PAM_DATA_SILENT);
234         return(AUTH_FAILURE);
235     }
236     (void) pam_close_session(pamh, 0);
237 #endif
238
239     if (pam_end(pamh, PAM_SUCCESS | PAM_DATA_SILENT) == PAM_SUCCESS)
240         return(AUTH_SUCCESS);
241     else
242         return(AUTH_FAILURE);
243 }
244
245 /*
246  * ``Conversation function'' for PAM.
247  * XXX - does not handle PAM_BINARY_PROMPT
248  */
249 static int
250 sudo_conv(num_msg, msg, response, appdata_ptr)
251     int num_msg;
252     PAM_CONST struct pam_message **msg;
253     struct pam_response **response;
254     void *appdata_ptr;
255 {
256     struct pam_response *pr;
257     PAM_CONST struct pam_message *pm;
258     const char *prompt;
259     char *pass;
260     int n, flags, std_prompt;
261
262     if ((*response = malloc(num_msg * sizeof(struct pam_response))) == NULL)
263         return(PAM_SYSTEM_ERR);
264     zero_bytes(*response, num_msg * sizeof(struct pam_response));
265
266     for (pr = *response, pm = *msg, n = num_msg; n--; pr++, pm++) {
267         flags = tgetpass_flags;
268         switch (pm->msg_style) {
269             case PAM_PROMPT_ECHO_ON:
270                 SET(flags, TGP_ECHO);
271             case PAM_PROMPT_ECHO_OFF:
272                 prompt = def_prompt;
273
274                 /* Is the sudo prompt standard? (If so, we'l just use PAM's) */
275                 std_prompt =  strncmp(def_prompt, "Password:", 9) == 0 &&
276                     (def_prompt[9] == '\0' ||
277                     (def_prompt[9] == ' ' && def_prompt[10] == '\0'));
278
279                 /* Only override PAM prompt if it matches /^Password: ?/ */
280 #if defined(PAM_TEXT_DOMAIN) && defined(HAVE_DGETTEXT)
281                 if (!def_passprompt_override && (std_prompt ||
282                     (strcmp(pm->msg, dgettext(PAM_TEXT_DOMAIN, "Password: ")) &&
283                     strcmp(pm->msg, dgettext(PAM_TEXT_DOMAIN, "Password:")))))
284                     prompt = pm->msg;
285 #else
286                 if (!def_passprompt_override && (std_prompt ||
287                     strncmp(pm->msg, "Password:", 9) || (pm->msg[9] != '\0'
288                     && (pm->msg[9] != ' ' || pm->msg[10] != '\0'))))
289                     prompt = pm->msg;
290 #endif
291                 /* Read the password unless interrupted. */
292                 pass = tgetpass(prompt, def_passwd_timeout * 60, flags);
293                 if (pass == NULL) {
294                     /* We got ^C instead of a password; abort quickly. */
295                     if (errno == EINTR)
296                         gotintr = 1;
297 #if defined(__darwin__) || defined(__APPLE__)
298                     pass = "";
299 #else
300                     goto err;
301 #endif
302                 }
303                 pr->resp = estrdup(pass);
304                 zero_bytes(pass, strlen(pass));
305                 break;
306             case PAM_TEXT_INFO:
307                 if (pm->msg)
308                     (void) puts(pm->msg);
309                 break;
310             case PAM_ERROR_MSG:
311                 if (pm->msg) {
312                     (void) fputs(pm->msg, stderr);
313                     (void) fputc('\n', stderr);
314                 }
315                 break;
316             default:
317                 goto err;
318         }
319     }
320
321     return(PAM_SUCCESS);
322
323 err:
324     /* Zero and free allocated memory and return an error. */
325     for (pr = *response, n = num_msg; n--; pr++) {
326         if (pr->resp != NULL) {
327             zero_bytes(pr->resp, strlen(pr->resp));
328             free(pr->resp);
329             pr->resp = NULL;
330         }
331     }
332     zero_bytes(*response, num_msg * sizeof(struct pam_response));
333     free(*response);
334     *response = NULL;
335     return(gotintr ? PAM_AUTH_ERR : PAM_CONV_ERR);
336 }