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