2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file user.c \brief User and user ID handling. */
10 #include <dirent.h> /* readdir() */
11 #include <sys/types.h>
13 #include "cmdline.h" /* TODO: This file should be independent of command line options */
20 * Describes one range of admissible user IDs.
22 * adu converts the admissible user ids given at the command line
23 * into an array of such structs.
26 /** Lowest admissible user ID. */
28 /** Greatest admissible user ID. */
32 /** Iterate over all uid ranges. */
33 #define FOR_EACH_UID_RANGE(ur, urs) for (ur = urs; ur->low <= ur->high; ur++)
35 /** Flags for the user hash table. */
37 /** Whether this slot of the hash table is used. */
39 /** Whether this uid should be taken into account. */
44 * Contains info for each user that owns at least one regular file.
46 * Even users that are not taken into account because of the --uid
47 * option occupy a slot in this hash table. This allows to find out
48 * quicky whether a uid is admissible. And yes, this has to be fast.
50 static struct user_info *uid_hash_table;
52 /** This is always a power of two. It is set in create_hash_table(). */
53 static uint32_t uid_hash_table_size;
55 /* Array of indices to the entries of \a uid_hash_table. */
56 static int *uid_hash_table_sort_idx;
58 /** The number of used slots in the hash table. */
59 static uint32_t num_uids;
62 * The columns of the per-user tables.
64 * Adu tracks disk usage on a per-user basis. For each user, a user table is
65 * being created. The rows of the user table have three columns: The directory
66 * number that may be resolved to the path using the directory table, the
67 * number of bytes and the number of files in that directory owned by the given
70 static struct osl_column_description user_table_cols[] = {
72 .storage_type = OSL_MAPPED_STORAGE,
73 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
75 .compare_function = uint64_compare,
76 .data_size = sizeof(uint64_t)
79 .storage_type = OSL_MAPPED_STORAGE,
80 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
81 .compare_function = size_compare,
83 .data_size = sizeof(uint64_t)
86 .storage_type = OSL_MAPPED_STORAGE,
87 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
88 .compare_function = size_compare,
90 .data_size = sizeof(uint64_t)
94 static int check_uid_arg(const char *arg, uint32_t *uid)
96 const uint32_t max = ~0U;
98 * we need an 64-bit int for string -> uid conversion because strtoll()
99 * returns a signed value.
102 int ret = atoi64(arg, &val);
106 if (val < 0 || val > max)
107 return -ERRNO_TO_ERROR(EINVAL);
112 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
115 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
117 if (!p || p == arg) { /* -42 or 42 */
118 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
121 ur->low = p? 0 : ur->high;
128 ret = check_uid_arg(arg, &ur->low);
132 if (*p) { /* 42-4711 */
133 ret = check_uid_arg(p, &ur->high);
137 if (ur->low > ur->high)
138 ret = -ERRNO_TO_ERROR(EINVAL);
141 ERROR_LOG("bad uid option: %s\n", orig_arg);
143 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
150 * Convert the --uid argument to an array of uid ranges.
152 * \param orig_arg The argument to the --uid option.
153 * \param ur Result pointer.
155 * Returns Negative on errors. On success, the number of uid ranges
158 int parse_uid_arg(const char *orig_arg, struct uid_range **ur)
166 arg = adu_strdup(orig_arg);
167 n = split_args(arg, &argv, ",");
170 *ur = adu_malloc((n + 1) * sizeof(struct uid_range));
171 for (i = 0; i < n; i++) {
172 ret = parse_uid_range(argv[i], *ur + i);
183 /* an empty range indicates the end of the list */
189 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
191 struct uid_range *ur;
194 if (!urs) /* empty array means all uids are allowed */
196 FOR_EACH_UID_RANGE(ur, urs)
197 if (ur->low <= uid && ur->high >= uid)
201 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
207 * Add each given user to the array of admissible users.
209 * \param users Array of user names to add.
210 * \param num_users Length of \a users.
211 * \param admissible_uids The users which are already admissible.
212 * \param num_uid_ranges The number of intervals of \a admissible_uids.
214 * For each given user, the function checks whether that user is already
215 * admissible, i.e. its uid is contained in one of the ranges given by \a
216 * admissible_uids. If it is, the function ignores that user. Otherwise, a new
217 * length-one range consisting of that uid only is appended to \a
220 * \return Negative on errors, the new number of uid ranges on success.
222 int append_users(char **users, int num_users,
223 struct uid_range **admissible_uids, int num_uid_ranges)
226 struct uid_range *au = *admissible_uids;
228 for (i = 0; i < num_users; i++) {
230 struct uid_range *ur;
231 struct passwd *pw = getpwnam(u);
234 ERROR_LOG("user %s not found\n", u);
235 return -ERRNO_TO_ERROR(EINVAL);
237 if (au && uid_is_admissible(pw->pw_uid, au))
238 continue; /* nothing to do */
239 /* add a range consisting of this uid only */
241 au = adu_realloc(au, (num_uid_ranges + 1) *
242 sizeof(struct uid_range));
243 *admissible_uids = au;
244 ur = au + num_uid_ranges - 1; /* the new uid range */
245 ur->low = ur->high = pw->pw_uid;
246 /* terminate the list */
251 return num_uid_ranges;
254 static inline int ui_used(struct user_info *ui)
256 return ui->flags & UI_FL_SLOT_USED;
259 static inline int ui_admissible(struct user_info *ui)
261 return ui->flags & UI_FL_ADMISSIBLE;
264 static int open_user_table(struct user_info *ui, int create)
269 ui->desc = adu_malloc(sizeof(*ui->desc));
270 ui->desc->num_columns = NUM_UT_COLUMNS;
272 ui->desc->column_descriptions = user_table_cols;
273 ui->desc->dir = adu_strdup(conf.database_dir_arg);
274 ui->desc->name = make_message("%u", (unsigned)ui->uid);
275 pw = getpwuid(ui->uid);
276 if (pw && pw->pw_name)
277 ui->pw_name = adu_strdup(pw->pw_name);
279 INFO_LOG("opening table for uid %u\n", (unsigned)ui->uid);
281 ret = osl(osl_create_table(ui->desc));
286 ret = osl(osl_open_table(ui->desc, &ui->table));
291 free((char *)ui->desc->name);
292 free((char *)ui->desc->dir);
295 ui->desc->name = NULL;
296 ui->desc->dir = NULL;
303 /** Iterate over each user in the uid hash table. */
304 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui < \
305 uid_hash_table + uid_hash_table_size; ui++)
309 * Execute the given function for each admissible user.
311 * \param func The function to execute.
312 * \param data Arbitrary pointer.
314 * This function calls \a func for each admissible user in the uid hash table.
315 * The \a data pointer is passed as the second argument to \a func.
317 * \return As soon as \a func returns a negative value, the loop is terminated
318 * and that negative value is returned. Otherwise, the function returns 1.
320 int for_each_admissible_user(int (*func)(struct user_info *, void *),
325 assert(uid_hash_table);
326 for (i = 0; i < uid_hash_table_size; i++) {
328 struct user_info *ui = uid_hash_table +
329 uid_hash_table_sort_idx[i];
331 if (!ui_used(ui) || !ui_admissible(ui))
333 ret = func(ui, data);
340 #define PRIME1 0xb11924e1
341 #define PRIME2 0x01000193
343 void create_hash_table(unsigned bits)
347 uid_hash_table_size = 1 << bits;
348 uid_hash_table = adu_calloc(uid_hash_table_size *
349 sizeof(struct user_info));
350 uid_hash_table_sort_idx = adu_malloc(uid_hash_table_size * sizeof(int));
351 for (i = 0; i < uid_hash_table_size; i++)
352 uid_hash_table_sort_idx[i] = i;
355 void close_user_tables(void)
357 struct user_info *ui;
366 INFO_LOG("closing user table for uid %u\n", (unsigned)ui->uid);
367 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
369 ERROR_LOG("failed to close user table %u: %s\n",
370 (unsigned)ui->uid, adu_strerror(-ret));
371 free((char *)ui->desc->name);
372 ui->desc->name = NULL;
373 free((char *)ui->desc->dir);
374 ui->desc->dir = NULL;
382 free(uid_hash_table);
383 uid_hash_table = NULL;
384 free(uid_hash_table_sort_idx);
385 uid_hash_table_sort_idx = NULL;
389 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
390 * interval [0..s-1]. Hash collisions are treated by open addressing, i.e.
391 * unused slots in the table are used to store different uids that hash to the
394 * If a hash collision occurs, different slots are successively probed in order
395 * to find an unused slot for the new uid. Probing is implemented via a second
396 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
399 * An odd number is sufficient to make sure each entry of the hash table gets
400 * probed for probe_num between 0 and s-1 because s is a power of two, hence
401 * the second hash value has never a common divisor with the hash table size.
402 * IOW: h is invertible in the ring [0..s-1].
404 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
406 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
407 % uid_hash_table_size;
410 static struct user_info *lookup_uid(uint32_t uid)
414 for (p = 0; p < uid_hash_table_size; p++) {
415 struct user_info *ui = uid_hash_table + double_hash(uid, p);
424 int create_user_table(uint32_t uid, struct user_info **ui_ptr)
426 struct user_info *ui = lookup_uid(uid);
429 return -E_HASH_TABLE_OVERFLOW;
434 ui->flags |= UI_FL_SLOT_USED;
435 return open_user_table(ui, 1);
438 static char *get_uid_list_name(void)
440 return make_message("%s/uid_list", conf.database_dir_arg);
443 static int (*hash_table_comparator)(struct user_info *a, struct user_info *b);
445 static int comp_wrapper(const void *a, const void *b)
447 struct user_info *x = uid_hash_table + *(unsigned *)a;
448 struct user_info *y = uid_hash_table + *(unsigned *)b;
449 return hash_table_comparator(x, y);
452 void sort_hash_table(int (*comp)(struct user_info *, struct user_info *))
454 hash_table_comparator = comp;
455 qsort(uid_hash_table_sort_idx, uid_hash_table_size,
456 sizeof(*uid_hash_table_sort_idx), comp_wrapper);
459 int open_admissible_user_tables(struct uid_range *admissible_uids)
461 struct user_info *ui;
463 assert(uid_hash_table);
464 DEBUG_LOG("size: %d\n", uid_hash_table_size);
470 if (!uid_is_admissible(ui->uid, admissible_uids)) {
471 DEBUG_LOG("uid %u is not admissible\n", ui->uid);
472 ui->flags &= ~UI_FL_ADMISSIBLE;
475 ui->flags |= UI_FL_ADMISSIBLE;
478 ret = open_user_table(ui, 0);
485 int read_uid_file(void)
489 char *filename = get_uid_list_name(), *map;
490 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
494 ERROR_LOG("failed to map %s\n", filename);
499 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
502 * Compute number of hash table bits. The hash table size must be a
503 * power of two and larger than the number of uids.
506 while (1 << bits < num_uids)
508 create_hash_table(bits);
509 for (n = 0; n < num_uids; n++) {
510 uint32_t uid = read_u32(map + n * sizeof(uid));
511 struct user_info *ui = lookup_uid(uid);
513 if (ui_used(ui)) { /* impossible */
514 ERROR_LOG("duplicate user id!?\n");
519 ui->flags |= UI_FL_SLOT_USED;
523 adu_munmap(map, size);
527 int write_uid_file(void)
529 char *buf, *p, *filename;
530 size_t size = num_uids * sizeof(uint32_t);
532 struct user_info *ui;
536 buf = p = adu_malloc(size);
540 write_u32(p, ui->uid);
541 p += sizeof(uint32_t);
543 filename = get_uid_list_name();
544 ret = adu_write_file(filename, buf, size);