clean up error.h
[paraslash.git] / user_list.c
1 /*
2  * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file user_list.c user handling for para_server */
20
21 #include "para.h"
22 #include "error.h"
23 #include "fd.h"
24 #include "string.h"
25 #include "user_list.h"
26
27 static struct list_head user_list;
28
29 /*
30  * lookup user in user list file. Fills in a user struct containing
31  * filename of the user's public key as well as the permissions of that user.
32  * Returns 1 on success, 0 if user does not exist and < 0 on errors.
33  */
34 static void populate_user_list(char *user_list_file)
35 {
36         FILE *file_ptr = NULL;
37         char *char_ptr;
38         char line[MAXLINE];
39         /* keyword, user, key, perms */
40         char w[MAXLINE], n[MAXLINE], k[MAXLINE], p[MAXLINE], tmp[4][MAXLINE];
41         int num, ret;
42
43         file_ptr = fopen(user_list_file, "r");
44         ret = -E_USERLIST;
45         if (!file_ptr)
46                 goto out;
47         for (;;) {
48                 struct user *u;
49                 ret = para_fgets(line, MAXLINE, file_ptr);
50                 if (ret < 0)
51                         PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
52                 if (ret <= 0)
53                         break;
54                 if (sscanf(line,"%200s %200s %200s %200s", w, n, k, p) < 3)
55                         continue;
56                 if (strcmp(w, "user"))
57                         continue;
58                 PARA_DEBUG_LOG("found entry for %s\n", n);
59                 u = para_malloc(sizeof(struct user));
60                 u->name = para_strdup(n);
61                 ret = get_rsa_key(k, &u->rsa, LOAD_PUBLIC_KEY);
62                 if (ret < 0)
63                         break;
64                 char_ptr = p;
65                 num = sscanf(char_ptr, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]",
66                         tmp[0], tmp[1], tmp[2], tmp[3]);
67                 PARA_DEBUG_LOG("found %i perm entries\n", num);
68                 u->perms = 0;
69                 while (num > 0) {
70                         num--;
71                         if (!strcmp(tmp[num], "VSS_READ"))
72                                 u->perms |= VSS_READ;
73                         else if (!strcmp(tmp[num], "VSS_WRITE"))
74                                 u->perms |= VSS_WRITE;
75                         else if (!strcmp(tmp[num], "AFS_READ"))
76                                 u->perms |= AFS_READ;
77                         else if (!strcmp(tmp[num], "AFS_WRITE"))
78                                 u->perms |= AFS_WRITE;
79                         else /* unknown permission */
80                                 PARA_WARNING_LOG("ignoring unknown permission: %s\n",
81                                         tmp[num]);
82                 }
83                 para_list_add(&u->node, &user_list);
84         }
85 out:
86         if (file_ptr)
87                 fclose(file_ptr);
88         if (ret >= 0)
89                 return;
90         PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
91         exit(EXIT_FAILURE);
92 }
93
94 /**
95  * initialize the list of users allowed to connecto to para_server
96  *
97  * \param user_list_file the file containing access information
98  *
99  * If this function is called a second time, the contents of the
100  * previous call are discarded.
101  */
102 void init_user_list(char *user_list_file)
103 {
104         struct user *u, *tmp;
105         static int initialized;
106
107         if (initialized) {
108                 list_for_each_entry_safe(u, tmp, &user_list, node) {
109                         list_del(&u->node);
110                         free(u->name);
111                         rsa_free(u->rsa);
112                         free(u);
113                 }
114         } else
115                 INIT_LIST_HEAD(&user_list);
116         initialized = 1;
117         populate_user_list(user_list_file);
118 }
119
120 /**
121  * lookup user in user_list.
122  *
123  * \param name of the user
124  *
125  * \return a pointer to the corresponding user struct if the user was found,
126  *  \p NULL otherwise.
127  */
128 struct user *lookup_user(const char *name)
129 {
130         struct user *u;
131         list_for_each_entry(u, &user_list, node) {
132                 if (strcmp(u->name, name))
133                         continue;
134                 return u;
135         }
136         return NULL;
137 }