summaryrefslogtreecommitdiffstats
path: root/lib/libaccess/aclscan.l
blob: 009e02db25a6f0d662bb7999d48cf8f723539396 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/** BEGIN COPYRIGHT BLOCK
 * Copyright 2001 Sun Microsystems, Inc.
 * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

/*
 * Lexical analyzer for ACL
 */

%{
#include "acl.tab.h"	/* token codes */
#include <base/file.h>
#include <libaccess/acl.h>
#include <libaccess/nserror.h>
#include "aclpriv.h"
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <libaccess/aclerror.h>
#ifdef XP_WIN32
#include <io.h>
#endif

#include "parse.h"
#include "aclscan.h"

static int acl_scanner_input(char *buffer, int max_size);

#define YY_NEVER_INTERACTIVE 1
#undef YY_INPUT
#define YY_INPUT(buf, result, max) (result = acl_scanner_input(buf, max))

static int acl_lineno;
static int acl_tokenpos;
static char acl_filename[500];
static NSErr_t *acl_errp;
static int acl_use_buffer;
static char *acl_buffer;
static int acl_buffer_length;
static int acl_buffer_offset;
static char *last_string;
static SYS_FILE acl_prfd;


%}

ws		[ \t]+
comment		#.*
qstring		\"[^\"\n]*[\"\n]
variable	[\*a-zA-Z0-9\.\-\_][\*a-zA-Z0-9\.\-\_]*

%%

\n.*		{ 
			acl_lineno++;
			acl_tokenpos = 0;
			yyless(1);
		}

{ws}		;

{comment}	;

<<EOF>>		{	
			yylval.string = NULL;
			last_string = yylval.string;
			return(0);
		}

{qstring}	{ 
			yylval.string = PERM_STRDUP( yytext+1 );
			last_string = yylval.string;
			if ( yylval.string[yyleng-2] != '"' )
				fprintf(stderr, "Unterminated string\n") ;
			else
				yylval.string[yyleng-2] = '\0';
			acl_tokenpos += yyleng;
			return ACL_QSTRING_TOK;
		}


absolute	{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return ACL_ABSOLUTE_TOK; 
		}

acl		{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return ACL_ACL_TOK; 
		}

allow		{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return ACL_ALLOW_TOK; 
		}

always		{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return ACL_ALWAYS_TOK; 
		}

at		{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return ACL_AT_TOK; 
		}

authenticate	{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return ACL_AUTHENTICATE_TOK; 
		}

content		{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return ACL_CONTENT_TOK; 
		}

default		{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return ACL_DEFAULT_TOK; 
		}

deny		{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return ACL_DENY_TOK; 
		}

in		{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return ACL_IN_TOK; 
		}

inherit		{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return ACL_INHERIT_TOK; 
		}

terminal	{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return ACL_TERMINAL_TOK; 
		}

version		{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return ACL_VERSION_TOK; 
		}

with		{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return ACL_WITH_TOK; 
		}

not		{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return ACL_NOT_TOK; 
		}

and 		{ 
			last_string = NULL;
			yylval.ival = ACL_EXPR_OP_AND;
			acl_tokenpos += yyleng; 
			return ACL_AND_TOK; 
		}

or		{ 
			last_string = NULL;
			yylval.ival = ACL_EXPR_OP_OR;
			acl_tokenpos += yyleng; 
			return ACL_OR_TOK; 
		}

"="		{ 
			last_string = NULL;
			yylval.ival = CMP_OP_EQ;
			acl_tokenpos += yyleng; 
			return ACL_EQ_TOK; 
		}

">="		{ 
			last_string = NULL;
			yylval.ival = CMP_OP_GE; 
			acl_tokenpos += yyleng; 
			return ACL_GE_TOK; 
		}

">"		{ 
			last_string = NULL;
			yylval.ival = CMP_OP_GT; 
			acl_tokenpos += yyleng; 
			return ACL_GT_TOK; 
		}

"<"		{ 	
			last_string = NULL;
			yylval.ival = CMP_OP_LT; 
			acl_tokenpos += yyleng; 
			return ACL_LT_TOK; 
		}

"<="		{ 
			last_string = NULL;
			yylval.ival = CMP_OP_LE; 
			acl_tokenpos += yyleng; 
			return ACL_LE_TOK; 
		}

"!="		{ 
			last_string = NULL;
			yylval.ival = CMP_OP_NE;
			acl_tokenpos += yyleng; 
			return ACL_NE_TOK; 
		}

[(){},;]	{ 
			last_string = NULL;
			acl_tokenpos += yyleng; 
			return yytext[0]; 
		}

{variable}	{ 
			acl_tokenpos += yyleng; 
			yylval.string = PERM_STRDUP( yytext );
			last_string = yylval.string;
			return ACL_VARIABLE_TOK;
		}
%%

void
acl_detab(char *t, char *s)
{
	int	ii;
	int	pos;
	int	len;

	if (s == NULL || t == NULL) 
		return;

	len = strlen(s);
	pos = 0;
	for (ii = 0; ii < len; ii++) {
		if (s[ii] == '\t') {
			t[pos] = ' ';
		} else {
			t[pos] = s[ii];
		}
		pos++;
	}
	t[pos] = '\0';
	return;
}

void
yyerror(const char *s)
{
char	errorStr[256];

#if defined(UTEST) || defined(ACL_COMPILER)
	printf("ACL file: %s\n", acl_filename);
	printf("Syntax error at line: %d, token: %s\n", acl_lineno, yytext);
	if ( last_string )
		free(last_string);
#else
	sprintf(errorStr, "%d", acl_lineno);
	if (yytext) {
		nserrGenerate(acl_errp, ACLERRPARSE, ACLERR1780, ACL_Program,
                      3, acl_filename, errorStr, yytext);
	} else {
		nserrGenerate(acl_errp, ACLERRPARSE, ACLERR1780, ACL_Program,
                      2, acl_filename, errorStr);
	}
	if ( last_string )
		free(last_string);
#endif

}

int
acl_InitScanner(NSErr_t *errp, char *filename, char *buffer)
{
	acl_errp = errp;
	acl_lineno = 1;
	acl_use_buffer = (filename == NULL) ? 1 : 0 ;
	if ( filename != NULL ) {
		strcpy(acl_filename, filename);
#ifdef UTEST
		yyin = fopen(filename, "r");
		if ( yyin == NULL ) {
			return(-1);
		}
#else
		acl_prfd = system_fopenRO(filename);
		if ( acl_prfd == NULL ) {
			return(-1);
		}
		yyin = (FILE *) acl_prfd;
#endif
		yyrestart(yyin);
	} else if ( buffer != NULL ) {
		strcpy(acl_filename, "internal-buffer");
		acl_buffer_offset = 0;
		acl_buffer_length = strlen(buffer);
		acl_buffer = PERM_STRDUP(buffer);
		if (acl_buffer == NULL)
			return(-1);
                yyrestart(NULL);
        } else {
		return(-1);
        }
	return(0);
}

int
acl_EndScanner()
{
	acl_lineno = 0;
	if ( acl_use_buffer) {
		if ( acl_buffer )
			PERM_FREE(acl_buffer);
        } else if ( yyin ) {
#ifdef UTEST
		fclose(yyin);
#else
		if ( acl_prfd ) {
			system_fclose(acl_prfd);
			acl_prfd = NULL;
		}
#endif
		yyin = NULL ;
	}
	return(0);
}

int
yywrap()
{
	return(1);
}

static int
acl_scanner_input(char *buffer, int max_size)
{
	int len = 0;

        if ( acl_use_buffer ) {
		len = (acl_buffer_length > max_size) ? max_size : 
                                                       acl_buffer_length;
		memcpy(buffer, (const void *) &acl_buffer[acl_buffer_offset], 
                       len);
		acl_buffer_length -= len;
		acl_buffer_offset += len;
        }
#ifdef UTEST
        else if ( ((len = fread( buffer, 1, max_size, aclin )) == 0) 
                    && ferror( aclin ) ) { 
                yyerror( "scanner input failed" );
	}
#else
        else if ( (len = system_fread( acl_prfd, buffer, max_size)) < 0 ) { 
                yyerror( "scanner input failed" );
	}
#endif

	return(len);
}