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 */
172 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
174 struct uid_range *ur;
177 if (!urs) /* empty array means all uids are allowed */
179 FOR_EACH_UID_RANGE(ur, urs)
180 if (ur->low <= uid && ur->high >= uid)
184 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
189 int append_users(char **users, int num_users,
190 struct uid_range **admissible_uids, int num_uid_ranges)
193 struct uid_range *au = *admissible_uids;
195 for (i = 0; i < num_users; i++) {
197 struct uid_range *ur;
198 struct passwd *pw = getpwnam(u);
201 ERROR_LOG("user %s not found\n", u);
202 return -ERRNO_TO_ERROR(EINVAL);
204 if (au && uid_is_admissible(pw->pw_uid, au))
205 continue; /* nothing to do */
206 /* add a range consisting of this uid only */
208 au = adu_realloc(au, (num_uid_ranges + 1) *
209 sizeof(struct uid_range));
210 *admissible_uids = au;
211 ur = au + num_uid_ranges - 1; /* the new uid range */
212 ur->low = ur->high = pw->pw_uid;
213 /* terminate the list */
218 return num_uid_ranges;
221 static inline int ui_used(struct user_info *ui)
223 return ui->flags & UI_FL_SLOT_USED;
226 static inline int ui_admissible(struct user_info *ui)
228 return ui->flags & UI_FL_ADMISSIBLE;
231 static int open_user_table(struct user_info *ui, int create)
236 ui->desc = adu_malloc(sizeof(*ui->desc));
237 ui->desc->num_columns = NUM_UT_COLUMNS;
239 ui->desc->column_descriptions = user_table_cols;
240 ui->desc->dir = adu_strdup(conf.database_dir_arg);
241 ui->desc->name = make_message("%u", (unsigned)ui->uid);
242 pw = getpwuid(ui->uid);
243 if (pw && pw->pw_name)
244 ui->pw_name = adu_strdup(pw->pw_name);
246 INFO_LOG(".............................uid #%u: %u\n",
247 (unsigned)num_uids, (unsigned)ui->uid);
249 ret = osl(osl_create_table(ui->desc));
254 ret = osl(osl_open_table(ui->desc, &ui->table));
259 free((char *)ui->desc->name);
260 free((char *)ui->desc->dir);
263 ui->desc->name = NULL;
264 ui->desc->dir = NULL;
271 int for_each_admissible_user(int (*func)(struct user_info *, void *),
274 struct user_info *ui = uid_hash_table;
277 return -ERRNO_TO_ERROR(EFAULT);
279 for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
282 if (!ui_used(ui) || !ui_admissible(ui))
284 ret = func(ui, data);
291 #define PRIME1 0xb11924e1
292 #define PRIME2 0x01000193
294 void create_hash_table(unsigned bits)
296 uid_hash_table_size = 1 << bits;
297 uid_hash_table = adu_calloc(uid_hash_table_size *
298 sizeof(struct user_info));
301 void free_hash_table(void)
303 free(uid_hash_table);
304 uid_hash_table = NULL;
307 static int close_user_table(struct user_info *ui, __a_unused void *data)
311 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
313 ERROR_LOG("failed to close user table %u: %s\n",
314 (unsigned) ui->uid, adu_strerror(-ret));
315 free((char *)ui->desc->name);
316 ui->desc->name = NULL;
317 free((char *)ui->desc->dir);
318 ui->desc->dir = NULL;
328 void close_user_tables(void)
330 for_each_admissible_user(close_user_table, NULL);
334 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
335 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
336 * unused slots in the table are used to store different uids that hash to the
339 * If a hash collision occurs, different slots are successively probed in order
340 * to find an unused slot for the new uid. Probing is implemented via a second
341 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
344 * An odd number is sufficient to make sure each entry of the hash table gets
345 * probed for probe_num between 0 and s-1 because s is a power of two, hence
346 * the second hash value has never a common divisor with the hash table size.
347 * IOW: h is invertible in the ring [0..s].
349 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
351 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
352 % uid_hash_table_size;
355 int search_uid(uint32_t uid, struct uid_range *urs,
356 enum search_uid_flags flags, struct user_info **ui_ptr)
360 for (p = 0; p < uid_hash_table_size; p++) {
361 struct user_info *ui = uid_hash_table + double_hash(uid, p);
368 ui->flags |= UI_FL_SLOT_USED;
369 if (!uid_is_admissible(uid, urs))
371 ui->flags |= UI_FL_ADMISSIBLE;
372 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
386 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
389 static char *get_uid_list_name(void)
391 return make_message("%s/uid_list", conf.database_dir_arg);
394 void sort_hash_table(int (*comp)(const void *, const void *))
396 qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
400 int read_uid_file(struct uid_range *admissible_uids)
404 char *filename = get_uid_list_name(), *map;
405 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
409 INFO_LOG("failed to map %s\n", filename);
414 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
417 * Compute number of hash table bits. The hash table size must be a
418 * power of two and larger than the number of uids.
421 while (1 << bits < num_uids)
423 create_hash_table(bits);
424 for (n = 0; n < num_uids; n++) {
425 uint32_t uid = read_u32(map + n * sizeof(uid));
426 ret = search_uid(uid, admissible_uids, OPEN_USER_TABLE, NULL);
431 adu_munmap(map, size);
435 static int write_uid(struct user_info *ui, void *data)
439 write_u32(*p, ui->uid);
440 *p += sizeof(uint32_t);
444 int write_uid_file(void)
446 char *buf, *p, *filename;
447 size_t size = num_uids * sizeof(uint32_t);
452 buf = p = adu_malloc(size);
453 ret = for_each_admissible_user(write_uid, &p);
456 filename = get_uid_list_name();
457 ret = adu_write_file(filename, buf, size);