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;
54 * The columns of the per-user tables.
56 * Adu tracks disk usage on a per-user basis. For each user, a user table is
57 * being created. The rows of the user table have three columns: The directory
58 * number that may be resolved to the path using the directory table, the
59 * number of bytes and the number of files in that directory owned by the given
62 static struct osl_column_description user_table_cols[] = {
64 .storage_type = OSL_MAPPED_STORAGE,
65 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
67 .compare_function = uint64_compare,
68 .data_size = sizeof(uint64_t)
71 .storage_type = OSL_MAPPED_STORAGE,
72 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
73 .compare_function = size_compare,
75 .data_size = sizeof(uint64_t)
78 .storage_type = OSL_MAPPED_STORAGE,
79 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
80 .compare_function = size_compare,
82 .data_size = sizeof(uint64_t)
86 static int check_uid_arg(const char *arg, uint32_t *uid)
88 const uint32_t max = ~0U;
90 * we need an 64-bit int for string -> uid conversion because strtoll()
91 * returns a signed value.
94 int ret = atoi64(arg, &val);
98 if (val < 0 || val > max)
99 return -ERRNO_TO_ERROR(EINVAL);
104 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
107 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
109 if (!p || p == arg) { /* -42 or 42 */
110 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
113 ur->low = p? 0 : ur->high;
120 ret = check_uid_arg(arg, &ur->low);
124 if (*p) { /* 42-4711 */
125 ret = check_uid_arg(p, &ur->high);
129 if (ur->low > ur->high)
130 ret = -ERRNO_TO_ERROR(EINVAL);
133 ERROR_LOG("bad uid option: %s\n", orig_arg);
135 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
141 int parse_uid_arg(const char *orig_arg, struct uid_range **ur)
149 arg = adu_strdup(orig_arg);
150 n = split_args(arg, &argv, ",");
153 *ur = adu_malloc((n + 1) * sizeof(struct uid_range));
154 for (i = 0; i < n; i++) {
155 ret = parse_uid_range(argv[i], *ur + i);
166 /* an empty range indicates the end of the list */
173 static inline int ui_used(struct user_info *ui)
175 return ui->flags & UI_FL_SLOT_USED;
178 static inline int ui_admissible(struct user_info *ui)
180 return ui->flags & UI_FL_ADMISSIBLE;
183 static int open_user_table(struct user_info *ui, int create)
188 ui->desc = adu_malloc(sizeof(*ui->desc));
189 ui->desc->num_columns = NUM_UT_COLUMNS;
191 ui->desc->column_descriptions = user_table_cols;
192 ui->desc->dir = adu_strdup(conf.database_dir_arg);
193 ui->desc->name = make_message("%u", (unsigned)ui->uid);
194 pw = getpwuid(ui->uid);
195 if (pw && pw->pw_name)
196 ui->pw_name = adu_strdup(pw->pw_name);
198 INFO_LOG(".............................uid #%u: %u\n",
199 (unsigned)num_uids, (unsigned)ui->uid);
201 ret = osl(osl_create_table(ui->desc));
206 ret = osl(osl_open_table(ui->desc, &ui->table));
211 free((char *)ui->desc->name);
212 free((char *)ui->desc->dir);
215 ui->desc->name = NULL;
216 ui->desc->dir = NULL;
223 int for_each_admissible_user(int (*func)(struct user_info *, void *),
226 struct user_info *ui = uid_hash_table;
229 return -ERRNO_TO_ERROR(EFAULT);
231 for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
234 if (!ui_used(ui) || !ui_admissible(ui))
236 ret = func(ui, data);
243 #define PRIME1 0xb11924e1
244 #define PRIME2 0x01000193
246 void create_hash_table(unsigned bits)
248 uid_hash_table_size = 1 << bits;
249 uid_hash_table = adu_calloc(uid_hash_table_size *
250 sizeof(struct user_info));
253 void free_hash_table(void)
255 free(uid_hash_table);
256 uid_hash_table = NULL;
259 static int close_user_table(struct user_info *ui, __a_unused void *data)
263 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
265 ERROR_LOG("failed to close user table %u: %s\n",
266 (unsigned) ui->uid, adu_strerror(-ret));
267 free((char *)ui->desc->name);
268 ui->desc->name = NULL;
269 free((char *)ui->desc->dir);
270 ui->desc->dir = NULL;
280 void close_user_tables(void)
282 for_each_admissible_user(close_user_table, NULL);
286 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
287 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
288 * unused slots in the table are used to store different uids that hash to the
291 * If a hash collision occurs, different slots are successively probed in order
292 * to find an unused slot for the new uid. Probing is implemented via a second
293 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
296 * An odd number is sufficient to make sure each entry of the hash table gets
297 * probed for probe_num between 0 and s-1 because s is a power of two, hence
298 * the second hash value has never a common divisor with the hash table size.
299 * IOW: h is invertible in the ring [0..s].
301 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
303 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
304 % uid_hash_table_size;
307 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
309 struct uid_range *ur;
312 if (!urs) /* empty array means all uids are allowed */
314 FOR_EACH_UID_RANGE(ur, urs)
315 if (ur->low <= uid && ur->high >= uid)
319 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
324 int search_uid(uint32_t uid, struct uid_range *urs,
325 enum search_uid_flags flags, struct user_info **ui_ptr)
329 for (p = 0; p < uid_hash_table_size; p++) {
330 struct user_info *ui = uid_hash_table + double_hash(uid, p);
337 ui->flags |= UI_FL_SLOT_USED;
338 if (!uid_is_admissible(uid, urs))
340 ui->flags |= UI_FL_ADMISSIBLE;
341 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
355 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
358 static char *get_uid_list_name(void)
360 return make_message("%s/uid_list", conf.database_dir_arg);
363 void sort_hash_table(int (*comp)(const void *, const void *))
365 qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
369 int read_uid_file(struct uid_range *admissible_uids)
373 char *filename = get_uid_list_name(), *map;
374 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
378 INFO_LOG("failed to map %s\n", filename);
383 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
386 * Compute number of hash table bits. The hash table size must be a
387 * power of two and larger than the number of uids.
390 while (1 << bits < num_uids)
392 create_hash_table(bits);
393 for (n = 0; n < num_uids; n++) {
394 uint32_t uid = read_u32(map + n * sizeof(uid));
395 ret = search_uid(uid, admissible_uids, OPEN_USER_TABLE, NULL);
400 adu_munmap(map, size);
404 static int write_uid(struct user_info *ui, void *data)
408 write_u32(*p, ui->uid);
409 *p += sizeof(uint32_t);
413 int write_uid_file(void)
415 char *buf, *p, *filename;
416 size_t size = num_uids * sizeof(uint32_t);
421 buf = p = adu_malloc(size);
422 ret = for_each_admissible_user(write_uid, &p);
425 filename = get_uid_list_name();
426 ret = adu_write_file(filename, buf, size);