Imported Upstream version 1.6.8p5
[debian/sudo] / auth / pam.c
1 /*
2  * Copyright (c) 1999-2004 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)
60 # define PAM_CONST      const
61 #else
62 # define PAM_CONST
63 #endif
64
65 #ifndef lint
66 static const char rcsid[] = "$Sudo: pam.c,v 1.43 2004/06/28 14:51:50 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,
95             "unable to initialize PAM");
96         return(AUTH_FATAL);
97     }
98     if (strcmp(user_tty, "unknown"))
99         (void) pam_set_item(pamh, PAM_TTY, user_tty);
100
101     return(AUTH_SUCCESS);
102 }
103
104 int
105 pam_verify(pw, prompt, auth)
106     struct passwd *pw;
107     char *prompt;
108     sudo_auth *auth;
109 {
110     const char *s;
111     int *pam_status = (int *) auth->data;
112
113     def_prompt = prompt;        /* for sudo_conv */
114
115     /* PAM_SILENT prevents the authentication service from generating output. */
116     *pam_status = pam_authenticate(pamh, PAM_SILENT);
117     switch (*pam_status) {
118         case PAM_SUCCESS:
119             *pam_status = pam_acct_mgmt(pamh, PAM_SILENT);
120             switch (*pam_status) {
121                 case PAM_SUCCESS:
122                     return(AUTH_SUCCESS);
123                 case PAM_AUTH_ERR:
124                     log_error(NO_EXIT|NO_MAIL, "pam_acct_mgmt: %d",
125                         *pam_status);
126                     return(AUTH_FAILURE);
127                 case PAM_NEW_AUTHTOK_REQD:
128                     log_error(NO_EXIT|NO_MAIL, "%s, %s"
129                         "Account or password is expired",
130                         "reset your password and try again");
131                     *pam_status = pam_chauthtok(pamh, PAM_CHANGE_EXPIRED_AUTHTOK);
132                     if (*pam_status == PAM_SUCCESS)
133                         return(AUTH_SUCCESS);
134                     if ((s = pam_strerror(pamh, *pam_status)))
135                         log_error(NO_EXIT|NO_MAIL, "pam_chauthtok: %s",s);
136                     return(AUTH_FAILURE);
137                 case PAM_ACCT_EXPIRED:
138                     log_error(NO_EXIT|NO_MAIL, "%s, %s"
139                         "Account or password is expired",
140                         "contact your system administrator");
141                     /* FALLTHROUGH */
142                 default:
143                     return(AUTH_FAILURE);
144             }
145         case PAM_AUTH_ERR:
146         case PAM_MAXTRIES:
147             return(AUTH_FAILURE);
148         default:
149             if ((s = pam_strerror(pamh, *pam_status)))
150                 log_error(NO_EXIT|NO_MAIL, "pam_authenticate: %s", s);
151             return(AUTH_FATAL);
152     }
153 }
154
155 int
156 pam_cleanup(pw, auth)
157     struct passwd *pw;
158     sudo_auth *auth;
159 {
160     int *pam_status = (int *) auth->data;
161
162     /* If successful, we can't close the session until pam_prep_user() */
163     if (auth->status == AUTH_SUCCESS)
164         return(AUTH_SUCCESS);
165
166     *pam_status = pam_end(pamh, *pam_status | PAM_DATA_SILENT);
167     return(*pam_status == PAM_SUCCESS ? AUTH_SUCCESS : AUTH_FAILURE);
168 }
169
170 int
171 pam_prep_user(pw)
172     struct passwd *pw;
173 {
174     if (pamh == NULL)
175         pam_init(pw, NULL, NULL);
176
177     /*
178      * Set PAM_USER to the user we are changing *to* and
179      * set PAM_RUSER to the user we are coming *from*.
180      */
181     (void) pam_set_item(pamh, PAM_USER, pw->pw_name);
182     (void) pam_set_item(pamh, PAM_RUSER, user_name);
183
184     /*
185      * Set credentials (may include resource limits, device ownership, etc).
186      * We don't check the return value here because in Linux-PAM 0.75
187      * it returns the last saved return code, not the return code
188      * for the setcred module.  Because we haven't called pam_authenticate(),
189      * this is not set and so pam_setcred() returns PAM_PERM_DENIED.
190      * We can't call pam_acct_mgmt() with Linux-PAM for a similar reason.
191      */
192     (void) pam_setcred(pamh, PAM_ESTABLISH_CRED);
193
194     if (pam_end(pamh, PAM_SUCCESS | PAM_DATA_SILENT) == PAM_SUCCESS)
195         return(AUTH_SUCCESS);
196     else
197         return(AUTH_FAILURE);
198 }
199
200 /*
201  * ``Conversation function'' for PAM.
202  * XXX - does not handle PAM_BINARY_PROMPT
203  */
204 static int
205 sudo_conv(num_msg, msg, response, appdata_ptr)
206     int num_msg;
207     PAM_CONST struct pam_message **msg;
208     struct pam_response **response;
209     VOID *appdata_ptr;
210 {
211     struct pam_response *pr;
212     PAM_CONST struct pam_message *pm;
213     const char *p = def_prompt;
214     char *pass;
215     int n, flags;
216     extern int nil_pw;
217
218     if ((*response = malloc(num_msg * sizeof(struct pam_response))) == NULL)
219         return(PAM_CONV_ERR);
220     zero_bytes(*response, num_msg * sizeof(struct pam_response));
221
222     for (pr = *response, pm = *msg, n = num_msg; n--; pr++, pm++) {
223         flags = tgetpass_flags;
224         switch (pm->msg_style) {
225             case PAM_PROMPT_ECHO_ON:
226                 SET(flags, TGP_ECHO);
227             case PAM_PROMPT_ECHO_OFF:
228                 /* Only override PAM prompt if it matches /^Password: ?/ */
229                 if (strncmp(pm->msg, "Password:", 9) || (pm->msg[9] != '\0'
230                     && (pm->msg[9] != ' ' || pm->msg[10] != '\0')))
231                     p = pm->msg;
232                 /* Read the password. */
233                 pass = tgetpass(p, def_passwd_timeout * 60, flags);
234                 pr->resp = estrdup(pass ? pass : "");
235                 if (*pr->resp == '\0')
236                     nil_pw = 1;         /* empty password */
237                 else
238                     zero_bytes(pass, strlen(pass));
239                 break;
240             case PAM_TEXT_INFO:
241                 if (pm->msg)
242                     (void) puts(pm->msg);
243                 break;
244             case PAM_ERROR_MSG:
245                 if (pm->msg) {
246                     (void) fputs(pm->msg, stderr);
247                     (void) fputc('\n', stderr);
248                 }
249                 break;
250             default:
251                 /* Zero and free allocated memory and return an error. */
252                 for (pr = *response, n = num_msg; n--; pr++) {
253                     if (pr->resp != NULL) {
254                         zero_bytes(pr->resp, strlen(pr->resp));
255                         free(pr->resp);
256                         pr->resp = NULL;
257                     }
258                 }
259                 zero_bytes(*response, num_msg * sizeof(struct pam_response));
260                 free(*response);
261                 *response = NULL;
262                 return(PAM_CONV_ERR);
263         }
264     }
265
266     return(PAM_SUCCESS);
267 }