xref: /titanic_41/usr/src/cmd/passwd/passwd.c (revision e4d060fb4c00d44cd578713eb9a921f594b733b8)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
30 /*	  All Rights Reserved	*/
31 
32 /*
33  * passwd is a program whose sole purpose is to manage
34  * the password file, map, or table. It allows system administrator
35  * to add, change and display password attributes.
36  * Non privileged user can change password or display
37  * password attributes which corresponds to their login name.
38  */
39 
40 #include <stdio.h>
41 #include <pwd.h>
42 #include <sys/types.h>
43 #include <errno.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <locale.h>
47 #include <stdarg.h>
48 #include <errno.h>
49 #include <string.h>
50 #include <security/pam_appl.h>
51 #include <security/pam_modules.h>
52 #include <security/pam_impl.h>
53 #include <rpcsvc/nis.h>
54 #undef GROUP
55 #include <syslog.h>
56 #include <userdefs.h>
57 #include <passwdutil.h>
58 
59 #include <nss_dbdefs.h>
60 
61 #include <deflt.h>
62 
63 #undef	GROUP
64 #include <bsm/adt.h>
65 #include <bsm/adt_event.h>
66 
67 /*
68  * flags indicate password attributes to be modified
69  */
70 
71 #define	LFLAG 0x001		/* lock user's password  */
72 #define	DFLAG 0x002		/* delete user's  password */
73 #define	MFLAG 0x004		/* set max field -- # of days passwd is valid */
74 #define	NFLAG 0x008		/* set min field -- # of days between */
75 				/* password changes */
76 #define	SFLAG 0x010		/* display password attributes */
77 #define	FFLAG 0x020		/* expire  user's password */
78 #define	AFLAG 0x040		/* display password attributes for all users */
79 #define	SAFLAG (SFLAG|AFLAG)	/* display password attributes for all users */
80 #define	WFLAG 0x100		/* warn user to change passwd */
81 #define	OFLAG 0x200		/* domain name */
82 #define	EFLAG 0x400		/* change shell */
83 #define	GFLAG 0x800		/* change gecos information */
84 #define	HFLAG 0x1000		/* change home directory */
85 #define	XFLAG 0x2000		/* no login */
86 #define	UFLAG 0x4000		/* unlock user's password */
87 
88 #define	NONAGEFLAG	(EFLAG | GFLAG | HFLAG)
89 #define	AGEFLAG	(LFLAG | FFLAG | MFLAG | NFLAG | WFLAG | XFLAG | UFLAG)
90 #define	MUTEXFLAG	(DFLAG | LFLAG | XFLAG | UFLAG | SAFLAG)
91 
92 
93 /*
94  * exit code
95  */
96 
97 #define	SUCCESS	0	/* succeeded */
98 #define	NOPERM	1	/* No permission */
99 #define	BADOPT	2	/* Invalid combination of option */
100 #define	FMERR	3	/* File/table manipulation error */
101 #define	FATAL	4	/* Old file/table can not be recovered */
102 #define	FBUSY	5	/* Lock file/table busy */
103 #define	BADSYN	6	/* Incorrect syntax */
104 #define	BADAGE	7	/* Aging is disabled  */
105 #define	NOMEM	8	/* No memory */
106 #define	SYSERR	9	/* System error */
107 #define	EXPIRED	10	/* Account expired */
108 
109 /*
110  * define error messages
111  */
112 #define	MSG_NP		"Permission denied"
113 #define	MSG_BS		"Invalid combination of options"
114 #define	MSG_FE		"Unexpected failure. Password file/table unchanged."
115 #define	MSG_FF		"Unexpected failure. Password file/table missing."
116 #define	MSG_FB		"Password file/table busy. Try again later."
117 #define	MSG_NV  	"Invalid argument to option"
118 #define	MSG_AD		"Password aging is disabled"
119 #define	MSG_RS		"Cannot change from restricted shell %s\n"
120 #define	MSG_NM		"Out of memory."
121 #define	MSG_UNACCEPT	"%s is unacceptable as a new shell\n"
122 #define	MSG_UNAVAIL	"warning: %s is unavailable on this machine\n"
123 #define	MSG_COLON	"':' is not allowed.\n"
124 #define	MSG_MAXLEN	"Maximum number of characters allowed is %d."
125 #define	MSG_CONTROL	"Control characters are not allowed.\n"
126 #define	MSG_SHELL_UNCHANGED	"Login shell unchanged.\n"
127 #define	MSG_GECOS_UNCHANGED	"Finger information unchanged.\n"
128 #define	MSG_DIR_UNCHANGED	"Homedir information unchanged.\n"
129 #define	MSG_NAME	"\nName [%s]: "
130 #define	MSG_HOMEDIR	"\nHome Directory [%s]: "
131 #define	MSG_OLDSHELL	"Old shell: %s\n"
132 #define	MSG_NEWSHELL	"New shell: "
133 #define	MSG_AGAIN	"\nPlease try again\n"
134 #define	MSG_INPUTHDR	"Default values are printed inside of '[]'.\n" \
135 			"To accept the default, type <return>.\n" \
136 			"To have a blank entry, type the word 'none'.\n"
137 #define	MSG_UNKNOWN	"%s: User unknown: %s\n"
138 #define	MSG_ACCOUNT_EXP	"User account has expired: %s\n"
139 #define	MSG_AUTHTOK_EXP	"Your password has been expired for too long.\n" \
140 			"Please contact the system administrator.\n"
141 #define	MSG_NIS_HOMEDIR	"-h does not apply to NIS"
142 #define	MSG_CUR_PASS	"Enter existing login password: "
143 #define	MSG_CUR_PASS_UNAME	"Enter %s's existing login password: "
144 #define	MSG_SUCCESS	"%s: password information changed for %s\n"
145 #define	MSG_SORRY	"%s: Sorry, wrong passwd\n"
146 #define	MSG_INFO	"%s: Changing password for %s\n"
147 
148 
149 /*
150  * return code from ckarg() routine
151  */
152 #define	FAIL 		-1
153 
154 /*
155  *  defind password file name
156  */
157 #define	PASSWD 			"/etc/passwd"
158 
159 #define	MAX_INPUT_LEN		512
160 
161 #define	DEF_ATTEMPTS	3
162 
163 /* Number of characters in that make up an encrypted password (for now) */
164 #define	NUMCP			13
165 
166 #ifdef DEBUG
167 #define	dprintf1	printf
168 #else
169 #define	dprintf1(w, x)
170 #endif
171 
172 extern int	optind;
173 
174 static int		retval = SUCCESS;
175 static int		pam_retval = PAM_SUCCESS;
176 static uid_t		uid;
177 static char		*prognamep;
178 static long		maxdate;	/* password aging information */
179 static int		passwd_conv(int, struct pam_message **,
180 			    struct pam_response **, void *);
181 static struct pam_conv	pam_conv = {passwd_conv, NULL};
182 static pam_handle_t	*pamh;		/* Authentication handle */
183 static char		*usrname;	/* user whose attribute we update */
184 static adt_session_data_t *ah;  /* audit session handle */
185 static adt_event_data_t *event = NULL; /* event to be generated */
186 
187 static pam_repository_t	auth_rep;
188 static pwu_repository_t	repository;
189 static pwu_repository_t	__REPFILES = { "files", NULL, 0 };
190 
191 /*
192  * Function Declarations
193  */
194 
195 extern	void		setusershell(void);
196 extern	char		*getusershell(void);
197 extern	void		endusershell(void);
198 
199 static	void		passwd_exit(int retcode) __NORETURN;
200 static	void		rusage(void);
201 static	int		ckuid(void);
202 static	int		ckarg(int argc, char **argv, attrlist **attributes);
203 
204 static	int		get_namelist(pwu_repository_t, char ***, int *);
205 static	int		get_namelist_files(char ***, int *);
206 static	int		get_namelist_local(char ***, int *);
207 static	int		get_attr(char *, pwu_repository_t *, attrlist **);
208 static	void		display_attr(char *, attrlist *);
209 static	void		free_attr(attrlist *);
210 static	void		attrlist_add(attrlist **, attrtype, char *);
211 static	void		attrlist_reorder(attrlist **);
212 static	char		*userinput(char *, pwu_repository_t *, attrtype);
213 static	char		*getresponse(char *);
214 
215 /*
216  * main():
217  *	The main routine will call ckarg() to parse the command line
218  *	arguments and call the appropriate functions to perform the
219  *	tasks specified by the arguments. It allows system
220  * 	administrator to add, change and display password attributes.
221  * 	Non privileged user can change password or display
222  * 	password attributes which corresponds to their login name.
223  */
224 
225 int
226 main(int argc, char *argv[])
227 {
228 
229 	int	flag;
230 	char	**namelist;
231 	int	num_user;
232 	int	i;
233 	attrlist *attributes = NULL;
234 	char	*input;
235 	int	tries = 1;
236 	int	updated_reps;
237 
238 
239 	if (prognamep = strrchr(argv[0], '/'))
240 		++prognamep;
241 	else
242 		prognamep = argv[0];
243 
244 	auth_rep.type = NULL;
245 	auth_rep.scope = NULL;
246 	repository.type = NULL;
247 	repository.scope = NULL;
248 	repository.scope_len = 0;
249 
250 
251 	/* initialization for variables, set locale and textdomain  */
252 	i = 0;
253 	flag = 0;
254 
255 	uid = getuid();		/* get the user id */
256 	(void) setlocale(LC_ALL, "");
257 
258 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
259 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
260 #endif
261 	(void) textdomain(TEXT_DOMAIN);
262 
263 	/*
264 	 * ckarg() parses the arguments. In case of an error,
265 	 * it sets the retval and returns FAIL (-1).
266 	 */
267 
268 	flag = ckarg(argc, argv, &attributes);
269 	dprintf1("flag is %0x\n", flag);
270 	if (flag == FAIL)
271 		passwd_exit(retval);
272 
273 	argc -= optind;
274 
275 	if (argc < 1) {
276 		if ((usrname = getlogin()) == NULL) {
277 			struct passwd *pass = getpwuid(uid);
278 			if (pass != NULL)
279 				usrname = pass->pw_name;
280 			else {
281 				rusage();
282 				exit(NOPERM);
283 			}
284 		} else if (flag == 0) {
285 			/*
286 			 * If flag is zero, change passwd.
287 			 * Otherwise, it will display or
288 			 * modify password aging attributes
289 			 */
290 			(void) fprintf(stderr, gettext(MSG_INFO), prognamep,
291 			    usrname);
292 		}
293 	} else
294 		usrname = argv[optind];
295 
296 	if (pam_start("passwd", usrname, &pam_conv, &pamh) != PAM_SUCCESS)
297 		passwd_exit(NOPERM);
298 
299 	auth_rep.type = repository.type;
300 	auth_rep.scope = repository.scope;
301 	auth_rep.scope_len = repository.scope_len;
302 
303 	if (auth_rep.type != NULL) {
304 		if (pam_set_item(pamh, PAM_REPOSITORY, (void *)&auth_rep)
305 		    != PAM_SUCCESS) {
306 			passwd_exit(NOPERM);
307 		}
308 	}
309 
310 	if (flag ==  SAFLAG) {	/* display password attributes for all users */
311 		retval = get_namelist(repository, &namelist, &num_user);
312 		if (retval != SUCCESS)
313 			(void) passwd_exit(retval);
314 
315 		if (num_user == 0) {
316 			(void) fprintf(stderr, "%s: %s\n", prognamep,
317 			    gettext(MSG_FF));
318 			passwd_exit(FATAL);
319 		}
320 		i = 0;
321 		while (namelist[i] != NULL) {
322 			(void) get_attr(namelist[i], &repository,
323 			    &attributes);
324 			(void) display_attr(namelist[i], attributes);
325 			(void) free(namelist[i]);
326 			(void) free_attr(attributes);
327 			i++;
328 		}
329 		(void) free(namelist);
330 		passwd_exit(SUCCESS);
331 	} else if (flag == SFLAG) { /* display password attributes by user */
332 		if (get_attr(usrname, &repository, &attributes) ==
333 		    PWU_SUCCESS) {
334 			(void) display_attr(usrname, attributes);
335 			(void) free_attr(attributes);
336 		}
337 		passwd_exit(SUCCESS);
338 		/* NOT REACHED */
339 	}
340 
341 
342 	switch (pam_authenticate(pamh, 0)) {
343 	case PAM_SUCCESS:
344 		break;
345 	case PAM_USER_UNKNOWN:
346 		(void) fprintf(stderr, gettext(MSG_UNKNOWN), prognamep,
347 		    usrname);
348 		passwd_exit(NOPERM);
349 		break;
350 	case PAM_PERM_DENIED:
351 		passwd_exit(NOPERM);
352 		break;
353 	case PAM_AUTH_ERR:
354 		(void) fprintf(stderr, gettext(MSG_SORRY), prognamep);
355 		passwd_exit(NOPERM);
356 		break;
357 	default:
358 		/* system error */
359 		passwd_exit(FMERR);
360 		break;
361 	}
362 
363 	if (flag == 0) {			/* changing user password */
364 		int	chk_authtok = 0;	/* check password strength */
365 
366 		dprintf1("call pam_chauthtok() repository name =%s\n",
367 		    repository.type);
368 
369 		/* Set up for Audit */
370 		if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA) != 0) {
371 			perror("adt_start_session");
372 			passwd_exit(SYSERR);
373 		}
374 		if ((event = adt_alloc_event(ah, ADT_passwd)) == NULL) {
375 			perror("adt_alloc_event");
376 			passwd_exit(NOMEM);
377 		}
378 		if (argc >= 1) {
379 			/* save target user */
380 			event->adt_passwd.username = usrname;
381 		}
382 
383 		/* Don't check account expiration when invoked by root */
384 		if (ckuid() != SUCCESS) {
385 			pam_retval = pam_acct_mgmt(pamh, PAM_SILENT);
386 			switch (pam_retval) {
387 			case PAM_ACCT_EXPIRED:
388 				(void) fprintf(stderr,
389 				    gettext(MSG_ACCOUNT_EXP), usrname);
390 				passwd_exit(EXPIRED);
391 				break;
392 			case PAM_AUTHTOK_EXPIRED:
393 				(void) fprintf(stderr,
394 				    gettext(MSG_AUTHTOK_EXP));
395 				passwd_exit(NOPERM);
396 				break;
397 			case PAM_NEW_AUTHTOK_REQD:
398 				/* valid error when changing passwords */
399 				break;
400 			case PAM_SUCCESS:
401 				/* Ok to change password */
402 				break;
403 			default:
404 				passwd_exit(NOPERM);
405 			}
406 		}
407 
408 
409 		pam_retval = PAM_AUTHTOK_ERR;
410 		tries = 1;
411 		if (ckuid() == SUCCESS) {
412 			/* bypass password strength checks */
413 			chk_authtok = PAM_NO_AUTHTOK_CHECK;
414 		}
415 
416 		while (pam_retval == PAM_AUTHTOK_ERR && tries <= DEF_ATTEMPTS) {
417 			if (tries > 1)
418 				(void) printf(gettext(MSG_AGAIN));
419 			pam_retval = pam_chauthtok(pamh, chk_authtok);
420 			if (pam_retval == PAM_TRY_AGAIN) {
421 				(void) sleep(1);
422 				pam_retval = pam_chauthtok(pamh, chk_authtok);
423 			}
424 			tries++;
425 		}
426 
427 		switch (pam_retval) {
428 		case PAM_SUCCESS:
429 			retval = SUCCESS;
430 			break;
431 		case PAM_AUTHTOK_DISABLE_AGING:
432 			retval = BADAGE;
433 			break;
434 		case PAM_AUTHTOK_LOCK_BUSY:
435 			retval = FBUSY;
436 			break;
437 		case PAM_TRY_AGAIN:
438 			retval = FBUSY;
439 			break;
440 		case PAM_AUTHTOK_ERR:
441 		case PAM_AUTHTOK_RECOVERY_ERR:
442 		default:
443 			retval = NOPERM;
444 			break;
445 		}
446 
447 		(void) passwd_exit(retval);
448 		/* NOT REACHED */
449 	} else {		/* changing attributes */
450 		switch (flag) {
451 		case EFLAG:		/* changing user password attributes */
452 			input = userinput(usrname, &repository, ATTR_SHELL);
453 			if (input)
454 				attrlist_add(&attributes, ATTR_SHELL, input);
455 			else
456 				(void) printf(gettext(MSG_SHELL_UNCHANGED));
457 			break;
458 		case GFLAG:
459 			input = userinput(usrname, &repository, ATTR_GECOS);
460 			if (input)
461 				attrlist_add(&attributes, ATTR_GECOS, input);
462 			else
463 				(void) printf(gettext(MSG_GECOS_UNCHANGED));
464 			break;
465 		case HFLAG:
466 			input = userinput(usrname, &repository, ATTR_HOMEDIR);
467 			if (input)
468 				attrlist_add(&attributes, ATTR_HOMEDIR, input);
469 			else
470 				(void) printf(gettext(MSG_DIR_UNCHANGED));
471 			break;
472 		}
473 
474 		if (attributes != NULL) {
475 			retval = __set_authtoken_attr(usrname,
476 			    pamh->ps_item[PAM_AUTHTOK].pi_addr,
477 			    &repository, attributes, &updated_reps);
478 			switch (retval) {
479 			case PWU_SUCCESS:
480 				for (i = 1; i <= REP_LAST; i <<= 1) {
481 					if ((updated_reps & i) == 0)
482 						continue;
483 					(void) printf(gettext(MSG_SUCCESS),
484 					    prognamep, usrname);
485 				}
486 				retval = SUCCESS;
487 				break;
488 			case PWU_AGING_DISABLED:
489 				retval = BADAGE;
490 				break;
491 			default:
492 				retval = NOPERM;
493 				break;
494 			}
495 		} else {
496 			retval = SUCCESS; /* nothing to change won't fail */
497 		}
498 		(void) passwd_exit(retval);
499 	}
500 	/* NOTREACHED */
501 	return (0);
502 }
503 
504 /*
505  * Get a line of input from the user.
506  *
507  * If the line is empty, or the input equals 'oldval', NULL is returned.
508  * therwise, a malloced string containing the input (minus the trailing
509  * newline) is returned.
510  */
511 char *
512 getresponse(char *oldval)
513 {
514 	char    resp[MAX_INPUT_LEN];
515 	char    *retval = NULL;
516 	int	resplen;
517 
518 	(void) fgets(resp, sizeof (resp) - 1, stdin);
519 	resplen = strlen(resp) - 1;
520 	if (resp[resplen] == '\n')
521 		resp[resplen] = '\0';
522 	if (*resp != '\0' && strcmp(resp, oldval) != 0)
523 		retval = strdup(resp);
524 	return (retval);
525 }
526 
527 /*
528  * char *userinput(item)
529  *
530  * user conversation function. The old value of attribute "item" is
531  * displayed while the user is asked to provide a new value.
532  *
533  * returns a malloc()-ed string if the user actualy provided input
534  * or NULL if the user simply hit return or the input equals the old
535  * value (not changed).
536  */
537 char *
538 userinput(char *name, pwu_repository_t *rep, attrtype type)
539 {
540 	attrlist oldattr;
541 	char *oldval;			/* shorthand for oldattr.data.val_s */
542 	char *valid;			/* points to valid shells */
543 	char *response;
544 	char *cp;
545 
546 	oldattr.type = type;
547 	oldattr.next = NULL;
548 
549 	if (__get_authtoken_attr(name, rep, &oldattr) != PWU_SUCCESS)
550 		passwd_exit(FMERR);
551 
552 	oldval = oldattr.data.val_s;
553 
554 	if (type == ATTR_SHELL) {
555 		/* No current shell: set DEFSHL as default choice */
556 		if (*oldval == '\0') {
557 			free(oldval);
558 			oldval = strdup(DEFSHL);
559 		}
560 
561 		if (ckuid() != SUCCESS) {
562 			/* User must currently have a valid shell */
563 			setusershell();
564 			valid = getusershell();
565 			while (valid && strcmp(valid, oldval) != 0)
566 				valid = getusershell();
567 			endusershell();
568 
569 			if (valid == NULL) {
570 				(void) fprintf(stderr, gettext(MSG_RS), oldval);
571 				free(oldval);
572 				return (NULL);
573 			}
574 		}
575 		(void) printf(gettext(MSG_OLDSHELL), oldval);
576 		(void) printf(gettext(MSG_NEWSHELL));
577 		(void) fflush(stdout);
578 
579 		response = getresponse(oldval);
580 		free(oldval); /* We don't need the old value anymore */
581 
582 		if (response == NULL || *response == '\0')
583 			return (NULL);
584 
585 		/* Make sure new shell is listed */
586 		setusershell();
587 		valid = getusershell();
588 		while (valid) {
589 			char *cp;
590 
591 			/* Allow user to give shell without path */
592 			if (*response == '/') {
593 				cp = valid;
594 			} else {
595 				if ((cp = strrchr(valid, '/')) == NULL)
596 					cp = valid;
597 				else
598 					cp++;
599 			}
600 			if (strcmp(cp, response) == 0) {
601 				if (*response != '/') {
602 					/* take shell name including path */
603 					free(response);
604 					response = strdup(valid);
605 				}
606 				break;
607 			}
608 			valid = getusershell();
609 		}
610 		endusershell();
611 
612 		if (valid == NULL) {    /* No valid shell matches */
613 			(void) fprintf(stderr, gettext(MSG_UNACCEPT), response);
614 			return (NULL);
615 		}
616 
617 		if (access(response, X_OK) < 0)
618 			(void) fprintf(stderr, gettext(MSG_UNAVAIL), response);
619 		return (response);
620 		/* NOT REACHED */
621 	}
622 	/*
623 	 * if type == SHELL, we have returned by now. Only GECOS and
624 	 * HOMEDIR get to this point.
625 	 */
626 	(void) printf(gettext(MSG_INPUTHDR));
627 
628 	/*
629 	 * PRE: oldval points to malloced string with Old Value
630 	 * INV: oldval remains unchanged
631 	 * POST:response points to valid string or NULL.
632 	 */
633 	for (;;) {
634 		if (type == ATTR_GECOS)
635 			(void) printf(gettext(MSG_NAME), oldval);
636 		else if (type == ATTR_HOMEDIR)
637 			(void) printf(gettext(MSG_HOMEDIR), oldval);
638 
639 		response = getresponse(oldval);
640 
641 		if (response && strcmp(response, "none") == 0)
642 			*response = '\0';
643 
644 		/* No-change or empty string are OK */
645 		if (response == NULL || *response == '\0')
646 			break;
647 
648 		/* Check for illegal characters */
649 		if (strchr(response, ':')) {
650 			(void) fprintf(stderr, "%s", gettext(MSG_COLON));
651 			free(response);
652 		} else if (strlen(response) > MAX_INPUT_LEN - 1) {
653 			(void) fprintf(stderr, gettext(MSG_MAXLEN),
654 			    MAX_INPUT_LEN);
655 			free(response);
656 		} else {
657 			/* don't allow control characters */
658 			for (cp = response; *cp >= 040; cp++)
659 				;
660 			if (*cp != '\0') {
661 				(void) fprintf(stderr, gettext(MSG_CONTROL));
662 				free(response);
663 			} else
664 				break;	/* response is a valid string */
665 		}
666 		/*
667 		 * We only get here if the input was invalid.
668 		 * In that case, we again ask the user for input.
669 		 */
670 	}
671 	free(oldval);
672 	return (response);
673 }
674 /*
675  * ckarg():
676  *	This function parses and verifies the
677  * 	arguments.  It takes three parameters:
678  * 	argc => # of arguments
679  * 	argv => pointer to an argument
680  * 	attrlist => pointer to list of password attributes
681  */
682 
683 static int
684 ckarg(int argc, char **argv, attrlist **attributes)
685 {
686 	extern char	*optarg;
687 	char		*char_p;
688 	int	opt;
689 	int	flag;
690 
691 	flag = 0;
692 
693 	while ((opt = getopt(argc, argv, "r:aldefghsux:n:w:N")) != EOF) {
694 		switch (opt) {
695 
696 		case 'r': /* Repository Specified */
697 			/* repository: this option should be specified first */
698 
699 			if (repository.type != NULL) {
700 				(void) fprintf(stderr, gettext(
701 			"Repository is already defined or specified.\n"));
702 				rusage();
703 				retval = BADSYN;
704 				return (FAIL);
705 			}
706 			if (strcmp(optarg, "nis") == 0) {
707 				repository.type = optarg;
708 			} else if (strcmp(optarg, "ldap") == 0) {
709 				repository.type = optarg;
710 			} else if (strcmp(optarg, "files") == 0) {
711 				repository.type = optarg;
712 			} else {
713 				(void) fprintf(stderr,
714 				    gettext("invalid repository: %s\n"),
715 				    optarg);
716 				rusage();
717 				retval = BADSYN;
718 				return (FAIL);
719 			}
720 			break;
721 
722 		case 'd': /* Delete Auth Token */
723 			/* if no repository the default for -d is files */
724 			if (repository.type == NULL)
725 				repository = __REPFILES;
726 
727 			/*
728 			 * Delete the password - only privileged processes
729 			 * can execute this for FILES or LDAP
730 			 */
731 			if (IS_FILES(repository) == FALSE &&
732 			    IS_LDAP(repository) == FALSE) {
733 				(void) fprintf(stderr, gettext(
734 				    "-d only applies to files "
735 				    "or ldap repository\n"));
736 				rusage();	/* exit */
737 				retval = BADSYN;
738 				return (FAIL);
739 			}
740 
741 			if (ckuid() != SUCCESS) {
742 				retval = NOPERM;
743 				return (FAIL);
744 			}
745 			if (flag & (LFLAG|SAFLAG|DFLAG|XFLAG|UFLAG)) {
746 				rusage();
747 				retval = BADOPT;
748 				return (FAIL);
749 			}
750 			flag |= DFLAG;
751 			attrlist_add(attributes, ATTR_PASSWD, NULL);
752 			break;
753 
754 		case 'N': /* set account to be "no login" */
755 
756 			/* if no repository the default for -N is files */
757 			if (repository.type == NULL)
758 				repository = __REPFILES;
759 
760 			if (IS_FILES(repository) == FALSE &&
761 			    IS_LDAP(repository) == FALSE) {
762 				(void) fprintf(stderr, gettext(
763 				    "-N only applies to files or ldap "
764 				    "repository\n"));
765 				rusage();	/* exit */
766 				retval = BADOPT;
767 				return (FAIL);
768 			}
769 
770 			/*
771 			 * Only privileged processes can execute this
772 			 * for FILES or LDAP
773 			 */
774 			if ((IS_FILES(repository) || IS_LDAP(repository)) &&
775 			    ((retval = ckuid()) != SUCCESS))
776 				return (FAIL);
777 			if (flag & (MUTEXFLAG|NONAGEFLAG)) {
778 				rusage();	/* exit */
779 				retval = BADOPT;
780 				return (FAIL);
781 			}
782 			flag |= XFLAG;
783 			attrlist_add(attributes, ATTR_NOLOGIN_ACCOUNT, NULL);
784 			break;
785 
786 		case 'l': /* lock the password */
787 
788 			/* if no repository the default for -l is files */
789 			if (repository.type == NULL)
790 				repository = __REPFILES;
791 
792 			if (IS_FILES(repository) == FALSE &&
793 			    IS_LDAP(repository) == FALSE) {
794 				(void) fprintf(stderr, gettext(
795 				    "-l only applies to files or ldap "
796 				    "repository\n"));
797 				rusage();	/* exit */
798 				retval = BADOPT;
799 				return (FAIL);
800 			}
801 
802 			/*
803 			 * Only privileged processes can execute this
804 			 * for FILES or LDAP
805 			 */
806 			if ((IS_FILES(repository) || IS_LDAP(repository)) &&
807 			    ((retval = ckuid()) != SUCCESS))
808 				return (FAIL);
809 			if (flag & (MUTEXFLAG|NONAGEFLAG)) {
810 				rusage();	/* exit */
811 				retval = BADOPT;
812 				return (FAIL);
813 			}
814 			flag |= LFLAG;
815 			attrlist_add(attributes, ATTR_LOCK_ACCOUNT, NULL);
816 			break;
817 
818 		case 'u': /* unlock the password */
819 
820 			/* if no repository the default for -u is files */
821 			if (repository.type == NULL)
822 				repository = __REPFILES;
823 
824 			if (IS_FILES(repository) == FALSE &&
825 			    IS_LDAP(repository) == FALSE) {
826 				(void) fprintf(stderr, gettext(
827 				    "-u only applies to files or ldap "
828 				    "repository\n"));
829 				rusage();	/* exit */
830 				retval = BADOPT;
831 				return (FAIL);
832 			}
833 
834 			/*
835 			 * Only privileged processes can execute this
836 			 * for FILES or LDAP
837 			 */
838 			if ((IS_FILES(repository) || IS_LDAP(repository)) &&
839 			    ((retval = ckuid()) != SUCCESS))
840 				return (FAIL);
841 			if (flag & (MUTEXFLAG|NONAGEFLAG)) {
842 				rusage();	/* exit */
843 				retval = BADOPT;
844 				return (FAIL);
845 			}
846 			flag |= UFLAG;
847 			attrlist_add(attributes, ATTR_UNLOCK_ACCOUNT, NULL);
848 			attrlist_add(attributes, ATTR_RST_FAILED_LOGINS, NULL);
849 			break;
850 
851 		case 'x': /* set the max date */
852 
853 			/* if no repository the default for -x is files */
854 			if (repository.type == NULL)
855 				repository = __REPFILES;
856 
857 			if (IS_FILES(repository) == FALSE &&
858 			    IS_LDAP(repository) == FALSE) {
859 				(void) fprintf(stderr, gettext(
860 				    "-x only applies to files or ldap "
861 				    "repository\n"));
862 				rusage();	/* exit */
863 				retval = BADSYN;
864 				return (FAIL);
865 			}
866 
867 			/*
868 			 * Only privileged process can execute this
869 			 * for FILES or LDAP
870 			 */
871 			if ((IS_FILES(repository) || IS_LDAP(repository)) &&
872 			    (ckuid() != SUCCESS)) {
873 				retval = NOPERM;
874 				return (FAIL);
875 			}
876 			if (flag & (SAFLAG|MFLAG|NONAGEFLAG)) {
877 				retval = BADOPT;
878 				return (FAIL);
879 			}
880 			flag |= MFLAG;
881 			if ((int)strlen(optarg)  <= 0 ||
882 			    (maxdate = strtol(optarg, &char_p, 10)) < -1 ||
883 			    *char_p != '\0') {
884 				(void) fprintf(stderr, "%s: %s -x\n",
885 				    prognamep, gettext(MSG_NV));
886 				retval = BADSYN;
887 				return (FAIL);
888 			}
889 			attrlist_add(attributes, ATTR_MAX, optarg);
890 			break;
891 
892 		case 'n': /* set the min date */
893 
894 			/* if no repository the default for -n is files */
895 			if (repository.type == NULL)
896 				repository = __REPFILES;
897 
898 			if (IS_FILES(repository) == FALSE &&
899 			    IS_LDAP(repository) == FALSE) {
900 				(void) fprintf(stderr, gettext(
901 				    "-n only applies to files or ldap "
902 				    "repository\n"));
903 				rusage();	/* exit */
904 				retval = BADSYN;
905 				return (FAIL);
906 			}
907 
908 			/*
909 			 * Only privileged process can execute this
910 			 * for FILES or LDAP
911 			 */
912 			if ((IS_FILES(repository) || IS_LDAP(repository)) &&
913 			    ((retval = ckuid()) != SUCCESS))
914 				return (FAIL);
915 			if (flag & (SAFLAG|NFLAG|NONAGEFLAG)) {
916 				retval = BADOPT;
917 				return (FAIL);
918 			}
919 			flag |= NFLAG;
920 			if ((int)strlen(optarg)  <= 0 ||
921 			    (strtol(optarg, &char_p, 10)) < 0 ||
922 			    *char_p != '\0') {
923 				(void) fprintf(stderr, "%s: %s -n\n",
924 				    prognamep, gettext(MSG_NV));
925 				retval = BADSYN;
926 				return (FAIL);
927 			}
928 			attrlist_add(attributes, ATTR_MIN, optarg);
929 			break;
930 
931 		case 'w': /* set the warning field */
932 
933 			/* if no repository the default for -w is files */
934 			if (repository.type == NULL)
935 				repository = __REPFILES;
936 
937 			if (IS_FILES(repository) == FALSE &&
938 			    IS_LDAP(repository) == FALSE) {
939 				(void) fprintf(stderr, gettext(
940 				    "-w only applies to files or ldap "
941 				    "repository\n"));
942 				rusage();	/* exit */
943 				retval = BADSYN;
944 				return (FAIL);
945 			}
946 
947 			/*
948 			 * Only privileged process can execute this
949 			 * for FILES or LDAP
950 			 */
951 			if ((IS_FILES(repository) || IS_LDAP(repository)) &&
952 			    (ckuid() != SUCCESS)) {
953 				retval = NOPERM;
954 				return (FAIL);
955 			}
956 			if (flag & (SAFLAG|WFLAG|NONAGEFLAG)) {
957 				retval = BADOPT;
958 				return (FAIL);
959 			}
960 			flag |= WFLAG;
961 			if ((int)strlen(optarg)  <= 0 ||
962 			    (strtol(optarg, &char_p, 10)) < 0 ||
963 			    *char_p != '\0') {
964 				(void) fprintf(stderr, "%s: %s -w\n",
965 				    prognamep, gettext(MSG_NV));
966 				retval = BADSYN;
967 				return (FAIL);
968 			}
969 			attrlist_add(attributes, ATTR_WARN, optarg);
970 			break;
971 
972 		case 's': /* display password attributes */
973 
974 			/* if no repository the default for -s is files */
975 			if (repository.type == NULL)
976 				repository = __REPFILES;
977 
978 
979 			/* display password attributes */
980 			if (IS_FILES(repository) == FALSE &&
981 			    IS_LDAP(repository) == FALSE) {
982 				(void) fprintf(stderr, gettext(
983 				    "-s only applies to files or ldap "
984 				    "repository\n"));
985 				rusage();	/* exit */
986 				retval = BADSYN;
987 				return (FAIL);
988 			}
989 
990 			/*
991 			 * Only privileged process can execute this
992 			 * for FILES or LDAP
993 			 */
994 			if ((IS_FILES(repository) || IS_LDAP(repository)) &&
995 			    ((retval = ckuid()) != SUCCESS))
996 				return (FAIL);
997 			if (flag && (flag != AFLAG)) {
998 				retval = BADOPT;
999 				return (FAIL);
1000 			}
1001 			flag |= SFLAG;
1002 			break;
1003 
1004 		case 'a': /* display password attributes */
1005 
1006 			/* if no repository the default for -a is files */
1007 			if (repository.type == NULL)
1008 				repository = __REPFILES;
1009 
1010 			if (IS_FILES(repository) == FALSE &&
1011 			    IS_LDAP(repository) == FALSE) {
1012 				(void) fprintf(stderr, gettext(
1013 				    "-a only applies to files or ldap "
1014 				    "repository\n"));
1015 				rusage();	/* exit */
1016 				retval = BADSYN;
1017 				return (FAIL);
1018 			}
1019 
1020 			/*
1021 			 * Only privileged process can execute this
1022 			 * for FILES or LDAP
1023 			 */
1024 			if ((IS_FILES(repository) || IS_LDAP(repository)) &&
1025 			    ((retval = ckuid()) != SUCCESS))
1026 				return (FAIL);
1027 			if (flag && (flag != SFLAG)) {
1028 				retval = BADOPT;
1029 				return (FAIL);
1030 			}
1031 			flag |= AFLAG;
1032 			break;
1033 
1034 		case 'f': /* expire password attributes	*/
1035 
1036 			/* if no repository the default for -f is files */
1037 			if (repository.type == NULL)
1038 				repository = __REPFILES;
1039 
1040 			if (IS_FILES(repository) == FALSE &&
1041 			    IS_LDAP(repository) == FALSE) {
1042 				(void) fprintf(stderr, gettext(
1043 				    "-f only applies to files or ldap "
1044 				    "repository\n"));
1045 				rusage();	/* exit */
1046 				retval = BADSYN;
1047 				return (FAIL);
1048 			}
1049 
1050 			/*
1051 			 * Only privileged process can execute this
1052 			 * for FILES or LDAP
1053 			 */
1054 			if ((IS_FILES(repository) || IS_LDAP(repository)) &&
1055 			    ((retval = ckuid()) != SUCCESS))
1056 				return (FAIL);
1057 			if (flag & (SAFLAG|FFLAG|NONAGEFLAG)) {
1058 				retval = BADOPT;
1059 				return (FAIL);
1060 			}
1061 			flag |= FFLAG;
1062 			attrlist_add(attributes, ATTR_EXPIRE_PASSWORD, NULL);
1063 			break;
1064 
1065 		case 'e': /* change login shell */
1066 
1067 			/* if no repository the default for -e is files */
1068 			if (repository.type == NULL)
1069 				repository = __REPFILES;
1070 
1071 			/*
1072 			 * Only privileged process can execute this
1073 			 * for FILES
1074 			 */
1075 			if (IS_FILES(repository) && (ckuid() != SUCCESS)) {
1076 				retval = NOPERM;
1077 				return (FAIL);
1078 			}
1079 			if (flag & (EFLAG|SAFLAG|AGEFLAG)) {
1080 				retval = BADOPT;
1081 				return (FAIL);
1082 			}
1083 			flag |= EFLAG;
1084 			break;
1085 
1086 		case 'g': /* change gecos information */
1087 
1088 			/* if no repository the default for -g is files */
1089 			if (repository.type == NULL)
1090 				repository = __REPFILES;
1091 
1092 			/*
1093 			 * Only privileged process can execute this
1094 			 * for FILES
1095 			 */
1096 			if (IS_FILES(repository) && (ckuid() != SUCCESS)) {
1097 				retval = NOPERM;
1098 				return (FAIL);
1099 			}
1100 			if (flag & (GFLAG|SAFLAG|AGEFLAG)) {
1101 				retval = BADOPT;
1102 				return (FAIL);
1103 			}
1104 			flag |= GFLAG;
1105 			break;
1106 
1107 		case 'h': /* change home dir */
1108 
1109 			/* if no repository the default for -h is files */
1110 			if (repository.type == NULL)
1111 				repository = __REPFILES;
1112 			/*
1113 			 * Only privileged process can execute this
1114 			 * for FILES
1115 			 */
1116 			if (IS_FILES(repository) && (ckuid() != SUCCESS)) {
1117 				retval = NOPERM;
1118 				return (FAIL);
1119 			}
1120 			if (IS_NIS(repository)) {
1121 				(void) fprintf(stderr, "%s\n",
1122 				    gettext(MSG_NIS_HOMEDIR));
1123 				retval = BADSYN;
1124 				return (FAIL);
1125 			}
1126 
1127 			if (flag & (HFLAG|SAFLAG|AGEFLAG)) {
1128 				retval = BADOPT;
1129 				return (FAIL);
1130 			}
1131 			flag |= HFLAG;
1132 			break;
1133 
1134 		case '?':
1135 			rusage();
1136 			retval = BADOPT;
1137 			return (FAIL);
1138 		}
1139 	}
1140 
1141 	argc -= optind;
1142 	if (argc > 1) {
1143 		rusage();
1144 		retval = BADSYN;
1145 		return (FAIL);
1146 	}
1147 
1148 	/* Make sure (EXPIRE comes after (MAX comes after MIN)) */
1149 	attrlist_reorder(attributes);
1150 
1151 	/* If no options are specified or only the show option */
1152 	/* is specified, return because no option error checking */
1153 	/* is needed */
1154 	if (!flag || (flag == SFLAG))
1155 		return (flag);
1156 
1157 	/* AFLAG must be used with SFLAG */
1158 	if (flag == AFLAG) {
1159 		rusage();
1160 		retval = BADSYN;
1161 		return (FAIL);
1162 	}
1163 
1164 	if (flag != SAFLAG && argc < 1) {
1165 		/*
1166 		 * user name is not specified (argc<1), it can't be
1167 		 * aging info update.
1168 		 */
1169 		if (!(flag & NONAGEFLAG)) {
1170 			rusage();
1171 			retval = BADSYN;
1172 			return (FAIL);
1173 		}
1174 	}
1175 
1176 	/* user name(s) may not be specified when SAFLAG is used. */
1177 	if (flag == SAFLAG && argc >= 1) {
1178 		rusage();
1179 		retval = BADSYN;
1180 		return (FAIL);
1181 	}
1182 
1183 	/*
1184 	 * If aging is being turned off (maxdate == -1), mindate may not
1185 	 * be specified.
1186 	 */
1187 	if ((maxdate == -1) && (flag & NFLAG)) {
1188 		(void) fprintf(stderr, "%s: %s -n\n",
1189 		    prognamep, gettext(MSG_NV));
1190 		retval = BADOPT;
1191 		return (FAIL);
1192 	}
1193 
1194 	return (flag);
1195 }
1196 
1197 /*
1198  *
1199  * ckuid():
1200  *	This function returns SUCCESS if the caller is root, else
1201  *	it returns NOPERM.
1202  *
1203  */
1204 
1205 static int
1206 ckuid(void)
1207 {
1208 	if (uid != 0) {
1209 		return (retval = NOPERM);
1210 	}
1211 	return (SUCCESS);
1212 }
1213 
1214 
1215 /*
1216  * get_attr()
1217  */
1218 int
1219 get_attr(char *username, pwu_repository_t *repository, attrlist **attributes)
1220 {
1221 	int res;
1222 
1223 	attrlist_add(attributes, ATTR_PASSWD, NULL);
1224 	attrlist_add(attributes, ATTR_LSTCHG, "0");
1225 	attrlist_add(attributes, ATTR_MIN, "0");
1226 	attrlist_add(attributes, ATTR_MAX, "0");
1227 	attrlist_add(attributes, ATTR_WARN, "0");
1228 
1229 	res = __get_authtoken_attr(username, repository, *attributes);
1230 
1231 	if (res == PWU_SUCCESS) {
1232 		retval = SUCCESS;
1233 		return (PWU_SUCCESS);
1234 	}
1235 
1236 	if (res == PWU_NOT_FOUND)
1237 		(void) fprintf(stderr, gettext(MSG_UNKNOWN), prognamep,
1238 		    username);
1239 
1240 	retval = NOPERM;
1241 	passwd_exit(retval);
1242 	/*NOTREACHED*/
1243 }
1244 
1245 /*
1246  * display_attr():
1247  * This function prints out the password attributes of a usr
1248  * onto standand output.
1249  */
1250 void
1251 display_attr(char *usrname, attrlist *attributes)
1252 {
1253 	char	*status;
1254 	char	*passwd;
1255 	long	lstchg;
1256 	int	min, max, warn;
1257 
1258 	while (attributes) {
1259 		switch (attributes->type) {
1260 		case ATTR_PASSWD:
1261 			passwd = attributes->data.val_s;
1262 			if (passwd == NULL || *passwd == '\0')
1263 				status = "NP  ";
1264 			else if (strncmp(passwd, LOCKSTRING,
1265 			    sizeof (LOCKSTRING)-1) == 0)
1266 				status = "LK  ";
1267 			else if (strncmp(passwd, NOLOGINSTRING,
1268 			    sizeof (NOLOGINSTRING)-1) == 0)
1269 				status = "NL  ";
1270 			else if ((strlen(passwd) == 13 && passwd[0] != '$') ||
1271 			    passwd[0] == '$')
1272 				status = "PS  ";
1273 			else
1274 				status = "UN  ";
1275 			break;
1276 		case ATTR_LSTCHG:
1277 			lstchg = attributes->data.val_i * DAY;
1278 			break;
1279 		case ATTR_MIN:
1280 			min = attributes->data.val_i;
1281 			break;
1282 		case ATTR_MAX:
1283 			max = attributes->data.val_i;
1284 			break;
1285 		case ATTR_WARN:
1286 			warn = attributes->data.val_i;
1287 			break;
1288 		}
1289 		attributes = attributes->next;
1290 	}
1291 	(void) fprintf(stdout, "%-8s  ", usrname);
1292 
1293 	if (status)
1294 		(void) fprintf(stdout, "%s  ", status);
1295 
1296 	if (max != -1) {
1297 		if (lstchg == 0) {
1298 			(void) fprintf(stdout, "00/00/00  ");
1299 		} else {
1300 			struct tm *tmp;
1301 			tmp = gmtime(&lstchg);
1302 			(void) fprintf(stdout, "%.2d/%.2d/%.2d  ",
1303 			    tmp->tm_mon + 1,
1304 			    tmp->tm_mday,
1305 			    tmp->tm_year % 100);
1306 		}
1307 		(void) fprintf(stdout, (min >= 0) ? "%4d  " : "      ", min);
1308 		(void) fprintf(stdout, "%4d  ", max);
1309 		(void) fprintf(stdout, (warn > 0) ? "%4d  " : "      ", warn);
1310 	}
1311 	(void) fprintf(stdout, "\n");
1312 }
1313 
1314 void
1315 free_attr(attrlist *attributes)
1316 {
1317 	while (attributes) {
1318 		if (attributes->type == ATTR_PASSWD)
1319 			free(attributes->data.val_s);
1320 		attributes = attributes->next;
1321 	}
1322 }
1323 
1324 /*
1325  *
1326  * get_namelist_files():
1327  *	This function gets a list of user names on the system from
1328  *	the /etc/passwd file.
1329  *
1330  */
1331 int
1332 get_namelist_files(char ***namelist_p, int *num_user)
1333 {
1334 	FILE		*pwfp;
1335 	struct passwd	*pwd;
1336 	int		max_user;
1337 	int		nuser;
1338 	char	**nl;
1339 
1340 	nuser = 0;
1341 	errno = 0;
1342 	pwd = NULL;
1343 
1344 	if ((pwfp = fopen(PASSWD, "r")) == NULL)
1345 		return (NOPERM);
1346 
1347 	/*
1348 	 * find out the actual number of entries in the PASSWD file
1349 	 */
1350 	max_user = 1;			/* need one slot for terminator NULL */
1351 	while ((pwd = fgetpwent(pwfp)) != NULL)
1352 		max_user++;
1353 
1354 	/*
1355 	 *	reset the file stream pointer
1356 	 */
1357 	rewind(pwfp);
1358 
1359 	nl = (char **)calloc(max_user, (sizeof (char *)));
1360 	if (nl == NULL) {
1361 		(void) fclose(pwfp);
1362 		return (FMERR);
1363 	}
1364 
1365 	while ((pwd = fgetpwent(pwfp)) != NULL) {
1366 		if ((nl[nuser] = strdup(pwd->pw_name)) == NULL) {
1367 			(void) fclose(pwfp);
1368 			return (FMERR);
1369 		}
1370 		nuser++;
1371 	}
1372 
1373 	nl[nuser] = NULL;
1374 	*num_user = nuser;
1375 	*namelist_p = nl;
1376 	(void) fclose(pwfp);
1377 	return (SUCCESS);
1378 }
1379 
1380 /*
1381  * get_namelist_local
1382  *
1383  */
1384 
1385 /*
1386  * Our private version of the switch frontend for getspent.  We want
1387  * to search just the ldap sp file, so we want to bypass
1388  * normal nsswitch.conf based processing.  This implementation
1389  * compatible with version 2 of the name service switch.
1390  */
1391 #define	NSS_LDAP_ONLY		"ldap"
1392 
1393 extern int str2spwd(const char *, int, void *, char *, int);
1394 
1395 static DEFINE_NSS_DB_ROOT(db_root);
1396 static DEFINE_NSS_GETENT(context);
1397 
1398 static char *local_config;
1399 static void
1400 _lc_nss_initf_shadow(nss_db_params_t *p)
1401 {
1402 	p->name	= NSS_DBNAM_SHADOW;
1403 	p->config_name    = NSS_DBNAM_PASSWD;	/* Use config for "passwd" */
1404 	p->default_config = local_config;   	/* Use ldap only */
1405 	p->flags = NSS_USE_DEFAULT_CONFIG;
1406 }
1407 
1408 static void
1409 _lc_setspent(void)
1410 {
1411 	nss_setent(&db_root, _lc_nss_initf_shadow, &context);
1412 }
1413 
1414 static void
1415 _lc_endspent(void)
1416 {
1417 	nss_endent(&db_root, _lc_nss_initf_shadow, &context);
1418 	nss_delete(&db_root);
1419 }
1420 
1421 static struct spwd *
1422 _lc_getspent_r(struct spwd *result, char *buffer, int buflen)
1423 {
1424 	nss_XbyY_args_t arg;
1425 	char		*nam;
1426 
1427 	/* In getXXent_r(), protect the unsuspecting caller from +/- entries */
1428 
1429 	do {
1430 		NSS_XbyY_INIT(&arg, result, buffer, buflen, str2spwd);
1431 			/* No key to fill in */
1432 		(void) nss_getent(&db_root, _lc_nss_initf_shadow, &context,
1433 		    &arg);
1434 	} while (arg.returnval != 0 &&
1435 	    (nam = ((struct spwd *)arg.returnval)->sp_namp) != 0 &&
1436 	    (*nam == '+' || *nam == '-'));
1437 
1438 	return (struct spwd *)NSS_XbyY_FINI(&arg);
1439 }
1440 
1441 static nss_XbyY_buf_t *buffer;
1442 
1443 static struct spwd *
1444 _lc_getspent(void)
1445 {
1446 	nss_XbyY_buf_t	*b;
1447 
1448 	b = NSS_XbyY_ALLOC(&buffer, sizeof (struct spwd), NSS_BUFLEN_SHADOW);
1449 
1450 	return (b == 0 ? 0 : _lc_getspent_r(b->result, b->buffer, b->buflen));
1451 }
1452 
1453 int
1454 get_namelist_local(char ***namelist_p, int *num_user)
1455 {
1456 	int nuser = 0;
1457 	int alloced = 100;
1458 	char **nl;
1459 	struct spwd *p;
1460 
1461 
1462 	if ((nl = calloc(alloced, sizeof (*nl))) == NULL)
1463 		return (FMERR);
1464 
1465 	(void) _lc_setspent();
1466 	while ((p = _lc_getspent()) != NULL) {
1467 		if ((nl[nuser] = strdup(p->sp_namp)) == NULL) {
1468 			_lc_endspent();
1469 			return (FMERR);
1470 		}
1471 		if (++nuser == alloced) {
1472 			alloced += 100;
1473 			nl = realloc(nl, alloced * (sizeof (*nl)));
1474 			if (nl == NULL) {
1475 				_lc_endspent();
1476 				return (FMERR);
1477 			}
1478 		}
1479 	}
1480 	(void) _lc_endspent();
1481 	nl[nuser] = NULL;
1482 
1483 	*namelist_p = nl;
1484 	*num_user = nuser;		/* including NULL */
1485 
1486 	return (SUCCESS);
1487 }
1488 
1489 int
1490 get_namelist(pwu_repository_t repository, char ***namelist, int *num_user)
1491 {
1492 	if (IS_LDAP(repository)) {
1493 		local_config = NSS_LDAP_ONLY;
1494 		return (get_namelist_local(namelist, num_user));
1495 	} else if (IS_FILES(repository))
1496 		return (get_namelist_files(namelist, num_user));
1497 
1498 	rusage();
1499 	return (BADSYN);
1500 }
1501 
1502 /*
1503  *
1504  * passwd_exit():
1505  *	This function will call exit() with appropriate exit code
1506  *	according to the input "retcode" value.
1507  *	It also calls pam_end() to clean-up buffers before exit.
1508  *
1509  */
1510 
1511 void
1512 passwd_exit(int retcode)
1513 {
1514 
1515 	if (pamh)
1516 		(void) pam_end(pamh, pam_retval);
1517 
1518 	switch (retcode) {
1519 	case SUCCESS:
1520 			break;
1521 	case NOPERM:
1522 			(void) fprintf(stderr, "%s\n", gettext(MSG_NP));
1523 			break;
1524 	case BADOPT:
1525 			(void) fprintf(stderr, "%s\n", gettext(MSG_BS));
1526 			break;
1527 	case FMERR:
1528 			(void) fprintf(stderr, "%s\n", gettext(MSG_FE));
1529 			break;
1530 	case FATAL:
1531 			(void) fprintf(stderr, "%s\n", gettext(MSG_FF));
1532 			break;
1533 	case FBUSY:
1534 			(void) fprintf(stderr, "%s\n", gettext(MSG_FB));
1535 			break;
1536 	case BADSYN:
1537 			(void) fprintf(stderr, "%s\n", gettext(MSG_NV));
1538 			break;
1539 	case BADAGE:
1540 			(void) fprintf(stderr, "%s\n", gettext(MSG_AD));
1541 			break;
1542 	case NOMEM:
1543 			(void) fprintf(stderr, "%s\n", gettext(MSG_NM));
1544 			break;
1545 	default:
1546 			(void) fprintf(stderr, "%s\n", gettext(MSG_NP));
1547 			retcode = NOPERM;
1548 			break;
1549 	}
1550 	/* write password record */
1551 	if (event != NULL) {
1552 		if (adt_put_event(event,
1553 		    retcode == SUCCESS ? ADT_SUCCESS : ADT_FAILURE,
1554 		    retcode == SUCCESS ? ADT_SUCCESS : ADT_FAIL_PAM +
1555 		    pam_retval) != 0) {
1556 			adt_free_event(event);
1557 			(void) adt_end_session(ah);
1558 			perror("adt_put_event");
1559 			exit(retcode);
1560 		}
1561 		adt_free_event(event);
1562 	}
1563 	(void) adt_end_session(ah);
1564 	exit(retcode);
1565 }
1566 
1567 /*
1568  *
1569  * passwd_conv():
1570  *	This is the conv (conversation) function called from
1571  *	a PAM authentication module to print error messages
1572  *	or garner information from the user.
1573  *
1574  */
1575 
1576 /*ARGSUSED*/
1577 static int
1578 passwd_conv(int num_msg, struct pam_message **msg,
1579 	    struct pam_response **response, void *appdata_ptr)
1580 {
1581 	struct pam_message	*m;
1582 	struct pam_response	*r;
1583 	char 			*temp;
1584 	int			k, i;
1585 
1586 	if (num_msg <= 0)
1587 		return (PAM_CONV_ERR);
1588 
1589 	*response = (struct pam_response *)calloc(num_msg,
1590 	    sizeof (struct pam_response));
1591 	if (*response == NULL)
1592 		return (PAM_BUF_ERR);
1593 
1594 	k = num_msg;
1595 	m = *msg;
1596 	r = *response;
1597 	while (k--) {
1598 
1599 		switch (m->msg_style) {
1600 
1601 		case PAM_PROMPT_ECHO_OFF:
1602 			temp = getpassphrase(m->msg);
1603 			if (temp != NULL) {
1604 				r->resp = strdup(temp);
1605 				(void) memset(temp, 0, strlen(temp));
1606 				if (r->resp == NULL) {
1607 					/* free responses */
1608 					r = *response;
1609 					for (i = 0; i < num_msg; i++, r++) {
1610 						if (r->resp)
1611 							free(r->resp);
1612 					}
1613 					free(*response);
1614 					*response = NULL;
1615 					return (PAM_BUF_ERR);
1616 				}
1617 			}
1618 			m++;
1619 			r++;
1620 			break;
1621 
1622 		case PAM_PROMPT_ECHO_ON:
1623 			if (m->msg != NULL) {
1624 				(void) fputs(m->msg, stdout);
1625 			}
1626 			r->resp = (char *)calloc(PAM_MAX_RESP_SIZE,
1627 			    sizeof (char));
1628 			if (r->resp == NULL) {
1629 				/* free responses */
1630 				r = *response;
1631 				for (i = 0; i < num_msg; i++, r++) {
1632 					if (r->resp)
1633 						free(r->resp);
1634 				}
1635 				free(*response);
1636 				*response = NULL;
1637 				return (PAM_BUF_ERR);
1638 			}
1639 			if (fgets(r->resp, PAM_MAX_RESP_SIZE-1, stdin)) {
1640 				int len = strlen(r->resp);
1641 				if (r->resp[len-1] == '\n')
1642 					r->resp[len-1] = '\0';
1643 			}
1644 			m++;
1645 			r++;
1646 			break;
1647 
1648 		case PAM_ERROR_MSG:
1649 			if (m->msg != NULL) {
1650 				(void) fputs(m->msg, stderr);
1651 				(void) fputs("\n", stderr);
1652 			}
1653 			m++;
1654 			r++;
1655 			break;
1656 		case PAM_TEXT_INFO:
1657 			if (m->msg != NULL) {
1658 				(void) fputs(m->msg, stdout);
1659 				(void) fputs("\n", stdout);
1660 			}
1661 			m++;
1662 			r++;
1663 			break;
1664 
1665 		default:
1666 			break;
1667 		}
1668 	}
1669 	return (PAM_SUCCESS);
1670 }
1671 
1672 /*
1673  * 		Utilities Functions
1674  */
1675 
1676 /*
1677  * int attrlist_add(attrlist **l, attrtype type, char *val)
1678  * add an item, with type "type" and value "val", at the tail of list l.
1679  * This functions exits the application on OutOfMem error.
1680  */
1681 void
1682 attrlist_add(attrlist **l, attrtype type, char *val)
1683 {
1684 	attrlist **w;
1685 
1686 	/* tail insert */
1687 	for (w = l; *w != NULL; w = &(*w)->next)
1688 		;
1689 
1690 	if ((*w = malloc(sizeof (**w))) == NULL)
1691 		passwd_exit(NOMEM);
1692 
1693 	(*w)->type = type;
1694 	(*w)->next = NULL;
1695 
1696 	switch (type) {
1697 	case ATTR_MIN:
1698 	case ATTR_WARN:
1699 	case ATTR_MAX:
1700 		(*w)->data.val_i = atoi(val);
1701 		break;
1702 	default:
1703 		(*w)->data.val_s = val;
1704 		break;
1705 	}
1706 }
1707 
1708 /*
1709  * attrlist_reorder(attrlist **l)
1710  * Make sure that
1711  * 	- if EXPIRE and MAX or MIN is set, EXPIRE comes after MAX/MIN
1712  *	- if both MIN and MAX are set, MAX comes before MIN.
1713  */
1714 
1715 static void
1716 attrlist_reorder(attrlist **l)
1717 {
1718 	attrlist	**w;
1719 	attrlist	*exp = NULL;	/* ATTR_EXPIRE_PASSWORD, if found */
1720 	attrlist	*max = NULL;	/* ATTR_MAX, if found */
1721 
1722 	if (*l == NULL || (*l)->next == NULL)
1723 		return;		/* order of list with <= one item is ok */
1724 
1725 	/*
1726 	 * We simply walk the list, take off the EXPIRE and MAX items if
1727 	 * they appear, and put them (first MAX, them EXPIRE) at the end
1728 	 * of the list.
1729 	 */
1730 	w = l;
1731 	while (*w != NULL) {
1732 		if ((*w)->type == ATTR_EXPIRE_PASSWORD) {
1733 			exp = *w;
1734 			*w = (*w)->next;
1735 		} else if ((*w)->type == ATTR_MAX) {
1736 			max = *w;
1737 			*w = (*w)->next;
1738 		} else
1739 			w = &(*w)->next;
1740 	}
1741 
1742 	/* 'w' points to the address of the 'next' field of the last element */
1743 
1744 	if (max) {
1745 		*w = max;
1746 		w = &max->next;
1747 	}
1748 	if (exp) {
1749 		*w = exp;
1750 		w = &exp->next;
1751 	}
1752 	*w = NULL;
1753 }
1754 
1755 void
1756 rusage(void)
1757 {
1758 
1759 #define	MSG(a) (void) fprintf(stderr, gettext((a)));
1760 
1761 	MSG("usage:\n");
1762 	MSG("\tpasswd [-r files | -r nis | -r ldap] [name]\n");
1763 	MSG("\tpasswd [-r files] [-egh] [name]\n");
1764 	MSG("\tpasswd [-r files] -sa\n");
1765 	MSG("\tpasswd [-r files] -s [name]\n");
1766 	MSG("\tpasswd [-r files] [-d|-l|-N|-u] [-f] [-n min] [-w warn] "
1767 	    "[-x max] name\n");
1768 	MSG("\tpasswd -r nis [-eg] [name]\n");
1769 	MSG("\t\t[-x max] name\n");
1770 	MSG("\tpasswd -r ldap [-egh] [name]\n");
1771 	MSG("\tpasswd -r ldap -sa\n");
1772 	MSG("\tpasswd -r ldap -s [name]\n");
1773 	MSG("\tpasswd -r ldap [-l|-N|-u] [-f] [-n min] [-w warn] "
1774 	    "[-x max] name\n");
1775 #undef MSG
1776 }
1777