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