aclscan.l 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /*
  7. * Lexical analyzer for ACL
  8. */
  9. %{
  10. #include "acl.tab.h" /* token codes */
  11. #include <base/file.h>
  12. #include <libaccess/acl.h>
  13. #include <libaccess/nserror.h>
  14. #include "aclpriv.h"
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include <ctype.h>
  18. #include <libaccess/aclerror.h>
  19. #ifdef XP_WIN32
  20. #include <io.h>
  21. #endif
  22. #include "parse.h"
  23. #include "aclscan.h"
  24. static int acl_scanner_input(char *buffer, int max_size);
  25. #define YY_NEVER_INTERACTIVE 1
  26. #undef YY_INPUT
  27. #define YY_INPUT(buf, result, max) (result = acl_scanner_input(buf, max))
  28. static int acl_lineno;
  29. static int acl_tokenpos;
  30. static char acl_filename[500];
  31. static NSErr_t *acl_errp;
  32. static int acl_use_buffer;
  33. static char *acl_buffer;
  34. static int acl_buffer_length;
  35. static int acl_buffer_offset;
  36. static char *last_string;
  37. static SYS_FILE acl_prfd;
  38. %}
  39. ws [ \t]+
  40. comment #.*
  41. qstring \"[^\"\n]*[\"\n]
  42. variable [\*a-zA-Z0-9\.\-\_][\*a-zA-Z0-9\.\-\_]*
  43. %%
  44. \n.* {
  45. acl_lineno++;
  46. acl_tokenpos = 0;
  47. yyless(1);
  48. }
  49. {ws} ;
  50. {comment} ;
  51. <<EOF>> {
  52. yylval.string = NULL;
  53. last_string = yylval.string;
  54. return(0);
  55. }
  56. {qstring} {
  57. yylval.string = PERM_STRDUP( yytext+1 );
  58. last_string = yylval.string;
  59. if ( yylval.string[yyleng-2] != '"' )
  60. fprintf(stderr, "Unterminated string\n") ;
  61. else
  62. yylval.string[yyleng-2] = '\0';
  63. acl_tokenpos += yyleng;
  64. return ACL_QSTRING_TOK;
  65. }
  66. absolute {
  67. last_string = NULL;
  68. acl_tokenpos += yyleng;
  69. return ACL_ABSOLUTE_TOK;
  70. }
  71. acl {
  72. last_string = NULL;
  73. acl_tokenpos += yyleng;
  74. return ACL_ACL_TOK;
  75. }
  76. allow {
  77. last_string = NULL;
  78. acl_tokenpos += yyleng;
  79. return ACL_ALLOW_TOK;
  80. }
  81. always {
  82. last_string = NULL;
  83. acl_tokenpos += yyleng;
  84. return ACL_ALWAYS_TOK;
  85. }
  86. at {
  87. last_string = NULL;
  88. acl_tokenpos += yyleng;
  89. return ACL_AT_TOK;
  90. }
  91. authenticate {
  92. last_string = NULL;
  93. acl_tokenpos += yyleng;
  94. return ACL_AUTHENTICATE_TOK;
  95. }
  96. content {
  97. last_string = NULL;
  98. acl_tokenpos += yyleng;
  99. return ACL_CONTENT_TOK;
  100. }
  101. default {
  102. last_string = NULL;
  103. acl_tokenpos += yyleng;
  104. return ACL_DEFAULT_TOK;
  105. }
  106. deny {
  107. last_string = NULL;
  108. acl_tokenpos += yyleng;
  109. return ACL_DENY_TOK;
  110. }
  111. in {
  112. last_string = NULL;
  113. acl_tokenpos += yyleng;
  114. return ACL_IN_TOK;
  115. }
  116. inherit {
  117. last_string = NULL;
  118. acl_tokenpos += yyleng;
  119. return ACL_INHERIT_TOK;
  120. }
  121. terminal {
  122. last_string = NULL;
  123. acl_tokenpos += yyleng;
  124. return ACL_TERMINAL_TOK;
  125. }
  126. version {
  127. last_string = NULL;
  128. acl_tokenpos += yyleng;
  129. return ACL_VERSION_TOK;
  130. }
  131. with {
  132. last_string = NULL;
  133. acl_tokenpos += yyleng;
  134. return ACL_WITH_TOK;
  135. }
  136. not {
  137. last_string = NULL;
  138. acl_tokenpos += yyleng;
  139. return ACL_NOT_TOK;
  140. }
  141. and {
  142. last_string = NULL;
  143. yylval.ival = ACL_EXPR_OP_AND;
  144. acl_tokenpos += yyleng;
  145. return ACL_AND_TOK;
  146. }
  147. or {
  148. last_string = NULL;
  149. yylval.ival = ACL_EXPR_OP_OR;
  150. acl_tokenpos += yyleng;
  151. return ACL_OR_TOK;
  152. }
  153. "=" {
  154. last_string = NULL;
  155. yylval.ival = CMP_OP_EQ;
  156. acl_tokenpos += yyleng;
  157. return ACL_EQ_TOK;
  158. }
  159. ">=" {
  160. last_string = NULL;
  161. yylval.ival = CMP_OP_GE;
  162. acl_tokenpos += yyleng;
  163. return ACL_GE_TOK;
  164. }
  165. ">" {
  166. last_string = NULL;
  167. yylval.ival = CMP_OP_GT;
  168. acl_tokenpos += yyleng;
  169. return ACL_GT_TOK;
  170. }
  171. "<" {
  172. last_string = NULL;
  173. yylval.ival = CMP_OP_LT;
  174. acl_tokenpos += yyleng;
  175. return ACL_LT_TOK;
  176. }
  177. "<=" {
  178. last_string = NULL;
  179. yylval.ival = CMP_OP_LE;
  180. acl_tokenpos += yyleng;
  181. return ACL_LE_TOK;
  182. }
  183. "!=" {
  184. last_string = NULL;
  185. yylval.ival = CMP_OP_NE;
  186. acl_tokenpos += yyleng;
  187. return ACL_NE_TOK;
  188. }
  189. [(){},;] {
  190. last_string = NULL;
  191. acl_tokenpos += yyleng;
  192. return yytext[0];
  193. }
  194. {variable} {
  195. acl_tokenpos += yyleng;
  196. yylval.string = PERM_STRDUP( yytext );
  197. last_string = yylval.string;
  198. return ACL_VARIABLE_TOK;
  199. }
  200. %%
  201. void
  202. acl_detab(char *t, char *s)
  203. {
  204. int ii;
  205. int pos;
  206. int len;
  207. if (s == NULL || t == NULL)
  208. return;
  209. len = strlen(s);
  210. pos = 0;
  211. for (ii = 0; ii < len; ii++) {
  212. if (s[ii] == '\t') {
  213. t[pos] = ' ';
  214. } else {
  215. t[pos] = s[ii];
  216. }
  217. pos++;
  218. }
  219. t[pos] = '\0';
  220. return;
  221. }
  222. void
  223. yyerror(const char *s)
  224. {
  225. char errorStr[256];
  226. #if defined(UTEST) || defined(ACL_COMPILER)
  227. printf("ACL file: %s\n", acl_filename);
  228. printf("Syntax error at line: %d, token: %s\n", acl_lineno, yytext);
  229. if ( last_string )
  230. free(last_string);
  231. #else
  232. sprintf(errorStr, "%d", acl_lineno);
  233. if (yytext) {
  234. nserrGenerate(acl_errp, ACLERRPARSE, ACLERR1780, ACL_Program,
  235. 3, acl_filename, errorStr, yytext);
  236. } else {
  237. nserrGenerate(acl_errp, ACLERRPARSE, ACLERR1780, ACL_Program,
  238. 2, acl_filename, errorStr);
  239. }
  240. if ( last_string )
  241. free(last_string);
  242. #endif
  243. }
  244. int
  245. acl_InitScanner(NSErr_t *errp, char *filename, char *buffer)
  246. {
  247. acl_errp = errp;
  248. acl_lineno = 1;
  249. acl_use_buffer = (filename == NULL) ? 1 : 0 ;
  250. if ( filename != NULL ) {
  251. strcpy(acl_filename, filename);
  252. #ifdef UTEST
  253. yyin = fopen(filename, "r");
  254. if ( yyin == NULL ) {
  255. return(-1);
  256. }
  257. #else
  258. acl_prfd = system_fopenRO(filename);
  259. if ( acl_prfd == NULL ) {
  260. return(-1);
  261. }
  262. yyin = (FILE *) acl_prfd;
  263. #endif
  264. yyrestart(yyin);
  265. } else if ( buffer != NULL ) {
  266. strcpy(acl_filename, "internal-buffer");
  267. acl_buffer_offset = 0;
  268. acl_buffer_length = strlen(buffer);
  269. acl_buffer = PERM_STRDUP(buffer);
  270. if (acl_buffer == NULL)
  271. return(-1);
  272. yyrestart(NULL);
  273. } else {
  274. return(-1);
  275. }
  276. return(0);
  277. }
  278. int
  279. acl_EndScanner()
  280. {
  281. acl_lineno = 0;
  282. if ( acl_use_buffer) {
  283. if ( acl_buffer )
  284. PERM_FREE(acl_buffer);
  285. } else if ( yyin ) {
  286. #ifdef UTEST
  287. fclose(yyin);
  288. #else
  289. if ( acl_prfd ) {
  290. system_fclose(acl_prfd);
  291. acl_prfd = NULL;
  292. }
  293. #endif
  294. yyin = NULL ;
  295. }
  296. return(0);
  297. }
  298. int
  299. yywrap()
  300. {
  301. return(1);
  302. }
  303. static int
  304. acl_scanner_input(char *buffer, int max_size)
  305. {
  306. int len = 0;
  307. if ( acl_use_buffer ) {
  308. len = (acl_buffer_length > max_size) ? max_size :
  309. acl_buffer_length;
  310. memcpy(buffer, (const void *) &acl_buffer[acl_buffer_offset],
  311. len);
  312. acl_buffer_length -= len;
  313. acl_buffer_offset += len;
  314. }
  315. #ifdef UTEST
  316. else if ( ((len = fread( buffer, 1, max_size, aclin )) == 0)
  317. && ferror( aclin ) ) {
  318. yyerror( "scanner input failed" );
  319. }
  320. #else
  321. else if ( (len = system_fread( acl_prfd, buffer, max_size)) < 0 ) {
  322. yyerror( "scanner input failed" );
  323. }
  324. #endif
  325. return(len);
  326. }