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