sendmail.c
6.92 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2004, 2005,
2006, 2007, 2008, 2009 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#ifdef ENABLE_SENDMAIL
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <confpaths.h>
#include <mailutils/address.h>
#include <mailutils/debug.h>
#include <mailutils/observer.h>
#include <mailutils/property.h>
#include <mailutils/url.h>
#include <mailutils/errno.h>
#include <mailutils/progmailer.h>
#include <url0.h>
#include <mailer0.h>
#include <registrar0.h>
static void sendmail_destroy (mu_mailer_t);
static int sendmail_open (mu_mailer_t, int);
static int sendmail_close (mu_mailer_t);
static int sendmail_send_message (mu_mailer_t, mu_message_t, mu_address_t,
mu_address_t);
static int
_url_sendmail_init (mu_url_t url)
{
/* not valid in a sendmail url */
if (url->user || url->secret || url->auth || url->qargc
|| url->host || url->port)
return EINVAL;
if (url->path == 0)
if ((url->path = strdup (PATH_SENDMAIL)) == 0)
return ENOMEM;
return 0;
}
int
_mu_mailer_sendmail_init (mu_mailer_t mailer)
{
int status;
mu_progmailer_t pm;
status = mu_progmailer_create (&pm);
if (status)
return status;
mailer->data = pm;
mailer->_destroy = sendmail_destroy;
mailer->_open = sendmail_open;
mailer->_close = sendmail_close;
mailer->_send_message = sendmail_send_message;
/* Set our properties. */
{
mu_property_t property = NULL;
mu_mailer_get_property (mailer, &property);
mu_property_set_value (property, "TYPE", "SENDMAIL", 1);
}
return 0;
}
static void
sendmail_destroy (mu_mailer_t mailer)
{
mu_progmailer_destroy ((mu_progmailer_t*)&mailer->data);
}
static int
sendmail_open (mu_mailer_t mailer, int flags)
{
mu_progmailer_t pm = mailer->data;
int status;
const char *path;
/* Sanity checks. */
if (pm == NULL)
return EINVAL;
mailer->flags = flags;
if ((status = mu_url_sget_path (mailer->url, &path)))
return status;
if (access (path, X_OK) == -1)
return errno;
mu_progmailer_set_debug (pm, mailer->debug);
status = mu_progmailer_set_command (pm, path);
MU_DEBUG1 (mailer->debug, MU_DEBUG_TRACE, "sendmail (%s)\n", path);
return status;
}
static int
sendmail_close (mu_mailer_t mailer)
{
return mu_progmailer_close (mailer->data);
}
static int
mailer_property_is_set (mu_mailer_t mailer, const char *name)
{
mu_property_t property = NULL;
mu_mailer_get_property (mailer, &property);
return mu_property_is_set (property, name);
}
static int
sendmail_send_message (mu_mailer_t mailer, mu_message_t msg, mu_address_t from,
mu_address_t to)
{
mu_progmailer_t pm = mailer->data;
int argc = 0;
const char **argvec = NULL;
size_t tocount = 0;
const char *emailfrom = NULL;
int status;
if (!pm)
return EINVAL;
/* Count the length of the arg vec: */
argc++; /* terminating NULL */
argc++; /* sendmail */
argc++; /* -oi (do not treat '.' as message
terminator) */
if (from)
{
if ((status = mu_address_sget_email (from, 1, &emailfrom)) != 0)
{
MU_DEBUG1 (mailer->debug, MU_DEBUG_ERROR,
"cannot get recipient email: %s\n",
mu_strerror (status));
return status;
}
if (!emailfrom)
{
/* the address wasn't fully qualified, choke (for now) */
MU_DEBUG1 (mailer->debug, MU_DEBUG_TRACE,
"envelope from (%s) not fully qualifed\n",
emailfrom);
return MU_ERR_BAD_822_FORMAT;
}
argc += 2; /* -f from */
}
if (to)
{
status = mu_address_get_email_count (to, &tocount);
if (status)
return status;
if (tocount == 0)
{
MU_DEBUG (mailer->debug, MU_DEBUG_TRACE,
"missing recipients\n");
return MU_ERR_NOENT;
}
argc += tocount; /* 1 per to address */
}
argc++; /* -t */
/* Allocate arg vec: */
if ((argvec = calloc (argc, sizeof (*argvec))) == 0)
return ENOMEM;
argc = 0;
if (mu_progmailer_sget_command (pm, &argvec[argc]) || argvec[argc] == NULL)
{
free (argvec);
return EINVAL;
}
argc++;
argvec[argc++] = "-oi";
if (from)
{
argvec[argc++] = "-f";
argvec[argc++] = emailfrom;
}
if (!to || mailer_property_is_set (mailer, "READ_RECIPIENTS"))
{
argvec[argc++] = "-t";
}
else
{
size_t i;
size_t count = 0;
mu_address_get_count (to, &count);
for (i = 1; i <= count; i++)
{
const char *email;
if ((status = mu_address_sget_email (to, i, &email)) != 0)
{
free (argvec);
MU_DEBUG2 (mailer->debug, MU_DEBUG_ERROR,
"cannot get email of recipient #%lu: %s\n",
(unsigned long) i, mu_strerror (status));
return status;
}
if (!email)
{
MU_DEBUG1 (mailer->debug, MU_DEBUG_TRACE,
"envelope to (%s) not fully qualifed\n",
email);
free (argvec);
return MU_ERR_BAD_822_FORMAT;
}
argvec[argc++] = email;
}
}
argvec[argc] = NULL;
mu_progmailer_set_debug (pm, mailer->debug);
status = mu_progmailer_open (pm, (char**) argvec);
if (status == 0)
{
status = mu_progmailer_send (pm, msg);
if (status == 0)
mu_observable_notify (mailer->observable, MU_EVT_MAILER_MESSAGE_SENT,
msg);
else
MU_DEBUG1 (mailer->debug, MU_DEBUG_ERROR,
"progmailer error: %s\n",
mu_strerror (status));
}
free (argvec);
return status;
}
static struct _mu_record _sendmail_record =
{
MU_SENDMAIL_PRIO,
MU_SENDMAIL_SCHEME,
_url_sendmail_init, /* url init. */
_mu_mailer_mailbox_init, /* Mailbox entry. */
_mu_mailer_sendmail_init, /* Mailer entry. */
_mu_mailer_folder_init, /* Folder entry. */
NULL, /* No need for a back pointer. */
NULL, /* _is_scheme method. */
NULL, /* _get_url method. */
NULL, /* _get_mailbox method. */
NULL, /* _get_mailer method. */
NULL /* _get_folder method. */
};
/* We export, url parsing and the initialisation of
the mailbox, via the register entry/record. */
mu_record_t mu_sendmail_record = &_sendmail_record;
#else
#include <stdio.h>
#include <registrar0.h>
mu_record_t mu_sendmail_record = NULL;
#endif