X-Git-Url: http://git.tuebingen.mpg.de/?p=adu.git;a=blobdiff_plain;f=user.c;h=dfa1c103a9211e1a78e5e0502b0537e4a22cb885;hp=27982091e9c2fb3dcd6bb2016a61c51b21c67657;hb=d623adf133aaaee36091b840b8644c1884cd970c;hpb=e584cd6d5a3782ddeb6640f69534ab1664da2605 diff --git a/user.c b/user.c index 2798209..dfa1c10 100644 --- a/user.c +++ b/user.c @@ -4,13 +4,12 @@ * Licensed under the GPL v2. For licencing details see COPYING. */ -/** \file user.c uid User and user ID handling. */ +/** \file user.c \brief User and user ID handling. */ #include "adu.h" #include /* readdir() */ #include #include -#include "cmdline.h" /* TODO: This file should be independent of command line options */ #include "user.h" #include "fd.h" #include "string.h" @@ -29,6 +28,7 @@ struct uid_range { uint32_t high; }; +/** Iterate over all uid ranges. */ #define FOR_EACH_UID_RANGE(ur, urs) for (ur = urs; ur->low <= ur->high; ur++) /** Flags for the user hash table. */ @@ -38,6 +38,7 @@ enum uid_info_flags { /** Whether this uid should be taken into account. */ UI_FL_ADMISSIBLE = 2, }; + /* * Contains info for each user that owns at least one regular file. * @@ -51,7 +52,7 @@ static struct user_info *uid_hash_table; static uint32_t uid_hash_table_size; /** The number of used slots in the hash table. */ -static uint32_t num_uids = 0; +static uint32_t num_uids; /* * The columns of the per-user tables. @@ -141,6 +142,15 @@ out: return ret; } +/** + * Convert the --uid argument to an array of uid ranges. + * + * \param orig_arg The argument to the --uid option. + * \param ur Result pointer. + * + * Returns Negative on errors. On success, the number of uid ranges + * is returned. + */ int parse_uid_arg(const char *orig_arg, struct uid_range **ur) { char *arg, **argv; @@ -189,6 +199,22 @@ out: return ret; } +/** + * Add each given user to the array of admissible users. + * + * \param users Array of user names to add. + * \param num_users Length of \a users. + * \param admissible_uids The users which are already admissible. + * \param num_uid_ranges The number of intervals of \a admissible_uids. + * + * For each given user, the function checks whether that user is already + * admissible, i.e. its uid is contained in one of the ranges given by \a + * admissible_uids. If it is, the function ignores that user. Otherwise, a new + * length-one range consisting of that uid only is appended to \a + * admissible_uids. + * + * \return Negative on errors, the new number of uid ranges on success. + */ int append_users(char **users, int num_users, struct uid_range **admissible_uids, int num_uid_ranges) { @@ -231,7 +257,7 @@ static inline int ui_admissible(struct user_info *ui) return ui->flags & UI_FL_ADMISSIBLE; } -static int open_user_table(struct user_info *ui, int create) +static int open_user_table(const char *dirname, struct user_info *ui, int create) { int ret; struct passwd *pw; @@ -240,13 +266,13 @@ static int open_user_table(struct user_info *ui, int create) ui->desc->num_columns = NUM_UT_COLUMNS; ui->desc->flags = 0; ui->desc->column_descriptions = user_table_cols; - ui->desc->dir = adu_strdup(conf.database_dir_arg); + ui->desc->dir = adu_strdup(dirname); ui->desc->name = make_message("%u", (unsigned)ui->uid); pw = getpwuid(ui->uid); if (pw && pw->pw_name) ui->pw_name = adu_strdup(pw->pw_name); - DEBUG_LOG("opening table for uid %u\n", (unsigned)ui->uid); + INFO_LOG("opening table for uid %u\n", (unsigned)ui->uid); if (create) { ret = osl(osl_create_table(ui->desc)); if (ret < 0) @@ -270,17 +296,32 @@ err: return ret; } +/** Iterate over each user in the uid hash table. */ #define FOR_EACH_USER(ui) for (ui = uid_hash_table; ui < \ uid_hash_table + uid_hash_table_size; ui++) + +/** + * Execute the given function for each admissible user. + * + * \param func The function to execute. + * \param data Arbitrary pointer. + * + * This function calls \a func for each admissible user in the uid hash table. + * The \a data pointer is passed as the second argument to \a func. + * + * \return As soon as \a func returns a negative value, the loop is terminated + * and that negative value is returned. Otherwise, the function returns 1. + */ int for_each_admissible_user(int (*func)(struct user_info *, void *), void *data) { - struct user_info *ui; + int i; assert(uid_hash_table); - FOR_EACH_USER(ui) { + for (i = 0; i < uid_hash_table_size; i++) { int ret; + struct user_info *ui = uid_hash_table + i; if (!ui_used(ui) || !ui_admissible(ui)) continue; @@ -291,9 +332,16 @@ int for_each_admissible_user(int (*func)(struct user_info *, void *), return 1; } +/** Prime number used for calculating the slot for an uid. */ #define PRIME1 0xb11924e1 +/** Prime number used for probe all slots. */ #define PRIME2 0x01000193 +/** + * Create a hash table large enough of given size. + * + * \param bits Sets the maximal number of hash table entries to ^bits. + */ void create_hash_table(unsigned bits) { uid_hash_table_size = 1 << bits; @@ -301,6 +349,12 @@ void create_hash_table(unsigned bits) sizeof(struct user_info)); } +/** + * Close all open user tables and destroy the uid hash table. + * + * For each used slot in the uid hash table, close the osl user table if it is + * open. Finally, free the uid hash table. + */ void close_user_tables(void) { struct user_info *ui; @@ -310,6 +364,9 @@ void close_user_tables(void) if (!ui_used(ui)) continue; + if (!ui->table) + continue; + INFO_LOG("closing user table for uid %u\n", (unsigned)ui->uid); ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN)); if (ret < 0) ERROR_LOG("failed to close user table %u: %s\n", @@ -331,7 +388,7 @@ void close_user_tables(void) /* * We use a hash table of size s=2^uid_hash_bits to map the uids into the - * interval [0..s]. Hash collisions are treated by open addressing, i.e. + * interval [0..s-1]. Hash collisions are treated by open addressing, i.e. * unused slots in the table are used to store different uids that hash to the * same slot. * @@ -343,7 +400,7 @@ void close_user_tables(void) * An odd number is sufficient to make sure each entry of the hash table gets * probed for probe_num between 0 and s-1 because s is a power of two, hence * the second hash value has never a common divisor with the hash table size. - * IOW: h is invertible in the ring [0..s]. + * IOW: h is invertible in the ring [0..s-1]. */ static uint32_t double_hash(uint32_t uid, uint32_t probe_num) { @@ -365,7 +422,21 @@ static struct user_info *lookup_uid(uint32_t uid) return NULL; } -int create_user_table(uint32_t uid, struct user_info **ui_ptr) +/** + * Create and open a osl table for the given uid. + * + * \param uid The user ID. + * \param ui_ptr Result pointer + * + * Find out whether \a uid already exists in the uid hash table. If yes, just + * return the user info struct via \a ui_ptr. Otherwise, insert \a uid into the + * uid hash table, create and open the osl user table and also return the user + * info struct via \a ui_ptr. + * + * \return Standard. + */ +int create_user_table(const char *dirname, uint32_t uid, + struct user_info **ui_ptr) { struct user_info *ui = lookup_uid(uid); @@ -376,25 +447,72 @@ int create_user_table(uint32_t uid, struct user_info **ui_ptr) return 1; ui->uid = uid; ui->flags |= UI_FL_SLOT_USED; - return open_user_table(ui, 1); + return open_user_table(dirname, ui, 1); } -static char *get_uid_list_name(void) +static char *get_uid_list_name(const char *dirname) { - return make_message("%s/uid_list", conf.database_dir_arg); + return make_message("%s/uid_list", dirname); } - -void sort_hash_table(int (*comp)(const void *, const void *)) +/** + * Open the osl tables for all admissible uids. + * + * \param admissible_uids Determines which uids are considered admissible. + * + * Each slot in the hash table contains, among other information, a bit which + * specifies whether the uid of the slot is admissible in the current context. + * + * This function iterates over all entries in the hash table and checks for + * each used slot whether the corresponding uid is admissible with respect to + * \a admissible_uids. If so, it sets the admissible bit for this slot and + * opens the osl table of the uid. + * + * \return Stamdard. + */ +int open_admissible_user_tables(const char *dirname, + struct uid_range *admissible_uids) { - qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info), - comp); + struct user_info *ui; + + assert(uid_hash_table); + DEBUG_LOG("size: %d\n", uid_hash_table_size); + FOR_EACH_USER(ui) { + int ret; + + if (!ui_used(ui)) + continue; + if (!uid_is_admissible(ui->uid, admissible_uids)) { + DEBUG_LOG("uid %u is not admissible\n", ui->uid); + ui->flags &= ~UI_FL_ADMISSIBLE; + continue; + } + ui->flags |= UI_FL_ADMISSIBLE; + if (ui->table) + continue; + ret = open_user_table(dirname, ui, 0); + if (ret < 0) + return ret; + } + return 1; } -int read_uid_file(struct uid_range *admissible_uids) +/** + * Read the file of all possible uids. + * + * This is called from select/interactive mode. First a large hash table, large + * enough to store all uids contained in the uid file is created. Next, the + * uids are read from the uid file which was created during the creation of the + * database and each uid is inserted into the hash table. + * + * \sa write_uid_file(). + * + * \return Standard. + */ +int read_uid_file(const char *dirname) { size_t size; uint32_t n; - char *filename = get_uid_list_name(), *map; + char *filename = get_uid_list_name(dirname), *map; int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL); unsigned bits; @@ -420,17 +538,11 @@ int read_uid_file(struct uid_range *admissible_uids) assert(ui); if (ui_used(ui)) { /* impossible */ ERROR_LOG("duplicate user id!?\n"); - ret =-EFAULT; + ret = -EFAULT; goto out; } ui->uid = uid; ui->flags |= UI_FL_SLOT_USED; - if (!uid_is_admissible(uid, admissible_uids)) - continue; - ui->flags |= UI_FL_ADMISSIBLE; - ret = open_user_table(ui, 0); - if (ret < 0) - return ret; } ret = 1; out: @@ -438,7 +550,19 @@ out: return ret; } -int write_uid_file(void) +/** + * Write the list of uids to permanent storage. + * + * This is called from create mode after the dir table and all uer tables have + * been created. The file simply contains the list of all uids that own at + * least one regular file in the base directory and hence an osl table for this + * uid exists. + * + * \sa read_uid_file(). + * + * \return Standard. + */ +int write_uid_file(const char *dirname) { char *buf, *p, *filename; size_t size = num_uids * sizeof(uint32_t); @@ -454,7 +578,7 @@ int write_uid_file(void) write_u32(p, ui->uid); p += sizeof(uint32_t); } - filename = get_uid_list_name(); + filename = get_uid_list_name(dirname); ret = adu_write_file(filename, buf, size); free(filename); free(buf);