command.c: rename send_description() to send_list_of_commands()
[paraslash.git] / user_list.c
1 /*
2  * Copyright (C) 2006 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                 u->rsa = para_malloc(sizeof(RSA));
62                 ret = get_rsa_key(k, &u->rsa, LOAD_PUBLIC_KEY);
63                 if (ret < 0)
64                         break;
65                 u->perms = 0;
66                 char_ptr = p;
67                 num = sscanf(char_ptr, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]",
68                         tmp[0], tmp[1], tmp[2], tmp[3]);
69                 PARA_DEBUG_LOG("found %i perm entries\n", num);
70                 u->perms = 0;
71                 while (num > 0) {
72                         num--;
73                         if (!strcmp(tmp[num], "AFS_READ"))
74                                 u->perms |= AFS_READ;
75                         else if (!strcmp(tmp[num], "AFS_WRITE"))
76                                 u->perms |= AFS_WRITE;
77                         else if (!strcmp(tmp[num], "DB_READ"))
78                                 u->perms |= DB_READ;
79                         else if (!strcmp(tmp[num], "DB_WRITE"))
80                                 u->perms |= DB_WRITE;
81                         else /* unknown permission */
82                                 PARA_WARNING_LOG("unknown permission: %s\n",
83                                         tmp[num]);
84                 }
85                 para_list_add(&u->node, &user_list);
86         }
87 out:
88         if (file_ptr)
89                 fclose(file_ptr);
90         if (ret >= 0)
91                 return;
92         PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
93         exit(EXIT_FAILURE);
94 }
95
96 void init_user_list(char *user_list_file)
97 {
98         struct user *u, *tmp;
99         static int initialized;
100
101         if (initialized) {
102                 list_for_each_entry_safe(u, tmp, &user_list, node) {
103                         list_del(&u->node);
104                         free(u->name);
105                         free(u->rsa);
106                         free(u);
107                 }
108         } else
109                 INIT_LIST_HEAD(&user_list);
110         initialized = 1;
111         populate_user_list(user_list_file);
112 }
113
114 /**
115  * lookup user in user_list.
116  *
117  * \param user: must initially contain the name of the user and is filled
118  * in by this function on success.
119  *
120  * \return 1 on success and < 0 on errors.
121  */
122 int lookup_user(struct user *user)
123 {
124         struct user *u;
125         list_for_each_entry(u, &user_list, node) {
126                 if (strcmp(u->name, user->name))
127                         continue;
128                 *user = *u;
129                 return 1;
130         }
131         return -E_BAD_USER;
132 }