#
# Check the X Server information
#

$auth = 0;
$open = 0;

&pcheck("X Server");

#----------------------------------------------------------
#
# Check the $xdmconfig file to check if Xauthority is on
#
if (open (XDMCONFIG, $xdmconfig)) {
	while (<XDMCONFIG>) {
		if (/^DisplayManager*authorize/) {
			if (/(off|false)/i) { $auth = 1; }
		}
	}
	close (XDMCONFIG);
} else {
        &report("ERROR:  Cannot open the $xdmconfig file.\n");
	&error;
	exit;
}

if ($auth) {
        &report("DANGER:  Xauthority is not turned on.  This means that ths host must rely\n",
	"\thost based X security, which is inherently insecure.  Please enable\n",
	"\tXauthority in the $xdmconfig file.\n");
}


#----------------------------------------------------------
#
# Check if the X Display (assuming there is one, if not it times out)
#  is open or closed. (Close stderr for the xhost command so it doesnt
#  mess up the nice output)
#
$ENV{'DISPLAY'}=":0";
open(SAVEERR, ">&STDERR");
close (STDERR);
$authout = `$xhost`;
open(STDERR, ">&SAVEERR");

if ($authout ne "") {
	if ($authout =~ /disabled/) {
		&report("DANGER:  The display at :0 (localhost) is open to the world. This means\n",
		"\tthat key strokes can be logged from the display.  Please close the\n",
		"\tdisplay with xhost - and use Xauthority.\n\n");
		$failed = 1;
	} elsif (!($authout eq "access control enabled, only authorized clients can connect\n")) {
		&report("WARNING: The display at :0 (localhost) is not open to the world, but is\n",
		"\topen.  Here is the output from the xhost check:\n",
		"-------[Start of Xhost Output]-------\n",
		"$authout",
		"--------[End of Xhost Output]--------\n");
		$warn = 1;
	} else {
		&report("INFO: The X Server at :0 is authorized and restricted.\n");
	}
}
	
#----------------------------------------------------------
#
# Return the results
#
if ($auth || $failed) {
	&failed;
} elsif ($warn) {
	&warn;
} else {
        &passed;
}
	
exit 0;
