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

Annotation of src/usr.bin/from/from.c, Revision 1.27

1.27    ! mestre      1: /*     $OpenBSD: from.c,v 1.26 2018/08/08 17:52:46 deraadt Exp $       */
1.1       deraadt     2: /*     $NetBSD: from.c,v 1.6 1995/09/01 01:39:10 jtc Exp $     */
                      3:
                      4: /*
                      5:  * Copyright (c) 1980, 1988, 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.
1.9       millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #include <sys/types.h>
                     34: #include <ctype.h>
                     35: #include <pwd.h>
                     36: #include <stdio.h>
                     37: #include <stdlib.h>
                     38: #include <unistd.h>
                     39: #include <paths.h>
1.7       deraadt    40: #include <string.h>
1.4       mickey     41: #include <err.h>
1.20      millert    42: #include <errno.h>
1.1       deraadt    43:
1.7       deraadt    44: int    match(char *, char *);
1.20      millert    45: char   *mail_spool(char *file, const char *user);
1.7       deraadt    46:
                     47: int
1.10      deraadt    48: main(int argc, char *argv[])
1.1       deraadt    49: {
1.20      millert    50:        int ch, newline, fflag = 0;
1.18      millert    51:        char *file, *line, *sender, *p;
                     52:        size_t linesize = 0;
                     53:        ssize_t linelen;
                     54:        FILE *fp;
1.1       deraadt    55:
1.18      millert    56:        file = line = sender = NULL;
                     57:        while ((ch = getopt(argc, argv, "f:s:")) != -1) {
1.16      okan       58:                switch(ch) {
1.1       deraadt    59:                case 'f':
1.20      millert    60:                        fflag = 1;
1.1       deraadt    61:                        file = optarg;
                     62:                        break;
                     63:                case 's':
                     64:                        sender = optarg;
                     65:                        for (p = sender; *p; ++p)
1.15      deraadt    66:                                if (isupper((unsigned char)*p))
                     67:                                        *p = tolower((unsigned char)*p);
1.1       deraadt    68:                        break;
                     69:                default:
1.13      jmc        70:                        fprintf(stderr,
                     71:                            "usage: from [-f file] [-s sender] [user]\n");
1.18      millert    72:                        exit(EXIT_FAILURE);
1.1       deraadt    73:                }
1.18      millert    74:        }
1.1       deraadt    75:        argv += optind;
                     76:
1.26      deraadt    77:        if (pledge("stdio unveil rpath getpw", NULL) == -1)
                     78:                err(1, "pledge");
                     79:
                     80:        file = mail_spool(file, *argv);
                     81:
                     82:        if (unveil(file, "r") == -1)
                     83:                err(1, "unveil");
1.27    ! mestre     84:        if (pledge("stdio rpath", NULL) == -1)
1.22      deraadt    85:                err(1, "pledge");
1.23      mmcc       86:
1.20      millert    87:        if ((fp = fopen(file, "r")) == NULL) {
                     88:                if (!fflag && errno == ENOENT)
                     89:                        exit(EXIT_SUCCESS);
                     90:                err(1, "%s", file);
                     91:        }
1.24      mmcc       92:
1.22      deraadt    93:        if (pledge("stdio", NULL) == -1)
                     94:                err(1, "pledge");
1.24      mmcc       95:
1.18      millert    96:        for (newline = 1; (linelen = getline(&line, &linesize, fp)) != -1;) {
                     97:                if (*line == '\n') {
                     98:                        newline = 1;
                     99:                        continue;
                    100:                }
                    101:                if (newline && !strncmp(line, "From ", 5) &&
                    102:                    (!sender || match(line + 5, sender)))
                    103:                        printf("%s", line);
                    104:                newline = 0;
                    105:        }
                    106:        free(line);
1.25      millert   107:        if (ferror(fp))
                    108:                err(EXIT_FAILURE, "getline");
                    109:        fclose(fp);
1.18      millert   110:        exit(EXIT_SUCCESS);
                    111: }
                    112:
1.20      millert   113: char *
                    114: mail_spool(char *file, const char *user)
1.18      millert   115: {
                    116:        struct passwd *pwd;
                    117:
1.1       deraadt   118:        /*
                    119:         * We find the mailbox by:
                    120:         *      1 -f flag
1.18      millert   121:         *      2 _PATH_MAILDIR/user (from argv)
1.1       deraadt   122:         *      2 MAIL environment variable
1.18      millert   123:         *      3 _PATH_MAILDIR/user (from environment or passwd db)
1.1       deraadt   124:         */
1.18      millert   125:        if (file == NULL) {
                    126:                if (user == NULL) {
                    127:                        if ((file = getenv("MAIL")) == NULL) {
                    128:                                if ((user = getenv("LOGNAME")) == NULL &&
                    129:                                    (user = getenv("USER")) == NULL) {
                    130:                                        if (!(pwd = getpwuid(getuid())))
                    131:                                                errx(1, "no password file "
                    132:                                                    "entry for you");
                    133:                                        user = pwd->pw_name;
                    134:                                }
1.1       deraadt   135:                        }
1.18      millert   136:                }
                    137:                if (file == NULL) {
1.20      millert   138:                        if (asprintf(&file, "%s/%s", _PATH_MAILDIR, user) == -1)
1.18      millert   139:                                err(1, NULL);
1.1       deraadt   140:                }
                    141:        }
1.20      millert   142:        return(file);
1.1       deraadt   143: }
                    144:
1.7       deraadt   145: int
1.10      deraadt   146: match(char *line, char *sender)
1.1       deraadt   147: {
1.8       mpech     148:        char ch, pch, first, *p, *t;
1.1       deraadt   149:
                    150:        for (first = *sender++;;) {
1.15      deraadt   151:                if (isspace((unsigned char)(ch = *line)))
1.1       deraadt   152:                        return(0);
                    153:                ++line;
1.15      deraadt   154:                if (isupper((unsigned char)ch))
                    155:                        ch = tolower((unsigned char)ch);
1.1       deraadt   156:                if (ch != first)
                    157:                        continue;
                    158:                for (p = sender, t = line;;) {
                    159:                        if (!(pch = *p++))
                    160:                                return(1);
1.15      deraadt   161:                        if (isupper((unsigned char)(ch = *t++)))
                    162:                                ch = tolower((unsigned char)ch);
1.1       deraadt   163:                        if (ch != pch)
                    164:                                break;
                    165:                }
                    166:        }
                    167:        /* NOTREACHED */
                    168: }