Merge commit 'fml/master'
[adu.git] / adu.c
1 #include "adu.h"
2 #include <dirent.h> /* readdir() */
3 #include <pwd.h>
4
5 #include "gcc-compat.h"
6 #include "cmdline.h"
7 #include "fd.h"
8 #include "string.h"
9 #include "error.h"
10 #include "portable_io.h"
11
12 DEFINE_ERRLIST;
13 int osl_errno;
14
15 /** In case a signal is received, its number is stored here. */
16 static int signum;
17
18 /** Command line and config file options. */
19 struct gengetopt_args_info conf;
20
21 /** Options passed to --select-options. */
22 struct select_args_info select_conf;
23
24 /** The number of different uids found so far. */
25 uint32_t num_uids = 0;
26
27 /** This is always a power of two. It is set in create_hash_table(). */
28 static uint32_t uid_hash_table_size;
29
30 /**
31  * Contains info for each user that owns at least one regular file.
32  *
33  * Even users that are not taken into account because of the --uid
34  * option occupy a slot in this hash table. This allows to find out
35  * quicky whether a uid is admissible. And yes, this has to be fast.
36  */
37 static struct user_info *uid_hash_table;
38
39 static inline int ui_used(struct user_info *ui)
40 {
41         return ui->flags & UI_FL_SLOT_USED;
42 }
43
44 static inline int ui_admissible(struct user_info *ui)
45 {
46         return ui->flags & UI_FL_ADMISSIBLE;
47 }
48
49 /**
50  * The table containing the directory names and statistics.
51  */
52 struct osl_table *dir_table = NULL;
53
54 /**
55  * Compare the size of two directories
56  *
57  * \param obj1 Pointer to the first object.
58  * \param obj2 Pointer to the second object.
59  *
60  * This function first compares the size values as usual integers. If they compare as
61  * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
62  * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
63  */
64 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
65 {
66         uint64_t d1 = *(uint64_t *)obj1->data;
67         uint64_t d2 = *(uint64_t *)obj2->data;
68         int ret = NUM_COMPARE(d2, d1);
69
70         if (ret)
71                 return ret;
72         //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
73         return NUM_COMPARE(obj2->data, obj1->data);
74 }
75
76 /**
77  * Compare two osl objects pointing to unsigned integers of 64 bit size.
78  *
79  * \param obj1 Pointer to the first integer.
80  * \param obj2 Pointer to the second integer.
81  *
82  * \return The values required for an osl compare function.
83  *
84  * \sa osl_compare_func, osl_hash_compare().
85  */
86 static int uint64_compare(const struct osl_object *obj1,
87                 const struct osl_object *obj2)
88 {
89         uint64_t d1 = read_u64((const char *)obj1->data);
90         uint64_t d2 = read_u64((const char *)obj2->data);
91
92         if (d1 < d2)
93                 return 1;
94         if (d1 > d2)
95                 return -1;
96         return 0;
97 }
98
99 static struct osl_column_description dir_table_cols[] = {
100         [DT_NAME] = {
101                 .storage_type = OSL_MAPPED_STORAGE,
102                 .storage_flags = 0,
103                 .name = "dir",
104         },
105         [DT_NUM] = {
106                 .storage_type = OSL_MAPPED_STORAGE,
107                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
108                 .name = "num",
109                 .compare_function = uint64_compare,
110                 .data_size = sizeof(uint64_t)
111         },
112         [DT_PARENT_NUM] = {
113                 .storage_type = OSL_MAPPED_STORAGE,
114                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
115                 .name = "parent_num",
116                 .compare_function = size_compare,
117                 .data_size = sizeof(uint64_t)
118         },
119         [DT_BYTES] = {
120                 .storage_type = OSL_MAPPED_STORAGE,
121                 .storage_flags =  OSL_RBTREE | OSL_FIXED_SIZE,
122                 .compare_function = size_compare,
123                 .name = "num_bytes",
124                 .data_size = sizeof(uint64_t)
125         },
126         [DT_FILES] = {
127                 .storage_type = OSL_MAPPED_STORAGE,
128                 .storage_flags =  OSL_RBTREE | OSL_FIXED_SIZE,
129                 .compare_function = size_compare,
130                 .name = "num_files",
131                 .data_size = sizeof(uint64_t)
132         }
133 };
134
135 static struct osl_table_description dir_table_desc = {
136         .name = "dir_table",
137         .num_columns = NUM_DT_COLUMNS,
138         .flags = 0,
139         .column_descriptions = dir_table_cols,
140 };
141
142 static struct osl_column_description user_table_cols[] = {
143         [UT_DIR_NUM] = {
144                 .storage_type = OSL_MAPPED_STORAGE,
145                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
146                 .name = "dir_num",
147                 .compare_function = uint64_compare,
148                 .data_size = sizeof(uint64_t)
149         },
150         [UT_BYTES] = {
151                 .storage_type = OSL_MAPPED_STORAGE,
152                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
153                 .compare_function = size_compare,
154                 .name = "num_bytes",
155                 .data_size = sizeof(uint64_t)
156         },
157         [UT_FILES] = {
158                 .storage_type = OSL_MAPPED_STORAGE,
159                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
160                 .compare_function = size_compare,
161                 .name = "num_files",
162                 .data_size = sizeof(uint64_t)
163         },
164 };
165
166 /**
167  * The log function.
168  *
169  * \param ll Loglevel.
170  * \param fml Usual format string.
171  *
172  * All XXX_LOG() macros use this function.
173  */
174 __printf_2_3 void __log(int ll, const char* fmt,...)
175 {
176         va_list argp;
177         FILE *outfd;
178         struct tm *tm;
179         time_t t1;
180         char str[255] = "";
181
182         if (ll < conf.loglevel_arg)
183                 return;
184         outfd = stderr;
185         time(&t1);
186         tm = localtime(&t1);
187         strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
188         fprintf(outfd, "%s ", str);
189         va_start(argp, fmt);
190         vfprintf(outfd, fmt, argp);
191         va_end(argp);
192 }
193
194 static int open_user_table(struct user_info *ui, int create)
195 {
196         int ret;
197         struct passwd *pw;
198
199         ui->desc = adu_malloc(sizeof(*ui->desc));
200         ui->desc->num_columns = NUM_UT_COLUMNS;
201         ui->desc->flags = 0;
202         ui->desc->column_descriptions = user_table_cols;
203         ui->desc->dir = adu_strdup(conf.database_dir_arg);
204         ui->desc->name = make_message("%u", (unsigned)ui->uid);
205         pw = getpwuid(ui->uid);
206         if (pw && pw->pw_name)
207                 ui->pw_name = adu_strdup(pw->pw_name);
208
209         INFO_LOG(".............................uid #%u: %u\n",
210                 (unsigned)num_uids, (unsigned)ui->uid);
211         if (create) {
212                 ret = osl(osl_create_table(ui->desc));
213                 if (ret < 0)
214                         goto err;
215                 num_uids++;
216         }
217         ret = osl(osl_open_table(ui->desc, &ui->table));
218         if (ret < 0)
219                 goto err;
220         return 1;
221 err:
222         free((char *)ui->desc->name);
223         free((char *)ui->desc->dir);
224         free(ui->pw_name);
225         free(ui->desc);
226         ui->desc->name = NULL;
227         ui->desc->dir = NULL;
228         ui->desc = NULL;
229         ui->table = NULL;
230         ui->flags = 0;
231         return ret;
232 }
233
234 int for_each_admissible_user(int (*func)(struct user_info *, void *),
235                 void *data)
236 {
237         struct user_info *ui = uid_hash_table;
238
239         if (!ui)
240                 return -ERRNO_TO_ERROR(EFAULT);
241
242         for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
243                 int ret;
244
245                 if (!ui_used(ui) || !ui_admissible(ui))
246                         continue;
247                 ret = func(ui, data);
248                 if (ret < 0)
249                         return ret;
250         }
251         return 1;
252 }
253
254 #define PRIME1 0xb11924e1
255 #define PRIME2 0x01000193
256
257 void create_hash_table(unsigned bits)
258 {
259         uid_hash_table_size = 1 << bits;
260         uid_hash_table = adu_calloc(uid_hash_table_size *
261                 sizeof(struct user_info));
262 }
263
264 static void free_hash_table(void)
265 {
266         free(uid_hash_table);
267         uid_hash_table = NULL;
268 }
269
270 static void close_dir_table(void)
271 {
272         int ret;
273
274         if (!dir_table)
275                 return;
276         ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
277         if (ret < 0)
278                 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
279         free((char *)dir_table_desc.dir);
280         dir_table = NULL;
281 }
282
283 static int close_user_table(struct user_info *ui, __a_unused void *data)
284 {
285         int ret;
286
287         ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
288         if (ret < 0)
289                 ERROR_LOG("failed to close user table %u: %s\n",
290                         (unsigned) ui->uid, adu_strerror(-ret));
291         free((char *)ui->desc->name);
292         ui->desc->name = NULL;
293         free((char *)ui->desc->dir);
294         ui->desc->dir = NULL;
295         free(ui->pw_name);
296         ui->pw_name = NULL;
297         free(ui->desc);
298         ui->desc = NULL;
299         ui->table = NULL;
300         ui->flags = 0;
301         return 1;
302 }
303
304 static void close_user_tables(void)
305 {
306         for_each_admissible_user(close_user_table, NULL);
307 }
308
309 void close_all_tables(void)
310 {
311         close_dir_table();
312         close_user_tables();
313         free_hash_table();
314 }
315
316 static void signal_handler(int s)
317 {
318         signum = s;
319 }
320
321 void check_signals(void)
322 {
323         if (likely(!signum))
324                 return;
325         EMERG_LOG("caught signal %d\n", signum);
326         close_all_tables();
327         exit(EXIT_FAILURE);
328 }
329
330 static int init_signals(void)
331 {
332         if (signal(SIGINT, &signal_handler) == SIG_ERR)
333                 return -E_SIGNAL_SIG_ERR;
334         if (signal(SIGTERM, &signal_handler) == SIG_ERR)
335                 return -E_SIGNAL_SIG_ERR;
336         if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
337                 return -E_SIGNAL_SIG_ERR;
338         return 1;
339 }
340
341 /*
342  * We use a hash table of size s=2^uid_hash_bits to map the uids into the
343  * interval [0..s]. Hash collisions are treated by open addressing, i.e.
344  * unused slots in the table are used to store different uids that hash to the
345  * same slot.
346  *
347  * If a hash collision occurs, different slots are successively probed in order
348  * to find an unused slot for the new uid. Probing is implemented via a second
349  * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
350  * odd number.
351  *
352  * An odd number is sufficient to make sure each entry of the hash table gets
353  * probed for probe_num between 0 and s-1 because s is a power of two, hence
354  * the second hash value has never a common divisor with the hash table size.
355  * IOW: h is invertible in the ring [0..s].
356  */
357 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
358 {
359         return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
360                 % uid_hash_table_size;
361 }
362
363 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
364 {
365         struct uid_range *ur;
366         int ret = 1;
367
368         if (!urs) /* empty array means all uids are allowed */
369                 return 1;
370         FOR_EACH_UID_RANGE(ur, urs)
371                 if (ur->low <= uid && ur->high >= uid)
372                         goto out;
373         ret = 0;
374 out:
375         DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
376                 ret? "" : "not ");
377         return ret;
378 }
379
380 int search_uid(uint32_t uid, struct uid_range *urs,
381                 enum search_uid_flags flags, struct user_info **ui_ptr)
382 {
383         uint32_t p;
384
385         for (p = 0; p < uid_hash_table_size; p++) {
386                 struct user_info *ui = uid_hash_table + double_hash(uid, p);
387
388                 if (!ui_used(ui)) {
389                         int ret;
390                         if (!flags)
391                                 return -E_BAD_UID;
392                         ui->uid = uid;
393                         ui->flags |= UI_FL_SLOT_USED;
394                         if (!uid_is_admissible(uid, urs))
395                                 return 0;
396                         ui->flags |= UI_FL_ADMISSIBLE;
397                         ret = open_user_table(ui, flags & CREATE_USER_TABLE);
398                         if (ret < 0)
399                                 return ret;
400
401                         if (ui_ptr)
402                                 *ui_ptr = ui;
403                         return 1;
404                 }
405                 if (ui->uid != uid)
406                         continue;
407                 if (ui_ptr)
408                         *ui_ptr = ui;
409                 return 0;
410         }
411         return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
412 }
413
414 char *get_uid_list_name(void)
415 {
416         return make_message("%s/uid_list", conf.database_dir_arg);
417 }
418
419 void sort_hash_table(int (*comp)(const void *, const void *))
420 {
421         qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
422                 comp);
423 }
424
425 int open_dir_table(int create)
426 {
427         dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
428
429         if (create) {
430                 int ret = osl(osl_create_table(&dir_table_desc));
431                 if (ret < 0) {
432                         free((char *)dir_table_desc.dir);
433                         return ret;
434                 }
435         }
436         return osl(osl_open_table(&dir_table_desc, &dir_table));
437 }
438
439 static int check_args(void)
440 {
441         if (conf.create_given && !conf.base_dir_given)
442                 return -E_SYNTAX;
443
444         /* remove trailing slashes from base-dir arg */
445         if (conf.base_dir_given) {
446                 size_t len = strlen(conf.base_dir_arg);
447                 for (;;) {
448                         if (!len) /* empty string */
449                                 return -ERRNO_TO_ERROR(EINVAL);
450                         if (!--len) /* length 1 is always OK */
451                                 break;
452                         if (conf.base_dir_arg[len] != '/')
453                                 break; /* no trailing slash, also OK */
454                         conf.base_dir_arg[len] = '\0';
455                 }
456         }
457         return 1;
458 }
459
460 int main(int argc, char **argv)
461 {
462         int ret;
463         struct cmdline_parser_params params = {
464                 .override = 0,
465                 .initialize = 1,
466                 .check_required = 1,
467                 .check_ambiguity = 1,
468                 .print_errors = 1
469         };
470
471         cmdline_parser_ext(argc, argv, &conf, &params); /* aborts on errors */
472         ret = check_args();
473         if (ret < 0)
474                 goto out;
475         ret = init_signals();
476         if (ret < 0)
477                 goto out;
478         ret = -E_SYNTAX;
479         if (conf.select_given)
480                 ret = com_select();
481         else if (conf.create_given)
482                 ret = com_create();
483         else
484                 ret = com_interactive();
485         if (ret < 0)
486                 goto out;
487 out:
488         if (ret < 0) {
489                 ERROR_LOG("%s\n", adu_strerror(-ret));
490                 return -EXIT_FAILURE;
491         }
492         return EXIT_SUCCESS;
493 }