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/uid.c
+++ new/usr/src/lib/libcmdutils/common/uid.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 *
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
20 20 * CDDL HEADER END
21 21 */
22 22 /*
23 23 * Copyright (c) 1997-2001 by Sun Microsystems, Inc.
24 24 * All rights reserved.
25 25 */
26 26
27 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 28 /* All Rights Reserved */
29 29
30 +/*
31 + * Copyright (c) 2013 RackTop Systems.
32 + */
30 33
31 -#pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.5 */
32 -
34 +#include <errno.h>
33 35 #include <sys/types.h>
34 36 #include <stdio.h>
35 37 #include <userdefs.h>
36 38 #include <pwd.h>
39 +#include <libcmdutils.h>
37 40
38 -#include <sys/param.h>
39 -#ifndef MAXUID
40 -#include <limits.h>
41 -#ifdef UID_MAX
42 -#define MAXUID UID_MAX
43 -#else
44 -#define MAXUID 60000
45 -#endif
46 -#endif
47 -
48 -static uid_t getrangeboundid(uid_t start, uid_t stop);
49 -static int isreserveduid(uid_t uid);
41 +static int findunuseduid(uid_t start, uid_t stop, uid_t *ret);
42 +static boolean_t isreserveduid(uid_t uid);
50 43
51 44 /*
52 - * Find the highest uid currently in use and return it. If the highest unused
53 - * uid is MAXUID, then attempt to find a hole in the range. If there are no
54 - * more unused uids, then return -1.
45 + * Find the highest unused uid. If the highest unused uid is "stop",
46 + * then attempt to find a hole in the range. Returns 0 on success.
55 47 */
56 -uid_t
57 -findnextuid(void)
48 +int
49 +findnextuid(uid_t start, uid_t stop, uid_t *ret)
58 50 {
59 - uid_t uid = DEFRID + 1;
51 + uid_t uid = start;
60 52 struct passwd *pwd;
61 - uchar_t overflow = 0;
53 + boolean_t overflow = B_FALSE;
62 54
63 55 setpwent();
64 56 for (pwd = getpwent(); pwd != NULL; pwd = getpwent()) {
65 57 if (isreserveduid(pwd->pw_uid)) /* Skip reserved IDs */
66 58 continue;
67 59 if (pwd->pw_uid >= uid) {
68 - if (pwd->pw_uid == MAXUID) { /* Overflow check */
69 - overflow = 1;
60 + if (pwd->pw_uid == stop) { /* Overflow check */
61 + overflow = B_TRUE;
70 62 break;
71 63 }
72 64 uid = pwd->pw_uid + 1;
73 - while (isreserveduid(uid) &&
74 - uid < MAXUID) { /* Skip reserved IDs */
75 - uid++;
76 - }
77 65 }
78 66 }
67 + if (pwd == NULL && errno != 0) {
68 + endpwent();
69 + return (-1);
70 + }
79 71 endpwent();
80 - if (overflow == 1) /* Find a hole */
81 - return (getrangeboundid(DEFRID + 1, MAXUID));
82 - return (uid);
72 + if (overflow == B_TRUE) /* Find a hole */
73 + return (findunuseduid(start, stop, ret));
74 + while (isreserveduid(uid) && uid < stop) /* Skip reserved IDs */
75 + uid++;
76 + *ret = uid;
77 + return (0);
83 78 }
84 79
85 80 /*
86 - * Check to see that the uid is a reserved uid
81 + * Check to see whether the uid is a reserved uid
87 82 * -- nobody, noaccess or nobody4
88 83 */
89 -static int
84 +static boolean_t
90 85 isreserveduid(uid_t uid)
91 86 {
92 87 return (uid == 60001 || uid == 60002 || uid == 65534);
93 88 }
94 89
95 -
96 90 /*
97 - * getrangeboundid() attempts to return the next valid usable id between the
98 - * supplied upper and lower limits. If these limits exceed the system
99 - * boundaries of DEFRID +1 and MAXUID (lower and upper bound respectively),
100 - * then they are ignored and DEFRID + 1 and MAXUID are used.
101 - *
102 - * Returns a valid uid_t between DEFRID +1 and MAXUID, -1 is returned on fail
91 + * findunuseduid() attempts to return the next valid usable id between the
92 + * supplied upper and lower limits. Returns 0 on success.
103 93 */
104 -static uid_t
105 -getrangeboundid(uid_t start, uid_t stop)
94 +static int
95 +findunuseduid(uid_t start, uid_t stop, uid_t *ret)
106 96 {
107 - uid_t low = (start <= DEFRID) ? DEFRID + 1 : start;
108 - uid_t high = (stop < MAXUID) ? stop : MAXUID;
109 97 uid_t uid;
110 98
111 - for (uid = low; uid <= high; uid++) {
99 + for (uid = start; uid <= stop; uid++) {
112 100 if (isreserveduid(uid))
113 101 continue;
114 - if (getpwuid(uid) == NULL)
102 + if (getpwuid(uid) == NULL) {
103 + if (errno != 0)
104 + return (-1);
115 105 break;
106 + }
116 107 }
117 - return ((uid > high) ? -1 : uid);
108 + if (uid > stop)
109 + return (-1);
110 + *ret = uid;
111 + return (0);
118 112 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX