/* calculate number of users on the system */ static int ucount(void) { struct utmpentry *ep; int nusers = 0; getutentries(NULL, &ep); for (; ep; ep = ep->next) nusers++; return (nusers); }
/* * utmp_chk - checks that the given user is actually logged in on * the given tty */ static int utmp_chk(const char *user, const char *tty) { struct utmpentry *ep; getutentries(NULL, &ep); for (; ep; ep = ep->next) { if (strcmp(user, ep->name) == 0 && strcmp(tty, ep->line) == 0) { return(0); } } return(1); }
/* calculate number of users on the system */ static int ucount(void) { static int onusers = -1; int nusers = 0; struct utmpentry *ehead; nusers = getutentries(NULL, &ehead); if (nusers != onusers) { if (nusers == 1) mvprintw(STATROW, STATCOL + 8, " "); else mvprintw(STATROW, STATCOL + 8, "s"); } onusers = nusers; return (nusers); }
/* * search_utmp - search utmp for the "best" terminal to write to * * Ignores terminals with messages disabled, and of the rest, returns * the one with the most recent access time. Returns as value the number * of the user's terminals with messages enabled, or -1 if the user is * not logged in at all. * * Special case for writing to yourself - ignore the terminal you're * writing from, unless that's the only terminal with messages enabled. */ static void search_utmp(const char *user, char *tty, size_t ttyl, const char *mytty, uid_t myuid) { struct utmpentry *ep; time_t bestatime, atime; int nloggedttys, nttys, msgsok, user_is_me; getutentries(NULL, &ep); nloggedttys = nttys = 0; bestatime = 0; user_is_me = 0; for (; ep; ep = ep->next) if (strcmp(user, ep->name) == 0) { ++nloggedttys; if (term_chk(ep->line, &msgsok, &atime, 0)) continue; /* bad term? skip */ if (myuid && !msgsok) continue; /* skip ttys with msgs off */ if (strcmp(ep->line, mytty) == 0) { user_is_me = 1; continue; /* don't write to yourself */ } ++nttys; if (atime > bestatime) { bestatime = atime; strlcpy(tty, ep->line, ttyl); } } if (nloggedttys == 0) errx(1, "%s is not logged in", user); if (nttys == 0) { if (user_is_me) { /* ok, so write to yourself! */ strlcpy(tty, mytty, ttyl); return; } errx(1, "%s has messages disabled", user); } else if (nttys > 1) { warnx("%s is logged in more than once; writing to %s", user, tty); } }
/* * Search utmp for the local user */ int find_user(const char *name, char *tty) { struct utmpentry *ep; int status; struct stat statb; time_t best = 0; char ftty[sizeof(_PATH_DEV) + sizeof(ep->line)]; getutentries(NULL, &ep); #define SCMPN(a, b) strncmp(a, b, sizeof (a)) status = NOT_HERE; (void) strcpy(ftty, _PATH_DEV); for (; ep; ep = ep->next) if (SCMPN(ep->name, name) == 0) { if (*tty == '\0' || best != 0) { if (best == 0) status = PERMISSION_DENIED; /* no particular tty was requested */ (void) strcpy(ftty + sizeof(_PATH_DEV) - 1, ep->line); if (stat(ftty, &statb) == 0) { if (!(statb.st_mode & 020)) continue; if (statb.st_atime > best) { best = statb.st_atime; (void) strcpy(tty, ep->line); status = SUCCESS; continue; } } } if (strcmp(ep->line, tty) == 0) { status = SUCCESS; break; } } return (status); }
int main(int argc, char *argv[]) { struct iovec iov; struct utmpentry *ep; int ch; int ingroup; struct wallgroup *g; struct group *grp; char **np; const char *p; struct passwd *pw; (void)setlocale(LC_CTYPE, ""); while ((ch = getopt(argc, argv, "g:n")) != -1) switch (ch) { case 'n': /* undoc option for shutdown: suppress banner */ if (geteuid() == 0) nobanner = 1; break; case 'g': g = (struct wallgroup *)malloc(sizeof *g); g->next = grouplist; g->name = optarg; g->gid = -1; grouplist = g; break; case '?': default: usage(); } argc -= optind; argv += optind; if (argc > 1) usage(); for (g = grouplist; g; g = g->next) { grp = getgrnam(g->name); if (grp != NULL) g->gid = grp->gr_gid; else warnx("%s: no such group", g->name); } makemsg(*argv); iov.iov_base = mbuf; iov.iov_len = mbufsize; getutentries(NULL, &ep); /* NOSTRICT */ for (; ep; ep = ep->next) { if (grouplist) { ingroup = 0; pw = getpwnam(ep->name); if (!pw) continue; for (g = grouplist; g && ingroup == 0; g = g->next) { if (g->gid == (gid_t)-1) continue; if (g->gid == pw->pw_gid) ingroup = 1; else if ((grp = getgrgid(g->gid)) != NULL) { for (np = grp->gr_mem; *np; np++) { if (strcmp(*np, ep->name) == 0) { ingroup = 1; break; } } } } if (ingroup == 0) continue; } /* skip [xgk]dm/xserver entries (":0", ":1", etc.) */ if (ep->line[0] == ':' && isdigit((unsigned char)ep->line[1])) continue; if ((p = ttymsg(&iov, 1, ep->line, 60*5)) != NULL) warnx("%s", p); } exit(0); }
int main(int argc, char **argv) { int ch; /* Allow user's locale settings to affect character output. */ setlocale(LC_CTYPE, ""); /* * Reset back to the C locale, unless we are using a known * single-byte 8-bit locale. */ if (strncmp(nl_langinfo(CODESET), "ISO8859-", 8)) setlocale(LC_CTYPE, "C"); oflag = 1; /* default to old "office" behavior */ while ((ch = getopt(argc, argv, "lmpshog8")) != -1) switch(ch) { case 'l': lflag = 1; /* long format */ break; case 'm': mflag = 1; /* force exact match of names */ break; case 'p': pplan = 1; /* don't show .plan/.project */ break; case 's': sflag = 1; /* short format */ break; case 'h': oflag = 0; /* remote host info */ break; case 'o': oflag = 1; /* office info */ break; case 'g': gflag = 1; /* no gecos info, besides name */ break; case '8': eightflag = 1; /* 8-bit pass-through */ break; case '?': default: (void)fprintf(stderr, "usage: finger [-lmpshog8] [login ...]\n"); exit(1); } argc -= optind; argv += optind; (void)time(&now); setpassent(1); entries = getutentries(NULL, &ehead); if (argc == 0) { /* * Assign explicit "small" format if no names given and -l * not selected. Force the -s BEFORE we get names so proper * screening will be done. */ if (!lflag) sflag = 1; /* if -l not explicit, force -s */ loginlist(); if (entries == 0) (void)printf("No one logged on.\n"); } else { userlist(argc, argv); /* * Assign explicit "large" format if names given and -s not * explicitly stated. Force the -l AFTER we get names so any * remote finger attempts specified won't be mishandled. */ if (!sflag) lflag = 1; /* if -s not explicit, force -l */ } if (entries) { if (lflag) lflag_print(); else sflag_print(); } return (0); }