mail.remote.c
7.51 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002, 2004,
2005, 2007 Free Software Foundation, Inc.
GNU Mailutils 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, or (at your option)
any later version.
GNU Mailutils 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 GNU Mailutils; 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
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <mailutils/address.h>
#include <mailutils/debug.h>
#include <mailutils/errno.h>
#include <mailutils/list.h>
#include <mailutils/mailer.h>
#include <mailutils/mutil.h>
#include <mailutils/message.h>
#include <mailutils/registrar.h>
#include <mailutils/stream.h>
#include <mailutils/property.h>
#include <mailutils/error.h>
#include <mailutils/nls.h>
#include <mailutils/mu_auth.h>
#include "mailutils/libargp.h"
#include "mu_umaxtostr.h"
const char *program_version = "mail.remote (" PACKAGE_STRING ")";
static char doc[] =
N_("GNU mail.remote -- pseudo-sendmail interface for mail delivery")
"\v"
N_("This is a simple drop-in replacement for sendmail to forward mail directly\n\
to an SMTP gateway.\n\
You should always specify your SMTP gateway using --mailer option\n\
(the best place to do so is in your configuration file).\n\
\n\
Examples:\n\
\n\
Deliver mail via SMTP gateway at \"mail.example.com\", reading its\n\
contents for recipients of the message.\n\
\n\
mail.remote --mailer smtp://mail.example.com -t\n\
\n\
Deliver mail only to \"devnull@foo.bar\"\n\
\n\
mail.remote --mailer smtp://mail.example.com devnull@foo.bar\n\
\n\
Deliver mail to \"devnull@foo.bar\" as well as to the recipients\n\
specified in the message itself:\n\
\n\
mail.remote --mailer smtp://mail.example.com -t devnull@foo.bar\n");
static struct argp_option options[] = {
{"from", 'f', N_("ADDR"), 0, N_("Override the default from address")},
{"read-recipients", 't', NULL, 0, N_("Read message for recipients.") },
{"debug", 'd', NULL, 0, N_("Print envelope commands in the SMTP protocol transaction. If specified more than once, the data part of the protocol transaction will also be printed.")},
{ NULL, 'o', N_("OPT"), 0, N_("Ignored for sendmail compatibility")},
{ NULL, 'b', N_("OPT"), 0, N_("Ignored for sendmail compatibility")},
{ NULL, 'i', NULL, 0, N_("Ignored for sendmail compatibility")},
{ NULL }
};
static int optdebug;
static const char *optfrom;
static int read_recipients; /* Read recipients from the message */
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
static struct mu_argp_node_list lst;
static int dbug = 0;
switch (key)
{
case 'f':
mu_argp_node_list_new (&lst, "from", arg);
break;
case 'd':
dbug++;
break;
case 'o':
case 'b':
case 'i':
break;
case 't':
mu_argp_node_list_new (&lst, "read-recipients", "yes");
break;
case ARGP_KEY_INIT:
mu_argp_node_list_init (&lst);
break;
case ARGP_KEY_FINI:
if (dbug)
mu_argp_node_list_new (&lst, "debug", mu_umaxtostr (0, dbug));
mu_argp_node_list_finish (&lst, NULL, NULL);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {
options,
parse_opt,
N_("[TO-ADDR]..."),
doc,
};
struct mu_cfg_param mail_remote_cfg_param[] = {
{ "from", mu_cfg_string, &optfrom, 0, NULL,
N_("Set sender email address."),
N_("email") },
{ "read-recipients", mu_cfg_string, &read_recipients, 0, NULL,
N_("Read recipient addresses from the message.") },
{ "debug", mu_cfg_int, &optdebug, 0, NULL,
N_("Set debug verbosity level. Level 1 prints envelope commands in "
"the SMTP protocol transaction. Levels 2 and above print the data "
"part of the transaction as well.") },
{ NULL }
};
static const char *capa[] = {
"auth",
"common",
"debug",
"mailer",
"address",
"license",
NULL
};
mu_mailer_t mailer; /* Mailer object */
mu_address_t from; /* Sender address */
mu_address_t to; /* Recipient addresses */
mu_stream_t in; /* Input stream */
void
mr_exit (int status)
{
mu_address_destroy (&from);
mu_address_destroy (&to);
mu_stream_destroy (&in, NULL);
mu_mailer_destroy (&mailer);
exit (status ? 1 : 0);
}
int
main (int argc, char **argv)
{
int status = 0;
int optind = 0;
mu_message_t msg = 0;
int mailer_flags = 0;
/* Native Language Support */
mu_init_nls ();
/* Register mailers. */
mu_registrar_record (mu_smtp_record);
MU_AUTH_REGISTER_ALL_MODULES();
mu_argp_init (program_version, NULL);
if (mu_app_init (&argp, capa, mail_remote_cfg_param,
argc, argv, 0, &optind, NULL))
exit (1);
if (optfrom)
{
if ((status = mu_address_create (&from, optfrom)))
{
mu_error (_("Parsing from addresses failed: %s"),
mu_strerror (status));
mr_exit (status);
}
}
if (argv[optind])
{
if ((status = mu_address_createv (&to, (const char **) (argv + optind), -1)))
{
mu_error (_("Parsing recipient addresses failed: %s"),
mu_strerror (status));
mr_exit (status);
}
}
if ((status = mu_stdio_stream_create (&in, stdin, MU_STREAM_SEEKABLE)))
{
mu_error (_("Failed: %s"), mu_strerror (status));
mr_exit (status);
}
if ((status = mu_stream_open (in)))
{
mu_error (_("Opening stdin failed: %s"), mu_strerror (status));
mr_exit (status);
}
if ((status = mu_message_create (&msg, NULL)))
{
mu_error (_("Failed: %s"), mu_strerror (status));
mr_exit (status);
}
if ((status = mu_message_set_stream (msg, in, NULL)))
{
mu_error (_("Failed: %s"), mu_strerror (status));
mr_exit (status);
}
if ((status = mu_mailer_create (&mailer, NULL)))
{
const char *url = NULL;
mu_mailer_get_url_default (&url);
mu_error (_("Creating mailer `%s' failed: %s"),
url, mu_strerror (status));
mr_exit (status);
}
if (optdebug)
{
mu_debug_t debug;
mu_mailer_get_debug (mailer, &debug);
mu_debug_set_level (debug, MU_DEBUG_LEVEL_UPTO (MU_DEBUG_PROT));
if (optdebug > 1)
mailer_flags = MAILER_FLAG_DEBUG_DATA;
}
if (read_recipients)
{
mu_property_t property = NULL;
mu_mailer_get_property (mailer, &property);
mu_property_set_value (property, "READ_RECIPIENTS", "true", 1);
}
if ((status = mu_mailer_open (mailer, mailer_flags)))
{
const char *url = NULL;
mu_mailer_get_url_default (&url);
mu_error (_("Opening mailer `%s' failed: %s"),
url, mu_strerror (status));
mr_exit (status);
}
if ((status = mu_mailer_send_message (mailer, msg, from, to)))
{
mu_error (_("Sending message failed: %s"), mu_strerror (status));
mr_exit (status);
}
if ((status = mu_mailer_close (mailer)))
{
mu_error (_("Closing mailer failed: %s"), mu_strerror (status));
mr_exit (status);
}
mr_exit (status);
}