require "lib.php";
/*
 * Make sure necessary parameters are present
 */
if (!isset($_POST["u"]) || !isset($_POST["p"]) || !isset($_POST["ciphers"]) || !isset($_POST["cpuinfo"])) {
    tjohn_error(TJOHN_ERROR_MISC, "Invalid parameters");
}
/*
 * Authenticate the supplied username/password
 *
 */
$user = tjohn_auth_user($_POST["u"], $_POST["p"]);
/*
 * The user is authenticated. 
 * Create an authcookie and register the node
 *
 */
$authcookie = md5(md5($_POST["u"]) . date("YmdHiS") . md5($_POST["p"]) . (string) mt_rand());
$q = "INSERT INTO nodes SET ";
$q .= "user_id={$user->id}, ";
$q .= "ciphers='" . $m->escape_string($_POST["ciphers"]) . "', ";
$q .= "cpuinfo='" . $m->escape_string($_POST["cpuinfo"]) . "', ";
$q .= "authcookie='" . $m->escape_string($authcookie) . "'";
if (@$m->query($q) === FALSE) {
    tjohn_error(TJOHN_ERROR_DB, $m->error);
}
// Keep track of the node's software version
tjohn_node_set_useragent($m->insert_id);
log_event("Node {$m->insert_id} registered by user {$user->id} ({$user->username})");
setcookie("node", $authcookie);
echo TJOHN_SUCCESS . " {$authcookie}";
<?php

/*
 * Login a node with an auth cookie (the "a" parameter)
 * Sends a Set-Cookie header if successful
 *
 */
require "../config.php";
require "../init.php";
require "lib.php";
if (!isset($_POST["a"])) {
    tjohn_error(TJOHN_ERROR_MISC, "Invalid parameters");
}
$node = tjohn_auth_node($_POST["a"]);
// Keep track of the node's software version
tjohn_node_set_useragent($node->id);
setcookie("node", $node->authcookie);
echo TJOHN_SUCCESS . " Logged in as node {$node->id}";
log_event("Node {$node->id} ({$node->nodename}) logged in");