3 // Initialize the server database and create a client account for the bank
5 require_once "lib/weblib.php";
7 // Define $dbdir, $bank_name, $index_file, $bankurl, $ssl_domain
8 if (file_exists("settings.php")) require_once "settings.php";
10 die_unless_server_properly_configured();
11 maybe_forward_to_ssl($ssl_domain);
18 if (get_magic_quotes_gpc()) return stripslashes($x);
23 return mq($_REQUEST[$x]);
30 function appenddebug($x) {
36 return htmlspecialchars($x);
39 function trimsig($x) {
41 $x = str_replace("\r", "", $x);
42 $x = str_replace("\n", "", $x);
47 global $dbdir, $bank_name, $bankurl;
48 global $template_file;
50 $init = mqreq('init');
51 $initadmin = mqreq('initadmin');
52 $drawadmin = mqreq('drawadmin');
53 $passphrase = mqreq('passphrase');
54 $verification = mqreq('verification');
55 $bankpass = mqreq('bankpass');
56 $name = mqreq('name');
57 $random = mqreq('random');
60 require_once "lib/fsdb.php";
61 require_once "lib/ssl.php";
62 require_once "lib/server.php";
65 $db = new fsdb($dbdir);
68 if (!$passphrase) $error = "Passphrase must be entered";
69 elseif ($passphrase != $verification) {
70 $error = "Passphrase doesn't match verification. Try again.";
72 $server = new server($db, $ssl, false, $bank_name, $bankurl);
74 $bankid = $server->bankid
;
75 $tokenid = $server->tokenid
;
76 $msg = "(0,bankid,0):0";
77 $res = $server->process($msg);
78 $args = $server->unpack_bankmsg($res, $t->BANKID
);
79 if (is_string($args)) $error = "Error testing server: $args";
81 $bal = $db->get("account/$bankid/balance/main/$tokenid");
82 if (!$bal) $error = "Bank has no token balance";
84 $args = $server->unpack_bankmsg($bal, $t->ATBALANCE
, $t->BALANCE
);
85 if (is_string($args)) $error = "On parsing bank token balance: $args";
93 if (file_exists('client/settings.php')) require_once "client/settings.php";
94 $dbdir = "client/$dbdir";
95 $template_file = "client/$template_file";
96 $error = die_unless_client_properly_configured(true);
98 require_once "lib/client.php";
99 $clientdb = new fsdb("$dbdir");
100 $client = new client($clientdb, $ssl);
102 if ($enabledebug) $client->showprocess
= 'appenddebug';
104 $hash = $client->passphrasehash($passphrase);
105 if ($clientdb->get("privkey/$hash") ||
106 $clientdb->get("account/$bankid/bank/$bankid/req")) {
107 $error = $client->login($passphrase);
108 if ($error ||
$bankid != $client->id
) {
109 $error = "Passphrase not for bank account";
112 $privkey = $db->get('privkey');
113 $pk = $ssl->load_private_key($privkey);
114 if (!$pk) $error = "Can't load bank private key";
116 openssl_pkey_export($pk, $privkey, $passphrase);
117 openssl_free_key($pk);
118 $error = $client->verifybank($bankurl, $bankid);
120 $error = $client->newuser($passphrase, $privkey);
128 $bankpass = $passphrase;
129 require_once "lib/LoomRandom.php";
130 $random = new LoomRandom();
131 $random = $random->random_id();
132 $sig = trimsig($ssl->sign($random, $db->get('privkey')));
134 } elseif ($initadmin) {
135 if (!$passphrase) $error = "Passphrase must be entered";
136 elseif ($passphrase != $verification) {
137 $error = "Passphrase doesn't match verification. Try again.";
139 // This requires you to get here by knowing the bank passphrase
140 $sig = trimsig($sig);
141 $newsig = trimsig($ssl->sign($random, $db->get('privkey')));
142 if ($newsig != $sig) {
143 echo "<p>Hacking attempt foiled!</p>";
144 echo "Lens: " . strlen($sig) . ", " . strlen($newsig) . "<br>\n";
145 echo "<pre>\"$sig\"\n\n\"$newsig\"</pre>";
149 if (!$server) $server = new server($db, $ssl, false, $bank_name, $bankurl);
151 $bankid = $server->bankid();
152 $tokenid = $server->tokenid
;
154 if (file_exists('client/settings.php')) require_once "client/settings.php";
155 $dbdir = "client/$dbdir";
156 $template_file = "client/$template_file";
157 $error = die_unless_client_properly_configured(true);
159 require_once "lib/client.php";
160 $clientdb = new fsdb("$dbdir");
161 $client = new client($clientdb, $ssl);
163 if ($enabledebug) $client->showprocess
= 'appenddebug';
165 $hash = $client->passphrasehash($passphrase);
166 if (!$clientdb->get("privkey/$hash")) {
167 // Create the new account
168 $error = $client->newuser($passphrase);
170 $error = $client->login($passphrase);
174 if ($clientdb->get("account/$id/bank/$bankid/req")) {
175 $error = 'Account already exists for that passphrase. ' .
176 'Use the <a href="client/">client interface</a> to administer.';
178 $error = $client->login($bankpass);
179 if (!$error) $error = $client->addbank($bankurl);
180 if (!$error) $error = $client->setbank($bankid);
181 if (!$error) $error = $client->spend($id, $tokenid, "10000");
182 if ($error) $error = "While spending tokens from bank: $error";
185 $error = $client->login($passphrase);
186 if (!$error) $error = $client->addbank($bankurl, $name);
189 <p
>Your bank is now ready
for business
. You may now login
as administrator in the
190 <a href
="client/">Client
interface</a
>, accept your initial tokens from
191 the bank
, and start inviting customers
.</p
>
201 if (!$error) $error = " ";
205 <p
>Congratulations
! You have succesfully initialized your bank
.</p
>
207 <p
>Use the form below to create an administration account
for your bank
.
208 Use the bank
's account only to spend usage tokens to the administration account.
209 Use the administration account to mint coupons, and to conduct other
212 <p style="color: red;"><?php echo $error; ?></p>
213 <form method="post" action="./bankinit.php" autocomplete="off">
214 <input type="hidden" name="drawadmin" value="true"/>
215 <input type="hidden" name="bankpass" value="<?php echo $bankpass; ?>"/>
216 <input type="hidden" name="random" value="<?php echo hsc($random); ?>"/>
217 <input type="hidden" name="sig" value="<?php echo hsc($sig); ?>"/>
220 <td><b>Admin Passphrase:</b></td>
221 <td><input type="password" name="passphrase" size="50"/></td>
224 <td><b>Verification:</b></td>
225 <td><input type="password" name="verification" size="50"/></td>
229 <td><b>Name (optional):</b></td>
230 <td><input type="text" name="name" size="40" value="<?php echo $name; ?>"/></td>
234 <td><input type="submit" name="initadmin" value="Create Admin Account"/></td>
243 <p style="color: red;"><?php echo $error; ?></p>
244 <form method="post" action="./bankinit.php" autocomplete="off">
247 <td><b>Bank Passphrase:</b></td>
248 <td><input type="password" name="passphrase" size="50"/></td>
251 <td><b>Verification:</b></td>
252 <td><input type="password" name="verification" size="50"/></td>
256 <td><input type="submit" name="init" value="Initialize"/></td>
263 function pagehead() {
267 <title>Trubanc Bank Initialization</title>
270 <body onload="document.forms[0].passphrase.focus()">
271 <p>This page initializes your bank, creates the bank private key,
272 and creates a client account for the bank.</p>
276 function pagetail() {
279 if ($debug) echo "<b>=== Debug log ===</b><br/><pre>$debug</pre>\n";