xref: /illumos-gate/usr/src/cmd/mail/sendlist.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*b7d62af5Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*b7d62af5Sceastha /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*b7d62af5Sceastha 
297c478bd9Sstevel@tonic-gate #include "mail.h"
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  *  NAME
327c478bd9Sstevel@tonic-gate  *	sendlist - send copy to specified users
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  *  SYNOPSIS
357c478bd9Sstevel@tonic-gate  *	int sendlist(reciplist *list, int letnum, int level)
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  *  DESCRIPTION
387c478bd9Sstevel@tonic-gate  *	sendlist() will traverse the current recipient list and
397c478bd9Sstevel@tonic-gate  *	send a copy of the given letter to each user specified,
407c478bd9Sstevel@tonic-gate  *	invoking send() to do the sending. It returns
417c478bd9Sstevel@tonic-gate  *	1 if the sending fails, 0 otherwise.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * mailx and mailtool read the SENDMAIL from an environment, since few
477c478bd9Sstevel@tonic-gate  *  people use /bin/mail as their user agent and since /bin/mail is often
487c478bd9Sstevel@tonic-gate  *  called as root or made setuid it's safer to leave this hardwired.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static char *sendmail_prog = SENDMAIL;
527c478bd9Sstevel@tonic-gate 
53*b7d62af5Sceastha static void notifybiff(char *);
54*b7d62af5Sceastha 
557c478bd9Sstevel@tonic-gate int
sendlist(reciplist * list,int letnum,int level)56*b7d62af5Sceastha sendlist(reciplist *list, int letnum, int level)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	recip *to;
597c478bd9Sstevel@tonic-gate 	int rc = 0;
607c478bd9Sstevel@tonic-gate 	FILE *fp;
617c478bd9Sstevel@tonic-gate 	int nargs = 4;			/* "sendmail", "-oi", "--", .. NULL */
627c478bd9Sstevel@tonic-gate 	char **argv;
637c478bd9Sstevel@tonic-gate 	char **p;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	/* Deliver mail directly to a mailbox */
667c478bd9Sstevel@tonic-gate 	if (deliverflag) {
677c478bd9Sstevel@tonic-gate 		/*
687c478bd9Sstevel@tonic-gate 		 * Note failure to deliver to any one of the recipients
697c478bd9Sstevel@tonic-gate 		 * should be considered a failure, so that the user
707c478bd9Sstevel@tonic-gate 		 * get's an indication of that failure.
717c478bd9Sstevel@tonic-gate 		 */
727c478bd9Sstevel@tonic-gate 		for (to = &(list->recip_list); to; to = to->next) {
737c478bd9Sstevel@tonic-gate 			if (to->name)
747c478bd9Sstevel@tonic-gate 				if (!send_mbox(to->name, letnum))
757c478bd9Sstevel@tonic-gate 					rc = 1;
767c478bd9Sstevel@tonic-gate 		}
777c478bd9Sstevel@tonic-gate 		return (rc);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	/*
817c478bd9Sstevel@tonic-gate 	 * build argv list, allowing for arbitrarily long deliver lists
827c478bd9Sstevel@tonic-gate 	 * and then  hand the message off to sendmail
837c478bd9Sstevel@tonic-gate 	 */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (!ismail)
867c478bd9Sstevel@tonic-gate 		nargs += 2;	/* for "-f", "Rpath" */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	for (to = &(list->recip_list); to; to = to->next)
897c478bd9Sstevel@tonic-gate 		if (to->name)
907c478bd9Sstevel@tonic-gate 			nargs++;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	argv = malloc(nargs * sizeof (char *));
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (argv == NULL)
957c478bd9Sstevel@tonic-gate 		return (1);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	p = argv;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	*p++ = sendmail_prog;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	/* If we're rmail add "-f", "Rpath" to the the command line */
1027c478bd9Sstevel@tonic-gate 	if (!ismail) {
1037c478bd9Sstevel@tonic-gate 		*p++ = "-f";
1047c478bd9Sstevel@tonic-gate 		*p++ = Rpath;
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	*p++ = "-oi";
1087c478bd9Sstevel@tonic-gate 	*p++ = "--";		/* extra protection: end of argument list */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	for (to = &(list->recip_list); to; to = to->next)
1117c478bd9Sstevel@tonic-gate 		if (to->name)
1127c478bd9Sstevel@tonic-gate 			*p++ = to->name;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	*p = NULL;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	fp = popenvp(sendmail_prog, argv, "w", 0);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	free(argv);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	if (fp == NULL)
1217c478bd9Sstevel@tonic-gate 		return (1);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	copylet(letnum, fp, ORDINARY);
1247c478bd9Sstevel@tonic-gate 	rc = pclosevp(fp);
1257c478bd9Sstevel@tonic-gate 	if (!rc)
1267c478bd9Sstevel@tonic-gate 		return (0);
1277c478bd9Sstevel@tonic-gate 	else
1287c478bd9Sstevel@tonic-gate 		return (1);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * send_mbox(user, letnum)  Sends the letter specified by letnum to the
1337c478bd9Sstevel@tonic-gate  *	"user"'s mailbox. It returns 1 if the sending fails;
1347c478bd9Sstevel@tonic-gate  *	0 on success.
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate int
send_mbox(char * mbox,int letnum)140*b7d62af5Sceastha send_mbox(char *mbox, int letnum)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	char file[PATH_MAX];
1437c478bd9Sstevel@tonic-gate 	char biffmsg[PATH_MAX];
1447c478bd9Sstevel@tonic-gate 	int mbfd;
1457c478bd9Sstevel@tonic-gate 	FILE *malf;
1467c478bd9Sstevel@tonic-gate 	int rc;
1477c478bd9Sstevel@tonic-gate 	uid_t useruid, saved_uid;
1487c478bd9Sstevel@tonic-gate 	void (*istat)(), (*qstat)(), (*hstat)();
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (!islocal(mbox, &useruid))
1517c478bd9Sstevel@tonic-gate 		return (1);
1527c478bd9Sstevel@tonic-gate 	(void) strlcpy(file, maildir, sizeof (file));
1537c478bd9Sstevel@tonic-gate 	if (strlcat(file, mbox, sizeof (file)) >= sizeof (file)) {
1547c478bd9Sstevel@tonic-gate 		rc = FALSE;
1557c478bd9Sstevel@tonic-gate 		goto done;
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/*
1597c478bd9Sstevel@tonic-gate 	 * We need to setgid and seteuid here since the users's mail box
1607c478bd9Sstevel@tonic-gate 	 * might be NFS mounted and since root can't write across NFS.
1617c478bd9Sstevel@tonic-gate 	 * Note this won't work with Secure NFS/RPC's.  Since delivering to
1627c478bd9Sstevel@tonic-gate 	 * NFS mounted directories isn't really supported that's OK for now.
1637c478bd9Sstevel@tonic-gate 	 */
1647c478bd9Sstevel@tonic-gate 	setgid(mailgrp);
1657c478bd9Sstevel@tonic-gate 	saved_uid = geteuid();
1667c478bd9Sstevel@tonic-gate 	seteuid(useruid);
1677c478bd9Sstevel@tonic-gate 	lock(mbox);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/* ignore signals */
1707c478bd9Sstevel@tonic-gate 	istat = signal(SIGINT, SIG_IGN);
1717c478bd9Sstevel@tonic-gate 	qstat = signal(SIGQUIT, SIG_IGN);
1727c478bd9Sstevel@tonic-gate 	hstat = signal(SIGHUP, SIG_IGN);
1737c478bd9Sstevel@tonic-gate 	/* now access mail box */
1747c478bd9Sstevel@tonic-gate 	mbfd = accessmf(file);
1757c478bd9Sstevel@tonic-gate 	if (mbfd == -1) {	/* mail box access failed, bail out */
1767c478bd9Sstevel@tonic-gate 		unlock();
1777c478bd9Sstevel@tonic-gate 		rc = FALSE;
1787c478bd9Sstevel@tonic-gate 		sav_errno = EACCES;
1797c478bd9Sstevel@tonic-gate 		goto done;
1807c478bd9Sstevel@tonic-gate 	} else {
1817c478bd9Sstevel@tonic-gate 				/* mail box is ok, now do append */
1827c478bd9Sstevel@tonic-gate 		if ((malf = fdopen(mbfd, "a")) != NULL) {
1837c478bd9Sstevel@tonic-gate 			(void) snprintf(biffmsg, sizeof (biffmsg),
1847c478bd9Sstevel@tonic-gate 			    "%s@%d\n", mbox, ftell(malf));
1857c478bd9Sstevel@tonic-gate 			rc = copylet(letnum, malf, ORDINARY);
1867c478bd9Sstevel@tonic-gate 			fclose(malf);
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (rc == FALSE)
1917c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Cannot append to %s\n", program, file);
1927c478bd9Sstevel@tonic-gate 	else
1937c478bd9Sstevel@tonic-gate 		notifybiff(biffmsg);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate done:
1967c478bd9Sstevel@tonic-gate 	/* restore signal */
1977c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, istat);
1987c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, qstat);
1997c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP, hstat);
2007c478bd9Sstevel@tonic-gate 	unlock();
2017c478bd9Sstevel@tonic-gate 	seteuid(saved_uid);
2027c478bd9Sstevel@tonic-gate 	return (rc);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate #include <sys/socket.h>
2067c478bd9Sstevel@tonic-gate #include <netinet/in.h>
2077c478bd9Sstevel@tonic-gate 
208*b7d62af5Sceastha static void
notifybiff(char * msg)209*b7d62af5Sceastha notifybiff(char *msg)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	static struct sockaddr_in addr;
2127c478bd9Sstevel@tonic-gate 	static int f = -1;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (addr.sin_family == 0) {
2157c478bd9Sstevel@tonic-gate 		addr.sin_family = AF_INET;
2167c478bd9Sstevel@tonic-gate 		addr.sin_addr.s_addr = INADDR_LOOPBACK;
2177c478bd9Sstevel@tonic-gate 		addr.sin_port = htons(IPPORT_BIFFUDP);
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 	if (f < 0)
2207c478bd9Sstevel@tonic-gate 		f = socket(AF_INET, SOCK_DGRAM, 0);
2217c478bd9Sstevel@tonic-gate 	sendto(f, msg, strlen(msg)+1, 0, (struct sockaddr *)&addr,
2227c478bd9Sstevel@tonic-gate 		sizeof (addr));
2237c478bd9Sstevel@tonic-gate }
224