afsconfig-and-rcsid-all-around-20010705
[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 <afs/param.h>
18 #include <afsconfig.h>
19
20 RCSID("$Header$");
21
22 #include <sys/param.h>
23 #include <afs/kautils.h>
24 #include "afs_message.h"
25 #include "afs_util.h"
26
27
28
29 #define RET(x) { retcode = (x); goto out; }
30
31 #if defined(AFS_KERBEROS_ENV)
32 extern char *ktc_tkt_string();
33 #endif
34
35 extern int
36 pam_sm_setcred(
37         pam_handle_t    *pamh,
38         int             flags,
39         int             argc,
40         const char      **argv)
41 {
42     int retcode = PAM_SUCCESS;
43     int errcode = PAM_SUCCESS;
44     int origmask;
45     int logmask = LOG_UPTO(LOG_INFO);
46     int nowarn = 0;
47     int use_first_pass = 1; /* use the password passed in by auth */
48     int try_first_pass = 0;
49     int got_authtok = 0;
50     int ignore_root = 0;
51     int trust_root = 0;
52     int set_expires = 0; /* the default is to not to set the env variable */
53     int i;
54     struct pam_conv *pam_convp = NULL;
55     char my_password_buf[256];
56     char sbuffer[100];
57     char *password = NULL;
58     int torch_password = 1;
59     int auth_ok = 0;
60     char*       lh;
61     char *user = NULL;
62     long password_expires= -1;
63     char*       reason = NULL;
64     struct passwd unix_pwd, *upwd = NULL;
65     char upwd_buf[2048];        /* size is a guess. */
66
67 #ifndef AFS_SUN56_ENV
68     openlog(pam_afs_ident, LOG_CONS, LOG_AUTH);
69 #endif
70     origmask = setlogmask(logmask);
71
72     /*
73      * Parse the user options.  Log an error for any unknown options.
74      */
75     for (i = 0; i < argc; i++) {
76         if (       strcasecmp(argv[i], "debug"         ) == 0) {
77             logmask |= LOG_MASK(LOG_DEBUG);
78             (void) setlogmask(logmask);
79         } else if (strcasecmp(argv[i], "nowarn"        ) == 0) {
80             nowarn = 1;
81         } else if (strcasecmp(argv[i], "use_first_pass") == 0) {
82             use_first_pass = 1; /* practically redundant */
83         } else if (strcasecmp(argv[i], "try_first_pass") == 0) {
84             try_first_pass = 1;
85         } else if (strcasecmp(argv[i], "ignore_root"   ) == 0) {
86             ignore_root = 1;
87         } else if (strcasecmp(argv[i], "trust_root"   ) == 0) {
88             trust_root = 1;
89         } else if (strcasecmp(argv[i], "catch_su"   ) == 0) {
90             use_first_pass = 0;
91         } else if (strcasecmp(argv[i], "setenv_password_expires")==0) {
92             set_expires = 1;
93         } else {
94             pam_afs_syslog(LOG_ERR, PAMAFS_UNKNOWNOPT, argv[i]);
95         }
96     }
97
98     if (use_first_pass) try_first_pass = 0;
99
100     pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass, try_first_pass);
101     /* Try to get the user-interaction info, if available. */
102     errcode = pam_get_item(pamh, PAM_CONV, (void **) &pam_convp);
103     if (errcode != PAM_SUCCESS) {
104         pam_afs_syslog(LOG_DEBUG, PAMAFS_NO_USER_INT);
105         pam_convp = NULL;
106     }
107
108     /* Who are we trying to authenticate here? */
109     if ((errcode = pam_get_user(pamh, &user, "AFS username:")) != PAM_SUCCESS) {
110         pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
111         RET(PAM_USER_UNKNOWN);
112     }
113     /*
114      * If the user has a "local" (or via nss, possibly nss_dce) pwent,
115      * and its uid==0, and "ignore_root" was given in pam.conf,
116      * ignore the user.
117      */
118 #if     defined(AFS_HPUX_ENV)
119 #if     defined(AFS_HPUX110_ENV)
120     i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
121 #else   /* AFS_HPUX110_ENV */
122     i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
123     if ( i == 0 )                       /* getpwnam_r success */
124         upwd = &unix_pwd;
125 #endif  /* AFS_HPUX110_ENV */
126     if (ignore_root && i == 0 && upwd->pw_uid == 0) {
127         pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
128         RET(PAM_AUTH_ERR);
129     }
130 #else
131 #ifdef AFS_LINUX20_ENV
132     upwd = getpwnam(user);
133 #else
134     upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
135 #endif
136     if (upwd != NULL && upwd->pw_uid == 0) {
137         if (ignore_root) { 
138                 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
139                 RET(PAM_AUTH_ERR);
140         } else if (trust_root) {
141                 pam_afs_syslog(LOG_INFO, PAMAFS_TRUSTROOT, user);
142                 RET(PAM_SUCCESS);
143         }
144     }
145 #endif
146
147     if (flags & PAM_DELETE_CRED) {
148         pam_afs_syslog(LOG_DEBUG, PAMAFS_DELCRED, user);
149
150         RET(PAM_SUCCESS);
151     } else if (flags & PAM_REINITIALIZE_CRED) {
152
153         pam_afs_syslog(LOG_DEBUG, PAMAFS_REINITCRED, user);
154         RET(PAM_SUCCESS);
155
156     } else { /* flags are PAM_REFRESH_CRED, PAM_ESTABLISH_CRED, unknown */
157
158         pam_afs_syslog(LOG_DEBUG, PAMAFS_ESTABCRED, user);
159
160         errcode = pam_get_data(pamh, pam_afs_lh, (const void **) &password);
161         if (errcode != PAM_SUCCESS || password == NULL) {
162             if (use_first_pass) {
163                 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
164                 RET(PAM_AUTH_ERR);
165             }
166             password = NULL;    /* In case it isn't already NULL */
167             pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
168         } else if (password[0] == '\0') {
169             /* Actually we *did* get one but it was empty. */
170             got_authtok = 1;
171             torch_password = 0;
172             /* So don't use it. */
173             password = NULL;
174             if (use_first_pass) {
175                 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
176                 RET(PAM_NEW_AUTHTOK_REQD);
177             }
178             pam_afs_syslog(LOG_DEBUG, PAMAFS_NILPASSWORD, user);
179         } else {
180             pam_afs_syslog(LOG_DEBUG, PAMAFS_GOTPASS, user);
181             torch_password = 0;
182             got_authtok = 1;
183         }
184         if (!(use_first_pass || try_first_pass)) {
185             password = NULL;
186         }
187
188     try_auth:
189         if (password == NULL) {
190
191             torch_password = 1;
192
193             if (use_first_pass)
194                 RET(PAM_AUTH_ERR);      /* shouldn't happen */
195             if (try_first_pass)
196                 try_first_pass = 0;     /* we come back if try_first_pass==1 below */
197             
198             if (pam_convp == NULL || pam_convp->conv == NULL) {
199                 pam_afs_syslog(LOG_ERR, PAMAFS_CANNOT_PROMPT);
200                 RET(PAM_AUTH_ERR);
201             }
202             
203             errcode = pam_afs_prompt(pam_convp, &password, 0, PAMAFS_PWD_PROMPT);
204             if (errcode != PAM_SUCCESS || password == NULL) {
205                 pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
206                 RET(PAM_AUTH_ERR);
207             }
208             if (password[0] == '\0') {
209                 pam_afs_syslog(LOG_DEBUG, PAMAFS_NILPASSWORD);
210                 RET(PAM_NEW_AUTHTOK_REQD);
211             }
212             /*
213              * We aren't going to free the password later (we will wipe it,
214              * though), because the storage for it if we get it from other
215              * paths may belong to someone else.  Since we do need to free
216              * this storage, copy it to a buffer that won't need to be freed
217              * later, and free this storage now.
218              */
219             strncpy(my_password_buf, password, sizeof(my_password_buf));
220             my_password_buf[sizeof(my_password_buf)-1] = '\0';
221             memset(password, 0, strlen(password));
222             free(password);
223             password = my_password_buf;
224         }
225
226         if ( flags & PAM_REFRESH_CRED ) {
227             if ( ka_VerifyUserPassword(
228                            KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG,
229                            user, /* kerberos name */
230                            (char *)0, /* instance */
231                            (char *)0, /* realm */
232                             password, /* password */
233                             0, /* spare 2 */
234                             &reason /* error string */
235                             )) {
236                 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
237             } else {
238                 auth_ok = 1;
239             }   
240         }
241             
242         if (  flags & PAM_ESTABLISH_CRED ) {
243             if ( ka_UserAuthenticateGeneral(
244                            KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG,
245                            user, /* kerberos name */
246                            (char *)0, /* instance */
247                            (char *)0, /* realm */
248                             password, /* password */
249                             0, /* default lifetime */
250                             &password_expires,
251                             0, /* spare 2 */
252                             &reason /* error string */
253                             )) {
254                 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
255             } else {
256                auth_ok = 1;
257             }
258         }
259
260         if (!auth_ok && try_first_pass) {
261             password = NULL;
262             goto try_auth;
263         }
264
265         if (auth_ok && !got_authtok) {
266             torch_password = 0;
267             (void) pam_set_item(pamh, PAM_AUTHTOK, password);
268         }
269
270         if (auth_ok) {
271             if (set_expires &&  (password_expires >= 0) ) {
272                 strcpy(sbuffer, "PASSWORD_EXPIRES=");
273                 strcat(sbuffer, cv2string(&sbuffer[100], password_expires));
274                 errcode = pam_putenv( pamh, sbuffer); 
275                 if ( errcode != PAM_SUCCESS )
276                     pam_afs_syslog(LOG_ERR, PAMAFS_PASSEXPFAIL, user);
277             }
278 #if defined(AFS_KERBEROS_ENV)
279             if (upwd)
280             {
281                 if ( chown(ktc_tkt_string(), upwd->pw_uid, upwd->pw_gid) < 0 )
282                     pam_afs_syslog(LOG_ERR, PAMAFS_CHOWNKRB, user);
283                 sprintf(sbuffer, "KRBTKFILE=%s", ktc_tkt_string());
284                 errcode = pam_putenv( pamh, sbuffer);
285                 if ( errcode != PAM_SUCCESS )
286                     pam_afs_syslog(LOG_ERR, PAMAFS_KRBFAIL, user);
287             }
288 #endif
289
290             RET(PAM_SUCCESS);
291         } else {
292             RET(PAM_CRED_ERR);
293         }
294     }
295
296  out:
297     if (password && torch_password) memset(password, 0, strlen(password));
298     (void) setlogmask(origmask);
299 #ifndef AFS_SUN56_ENV
300     closelog();
301 #endif
302     return retcode;
303 }