pam_unix.c revision 1.8 1 /* $NetBSD: pam_unix.c,v 1.8 2005/04/19 03:15:37 christos Exp $ */
2
3 /*-
4 * Copyright 1998 Juniper Networks, Inc.
5 * All rights reserved.
6 * Copyright (c) 2002-2003 Networks Associates Technology, Inc.
7 * All rights reserved.
8 *
9 * Portions of this software was developed for the FreeBSD Project by
10 * ThinkSec AS and NAI Labs, the Security Research Division of Network
11 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
12 * ("CBOSS"), as part of the DARPA CHATS research program.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. The name of the author may not be used to endorse or promote
23 * products derived from this software without specific prior written
24 * permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifdef __FreeBSD__
41 __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_unix/pam_unix.c,v 1.49 2004/02/10 10:13:21 des Exp $");
42 #else
43 __RCSID("$NetBSD: pam_unix.c,v 1.8 2005/04/19 03:15:37 christos Exp $");
44 #endif
45
46
47 #include <sys/types.h>
48
49 #include <ctype.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <pwd.h>
53 #include <grp.h>
54 #include <limits.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <stdio.h>
58 #include <login_cap.h>
59 #include <time.h>
60 #include <tzfile.h>
61 #include <unistd.h>
62
63 #include <util.h>
64
65 #ifdef YP
66 #include <rpc/rpc.h>
67 #include <rpcsvc/ypclnt.h>
68 #include <rpcsvc/yppasswd.h>
69 #endif
70
71 #define PAM_SM_AUTH
72 #define PAM_SM_ACCOUNT
73 #define PAM_SM_PASSWORD
74
75 #include <security/pam_appl.h>
76 #include <security/pam_modules.h>
77 #include <security/pam_mod_misc.h>
78
79 /*
80 * authentication management
81 */
82 PAM_EXTERN int
83 /*ARGSUSED*/
84 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
85 int argc __unused, const char *argv[] __unused)
86 {
87 login_cap_t *lc;
88 struct passwd *pwd, pwres;
89 int retval;
90 const char *pass, *user, *realpw;
91 char pwbuf[1024];
92
93 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) {
94 (void) getpwnam_r(getlogin(), &pwres, pwbuf, sizeof(pwbuf),
95 &pwd);
96 } else {
97 retval = pam_get_user(pamh, &user, NULL);
98 if (retval != PAM_SUCCESS)
99 return (retval);
100 PAM_LOG("Got user: %s", user);
101 (void) getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd);
102 }
103
104 if (pwd != NULL) {
105 PAM_LOG("Doing real authentication");
106 realpw = pwd->pw_passwd;
107 if (realpw[0] == '\0') {
108 if (!(flags & PAM_DISALLOW_NULL_AUTHTOK) &&
109 openpam_get_option(pamh, PAM_OPT_NULLOK))
110 return (PAM_SUCCESS);
111 realpw = "*";
112 }
113 lc = login_getpwclass(pwd);
114 } else {
115 PAM_LOG("Doing dummy authentication");
116 realpw = "*";
117 lc = login_getclass(NULL);
118 }
119 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, NULL);
120 login_close(lc);
121 if (retval != PAM_SUCCESS)
122 return (retval);
123 PAM_LOG("Got password");
124 if (strcmp(crypt(pass, realpw), realpw) == 0)
125 return (PAM_SUCCESS);
126
127 PAM_VERBOSE_ERROR("UNIX authentication refused");
128 return (PAM_AUTH_ERR);
129 }
130
131 PAM_EXTERN int
132 /*ARGSUSED*/
133 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
134 int argc __unused, const char *argv[] __unused)
135 {
136
137 return (PAM_SUCCESS);
138 }
139
140 /*
141 * account management
142 */
143 PAM_EXTERN int
144 /*ARGSUSED*/
145 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
146 int argc __unused, const char *argv[] __unused)
147 {
148 struct passwd *pwd, pwres;
149 struct timeval now;
150 login_cap_t *lc;
151 time_t warntime;
152 int retval;
153 const char *user;
154 char pwbuf[1024];
155
156 retval = pam_get_user(pamh, &user, NULL);
157 if (retval != PAM_SUCCESS)
158 return (retval);
159
160 if (user == NULL ||
161 getpwnam_r(user, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
162 pwd == NULL)
163 return (PAM_SERVICE_ERR);
164
165 PAM_LOG("Got user: %s", user);
166
167 if (*pwd->pw_passwd == '\0' &&
168 (flags & PAM_DISALLOW_NULL_AUTHTOK) != 0)
169 return (PAM_NEW_AUTHTOK_REQD);
170
171 lc = login_getpwclass(pwd);
172 if (lc == NULL) {
173 PAM_LOG("Unable to get login class for user %s", user);
174 return (PAM_SERVICE_ERR);
175 }
176
177 PAM_LOG("Got login_cap");
178
179 if (pwd->pw_change || pwd->pw_expire)
180 (void) gettimeofday(&now, NULL);
181
182 warntime = (time_t)login_getcaptime(lc, "password-warn",
183 (quad_t)(_PASSWORD_WARNDAYS * SECSPERDAY),
184 (quad_t)(_PASSWORD_WARNDAYS * SECSPERDAY));
185
186 /*
187 * Check pw_expire before pw_change - no point in letting the
188 * user change the password on an expired account.
189 */
190
191 if (pwd->pw_expire) {
192 if (now.tv_sec >= pwd->pw_expire) {
193 login_close(lc);
194 return (PAM_ACCT_EXPIRED);
195 } else if (pwd->pw_expire - now.tv_sec < warntime &&
196 (flags & PAM_SILENT) == 0) {
197 pam_error(pamh, "Warning: your account expires on %s",
198 ctime(&pwd->pw_expire));
199 }
200 }
201
202 if (pwd->pw_change) {
203 /* XXX How to handle _PASSWORD_CHGNOW? --thorpej */
204 if (now.tv_sec >= pwd->pw_change) {
205 login_close(lc);
206 return (PAM_NEW_AUTHTOK_REQD);
207 } else if (pwd->pw_change - now.tv_sec < warntime &&
208 (flags & PAM_SILENT) == 0) {
209 pam_error(pamh, "Warning: your password expires on %s",
210 ctime(&pwd->pw_change));
211 }
212 }
213
214 login_close(lc);
215
216 return (PAM_SUCCESS);
217 }
218
219 #ifdef YP
220 /*
221 * yp_check_user:
222 *
223 * Helper function; check that a user exists in the NIS
224 * password map.
225 */
226 static int
227 yp_check_user(const char *domain, const char *user)
228 {
229 char *val;
230 int reason, vallen;
231
232 val = NULL;
233 reason = yp_match(domain, "passwd.byname", user, (int)strlen(user),
234 &val, &vallen);
235 if (reason != 0) {
236 if (val != NULL)
237 free(val);
238 return (0);
239 }
240 free(val);
241 return (1);
242 }
243
244 static int
245 /*ARGSUSED*/
246 yp_set_password(pam_handle_t *pamh, struct passwd *opwd,
247 struct passwd *pwd, const char *old_pass, const char *domain)
248 {
249 char *master;
250 int r, rpcport, status;
251 struct yppasswd yppwd;
252 CLIENT *client;
253 uid_t uid;
254 int retval = PAM_SERVICE_ERR;
255 struct timeval tv;
256
257 /*
258 * Find the master for the passwd map; it should be running
259 * rpc.yppasswdd.
260 */
261 if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
262 pam_error(pamh, "Can't find master NIS server. Reason: %s",
263 yperr_string(r));
264 return (PAM_SERVICE_ERR);
265 }
266
267 /*
268 * Ask the portmapper for the port of rpc.yppasswdd.
269 */
270 if ((rpcport = getrpcport(master, YPPASSWDPROG,
271 YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
272 pam_error(pamh,
273 "Master NIS server not runing yppasswd daemon.\n\t"
274 "Can't change NIS password.");
275 return (PAM_SERVICE_ERR);
276 }
277
278 /*
279 * Be sure the port is privileged.
280 */
281 if (rpcport >= IPPORT_RESERVED) {
282 pam_error(pamh, "yppasswd daemon is on an invalid port.");
283 return (PAM_SERVICE_ERR);
284 }
285
286 uid = getuid();
287 if (uid != 0 && uid != pwd->pw_uid) {
288 pam_error(pamh, "You may only change your own password: %s",
289 strerror(EACCES));
290 return (PAM_SERVICE_ERR);
291 }
292
293 /*
294 * Fill in the yppasswd structure for yppasswdd.
295 */
296 memset(&yppwd, 0, sizeof(yppwd));
297 yppwd.oldpass = strdup(old_pass);
298 if ((yppwd.newpw.pw_passwd = strdup(pwd->pw_passwd)) == NULL)
299 goto malloc_failure;
300 if ((yppwd.newpw.pw_name = strdup(pwd->pw_name)) == NULL)
301 goto malloc_failure;
302 yppwd.newpw.pw_uid = pwd->pw_uid;
303 yppwd.newpw.pw_gid = pwd->pw_gid;
304 if ((yppwd.newpw.pw_gecos = strdup(pwd->pw_gecos)) == NULL)
305 goto malloc_failure;
306 if ((yppwd.newpw.pw_dir = strdup(pwd->pw_dir)) == NULL)
307 goto malloc_failure;
308 if ((yppwd.newpw.pw_shell = strdup(pwd->pw_shell)) == NULL)
309 goto malloc_failure;
310
311 client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
312 if (client == NULL) {
313 pam_error(pamh, "Can't contact yppasswdd on %s: Reason: %s",
314 master, yperr_string(YPERR_YPBIND));
315 goto out;
316 }
317
318 client->cl_auth = authunix_create_default();
319 tv.tv_sec = 2;
320 tv.tv_usec = 0;
321 r = clnt_call(client, YPPASSWDPROC_UPDATE,
322 xdr_yppasswd, &yppwd, xdr_int, &status, tv);
323 if (r)
324 pam_error(pamh, "RPC to yppasswdd failed.");
325 else if (status)
326 pam_error(pamh, "Couldn't change NIS password.");
327 else {
328 pam_info(pamh, "The NIS password has been changed on %s, "
329 "the master NIS passwd server.", master);
330 retval = PAM_SUCCESS;
331 }
332
333 out:
334 if (yppwd.oldpass != NULL)
335 free(yppwd.oldpass);
336 if (yppwd.newpw.pw_passwd != NULL)
337 free(yppwd.newpw.pw_passwd);
338 if (yppwd.newpw.pw_name != NULL)
339 free(yppwd.newpw.pw_name);
340 if (yppwd.newpw.pw_gecos != NULL)
341 free(yppwd.newpw.pw_gecos);
342 if (yppwd.newpw.pw_dir != NULL)
343 free(yppwd.newpw.pw_dir);
344 if (yppwd.newpw.pw_shell != NULL)
345 free(yppwd.newpw.pw_shell);
346 return (retval);
347
348 malloc_failure:
349 pam_error(pamh, "memory allocation failure");
350 goto out;
351 }
352 #endif /* YP */
353
354 static int
355 local_set_password(pam_handle_t *pamh, struct passwd *opwd,
356 struct passwd *pwd)
357 {
358 char errbuf[200];
359 int tfd, pfd;
360
361 pw_init();
362 tfd = pw_lock(0);
363 if (tfd < 0) {
364 pam_error(pamh, "The password file is busy, waiting...");
365 tfd = pw_lock(10);
366 if (tfd < 0) {
367 pam_error(pamh, "The password file is still busy, "
368 "try again later.");
369 return (PAM_SERVICE_ERR);
370 }
371 }
372
373 pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
374 if (pfd < 0) {
375 pam_error(pamh, "%s: %s", _PATH_MASTERPASSWD, strerror(errno));
376 pw_abort();
377 return (PAM_SERVICE_ERR);
378 }
379
380 if (pw_copyx(pfd, tfd, pwd, opwd, errbuf, sizeof(errbuf)) == 0) {
381 pam_error(pamh, "Unable to update password entry: %s",
382 errbuf);
383 pw_abort();
384 return (PAM_SERVICE_ERR);
385 }
386
387 if (pw_mkdb(pwd->pw_name, opwd->pw_change == pwd->pw_change) < 0) {
388 pam_error(pamh, "Unable to rebuild local password database.");
389 pw_abort();
390 return (PAM_SERVICE_ERR);
391 }
392
393 return (PAM_SUCCESS);
394 }
395
396 /*
397 * password management
398 *
399 * standard Unix and NIS password changing
400 */
401 PAM_EXTERN int
402 /*ARGSUSED*/
403 pam_sm_chauthtok(pam_handle_t *pamh, int flags,
404 int argc __unused, const char *argv[] __unused)
405 {
406 struct passwd *pwd, old_pwd;
407 login_cap_t *lc;
408 const char *user, *passwd_db, *new_pass, *old_pass, *p;
409 int retval, tries, min_pw_len = 0, pw_expiry = 0;
410 char salt[_PASSWORD_LEN+1];
411 char old_pwbuf[1024];
412 #ifdef YP
413 char *domain;
414 int r;
415 #endif
416
417 if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF))
418 (void) getpwnam_r(getlogin(), &old_pwd, old_pwbuf,
419 sizeof(old_pwbuf), &pwd);
420 else {
421 retval = pam_get_user(pamh, &user, NULL);
422 if (retval != PAM_SUCCESS)
423 return (retval);
424 (void) getpwnam_r(user, &old_pwd, old_pwbuf,
425 sizeof(old_pwbuf), &pwd);
426 }
427
428 if (pwd == NULL)
429 return (PAM_AUTHTOK_RECOVERY_ERR);
430
431 PAM_LOG("Got user: %s", user);
432
433 /*
434 * Determine which password type we're going to change, and
435 * remember it.
436 *
437 * NOTE: domain does not need to be freed; its storage is
438 * allocated statically in libc.
439 */
440 passwd_db = openpam_get_option(pamh, "passwd_db");
441 if (passwd_db == NULL) {
442 #ifdef YP
443 /* Prefer YP, if configured. */
444 if (_yp_check(NULL)) {
445 /* If _yp_check() succeeded, then this must. */
446 if ((r = yp_get_default_domain(&domain)) != 0) {
447 pam_error(pamh,
448 "Unable to get NIS domain, reason: %s",
449 yperr_string(r));
450 return (PAM_SERVICE_ERR);
451 }
452 if (yp_check_user(domain, user))
453 passwd_db = "nis";
454 }
455 #endif
456 /* Otherwise we always use local files. */
457 if (passwd_db == NULL) {
458 /* XXX Any validation to do here? */
459 passwd_db = "files";
460 }
461
462 if (passwd_db == NULL) {
463 pam_error(pamh, "Unable to determine Unix password DB");
464 return (PAM_SERVICE_ERR);
465 } else if ((retval = openpam_set_option(pamh, "passwd_db",
466 passwd_db)) != PAM_SUCCESS) {
467 return (retval);
468 }
469 } else {
470 /* Check to see if the specified password DB is usable. */
471 #ifdef YP
472 if (strcmp(passwd_db, "nis") == 0) {
473 if (_yp_check(NULL) == 0) {
474 pam_error(pamh, "NIS not in use.");
475 return (PAM_SERVICE_ERR);
476 }
477 if ((r = yp_get_default_domain(&domain)) != 0) {
478 pam_error(pamh,
479 "Unable to get NIS domain, reason: %s",
480 yperr_string(r));
481 return (PAM_SERVICE_ERR);
482 }
483 if (yp_check_user(domain, user) == 0) {
484 pam_error(pamh,
485 "User %s does not exist in NIS.", user);
486 return (PAM_USER_UNKNOWN);
487 }
488 goto known_passwd_db;
489 }
490 #endif
491 if (strcmp(passwd_db, "files") == 0) {
492 /* XXX Any validation to do here? */
493 goto known_passwd_db;
494 }
495 pam_error(pamh, "Unknown Unix password DB: %s", passwd_db);
496 return (PAM_SERVICE_ERR);
497 }
498 known_passwd_db:
499
500 if (flags & PAM_PRELIM_CHECK) {
501 PAM_LOG("PRELIM round");
502
503 if (strcmp(passwd_db, "files") == 0) {
504 if (getuid() == 0) {
505 /* Root doesn't need the old password. */
506 return (pam_set_item(pamh, PAM_OLDAUTHTOK, ""));
507 }
508 }
509
510 if (pwd->pw_passwd[0] == '\0') {
511 /*
512 * No password case.
513 * XXX Are we giviing too much away by not prompting
514 * XXX for a password?
515 * XXX Check PAM_DISALLOW_NULL_AUTHTOK
516 */
517 return (pam_set_item(pamh, PAM_OLDAUTHTOK, ""));
518 } else {
519 retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK,
520 &old_pass, NULL);
521 if (retval != PAM_SUCCESS)
522 return (retval);
523 if (strcmp(crypt(old_pass, pwd->pw_passwd),
524 pwd->pw_passwd) != 0)
525 return (PAM_PERM_DENIED);
526 return (PAM_SUCCESS);
527 }
528 }
529
530 if (flags & PAM_UPDATE_AUTHTOK) {
531 char option[LINE_MAX], *key, *opt;
532
533 PAM_LOG("UPDATE round");
534
535 if ((lc = login_getclass(pwd->pw_class)) != NULL) {
536 min_pw_len = (int) login_getcapnum(lc,
537 "minpasswordlen", (quad_t)0, (quad_t)0);
538 pw_expiry = (int) login_getcapnum(lc,
539 "passwordtime", (quad_t)0, (quad_t)0);
540 login_close(lc);
541 }
542
543 retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &old_pass, NULL);
544 if (retval != PAM_SUCCESS)
545 return (retval);
546
547 /* Get the new password. */
548 for (tries = 0;;) {
549 pam_set_item(pamh, PAM_AUTHTOK, NULL);
550 retval = pam_get_authtok(pamh, PAM_AUTHTOK, &new_pass,
551 NULL);
552 if (retval == PAM_TRY_AGAIN) {
553 pam_error(pamh,
554 "Mismatch; try again, EOF to quit.");
555 continue;
556 }
557 if (retval != PAM_SUCCESS) {
558 PAM_VERBOSE_ERROR("Unable to get new password");
559 return (retval);
560 }
561 /* Successfully got new password. */
562 if (new_pass[0] == '\0') {
563 pam_info(pamh, "Password unchanged.");
564 return (PAM_SUCCESS);
565 }
566 if (min_pw_len > 0 && strlen(new_pass) < min_pw_len) {
567 pam_error(pamh, "Password is too short.");
568 continue;
569 }
570 if (strlen(new_pass) <= 5 && ++tries < 2) {
571 pam_error(pamh,
572 "Please enter a longer password.");
573 continue;
574 }
575 for (p = new_pass; *p && islower((unsigned char)*p); ++p);
576 if (!*p && ++tries < 2) {
577 pam_error(pamh,
578 "Please don't use an all-lower case "
579 "password.\nUnusual capitalization, "
580 "control characters or digits are "
581 "suggested.");
582 continue;
583 }
584 /* Password is OK. */
585 break;
586 }
587 pw_getpwconf(option, sizeof(option), pwd,
588 #ifdef YP
589 strcmp(passwd_db, "nis") == 0 ? "ypcipher" :
590 #endif
591 "localcipher");
592 opt = option;
593 key = strsep(&opt, ",");
594
595 if (pw_gensalt(salt, _PASSWORD_LEN, key, opt) == -1) {
596 pam_error(pamh, "Couldn't generate salt.");
597 return (PAM_SERVICE_ERR);
598 }
599
600 pwd->pw_passwd = crypt(new_pass, salt);
601 pwd->pw_change = pw_expiry ? pw_expiry + time(NULL) : 0;
602
603 retval = PAM_SERVICE_ERR;
604 if (strcmp(passwd_db, "files") == 0)
605 retval = local_set_password(pamh, &old_pwd, pwd);
606 #ifdef YP
607 if (strcmp(passwd_db, "nis") == 0)
608 retval = yp_set_password(pamh, &old_pwd, pwd, old_pass,
609 domain);
610 #endif
611 return (retval);
612 }
613
614 PAM_LOG("Illegal flags argument");
615 return (PAM_ABORT);
616 }
617
618 PAM_MODULE_ENTRY("pam_unix");
619