-
Notifications
You must be signed in to change notification settings - Fork 6
/
smtp-etrn.c
351 lines (297 loc) · 9.18 KB
/
smtp-etrn.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* This file is part of libESMTP, a library for submission of RFC 2822
* formatted electronic mail messages using the SMTP protocol described
* in RFC 2821.
*
* Copyright (C) 2001,2002 Brian Stafford <[email protected]>
*
* 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 2.1 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <config.h>
#ifdef USE_ETRN
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <missing.h> /* declarations for missing library functions */
#include "libesmtp-private.h"
#include "siobuf.h"
#include "protocol.h"
#include "attribute.h"
/**
* DOC: RFC 1985
*
* Remote Message Queue Starting (ETRN)
* ------------------------------------
*
* The SMTP ETRN extension is used to request a remote MTA to start its
* delivery queue for the specified domain. If the application requests
* the use if the ETRN extension and the remote MTA does not list ETRN,
* libESMTP will use the event callback to notify the application.
*/
struct smtp_etrn_node
{
struct smtp_etrn_node *next;
struct smtp_session *session; /* Back reference */
/* Application data */
void *application_data; /* Pointer to data maintained by app */
void (*release) (void *); /* function to free/unref data */
/* Node Info */
int option; /* Option character */
char *domain; /* Remote queue to start */
/* Status */
smtp_status_t status; /* Status from MTA greeting */
};
/**
* smtp_etrn_add_node() - create an ETRN node.
* @session: The session.
* @option: The option character.
* @domain: Request mail for this domain.
*
* Add an ETRN node to the SMTP session.
*
* Return: An &typedef smtp_etrn_node_t or %NULL on failure.
*/
smtp_etrn_node_t
smtp_etrn_add_node (smtp_session_t session, int option, const char *domain)
{
smtp_etrn_node_t node;
char *dup_domain;
SMTPAPI_CHECK_ARGS (session != NULL
&& domain != NULL
&& (option == 0 || option == '@'), NULL);
if ((node = malloc (sizeof (struct smtp_etrn_node))) == NULL)
{
set_errno (ENOMEM);
return NULL;
}
if ((dup_domain = strdup (domain)) == NULL)
{
free (node);
set_errno (ENOMEM);
return NULL;
}
memset (node, 0, sizeof (struct smtp_etrn_node));
node->session = session;
node->option = option;
node->domain = dup_domain;
APPEND_LIST (session->etrn_nodes, session->end_etrn_nodes, node);
session->required_extensions |= EXT_ETRN;
return node;
}
/**
* smtp_etrn_enumerate_nodes() - Call function for each ETRN node in session.
* @session: The session.
* @cb: Callback function
* @arg: Argument (closure) passed to callback.
*
* Call the callback function once for each ETRN node in the SMTP session.
*
* Return: Zero on failure, non-zero on success.
*/
int
smtp_etrn_enumerate_nodes (smtp_session_t session,
smtp_etrn_enumerate_nodecb_t cb, void *arg)
{
smtp_etrn_node_t node;
SMTPAPI_CHECK_ARGS (session != NULL && cb != NULL, 0);
for (node = session->etrn_nodes; node != NULL; node = node->next)
(*cb) (node, node->option, node->domain, arg);
return 1;
}
/**
* smtp_etrn_node_status() - Retrieve ETRN status.
* @node: An &smtp_etrn_node_t returned by smtp_etrn_add_node()
*
* Retrieve the ETRN node success/failure status from a previous SMTP session.
* This includes SMTP status codes, RFC 2034 enhanced status codes, if
* available and text from the server describing the status.
*
* Return: %NULL if no status information is available,
* otherwise a pointer to the status information. The pointer remains valid
* until the next call to libESMTP in the same thread.
*/
const smtp_status_t *
smtp_etrn_node_status (smtp_etrn_node_t node)
{
SMTPAPI_CHECK_ARGS (node != NULL, NULL);
return &node->status;
}
/**
* smtp_etrn_set_application_data() - Associate data with an ETRN node.
* @node: An &smtp_etrn_node_t returned by smtp_etrn_add_node()
* @data: Application data to set
*
* Associate application defined data with the opaque ETRN structure.
*
* Only available when %LIBESMTP_ENABLE_DEPRECATED_SYMBOLS is defined.
* Use smtp_etrn_set_application_data_release() instead.
*
* Return: Previously set application data or %NULL.
*/
void *
smtp_etrn_set_application_data (smtp_etrn_node_t node, void *data)
{
void *old;
SMTPAPI_CHECK_ARGS (node != NULL, NULL);
old = node->application_data;
node->application_data = data;
node->release = NULL;
return old;
}
/**
* smtp_etrn_set_application_data_release() - Associate data with an ETRN node.
* @node: An &smtp_etrn_node_t returned by smtp_etrn_add_node()
* @data: Application data
* @release: function to free/unref data.
*
* Associate application data with the ETRN node.
* If @release is non-NULL it is called to free or unreference data when
* changed or the session is destroyed.
*/
void
smtp_etrn_set_application_data_release (smtp_etrn_node_t node, void *data,
void (*release) (void *))
{
SMTPAPI_CHECK_ARGS (node != NULL, /* void */);
if (node->application_data != NULL && node->release != NULL)
(*node->release) (node->application_data);
node->release = release;
node->application_data = data;
}
/**
* smtp_etrn_get_application_data() - Get data from an ETRN node.
* @node: An &smtp_etrn_node_t returned by smtp_etrn_add_node()
*
* Retrieve application data from the opaque ETRN structure.
*
* Return: Application data or %NULL.
*/
void *
smtp_etrn_get_application_data (smtp_etrn_node_t node)
{
SMTPAPI_CHECK_ARGS (node != NULL, NULL);
return node->application_data;
}
int
check_etrn (smtp_session_t session)
{
return (session->extensions & EXT_ETRN) && session->etrn_nodes != NULL;
}
void
destroy_etrn_nodes (smtp_session_t session)
{
smtp_etrn_node_t node, next;
for (node = session->etrn_nodes; node != NULL; node = next)
{
next = node->next;
if (node->application_data != NULL && node->release != NULL)
(*node->release) (node->application_data);
free (node->domain);
free (node);
}
session->etrn_nodes = session->end_etrn_nodes = NULL;
session->cmd_etrn_node = session->rsp_etrn_node = NULL;
}
void
cmd_etrn (siobuf_t conn, smtp_session_t session)
{
smtp_etrn_node_t node;
if (session->cmd_etrn_node == NULL)
session->cmd_etrn_node = session->etrn_nodes;
node = session->cmd_etrn_node;
sio_printf (conn, "ETRN %c%s\r\n",
node->option ? node->option : ' ', node->domain);
session->cmd_etrn_node = session->cmd_etrn_node->next;
if (session->cmd_etrn_node != NULL)
session->cmd_state = S_etrn;
else if (session->cmd_recipient != NULL)
session->cmd_state = initial_transaction_state (session);
else
session->cmd_state = S_quit;
}
void
rsp_etrn (siobuf_t conn, smtp_session_t session)
{
int code;
smtp_etrn_node_t node;
if (session->rsp_etrn_node == NULL)
session->rsp_etrn_node = session->etrn_nodes;
node = session->rsp_etrn_node;
code = read_smtp_response (conn, session, &node->status, NULL);
if (code < 0)
{
session->rsp_state = S_quit;
return;
}
/* Notify the ETRN status */
if (session->event_cb != NULL)
(*session->event_cb) (session, SMTP_EV_ETRNSTATUS, session->event_cb_arg,
node->option, node->domain);
session->rsp_etrn_node = session->rsp_etrn_node->next;
if (session->rsp_etrn_node != NULL)
session->rsp_state = S_etrn;
else if (session->rsp_recipient != NULL)
session->rsp_state = initial_transaction_state (session);
else
session->rsp_state = S_quit;
}
#else
#include <stdlib.h>
#include <errno.h>
#include "libesmtp-private.h"
smtp_etrn_node_t
smtp_etrn_add_node (smtp_session_t session, int option, const char *domain)
{
SMTPAPI_CHECK_ARGS (session != NULL
&& domain != NULL
&& (option == 0 || option == '@'), NULL);
return NULL;
}
int
smtp_etrn_enumerate_nodes (smtp_session_t session,
smtp_etrn_enumerate_nodecb_t cb,
void *arg __attribute__ ((unused)))
{
SMTPAPI_CHECK_ARGS (session != NULL && cb != NULL, 0);
return 0;
}
const smtp_status_t *
smtp_etrn_node_status (smtp_etrn_node_t node)
{
SMTPAPI_CHECK_ARGS (node != NULL, NULL);
return NULL;
}
void *
smtp_etrn_set_application_data (smtp_etrn_node_t node,
void *data __attribute__ ((unused)))
{
SMTPAPI_CHECK_ARGS (node != NULL, NULL);
return NULL;
}
void
smtp_etrn_set_application_data_release (smtp_etrn_node_t node
__attribute__ ((unused)),
void *data __attribute__ ((unused)),
void (*release) (void *)
__attribute__ ((unused)))
{
}
void *
smtp_etrn_get_application_data (smtp_etrn_node_t node)
{
SMTPAPI_CHECK_ARGS (node != NULL, NULL);
return NULL;
}
#endif