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