5652774e0685fc423b462071d72ef728e8b99547
[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
41 ret = para_fgets(line, MAXLINE, file_ptr);
42 if (ret <= 0)
43 break;
44 if (sscanf(line,"%200s %200s %200s %200s", w, n, k, p) < 3)
45 continue;
46 if (strcmp(w, "user"))
47 continue;
48 PARA_DEBUG_LOG("found entry for %s\n", n);
49 u = para_malloc(sizeof(*u));
50 u->name = para_strdup(n);
51 ret = get_rsa_key(k, &u->rsa, LOAD_PUBLIC_KEY);
52 if (ret < 0)
53 break;
54 num = sscanf(p, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]",
55 tmp[0], tmp[1], tmp[2], tmp[3]);
56 PARA_DEBUG_LOG("found %i perm entries\n", num);
57 u->perms = 0;
58 while (num > 0) {
59 num--;
60 if (!strcmp(tmp[num], "VSS_READ"))
61 u->perms |= VSS_READ;
62 else if (!strcmp(tmp[num], "VSS_WRITE"))
63 u->perms |= VSS_WRITE;
64 else if (!strcmp(tmp[num], "AFS_READ"))
65 u->perms |= AFS_READ;
66 else if (!strcmp(tmp[num], "AFS_WRITE"))
67 u->perms |= AFS_WRITE;
68 else /* unknown permission */
69 PARA_WARNING_LOG("ignoring unknown permission: %s\n",
70 tmp[num]);
71 }
72 para_list_add(&u->node, &user_list);
73 }
74 fclose(file_ptr);
75 if (ret >= 0)
76 return;
77 err:
78 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
79 exit(EXIT_FAILURE);
80 }
81
82 /**
83 * initialize the list of users allowed to connecto to para_server
84 *
85 * \param user_list_file the file containing access information
86 *
87 * If this function is called a second time, the contents of the
88 * previous call are discarded.
89 */
90 void init_user_list(char *user_list_file)
91 {
92 struct user *u, *tmp;
93 static int initialized;
94
95 if (initialized) {
96 list_for_each_entry_safe(u, tmp, &user_list, node) {
97 list_del(&u->node);
98 free(u->name);
99 rsa_free(u->rsa);
100 free(u);
101 }
102 } else
103 INIT_LIST_HEAD(&user_list);
104 initialized = 1;
105 populate_user_list(user_list_file);
106 }
107
108 /**
109 * lookup user in user_list.
110 *
111 * \param name of the user
112 *
113 * \return a pointer to the corresponding user struct if the user was found,
114 * \p NULL otherwise.
115 */
116 struct user *lookup_user(const char *name)
117 {
118 struct user *u;
119 list_for_each_entry(u, &user_list, node) {
120 if (strcmp(u->name, name))
121 continue;
122 return u;
123 }
124 return NULL;
125 }