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