xref: /illumos-gate/usr/src/lib/pam_modules/unix_account/unix_acct.c (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <security/pam_appl.h>
33 #include <security/pam_modules.h>
34 #include <security/pam_impl.h>
35 #include <syslog.h>
36 #include <pwd.h>
37 #include <shadow.h>
38 #include <lastlog.h>
39 #include <ctype.h>
40 #include <unistd.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <libintl.h>
44 #include <signal.h>
45 #include <thread.h>
46 #include <synch.h>
47 #include <errno.h>
48 #include <time.h>
49 #include <string.h>
50 #include <crypt.h>
51 #include <assert.h>
52 #include <deflt.h>
53 #include <libintl.h>
54 #include <passwdutil.h>
55 
56 #define	LASTLOG		"/var/adm/lastlog"
57 #define	LOGINADMIN	"/etc/default/login"
58 #define	UNIX_AUTH_DATA		"SUNW-UNIX-AUTH-DATA"
59 #define	UNIX_AUTHTOK_DATA	"SUNW-UNIX-AUTHTOK-DATA"
60 
61 /*
62  * Function Declarations
63  */
64 extern int		defopen(char *);
65 extern char		*defread(char *);
66 extern void		setusershell();
67 extern int		_nfssys(int, void *);
68 
69 typedef struct _unix_authtok_data_ {
70 	int age_status;
71 }unix_authtok_data;
72 
73 /*ARGSUSED*/
74 static void
75 unix_cleanup(
76 	pam_handle_t *pamh,
77 	void *data,
78 	int pam_status)
79 {
80 	free((unix_authtok_data *)data);
81 }
82 
83 /*
84  * check_for_login_inactivity	- Check for login inactivity
85  *
86  */
87 
88 static int
89 check_for_login_inactivity(
90 	uid_t		pw_uid,
91 	struct 	spwd 	*shpwd)
92 {
93 	int		fdl;
94 	struct lastlog	ll;
95 	int		retval;
96 	offset_t	offset;
97 
98 	offset = (offset_t)pw_uid * (offset_t)sizeof (struct lastlog);
99 
100 	if ((fdl = open(LASTLOG, O_RDWR|O_CREAT, 0444)) >= 0) {
101 		/*
102 		 * Read the last login (ll) time
103 		 */
104 		if (llseek(fdl, offset, SEEK_SET) != offset) {
105 			syslog(LOG_ERR, "pam_unix_acct: pam_sm_acct_mgmt: "
106 			    "can't obtain last login info on uid %d "
107 			    "(uid too large)", pw_uid);
108 			return (0);
109 		}
110 
111 		retval = read(fdl, (char *)&ll, sizeof (ll));
112 
113 		/* Check for login inactivity */
114 
115 		if ((shpwd->sp_inact > 0) && (retval == sizeof (ll)) &&
116 		    ll.ll_time) {
117 			/*
118 			 * account inactive too long.
119 			 * and no update password set
120 			 * and no last pwd change date in shadow file
121 			 * and last pwd change more than inactive time
122 			 * then account inactive too long and no access.
123 			 */
124 			if (((time_t)((ll.ll_time / DAY) + shpwd->sp_inact)
125 				< DAY_NOW) &&
126 			    (shpwd->sp_lstchg != 0) &&
127 			    (shpwd->sp_lstchg != -1) &&
128 			    ((shpwd->sp_lstchg + shpwd->sp_inact) < DAY_NOW)) {
129 				/*
130 				 * Account inactive for too long
131 				 */
132 				(void) close(fdl);
133 				return (1);
134 			}
135 		}
136 
137 		(void) close(fdl);
138 	}
139 	return (0);
140 }
141 
142 /*
143  * new_password_check()
144  *
145  * check to see if the user needs to change their password
146  */
147 
148 static int
149 new_password_check(pw_uid, shpwd, flags)
150 	uid_t		pw_uid;
151 	struct 	spwd 	*shpwd;
152 	int 		flags;
153 {
154 	time_t	now  = DAY_NOW;
155 
156 	/*
157 	 * We want to make sure that we change the password only if
158 	 * passwords are required for the system, the user does not
159 	 * have a password, AND the user's NULL password can be changed
160 	 * according to its password aging information
161 	 */
162 
163 	if ((flags & PAM_DISALLOW_NULL_AUTHTOK) != 0) {
164 		if (shpwd->sp_pwdp[0] == '\0') {
165 			if ((pw_uid != 0) &&
166 				((shpwd->sp_max == -1) ||
167 				((time_t)shpwd->sp_lstchg > now) ||
168 				((now >= (time_t)(shpwd->sp_lstchg +
169 							shpwd->sp_min)) &&
170 				(shpwd->sp_max >= shpwd->sp_min)))) {
171 					return (PAM_NEW_AUTHTOK_REQD);
172 			}
173 		}
174 	}
175 	return (PAM_SUCCESS);
176 }
177 
178 /*
179  * perform_passwd_aging_check
180  *		- Check for password exipration.
181  */
182 static	int
183 perform_passwd_aging_check(
184 	pam_handle_t *pamh,
185 	struct 	spwd 	*shpwd,
186 	int	flags)
187 {
188 	time_t 	now = DAY_NOW;
189 	int	idledays = -1;
190 	char	*ptr;
191 	char	messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
192 
193 
194 	if (defopen(LOGINADMIN) == 0) {
195 		if ((ptr = defread("IDLEWEEKS=")) != NULL)
196 			idledays = 7 * atoi(ptr);
197 		(void) defopen(NULL);
198 	}
199 
200 	/*
201 	 * if (sp_lstchg == 0), the administrator has forced the
202 	 * user to change his/her passwd
203 	 */
204 	if (shpwd->sp_lstchg == 0)
205 		return (PAM_NEW_AUTHTOK_REQD);
206 
207 	/* If password aging is disabled (or min>max), all is well */
208 	if (shpwd->sp_max < 0 || shpwd->sp_max < shpwd->sp_min)
209 		return (PAM_SUCCESS);
210 
211 	/* Password aging is enabled. See if the password has aged */
212 	if (now < (time_t)(shpwd->sp_lstchg + shpwd->sp_max))
213 		return (PAM_SUCCESS);
214 
215 	/* Password has aged. Has it aged more than idledays ? */
216 	if (idledays < 0)			/* IDLEWEEKS not configured */
217 		return (PAM_NEW_AUTHTOK_REQD);
218 
219 	/* idledays is configured */
220 	if (idledays > 0 && (now < (time_t)(shpwd->sp_lstchg + idledays)))
221 		return (PAM_NEW_AUTHTOK_REQD);
222 
223 	/* password has aged more that allowed for by IDLEWEEKS */
224 	if (!(flags & PAM_SILENT)) {
225 		(void) strlcpy(messages[0], dgettext(TEXT_DOMAIN,
226 		    "Your password has been expired for too long."),
227 		    sizeof (messages[0]));
228 		(void) strlcpy(messages[1], dgettext(TEXT_DOMAIN,
229 		    "Please contact the system administrator."),
230 		    sizeof (messages[0]));
231 		(void) __pam_display_msg(pamh, PAM_ERROR_MSG, 2, messages,
232 		    NULL);
233 	}
234 	return (PAM_AUTHTOK_EXPIRED);
235 }
236 
237 /*
238  * warn_user_passwd_will_expire	- warn the user when the password will
239  *					  expire.
240  */
241 
242 static void
243 warn_user_passwd_will_expire(
244 	pam_handle_t *pamh,
245 	struct 	spwd shpwd)
246 {
247 	time_t 	now	= DAY_NOW;
248 	char	messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
249 	time_t	days;
250 
251 
252 	if ((shpwd.sp_warn > 0) && (shpwd.sp_max > 0) &&
253 	    (now + shpwd.sp_warn) >= (time_t)(shpwd.sp_lstchg + shpwd.sp_max)) {
254 		days = (time_t)(shpwd.sp_lstchg + shpwd.sp_max) - now;
255 		if (days <= 0)
256 			(void) snprintf(messages[0],
257 				sizeof (messages[0]),
258 				dgettext(TEXT_DOMAIN,
259 				"Your password will expire within 24 hours."));
260 		else if (days == 1)
261 			(void) snprintf(messages[0],
262 				sizeof (messages[0]),
263 				dgettext(TEXT_DOMAIN,
264 				"Your password will expire in 1 day."));
265 		else
266 			(void) snprintf(messages[0],
267 				sizeof (messages[0]),
268 				dgettext(TEXT_DOMAIN,
269 				"Your password will expire in %d days."),
270 				(int)days);
271 
272 		(void) __pam_display_msg(pamh, PAM_TEXT_INFO, 1, messages,
273 		    NULL);
274 	}
275 }
276 
277 /*
278  * pam_sm_acct_mgmt	- 	main account managment routine.
279  *			  Returns: module error or specific error on failure
280  */
281 
282 int
283 pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv)
284 {
285 	uid_t			pw_uid;
286 	char			*repository_name = NULL;
287 	char    		*user;
288 	attrlist		attr_pw[3];
289 	attrlist		attr_spw[7];
290 	pwu_repository_t	*pwu_rep = PWU_DEFAULT_REP;
291 	pwu_repository_t	*auth_rep = NULL;
292 	int 			error = PAM_ACCT_EXPIRED;
293 	int			result;
294 	int			i;
295 	int			debug = 0;
296 	int			server_policy = 0;
297 	unix_authtok_data	*status;
298 	struct 	spwd		shpwd = {NULL, NULL,
299 					-1, -1, -1, -1, -1, -1, 0};
300 
301 	for (i = 0; i < argc; i++) {
302 		if (strcasecmp(argv[i], "debug") == 0)
303 			debug = 1;
304 		else if (strcasecmp(argv[i], "server_policy") == 0)
305 			server_policy = 1;
306 		else if (strcasecmp(argv[i], "nowarn") == 0) {
307 			flags = flags | PAM_SILENT;
308 		} else {
309 			syslog(LOG_ERR,
310 				"ACCOUNT:pam_sm_acct_mgmt: illegal option %s",
311 				argv[i]);
312 		}
313 	}
314 
315 	if (debug)
316 		syslog(LOG_AUTH | LOG_DEBUG,
317 		    "pam_unix_account: entering pam_sm_acct_mgmt()");
318 
319 	if ((error = pam_get_item(pamh, PAM_USER, (void **)&user))
320 							!= PAM_SUCCESS)
321 		goto out;
322 
323 	if (user == NULL) {
324 		error = PAM_USER_UNKNOWN;
325 		goto out;
326 	} else
327 		shpwd.sp_namp = user;
328 
329 	if ((error = pam_get_item(pamh, PAM_REPOSITORY, (void **)&auth_rep))
330 							!= PAM_SUCCESS)
331 		goto out;
332 
333 	if (auth_rep == NULL) {
334 		pwu_rep = PWU_DEFAULT_REP;
335 	} else {
336 		if ((pwu_rep = calloc(1, sizeof (*pwu_rep))) == NULL) {
337 			error = PAM_BUF_ERR;
338 			goto out;
339 		}
340 		pwu_rep->type = auth_rep->type;
341 		pwu_rep->scope = auth_rep->scope;
342 		pwu_rep->scope_len = auth_rep->scope_len;
343 	}
344 
345 	/*
346 	 * First get the password information
347 	 */
348 	attr_pw[0].type =  ATTR_REP_NAME;	attr_pw[0].next = &attr_pw[1];
349 	attr_pw[1].type =  ATTR_UID;		attr_pw[1].next = &attr_pw[2];
350 	attr_pw[2].type =  ATTR_PASSWD;		attr_pw[2].next = NULL;
351 	result = __get_authtoken_attr(user, pwu_rep, attr_pw);
352 
353 	if (result == PWU_NOT_FOUND) {
354 		error = PAM_USER_UNKNOWN;
355 		goto out;
356 	} else if (result == PWU_DENIED) {
357 		error = PAM_PERM_DENIED;
358 		goto out;
359 	} else if (result == PWU_NOMEM) {
360 		error = PAM_BUF_ERR;
361 		goto out;
362 	} else if (result != PWU_SUCCESS) {
363 		error = PAM_SERVICE_ERR;
364 		goto out;
365 	} else {
366 		repository_name = attr_pw[0].data.val_s;
367 		pw_uid = attr_pw[1].data.val_i;
368 		shpwd.sp_pwdp = attr_pw[2].data.val_s;
369 	}
370 
371 	/*
372 	 * if repository is not files|nis|nisplus, and
373 	 * user wants server_policy, we don't care
374 	 * about aging and hence return PAM_IGNORE
375 	 */
376 	if (server_policy &&
377 	    strcmp(repository_name, "files") != 0 &&
378 	    strcmp(repository_name, "nis") != 0 &&
379 	    strcmp(repository_name, "nisplus") != 0) {
380 		error = PAM_IGNORE;
381 		goto out;
382 	}
383 
384 	/*
385 	 * Now get the aging information
386 	 */
387 	attr_spw[0].type =  ATTR_LSTCHG;	attr_spw[0].next = &attr_spw[1];
388 	attr_spw[1].type =  ATTR_MIN;		attr_spw[1].next = &attr_spw[2];
389 	attr_spw[2].type =  ATTR_MAX;		attr_spw[2].next = &attr_spw[3];
390 	attr_spw[3].type =  ATTR_WARN;		attr_spw[3].next = &attr_spw[4];
391 	attr_spw[4].type =  ATTR_INACT;		attr_spw[4].next = &attr_spw[5];
392 	attr_spw[5].type =  ATTR_EXPIRE;	attr_spw[5].next = &attr_spw[6];
393 	attr_spw[6].type =  ATTR_FLAG;		attr_spw[6].next = NULL;
394 
395 	result = __get_authtoken_attr(user, pwu_rep, attr_spw);
396 	if (result == PWU_SUCCESS) {
397 		shpwd.sp_lstchg = attr_spw[0].data.val_i;
398 		shpwd.sp_min = attr_spw[1].data.val_i;
399 		shpwd.sp_max = attr_spw[2].data.val_i;
400 		shpwd.sp_warn = attr_spw[3].data.val_i;
401 		shpwd.sp_inact = attr_spw[4].data.val_i;
402 		shpwd.sp_expire = attr_spw[5].data.val_i;
403 		shpwd.sp_flag = attr_spw[6].data.val_i;
404 	}
405 
406 	if (debug) {
407 		char *pw = "Unix PW";
408 
409 		if (shpwd.sp_pwdp == NULL)
410 			pw = "NULL";
411 		else if (strncmp(shpwd.sp_pwdp, LOCKSTRING,
412 		    sizeof (LOCKSTRING) - 1) == 0)
413 			pw = LOCKSTRING;
414 		else if (strcmp(shpwd.sp_pwdp, NOPWDRTR) == 0)
415 			pw = NOPWDRTR;
416 
417 		if (result ==  PWU_DENIED) {
418 			syslog(LOG_AUTH | LOG_DEBUG,
419 				"pam_unix_account: %s: permission denied "
420 				"to access password aging information. "
421 				"Using defaults.", user);
422 		}
423 
424 		syslog(LOG_AUTH | LOG_DEBUG,
425 		    "%s Policy:Unix, pw=%s, lstchg=%d, min=%d, max=%d, "
426 		    "warn=%d, inact=%d, expire=%d",
427 		    user, pw, shpwd.sp_lstchg, shpwd.sp_min, shpwd.sp_max,
428 		    shpwd.sp_warn, shpwd.sp_inact, shpwd.sp_expire);
429 	}
430 
431 	if (pwu_rep != PWU_DEFAULT_REP) {
432 		free(pwu_rep);
433 		pwu_rep = PWU_DEFAULT_REP;
434 	}
435 
436 	if (result == PWU_NOT_FOUND) {
437 		error = PAM_USER_UNKNOWN;
438 		goto out;
439 	} else if (result == PWU_NOMEM) {
440 		error = PAM_BUF_ERR;
441 		goto out;
442 	} else if (result != PWU_SUCCESS && result != PWU_DENIED) {
443 		error = PAM_SERVICE_ERR;
444 		goto out;
445 	}
446 
447 	/*
448 	 * Check for locked account
449 	 */
450 	if (shpwd.sp_pwdp != NULL &&
451 	    strncmp(shpwd.sp_pwdp, LOCKSTRING, sizeof (LOCKSTRING) - 1) == 0) {
452 		char *service;
453 		char *rhost = NULL;
454 
455 		(void) pam_get_item(pamh, PAM_SERVICE, (void **)&service);
456 		(void) pam_get_item(pamh, PAM_RHOST, (void **)&rhost);
457 		__pam_log(LOG_AUTH | LOG_NOTICE,
458 		    "pam_unix_account: %s attempting to validate locked "
459 		    "account %s from %s",
460 		    service, user,
461 		    (rhost != NULL && *rhost != '\0') ? rhost : "local host");
462 		error = PAM_PERM_DENIED;
463 		goto out;
464 	}
465 
466 	/*
467 	 * Check for account expiration
468 	 */
469 	if (shpwd.sp_expire > 0 &&
470 	    (time_t)shpwd.sp_expire < DAY_NOW) {
471 		error = PAM_ACCT_EXPIRED;
472 		goto out;
473 	}
474 
475 	/*
476 	 * Check for excessive login account inactivity
477 	 */
478 	if (check_for_login_inactivity(pw_uid, &shpwd)) {
479 		error = PAM_PERM_DENIED;
480 		goto out;
481 	}
482 
483 	/*
484 	 * Check to see if the user needs to change their password
485 	 */
486 	if (error = new_password_check(pw_uid, &shpwd, flags)) {
487 		goto out;
488 	}
489 
490 	/*
491 	 * Check to make sure password aging information is okay
492 	 */
493 	if ((error = perform_passwd_aging_check(pamh, &shpwd, flags))
494 							!= PAM_SUCCESS) {
495 		goto out;
496 	}
497 
498 	/*
499 	 * Finally, warn the user if their password is about to expire.
500 	 */
501 	if (!(flags & PAM_SILENT)) {
502 		warn_user_passwd_will_expire(pamh, shpwd);
503 	}
504 
505 	/*
506 	 * All done, return Success
507 	 */
508 	error = PAM_SUCCESS;
509 
510 out:
511 
512 	{
513 		int pam_res;
514 		unix_authtok_data *authtok_data;
515 
516 		if (debug) {
517 			syslog(LOG_AUTH | LOG_DEBUG,
518 				"pam_unix_account: %s: %s",
519 				(user == NULL)?"NULL":user,
520 				pam_strerror(pamh, error));
521 		}
522 
523 		if (repository_name)
524 			free(repository_name);
525 		if (pwu_rep != PWU_DEFAULT_REP)
526 			free(pwu_rep);
527 		if (shpwd.sp_pwdp) {
528 			(void) memset(shpwd.sp_pwdp, 0, strlen(shpwd.sp_pwdp));
529 			free(shpwd.sp_pwdp);
530 		}
531 
532 		/* store the password aging status in the pam handle */
533 		pam_res = pam_get_data(
534 			pamh, UNIX_AUTHTOK_DATA, (const void **)&authtok_data);
535 
536 		if ((status = (unix_authtok_data *)calloc
537 			(1, sizeof (unix_authtok_data))) == NULL) {
538 			return (PAM_BUF_ERR);
539 		}
540 
541 		if (pam_res == PAM_SUCCESS)
542 			(void) memcpy(status, authtok_data,
543 				sizeof (unix_authtok_data));
544 
545 		status->age_status = error;
546 		if (pam_set_data(pamh, UNIX_AUTHTOK_DATA, status, unix_cleanup)
547 							!= PAM_SUCCESS) {
548 			free(status);
549 			return (PAM_SERVICE_ERR);
550 		}
551 	}
552 
553 	return (error);
554 }
555