dfd8248c7f21940d7a94b9fe79442f02693a070a
[paraslash.git] / user_list.c
1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file user_list.c User handling for para_server. */
4
5 #include <regex.h>
6 #include <sys/types.h>
7
8 #include "para.h"
9 #include "error.h"
10 #include "crypt.h"
11 #include "fd.h"
12 #include "string.h"
13 #include "list.h"
14 #include "user_list.h"
15
16 INITIALIZED_LIST_HEAD(user_list);
17
18 /*
19  * Wrapper for fgets(3).
20  *
21  * Unlike fgets(3), an integer value is returned. On success, this function
22  * returns 1. On errors, -E_FGETS is returned. A zero return value indicates an
23  * end of file condition.
24  */
25 static int xfgets(char *line, int size, FILE *f)
26 {
27 again:
28         if (fgets(line, size, f))
29                 return 1;
30         if (feof(f))
31                 return 0;
32         if (!ferror(f))
33                 return -E_FGETS;
34         if (errno != EINTR) {
35                 PARA_ERROR_LOG("%s\n", strerror(errno));
36                 return -E_FGETS;
37         }
38         clearerr(f);
39         goto again;
40 }
41
42 /*
43  * Fill the list of users known to para_server.
44  *
45  * Populates a linked list of all users in \a user_list_file.  Returns on
46  * success, calls exit() on errors.
47  */
48 static void populate(char *user_list_file)
49 {
50         int ret = -E_USERLIST;
51         FILE *file_ptr = fopen(user_list_file, "r");
52
53         if (!file_ptr)
54                 goto err;
55         for (;;) {
56                 int num;
57                 char line[255];
58                 /* keyword, name, key, perms */
59                 char w[255], n[255], k[255], p[255], tmp[4][255];
60                 struct user *u;
61                 struct asymmetric_key *pubkey;
62
63                 ret = xfgets(line, sizeof(line), file_ptr);
64                 if (ret <= 0)
65                         break;
66                 if (sscanf(line,"%200s %200s %200s %200s", w, n, k, p) < 3)
67                         continue;
68                 if (strcmp(w, "user"))
69                         continue;
70                 PARA_DEBUG_LOG("found entry for user %s\n", n);
71                 ret = get_public_key(k, &pubkey);
72                 if (ret < 0) {
73                         PARA_NOTICE_LOG("skipping entry for user %s: %s\n", n,
74                                 para_strerror(-ret));
75                         continue;
76                 }
77                 /*
78                  * In order to encrypt len := CHALLENGE_SIZE + 2 * SESSION_KEY_LEN
79                  * bytes using RSA_public_encrypt() with EME-OAEP padding mode,
80                  * RSA_size(rsa) must be greater than len + 41. So ignore keys
81                  * which are too short. For details see RSA_public_encrypt(3).
82                  */
83                 if (ret <= CHALLENGE_SIZE + 2 * SESSION_KEY_LEN + 41) {
84                         PARA_WARNING_LOG("public key %s too short (%d)\n",
85                                 k, ret);
86                         free_public_key(pubkey);
87                         continue;
88                 }
89                 u = para_malloc(sizeof(*u));
90                 u->name = para_strdup(n);
91                 u->pubkey = pubkey;
92                 u->perms = 0;
93                 num = sscanf(p, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]",
94                         tmp[0], tmp[1], tmp[2], tmp[3]);
95                 PARA_DEBUG_LOG("found %i perm entries\n", num);
96                 while (num > 0) {
97                         num--;
98                         if (!strcmp(tmp[num], "VSS_READ"))
99                                 u->perms |= VSS_READ;
100                         else if (!strcmp(tmp[num], "VSS_WRITE"))
101                                 u->perms |= VSS_WRITE;
102                         else if (!strcmp(tmp[num], "AFS_READ"))
103                                 u->perms |= AFS_READ;
104                         else if (!strcmp(tmp[num], "AFS_WRITE"))
105                                 u->perms |= AFS_WRITE;
106                         else /* unknown permission */
107                                 PARA_WARNING_LOG("ignoring unknown permission: %s\n",
108                                         tmp[num]);
109                 }
110                 para_list_add(&u->node, &user_list);
111         }
112         fclose(file_ptr);
113         if (ret >= 0)
114                 return;
115 err:
116         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
117         exit(EXIT_FAILURE);
118 }
119
120 /**
121  * Initialize the list of users allowed to connect to para_server.
122  *
123  * \param user_list_file The file containing access information.
124  *
125  * If this function is called for the second time, the contents of the
126  * previous call are discarded, i.e. the user list is reloaded.
127  */
128 void user_list_init(char *user_list_file)
129 {
130         struct user *u, *tmp;
131
132         list_for_each_entry_safe(u, tmp, &user_list, node) {
133                 list_del(&u->node);
134                 free(u->name);
135                 free_public_key(u->pubkey);
136                 free(u);
137         }
138         populate(user_list_file);
139 }
140
141 /**
142  * Lookup a user in the user list.
143  *
144  * \param name The name of the user.
145  *
146  * \return A pointer to the corresponding user struct if the user was found, \p
147  * NULL otherwise.
148  */
149 const struct user *user_list_lookup(const char *name)
150 {
151         const struct user *u;
152         list_for_each_entry(u, &user_list, node) {
153                 if (strcmp(u->name, name))
154                         continue;
155                 return u;
156         }
157         return NULL;
158 }