BSD 4_4 release
[unix-history] / usr / src / libexec / comsat / comsat.c
index 3991eb4..e2e4477 100644 (file)
@@ -1,28 +1,44 @@
 /*
 /*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1980, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * 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.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1980 Regents of the University of California.\n\
- All rights reserved.\n";
+static char copyright[] =
+"@(#) Copyright (c) 1980, 1993\n\
      The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)comsat.c   5.17 (Berkeley) %G%";
+static char sccsid[] = "@(#)comsat.c   8.1 (Berkeley) 6/4/93";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -33,21 +49,20 @@ static char sccsid[] = "@(#)comsat.c        5.17 (Berkeley) %G%";
 
 #include <netinet/in.h>
 
 
 #include <netinet/in.h>
 
-#include <stdio.h>
-#include <sgtty.h>
-#include <utmp.h>
-#include <signal.h>
+#include <ctype.h>
 #include <errno.h>
 #include <netdb.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 <syslog.h>
-#include <strings.h>
-#include <ctype.h>
-
-#include "pathnames.h"
+#include <unistd.h>
+#include <utmp.h>
 
 
-/*
- * comsat
- */
 int    debug = 0;
 #define        dsyslog if (debug) syslog
 
 int    debug = 0;
 #define        dsyslog if (debug) syslog
 
@@ -55,41 +70,46 @@ int debug = 0;
 
 char   hostname[MAXHOSTNAMELEN];
 struct utmp *utmp = NULL;
 
 char   hostname[MAXHOSTNAMELEN];
 struct utmp *utmp = NULL;
-time_t lastmsgtime, time();
+time_t lastmsgtime;
 int    nutmp, uf;
 
 int    nutmp, uf;
 
-/* ARGSUSED */
+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)
        int argc;
 main(argc, argv)
        int argc;
-       char **argv;
+       char *argv[];
 {
 {
-       extern int errno;
-       register int cc;
-       char msgbuf[100];
        struct sockaddr_in from;
        struct sockaddr_in from;
+       register int cc;
        int fromlen;
        int fromlen;
-       void onalrm(), reapchildren();
+       char msgbuf[100];
 
        /* verify proper invocation */
        fromlen = sizeof(from);
 
        /* verify proper invocation */
        fromlen = sizeof(from);
-       if (getsockname(0, &from, &fromlen) < 0) {
+       if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
                (void)fprintf(stderr,
                    "comsat: getsockname: %s.\n", strerror(errno));
                exit(1);
        }
        openlog("comsat", LOG_PID, LOG_DAEMON);
                (void)fprintf(stderr,
                    "comsat: getsockname: %s.\n", strerror(errno));
                exit(1);
        }
        openlog("comsat", LOG_PID, LOG_DAEMON);
-       if (chdir(_PATH_MAIL)) {
-               syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAIL);
+       if (chdir(_PATH_MAILDIR)) {
+               syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
+               (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
                exit(1);
        }
        if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
                exit(1);
        }
        if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
-               syslog(LOG_ERR, ".main: %s: %m", _PATH_UTMP);
+               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));
                (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
                exit(1);
        }
        (void)time(&lastmsgtime);
        (void)gethostname(hostname, sizeof(hostname));
-       onalrm();
+       onalrm(0);
        (void)signal(SIGALRM, onalrm);
        (void)signal(SIGTTOU, SIG_IGN);
        (void)signal(SIGCHLD, reapchildren);
        (void)signal(SIGALRM, onalrm);
        (void)signal(SIGTTOU, SIG_IGN);
        (void)signal(SIGCHLD, reapchildren);
@@ -104,7 +124,7 @@ main(argc, argv)
                if (!nutmp)             /* no one has logged in yet */
                        continue;
                sigblock(sigmask(SIGALRM));
                if (!nutmp)             /* no one has logged in yet */
                        continue;
                sigblock(sigmask(SIGALRM));
-               msgbuf[cc] = 0;
+               msgbuf[cc] = '\0';
                (void)time(&lastmsgtime);
                mailfor(msgbuf);
                sigsetmask(0L);
                (void)time(&lastmsgtime);
                mailfor(msgbuf);
                sigsetmask(0L);
@@ -112,21 +132,21 @@ main(argc, argv)
 }
 
 void
 }
 
 void
-reapchildren()
+reapchildren(signo)
+       int signo;
 {
 {
-       while (wait3((union wait *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
+       while (wait3(NULL, WNOHANG, NULL) > 0);
 }
 
 void
 }
 
 void
-onalrm()
+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;
 {
        static u_int utmpsize;          /* last malloced size for utmp */
        static u_int utmpmtime;         /* last modification time for utmp */
        struct stat statbf;
-       off_t lseek();
-       char *malloc(), *realloc();
 
 
-       if (time((time_t *)NULL) - lastmsgtime >= MAXIDLE)
+       if (time(NULL) - lastmsgtime >= MAXIDLE)
                exit(0);
        (void)alarm((u_int)15);
        (void)fstat(uf, &statbf);
                exit(0);
        (void)alarm((u_int)15);
        (void)fstat(uf, &statbf);
@@ -134,28 +154,25 @@ onalrm()
                utmpmtime = statbf.st_mtime;
                if (statbf.st_size > utmpsize) {
                        utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
                utmpmtime = statbf.st_mtime;
                if (statbf.st_size > utmpsize) {
                        utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
-                       if (utmp)
-                               utmp = (struct utmp *)realloc((char *)utmp, utmpsize);
-                       else
-                               utmp = (struct utmp *)malloc(utmpsize);
-                       if (!utmp) {
-                               syslog(LOG_ERR, "malloc failed");
+                       if ((utmp = realloc(utmp, utmpsize)) == NULL) {
+                               syslog(LOG_ERR, "%s", strerror(errno));
                                exit(1);
                        }
                }
                                exit(1);
                        }
                }
-               (void)lseek(uf, 0L, L_SET);
+               (void)lseek(uf, (off_t)0, L_SET);
                nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp);
        }
 }
 
                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;
-       off_t offset, atol();
+       off_t offset;
 
 
-       if (!(cp = index(name, '@')))
+       if (!(cp = strchr(name, '@')))
                return;
        *cp = '\0';
        offset = atoi(cp + 1);
                return;
        *cp = '\0';
        offset = atoi(cp + 1);
@@ -166,18 +183,23 @@ mailfor(name)
 
 static char *cr;
 
 
 static char *cr;
 
+void
 notify(utp, offset)
        register struct utmp *utp;
        off_t offset;
 {
 notify(utp, offset)
        register struct utmp *utp;
        off_t offset;
 {
-       static char tty[20] = _PATH_DEV;
-       struct sgttyb gttybuf;
-       struct stat stb;
        FILE *tp;
        FILE *tp;
-       char name[sizeof(utmp[0].ut_name) + 1];
+       struct stat stb;
+       struct sgttyb gttybuf;
+       char tty[20], name[sizeof(utmp[0].ut_name) + 1];
 
 
-       (void)strncpy(tty + sizeof(_PATH_DEV) - 1, utp->ut_line,
-           sizeof(utp->ut_line));
+       (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;
        if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
                dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
                return;
@@ -188,7 +210,7 @@ notify(utp, offset)
        (void)signal(SIGALRM, SIG_DFL);
        (void)alarm((u_int)30);
        if ((tp = fopen(tty, "w")) == NULL) {
        (void)signal(SIGALRM, SIG_DFL);
        (void)alarm((u_int)30);
        if ((tp = fopen(tty, "w")) == NULL) {
-               dsyslog(LOG_ERR, "fopen of tty %s failed", tty);
+               dsyslog(LOG_ERR, "%s: %s", tty, strerror(errno));
                _exit(-1);
        }
        (void)ioctl(fileno(tp), TIOCGETP, &gttybuf);
                _exit(-1);
        }
        (void)ioctl(fileno(tp), TIOCGETP, &gttybuf);
@@ -197,12 +219,13 @@ notify(utp, offset)
        (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",
        (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, sizeof(hostname), hostname, cr, cr);
+           cr, name, (int)sizeof(hostname), hostname, cr, cr);
        jkfprintf(tp, name, offset);
        (void)fclose(tp);
        _exit(0);
 }
 
        jkfprintf(tp, name, offset);
        (void)fclose(tp);
        _exit(0);
 }
 
+void
 jkfprintf(tp, name, offset)
        register FILE *tp;
        char name[];
 jkfprintf(tp, name, offset)
        register FILE *tp;
        char name[];
@@ -211,13 +234,18 @@ jkfprintf(tp, name, offset)
        register char *cp, ch;
        register FILE *fi;
        register int linecnt, charcnt, inheader;
        register char *cp, ch;
        register FILE *fi;
        register int linecnt, charcnt, inheader;
+       register struct passwd *p;
        char line[BUFSIZ];
        char line[BUFSIZ];
-       off_t fseek();
+
+       /* Set effective uid to user in case mail drop is on nfs */
+       if ((p = getpwnam(name)) != NULL)
+               (void) setuid(p->pw_uid);
 
        if ((fi = fopen(name, "r")) == NULL)
                return;
 
        if ((fi = fopen(name, "r")) == NULL)
                return;
+
        (void)fseek(fi, offset, L_SET);
        (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.
         * 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.
@@ -238,11 +266,12 @@ jkfprintf(tp, name, offset)
                }
                if (linecnt <= 0 || charcnt <= 0) {
                        (void)fprintf(tp, "...more...%s", cr);
                }
                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) {
                        return;
                }
                /* strip weird stuff so can't trojan horse stupid terminals */
                for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
-                       ch &= 0x7f;
+                       ch = toascii(ch);
                        if (!isprint(ch) && !isspace(ch))
                                ch |= 0x40;
                        (void)fputc(ch, tp);
                        if (!isprint(ch) && !isspace(ch))
                                ch |= 0x40;
                        (void)fputc(ch, tp);
@@ -251,4 +280,5 @@ jkfprintf(tp, name, offset)
                --linecnt;
        }
        (void)fprintf(tp, "----%s\n", cr);
                --linecnt;
        }
        (void)fprintf(tp, "----%s\n", cr);
+       (void)fclose(fi);
 }
 }