pull-prototypes-to-head-20020821
[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 <security/pam_appl.h>
11 #include <security/pam_modules.h>
12 #include <syslog.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <pwd.h>
16 #include <unistd.h>
17 #include <errno.h>
18 #include <afsconfig.h>
19 #include <afs/param.h>
20
21 RCSID("$Header$");
22
23 #include <sys/param.h>
24 #include <afs/kautils.h>
25 #include "afs_message.h"
26 #include "afs_util.h"
27
28
29
30 #define RET(x) { retcode = (x); goto out; }
31
32 #if defined(AFS_KERBEROS_ENV)
33 extern char *ktc_tkt_string();
34 #endif
35
36 extern int
37 pam_sm_setcred(
38         pam_handle_t    *pamh,
39         int             flags,
40         int             argc,
41         const char      **argv)
42 {
43     int retcode = PAM_SUCCESS;
44     int errcode = PAM_SUCCESS;
45     int origmask;
46     int logmask = LOG_UPTO(LOG_INFO);
47     int nowarn = 0;
48     int use_first_pass = 1; /* use the password passed in by auth */
49     int try_first_pass = 0;
50     int got_authtok = 0;
51     int ignore_uid  = 0;
52     int no_unlog = 0;
53     uid_t ignore_uid_id = 0;
54     int refresh_token = 0;
55     int set_expires = 0; /* the default is to not to set the env variable */
56     int use_klog = 0;
57     int i;
58     struct pam_conv *pam_convp = NULL;
59     char my_password_buf[256];
60     char *cell_ptr=NULL;
61     char sbuffer[100];
62     char *password = NULL;
63     int torch_password = 1;
64     int auth_ok = 0;
65     char*       lh;
66     char *user = NULL;
67     long password_expires= -1;
68     char*       reason = NULL;
69     struct passwd unix_pwd, *upwd = NULL;
70     char upwd_buf[2048];        /* size is a guess. */
71
72 #ifndef AFS_SUN56_ENV
73     openlog(pam_afs_ident, LOG_CONS, 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; /* practically redundant */
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, "ignore_uid missing argument");
97                 ignore_uid = 0;
98             } else {
99                 ignore_uid = 1;
100                 ignore_uid_id = (uid_t) strtol(argv[i], (char**)NULL, 10);
101                 if ( (0 > ignore_uid_id) || (ignore_uid_id > IGNORE_MAX) ) {
102                         ignore_uid = 0;
103                         pam_afs_syslog(LOG_ERR, PAMAFS_IGNOREUID, argv[i]);
104                 }
105             }
106         } else if (strcasecmp(argv[i], "cell") == 0) {
107             i++;
108             if (i == argc) {
109                 pam_afs_syslog(LOG_ERR, PAMAFS_OTHERCELL, "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) try_first_pass = 0;
132
133     if (logmask && LOG_MASK(LOG_DEBUG))
134       pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass, try_first_pass, ignore_uid, ignore_uid_id, 8, 8, 8, 8);
135     /* Try to get the user-interaction info, if available. */
136     errcode = pam_get_item(pamh, PAM_CONV, (const void **) &pam_convp);
137     if (errcode != PAM_SUCCESS) {
138       if (logmask && LOG_MASK(LOG_DEBUG))
139         pam_afs_syslog(LOG_DEBUG, PAMAFS_NO_USER_INT);
140         pam_convp = NULL;
141     }
142
143     /* Who are we trying to authenticate here? */
144     if ((errcode = pam_get_user(pamh, (const char **)&user, "AFS username:")) != PAM_SUCCESS) {
145         pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
146         RET(PAM_USER_UNKNOWN);
147     }
148     /*
149      * If the user has a "local" (or via nss, possibly nss_dce) pwent,
150      * and its uid==0, and "ignore_root" was given in pam.conf,
151      * ignore the user.
152      */
153     /* enhanced: use "ignore_uid <number>" to specify the largest uid
154      * which should be ignored by this module
155      */
156 #if     defined(AFS_HPUX_ENV)
157 #if     defined(AFS_HPUX110_ENV)
158     i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
159 #else   /* AFS_HPUX110_ENV */
160     i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
161     if ( i == 0 )                       /* getpwnam_r success */
162         upwd = &unix_pwd;
163 #endif  /* AFS_HPUX110_ENV */
164     if (ignore_uid && i == 0 && upwd->pw_uid <= ignore_uid_id) {
165         pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
166         RET(PAM_AUTH_ERR);
167     }
168 #else
169 #if     defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV)
170     upwd = getpwnam(user);
171 #else
172     upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
173 #endif
174     if (ignore_uid && upwd != NULL && upwd->pw_uid <= ignore_uid_id) {
175                 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
176                 RET(PAM_AUTH_ERR);
177     }
178 #endif
179
180     if (flags & PAM_DELETE_CRED) {
181       if (logmask && LOG_MASK(LOG_DEBUG))
182         pam_afs_syslog(LOG_DEBUG, PAMAFS_DELCRED, user);
183
184         RET(PAM_SUCCESS);
185     } else if (flags & PAM_REINITIALIZE_CRED) {
186
187       if (logmask && LOG_MASK(LOG_DEBUG))
188         pam_afs_syslog(LOG_DEBUG, PAMAFS_REINITCRED, user);
189         RET(PAM_SUCCESS);
190
191     } else { /* flags are PAM_REFRESH_CRED, PAM_ESTABLISH_CRED, unknown */
192
193       if (logmask && LOG_MASK(LOG_DEBUG))
194         pam_afs_syslog(LOG_DEBUG, PAMAFS_ESTABCRED, user);
195
196         errcode = pam_get_data(pamh, pam_afs_lh, (const void **) &password);
197         if (errcode != PAM_SUCCESS || password == NULL) {
198             if (use_first_pass) {
199                 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
200                 RET(PAM_AUTH_ERR);
201             }
202             password = NULL;    /* In case it isn't already NULL */
203             if (logmask && LOG_MASK(LOG_DEBUG))
204               pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
205         } else if (password[0] == '\0') {
206             /* Actually we *did* get one but it was empty. */
207             got_authtok = 1;
208             torch_password = 0;
209             /* So don't use it. */
210             password = NULL;
211             if (use_first_pass) {
212                 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
213                 RET(PAM_NEW_AUTHTOK_REQD);
214             }
215             if (logmask && LOG_MASK(LOG_DEBUG))
216               pam_afs_syslog(LOG_DEBUG, PAMAFS_NILPASSWORD, user);
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               if (logmask && LOG_MASK(LOG_DEBUG))
249                 pam_afs_syslog(LOG_DEBUG, PAMAFS_NILPASSWORD);
250                 RET(PAM_NEW_AUTHTOK_REQD);
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          * We only set a PAG here, if we haven't got one before in
268          * pam_sm_authenticate() or if it was destroyed by the application
269          */
270         if ((!refresh_token) && (getPAG() == -1)) {
271           if (logmask && LOG_MASK(LOG_DEBUG))
272             syslog(LOG_DEBUG, "New PAG created in pam_setcred()");
273            setpag();
274 #ifdef AFS_KERBEROS_ENV
275            ktc_newpag();
276 #endif
277         }
278
279         if ( flags & PAM_REFRESH_CRED ) {
280             if (use_klog) {
281                auth_ok = ! do_klog(user, password, "00:00:01", cell_ptr);
282                ktc_ForgetAllTokens();
283             } else {
284             if ( ka_VerifyUserPassword(
285                            KA_USERAUTH_VERSION,
286                            user, /* kerberos name */
287                            NULL, /* instance */
288                            cell_ptr, /* realm */
289                             password, /* password */
290                             0, /* spare 2 */
291                             &reason /* error string */
292                             )) {
293                 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
294             } else {
295                 auth_ok = 1;
296             }   
297         }
298         }
299             
300         if (  flags & PAM_ESTABLISH_CRED ) {
301            if (use_klog) auth_ok = ! do_klog(user, password, NULL, cell_ptr);
302            else {
303             if ( ka_UserAuthenticateGeneral(
304                            KA_USERAUTH_VERSION,
305                            user, /* kerberos name */
306                            NULL, /* instance */
307                            cell_ptr, /* realm */
308                             password, /* password */
309                             0, /* default lifetime */
310                             &password_expires,
311                             0, /* spare 2 */
312                             &reason /* error string */
313                             )) {
314                 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
315             } else {
316                auth_ok = 1;
317             }
318         }
319         }
320
321         if (!auth_ok && try_first_pass) {
322             password = NULL;
323             goto try_auth;
324         }
325
326         /* pam_sm_authenticate should have set this 
327          * if (auth_ok && !got_authtok) {
328          *     torch_password = 0;
329          *     (void) pam_set_item(pamh, PAM_AUTHTOK, password);
330          * }
331          */
332
333         if (auth_ok) {
334             if (set_expires && !use_klog &&  (password_expires >= 0) ) {
335                 strcpy(sbuffer, "PASSWORD_EXPIRES=");
336                 strcat(sbuffer, cv2string(&sbuffer[100], password_expires));
337                 errcode = pam_putenv( pamh, sbuffer); 
338                 if ( errcode != PAM_SUCCESS )
339                     pam_afs_syslog(LOG_ERR, PAMAFS_PASSEXPFAIL, user);
340             }
341 #if defined(AFS_KERBEROS_ENV)
342                if (upwd) {
343                 if ( chown(ktc_tkt_string(), upwd->pw_uid, upwd->pw_gid) < 0 )
344                     pam_afs_syslog(LOG_ERR, PAMAFS_CHOWNKRB, user);
345                 sprintf(sbuffer, "KRBTKFILE=%s", ktc_tkt_string());
346                 errcode = pam_putenv( pamh, sbuffer);
347                 if ( errcode != PAM_SUCCESS )
348                     pam_afs_syslog(LOG_ERR, PAMAFS_KRBFAIL, user);
349                }
350 #endif
351
352             RET(PAM_SUCCESS);
353         } else {
354             RET(PAM_CRED_ERR);
355         }
356     }
357
358  out:
359     if (password && torch_password) memset(password, 0, strlen(password));
360     (void) setlogmask(origmask);
361 #ifndef AFS_SUN56_ENV
362     closelog();
363 #endif
364     return retcode;
365 }