#!/usr/local/bin/php -f
<?php

/*
 * pf_auth username=.... : return a "python-formatted" user and its privileges:
 *    ({'username':...,'displayname':....}, ('priv1','priv2',...))
 * with password=.... : only return the user if authenticate on pfsense
 *
 */

array_shift($argv);
foreach ($argv as $arg) {
	list($name, $value) = explode('=', $arg, 2);
	switch($name) {
		case 'username':
			$username=$value;
			break;
		case 'password':
			$password=$value;
			break;
		default:
			echo "BAD SYNTAX\n";
			exit(2);
	}
}

require("auth.inc");

if (isset($password)) {
	if (!authenticate_user($username, $password)) {
		echo "AUTH FAIL\n";
		exit(1);
	}
}

$user = getUserEntry($username);
if (!$user) {
	echo "UNKNOWN USER\n";
	exit(3);
}
$k = array(
	"name" => "username",
	"descr" => "displayname",
);
echo "({";
foreach($k as $key => $attr) {
	echo '"' . $attr . '":"""' . $user[$key] . '""", ';
}
echo "},";
$privs = get_user_privileges($user);
if ($privs) {
	echo "(";
	foreach($privs as $val) {
		echo "'$val', ";
	}
	echo ")";
} else {
	echo "None";
}
echo ")";
exit(0);

?>
