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