Remove rc4.h.
[paraslash.git] / user_list.c
1 /*
2 * Copyright (C) 2006-2011 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
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 struct asymmetric_key *pubkey;
43
44 ret = para_fgets(line, sizeof(line), 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_asymmetric_key(k, LOAD_PUBLIC_KEY, &pubkey);
53 if (ret < 0) {
54 PARA_NOTICE_LOG("skipping entry for user %s: %s\n", n,
55 para_strerror(-ret));
56 continue;
57 }
58 /*
59 * In order to encrypt len := CHALLENGE_SIZE + 2 * SESSION_KEY_LEN
60 * bytes using RSA_public_encrypt() with EME-OAEP padding mode,
61 * RSA_size(rsa) must be greater than len + 41. So ignore keys
62 * which are too short. For details see RSA_public_encrypt(3).
63 */
64 if (ret <= CHALLENGE_SIZE + 2 * SESSION_KEY_LEN + 41) {
65 PARA_WARNING_LOG("public key %s too short (%d)\n",
66 k, ret);
67 free_asymmetric_key(pubkey);
68 continue;
69 }
70 u = para_malloc(sizeof(*u));
71 u->name = para_strdup(n);
72 u->pubkey = pubkey;
73 u->perms = 0;
74 num = sscanf(p, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]",
75 tmp[0], tmp[1], tmp[2], tmp[3]);
76 PARA_DEBUG_LOG("found %i perm entries\n", num);
77 while (num > 0) {
78 num--;
79 if (!strcmp(tmp[num], "VSS_READ"))
80 u->perms |= VSS_READ;
81 else if (!strcmp(tmp[num], "VSS_WRITE"))
82 u->perms |= VSS_WRITE;
83 else if (!strcmp(tmp[num], "AFS_READ"))
84 u->perms |= AFS_READ;
85 else if (!strcmp(tmp[num], "AFS_WRITE"))
86 u->perms |= AFS_WRITE;
87 else /* unknown permission */
88 PARA_WARNING_LOG("ignoring unknown permission: %s\n",
89 tmp[num]);
90 }
91 para_list_add(&u->node, &user_list);
92 }
93 fclose(file_ptr);
94 if (ret >= 0)
95 return;
96 err:
97 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
98 exit(EXIT_FAILURE);
99 }
100
101 /**
102 * Initialize the list of users allowed to connect to to para_server.
103 *
104 * \param user_list_file The file containing access information.
105 *
106 * If this function is called for the second time, the contents of the
107 * previous call are discarded, i.e. the user list is reloaded.
108 */
109 void init_user_list(char *user_list_file)
110 {
111 struct user *u, *tmp;
112 static int initialized;
113
114 if (initialized) {
115 list_for_each_entry_safe(u, tmp, &user_list, node) {
116 list_del(&u->node);
117 free(u->name);
118 free_asymmetric_key(u->pubkey);
119 free(u);
120 }
121 } else
122 INIT_LIST_HEAD(&user_list);
123 initialized = 1;
124 populate_user_list(user_list_file);
125 }
126
127 /**
128 * Lookup a user in the user list.
129 *
130 * \param name The name of the user.
131 *
132 * \return A pointer to the corresponding user struct if the user was found, \p
133 * NULL otherwise.
134 */
135 struct user *lookup_user(const char *name)
136 {
137 struct user *u;
138 list_for_each_entry(u, &user_list, node) {
139 if (strcmp(u->name, name))
140 continue;
141 return u;
142 }
143 return NULL;
144 }