xref: /illumos-gate/usr/src/cmd/oamuser/user/userdefs.c (revision 9e678d63)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
2449335bdeSbasabi  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
2849335bdeSbasabi /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29*9e678d63SAndy Fiddaman /*	  All Rights Reserved	*/
3049335bdeSbasabi 
31a7fe1d5bSAndy Stormont /*
32a7fe1d5bSAndy Stormont  * Copyright (c) 2013 RackTop Systems.
33*9e678d63SAndy Fiddaman  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
34a7fe1d5bSAndy Stormont  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include	<sys/types.h>
377c478bd9Sstevel@tonic-gate #include	<stdio.h>
387c478bd9Sstevel@tonic-gate #include	<string.h>
397c478bd9Sstevel@tonic-gate #include	<userdefs.h>
407c478bd9Sstevel@tonic-gate #include	<user_attr.h>
417c478bd9Sstevel@tonic-gate #include	<limits.h>
427c478bd9Sstevel@tonic-gate #include	<stdlib.h>
437c478bd9Sstevel@tonic-gate #include	<stddef.h>
447c478bd9Sstevel@tonic-gate #include	<time.h>
457c478bd9Sstevel@tonic-gate #include	<unistd.h>
467c478bd9Sstevel@tonic-gate #include	"userdisp.h"
477c478bd9Sstevel@tonic-gate #include	"funcs.h"
487c478bd9Sstevel@tonic-gate #include	"messages.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* Print out a NL when the line gets too long */
517c478bd9Sstevel@tonic-gate #define	PRINTNL()	\
527c478bd9Sstevel@tonic-gate 	if (outcount > 40) { \
537c478bd9Sstevel@tonic-gate 		outcount = 0; \
547c478bd9Sstevel@tonic-gate 		(void) fprintf(fptr, "\n"); \
557c478bd9Sstevel@tonic-gate 	}
567c478bd9Sstevel@tonic-gate 
57*9e678d63SAndy Fiddaman #define	SKIPWS(ptr)	while (*(ptr) == ' ' || *(ptr) == '\t') (ptr)++
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static char *dup_to_nl(char *);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static struct userdefs defaults = {
627c478bd9Sstevel@tonic-gate 	DEFRID, DEFGROUP, DEFGNAME, DEFPARENT, DEFSKL,
637c478bd9Sstevel@tonic-gate 	DEFSHL, DEFINACT, DEFEXPIRE, DEFAUTH, DEFPROF,
647c478bd9Sstevel@tonic-gate 	DEFROLE, DEFPROJ, DEFPROJNAME, DEFLIMPRIV,
65*9e678d63SAndy Fiddaman 	DEFDFLTPRIV, DEFLOCK_AFTER_RETRIES, DEFROLEAUTH
667c478bd9Sstevel@tonic-gate };
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	INT	0
697c478bd9Sstevel@tonic-gate #define	STR	1
707c478bd9Sstevel@tonic-gate #define	PROJID	2
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	DEFOFF(field)		offsetof(struct userdefs, field)
737c478bd9Sstevel@tonic-gate #define	FIELD(up, pe, type)	(*(type *)((char *)(up) + (pe)->off))
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate typedef struct parsent {
767c478bd9Sstevel@tonic-gate 	const char *name;	/* deffoo= */
777c478bd9Sstevel@tonic-gate 	const size_t nmsz;	/* length of def= string (excluding \0) */
787c478bd9Sstevel@tonic-gate 	const int type;		/* type of entry */
797c478bd9Sstevel@tonic-gate 	const ptrdiff_t off;	/* offset in userdefs structure */
807c478bd9Sstevel@tonic-gate 	const char *uakey;	/* user_attr key, if defined */
817c478bd9Sstevel@tonic-gate } parsent_t;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static const parsent_t tab[] = {
847c478bd9Sstevel@tonic-gate 	{ GIDSTR,	sizeof (GIDSTR) - 1,	INT,	DEFOFF(defgroup) },
857c478bd9Sstevel@tonic-gate 	{ GNAMSTR,	sizeof (GNAMSTR) - 1,	STR,	DEFOFF(defgname) },
867c478bd9Sstevel@tonic-gate 	{ PARSTR,	sizeof (PARSTR) - 1,	STR,	DEFOFF(defparent) },
877c478bd9Sstevel@tonic-gate 	{ SKLSTR,	sizeof (SKLSTR) - 1,	STR,	DEFOFF(defskel) },
887c478bd9Sstevel@tonic-gate 	{ SHELLSTR,	sizeof (SHELLSTR) - 1,	STR,	DEFOFF(defshell) },
897c478bd9Sstevel@tonic-gate 	{ INACTSTR,	sizeof (INACTSTR) - 1,	INT,	DEFOFF(definact) },
907c478bd9Sstevel@tonic-gate 	{ EXPIRESTR,	sizeof (EXPIRESTR) - 1,	STR,	DEFOFF(defexpire) },
917c478bd9Sstevel@tonic-gate 	{ AUTHSTR,	sizeof (AUTHSTR) - 1,	STR,	DEFOFF(defauth),
927c478bd9Sstevel@tonic-gate 		USERATTR_AUTHS_KW },
937c478bd9Sstevel@tonic-gate 	{ ROLESTR,	sizeof (ROLESTR) - 1,	STR,	DEFOFF(defrole),
947c478bd9Sstevel@tonic-gate 		USERATTR_ROLES_KW },
957c478bd9Sstevel@tonic-gate 	{ PROFSTR,	sizeof (PROFSTR) - 1,	STR,	DEFOFF(defprof),
967c478bd9Sstevel@tonic-gate 		USERATTR_PROFILES_KW },
977c478bd9Sstevel@tonic-gate 	{ PROJSTR,	sizeof (PROJSTR) - 1,	PROJID,	DEFOFF(defproj) },
987c478bd9Sstevel@tonic-gate 	{ PROJNMSTR,	sizeof (PROJNMSTR) - 1,	STR,	DEFOFF(defprojname) },
997c478bd9Sstevel@tonic-gate 	{ LIMPRSTR,	sizeof (LIMPRSTR) - 1,	STR,	DEFOFF(deflimpriv),
1007c478bd9Sstevel@tonic-gate 		USERATTR_LIMPRIV_KW },
1017c478bd9Sstevel@tonic-gate 	{ DFLTPRSTR,	sizeof (DFLTPRSTR) - 1,	STR,	DEFOFF(defdfltpriv),
1027c478bd9Sstevel@tonic-gate 		USERATTR_DFLTPRIV_KW },
1037c478bd9Sstevel@tonic-gate 	{ LOCK_AFTER_RETRIESSTR,	sizeof (LOCK_AFTER_RETRIESSTR) - 1,
1047c478bd9Sstevel@tonic-gate 		STR,	DEFOFF(deflock_after_retries),
1057c478bd9Sstevel@tonic-gate 		USERATTR_LOCK_AFTER_RETRIES_KW },
106*9e678d63SAndy Fiddaman 	{ ROLEAUTHSTR,	sizeof (ROLEAUTHSTR) - 1, STR,	DEFOFF(defroleauth),
107*9e678d63SAndy Fiddaman 		USERATTR_ROLEAUTH_KW },
1087c478bd9Sstevel@tonic-gate };
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #define	NDEF	(sizeof (tab) / sizeof (parsent_t))
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate FILE *defptr;		/* default file - fptr */
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static const parsent_t *
scan(char ** start_p)1157c478bd9Sstevel@tonic-gate scan(char **start_p)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	static int ind = NDEF - 1;
1187c478bd9Sstevel@tonic-gate 	char *cur_p = *start_p;
1197c478bd9Sstevel@tonic-gate 	int lastind = ind;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (!*cur_p || *cur_p == '\n' || *cur_p == '#')
1227c478bd9Sstevel@tonic-gate 		return (NULL);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	/*
1257c478bd9Sstevel@tonic-gate 	 * The magic in this loop is remembering the last index when
1267c478bd9Sstevel@tonic-gate 	 * reentering the function; the entries above are also used to
1277c478bd9Sstevel@tonic-gate 	 * order the output to the default file.
1287c478bd9Sstevel@tonic-gate 	 */
1297c478bd9Sstevel@tonic-gate 	do {
1307c478bd9Sstevel@tonic-gate 		ind++;
1317c478bd9Sstevel@tonic-gate 		ind %= NDEF;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		if (strncmp(cur_p, tab[ind].name, tab[ind].nmsz) == 0) {
1347c478bd9Sstevel@tonic-gate 			*start_p = cur_p + tab[ind].nmsz;
1357c478bd9Sstevel@tonic-gate 			return (&tab[ind]);
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 	} while (ind != lastind);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	return (NULL);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * getusrdef - access the user defaults file.  If it doesn't exist,
1447c478bd9Sstevel@tonic-gate  *		then returns default values of (values in userdefs.h):
1457c478bd9Sstevel@tonic-gate  *		defrid = 100
1467c478bd9Sstevel@tonic-gate  *		defgroup = 1
1477c478bd9Sstevel@tonic-gate  *		defgname = other
1487c478bd9Sstevel@tonic-gate  *		defparent = /home
1497c478bd9Sstevel@tonic-gate  *		defskel	= /usr/sadm/skel
1507c478bd9Sstevel@tonic-gate  *		defshell = /bin/sh
1517c478bd9Sstevel@tonic-gate  *		definact = 0
1527c478bd9Sstevel@tonic-gate  *		defexpire = 0
1537c478bd9Sstevel@tonic-gate  *		defauth = 0
1547c478bd9Sstevel@tonic-gate  *		defprof = 0
1557c478bd9Sstevel@tonic-gate  *		defrole = 0
156*9e678d63SAndy Fiddaman  *		defroleauth = role
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  *	If getusrdef() is unable to access the defaults file, it
1597c478bd9Sstevel@tonic-gate  *	returns a NULL pointer.
1607c478bd9Sstevel@tonic-gate  *
161*9e678d63SAndy Fiddaman  *	If user defaults file exists, then getusrdef uses values
162*9e678d63SAndy Fiddaman  *	in it to override the above values.
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate struct userdefs *
getusrdef(char * usertype)1667c478bd9Sstevel@tonic-gate getusrdef(char *usertype)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	char instr[512], *ptr;
1697c478bd9Sstevel@tonic-gate 	const parsent_t *pe;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	if (is_role(usertype)) {
1727c478bd9Sstevel@tonic-gate 		if ((defptr = fopen(DEFROLEFILE, "r")) == NULL) {
1737c478bd9Sstevel@tonic-gate 			defaults.defshell = DEFROLESHL;
1747c478bd9Sstevel@tonic-gate 			defaults.defprof = DEFROLEPROF;
175*9e678d63SAndy Fiddaman 			defaults.defroleauth = DEFROLEROLEAUTH;
1767c478bd9Sstevel@tonic-gate 			return (&defaults);
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 	} else {
1797c478bd9Sstevel@tonic-gate 		if ((defptr = fopen(DEFFILE, "r")) == NULL)
1807c478bd9Sstevel@tonic-gate 			return (&defaults);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	while (fgets(instr, sizeof (instr), defptr) != NULL) {
1847c478bd9Sstevel@tonic-gate 		ptr = instr;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		SKIPWS(ptr);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		if (*ptr == '#')
1897c478bd9Sstevel@tonic-gate 			continue;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		pe = scan(&ptr);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		if (pe != NULL) {
1947c478bd9Sstevel@tonic-gate 			switch (pe->type) {
1957c478bd9Sstevel@tonic-gate 			case INT:
1967c478bd9Sstevel@tonic-gate 				FIELD(&defaults, pe, int) =
197*9e678d63SAndy Fiddaman 				    (int)strtol(ptr, NULL, 10);
1987c478bd9Sstevel@tonic-gate 				break;
1997c478bd9Sstevel@tonic-gate 			case PROJID:
2007c478bd9Sstevel@tonic-gate 				FIELD(&defaults, pe, projid_t) =
201*9e678d63SAndy Fiddaman 				    (projid_t)strtol(ptr, NULL, 10);
2027c478bd9Sstevel@tonic-gate 				break;
2037c478bd9Sstevel@tonic-gate 			case STR:
2047c478bd9Sstevel@tonic-gate 				FIELD(&defaults, pe, char *) = dup_to_nl(ptr);
2057c478bd9Sstevel@tonic-gate 				break;
2067c478bd9Sstevel@tonic-gate 			}
2077c478bd9Sstevel@tonic-gate 		}
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	(void) fclose(defptr);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	return (&defaults);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate static char *
dup_to_nl(char * from)2167c478bd9Sstevel@tonic-gate dup_to_nl(char *from)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	char *res = strdup(from);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	char *p = strchr(res, '\n');
2217c478bd9Sstevel@tonic-gate 	if (p)
2227c478bd9Sstevel@tonic-gate 		*p = '\0';
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	return (res);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate void
dispusrdef(FILE * fptr,unsigned flags,char * usertype)22849335bdeSbasabi dispusrdef(FILE *fptr, unsigned flags, char *usertype)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	struct userdefs *deflts = getusrdef(usertype);
23149335bdeSbasabi 	int outcount = 0;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/* Print out values */
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (flags & D_GROUP) {
2367c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "group=%s,%ld  ",
237*9e678d63SAndy Fiddaman 		    deflts->defgname, deflts->defgroup);
2387c478bd9Sstevel@tonic-gate 		PRINTNL();
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (flags & D_PROJ) {
2427c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "project=%s,%ld  ",
2437c478bd9Sstevel@tonic-gate 		    deflts->defprojname, deflts->defproj);
2447c478bd9Sstevel@tonic-gate 		PRINTNL();
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if (flags & D_BASEDIR) {
2487c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "basedir=%s  ", deflts->defparent);
2497c478bd9Sstevel@tonic-gate 		PRINTNL();
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (flags & D_RID) {
2537c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "rid=%ld  ", deflts->defrid);
2547c478bd9Sstevel@tonic-gate 		PRINTNL();
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (flags & D_SKEL) {
2587c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "skel=%s  ", deflts->defskel);
2597c478bd9Sstevel@tonic-gate 		PRINTNL();
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	if (flags & D_SHELL) {
2637c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "shell=%s  ", deflts->defshell);
2647c478bd9Sstevel@tonic-gate 		PRINTNL();
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	if (flags & D_INACT) {
2687c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "inactive=%d  ", deflts->definact);
2697c478bd9Sstevel@tonic-gate 		PRINTNL();
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if (flags & D_EXPIRE) {
2737c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "expire=%s  ", deflts->defexpire);
2747c478bd9Sstevel@tonic-gate 		PRINTNL();
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if (flags & D_AUTH) {
2787c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "auths=%s  ", deflts->defauth);
2797c478bd9Sstevel@tonic-gate 		PRINTNL();
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	if (flags & D_PROF) {
2837c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "profiles=%s  ", deflts->defprof);
2847c478bd9Sstevel@tonic-gate 		PRINTNL();
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	if ((flags & D_ROLE) &&
2887c478bd9Sstevel@tonic-gate 	    (!is_role(usertype))) {
2897c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "roles=%s  ", deflts->defrole);
2907c478bd9Sstevel@tonic-gate 		PRINTNL();
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	if (flags & D_LPRIV) {
294*9e678d63SAndy Fiddaman 		outcount += fprintf(fptr, "limitpriv=%s  ", deflts->deflimpriv);
2957c478bd9Sstevel@tonic-gate 		PRINTNL();
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (flags & D_DPRIV) {
2997c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "defaultpriv=%s  ",
300*9e678d63SAndy Fiddaman 		    deflts->defdfltpriv);
3017c478bd9Sstevel@tonic-gate 		PRINTNL();
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	if (flags & D_LOCK) {
3057c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "lock_after_retries=%s  ",
3067c478bd9Sstevel@tonic-gate 		    deflts->deflock_after_retries);
307*9e678d63SAndy Fiddaman 		PRINTNL();
308*9e678d63SAndy Fiddaman 	}
309*9e678d63SAndy Fiddaman 
310*9e678d63SAndy Fiddaman 	if ((flags & D_ROLEAUTH) && is_role(usertype)) {
311*9e678d63SAndy Fiddaman 		outcount += fprintf(fptr, "roleauth=%s  ",
312*9e678d63SAndy Fiddaman 		    deflts->defroleauth);
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if (outcount > 0)
3167c478bd9Sstevel@tonic-gate 		(void) fprintf(fptr, "\n");
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate  * putusrdef -
321*9e678d63SAndy Fiddaman  *	changes default values in defadduser file
3227c478bd9Sstevel@tonic-gate  */
3237c478bd9Sstevel@tonic-gate int
putusrdef(struct userdefs * defs,char * usertype)3247c478bd9Sstevel@tonic-gate putusrdef(struct userdefs *defs, char *usertype)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	time_t timeval;		/* time value from time */
3277c478bd9Sstevel@tonic-gate 	int i;
3287c478bd9Sstevel@tonic-gate 	ptrdiff_t skip;
3297c478bd9Sstevel@tonic-gate 	char *hdr;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/*
3327c478bd9Sstevel@tonic-gate 	 * file format is:
3337c478bd9Sstevel@tonic-gate 	 * #<tab>Default values for adduser.  Changed mm/dd/yy hh:mm:ss.
3347c478bd9Sstevel@tonic-gate 	 * defgroup=m	(m=default group id)
3357c478bd9Sstevel@tonic-gate 	 * defgname=str1	(str1=default group name)
3367c478bd9Sstevel@tonic-gate 	 * defparent=str2	(str2=default base directory)
3377c478bd9Sstevel@tonic-gate 	 * definactive=x	(x=default inactive)
3387c478bd9Sstevel@tonic-gate 	 * defexpire=y		(y=default expire)
3397c478bd9Sstevel@tonic-gate 	 * defproj=z		(z=numeric project id)
3407c478bd9Sstevel@tonic-gate 	 * defprojname=str3	(str3=default project name)
3417c478bd9Sstevel@tonic-gate 	 * ... etc ...
3427c478bd9Sstevel@tonic-gate 	 */
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (is_role(usertype)) {
3457c478bd9Sstevel@tonic-gate 		if ((defptr = fopen(DEFROLEFILE, "w")) == NULL) {
3467c478bd9Sstevel@tonic-gate 			errmsg(M_FAILED);
3477c478bd9Sstevel@tonic-gate 			return (EX_UPDATE);
3487c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 	} else {
3507c478bd9Sstevel@tonic-gate 		if ((defptr = fopen(DEFFILE, "w")) == NULL) {
3517c478bd9Sstevel@tonic-gate 			errmsg(M_FAILED);
3527c478bd9Sstevel@tonic-gate 			return (EX_UPDATE);
3537c478bd9Sstevel@tonic-gate 		}
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if (lockf(fileno(defptr), F_LOCK, 0) != 0) {
3577c478bd9Sstevel@tonic-gate 		/* print error if can't lock whole of DEFFILE */
3587c478bd9Sstevel@tonic-gate 		errmsg(M_UPDATE, "created");
3597c478bd9Sstevel@tonic-gate 		return (EX_UPDATE);
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	if (is_role(usertype)) {
3637c478bd9Sstevel@tonic-gate 		/* If it's a role, we must skip the defrole field */
364*9e678d63SAndy Fiddaman 		skip = DEFOFF(defrole);
3657c478bd9Sstevel@tonic-gate 		hdr = FHEADER_ROLE;
3667c478bd9Sstevel@tonic-gate 	} else {
367*9e678d63SAndy Fiddaman 		/* If it's a user, we must skip the defroleauth field */
368*9e678d63SAndy Fiddaman 		skip = DEFOFF(defroleauth);
3697c478bd9Sstevel@tonic-gate 		hdr = FHEADER;
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	/* get time */
3737c478bd9Sstevel@tonic-gate 	timeval = time(NULL);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	/* write it to file */
3767c478bd9Sstevel@tonic-gate 	if (fprintf(defptr, "%s%s\n", hdr, ctime(&timeval)) <= 0) {
3777c478bd9Sstevel@tonic-gate 		errmsg(M_UPDATE, "created");
3787c478bd9Sstevel@tonic-gate 		return (EX_UPDATE);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	for (i = 0; i < NDEF; i++) {
3827c478bd9Sstevel@tonic-gate 		int res = 0;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 		if (tab[i].off == skip)
3857c478bd9Sstevel@tonic-gate 			continue;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 		switch (tab[i].type) {
3887c478bd9Sstevel@tonic-gate 		case INT:
3897c478bd9Sstevel@tonic-gate 			res = fprintf(defptr, "%s%d\n", tab[i].name,
390*9e678d63SAndy Fiddaman 			    FIELD(defs, &tab[i], int));
3917c478bd9Sstevel@tonic-gate 			break;
3927c478bd9Sstevel@tonic-gate 		case STR:
3937c478bd9Sstevel@tonic-gate 			res = fprintf(defptr, "%s%s\n", tab[i].name,
394*9e678d63SAndy Fiddaman 			    FIELD(defs, &tab[i], char *));
3957c478bd9Sstevel@tonic-gate 			break;
3967c478bd9Sstevel@tonic-gate 		case PROJID:
3977c478bd9Sstevel@tonic-gate 			res = fprintf(defptr, "%s%d\n", tab[i].name,
398*9e678d63SAndy Fiddaman 			    (int)FIELD(defs, &tab[i], projid_t));
3997c478bd9Sstevel@tonic-gate 			break;
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		if (res <= 0) {
4037c478bd9Sstevel@tonic-gate 			errmsg(M_UPDATE, "created");
4047c478bd9Sstevel@tonic-gate 			return (EX_UPDATE);
4057c478bd9Sstevel@tonic-gate 		}
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	(void) lockf(fileno(defptr), F_ULOCK, 0);
4097c478bd9Sstevel@tonic-gate 	(void) fclose(defptr);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	return (EX_SUCCESS);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /* Export command line keys to defaults for useradd -D */
4157c478bd9Sstevel@tonic-gate void
update_def(struct userdefs * ud)4167c478bd9Sstevel@tonic-gate update_def(struct userdefs *ud)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	int i;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	for (i = 0; i < NDEF; i++) {
4217c478bd9Sstevel@tonic-gate 		char *val;
4227c478bd9Sstevel@tonic-gate 		if (tab[i].uakey != NULL &&
4237c478bd9Sstevel@tonic-gate 		    (val = getsetdefval(tab[i].uakey, NULL)) != NULL)
4247c478bd9Sstevel@tonic-gate 			FIELD(ud, &tab[i], char *) = val;
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate /* Import default keys for ordinary useradd */
4297c478bd9Sstevel@tonic-gate void
import_def(struct userdefs * ud)4307c478bd9Sstevel@tonic-gate import_def(struct userdefs *ud)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 	int i;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	for (i = 0; i < NDEF; i++) {
4357c478bd9Sstevel@tonic-gate 		if (tab[i].uakey != NULL && tab[i].type == STR) {
4367c478bd9Sstevel@tonic-gate 			char *val = FIELD(ud, &tab[i], char *);
4377c478bd9Sstevel@tonic-gate 			if (val == getsetdefval(tab[i].uakey, val))
4387c478bd9Sstevel@tonic-gate 				nkeys ++;
4397c478bd9Sstevel@tonic-gate 		}
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate }
442