1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/types.h> 33 34 #include <capsicum_helpers.h> 35 #include <ctype.h> 36 #include <err.h> 37 #include <pwd.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <paths.h> 41 #include <string.h> 42 #include <unistd.h> 43 44 static int match(const char *, const char *); 45 static void usage(void) __dead2; 46 47 int 48 main(int argc, char **argv) 49 { 50 FILE *mbox; 51 struct passwd *pwd; 52 int ch, count, newline; 53 const char *file; 54 char *sender, *p; 55 #if MAXPATHLEN > BUFSIZ 56 char buf[MAXPATHLEN]; 57 #else 58 char buf[BUFSIZ]; 59 #endif 60 61 file = sender = NULL; 62 count = -1; 63 while ((ch = getopt(argc, argv, "cf:s:")) != -1) 64 switch (ch) { 65 case 'c': 66 count = 0; 67 break; 68 case 'f': 69 file = optarg; 70 break; 71 case 's': 72 sender = optarg; 73 for (p = sender; *p; ++p) 74 *p = tolower(*p); 75 break; 76 case '?': 77 default: 78 usage(); 79 } 80 argc -= optind; 81 argv += optind; 82 83 if (file == NULL) { 84 if (argc) { 85 (void)snprintf(buf, sizeof(buf), "%s/%s", _PATH_MAILDIR, *argv); 86 file = buf; 87 } else { 88 if (!(file = getenv("MAIL"))) { 89 if (!(pwd = getpwuid(getuid()))) 90 errx(1, "no password file entry for you"); 91 file = pwd->pw_name; 92 (void)snprintf(buf, sizeof(buf), 93 "%s/%s", _PATH_MAILDIR, file); 94 file = buf; 95 } 96 } 97 } 98 99 /* read from stdin */ 100 if (strcmp(file, "-") == 0) { 101 mbox = stdin; 102 } 103 else if ((mbox = fopen(file, "r")) == NULL) { 104 errx(EXIT_FAILURE, "can't read %s", file); 105 } 106 if (caph_limit_stdio() < 0 || caph_enter() < 0) { 107 err(EXIT_FAILURE, "capsicum"); 108 } 109 for (newline = 1; fgets(buf, sizeof(buf), mbox);) { 110 if (*buf == '\n') { 111 newline = 1; 112 continue; 113 } 114 if (newline && !strncmp(buf, "From ", 5) && 115 (!sender || match(buf + 5, sender))) { 116 if (count != -1) 117 count++; 118 else 119 printf("%s", buf); 120 } 121 newline = 0; 122 } 123 if (count != -1) 124 printf("There %s %d message%s in your incoming mailbox.\n", 125 count == 1 ? "is" : "are", count, count == 1 ? "" : "s"); 126 fclose(mbox); 127 exit(EXIT_SUCCESS); 128 } 129 130 static void 131 usage(void) 132 { 133 fprintf(stderr, "usage: from [-c] [-f file] [-s sender] [user]\n"); 134 exit(EXIT_FAILURE); 135 } 136 137 static int 138 match(const char *line, const char *sender) 139 { 140 char ch, pch, first; 141 const char *p, *t; 142 143 for (first = *sender++;;) { 144 if (isspace(ch = *line)) 145 return (0); 146 ++line; 147 ch = tolower(ch); 148 if (ch != first) 149 continue; 150 for (p = sender, t = line;;) { 151 if (!(pch = *p++)) 152 return (1); 153 ch = tolower(*t); 154 t++; 155 if (ch != pch) 156 break; 157 } 158 } 159 /* NOTREACHED */ 160 } 161