xref: /csrg-svn/libexec/comsat/comsat.c (revision 9878)
19289Ssam #ifndef lint
2*9878Ssam static	char *sccsid = "@(#)comsat.c	4.5 82/12/23";
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  */
221513Sroot #define	dprintf	if (0) printf
231513Sroot 
241513Sroot #define MAXUTMP 100		/* down from init */
251513Sroot 
26*9878Ssam struct	sockaddr_in sin = { AF_INET };
276394Sroot extern	errno;
286394Sroot 
291513Sroot struct	utmp utmp[100];
301513Sroot int	nutmp;
311513Sroot int	uf;
321513Sroot unsigned utmpmtime;			/* last modification time for utmp */
331513Sroot int	onalrm();
349289Ssam struct	servent *sp;
351513Sroot 
361513Sroot #define NAMLEN (sizeof (uts[0].ut_name) + 1)
371513Sroot 
381513Sroot main(argc, argv)
391513Sroot char **argv;
401513Sroot {
411513Sroot 	register cc;
421513Sroot 	char buf[BUFSIZ];
436394Sroot 	int s;
441513Sroot 
459289Ssam 	sp = getservbyname("biff", "udp");
469289Ssam 	if (sp == 0) {
479289Ssam 		fprintf(stderr, "comsat: biff/udp: unknown service\n");
489289Ssam 		exit(1);
499289Ssam 	}
506394Sroot #ifndef DEBUG
511513Sroot 	if (fork())
521513Sroot 		exit();
536394Sroot #endif
541513Sroot 	chdir("/usr/spool/mail");
551513Sroot 	if((uf = open("/etc/utmp",0)) < 0)
561513Sroot 		perror("/etc/utmp"), exit(1);
576394Sroot #ifndef DEBUG
581513Sroot 	while (fork())
591513Sroot 		wait(0);
606394Sroot #endif
611572Sbill 	sleep(10);
621513Sroot 	onalrm();
631513Sroot 	sigset(SIGALRM, onalrm);
641513Sroot 	sigignore(SIGTTOU);
659289Ssam 	s = socket(AF_INET, SOCK_DGRAM, 0, 0);
666394Sroot 	if (s < 0) {
676394Sroot 		perror("socket");
681513Sroot 		exit(1);
691513Sroot 	}
709289Ssam 	sin.sin_port = sp->s_port;
719289Ssam 	if (bind(s, &sin, sizeof (sin), 0) < 0) {
729289Ssam 		perror("bind");
739289Ssam 		exit(1);
749289Ssam 	}
756394Sroot 	for (;;) {
766394Sroot 		char msgbuf[100];
776394Sroot 		int cc;
781513Sroot 
799289Ssam 		cc = recv(s, msgbuf, sizeof (msgbuf) - 1, 0);
806394Sroot 		if (cc <= 0) {
816394Sroot 			if (errno != EINTR)
826394Sroot 				sleep(1);
836394Sroot 			errno = 0;
846394Sroot 			continue;
851513Sroot 		}
866394Sroot 		msgbuf[cc] = 0;
876394Sroot 		mailfor(msgbuf);
881513Sroot 	}
891513Sroot }
901513Sroot 
911513Sroot onalrm()
921513Sroot {
931513Sroot 	struct stat statbf;
941513Sroot 	struct utmp *utp;
951513Sroot 
961513Sroot 	dprintf("alarm\n");
971513Sroot 	alarm(15);
981513Sroot 	fstat(uf,&statbf);
991513Sroot 	if (statbf.st_mtime > utmpmtime) {
1001513Sroot 		dprintf(" changed\n");
1011513Sroot 		utmpmtime = statbf.st_mtime;
1021513Sroot 		lseek(uf, 0, 0);
1031513Sroot 		nutmp = read(uf,utmp,sizeof(utmp))/sizeof(struct utmp);
1041513Sroot 	} else
1051513Sroot 		dprintf(" ok\n");
1061513Sroot }
1071513Sroot 
1081513Sroot mailfor(name)
1091513Sroot 	char *name;
1101513Sroot {
1111513Sroot 	register struct utmp *utp = &utmp[nutmp];
1121513Sroot 	register char *cp;
1131513Sroot 	char *rindex();
1141513Sroot 	int offset;
1151513Sroot 
1161513Sroot 	dprintf("mailfor %s\n", name);
1171513Sroot 	cp = name;
1181513Sroot 	while (*cp && *cp != '@')
1191513Sroot 		cp++;
1201513Sroot 	if (*cp == 0) {
1211513Sroot 		dprintf("bad format\n");
1221513Sroot 		return;
1231513Sroot 	}
1241513Sroot 	*cp = 0;
1251513Sroot 	offset = atoi(cp+1);
1261513Sroot 	while (--utp >= utmp)
1271513Sroot 		if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
1281513Sroot 			if (fork() == 0) {
1291513Sroot 				signal(SIGALRM, SIG_DFL);
1301513Sroot 				alarm(30);
1311513Sroot 				notify(utp, offset), exit(0);
1321513Sroot 			} else
1331513Sroot 				while (wait3(0, WNOHANG, 0) > 0)
1341513Sroot 					continue;
1351513Sroot }
1361513Sroot 
1371513Sroot char *cr;
1381513Sroot 
1391513Sroot notify(utp, offset)
1401513Sroot 	register struct utmp *utp;
1411513Sroot {
1421513Sroot 	FILE *tp;
1431513Sroot 	struct sgttyb gttybuf;
1441513Sroot 	char tty[20];
1451513Sroot 	char name[sizeof (utmp[0].ut_name) + 1];
1461513Sroot 	struct stat stb;
1471513Sroot 
1481513Sroot 	strcpy(tty, "/dev/");
1491513Sroot 	strncat(tty, utp->ut_line, sizeof(utp->ut_line));
1501513Sroot 	dprintf("notify %s on %s\n", utp->ut_name, tty);
1511513Sroot 	if (stat(tty, &stb) == 0 && (stb.st_mode & 0100) == 0) {
1521513Sroot 		dprintf("wrong mode\n");
1531513Sroot 		return;
1541513Sroot 	}
1551513Sroot 	if ((tp = fopen(tty,"w")) == 0) {
1561513Sroot 		dprintf("fopen failed\n");
1571513Sroot 		return;
1581513Sroot 	}
1591513Sroot 	gtty(fileno(tp),&gttybuf);
1601513Sroot 	cr = (gttybuf.sg_flags & CRMOD) ? "" : "\r";
1611513Sroot 	strncpy(name, utp->ut_name, sizeof (utp->ut_name));
1621513Sroot 	name[sizeof (name) - 1] = 0;
1631513Sroot 	fprintf(tp,"%s\n\007New mail for %s\007 has arrived:%s\n",
1641513Sroot 	    cr, name, cr);
1651513Sroot 	fprintf(tp,"----%s\n", cr);
1661513Sroot 	jkfprintf(tp, name, offset);
1671513Sroot 	 fclose(tp);
1681513Sroot }
1691513Sroot 
1701513Sroot jkfprintf(tp, name, offset)
1711513Sroot 	register FILE *tp;
1721513Sroot {
1731513Sroot 	register FILE *fi;
1741513Sroot 	register int linecnt, charcnt;
1751513Sroot 
1761513Sroot 	dprintf("HERE %s's mail starting at %d\n",
1771513Sroot 	    name, offset);
1781513Sroot 	if ((fi = fopen(name,"r")) == NULL) {
1791513Sroot 		dprintf("Cant read the mail\n");
1801513Sroot 		return;
1811513Sroot 	}
1821513Sroot 	fseek(fi, offset, 0);
1831513Sroot 	linecnt = 7;
1841513Sroot 	charcnt = 560;
1851513Sroot 	/*
1861513Sroot 	 * print the first 7 lines or 560 characters of the new mail
1871513Sroot 	 * (whichever comes first)
1881513Sroot 	 */
1891513Sroot 	for (;;) {
1901513Sroot 		register ch;
1911513Sroot 
1921513Sroot 	 	if ((ch = getc(fi)) == EOF) {
1931513Sroot 			fprintf(tp,"----%s\n", cr);
1941513Sroot 			break;
1951513Sroot 		}
1961513Sroot 		if (ch == '\n') {
1971513Sroot 			fprintf(tp,"%s\n", cr);
1981513Sroot 		 	if (linecnt-- < 0) {
1991513Sroot 				fprintf(tp,"...more...%s\n", cr);
2001513Sroot 				break;
2011513Sroot 			}
2021513Sroot 		} else if(linecnt <= 0) {
2031513Sroot 			fprintf(tp,"...more...%s\n", cr);
2041513Sroot 			break;
2051513Sroot 		} else
2061513Sroot 			putc(ch, tp);
2071513Sroot 		if (charcnt-- == 0) {
2081513Sroot 			fprintf(tp, "%s\n", cr);
2091513Sroot 			break;
2101513Sroot 		}
2111513Sroot 	}
2121513Sroot }
213