]> git.tuebingen.mpg.de Git - adu.git/blob - user.c
233764fe40089e387d03b783f01295b180112636
[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 /*
54  * The columns of the per-user tables.
55  *
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
60  * user.
61  */
62 static struct osl_column_description user_table_cols[] = {
63         [UT_DIR_NUM] = {
64                 .storage_type = OSL_MAPPED_STORAGE,
65                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
66                 .name = "dir_num",
67                 .compare_function = uint64_compare,
68                 .data_size = sizeof(uint64_t)
69         },
70         [UT_BYTES] = {
71                 .storage_type = OSL_MAPPED_STORAGE,
72                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
73                 .compare_function = size_compare,
74                 .name = "num_bytes",
75                 .data_size = sizeof(uint64_t)
76         },
77         [UT_FILES] = {
78                 .storage_type = OSL_MAPPED_STORAGE,
79                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
80                 .compare_function = size_compare,
81                 .name = "num_files",
82                 .data_size = sizeof(uint64_t)
83         },
84 };
85
86 static int check_uid_arg(const char *arg, uint32_t *uid)
87 {
88         const uint32_t max = ~0U;
89         /*
90          * we need an 64-bit int for string -> uid conversion because strtoll()
91          * returns a signed value.
92          */
93         int64_t val;
94         int ret = atoi64(arg, &val);
95
96         if (ret < 0)
97                 return ret;
98         if (val < 0 || val > max)
99                 return -ERRNO_TO_ERROR(EINVAL);
100         *uid = val;
101         return 1;
102 }
103
104 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
105 {
106         int ret;
107         char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
108
109         if (!p || p == arg) { /* -42 or 42 */
110                 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
111                 if (ret < 0)
112                         goto out;
113                 ur->low = p? 0 : ur->high;
114                 ret = 1;
115                 goto out;
116         }
117         /* 42- or 42-4711 */
118         *p = '\0';
119         p++;
120         ret = check_uid_arg(arg, &ur->low);
121         if (ret < 0)
122                 goto out;
123         ur->high = ~0U;
124         if (*p) { /* 42-4711 */
125                 ret = check_uid_arg(p, &ur->high);
126                 if (ret < 0)
127                         goto out;
128         }
129         if (ur->low > ur->high)
130                 ret = -ERRNO_TO_ERROR(EINVAL);
131 out:
132         if (ret < 0)
133                 ERROR_LOG("bad uid option: %s\n", orig_arg);
134         else
135                 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
136                         ur->high);
137         free(arg);
138         return ret;
139 }
140
141 int parse_uid_arg(const char *orig_arg, struct uid_range **ur)
142 {
143         char *arg, **argv;
144         unsigned n;
145         int i, ret = 1;
146
147         if (!orig_arg)
148                 return 0;
149         arg = adu_strdup(orig_arg);
150         n = split_args(arg, &argv, ",");
151         if (!n)
152                 return -E_SYNTAX;
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);
156                 if (ret < 0)
157                         break;
158         }
159         free(argv);
160         free(arg);
161         if (ret < 0) {
162                 free(*ur);
163                 *ur = NULL;
164                 return ret;
165         }
166         /* an empty range indicates the end of the list */
167         (*ur)[n].low = 1;
168         (*ur)[n].high = 0;
169         return n;
170 }
171
172
173 static inline int ui_used(struct user_info *ui)
174 {
175         return ui->flags & UI_FL_SLOT_USED;
176 }
177
178 static inline int ui_admissible(struct user_info *ui)
179 {
180         return ui->flags & UI_FL_ADMISSIBLE;
181 }
182
183 static int open_user_table(struct user_info *ui, int create)
184 {
185         int ret;
186         struct passwd *pw;
187
188         ui->desc = adu_malloc(sizeof(*ui->desc));
189         ui->desc->num_columns = NUM_UT_COLUMNS;
190         ui->desc->flags = 0;
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);
197
198         INFO_LOG(".............................uid #%u: %u\n",
199                 (unsigned)num_uids, (unsigned)ui->uid);
200         if (create) {
201                 ret = osl(osl_create_table(ui->desc));
202                 if (ret < 0)
203                         goto err;
204                 num_uids++;
205         }
206         ret = osl(osl_open_table(ui->desc, &ui->table));
207         if (ret < 0)
208                 goto err;
209         return 1;
210 err:
211         free((char *)ui->desc->name);
212         free((char *)ui->desc->dir);
213         free(ui->pw_name);
214         free(ui->desc);
215         ui->desc->name = NULL;
216         ui->desc->dir = NULL;
217         ui->desc = NULL;
218         ui->table = NULL;
219         ui->flags = 0;
220         return ret;
221 }
222
223 int for_each_admissible_user(int (*func)(struct user_info *, void *),
224                 void *data)
225 {
226         struct user_info *ui = uid_hash_table;
227
228         if (!ui)
229                 return -ERRNO_TO_ERROR(EFAULT);
230
231         for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
232                 int ret;
233
234                 if (!ui_used(ui) || !ui_admissible(ui))
235                         continue;
236                 ret = func(ui, data);
237                 if (ret < 0)
238                         return ret;
239         }
240         return 1;
241 }
242
243 #define PRIME1 0xb11924e1
244 #define PRIME2 0x01000193
245
246 void create_hash_table(unsigned bits)
247 {
248         uid_hash_table_size = 1 << bits;
249         uid_hash_table = adu_calloc(uid_hash_table_size *
250                 sizeof(struct user_info));
251 }
252
253 void free_hash_table(void)
254 {
255         free(uid_hash_table);
256         uid_hash_table = NULL;
257 }
258
259 static int close_user_table(struct user_info *ui, __a_unused void *data)
260 {
261         int ret;
262
263         ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
264         if (ret < 0)
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;
271         free(ui->pw_name);
272         ui->pw_name = NULL;
273         free(ui->desc);
274         ui->desc = NULL;
275         ui->table = NULL;
276         ui->flags = 0;
277         return 1;
278 }
279
280 void close_user_tables(void)
281 {
282         for_each_admissible_user(close_user_table, NULL);
283 }
284
285 /*
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
289  * same slot.
290  *
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
294  * odd number.
295  *
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].
300  */
301 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
302 {
303         return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
304                 % uid_hash_table_size;
305 }
306
307 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
308 {
309         struct uid_range *ur;
310         int ret = 1;
311
312         if (!urs) /* empty array means all uids are allowed */
313                 return 1;
314         FOR_EACH_UID_RANGE(ur, urs)
315                 if (ur->low <= uid && ur->high >= uid)
316                         goto out;
317         ret = 0;
318 out:
319         DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
320                 ret? "" : "not ");
321         return ret;
322 }
323
324 int search_uid(uint32_t uid, struct uid_range *urs,
325                 enum search_uid_flags flags, struct user_info **ui_ptr)
326 {
327         uint32_t p;
328
329         for (p = 0; p < uid_hash_table_size; p++) {
330                 struct user_info *ui = uid_hash_table + double_hash(uid, p);
331
332                 if (!ui_used(ui)) {
333                         int ret;
334                         if (!flags)
335                                 return -E_BAD_UID;
336                         ui->uid = uid;
337                         ui->flags |= UI_FL_SLOT_USED;
338                         if (!uid_is_admissible(uid, urs))
339                                 return 0;
340                         ui->flags |= UI_FL_ADMISSIBLE;
341                         ret = open_user_table(ui, flags & CREATE_USER_TABLE);
342                         if (ret < 0)
343                                 return ret;
344
345                         if (ui_ptr)
346                                 *ui_ptr = ui;
347                         return 1;
348                 }
349                 if (ui->uid != uid)
350                         continue;
351                 if (ui_ptr)
352                         *ui_ptr = ui;
353                 return 0;
354         }
355         return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
356 }
357
358 static char *get_uid_list_name(void)
359 {
360         return make_message("%s/uid_list", conf.database_dir_arg);
361 }
362
363 void sort_hash_table(int (*comp)(const void *, const void *))
364 {
365         qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
366                 comp);
367 }
368
369 int read_uid_file(struct uid_range *admissible_uids)
370 {
371         size_t size;
372         uint32_t n;
373         char *filename = get_uid_list_name(), *map;
374         int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
375         unsigned bits;
376
377         if (ret < 0) {
378                 INFO_LOG("failed to map %s\n", filename);
379                 free(filename);
380                 return ret;
381         }
382         num_uids = size / 4;
383         INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
384         free(filename);
385         /*
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.
388          */
389         bits = 2;
390         while (1 << bits < num_uids)
391                 bits++;
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);
396                 if (ret < 0)
397                         goto out;
398         }
399 out:
400         adu_munmap(map, size);
401         return ret;
402 }
403
404 static int write_uid(struct user_info *ui, void *data)
405 {
406         char **p = data;
407
408         write_u32(*p, ui->uid);
409         *p += sizeof(uint32_t);
410         return 1;
411 }
412
413 int write_uid_file(void)
414 {
415         char *buf, *p, *filename;
416         size_t size = num_uids * sizeof(uint32_t);
417         int ret;
418
419         if (!num_uids)
420                 return 0;
421         buf = p = adu_malloc(size);
422         ret = for_each_admissible_user(write_uid, &p);
423         if (ret < 0)
424                 goto out;
425         filename = get_uid_list_name();
426         ret = adu_write_file(filename, buf, size);
427         free(filename);
428 out:
429         free(buf);
430         return ret;
431 }