Files
MUA-Mikes-Unraid-Agent/scripts/verify-live-shell.php

16 lines
3.5 KiB
PHP

<?php
// Read-only acceptance; credentials stay on Unraid. No file copies or private logs.
function need($ok,$label){if(!$ok)throw new RuntimeException($label);}
$config=[];foreach(file('/boot/config/plugins/mua/mua.conf') as $line){$parts=explode('=',trim($line),2);if(count($parts)===2)$config[trim($parts[0])]=trim($parts[1]);}
function request($method,$params,$authenticated=true){global $config;$headers=['Content-Type: application/json','Accept: application/json, text/event-stream'];if($authenticated)$headers[]='Authorization: Bearer '.$config['MUA_API_KEY'];$c=curl_init('http://127.0.0.1:3002/mcp');curl_setopt_array($c,[CURLOPT_RETURNTRANSFER=>true,CURLOPT_POST=>true,CURLOPT_HTTPHEADER=>$headers,CURLOPT_POSTFIELDS=>json_encode(['jsonrpc'=>'2.0','id'=>1,'method'=>$method,'params'=>$params]),CURLOPT_TIMEOUT=>30]);$body=curl_exec($c);$code=curl_getinfo($c,CURLINFO_HTTP_CODE);$errno=curl_errno($c);curl_close($c);need($errno===0,'Local test transport failed');return [$code,json_decode($body,true)];}
function rpc($method,$params){[$code,$r]=request($method,$params);need($code===200&&isset($r['result'])&&empty($r['result']['isError']),'Authenticated MCP request failed');return $r['result'];}
[$unauth]=request('initialize',(object)[],false);need($unauth===401,'Unauthenticated request must be rejected');
$i=rpc('initialize',['protocolVersion'=>'2025-03-26','capabilities'=>(object)[],'clientInfo'=>['name'=>'read-only-shell-acceptance','version'=>'1']]);need($i['serverInfo']['version']==='2026.10.12.r031','Expected loaded r031');
$list=rpc('tools/list',(object)[]);$names=array_column($list['tools'],'name');foreach(['unraid_system_shell','unraid_system_shell_readonly','unraid_system_connection_test'] as $name)need(in_array($name,$names,true),'Required tool disabled; do not change permission');
function tool($name,$args){$r=rpc('tools/call',['name'=>$name,'arguments'=>$args]);return json_decode($r['content'][0]['text'],true);}
$host=trim(shell_exec('hostname'));$readonly=tool('unraid_system_shell_readonly',['program'=>'hostname']);need($readonly['exit_code']===0&&$readonly['stdout']===$host,'Read-only host differs');
$shell=tool('unraid_system_shell',['command'=>'hostname; if test -d /mnt/user/Musik; then printf "Musik_exists\n"; else exit 3; fi','timeout_seconds'=>15]);need($shell['exit_code']===0&&$shell['stdout']===$host."\nMusik_exists",'Host command or Musik existence failed');need($shell['execution']['host']===$host&&$shell['execution']['unraid_host_confirmed']&&$shell['execution']['cwd']==='/','Wrong execution context');
$connection=tool('unraid_system_connection_test',(object)[]);need($connection['ok']===true&&$connection['command_execution']['exit_code']===0&&$connection['execution']['host']===$host,'Connection/host command check failed');need($connection['phases']['authentication']['ssh_login']==='not_applicable','Unexpected SSH transport');
$p=(int)trim(file_get_contents('/var/run/mua.pid'));need(readlink('/proc/'.$p.'/cwd')==='/','Daemon cwd not stable');
echo json_encode(['version'=>$i['serverInfo']['version'],'host'=>$host,'executionLocation'=>'Unraid host plugin, no SSH','daemonCwd'=>'/','transport'=>'HTTP request succeeds','authentication'=>'unauthenticated 401; authenticated initialize/list/tool calls succeed','shell'=>'hostname and existing /mnt/user/Musik verified','readonly'=>'same host confirmed','connectionTest'=>'all phases verified','mutationsExecuted'=>0,'fileRelatedToolNames'=>array_values(array_filter($names,fn($n)=>preg_match('/file|upload|download|transfer|copy/',$n)))],JSON_PRETTY_PRINT).PHP_EOL;