nobotgate/nobotgate.c
2026-09-08 18:36:12 +02:00

57 lines
1.2 KiB
C

#define _XOPEN_SOURCE 700
#include <libowfat/buffer.h>
#include <libowfat/errmsg.h>
#include <unistd.h>
#include <stdlib.h>
#include "nobotgate.h"
#define dieusage() die(100,"usage: ",argv0," tokenfile allowed-handler denied-handler")
static stralloc token;
static char *tokenfile, *cmd_valid, *cmd_invalid;
static void run(void) {
char tmp[] = PATH_TMP"/nobotgate.XXXXXX";
int tmpfd;
char buf[8192];
buffer b;
char *cmd;
tmpfd = mkstemp(tmp);
buffer_init_write(&b,tmpfd,buf,sizeof buf);
if (!send_headers(buffer_0, &b))
die(111,"copy stdin to temporary file");
lseek(tmpfd,0,SEEK_SET);
buffer_init_read(&b,tmpfd,buf,sizeof buf);
if (!parse_token(&b,&token)) {
carp("failed to parse token");
goto err;
}
cmd=check_token(tokenfile,&token)
? cmd_valid
: cmd_invalid;
lseek(tmpfd,0,SEEK_SET);
dup2(tmpfd,0);
unlink(tmp);
buffer_close(&b);
stralloc_free(&token);
carp("execing ",cmd);
execlp(cmd,cmd,NULL);
err:
unlink(tmp);
exit(111);
}
int main(int argc,char **argv)
{
errmsg_iam(*argv);
argv++; argc--;
if (argc != 3) dieusage();
tokenfile=argv[0];
cmd_valid=argv[1];
cmd_invalid=argv[2];
run();
}