19289Ssam #ifndef lint 2*12746Ssam static char *sccsid = "@(#)comsat.c 4.6 83/05/27"; 39289Ssam #endif 46394Sroot 59289Ssam #include <sys/types.h> 69289Ssam #include <sys/socket.h> 79289Ssam 89289Ssam #include <netinet/in.h> 99289Ssam 101513Sroot #include <stdio.h> 111513Sroot #include <sgtty.h> 121513Sroot #include <utmp.h> 131513Sroot #include <stat.h> 141513Sroot #include <wait.h> 151513Sroot #include <signal.h> 166394Sroot #include <errno.h> 179289Ssam #include <netdb.h> 181513Sroot 191513Sroot /* 201513Sroot * comsat 211513Sroot */ 22*12746Ssam int debug = 0; 23*12746Ssam #define dprintf if (debug) printf 241513Sroot 251513Sroot #define MAXUTMP 100 /* down from init */ 261513Sroot 279878Ssam struct sockaddr_in sin = { AF_INET }; 286394Sroot extern errno; 296394Sroot 301513Sroot struct utmp utmp[100]; 311513Sroot int nutmp; 321513Sroot int uf; 331513Sroot unsigned utmpmtime; /* last modification time for utmp */ 341513Sroot int onalrm(); 359289Ssam struct servent *sp; 361513Sroot 371513Sroot #define NAMLEN (sizeof (uts[0].ut_name) + 1) 381513Sroot 391513Sroot main(argc, argv) 401513Sroot char **argv; 411513Sroot { 421513Sroot register cc; 431513Sroot char buf[BUFSIZ]; 446394Sroot int s; 451513Sroot 469289Ssam sp = getservbyname("biff", "udp"); 479289Ssam if (sp == 0) { 489289Ssam fprintf(stderr, "comsat: biff/udp: unknown service\n"); 499289Ssam exit(1); 509289Ssam } 51*12746Ssam if (!debug) 521513Sroot if (fork()) 531513Sroot exit(); 541513Sroot chdir("/usr/spool/mail"); 551513Sroot if((uf = open("/etc/utmp",0)) < 0) 561513Sroot perror("/etc/utmp"), exit(1); 57*12746Ssam if (!debug) 581513Sroot while (fork()) 591513Sroot wait(0); 601572Sbill sleep(10); 611513Sroot onalrm(); 621513Sroot sigset(SIGALRM, onalrm); 631513Sroot sigignore(SIGTTOU); 649289Ssam s = socket(AF_INET, SOCK_DGRAM, 0, 0); 656394Sroot if (s < 0) { 666394Sroot perror("socket"); 671513Sroot exit(1); 681513Sroot } 699289Ssam sin.sin_port = sp->s_port; 709289Ssam if (bind(s, &sin, sizeof (sin), 0) < 0) { 719289Ssam perror("bind"); 729289Ssam exit(1); 739289Ssam } 746394Sroot for (;;) { 756394Sroot char msgbuf[100]; 766394Sroot int cc; 771513Sroot 789289Ssam cc = recv(s, msgbuf, sizeof (msgbuf) - 1, 0); 796394Sroot if (cc <= 0) { 806394Sroot if (errno != EINTR) 816394Sroot sleep(1); 826394Sroot errno = 0; 836394Sroot continue; 841513Sroot } 856394Sroot msgbuf[cc] = 0; 866394Sroot mailfor(msgbuf); 871513Sroot } 881513Sroot } 891513Sroot 901513Sroot onalrm() 911513Sroot { 921513Sroot struct stat statbf; 931513Sroot struct utmp *utp; 941513Sroot 951513Sroot dprintf("alarm\n"); 961513Sroot alarm(15); 971513Sroot fstat(uf,&statbf); 981513Sroot if (statbf.st_mtime > utmpmtime) { 991513Sroot dprintf(" changed\n"); 1001513Sroot utmpmtime = statbf.st_mtime; 1011513Sroot lseek(uf, 0, 0); 1021513Sroot nutmp = read(uf,utmp,sizeof(utmp))/sizeof(struct utmp); 1031513Sroot } else 1041513Sroot dprintf(" ok\n"); 1051513Sroot } 1061513Sroot 1071513Sroot mailfor(name) 1081513Sroot char *name; 1091513Sroot { 1101513Sroot register struct utmp *utp = &utmp[nutmp]; 1111513Sroot register char *cp; 1121513Sroot char *rindex(); 1131513Sroot int offset; 1141513Sroot 1151513Sroot dprintf("mailfor %s\n", name); 1161513Sroot cp = name; 1171513Sroot while (*cp && *cp != '@') 1181513Sroot cp++; 1191513Sroot if (*cp == 0) { 1201513Sroot dprintf("bad format\n"); 1211513Sroot return; 1221513Sroot } 1231513Sroot *cp = 0; 1241513Sroot offset = atoi(cp+1); 1251513Sroot while (--utp >= utmp) 1261513Sroot if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name))) 1271513Sroot if (fork() == 0) { 1281513Sroot signal(SIGALRM, SIG_DFL); 1291513Sroot alarm(30); 1301513Sroot notify(utp, offset), exit(0); 1311513Sroot } else 1321513Sroot while (wait3(0, WNOHANG, 0) > 0) 1331513Sroot continue; 1341513Sroot } 1351513Sroot 1361513Sroot char *cr; 1371513Sroot 1381513Sroot notify(utp, offset) 1391513Sroot register struct utmp *utp; 1401513Sroot { 1411513Sroot FILE *tp; 1421513Sroot struct sgttyb gttybuf; 143*12746Ssam char tty[20], hostname[32]; 1441513Sroot char name[sizeof (utmp[0].ut_name) + 1]; 1451513Sroot struct stat stb; 1461513Sroot 1471513Sroot strcpy(tty, "/dev/"); 1481513Sroot strncat(tty, utp->ut_line, sizeof(utp->ut_line)); 1491513Sroot dprintf("notify %s on %s\n", utp->ut_name, tty); 1501513Sroot if (stat(tty, &stb) == 0 && (stb.st_mode & 0100) == 0) { 1511513Sroot dprintf("wrong mode\n"); 1521513Sroot return; 1531513Sroot } 1541513Sroot if ((tp = fopen(tty,"w")) == 0) { 1551513Sroot dprintf("fopen failed\n"); 1561513Sroot return; 1571513Sroot } 158*12746Ssam ioctl(fileno(tp), TIOCGETP, >tybuf); 1591513Sroot cr = (gttybuf.sg_flags & CRMOD) ? "" : "\r"; 160*12746Ssam gethostname(hostname, sizeof (hostname)); 1611513Sroot strncpy(name, utp->ut_name, sizeof (utp->ut_name)); 1621513Sroot name[sizeof (name) - 1] = 0; 163*12746Ssam fprintf(tp,"%s\n\007New mail for %s@%s\007 has arrived:%s\n", 164*12746Ssam cr, name, hostname, cr); 1651513Sroot fprintf(tp,"----%s\n", cr); 1661513Sroot jkfprintf(tp, name, offset); 167*12746Ssam fclose(tp); 1681513Sroot } 1691513Sroot 1701513Sroot jkfprintf(tp, name, offset) 1711513Sroot register FILE *tp; 1721513Sroot { 1731513Sroot register FILE *fi; 1741513Sroot register int linecnt, charcnt; 175*12746Ssam char line[BUFSIZ]; 1761513Sroot 1771513Sroot dprintf("HERE %s's mail starting at %d\n", 1781513Sroot name, offset); 1791513Sroot if ((fi = fopen(name,"r")) == NULL) { 1801513Sroot dprintf("Cant read the mail\n"); 1811513Sroot return; 1821513Sroot } 1831513Sroot fseek(fi, offset, 0); 184*12746Ssam /* 185*12746Ssam * Print the first 7 lines or 560 characters of the new mail 186*12746Ssam * (whichever comes first). Skip header crap other than 187*12746Ssam * From: and Subject:. 188*12746Ssam */ 1891513Sroot linecnt = 7; 1901513Sroot charcnt = 560; 191*12746Ssam while (fgets(line, sizeof (line), fi) != NULL) { 192*12746Ssam register char *cp; 193*12746Ssam char *index(); 1941513Sroot 195*12746Ssam if (linecnt <= 0 || charcnt <= 0) { 1961513Sroot fprintf(tp,"...more...%s\n", cr); 1971513Sroot break; 1981513Sroot } 199*12746Ssam cp = index(line, ':'); 200*12746Ssam if (cp && 201*12746Ssam strncmp(line, "Date", cp - line) && 202*12746Ssam strncmp(line, "From", cp - line) && 203*12746Ssam strncmp(line, "Subject", cp - line) && 204*12746Ssam strncmp(line, "To", cp - line)) 205*12746Ssam continue; 206*12746Ssam cp = index(line, '\n'); 207*12746Ssam if (cp) 208*12746Ssam *cp = '\0'; 209*12746Ssam fprintf(tp,"%s%s\n", line, cr); 210*12746Ssam linecnt--, charcnt -= strlen(line); 2111513Sroot } 212*12746Ssam fprintf(tp,"----%s\n", cr); 2131513Sroot } 214