summaryrefslogtreecommitdiffstats
path: root/lex.cgi
blob: a87131a89c4d9752b7fe7ac62e99d36330043b35 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/perl

use strict;
use warnings;
use Email::MIME;
use Fcntl qw(:flock);
use File::Spec;
use File::Find;
use File::Path qw(make_path);
use OpenBSD::Pledge;
use OpenBSD::Unveil;
use Time::HiRes qw(gettimeofday time);

my $start_time = time();

# Load Crypt::OpenPGP and all Crypt:: sub-modules before unveil()
BEGIN {
	require Crypt::OpenPGP;

	for my $inc_dir (@INC) {
		next unless -d $inc_dir;
		my $crypt_dir = File::Spec->catdir($inc_dir, 'Crypt');
		next unless -d $crypt_dir;

		find(sub {
			return unless /\.pm$/;
			my $rel = File::Spec->abs2rel($File::Find::name, $inc_dir);
			$rel =~ s/\.pm$//;
			my $module = join('::', File::Spec->splitdir($rel));
			eval "require $module;";
		}, $crypt_dir);
	}

	# Trigger backend instantiation (Math::BigInt, AutoLoader, Ciphers)
	eval {
		my $pgp = Crypt::OpenPGP->new();
	};
}

my $log_dir = "/var/log";
my $base_dir = "/var/lex";
my $spool_dir = "/var/lex/spool";
my $keyring_file = "$base_dir/pubring.gpg";

unveil("/dev/null", "rw") or die "unveil /dev/null failed: $!";
unveil($log_dir, "rwc") or die "unveil $base_dir failed: $!";
unveil($base_dir, "rwc") or die "unveil $base_dir failed: $!";
unveil() or die "unveil lock failed: $!";

pledge('stdio rpath wpath cpath flock') or die "pledge failed: $!";

my $dop = 5;
my $log_file = "$log_dir/lexcgi.log";
my $max_log_bytes = 1024 * 1024; # 1 MB cap

sub log_msg {
	my ($msg) = @_;

	my $lh;
	if (open($lh, '>>', $log_file)) {
		my $tz_offset = 8 * 3600;
		my ($sec, $min, $hour, $mday, $mon, $year) = gmtime(time() + $tz_offset);
		my $timestamp = sprintf(
			"%04d-%02d-%02d %02d:%02d:%02d",
			$year + 1900, $mon + 1, $mday, $hour, $min, $sec
		);
		print $lh "[$timestamp] [$$] $msg\n";
		close($lh);
	}
}

sub respond_ok {
	print "Status: 200 OK\r\n";
	print "Content-Type: text/plain; charset=utf-8\r\n\r\n";
	print "OK\n";
	exit 0;
}

my $pgp_error;

sub set_pgp_error {
	my ($msg) = @_;
	$msg //= 'unknown error';
	$msg =~ s/\s+/ /g;   # collapse any newlines/whitespace runs to single spaces
	$msg =~ s/^\s+|\s+$//g; # trim leading/trailing space
	$pgp_error = $msg;
}

sub verify_pgp_signature {
	my ($signed_data, $signature) = @_;

	$pgp_error = undef;

	unless (-f $keyring_file) {
		set_pgp_error("No keyring at $keyring_file");
		return 0;
	}

	# Ensure CRLF line endings per OpenPGP canonical specification
	$signed_data =~ s/\r?\n/\r\n/g;

	my $pgp = Crypt::OpenPGP->new(
		PubRing => $keyring_file,
	);

	unless ($pgp) {
		set_pgp_error(Crypt::OpenPGP->errstr // 'unknown PGP init error');
		return 0;
	}

	my $verified = $pgp->verify(
		Data      => $signed_data,
		Signature => $signature,
	);

	unless ($verified) {
		set_pgp_error($pgp->errstr // 'unknown error');
	}

	return $verified ? 1 : 0;
}

sub acquire_lock {
	my ($max_slots) = @_;
	my $lock_dir = "$base_dir/locks";

	unless (-d $lock_dir) {
		make_path($lock_dir) or do {
			log_msg("ERROR: Failed to create lock dir $lock_dir: $!");
			return undef;
		};
	}

	for my $i (1 .. $max_slots) {
		my $slot_file = "$lock_dir/slot_$i.lock";
		my $fh;
		
		unless (open($fh, '>>', $slot_file)) {
			next;
		}

		if (flock($fh, LOCK_EX | LOCK_NB)) {
			return $fh;
		}
		close($fh);
	}

	return undef; # All slots full
}

my $lock_fh = acquire_lock($dop);
unless ($lock_fh) {
	log_msg("WARN: Concurrency limit ($dop) reached. Dropping request.");
	respond_ok();
}

my $content_type   = $ENV{'CONTENT_TYPE'} // '';
my $content_length = $ENV{'CONTENT_LENGTH'} // 0;

my $remote_ip  = $ENV{'REMOTE_ADDR'} // 'unknown';
my $user_agent = $ENV{'HTTP_USER_AGENT'} // 'unknown';
log_msg("INFO: New request from $remote_ip [$user_agent]");

unless ($content_length > 0) {
	log_msg("WARN: content_length is 0 or missing");
	respond_ok();
}

binmode(STDIN);
my $raw_data = '';
read(STDIN, $raw_data, $content_length);

unless ($content_type =~ /application\/x-www-form-urlencoded/i) {
	log_msg("WARN: Content-Type is not urlencoded: '$content_type'");
	respond_ok();
}

my %params;
for my $pair (split(/&/, $raw_data)) {
	my ($key, $val) = split(/=/, $pair, 2);
	next unless defined $key;

	$key =~ tr/+/ /;
	$key =~ s/%([a-fA-F0-9]{2})/chr(hex($1))/eg;

	$val //= '';
	$val =~ tr/+/ /;
	$val =~ s/%([a-fA-F0-9]{2})/chr(hex($1))/eg;

	$params{$key} //= $val;
}

my $mime_raw = $params{'body-mime'} // '';
if ($mime_raw eq '') {
	log_msg("WARN: No MIME message in payload");
	respond_ok();
}

# Parse MIME message
my $parsed = Email::MIME->new($mime_raw);
my @parts  = $parsed->parts;

unless (@parts >= 2) {
	log_msg("WARN: MIME parts count < 2 (found " . scalar(@parts) . ")");
	respond_ok();
}

# Extract the boundary string from the outer Content-Type header so the
# signed part can be sliced out of the raw message verbatim. PGP/MIME
# signature verification (RFC 3156) requires the exact original octets of
# the signed entity; re-serializing it via Email::MIME's ->as_string()
# is not safe, since header folding, whitespace, and field order can be
# normalized differently on reserialization than the sender produced,
# which breaks the signature hash even though the content is unchanged.
my ($boundary) = $parsed->content_type =~ /boundary="?([^";]+)"?/;

unless ($boundary) {
	log_msg("WARN: No MIME boundary found in Content-Type header");
	respond_ok();
}

# Slice the literal bytes between the first boundary delimiter and the
# CRLF immediately preceding the second boundary delimiter. That trailing
# CRLF belongs to the boundary line itself, not the signed content, per
# the MIME multipart spec.
my $signed_part;
if ($mime_raw =~ /--\Q$boundary\E\r?\n(.*?)\r?\n--\Q$boundary\E/s) {
	$signed_part = $1;
	$signed_part =~ s/\r?\n/\r\n/g; # canonical OpenPGP line endings
}

# Extract PGP signature block
my $sig_part_raw = $parts[1]->body_raw // '';
my ($pgp_sig)    = $sig_part_raw =~ /(-----BEGIN PGP SIGNATURE-----[\s\S]*?-----END PGP SIGNATURE-----)/;

unless ($pgp_sig) {
	my $sig_part_str = $parts[1]->body_str // '';
	($pgp_sig) = $sig_part_str =~ /(-----BEGIN PGP SIGNATURE-----[\s\S]*?-----END PGP SIGNATURE-----)/;
}

unless (defined $signed_part && length($signed_part) > 0 && $pgp_sig) {
	log_msg("WARN: Missing PGP signature block");
	respond_ok();
}

unless (verify_pgp_signature($signed_part, $pgp_sig)) {
	log_msg("WARN: PGP signature verification failed: " . ($pgp_error // 'unknown error'));
	respond_ok();
}

my $plain_text = $parts[0]->body_str // '';
$plain_text =~ s/\r\n/\n/g;
$plain_text =~ s/\n-- \n.*$//s;
$plain_text =~ s/^\s+|\s+$//g;

my $plain_text_len = length($plain_text);

unless ($plain_text_len > 0) {
	log_msg("WARN: Message is empty after stripping");
	respond_ok();
}

unless ($plain_text_len <= 200) {
	log_msg("WARN: Message exceeds 200 characters");
	respond_ok();
}

my ($sec, $usec) = gettimeofday();
my $spool_filename = sprintf("spool_%d%06d_%d.job", $sec, $usec, $$);
my $tmp_spool_file = "$spool_dir/tmp/$spool_filename.tmp";
my $spool_file = "$spool_dir/new/$spool_filename";

# Set umask so group gets read/write (0666 & ~0007 = 0660)
my $old_umask = umask(007);
my $sfh;
unless (open($sfh, '>:utf8', $tmp_spool_file)) {
	umask($old_umask);
	log_msg("ERROR: Failed to create $tmp_spool_file: $!");
	respond_ok();
}

print $sfh $plain_text . "\n";
close($sfh);

unless (rename($tmp_spool_file, $spool_file)) {
	umask($old_umask);
	log_msg("ERROR: Failed to create $spool_file: $!");
	respond_ok();
}

umask($old_umask);

my $elapsed_ms = sprintf("%.2fms", (time() - $start_time) * 1000);
my $msg_id = sprintf("msg_%.6f_$$", time());
my $content_length_kb = sprintf("%.2f KB", $content_length / 1024);

log_msg("INFO: Processed $msg_id ($content_length_kb) in $elapsed_ms");
respond_ok();