summaryrefslogtreecommitdiff
path: root/usr/src/cmd/mail/sendlist.c
blob: 00d5696a335b38ca5527daac601bb0dd67850494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include "mail.h"
/*
 *  NAME
 *	sendlist - send copy to specified users
 *
 *  SYNOPSIS
 *	int sendlist(reciplist *list, int letnum, int level)
 *
 *  DESCRIPTION
 *	sendlist() will traverse the current recipient list and
 *	send a copy of the given letter to each user specified,
 *	invoking send() to do the sending. It returns
 *	1 if the sending fails, 0 otherwise.
 */


/*
 * mailx and mailtool read the SENDMAIL from an environment, since few
 *  people use /bin/mail as their user agent and since /bin/mail is often
 *  called as root or made setuid it's safer to leave this hardwired.
 */

static char *sendmail_prog = SENDMAIL;

static void notifybiff(char *);

int
sendlist(reciplist *list, int letnum, int level)
{
	recip *to;
	int rc = 0;
	FILE *fp;
	int nargs = 4;			/* "sendmail", "-oi", "--", .. NULL */
	char **argv;
	char **p;

	/* Deliver mail directly to a mailbox */
	if (deliverflag) {
		/*
		 * Note failure to deliver to any one of the recipients
		 * should be considered a failure, so that the user
		 * get's an indication of that failure.
		 */
		for (to = &(list->recip_list); to; to = to->next) {
			if (to->name)
				if (!send_mbox(to->name, letnum))
					rc = 1;
		}
		return (rc);
	}

	/*
	 * build argv list, allowing for arbitrarily long deliver lists
	 * and then  hand the message off to sendmail
	 */

	if (!ismail)
		nargs += 2;	/* for "-f", "Rpath" */

	for (to = &(list->recip_list); to; to = to->next)
		if (to->name)
			nargs++;

	argv = malloc(nargs * sizeof (char *));

	if (argv == NULL)
		return (1);

	p = argv;

	*p++ = sendmail_prog;

	/* If we're rmail add "-f", "Rpath" to the the command line */
	if (!ismail) {
		*p++ = "-f";
		*p++ = Rpath;
	}

	*p++ = "-oi";
	*p++ = "--";		/* extra protection: end of argument list */

	for (to = &(list->recip_list); to; to = to->next)
		if (to->name)
			*p++ = to->name;

	*p = NULL;

	fp = popenvp(sendmail_prog, argv, "w", 0);

	free(argv);

	if (fp == NULL)
		return (1);

	copylet(letnum, fp, ORDINARY);
	rc = pclosevp(fp);
	if (!rc)
		return (0);
	else
		return (1);
}

/*
 * send_mbox(user, letnum)  Sends the letter specified by letnum to the
 *	"user"'s mailbox. It returns 1 if the sending fails;
 *	0 on success.
 */



int
send_mbox(char *mbox, int letnum)
{
	char file[PATH_MAX];
	char biffmsg[PATH_MAX];
	int mbfd;
	FILE *malf;
	int rc;
	uid_t useruid, saved_uid;
	void (*istat)(), (*qstat)(), (*hstat)();

	if (!islocal(mbox, &useruid))
		return (1);
	(void) strlcpy(file, maildir, sizeof (file));
	if (strlcat(file, mbox, sizeof (file)) >= sizeof (file)) {
		rc = FALSE;
		goto done;
	}

	/*
	 * We need to setgid and seteuid here since the users's mail box
	 * might be NFS mounted and since root can't write across NFS.
	 * Note this won't work with Secure NFS/RPC's.  Since delivering to
	 * NFS mounted directories isn't really supported that's OK for now.
	 */
	setgid(mailgrp);
	saved_uid = geteuid();
	seteuid(useruid);
	lock(mbox);

	/* ignore signals */
	istat = signal(SIGINT, SIG_IGN);
	qstat = signal(SIGQUIT, SIG_IGN);
	hstat = signal(SIGHUP, SIG_IGN);
	/* now access mail box */
	mbfd = accessmf(file);
	if (mbfd == -1) {	/* mail box access failed, bail out */
		unlock();
		rc = FALSE;
		sav_errno = EACCES;
		goto done;
	} else {
				/* mail box is ok, now do append */
		if ((malf = fdopen(mbfd, "a")) != NULL) {
			(void) snprintf(biffmsg, sizeof (biffmsg),
			    "%s@%d\n", mbox, ftell(malf));
			rc = copylet(letnum, malf, ORDINARY);
			fclose(malf);
		}
	}

	if (rc == FALSE)
		fprintf(stderr, "%s: Cannot append to %s\n", program, file);
	else
		notifybiff(biffmsg);

done:
	/* restore signal */
	(void) signal(SIGINT, istat);
	(void) signal(SIGQUIT, qstat);
	(void) signal(SIGHUP, hstat);
	unlock();
	seteuid(saved_uid);
	return (rc);
}

#include <sys/socket.h>
#include <netinet/in.h>

static void
notifybiff(char *msg)
{
	static struct sockaddr_in addr;
	static int f = -1;

	if (addr.sin_family == 0) {
		addr.sin_family = AF_INET;
		addr.sin_addr.s_addr = INADDR_LOOPBACK;
		addr.sin_port = htons(IPPORT_BIFFUDP);
	}
	if (f < 0)
		f = socket(AF_INET, SOCK_DGRAM, 0);
	sendto(f, msg, strlen(msg)+1, 0, (struct sockaddr *)&addr,
		sizeof (addr));
}