dragonfly-bsd-userland-20090427
[openafs.git] / src / pam / afs_setcred.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 <syslog.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <pwd.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <afsconfig.h>
17 #include <afs/param.h>
18
19 #include <security/pam_appl.h>
20 #include <security/pam_modules.h>
21
22 RCSID
23     ("$Header$");
24
25 #include <sys/param.h>
26 #include <afs/kautils.h>
27 #include "afs_message.h"
28 #include "afs_util.h"
29
30
31
32 #define RET(x) { retcode = (x); goto out; }
33
34 #if defined(AFS_KERBEROS_ENV)
35 extern char *ktc_tkt_string();
36 #endif
37
38 extern int
39 pam_sm_setcred(pam_handle_t * pamh, int flags, int argc, const char **argv)
40 {
41     int retcode = PAM_SUCCESS;
42     int errcode = PAM_SUCCESS;
43     int origmask;
44     int logmask = LOG_UPTO(LOG_INFO);
45     int nowarn = 0;
46     int use_first_pass = 1;     /* use the password passed in by auth */
47     int try_first_pass = 0;
48     int got_authtok = 0;
49     int ignore_uid = 0;
50     int no_unlog = 0;
51     uid_t ignore_uid_id = 0;
52     int refresh_token = 0;
53     int set_expires = 0;        /* the default is to not to set the env variable */
54     int use_klog = 0;
55     int i;
56     struct pam_conv *pam_convp = NULL;
57     char my_password_buf[256];
58     char *cell_ptr = NULL;
59     char sbuffer[100];
60     char *password = NULL;
61     int torch_password = 1;
62     int auth_ok = 0;
63     char *lh;
64     char *user = NULL;
65     int password_expires = -1;
66     char *reason = NULL;
67     struct passwd unix_pwd, *upwd = NULL;
68     char upwd_buf[2048];        /* size is a guess. */
69
70 #ifndef AFS_SUN56_ENV
71     openlog(pam_afs_ident, LOG_CONS, LOG_AUTH);
72 #endif
73     origmask = setlogmask(logmask);
74
75     /*
76      * Parse the user options.  Log an error for any unknown options.
77      */
78     for (i = 0; i < argc; i++) {
79         if (strcasecmp(argv[i], "debug") == 0) {
80             logmask |= LOG_MASK(LOG_DEBUG);
81             (void)setlogmask(logmask);
82         } else if (strcasecmp(argv[i], "nowarn") == 0) {
83             nowarn = 1;
84         } else if (strcasecmp(argv[i], "use_first_pass") == 0) {
85             use_first_pass = 1; /* practically redundant */
86         } else if (strcasecmp(argv[i], "try_first_pass") == 0) {
87             try_first_pass = 1;
88         } else if (strcasecmp(argv[i], "ignore_root") == 0) {
89             ignore_uid = 1;
90             ignore_uid_id = 0;
91         } else if (strcasecmp(argv[i], "ignore_uid") == 0) {
92             i++;
93             if (i == argc) {
94                 pam_afs_syslog(LOG_ERR, PAMAFS_IGNOREUID,
95                                "ignore_uid missing argument");
96                 ignore_uid = 0;
97             } else {
98                 ignore_uid = 1;
99                 ignore_uid_id = (uid_t) strtol(argv[i], (char **)NULL, 10);
100                 if ((0 > ignore_uid_id) || (ignore_uid_id > IGNORE_MAX)) {
101                     ignore_uid = 0;
102                     pam_afs_syslog(LOG_ERR, PAMAFS_IGNOREUID, argv[i]);
103                 }
104             }
105         } else if (strcasecmp(argv[i], "cell") == 0) {
106             i++;
107             if (i == argc) {
108                 pam_afs_syslog(LOG_ERR, PAMAFS_OTHERCELL,
109                                "cell missing argument");
110             } else {
111                 cell_ptr = argv[i];
112                 pam_afs_syslog(LOG_INFO, PAMAFS_OTHERCELL, cell_ptr);
113             }
114         } else if (strcasecmp(argv[i], "no_unlog") == 0) {
115             no_unlog = 1;
116         } else if (strcasecmp(argv[i], "refresh_token") == 0) {
117             refresh_token = 1;
118         } else if (strcasecmp(argv[i], "set_token") == 0) {
119             ;
120         } else if (strcasecmp(argv[i], "dont_fork") == 0) {
121             ;
122         } else if (strcasecmp(argv[i], "use_klog") == 0) {
123             use_klog = 1;
124         } else if (strcasecmp(argv[i], "setenv_password_expires") == 0) {
125             set_expires = 1;
126         } else {
127             pam_afs_syslog(LOG_ERR, PAMAFS_UNKNOWNOPT, argv[i]);
128         }
129     }
130
131     if (use_first_pass)
132         try_first_pass = 0;
133
134     if (logmask && LOG_MASK(LOG_DEBUG))
135         pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass,
136                        try_first_pass, ignore_uid, ignore_uid_id, 8, 8, 8, 8);
137     /* Try to get the user-interaction info, if available. */
138     errcode = pam_get_item(pamh, PAM_CONV, (const void **)&pam_convp);
139     if (errcode != PAM_SUCCESS) {
140         if (logmask && LOG_MASK(LOG_DEBUG))
141             pam_afs_syslog(LOG_DEBUG, PAMAFS_NO_USER_INT);
142         pam_convp = NULL;
143     }
144
145     /* Who are we trying to authenticate here? */
146     if ((errcode =
147          pam_get_user(pamh, (const char **)&user,
148                       "AFS username:")) != PAM_SUCCESS) {
149         pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
150         RET(PAM_USER_UNKNOWN);
151     }
152     /*
153      * If the user has a "local" (or via nss, possibly nss_dce) pwent,
154      * and its uid==0, and "ignore_root" was given in pam.conf,
155      * ignore the user.
156      */
157     /* enhanced: use "ignore_uid <number>" to specify the largest uid
158      * which should be ignored by this module
159      */
160 #if     defined(AFS_HPUX_ENV)
161 #if     defined(AFS_HPUX110_ENV)
162     i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
163 #else /* AFS_HPUX110_ENV */
164     i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
165     if (i == 0)                 /* getpwnam_r success */
166         upwd = &unix_pwd;
167 #endif /* AFS_HPUX110_ENV */
168     if (ignore_uid && i == 0 && upwd->pw_uid <= ignore_uid_id) {
169         pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
170         RET(PAM_AUTH_ERR);
171     }
172 #else
173 #if     defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV) || defined(AFS_NBSD_ENV)
174     upwd = getpwnam(user);
175 #else
176     upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
177 #endif
178     if (ignore_uid && upwd != NULL && upwd->pw_uid <= ignore_uid_id) {
179         pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
180         RET(PAM_AUTH_ERR);
181     }
182 #endif
183
184     if (flags & PAM_DELETE_CRED) {
185         if (logmask && LOG_MASK(LOG_DEBUG))
186             pam_afs_syslog(LOG_DEBUG, PAMAFS_DELCRED, user);
187
188         RET(PAM_SUCCESS);
189     } else if (flags & PAM_REINITIALIZE_CRED) {
190
191         if (logmask && LOG_MASK(LOG_DEBUG))
192             pam_afs_syslog(LOG_DEBUG, PAMAFS_REINITCRED, user);
193         RET(PAM_SUCCESS);
194
195     } else {                    /* flags are PAM_REFRESH_CRED, PAM_ESTABLISH_CRED, unknown */
196
197         if (logmask && LOG_MASK(LOG_DEBUG))
198             pam_afs_syslog(LOG_DEBUG, PAMAFS_ESTABCRED, user);
199
200         errcode = pam_get_data(pamh, pam_afs_lh, (const void **)&password);
201         if (errcode != PAM_SUCCESS || password == NULL) {
202             if (use_first_pass) {
203                 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
204                 RET(PAM_AUTH_ERR);
205             }
206             password = NULL;    /* In case it isn't already NULL */
207             if (logmask && LOG_MASK(LOG_DEBUG))
208                 pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
209         } else if (password[0] == '\0') {
210             /* Actually we *did* get one but it was empty. */
211             got_authtok = 1;
212             torch_password = 0;
213             /* So don't use it. */
214             password = NULL;
215             if (use_first_pass) {
216                 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
217                 RET(PAM_NEW_AUTHTOK_REQD);
218             }
219             if (logmask && LOG_MASK(LOG_DEBUG))
220                 pam_afs_syslog(LOG_DEBUG, PAMAFS_NILPASSWORD, user);
221         } else {
222             if (logmask && LOG_MASK(LOG_DEBUG))
223                 pam_afs_syslog(LOG_DEBUG, PAMAFS_GOTPASS, user);
224             torch_password = 0;
225             got_authtok = 1;
226         }
227         if (!(use_first_pass || try_first_pass)) {
228             password = NULL;
229         }
230
231       try_auth:
232         if (password == NULL) {
233
234             torch_password = 1;
235
236             if (use_first_pass)
237                 RET(PAM_AUTH_ERR);      /* shouldn't happen */
238             if (try_first_pass)
239                 try_first_pass = 0;     /* we come back if try_first_pass==1 below */
240
241             if (pam_convp == NULL || pam_convp->conv == NULL) {
242                 pam_afs_syslog(LOG_ERR, PAMAFS_CANNOT_PROMPT);
243                 RET(PAM_AUTH_ERR);
244             }
245
246             errcode =
247                 pam_afs_prompt(pam_convp, &password, 0, PAMAFS_PWD_PROMPT);
248             if (errcode != PAM_SUCCESS || password == NULL) {
249                 pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
250                 RET(PAM_AUTH_ERR);
251             }
252             if (password[0] == '\0') {
253                 if (logmask && LOG_MASK(LOG_DEBUG))
254                     pam_afs_syslog(LOG_DEBUG, PAMAFS_NILPASSWORD);
255                 RET(PAM_NEW_AUTHTOK_REQD);
256             }
257             /*
258              * We aren't going to free the password later (we will wipe it,
259              * though), because the storage for it if we get it from other
260              * paths may belong to someone else.  Since we do need to free
261              * this storage, copy it to a buffer that won't need to be freed
262              * later, and free this storage now.
263              */
264
265             strncpy(my_password_buf, password, sizeof(my_password_buf));
266             my_password_buf[sizeof(my_password_buf) - 1] = '\0';
267             memset(password, 0, strlen(password));
268             free(password);
269             password = my_password_buf;
270         }
271         /*
272          * We only set a PAG here, if we haven't got one before in
273          * pam_sm_authenticate() or if it was destroyed by the application
274          */
275         if ((!refresh_token) && (getPAG() == -1)) {
276             if (logmask && LOG_MASK(LOG_DEBUG))
277                 syslog(LOG_DEBUG, "New PAG created in pam_setcred()");
278             setpag();
279 #ifdef AFS_KERBEROS_ENV
280             ktc_newpag();
281 #endif
282         }
283
284         if (flags & PAM_REFRESH_CRED) {
285             if (use_klog) {
286                 auth_ok = !do_klog(user, password, "00:00:01", cell_ptr);
287                 ktc_ForgetAllTokens();
288             } else {
289                 if (ka_VerifyUserPassword(KA_USERAUTH_VERSION, user,    /* kerberos name */
290                                           NULL, /* instance */
291                                           cell_ptr,     /* realm */
292                                           password,     /* password */
293                                           0,    /* spare 2 */
294                                           &reason       /* error string */
295                     )) {
296                     pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user,
297                                    reason);
298                 } else {
299                     auth_ok = 1;
300                 }
301             }
302         }
303
304         if (flags & PAM_ESTABLISH_CRED) {
305             if (use_klog)
306                 auth_ok = !do_klog(user, password, NULL, cell_ptr);
307             else {
308                 if (ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION, user,       /* kerberos name */
309                                                NULL,    /* instance */
310                                                cell_ptr,        /* realm */
311                                                password,        /* password */
312                                                0,       /* default lifetime */
313                                                &password_expires, 0,    /* spare 2 */
314                                                &reason  /* error string */
315                     )) {
316                     pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user,
317                                    reason);
318                 } else {
319                     auth_ok = 1;
320                 }
321             }
322         }
323
324         if (!auth_ok && try_first_pass) {
325             password = NULL;
326             goto try_auth;
327         }
328
329         /* pam_sm_authenticate should have set this 
330          * if (auth_ok && !got_authtok) {
331          *     torch_password = 0;
332          *     (void) pam_set_item(pamh, PAM_AUTHTOK, password);
333          * }
334          */
335
336         if (auth_ok) {
337             if (set_expires && !use_klog && (password_expires >= 0)) {
338                 strcpy(sbuffer, "PASSWORD_EXPIRES=");
339                 strcat(sbuffer, cv2string(&sbuffer[100], password_expires));
340                 errcode = pam_putenv(pamh, sbuffer);
341                 if (errcode != PAM_SUCCESS)
342                     pam_afs_syslog(LOG_ERR, PAMAFS_PASSEXPFAIL, user);
343             }
344 #if defined(AFS_KERBEROS_ENV)
345             if (upwd) {
346                 if (chown(ktc_tkt_string(), upwd->pw_uid, upwd->pw_gid) < 0)
347                     pam_afs_syslog(LOG_ERR, PAMAFS_CHOWNKRB, user);
348                 sprintf(sbuffer, "KRBTKFILE=%s", ktc_tkt_string());
349                 errcode = pam_putenv(pamh, sbuffer);
350                 if (errcode != PAM_SUCCESS)
351                     pam_afs_syslog(LOG_ERR, PAMAFS_KRBFAIL, user);
352             }
353 #endif
354
355             RET(PAM_SUCCESS);
356         } else {
357             RET(PAM_CRED_ERR);
358         }
359     }
360
361   out:
362     if (password && torch_password)
363         memset(password, 0, strlen(password));
364     (void)setlogmask(origmask);
365 #ifndef AFS_SUN56_ENV
366     closelog();
367 #endif
368     return retcode;
369 }