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