vss_init(): Use FOR_EACH_AUDIO_FORMAT() instead of open coding the loop.
[paraslash.git] / user_list.c
1 /*
2  * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file user_list.c user handling for para_server */
8
9 #include "para.h"
10 #include "error.h"
11 #include "fd.h"
12 #include "string.h"
13 #include "list.h"
14 #include "user_list.h"
15
16 static struct list_head user_list;
17
18 /*
19  * lookup user in user list file. Fills in a user struct containing
20  * filename of the user's public key as well as the permissions of that user.
21  * Returns 1 on success, 0 if user does not exist and < 0 on errors.
22  */
23 static void populate_user_list(char *user_list_file)
24 {
25         FILE *file_ptr = NULL;
26         char *char_ptr;
27         char line[MAXLINE];
28         /* keyword, user, key, perms */
29         char w[MAXLINE], n[MAXLINE], k[MAXLINE], p[MAXLINE], tmp[4][MAXLINE];
30         int num, ret;
31
32         file_ptr = fopen(user_list_file, "r");
33         ret = -E_USERLIST;
34         if (!file_ptr)
35                 goto out;
36         for (;;) {
37                 struct user *u;
38                 ret = para_fgets(line, MAXLINE, file_ptr);
39                 if (ret < 0)
40                         PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
41                 if (ret <= 0)
42                         break;
43                 if (sscanf(line,"%200s %200s %200s %200s", w, n, k, p) < 3)
44                         continue;
45                 if (strcmp(w, "user"))
46                         continue;
47                 PARA_DEBUG_LOG("found entry for %s\n", n);
48                 u = para_malloc(sizeof(struct user));
49                 u->name = para_strdup(n);
50                 ret = get_rsa_key(k, &u->rsa, LOAD_PUBLIC_KEY);
51                 if (ret < 0)
52                         break;
53                 char_ptr = p;
54                 num = sscanf(char_ptr, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]",
55                         tmp[0], tmp[1], tmp[2], tmp[3]);
56                 PARA_DEBUG_LOG("found %i perm entries\n", num);
57                 u->perms = 0;
58                 while (num > 0) {
59                         num--;
60                         if (!strcmp(tmp[num], "VSS_READ"))
61                                 u->perms |= VSS_READ;
62                         else if (!strcmp(tmp[num], "VSS_WRITE"))
63                                 u->perms |= VSS_WRITE;
64                         else if (!strcmp(tmp[num], "AFS_READ"))
65                                 u->perms |= AFS_READ;
66                         else if (!strcmp(tmp[num], "AFS_WRITE"))
67                                 u->perms |= AFS_WRITE;
68                         else /* unknown permission */
69                                 PARA_WARNING_LOG("ignoring unknown permission: %s\n",
70                                         tmp[num]);
71                 }
72                 para_list_add(&u->node, &user_list);
73         }
74 out:
75         if (file_ptr)
76                 fclose(file_ptr);
77         if (ret >= 0)
78                 return;
79         PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
80         exit(EXIT_FAILURE);
81 }
82
83 /**
84  * initialize the list of users allowed to connecto to para_server
85  *
86  * \param user_list_file the file containing access information
87  *
88  * If this function is called a second time, the contents of the
89  * previous call are discarded.
90  */
91 void init_user_list(char *user_list_file)
92 {
93         struct user *u, *tmp;
94         static int initialized;
95
96         if (initialized) {
97                 list_for_each_entry_safe(u, tmp, &user_list, node) {
98                         list_del(&u->node);
99                         free(u->name);
100                         rsa_free(u->rsa);
101                         free(u);
102                 }
103         } else
104                 INIT_LIST_HEAD(&user_list);
105         initialized = 1;
106         populate_user_list(user_list_file);
107 }
108
109 /**
110  * lookup user in user_list.
111  *
112  * \param name of the user
113  *
114  * \return a pointer to the corresponding user struct if the user was found,
115  *  \p NULL otherwise.
116  */
117 struct user *lookup_user(const char *name)
118 {
119         struct user *u;
120         list_for_each_entry(u, &user_list, node) {
121                 if (strcmp(u->name, name))
122                         continue;
123                 return u;
124         }
125         return NULL;
126 }