#!/usr/bin/php
<?php
/*
	s_srv.php - server for stream manager
	Copyright 2004 by Stephen D. Teter
	All rights reserved.
*/

error_reporting (E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit (0);

/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush ();

/* The following definitions must not contain $explode_char!! */
$maintag = "FifDasuk0";
$stopcmd = "GecyecWen6";

/* Edit these to suit */
$address = '127.0.0.1';
$port = 29406;
$explode_char = chr(1);
$space_char = chr(2);
$mainkey = "vegTudPewgCxcMajFis:ek^bDigh#UkF:ved8v9";
$ssh_loc="/usr/bin";
$id_rsa_loc="/var/www/iee/scanctl/include";
$log_file="/dev/null";

/* EDIT THIS: Separate commands with $explode_char Ex: <cmd><explode_char><cmd>... */
$reqlist = "send_ssh".chr(1)."do_upload";

/* Do not change anything below this line unless you know what you are doing */

function dc( $buf ) {
    global $mainkey;
    $td = mcrypt_module_open ('des', '', 'ecb', '');
    $key = substr ($mainkey, 0, mcrypt_enc_get_key_size ($td));
    $iv_size = mcrypt_enc_get_iv_size ($td);
    $iv = mcrypt_create_iv ($iv_size, MCRYPT_RAND);
    if (mcrypt_generic_init ($td, $key, $iv) != -1) {
        $plain = mdecrypt_generic ($td, $buf);
        mcrypt_generic_deinit ($td);
    }
    mcrypt_module_close ($td);
    return( $plain );
}

function validreq( $req ) {
    global $reqlist, $explode_char;
    $res = 0;
    $rl = explode($explode_char, $reqlist);
    $rc = count($rl);
    for($i=0; $i<$rc; $i++) {
// error_log("s_srv: req=[$req], rl[$i]=[$rl[$i]]\n");
        if(strcmp($req, $rl[ $i ]) == 0) {
            $res = 1;
            break;
        }
    }
    if(!$res) {
        $e_msg = "s_srv: request [$req] did not validate\n";
        error_log( $e_msg, 3, $log_file );
    }
    return( $res );
}

function send_ssh($args) {
    global $ssh_loc, $space_char, $id_rsa_loc;
    list($a, $cmd) = explode($space_char, $args);
    $str = $ssh_loc."/ssh -i $id_rsa_loc/id_rsa -n ".$a." ".$cmd;
    return $str;
}

function do_upload($args) {
	global $space_char;
	list($newdir, $userfile, $userfile_name) = explode($space_char, $args);
	if(!is_dir($newdir)) {
		$str = "mkdir -p $newdir";
		exec($str);
	}
	$str = "chmod g+w $newdir";
	exec($str);
	$upload_to = $newdir."/".$userfile_name;
	if(is_uploaded_file($userfile)) {
		move_uploaded_file($userfile, $upload_to);
		$str = "echo \"OK\"";
	} else {
		$str = "echo \"FAILED\"";
	}
	return( $str );
}


if (($sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
    $e_msg = "s_srv: socket_create() failed: reason: ".socket_strerror($sock)."\n";
    error_log( $e_msg, 3, $log_file );
    exit;
}

if (($ret = socket_bind ($sock, $address, $port)) < 0) {
    $e_msg = "s_srv: socket_bind() failed: reason: ".socket_strerror($ret)."\n";
    error_log( $e_msg, 3, $log_file );
    exit;
}

if (($ret = socket_listen ($sock, 5)) < 0) {
    $e_msg = "s_srv: socket_listen() failed: reason: ".socket_strerror($ret)."\n";
    error_log( $e_msg, 3, $log_file );
    exit;
}

do {
    if (($msgsock = socket_accept($sock)) < 0) {
        $e_msg = "s_srv: socket_accept() failed: reason: ".socket_strerror($msgsock)."\n";
        error_log( $e_msg, 3, $log_file );
    } else {
        if (FALSE === ($buf = socket_read ($msgsock, 100000, PHP_BINARY_READ))) {
            $e_msg = "s_srv: socket_read() failed: reason: ".socket_strerror(socket_last_error($msgsock))."\n";
            error_log( $e_msg, 3, $log_file );
        } else {
            $buf = dc($buf);
            list($tag,$req,$args) = explode($explode_char, $buf);
            $args = trim($args);
 // error_log("s_srv: tag:[$tag] req:[$req] args=[$args]\n", 3, $log_file);
            if(strcmp($tag,$maintag) == 0) {
 // error_log("s_srv: strcmp ok\n", 3, $log_file);
                if(validreq($req)) {
 // error_log("s_srv: validreq ok\n", 3, $log_file);
                    if(strcmp($req, "send_ssh") == 0) {
 // error_log("s_srv: strcmp 2 ok\n", 3, $log_file);
                        $do_me = send_ssh($args);
 // error_log("s_srv: do_me = [$do_me]\n", 3, $log_file);
                    }
//                    unset($resp_array);
//                    exec($do_me, $resp_array);
//                    if(count($resp_array) > 0 ) {
//                        $r = implode("\n", $resp_array);
//                        $r2 = "s_srv: ".implode("\n", $resp_array);
// error_log("s_srv: r = [$r2]\n", 3, $log_file);
//                    }
					$r=shell_exec($do_me);
					trim($r);
// error_log("s_srv: r = [$r]\n", 3, $log_file);
                    socket_write($msgsock, $r, strlen($r));
                }
            } else {
                $e_msg = "s_srv: tag [$tag] did not validate\n";
                // error_log( $e_msg, 3, $log_file );
            }
        }
        socket_close ($msgsock);
    }
} while (true);

socket_close ($sock);
?>
