source: libabac/ort0.y @ 53e540d

mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2 yap_rt0
Last change on this file since 53e540d was 53e540d, checked in by Mei <mei@…>, 13 years ago

1) adding appendL to do the credential list appending

  • Property mode set to 100644
File size: 4.8 KB
Line 
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
20extern char *abac_cn_with_sha(char*);
21
22FILE *abac_yyin = NULL;
23FILE *abac_yyout = NULL;
24char *abac_yap_clause = NULL;
25char *abac_yyfptr = NULL;
26/* index to generate, C1,C2 .. */
27int cred_count = 0;
28
29int sz_overhead = 0;
30
31char *compose_cred_list();
32int get_next_cred_idx();
33
34%}
35/* Bison declarations */
36%union {
37int  number;
38char *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
51input:      /* empty */
52             { }
53        | stmt
54             { fprintf (abac_yyout,"stmt (%s)\n", $1);
55               abac_yap_clause = $1;
56             }
57 
58stmt : 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
84left : 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 */
93keypart : IDEN
94             {
95             char *cn=abac_cn_with_sha($1);
96             $$=strdup(cn);
97             }
98
99/* friend */
100rolepart : IDEN
101             { $$=strdup($1); }
102
103right : 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
114term : 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 */
142int yywrap()
143{
144     /* exit when done lexing the current input */
145     return 1;
146}
147
148int yyerror (char *s)
149{
150     fprintf (abac_yyout,"yyerror: %s\n", s);
151}
152
153/* setting defaults */
154void 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
162char *abac_get_yap_clause()
163{
164    return abac_yap_clause;
165}
166
167char *abac_free_yap_clause()
168{
169    if (abac_yap_clause != NULL)
170        free(abac_yap_clause);
171}
172
173int get_next_cred_idx()
174{
175    printf("ZZZ count it once..%d\n", cred_count);
176    cred_count++;
177    return cred_count;
178}
179
180int get_curr_cred_idx()
181{
182    return cred_count;
183}
184
185/* remember to free the returned string */
186char *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}
Note: See TracBrowser for help on using the repository browser.