aclscan.l 8.7 KB

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