From c2708c767b300a33c8636681ed4550d4f3be0ce0 Mon Sep 17 00:00:00 2001 From: Andre Date: Wed, 1 Nov 2006 14:09:17 +0100 Subject: [PATCH] move user handling code into seperate functions They are called user_list.c and user_list.h --- command.c | 2 +- configure.ac | 2 +- error.h | 5 +- mysql_selector.c | 1 + playlist_selector.c | 1 + server.c | 109 +----------------------------------- server.h | 25 --------- user_list.c | 132 ++++++++++++++++++++++++++++++++++++++++++++ user_list.h | 49 ++++++++++++++++ 9 files changed, 191 insertions(+), 135 deletions(-) create mode 100644 user_list.c create mode 100644 user_list.h diff --git a/command.c b/command.c index 0be61e80..5da5833b 100644 --- a/command.c +++ b/command.c @@ -19,7 +19,6 @@ /** \file command.c does client authentication and executes server commands */ #include /* gettimeofday */ -#include "crypt.h" #include "server.cmdline.h" #include "db.h" #include "server.h" @@ -32,6 +31,7 @@ #include "daemon.h" #include "string.h" #include "fd.h" +#include "user_list.h" static RC4_KEY rc4_recv_key; static RC4_KEY rc4_send_key; diff --git a/configure.ac b/configure.ac index efd27d4e..faa5d4ad 100644 --- a/configure.ac +++ b/configure.ac @@ -83,7 +83,7 @@ audiod_audio_formats="" server_cmdline_objs="server.cmdline" server_errlist_objs="server mp3_afh afs command net string signal random_selector time daemon stat crypt http_send db close_on_fork playlist_selector - ipc dccp dccp_send fd" + ipc dccp dccp_send fd user_list" server_ldflags="" server_audio_formats=" mp3" diff --git a/error.h b/error.h index 54d9426f..b94139e3 100644 --- a/error.h +++ b/error.h @@ -74,10 +74,12 @@ enum para_subsystem { SS_ALSA_WRITE, SS_FILE_WRITE, SS_OSX_WRITE, + SS_USER_LIST, NUM_SS }; /* these do not need error handling (yet) */ +#define SERVER_ERRORS #define CLIENT_ERRORS #define WAV_ERRORS #define COMPRESS_ERRORS @@ -93,7 +95,7 @@ enum para_subsystem { extern const char **para_errlist[]; /** \endcond */ -#define SERVER_ERRORS \ +#define USER_LIST_ERRORS \ PARA_ERROR(USERLIST, "failed to open user list file"), \ PARA_ERROR(BAD_USER, "you don't exist. Go away."), \ @@ -573,6 +575,7 @@ SS_ENUM(RINGBUFFER); SS_ENUM(CLIENT); SS_ENUM(CLIENT_COMMON); SS_ENUM(AUDIOC); +SS_ENUM(USER_LIST); /** \endcond */ #undef PARA_ERROR /* rest of the world only sees the error text */ diff --git a/mysql_selector.c b/mysql_selector.c index 51273644..9363dba4 100644 --- a/mysql_selector.c +++ b/mysql_selector.c @@ -31,6 +31,7 @@ #include "error.h" #include "net.h" #include "string.h" +#include "user_list.h" /** pointer to the shared memory area */ extern struct misc_meta_data *mmd; diff --git a/playlist_selector.c b/playlist_selector.c index b925453e..010c9d21 100644 --- a/playlist_selector.c +++ b/playlist_selector.c @@ -24,6 +24,7 @@ #include "net.h" #include "string.h" #include "ipc.h" +#include "user_list.h" /** * structure used for transmission of the playlist diff --git a/server.c b/server.c index 7d795321..7a96eeff 100644 --- a/server.c +++ b/server.c @@ -42,7 +42,7 @@ #include "string.h" #include "ipc.h" #include "fd.h" -#include "crypt.h" +#include "user_list.h" /** define the array of error lists needed by para_server */ INIT_SERVER_ERRLISTS; @@ -59,7 +59,6 @@ struct misc_meta_data *mmd; */ struct server_args_info conf; char *user_list_file = NULL; -struct list_head user_list; extern void dccp_send_init(struct sender *); extern void http_send_init(struct sender *); extern void ortp_send_init(struct sender *); @@ -276,110 +275,6 @@ static void setup_signal_handling(void) } } -/* - * lookup user in user list file. Fills in a user struct containing - * filename of the user's public key as well as the permissions of that user. - * Returns 1 on success, 0 if user does not exist and < 0 on errors. - */ -static void populate_user_list(void) { - FILE *file_ptr = NULL; - char *char_ptr; - char line[MAXLINE]; - /* keyword, user, key, perms */ - char w[MAXLINE], n[MAXLINE], k[MAXLINE], p[MAXLINE], tmp[4][MAXLINE]; - int num, ret; - - file_ptr = fopen(user_list_file, "r"); - ret = -E_USERLIST; - if (!file_ptr) - goto out; - for (;;) { - struct user *u; - ret = para_fgets(line, MAXLINE, file_ptr); - if (ret < 0) - PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret)); - if (ret <= 0) - break; - if (sscanf(line,"%200s %200s %200s %200s", w, n, k, p) < 3) - continue; - if (strcmp(w, "user")) - continue; - PARA_DEBUG_LOG("found entry for %s\n", n); - u = para_malloc(sizeof(struct user)); - u->name = para_strdup(n); - u->rsa = para_malloc(sizeof(RSA)); - ret = get_rsa_key(k, &u->rsa, LOAD_PUBLIC_KEY); - if (ret < 0) - break; - u->perms = 0; - char_ptr = p; - num = sscanf(char_ptr, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]", - tmp[0], tmp[1], tmp[2], tmp[3]); - PARA_DEBUG_LOG("found %i perm entries\n", num); - u->perms = 0; - while (num > 0) { - num--; - if (!strcmp(tmp[num], "AFS_READ")) - u->perms |= AFS_READ; - else if (!strcmp(tmp[num], "AFS_WRITE")) - u->perms |= AFS_WRITE; - else if (!strcmp(tmp[num], "DB_READ")) - u->perms |= DB_READ; - else if (!strcmp(tmp[num], "DB_WRITE")) - u->perms |= DB_WRITE; - else /* unknown permission */ - PARA_WARNING_LOG("unknown permission: %s\n", - tmp[num]); - } - para_list_add(&u->node, &user_list); - } -out: - if (file_ptr) - fclose(file_ptr); - if (ret >= 0) - return; - PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret)); - exit(EXIT_FAILURE); -} - -static void init_user_list(void) -{ - struct user *u, *tmp; - static int initialized; - - if (initialized) { - list_for_each_entry_safe(u, tmp, &user_list, node) { - list_del(&u->node); - free(u->name); - free(u->rsa); - free(u); - } - } else - INIT_LIST_HEAD(&user_list); - initialized = 1; - populate_user_list(); -} - -/** - * lookup user in user_list. - * - * \param user: must initially contain the name of the user and is filled - * in by this function on success. - * - * \return 1 on success and < 0 on errors. - */ -int get_user(struct user *user) -{ - struct user *u; - list_for_each_entry(u, &user_list, node) { - if (strcmp(u->name, user->name)) - continue; - *user = *u; - return 1; - } - return -E_BAD_USER; -} - static void init_selector(void) { int i, ret; @@ -459,7 +354,7 @@ static unsigned do_inits(int argc, char **argv) log_welcome("para_server", conf.loglevel_arg); shm_init(); /* init mmd struct */ server_uptime(UPTIME_SET); /* reset server uptime */ - init_user_list(); + init_user_list(user_list_file); /* become daemon */ if (conf.daemon_given) daemon_init(); diff --git a/server.h b/server.h index bf835374..1648acfe 100644 --- a/server.h +++ b/server.h @@ -25,30 +25,6 @@ /** size of the selector_info and audio_file info strings of struct misc_meta_data */ #define MMD_INFO_SIZE 16384 -/** - * permission flags that can be set individually for any server command - * - * - DB_READ: command reads from the database - * - DB_WRITE: command changes the contents of the database - * - AFS_READ: command reads information about the current audio stream - * - AFS_WRITE: command changes the current audio stream - */ -enum {DB_READ = 1, DB_WRITE = 2, AFS_READ = 4, AFS_WRITE = 8}; - -/** - * data needed to authenticate the user - */ -struct user { - /** the position of this user in the list of users */ - struct list_head node; - /** the username */ - char *name; - /** the public RSA key */ - RSA *rsa; - /** the privileges that this user has */ - unsigned int perms; -}; - /** * defines one command of para_server */ @@ -155,6 +131,5 @@ struct misc_meta_data{ extern struct server_args_info conf; int handle_connect(int fd, struct sockaddr_in *addr); -int get_user(struct user *user); void mmd_unlock(void); void mmd_lock(void); diff --git a/user_list.c b/user_list.c new file mode 100644 index 00000000..54e27caa --- /dev/null +++ b/user_list.c @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2006 Andre Noll + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + */ + +/** \file user_list.c user handling for para_server */ + +#include "para.h" +#include "error.h" +#include "fd.h" +#include "string.h" +#include "user_list.h" + +static struct list_head user_list; + +/* + * lookup user in user list file. Fills in a user struct containing + * filename of the user's public key as well as the permissions of that user. + * Returns 1 on success, 0 if user does not exist and < 0 on errors. + */ +static void populate_user_list(char *user_list_file) +{ + FILE *file_ptr = NULL; + char *char_ptr; + char line[MAXLINE]; + /* keyword, user, key, perms */ + char w[MAXLINE], n[MAXLINE], k[MAXLINE], p[MAXLINE], tmp[4][MAXLINE]; + int num, ret; + + file_ptr = fopen(user_list_file, "r"); + ret = -E_USERLIST; + if (!file_ptr) + goto out; + for (;;) { + struct user *u; + ret = para_fgets(line, MAXLINE, file_ptr); + if (ret < 0) + PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret)); + if (ret <= 0) + break; + if (sscanf(line,"%200s %200s %200s %200s", w, n, k, p) < 3) + continue; + if (strcmp(w, "user")) + continue; + PARA_DEBUG_LOG("found entry for %s\n", n); + u = para_malloc(sizeof(struct user)); + u->name = para_strdup(n); + u->rsa = para_malloc(sizeof(RSA)); + ret = get_rsa_key(k, &u->rsa, LOAD_PUBLIC_KEY); + if (ret < 0) + break; + u->perms = 0; + char_ptr = p; + num = sscanf(char_ptr, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]", + tmp[0], tmp[1], tmp[2], tmp[3]); + PARA_DEBUG_LOG("found %i perm entries\n", num); + u->perms = 0; + while (num > 0) { + num--; + if (!strcmp(tmp[num], "AFS_READ")) + u->perms |= AFS_READ; + else if (!strcmp(tmp[num], "AFS_WRITE")) + u->perms |= AFS_WRITE; + else if (!strcmp(tmp[num], "DB_READ")) + u->perms |= DB_READ; + else if (!strcmp(tmp[num], "DB_WRITE")) + u->perms |= DB_WRITE; + else /* unknown permission */ + PARA_WARNING_LOG("unknown permission: %s\n", + tmp[num]); + } + para_list_add(&u->node, &user_list); + } +out: + if (file_ptr) + fclose(file_ptr); + if (ret >= 0) + return; + PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret)); + exit(EXIT_FAILURE); +} + +void init_user_list(char *user_list_file) +{ + struct user *u, *tmp; + static int initialized; + + if (initialized) { + list_for_each_entry_safe(u, tmp, &user_list, node) { + list_del(&u->node); + free(u->name); + free(u->rsa); + free(u); + } + } else + INIT_LIST_HEAD(&user_list); + initialized = 1; + populate_user_list(user_list_file); +} + +/** + * lookup user in user_list. + * + * \param user: must initially contain the name of the user and is filled + * in by this function on success. + * + * \return 1 on success and < 0 on errors. + */ +int get_user(struct user *user) +{ + struct user *u; + list_for_each_entry(u, &user_list, node) { + if (strcmp(u->name, user->name)) + continue; + *user = *u; + return 1; + } + return -E_BAD_USER; +} diff --git a/user_list.h b/user_list.h new file mode 100644 index 00000000..0efa5c55 --- /dev/null +++ b/user_list.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2006 Andre Noll + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. + */ + +/** \file user_list.h exported functions from user_list.c */ + +#include "list.h" +#include "crypt.h" + +/** + * permission flags that can be set individually for any server command + * + * - DB_READ: command reads from the database + * - DB_WRITE: command changes the contents of the database + * - AFS_READ: command reads information about the current audio stream + * - AFS_WRITE: command changes the current audio stream + */ +enum {DB_READ = 1, DB_WRITE = 2, AFS_READ = 4, AFS_WRITE = 8}; + +/** + * data needed to authenticate the user + */ +struct user { + /** the position of this user in the list of users */ + struct list_head node; + /** the username */ + char *name; + /** the public RSA key */ + RSA *rsa; + /** the privileges that this user has */ + unsigned int perms; +}; + +void init_user_list(char *user_list_file); +int get_user(struct user *user); -- 2.39.5