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