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