-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailer.c
266 lines (231 loc) · 7.57 KB
/
mailer.c
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright (c) 1995-2019 Peter Simons <simons@cryp.to>
* Copyright (c) 2000-2001 Cable & Wireless GmbH
* Copyright (c) 1999-2000 CyberSolutions GmbH
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdlib.h>
#include <stdarg.h>
#include <limits.h>
#include <sys/wait.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include "libtext/text.h"
#include "petidomo.h"
#ifndef ARG_NUM_MAX
# define ARG_NUM_MAX 4096
#endif
#ifndef ARG_MAX
# define ARG_MAX 4096
#endif
static char *
my_strcpy(char * dst, const char * src)
{
while((*dst++ = *src++) != '\0')
;
return dst-1;
}
FILE *
OpenMailer(const char * envelope, const char * recipients[])
{
assert(1==0);
return NULL;
}
FILE *
vOpenMailer(const char * envelope, ...)
{
const struct PD_Config * MasterConfig;
va_list ap;
FILE * fh;
char * cmdline;
char * p;
const char * q;
const char * options;
unsigned int cmdline_len;
MasterConfig = getMasterConfig();
/* Determine the length of the required buffer. */
cmdline_len = strlen(MasterConfig->mta);
cmdline_len += strlen(MasterConfig->mta_options);
cmdline_len += strlen(envelope);
va_start(ap, envelope);
while ((q = va_arg(ap, const char *)) != NULL) {
cmdline_len += strlen(q) + 1;
}
va_end(ap);
cmdline = xmalloc(cmdline_len+8); /* we don't take any risks :) */
/* Copy the mta's path and name into the buffer. */
p = my_strcpy(cmdline, MasterConfig->mta);
*p++ = ' ';
/* Copy the mta's options into the array, while replacing '%s'
with the envelope. */
for (options = MasterConfig->mta_options; *options != '\0'; )
{
if (options[0] == '%' && options[1] == 's')
{
p = my_strcpy(p, envelope);
*p++ = ' ';
options += 2;
break;
}
else
{
*p++ = *options++;
}
}
*p++ = ' ';
/* Append the list of recipients. */
va_start(ap, envelope);
while ((q = va_arg(ap, const char *)) != NULL)
{
p = my_strcpy(p, q);
*p++ = ' ';
}
p[-1] = '\0';
va_end(ap);
fh = popen(cmdline, "w");
if (fh == NULL)
syslog(LOG_ERR, "Failed opening pipe to \"%s\": %s", cmdline, strerror(errno));
free(cmdline);
return fh;
}
int
CloseMailer(FILE * fh)
{
return pclose(fh);
}
static int
my_strlen(const char * p)
{
unsigned int i;
for (i = 0; *p && !isspace((int)*p); p++)
i++;
return i;
}
#define MYPIPE_READ fildes[0]
#define MYPIPE_WRITE fildes[1]
int
ListMail(const char * envelope, const char * listname, const struct Mail * MailStruct)
{
const struct PD_Config * MasterConfig = getMasterConfig();
const struct List_Config * ListConfig = getListConfig(listname);
char ** arguments;
unsigned int arguments_num = 256;
char buffer[256];
char * listfile;
char * nextAddress;
char * currAddress;
char * p;
unsigned int counter;
unsigned int len;
unsigned int address_byte;
unsigned int max_address_byte;
int fildes[2];
pid_t child_pid;
int child_status;
/* Initialize internal stuff. */
arguments = xmalloc((arguments_num+1) * sizeof(char *));
max_address_byte = ARG_MAX - strlen(envelope) - strlen(MasterConfig->mta) -
strlen(MasterConfig->mta_options) - 8;
/* Load the list of recipients. */
listfile = loadfile(ListConfig->address_file);
if (listfile == NULL)
return 1;
/* Now go into delivery loop until we're finished. */
for(counter = 0, currAddress = listfile; *currAddress != '\0'; counter = 0)
{
/* Set up the call to the MTA, including options. */
arguments[counter++] = MasterConfig->mta;
sprintf(buffer, MasterConfig->mta_options, envelope);
for (p = buffer, arguments[counter++] = buffer; *p != '\0'; p++)
{
if (isspace((int)*p))
{
*p++ = '\0';
while(*p != '\0' && isspace((int)*p))
p++;
arguments[counter++] = p;
}
}
if (strlen(arguments[counter-1]) == 0)
counter--;
/* Append as many recipients as fit. */
for (address_byte = 0; *currAddress != '\0' ; currAddress = nextAddress)
{
nextAddress = text_find_next_line(currAddress);
len = my_strlen(currAddress);
if (address_byte + len > max_address_byte)
break;
if (counter > ARG_NUM_MAX)
break;
currAddress[len] = '\0';
address_byte += len;
arguments[counter++] = currAddress;
if (counter+8 >= arguments_num)
{
arguments_num += 256;
arguments = realloc(arguments, (arguments_num+1) * sizeof(char *));
if (arguments == NULL)
return -1;
}
}
/* Deliver the mail. */
arguments[counter++] = NULL;
if (pipe(fildes) == -1)
{
syslog(LOG_ERR, "Couldn't open a pipe to my child process: %s", strerror(errno));
return -1;
}
child_pid = fork();
switch(child_pid)
{
case 0:
/* Child */
close(MYPIPE_WRITE);
if (dup2(MYPIPE_READ, STDIN_FILENO) == -1)
{
syslog(LOG_ERR, "Child process couldn't read from pipe: %s", strerror(errno));
return -1;
}
close(MYPIPE_READ);
execv(MasterConfig->mta, arguments);
syslog(LOG_ERR, "Couldn't exec(\"%s\"): %s", MasterConfig->mta, strerror(errno));
return -1;
case -1:
/* Error */
syslog(LOG_ERR, "Couldn't fork: %s", strerror(errno));
return -1;
default:
/* everything is fine */
close(MYPIPE_READ);
}
write(MYPIPE_WRITE, MailStruct->Header, strlen(MailStruct->Header));
write(MYPIPE_WRITE, "\n", 1);
write(MYPIPE_WRITE, MailStruct->Body, strlen(MailStruct->Body));
if (MailStruct->ListSignature != NULL)
write(MYPIPE_WRITE, MailStruct->ListSignature, strlen(MailStruct->ListSignature));
close(MYPIPE_WRITE);
waitpid(child_pid, &child_status, 0);
if (!(WIFEXITED(child_status) && WEXITSTATUS(child_status) == 0))
{
syslog(LOG_ERR, "The executed mail agent return error %d, aborting.",
WEXITSTATUS(child_status));
return -1;
}
}
return 0;
}