X-Git-Url: https://git.defcon.no/?a=blobdiff_plain;f=api%2Fauth.php;h=451581321a03b1f6e233d73713f73ad8a34388b5;hb=87cde98c94c33708506b8e94db43726393df5dfa;hp=1513aae2d7585dd948c387f439e1a240abca605a;hpb=6496a650839b71ea7bfaab1b3b461886de4475a8;p=hermes diff --git a/api/auth.php b/api/auth.php index 1513aae..4515813 100644 --- a/api/auth.php +++ b/api/auth.php @@ -1,4 +1,30 @@ 'pong' // along with the new auth_key. - $session_name = $_GET['session']; + $session_name = $_POST['session']; $authid = $_SESSION['authid']; $auth_key = update_authkey( $session_name, $authid ); print json_encode( array( 'response' => 'pong', 'auth_key' => $auth_key )); @@ -112,11 +138,11 @@ else if ( ! can_write() ) simple_authfail(); - if ( array_key_exists('host_ip', $_GET ) - && array_key_exists('access', $_GET )) + if ( array_key_exists('host_ip', $_POST ) + && array_key_exists('access', $_POST )) { - $host = $_GET['host_ip']; - $access = $_GET['access']; + $host = $_POST['host_ip']; + $access = $_POST['access']; if (! preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $host) || ! authlevel_value( $access ) ) { @@ -130,7 +156,7 @@ else print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database error.')); break; } - print json_encode( array( 'response' => 'ok', 'key' => $key, 'host' => $host, 'access' => authlevel_name( $level ) ) ); + print json_encode( array( 'response' => 'ok', 'key' => $key, 'host' => $host, 'access' => authlevel_name( get_authorization( "key", $key ) ) ) ); break; } else print json_encode ( array( 'response' => 'invalid') ); @@ -141,9 +167,9 @@ else if ( ! can_write() ) simple_authfail(); - if ( array_key_exists('api_key', $_GET ) ) + if ( array_key_exists('api_key', $_POST ) ) { - $key = sql_clean( $_GET['api_key'] ); + $key = sql_clean( $_POST['api_key'] ); // Perform a key-verification, skipping host/remote-address check. if ( ! verify_apikey( $key, true ) ) { @@ -175,16 +201,89 @@ else // needed parameters should be username and access level // If the authorization does not exist, add it. // If the user is already authorized, replace access level. + if ( ! can_write() ) + simple_authfail(); + + if ( array_key_exists('username', $_POST ) + && array_key_exists('access', $_POST )) + { + $user = $_POST['username']; + $access = $_POST['access']; + $level = authlevel_value( $access ); + + if ( ! $level ) + { + print json_encode ( array( 'response' => 'invalid', 'cause' => 'parameters' ) ); + break; + } + if ( ! authuser_getinfo( $user ) ) + { + print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant')); + break; + } + + if ( ! update_authorization( "user", $user, $level ) ) + { + print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database error.')); + break; + } + + print json_encode( array( 'response' => 'ok', 'user' => $user, 'access' => authlevel_name( get_authorization( "user", $user ) ) ) ); + break; + } + else print json_encode ( array( 'response' => 'invalid') ); + break; + case "/remove_user": // If the current authentication has write access: // Remove authorization for the given users. // Delete user from backend if backend is read-write. + if ( ! can_write() ) + simple_authfail(); + + if ( array_key_exists('username', $_POST )) + { + $user = $_POST['username']; + + $t_level = get_authorization( "user", $user ); + + if ( $t_level && ! remove_authorization( $user ) ) + { + print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database error.')); + break; + } + if ( ! authmethod_readonly() ) + { + if ( !authuser_getinfo( $user ) ) + { + print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant')); + break; + } + if ( !authuser_delete( $user ) ) + { + print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database error.')); + break; + } + } + + print json_encode( array( 'response' => 'ok', 'user' => $user ) ); + break; + } + else print json_encode ( array( 'response' => 'invalid') ); + break; + case "/list_users": // List valid API user-acounts. // Fail with notauthorized if current authentication // does not have write access. // Should not return users from backend, // but should only return users with authorization. + if ( ! can_write() ) + simple_authfail(); + $list = list_authusers(); + print json_encode( array( 'response' => 'ok', 'list' => $list ) ); + break; + case "/add_user": // Add user to backend if backend is read-write and // the current authentication has write access.