compute_user_summary does not need any private data.
[adu.git] / user.c
1 /*
2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file user.c uid User and user ID handling. */
8
9 #include "adu.h"
10 #include <dirent.h> /* readdir() */
11 #include <sys/types.h>
12 #include <pwd.h>
13 #include "cmdline.h" /* TODO: This file should be independent of command line options */
14 #include "user.h"
15 #include "fd.h"
16 #include "string.h"
17 #include "error.h"
18
19 /**
20 * Describes one range of admissible user IDs.
21 *
22 * adu converts the admissible user ids given at the command line
23 * into an array of such structs.
24 */
25 struct uid_range {
26 /** Lowest admissible user ID. */
27 uint32_t low;
28 /** Greatest admissible user ID. */
29 uint32_t high;
30 };
31
32 #define FOR_EACH_UID_RANGE(ur, urs) for (ur = urs; ur->low <= ur->high; ur++)
33
34 /** Flags for the user hash table. */
35 enum uid_info_flags {
36 /** Whether this slot of the hash table is used. */
37 UI_FL_SLOT_USED = 1,
38 /** Whether this uid should be taken into account. */
39 UI_FL_ADMISSIBLE = 2,
40 };
41 /*
42 * Contains info for each user that owns at least one regular file.
43 *
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.
47 */
48 static struct user_info *uid_hash_table;
49
50 /** This is always a power of two. It is set in create_hash_table(). */
51 static uint32_t uid_hash_table_size;
52
53 /* Array of indices to the entries of \a uid_hash_table. */
54 static int *uid_hash_table_sort_idx;
55
56 /** The number of used slots in the hash table. */
57 static uint32_t num_uids = 0;
58
59 /*
60 * The columns of the per-user tables.
61 *
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
66 * user.
67 */
68 static struct osl_column_description user_table_cols[] = {
69 [UT_DIR_NUM] = {
70 .storage_type = OSL_MAPPED_STORAGE,
71 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
72 .name = "dir_num",
73 .compare_function = uint64_compare,
74 .data_size = sizeof(uint64_t)
75 },
76 [UT_BYTES] = {
77 .storage_type = OSL_MAPPED_STORAGE,
78 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
79 .compare_function = size_compare,
80 .name = "num_bytes",
81 .data_size = sizeof(uint64_t)
82 },
83 [UT_FILES] = {
84 .storage_type = OSL_MAPPED_STORAGE,
85 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
86 .compare_function = size_compare,
87 .name = "num_files",
88 .data_size = sizeof(uint64_t)
89 },
90 };
91
92 static int check_uid_arg(const char *arg, uint32_t *uid)
93 {
94 const uint32_t max = ~0U;
95 /*
96 * we need an 64-bit int for string -> uid conversion because strtoll()
97 * returns a signed value.
98 */
99 int64_t val;
100 int ret = atoi64(arg, &val);
101
102 if (ret < 0)
103 return ret;
104 if (val < 0 || val > max)
105 return -ERRNO_TO_ERROR(EINVAL);
106 *uid = val;
107 return 1;
108 }
109
110 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
111 {
112 int ret;
113 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
114
115 if (!p || p == arg) { /* -42 or 42 */
116 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
117 if (ret < 0)
118 goto out;
119 ur->low = p? 0 : ur->high;
120 ret = 1;
121 goto out;
122 }
123 /* 42- or 42-4711 */
124 *p = '\0';
125 p++;
126 ret = check_uid_arg(arg, &ur->low);
127 if (ret < 0)
128 goto out;
129 ur->high = ~0U;
130 if (*p) { /* 42-4711 */
131 ret = check_uid_arg(p, &ur->high);
132 if (ret < 0)
133 goto out;
134 }
135 if (ur->low > ur->high)
136 ret = -ERRNO_TO_ERROR(EINVAL);
137 out:
138 if (ret < 0)
139 ERROR_LOG("bad uid option: %s\n", orig_arg);
140 else
141 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
142 ur->high);
143 free(arg);
144 return ret;
145 }
146
147 int parse_uid_arg(const char *orig_arg, struct uid_range **ur)
148 {
149 char *arg, **argv;
150 unsigned n;
151 int i, ret = 1;
152
153 if (!orig_arg)
154 return 0;
155 arg = adu_strdup(orig_arg);
156 n = split_args(arg, &argv, ",");
157 if (!n)
158 return -E_SYNTAX;
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);
162 if (ret < 0)
163 break;
164 }
165 free(argv);
166 free(arg);
167 if (ret < 0) {
168 free(*ur);
169 *ur = NULL;
170 return ret;
171 }
172 /* an empty range indicates the end of the list */
173 (*ur)[n].low = 1;
174 (*ur)[n].high = 0;
175 return n;
176 }
177
178 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
179 {
180 struct uid_range *ur;
181 int ret = 1;
182
183 if (!urs) /* empty array means all uids are allowed */
184 return 1;
185 FOR_EACH_UID_RANGE(ur, urs)
186 if (ur->low <= uid && ur->high >= uid)
187 goto out;
188 ret = 0;
189 out:
190 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
191 ret? "" : "not ");
192 return ret;
193 }
194
195 int append_users(char **users, int num_users,
196 struct uid_range **admissible_uids, int num_uid_ranges)
197 {
198 int i;
199 struct uid_range *au = *admissible_uids;
200
201 for (i = 0; i < num_users; i++) {
202 char *u = users[i];
203 struct uid_range *ur;
204 struct passwd *pw = getpwnam(u);
205
206 if (!pw) {
207 ERROR_LOG("user %s not found\n", u);
208 return -ERRNO_TO_ERROR(EINVAL);
209 }
210 if (au && uid_is_admissible(pw->pw_uid, au))
211 continue; /* nothing to do */
212 /* add a range consisting of this uid only */
213 num_uid_ranges++;
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 */
220 ur++;
221 ur->low = 1;
222 ur->high = 0;
223 }
224 return num_uid_ranges;
225 }
226
227 static inline int ui_used(struct user_info *ui)
228 {
229 return ui->flags & UI_FL_SLOT_USED;
230 }
231
232 static inline int ui_admissible(struct user_info *ui)
233 {
234 return ui->flags & UI_FL_ADMISSIBLE;
235 }
236
237 static int open_user_table(struct user_info *ui, int create)
238 {
239 int ret;
240 struct passwd *pw;
241
242 ui->desc = adu_malloc(sizeof(*ui->desc));
243 ui->desc->num_columns = NUM_UT_COLUMNS;
244 ui->desc->flags = 0;
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);
251
252 INFO_LOG("opening table for uid %u\n", (unsigned)ui->uid);
253 if (create) {
254 ret = osl(osl_create_table(ui->desc));
255 if (ret < 0)
256 goto err;
257 num_uids++;
258 }
259 ret = osl(osl_open_table(ui->desc, &ui->table));
260 if (ret < 0)
261 goto err;
262 return 1;
263 err:
264 free((char *)ui->desc->name);
265 free((char *)ui->desc->dir);
266 free(ui->pw_name);
267 free(ui->desc);
268 ui->desc->name = NULL;
269 ui->desc->dir = NULL;
270 ui->desc = NULL;
271 ui->table = NULL;
272 ui->flags = 0;
273 return ret;
274 }
275
276 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui < \
277 uid_hash_table + uid_hash_table_size; ui++)
278
279 int for_each_admissible_user(int (*func)(struct user_info *, void *),
280 void *data)
281 {
282 int i;
283
284 assert(uid_hash_table);
285 for (i = 0; i < uid_hash_table_size; i++) {
286 int ret;
287 struct user_info *ui = uid_hash_table +
288 uid_hash_table_sort_idx[i];
289
290 if (!ui_used(ui) || !ui_admissible(ui))
291 continue;
292 ret = func(ui, data);
293 if (ret < 0)
294 return ret;
295 }
296 return 1;
297 }
298
299 #define PRIME1 0xb11924e1
300 #define PRIME2 0x01000193
301
302 void create_hash_table(unsigned bits)
303 {
304 int i;
305
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;
312 }
313
314 void close_user_tables(void)
315 {
316 struct user_info *ui;
317
318 FOR_EACH_USER(ui) {
319 int ret;
320
321 if (!ui_used(ui))
322 continue;
323 if (!ui->table)
324 continue;
325 INFO_LOG("closing user table for uid %u\n", (unsigned)ui->uid);
326 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
327 if (ret < 0)
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;
334 free(ui->pw_name);
335 ui->pw_name = NULL;
336 free(ui->desc);
337 ui->desc = NULL;
338 ui->table = NULL;
339 ui->flags = 0;
340 }
341 free(uid_hash_table);
342 uid_hash_table = NULL;
343 free(uid_hash_table_sort_idx);
344 uid_hash_table_sort_idx = NULL;
345 }
346
347 /*
348 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
349 * interval [0..s]. 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
351 * same slot.
352 *
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
356 * odd number.
357 *
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].
362 */
363 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
364 {
365 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
366 % uid_hash_table_size;
367 }
368
369 static struct user_info *lookup_uid(uint32_t uid)
370 {
371 uint32_t p;
372
373 for (p = 0; p < uid_hash_table_size; p++) {
374 struct user_info *ui = uid_hash_table + double_hash(uid, p);
375 if (!ui_used(ui))
376 return ui;
377 if (ui->uid == uid)
378 return ui;
379 }
380 return NULL;
381 }
382
383 int create_user_table(uint32_t uid, struct user_info **ui_ptr)
384 {
385 struct user_info *ui = lookup_uid(uid);
386
387 if (!ui)
388 return -E_HASH_TABLE_OVERFLOW;
389 *ui_ptr = ui;
390 if (ui_used(ui))
391 return 1;
392 ui->uid = uid;
393 ui->flags |= UI_FL_SLOT_USED;
394 return open_user_table(ui, 1);
395 }
396
397 static char *get_uid_list_name(void)
398 {
399 return make_message("%s/uid_list", conf.database_dir_arg);
400 }
401
402 static int (*hash_table_comparator)(struct user_info *a, struct user_info *b);
403
404 static int comp_wrapper(const void *a, const void *b)
405 {
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);
409 }
410
411 void sort_hash_table(int (*comp)(struct user_info *, struct user_info *))
412 {
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);
416 }
417
418 int open_admissible_user_tables(struct uid_range *admissible_uids)
419 {
420 struct user_info *ui;
421
422 assert(uid_hash_table);
423 DEBUG_LOG("size: %d\n", uid_hash_table_size);
424 FOR_EACH_USER(ui) {
425 int ret;
426
427 if (!ui_used(ui)) {
428 continue;
429 }
430 if (!uid_is_admissible(ui->uid, admissible_uids)) {
431 DEBUG_LOG("uid %u is not admissible\n", ui->uid);
432 ui->flags &= ~UI_FL_ADMISSIBLE;
433 continue;
434 }
435 ui->flags |= UI_FL_ADMISSIBLE;
436 if (ui->table)
437 continue;
438 ret = open_user_table(ui, 0);
439 if (ret < 0)
440 return ret;
441 }
442 return 1;
443 }
444
445 int read_uid_file(void)
446 {
447 size_t size;
448 uint32_t n;
449 char *filename = get_uid_list_name(), *map;
450 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
451 unsigned bits;
452
453 if (ret < 0) {
454 ERROR_LOG("failed to map %s\n", filename);
455 free(filename);
456 return ret;
457 }
458 num_uids = size / 4;
459 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
460 free(filename);
461 /*
462 * Compute number of hash table bits. The hash table size must be a
463 * power of two and larger than the number of uids.
464 */
465 bits = 2;
466 while (1 << bits < num_uids)
467 bits++;
468 create_hash_table(bits);
469 for (n = 0; n < num_uids; n++) {
470 uint32_t uid = read_u32(map + n * sizeof(uid));
471 struct user_info *ui = lookup_uid(uid);
472 assert(ui);
473 if (ui_used(ui)) { /* impossible */
474 ERROR_LOG("duplicate user id!?\n");
475 ret = -EFAULT;
476 goto out;
477 }
478 ui->uid = uid;
479 ui->flags |= UI_FL_SLOT_USED;
480 }
481 ret = 1;
482 out:
483 adu_munmap(map, size);
484 return ret;
485 }
486
487 int write_uid_file(void)
488 {
489 char *buf, *p, *filename;
490 size_t size = num_uids * sizeof(uint32_t);
491 int ret;
492 struct user_info *ui;
493
494 if (!num_uids)
495 return 0;
496 buf = p = adu_malloc(size);
497 FOR_EACH_USER(ui) {
498 if (!ui_used(ui))
499 continue;
500 write_u32(p, ui->uid);
501 p += sizeof(uint32_t);
502 }
503 filename = get_uid_list_name();
504 ret = adu_write_file(filename, buf, size);
505 free(filename);
506 free(buf);
507 return ret;
508 }