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