aclscan.l 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. /*
  39. * Lexical analyzer for ACL
  40. */
  41. %{
  42. #include "acl.tab.h" /* token codes */
  43. #include <base/file.h>
  44. #include <libaccess/acl.h>
  45. #include <libaccess/nserror.h>
  46. #include "aclpriv.h"
  47. #include <string.h>
  48. #include <stdio.h>
  49. #include <ctype.h>
  50. #include <libaccess/aclerror.h>
  51. #ifdef XP_WIN32
  52. #include <io.h>
  53. #endif
  54. #include "parse.h"
  55. #include "aclscan.h"
  56. static int acl_scanner_input(char *buffer, int max_size);
  57. #define YY_NEVER_INTERACTIVE 1
  58. #undef YY_INPUT
  59. #define YY_INPUT(buf, result, max) (result = acl_scanner_input(buf, max))
  60. static int acl_lineno;
  61. static int acl_tokenpos;
  62. static char acl_filename[500];
  63. static NSErr_t *acl_errp;
  64. static int acl_use_buffer;
  65. static char *acl_buffer;
  66. static int acl_buffer_length;
  67. static int acl_buffer_offset;
  68. static char *last_string;
  69. static SYS_FILE acl_prfd;
  70. %}
  71. ws [ \t]+
  72. comment #.*
  73. qstring \"[^\"\n]*[\"\n]
  74. variable [\*a-zA-Z0-9\.\-\_][\*a-zA-Z0-9\.\-\_]*
  75. %%
  76. \n.* {
  77. acl_lineno++;
  78. acl_tokenpos = 0;
  79. yyless(1);
  80. }
  81. {ws} ;
  82. {comment} ;
  83. <<EOF>> {
  84. yylval.string = NULL;
  85. last_string = yylval.string;
  86. return(0);
  87. }
  88. {qstring} {
  89. yylval.string = PERM_STRDUP( yytext+1 );
  90. last_string = yylval.string;
  91. if ( yylval.string[yyleng-2] != '"' )
  92. fprintf(stderr, "Unterminated string\n") ;
  93. else
  94. yylval.string[yyleng-2] = '\0';
  95. acl_tokenpos += yyleng;
  96. return ACL_QSTRING_TOK;
  97. }
  98. absolute {
  99. last_string = NULL;
  100. acl_tokenpos += yyleng;
  101. return ACL_ABSOLUTE_TOK;
  102. }
  103. acl {
  104. last_string = NULL;
  105. acl_tokenpos += yyleng;
  106. return ACL_ACL_TOK;
  107. }
  108. allow {
  109. last_string = NULL;
  110. acl_tokenpos += yyleng;
  111. return ACL_ALLOW_TOK;
  112. }
  113. always {
  114. last_string = NULL;
  115. acl_tokenpos += yyleng;
  116. return ACL_ALWAYS_TOK;
  117. }
  118. at {
  119. last_string = NULL;
  120. acl_tokenpos += yyleng;
  121. return ACL_AT_TOK;
  122. }
  123. authenticate {
  124. last_string = NULL;
  125. acl_tokenpos += yyleng;
  126. return ACL_AUTHENTICATE_TOK;
  127. }
  128. content {
  129. last_string = NULL;
  130. acl_tokenpos += yyleng;
  131. return ACL_CONTENT_TOK;
  132. }
  133. default {
  134. last_string = NULL;
  135. acl_tokenpos += yyleng;
  136. return ACL_DEFAULT_TOK;
  137. }
  138. deny {
  139. last_string = NULL;
  140. acl_tokenpos += yyleng;
  141. return ACL_DENY_TOK;
  142. }
  143. in {
  144. last_string = NULL;
  145. acl_tokenpos += yyleng;
  146. return ACL_IN_TOK;
  147. }
  148. inherit {
  149. last_string = NULL;
  150. acl_tokenpos += yyleng;
  151. return ACL_INHERIT_TOK;
  152. }
  153. terminal {
  154. last_string = NULL;
  155. acl_tokenpos += yyleng;
  156. return ACL_TERMINAL_TOK;
  157. }
  158. version {
  159. last_string = NULL;
  160. acl_tokenpos += yyleng;
  161. return ACL_VERSION_TOK;
  162. }
  163. with {
  164. last_string = NULL;
  165. acl_tokenpos += yyleng;
  166. return ACL_WITH_TOK;
  167. }
  168. not {
  169. last_string = NULL;
  170. acl_tokenpos += yyleng;
  171. return ACL_NOT_TOK;
  172. }
  173. and {
  174. last_string = NULL;
  175. yylval.ival = ACL_EXPR_OP_AND;
  176. acl_tokenpos += yyleng;
  177. return ACL_AND_TOK;
  178. }
  179. or {
  180. last_string = NULL;
  181. yylval.ival = ACL_EXPR_OP_OR;
  182. acl_tokenpos += yyleng;
  183. return ACL_OR_TOK;
  184. }
  185. "=" {
  186. last_string = NULL;
  187. yylval.ival = CMP_OP_EQ;
  188. acl_tokenpos += yyleng;
  189. return ACL_EQ_TOK;
  190. }
  191. ">=" {
  192. last_string = NULL;
  193. yylval.ival = CMP_OP_GE;
  194. acl_tokenpos += yyleng;
  195. return ACL_GE_TOK;
  196. }
  197. ">" {
  198. last_string = NULL;
  199. yylval.ival = CMP_OP_GT;
  200. acl_tokenpos += yyleng;
  201. return ACL_GT_TOK;
  202. }
  203. "<" {
  204. last_string = NULL;
  205. yylval.ival = CMP_OP_LT;
  206. acl_tokenpos += yyleng;
  207. return ACL_LT_TOK;
  208. }
  209. "<=" {
  210. last_string = NULL;
  211. yylval.ival = CMP_OP_LE;
  212. acl_tokenpos += yyleng;
  213. return ACL_LE_TOK;
  214. }
  215. "!=" {
  216. last_string = NULL;
  217. yylval.ival = CMP_OP_NE;
  218. acl_tokenpos += yyleng;
  219. return ACL_NE_TOK;
  220. }
  221. [(){},;] {
  222. last_string = NULL;
  223. acl_tokenpos += yyleng;
  224. return yytext[0];
  225. }
  226. {variable} {
  227. acl_tokenpos += yyleng;
  228. yylval.string = PERM_STRDUP( yytext );
  229. last_string = yylval.string;
  230. return ACL_VARIABLE_TOK;
  231. }
  232. %%
  233. void
  234. acl_detab(char *t, char *s)
  235. {
  236. int ii;
  237. int pos;
  238. int len;
  239. if (s == NULL || t == NULL)
  240. return;
  241. len = strlen(s);
  242. pos = 0;
  243. for (ii = 0; ii < len; ii++) {
  244. if (s[ii] == '\t') {
  245. t[pos] = ' ';
  246. } else {
  247. t[pos] = s[ii];
  248. }
  249. pos++;
  250. }
  251. t[pos] = '\0';
  252. return;
  253. }
  254. void
  255. yyerror(const char *s)
  256. {
  257. char errorStr[256];
  258. #if defined(UTEST) || defined(ACL_COMPILER)
  259. printf("ACL file: %s\n", acl_filename);
  260. printf("Syntax error at line: %d, token: %s\n", acl_lineno, yytext);
  261. if ( last_string )
  262. free(last_string);
  263. #else
  264. sprintf(errorStr, "%d", acl_lineno);
  265. if (yytext) {
  266. nserrGenerate(acl_errp, ACLERRPARSE, ACLERR1780, ACL_Program,
  267. 3, acl_filename, errorStr, yytext);
  268. } else {
  269. nserrGenerate(acl_errp, ACLERRPARSE, ACLERR1780, ACL_Program,
  270. 2, acl_filename, errorStr);
  271. }
  272. if ( last_string )
  273. free(last_string);
  274. #endif
  275. }
  276. int
  277. acl_InitScanner(NSErr_t *errp, char *filename, char *buffer)
  278. {
  279. acl_errp = errp;
  280. acl_lineno = 1;
  281. acl_use_buffer = (filename == NULL) ? 1 : 0 ;
  282. if ( filename != NULL ) {
  283. strcpy(acl_filename, filename);
  284. #ifdef UTEST
  285. yyin = fopen(filename, "r");
  286. if ( yyin == NULL ) {
  287. return(-1);
  288. }
  289. #else
  290. acl_prfd = system_fopenRO(filename);
  291. if ( acl_prfd == NULL ) {
  292. return(-1);
  293. }
  294. yyin = (FILE *) acl_prfd;
  295. #endif
  296. yyrestart(yyin);
  297. } else if ( buffer != NULL ) {
  298. strcpy(acl_filename, "internal-buffer");
  299. acl_buffer_offset = 0;
  300. acl_buffer_length = strlen(buffer);
  301. acl_buffer = PERM_STRDUP(buffer);
  302. if (acl_buffer == NULL)
  303. return(-1);
  304. yyrestart(NULL);
  305. } else {
  306. return(-1);
  307. }
  308. return(0);
  309. }
  310. int
  311. acl_EndScanner()
  312. {
  313. acl_lineno = 0;
  314. if ( acl_use_buffer) {
  315. if ( acl_buffer )
  316. PERM_FREE(acl_buffer);
  317. } else if ( yyin ) {
  318. #ifdef UTEST
  319. fclose(yyin);
  320. #else
  321. if ( acl_prfd ) {
  322. system_fclose(acl_prfd);
  323. acl_prfd = NULL;
  324. }
  325. #endif
  326. yyin = NULL ;
  327. }
  328. return(0);
  329. }
  330. int
  331. yywrap()
  332. {
  333. return(1);
  334. }
  335. static int
  336. acl_scanner_input(char *buffer, int max_size)
  337. {
  338. int len = 0;
  339. if ( acl_use_buffer ) {
  340. len = (acl_buffer_length > max_size) ? max_size :
  341. acl_buffer_length;
  342. memcpy(buffer, (const void *) &acl_buffer[acl_buffer_offset],
  343. len);
  344. acl_buffer_length -= len;
  345. acl_buffer_offset += len;
  346. }
  347. #ifdef UTEST
  348. else if ( ((len = fread( buffer, 1, max_size, aclin )) == 0)
  349. && ferror( aclin ) ) {
  350. yyerror( "scanner input failed" );
  351. }
  352. #else
  353. else if ( (len = system_fread( acl_prfd, buffer, max_size)) < 0 ) {
  354. yyerror( "scanner input failed" );
  355. }
  356. #endif
  357. return(len);
  358. }