xref: /onnv-gate/usr/src/cmd/oamuser/user/uid.c (revision 0:68f95e015346)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1997-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.5 */
32 
33 #include <sys/types.h>
34 #include <stdio.h>
35 #include <userdefs.h>
36 #include <pwd.h>
37 
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);
50 
51 /*
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.
55  */
56 uid_t
findnextuid(void)57 findnextuid(void)
58 {
59 	uid_t uid = DEFRID + 1;
60 	struct passwd *pwd;
61 	uchar_t overflow = 0;
62 
63 	setpwent();
64 	for (pwd = getpwent(); pwd != NULL; pwd = getpwent()) {
65 		if (isreserveduid(pwd->pw_uid))		/* Skip reserved IDs */
66 			continue;
67 		if (pwd->pw_uid >= uid) {
68 			if (pwd->pw_uid == MAXUID) {	/* Overflow check */
69 				overflow = 1;
70 				break;
71 			}
72 			uid = pwd->pw_uid + 1;
73 			while (isreserveduid(uid) &&
74 			    uid < MAXUID) {		/* Skip reserved IDs */
75 				uid++;
76 			}
77 		}
78 	}
79 	endpwent();
80 	if (overflow == 1)				/* Find a hole */
81 		return (getrangeboundid(DEFRID + 1, MAXUID));
82 	return (uid);
83 }
84 
85 /*
86  * Check to see that the uid is a reserved uid
87  * -- nobody, noaccess or nobody4
88  */
89 static int
isreserveduid(uid_t uid)90 isreserveduid(uid_t uid)
91 {
92 	return (uid == 60001 || uid == 60002 || uid == 65534);
93 }
94 
95 
96 /*
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
103  */
104 static uid_t
getrangeboundid(uid_t start,uid_t stop)105 getrangeboundid(uid_t start, uid_t stop)
106 {
107 	uid_t low = (start <= DEFRID) ? DEFRID + 1 : start;
108 	uid_t high = (stop < MAXUID) ? stop : MAXUID;
109 	uid_t uid;
110 
111 	for (uid = low; uid <= high; uid++) {
112 		if (isreserveduid(uid))
113 			continue;
114 		if (getpwuid(uid) == NULL)
115 			break;
116 	}
117 	return ((uid > high) ? -1 : uid);
118 }
119