aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
m---------extern/spongycastle0
1 files changed, 0 insertions, 0 deletions
diff --git a/extern/spongycastle b/extern/spongycastle
-Subproject 2c744ebade816d2e4f65f3734db373e4c19c2e4
+Subproject b767c5afff37e7fbd26e79387d11cd5bded6eff
ref='#n60'>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 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
# *
# * labelfuncs.sh
# *
# * Copyright (C) 2005 IBM Corporation
# *
# * Authors:
# * Stefan Berger <stefanb@us.ibm.com>
# *
# * This program is free software; you can redistribute it and/or
# * modify it under the terms of the GNU General Public License as
# * published by the Free Software Foundation, version 2 of the
# * License.
# *
# *
# * A collection of functions to handle polcies, mapfiles,
# * and ssidrefs.
#


# Find the mapfile given a policy nmame
# Parameters:
# 1st : the name of the policy whose map file is to be found, i.e.,
#       chwall
# Results:
# The variable mapfile will hold the realtive path to the mapfile
# for the given policy.
# In case the mapfile could be found, the functions returns a '1',
# a '0' otherwise.
findMapFile ()
{
	mapfile="./$1.map"
	if [ -r "$mapfile" ]; then
		return 1
	fi

	mapfile="./policies/$1/$1.map"
	if [ -r "$mapfile" ]; then
		return 1
	fi

	return 0
}


# Determine the name of the primary policy
# Parameters
# 1st : the path to the mapfile; the path may be relative
#       to the current directory
# Results
# The variable primary will hold the name of the primary policy
getPrimaryPolicy ()
{
	mapfile=$1
	primary=`cat $mapfile  |   \
	         awk '             \
	          {                \
	            if ( $1 == "PRIMARY" ) { \
	              res=$2;                \
	            }                        \
	          } END {                    \
	            print res;               \
	          } '`
}


# Determine the name of the secondary policy
# Parameters
# 1st : the path to the mapfile; the path may be relative
#       to the current directory
# Results
# The variable secondary will hold the name of the secondary policy
getSecondaryPolicy ()
{
	mapfile=$1
	secondary=`cat $mapfile  |   \
	         awk '             \
	          {                \
	            if ( $1 == "SECONDARY" ) { \
	              res=$2;                \
	            }                        \
	          } END {                    \
	            print res;               \
	          } '`
}


#Return where the grub.conf file is.
#I only know of one place it can be.
findGrubConf()
{
	grubconf="/boot/grub/grub.conf"
	if [ -w $grubconf ]; then
		return 1
	fi
	if [ -r $grubconf ]; then
		return 2
	fi
	return 0
}


# This function sets the global variable 'linux'
# to the name and version of the Linux kernel that was compiled
# for domain 0.
# If this variable could not be found, the variable 'linux'
# will hold a pattern
# Parameters:
# 1st: the path to reach the root directory of the XEN build tree
#      where linux-*-xen0 is located at
# Results:
# The variable linux holds then name and version of the compiled
# kernel, i.e., 'vmlinuz-2.6.12-xen0'
getLinuxVersion ()
{
	path=$1
	linux=""
	for f in $path/linux-*-xen0 ; do
		versionfile=$f/include/linux/version.h
		if [ -r $versionfile ]; then
			lnx=`cat $versionfile | \
			     grep UTS_RELEASE | \
			     awk '{             \
			       len=length($3);  \
			       print substr($3,2,len-2) }'`
		fi
		if [ "$lnx" != "" ]; then
			linux="[./0-9a-zA-z]*$lnx"
			return;
		fi
	done

	#Last resort.
	linux="vmlinuz-2.[45678].[0-9]*[.0-9]*-xen0$"
}


# Find out with which policy the hypervisor was booted with.
# Parameters
# 1st : The complete path to grub.conf, i.e., /boot/grub/grub.conf
#
findPolicyInGrub ()
{
	grubconf=$1
	linux=`uname -r`
	policy=`cat $grubconf |                        \
	         awk -vlinux=$linux '{                 \
	           if ( $1 == "title" ) {              \
	             kernelfound = 0;                  \
	             policymaycome = 0;                \
	           }                                   \
	           else if ( $1 == "kernel" ) {        \
	             if ( match($2,"xen.gz$") ) {      \
	               pathlen=RSTART;                 \
	               kernelfound = 1;                \
	             }                                 \
	           }                                   \
	           else if ( $1 == "module" &&         \
	                     kernelfound == 1 &&       \
	                     match($2,linux) ) {       \
	              policymaycome = 1;               \
	           }                                   \
	           else if ( $1 == "module" &&         \
	                     kernelfound == 1 &&       \
	                     policymaycome == 1 &&     \
	                     match($2,"[0-9a-zA-Z_]*.bin$") ) { \
	              policymaycome = 0;               \
	              kernelfound = 0;                 \
	              polname = substr($2,pathlen);    \
	              len=length(polname);             \
	              polname = substr(polname,0,len-4); \
	           }                                   \
	         } END {                               \
	           print polname                       \
	         }'`
}


# Get the SSID of a domain
# Parameters:
# 1st : domain ID, i.e. '1'
# Results
# If the ssid could be found, the variable 'ssid' will hold
# the currently used ssid in the hex format, i.e., '0x00010001'.
# The funtion returns '1' on success, '0' on failure
getSSIDUsingSecpolTool ()
{
	domid=$1
	export PATH=$PATH:.
	ssid=`secpol_tool getssid -d $domid -f | \
	        grep -E "SSID:" |          \
	        awk '{ print $4 }'`

	if [ "$ssid" != "" ]; then
		return 1
	fi
	return 0
}


# Break the ssid identifier into its high and low values,
# which are equal to the secondary and primary policy references.
# Parameters:
# 1st: ssid to break into high and low value, i.e., '0x00010002'
# Results:
# The variable ssidlo_int and ssidhi_int will hold the low and
# high ssid values as integers.
getSSIDLOHI ()
{
	ssid=$1
	ssidlo_int=`echo $ssid | awk          \
	            '{                        \
	               len=length($0);        \
	               beg=substr($0,1,2);    \
	               if ( beg == "0x" ) {   \
	                   dig = len - 2;     \
	                   if (dig <= 0) {    \
	                     exit;            \
	                   }                  \
	                   if (dig > 4) {     \
	                     dig=4;           \
	                   }                  \
	                   lo=sprintf("0x%s",substr($0,len-dig+1,dig)); \
	                   print strtonum(lo);\
	               } else {               \
	                   lo=strtonum($0);   \
	                   if (lo < 65536) {  \
	                     print lo;        \
	                   } else {           \
	                     hi=lo;           \
	                     hi2= (hi / 65536);\
	                     hi2_str=sprintf("%d",hi2); \
	                     hi2=strtonum(hi2_str);\
	                     lo=hi-(hi2*65536); \
	                     printf("%d",lo); \
	                   }                  \
			}                     \
	            }'`
	ssidhi_int=`echo $ssid | awk          \
	            '{                        \
	               len=length($0);        \
	               beg=substr($0,1,2);    \
	               if ( beg == "0x" ) {   \
	                   dig = len - 2;     \
	                   if (dig <= 0 ||    \
	                     dig >  8) {      \
	                     exit;            \
	                   }                  \
	                   if (dig < 4) {     \
	                     print 0;         \
	                     exit;            \
	                   }                  \
	                   dig -= 4;          \
	                   hi=sprintf("0x%s",substr($0,len-4-dig+1,dig)); \
	                   print strtonum(hi);\
	               } else {               \
	                   hi=strtonum($0);   \
	                   if (hi >= 65536) { \
	                     hi = hi / 65536; \
	                     printf ("%d",hi);\
	                   } else {           \
	                     printf ("0");    \
	                   }                  \
	               }                      \
	            }'`
	if [ "$ssidhi_int" == "" -o \
	     "$ssidlo_int" == "" ]; then
		return 0;
	fi
	return 1
}


#Update the grub configuration file.
#Search for existing entries and replace the current
#policy entry with the policy passed to this script
#
#Arguments passed to this function
# 1st : the grub configuration file with full path
# 2nd : the binary policy file name, i.e. chwall.bin
# 3rd : the name or pattern of the linux kernel name to match
#       (this determines where the module entry will be made)
#
# The algorithm here is based on pattern matching
# and is working correctly if
# - under a title a line beginning with 'kernel' is found
#   whose following item ends with "xen.gz"
#   Example:  kernel /xen.gz dom0_mem=....
# - a module line matching the 3rd parameter is found
#
updateGrub ()
{
	grubconf=$1
	policyfile=$2
	linux=$3

	tmpfile="/tmp/new_grub.conf"

	cat $grubconf |                                \
	         awk -vpolicy=$policyfile              \
	             -vlinux=$linux '{                 \
	           if ( $1 == "title" ) {              \
	             kernelfound = 0;                  \
	             if ( policymaycome == 1 ){        \
	               printf ("\tmodule %s%s\n", path, policy);      \
	             }                                 \
	             policymaycome = 0;                \
	           }                                   \
	           else if ( $1 == "kernel" ) {        \
	             if ( match($2,"xen.gz$") ) {      \
	               path=substr($2,1,RSTART-1);     \
	               kernelfound = 1;                \
	             }                                 \
	           }                                   \
	           else if ( $1 == "module" &&         \
	                     kernelfound == 1 &&       \
	                     match($2,linux) ) {       \
	              policymaycome = 1;               \
	           }                                   \
	           else if ( $1 == "module" &&         \
	                     kernelfound == 1 &&       \
	                     policymaycome == 1 &&     \
	                     match($2,"[0-9a-zA-Z]*.bin$") ) { \
	              printf ("\tmodule %s%s\n", path, policy); \
	              policymaycome = 0;               \
	              kernelfound = 0;                 \
	              dontprint = 1;                   \
	           }                                   \
	           else if ( $1 == "" &&               \
	                     kernelfound == 1 &&       \
	                     policymaycome == 1) {     \
	              dontprint = 1;                   \
	           }                                   \
	           if (dontprint == 0) {               \
	             printf ("%s\n", $0);              \
	           }                                   \
	           dontprint = 0;                      \
	         } END {                               \
	           if ( policymaycome == 1 ) {         \
	             printf ("\tmodule %s%s\n", path, policy);  \
	           }                                   \
	         }' > $tmpfile
	if [ ! -r $tmpfile ]; then
		echo "Could not create temporary file! Aborting."
		exit -1
	fi
	mv -f $tmpfile $grubconf
}


# Display all the labels in a given mapfile
# Parameters
# 1st: Full or relative path to the policy's mapfile
showLabels ()
{
	mapfile=$1
	if [ ! -r "$mapfile" -o "$mapfile" == "" ]; then
		echo "Cannot read from vm configuration file $vmfile."
		return -1
	fi

	getPrimaryPolicy $mapfile
	getSecondaryPolicy $mapfile

	echo "The following labels are available:"
	let line=1
	while [ 1 ]; do
		ITEM=`cat $mapfile |         \
		      awk -vline=$line       \
		          -vprimary=$primary \
		      '{                     \
		         if ($1 == "LABEL->SSID" &&  \
		             $2 == "VM" &&           \
		             $3 == primary ) {       \
		           ctr++;                    \
		           if (ctr == line) {        \
		             print $4;               \
		           }                         \
		         }                           \
		       } END {                       \
		       }'`

		if [ "$ITEM" == "" ]; then
			break
		fi
		if [ "$secondary" != "NULL" ]; then
			LABEL=`cat $mapfile |     \
			       awk -vitem=$ITEM   \
			       '{
			          if ($1 == "LABEL->SSID" && \
			              $2 == "VM" &&          \
			              $3 == "CHWALL" &&      \
			              $4 == item ) {         \
			            result = item;           \
			          }                          \
			        } END {                      \
			            print result             \
			        }'`
		else
			LABEL=$ITEM
		fi

		if [ "$LABEL" != "" ]; then
			echo "$LABEL"
			found=1
		fi
		let line=line+1
	done
	if [ "$found" != "1" ]; then
		echo "No labels found."
	fi
}


# Get the default SSID given a mapfile and the policy name
# Parameters
# 1st: Full or relative path to the policy's mapfile
# 2nd: the name of the policy
getDefaultSsid ()
{
	mapfile=$1
	pol=$2
	RES=`cat $mapfile    \
	     awk -vpol=$pol  \
	      {              \
	        if ($1 == "LABEL->SSID" && \
	            $2 == "ANY"         && \
	            $3 == pol           && \
	            $4 == "DEFAULT"       ) {\
	              res=$5;                \
	        }                            \
	      } END {                        \
	        printf "%04x", strtonum(res) \
	     }'`
	echo "default NULL mapping is $RES"
	defaultssid=$RES
}


#Relabel a VM configuration file
# Parameters
# 1st: Full or relative path to the VM configuration file
# 2nd: The label to translate into an ssidref
# 3rd: Full or relative path to the policy's map file
# 4th: The mode this function is supposed to operate in:
#      'relabel' : Relabels the file without querying the user
#      other     : Prompts the user whether to proceed
relabel ()
{
	vmfile=$1
	label=$2
	mapfile=$3
	mode=$4

	if [ ! -r "$vmfile" ]; then
		echo "Cannot read from vm configuration file $vmfile."
		return -1
	fi

	if [ ! -w "$vmfile" ]; then
		echo "Cannot write to vm configuration file $vmfile."
		return -1
	fi

	if [ ! -r "$mapfile" ] ; then
		echo "Cannot read mapping file $mapfile."
		return -1
	fi

	# Determine which policy is primary, which sec.
	getPrimaryPolicy $mapfile
	getSecondaryPolicy $mapfile

	# Calculate the primary policy's SSIDREF
	if [ "$primary" == "NULL" ]; then
		SSIDLO="0001"
	else
		SSIDLO=`cat $mapfile |                    \
		        awk -vlabel=$label                \
		            -vprimary=$primary            \
		           '{                             \
		              if ( $1 == "LABEL->SSID" && \
		                   $2 == "VM" &&          \
		                   $3 == primary  &&      \
		                   $4 == label ) {        \
		                result=$5                 \
		              }                           \
		           } END {                        \
		             if (result != "" )           \
		               {printf "%04x", strtonum(result)}\
		           }'`
	fi