summaryrefslogtreecommitdiff
path: root/usr/src/lib/libsec/common/acl.y
blob: fe3a1416052070ee3467615f22a5ea06e3fcdc5e (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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
%{
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <acl_common.h>
#include <aclutils.h>

extern int yyinteractive;
extern acl_t *yyacl;
%}

%union {
	char *str;
	int val;
	struct acl_perm_type acl_perm;
	ace_t ace;
	aclent_t aclent;
	acl_t *acl;
}


%token USER_TOK USER_SID_TOK GROUP_TOK GROUP_SID_TOK MASK_TOK OTHER_TOK
%token OWNERAT_TOK GROUPAT_TOK EVERYONEAT_TOK DEFAULT_USER_TOK 
%token DEFAULT_GROUP_TOK DEFAULT_MASK_TOK DEFAULT_OTHER_TOK
%token COLON COMMA NL SLASH
%token <str> ID IDNAME PERM_TOK INHERIT_TOK SID
%token <val> ERROR ACE_PERM ACE_INHERIT ENTRY_TYPE ACCESS_TYPE

%type <str> idname id
%type <acl_perm> perms perm aclent_perm ace_perms
%type <acl> acl_entry
%type <ace> ace 
%type <aclent> aclent
%type <val> iflags verbose_iflag compact_iflag access_type entry_type

%left ERROR COLON

%%

acl:	acl_entry NL 
	{ 
		yyacl = $1;
		return (0);
	} 

	/* This seems illegal, but the old aclfromtext() allows it */
	| acl_entry COMMA NL	
	{
		yyacl = $1;
		return (0);
	}
	| acl_entry COMMA acl 
	{ 
		yyacl = $1;
		return (0);
	}
	
acl_entry: ace 
	{
		ace_t *acep;

		if (yyacl == NULL) {
			yyacl = acl_alloc(ACE_T);
			if (yyacl == NULL) {
				yycleanup();
				return (EACL_MEM_ERROR);
			}
		} 

		$$ = yyacl;
		if ($$->acl_type == ACLENT_T) {
			acl_error(dgettext(TEXT_DOMAIN,
			    "Cannot have POSIX draft ACL entries"
			    " with NFSv4/ZFS ACL entries.\n"));
			acl_free(yyacl);
			yyacl = NULL;
			yycleanup();
			return (EACL_DIFF_TYPE);
		}
			
		$$->acl_aclp = realloc($$->acl_aclp,
		    ($$->acl_entry_size * ($$->acl_cnt + 1)));
		if ($$->acl_aclp == NULL) {
			free (yyacl);
			yycleanup();
			return (EACL_MEM_ERROR);	
		}
		acep = $$->acl_aclp;
		acep[$$->acl_cnt] = $1;
		$$->acl_cnt++;
		yycleanup();
	}
	| aclent
	{
		aclent_t *aclent;

		if (yyacl == NULL) {
			yyacl = acl_alloc(ACLENT_T);
			if (yyacl == NULL) {
				yycleanup();
				return (EACL_MEM_ERROR);
			}
		} 

		$$ = yyacl;
		if ($$->acl_type == ACE_T) {
			acl_error(dgettext(TEXT_DOMAIN,
			    "Cannot have NFSv4/ZFS ACL entries"
			    " with POSIX draft ACL entries.\n"));
			acl_free(yyacl);
			yyacl = NULL;
			yycleanup();
			return (EACL_DIFF_TYPE);
		}

		$$->acl_aclp = realloc($$->acl_aclp,
		    ($$->acl_entry_size  * ($$->acl_cnt +1)));
		if ($$->acl_aclp == NULL) {
			free (yyacl);
			yycleanup();
			return (EACL_MEM_ERROR);	
		}
		aclent = $$->acl_aclp;
		aclent[$$->acl_cnt] = $1;
		$$->acl_cnt++;
		yycleanup();
	}

ace:	entry_type idname ace_perms access_type
	{
		int error;
		uid_t id;
		int mask;

		error = get_id($1, $2, &id);
		if (error) {
			bad_entry_type($1, $2);
			yycleanup();
			return (EACL_INVALID_USER_GROUP);
		}
			
		$$.a_who = id;
		$$.a_flags = ace_entry_type($1);
		error = ace_perm_mask(&$3, &$$.a_access_mask);
		if (error) {
			yycleanup();
			return (error);
		}
		$$.a_type = $4;

	}
	| entry_type idname ace_perms access_type COLON id
	{
		int error;
		uid_t id;

		if (yyinteractive) {
			acl_error(dgettext(TEXT_DOMAIN,
			    "Extra fields on the end of "
			    "ACL specification.\n"));
			yycleanup();
			return (EACL_UNKNOWN_DATA);
		}
		error = get_id($1, $2, &id);
		if (error) {
			$$.a_who = get_id_nofail($1, $6);
		} else {
			$$.a_who = id;
		}
		$$.a_flags = ace_entry_type($1);
		error = ace_perm_mask(&$3, &$$.a_access_mask);
		if (error) {
			yycleanup();
			return (error);
		}
		$$.a_type = $4;
	}
	| entry_type idname ace_perms iflags access_type 
	{
		int error;
		uid_t id;

		error = get_id($1, $2, &id);
		if (error) {
			bad_entry_type($1, $2);
			yycleanup();
			return (EACL_INVALID_USER_GROUP);
		}
		
		$$.a_who = id;
		$$.a_flags = ace_entry_type($1);
		error = ace_perm_mask(&$3, &$$.a_access_mask);
		if (error) {
			yycleanup();
			return (error);
		}
		$$.a_type = $5;
		$$.a_flags |= $4;
	}
	| entry_type idname ace_perms iflags access_type COLON id
	{
		int error;
		uid_t  id;

		if (yyinteractive) {
			acl_error(dgettext(TEXT_DOMAIN,
			    "Extra fields on the end of "
			    "ACL specification.\n"));
			yycleanup();
			return (EACL_UNKNOWN_DATA);
		}
		error = get_id($1, $2, &id);
		if (error) {
			$$.a_who = get_id_nofail($1, $7);
		} else {
			$$.a_who = id;
		}

		$$.a_flags = ace_entry_type($1);
		error = ace_perm_mask(&$3, &$$.a_access_mask);
		if (error) {
			yycleanup();
			return (error);
		}

		$$.a_type = $5;
		$$.a_flags |= $4;
	}
	| entry_type ace_perms access_type
	{ 
		int error;

		$$.a_who = -1;
		$$.a_flags = ace_entry_type($1);
		error = ace_perm_mask(&$2, &$$.a_access_mask);
		if (error) {
			yycleanup();
			return (error);
		}
		$$.a_type = $3;
	} 
	| entry_type ace_perms access_type COLON id
	{
		yycleanup();
		if (yyinteractive) {
			acl_error(dgettext(TEXT_DOMAIN,
			    "Extra fields on the end of "
			    "ACL specification.\n"));
			return (EACL_UNKNOWN_DATA);
		}

		return (EACL_ENTRY_ERROR);
	}
	| entry_type ace_perms iflags access_type 
	{
		int error;

		$$.a_who = -1;
		$$.a_flags = ace_entry_type($1);
		error = ace_perm_mask(&$2, &$$.a_access_mask);
		if (error) {
			yycleanup();
			return (error);
		}
		$$.a_type = $4;
		$$.a_flags |= $3;

	}
	| entry_type ace_perms iflags access_type COLON id
	{
		yycleanup();
		if (yyinteractive) {
			acl_error(dgettext(TEXT_DOMAIN,
			    "Extra fields on the end of "
			    "ACL specification.\n"));
			return (EACL_UNKNOWN_DATA);
		}
		return (EACL_ENTRY_ERROR);
	}

aclent: entry_type idname aclent_perm	/* user or group */
	{
		int error;
		uid_t id;

		error = get_id($1, $2, &id);
		if (error) {
			bad_entry_type($1, $2);
			yycleanup();
			return (EACL_INVALID_USER_GROUP);
		}

		error = compute_aclent_perms($3.perm_str, &$$.a_perm);
		if (error) {
			acl_error(dgettext(TEXT_DOMAIN,
			    "Invalid permission(s) '%s' specified.\n"),
			    $3.perm_str);
			yycleanup();
			return (error);
		}
		$$.a_id = id;
		error = aclent_entry_type($1, 0, &$$.a_type);
		if (error) {
			acl_error(
			    dgettext(TEXT_DOMAIN,
			    "Invalid ACL entry type '%s' specified.\n"), $1);
			yycleanup();
			return (error);
		}
	}
	| entry_type COLON aclent_perm		/* owner group other */
	{
		int error;

		error = compute_aclent_perms($3.perm_str, &$$.a_perm);
		if (error) {
			acl_error(dgettext(TEXT_DOMAIN,
			    "Invalid permission(s) '%s' specified.\n"),
			    $3.perm_str);
			yycleanup();
			return (error);
		}
		$$.a_id = -1;
		error = aclent_entry_type($1, 1, &$$.a_type);
		if (error) {
			acl_error(
			    dgettext(TEXT_DOMAIN,
			    "Invalid ACL entry type '%s' specified.\n"), $1);
			yycleanup();
			return (error);
		}
	}
	| entry_type COLON aclent_perm COLON id
	{ 
		yycleanup();
		if (yyinteractive) {
			acl_error(dgettext(TEXT_DOMAIN,
			    "Extra fields on the end of ACL specification.\n"));
			return (EACL_UNKNOWN_DATA);
		}
		return (EACL_ENTRY_ERROR);
	}
	| entry_type idname aclent_perm COLON id 	/* user or group */
	{	
		int error;
		uid_t id;

		if (yyinteractive) {
			acl_error(dgettext(TEXT_DOMAIN,
			    "Extra fields on the end of ACL specification.\n"));
			yycleanup();
			return (EACL_UNKNOWN_DATA);
		}
		error = compute_aclent_perms($3.perm_str, &$$.a_perm);
		if (error) {
			acl_error(dgettext(TEXT_DOMAIN,
			    "Invalid permission(s) '%s' specified.\n"),
			    $3.perm_str);
			yycleanup();
			return (error);
		}
		error = get_id($1, $2, &id);
		if (error) {
			$$.a_id = get_id_nofail($1, $5);
		} else 
			$$.a_id = id;

		error = aclent_entry_type($1, 0, &$$.a_type);
		if (error) {
			acl_error(
			    dgettext(TEXT_DOMAIN,
			    "Invalid ACL entry type '%s' specified.\n"), $1);
			yycleanup();
			return (error);
		}
	}
	| entry_type aclent_perm  /* mask entry */
	{
		int error;

		error = compute_aclent_perms($2.perm_str, &$$.a_perm);
		if (error) {
			acl_error(dgettext(TEXT_DOMAIN,
			    "Invalid permission(s) '%s' specified.\n"),
			    $2.perm_str);
			yycleanup();
			return (error);
		}
		$$.a_id = -1;
		error = aclent_entry_type($1, 0, &$$.a_type);
		if (error) {
			acl_error(
			    dgettext(TEXT_DOMAIN,
			    "Invalid ACL entry type specified %d.\n"),
			    error);
			yycleanup();
			return (error);
		}
	}
	| entry_type aclent_perm COLON id
	{
		yycleanup();
		if (yyinteractive) {
			acl_error(dgettext(TEXT_DOMAIN,
			    "Extra fields on the end of ACL specification.\n"));
			return (EACL_UNKNOWN_DATA);
		}
		return (EACL_ENTRY_ERROR);
	}

iflags: compact_iflag COLON {$$ = $1;}
	| verbose_iflag COLON {$$ = $1;}
	| COLON {$$ = 0;}

compact_iflag : INHERIT_TOK
	{
		int error;
		uint32_t iflags;

		error = compute_ace_inherit($1, &iflags);
		if (error) {
			acl_error(dgettext(TEXT_DOMAIN,
			    "Invalid inheritance flags '%s' specified.\n"), $1);
			yycleanup();
			return (error);
		}
		$$ = iflags;
	}
	| INHERIT_TOK SLASH verbose_iflag
	{
		acl_error(dgettext(TEXT_DOMAIN,
		    "Can't mix compact inherit flags with"
		    " verbose inheritance flags.\n"));
		yycleanup();
		return (EACL_INHERIT_ERROR);
	}

verbose_iflag: ACE_INHERIT	{$$ |= $1;}
	| ACE_INHERIT SLASH verbose_iflag {$$ = $1 | $3;}
	| ACE_INHERIT SLASH compact_iflag
	{
		acl_error(dgettext(TEXT_DOMAIN,
		    "Can't mix verbose inherit flags with"
		    " compact inheritance flags.\n"));
		yycleanup();
		return (EACL_INHERIT_ERROR);
	}
	| ACE_INHERIT SLASH ACCESS_TYPE
	{
		acl_error(dgettext(TEXT_DOMAIN,
		    "Inheritance flags can't be mixed with access type.\n"));
		yycleanup();
		return (EACL_INHERIT_ERROR);
	}
	| ACE_INHERIT SLASH ERROR
	{
		yycleanup();
		return ($3);
	}
	
aclent_perm: PERM_TOK
	{
		$$.perm_style = PERM_TYPE_UNKNOWN;
		$$.perm_str = $1;
		$$.perm_val = 0;
	}
	| PERM_TOK ERROR 
	{
		acl_error(dgettext(TEXT_DOMAIN,
		    "ACL entry permissions are incorrectly specified.\n"));
		yycleanup();
		return ($2);
	}

access_type: ACCESS_TYPE {$$ = $1;}	
	| ERROR
	{
		yycleanup();
		return ($1);
	}

id: ID {$$ = $1;}
	| SID {$$ = $1;}
  	| COLON
	{
		acl_error(dgettext(TEXT_DOMAIN,
		    "Invalid uid/gid specified.\nThe field"
		    " should be a numeric value.\n")); 
		yycleanup();
		return (EACL_UNKNOWN_DATA);
	}
	| ERROR
	{
		yycleanup();
		return ($1);
	}

ace_perms: perm {$$ = $1;}
	| aclent_perm COLON {$$ = $1;}
	| ERROR
	{
		yycleanup();
		return ($1);
	}

perm: perms COLON {$$ = $1;}
    	| COLON {$$.perm_style = PERM_TYPE_EMPTY;}

perms: ACE_PERM 
     	{
		$$.perm_style = PERM_TYPE_ACE;
		$$.perm_val |= $1;
	}
	| ACE_PERM SLASH perms
	{
		$$.perm_style = PERM_TYPE_ACE;
		$$.perm_val = $1 | $3.perm_val;
	}
	| ACE_PERM SLASH aclent_perm
	{

		acl_error(dgettext(TEXT_DOMAIN,
		   "Can't mix verbose permissions with"
		    " compact permission.\n"));
		yycleanup();
		return (EACL_PERM_MASK_ERROR);

	}
	| ACE_PERM SLASH ERROR
	{
		yycleanup();
		return ($3);
	}
		

idname: IDNAME {$$ = $1;}

entry_type: ENTRY_TYPE {$$ = $1;}
	| ERROR
	{
		yycleanup();
		return ($1);
	}

%%
static void
bad_entry_type(int toketype, char *str)
{
	switch(toketype) {
	case USER_TOK:
	case DEFAULT_USER_TOK:
		acl_error(dgettext(TEXT_DOMAIN,
		    "Invalid user %s specified.\n"), str);
		break;

	case GROUP_TOK:
	case DEFAULT_GROUP_TOK:
		acl_error(dgettext(TEXT_DOMAIN,
		    "Invalid group %s specified.\n"), str);
		break;
	
	case USER_SID_TOK:
		acl_error(dgettext(TEXT_DOMAIN,
		    "Invalid user SID %s specified.\n"), str);
		break;

	case GROUP_SID_TOK:
		acl_error(dgettext(TEXT_DOMAIN,
		    "Invalid group SID %s specified.\n"), str);
	}

}