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