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
|
/**
* \file local.c
* Local delivery of mail via a MDA.
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/wait.h>
#include "local.h"
#include "main.h"
#include "xmalloc.h"
char *mda = NULL;
FILE *mda_fp = NULL;
int local_address(const char *address)
{
return !strchr(address, '@');
}
/** replace ' by _ */
static void sanitize(char *s)
{
char *cp;
for (cp = s; (cp = strchr (cp, '\'')); cp++)
*cp = '_';
}
/**
* Pipe the message to the MDA for local delivery.
*
* Based on fetchmail's open_mda_sink().
*/
void local_init(message_t *message)
{
int length = 0, fromlen = 0, nameslen = 0;
char *names = NULL, *before, *after, *from = NULL;
if (!mda)
{
fprintf(stderr, "Local delivery not possible without a MDA\n");
exit(EX_OSFILE);
}
length = strlen(mda);
before = xstrdup(mda);
/* get user addresses for %T */
if (strstr(before, "%T"))
{
struct list_head *ptr;
char *p;
/*
* We go through this in order to be able to handle very
* long lists of users and (re)implement %s.
*/
nameslen = 0;
list_for_each(ptr, &message->local_recipients)
{
recipient_t *recipient = list_entry(ptr, recipient_t, list);
assert(recipient->address);
nameslen += (strlen(recipient->address) + 3); /* string + quotes + ' ' */
}
names = (char *)xmalloc(nameslen + 1); /* account for '\0' */
p = names;
list_for_each(ptr, &message->local_recipients)
{
recipient_t *recipient = list_entry(ptr, recipient_t, list);
int written;
sanitize(recipient->address);
written = sprintf(p, "'%s' ", recipient->address);
if (written < 0)
{
perror(NULL);
exit(EX_OSERR);
}
p += written;
}
names[--nameslen] = '\0'; /* chop trailing space */
}
/* get From address for %F */
if (strstr(before, "%F"))
{
from = xstrdup(message->reverse_path);
sanitize(from);
fromlen = strlen(from);
}
/* do we have to build an mda string? */
if (names || from)
{
char *sp, *dp;
/* find length of resulting mda string */
sp = before;
while ((sp = strstr(sp, "%s"))) {
length += nameslen; /* subtract %s and add '' */
sp += 2;
}
sp = before;
while ((sp = strstr(sp, "%T"))) {
length += nameslen; /* subtract %T and add '' */
sp += 2;
}
sp = before;
while ((sp = strstr(sp, "%F"))) {
length += fromlen; /* subtract %F and add '' */
sp += 2;
}
after = xmalloc(length + 1);
/* copy mda source string to after, while expanding %[sTF] */
for (dp = after, sp = before; (*dp = *sp); dp++, sp++) {
if (sp[0] != '%') continue;
/* need to expand? BTW, no here overflow, because in
** the worst case (end of string) sp[1] == '\0' */
if (sp[1] == 'T') {
strcpy(dp, names);
dp += nameslen;
sp++; /* position sp over [sT] */
dp--; /* adjust dp */
} else if (sp[1] == 'F') {
*dp++ = '\'';
strcpy(dp, from);
dp += fromlen;
*dp++ = '\'';
sp++; /* position sp over F */
dp--; /* adjust dp */
}
}
if (names) {
free(names);
names = NULL;
}
if (from) {
free(from);
from = NULL;
}
free(before);
before = after;
}
if(!(mda_fp = popen(before, "w")))
{
fprintf(stderr, "Failed to connect to MDA\n");
exit(EX_OSERR);
}
if(verbose)
fprintf(stdout, "Connected to MDA: %s\n", before);
free(before);
}
void local_flush(message_t *message)
{
char buffer[BUFSIZ];
size_t n;
do {
n = message_read(message, buffer, BUFSIZ);
if(ferror(mda_fp))
{
perror(NULL);
exit(EX_OSERR);
}
} while(n == BUFSIZ);
}
void local_cleanup(void)
{
if(mda_fp)
{
int status;
if((status = pclose(mda_fp)))
{
if(WIFSIGNALED(status))
fprintf(stderr, "MDA died of signal %d\n", WTERMSIG(status));
else if(WIFEXITED(status))
fprintf(stderr, "MDA returned nonzero status %d\n", WEXITSTATUS(status));
else
fprintf(stderr, "MDA failed\n");
exit(EX_OSERR);
}
mda_fp = NULL;
if(verbose)
fprintf(stdout, "Disconnected to MDA\n");
}
if(mda)
free(mda);
}
|