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