1 | |
---|
2 | |
---|
3 | /* bison grammar rules for process old rt0 statements */ |
---|
4 | /* |
---|
5 | isi.employee <- ted |
---|
6 | ted.friend <- mike |
---|
7 | usc.employee <- isi.employee |
---|
8 | usc.playground <- usc.employee.friend |
---|
9 | query, |
---|
10 | usc.playground <- ?Z |
---|
11 | isi.playground <- mike |
---|
12 | */ |
---|
13 | |
---|
14 | %{ |
---|
15 | /* C declarations */ |
---|
16 | #include <stdio.h> |
---|
17 | #include <string.h> |
---|
18 | #include <abac_stack.h> |
---|
19 | |
---|
20 | extern char *abac_cn_with_sha(char*); |
---|
21 | |
---|
22 | FILE *abac_yyin = NULL; |
---|
23 | FILE *abac_yyout = NULL; |
---|
24 | char *abac_yap_clause = NULL; |
---|
25 | char *abac_yyfptr = NULL; |
---|
26 | /* index to generate, C1,C2 .. */ |
---|
27 | int cred_count = 0; |
---|
28 | |
---|
29 | int sz_overhead = 0; |
---|
30 | |
---|
31 | char *compose_cred_list(); |
---|
32 | int get_next_cred_idx(); |
---|
33 | |
---|
34 | %} |
---|
35 | /* Bison declarations */ |
---|
36 | %union { |
---|
37 | int number; |
---|
38 | char *string; /* For returning char strings */ |
---|
39 | } |
---|
40 | %type <string> keypart rolepart left right term stmt |
---|
41 | |
---|
42 | %token <string> IDEN /* keyname or rolename */ |
---|
43 | |
---|
44 | %token <operator> DERIVE "<-" |
---|
45 | %token <operator> DOT "." |
---|
46 | %token <operator> AND "&" |
---|
47 | |
---|
48 | %% |
---|
49 | /* Grammar rules */ |
---|
50 | |
---|
51 | input: /* empty */ |
---|
52 | { } |
---|
53 | | stmt |
---|
54 | { fprintf (abac_yyout,"stmt (%s)\n", $1); |
---|
55 | abac_yap_clause = $1; |
---|
56 | } |
---|
57 | |
---|
58 | stmt : left DERIVE right |
---|
59 | { int sz; |
---|
60 | char *ptr=strstr($3,"role("); |
---|
61 | char *tmp; |
---|
62 | if(ptr==NULL) { |
---|
63 | sz=strlen($3)+strlen($1)+sz_overhead; |
---|
64 | tmp=(char *)abac_xmalloc(sz); |
---|
65 | sprintf(tmp,"isMember(%s,%s,['%s'])",$3,$1,abac_yyfptr); |
---|
66 | } else { |
---|
67 | sz=strlen($3)+strlen($1)+sz_overhead; |
---|
68 | tmp=(char *)abac_xmalloc(sz); |
---|
69 | if (cred_count==0) { |
---|
70 | sprintf(tmp,"isMember(X,%s,['%s']):-%s", |
---|
71 | $1, abac_yyfptr, $3); |
---|
72 | } else { |
---|
73 | char *tmp_cred_list=compose_cred_list(); |
---|
74 | sprintf(tmp,"isMember(X,%s,L):-%s, appendL([['%s'],%s],L)", |
---|
75 | $1, $3, abac_yyfptr, tmp_cred_list); |
---|
76 | free(tmp_cred_list); |
---|
77 | cred_count=0; |
---|
78 | } |
---|
79 | } |
---|
80 | $$=tmp; |
---|
81 | free($3);free($1); |
---|
82 | } |
---|
83 | |
---|
84 | left : keypart DOT rolepart |
---|
85 | { |
---|
86 | int sz=strlen($1)+strlen($3)+sz_overhead; |
---|
87 | char *tmp=(char *)abac_xmalloc(sz); |
---|
88 | sprintf(tmp,"role(%s,%s)", $1, $3); |
---|
89 | $$=tmp; |
---|
90 | free($3);free($1); |
---|
91 | } |
---|
92 | /* isi */ |
---|
93 | keypart : IDEN |
---|
94 | { |
---|
95 | char *cn=abac_cn_with_sha($1); |
---|
96 | $$=strdup(cn); |
---|
97 | } |
---|
98 | |
---|
99 | /* friend */ |
---|
100 | rolepart : IDEN |
---|
101 | { $$=strdup($1); } |
---|
102 | |
---|
103 | right : term |
---|
104 | { $$=$1; } |
---|
105 | | term AND right |
---|
106 | { |
---|
107 | int sz=strlen($1)+strlen($3)+2; |
---|
108 | char *tmp=(char *)abac_xmalloc(sz); |
---|
109 | sprintf(tmp,"%s,%s",$1,$3); |
---|
110 | $$=tmp; |
---|
111 | free($1);free($3); |
---|
112 | } |
---|
113 | |
---|
114 | term : keypart DOT IDEN DOT IDEN |
---|
115 | { |
---|
116 | int sz=strlen($1)+strlen($3)+strlen($5)+sz_overhead; |
---|
117 | char *tmp=(char *)abac_xmalloc(sz); |
---|
118 | int fst=get_next_cred_idx(); |
---|
119 | int snd=get_next_cred_idx(); |
---|
120 | sprintf(tmp,"isMember(Y,role(%s,%s),C%d),isMember(X,role(Y,%s),C%d)",$1,$3,fst,$5,snd); |
---|
121 | $$=tmp; |
---|
122 | free($1);free($3);free($5); |
---|
123 | } |
---|
124 | | keypart DOT IDEN |
---|
125 | { |
---|
126 | int sz=strlen($1)+strlen($3)+sz_overhead; |
---|
127 | int fst=get_next_cred_idx(); |
---|
128 | char *tmp=(char *)abac_xmalloc(sz); |
---|
129 | sprintf(tmp,"isMember(X,role(%s,%s),C%d)", $1, $3, fst); |
---|
130 | $$=tmp; |
---|
131 | free($1);free($3); |
---|
132 | } |
---|
133 | | keypart |
---|
134 | { |
---|
135 | $$=$1; |
---|
136 | } |
---|
137 | |
---|
138 | %% |
---|
139 | #include "abac_rt0.h" |
---|
140 | |
---|
141 | /* Additional C code */ |
---|
142 | int yywrap() |
---|
143 | { |
---|
144 | /* exit when done lexing the current input */ |
---|
145 | return 1; |
---|
146 | } |
---|
147 | |
---|
148 | int yyerror (char *s) |
---|
149 | { |
---|
150 | fprintf (abac_yyout,"yyerror: %s\n", s); |
---|
151 | } |
---|
152 | |
---|
153 | /* setting defaults */ |
---|
154 | void abac_yyinit() |
---|
155 | { |
---|
156 | abac_yyin=abac_get_yyin(); |
---|
157 | abac_yyout=abac_get_yyout(); |
---|
158 | abac_yyfptr = abac_get_yyfptr(); |
---|
159 | sz_overhead = strlen(abac_yyfptr)+2000; |
---|
160 | } |
---|
161 | |
---|
162 | char *abac_get_yap_clause() |
---|
163 | { |
---|
164 | return abac_yap_clause; |
---|
165 | } |
---|
166 | |
---|
167 | char *abac_free_yap_clause() |
---|
168 | { |
---|
169 | if (abac_yap_clause != NULL) |
---|
170 | free(abac_yap_clause); |
---|
171 | } |
---|
172 | |
---|
173 | int get_next_cred_idx() |
---|
174 | { |
---|
175 | printf("ZZZ count it once..%d\n", cred_count); |
---|
176 | cred_count++; |
---|
177 | return cred_count; |
---|
178 | } |
---|
179 | |
---|
180 | int get_curr_cred_idx() |
---|
181 | { |
---|
182 | return cred_count; |
---|
183 | } |
---|
184 | |
---|
185 | /* remember to free the returned string */ |
---|
186 | char *compose_cred_list() |
---|
187 | { |
---|
188 | int i=cred_count;; |
---|
189 | if(cred_count==0) return ""; |
---|
190 | |
---|
191 | char *clist=(char *)abac_xmalloc(sizeof(char)*(cred_count)*3); |
---|
192 | strcpy(clist,"C1"); |
---|
193 | if (cred_count==1) return clist; |
---|
194 | |
---|
195 | i=1; |
---|
196 | while(i!=cred_count) { |
---|
197 | i++; |
---|
198 | sprintf(clist,"%s,C%d",clist,i); |
---|
199 | } |
---|
200 | return clist; |
---|
201 | } |
---|