xref: /illumos-gate/usr/src/cmd/oamuser/user/userdefs.c (revision 49335bde)
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 /*
24*49335bdeSbasabi  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
28*49335bdeSbasabi /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29*49335bdeSbasabi /*	  All Rights Reserved  	*/
30*49335bdeSbasabi 
317c478bd9Sstevel@tonic-gate 
32*49335bdeSbasabi #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
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 
577c478bd9Sstevel@tonic-gate #define	SKIPWS(ptr)	while (*ptr && *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,
657c478bd9Sstevel@tonic-gate 	DEFDFLTPRIV, DEFLOCK_AFTER_RETRIES
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 },
1067c478bd9Sstevel@tonic-gate };
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #define	NDEF	(sizeof (tab) / sizeof (parsent_t))
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate FILE *defptr;		/* default file - fptr */
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static const parsent_t *
1137c478bd9Sstevel@tonic-gate scan(char **start_p)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	static int ind = NDEF - 1;
1167c478bd9Sstevel@tonic-gate 	char *cur_p = *start_p;
1177c478bd9Sstevel@tonic-gate 	int lastind = ind;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (!*cur_p || *cur_p == '\n' || *cur_p == '#')
1207c478bd9Sstevel@tonic-gate 		return (NULL);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	/*
1237c478bd9Sstevel@tonic-gate 	 * The magic in this loop is remembering the last index when
1247c478bd9Sstevel@tonic-gate 	 * reentering the function; the entries above are also used to
1257c478bd9Sstevel@tonic-gate 	 * order the output to the default file.
1267c478bd9Sstevel@tonic-gate 	 */
1277c478bd9Sstevel@tonic-gate 	do {
1287c478bd9Sstevel@tonic-gate 		ind++;
1297c478bd9Sstevel@tonic-gate 		ind %= NDEF;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 		if (strncmp(cur_p, tab[ind].name, tab[ind].nmsz) == 0) {
1327c478bd9Sstevel@tonic-gate 			*start_p = cur_p + tab[ind].nmsz;
1337c478bd9Sstevel@tonic-gate 			return (&tab[ind]);
1347c478bd9Sstevel@tonic-gate 		}
1357c478bd9Sstevel@tonic-gate 	} while (ind != lastind);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	return (NULL);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * getusrdef - access the user defaults file.  If it doesn't exist,
1427c478bd9Sstevel@tonic-gate  *		then returns default values of (values in userdefs.h):
1437c478bd9Sstevel@tonic-gate  *		defrid = 100
1447c478bd9Sstevel@tonic-gate  *		defgroup = 1
1457c478bd9Sstevel@tonic-gate  *		defgname = other
1467c478bd9Sstevel@tonic-gate  *		defparent = /home
1477c478bd9Sstevel@tonic-gate  *		defskel	= /usr/sadm/skel
1487c478bd9Sstevel@tonic-gate  *		defshell = /bin/sh
1497c478bd9Sstevel@tonic-gate  *		definact = 0
1507c478bd9Sstevel@tonic-gate  *		defexpire = 0
1517c478bd9Sstevel@tonic-gate  *		defauth = 0
1527c478bd9Sstevel@tonic-gate  *		defprof = 0
1537c478bd9Sstevel@tonic-gate  *		defrole = 0
1547c478bd9Sstevel@tonic-gate  *
1557c478bd9Sstevel@tonic-gate  *	If getusrdef() is unable to access the defaults file, it
1567c478bd9Sstevel@tonic-gate  *	returns a NULL pointer.
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  * 	If user defaults file exists, then getusrdef uses values
1597c478bd9Sstevel@tonic-gate  *  in it to override the above values.
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate struct userdefs *
1637c478bd9Sstevel@tonic-gate getusrdef(char *usertype)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	char instr[512], *ptr;
1667c478bd9Sstevel@tonic-gate 	const parsent_t *pe;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if (is_role(usertype)) {
1697c478bd9Sstevel@tonic-gate 		if ((defptr = fopen(DEFROLEFILE, "r")) == NULL) {
1707c478bd9Sstevel@tonic-gate 			defaults.defshell = DEFROLESHL;
1717c478bd9Sstevel@tonic-gate 			defaults.defprof = DEFROLEPROF;
1727c478bd9Sstevel@tonic-gate 			return (&defaults);
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 	} else {
1757c478bd9Sstevel@tonic-gate 		if ((defptr = fopen(DEFFILE, "r")) == NULL)
1767c478bd9Sstevel@tonic-gate 			return (&defaults);
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	while (fgets(instr, sizeof (instr), defptr) != NULL) {
1807c478bd9Sstevel@tonic-gate 		ptr = instr;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		SKIPWS(ptr);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		if (*ptr == '#')
1857c478bd9Sstevel@tonic-gate 			continue;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 		pe = scan(&ptr);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		if (pe != NULL) {
1907c478bd9Sstevel@tonic-gate 			switch (pe->type) {
1917c478bd9Sstevel@tonic-gate 			case INT:
1927c478bd9Sstevel@tonic-gate 				FIELD(&defaults, pe, int) =
1937c478bd9Sstevel@tonic-gate 					(int)strtol(ptr, NULL, 10);
1947c478bd9Sstevel@tonic-gate 				break;
1957c478bd9Sstevel@tonic-gate 			case PROJID:
1967c478bd9Sstevel@tonic-gate 				FIELD(&defaults, pe, projid_t) =
1977c478bd9Sstevel@tonic-gate 					(projid_t)strtol(ptr, NULL, 10);
1987c478bd9Sstevel@tonic-gate 				break;
1997c478bd9Sstevel@tonic-gate 			case STR:
2007c478bd9Sstevel@tonic-gate 				FIELD(&defaults, pe, char *) = dup_to_nl(ptr);
2017c478bd9Sstevel@tonic-gate 				break;
2027c478bd9Sstevel@tonic-gate 			}
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	(void) fclose(defptr);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	return (&defaults);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static char *
2127c478bd9Sstevel@tonic-gate dup_to_nl(char *from)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	char *res = strdup(from);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	char *p = strchr(res, '\n');
2177c478bd9Sstevel@tonic-gate 	if (p)
2187c478bd9Sstevel@tonic-gate 		*p = '\0';
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	return (res);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate void
224*49335bdeSbasabi dispusrdef(FILE *fptr, unsigned flags, char *usertype)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	struct userdefs *deflts = getusrdef(usertype);
227*49335bdeSbasabi 	int outcount = 0;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/* Print out values */
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if (flags & D_GROUP) {
2327c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "group=%s,%ld  ",
2337c478bd9Sstevel@tonic-gate 			deflts->defgname, deflts->defgroup);
2347c478bd9Sstevel@tonic-gate 		PRINTNL();
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (flags & D_PROJ) {
2387c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "project=%s,%ld  ",
2397c478bd9Sstevel@tonic-gate 		    deflts->defprojname, deflts->defproj);
2407c478bd9Sstevel@tonic-gate 		PRINTNL();
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	if (flags & D_BASEDIR) {
2447c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "basedir=%s  ", deflts->defparent);
2457c478bd9Sstevel@tonic-gate 		PRINTNL();
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (flags & D_RID) {
2497c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "rid=%ld  ", deflts->defrid);
2507c478bd9Sstevel@tonic-gate 		PRINTNL();
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	if (flags & D_SKEL) {
2547c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "skel=%s  ", deflts->defskel);
2557c478bd9Sstevel@tonic-gate 		PRINTNL();
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (flags & D_SHELL) {
2597c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "shell=%s  ", deflts->defshell);
2607c478bd9Sstevel@tonic-gate 		PRINTNL();
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if (flags & D_INACT) {
2647c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "inactive=%d  ", deflts->definact);
2657c478bd9Sstevel@tonic-gate 		PRINTNL();
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if (flags & D_EXPIRE) {
2697c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "expire=%s  ", deflts->defexpire);
2707c478bd9Sstevel@tonic-gate 		PRINTNL();
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (flags & D_AUTH) {
2747c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "auths=%s  ", deflts->defauth);
2757c478bd9Sstevel@tonic-gate 		PRINTNL();
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	if (flags & D_PROF) {
2797c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "profiles=%s  ", deflts->defprof);
2807c478bd9Sstevel@tonic-gate 		PRINTNL();
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if ((flags & D_ROLE) &&
2847c478bd9Sstevel@tonic-gate 	    (!is_role(usertype))) {
2857c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "roles=%s  ", deflts->defrole);
2867c478bd9Sstevel@tonic-gate 		PRINTNL();
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	if (flags & D_LPRIV) {
2907c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "limitpriv=%s  ",
2917c478bd9Sstevel@tonic-gate 			deflts->deflimpriv);
2927c478bd9Sstevel@tonic-gate 		PRINTNL();
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if (flags & D_DPRIV) {
2967c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "defaultpriv=%s  ",
2977c478bd9Sstevel@tonic-gate 			deflts->defdfltpriv);
2987c478bd9Sstevel@tonic-gate 		PRINTNL();
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (flags & D_LOCK) {
3027c478bd9Sstevel@tonic-gate 		outcount += fprintf(fptr, "lock_after_retries=%s  ",
3037c478bd9Sstevel@tonic-gate 		    deflts->deflock_after_retries);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (outcount > 0)
3077c478bd9Sstevel@tonic-gate 		(void) fprintf(fptr, "\n");
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate /*
3117c478bd9Sstevel@tonic-gate  * putusrdef -
3127c478bd9Sstevel@tonic-gate  * 	changes default values in defadduser file
3137c478bd9Sstevel@tonic-gate  */
3147c478bd9Sstevel@tonic-gate int
3157c478bd9Sstevel@tonic-gate putusrdef(struct userdefs *defs, char *usertype)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	time_t timeval;		/* time value from time */
3187c478bd9Sstevel@tonic-gate 	int i;
3197c478bd9Sstevel@tonic-gate 	ptrdiff_t skip;
3207c478bd9Sstevel@tonic-gate 	char *hdr;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/*
3237c478bd9Sstevel@tonic-gate 	 * file format is:
3247c478bd9Sstevel@tonic-gate 	 * #<tab>Default values for adduser.  Changed mm/dd/yy hh:mm:ss.
3257c478bd9Sstevel@tonic-gate 	 * defgroup=m	(m=default group id)
3267c478bd9Sstevel@tonic-gate 	 * defgname=str1	(str1=default group name)
3277c478bd9Sstevel@tonic-gate 	 * defparent=str2	(str2=default base directory)
3287c478bd9Sstevel@tonic-gate 	 * definactive=x	(x=default inactive)
3297c478bd9Sstevel@tonic-gate 	 * defexpire=y		(y=default expire)
3307c478bd9Sstevel@tonic-gate 	 * defproj=z		(z=numeric project id)
3317c478bd9Sstevel@tonic-gate 	 * defprojname=str3	(str3=default project name)
3327c478bd9Sstevel@tonic-gate 	 * ... etc ...
3337c478bd9Sstevel@tonic-gate 	 */
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if (is_role(usertype)) {
3367c478bd9Sstevel@tonic-gate 		if ((defptr = fopen(DEFROLEFILE, "w")) == NULL) {
3377c478bd9Sstevel@tonic-gate 			errmsg(M_FAILED);
3387c478bd9Sstevel@tonic-gate 			return (EX_UPDATE);
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 	} else {
3417c478bd9Sstevel@tonic-gate 		if ((defptr = fopen(DEFFILE, "w")) == NULL) {
3427c478bd9Sstevel@tonic-gate 			errmsg(M_FAILED);
3437c478bd9Sstevel@tonic-gate 			return (EX_UPDATE);
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (lockf(fileno(defptr), F_LOCK, 0) != 0) {
3487c478bd9Sstevel@tonic-gate 		/* print error if can't lock whole of DEFFILE */
3497c478bd9Sstevel@tonic-gate 		errmsg(M_UPDATE, "created");
3507c478bd9Sstevel@tonic-gate 		return (EX_UPDATE);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (is_role(usertype)) {
3547c478bd9Sstevel@tonic-gate 		/* If it's a role, we must skip the defrole field */
3557c478bd9Sstevel@tonic-gate 		skip = offsetof(struct userdefs, defrole);
3567c478bd9Sstevel@tonic-gate 		hdr = FHEADER_ROLE;
3577c478bd9Sstevel@tonic-gate 	} else {
3587c478bd9Sstevel@tonic-gate 		skip = -1;
3597c478bd9Sstevel@tonic-gate 		hdr = FHEADER;
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/* get time */
3637c478bd9Sstevel@tonic-gate 	timeval = time(NULL);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/* write it to file */
3667c478bd9Sstevel@tonic-gate 	if (fprintf(defptr, "%s%s\n", hdr, ctime(&timeval)) <= 0) {
3677c478bd9Sstevel@tonic-gate 		errmsg(M_UPDATE, "created");
3687c478bd9Sstevel@tonic-gate 		return (EX_UPDATE);
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	for (i = 0; i < NDEF; i++) {
3727c478bd9Sstevel@tonic-gate 		int res = 0;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 		if (tab[i].off == skip)
3757c478bd9Sstevel@tonic-gate 			continue;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 		switch (tab[i].type) {
3787c478bd9Sstevel@tonic-gate 		case INT:
3797c478bd9Sstevel@tonic-gate 			res = fprintf(defptr, "%s%d\n", tab[i].name,
3807c478bd9Sstevel@tonic-gate 					FIELD(defs, &tab[i], int));
3817c478bd9Sstevel@tonic-gate 			break;
3827c478bd9Sstevel@tonic-gate 		case STR:
3837c478bd9Sstevel@tonic-gate 			res = fprintf(defptr, "%s%s\n", tab[i].name,
3847c478bd9Sstevel@tonic-gate 					FIELD(defs, &tab[i], char *));
3857c478bd9Sstevel@tonic-gate 			break;
3867c478bd9Sstevel@tonic-gate 		case PROJID:
3877c478bd9Sstevel@tonic-gate 			res = fprintf(defptr, "%s%d\n", tab[i].name,
3887c478bd9Sstevel@tonic-gate 					(int)FIELD(defs, &tab[i], projid_t));
3897c478bd9Sstevel@tonic-gate 			break;
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 		if (res <= 0) {
3937c478bd9Sstevel@tonic-gate 			errmsg(M_UPDATE, "created");
3947c478bd9Sstevel@tonic-gate 			return (EX_UPDATE);
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	(void) lockf(fileno(defptr), F_ULOCK, 0);
3997c478bd9Sstevel@tonic-gate 	(void) fclose(defptr);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	return (EX_SUCCESS);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate /* Export command line keys to defaults for useradd -D */
4057c478bd9Sstevel@tonic-gate void
4067c478bd9Sstevel@tonic-gate update_def(struct userdefs *ud)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	int i;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	for (i = 0; i < NDEF; i++) {
4117c478bd9Sstevel@tonic-gate 		char *val;
4127c478bd9Sstevel@tonic-gate 		if (tab[i].uakey != NULL &&
4137c478bd9Sstevel@tonic-gate 		    (val = getsetdefval(tab[i].uakey, NULL)) != NULL)
4147c478bd9Sstevel@tonic-gate 			FIELD(ud, &tab[i], char *) = val;
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate /* Import default keys for ordinary useradd */
4197c478bd9Sstevel@tonic-gate void
4207c478bd9Sstevel@tonic-gate import_def(struct userdefs *ud)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	int i;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	for (i = 0; i < NDEF; i++) {
4257c478bd9Sstevel@tonic-gate 		if (tab[i].uakey != NULL && tab[i].type == STR) {
4267c478bd9Sstevel@tonic-gate 			char *val = FIELD(ud, &tab[i], char *);
4277c478bd9Sstevel@tonic-gate 			if (val == getsetdefval(tab[i].uakey, val))
4287c478bd9Sstevel@tonic-gate 				nkeys ++;
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate }
432