Fix segfault in select mode if --uid was given.
[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 /** The number of used slots in the hash table. */
54 static uint32_t num_uids = 0;
55
56 /*
57  * The columns of the per-user tables.
58  *
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
63  * user.
64  */
65 static struct osl_column_description user_table_cols[] = {
66         [UT_DIR_NUM] = {
67                 .storage_type = OSL_MAPPED_STORAGE,
68                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
69                 .name = "dir_num",
70                 .compare_function = uint64_compare,
71                 .data_size = sizeof(uint64_t)
72         },
73         [UT_BYTES] = {
74                 .storage_type = OSL_MAPPED_STORAGE,
75                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
76                 .compare_function = size_compare,
77                 .name = "num_bytes",
78                 .data_size = sizeof(uint64_t)
79         },
80         [UT_FILES] = {
81                 .storage_type = OSL_MAPPED_STORAGE,
82                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
83                 .compare_function = size_compare,
84                 .name = "num_files",
85                 .data_size = sizeof(uint64_t)
86         },
87 };
88
89 static int check_uid_arg(const char *arg, uint32_t *uid)
90 {
91         const uint32_t max = ~0U;
92         /*
93          * we need an 64-bit int for string -> uid conversion because strtoll()
94          * returns a signed value.
95          */
96         int64_t val;
97         int ret = atoi64(arg, &val);
98
99         if (ret < 0)
100                 return ret;
101         if (val < 0 || val > max)
102                 return -ERRNO_TO_ERROR(EINVAL);
103         *uid = val;
104         return 1;
105 }
106
107 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
108 {
109         int ret;
110         char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
111
112         if (!p || p == arg) { /* -42 or 42 */
113                 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
114                 if (ret < 0)
115                         goto out;
116                 ur->low = p? 0 : ur->high;
117                 ret = 1;
118                 goto out;
119         }
120         /* 42- or 42-4711 */
121         *p = '\0';
122         p++;
123         ret = check_uid_arg(arg, &ur->low);
124         if (ret < 0)
125                 goto out;
126         ur->high = ~0U;
127         if (*p) { /* 42-4711 */
128                 ret = check_uid_arg(p, &ur->high);
129                 if (ret < 0)
130                         goto out;
131         }
132         if (ur->low > ur->high)
133                 ret = -ERRNO_TO_ERROR(EINVAL);
134 out:
135         if (ret < 0)
136                 ERROR_LOG("bad uid option: %s\n", orig_arg);
137         else
138                 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
139                         ur->high);
140         free(arg);
141         return ret;
142 }
143
144 int parse_uid_arg(const char *orig_arg, struct uid_range **ur)
145 {
146         char *arg, **argv;
147         unsigned n;
148         int i, ret = 1;
149
150         if (!orig_arg)
151                 return 0;
152         arg = adu_strdup(orig_arg);
153         n = split_args(arg, &argv, ",");
154         if (!n)
155                 return -E_SYNTAX;
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);
159                 if (ret < 0)
160                         break;
161         }
162         free(argv);
163         free(arg);
164         if (ret < 0) {
165                 free(*ur);
166                 *ur = NULL;
167                 return ret;
168         }
169         /* an empty range indicates the end of the list */
170         (*ur)[n].low = 1;
171         (*ur)[n].high = 0;
172         return n;
173 }
174
175 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
176 {
177         struct uid_range *ur;
178         int ret = 1;
179
180         if (!urs) /* empty array means all uids are allowed */
181                 return 1;
182         FOR_EACH_UID_RANGE(ur, urs)
183                 if (ur->low <= uid && ur->high >= uid)
184                         goto out;
185         ret = 0;
186 out:
187         DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
188                 ret? "" : "not ");
189         return ret;
190 }
191
192 int append_users(char **users, int num_users,
193                 struct uid_range **admissible_uids, int num_uid_ranges)
194 {
195         int i;
196         struct uid_range *au = *admissible_uids;
197
198         for (i = 0; i < num_users; i++) {
199                 char *u = users[i];
200                 struct uid_range *ur;
201                 struct passwd *pw = getpwnam(u);
202
203                 if (!pw) {
204                         ERROR_LOG("user %s not found\n", u);
205                         return -ERRNO_TO_ERROR(EINVAL);
206                 }
207                 if (au && uid_is_admissible(pw->pw_uid, au))
208                         continue; /* nothing to do */
209                 /* add a range consisting of this uid only */
210                 num_uid_ranges++;
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 */
217                 ur++;
218                 ur->low = 1;
219                 ur->high = 0;
220         }
221         return num_uid_ranges;
222 }
223
224 static inline int ui_used(struct user_info *ui)
225 {
226         return ui->flags & UI_FL_SLOT_USED;
227 }
228
229 static inline int ui_admissible(struct user_info *ui)
230 {
231         return ui->flags & UI_FL_ADMISSIBLE;
232 }
233
234 static int open_user_table(struct user_info *ui, int create)
235 {
236         int ret;
237         struct passwd *pw;
238
239         ui->desc = adu_malloc(sizeof(*ui->desc));
240         ui->desc->num_columns = NUM_UT_COLUMNS;
241         ui->desc->flags = 0;
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);
248
249         DEBUG_LOG("opening table for uid %u\n", (unsigned)ui->uid);
250         if (create) {
251                 ret = osl(osl_create_table(ui->desc));
252                 if (ret < 0)
253                         goto err;
254                 num_uids++;
255         }
256         ret = osl(osl_open_table(ui->desc, &ui->table));
257         if (ret < 0)
258                 goto err;
259         return 1;
260 err:
261         free((char *)ui->desc->name);
262         free((char *)ui->desc->dir);
263         free(ui->pw_name);
264         free(ui->desc);
265         ui->desc->name = NULL;
266         ui->desc->dir = NULL;
267         ui->desc = NULL;
268         ui->table = NULL;
269         ui->flags = 0;
270         return ret;
271 }
272
273 #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui < \
274         uid_hash_table + uid_hash_table_size; ui++)
275
276 int for_each_admissible_user(int (*func)(struct user_info *, void *),
277                 void *data)
278 {
279         struct user_info *ui;
280
281         assert(uid_hash_table);
282         FOR_EACH_USER(ui) {
283                 int ret;
284
285                 if (!ui_used(ui) || !ui_admissible(ui))
286                         continue;
287                 ret = func(ui, data);
288                 if (ret < 0)
289                         return ret;
290         }
291         return 1;
292 }
293
294 #define PRIME1 0xb11924e1
295 #define PRIME2 0x01000193
296
297 void create_hash_table(unsigned bits)
298 {
299         uid_hash_table_size = 1 << bits;
300         uid_hash_table = adu_calloc(uid_hash_table_size *
301                 sizeof(struct user_info));
302 }
303
304 void close_user_tables(void)
305 {
306         struct user_info *ui;
307
308         FOR_EACH_USER(ui) {
309                 int ret;
310
311                 if (!ui_used(ui))
312                         continue;
313                 if (!ui->table)
314                         continue;
315                 DEBUG_LOG("closing user table for uid %u\n", (unsigned)ui->uid);
316                 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
317                 if (ret < 0)
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;
324                 free(ui->pw_name);
325                 ui->pw_name = NULL;
326                 free(ui->desc);
327                 ui->desc = NULL;
328                 ui->table = NULL;
329                 ui->flags = 0;
330         }
331         free(uid_hash_table);
332         uid_hash_table = NULL;
333 }
334
335 /*
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
339  * same slot.
340  *
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
344  * odd number.
345  *
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].
350  */
351 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
352 {
353         return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
354                 % uid_hash_table_size;
355 }
356
357 static struct user_info *lookup_uid(uint32_t uid)
358 {
359         uint32_t p;
360
361         for (p = 0; p < uid_hash_table_size; p++) {
362                 struct user_info *ui = uid_hash_table + double_hash(uid, p);
363                 if (!ui_used(ui))
364                         return ui;
365                 if (ui->uid == uid)
366                         return ui;
367         }
368         return NULL;
369 }
370
371 int create_user_table(uint32_t uid, struct user_info **ui_ptr)
372 {
373         struct user_info *ui = lookup_uid(uid);
374
375         if (!ui)
376                 return -E_HASH_TABLE_OVERFLOW;
377         *ui_ptr = ui;
378         if (ui_used(ui))
379                 return 1;
380         ui->uid = uid;
381         ui->flags |= UI_FL_SLOT_USED;
382         return open_user_table(ui, 1);
383 }
384
385 static char *get_uid_list_name(void)
386 {
387         return make_message("%s/uid_list", conf.database_dir_arg);
388 }
389
390 void sort_hash_table(int (*comp)(const void *, const void *))
391 {
392         qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
393                 comp);
394 }
395
396 int read_uid_file(struct uid_range *admissible_uids)
397 {
398         size_t size;
399         uint32_t n;
400         char *filename = get_uid_list_name(), *map;
401         int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
402         unsigned bits;
403
404         if (ret < 0) {
405                 ERROR_LOG("failed to map %s\n", filename);
406                 free(filename);
407                 return ret;
408         }
409         num_uids = size / 4;
410         INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
411         free(filename);
412         /*
413          * Compute number of hash table bits. The hash table size must be a
414          * power of two and larger than the number of uids.
415          */
416         bits = 2;
417         while (1 << bits < num_uids)
418                 bits++;
419         create_hash_table(bits);
420         for (n = 0; n < num_uids; n++) {
421                 uint32_t uid = read_u32(map + n * sizeof(uid));
422                 struct user_info *ui = lookup_uid(uid);
423                 assert(ui);
424                 if (ui_used(ui)) { /* impossible */
425                         ERROR_LOG("duplicate user id!?\n");
426                         ret =-EFAULT;
427                         goto out;
428                 }
429                 ui->uid = uid;
430                 ui->flags |= UI_FL_SLOT_USED;
431                 if (!uid_is_admissible(uid, admissible_uids))
432                         continue;
433                 ui->flags |= UI_FL_ADMISSIBLE;
434                 ret = open_user_table(ui, 0);
435                 if (ret < 0)
436                         return ret;
437         }
438         ret = 1;
439 out:
440         adu_munmap(map, size);
441         return ret;
442 }
443
444 int write_uid_file(void)
445 {
446         char *buf, *p, *filename;
447         size_t size = num_uids * sizeof(uint32_t);
448         int ret;
449         struct user_info *ui;
450
451         if (!num_uids)
452                 return 0;
453         buf = p = adu_malloc(size);
454         FOR_EACH_USER(ui) {
455                 if (!ui_used(ui))
456                         continue;
457                 write_u32(p, ui->uid);
458                 p += sizeof(uint32_t);
459         }
460         filename = get_uid_list_name();
461         ret = adu_write_file(filename, buf, size);
462         free(filename);
463         free(buf);
464         return ret;
465 }