2 * Copyright (c) 2012 - Eric Biederman
9 #include "prototypes.h"
13 #include "subordinateio.h"
14 #include <sys/types.h>
17 struct subordinate_range {
26 * subordinate_dup: create a duplicate range
28 * @ent: a pointer to a subordinate_range struct
30 * Returns a pointer to a newly allocated duplicate subordinate_range struct
33 static /*@null@*/ /*@only@*/void *subordinate_dup (const void *ent)
35 const struct subordinate_range *rangeent = ent;
36 struct subordinate_range *range;
38 range = (struct subordinate_range *) malloc (sizeof *range);
42 range->owner = strdup (rangeent->owner);
43 if (NULL == range->owner) {
47 range->start = rangeent->start;
48 range->count = rangeent->count;
54 * subordinate_free: free a subordinate_range struct
56 * @ent: pointer to a subordinate_range struct to free.
58 static void subordinate_free (/*@out@*/ /*@only@*/void *ent)
60 struct subordinate_range *rangeent = ent;
62 free ((void *)(rangeent->owner));
69 * @line: a line to parse
71 * Returns a pointer to a subordinate_range struct representing the values
72 * in @line, or NULL on failure. Note that the returned value should not
73 * be freed by the caller.
75 static void *subordinate_parse (const char *line)
77 static struct subordinate_range range;
78 static char rangebuf[1024];
81 char *fields[NFIELDS];
84 * Copy the string to a temporary buffer so the substrings can
85 * be modified to be NULL terminated.
87 if (strlen (line) >= sizeof rangebuf)
88 return NULL; /* fail if too long */
89 strcpy (rangebuf, line);
92 * Save a pointer to the start of each colon separated
93 * field. The fields are converted into NUL terminated strings.
96 for (cp = rangebuf, i = 0; (i < NFIELDS) && (NULL != cp); i++) {
98 while (('\0' != *cp) && (':' != *cp)) {
111 * There must be exactly NFIELDS colon separated fields or
112 * the entry is invalid. Also, fields must be non-blank.
114 if (i != NFIELDS || *fields[0] == '\0' || *fields[1] == '\0' || *fields[2] == '\0')
116 range.owner = fields[0];
117 if (getulong (fields[1], &range.start) == 0)
119 if (getulong (fields[2], &range.count) == 0)
126 * subordinate_put: print a subordinate_range value to a file
128 * @ent: a pointer to a subordinate_range struct to print out.
129 * @file: file to which to print.
131 * Returns 0 on success, -1 on error.
133 static int subordinate_put (const void *ent, FILE * file)
135 const struct subordinate_range *range = ent;
137 return fprintf(file, "%s:%lu:%lu\n",
140 range->count) < 0 ? -1 : 0;
143 static struct commonio_ops subordinate_ops = {
144 subordinate_dup, /* dup */
145 subordinate_free, /* free */
147 subordinate_parse, /* parse */
148 subordinate_put, /* put */
151 NULL, /* open_hook */
152 NULL, /* close_hook */
156 * range_exists: Check whether @owner owns any ranges
158 * @db: database to query
159 * @owner: owner being queried
161 * Returns true if @owner owns any subuid ranges, false otherwise.
163 static const bool range_exists(struct commonio_db *db, const char *owner)
165 const struct subordinate_range *range;
167 while ((range = commonio_next(db)) != NULL) {
168 if (0 == strcmp(range->owner, owner))
175 * find_range: find a range which @owner is authorized to use which includes
178 * @db: database to query
179 * @owner: owning uid being queried
180 * @val: subuid being searched for.
182 * Returns a range of subuids belonging to @owner and including the subuid
183 * @val, or NULL if no such range exists.
185 static const struct subordinate_range *find_range(struct commonio_db *db,
186 const char *owner, unsigned long val)
188 const struct subordinate_range *range;
191 * Search for exact username/group specification
193 * This is the original method - go fast through the db, doing only
194 * exact username/group string comparison. Therefore we leave it as-is
195 * for the time being, in order to keep it equally fast as it was
199 while ((range = commonio_next(db)) != NULL) {
200 unsigned long first = range->start;
201 unsigned long last = first + range->count - 1;
203 if (0 != strcmp(range->owner, owner))
206 if ((val >= first) && (val <= last))
212 * We only do special handling for these two files
214 if ((0 != strcmp(db->filename, "/etc/subuid")) && (0 != strcmp(db->filename, "/etc/subgid")))
218 * Search loop above did not produce any result. Let's rerun it,
219 * but this time try to match actual UIDs. The first entry that
220 * matches is considered a success.
221 * (It may be specified as literal UID or as another username which
222 * has the same UID as the username we are looking for.)
226 char owner_uid_string[33] = "";
229 /* Get UID of the username we are looking for */
230 pwd = getpwnam(owner);
232 /* Username not defined in /etc/passwd, or error occured during lookup */
235 owner_uid = pwd->pw_uid;
236 sprintf(owner_uid_string, "%lu", (unsigned long int)owner_uid);
239 while ((range = commonio_next(db)) != NULL) {
240 unsigned long first = range->start;
241 unsigned long last = first + range->count - 1;
243 /* For performance reasons check range before using getpwnam() */
244 if ((val < first) || (val > last)) {
249 * Range matches. Check if range owner is specified
250 * as numeric UID and if it matches.
252 if (0 == strcmp(range->owner, owner_uid_string)) {
257 * Ok, this range owner is not specified as numeric UID
258 * we are looking for. It may be specified as another
259 * UID or as a literal username.
261 * If specified as another UID, the call to getpwnam()
264 * If specified as literal username, we will get its
265 * UID and compare that to UID we are looking for.
267 const struct passwd *range_owner_pwd;
269 range_owner_pwd = getpwnam(range->owner);
270 if (NULL == range_owner_pwd) {
274 if (owner_uid == range_owner_pwd->pw_uid) {
283 * have_range: check whether @owner is authorized to use the range
284 * (@start .. @start+@count-1).
285 * @db: database to check
286 * @owner: owning uid being queried
287 * @start: start of range
288 * @count: number of uids in range
290 * Returns true if @owner is authorized to use the range, false otherwise.
292 static bool have_range(struct commonio_db *db,
293 const char *owner, unsigned long start, unsigned long count)
295 const struct subordinate_range *range;
301 end = start + count - 1;
302 range = find_range (db, owner, start);
306 last = range->start + range->count - 1;
307 if (last >= (start + count - 1))
312 range = find_range(db, owner, start);
318 * subordinate_range_cmp: compare uid ranges
320 * @p1: pointer to a commonio_entry struct to compare
321 * @p2: pointer to second commonio_entry struct to compare
323 * Returns 0 if the entries are the same. Otherwise return -1
324 * if the range in p1 is lower than that in p2, or (if the ranges are
325 * equal) if the owning uid in p1 is lower than p2's. Return 1 if p1's
326 * range or owning uid is great than p2's.
328 static int subordinate_range_cmp (const void *p1, const void *p2)
330 struct subordinate_range *range1, *range2;
332 if ((*(struct commonio_entry **) p1)->eptr == NULL)
334 if ((*(struct commonio_entry **) p2)->eptr == NULL)
337 range1 = ((struct subordinate_range *) (*(struct commonio_entry **) p1)->eptr);
338 range2 = ((struct subordinate_range *) (*(struct commonio_entry **) p2)->eptr);
340 if (range1->start < range2->start)
342 else if (range1->start > range2->start)
344 else if (range1->count < range2->count)
346 else if (range1->count > range2->count)
349 return strcmp(range1->owner, range2->owner);
353 * find_free_range: find an unused consecutive sequence of ids to allocate
355 * @db: database to search
356 * @min: the first uid in the range to find
357 * @max: the highest uid to find
358 * @count: the number of uids needed
360 * Return the lowest new uid, or ULONG_MAX on failure.
362 static unsigned long find_free_range(struct commonio_db *db,
363 unsigned long min, unsigned long max,
366 const struct subordinate_range *range;
367 unsigned long low, high;
369 /* When given invalid parameters fail */
370 if ((count == 0) || (max < min))
373 /* Sort by range then by owner */
374 commonio_sort (db, subordinate_range_cmp);
378 while ((range = commonio_next(db)) != NULL) {
379 unsigned long first = range->start;
380 unsigned long last = first + range->count - 1;
382 /* Find the top end of the hole before this range */
385 /* Don't allocate IDs after max (included) */
386 if (high > max + 1) {
390 /* Is the hole before this range large enough? */
391 if ((high > low) && ((high - low) >= count))
394 /* Compute the low end of the next hole */
395 if (low < (last + 1))
401 /* Is the remaining unclaimed area large enough? */
402 if (((max - low) + 1) >= count)
409 * add_range: add a subuid range to an owning uid's list of authorized
411 * @db: database to which to add
412 * @owner: uid which owns the subuid
413 * @start: the first uid in the owned range
414 * @count: the number of uids in the range
416 * Return 1 if the range is already present or on success. On error
417 * return 0 and set errno appropriately.
419 static int add_range(struct commonio_db *db,
420 const char *owner, unsigned long start, unsigned long count)
422 struct subordinate_range range;
427 /* See if the range is already present */
428 if (have_range(db, owner, start, count))
431 /* Otherwise append the range */
432 return commonio_append(db, &range);
436 * remove_range: remove a range of subuids from an owning uid's list
437 * of authorized subuids.
438 * @db: database to work on
439 * @owner: owning uid whose range is being removed
440 * @start: start of the range to be removed
441 * @count: number of uids in the range.
443 * Returns 0 on failure, 1 on success. Failure means that we needed to
444 * create a new range to represent the new limits, and failed doing so.
446 static int remove_range (struct commonio_db *db,
448 unsigned long start, unsigned long count)
450 struct commonio_entry *ent;
457 end = start + count - 1;
458 for (ent = db->head; NULL != ent; ent = ent->next) {
459 struct subordinate_range *range = ent->eptr;
463 /* Skip unparsed entries */
468 first = range->start;
469 last = first + range->count - 1;
471 /* Skip entries with a different owner */
472 if (0 != strcmp (range->owner, owner)) {
476 /* Skip entries outside of the range to remove */
477 if ((end < first) || (start > last)) {
481 if (start <= first) {
483 /* to be removed: [start, end]
484 * range: [first, last] */
485 /* entry completely contained in the
487 commonio_del_entry (db, ent);
489 /* to be removed: [start, end]
490 * range: [first, last] */
491 /* Remove only the start of the entry */
492 range->start = end + 1;
493 range->count = (last - range->start) + 1;
500 /* to be removed: [start, end]
501 * range: [first, last] */
502 /* Remove only the end of the entry */
503 range->count = start - range->start;
508 /* to be removed: [start, end]
509 * range: [first, last] */
510 /* Remove the middle of the range
511 * This requires to create a new range */
512 struct subordinate_range tail;
513 tail.owner = range->owner;
514 tail.start = end + 1;
515 tail.count = (last - tail.start) + 1;
517 if (commonio_append (db, &tail) == 0) {
521 range->count = start - range->start;
532 static struct commonio_db subordinate_uid_db = {
533 "/etc/subuid", /* filename */
534 &subordinate_ops, /* ops */
548 false, /* readonly */
552 int sub_uid_setdbname (const char *filename)
554 return commonio_setname (&subordinate_uid_db, filename);
557 /*@observer@*/const char *sub_uid_dbname (void)
559 return subordinate_uid_db.filename;
562 bool sub_uid_file_present (void)
564 return commonio_present (&subordinate_uid_db);
567 int sub_uid_lock (void)
569 return commonio_lock (&subordinate_uid_db);
572 int sub_uid_open (int mode)
574 return commonio_open (&subordinate_uid_db, mode);
577 bool sub_uid_assigned(const char *owner)
579 return range_exists (&subordinate_uid_db, owner);
582 bool have_sub_uids(const char *owner, uid_t start, unsigned long count)
584 return have_range (&subordinate_uid_db, owner, start, count);
587 int sub_uid_add (const char *owner, uid_t start, unsigned long count)
589 return add_range (&subordinate_uid_db, owner, start, count);
592 int sub_uid_remove (const char *owner, uid_t start, unsigned long count)
594 return remove_range (&subordinate_uid_db, owner, start, count);
597 int sub_uid_close (void)
599 return commonio_close (&subordinate_uid_db);
602 int sub_uid_unlock (void)
604 return commonio_unlock (&subordinate_uid_db);
607 uid_t sub_uid_find_free_range(uid_t min, uid_t max, unsigned long count)
610 start = find_free_range (&subordinate_uid_db, min, max, count);
611 return start == ULONG_MAX ? (uid_t) -1 : start;
614 static struct commonio_db subordinate_gid_db = {
615 "/etc/subgid", /* filename */
616 &subordinate_ops, /* ops */
630 false, /* readonly */
634 int sub_gid_setdbname (const char *filename)
636 return commonio_setname (&subordinate_gid_db, filename);
639 /*@observer@*/const char *sub_gid_dbname (void)
641 return subordinate_gid_db.filename;
644 bool sub_gid_file_present (void)
646 return commonio_present (&subordinate_gid_db);
649 int sub_gid_lock (void)
651 return commonio_lock (&subordinate_gid_db);
654 int sub_gid_open (int mode)
656 return commonio_open (&subordinate_gid_db, mode);
659 bool have_sub_gids(const char *owner, gid_t start, unsigned long count)
661 return have_range(&subordinate_gid_db, owner, start, count);
664 bool sub_gid_assigned(const char *owner)
666 return range_exists (&subordinate_gid_db, owner);
669 int sub_gid_add (const char *owner, gid_t start, unsigned long count)
671 return add_range (&subordinate_gid_db, owner, start, count);
674 int sub_gid_remove (const char *owner, gid_t start, unsigned long count)
676 return remove_range (&subordinate_gid_db, owner, start, count);
679 int sub_gid_close (void)
681 return commonio_close (&subordinate_gid_db);
684 int sub_gid_unlock (void)
686 return commonio_unlock (&subordinate_gid_db);
689 gid_t sub_gid_find_free_range(gid_t min, gid_t max, unsigned long count)
692 start = find_free_range (&subordinate_gid_db, min, max, count);
693 return start == ULONG_MAX ? (gid_t) -1 : start;
695 #else /* !ENABLE_SUBIDS */
696 extern int errno; /* warning: ANSI C forbids an empty source file */
697 #endif /* !ENABLE_SUBIDS */