MacOS 10.6 support update
[openafs.git] / src / pam / afs_auth.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13
14 #include <syslog.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <pwd.h>
18 #include <unistd.h>
19 #include <sys/param.h>
20 #include <sys/wait.h>
21 #include <afs/kautils.h>
22 #include <signal.h>
23 #include <errno.h>
24
25 #include <security/pam_appl.h>
26 #include <security/pam_modules.h>
27
28 #include "afs_message.h"
29 #include "afs_util.h"
30
31
32 #define RET(x) { retcode = (x); goto out; }
33
34 extern int
35 pam_sm_authenticate(pam_handle_t * pamh, int flags, int argc,
36                     const char **argv)
37 {
38     int retcode = PAM_SUCCESS;
39     int errcode = PAM_SUCCESS;
40     int code;
41     int origmask;
42     int logmask = LOG_UPTO(LOG_INFO);
43     int nowarn = 0;
44     int use_first_pass = 0;
45     int try_first_pass = 0;
46     int ignore_uid = 0;
47     uid_t ignore_uid_id = 0;
48     char my_password_buf[256];
49     char *cell_ptr = NULL;
50     /*
51      * these options are added to handle stupid apps, which won't call
52      * pam_set_cred()
53      */
54     int refresh_token = 0;
55     int set_token = 0;
56     int dont_fork = 0;
57     /* satisfy kdm 2.x
58      */
59     int use_klog = 0;
60     int set_expires = 0;        /* This option is only used in pam_set_cred() */
61     int got_authtok = 0;        /* got PAM_AUTHTOK upon entry */
62     char *user = NULL, *password = NULL;
63     long password_expires = -1;
64     int torch_password = 1;
65     int i;
66     struct pam_conv *pam_convp = NULL;
67     int auth_ok;
68     struct passwd unix_pwd, *upwd = NULL;
69     char upwd_buf[2048];        /* size is a guess. */
70     char *reason = NULL;
71     pid_t cpid, rcpid;
72     int status;
73     struct sigaction newAction, origAction;
74
75
76 #ifndef AFS_SUN56_ENV
77     openlog(pam_afs_ident, LOG_CONS | LOG_PID, LOG_AUTH);
78 #endif
79     origmask = setlogmask(logmask);
80
81     /*
82      * Parse the user options.  Log an error for any unknown options.
83      */
84     for (i = 0; i < argc; i++) {
85         if (strcasecmp(argv[i], "debug") == 0) {
86             logmask |= LOG_MASK(LOG_DEBUG);
87             (void)setlogmask(logmask);
88         } else if (strcasecmp(argv[i], "nowarn") == 0) {
89             nowarn = 1;
90         } else if (strcasecmp(argv[i], "use_first_pass") == 0) {
91             use_first_pass = 1;
92         } else if (strcasecmp(argv[i], "try_first_pass") == 0) {
93             try_first_pass = 1;
94         } else if (strcasecmp(argv[i], "ignore_root") == 0) {
95             ignore_uid = 1;
96             ignore_uid_id = 0;
97         } else if (strcasecmp(argv[i], "ignore_uid") == 0) {
98             i++;
99             if (i == argc) {
100                 pam_afs_syslog(LOG_ERR, PAMAFS_IGNOREUID,
101                                "ignore_uid missing argument");
102                 ignore_uid = 0;
103             } else {
104                 ignore_uid = 1;
105                 ignore_uid_id = (uid_t) strtol(argv[i], (char **)NULL, 10);
106                 if ((ignore_uid_id < 0) || (ignore_uid_id > IGNORE_MAX)) {
107                     ignore_uid = 0;
108                     pam_afs_syslog(LOG_ERR, PAMAFS_IGNOREUID, argv[i]);
109                 }
110             }
111         } else if (strcasecmp(argv[i], "cell") == 0) {
112             i++;
113             if (i == argc) {
114                 pam_afs_syslog(LOG_ERR, PAMAFS_OTHERCELL,
115                                "cell missing argument");
116             } else {
117                 cell_ptr = argv[i];
118                 pam_afs_syslog(LOG_INFO, PAMAFS_OTHERCELL, cell_ptr);
119             }
120         } else if (strcasecmp(argv[i], "refresh_token") == 0) {
121             refresh_token = 1;
122         } else if (strcasecmp(argv[i], "set_token") == 0) {
123             set_token = 1;
124         } else if (strcasecmp(argv[i], "dont_fork") == 0) {
125             if (!use_klog)
126                 dont_fork = 1;
127             else
128                 pam_afs_syslog(LOG_ERR, PAMAFS_CONFLICTOPT, "dont_fork");
129         } else if (strcasecmp(argv[i], "use_klog") == 0) {
130             if (!dont_fork)
131                 use_klog = 1;
132             else
133                 pam_afs_syslog(LOG_ERR, PAMAFS_CONFLICTOPT, "use_klog");
134         } else if (strcasecmp(argv[i], "setenv_password_expires") == 0) {
135             set_expires = 1;
136         } else {
137             pam_afs_syslog(LOG_ERR, PAMAFS_UNKNOWNOPT, argv[i]);
138         }
139     }
140
141     /* Later we use try_first_pass to see if we can try again.    */
142     /* If use_first_pass is true we don't want to ever try again, */
143     /* so turn that flag off right now.                           */
144     if (use_first_pass)
145         try_first_pass = 0;
146
147     if (logmask && LOG_MASK(LOG_DEBUG))
148         pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass,
149                        try_first_pass, ignore_uid, ignore_uid_id,
150                        refresh_token, set_token, dont_fork, use_klog);
151
152     /* Try to get the user-interaction info, if available. */
153     errcode = pam_get_item(pamh, PAM_CONV, (const void **)&pam_convp);
154     if (errcode != PAM_SUCCESS) {
155         pam_afs_syslog(LOG_WARNING, PAMAFS_NO_USER_INT);
156         pam_convp = NULL;
157     }
158
159     /* Who are we trying to authenticate here? */
160     if ((errcode =
161          pam_get_user(pamh, (const char **)&user,
162                       "login: ")) != PAM_SUCCESS) {
163         pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
164         RET(PAM_USER_UNKNOWN);
165     }
166
167     if (logmask && LOG_MASK(LOG_DEBUG))
168         pam_afs_syslog(LOG_DEBUG, PAMAFS_USERNAMEDEBUG, user);
169
170     /*
171      * If the user has a "local" (or via nss, possibly nss_dce) pwent,
172      * and its uid==0, and "ignore_root" was given in pam.conf,
173      * ignore the user.
174      */
175     /* enhanced: use "ignore_uid <number>" to specify the largest uid
176      * which should be ignored by this module
177      */
178 #if     defined(AFS_HPUX_ENV) || defined(AFS_DARWIN100_ENV)
179 #if     defined(AFS_HPUX110_ENV) || defined(AFS_DARWIN100_ENV)
180     i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
181 #else /* AFS_HPUX110_ENV */
182     i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
183     if (i == 0)                 /* getpwnam_r success */
184         upwd = &unix_pwd;
185 #endif /* else AFS_HPUX110_ENV */
186     if (ignore_uid && i == 0 && upwd->pw_uid <= ignore_uid_id) {
187         pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
188         RET(PAM_AUTH_ERR);
189     }
190 #else
191 #if     defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV) || defined(AFS_NBSD_ENV)
192     upwd = getpwnam(user);
193 #elif   defined(_POSIX_PTHREAD_SEMANTICS) && defined(AFS_SUN5_ENV)
194     getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
195 #else
196     upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
197 #endif
198     if (ignore_uid && upwd != NULL && upwd->pw_uid <= ignore_uid_id) {
199         pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
200         RET(PAM_AUTH_ERR);
201     }
202 #endif
203     errcode = pam_get_item(pamh, PAM_AUTHTOK, (const void **)&password);
204     if (errcode != PAM_SUCCESS || password == NULL) {
205         if (use_first_pass) {
206             pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
207             RET(PAM_AUTH_ERR);
208         }
209         password = NULL;        /* In case it isn't already NULL */
210         if (logmask && LOG_MASK(LOG_DEBUG))
211             pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
212     } else if (password[0] == '\0') {
213         /* Actually we *did* get one but it was empty. */
214         torch_password = 0;
215         pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
216         RET(PAM_NEW_AUTHTOK_REQD);
217     } else {
218         if (logmask && LOG_MASK(LOG_DEBUG))
219             pam_afs_syslog(LOG_DEBUG, PAMAFS_GOTPASS, user);
220         torch_password = 0;
221         got_authtok = 1;
222     }
223     if (!(use_first_pass || try_first_pass)) {
224         password = NULL;
225     }
226
227   try_auth:
228     if (password == NULL) {
229
230         torch_password = 1;
231
232         if (use_first_pass)
233             RET(PAM_AUTH_ERR);  /* shouldn't happen */
234         if (try_first_pass)
235             try_first_pass = 0; /* we come back if try_first_pass==1 below */
236
237         if (pam_convp == NULL || pam_convp->conv == NULL) {
238             pam_afs_syslog(LOG_ERR, PAMAFS_CANNOT_PROMPT);
239             RET(PAM_AUTH_ERR);
240         }
241
242         errcode = pam_afs_prompt(pam_convp, &password, 0, PAMAFS_PWD_PROMPT);
243         if (errcode != PAM_SUCCESS || password == NULL) {
244             pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
245             RET(PAM_AUTH_ERR);
246         }
247         if (password[0] == '\0') {
248             pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
249             RET(PAM_NEW_AUTHTOK_REQD);
250         }
251
252         /*
253          * We aren't going to free the password later (we will wipe it,
254          * though), because the storage for it if we get it from other
255          * paths may belong to someone else.  Since we do need to free
256          * this storage, copy it to a buffer that won't need to be freed
257          * later, and free this storage now.
258          */
259
260         strncpy(my_password_buf, password, sizeof(my_password_buf));
261         my_password_buf[sizeof(my_password_buf) - 1] = '\0';
262         memset(password, 0, strlen(password));
263         free(password);
264         password = my_password_buf;
265
266     }
267
268     /* Be sure to allocate a PAG here if we should set a token,
269      * All of the remaining stuff to authenticate the user and to
270      * get a token is done in a child process - if not suppressed by the config,
271      * see below
272      * But dont get a PAG if the refresh_token option was set
273      * We have to do this in such a way because some
274      * apps (such as screensavers) wont call setcred but authenticate :-(
275      */
276     if (!refresh_token) {
277         setpag();
278 #ifdef AFS_KERBEROS_ENV
279         ktc_newpag();
280 #endif
281         if (logmask && LOG_MASK(LOG_DEBUG))
282             syslog(LOG_DEBUG, "New PAG created in pam_authenticate()");
283     }
284
285     if (!dont_fork) {
286         /* Prepare for fork(): set SIGCHLD signal handler to default */
287         sigemptyset(&newAction.sa_mask);
288         newAction.sa_handler = SIG_DFL;
289         newAction.sa_flags = 0;
290         code = sigaction(SIGCHLD, &newAction, &origAction);
291         if (code) {
292             pam_afs_syslog(LOG_ERR, PAMAFS_PAMERROR, errno);
293             RET(PAM_AUTH_ERR);
294         }
295
296         /* Fork a process and let it verify authentication. So that any
297          * memory/sockets allocated will get cleaned up when the child
298          * exits: defect 11686.
299          */
300         if (use_klog) {         /* used by kdm 2.x */
301             if (refresh_token || set_token) {
302                 i = do_klog(user, password, NULL, cell_ptr);
303             } else {
304                 i = do_klog(user, password, "00:00:01", cell_ptr);
305                 ktc_ForgetAllTokens();
306             }
307             if (logmask && LOG_MASK(LOG_DEBUG))
308                 syslog(LOG_DEBUG, "do_klog returned %d", i);
309             auth_ok = i ? 0 : 1;
310         } else {
311             if (logmask && LOG_MASK(LOG_DEBUG))
312                 syslog(LOG_DEBUG, "forking ...");
313             cpid = fork();
314             if (cpid <= 0) {    /* The child process */
315                 if (logmask && LOG_MASK(LOG_DEBUG))
316                     syslog(LOG_DEBUG, "in child");
317                 if (refresh_token || set_token)
318                     code = ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION, user,        /* kerberos name */
319                                                       NULL,     /* instance */
320                                                       cell_ptr, /* realm */
321                                                       password, /* password */
322                                                       0,        /* default lifetime */
323                                                       &password_expires, 0,     /* spare 2 */
324                                                       &reason
325                                                       /* error string */ );
326                 else
327                     code = ka_VerifyUserPassword(KA_USERAUTH_VERSION, user,     /* kerberos name */
328                                                  NULL,  /* instance */
329                                                  cell_ptr,      /* realm */
330                                                  password,      /* password */
331                                                  0,     /* spare 2 */
332                                                  &reason /* error string */ );
333                 if (code) {
334                     pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user,
335                                    reason);
336                     auth_ok = 0;
337                 } else {
338                     auth_ok = 1;
339                 }
340                 if (logmask && LOG_MASK(LOG_DEBUG))
341                     syslog(LOG_DEBUG, "child: auth_ok=%d", auth_ok);
342                 if (cpid == 0)
343                     exit(auth_ok);
344             } else {
345                 do {
346                     if (logmask && LOG_MASK(LOG_DEBUG))
347                         syslog(LOG_DEBUG, "in parent, waiting ...");
348                     rcpid = waitpid(cpid, &status, 0);
349                 } while ((rcpid == -1) && (errno == EINTR));
350
351                 if ((rcpid == cpid) && WIFEXITED(status)) {
352                     auth_ok = WEXITSTATUS(status);
353                 } else {
354                     auth_ok = 0;
355                 }
356                 if (logmask && LOG_MASK(LOG_DEBUG))
357                     syslog(LOG_DEBUG, "parent: auth_ok=%d", auth_ok);
358             }
359         }
360         /* Restore old signal handler */
361         code = sigaction(SIGCHLD, &origAction, NULL);
362         if (code) {
363             pam_afs_syslog(LOG_ERR, PAMAFS_PAMERROR, errno);
364         }
365     } else {                    /* dont_fork, used by httpd */
366         if (logmask && LOG_MASK(LOG_DEBUG))
367             syslog(LOG_DEBUG, "dont_fork");
368         if (refresh_token || set_token)
369             code = ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION, user,        /* kerberos name */
370                                               NULL,     /* instance */
371                                               cell_ptr, /* realm */
372                                               password, /* password */
373                                               0,        /* default lifetime */
374                                               &password_expires, 0,     /* spare 2 */
375                                               &reason /* error string */ );
376         else
377             code = ka_VerifyUserPassword(KA_USERAUTH_VERSION, user,     /* kerberos name */
378                                          NULL,  /* instance */
379                                          cell_ptr,      /* realm */
380                                          password,      /* password */
381                                          0,     /* spare 2 */
382                                          &reason /* error string */ );
383         if (logmask && LOG_MASK(LOG_DEBUG))
384             syslog(LOG_DEBUG, "dont_fork, code = %d", code);
385         if (code) {
386             pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
387             auth_ok = 0;
388         } else {
389             auth_ok = 1;
390         }
391         if (logmask && LOG_MASK(LOG_DEBUG))
392             syslog(LOG_DEBUG, "dont_fork: auth_ok=%d", auth_ok);
393     }
394
395     if (!auth_ok && try_first_pass) {
396         password = NULL;
397         goto try_auth;
398     }
399
400     /* We don't care if this fails; all we can do is try. */
401     /* It is not reasonable to store the password only if it was correct
402      * because it could satisfy another module that is called in the chain
403      * after pam_afs
404      */
405     if (!got_authtok) {
406         torch_password = 0;
407         (void)pam_set_item(pamh, PAM_AUTHTOK, password);
408     }
409
410     if (logmask && LOG_MASK(LOG_DEBUG))
411         syslog(LOG_DEBUG, "leaving auth: auth_ok=%d", auth_ok);
412     if (code == KANOENT)
413         RET(PAM_USER_UNKNOWN);
414     RET(auth_ok ? PAM_SUCCESS : PAM_AUTH_ERR);
415
416   out:
417     if (password) {
418         /* we store the password in the data portion */
419         char *tmp = strdup(password);
420         (void)pam_set_data(pamh, pam_afs_lh, tmp, lc_cleanup);
421         if (torch_password)
422             memset(password, 0, strlen(password));
423     }
424     (void)setlogmask(origmask);
425 #ifndef AFS_SUN56_ENV
426     closelog();
427 #endif
428     return retcode;
429 }