summaryrefslogtreecommitdiff
path: root/libmisc/find_new_uid.c
blob: aac1909c51d6870d31b2dd544ddf089a5560ef82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright (c) 1991 - 1994, Julianne Frances Haugh
 * Copyright (c) 2008 - 2011, Nicolas François
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the copyright holders or contributors may not be used to
 *    endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <config.h>

#include <assert.h>
#include <stdio.h>
#include <errno.h>

#include "prototypes.h"
#include "pwio.h"
#include "getdef.h"

/*
 * find_new_uid - Find a new unused UID.
 *
 * If successful, find_new_uid provides an unused user ID in the
 * [UID_MIN:UID_MAX] range.
 * This ID should be higher than all the used UID, but if not possible,
 * the lowest unused ID in the range will be returned.
 * 
 * Return 0 on success, -1 if no unused UIDs are available.
 */
int find_new_uid (bool sys_user,
                  uid_t *uid,
                  /*@null@*/uid_t const *preferred_uid)
{
	const struct passwd *pwd;
	uid_t uid_min, uid_max, user_id;
	bool *used_uids;

	assert (uid != NULL);

	if (!sys_user) {
		uid_min = (uid_t) getdef_ulong ("UID_MIN", 1000UL);
		uid_max = (uid_t) getdef_ulong ("UID_MAX", 60000UL);
		if (uid_max < uid_min) {
			(void) fprintf (stderr,
			                _("%s: Invalid configuration: UID_MIN (%lu), UID_MAX (%lu)\n"),
			                Prog, (unsigned long) uid_min, (unsigned long) uid_max);
			return -1;
		}
	} else {
		uid_min = (uid_t) getdef_ulong ("SYS_UID_MIN", 101UL);
		uid_max = (uid_t) getdef_ulong ("UID_MIN", 1000UL) - 1;
		uid_max = (uid_t) getdef_ulong ("SYS_UID_MAX", (unsigned long) uid_max);
		if (uid_max < uid_min) {
			(void) fprintf (stderr,
			                _("%s: Invalid configuration: SYS_UID_MIN (%lu), UID_MIN (%lu), SYS_UID_MAX (%lu)\n"),
			                Prog, (unsigned long) uid_min, getdef_ulong ("UID_MIN", 1000UL), (unsigned long) uid_max);
			return -1;
		}
	}
	used_uids = malloc (sizeof (bool) * (uid_max +1));
	if (NULL == used_uids) {
		fprintf (stderr,
		         _("%s: failed to allocate memory: %s\n"),
		         Prog, strerror (errno));
		return -1;
	}
	memset (used_uids, false, sizeof (bool) * (uid_max + 1));

	if (   (NULL != preferred_uid)
	    && (*preferred_uid >= uid_min)
	    && (*preferred_uid <= uid_max)
	    /* Check if the user exists according to NSS */
	    && (getpwuid (*preferred_uid) == NULL)
	    /* Check also the local database in case of uncommitted
	     * changes */
	    && (pw_locate_uid (*preferred_uid) == NULL)) {
		*uid = *preferred_uid;
		free (used_uids);
		return 0;
	}


	/*
	 * Search the entire password file,
	 * looking for the largest unused value.
	 *
	 * We check the list of users according to NSS (setpwent/getpwent),
	 * but we also check the local database (pw_rewind/pw_next) in case
	 * some users were created but the changes were not committed yet.
	 */
	if (sys_user) {
		uid_t id;
		/* setpwent / getpwent / endpwent can be very slow with
		 * LDAP configurations (and many accounts).
		 * Since there is a limited amount of IDs to be tested
		 * for system accounts, we just check the existence
		 * of IDs with getpwuid.
		 */
		user_id = uid_max;
		for (id = uid_max; id >= uid_min; id--) {
			if (getpwuid (id) != NULL) {
				user_id = id - 1;
				used_uids[id] = true;
			}
		}

		(void) pw_rewind ();
		while ((pwd = pw_next ()) != NULL) {
			if ((pwd->pw_uid <= user_id) && (pwd->pw_uid >= uid_min)) {
				user_id = pwd->pw_uid - 1;
			}
			/* create index of used UIDs */
			if (pwd->pw_uid <= uid_max) {
				used_uids[pwd->pw_uid] = true;
			}
		}
	} else {
		user_id = uid_min;
		setpwent ();
		while ((pwd = getpwent ()) != NULL) {
			if ((pwd->pw_uid >= user_id) && (pwd->pw_uid <= uid_max)) {
				user_id = pwd->pw_uid + 1;
			}
			/* create index of used UIDs */
			if (pwd->pw_uid <= uid_max) {
				used_uids[pwd->pw_uid] = true;
			}
		}
		endpwent ();

		(void) pw_rewind ();
		while ((pwd = pw_next ()) != NULL) {
			if ((pwd->pw_uid >= user_id) && (pwd->pw_uid <= uid_max)) {
				user_id = pwd->pw_uid + 1;
			}
			/* create index of used UIDs */
			if (pwd->pw_uid <= uid_max) {
				used_uids[pwd->pw_uid] = true;
			}
		}
	}

	/*
	 * If a user (resp. system user) with UID equal to UID_MAX (resp.
	 * UID_MIN) exists, the above algorithm will give us UID_MAX+1
	 * (resp. UID_MIN-1) even if not unique. Search for the first free
	 * UID starting with UID_MIN (resp. UID_MAX).
	 */
	if (sys_user) {
		if (user_id < uid_min) {
			for (user_id = uid_max; user_id >= uid_min; user_id--) {
				if (false == used_uids[user_id]) {
					break;
				}
			}
			if (user_id < uid_min ) {
				fprintf (stderr,
				         _("%s: Can't get unique system UID (no more available UIDs)\n"),
				         Prog);
				SYSLOG ((LOG_WARN,
				         "no more available UID on the system"));
				free (used_uids);
				return -1;
			}
		}
	} else {
		if (user_id > uid_max) {
			for (user_id = uid_min; user_id <= uid_max; user_id++) {
				if (false == used_uids[user_id]) {
					break;
				}
			}
			if (user_id > uid_max) {
				fprintf (stderr,
				         _("%s: Can't get unique UID (no more available UIDs)\n"),
				         Prog);
				SYSLOG ((LOG_WARN, "no more available UID on the system"));
				free (used_uids);
				return -1;
			}
		}
	}

	free (used_uids);
	*uid = user_id;
	return 0;
}