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