home *** CD-ROM | disk | FTP | other *** search
- /* IsOn... Copyright 1990, 1991 NCEMRSoft. Use at your own risk!
- - v1.0 : 1990 : Phil Dietz, NCEMRSoft.
- : Original release.
- - v2.0 : 05 Feb 91 : Phil Dietz, NCEMRSoft.
- : Added 'finger'ing remote machines.
- : Names and searches are now case insensitive.
- - v3.0 : 04 Aug 91 : Mike Gleason, NCEMRSoft.
- : Complete rewrite, using unix system calls.
- : Remote addresses are recognized automatically.
- : IsOn nice's (lowers it's priority) itself.
- : Remote commands are exec'd instead of subshelled.
- : Uses handy getopt() function.
- : Added -f option, in case you don't have finger,
- : our finger flags don't work, or want to try
- : it rwho, etc.
- : Added -d debugging option, so you can watch
- : ison's progress. This is also useful if you
- : want to log to a file.
-
- To compile: "cc -O ison.c -o ison" */
-
- #define VERSION_STR "Version 3.0 (04 Aug 91)"
-
- #include <sys/types.h>
- #include <sys/time.h>
- #include <utmp.h>
- #include <stdio.h>
- #include <ctype.h>
-
- #define SZ(expr) ((size_t) (expr))
- #define DSLEEP 10 /* seconds to delay between iterations */
- #define DDEBUG 0 /* prints stuff if > 0 */
- #define DMAXITER -1L /* loop forever until we find the guy */
- #define DCOMMAND NULL /* command line to do when user is found */
- #define DFINGER "finger -fq"
-
- #ifndef INDEX
- # ifdef _SYSTYPE_SYSV
- # define INDEX strchr
- # else /* bsd */
- # define INDEX index
- # endif
- #endif
-
- extern size_t fread();
- extern FILE *fopen(), *popen();
- extern char *INDEX();
- int strnicmp(), Nice(), Utmp(), Finger();
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- int sleep_sec = DSLEEP;
- int debug = DDEBUG;
- long maxiter = DMAXITER;
- int notfound, flag;
- char *username, hostname[64], *cp;
- char *fingercmd = DFINGER;
- char *cmd = DCOMMAND;
- time_t logontime;
- extern int getopt(), optind; /* getopt() stuff */
- extern char *optarg; /* getopt() stuff */
-
- if (argc <= 1)
- usage (argv[0], fingercmd);
-
- while ((flag = getopt (argc, argv, "dvs:p:i:f:")) != EOF)
- switch(flag) {
- case 'd':
- case 'v': /* debug mode, verbose mode, same thing */
- debug++;
- break;
- case 's':
- cmd = optarg;
- break;
- case 'p':
- sleep_sec = atoi (optarg);
- if (sleep_sec < 0) sleep_sec = DSLEEP;
- break;
- case 'i':
- maxiter = (long) atol (optarg);
- break;
- case 'f':
- fingercmd = optarg;
- break;
- default: usage (argv[0], fingercmd);
- }
- username = argv[optind];
- if (username == NULL || strlen(username) == SZ(0))
- usage (argv[0], fingercmd); /* no user specified! */
-
- /* lower our process' priority (nice) */
- if (Nice (getpid ()))
- perror ("Nice");
-
- /* Check the username for an @, which would suggest that it is
- a domain-style address. */
- if ((cp = INDEX (username, (int)'@')) != NULL) {
- strcpy (hostname, cp); /* @machine.domain.xxx */
- *cp = '\0'; /* shorten address down to just username */
- notfound = Finger (username, sleep_sec, maxiter, argv[0],
- hostname, fingercmd, debug);
- time(&logontime);
- } else
- notfound = Utmp (username, sleep_sec, maxiter, argv[0],
- debug, &logontime);
-
- /* See if the user was found. If not, explain why not. */
- if (notfound != 0) {
- if (notfound > 0) /* maxiter encoutered */
- (void) fprintf (stderr, "## %s is not on.\n", username);
- else (void) fprintf (stderr,
- "## %s: cannot go on because of errors.\n", argv[0]);
- } else {
- /* When we get here, the user we're looking for was detected. */
- (void) fprintf (stderr, "** %s%s logged in since %s",
- #ifdef NO_BEEP
- "",
- #else
- "\007", /* Control-G, the ascii BEL character */
- #endif
- username, ctime(&logontime));
- if (cmd != NULL) {
- /* Run a command (script) if the user requested to. */
- (void) execlp ("/bin/sh", "sh", "-c", cmd, NULL);
- (void) perror (cmd);
- }
- }
- exit (notfound);
- } /* main */
-
-
-
-
- int Utmp(username, sleep_sec, maxiter, progname, debug, tyme)
- char *username, *progname;
- int sleep_sec, debug;
- long maxiter;
- time_t *tyme;
- {
- struct utmp info;
- FILE *in;
- register int not_on = 1, iter = 1;
- register size_t unamelen = strlen (username);
-
- /* Open the utmp file, which is a list of all logged on users. */
- if ((in = fopen (UTMP_FILE, "r")) == NULL) {
- (void) perror (UTMP_FILE);
- return (1);
- }
-
- do {
- if (debug > 0) {
- time(tyme);
- (void) printf("## %s: checking utmp (try #%d) at %s",
- progname, iter, ctime(tyme));
- }
-
- /* Reset the utmp file and re-read it. */
- (void) rewind (in);
-
- /* Cycle through all 'users' logged in. */
- while (not_on && (fread (&info, SZ(sizeof (info)), SZ(1), in)) == SZ(1)) {
- not_on = strnicmp(info.ut_name, username, unamelen);
- if (debug > 1 && *info.ut_name) {
- info.ut_name[8] = '\0';
- printf("%s\n", info.ut_name);
- }
- }
-
- /* Delay a little so we won't hog the CPU */
- if (not_on) {
- if (maxiter > 0 && ++iter > maxiter) {
- not_on = 1; break;
- }
- (void) sleep (sleep_sec);
- }
- } while (not_on);
-
- *tyme = info.ut_time; /* will hold garbage if user not found! */
- (void) fclose (in);
- return (not_on);
- } /* Utmp */
-
-
-
-
- int Finger(username, sleep_sec, maxiter, progname, hostname, fingercmd, debug)
- char *username, *progname, *hostname, *fingercmd;
- int sleep_sec, debug;
- long maxiter;
- {
- FILE *in;
- register int not_on = 1, iter = 1, piperesult;
- register size_t unamelen = strlen (username);
- extern int errno;
- char buf[160], pipename[128];
- time_t now;
-
- strcpy(pipename, fingercmd);
- strcat(pipename, " ");
- if (strnicmp("finger", fingercmd, SZ(6)) != 0)
- hostname++; /* Skip the @ sign if it's not finger! */
- strcat(pipename, hostname);
-
- do {
- if (debug > 0) {
- time(&now);
- (void) printf("## %s: %s (try #%d), at %s",
- progname, pipename, iter, ctime(&now));
- }
-
- if ((in = popen (pipename, "r")) == NULL) {
- perror (fingercmd);
- not_on = -errno;
- break;
- }
-
- /* Cycle through all 'users' logged in. */
- while (not_on && fgets (buf, (int)sizeof(buf), in) != NULL) {
- if (debug > 1) printf(buf);
- not_on = strnicmp(buf, username, unamelen);
- }
-
- piperesult = pclose(in); /* close pipe */
- if (piperesult && not_on) {
- not_on = (piperesult > 0) ? -piperesult : piperesult;
- break;
- }
-
- /* Delay a little so we won't hog the CPU */
- if (not_on) {
- if (maxiter > 0 && ++iter > maxiter) {
- not_on = 1; break;
- }
- (void) sleep (sleep_sec);
- }
- } while (not_on);
- return (not_on);
- } /* Finger */
-
-
-
- strnicmp(a, b, n)
- char *a, *b;
- register size_t n;
- {
- register int i;
-
- for ( ; tolower(*a) == tolower(*b) && n-- > 0; a++, b++)
- if (*a == '\0')
- return (0); /* equal */
- return (n <= 0 ? 0 : tolower(*a) - tolower(*b));
- }
-
-
-
-
- usage(progname, fingercmd)
- char *progname, *fingercmd;
- {
- (void) fprintf (stderr,
- "usage: %s [-p N] [-i N] [-s cmd] [-f cmd] [-d] username &\n\
- \t-p N : Delay 'N' seconds between iterations (default 10).\n\
- \t-i N : Give up after 'N' iterations (default is infinity)\n\
- \t-s cmd : Command to execute when user is found (i.e. \"talk username\")\n\
- \t-f cmd : Command to execute for remote addresses (def: \"%s\")\n\
- \t-d : Debugging mode. More d's, more stuff.\n\n\
- %s by Phil Dietz & Mike Gleason, NCEMRSoft.\n\
- This is TuitionWare. Please send a buck to help us through school!\n\
- \tPhil Dietz, Box 306, Yutan, NE 68073, USA... Thanks!\n\n",
- progname, fingercmd, VERSION_STR);
- exit (1);
- } /* usage */
-
-
-
-
- #include <sys/resource.h>
- #define DEFAULT_NICE_INCREMENT 10
-
- int Nice(pid)
- pid_t pid;
- {
- extern int errno;
- register int priority;
-
- #ifndef NOT_NICE /* #define it if you don't want to be nice'd */
- errno = 0; /* clear it, since priority can be -1! */
- priority = getpriority(PRIO_PROCESS, pid);
- if (errno == 0) {
- priority += DEFAULT_NICE_INCREMENT;
- if (priority > PRIO_MAX)
- priority = PRIO_MAX;
- (void) setpriority(PRIO_PROCESS, pid, priority);
- }
- #endif
- return (errno);
- } /* Nice */
-
- /* eof */
-