audiod: free pointer to config file path.
[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 "para.h"
10 #include "error.h"
11 #include "fd.h"
12 #include "string.h"
13 #include "user_list.h"
14
15 static struct list_head user_list;
16
17 /*
18  * lookup user in user list file. Fills in a user struct containing
19  * filename of the user's public key as well as the permissions of that user.
20  * Returns 1 on success, 0 if user does not exist and < 0 on errors.
21  */
22 static void populate_user_list(char *user_list_file)
23 {
24         FILE *file_ptr = NULL;
25         char *char_ptr;
26         char line[MAXLINE];
27         /* keyword, user, key, perms */
28         char w[MAXLINE], n[MAXLINE], k[MAXLINE], p[MAXLINE], tmp[4][MAXLINE];
29         int num, ret;
30
31         file_ptr = fopen(user_list_file, "r");
32         ret = -E_USERLIST;
33         if (!file_ptr)
34                 goto out;
35         for (;;) {
36                 struct user *u;
37                 ret = para_fgets(line, MAXLINE, file_ptr);
38                 if (ret < 0)
39                         PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
40                 if (ret <= 0)
41                         break;
42                 if (sscanf(line,"%200s %200s %200s %200s", w, n, k, p) < 3)
43                         continue;
44                 if (strcmp(w, "user"))
45                         continue;
46                 PARA_DEBUG_LOG("found entry for %s\n", n);
47                 u = para_malloc(sizeof(struct user));
48                 u->name = para_strdup(n);
49                 ret = get_rsa_key(k, &u->rsa, LOAD_PUBLIC_KEY);
50                 if (ret < 0)
51                         break;
52                 char_ptr = p;
53                 num = sscanf(char_ptr, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]",
54                         tmp[0], tmp[1], tmp[2], tmp[3]);
55                 PARA_DEBUG_LOG("found %i perm entries\n", num);
56                 u->perms = 0;
57                 while (num > 0) {
58                         num--;
59                         if (!strcmp(tmp[num], "VSS_READ"))
60                                 u->perms |= VSS_READ;
61                         else if (!strcmp(tmp[num], "VSS_WRITE"))
62                                 u->perms |= VSS_WRITE;
63                         else if (!strcmp(tmp[num], "AFS_READ"))
64                                 u->perms |= AFS_READ;
65                         else if (!strcmp(tmp[num], "AFS_WRITE"))
66                                 u->perms |= AFS_WRITE;
67                         else /* unknown permission */
68                                 PARA_WARNING_LOG("ignoring unknown permission: %s\n",
69                                         tmp[num]);
70                 }
71                 para_list_add(&u->node, &user_list);
72         }
73 out:
74         if (file_ptr)
75                 fclose(file_ptr);
76         if (ret >= 0)
77                 return;
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 }