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