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
|
/*
Copyright (C) 1999-2004 IC & S dbmail@ic-s.nl
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* $Id$
*
* takes care of forwarding mail to an external address */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "db.h"
#include "dbmail.h"
#include "debug.h"
#include "forward.h"
#include "list.h"
/* For each of the addresses or programs in targets,
* send out a copy of the message pointed to by msgidnr.
*
* Returns 0 if all went well, -1 if there was an error.
* FIXME: there is no detail in the error reporting,
* so there's no way to tell *which* targets failed...
* */
int forward(u64_t msgidnr, struct list *targets, const char *from,
const char *header, u64_t headersize UNUSED)
{
struct element *target = NULL;
char *command = NULL;
size_t command_len = 0;
FILE *pipe = NULL;
int err;
field_t sendmail;
char timestr[50];
time_t td;
struct tm tm;
time(&td); /* get time */
tm = *localtime(&td); /* get components */
strftime(timestr, sizeof(timestr), "%a %b %e %H:%M:%S %Y", &tm);
config_get_value("SENDMAIL", "SMTP", sendmail);
if (sendmail[0] == '\0')
trace(TRACE_FATAL,
"%s,%s: SENDMAIL not configured (see config file). "
"Stop.", __FILE__, __func__);
trace(TRACE_INFO,
"%s,%s: delivering to [%ld] external addresses",
__FILE__, __func__, list_totalnodes(targets));
if (!msgidnr) {
trace(TRACE_ERROR,
"%s,%s: got NULL as message id number",
__FILE__, __func__);
return -1;
}
target = list_getstart(targets);
while (target != NULL) {
if ((((char *) target->data)[0] == '|')
|| (((char *) target->data)[0] == '!')) {
/* external pipe command */
command_len = strlen((char *) (target->data)) + 1;
command = dm_malloc(command_len * sizeof(char));
if (!command) {
trace(TRACE_ERROR,
"%s,%s: out of memory",
__FILE__, __func__);
return -1;
}
/* skip the pipe (|) sign */
strncpy(command, (char *) (target->data) + 1, command_len);
} else {
/* pipe to sendmail */
command_len = strlen(sendmail) + strlen(" -f ") +
strlen(from) + strlen (" ") +
strlen((char *) (target->data)) + 1;
command = dm_malloc(command_len * sizeof(char));
if (!command) {
trace(TRACE_ERROR,
"%s,%s: out of memory",
__FILE__, __func__);
return -1;
}
trace(TRACE_DEBUG,
"%s,%s: allocated memory for external "
"command call", __FILE__, __func__);
snprintf(command, command_len, "%s -f %s %s", sendmail, from,
(char *) (target->data));
}
trace(TRACE_INFO, "%s,%s: opening pipe to command %s",
__FILE__, __func__, command);
pipe = popen(command, "w"); /* opening pipe */
dm_free(command);
command = NULL;
if (pipe != NULL) {
trace(TRACE_DEBUG,
"%s,%s: call to popen() successfully "
"opened pipe [%d]", __FILE__, __func__,
fileno(pipe));
if (((char *) target->data)[0] == '!') {
/* ! tells us to prepend an mbox style header in this pipe */
trace(TRACE_DEBUG,
"%s,%s: appending mbox style from "
"header to pipe returnpath : %s",
__FILE__, __func__, from);
/* format: From<space>address<space><space>Date */
fprintf(pipe, "From %s %s\n", from,
timestr);
}
trace(TRACE_INFO,
"%s,%s: sending message id number [%llu] "
"to forward pipe", __FILE__, __func__,
msgidnr);
err = ferror(pipe);
trace(TRACE_DEBUG, "%s,%s: ferror reports"
" %d, feof reports %d on pipe %d", __FILE__,
__func__, err, feof(pipe), fileno(pipe));
if (!err) {
if (msgidnr != 0) {
trace(TRACE_DEBUG,
"%s,%s: sending lines from "
"message %llu on pipe %d",
__FILE__, __func__,
msgidnr, fileno(pipe));
db_send_message_lines(pipe,
msgidnr, -2,
1);
} else {
/* only send the headers if there
* is no message to send. */
trace(TRACE_DEBUG,
"%s,%s: writing header to "
"pipe", __FILE__,
__func__);
fprintf(pipe, "%s", header);
}
}
trace(TRACE_DEBUG, "%s,%s: closing pipes",
__FILE__, __func__);
if (!ferror(pipe)) {
pclose(pipe);
trace(TRACE_DEBUG,
"%s,%s: pipe closed",
__FILE__, __func__);
} else {
trace(TRACE_ERROR,
"%s,%s: error on pipe",
__FILE__, __func__);
}
} else {
trace(TRACE_ERROR,
"%s,%s: Could not open pipe to [%s]",
__FILE__, __func__, sendmail);
}
target = target->nextnode;
}
return 0;
}
|