[BACK]Return to vacation.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / vacation

Annotation of src/usr.bin/vacation/vacation.c, Revision 1.16

1.16    ! mpech       1: /*     $OpenBSD: vacation.c,v 1.15 2001/11/19 19:02:17 mpech Exp $     */
1.1       deraadt     2: /*     $NetBSD: vacation.c,v 1.7 1995/04/29 05:58:27 cgd Exp $ */
                      3:
                      4: /*
                      5:  * Copyright (c) 1983, 1987, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: static char copyright[] =
                     39: "@(#) Copyright (c) 1983, 1987, 1993\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
                     45: static char sccsid[] = "@(#)vacation.c 8.2 (Berkeley) 1/26/94";
                     46: #endif
1.16    ! mpech      47: static char rcsid[] = "$OpenBSD: vacation.c,v 1.15 2001/11/19 19:02:17 mpech Exp $";
1.1       deraadt    48: #endif /* not lint */
                     49:
                     50: /*
                     51: **  Vacation
                     52: **  Copyright (c) 1983  Eric P. Allman
                     53: **  Berkeley, California
                     54: */
                     55:
                     56: #include <sys/param.h>
                     57: #include <sys/stat.h>
                     58: #include <fcntl.h>
                     59: #include <pwd.h>
                     60: #include <db.h>
                     61: #include <time.h>
                     62: #include <syslog.h>
                     63: #include <tzfile.h>
                     64: #include <errno.h>
                     65: #include <unistd.h>
                     66: #include <stdio.h>
                     67: #include <ctype.h>
                     68: #include <stdlib.h>
                     69: #include <string.h>
                     70: #include <paths.h>
                     71:
                     72: /*
                     73:  *  VACATION -- return a message to the sender when on vacation.
                     74:  *
                     75:  *     This program is invoked as a message receiver.  It returns a
                     76:  *     message specified by the user to whomever sent the mail, taking
                     77:  *     care not to return a message too often to prevent "I am on
                     78:  *     vacation" loops.
                     79:  */
                     80:
                     81: #define        MAXLINE 1024                    /* max line from mail header */
                     82: #define        VDB     ".vacation.db"          /* dbm's database */
                     83: #define        VMSG    ".vacation.msg"         /* vacation message */
                     84:
                     85: typedef struct alias {
                     86:        struct alias *next;
                     87:        char *name;
                     88: } ALIAS;
                     89: ALIAS *names;
                     90:
                     91: DB *db;
                     92: char from[MAXLINE];
1.12      marc       93: char subj[MAXLINE];
1.1       deraadt    94:
                     95: int junkmail __P((void));
                     96: int nsearch __P((char *, char *));
                     97: void readheaders __P((void));
                     98: int recent __P((void));
                     99: void sendmessage __P((char *));
                    100: void setinterval __P((time_t));
                    101: void setreply __P((void));
                    102: void usage __P((void));
                    103:
                    104: int
                    105: main(argc, argv)
                    106:        int argc;
                    107:        char **argv;
                    108: {
                    109:        struct passwd *pw;
1.9       millert   110:        struct stat sb;
1.1       deraadt   111:        ALIAS *cur;
                    112:        time_t interval;
1.9       millert   113:        int ch, iflag, flags;
1.1       deraadt   114:
                    115:        opterr = iflag = 0;
                    116:        interval = -1;
1.4       millert   117:        while ((ch = getopt(argc, argv, "a:Iir:")) != -1)
1.1       deraadt   118:                switch((char)ch) {
                    119:                case 'a':                       /* alias */
                    120:                        if (!(cur = (ALIAS *)malloc((u_int)sizeof(ALIAS))))
                    121:                                break;
                    122:                        cur->name = optarg;
                    123:                        cur->next = names;
                    124:                        names = cur;
                    125:                        break;
                    126:                case 'I':                       /* backward compatible */
                    127:                case 'i':                       /* init the database */
                    128:                        iflag = 1;
                    129:                        break;
                    130:                case 'r':
                    131:                        if (isdigit(*optarg)) {
                    132:                                interval = atol(optarg) * SECSPERDAY;
                    133:                                if (interval < 0)
                    134:                                        usage();
                    135:                        }
                    136:                        else
                    137:                                interval = (time_t)LONG_MAX;    /* XXX */
                    138:                        break;
                    139:                case '?':
                    140:                default:
                    141:                        usage();
                    142:                }
                    143:        argc -= optind;
                    144:        argv += optind;
                    145:
                    146:        if (argc != 1) {
                    147:                if (!iflag)
                    148:                        usage();
                    149:                if (!(pw = getpwuid(getuid()))) {
                    150:                        syslog(LOG_ERR,
1.11      marc      151:                            "no such user uid %u.", getuid());
1.1       deraadt   152:                        exit(1);
                    153:                }
                    154:        }
                    155:        else if (!(pw = getpwnam(*argv))) {
1.11      marc      156:                syslog(LOG_ERR, "no such user %s.", *argv);
1.1       deraadt   157:                exit(1);
                    158:        }
                    159:        if (chdir(pw->pw_dir)) {
                    160:                syslog(LOG_NOTICE,
1.11      marc      161:                    "no such directory %s.", pw->pw_dir);
1.1       deraadt   162:                exit(1);
                    163:        }
                    164:
1.9       millert   165:        /*
                    166:         * dbopen(3) can not deal with a zero-length file w/o O_TRUNC.
                    167:         */
                    168:        if (iflag == 1 || (stat(VDB, &sb) == 0 && sb.st_size == (off_t)0))
                    169:                flags = O_CREAT|O_RDWR|O_TRUNC;
                    170:        else
                    171:                flags = O_CREAT|O_RDWR;
                    172:
                    173:        db = dbopen(VDB, flags, S_IRUSR|S_IWUSR, DB_HASH, NULL);
1.1       deraadt   174:        if (!db) {
1.11      marc      175:                syslog(LOG_NOTICE, "%s: %m", VDB);
1.1       deraadt   176:                exit(1);
                    177:        }
                    178:
                    179:        if (interval != -1)
                    180:                setinterval(interval);
                    181:
                    182:        if (iflag) {
                    183:                (void)(db->close)(db);
                    184:                exit(0);
                    185:        }
                    186:
                    187:        if (!(cur = malloc((u_int)sizeof(ALIAS))))
                    188:                exit(1);
                    189:        cur->name = pw->pw_name;
                    190:        cur->next = names;
                    191:        names = cur;
                    192:
                    193:        readheaders();
                    194:        if (!recent()) {
                    195:                setreply();
                    196:                (void)(db->close)(db);
                    197:                sendmessage(pw->pw_name);
                    198:        }
                    199:        else
                    200:                (void)(db->close)(db);
                    201:        exit(0);
                    202:        /* NOTREACHED */
                    203: }
                    204:
                    205: /*
                    206:  * readheaders --
                    207:  *     read mail headers
                    208:  */
                    209: void
                    210: readheaders()
                    211: {
1.15      mpech     212:        ALIAS *cur;
                    213:        char *p;
1.1       deraadt   214:        int tome, cont;
                    215:        char buf[MAXLINE];
                    216:
                    217:        cont = tome = 0;
                    218:        while (fgets(buf, sizeof(buf), stdin) && *buf != '\n')
                    219:                switch(*buf) {
                    220:                case 'F':               /* "From " */
1.16    ! mpech     221:                case 'f':
1.1       deraadt   222:                        cont = 0;
1.16    ! mpech     223:                        if (!strncasecmp(buf, "From ", 5)) {
1.12      marc      224:                                for (p = buf + 5; *p && *p != ' '; ++p)
                    225:                                        ;
1.1       deraadt   226:                                *p = '\0';
                    227:                                (void)strcpy(from, buf + 5);
1.13      pjanzen   228:                                if ((p = strchr(from, '\n')))
1.1       deraadt   229:                                        *p = '\0';
                    230:                                if (junkmail())
                    231:                                        exit(0);
                    232:                        }
                    233:                        break;
1.11      marc      234:                case 'R':               /* "Return-Path:" */
1.16    ! mpech     235:                case 'r':
1.11      marc      236:                        cont = 0;
                    237:                        if (strncasecmp(buf, "Return-Path:",
                    238:                                        sizeof("Return-Path:")-1) ||
1.13      pjanzen   239:                            (buf[12] != ' ' && buf[12] != '\t'))
1.11      marc      240:                                break;
1.12      marc      241:                        for (p = buf + 12; *p && isspace(*p); ++p)
                    242:                                ;
1.11      marc      243:                        if (strlcpy(from, p, sizeof from ) > sizeof from) {
                    244:                                syslog(LOG_NOTICE,
                    245:                                       "Return-Path %s exceeds limits", p);
                    246:                                exit(1);
                    247:                        }
1.13      pjanzen   248:                        if ((p = strchr(from, '\n')))
1.11      marc      249:                                *p = '\0';
                    250:                        if (junkmail())
                    251:                                exit(0);
                    252:                        break;
1.1       deraadt   253:                case 'P':               /* "Precedence:" */
1.16    ! mpech     254:                case 'p':
1.1       deraadt   255:                        cont = 0;
                    256:                        if (strncasecmp(buf, "Precedence", 10) ||
1.13      pjanzen   257:                            (buf[10] != ':' && buf[10] != ' ' &&
                    258:                            buf[10] != '\t'))
1.1       deraadt   259:                                break;
1.5       millert   260:                        if (!(p = strchr(buf, ':')))
1.1       deraadt   261:                                break;
                    262:                        while (*++p && isspace(*p));
                    263:                        if (!*p)
                    264:                                break;
                    265:                        if (!strncasecmp(p, "junk", 4) ||
                    266:                            !strncasecmp(p, "bulk", 4) ||
                    267:                            !strncasecmp(p, "list", 4))
                    268:                                exit(0);
                    269:                        break;
1.12      marc      270:                case 'S':               /* Subject: */
1.16    ! mpech     271:                case 's':
1.12      marc      272:                        cont = 0;
                    273:                        if (strncasecmp(buf, "Subject:",
                    274:                                        sizeof("Subject:")-1) ||
1.13      pjanzen   275:                            (buf[8] != ' ' && buf[8] != '\t'))
1.12      marc      276:                                break;
                    277:                        for (p = buf + 8; *p && isspace(*p); ++p)
                    278:                                ;
                    279:                        if (strlcpy(subj, p, sizeof subj ) > sizeof subj) {
                    280:                                syslog(LOG_NOTICE,
                    281:                                       "Subject %s exceeds limits", p);
                    282:                                exit(1);
                    283:                        }
1.13      pjanzen   284:                        if ((p = strchr(subj, '\n')))
1.12      marc      285:                                *p = '\0';
                    286:                        break;
1.1       deraadt   287:                case 'C':               /* "Cc:" */
1.16    ! mpech     288:                case 'c':
        !           289:                        if (strncasecmp(buf, "Cc:", 3))
1.1       deraadt   290:                                break;
                    291:                        cont = 1;
                    292:                        goto findme;
                    293:                case 'T':               /* "To:" */
1.16    ! mpech     294:                case 't':
        !           295:                        if (strncasecmp(buf, "To:", 3))
1.1       deraadt   296:                                break;
                    297:                        cont = 1;
                    298:                        goto findme;
                    299:                default:
                    300:                        if (!isspace(*buf) || !cont || tome) {
                    301:                                cont = 0;
                    302:                                break;
                    303:                        }
                    304: findme:                        for (cur = names; !tome && cur; cur = cur->next)
                    305:                                tome += nsearch(cur->name, buf);
                    306:                }
                    307:        if (!tome)
                    308:                exit(0);
                    309:        if (!*from) {
1.11      marc      310:                syslog(LOG_NOTICE, "no initial \"From\" or \"Return-Path\"line.");
1.1       deraadt   311:                exit(1);
                    312:        }
                    313: }
                    314:
                    315: /*
                    316:  * nsearch --
                    317:  *     do a nice, slow, search of a string for a substring.
                    318:  */
                    319: int
                    320: nsearch(name, str)
1.15      mpech     321:        char *name, *str;
1.1       deraadt   322: {
1.15      mpech     323:        int len;
1.1       deraadt   324:
                    325:        for (len = strlen(name); *str; ++str)
1.13      pjanzen   326:                if (!strncasecmp(name, str, len))
1.1       deraadt   327:                        return(1);
                    328:        return(0);
                    329: }
                    330:
                    331: /*
                    332:  * junkmail --
                    333:  *     read the header and return if automagic/junk/bulk/list mail
                    334:  */
                    335: int
                    336: junkmail()
                    337: {
                    338:        static struct ignore {
                    339:                char    *name;
                    340:                int     len;
                    341:        } ignore[] = {
1.13      pjanzen   342:                { "-request", 8 },
                    343:                { "postmaster", 10 },
                    344:                { "uucp", 4 },
                    345:                { "mailer-daemon", 13 },
                    346:                { "mailer", 6 },
                    347:                { "-relay", 6 },
                    348:                { NULL, 0 }
1.1       deraadt   349:        };
1.15      mpech     350:        struct ignore *cur;
                    351:        int len;
                    352:        char *p;
1.1       deraadt   353:
                    354:        /*
                    355:         * This is mildly amusing, and I'm not positive it's right; trying
                    356:         * to find the "real" name of the sender, assuming that addresses
                    357:         * will be some variant of:
                    358:         *
                    359:         * From site!site!SENDER%site.domain%site.domain@site.domain
                    360:         */
1.5       millert   361:        if (!(p = strchr(from, '%')))
                    362:                if (!(p = strchr(from, '@'))) {
1.13      pjanzen   363:                        if ((p = strrchr(from, '!')))
1.1       deraadt   364:                                ++p;
                    365:                        else
                    366:                                p = from;
1.12      marc      367:                        for (; *p; ++p)
                    368:                                ;
1.1       deraadt   369:                }
                    370:        len = p - from;
                    371:        for (cur = ignore; cur->name; ++cur)
                    372:                if (len >= cur->len &&
                    373:                    !strncasecmp(cur->name, p - cur->len, cur->len))
                    374:                        return(1);
                    375:        return(0);
                    376: }
                    377:
                    378: #define        VIT     "__VACATION__INTERVAL__TIMER__"
                    379:
                    380: /*
                    381:  * recent --
                    382:  *     find out if user has gotten a vacation message recently.
                    383:  *     use bcopy for machines with alignment restrictions
                    384:  */
                    385: int
                    386: recent()
                    387: {
                    388:        DBT key, data;
                    389:        time_t then, next;
                    390:
                    391:        /* get interval time */
                    392:        key.data = VIT;
                    393:        key.size = sizeof(VIT);
                    394:        if ((db->get)(db, &key, &data, 0))
                    395:                next = SECSPERDAY * DAYSPERWEEK;
                    396:        else
                    397:                bcopy(data.data, &next, sizeof(next));
                    398:
                    399:        /* get record for this address */
                    400:        key.data = from;
                    401:        key.size = strlen(from);
                    402:        if (!(db->get)(db, &key, &data, 0)) {
                    403:                bcopy(data.data, &then, sizeof(then));
                    404:                if (next == (time_t)LONG_MAX ||                 /* XXX */
                    405:                    then + next > time(NULL))
                    406:                        return(1);
                    407:        }
                    408:        return(0);
                    409: }
                    410:
                    411: /*
                    412:  * setinterval --
                    413:  *     store the reply interval
                    414:  */
                    415: void
                    416: setinterval(interval)
                    417:        time_t interval;
                    418: {
                    419:        DBT key, data;
                    420:
                    421:        key.data = VIT;
                    422:        key.size = sizeof(VIT);
                    423:        data.data = &interval;
                    424:        data.size = sizeof(interval);
                    425:        (void)(db->put)(db, &key, &data, 0);
                    426: }
                    427:
                    428: /*
                    429:  * setreply --
                    430:  *     store that this user knows about the vacation.
                    431:  */
                    432: void
                    433: setreply()
                    434: {
                    435:        DBT key, data;
                    436:        time_t now;
                    437:
                    438:        key.data = from;
                    439:        key.size = strlen(from);
                    440:        (void)time(&now);
                    441:        data.data = &now;
                    442:        data.size = sizeof(now);
                    443:        (void)(db->put)(db, &key, &data, 0);
                    444: }
                    445:
                    446: /*
                    447:  * sendmessage --
                    448:  *     exec sendmail to send the vacation file to sender
                    449:  */
                    450: void
                    451: sendmessage(myname)
                    452:        char *myname;
                    453: {
                    454:        FILE *mfp, *sfp;
                    455:        int i;
                    456:        int pvect[2];
                    457:        char buf[MAXLINE];
                    458:
                    459:        mfp = fopen(VMSG, "r");
                    460:        if (mfp == NULL) {
1.11      marc      461:                syslog(LOG_NOTICE, "no ~%s/%s file.", myname, VMSG);
1.1       deraadt   462:                exit(1);
                    463:        }
                    464:        if (pipe(pvect) < 0) {
1.11      marc      465:                syslog(LOG_ERR, "pipe: %m");
1.1       deraadt   466:                exit(1);
                    467:        }
                    468:        i = vfork();
                    469:        if (i < 0) {
1.11      marc      470:                syslog(LOG_ERR, "fork: %m");
1.1       deraadt   471:                exit(1);
                    472:        }
                    473:        if (i == 0) {
                    474:                dup2(pvect[0], 0);
                    475:                close(pvect[0]);
                    476:                close(pvect[1]);
1.10      deraadt   477:                close(fileno(mfp));
1.8       deraadt   478:                execl(_PATH_SENDMAIL, "sendmail", "-f", myname, "--",
1.14      deraadt   479:                    from, (char *)NULL);
1.11      marc      480:                syslog(LOG_ERR, "can't exec %s: %m", _PATH_SENDMAIL);
1.3       deraadt   481:                _exit(1);
1.1       deraadt   482:        }
                    483:        close(pvect[0]);
                    484:        sfp = fdopen(pvect[1], "w");
                    485:        fprintf(sfp, "To: %s\n", from);
1.12      marc      486:        while (fgets(buf, sizeof buf, mfp)) {
                    487:                char *s = strstr(buf, "$SUBJECT");
                    488:                if ( s ) {
                    489:                        *s = 0;
                    490:                        fputs(buf, sfp);
                    491:                        fputs(subj, sfp);
                    492:                        fputs(s+8, sfp);
                    493:                } else {
                    494:                        fputs(buf, sfp);
                    495:                }
                    496:        }
1.1       deraadt   497:        fclose(mfp);
                    498:        fclose(sfp);
                    499: }
                    500:
                    501: void
                    502: usage()
                    503: {
1.9       millert   504:        syslog(LOG_NOTICE, "uid %u: usage: vacation [-i] [-a alias] login",
1.1       deraadt   505:            getuid());
                    506:        exit(1);
                    507: }