int main(int argc, char *argv[]) { char tty[PATH_MAX], *mytty, *cp; int msgsok, myttyfd; time_t atime; uid_t myuid; /* check that sender has write enabled */ if (isatty(fileno(stdin))) myttyfd = fileno(stdin); else if (isatty(fileno(stdout))) myttyfd = fileno(stdout); else if (isatty(fileno(stderr))) myttyfd = fileno(stderr); else errx(1, "can't find your tty"); if (!(mytty = ttyname(myttyfd))) errx(1, "can't find your tty's name"); if ((cp = strrchr(mytty, '/'))) mytty = cp + 1; if (term_chk(mytty, &msgsok, &atime, 1)) exit(1); if (!msgsok) warnx("you have write permission turned off"); myuid = getuid(); /* check args */ switch (argc) { case 2: search_utmp(argv[1], tty, sizeof tty, mytty, myuid); do_write(tty, mytty, myuid); break; case 3: if (!strncmp(argv[2], _PATH_DEV, sizeof(_PATH_DEV) - 1)) argv[2] += sizeof(_PATH_DEV) - 1; if (utmp_chk(argv[1], argv[2])) errx(1, "%s is not logged in on %s", argv[1], argv[2]); if (term_chk(argv[2], &msgsok, &atime, 1)) exit(1); if (myuid && !msgsok) errx(1, "%s has messages disabled on %s", argv[1], argv[2]); do_write(argv[2], mytty, myuid); break; default: (void)fprintf(stderr, "usage: write user [ttyname]\n"); exit(1); } done(0); /* NOTREACHED */ return (0); }
int main(int argc, char **argv) { time_t atime; uid_t myuid; int msgsok, mymsgok, myttyfd; char tty[MAXPATHLEN], *mytty; setlocale(LC_CTYPE, ""); /* check that sender has write enabled */ if (isatty(fileno(stdin))) myttyfd = fileno(stdin); else if (isatty(fileno(stdout))) myttyfd = fileno(stdout); else if (isatty(fileno(stderr))) myttyfd = fileno(stderr); else errx(1, "can't find your tty"); if (!(mytty = ttyname(myttyfd))) errx(1, "can't find your tty's name"); if (!strncmp(mytty, _PATH_DEV, strlen(_PATH_DEV))) mytty += strlen(_PATH_DEV); if (term_chk(mytty, &mymsgok, &atime, 1)) exit(1); if (!mymsgok) warnx("you have write permission turned off (man mesg)"); myuid = getuid(); /* check args */ switch (argc) { case 2: search_utmp(argv[1], tty, sizeof tty, mytty, myuid); do_write(tty, mytty, myuid, &mymsgok); break; case 3: if (!strncmp(argv[2], _PATH_DEV, strlen(_PATH_DEV))) argv[2] += strlen(_PATH_DEV); if (utmp_chk(argv[1], argv[2])) errx(1, "%s is not logged in on %s", argv[1], argv[2]); if (term_chk(argv[2], &msgsok, &atime, 1)) exit(1); if (myuid && !msgsok) errx(1, "%s has messages disabled on %s", argv[1], argv[2]); do_write(argv[2], mytty, myuid, &mymsgok); break; default: usage(); } done(0); return (0); }
char * check_sender(time_t *atime, uid_t myuid, gid_t saved_egid) { int myttyfd; int msgsok; char *mytty; /* check that sender has write enabled */ if (isatty(fileno(stdin))) myttyfd = fileno(stdin); else if (isatty(fileno(stdout))) myttyfd = fileno(stdout); else if (isatty(fileno(stderr))) myttyfd = fileno(stderr); else if (atime == NULL) return NULL; else errx(1, "Cannot find your tty"); if ((mytty = ttyname(myttyfd)) == NULL) err(1, "Cannot find the name of your tty"); if (strncmp(mytty, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) mytty += sizeof(_PATH_DEV) - 1; if (term_chk(myuid, mytty, &msgsok, atime, 1, saved_egid) == -1) err(1, "%s%s", _PATH_DEV, mytty); if (!msgsok) { warnx( "You have write permission turned off; no reply possible"); } return mytty; }
/* * 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. */ void search_utmp(char *user, char *tty, int ttyl, char *mytty, uid_t myuid) { struct utmp u; time_t bestatime, atime; int ufd, nloggedttys, nttys, msgsok, user_is_me; char atty[UT_LINESIZE + 1]; if ((ufd = open(_PATH_UTMP, O_RDONLY)) < 0) err(1, "%s", _PATH_UTMP); nloggedttys = nttys = 0; bestatime = 0; user_is_me = 0; while (read(ufd, (char *) &u, sizeof(u)) == sizeof(u)) if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) { ++nloggedttys; (void)strncpy(atty, u.ut_line, UT_LINESIZE); atty[UT_LINESIZE] = '\0'; if (term_chk(atty, &msgsok, &atime, 0)) continue; /* bad term? skip */ if (myuid && !msgsok) continue; /* skip ttys with msgs off */ if (strcmp(atty, mytty) == 0) { user_is_me = 1; continue; /* don't write to yourself */ } ++nttys; if (atime > bestatime) { bestatime = atime; (void)strlcpy(tty, atty, ttyl); } } (void)close(ufd); if (nloggedttys == 0) errx(1, "%s is not logged in", user); if (nttys == 0) { if (user_is_me) { /* ok, so write to yourself! */ (void)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 - 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); } }