]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
move user handling code into seperate functions
authorAndre <maan@meins.(none)>
Wed, 1 Nov 2006 13:09:17 +0000 (14:09 +0100)
committerAndre <maan@meins.(none)>
Wed, 1 Nov 2006 13:09:17 +0000 (14:09 +0100)
They are called user_list.c and user_list.h

command.c
configure.ac
error.h
mysql_selector.c
playlist_selector.c
server.c
server.h
user_list.c [new file with mode: 0644]
user_list.h [new file with mode: 0644]

index 0be61e809471902c7b4e8a2243d0735d32bec240..5da5833ba3fb953d7ccdfe521ddc5f14632fb4f8 100644 (file)
--- a/command.c
+++ b/command.c
@@ -19,7 +19,6 @@
 /** \file command.c does client authentication and executes server commands */
 
 #include <sys/time.h> /* 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;
index efd27d4e518be5782045408b9d4be4cb42b05ce3..faa5d4addcd8a7c153f706c208d0b8afee283137 100644 (file)
@@ -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 54d9426feaa72fac86ab8c4a27fd607f34731443..b94139e38b013cec2bd5cbbcb954f04ee2708c72 100644 (file)
--- 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 */
index 51273644413ec0b67c4d100281857a023bac5d77..9363dba45c0b7a031adf5f9e0b64326243ee0b1a 100644 (file)
@@ -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;
index b925453ec203234a799bbd124c5c60019b71f9dc..010c9d21e05a6bbde2092e6433d458d65e7657c7 100644 (file)
@@ -24,6 +24,7 @@
 #include "net.h"
 #include "string.h"
 #include "ipc.h"
+#include "user_list.h"
 
 /**
  * structure used for transmission of the playlist
index 7d7953215b7a8a0c7a52bc1c0c39d3eb432d54ef..7a96eeff76c039ba2a01e84ffc9c232fd057d27d 100644 (file)
--- 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();
index bf8353741c5f73f93467a17aed8a323e406f1936..1648acfe700b9486860087cb75cea73ff9d47a0e 100644 (file)
--- a/server.h
+++ b/server.h
 /** 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 (file)
index 0000000..54e27ca
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
+ *
+ *     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 (file)
index 0000000..0efa5c5
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
+ *
+ *     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);