2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file user.c uid 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 #define FOR_EACH_UID_RANGE(ur, urs) for (ur = urs; ur->low <= ur->high; ur++)
34 /** Flags for the user hash table. */
36 /** Whether this slot of the hash table is used. */
38 /** Whether this uid should be taken into account. */
42 * Contains info for each user that owns at least one regular file.
44 * Even users that are not taken into account because of the --uid
45 * option occupy a slot in this hash table. This allows to find out
46 * quicky whether a uid is admissible. And yes, this has to be fast.
48 static struct user_info *uid_hash_table;
50 /** This is always a power of two. It is set in create_hash_table(). */
51 static uint32_t uid_hash_table_size;
53 /* Array of indices to the entries of \a uid_hash_table. */
54 static int *uid_hash_table_sort_idx;
56 /** The number of used slots in the hash table. */
57 static uint32_t num_uids;
60 * The columns of the per-user tables.
62 * Adu tracks disk usage on a per-user basis. For each user, a user table is
63 * being created. The rows of the user table have three columns: The directory
64 * number that may be resolved to the path using the directory table, the
65 * number of bytes and the number of files in that directory owned by the given
68 static struct osl_column_description user_table_cols[] = {
70 .storage_type = OSL_MAPPED_STORAGE,
71 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
73 .compare_function = uint64_compare,
74 .data_size = sizeof(uint64_t)
77 .storage_type = OSL_MAPPED_STORAGE,
78 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
79 .compare_function = size_compare,
81 .data_size = sizeof(uint64_t)
84 .storage_type = OSL_MAPPED_STORAGE,
85 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
86 .compare_function = size_compare,
88 .data_size = sizeof(uint64_t)
92 static int check_uid_arg(const char *arg, uint32_t *uid)
94 const uint32_t max = ~0U;
96 * we need an 64-bit int for string -> uid conversion because strtoll()
97 * returns a signed value.
100 int ret = atoi64(arg, &val);
104 if (val < 0 || val > max)
105 return -ERRNO_TO_ERROR(EINVAL);
110 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
113 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
115 if (!p || p == arg) { /* -42 or 42 */
116 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
119 ur->low = p? 0 : ur->high;
126 ret = check_uid_arg(arg, &ur->low);
130 if (*p) { /* 42-4711 */
131 ret = check_uid_arg(p, &ur->high);
135 if (ur->low > ur->high)
136 ret = -ERRNO_TO_ERROR(EINVAL);
139 ERROR_LOG("bad uid option: %s\n", orig_arg);
141 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
147 int parse_uid_arg(const char *orig_arg, struct uid_range **ur)
155 arg = adu_strdup(orig_arg);
156 n = split_args(arg, &argv, ",");
159 *ur = adu_malloc((n + 1) * sizeof(struct uid_range));
160 for (i = 0; i < n; i++) {
161 ret = parse_uid_range(argv[i], *ur + i);
172 /* an empty range indicates the end of the list */
178 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
180 struct uid_range *ur;
183 if (!urs) /* empty array means all uids are allowed */
185 FOR_EACH_UID_RANGE(ur, urs)
186 if (ur->low <= uid && ur->high >= uid)
190 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
195 int append_users(char **users, int num_users,
196 struct uid_range **admissible_uids, int num_uid_ranges)
199 struct uid_range *au = *admissible_uids;
201 for (i = 0; i < num_users; i++) {
203 struct uid_range *ur;
204 struct passwd *pw = getpwnam(u);
207 ERROR_LOG("user %s not found\n", u);
208 return -ERRNO_TO_ERROR(EINVAL);
210 if (au && uid_is_admissible(pw->pw_uid, au))
211 continue; /* nothing to do */
212 /* add a range consisting of this uid only */
214 au = adu_realloc(au, (num_uid_ranges + 1) *
215 sizeof(struct uid_range));
216 *admissible_uids = au;
217 ur = au + num_uid_ranges - 1; /* the new uid range */
218 ur->low = ur->high = pw->pw_uid;
219 /* terminate the list */
224 return num_uid_ranges;
227 static inline int ui_used(struct user_info *ui)
229 return ui->flags & UI_FL_SLOT_USED;
232 static inline int ui_admissible(struct user_info *ui)
234 return ui->flags & UI_FL_ADMISSIBLE;
237 static int open_user_table(struct user_info *ui, int create)
242 ui->desc = adu_malloc(sizeof(*ui->desc));
243 ui->desc->num_columns = NUM_UT_COLUMNS;
245 ui->desc->column_descriptions = user_table_cols;
246 ui->desc->dir = adu_strdup(conf.database_dir_arg);
247 ui->desc->name = make_message("%u", (unsigned)ui->uid);
248 pw = getpwuid(ui->uid);
249 if (pw && pw->pw_name)
250 ui->pw_name = adu_strdup(pw->pw_name);
252 INFO_LOG("opening table for uid %u\n", (unsigned)ui->uid);
254 ret = osl(osl_create_table(ui->desc));
259 ret = osl(osl_open_table(ui->desc, &ui->table));
264 free((char *)ui->desc->name);
265 free((char *)ui->desc->dir);
268 ui->desc->name = NULL;
269 ui->desc->dir = NULL;
276 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui < \
277 uid_hash_table + uid_hash_table_size; ui++)
279 int for_each_admissible_user(int (*func)(struct user_info *, void *),
284 assert(uid_hash_table);
285 for (i = 0; i < uid_hash_table_size; i++) {
287 struct user_info *ui = uid_hash_table +
288 uid_hash_table_sort_idx[i];
290 if (!ui_used(ui) || !ui_admissible(ui))
292 ret = func(ui, data);
299 #define PRIME1 0xb11924e1
300 #define PRIME2 0x01000193
302 void create_hash_table(unsigned bits)
306 uid_hash_table_size = 1 << bits;
307 uid_hash_table = adu_calloc(uid_hash_table_size *
308 sizeof(struct user_info));
309 uid_hash_table_sort_idx = adu_malloc(uid_hash_table_size * sizeof(int));
310 for (i = 0; i < uid_hash_table_size; i++)
311 uid_hash_table_sort_idx[i] = i;
314 void close_user_tables(void)
316 struct user_info *ui;
325 INFO_LOG("closing user table for uid %u\n", (unsigned)ui->uid);
326 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
328 ERROR_LOG("failed to close user table %u: %s\n",
329 (unsigned)ui->uid, adu_strerror(-ret));
330 free((char *)ui->desc->name);
331 ui->desc->name = NULL;
332 free((char *)ui->desc->dir);
333 ui->desc->dir = NULL;
341 free(uid_hash_table);
342 uid_hash_table = NULL;
343 free(uid_hash_table_sort_idx);
344 uid_hash_table_sort_idx = NULL;
348 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
349 * interval [0..s-1]. Hash collisions are treated by open addressing, i.e.
350 * unused slots in the table are used to store different uids that hash to the
353 * If a hash collision occurs, different slots are successively probed in order
354 * to find an unused slot for the new uid. Probing is implemented via a second
355 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
358 * An odd number is sufficient to make sure each entry of the hash table gets
359 * probed for probe_num between 0 and s-1 because s is a power of two, hence
360 * the second hash value has never a common divisor with the hash table size.
361 * IOW: h is invertible in the ring [0..s-1].
363 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
365 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
366 % uid_hash_table_size;
369 static struct user_info *lookup_uid(uint32_t uid)
373 for (p = 0; p < uid_hash_table_size; p++) {
374 struct user_info *ui = uid_hash_table + double_hash(uid, p);
383 int create_user_table(uint32_t uid, struct user_info **ui_ptr)
385 struct user_info *ui = lookup_uid(uid);
388 return -E_HASH_TABLE_OVERFLOW;
393 ui->flags |= UI_FL_SLOT_USED;
394 return open_user_table(ui, 1);
397 static char *get_uid_list_name(void)
399 return make_message("%s/uid_list", conf.database_dir_arg);
402 static int (*hash_table_comparator)(struct user_info *a, struct user_info *b);
404 static int comp_wrapper(const void *a, const void *b)
406 struct user_info *x = uid_hash_table + *(unsigned *)a;
407 struct user_info *y = uid_hash_table + *(unsigned *)b;
408 return hash_table_comparator(x, y);
411 void sort_hash_table(int (*comp)(struct user_info *, struct user_info *))
413 hash_table_comparator = comp;
414 qsort(uid_hash_table_sort_idx, uid_hash_table_size,
415 sizeof(*uid_hash_table_sort_idx), comp_wrapper);
418 int open_admissible_user_tables(struct uid_range *admissible_uids)
420 struct user_info *ui;
422 assert(uid_hash_table);
423 DEBUG_LOG("size: %d\n", uid_hash_table_size);
429 if (!uid_is_admissible(ui->uid, admissible_uids)) {
430 DEBUG_LOG("uid %u is not admissible\n", ui->uid);
431 ui->flags &= ~UI_FL_ADMISSIBLE;
434 ui->flags |= UI_FL_ADMISSIBLE;
437 ret = open_user_table(ui, 0);
444 int read_uid_file(void)
448 char *filename = get_uid_list_name(), *map;
449 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
453 ERROR_LOG("failed to map %s\n", filename);
458 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
461 * Compute number of hash table bits. The hash table size must be a
462 * power of two and larger than the number of uids.
465 while (1 << bits < num_uids)
467 create_hash_table(bits);
468 for (n = 0; n < num_uids; n++) {
469 uint32_t uid = read_u32(map + n * sizeof(uid));
470 struct user_info *ui = lookup_uid(uid);
472 if (ui_used(ui)) { /* impossible */
473 ERROR_LOG("duplicate user id!?\n");
478 ui->flags |= UI_FL_SLOT_USED;
482 adu_munmap(map, size);
486 int write_uid_file(void)
488 char *buf, *p, *filename;
489 size_t size = num_uids * sizeof(uint32_t);
491 struct user_info *ui;
495 buf = p = adu_malloc(size);
499 write_u32(p, ui->uid);
500 p += sizeof(uint32_t);
502 filename = get_uid_list_name();
503 ret = adu_write_file(filename, buf, size);