8648f10b049679046e5812677e34f5c2b9e06132
[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 FILE *file_ptr = NULL;
30 char *char_ptr;
31 char line[MAXLINE];
32 /* keyword, user, key, perms */
33 char w[MAXLINE], n[MAXLINE], k[MAXLINE], p[MAXLINE], tmp[4][MAXLINE];
34 int num, ret;
35
36 file_ptr = fopen(user_list_file, "r");
37 ret = -E_USERLIST;
38 if (!file_ptr)
39 goto out;
40 for (;;) {
41 struct user *u;
42 ret = para_fgets(line, MAXLINE, file_ptr);
43 if (ret < 0)
44 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
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 %s\n", n);
52 u = para_malloc(sizeof(struct user));
53 u->name = para_strdup(n);
54 ret = get_rsa_key(k, &u->rsa, LOAD_PUBLIC_KEY);
55 if (ret < 0)
56 break;
57 char_ptr = p;
58 num = sscanf(char_ptr, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]",
59 tmp[0], tmp[1], tmp[2], tmp[3]);
60 PARA_DEBUG_LOG("found %i perm entries\n", num);
61 u->perms = 0;
62 while (num > 0) {
63 num--;
64 if (!strcmp(tmp[num], "VSS_READ"))
65 u->perms |= VSS_READ;
66 else if (!strcmp(tmp[num], "VSS_WRITE"))
67 u->perms |= VSS_WRITE;
68 else if (!strcmp(tmp[num], "AFS_READ"))
69 u->perms |= AFS_READ;
70 else if (!strcmp(tmp[num], "AFS_WRITE"))
71 u->perms |= AFS_WRITE;
72 else /* unknown permission */
73 PARA_WARNING_LOG("ignoring unknown permission: %s\n",
74 tmp[num]);
75 }
76 para_list_add(&u->node, &user_list);
77 }
78 out:
79 if (file_ptr)
80 fclose(file_ptr);
81 if (ret >= 0)
82 return;
83 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
84 exit(EXIT_FAILURE);
85 }
86
87 /**
88 * initialize the list of users allowed to connecto to para_server
89 *
90 * \param user_list_file the file containing access information
91 *
92 * If this function is called a second time, the contents of the
93 * previous call are discarded.
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 user in user_list.
115 *
116 * \param name of the user
117 *
118 * \return a pointer to the corresponding user struct if the user was found,
119 * \p 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 }