dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / mail / sendlist.c
blob00d5696a335b38ca5527daac601bb0dd67850494
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
29 #pragma ident "%Z%%M% %I% %E% SMI"
31 #include "mail.h"
33 * NAME
34 * sendlist - send copy to specified users
36 * SYNOPSIS
37 * int sendlist(reciplist *list, int letnum, int level)
39 * DESCRIPTION
40 * sendlist() will traverse the current recipient list and
41 * send a copy of the given letter to each user specified,
42 * invoking send() to do the sending. It returns
43 * 1 if the sending fails, 0 otherwise.
48 * mailx and mailtool read the SENDMAIL from an environment, since few
49 * people use /bin/mail as their user agent and since /bin/mail is often
50 * called as root or made setuid it's safer to leave this hardwired.
53 static char *sendmail_prog = SENDMAIL;
55 static void notifybiff(char *);
57 int
58 sendlist(reciplist *list, int letnum, int level)
60 recip *to;
61 int rc = 0;
62 FILE *fp;
63 int nargs = 4; /* "sendmail", "-oi", "--", .. NULL */
64 char **argv;
65 char **p;
67 /* Deliver mail directly to a mailbox */
68 if (deliverflag) {
70 * Note failure to deliver to any one of the recipients
71 * should be considered a failure, so that the user
72 * get's an indication of that failure.
74 for (to = &(list->recip_list); to; to = to->next) {
75 if (to->name)
76 if (!send_mbox(to->name, letnum))
77 rc = 1;
79 return (rc);
83 * build argv list, allowing for arbitrarily long deliver lists
84 * and then hand the message off to sendmail
87 if (!ismail)
88 nargs += 2; /* for "-f", "Rpath" */
90 for (to = &(list->recip_list); to; to = to->next)
91 if (to->name)
92 nargs++;
94 argv = malloc(nargs * sizeof (char *));
96 if (argv == NULL)
97 return (1);
99 p = argv;
101 *p++ = sendmail_prog;
103 /* If we're rmail add "-f", "Rpath" to the the command line */
104 if (!ismail) {
105 *p++ = "-f";
106 *p++ = Rpath;
109 *p++ = "-oi";
110 *p++ = "--"; /* extra protection: end of argument list */
112 for (to = &(list->recip_list); to; to = to->next)
113 if (to->name)
114 *p++ = to->name;
116 *p = NULL;
118 fp = popenvp(sendmail_prog, argv, "w", 0);
120 free(argv);
122 if (fp == NULL)
123 return (1);
125 copylet(letnum, fp, ORDINARY);
126 rc = pclosevp(fp);
127 if (!rc)
128 return (0);
129 else
130 return (1);
134 * send_mbox(user, letnum) Sends the letter specified by letnum to the
135 * "user"'s mailbox. It returns 1 if the sending fails;
136 * 0 on success.
142 send_mbox(char *mbox, int letnum)
144 char file[PATH_MAX];
145 char biffmsg[PATH_MAX];
146 int mbfd;
147 FILE *malf;
148 int rc;
149 uid_t useruid, saved_uid;
150 void (*istat)(), (*qstat)(), (*hstat)();
152 if (!islocal(mbox, &useruid))
153 return (1);
154 (void) strlcpy(file, maildir, sizeof (file));
155 if (strlcat(file, mbox, sizeof (file)) >= sizeof (file)) {
156 rc = FALSE;
157 goto done;
161 * We need to setgid and seteuid here since the users's mail box
162 * might be NFS mounted and since root can't write across NFS.
163 * Note this won't work with Secure NFS/RPC's. Since delivering to
164 * NFS mounted directories isn't really supported that's OK for now.
166 setgid(mailgrp);
167 saved_uid = geteuid();
168 seteuid(useruid);
169 lock(mbox);
171 /* ignore signals */
172 istat = signal(SIGINT, SIG_IGN);
173 qstat = signal(SIGQUIT, SIG_IGN);
174 hstat = signal(SIGHUP, SIG_IGN);
175 /* now access mail box */
176 mbfd = accessmf(file);
177 if (mbfd == -1) { /* mail box access failed, bail out */
178 unlock();
179 rc = FALSE;
180 sav_errno = EACCES;
181 goto done;
182 } else {
183 /* mail box is ok, now do append */
184 if ((malf = fdopen(mbfd, "a")) != NULL) {
185 (void) snprintf(biffmsg, sizeof (biffmsg),
186 "%s@%d\n", mbox, ftell(malf));
187 rc = copylet(letnum, malf, ORDINARY);
188 fclose(malf);
192 if (rc == FALSE)
193 fprintf(stderr, "%s: Cannot append to %s\n", program, file);
194 else
195 notifybiff(biffmsg);
197 done:
198 /* restore signal */
199 (void) signal(SIGINT, istat);
200 (void) signal(SIGQUIT, qstat);
201 (void) signal(SIGHUP, hstat);
202 unlock();
203 seteuid(saved_uid);
204 return (rc);
207 #include <sys/socket.h>
208 #include <netinet/in.h>
210 static void
211 notifybiff(char *msg)
213 static struct sockaddr_in addr;
214 static int f = -1;
216 if (addr.sin_family == 0) {
217 addr.sin_family = AF_INET;
218 addr.sin_addr.s_addr = INADDR_LOOPBACK;
219 addr.sin_port = htons(IPPORT_BIFFUDP);
221 if (f < 0)
222 f = socket(AF_INET, SOCK_DGRAM, 0);
223 sendto(f, msg, strlen(msg)+1, 0, (struct sockaddr *)&addr,
224 sizeof (addr));