Author: biurrun Date: Wed Nov 25 13:43:58 2009 New Revision: 1776 Log: Reorder functions to get rid of forward declarations. Modified: trunk/pairing/management.c Modified: trunk/pairing/management.c ============================================================================== --- trunk/pairing/management.c Wed Nov 25 13:22:30 2009 (r1775) +++ trunk/pairing/management.c Wed Nov 25 13:43:58 2009 (r1776) @@ -47,16 +47,6 @@ {0, 0, 0, 0} }; -static int pisa_userlist_entry_compare_hit(const void *p1, const void *p2); -static int pisa_userlist_entry_compare_name(const void *p1, const void *p2); -static void pisa_user_hit_list_destroy(pisa_userhitlist *list); -static void pisa_userlist_destroy(pisa_userlist *list); -static void pisa_mgmt_conf_init(management_conf *m); -static pisa_userhitlist * pisa_merge_user_and_hit_list(pisa_userlist *ul, pisa_hitlist *hl); -static void pisa_userlist_parse(struct cfg_group *g, void *user_data); -static void remove_leading_zeroes_from_hit(char *hit); -static int pisa_userlist_entry_remove(pisa_userlist_entry *entry); - /** * Frees the memory allocated for the user_hit_list * @param list The user_hit_list which should be freed @@ -376,246 +366,6 @@ return 1; } - -/** Main program that will be run on the user's computer. - */ -int main(int argc, char *argv[]) -{ - int result = 0; - - struct options options; - pisa_hitlist *hitlist_allowed_hosts = NULL; - pisa_hitlist_entry *entry = NULL; - pisa_userlist *userlist = NULL; - pisa_userlist_entry *ulentry = NULL; - pisa_userhitlist *merged_list = NULL; - pisa_userhitlist_entry *uhlentry = NULL; - pisa_userhitlist_entry new_entry; - - memset(&options, 0, sizeof(options)); - memset(&new_entry, 0, sizeof(new_entry)); - - if(!get_lock()) - { - PISA_ERROR("A management instance is already running!"); - return ERROR_ALREADY_RUNNING; - } - - pisa_mgmt_conf_init(&managementconf); - - new_entry.active = NO_CHANGE; - - if (!pisa_mgmt_parse_options(argc, argv, &new_entry, &managementconf, &options)) - { - return ERROR_PARSING_COMMANDLINE; - } - - // Initialize Configs - if (!pisa_cfg_authorized_hosts_setup_file(managementconf.authorizedcfgfile)) - { - return ERROR_OPENING_CONFIG; - } - if (!pisa_cfg_setup_file(managementconf.userscfgfile)) - { - return ERROR_OPENING_CONFIG; - } - - switch (options.mode) - { - // Different list-modes - case LIST_SINGLE_ONLY_HIT: - entry = find_hit_in_hitlist(new_entry.hit); - - if (!NULL) - { - pisa_hitlist_entry_print(entry, options.list_mode); - } - - break; - - case LIST_ALL: - userlist = pisa_userlist_build(USERS_SECTION); - hitlist_allowed_hosts = pisa_hitlist_build("allowed_hosts"); - merged_list = pisa_merge_user_and_hit_list(userlist, hitlist_allowed_hosts); - int i; - - for (i = 0;i < merged_list->cur_slots;i++) - { - uhlentry = &merged_list->entries[i]; - pisa_userhitlist_entry_print(uhlentry, options.list_mode); - } - - pisa_user_hit_list_destroy(merged_list); - break; - - case LIST_SINGLE: - userlist = pisa_userlist_build(USERS_SECTION); - ulentry = pisa_userlist_find_username(userlist, new_entry.user_name); - - if (ulentry != NULL) - { - pisa_userlist_entry_print(ulentry, options.list_mode); - } - - break; - - // Different remove-modes - - case REMOVE_ONLY_HIT: - // Remove Hit - result = remove_hit(new_entry.hit); - - if (result <= 0) - { - if (result == 0) - { - result = ERROR_NOT_FOUND; - } - else - { - result = ERROR_WRITING_CONFIG; - } - - PISA_ERROR("Error removing HIT.\n"); - } - - break; - - case REMOVE: - // Remove Hit and Users entry - // Only Username was given - - if (new_entry.hit[0] == '\0') - { - userlist = pisa_userlist_build(USERS_SECTION); - ulentry = pisa_userlist_find_username(userlist, new_entry.user_name); - - if (ulentry != NULL) - { - strncpy(new_entry.hit, ulentry->hit, INET6_ADDRSTRLEN); - } - else - { - PISA_ERROR("Username not found!\n"); - result = ERROR_NOT_FOUND; - break; - } - } - - result = pisa_mgmt_remove_userlist_entry_by_hit(new_entry.hit); - - if (result <= 0) - { - if (result == 0) - { - result = ERROR_GENERAL; - } - else - { - result = ERROR_WRITING_CONFIG; - } - } - else - { - if (ulentry->active) - { - result = remove_hit(new_entry.hit); - - if (result <= 0) - { - if (result == 0) - { - result = ERROR_NOT_FOUND; - } - else - { - result = ERROR_WRITING_CONFIG; - } - - PISA_ERROR("Error removing HIT.\n"); - } - } - } - - break; - - // Different add-modes - - case ADD_OR_EDIT_ONLY_HIT: - // No username was given - result = write_config(new_entry.hit, new_entry.expiration, CONFIG_TYPE_HIT); - - if (!result) - { - result = ERROR_WRITING_CONFIG; - } - - break; - - case ADD_OR_EDIT: - // Find HIT through users config - if (new_entry.hit[0] == '\0') - { - userlist = pisa_userlist_build(USERS_SECTION); - ulentry = pisa_userlist_find_username(userlist, new_entry.user_name); - - if (ulentry != NULL) - { - strncpy(new_entry.hit, ulentry->hit, INET6_ADDRSTRLEN); - } - else - { - PISA_ERROR("Username not found!\n"); - result = ERROR_NOT_FOUND; - break; - } - - }else - { - if(new_entry.creditcard[0] == '\0') - { - PISA_ERROR("No Creditcard given"); - pisa_mgmt_print_usage(argv); - result = ERROR_PARSING_COMMANDLINE; - break; - } - } - - result = pisa_mgmt_users_config_write(&new_entry); - - if (result <= 0) - { - if (result == 0) - { - result = ERROR_GENERAL; - } - else - { - result = ERROR_WRITING_CONFIG; - } - } - - break; - - default: - PISA_ERROR("Unknown error occurred.\n"); - return ERROR_GENERAL; - break; - } - - // Free lists - pisa_hitlist_destroy(hitlist_allowed_hosts); - pisa_userlist_destroy(userlist); - - // Config cleanup - pisa_cfg_cleanup(); - pisa_cfg_authorized_hosts_cleanup(); - - result = (result == 1) ? 0 : result; - - return result; -} - /** * Initializes the management_conf struct * @param m Pointer to the management_conf struct @@ -674,6 +424,17 @@ argv[0], argv[0], argv[0]); } +/** + * Remove zeroes from HITs :0ba8 becomes :ba8 + * @param hit Pointer to the HIT from which the zeroes should be removed + */ +static void remove_leading_zeroes_from_hit(char *hit) +{ + struct sockaddr_in6 hit_addr; + inet_pton(PF_INET6, hit, &(hit_addr.sin6_addr)); + inet_ntop(AF_INET6, &(hit_addr.sin6_addr), hit, INET6_ADDRSTRLEN); +} + /** This function parses the command line input options and stores in the information accordingly. * * @param argc The argument counter. @@ -1140,9 +901,43 @@ /** * Removes a user_entry from the users configuration file * - * @param hit Search for the entry by the given Hit. + * @param entry Remove the given userlist_entry. * - * @return 0-> Error, 1-> Success, -1 -> Write error + * @return 0-> Error, 1-> Success, -1 Write error + */ +static int pisa_userlist_entry_remove(pisa_userlist_entry *entry) +{ + config_setting_t *group; + config_setting_t *parent; + + if (!entry) + { + return 0; + } + + group = entry->group; + parent = config_setting_parent(group); + + if (config_setting_remove_elem(parent, config_setting_index(group)) != CONFIG_TRUE) + { + DEBUG("Error removing HIT"); + return 0; + } + + if (config_write_file(&cfg, managementconf.userscfgfile) != CONFIG_TRUE) + { + return -1; + } + + return 1; +} + +/** + * Removes a user_entry from the users configuration file + * + * @param hit Search for the entry by the given Hit. + * + * @return 0-> Error, 1-> Success, -1 -> Write error */ int pisa_mgmt_remove_userlist_entry_by_hit(char *hit) { @@ -1188,47 +983,242 @@ return result; } -/** - * Removes a user_entry from the users configuration file - * - * @param entry Remove the given userlist_entry. - * - * @return 0-> Error, 1-> Success, -1 Write error + +/** Main program that will be run on the user's computer. */ -static int pisa_userlist_entry_remove(pisa_userlist_entry *entry) +int main(int argc, char *argv[]) { - config_setting_t *group; - config_setting_t *parent; + int result = 0; - if (!entry) + struct options options; + pisa_hitlist *hitlist_allowed_hosts = NULL; + pisa_hitlist_entry *entry = NULL; + pisa_userlist *userlist = NULL; + pisa_userlist_entry *ulentry = NULL; + pisa_userhitlist *merged_list = NULL; + pisa_userhitlist_entry *uhlentry = NULL; + pisa_userhitlist_entry new_entry; + + memset(&options, 0, sizeof(options)); + memset(&new_entry, 0, sizeof(new_entry)); + + if(!get_lock()) + { + PISA_ERROR("A management instance is already running!"); + return ERROR_ALREADY_RUNNING; + } + + pisa_mgmt_conf_init(&managementconf); + + new_entry.active = NO_CHANGE; + + if (!pisa_mgmt_parse_options(argc, argv, &new_entry, &managementconf, &options)) { - return 0; + return ERROR_PARSING_COMMANDLINE; } - group = entry->group; - parent = config_setting_parent(group); - - if (config_setting_remove_elem(parent, config_setting_index(group)) != CONFIG_TRUE) + // Initialize Configs + if (!pisa_cfg_authorized_hosts_setup_file(managementconf.authorizedcfgfile)) { - DEBUG("Error removing HIT"); - return 0; + return ERROR_OPENING_CONFIG; + } + if (!pisa_cfg_setup_file(managementconf.userscfgfile)) + { + return ERROR_OPENING_CONFIG; } - if (config_write_file(&cfg, managementconf.userscfgfile) != CONFIG_TRUE) + switch (options.mode) { - return -1; + // Different list-modes + case LIST_SINGLE_ONLY_HIT: + entry = find_hit_in_hitlist(new_entry.hit); + + if (!NULL) + { + pisa_hitlist_entry_print(entry, options.list_mode); + } + + break; + + case LIST_ALL: + userlist = pisa_userlist_build(USERS_SECTION); + hitlist_allowed_hosts = pisa_hitlist_build("allowed_hosts"); + merged_list = pisa_merge_user_and_hit_list(userlist, hitlist_allowed_hosts); + int i; + + for (i = 0;i < merged_list->cur_slots;i++) + { + uhlentry = &merged_list->entries[i]; + pisa_userhitlist_entry_print(uhlentry, options.list_mode); + } + + pisa_user_hit_list_destroy(merged_list); + break; + + case LIST_SINGLE: + userlist = pisa_userlist_build(USERS_SECTION); + ulentry = pisa_userlist_find_username(userlist, new_entry.user_name); + + if (ulentry != NULL) + { + pisa_userlist_entry_print(ulentry, options.list_mode); + } + + break; + + // Different remove-modes + + case REMOVE_ONLY_HIT: + // Remove Hit + result = remove_hit(new_entry.hit); + + if (result <= 0) + { + if (result == 0) + { + result = ERROR_NOT_FOUND; + } + else + { + result = ERROR_WRITING_CONFIG; + } + + PISA_ERROR("Error removing HIT.\n"); + } + + break; + + case REMOVE: + // Remove Hit and Users entry + // Only Username was given + + if (new_entry.hit[0] == '\0') + { + userlist = pisa_userlist_build(USERS_SECTION); + ulentry = pisa_userlist_find_username(userlist, new_entry.user_name); + + if (ulentry != NULL) + { + strncpy(new_entry.hit, ulentry->hit, INET6_ADDRSTRLEN); + } + else + { + PISA_ERROR("Username not found!\n"); + result = ERROR_NOT_FOUND; + break; + } + } + + result = pisa_mgmt_remove_userlist_entry_by_hit(new_entry.hit); + + if (result <= 0) + { + if (result == 0) + { + result = ERROR_GENERAL; + } + else + { + result = ERROR_WRITING_CONFIG; + } + } + else + { + if (ulentry->active) + { + result = remove_hit(new_entry.hit); + + if (result <= 0) + { + if (result == 0) + { + result = ERROR_NOT_FOUND; + } + else + { + result = ERROR_WRITING_CONFIG; + } + + PISA_ERROR("Error removing HIT.\n"); + } + } + } + + break; + + // Different add-modes + + case ADD_OR_EDIT_ONLY_HIT: + // No username was given + result = write_config(new_entry.hit, new_entry.expiration, CONFIG_TYPE_HIT); + + if (!result) + { + result = ERROR_WRITING_CONFIG; + } + + break; + + case ADD_OR_EDIT: + // Find HIT through users config + if (new_entry.hit[0] == '\0') + { + userlist = pisa_userlist_build(USERS_SECTION); + ulentry = pisa_userlist_find_username(userlist, new_entry.user_name); + + if (ulentry != NULL) + { + strncpy(new_entry.hit, ulentry->hit, INET6_ADDRSTRLEN); + } + else + { + PISA_ERROR("Username not found!\n"); + result = ERROR_NOT_FOUND; + break; + } + + }else + { + if(new_entry.creditcard[0] == '\0') + { + PISA_ERROR("No Creditcard given"); + pisa_mgmt_print_usage(argv); + result = ERROR_PARSING_COMMANDLINE; + break; + } + } + + result = pisa_mgmt_users_config_write(&new_entry); + + if (result <= 0) + { + if (result == 0) + { + result = ERROR_GENERAL; + } + else + { + result = ERROR_WRITING_CONFIG; + } + } + + break; + + default: + PISA_ERROR("Unknown error occurred.\n"); + return ERROR_GENERAL; + break; } - return 1; -} + // Free lists + pisa_hitlist_destroy(hitlist_allowed_hosts); + pisa_userlist_destroy(userlist); -/** - * Remove zeroes from HITs :0ba8 becomes :ba8 - * @param hit Pointer to the HIT from which the zeroes should be removed - */ -static void remove_leading_zeroes_from_hit(char *hit) -{ - struct sockaddr_in6 hit_addr; - inet_pton(PF_INET6, hit, &(hit_addr.sin6_addr)); - inet_ntop(AF_INET6, &(hit_addr.sin6_addr), hit, INET6_ADDRSTRLEN); + // Config cleanup + pisa_cfg_cleanup(); + pisa_cfg_authorized_hosts_cleanup(); + + result = (result == 1) ? 0 : result; + + return result; }