10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
23*373Sceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
27*373Sceastha /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*373Sceastha
290Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include "mail.h"
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate * NAME
340Sstevel@tonic-gate * sendlist - send copy to specified users
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * SYNOPSIS
370Sstevel@tonic-gate * int sendlist(reciplist *list, int letnum, int level)
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * DESCRIPTION
400Sstevel@tonic-gate * sendlist() will traverse the current recipient list and
410Sstevel@tonic-gate * send a copy of the given letter to each user specified,
420Sstevel@tonic-gate * invoking send() to do the sending. It returns
430Sstevel@tonic-gate * 1 if the sending fails, 0 otherwise.
440Sstevel@tonic-gate */
450Sstevel@tonic-gate
460Sstevel@tonic-gate
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate * mailx and mailtool read the SENDMAIL from an environment, since few
490Sstevel@tonic-gate * people use /bin/mail as their user agent and since /bin/mail is often
500Sstevel@tonic-gate * called as root or made setuid it's safer to leave this hardwired.
510Sstevel@tonic-gate */
520Sstevel@tonic-gate
530Sstevel@tonic-gate static char *sendmail_prog = SENDMAIL;
540Sstevel@tonic-gate
55*373Sceastha static void notifybiff(char *);
56*373Sceastha
570Sstevel@tonic-gate int
sendlist(reciplist * list,int letnum,int level)58*373Sceastha sendlist(reciplist *list, int letnum, int level)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate recip *to;
610Sstevel@tonic-gate int rc = 0;
620Sstevel@tonic-gate FILE *fp;
630Sstevel@tonic-gate int nargs = 4; /* "sendmail", "-oi", "--", .. NULL */
640Sstevel@tonic-gate char **argv;
650Sstevel@tonic-gate char **p;
660Sstevel@tonic-gate
670Sstevel@tonic-gate /* Deliver mail directly to a mailbox */
680Sstevel@tonic-gate if (deliverflag) {
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate * Note failure to deliver to any one of the recipients
710Sstevel@tonic-gate * should be considered a failure, so that the user
720Sstevel@tonic-gate * get's an indication of that failure.
730Sstevel@tonic-gate */
740Sstevel@tonic-gate for (to = &(list->recip_list); to; to = to->next) {
750Sstevel@tonic-gate if (to->name)
760Sstevel@tonic-gate if (!send_mbox(to->name, letnum))
770Sstevel@tonic-gate rc = 1;
780Sstevel@tonic-gate }
790Sstevel@tonic-gate return (rc);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate * build argv list, allowing for arbitrarily long deliver lists
840Sstevel@tonic-gate * and then hand the message off to sendmail
850Sstevel@tonic-gate */
860Sstevel@tonic-gate
870Sstevel@tonic-gate if (!ismail)
880Sstevel@tonic-gate nargs += 2; /* for "-f", "Rpath" */
890Sstevel@tonic-gate
900Sstevel@tonic-gate for (to = &(list->recip_list); to; to = to->next)
910Sstevel@tonic-gate if (to->name)
920Sstevel@tonic-gate nargs++;
930Sstevel@tonic-gate
940Sstevel@tonic-gate argv = malloc(nargs * sizeof (char *));
950Sstevel@tonic-gate
960Sstevel@tonic-gate if (argv == NULL)
970Sstevel@tonic-gate return (1);
980Sstevel@tonic-gate
990Sstevel@tonic-gate p = argv;
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate *p++ = sendmail_prog;
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate /* If we're rmail add "-f", "Rpath" to the the command line */
1040Sstevel@tonic-gate if (!ismail) {
1050Sstevel@tonic-gate *p++ = "-f";
1060Sstevel@tonic-gate *p++ = Rpath;
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate *p++ = "-oi";
1100Sstevel@tonic-gate *p++ = "--"; /* extra protection: end of argument list */
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate for (to = &(list->recip_list); to; to = to->next)
1130Sstevel@tonic-gate if (to->name)
1140Sstevel@tonic-gate *p++ = to->name;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate *p = NULL;
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate fp = popenvp(sendmail_prog, argv, "w", 0);
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate free(argv);
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate if (fp == NULL)
1230Sstevel@tonic-gate return (1);
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate copylet(letnum, fp, ORDINARY);
1260Sstevel@tonic-gate rc = pclosevp(fp);
1270Sstevel@tonic-gate if (!rc)
1280Sstevel@tonic-gate return (0);
1290Sstevel@tonic-gate else
1300Sstevel@tonic-gate return (1);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate * send_mbox(user, letnum) Sends the letter specified by letnum to the
1350Sstevel@tonic-gate * "user"'s mailbox. It returns 1 if the sending fails;
1360Sstevel@tonic-gate * 0 on success.
1370Sstevel@tonic-gate */
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate int
send_mbox(char * mbox,int letnum)142*373Sceastha send_mbox(char *mbox, int letnum)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate char file[PATH_MAX];
1450Sstevel@tonic-gate char biffmsg[PATH_MAX];
1460Sstevel@tonic-gate int mbfd;
1470Sstevel@tonic-gate FILE *malf;
1480Sstevel@tonic-gate int rc;
1490Sstevel@tonic-gate uid_t useruid, saved_uid;
1500Sstevel@tonic-gate void (*istat)(), (*qstat)(), (*hstat)();
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate if (!islocal(mbox, &useruid))
1530Sstevel@tonic-gate return (1);
1540Sstevel@tonic-gate (void) strlcpy(file, maildir, sizeof (file));
1550Sstevel@tonic-gate if (strlcat(file, mbox, sizeof (file)) >= sizeof (file)) {
1560Sstevel@tonic-gate rc = FALSE;
1570Sstevel@tonic-gate goto done;
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate * We need to setgid and seteuid here since the users's mail box
1620Sstevel@tonic-gate * might be NFS mounted and since root can't write across NFS.
1630Sstevel@tonic-gate * Note this won't work with Secure NFS/RPC's. Since delivering to
1640Sstevel@tonic-gate * NFS mounted directories isn't really supported that's OK for now.
1650Sstevel@tonic-gate */
1660Sstevel@tonic-gate setgid(mailgrp);
1670Sstevel@tonic-gate saved_uid = geteuid();
1680Sstevel@tonic-gate seteuid(useruid);
1690Sstevel@tonic-gate lock(mbox);
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate /* ignore signals */
1720Sstevel@tonic-gate istat = signal(SIGINT, SIG_IGN);
1730Sstevel@tonic-gate qstat = signal(SIGQUIT, SIG_IGN);
1740Sstevel@tonic-gate hstat = signal(SIGHUP, SIG_IGN);
1750Sstevel@tonic-gate /* now access mail box */
1760Sstevel@tonic-gate mbfd = accessmf(file);
1770Sstevel@tonic-gate if (mbfd == -1) { /* mail box access failed, bail out */
1780Sstevel@tonic-gate unlock();
1790Sstevel@tonic-gate rc = FALSE;
1800Sstevel@tonic-gate sav_errno = EACCES;
1810Sstevel@tonic-gate goto done;
1820Sstevel@tonic-gate } else {
1830Sstevel@tonic-gate /* mail box is ok, now do append */
1840Sstevel@tonic-gate if ((malf = fdopen(mbfd, "a")) != NULL) {
1850Sstevel@tonic-gate (void) snprintf(biffmsg, sizeof (biffmsg),
1860Sstevel@tonic-gate "%s@%d\n", mbox, ftell(malf));
1870Sstevel@tonic-gate rc = copylet(letnum, malf, ORDINARY);
1880Sstevel@tonic-gate fclose(malf);
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate if (rc == FALSE)
1930Sstevel@tonic-gate fprintf(stderr, "%s: Cannot append to %s\n", program, file);
1940Sstevel@tonic-gate else
1950Sstevel@tonic-gate notifybiff(biffmsg);
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate done:
1980Sstevel@tonic-gate /* restore signal */
1990Sstevel@tonic-gate (void) signal(SIGINT, istat);
2000Sstevel@tonic-gate (void) signal(SIGQUIT, qstat);
2010Sstevel@tonic-gate (void) signal(SIGHUP, hstat);
2020Sstevel@tonic-gate unlock();
2030Sstevel@tonic-gate seteuid(saved_uid);
2040Sstevel@tonic-gate return (rc);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate #include <sys/socket.h>
2080Sstevel@tonic-gate #include <netinet/in.h>
2090Sstevel@tonic-gate
210*373Sceastha static void
notifybiff(char * msg)211*373Sceastha notifybiff(char *msg)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate static struct sockaddr_in addr;
2140Sstevel@tonic-gate static int f = -1;
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate if (addr.sin_family == 0) {
2170Sstevel@tonic-gate addr.sin_family = AF_INET;
2180Sstevel@tonic-gate addr.sin_addr.s_addr = INADDR_LOOPBACK;
2190Sstevel@tonic-gate addr.sin_port = htons(IPPORT_BIFFUDP);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate if (f < 0)
2220Sstevel@tonic-gate f = socket(AF_INET, SOCK_DGRAM, 0);
2230Sstevel@tonic-gate sendto(f, msg, strlen(msg)+1, 0, (struct sockaddr *)&addr,
2240Sstevel@tonic-gate sizeof (addr));
2250Sstevel@tonic-gate }
226