Home | History | Annotate | Line # | Download | only in mail.local
mail.local.c revision 1.3
      1  1.1      cgd /*-
      2  1.1      cgd  * Copyright (c) 1990 The Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.1      cgd char copyright[] =
     36  1.1      cgd "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
     37  1.1      cgd  All rights reserved.\n";
     38  1.1      cgd #endif /* not lint */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.1      cgd static char sccsid[] = "@(#)mail.local.c	5.6 (Berkeley) 6/19/91";
     42  1.1      cgd #endif /* not lint */
     43  1.1      cgd 
     44  1.1      cgd #include <sys/param.h>
     45  1.1      cgd #include <sys/stat.h>
     46  1.1      cgd #include <sys/socket.h>
     47  1.1      cgd #include <netinet/in.h>
     48  1.1      cgd #include <syslog.h>
     49  1.1      cgd #include <fcntl.h>
     50  1.1      cgd #include <netdb.h>
     51  1.1      cgd #include <pwd.h>
     52  1.1      cgd #include <time.h>
     53  1.1      cgd #include <unistd.h>
     54  1.1      cgd #include <errno.h>
     55  1.1      cgd #include <stdio.h>
     56  1.1      cgd #include <stdlib.h>
     57  1.1      cgd #include <string.h>
     58  1.1      cgd #include "pathnames.h"
     59  1.1      cgd 
     60  1.1      cgd #define	FATAL		1
     61  1.1      cgd #define	NOTFATAL	0
     62  1.1      cgd 
     63  1.2  deraadt int	deliver __P((int, char *, int));
     64  1.1      cgd void	err __P((int, const char *, ...));
     65  1.1      cgd void	notifybiff __P((char *));
     66  1.1      cgd int	store __P((char *));
     67  1.1      cgd void	usage __P((void));
     68  1.1      cgd 
     69  1.1      cgd main(argc, argv)
     70  1.1      cgd 	int argc;
     71  1.1      cgd 	char **argv;
     72  1.1      cgd {
     73  1.1      cgd 	extern int optind;
     74  1.1      cgd 	extern char *optarg;
     75  1.1      cgd 	struct passwd *pw;
     76  1.2  deraadt 	int ch, fd, eval, lockfile=0;
     77  1.1      cgd 	uid_t uid;
     78  1.1      cgd 	char *from;
     79  1.1      cgd 
     80  1.1      cgd 	openlog("mail.local", LOG_PERROR, LOG_MAIL);
     81  1.1      cgd 
     82  1.1      cgd 	from = NULL;
     83  1.1      cgd 	while ((ch = getopt(argc, argv, "df:r:")) != EOF)
     84  1.1      cgd 		switch(ch) {
     85  1.1      cgd 		case 'd':		/* backward compatible */
     86  1.1      cgd 			break;
     87  1.1      cgd 		case 'f':
     88  1.1      cgd 		case 'r':		/* backward compatible */
     89  1.1      cgd 			if (from)
     90  1.1      cgd 			    err(FATAL, "multiple -f options");
     91  1.1      cgd 			from = optarg;
     92  1.1      cgd 			break;
     93  1.2  deraadt 		case 'l':
     94  1.2  deraadt 			lockfile++;
     95  1.2  deraadt 			break;
     96  1.1      cgd 		case '?':
     97  1.1      cgd 		default:
     98  1.1      cgd 			usage();
     99  1.1      cgd 		}
    100  1.1      cgd 	argc -= optind;
    101  1.1      cgd 	argv += optind;
    102  1.1      cgd 
    103  1.1      cgd 	if (!*argv)
    104  1.1      cgd 		usage();
    105  1.1      cgd 
    106  1.1      cgd 	/*
    107  1.1      cgd 	 * If from not specified, use the name from getlogin() if the
    108  1.1      cgd 	 * uid matches, otherwise, use the name from the password file
    109  1.1      cgd 	 * corresponding to the uid.
    110  1.1      cgd 	 */
    111  1.1      cgd 	uid = getuid();
    112  1.1      cgd 	if (!from && (!(from = getlogin()) ||
    113  1.1      cgd 	    !(pw = getpwnam(from)) || pw->pw_uid != uid))
    114  1.1      cgd 		from = (pw = getpwuid(uid)) ? pw->pw_name : "???";
    115  1.1      cgd 
    116  1.1      cgd 	fd = store(from);
    117  1.1      cgd 	for (eval = 0; *argv; ++argv)
    118  1.2  deraadt 		eval |= deliver(fd, *argv, lockfile);
    119  1.1      cgd 	exit(eval);
    120  1.1      cgd }
    121  1.1      cgd 
    122  1.1      cgd store(from)
    123  1.1      cgd 	char *from;
    124  1.1      cgd {
    125  1.1      cgd 	FILE *fp;
    126  1.1      cgd 	time_t tval;
    127  1.1      cgd 	int fd, eline;
    128  1.1      cgd 	char *tn, line[2048];
    129  1.1      cgd 
    130  1.1      cgd 	tn = strdup(_PATH_LOCTMP);
    131  1.1      cgd 	if ((fd = mkstemp(tn)) == -1 || !(fp = fdopen(fd, "w+")))
    132  1.1      cgd 		err(FATAL, "unable to open temporary file");
    133  1.1      cgd 	(void)unlink(tn);
    134  1.1      cgd 	free(tn);
    135  1.1      cgd 
    136  1.1      cgd 	(void)time(&tval);
    137  1.1      cgd 	(void)fprintf(fp, "From %s %s", from, ctime(&tval));
    138  1.1      cgd 
    139  1.1      cgd 	line[0] = '\0';
    140  1.1      cgd 	for (eline = 1; fgets(line, sizeof(line), stdin);) {
    141  1.1      cgd 		if (line[0] == '\n')
    142  1.1      cgd 			eline = 1;
    143  1.1      cgd 		else {
    144  1.1      cgd 			if (eline && line[0] == 'F' && !bcmp(line, "From ", 5))
    145  1.1      cgd 				(void)putc('>', fp);
    146  1.1      cgd 			eline = 0;
    147  1.1      cgd 		}
    148  1.1      cgd 		(void)fprintf(fp, "%s", line);
    149  1.1      cgd 		if (ferror(fp))
    150  1.1      cgd 			break;
    151  1.1      cgd 	}
    152  1.1      cgd 
    153  1.1      cgd 	/* If message not newline terminated, need an extra. */
    154  1.1      cgd 	if (!index(line, '\n'))
    155  1.1      cgd 		(void)putc('\n', fp);
    156  1.1      cgd 	/* Output a newline; note, empty messages are allowed. */
    157  1.1      cgd 	(void)putc('\n', fp);
    158  1.1      cgd 
    159  1.1      cgd 	(void)fflush(fp);
    160  1.1      cgd 	if (ferror(fp))
    161  1.1      cgd 		err(FATAL, "temporary file write error");
    162  1.1      cgd 	return(fd);
    163  1.1      cgd }
    164  1.1      cgd 
    165  1.2  deraadt deliver(fd, name, lockfile)
    166  1.1      cgd 	int fd;
    167  1.1      cgd 	char *name;
    168  1.2  deraadt 	int lockfile;
    169  1.1      cgd {
    170  1.1      cgd 	struct stat sb;
    171  1.1      cgd 	struct passwd *pw;
    172  1.2  deraadt 	int created, mbfd, nr, nw, off, rval, lfd=-1;
    173  1.2  deraadt 	char biffmsg[100], buf[8*1024], path[MAXPATHLEN], lpath[MAXPATHLEN];
    174  1.1      cgd 	off_t curoff, lseek();
    175  1.1      cgd 
    176  1.1      cgd 	/*
    177  1.1      cgd 	 * Disallow delivery to unknown names -- special mailboxes can be
    178  1.1      cgd 	 * handled in the sendmail aliases file.
    179  1.1      cgd 	 */
    180  1.1      cgd 	if (!(pw = getpwnam(name))) {
    181  1.1      cgd 		err(NOTFATAL, "unknown name: %s", name);
    182  1.1      cgd 		return(1);
    183  1.1      cgd 	}
    184  1.1      cgd 
    185  1.1      cgd 	(void)sprintf(path, "%s/%s", _PATH_MAILDIR, name);
    186  1.1      cgd 
    187  1.2  deraadt 	if(lockfile) {
    188  1.2  deraadt 		(void)sprintf(lpath, "%s/%s.lock", _PATH_MAILDIR, name);
    189  1.2  deraadt 
    190  1.2  deraadt 		if((lfd = open(lpath, O_CREAT|O_WRONLY|O_EXCL,
    191  1.2  deraadt 		    S_IRUSR|S_IWUSR)) < 0) {
    192  1.2  deraadt 			err(NOTFATAL, "%s: %s", lpath, strerror(errno));
    193  1.2  deraadt 			return(1);
    194  1.2  deraadt 		}
    195  1.2  deraadt 	}
    196  1.2  deraadt 
    197  1.1      cgd 	if (!(created = lstat(path, &sb)) &&
    198  1.1      cgd 	    (sb.st_nlink != 1 || S_ISLNK(sb.st_mode))) {
    199  1.1      cgd 		err(NOTFATAL, "%s: linked file", path);
    200  1.1      cgd 		return(1);
    201  1.1      cgd 	}
    202  1.2  deraadt 	if((mbfd = open(path, O_APPEND|O_WRONLY|O_EXLOCK,
    203  1.2  deraadt 	    S_IRUSR|S_IWUSR)) < 0) {
    204  1.2  deraadt 		if ((mbfd = open(path, O_APPEND|O_CREAT|O_WRONLY|O_EXLOCK,
    205  1.2  deraadt 		    S_IRUSR|S_IWUSR)) < 0) {
    206  1.1      cgd 		err(NOTFATAL, "%s: %s", path, strerror(errno));
    207  1.1      cgd 		return(1);
    208  1.1      cgd 	}
    209  1.1      cgd 	}
    210  1.1      cgd 
    211  1.1      cgd 	curoff = lseek(mbfd, 0L, SEEK_END);
    212  1.1      cgd 	(void)sprintf(biffmsg, "%s@%ld\n", name, curoff);
    213  1.1      cgd 	if (lseek(fd, 0L, SEEK_SET) == (off_t)-1) {
    214  1.1      cgd 		err(FATAL, "temporary file: %s", strerror(errno));
    215  1.1      cgd 		rval = 1;
    216  1.1      cgd 		goto bad;
    217  1.1      cgd 	}
    218  1.1      cgd 
    219  1.1      cgd 	while ((nr = read(fd, buf, sizeof(buf))) > 0)
    220  1.3  mycroft 		for (off = 0; off < nr;  off += nw)
    221  1.3  mycroft 			if ((nw = write(mbfd, buf + off, nr - off)) < 0) {
    222  1.1      cgd 				err(NOTFATAL, "%s: %s", path, strerror(errno));
    223  1.1      cgd 				goto trunc;
    224  1.1      cgd 			}
    225  1.1      cgd 	if (nr < 0) {
    226  1.1      cgd 		err(FATAL, "temporary file: %s", strerror(errno));
    227  1.1      cgd trunc:		(void)ftruncate(mbfd, curoff);
    228  1.1      cgd 		rval = 1;
    229  1.1      cgd 	}
    230  1.1      cgd 
    231  1.1      cgd 	/*
    232  1.1      cgd 	 * Set the owner and group.  Historically, binmail repeated this at
    233  1.1      cgd 	 * each mail delivery.  We no longer do this, assuming that if the
    234  1.1      cgd 	 * ownership or permissions were changed there was a reason for doing
    235  1.1      cgd 	 * so.
    236  1.1      cgd 	 */
    237  1.2  deraadt bad:
    238  1.2  deraadt 	if(lockfile) {
    239  1.2  deraadt 		if(lfd >= 0) {
    240  1.2  deraadt 			unlink(lpath);
    241  1.2  deraadt 			close(lfd);
    242  1.2  deraadt 		}
    243  1.2  deraadt 	}
    244  1.2  deraadt 	if (created)
    245  1.1      cgd 		(void)fchown(mbfd, pw->pw_uid, pw->pw_gid);
    246  1.1      cgd 
    247  1.1      cgd 	(void)fsync(mbfd);		/* Don't wait for update. */
    248  1.1      cgd 	(void)close(mbfd);		/* Implicit unlock. */
    249  1.1      cgd 
    250  1.1      cgd 	if (!rval)
    251  1.1      cgd 		notifybiff(biffmsg);
    252  1.1      cgd 	return(rval);
    253  1.1      cgd }
    254  1.1      cgd 
    255  1.1      cgd void
    256  1.1      cgd notifybiff(msg)
    257  1.1      cgd 	char *msg;
    258  1.1      cgd {
    259  1.1      cgd 	static struct sockaddr_in addr;
    260  1.1      cgd 	static int f = -1;
    261  1.1      cgd 	struct hostent *hp;
    262  1.1      cgd 	struct servent *sp;
    263  1.1      cgd 	int len;
    264  1.1      cgd 
    265  1.1      cgd 	if (!addr.sin_family) {
    266  1.1      cgd 		/* Be silent if biff service not available. */
    267  1.1      cgd 		if (!(sp = getservbyname("biff", "udp")))
    268  1.1      cgd 			return;
    269  1.1      cgd 		if (!(hp = gethostbyname("localhost"))) {
    270  1.1      cgd 			err(NOTFATAL, "localhost: %s", strerror(errno));
    271  1.1      cgd 			return;
    272  1.1      cgd 		}
    273  1.1      cgd 		addr.sin_family = hp->h_addrtype;
    274  1.1      cgd 		bcopy(hp->h_addr, &addr.sin_addr, hp->h_length);
    275  1.1      cgd 		addr.sin_port = sp->s_port;
    276  1.1      cgd 	}
    277  1.1      cgd 	if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    278  1.1      cgd 		err(NOTFATAL, "socket: %s", strerror(errno));
    279  1.1      cgd 		return;
    280  1.1      cgd 	}
    281  1.1      cgd 	len = strlen(msg) + 1;
    282  1.1      cgd 	if (sendto(f, msg, len, 0, (struct sockaddr *)&addr, sizeof(addr))
    283  1.1      cgd 	    != len)
    284  1.1      cgd 		err(NOTFATAL, "sendto biff: %s", strerror(errno));
    285  1.1      cgd }
    286  1.1      cgd 
    287  1.1      cgd void
    288  1.1      cgd usage()
    289  1.1      cgd {
    290  1.1      cgd 	err(FATAL, "usage: mail.local [-f from] user ...");
    291  1.1      cgd }
    292  1.1      cgd 
    293  1.1      cgd #if __STDC__
    294  1.1      cgd #include <stdarg.h>
    295  1.1      cgd #else
    296  1.1      cgd #include <varargs.h>
    297  1.1      cgd #endif
    298  1.1      cgd 
    299  1.1      cgd void
    300  1.1      cgd #if __STDC__
    301  1.1      cgd err(int isfatal, const char *fmt, ...)
    302  1.1      cgd #else
    303  1.1      cgd err(isfatal, fmt)
    304  1.1      cgd 	int isfatal;
    305  1.1      cgd 	char *fmt;
    306  1.1      cgd 	va_dcl
    307  1.1      cgd #endif
    308  1.1      cgd {
    309  1.1      cgd 	va_list ap;
    310  1.1      cgd #if __STDC__
    311  1.1      cgd 	va_start(ap, fmt);
    312  1.1      cgd #else
    313  1.1      cgd 	va_start(ap);
    314  1.1      cgd #endif
    315  1.1      cgd 	vsyslog(LOG_ERR, fmt, ap);
    316  1.1      cgd 	va_end(ap);
    317  1.1      cgd 	if (isfatal)
    318  1.1      cgd 		exit(1);
    319  1.1      cgd }
    320