1 #include "alloc.h"
2 #include "stralloc.h"
3 #include "open.h"
4 #include "cdb.h"
5 #include "rules.h"
6
7 stralloc rules_name = {0};
8
9 static struct cdb c;
10
dorule(void (* callback)(char *,unsigned int))11 static int dorule(void (*callback)(char *,unsigned int))
12 {
13 char *data;
14 unsigned int datalen;
15
16 switch(cdb_find(&c,rules_name.s,rules_name.len)) {
17 case -1: return -1;
18 case 0: return 0;
19 }
20
21 datalen = cdb_datalen(&c);
22 data = alloc(datalen);
23 if (!data) return -1;
24 if (cdb_read(&c,data,datalen,cdb_datapos(&c)) == -1) {
25 alloc_free(data);
26 return -1;
27 }
28
29 callback(data,datalen);
30 alloc_free(data);
31 return 1;
32 }
33
doit(void (* callback)(char *,unsigned int),const char * euid,const char * egid)34 static int doit(void (*callback)(char *,unsigned int),const char *euid,const char *egid)
35 {
36 int r;
37
38 if (euid && egid) {
39 if (!stralloc_copys(&rules_name,euid)) return -1;
40 if (!stralloc_cats(&rules_name,".")) return -1;
41 if (!stralloc_cats(&rules_name,egid)) return -1;
42 r = dorule(callback);
43 if (r) return r;
44 }
45
46 if (euid) {
47 if (!stralloc_copys(&rules_name,euid)) return -1;
48 r = dorule(callback);
49 if (r) return r;
50 }
51
52 if (egid) {
53 if (!stralloc_copys(&rules_name,".")) return -1;
54 if (!stralloc_cats(&rules_name,egid)) return -1;
55 r = dorule(callback);
56 if (r) return r;
57 }
58
59 rules_name.len = 0;
60 return dorule(callback);
61 }
62
doit_exec(void (* callback)(char *,unsigned int),const char * euid,const char * egid,const char * prog)63 static int doit_exec(void (*callback)(char *,unsigned int),const char *euid,const char *egid,const char *prog)
64 {
65 int r;
66
67 if (euid && egid) {
68 if (!stralloc_copys(&rules_name,euid)) return -1;
69 if (!stralloc_cats(&rules_name,".")) return -1;
70 if (!stralloc_cats(&rules_name,egid)) return -1;
71 if (!stralloc_cats(&rules_name,",")) return -1;
72 if (!stralloc_cats(&rules_name,prog)) return -1;
73 r = dorule(callback);
74 if (r) return r;
75 }
76
77 if (euid) {
78 if (!stralloc_copys(&rules_name,euid)) return -1;
79 if (!stralloc_cats(&rules_name,",")) return -1;
80 if (!stralloc_cats(&rules_name,prog)) return -1;
81 r = dorule(callback);
82 if (r) return r;
83 }
84
85 if (egid) {
86 if (!stralloc_copys(&rules_name,".")) return -1;
87 if (!stralloc_cats(&rules_name,egid)) return -1;
88 if (!stralloc_cats(&rules_name,",")) return -1;
89 if (!stralloc_cats(&rules_name,prog)) return -1;
90 r = dorule(callback);
91 if (r) return r;
92 }
93
94 if (!stralloc_copys(&rules_name,",")) return -1;
95 if (!stralloc_cats(&rules_name,prog)) return -1;
96 r = dorule(callback);
97 if (r) return r;
98
99 rules_name.len = 0;
100 return dorule(callback);
101 }
102
rules(void (* callback)(char *,unsigned int),int fd,const char * euid,const char * egid)103 int rules(void (*callback)(char *,unsigned int),int fd,const char *euid,const char *egid)
104 {
105 int r;
106 cdb_init(&c,fd);
107 r = doit(callback,euid,egid);
108 cdb_free(&c);
109 return r;
110 }
111
rules_exec(void (* callback)(char *,unsigned int),int fd,const char * euid,const char * egid,const char * prog)112 int rules_exec(void (*callback)(char *,unsigned int),int fd,const char *euid,const char *egid,const char *prog)
113 {
114 int r;
115 cdb_init(&c,fd);
116 r = doit_exec(callback,euid,egid,prog);
117 return r;
118 }
119
120