dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / oamuser / user / userdefs.c
blob1ca3d2d100d040c6e194d7bfbae501db97923a51
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
32 * Copyright (c) 2013 RackTop Systems.
35 /*LINTLIBRARY*/
37 #include <sys/types.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <userdefs.h>
41 #include <user_attr.h>
42 #include <limits.h>
43 #include <stdlib.h>
44 #include <stddef.h>
45 #include <time.h>
46 #include <unistd.h>
47 #include "userdisp.h"
48 #include "funcs.h"
49 #include "messages.h"
51 /* Print out a NL when the line gets too long */
52 #define PRINTNL() \
53 if (outcount > 40) { \
54 outcount = 0; \
55 (void) fprintf(fptr, "\n"); \
58 #define SKIPWS(ptr) while (*ptr && (*ptr == ' ' || *ptr == '\t')) ptr++
60 static char *dup_to_nl(char *);
62 static struct userdefs defaults = {
63 DEFRID, DEFGROUP, DEFGNAME, DEFPARENT, DEFSKL,
64 DEFSHL, DEFINACT, DEFEXPIRE, DEFAUTH, DEFPROF,
65 DEFROLE, DEFPROJ, DEFPROJNAME, DEFLIMPRIV,
66 DEFDFLTPRIV, DEFLOCK_AFTER_RETRIES
69 #define INT 0
70 #define STR 1
71 #define PROJID 2
73 #define DEFOFF(field) offsetof(struct userdefs, field)
74 #define FIELD(up, pe, type) (*(type *)((char *)(up) + (pe)->off))
76 typedef struct parsent {
77 const char *name; /* deffoo= */
78 const size_t nmsz; /* length of def= string (excluding \0) */
79 const int type; /* type of entry */
80 const ptrdiff_t off; /* offset in userdefs structure */
81 const char *uakey; /* user_attr key, if defined */
82 } parsent_t;
84 static const parsent_t tab[] = {
85 { GIDSTR, sizeof (GIDSTR) - 1, INT, DEFOFF(defgroup) },
86 { GNAMSTR, sizeof (GNAMSTR) - 1, STR, DEFOFF(defgname) },
87 { PARSTR, sizeof (PARSTR) - 1, STR, DEFOFF(defparent) },
88 { SKLSTR, sizeof (SKLSTR) - 1, STR, DEFOFF(defskel) },
89 { SHELLSTR, sizeof (SHELLSTR) - 1, STR, DEFOFF(defshell) },
90 { INACTSTR, sizeof (INACTSTR) - 1, INT, DEFOFF(definact) },
91 { EXPIRESTR, sizeof (EXPIRESTR) - 1, STR, DEFOFF(defexpire) },
92 { AUTHSTR, sizeof (AUTHSTR) - 1, STR, DEFOFF(defauth),
93 USERATTR_AUTHS_KW },
94 { ROLESTR, sizeof (ROLESTR) - 1, STR, DEFOFF(defrole),
95 USERATTR_ROLES_KW },
96 { PROFSTR, sizeof (PROFSTR) - 1, STR, DEFOFF(defprof),
97 USERATTR_PROFILES_KW },
98 { PROJSTR, sizeof (PROJSTR) - 1, PROJID, DEFOFF(defproj) },
99 { PROJNMSTR, sizeof (PROJNMSTR) - 1, STR, DEFOFF(defprojname) },
100 { LIMPRSTR, sizeof (LIMPRSTR) - 1, STR, DEFOFF(deflimpriv),
101 USERATTR_LIMPRIV_KW },
102 { DFLTPRSTR, sizeof (DFLTPRSTR) - 1, STR, DEFOFF(defdfltpriv),
103 USERATTR_DFLTPRIV_KW },
104 { LOCK_AFTER_RETRIESSTR, sizeof (LOCK_AFTER_RETRIESSTR) - 1,
105 STR, DEFOFF(deflock_after_retries),
106 USERATTR_LOCK_AFTER_RETRIES_KW },
109 #define NDEF (sizeof (tab) / sizeof (parsent_t))
111 static const parsent_t *
112 scan(char **start_p)
114 static int ind = NDEF - 1;
115 char *cur_p = *start_p;
116 int lastind = ind;
118 if (!*cur_p || *cur_p == '\n' || *cur_p == '#')
119 return (NULL);
122 * The magic in this loop is remembering the last index when
123 * reentering the function; the entries above are also used to
124 * order the output to the default file.
126 do {
127 ind++;
128 ind %= NDEF;
130 if (strncmp(cur_p, tab[ind].name, tab[ind].nmsz) == 0) {
131 *start_p = cur_p + tab[ind].nmsz;
132 return (&tab[ind]);
134 } while (ind != lastind);
136 return (NULL);
140 * getusrdef - returns default values of values in userdefs.h.
143 struct userdefs *
144 getusrdef(char *usertype)
146 return (&defaults);
149 static char *
150 dup_to_nl(char *from)
152 char *res = strdup(from);
154 char *p = strchr(res, '\n');
155 if (p)
156 *p = '\0';
158 return (res);
161 void
162 dispusrdef(FILE *fptr, unsigned flags, char *usertype)
164 struct userdefs *deflts = getusrdef(usertype);
165 int outcount = 0;
167 /* Print out values */
169 if (flags & D_GROUP) {
170 outcount += fprintf(fptr, "group=%s,%ld ",
171 deflts->defgname, deflts->defgroup);
172 PRINTNL();
175 if (flags & D_PROJ) {
176 outcount += fprintf(fptr, "project=%s,%ld ",
177 deflts->defprojname, deflts->defproj);
178 PRINTNL();
181 if (flags & D_BASEDIR) {
182 outcount += fprintf(fptr, "basedir=%s ", deflts->defparent);
183 PRINTNL();
186 if (flags & D_RID) {
187 outcount += fprintf(fptr, "rid=%ld ", deflts->defrid);
188 PRINTNL();
191 if (flags & D_SKEL) {
192 outcount += fprintf(fptr, "skel=%s ", deflts->defskel);
193 PRINTNL();
196 if (flags & D_SHELL) {
197 outcount += fprintf(fptr, "shell=%s ", deflts->defshell);
198 PRINTNL();
201 if (flags & D_INACT) {
202 outcount += fprintf(fptr, "inactive=%d ", deflts->definact);
203 PRINTNL();
206 if (flags & D_EXPIRE) {
207 outcount += fprintf(fptr, "expire=%s ", deflts->defexpire);
208 PRINTNL();
211 if (flags & D_AUTH) {
212 outcount += fprintf(fptr, "auths=%s ", deflts->defauth);
213 PRINTNL();
216 if (flags & D_PROF) {
217 outcount += fprintf(fptr, "profiles=%s ", deflts->defprof);
218 PRINTNL();
221 if ((flags & D_ROLE) &&
222 (!is_role(usertype))) {
223 outcount += fprintf(fptr, "roles=%s ", deflts->defrole);
224 PRINTNL();
227 if (flags & D_LPRIV) {
228 outcount += fprintf(fptr, "limitpriv=%s ",
229 deflts->deflimpriv);
230 PRINTNL();
233 if (flags & D_DPRIV) {
234 outcount += fprintf(fptr, "defaultpriv=%s ",
235 deflts->defdfltpriv);
236 PRINTNL();
239 if (flags & D_LOCK) {
240 outcount += fprintf(fptr, "lock_after_retries=%s ",
241 deflts->deflock_after_retries);
244 if (outcount > 0)
245 (void) fprintf(fptr, "\n");
248 /* Import default keys for ordinary useradd */
249 void
250 import_def(struct userdefs *ud)
252 int i;
254 for (i = 0; i < NDEF; i++) {
255 if (tab[i].uakey != NULL && tab[i].type == STR) {
256 char *val = FIELD(ud, &tab[i], char *);
257 if (val == getsetdefval(tab[i].uakey, val))
258 nkeys ++;