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 #pragma ident "%Z%%M% %I% %E% SMI"
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include "mail.h"
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate * NAME
347c478bd9Sstevel@tonic-gate * sendlist - send copy to specified users
357c478bd9Sstevel@tonic-gate *
367c478bd9Sstevel@tonic-gate * SYNOPSIS
377c478bd9Sstevel@tonic-gate * int sendlist(reciplist *list, int letnum, int level)
387c478bd9Sstevel@tonic-gate *
397c478bd9Sstevel@tonic-gate * DESCRIPTION
407c478bd9Sstevel@tonic-gate * sendlist() will traverse the current recipient list and
417c478bd9Sstevel@tonic-gate * send a copy of the given letter to each user specified,
427c478bd9Sstevel@tonic-gate * invoking send() to do the sending. It returns
437c478bd9Sstevel@tonic-gate * 1 if the sending fails, 0 otherwise.
447c478bd9Sstevel@tonic-gate */
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate * mailx and mailtool read the SENDMAIL from an environment, since few
497c478bd9Sstevel@tonic-gate * people use /bin/mail as their user agent and since /bin/mail is often
507c478bd9Sstevel@tonic-gate * called as root or made setuid it's safer to leave this hardwired.
517c478bd9Sstevel@tonic-gate */
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate static char *sendmail_prog = SENDMAIL;
547c478bd9Sstevel@tonic-gate
55*b7d62af5Sceastha static void notifybiff(char *);
56*b7d62af5Sceastha
577c478bd9Sstevel@tonic-gate int
sendlist(reciplist * list,int letnum,int level)58*b7d62af5Sceastha sendlist(reciplist *list, int letnum, int level)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate recip *to;
617c478bd9Sstevel@tonic-gate int rc = 0;
627c478bd9Sstevel@tonic-gate FILE *fp;
637c478bd9Sstevel@tonic-gate int nargs = 4; /* "sendmail", "-oi", "--", .. NULL */
647c478bd9Sstevel@tonic-gate char **argv;
657c478bd9Sstevel@tonic-gate char **p;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate /* Deliver mail directly to a mailbox */
687c478bd9Sstevel@tonic-gate if (deliverflag) {
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate * Note failure to deliver to any one of the recipients
717c478bd9Sstevel@tonic-gate * should be considered a failure, so that the user
727c478bd9Sstevel@tonic-gate * get's an indication of that failure.
737c478bd9Sstevel@tonic-gate */
747c478bd9Sstevel@tonic-gate for (to = &(list->recip_list); to; to = to->next) {
757c478bd9Sstevel@tonic-gate if (to->name)
767c478bd9Sstevel@tonic-gate if (!send_mbox(to->name, letnum))
777c478bd9Sstevel@tonic-gate rc = 1;
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate return (rc);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate * build argv list, allowing for arbitrarily long deliver lists
847c478bd9Sstevel@tonic-gate * and then hand the message off to sendmail
857c478bd9Sstevel@tonic-gate */
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate if (!ismail)
887c478bd9Sstevel@tonic-gate nargs += 2; /* for "-f", "Rpath" */
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate for (to = &(list->recip_list); to; to = to->next)
917c478bd9Sstevel@tonic-gate if (to->name)
927c478bd9Sstevel@tonic-gate nargs++;
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate argv = malloc(nargs * sizeof (char *));
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate if (argv == NULL)
977c478bd9Sstevel@tonic-gate return (1);
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate p = argv;
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate *p++ = sendmail_prog;
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate /* If we're rmail add "-f", "Rpath" to the the command line */
1047c478bd9Sstevel@tonic-gate if (!ismail) {
1057c478bd9Sstevel@tonic-gate *p++ = "-f";
1067c478bd9Sstevel@tonic-gate *p++ = Rpath;
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate *p++ = "-oi";
1107c478bd9Sstevel@tonic-gate *p++ = "--"; /* extra protection: end of argument list */
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate for (to = &(list->recip_list); to; to = to->next)
1137c478bd9Sstevel@tonic-gate if (to->name)
1147c478bd9Sstevel@tonic-gate *p++ = to->name;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate *p = NULL;
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate fp = popenvp(sendmail_prog, argv, "w", 0);
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate free(argv);
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate if (fp == NULL)
1237c478bd9Sstevel@tonic-gate return (1);
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate copylet(letnum, fp, ORDINARY);
1267c478bd9Sstevel@tonic-gate rc = pclosevp(fp);
1277c478bd9Sstevel@tonic-gate if (!rc)
1287c478bd9Sstevel@tonic-gate return (0);
1297c478bd9Sstevel@tonic-gate else
1307c478bd9Sstevel@tonic-gate return (1);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate * send_mbox(user, letnum) Sends the letter specified by letnum to the
1357c478bd9Sstevel@tonic-gate * "user"'s mailbox. It returns 1 if the sending fails;
1367c478bd9Sstevel@tonic-gate * 0 on success.
1377c478bd9Sstevel@tonic-gate */
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate int
send_mbox(char * mbox,int letnum)142*b7d62af5Sceastha send_mbox(char *mbox, int letnum)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate char file[PATH_MAX];
1457c478bd9Sstevel@tonic-gate char biffmsg[PATH_MAX];
1467c478bd9Sstevel@tonic-gate int mbfd;
1477c478bd9Sstevel@tonic-gate FILE *malf;
1487c478bd9Sstevel@tonic-gate int rc;
1497c478bd9Sstevel@tonic-gate uid_t useruid, saved_uid;
1507c478bd9Sstevel@tonic-gate void (*istat)(), (*qstat)(), (*hstat)();
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate if (!islocal(mbox, &useruid))
1537c478bd9Sstevel@tonic-gate return (1);
1547c478bd9Sstevel@tonic-gate (void) strlcpy(file, maildir, sizeof (file));
1557c478bd9Sstevel@tonic-gate if (strlcat(file, mbox, sizeof (file)) >= sizeof (file)) {
1567c478bd9Sstevel@tonic-gate rc = FALSE;
1577c478bd9Sstevel@tonic-gate goto done;
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate * We need to setgid and seteuid here since the users's mail box
1627c478bd9Sstevel@tonic-gate * might be NFS mounted and since root can't write across NFS.
1637c478bd9Sstevel@tonic-gate * Note this won't work with Secure NFS/RPC's. Since delivering to
1647c478bd9Sstevel@tonic-gate * NFS mounted directories isn't really supported that's OK for now.
1657c478bd9Sstevel@tonic-gate */
1667c478bd9Sstevel@tonic-gate setgid(mailgrp);
1677c478bd9Sstevel@tonic-gate saved_uid = geteuid();
1687c478bd9Sstevel@tonic-gate seteuid(useruid);
1697c478bd9Sstevel@tonic-gate lock(mbox);
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate /* ignore signals */
1727c478bd9Sstevel@tonic-gate istat = signal(SIGINT, SIG_IGN);
1737c478bd9Sstevel@tonic-gate qstat = signal(SIGQUIT, SIG_IGN);
1747c478bd9Sstevel@tonic-gate hstat = signal(SIGHUP, SIG_IGN);
1757c478bd9Sstevel@tonic-gate /* now access mail box */
1767c478bd9Sstevel@tonic-gate mbfd = accessmf(file);
1777c478bd9Sstevel@tonic-gate if (mbfd == -1) { /* mail box access failed, bail out */
1787c478bd9Sstevel@tonic-gate unlock();
1797c478bd9Sstevel@tonic-gate rc = FALSE;
1807c478bd9Sstevel@tonic-gate sav_errno = EACCES;
1817c478bd9Sstevel@tonic-gate goto done;
1827c478bd9Sstevel@tonic-gate } else {
1837c478bd9Sstevel@tonic-gate /* mail box is ok, now do append */
1847c478bd9Sstevel@tonic-gate if ((malf = fdopen(mbfd, "a")) != NULL) {
1857c478bd9Sstevel@tonic-gate (void) snprintf(biffmsg, sizeof (biffmsg),
1867c478bd9Sstevel@tonic-gate "%s@%d\n", mbox, ftell(malf));
1877c478bd9Sstevel@tonic-gate rc = copylet(letnum, malf, ORDINARY);
1887c478bd9Sstevel@tonic-gate fclose(malf);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate if (rc == FALSE)
1937c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: Cannot append to %s\n", program, file);
1947c478bd9Sstevel@tonic-gate else
1957c478bd9Sstevel@tonic-gate notifybiff(biffmsg);
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate done:
1987c478bd9Sstevel@tonic-gate /* restore signal */
1997c478bd9Sstevel@tonic-gate (void) signal(SIGINT, istat);
2007c478bd9Sstevel@tonic-gate (void) signal(SIGQUIT, qstat);
2017c478bd9Sstevel@tonic-gate (void) signal(SIGHUP, hstat);
2027c478bd9Sstevel@tonic-gate unlock();
2037c478bd9Sstevel@tonic-gate seteuid(saved_uid);
2047c478bd9Sstevel@tonic-gate return (rc);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate #include <sys/socket.h>
2087c478bd9Sstevel@tonic-gate #include <netinet/in.h>
2097c478bd9Sstevel@tonic-gate
210*b7d62af5Sceastha static void
notifybiff(char * msg)211*b7d62af5Sceastha notifybiff(char *msg)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate static struct sockaddr_in addr;
2147c478bd9Sstevel@tonic-gate static int f = -1;
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate if (addr.sin_family == 0) {
2177c478bd9Sstevel@tonic-gate addr.sin_family = AF_INET;
2187c478bd9Sstevel@tonic-gate addr.sin_addr.s_addr = INADDR_LOOPBACK;
2197c478bd9Sstevel@tonic-gate addr.sin_port = htons(IPPORT_BIFFUDP);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate if (f < 0)
2227c478bd9Sstevel@tonic-gate f = socket(AF_INET, SOCK_DGRAM, 0);
2237c478bd9Sstevel@tonic-gate sendto(f, msg, strlen(msg)+1, 0, (struct sockaddr *)&addr,
2247c478bd9Sstevel@tonic-gate sizeof (addr));
2257c478bd9Sstevel@tonic-gate }
226