Chris PeBenito 134191
#!/bin/sh -
Chris PeBenito 134191
#
Chris PeBenito 134191
Chris PeBenito 134191
# FLASK
Chris PeBenito 134191
Chris PeBenito 134191
set -e
Chris PeBenito 134191
Chris PeBenito 134191
awk=$1
Chris PeBenito 134191
shift
Chris PeBenito 134191
Chris PeBenito 134191
# output files
Chris PeBenito 134191
av_permissions="av_permissions.h"
Chris PeBenito 134191
av_inherit="av_inherit.h"
Chris PeBenito 134191
common_perm_to_string="common_perm_to_string.h"
Chris PeBenito 134191
av_perm_to_string="av_perm_to_string.h"
Chris PeBenito 134191
Chris PeBenito 134191
cat $* | $awk "
Chris PeBenito 134191
BEGIN	{
Chris PeBenito 134191
		outfile = \"$av_permissions\"
Chris PeBenito 134191
		inheritfile = \"$av_inherit\"
Chris PeBenito 134191
		cpermfile = \"$common_perm_to_string\"
Chris PeBenito 134191
		avpermfile = \"$av_perm_to_string\"
Chris PeBenito 134191
		"'
Chris PeBenito 134191
		nextstate = "COMMON_OR_AV";
Chris PeBenito 134191
		printf("/* This file is automatically generated.  Do not edit. */\n") > outfile;
Chris PeBenito 134191
		printf("/* This file is automatically generated.  Do not edit. */\n") > inheritfile;
Chris PeBenito 134191
		printf("/* This file is automatically generated.  Do not edit. */\n") > cpermfile;
Chris PeBenito 134191
		printf("/* This file is automatically generated.  Do not edit. */\n") > avpermfile;
Chris PeBenito 134191
;
Chris PeBenito 134191
	}
Chris PeBenito 134191
/^[ \t]*#/	{ 
Chris PeBenito 134191
			next;
Chris PeBenito 134191
		}
Chris PeBenito 134191
$1 == "common"	{ 
Chris PeBenito 134191
			if (nextstate != "COMMON_OR_AV")
Chris PeBenito 134191
			{
Chris PeBenito 134191
				printf("Parse error:  Unexpected COMMON definition on line %d\n", NR);
Chris PeBenito 134191
				next;	
Chris PeBenito 134191
			}
Chris PeBenito 134191
Chris PeBenito 134191
			if ($2 in common_defined)
Chris PeBenito 134191
			{
Chris PeBenito 134191
				printf("Duplicate COMMON definition for %s on line %d.\n", $2, NR);
Chris PeBenito 134191
				next;
Chris PeBenito 134191
			}	
Chris PeBenito 134191
			common_defined[$2] = 1;
Chris PeBenito 134191
Chris PeBenito 134191
			tclass = $2;
Chris PeBenito 134191
			common_name = $2; 
Chris PeBenito 134191
			permission = 1;
Chris PeBenito 134191
Chris PeBenito 134191
			printf("TB_(common_%s_perm_to_string)\n", $2) > cpermfile;
Chris PeBenito 134191
Chris PeBenito 134191
			nextstate = "COMMON-OPENBRACKET";
Chris PeBenito 134191
			next;
Chris PeBenito 134191
		}
Chris PeBenito 134191
$1 == "class"	{
Chris PeBenito 134191
			if (nextstate != "COMMON_OR_AV" &&
Chris PeBenito 134191
			    nextstate != "CLASS_OR_CLASS-OPENBRACKET")
Chris PeBenito 134191
			{
Chris PeBenito 134191
				printf("Parse error:  Unexpected class definition on line %d\n", NR);
Chris PeBenito 134191
				next;	
Chris PeBenito 134191
			}
Chris PeBenito 134191
Chris PeBenito 134191
			tclass = $2;
Chris PeBenito 134191
Chris PeBenito 134191
			if (tclass in av_defined)
Chris PeBenito 134191
			{
Chris PeBenito 134191
				printf("Duplicate access vector definition for %s on line %d\n", tclass, NR);
Chris PeBenito 134191
				next;
Chris PeBenito 134191
			} 
Chris PeBenito 134191
			av_defined[tclass] = 1;
Chris PeBenito 134191
Chris PeBenito 134191
			inherits = "";
Chris PeBenito 134191
			permission = 1;
Chris PeBenito 134191
Chris PeBenito 134191
			nextstate = "INHERITS_OR_CLASS-OPENBRACKET";
Chris PeBenito 134191
			next;
Chris PeBenito 134191
		}
Chris PeBenito 134191
$1 == "inherits" {			
Chris PeBenito 134191
			if (nextstate != "INHERITS_OR_CLASS-OPENBRACKET")
Chris PeBenito 134191
			{
Chris PeBenito 134191
				printf("Parse error:  Unexpected INHERITS definition on line %d\n", NR);
Chris PeBenito 134191
				next;	
Chris PeBenito 134191
			}
Chris PeBenito 134191
Chris PeBenito 134191
			if (!($2 in common_defined))
Chris PeBenito 134191
			{
Chris PeBenito 134191
				printf("COMMON %s is not defined (line %d).\n", $2, NR);
Chris PeBenito 134191
				next;
Chris PeBenito 134191
			}
Chris PeBenito 134191
Chris PeBenito 134191
			inherits = $2;
Chris PeBenito 134191
			permission = common_base[$2];
Chris PeBenito 134191
Chris PeBenito 134191
			for (combined in common_perms)
Chris PeBenito 134191
			{
Chris PeBenito 134191
				split(combined,separate, SUBSEP);
Chris PeBenito 134191
				if (separate[1] == inherits)
Chris PeBenito 134191
				{
Chris PeBenito 134191
					inherited_perms[common_perms[combined]] = separate[2];
Chris PeBenito 134191
				}
Chris PeBenito 134191
			}
Chris PeBenito 134191
Chris PeBenito 134191
                        j = 1;
Chris PeBenito 134191
                        for (i in inherited_perms) {
Chris PeBenito 134191
                            ind[j] = i + 0;
Chris PeBenito 134191
                            j++;
Chris PeBenito 134191
                        }
Chris PeBenito 134191
                        n = asort(ind);
Chris PeBenito 134191
			for (i = 1; i <= n; i++) {
Chris PeBenito 134191
				perm = inherited_perms[ind[i]];
Chris PeBenito 134191
				printf("#define %s__%s", toupper(tclass), toupper(perm)) > outfile; 
Chris PeBenito 134191
				spaces = 40 - (length(perm) + length(tclass));
Chris PeBenito 134191
				if (spaces < 1)
Chris PeBenito 134191
				      spaces = 1;
Chris PeBenito 134191
				for (j = 0; j < spaces; j++) 
Chris PeBenito 134191
					printf(" ") > outfile; 
Chris PeBenito 134191
				printf("0x%08xUL\n", ind[i]) > outfile; 
Chris PeBenito 134191
			}
Chris PeBenito 134191
			printf("\n") > outfile;
Chris PeBenito 134191
                        for (i in ind) delete ind[i];
Chris PeBenito 134191
                        for (i in inherited_perms) delete inherited_perms[i];
Chris PeBenito 134191
Chris PeBenito 134191
			printf("   S_(SECCLASS_%s, %s, 0x%08xUL)\n", toupper(tclass), inherits, permission) > inheritfile; 
Chris PeBenito 134191
Chris PeBenito 134191
			nextstate = "CLASS_OR_CLASS-OPENBRACKET";
Chris PeBenito 134191
			next;
Chris PeBenito 134191
		}
Chris PeBenito 134191
$1 == "{"	{ 
Chris PeBenito 134191
			if (nextstate != "INHERITS_OR_CLASS-OPENBRACKET" &&
Chris PeBenito 134191
			    nextstate != "CLASS_OR_CLASS-OPENBRACKET" &&
Chris PeBenito 134191
			    nextstate != "COMMON-OPENBRACKET")
Chris PeBenito 134191
			{
Chris PeBenito 134191
				printf("Parse error:  Unexpected { on line %d\n", NR);
Chris PeBenito 134191
				next;
Chris PeBenito 134191
			}
Chris PeBenito 134191
Chris PeBenito 134191
			if (nextstate == "INHERITS_OR_CLASS-OPENBRACKET")
Chris PeBenito 134191
				nextstate = "CLASS-CLOSEBRACKET";
Chris PeBenito 134191
Chris PeBenito 134191
			if (nextstate == "CLASS_OR_CLASS-OPENBRACKET")
Chris PeBenito 134191
				nextstate = "CLASS-CLOSEBRACKET";
Chris PeBenito 134191
Chris PeBenito 134191
			if (nextstate == "COMMON-OPENBRACKET")
Chris PeBenito 134191
				nextstate = "COMMON-CLOSEBRACKET";
Chris PeBenito 134191
		}
Chris PeBenito 134191
/[a-z][a-z_]*/	{
Chris PeBenito 134191
			if (nextstate != "COMMON-CLOSEBRACKET" &&
Chris PeBenito 134191
			    nextstate != "CLASS-CLOSEBRACKET")
Chris PeBenito 134191
			{
Chris PeBenito 134191
				printf("Parse error:  Unexpected symbol %s on line %d\n", $1, NR);		
Chris PeBenito 134191
				next;
Chris PeBenito 134191
			}
Chris PeBenito 134191
Chris PeBenito 134191
			if (nextstate == "COMMON-CLOSEBRACKET")
Chris PeBenito 134191
			{
Chris PeBenito 134191
				if ((common_name,$1) in common_perms)
Chris PeBenito 134191
				{
Chris PeBenito 134191
					printf("Duplicate permission %s for common %s on line %d.\n", $1, common_name, NR);
Chris PeBenito 134191
					next;
Chris PeBenito 134191
				}
Chris PeBenito 134191
Chris PeBenito 134191
				common_perms[common_name,$1] = permission;
Chris PeBenito 134191
Chris PeBenito 134191
				printf("#define COMMON_%s__%s", toupper(common_name), toupper($1)) > outfile; 
Chris PeBenito 134191
Chris PeBenito 134191
				printf("    S_(\"%s\")\n", $1) > cpermfile;
Chris PeBenito 134191
			}
Chris PeBenito 134191
			else
Chris PeBenito 134191
			{
Chris PeBenito 134191
				if ((tclass,$1) in av_perms)
Chris PeBenito 134191
				{
Chris PeBenito 134191
					printf("Duplicate permission %s for %s on line %d.\n", $1, tclass, NR);
Chris PeBenito 134191
					next;
Chris PeBenito 134191
				}
Chris PeBenito 134191
Chris PeBenito 134191
				av_perms[tclass,$1] = permission;
Chris PeBenito 134191
		
Chris PeBenito 134191
				if (inherits != "")
Chris PeBenito 134191
				{
Chris PeBenito 134191
					if ((inherits,$1) in common_perms)
Chris PeBenito 134191
					{
Chris PeBenito 134191
						printf("Permission %s in %s on line %d conflicts with common permission.\n", $1, tclass, inherits, NR);
Chris PeBenito 134191
						next;
Chris PeBenito 134191
					}
Chris PeBenito 134191
				}
Chris PeBenito 134191
Chris PeBenito 134191
				printf("#define %s__%s", toupper(tclass), toupper($1)) > outfile; 
Chris PeBenito 134191
Chris PeBenito 134191
				printf("   S_(SECCLASS_%s, %s__%s, \"%s\")\n", toupper(tclass), toupper(tclass), toupper($1), $1) > avpermfile; 
Chris PeBenito 134191
			}
Chris PeBenito 134191
Chris PeBenito 134191
			spaces = 40 - (length($1) + length(tclass));
Chris PeBenito 134191
			if (spaces < 1)
Chris PeBenito 134191
			      spaces = 1;
Chris PeBenito 134191
Chris PeBenito 134191
			for (i = 0; i < spaces; i++) 
Chris PeBenito 134191
				printf(" ") > outfile; 
Chris PeBenito 134191
			printf("0x%08xUL\n", permission) > outfile; 
Chris PeBenito 134191
			permission = permission * 2;
Chris PeBenito 134191
		}
Chris PeBenito 134191
$1 == "}"	{
Chris PeBenito 134191
			if (nextstate != "CLASS-CLOSEBRACKET" && 
Chris PeBenito 134191
			    nextstate != "COMMON-CLOSEBRACKET")
Chris PeBenito 134191
			{
Chris PeBenito 134191
				printf("Parse error:  Unexpected } on line %d\n", NR);
Chris PeBenito 134191
				next;
Chris PeBenito 134191
			}
Chris PeBenito 134191
Chris PeBenito 134191
			if (nextstate == "COMMON-CLOSEBRACKET")
Chris PeBenito 134191
			{
Chris PeBenito 134191
				common_base[common_name] = permission;
Chris PeBenito 134191
				printf("TE_(common_%s_perm_to_string)\n\n", common_name) > cpermfile; 
Chris PeBenito 134191
			}
Chris PeBenito 134191
Chris PeBenito 134191
			printf("\n") > outfile;
Chris PeBenito 134191
Chris PeBenito 134191
			nextstate = "COMMON_OR_AV";
Chris PeBenito 134191
		}
Chris PeBenito 134191
END	{
Chris PeBenito 134191
		if (nextstate != "COMMON_OR_AV" && nextstate != "CLASS_OR_CLASS-OPENBRACKET")
Chris PeBenito 134191
			printf("Parse error:  Unexpected end of file\n");
Chris PeBenito 134191
Chris PeBenito 134191
	}'
Chris PeBenito 134191
Chris PeBenito 134191
# FLASK