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