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 /** The number of used slots in the hash table. */
54 static uint32_t num_uids = 0;
57 * The columns of the per-user tables.
59 * Adu tracks disk usage on a per-user basis. For each user, a user table is
60 * being created. The rows of the user table have three columns: The directory
61 * number that may be resolved to the path using the directory table, the
62 * number of bytes and the number of files in that directory owned by the given
65 static struct osl_column_description user_table_cols[] = {
67 .storage_type = OSL_MAPPED_STORAGE,
68 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
70 .compare_function = uint64_compare,
71 .data_size = sizeof(uint64_t)
74 .storage_type = OSL_MAPPED_STORAGE,
75 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
76 .compare_function = size_compare,
78 .data_size = sizeof(uint64_t)
81 .storage_type = OSL_MAPPED_STORAGE,
82 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
83 .compare_function = size_compare,
85 .data_size = sizeof(uint64_t)
89 static int check_uid_arg(const char *arg, uint32_t *uid)
91 const uint32_t max = ~0U;
93 * we need an 64-bit int for string -> uid conversion because strtoll()
94 * returns a signed value.
97 int ret = atoi64(arg, &val);
101 if (val < 0 || val > max)
102 return -ERRNO_TO_ERROR(EINVAL);
107 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
110 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
112 if (!p || p == arg) { /* -42 or 42 */
113 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
116 ur->low = p? 0 : ur->high;
123 ret = check_uid_arg(arg, &ur->low);
127 if (*p) { /* 42-4711 */
128 ret = check_uid_arg(p, &ur->high);
132 if (ur->low > ur->high)
133 ret = -ERRNO_TO_ERROR(EINVAL);
136 ERROR_LOG("bad uid option: %s\n", orig_arg);
138 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
144 int parse_uid_arg(const char *orig_arg, struct uid_range **ur)
152 arg = adu_strdup(orig_arg);
153 n = split_args(arg, &argv, ",");
156 *ur = adu_malloc((n + 1) * sizeof(struct uid_range));
157 for (i = 0; i < n; i++) {
158 ret = parse_uid_range(argv[i], *ur + i);
169 /* an empty range indicates the end of the list */
175 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
177 struct uid_range *ur;
180 if (!urs) /* empty array means all uids are allowed */
182 FOR_EACH_UID_RANGE(ur, urs)
183 if (ur->low <= uid && ur->high >= uid)
187 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
192 int append_users(char **users, int num_users,
193 struct uid_range **admissible_uids, int num_uid_ranges)
196 struct uid_range *au = *admissible_uids;
198 for (i = 0; i < num_users; i++) {
200 struct uid_range *ur;
201 struct passwd *pw = getpwnam(u);
204 ERROR_LOG("user %s not found\n", u);
205 return -ERRNO_TO_ERROR(EINVAL);
207 if (au && uid_is_admissible(pw->pw_uid, au))
208 continue; /* nothing to do */
209 /* add a range consisting of this uid only */
211 au = adu_realloc(au, (num_uid_ranges + 1) *
212 sizeof(struct uid_range));
213 *admissible_uids = au;
214 ur = au + num_uid_ranges - 1; /* the new uid range */
215 ur->low = ur->high = pw->pw_uid;
216 /* terminate the list */
221 return num_uid_ranges;
224 static inline int ui_used(struct user_info *ui)
226 return ui->flags & UI_FL_SLOT_USED;
229 static inline int ui_admissible(struct user_info *ui)
231 return ui->flags & UI_FL_ADMISSIBLE;
234 static int open_user_table(struct user_info *ui, int create)
239 ui->desc = adu_malloc(sizeof(*ui->desc));
240 ui->desc->num_columns = NUM_UT_COLUMNS;
242 ui->desc->column_descriptions = user_table_cols;
243 ui->desc->dir = adu_strdup(conf.database_dir_arg);
244 ui->desc->name = make_message("%u", (unsigned)ui->uid);
245 pw = getpwuid(ui->uid);
246 if (pw && pw->pw_name)
247 ui->pw_name = adu_strdup(pw->pw_name);
249 INFO_LOG("opening table for uid %u\n", (unsigned)ui->uid);
251 ret = osl(osl_create_table(ui->desc));
256 ret = osl(osl_open_table(ui->desc, &ui->table));
261 free((char *)ui->desc->name);
262 free((char *)ui->desc->dir);
265 ui->desc->name = NULL;
266 ui->desc->dir = NULL;
273 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui < \
274 uid_hash_table + uid_hash_table_size; ui++)
276 int for_each_admissible_user(int (*func)(struct user_info *, void *),
279 struct user_info *ui;
281 assert(uid_hash_table);
285 if (!ui_used(ui) || !ui_admissible(ui))
287 ret = func(ui, data);
294 #define PRIME1 0xb11924e1
295 #define PRIME2 0x01000193
297 void create_hash_table(unsigned bits)
299 uid_hash_table_size = 1 << bits;
300 uid_hash_table = adu_calloc(uid_hash_table_size *
301 sizeof(struct user_info));
304 void close_user_tables(void)
306 struct user_info *ui;
315 INFO_LOG("closing user table for uid %u\n", (unsigned)ui->uid);
316 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
318 ERROR_LOG("failed to close user table %u: %s\n",
319 (unsigned)ui->uid, adu_strerror(-ret));
320 free((char *)ui->desc->name);
321 ui->desc->name = NULL;
322 free((char *)ui->desc->dir);
323 ui->desc->dir = NULL;
331 free(uid_hash_table);
332 uid_hash_table = NULL;
336 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
337 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
338 * unused slots in the table are used to store different uids that hash to the
341 * If a hash collision occurs, different slots are successively probed in order
342 * to find an unused slot for the new uid. Probing is implemented via a second
343 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
346 * An odd number is sufficient to make sure each entry of the hash table gets
347 * probed for probe_num between 0 and s-1 because s is a power of two, hence
348 * the second hash value has never a common divisor with the hash table size.
349 * IOW: h is invertible in the ring [0..s].
351 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
353 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
354 % uid_hash_table_size;
357 static struct user_info *lookup_uid(uint32_t uid)
361 for (p = 0; p < uid_hash_table_size; p++) {
362 struct user_info *ui = uid_hash_table + double_hash(uid, p);
371 int create_user_table(uint32_t uid, struct user_info **ui_ptr)
373 struct user_info *ui = lookup_uid(uid);
376 return -E_HASH_TABLE_OVERFLOW;
381 ui->flags |= UI_FL_SLOT_USED;
382 return open_user_table(ui, 1);
385 static char *get_uid_list_name(void)
387 return make_message("%s/uid_list", conf.database_dir_arg);
390 void sort_hash_table(int (*comp)(const void *, const void *))
392 qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
396 int open_admissible_user_tables(struct uid_range *admissible_uids)
398 struct user_info *ui;
400 assert(uid_hash_table);
401 DEBUG_LOG("size: %d\n", uid_hash_table_size);
408 if (!uid_is_admissible(ui->uid, admissible_uids)) {
409 DEBUG_LOG("uid %u is not admissible\n", ui->uid);
410 ui->flags &= ~UI_FL_ADMISSIBLE;
413 ui->flags |= UI_FL_ADMISSIBLE;
416 ret = open_user_table(ui, 0);
423 int read_uid_file(void)
427 char *filename = get_uid_list_name(), *map;
428 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
432 ERROR_LOG("failed to map %s\n", filename);
437 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
440 * Compute number of hash table bits. The hash table size must be a
441 * power of two and larger than the number of uids.
444 while (1 << bits < num_uids)
446 create_hash_table(bits);
447 for (n = 0; n < num_uids; n++) {
448 uint32_t uid = read_u32(map + n * sizeof(uid));
449 struct user_info *ui = lookup_uid(uid);
451 if (ui_used(ui)) { /* impossible */
452 ERROR_LOG("duplicate user id!?\n");
457 ui->flags |= UI_FL_SLOT_USED;
461 adu_munmap(map, size);
465 int write_uid_file(void)
467 char *buf, *p, *filename;
468 size_t size = num_uids * sizeof(uint32_t);
470 struct user_info *ui;
474 buf = p = adu_malloc(size);
478 write_u32(p, ui->uid);
479 p += sizeof(uint32_t);
481 filename = get_uid_list_name();
482 ret = adu_write_file(filename, buf, size);