xref: /netbsd-src/lib/libpam/modules/pam_unix/pam_unix.c (revision 81e0d2b0af8485d94ed5da487d4253841a2e6e45)
1 /*	$NetBSD: pam_unix.c,v 1.4 2005/01/12 03:36:12 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.4 2005/01/12 03:36:12 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;
89 	int retval;
90 	const char *pass, *user, *realpw;
91 
92 	if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF)) {
93 		pwd = getpwnam(getlogin());
94 	} else {
95 		retval = pam_get_user(pamh, &user, NULL);
96 		if (retval != PAM_SUCCESS)
97 			return (retval);
98 		PAM_LOG("Got user: %s", user);
99 		pwd = getpwnam(user);
100 	}
101 
102 	if (pwd != NULL) {
103 		PAM_LOG("Doing real authentication");
104 		realpw = pwd->pw_passwd;
105 		if (realpw[0] == '\0') {
106 			if (!(flags & PAM_DISALLOW_NULL_AUTHTOK) &&
107 			    openpam_get_option(pamh, PAM_OPT_NULLOK))
108 				return (PAM_SUCCESS);
109 			realpw = "*";
110 		}
111 		lc = login_getpwclass(pwd);
112 	} else {
113 		PAM_LOG("Doing dummy authentication");
114 		realpw = "*";
115 		lc = login_getclass(NULL);
116 	}
117 	retval = pam_get_authtok(pamh, PAM_AUTHTOK, &pass, NULL);
118 	login_close(lc);
119 	if (retval != PAM_SUCCESS)
120 		return (retval);
121 	PAM_LOG("Got password");
122 	if (strcmp(crypt(pass, realpw), realpw) == 0)
123 		return (PAM_SUCCESS);
124 
125 	PAM_VERBOSE_ERROR("UNIX authentication refused");
126 	return (PAM_AUTH_ERR);
127 }
128 
129 PAM_EXTERN int
130 /*ARGSUSED*/
131 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
132     int argc __unused, const char *argv[] __unused)
133 {
134 
135 	return (PAM_SUCCESS);
136 }
137 
138 /*
139  * account management
140  */
141 PAM_EXTERN int
142 /*ARGSUSED*/
143 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags __unused,
144     int argc __unused, const char *argv[] __unused)
145 {
146 	struct passwd *pwd;
147 	struct timeval now;
148 	login_cap_t *lc;
149 	time_t warntime;
150 	int retval;
151 	const char *user;
152 
153 	retval = pam_get_user(pamh, &user, NULL);
154 	if (retval != PAM_SUCCESS)
155 		return (retval);
156 
157 	if (user == NULL || (pwd = getpwnam(user)) == NULL)
158 		return (PAM_SERVICE_ERR);
159 
160 	PAM_LOG("Got user: %s", user);
161 
162 	if (*pwd->pw_passwd == '\0' &&
163 	    (flags & PAM_DISALLOW_NULL_AUTHTOK) != 0)
164 		return (PAM_NEW_AUTHTOK_REQD);
165 
166 	lc = login_getpwclass(pwd);
167 	if (lc == NULL) {
168 		PAM_LOG("Unable to get login class for user %s", user);
169 		return (PAM_SERVICE_ERR);
170 	}
171 
172 	PAM_LOG("Got login_cap");
173 
174 	if (pwd->pw_change || pwd->pw_expire)
175 		(void) gettimeofday(&now, NULL);
176 
177 	warntime = (time_t)login_getcaptime(lc, "password-warn",
178 	    (quad_t)(_PASSWORD_WARNDAYS * SECSPERDAY),
179 	    (quad_t)(_PASSWORD_WARNDAYS * SECSPERDAY));
180 
181 	/*
182 	 * Check pw_expire before pw_change - no point in letting the
183 	 * user change the password on an expired account.
184 	 */
185 
186 	if (pwd->pw_expire) {
187 		if (now.tv_sec >= pwd->pw_expire) {
188 			login_close(lc);
189 			return (PAM_ACCT_EXPIRED);
190 		} else if (pwd->pw_expire - now.tv_sec < warntime &&
191 		    (flags & PAM_SILENT) == 0) {
192 			pam_error(pamh, "Warning: your account expires on %s",
193 			    ctime(&pwd->pw_expire));
194 		}
195 	}
196 
197 	if (pwd->pw_change) {
198 		/* XXX How to handle _PASSWORD_CHGNOW?  --thorpej */
199 		if (now.tv_sec >= pwd->pw_change) {
200 			login_close(lc);
201 			return (PAM_NEW_AUTHTOK_REQD);
202 		} else if (pwd->pw_change - now.tv_sec < warntime &&
203 		    (flags & PAM_SILENT) == 0) {
204 			pam_error(pamh, "Warning: your password expires on %s",
205 			    ctime(&pwd->pw_change));
206 		}
207 	}
208 
209 	login_close(lc);
210 
211 	return (PAM_SUCCESS);
212 }
213 
214 #ifdef YP
215 /*
216  * yp_check_user:
217  *
218  *	Helper function; check that a user exists in the NIS
219  *	password map.
220  */
221 static int
222 yp_check_user(const char *domain, const char *user)
223 {
224 	char *val;
225 	int reason, vallen;
226 
227 	val = NULL;
228 	reason = yp_match(domain, "passwd.byname", user, (int)strlen(user),
229 	    &val, &vallen);
230 	if (reason != 0) {
231 		if (val != NULL)
232 			free(val);
233 		return (0);
234 	}
235 	free(val);
236 	return (1);
237 }
238 
239 static int
240 /*ARGSUSED*/
241 yp_set_password(pam_handle_t *pamh, struct passwd *opwd,
242     struct passwd *pwd, const char *old_pass, const char *domain)
243 {
244 	char *master;
245 	int r, rpcport, status;
246 	struct yppasswd yppwd;
247 	CLIENT *client;
248 	uid_t uid;
249 	int retval = PAM_SERVICE_ERR;
250 	struct timeval tv;
251 
252 	/*
253 	 * Find the master for the passwd map; it should be running
254 	 * rpc.yppasswdd.
255 	 */
256 	if ((r = yp_master(domain, "passwd.byname", &master)) != 0) {
257 		pam_error(pamh, "Can't find master NIS server.  Reason: %s",
258 		    yperr_string(r));
259 		return (PAM_SERVICE_ERR);
260 	}
261 
262 	/*
263 	 * Ask the portmapper for the port of rpc.yppasswdd.
264 	 */
265 	if ((rpcport = getrpcport(master, YPPASSWDPROG,
266 				  YPPASSWDPROC_UPDATE, IPPROTO_UDP)) == 0) {
267 		pam_error(pamh,
268 		    "Master NIS server not runing yppasswd daemon.\n\t"
269 		    "Can't change NIS password.");
270 		return (PAM_SERVICE_ERR);
271 	}
272 
273 	/*
274 	 * Be sure the port is privileged.
275 	 */
276 	if (rpcport >= IPPORT_RESERVED) {
277 		pam_error(pamh, "yppasswd daemon is on an invalid port.");
278 		return (PAM_SERVICE_ERR);
279 	}
280 
281 	uid = getuid();
282 	if (uid != 0 && uid != pwd->pw_uid) {
283 		pam_error(pamh, "You may only change your own password: %s",
284 		    strerror(EACCES));
285 		return (PAM_SERVICE_ERR);
286 	}
287 
288 	/*
289 	 * Fill in the yppasswd structure for yppasswdd.
290 	 */
291 	memset(&yppwd, 0, sizeof(yppwd));
292 	if ((yppwd.newpw.pw_passwd = strdup(pwd->pw_passwd)) == NULL)
293 		goto malloc_failure;
294 	if ((yppwd.newpw.pw_name = strdup(pwd->pw_name)) == NULL)
295 		goto malloc_failure;
296 	yppwd.newpw.pw_uid = pwd->pw_uid;
297 	yppwd.newpw.pw_gid = pwd->pw_gid;
298 	if ((yppwd.newpw.pw_gecos = strdup(pwd->pw_gecos)) == NULL)
299 		goto malloc_failure;
300 	if ((yppwd.newpw.pw_dir = strdup(pwd->pw_dir)) == NULL)
301 		goto malloc_failure;
302 	if ((yppwd.newpw.pw_shell = strdup(pwd->pw_shell)) == NULL)
303 		goto malloc_failure;
304 
305 	client = clnt_create(master, YPPASSWDPROG, YPPASSWDVERS, "udp");
306 	if (client == NULL) {
307 		pam_error(pamh, "Can't contact yppasswdd on %s: Reason: %s",
308 		    master, yperr_string(YPERR_YPBIND));
309 		goto out;
310 	}
311 
312 	client->cl_auth = authunix_create_default();
313 	tv.tv_sec = 2;
314 	tv.tv_usec = 0;
315 	r = clnt_call(client, YPPASSWDPROC_UPDATE,
316 	    xdr_yppasswd, &yppwd, xdr_int, &status, tv);
317 	if (r)
318 		pam_error(pamh, "RPC to yppasswdd failed.");
319 	else if (status)
320 		pam_error(pamh, "Couldn't change NIS password.");
321 	else {
322 		pam_info(pamh, "The NIS password has been changed on %s, "
323 		    "the master NIS passwd server.", master);
324 		retval = PAM_SUCCESS;
325 	}
326 
327  out:
328 	if (yppwd.newpw.pw_passwd != NULL)
329 		free(yppwd.newpw.pw_passwd);
330 	if (yppwd.newpw.pw_name != NULL)
331 		free(yppwd.newpw.pw_name);
332 	if (yppwd.newpw.pw_gecos != NULL)
333 		free(yppwd.newpw.pw_gecos);
334 	if (yppwd.newpw.pw_dir != NULL)
335 		free(yppwd.newpw.pw_dir);
336 	if (yppwd.newpw.pw_shell != NULL)
337 		free(yppwd.newpw.pw_shell);
338 	return (retval);
339 
340  malloc_failure:
341 	pam_error(pamh, "memory allocation failure");
342 	goto out;
343 }
344 #endif /* YP */
345 
346 static int
347 local_set_password(pam_handle_t *pamh, struct passwd *opwd,
348     struct passwd *pwd)
349 {
350 	char errbuf[200];
351 	int tfd, pfd;
352 
353 	pw_init();
354 	tfd = pw_lock(0);
355 	if (tfd < 0) {
356 		pam_error(pamh, "The password file is busy, waiting...");
357 		tfd = pw_lock(10);
358 		if (tfd < 0) {
359 			pam_error(pamh, "The password file is still busy, "
360 			    "try again later.");
361 			return (PAM_SERVICE_ERR);
362 		}
363 	}
364 
365 	pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
366 	if (pfd < 0) {
367 		pam_error(pamh, "%s: %s", _PATH_MASTERPASSWD, strerror(errno));
368 		pw_abort();
369 		return (PAM_SERVICE_ERR);
370 	}
371 
372 	if (pw_copyx(pfd, tfd, pwd, opwd, errbuf, sizeof(errbuf)) == 0) {
373 		pam_error(pamh, "Unable to update password entry: %s",
374 		    errbuf);
375 		pw_abort();
376 		return (PAM_SERVICE_ERR);
377 	}
378 
379 	if (pw_mkdb(pwd->pw_name, opwd->pw_change == pwd->pw_change) < 0) {
380 		pam_error(pamh, "Unable to rebuild local password database.");
381 		pw_abort();
382 		return (PAM_SERVICE_ERR);
383 	}
384 
385 	return (PAM_SUCCESS);
386 }
387 
388 /*
389  * password management
390  *
391  * standard Unix and NIS password changing
392  */
393 PAM_EXTERN int
394 /*ARGSUSED*/
395 pam_sm_chauthtok(pam_handle_t *pamh, int flags,
396     int argc __unused, const char *argv[] __unused)
397 {
398 	struct passwd *pwd, old_pwd;
399 	login_cap_t *lc;
400 	const char *user, *passwd_db, *new_pass, *old_pass, *p;
401 	int retval, tries, min_pw_len = 0, pw_expiry = 0;
402 	char salt[_PASSWORD_LEN+1];
403 #ifdef YP
404 	char *domain;
405 	int r;
406 #endif
407 
408 	if (openpam_get_option(pamh, PAM_OPT_AUTH_AS_SELF))
409 		pwd = getpwnam(getlogin());
410 	else {
411 		retval = pam_get_user(pamh, &user, NULL);
412 		if (retval != PAM_SUCCESS)
413 			return (retval);
414 		pwd = getpwnam(user);
415 	}
416 
417 	if (pwd == NULL)
418 		return (PAM_AUTHTOK_RECOVERY_ERR);
419 
420 	old_pwd = *pwd;
421 
422 	PAM_LOG("Got user: %s", user);
423 
424 	/*
425 	 * Determine which password type we're going to change, and
426 	 * remember it.
427 	 *
428 	 * NOTE: domain does not need to be freed; its storage is
429 	 * allocated statically in libc.
430 	 */
431 	passwd_db = openpam_get_option(pamh, "passwd_db");
432 	if (passwd_db == NULL) {
433 #ifdef YP
434 		/* Prefer YP, if configured. */
435 		if (_yp_check(NULL)) {
436 			/* If _yp_check() succeeded, then this must. */
437 			if ((r = yp_get_default_domain(&domain)) != 0) {
438 				pam_error(pamh,
439 				    "Unable to get NIS domain, reason: %s",
440 				    yperr_string(r));
441 				return (PAM_SERVICE_ERR);
442 			}
443 			if (yp_check_user(domain, user))
444 				passwd_db = "nis";
445 		}
446 #endif
447 		/* Otherwise we always use local files. */
448 		if (passwd_db == NULL) {
449 			/* XXX Any validation to do here? */
450 			passwd_db = "files";
451 		}
452 
453 		if (passwd_db == NULL) {
454 			pam_error(pamh, "Unable to determine Unix password DB");
455 			return (PAM_SERVICE_ERR);
456 		} else if ((retval = openpam_set_option(pamh, "passwd_db",
457 						passwd_db)) != PAM_SUCCESS) {
458 			return (retval);
459 		}
460 	} else {
461 		/* Check to see if the specified password DB is usable. */
462 #ifdef YP
463 		if (strcmp(passwd_db, "nis") == 0) {
464 			if (_yp_check(NULL) == 0) {
465 				pam_error(pamh, "NIS not in use.");
466 				return (PAM_SERVICE_ERR);
467 			}
468 			if ((r = yp_get_default_domain(&domain)) != 0) {
469 				pam_error(pamh,
470 				    "Unable to get NIS domain, reason: %s",
471 				    yperr_string(r));
472 				return (PAM_SERVICE_ERR);
473 			}
474 			if (yp_check_user(domain, user) == 0) {
475 				pam_error(pamh,
476 				    "User %s does not exist in NIS.", user);
477 				return (PAM_USER_UNKNOWN);
478 			}
479 			goto known_passwd_db;
480 		}
481 #endif
482 		if (strcmp(passwd_db, "files") == 0) {
483 			/* XXX Any validation to do here? */
484 			goto known_passwd_db;
485 		}
486 		pam_error(pamh, "Unknown Unix password DB: %s", passwd_db);
487 		return (PAM_SERVICE_ERR);
488 	}
489  known_passwd_db:
490 
491 	if (flags & PAM_PRELIM_CHECK) {
492 		PAM_LOG("PRELIM round");
493 
494 		if (strcmp(passwd_db, "files") == 0) {
495 			if (getuid() == 0) {
496 				/* Root doesn't need the old password. */
497 				return (pam_set_item(pamh, PAM_OLDAUTHTOK, ""));
498 			}
499 		}
500 
501 		if (pwd->pw_passwd[0] == '\0') {
502 			/*
503 			 * No password case.
504 			 * XXX Are we giviing too much away by not prompting
505 			 * XXX for a password?
506 			 * XXX Check PAM_DISALLOW_NULL_AUTHTOK
507 			 */
508 			return (pam_set_item(pamh, PAM_OLDAUTHTOK, ""));
509 		} else {
510 			retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK,
511 			    &old_pass, NULL);
512 			if (retval != PAM_SUCCESS)
513 				return (retval);
514 			if (strcmp(crypt(old_pass, pwd->pw_passwd),
515 				   pwd->pw_passwd) != 0)
516 				return (PAM_PERM_DENIED);
517 			return (PAM_SUCCESS);
518 		}
519 	}
520 
521 	if (flags & PAM_UPDATE_AUTHTOK) {
522 		char option[LINE_MAX], *key, *opt;
523 
524 		PAM_LOG("UPDATE round");
525 
526 		if ((lc = login_getclass(pwd->pw_class)) != NULL) {
527 			min_pw_len = (int) login_getcapnum(lc,
528 			    "minpasswordlen", (quad_t)0, (quad_t)0);
529 			pw_expiry = (int) login_getcapnum(lc,
530 			    "passwordtime", (quad_t)0, (quad_t)0);
531 			login_close(lc);
532 		}
533 
534 		retval = pam_get_authtok(pamh, PAM_OLDAUTHTOK, &old_pass, NULL);
535 		if (retval != PAM_SUCCESS)
536 			return (retval);
537 
538 		/* Get the new password. */
539 		for (tries = 0;;) {
540 			retval = pam_get_authtok(pamh, PAM_AUTHTOK, &new_pass,
541 			    NULL);
542 			if (retval == PAM_TRY_AGAIN) {
543 				pam_error(pamh,
544 				    "Mismatch; try again, EOF to quit.");
545 				continue;
546 			}
547 			if (retval != PAM_SUCCESS) {
548 				PAM_VERBOSE_ERROR("Unable to get new password");
549 				return (retval);
550 			}
551 			/* Successfully got new password. */
552 			if (new_pass[0] == '\0') {
553 				pam_info(pamh, "Password unchanged.");
554 				return (PAM_SUCCESS);
555 			}
556 			if (min_pw_len > 0 && strlen(new_pass) < min_pw_len) {
557 				pam_error(pamh, "Password is too short.");
558 				continue;
559 			}
560 			if (strlen(new_pass) <= 5 && ++tries < 2) {
561 				pam_error(pamh,
562 				    "Please enter a longer password.");
563 				continue;
564 			}
565 			for (p = new_pass; *p && islower((unsigned char)*p); ++p);
566 			if (!*p && ++tries < 2) {
567 				pam_error(pamh,
568 				    "Please don't use an all-lower case "
569 				    "password.\nUnusual capitalization, "
570 				    "control characters or digits are "
571 				    "suggested.");
572 				continue;
573 			}
574 			/* Password is OK. */
575 			break;
576 		}
577 		pw_getpwconf(option, sizeof(option), pwd,
578 #ifdef YP
579 		    strcmp(passwd_db, "nis") == 0 ? "ypcipher" :
580 #endif
581 		    "localcipher");
582 		opt = option;
583 		key = strsep(&opt, ",");
584 
585 		if (pw_gensalt(salt, _PASSWORD_LEN, key, opt) == -1) {
586 			pam_error(pamh, "Couldn't generate salt.");
587 			return (PAM_SERVICE_ERR);
588 		}
589 
590 		pwd->pw_passwd = crypt(new_pass, salt);
591 		pwd->pw_change = pw_expiry ? pw_expiry + time(NULL) : 0;
592 
593 		retval = PAM_SERVICE_ERR;
594 		if (strcmp(passwd_db, "files") == 0)
595 			retval = local_set_password(pamh, &old_pwd, pwd);
596 #ifdef YP
597 		if (strcmp(passwd_db, "nis") == 0)
598 			retval = yp_set_password(pamh, &old_pwd, pwd, old_pass,
599 			    domain);
600 #endif
601 		return (retval);
602 	}
603 
604 	PAM_LOG("Illegal flags argument");
605 	return (PAM_ABORT);
606 }
607 
608 PAM_MODULE_ENTRY("pam_unix");
609