/* lookout.c v0.1 from intrusive - (intrusive@portalofevil.com) - checks to see if anyone new has logged in and executes a command (or command string) when the specified user logs in or just notifies you of their presence with a beep. is either a shell-script or a quote(") encapsulated string containing a ';' or '&&' seperated list of commands to be run via a single call to system() when logs in. if "any" is given for username, notification will be delivered and any commands specified will be executed upon the login of any user" because utmp seems to round the login time to the minute, the login or logout of any user will cause the program to interpret it as logging in IF is already present in utmp AND less than approx one minute has elapsed between the time when was recorded in the file and the time that any user has logged in/out. December 8, 2002 */ #include #include #include #include #include #include #include main(int argc, char **argv) { FILE *fptr; struct stat stbuf; struct utmp ubuff; time_t lastacc, logt; int r, x = 0; char buff[sizeof(struct utmp)]; if((argc != 4) && (argc != 2)) { fprintf(stderr, "usage: %s [-e ]\n\n", argv[0]); exit(1); } if(argc == 4) { if(!strncmp(argv[2], "-e", strlen(argv[2])) == 0) { fprintf(stderr, "usage: %s [-e ]\n\n", argv[0]); exit(1); } else x = 1; } if((argc > 2) && (strlen(argv[1]) > 32)) { fprintf(stderr, "login names must be 32 characters or less.\n"); exit(1); } if(stat("/var/run/utmp", &stbuf) == -1) { fprintf(stderr, "error getting file stats\n"); exit(1); } if((r = fork()) == -1) { fprintf(stderr, "error with fork()\n"); exit(1); } if(r > 0) exit(0); lastacc = stbuf.st_mtime; while(1) { sleep(1); if(stat("/var/run/utmp", &stbuf) == -1) { fprintf(stderr, "error getting file stats\n"); exit(1); } if(lastacc < stbuf.st_mtime) { if((fptr = fopen("/var/run/utmp", "r")) == NULL) { fprintf(stderr, "error opening utmp\n"); exit(1); } while(fread(&ubuff, 1, sizeof(struct utmp), fptr)) { if((strncmp(ubuff.ut_name, argv[1], strlen(argv[1])) == 0) && (ubuff.ut_time >= lastacc)) { printf("\a"); fprintf(stdout, "%s logged in\n", ubuff.ut_name); if(x == 1) system(argv[3]); exit(0); } if((strncmp(argv[1], "any", 3) == 0) && (ubuff.ut_time >= lastacc)) if(!strstr(ubuff.ut_name, "LOGIN")) { printf("\a"); fprintf(stdout, "%s just logged in\n", ubuff.ut_name); if(x == 1) system(argv[3]); exit(0); } } rewind(fptr); lastacc = stbuf.st_mtime; fclose(fptr); } } }