BSD 4_4 release
[unix-history] / usr / src / libexec / comsat / comsat.c
index 278b846..e2e4477 100644 (file)
-static char *sccsid = "@(#)comsat.c    4.1 (Berkeley) %G%";
-#include <stdio.h>
-#include <sys/mx.h>
-#include <sgtty.h>
-#include <utmp.h>
-#include <sys/types.h>
-#include <stat.h>
-#include <wait.h>
-#include <signal.h>
-
 /*
 /*
- * comsat
+ * Copyright (c) 1980, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
  */
-#define        dprintf if (0) printf
-int    xd;
 
 
-struct  ctp {
-       short   ctrl;
-       short   ctrlarg;
-       struct  sgttyb ctrlv;
-} ctp;
+#ifndef lint
+static char copyright[] =
+"@(#) Copyright (c) 1980, 1993\n\
+       The Regents of the University of California.  All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)comsat.c   8.1 (Berkeley) 6/4/93";
+#endif /* not lint */
 
 
-#define MAXUTMP 100            /* down from init */
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <netdb.h>
+#include <paths.h>
+#include <pwd.h>
+#include <sgtty.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+#include <unistd.h>
+#include <utmp.h>
 
 
-struct utmp utmp[100];
-int    nutmp;
-int    uf;
-unsigned utmpmtime;                    /* last modification time for utmp */
-int    onalrm();
+int    debug = 0;
+#define        dsyslog if (debug) syslog
 
 
-#define NAMLEN (sizeof (uts[0].ut_name) + 1)
+#define MAXIDLE        120
 
 
+char   hostname[MAXHOSTNAMELEN];
+struct utmp *utmp = NULL;
+time_t lastmsgtime;
+int    nutmp, uf;
+
+void jkfprintf __P((FILE *, char[], off_t));
+void mailfor __P((char *));
+void notify __P((struct utmp *, off_t));
+void onalrm __P((int));
+void reapchildren __P((int));
+
+int
 main(argc, argv)
 main(argc, argv)
-char **argv;
+       int argc;
+       char *argv[];
 {
 {
-       register cc;
-       char buf[BUFSIZ];
+       struct sockaddr_in from;
+       register int cc;
+       int fromlen;
+       char msgbuf[100];
 
 
-       if (fork())
-               exit();
-       chdir("/usr/spool/mail");
-       if((uf = open("/etc/utmp",0)) < 0)
-               perror("/etc/utmp"), exit(1);
-       while (fork())
-               wait(0);
-       onalrm();
-       sigset(SIGALRM, onalrm);
-       sigignore(SIGTTOU);
-       unlink("/dev/mail");
-       xd = mpx("/dev/mail", 0666);
-       if (xd < 0) {
-               close(2);
-               open("/dev/console", 1);
-               perror("/dev/mail");
+       /* verify proper invocation */
+       fromlen = sizeof(from);
+       if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
+               (void)fprintf(stderr,
+                   "comsat: getsockname: %s.\n", strerror(errno));
                exit(1);
        }
                exit(1);
        }
-       while((cc=read(xd, buf, BUFSIZ)) >= 0) {
-               dprintf("0: got %d bytes\n", cc);
-               unpack(buf, cc);
+       openlog("comsat", LOG_PID, LOG_DAEMON);
+       if (chdir(_PATH_MAILDIR)) {
+               syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
+               (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
+               exit(1);
        }
        }
-}
-
-#define        skip(rp, c)     ((struct rh *)(((char *)rp)+c))
-
-unpack(rp, cc)
-       register struct rh *rp;
-{
-       register struct rh *end;
-       int i;
-
-       i = 0;
-       end = skip(rp, cc);
-       while (rp < end) {
-               dprintf("%d: ", ++i);
-               if (rp->count==0) {
-                       dprintf("%d byte control message\n", rp->ccount);
-                       control(rp->index, rp+1, rp->ccount);
-               } else {
-                       dprintf("%*.*s\n", rp->count, rp->count, rp+1);
-                       sighold(SIGALRM);
-                       mailfor(rp+1);
-                       sigrelse(SIGALRM);
+       if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
+               syslog(LOG_ERR, "open: %s: %m", _PATH_UTMP);
+               (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
+               exit(1);
+       }
+       (void)time(&lastmsgtime);
+       (void)gethostname(hostname, sizeof(hostname));
+       onalrm(0);
+       (void)signal(SIGALRM, onalrm);
+       (void)signal(SIGTTOU, SIG_IGN);
+       (void)signal(SIGCHLD, reapchildren);
+       for (;;) {
+               cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
+               if (cc <= 0) {
+                       if (errno != EINTR)
+                               sleep(1);
+                       errno = 0;
+                       continue;
                }
                }
-               rp->count += rp->ccount;
-               if (rp->count & 1)
-                       rp->count++;
-               rp = skip(rp, rp->count);
-               rp++;
+               if (!nutmp)             /* no one has logged in yet */
+                       continue;
+               sigblock(sigmask(SIGALRM));
+               msgbuf[cc] = '\0';
+               (void)time(&lastmsgtime);
+               mailfor(msgbuf);
+               sigsetmask(0L);
        }
 }
 
        }
 }
 
-control(x, cb, cc)
-       register char *cb;
+void
+reapchildren(signo)
+       int signo;
 {
 {
-       register char *end;
-       int cmd;
-       short *sp;
-       struct wh or;
-
-       end = cb + cc;
-       cmd = *cb++;
-       sp = (short *)cb+1;
-       switch (cmd) {
-
-       case M_WATCH:
-               dprintf("attach %x, uid %d\n", x, *sp);
-               attach(x, xd);
-               break;
-
-       case M_CLOSE:
-               sp = (short *)cb;
-               dprintf("detach %x, uid %d\n", x, *sp);
-               detach(x, xd);
-               break;
-
-       case M_IOCTL:
-               dprintf("ioctl %x\n", x);
-               or.index = x;
-               or.count = 0;
-               or.ccount = sizeof ctp;
-               or.data = (char *) &ctp.ctrlarg;
-               ctp.ctrlarg = M_IOANS;
-               write(xd, &or, sizeof or);
-               break;
-               
-       default:
-               dprintf("unknown command %d\n", cmd);
-               return;
-       }
+       while (wait3(NULL, WNOHANG, NULL) > 0);
 }
 
 }
 
-onalrm()
+void
+onalrm(signo)
+       int signo;
 {
 {
+       static u_int utmpsize;          /* last malloced size for utmp */
+       static u_int utmpmtime;         /* last modification time for utmp */
        struct stat statbf;
        struct stat statbf;
-       struct utmp *utp;
 
 
-       dprintf("alarm\n");
-       alarm(15);
-       fstat(uf,&statbf);
+       if (time(NULL) - lastmsgtime >= MAXIDLE)
+               exit(0);
+       (void)alarm((u_int)15);
+       (void)fstat(uf, &statbf);
        if (statbf.st_mtime > utmpmtime) {
        if (statbf.st_mtime > utmpmtime) {
-               dprintf(" changed\n");
                utmpmtime = statbf.st_mtime;
                utmpmtime = statbf.st_mtime;
-               lseek(uf, 0, 0);
-               nutmp = read(uf,utmp,sizeof(utmp))/sizeof(struct utmp);
-       } else
-               dprintf(" ok\n");
+               if (statbf.st_size > utmpsize) {
+                       utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
+                       if ((utmp = realloc(utmp, utmpsize)) == NULL) {
+                               syslog(LOG_ERR, "%s", strerror(errno));
+                               exit(1);
+                       }
+               }
+               (void)lseek(uf, (off_t)0, L_SET);
+               nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp);
+       }
 }
 
 }
 
+void
 mailfor(name)
        char *name;
 {
        register struct utmp *utp = &utmp[nutmp];
        register char *cp;
 mailfor(name)
        char *name;
 {
        register struct utmp *utp = &utmp[nutmp];
        register char *cp;
-       char *rindex();
-       int offset;
-
-       dprintf("mailfor %s\n", name);
-       cp = name;
-       while (*cp && *cp != '@')
-               cp++;
-       if (*cp == 0) {
-               dprintf("bad format\n");
+       off_t offset;
+
+       if (!(cp = strchr(name, '@')))
                return;
                return;
-       }
-       *cp = 0;
-       offset = atoi(cp+1);
+       *cp = '\0';
+       offset = atoi(cp + 1);
        while (--utp >= utmp)
                if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
        while (--utp >= utmp)
                if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
-                       if (fork() == 0) {
-                               signal(SIGALRM, SIG_DFL);
-                               alarm(30);
-                               notify(utp, offset), exit(0);
-                       } else
-                               while (wait3(0, WNOHANG, 0) > 0)
-                                       continue;
+                       notify(utp, offset);
 }
 
 }
 
-char *cr;
+static char *cr;
 
 
+void
 notify(utp, offset)
        register struct utmp *utp;
 notify(utp, offset)
        register struct utmp *utp;
+       off_t offset;
 {
        FILE *tp;
 {
        FILE *tp;
-       struct sgttyb gttybuf;
-       char tty[20];
-       char name[sizeof (utmp[0].ut_name) + 1];
        struct stat stb;
        struct stat stb;
+       struct sgttyb gttybuf;
+       char tty[20], name[sizeof(utmp[0].ut_name) + 1];
 
 
-       strcpy(tty, "/dev/");
-       strncat(tty, utp->ut_line, sizeof(utp->ut_line));
-       dprintf("notify %s on %s\n", utp->ut_name, tty);
-       if (stat(tty, &stb) == 0 && (stb.st_mode & 0100) == 0) {
-               dprintf("wrong mode\n");
+       (void)snprintf(tty, sizeof(tty), "%s%.*s",
+           _PATH_DEV, (int)sizeof(utp->ut_line), utp->ut_line);
+       if (strchr(tty + sizeof(_PATH_DEV) - 1, '/')) {
+               /* A slash is an attempt to break security... */
+               syslog(LOG_AUTH | LOG_NOTICE, "'/' in \"%s\"", tty);
+               return;
+       }
+       if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
+               dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
                return;
        }
                return;
        }
-       if ((tp = fopen(tty,"w")) == 0) {
-               dprintf("fopen failed\n");
+       dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty);
+       if (fork())
                return;
                return;
+       (void)signal(SIGALRM, SIG_DFL);
+       (void)alarm((u_int)30);
+       if ((tp = fopen(tty, "w")) == NULL) {
+               dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
+               _exit(-1);
        }
        }
-       gtty(fileno(tp),&gttybuf);
-       cr = (gttybuf.sg_flags & CRMOD) ? "" : "\r";
-       strncpy(name, utp->ut_name, sizeof (utp->ut_name));
-       name[sizeof (name) - 1] = 0;
-       fprintf(tp,"%s\n\007New mail for %s\007 has arrived:%s\n",
-           cr, name, cr);
-       fprintf(tp,"----%s\n", cr);
+       (void)ioctl(fileno(tp), TIOCGETP, &gttybuf);
+       cr = (gttybuf.sg_flags&CRMOD) && !(gttybuf.sg_flags&RAW) ?
+           "\n" : "\n\r";
+       (void)strncpy(name, utp->ut_name, sizeof(utp->ut_name));
+       name[sizeof(name) - 1] = '\0';
+       (void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s",
+           cr, name, (int)sizeof(hostname), hostname, cr, cr);
        jkfprintf(tp, name, offset);
        jkfprintf(tp, name, offset);
-        fclose(tp);
+       (void)fclose(tp);
+       _exit(0);
 }
 
 }
 
+void
 jkfprintf(tp, name, offset)
        register FILE *tp;
 jkfprintf(tp, name, offset)
        register FILE *tp;
+       char name[];
+       off_t offset;
 {
 {
+       register char *cp, ch;
        register FILE *fi;
        register FILE *fi;
-       register int linecnt, charcnt;
+       register int linecnt, charcnt, inheader;
+       register struct passwd *p;
+       char line[BUFSIZ];
+
+       /* Set effective uid to user in case mail drop is on nfs */
+       if ((p = getpwnam(name)) != NULL)
+               (void) setuid(p->pw_uid);
 
 
-       dprintf("HERE %s's mail starting at %d\n",
-           name, offset);
-       if ((fi = fopen(name,"r")) == NULL) {
-               dprintf("Cant read the mail\n");
+       if ((fi = fopen(name, "r")) == NULL)
                return;
                return;
-       }
-       fseek(fi, offset, 0);
+
+       (void)fseek(fi, offset, L_SET);
+       /*
+        * Print the first 7 lines or 560 characters of the new mail
+        * (whichever comes first).  Skip header crap other than
+        * From, Subject, To, and Date.
+        */
        linecnt = 7;
        charcnt = 560;
        linecnt = 7;
        charcnt = 560;
-       /* 
-        * print the first 7 lines or 560 characters of the new mail
-        * (whichever comes first)
-        */
-       for (;;) {
-               register ch;
-
-               if ((ch = getc(fi)) == EOF) {  
-                       fprintf(tp,"----%s\n", cr);
-                       break;
-               }
-               if (ch == '\n') {  
-                       fprintf(tp,"%s\n", cr);
-                       if (linecnt-- < 0) {  
-                               fprintf(tp,"...more...%s\n", cr);
-                               break;
+       inheader = 1;
+       while (fgets(line, sizeof(line), fi) != NULL) {
+               if (inheader) {
+                       if (line[0] == '\n') {
+                               inheader = 0;
+                               continue;
                        }
                        }
-               } else if(linecnt <= 0) {  
-                       fprintf(tp,"...more...%s\n", cr);
-                       break;
-               } else
-                       putc(ch, tp);
-               if (charcnt-- == 0) {   
-                       fprintf(tp, "%s\n", cr);
-                       break;
+                       if (line[0] == ' ' || line[0] == '\t' ||
+                           strncmp(line, "From:", 5) &&
+                           strncmp(line, "Subject:", 8))
+                               continue;
+               }
+               if (linecnt <= 0 || charcnt <= 0) {
+                       (void)fprintf(tp, "...more...%s", cr);
+                       (void)fclose(fi);
+                       return;
+               }
+               /* strip weird stuff so can't trojan horse stupid terminals */
+               for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
+                       ch = toascii(ch);
+                       if (!isprint(ch) && !isspace(ch))
+                               ch |= 0x40;
+                       (void)fputc(ch, tp);
                }
                }
+               (void)fputs(cr, tp);
+               --linecnt;
        }
        }
+       (void)fprintf(tp, "----%s\n", cr);
+       (void)fclose(fi);
 }
 }