1 /*
2  * vQadmin Virtual Administration Interface
3  * Copyright (C) 2000-2002 Inter7 Internet Technologies, Inc.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * vol@inter7.com
20  */
21 
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include "global.h"
26 
27 struct acl_t_l {
28   char id;
29   unsigned int bit;
30 };
31 
32 extern char vqa_group[],
33             vqa_user[];
34 
35 struct acl_t_l acl_types[] = {
36   { 'C', ACL_USER_CREATE },
37   { 'D', ACL_USER_DELETE },
38   { 'V', ACL_USER_VIEW },
39   { 'M', ACL_USER_MOD },
40   { 'A', ACL_DOMAIN_CREATE },
41   { 'X', ACL_DOMAIN_DELETE },
42   { 'I', ACL_DOMAIN_VIEW },
43   { 'U', ACL_DOMAIN_MOD },
44   { '\0', ACL_NONE },
45 };
46 
47 unsigned int acl_features = ACL_NONE, acl_d_features = ACL_NONE;
48 
acl_init(void)49 void acl_init(void)
50 {
51   acl_read();
52 
53   if (!(vqa_group[0])) {
54     acl_features = acl_d_features;
55     memcpy((char *)vqa_group, (char *)"default", 7);
56   }
57 }
58 
acl_read(void)59 void acl_read(void)
60 {
61  FILE *stream = NULL;
62  char b[80], *p = NULL;
63 
64   stream = fopen(ACL_FILENAME, "r");
65   if (stream == NULL) global_error("Unable to read access lists", 1, 0);
66 
67   while(1) {
68     memset((char *)b, 0, 80);
69     fgets(b, 80, stream);
70 
71     if (feof(stream)) break;
72 
73     if ((*b) && (*b != '#') && (*b != '\n') && (*b != '\r')) {
74       for (p = b; *p; p++) {
75         if ((*p == '\n') || (*p == '\r')) {
76           *p = '\0';
77           break;
78         }
79       }
80 
81       acl_parse(b);
82 
83       if (vqa_group[0]) break;
84     }
85   }
86 
87   fclose(stream);
88 }
89 
acl_parse(char * b)90 void acl_parse(char *b)
91 {
92  char *h = NULL, *t = NULL, i = 0, *group = NULL;
93  unsigned int f=0;
94 
95   for (h = t = b; *h; h++) {
96     if (*h == ' ') {
97       i++;
98 
99       if (i > 2) break;
100     }
101   }
102 
103   if (i != 2) global_error("Syntax erorr in access lists", 1, 0);
104 
105   for (h = b; *h != ' '; h++); *h++ = '\0'; group = t;
106   for (t = h; *h != ' '; h++); *h++ = '\0';
107 
108   if (*t == '*') f = ACL_ALL;
109   else f = acl_parse_features(t);
110 
111   if (!(strcasecmp(group, "default"))) acl_d_features = f;
112 
113   if (acl_parse_multi(h)) {
114     memcpy((char *)vqa_group, (char *)group, MAX_GLOBAL_LENGTH);
115     acl_features = f;
116   }
117 }
118 
acl_parse_features(char * b)119 char acl_parse_features(char *b)
120 {
121  int i = 0;
122  int bits = 0;
123  char *p = NULL;
124 
125   bits = ACL_NONE;
126 
127   for (p = b; *p; p++) {
128     for (i = 0; acl_types[i].id!='\0'; i++) {
129       if (*p == acl_types[i].id) {
130         if (!(bits & acl_types[i].bit)) bits |= acl_types[i].bit;
131       }
132     }
133   }
134 
135   return bits;
136 }
137 
acl_parse_multi(char * b)138 int acl_parse_multi(char *b)
139 {
140   char *h = NULL, *t = NULL;
141 
142   for (h = t = b;;) {
143     if ((*h == ',') || (*h == '\0') || (*h == ' ') ) {
144 
145       /* spaces or comma are separators */
146       if (*h == ',' || *h == ' ') *h = '\0';
147       else h = NULL;
148 
149       if (!(strcmp(t, vqa_user))) return 1;
150 
151       if (h == NULL) break;
152 
153       h++;
154       t = h;
155     } else {
156       h++;
157     }
158   }
159 
160   return 0;
161 }
162