Print this page
4078 groupadd execs getent unnecessarily
Reviewed by: Rich Lowe <richlowe@richlowe.net>
Reviewed by: Gary Mills <gary_mills@fastmail.fm>
Reviewed by: Milan Jurik <milan.jurik@xylab.cz>
Reviewed by: Gordon Ross <Gordon.W.Ross@gmail.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/oamuser/user/userdefs.c
+++ new/usr/src/cmd/oamuser/user/userdefs.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License, Version 1.0 only
6 6 * (the "License"). You may not use this file except in compliance
7 7 * with the License.
8 8 *
9 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 * or http://www.opensolaris.org/os/licensing.
11 11 * See the License for the specific language governing permissions
12 12 * and limitations under the License.
13 13 *
14 14 * When distributing Covered Code, include this CDDL HEADER in each
15 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 * If applicable, add the following below this CDDL HEADER, with the
17 17 * fields enclosed by brackets "[]" replaced with your own identifying
18 18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 19 *
20 20 * CDDL HEADER END
↓ open down ↓ |
20 lines elided |
↑ open up ↑ |
21 21 */
22 22
23 23 /*
24 24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 25 * Use is subject to license terms.
26 26 */
27 27
28 28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 29 /* All Rights Reserved */
30 30
31 -
32 -#pragma ident "%Z%%M% %I% %E% SMI"
31 +/*
32 + * Copyright (c) 2013 RackTop Systems.
33 + */
33 34
34 35 /*LINTLIBRARY*/
35 36
36 37 #include <sys/types.h>
37 38 #include <stdio.h>
38 39 #include <string.h>
39 40 #include <userdefs.h>
40 41 #include <user_attr.h>
41 42 #include <limits.h>
42 43 #include <stdlib.h>
43 44 #include <stddef.h>
44 45 #include <time.h>
45 46 #include <unistd.h>
46 47 #include "userdisp.h"
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
47 48 #include "funcs.h"
48 49 #include "messages.h"
49 50
50 51 /* Print out a NL when the line gets too long */
51 52 #define PRINTNL() \
52 53 if (outcount > 40) { \
53 54 outcount = 0; \
54 55 (void) fprintf(fptr, "\n"); \
55 56 }
56 57
57 -#define SKIPWS(ptr) while (*ptr && *ptr == ' ' || *ptr == '\t') ptr++
58 +#define SKIPWS(ptr) while (*ptr && (*ptr == ' ' || *ptr == '\t')) ptr++
58 59
59 60 static char *dup_to_nl(char *);
60 61
61 62 static struct userdefs defaults = {
62 63 DEFRID, DEFGROUP, DEFGNAME, DEFPARENT, DEFSKL,
63 64 DEFSHL, DEFINACT, DEFEXPIRE, DEFAUTH, DEFPROF,
64 65 DEFROLE, DEFPROJ, DEFPROJNAME, DEFLIMPRIV,
65 66 DEFDFLTPRIV, DEFLOCK_AFTER_RETRIES
66 67 };
67 68
68 69 #define INT 0
69 70 #define STR 1
70 71 #define PROJID 2
71 72
72 73 #define DEFOFF(field) offsetof(struct userdefs, field)
73 74 #define FIELD(up, pe, type) (*(type *)((char *)(up) + (pe)->off))
74 75
75 76 typedef struct parsent {
76 77 const char *name; /* deffoo= */
77 78 const size_t nmsz; /* length of def= string (excluding \0) */
78 79 const int type; /* type of entry */
79 80 const ptrdiff_t off; /* offset in userdefs structure */
80 81 const char *uakey; /* user_attr key, if defined */
81 82 } parsent_t;
82 83
83 84 static const parsent_t tab[] = {
84 85 { GIDSTR, sizeof (GIDSTR) - 1, INT, DEFOFF(defgroup) },
85 86 { GNAMSTR, sizeof (GNAMSTR) - 1, STR, DEFOFF(defgname) },
86 87 { PARSTR, sizeof (PARSTR) - 1, STR, DEFOFF(defparent) },
87 88 { SKLSTR, sizeof (SKLSTR) - 1, STR, DEFOFF(defskel) },
88 89 { SHELLSTR, sizeof (SHELLSTR) - 1, STR, DEFOFF(defshell) },
89 90 { INACTSTR, sizeof (INACTSTR) - 1, INT, DEFOFF(definact) },
90 91 { EXPIRESTR, sizeof (EXPIRESTR) - 1, STR, DEFOFF(defexpire) },
91 92 { AUTHSTR, sizeof (AUTHSTR) - 1, STR, DEFOFF(defauth),
92 93 USERATTR_AUTHS_KW },
93 94 { ROLESTR, sizeof (ROLESTR) - 1, STR, DEFOFF(defrole),
94 95 USERATTR_ROLES_KW },
95 96 { PROFSTR, sizeof (PROFSTR) - 1, STR, DEFOFF(defprof),
96 97 USERATTR_PROFILES_KW },
97 98 { PROJSTR, sizeof (PROJSTR) - 1, PROJID, DEFOFF(defproj) },
98 99 { PROJNMSTR, sizeof (PROJNMSTR) - 1, STR, DEFOFF(defprojname) },
99 100 { LIMPRSTR, sizeof (LIMPRSTR) - 1, STR, DEFOFF(deflimpriv),
100 101 USERATTR_LIMPRIV_KW },
101 102 { DFLTPRSTR, sizeof (DFLTPRSTR) - 1, STR, DEFOFF(defdfltpriv),
102 103 USERATTR_DFLTPRIV_KW },
103 104 { LOCK_AFTER_RETRIESSTR, sizeof (LOCK_AFTER_RETRIESSTR) - 1,
104 105 STR, DEFOFF(deflock_after_retries),
105 106 USERATTR_LOCK_AFTER_RETRIES_KW },
106 107 };
107 108
108 109 #define NDEF (sizeof (tab) / sizeof (parsent_t))
109 110
110 111 FILE *defptr; /* default file - fptr */
111 112
112 113 static const parsent_t *
113 114 scan(char **start_p)
114 115 {
115 116 static int ind = NDEF - 1;
116 117 char *cur_p = *start_p;
117 118 int lastind = ind;
118 119
119 120 if (!*cur_p || *cur_p == '\n' || *cur_p == '#')
120 121 return (NULL);
121 122
122 123 /*
123 124 * The magic in this loop is remembering the last index when
124 125 * reentering the function; the entries above are also used to
125 126 * order the output to the default file.
126 127 */
127 128 do {
128 129 ind++;
129 130 ind %= NDEF;
130 131
131 132 if (strncmp(cur_p, tab[ind].name, tab[ind].nmsz) == 0) {
132 133 *start_p = cur_p + tab[ind].nmsz;
133 134 return (&tab[ind]);
134 135 }
135 136 } while (ind != lastind);
136 137
137 138 return (NULL);
138 139 }
139 140
140 141 /*
141 142 * getusrdef - access the user defaults file. If it doesn't exist,
142 143 * then returns default values of (values in userdefs.h):
143 144 * defrid = 100
144 145 * defgroup = 1
145 146 * defgname = other
146 147 * defparent = /home
147 148 * defskel = /usr/sadm/skel
148 149 * defshell = /bin/sh
149 150 * definact = 0
150 151 * defexpire = 0
151 152 * defauth = 0
152 153 * defprof = 0
153 154 * defrole = 0
154 155 *
155 156 * If getusrdef() is unable to access the defaults file, it
156 157 * returns a NULL pointer.
157 158 *
158 159 * If user defaults file exists, then getusrdef uses values
159 160 * in it to override the above values.
160 161 */
161 162
162 163 struct userdefs *
163 164 getusrdef(char *usertype)
164 165 {
165 166 char instr[512], *ptr;
166 167 const parsent_t *pe;
167 168
168 169 if (is_role(usertype)) {
169 170 if ((defptr = fopen(DEFROLEFILE, "r")) == NULL) {
170 171 defaults.defshell = DEFROLESHL;
171 172 defaults.defprof = DEFROLEPROF;
172 173 return (&defaults);
173 174 }
174 175 } else {
175 176 if ((defptr = fopen(DEFFILE, "r")) == NULL)
176 177 return (&defaults);
177 178 }
178 179
179 180 while (fgets(instr, sizeof (instr), defptr) != NULL) {
180 181 ptr = instr;
181 182
182 183 SKIPWS(ptr);
183 184
184 185 if (*ptr == '#')
185 186 continue;
186 187
187 188 pe = scan(&ptr);
188 189
189 190 if (pe != NULL) {
190 191 switch (pe->type) {
191 192 case INT:
192 193 FIELD(&defaults, pe, int) =
193 194 (int)strtol(ptr, NULL, 10);
194 195 break;
195 196 case PROJID:
196 197 FIELD(&defaults, pe, projid_t) =
197 198 (projid_t)strtol(ptr, NULL, 10);
198 199 break;
199 200 case STR:
200 201 FIELD(&defaults, pe, char *) = dup_to_nl(ptr);
201 202 break;
202 203 }
203 204 }
204 205 }
205 206
206 207 (void) fclose(defptr);
207 208
208 209 return (&defaults);
209 210 }
210 211
211 212 static char *
212 213 dup_to_nl(char *from)
213 214 {
214 215 char *res = strdup(from);
215 216
216 217 char *p = strchr(res, '\n');
217 218 if (p)
218 219 *p = '\0';
219 220
220 221 return (res);
221 222 }
222 223
223 224 void
224 225 dispusrdef(FILE *fptr, unsigned flags, char *usertype)
225 226 {
226 227 struct userdefs *deflts = getusrdef(usertype);
227 228 int outcount = 0;
228 229
229 230 /* Print out values */
230 231
231 232 if (flags & D_GROUP) {
232 233 outcount += fprintf(fptr, "group=%s,%ld ",
233 234 deflts->defgname, deflts->defgroup);
234 235 PRINTNL();
235 236 }
236 237
237 238 if (flags & D_PROJ) {
238 239 outcount += fprintf(fptr, "project=%s,%ld ",
239 240 deflts->defprojname, deflts->defproj);
240 241 PRINTNL();
241 242 }
242 243
243 244 if (flags & D_BASEDIR) {
244 245 outcount += fprintf(fptr, "basedir=%s ", deflts->defparent);
245 246 PRINTNL();
246 247 }
247 248
248 249 if (flags & D_RID) {
249 250 outcount += fprintf(fptr, "rid=%ld ", deflts->defrid);
250 251 PRINTNL();
251 252 }
252 253
253 254 if (flags & D_SKEL) {
254 255 outcount += fprintf(fptr, "skel=%s ", deflts->defskel);
255 256 PRINTNL();
256 257 }
257 258
258 259 if (flags & D_SHELL) {
259 260 outcount += fprintf(fptr, "shell=%s ", deflts->defshell);
260 261 PRINTNL();
261 262 }
262 263
263 264 if (flags & D_INACT) {
264 265 outcount += fprintf(fptr, "inactive=%d ", deflts->definact);
265 266 PRINTNL();
266 267 }
267 268
268 269 if (flags & D_EXPIRE) {
269 270 outcount += fprintf(fptr, "expire=%s ", deflts->defexpire);
270 271 PRINTNL();
271 272 }
272 273
273 274 if (flags & D_AUTH) {
274 275 outcount += fprintf(fptr, "auths=%s ", deflts->defauth);
275 276 PRINTNL();
276 277 }
277 278
278 279 if (flags & D_PROF) {
279 280 outcount += fprintf(fptr, "profiles=%s ", deflts->defprof);
280 281 PRINTNL();
281 282 }
282 283
283 284 if ((flags & D_ROLE) &&
284 285 (!is_role(usertype))) {
285 286 outcount += fprintf(fptr, "roles=%s ", deflts->defrole);
286 287 PRINTNL();
287 288 }
288 289
289 290 if (flags & D_LPRIV) {
290 291 outcount += fprintf(fptr, "limitpriv=%s ",
291 292 deflts->deflimpriv);
292 293 PRINTNL();
293 294 }
294 295
295 296 if (flags & D_DPRIV) {
296 297 outcount += fprintf(fptr, "defaultpriv=%s ",
297 298 deflts->defdfltpriv);
298 299 PRINTNL();
299 300 }
300 301
301 302 if (flags & D_LOCK) {
302 303 outcount += fprintf(fptr, "lock_after_retries=%s ",
303 304 deflts->deflock_after_retries);
304 305 }
305 306
306 307 if (outcount > 0)
307 308 (void) fprintf(fptr, "\n");
308 309 }
309 310
310 311 /*
311 312 * putusrdef -
312 313 * changes default values in defadduser file
313 314 */
314 315 int
315 316 putusrdef(struct userdefs *defs, char *usertype)
316 317 {
317 318 time_t timeval; /* time value from time */
318 319 int i;
319 320 ptrdiff_t skip;
320 321 char *hdr;
321 322
322 323 /*
323 324 * file format is:
324 325 * #<tab>Default values for adduser. Changed mm/dd/yy hh:mm:ss.
325 326 * defgroup=m (m=default group id)
326 327 * defgname=str1 (str1=default group name)
327 328 * defparent=str2 (str2=default base directory)
328 329 * definactive=x (x=default inactive)
329 330 * defexpire=y (y=default expire)
330 331 * defproj=z (z=numeric project id)
331 332 * defprojname=str3 (str3=default project name)
332 333 * ... etc ...
333 334 */
334 335
335 336 if (is_role(usertype)) {
336 337 if ((defptr = fopen(DEFROLEFILE, "w")) == NULL) {
337 338 errmsg(M_FAILED);
338 339 return (EX_UPDATE);
339 340 }
340 341 } else {
341 342 if ((defptr = fopen(DEFFILE, "w")) == NULL) {
342 343 errmsg(M_FAILED);
343 344 return (EX_UPDATE);
344 345 }
345 346 }
346 347
347 348 if (lockf(fileno(defptr), F_LOCK, 0) != 0) {
348 349 /* print error if can't lock whole of DEFFILE */
349 350 errmsg(M_UPDATE, "created");
350 351 return (EX_UPDATE);
351 352 }
352 353
353 354 if (is_role(usertype)) {
354 355 /* If it's a role, we must skip the defrole field */
355 356 skip = offsetof(struct userdefs, defrole);
356 357 hdr = FHEADER_ROLE;
357 358 } else {
358 359 skip = -1;
359 360 hdr = FHEADER;
360 361 }
361 362
362 363 /* get time */
363 364 timeval = time(NULL);
364 365
365 366 /* write it to file */
366 367 if (fprintf(defptr, "%s%s\n", hdr, ctime(&timeval)) <= 0) {
367 368 errmsg(M_UPDATE, "created");
368 369 return (EX_UPDATE);
369 370 }
370 371
371 372 for (i = 0; i < NDEF; i++) {
372 373 int res = 0;
373 374
374 375 if (tab[i].off == skip)
375 376 continue;
376 377
377 378 switch (tab[i].type) {
378 379 case INT:
379 380 res = fprintf(defptr, "%s%d\n", tab[i].name,
380 381 FIELD(defs, &tab[i], int));
381 382 break;
382 383 case STR:
383 384 res = fprintf(defptr, "%s%s\n", tab[i].name,
384 385 FIELD(defs, &tab[i], char *));
385 386 break;
386 387 case PROJID:
387 388 res = fprintf(defptr, "%s%d\n", tab[i].name,
388 389 (int)FIELD(defs, &tab[i], projid_t));
389 390 break;
390 391 }
391 392
392 393 if (res <= 0) {
393 394 errmsg(M_UPDATE, "created");
394 395 return (EX_UPDATE);
395 396 }
396 397 }
397 398
398 399 (void) lockf(fileno(defptr), F_ULOCK, 0);
399 400 (void) fclose(defptr);
400 401
401 402 return (EX_SUCCESS);
402 403 }
403 404
404 405 /* Export command line keys to defaults for useradd -D */
405 406 void
406 407 update_def(struct userdefs *ud)
407 408 {
408 409 int i;
409 410
410 411 for (i = 0; i < NDEF; i++) {
411 412 char *val;
412 413 if (tab[i].uakey != NULL &&
413 414 (val = getsetdefval(tab[i].uakey, NULL)) != NULL)
414 415 FIELD(ud, &tab[i], char *) = val;
415 416 }
416 417 }
417 418
418 419 /* Import default keys for ordinary useradd */
419 420 void
420 421 import_def(struct userdefs *ud)
421 422 {
422 423 int i;
423 424
424 425 for (i = 0; i < NDEF; i++) {
425 426 if (tab[i].uakey != NULL && tab[i].type == STR) {
426 427 char *val = FIELD(ud, &tab[i], char *);
427 428 if (val == getsetdefval(tab[i].uakey, val))
428 429 nkeys ++;
429 430 }
430 431 }
431 432 }
↓ open down ↓ |
364 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX