Annotation of 43BSDReno/libexec/comsat/comsat.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1980 Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted provided
        !             6:  * that: (1) source distributions retain this entire copyright notice and
        !             7:  * comment, and (2) distributions including binaries display the following
        !             8:  * acknowledgement:  ``This product includes software developed by the
        !             9:  * University of California, Berkeley and its contributors'' in the
        !            10:  * documentation or other materials provided with the distribution and in
        !            11:  * all advertising materials mentioning features or use of this software.
        !            12:  * Neither the name of the University nor the names of its contributors may
        !            13:  * be used to endorse or promote products derived from this software without
        !            14:  * specific prior written permission.
        !            15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
        !            16:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
        !            17:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            18:  */
        !            19: 
        !            20: #ifndef lint
        !            21: char copyright[] =
        !            22: "@(#) Copyright (c) 1980 Regents of the University of California.\n\
        !            23:  All rights reserved.\n";
        !            24: #endif /* not lint */
        !            25: 
        !            26: #ifndef lint
        !            27: static char sccsid[] = "@(#)comsat.c   5.22 (Berkeley) 6/24/90";
        !            28: #endif /* not lint */
        !            29: 
        !            30: #include <sys/param.h>
        !            31: #include <sys/socket.h>
        !            32: #include <sys/stat.h>
        !            33: #include <sys/file.h>
        !            34: #include <sys/wait.h>
        !            35: 
        !            36: #include <netinet/in.h>
        !            37: 
        !            38: #include <stdio.h>
        !            39: #include <sgtty.h>
        !            40: #include <utmp.h>
        !            41: #include <signal.h>
        !            42: #include <errno.h>
        !            43: #include <netdb.h>
        !            44: #include <syslog.h>
        !            45: #include <ctype.h>
        !            46: #include <string.h>
        !            47: #include <paths.h>
        !            48: 
        !            49: int    debug = 0;
        !            50: #define        dsyslog if (debug) syslog
        !            51: 
        !            52: #define MAXIDLE        120
        !            53: 
        !            54: char   hostname[MAXHOSTNAMELEN];
        !            55: struct utmp *utmp = NULL;
        !            56: time_t lastmsgtime, time();
        !            57: int    nutmp, uf;
        !            58: 
        !            59: /* ARGSUSED */
        !            60: main(argc, argv)
        !            61:        int argc;
        !            62:        char **argv;
        !            63: {
        !            64:        extern int errno;
        !            65:        register int cc;
        !            66:        char msgbuf[100];
        !            67:        struct sockaddr_in from;
        !            68:        int fromlen;
        !            69:        void onalrm(), reapchildren();
        !            70: 
        !            71:        /* verify proper invocation */
        !            72:        fromlen = sizeof(from);
        !            73:        if (getsockname(0, &from, &fromlen) < 0) {
        !            74:                (void)fprintf(stderr,
        !            75:                    "comsat: getsockname: %s.\n", strerror(errno));
        !            76:                exit(1);
        !            77:        }
        !            78:        openlog("comsat", LOG_PID, LOG_DAEMON);
        !            79:        if (chdir(_PATH_MAILDIR)) {
        !            80:                syslog(LOG_ERR, "chdir: %s: %m", _PATH_MAILDIR);
        !            81:                exit(1);
        !            82:        }
        !            83:        if ((uf = open(_PATH_UTMP, O_RDONLY, 0)) < 0) {
        !            84:                syslog(LOG_ERR, ".main: %s: %m", _PATH_UTMP);
        !            85:                (void) recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
        !            86:                exit(1);
        !            87:        }
        !            88:        (void)time(&lastmsgtime);
        !            89:        (void)gethostname(hostname, sizeof(hostname));
        !            90:        onalrm();
        !            91:        (void)signal(SIGALRM, onalrm);
        !            92:        (void)signal(SIGTTOU, SIG_IGN);
        !            93:        (void)signal(SIGCHLD, reapchildren);
        !            94:        for (;;) {
        !            95:                cc = recv(0, msgbuf, sizeof(msgbuf) - 1, 0);
        !            96:                if (cc <= 0) {
        !            97:                        if (errno != EINTR)
        !            98:                                sleep(1);
        !            99:                        errno = 0;
        !           100:                        continue;
        !           101:                }
        !           102:                if (!nutmp)             /* no one has logged in yet */
        !           103:                        continue;
        !           104:                sigblock(sigmask(SIGALRM));
        !           105:                msgbuf[cc] = 0;
        !           106:                (void)time(&lastmsgtime);
        !           107:                mailfor(msgbuf);
        !           108:                sigsetmask(0L);
        !           109:        }
        !           110: }
        !           111: 
        !           112: void
        !           113: reapchildren()
        !           114: {
        !           115:        while (wait3((union wait *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
        !           116: }
        !           117: 
        !           118: void
        !           119: onalrm()
        !           120: {
        !           121:        static u_int utmpsize;          /* last malloced size for utmp */
        !           122:        static u_int utmpmtime;         /* last modification time for utmp */
        !           123:        struct stat statbf;
        !           124:        off_t lseek();
        !           125:        char *malloc(), *realloc();
        !           126: 
        !           127:        if (time((time_t *)NULL) - lastmsgtime >= MAXIDLE)
        !           128:                exit(0);
        !           129:        (void)alarm((u_int)15);
        !           130:        (void)fstat(uf, &statbf);
        !           131:        if (statbf.st_mtime > utmpmtime) {
        !           132:                utmpmtime = statbf.st_mtime;
        !           133:                if (statbf.st_size > utmpsize) {
        !           134:                        utmpsize = statbf.st_size + 10 * sizeof(struct utmp);
        !           135:                        if (utmp)
        !           136:                                utmp = (struct utmp *)realloc((char *)utmp, utmpsize);
        !           137:                        else
        !           138:                                utmp = (struct utmp *)malloc(utmpsize);
        !           139:                        if (!utmp) {
        !           140:                                syslog(LOG_ERR, "malloc failed");
        !           141:                                exit(1);
        !           142:                        }
        !           143:                }
        !           144:                (void)lseek(uf, 0L, L_SET);
        !           145:                nutmp = read(uf, utmp, (int)statbf.st_size)/sizeof(struct utmp);
        !           146:        }
        !           147: }
        !           148: 
        !           149: mailfor(name)
        !           150:        char *name;
        !           151: {
        !           152:        register struct utmp *utp = &utmp[nutmp];
        !           153:        register char *cp;
        !           154:        off_t offset;
        !           155: 
        !           156:        if (!(cp = index(name, '@')))
        !           157:                return;
        !           158:        *cp = '\0';
        !           159:        offset = atoi(cp + 1);
        !           160:        while (--utp >= utmp)
        !           161:                if (!strncmp(utp->ut_name, name, sizeof(utmp[0].ut_name)))
        !           162:                        notify(utp, offset);
        !           163: }
        !           164: 
        !           165: static char *cr;
        !           166: 
        !           167: notify(utp, offset)
        !           168:        register struct utmp *utp;
        !           169:        off_t offset;
        !           170: {
        !           171:        static char tty[20] = _PATH_DEV;
        !           172:        struct sgttyb gttybuf;
        !           173:        struct stat stb;
        !           174:        FILE *tp;
        !           175:        char name[sizeof(utmp[0].ut_name) + 1];
        !           176: 
        !           177:        (void)strncpy(tty + sizeof(_PATH_DEV) - 1, utp->ut_line,
        !           178:            sizeof(utp->ut_line));
        !           179:        if (stat(tty, &stb) || !(stb.st_mode & S_IEXEC)) {
        !           180:                dsyslog(LOG_DEBUG, "%s: wrong mode on %s", utp->ut_name, tty);
        !           181:                return;
        !           182:        }
        !           183:        dsyslog(LOG_DEBUG, "notify %s on %s\n", utp->ut_name, tty);
        !           184:        if (fork())
        !           185:                return;
        !           186:        (void)signal(SIGALRM, SIG_DFL);
        !           187:        (void)alarm((u_int)30);
        !           188:        if ((tp = fopen(tty, "w")) == NULL) {
        !           189:                dsyslog(LOG_ERR, "fopen of tty %s failed", tty);
        !           190:                _exit(-1);
        !           191:        }
        !           192:        (void)ioctl(fileno(tp), TIOCGETP, &gttybuf);
        !           193:        cr = (gttybuf.sg_flags&CRMOD) && !(gttybuf.sg_flags&RAW) ?
        !           194:            "\n" : "\n\r";
        !           195:        (void)strncpy(name, utp->ut_name, sizeof(utp->ut_name));
        !           196:        name[sizeof(name) - 1] = '\0';
        !           197:        (void)fprintf(tp, "%s\007New mail for %s@%.*s\007 has arrived:%s----%s",
        !           198:            cr, name, sizeof(hostname), hostname, cr, cr);
        !           199:        jkfprintf(tp, name, offset);
        !           200:        (void)fclose(tp);
        !           201:        _exit(0);
        !           202: }
        !           203: 
        !           204: jkfprintf(tp, name, offset)
        !           205:        register FILE *tp;
        !           206:        char name[];
        !           207:        off_t offset;
        !           208: {
        !           209:        register char *cp, ch;
        !           210:        register FILE *fi;
        !           211:        register int linecnt, charcnt, inheader;
        !           212:        char line[BUFSIZ];
        !           213:        off_t fseek();
        !           214: 
        !           215:        if ((fi = fopen(name, "r")) == NULL)
        !           216:                return;
        !           217:        (void)fseek(fi, offset, L_SET);
        !           218:        /*
        !           219:         * Print the first 7 lines or 560 characters of the new mail
        !           220:         * (whichever comes first).  Skip header crap other than
        !           221:         * From, Subject, To, and Date.
        !           222:         */
        !           223:        linecnt = 7;
        !           224:        charcnt = 560;
        !           225:        inheader = 1;
        !           226:        while (fgets(line, sizeof(line), fi) != NULL) {
        !           227:                if (inheader) {
        !           228:                        if (line[0] == '\n') {
        !           229:                                inheader = 0;
        !           230:                                continue;
        !           231:                        }
        !           232:                        if (line[0] == ' ' || line[0] == '\t' ||
        !           233:                            strncmp(line, "From:", 5) &&
        !           234:                            strncmp(line, "Subject:", 8))
        !           235:                                continue;
        !           236:                }
        !           237:                if (linecnt <= 0 || charcnt <= 0) {
        !           238:                        (void)fprintf(tp, "...more...%s", cr);
        !           239:                        return;
        !           240:                }
        !           241:                /* strip weird stuff so can't trojan horse stupid terminals */
        !           242:                for (cp = line; (ch = *cp) && ch != '\n'; ++cp, --charcnt) {
        !           243:                        ch = toascii(ch);
        !           244:                        if (!isprint(ch) && !isspace(ch))
        !           245:                                ch |= 0x40;
        !           246:                        (void)fputc(ch, tp);
        !           247:                }
        !           248:                (void)fputs(cr, tp);
        !           249:                --linecnt;
        !           250:        }
        !           251:        (void)fprintf(tp, "----%s\n", cr);
        !           252: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.