]> git.tuebingen.mpg.de Git - paraslash.git/blob - user_list.c
840802b33ba811816e3459284cd471644b134bb8
[paraslash.git] / user_list.c
1 /*
2  * Copyright (C) 2006-2009 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 <sys/types.h>
10 #include <dirent.h>
11 #include <openssl/rc4.h>
12
13 #include "para.h"
14 #include "error.h"
15 #include "crypt.h"
16 #include "fd.h"
17 #include "string.h"
18 #include "list.h"
19 #include "user_list.h"
20
21 static struct list_head user_list;
22
23 /*
24  * Fill the list of users known to para_server.
25  *
26  * Populates a linked list of all users in \a user_list_file.  Returns on
27  * success, calls exit() on errors.
28  */
29 static void populate_user_list(char *user_list_file)
30 {
31         int ret = -E_USERLIST;
32         FILE *file_ptr = fopen(user_list_file, "r");
33
34         if (!file_ptr)
35                 goto err;
36         for (;;) {
37                 int num;
38                 char line[255];
39                 /* keyword, name, key, perms */
40                 char w[255], n[255], k[255], p[255], tmp[4][255];
41                 struct user *u;
42                 RSA *rsa;
43
44                 ret = para_fgets(line, MAXLINE, file_ptr);
45                 if (ret <= 0)
46                         break;
47                 if (sscanf(line,"%200s %200s %200s %200s", w, n, k, p) < 3)
48                         continue;
49                 if (strcmp(w, "user"))
50                         continue;
51                 PARA_DEBUG_LOG("found entry for user %s\n", n);
52                 ret = get_rsa_key(k, &rsa, LOAD_PUBLIC_KEY);
53                 if (ret < 0) {
54                         PARA_NOTICE_LOG("skipping entry for user %s: %s\n", n,
55                                 para_strerror(-ret));
56                         continue;
57                 }
58                 u = para_malloc(sizeof(*u));
59                 u->name = para_strdup(n);
60                 u->rsa = rsa;
61                 u->perms = 0;
62                 num = sscanf(p, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]",
63                         tmp[0], tmp[1], tmp[2], tmp[3]);
64                 PARA_DEBUG_LOG("found %i perm entries\n", num);
65                 while (num > 0) {
66                         num--;
67                         if (!strcmp(tmp[num], "VSS_READ"))
68                                 u->perms |= VSS_READ;
69                         else if (!strcmp(tmp[num], "VSS_WRITE"))
70                                 u->perms |= VSS_WRITE;
71                         else if (!strcmp(tmp[num], "AFS_READ"))
72                                 u->perms |= AFS_READ;
73                         else if (!strcmp(tmp[num], "AFS_WRITE"))
74                                 u->perms |= AFS_WRITE;
75                         else /* unknown permission */
76                                 PARA_WARNING_LOG("ignoring unknown permission: %s\n",
77                                         tmp[num]);
78                 }
79                 para_list_add(&u->node, &user_list);
80         }
81         fclose(file_ptr);
82         if (ret >= 0)
83                 return;
84 err:
85         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
86         exit(EXIT_FAILURE);
87 }
88
89 /**
90  * Initialize the list of users allowed to connect to to para_server.
91  *
92  * \param user_list_file The file containing access information.
93  *
94  * If this function is called for the second time, the contents of the
95  * previous call are discarded, i.e. the user list is reloaded.
96  */
97 void init_user_list(char *user_list_file)
98 {
99         struct user *u, *tmp;
100         static int initialized;
101
102         if (initialized) {
103                 list_for_each_entry_safe(u, tmp, &user_list, node) {
104                         list_del(&u->node);
105                         free(u->name);
106                         rsa_free(u->rsa);
107                         free(u);
108                 }
109         } else
110                 INIT_LIST_HEAD(&user_list);
111         initialized = 1;
112         populate_user_list(user_list_file);
113 }
114
115 /**
116  * Lookup a user in the user list.
117  *
118  * \param name The name of the user.
119  *
120  * \return A pointer to the corresponding user struct if the user was found, \p
121  * NULL otherwise.
122  */
123 struct user *lookup_user(const char *name)
124 {
125         struct user *u;
126         list_for_each_entry(u, &user_list, node) {
127                 if (strcmp(u->name, name))
128                         continue;
129                 return u;
130         }
131         return NULL;
132 }