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 /** The number of used slots in the hash table. */
56 static uint32_t num_uids;
59 * The columns of the per-user tables.
61 * Adu tracks disk usage on a per-user basis. For each user, a user table is
62 * being created. The rows of the user table have three columns: The directory
63 * number that may be resolved to the path using the directory table, the
64 * number of bytes and the number of files in that directory owned by the given
67 static struct osl_column_description user_table_cols[] = {
69 .storage_type = OSL_MAPPED_STORAGE,
70 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
72 .compare_function = uint64_compare,
73 .data_size = sizeof(uint64_t)
76 .storage_type = OSL_MAPPED_STORAGE,
77 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
78 .compare_function = size_compare,
80 .data_size = sizeof(uint64_t)
83 .storage_type = OSL_MAPPED_STORAGE,
84 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
85 .compare_function = size_compare,
87 .data_size = sizeof(uint64_t)
91 static int check_uid_arg(const char *arg, uint32_t *uid)
93 const uint32_t max = ~0U;
95 * we need an 64-bit int for string -> uid conversion because strtoll()
96 * returns a signed value.
99 int ret = atoi64(arg, &val);
103 if (val < 0 || val > max)
104 return -ERRNO_TO_ERROR(EINVAL);
109 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
112 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
114 if (!p || p == arg) { /* -42 or 42 */
115 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
118 ur->low = p? 0 : ur->high;
125 ret = check_uid_arg(arg, &ur->low);
129 if (*p) { /* 42-4711 */
130 ret = check_uid_arg(p, &ur->high);
134 if (ur->low > ur->high)
135 ret = -ERRNO_TO_ERROR(EINVAL);
138 ERROR_LOG("bad uid option: %s\n", orig_arg);
140 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
147 * Convert the --uid argument to an array of uid ranges.
149 * \param orig_arg The argument to the --uid option.
150 * \param ur Result pointer.
152 * Returns Negative on errors. On success, the number of uid ranges
155 int parse_uid_arg(const char *orig_arg, struct uid_range **ur)
163 arg = adu_strdup(orig_arg);
164 n = split_args(arg, &argv, ",");
167 *ur = adu_malloc((n + 1) * sizeof(struct uid_range));
168 for (i = 0; i < n; i++) {
169 ret = parse_uid_range(argv[i], *ur + i);
180 /* an empty range indicates the end of the list */
186 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
188 struct uid_range *ur;
191 if (!urs) /* empty array means all uids are allowed */
193 FOR_EACH_UID_RANGE(ur, urs)
194 if (ur->low <= uid && ur->high >= uid)
198 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
204 * Add each given user to the array of admissible users.
206 * \param users Array of user names to add.
207 * \param num_users Length of \a users.
208 * \param admissible_uids The users which are already admissible.
209 * \param num_uid_ranges The number of intervals of \a admissible_uids.
211 * For each given user, the function checks whether that user is already
212 * admissible, i.e. its uid is contained in one of the ranges given by \a
213 * admissible_uids. If it is, the function ignores that user. Otherwise, a new
214 * length-one range consisting of that uid only is appended to \a
217 * \return Negative on errors, the new number of uid ranges on success.
219 int append_users(char **users, int num_users,
220 struct uid_range **admissible_uids, int num_uid_ranges)
223 struct uid_range *au = *admissible_uids;
225 for (i = 0; i < num_users; i++) {
227 struct uid_range *ur;
228 struct passwd *pw = getpwnam(u);
231 ERROR_LOG("user %s not found\n", u);
232 return -ERRNO_TO_ERROR(EINVAL);
234 if (au && uid_is_admissible(pw->pw_uid, au))
235 continue; /* nothing to do */
236 /* add a range consisting of this uid only */
238 au = adu_realloc(au, (num_uid_ranges + 1) *
239 sizeof(struct uid_range));
240 *admissible_uids = au;
241 ur = au + num_uid_ranges - 1; /* the new uid range */
242 ur->low = ur->high = pw->pw_uid;
243 /* terminate the list */
248 return num_uid_ranges;
251 static inline int ui_used(struct user_info *ui)
253 return ui->flags & UI_FL_SLOT_USED;
256 static inline int ui_admissible(struct user_info *ui)
258 return ui->flags & UI_FL_ADMISSIBLE;
261 static int open_user_table(struct user_info *ui, int create)
266 ui->desc = adu_malloc(sizeof(*ui->desc));
267 ui->desc->num_columns = NUM_UT_COLUMNS;
269 ui->desc->column_descriptions = user_table_cols;
270 ui->desc->dir = adu_strdup(conf.database_dir_arg);
271 ui->desc->name = make_message("%u", (unsigned)ui->uid);
272 pw = getpwuid(ui->uid);
273 if (pw && pw->pw_name)
274 ui->pw_name = adu_strdup(pw->pw_name);
276 INFO_LOG("opening table for uid %u\n", (unsigned)ui->uid);
278 ret = osl(osl_create_table(ui->desc));
283 ret = osl(osl_open_table(ui->desc, &ui->table));
288 free((char *)ui->desc->name);
289 free((char *)ui->desc->dir);
292 ui->desc->name = NULL;
293 ui->desc->dir = NULL;
300 /** Iterate over each user in the uid hash table. */
301 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui < \
302 uid_hash_table + uid_hash_table_size; ui++)
306 * Execute the given function for each admissible user.
308 * \param func The function to execute.
309 * \param data Arbitrary pointer.
311 * This function calls \a func for each admissible user in the uid hash table.
312 * The \a data pointer is passed as the second argument to \a func.
314 * \return As soon as \a func returns a negative value, the loop is terminated
315 * and that negative value is returned. Otherwise, the function returns 1.
317 int for_each_admissible_user(int (*func)(struct user_info *, void *),
322 assert(uid_hash_table);
323 for (i = 0; i < uid_hash_table_size; i++) {
325 struct user_info *ui = uid_hash_table + i;
327 if (!ui_used(ui) || !ui_admissible(ui))
329 ret = func(ui, data);
336 /** Prime number used for calculating the slot for an uid. */
337 #define PRIME1 0xb11924e1
338 /** Prime number used for probe all slots. */
339 #define PRIME2 0x01000193
342 * Create a hash table large enough of given size.
344 * \param bits Sets the maximal number of hash table entries to ^bits.
346 void create_hash_table(unsigned bits)
348 uid_hash_table_size = 1 << bits;
349 uid_hash_table = adu_calloc(uid_hash_table_size *
350 sizeof(struct user_info));
354 * Close all open user tables and destroy the uid hash table.
356 * For each used slot in the uid hash table, close the osl user table if it is
357 * open. Finally, free the uid hash table.
359 void close_user_tables(void)
361 struct user_info *ui;
370 INFO_LOG("closing user table for uid %u\n", (unsigned)ui->uid);
371 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
373 ERROR_LOG("failed to close user table %u: %s\n",
374 (unsigned)ui->uid, adu_strerror(-ret));
375 free((char *)ui->desc->name);
376 ui->desc->name = NULL;
377 free((char *)ui->desc->dir);
378 ui->desc->dir = NULL;
386 free(uid_hash_table);
387 uid_hash_table = NULL;
391 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
392 * interval [0..s-1]. Hash collisions are treated by open addressing, i.e.
393 * unused slots in the table are used to store different uids that hash to the
396 * If a hash collision occurs, different slots are successively probed in order
397 * to find an unused slot for the new uid. Probing is implemented via a second
398 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
401 * An odd number is sufficient to make sure each entry of the hash table gets
402 * probed for probe_num between 0 and s-1 because s is a power of two, hence
403 * the second hash value has never a common divisor with the hash table size.
404 * IOW: h is invertible in the ring [0..s-1].
406 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
408 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
409 % uid_hash_table_size;
412 static struct user_info *lookup_uid(uint32_t uid)
416 for (p = 0; p < uid_hash_table_size; p++) {
417 struct user_info *ui = uid_hash_table + double_hash(uid, p);
427 * Create and open a osl table for the given uid.
429 * \param uid The user ID.
430 * \param ui_ptr Result pointer
432 * Find out whether \a uid already exists in the uid hash table. If yes, just
433 * return the user info struct via \a ui_ptr. Otherwise, insert \a uid into the
434 * uid hash table, create and open the osl user table and also return the user
435 * info struct via \a ui_ptr.
439 int create_user_table(uint32_t uid, struct user_info **ui_ptr)
441 struct user_info *ui = lookup_uid(uid);
444 return -E_HASH_TABLE_OVERFLOW;
449 ui->flags |= UI_FL_SLOT_USED;
450 return open_user_table(ui, 1);
453 static char *get_uid_list_name(void)
455 return make_message("%s/uid_list", conf.database_dir_arg);
458 * Open the osl tables for all admissible uids.
460 * \param admissible_uids Determines which uids are considered admissible.
462 * Each slot in the hash table contains, among other information, a bit which
463 * specifies whether the uid of the slot is admissible in the current context.
465 * This function iterates over all entries in the hash table and checks for
466 * each used slot whether the corresponding uid is admissible with respect to
467 * \a admissible_uids. If so, it sets the admissible bit for this slot and
468 * opens the osl table of the uid.
472 int open_admissible_user_tables(struct uid_range *admissible_uids)
474 struct user_info *ui;
476 assert(uid_hash_table);
477 DEBUG_LOG("size: %d\n", uid_hash_table_size);
483 if (!uid_is_admissible(ui->uid, admissible_uids)) {
484 DEBUG_LOG("uid %u is not admissible\n", ui->uid);
485 ui->flags &= ~UI_FL_ADMISSIBLE;
488 ui->flags |= UI_FL_ADMISSIBLE;
491 ret = open_user_table(ui, 0);
499 * Read the file of all possible uids.
501 * This is called from select/interactive mode. First a large hash table, large
502 * enough to store all uids contained in the uid file is created. Next, the
503 * uids are read from the uid file which was created during the creation of the
504 * database and each uid is inserted into the hash table.
506 * \sa write_uid_file().
510 int read_uid_file(void)
514 char *filename = get_uid_list_name(), *map;
515 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
519 ERROR_LOG("failed to map %s\n", filename);
524 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
527 * Compute number of hash table bits. The hash table size must be a
528 * power of two and larger than the number of uids.
531 while (1 << bits < num_uids)
533 create_hash_table(bits);
534 for (n = 0; n < num_uids; n++) {
535 uint32_t uid = read_u32(map + n * sizeof(uid));
536 struct user_info *ui = lookup_uid(uid);
538 if (ui_used(ui)) { /* impossible */
539 ERROR_LOG("duplicate user id!?\n");
544 ui->flags |= UI_FL_SLOT_USED;
548 adu_munmap(map, size);
553 * Write the list of uids to permanent storage.
555 * This is called from create mode after the dir table and all uer tables have
556 * been created. The file simply contains the list of all uids that own at
557 * least one regular file in the base directory and hence an osl table for this
560 * \sa read_uid_file().
564 int write_uid_file(void)
566 char *buf, *p, *filename;
567 size_t size = num_uids * sizeof(uint32_t);
569 struct user_info *ui;
573 buf = p = adu_malloc(size);
577 write_u32(p, ui->uid);
578 p += sizeof(uint32_t);
580 filename = get_uid_list_name();
581 ret = adu_write_file(filename, buf, size);