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