#include #include #include #include #include #include #include #include #include #include #define COMMANDER_PORT 9999 main(int argc, char **argv) { struct hostent *e; struct sockaddr_in addr; int s, i; char buf[1000]; FILE *f; struct in_addr ia; enum { Waiting, Printing } state = Waiting; char wd[MAXPATHLEN]; uid_t uid; struct passwd *passwdPtr; addr.sin_family = AF_INET; addr.sin_port = htons(COMMANDER_PORT); ia.s_addr = inet_addr("127.0.0.1"); memcpy(&addr.sin_addr, &ia, sizeof(ia)); s = socket(AF_INET, SOCK_STREAM, 0); if (s < 0) { perror("Making socket"); exit(3); } if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { close(s); if (errno == ECONNREFUSED) exit(2); else exit(3); } f = fdopen(s, "w"); uid = getuid(); passwdPtr = getpwuid(uid); fprintf(f, "AssertUser %s;", passwdPtr->pw_name); fprintf(f, "cd %s; echo -s -n \007; ", getwd(wd)); for (i = 1; i < argc; i++) { if (i != 1) fputc(' ', f); fputs(argv[i], f); } fprintf(f, "; echo -s -n \"\\001 Ignore this line...\";; echo -s -n \007\n"); fflush(f); shutdown(s, 1); while (read(s, buf, 1)) { long count; ioctl(s, FIONREAD, &count); read(s, buf + 1, count > sizeof(buf) - 1 ? sizeof(buf) - 1 : count); for (i = 0; i <= count; i++) { switch (state) { case Waiting: if (buf[i] == '\007') state = Printing; break; case Printing: if (buf[i] == '\007') exit(1); else if (buf[i] == '\001') /* success marker */ exit(0); else { putc(buf[i], stdout); break; } } } fflush(stdout); } exit(3); /* Shouldn't get EOF */ }