Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / pam / afs_password.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 #ifdef HAVE_SYS_WAIT_H
16 #include <sys/wait.h>
17 #endif
18
19 #include <security/pam_appl.h>
20 #include <security/pam_modules.h>
21
22 #include <afs/kautils.h>
23 #include "afs_message.h"
24 #include "afs_util.h"
25 #include "afs_pam_msg.h"
26
27 #define RET(x) { retcode = (x); goto out; }
28
29 extern int
30 pam_sm_chauthtok(pam_handle_t * pamh, int flags, int argc, const char **argv)
31 {
32 int retcode = PAM_SUCCESS;
33 int errcode = PAM_SUCCESS;
34 int code;
35 int origmask;
36 int logmask = LOG_UPTO(LOG_INFO);
37 int nowarn = 0;
38 int use_first_pass = 0;
39 int try_first_pass = 0;
40 int ignore_root = 0;
41 char *torch_password = NULL;
42 int i;
43 char my_password_buf[256];
44 char instance[256];
45 char realm[256];
46 char cell[256];
47 char *localcell;
48 PAM_CONST char *user = NULL, *password = NULL;
49 char *new_password = NULL, *verify_password = NULL;
50 char *reason = NULL;
51 struct ktc_encryptionKey oldkey, newkey;
52 struct ktc_token token;
53 struct ubik_client *conn = 0;
54 PAM_CONST struct pam_conv *pam_convp = NULL;
55 struct passwd *upwd = NULL;
56 #if !(defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV) || defined(AFS_NBSD_ENV))
57 char upwd_buf[2048]; /* size is a guess. */
58 struct passwd unix_pwd;
59 #endif
60
61 #ifndef AFS_SUN5_ENV
62 openlog(pam_afs_ident, LOG_CONS, LOG_AUTH);
63 #endif
64 origmask = setlogmask(logmask);
65
66 /*
67 * Parse the user options. Log an error for any unknown options.
68 *
69 * Todo options: PAM_SILENT
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;
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_root = 1;
83 } else {
84 pam_afs_syslog(LOG_ERR, PAMAFS_UNKNOWNOPT, argv[i]);
85 }
86 }
87
88 if (use_first_pass)
89 try_first_pass = 0;
90
91 if (logmask & LOG_MASK(LOG_DEBUG)) {
92 pam_afs_syslog(LOG_DEBUG, PAMAFS_OPTIONS, nowarn, use_first_pass,
93 try_first_pass);
94 pam_afs_syslog(LOG_DEBUG, PAMAFS_PAMERROR, flags);
95 }
96
97 /* Try to get the user-interaction info, if available. */
98 errcode = pam_get_item(pamh, PAM_CONV, (PAM_CONST void **)&pam_convp);
99 if (errcode != PAM_SUCCESS) {
100 pam_afs_syslog(LOG_WARNING, PAMAFS_NO_USER_INT);
101 pam_convp = NULL;
102 }
103
104 /* Who are we trying to authenticate here? */
105 if ((errcode =
106 pam_get_user(pamh, &user,
107 "AFS username: ")) != PAM_SUCCESS) {
108 pam_afs_syslog(LOG_ERR, PAMAFS_NOUSER, errcode);
109 RET(PAM_USER_UNKNOWN);
110 }
111
112 if (logmask & LOG_MASK(LOG_DEBUG))
113 pam_afs_syslog(LOG_DEBUG, PAMAFS_USERNAMEDEBUG, user);
114
115 /*
116 * If the user has a "local" (or via nss, possibly nss_dce) pwent,
117 * and its uid==0, and "ignore_root" was given in pam.conf,
118 * ignore the user.
119 */
120 #if defined(AFS_HPUX_ENV) || defined(AFS_DARWIN100_ENV) || defined(AFS_SUN5_ENV)
121 #if defined(AFS_HPUX110_ENV) || defined(AFS_DARWIN100_ENV) || defined(AFS_SUN5_ENV)
122 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd);
123 #else /* AFS_HPUX110_ENV */
124 i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
125 if (i == 0) /* getpwnam_r success */
126 upwd = &unix_pwd;
127 #endif /* else AFS_HPUX110_ENV */
128 if (ignore_root && i == 0 && upwd && upwd->pw_uid == 0) {
129 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
130 RET(PAM_AUTH_ERR);
131 }
132 #else
133 #if defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DFBSD_ENV) || defined(AFS_NBSD_ENV)
134 upwd = getpwnam(user);
135 #else
136 upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf));
137 #endif
138 if (ignore_root && upwd != NULL && upwd->pw_uid == 0) {
139 pam_afs_syslog(LOG_INFO, PAMAFS_IGNORINGROOT, user);
140 RET(PAM_AUTH_ERR);
141 }
142 #endif
143
144 errcode = pam_get_item(pamh, PAM_AUTHTOK, (PAM_CONST void **)&password);
145 if (errcode != PAM_SUCCESS || password == NULL) {
146 if (use_first_pass) {
147 pam_afs_syslog(LOG_ERR, PAMAFS_PASSWD_REQ, user);
148 RET(PAM_AUTH_ERR);
149 }
150 password = NULL; /* In case it isn't already NULL */
151 if (logmask & LOG_MASK(LOG_DEBUG))
152 pam_afs_syslog(LOG_DEBUG, PAMAFS_NOFIRSTPASS, user);
153 } else if (password[0] == '\0') {
154 /* Actually we *did* get one but it was empty. */
155 pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
156 RET(PAM_NEW_AUTHTOK_REQD);
157 } else {
158 if (logmask & LOG_MASK(LOG_DEBUG))
159 pam_afs_syslog(LOG_DEBUG, PAMAFS_GOTPASS, user);
160 }
161 if (!(use_first_pass || try_first_pass)) {
162 password = NULL;
163 }
164
165 if (password == NULL) {
166 char *prompt_password;
167 if (use_first_pass)
168 RET(PAM_AUTH_ERR); /* shouldn't happen */
169 if (try_first_pass)
170 try_first_pass = 0;
171 if (pam_convp == NULL || pam_convp->conv == NULL) {
172 pam_afs_syslog(LOG_ERR, PAMAFS_CANNOT_PROMPT);
173 RET(PAM_AUTH_ERR);
174 }
175
176 errcode = pam_afs_prompt(pam_convp, &prompt_password, 0, PAMAFS_PWD_PROMPT);
177 if (errcode != PAM_SUCCESS || prompt_password == NULL) {
178 pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
179 RET(PAM_AUTH_ERR);
180 }
181 if (prompt_password[0] == '\0') {
182 pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
183 RET(PAM_NEW_AUTHTOK_REQD);
184 }
185
186 /*
187 * We aren't going to free the password later (we will wipe it,
188 * though), because the storage for it if we get it from other
189 * paths may belong to someone else. Since we do need to free
190 * this storage, copy it to a buffer that won't need to be freed
191 * later, and free this storage now.
192 */
193 strncpy(my_password_buf, prompt_password, sizeof(my_password_buf));
194 my_password_buf[sizeof(my_password_buf) - 1] = '\0';
195 memset(prompt_password, 0, strlen(prompt_password));
196 free(prompt_password);
197 password = torch_password = my_password_buf;
198 }
199
200 if ((code = ka_VerifyUserPassword(KA_USERAUTH_VERSION + KA_USERAUTH_DOSETPAG, (char *)user, /* kerberos name */
201 NULL, /* instance */
202 NULL, /* realm */
203 (char *)password, /* password */
204 0, /* spare 2 */
205 &reason /* error string */ )) != 0) {
206 pam_afs_syslog(LOG_ERR, PAMAFS_LOGIN_FAILED, user, reason);
207 RET(PAM_AUTH_ERR);
208 }
209 torch_password = NULL;
210 pam_set_item(pamh, PAM_AUTHTOK, password);
211 pam_set_item(pamh, PAM_OLDAUTHTOK, password);
212 if (flags & PAM_PRELIM_CHECK) {
213 /* only auth check was requested, so return success here */
214 return (PAM_SUCCESS);
215 }
216 if (!(flags & PAM_UPDATE_AUTHTOK)) {
217 /* these lines are never executed ... */
218 /* UPDATE_AUTHTOK flag is required, return with error */
219 pam_afs_syslog(LOG_ERR, PAMAFS_FLAGS, "PAM_UPDATE_AUTHTOK");
220 RET(PAM_AUTH_ERR);
221 }
222
223 /* get the new passwd and verify it */
224 errcode =
225 pam_afs_prompt(pam_convp, &new_password, 0, PAMAFS_NEW_PWD_PROMPT);
226 if (errcode != PAM_SUCCESS || new_password == NULL) {
227 pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
228 RET(PAM_AUTH_ERR);
229 }
230 if (new_password[0] == '\0') {
231 pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
232 RET(PAM_AUTH_ERR);
233 }
234 errcode =
235 pam_afs_prompt(pam_convp, &verify_password, 0,
236 PAMAFS_VERIFY_PWD_PROMPT);
237 if (errcode != PAM_SUCCESS || verify_password == NULL) {
238 pam_afs_syslog(LOG_ERR, PAMAFS_GETPASS_FAILED);
239 memset(new_password, 0, strlen(new_password));
240 RET(PAM_AUTH_ERR);
241 }
242 if (verify_password[0] == '\0') {
243 pam_afs_syslog(LOG_INFO, PAMAFS_NILPASSWORD, user);
244 memset(new_password, 0, strlen(new_password));
245 RET(PAM_AUTH_ERR);
246 }
247 if (strcmp(new_password, verify_password) != 0) {
248 pam_afs_syslog(LOG_INFO, PAMAFS_NE_PASSWORD);
249 memset(new_password, 0, strlen(new_password));
250 memset(verify_password, 0, strlen(verify_password));
251 RET(PAM_AUTH_ERR);
252 }
253 memset(verify_password, 0, strlen(verify_password));
254 /* checking password length and quality is up to other PAM modules */
255
256 /* set the new password */
257 if ((code = ka_Init(0)) != 0) {
258 pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
259 RET(PAM_AUTH_ERR);
260 }
261 if ((code = rx_Init(0)) != 0) {
262 pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
263 RET(PAM_AUTH_ERR);
264 }
265 strcpy(instance, "");
266 if ((localcell = ka_LocalCell()) == NULL) {
267 pam_afs_syslog(LOG_ERR, PAMAFS_NOCELLNAME);
268 RET(PAM_AUTH_ERR);
269 }
270 strcpy(realm, localcell);
271 strcpy(cell, realm);
272 /* oldkey is not used in ka_ChangePassword (only for ka_auth) */
273 ka_StringToKey((char *)password, realm, &oldkey);
274 ka_StringToKey(new_password, realm, &newkey);
275 if ((code =
276 ka_GetAdminToken((char *)user, instance, realm, &oldkey, 20, &token,
277 0)) != 0) {
278 pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
279 RET(PAM_AUTH_ERR);
280 }
281 if ((code =
282 ka_AuthServerConn(realm, KA_MAINTENANCE_SERVICE, &token,
283 &conn)) != 0) {
284 pam_afs_syslog(LOG_ERR, PAMAFS_KAERROR, code);
285 RET(PAM_AUTH_ERR);
286 }
287 if ((code = ka_ChangePassword((char *)user, /* kerberos name */
288 instance, /* instance */
289 conn, /* conn */
290 0, /* old password unused */
291 &newkey /* new password */ )) != 0) {
292 pam_afs_syslog(LOG_ERR, PAMAFS_KAPASS_FAIL);
293 memset(new_password, 0, strlen(new_password));
294 RET(PAM_AUTH_ERR);
295 } else {
296 pam_set_item(pamh, PAM_AUTHTOK, new_password);
297 RET(PAM_SUCCESS);
298 }
299
300 out:
301 if (password && torch_password) {
302 memset(torch_password, 0, strlen(torch_password));
303 }
304 (void)setlogmask(origmask);
305 #ifndef AFS_SUN5_ENV
306 closelog();
307 #endif
308 return retcode;
309 }