#define _XOPEN_SOURCE 700 #include #include #include #include #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); if (tmpfd<0) diesys(111,"create temporary file ",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(); }