1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
<?php
declare(strict_types=1);
require_once __DIR__ . '/yamn_config.php';
/**
* Send an already encrypted YAMN envelope through the local Rust Nym sender.
*
* The handoff callback runs after the complete request has been written to
* the sender's stdin, but before waiting for the sender's bounded flush grace
* period. This lets the HTTP layer finish a FastCGI response while the lock
* remains held until the sender exits.
*/
function sendNymEnvelope(
string $payload,
int $copies = 1,
?string $selectedEntryAddress = null,
?callable $onHandoff = null
): array
{
$sender = yamnConfig('YAMN_NYM_SENDER', '/usr/local/bin/yamn-nym-submit');
$recipient = yamnConfig('YAMN_NYM_RECIPIENT');
$storage = yamnConfig('YAMN_NYM_STORAGE', '/var/lib/yamnweb/nym-client');
$lockPath = yamnConfig('YAMN_NYM_LOCK', '/var/lib/yamnweb/nym-submit.lock');
$entryAddress = $selectedEntryAddress ?? yamnConfig('YAMN_ENTRY_ADDRESS');
if (!is_executable($sender)) {
return ['success' => false, 'error' => 'Nym sender is not available'];
}
if ($recipient === '' || strlen($recipient) > 512 || preg_match('/\s/', $recipient)) {
return ['success' => false, 'error' => 'Nym recipient is not configured'];
}
if ($entryAddress === '' || strlen($entryAddress) > 320 || preg_match('/[\r\n\0\s]/', $entryAddress)) {
return ['success' => false, 'error' => 'YAMN entry address is not configured'];
}
if ($copies !== 1) {
return ['success' => false, 'error' => 'Multiple copies are not enabled for Nym'];
}
if ($payload === '' || strlen($payload) > 65536 || str_contains($payload, "\0")) {
return ['success' => false, 'error' => 'Invalid encrypted YAMN envelope'];
}
try {
$input = json_encode([
'entry_address' => $entryAddress,
'payload' => $payload,
], JSON_THROW_ON_ERROR);
} catch (JsonException $exception) {
return ['success' => false, 'error' => 'Unable to encode Nym request'];
}
$lock = @fopen($lockPath, 'c');
if ($lock === false) {
return ['success' => false, 'error' => 'Nym sender lock is not available'];
}
if (!flock($lock, LOCK_EX | LOCK_NB)) {
fclose($lock);
return ['success' => false, 'error' => 'Nym sender is busy, please retry later'];
}
$descriptors = [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']];
$environment = $_ENV;
$environment['YAMN_NYM_RECIPIENT'] = $recipient;
$environment['YAMN_NYM_STORAGE'] = $storage;
$process = proc_open([$sender], $descriptors, $pipes, null, $environment);
if (!is_resource($process)) {
flock($lock, LOCK_UN);
fclose($lock);
return ['success' => false, 'error' => 'Unable to start Nym sender'];
}
$inputLength = strlen($input);
$offset = 0;
while ($offset < $inputLength) {
$written = fwrite($pipes[0], substr($input, $offset));
if ($written === false || $written === 0) {
break;
}
$offset += $written;
}
if ($offset !== $inputLength) {
fclose($pipes[0]);
proc_terminate($process);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
flock($lock, LOCK_UN);
fclose($lock);
return ['success' => false, 'error' => 'Unable to hand envelope to Nym sender'];
}
fclose($pipes[0]);
if ($onHandoff !== null) {
$onHandoff();
}
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$status = proc_close($process);
flock($lock, LOCK_UN);
fclose($lock);
if ($status !== 0) {
return ['success' => false, 'error' => 'Nym submission failed'];
}
$response = json_decode($stdout, true);
if (!is_array($response) || ($response['success'] ?? false) !== true) {
return ['success' => false, 'error' => 'Invalid response from Nym sender'];
}
return ['success' => true];
}
|