-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosmtpd_run.3
More file actions
513 lines (513 loc) · 14.2 KB
/
osmtpd_run.3
File metadata and controls
513 lines (513 loc) · 14.2 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
.\" $OpenBSD$
.\"
.\" Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd $Mdocdate$
.Dt OSMTPD_RUN 3
.Os
.Sh NAME
.Nm osmtpd_register_filter_connect ,
.Nm osmtpd_register_filter_helo ,
.Nm osmtpd_register_filter_ehlo ,
.Nm osmtpd_register_filter_starttls ,
.Nm osmtpd_register_filter_auth ,
.Nm osmtpd_register_filter_mailfrom ,
.Nm osmtpd_register_filter_rcptto ,
.Nm osmtpd_register_filter_data ,
.Nm osmtpd_register_filter_dataline ,
.Nm osmtpd_register_filter_rset ,
.Nm osmtpd_register_filter_quit ,
.Nm osmtpd_register_filter_noop ,
.Nm osmtpd_register_filter_help ,
.Nm osmtpd_register_filter_wiz ,
.Nm osmtpd_register_filter_commit ,
.Nm osmtpd_register_report_connect ,
.Nm osmtpd_register_report_disconnect ,
.Nm osmtpd_register_report_identify ,
.Nm osmtpd_register_report_tls ,
.Nm osmtpd_register_report_begin ,
.Nm osmtpd_register_report_mail ,
.Nm osmtpd_register_report_rcpt ,
.Nm osmtpd_register_report_envelope ,
.Nm osmtpd_register_report_data ,
.Nm osmtpd_register_report_commit ,
.Nm osmtpd_register_report_rollback ,
.Nm osmtpd_register_report_client ,
.Nm osmtpd_register_report_server ,
.Nm osmtpd_register_report_response ,
.Nm osmtpd_register_report_timeout ,
.Nm osmtpd_local_session ,
.Nm osmtpd_local_message ,
.Nm osmtpd_need ,
.Nm osmtpd_filter_proceed ,
.Nm osmtpd_filter_reject ,
.Nm osmtpd_filter_disconnect ,
.Nm osmtpd_filter_rewrite ,
.Nm osmtpd_filter_dataline ,
.Nm osmtpd_run ,
.Nm osmtpd_err ,
.Nm osmtpd_errx
.Nd C filter API for
.Xr smtpd 8
.Sh SYNOPSIS
.In opensmtpd.h
.Ft void
.Fo osmtpd_register_filter_connect
.Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *hostname, struct sockaddr_storage *ss)"
.Fc
.Ft void
.Fo osmtpd_register_filter_helo
.Fa "void (cb*)(struct osmtpd_ctx *ctx, const char *helo)"
.Fc
.Ft void
.Fo osmtpd_register_filter_ehlo
.Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *ehlo)"
.Fc
.Ft void
.Fo osmtpd_register_filter_starttls
.Fa "void (*cb)(struct osmtpd_ctx *ctx)"
.Fc
.Ft void
.Fo osmtpd_register_filter_auth
.Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *auth)"
.Fc
.Ft void
.Fo osmtpd_register_filter_mailfrom
.Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *from)"
.Fc
.Ft void
.Fo osmtpd_register_filter_rcptto
.Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *rcpt)"
.Fc
.Ft void
.Fo osmtpd_register_filter_data
.Fa "void (*cb)(struct osmtpd_ctx *ctx)"
.Fc
.Ft void
.Fo osmtpd_register_filter_dataline
.Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *line)"
.Fc
.Ft void
.Fo osmtpd_register_filter_rset
.Fa "void (*cb)(struct osmtpd_ctx *ctx)"
.Fc
.Ft void
.Fo osmtpd_register_filter_quit
.Fa "void (*cb)(struct osmtpd_ctx *ctx)"
.Fc
.Ft void
.Fo osmtpd_register_filter_noop
.Fa "void (*cb)(struct osmtpd_ctx *ctx)"
.Fc
.Ft void
.Fo osmtpd_register_filter_help
.Fa "void (*cb)(struct osmtpd_ctx *ctx)"
.Fc
.Ft void
.Fo osmtpd_register_filter_wiz
.Fa "void (*cb)(struct osmtpd_ctx *ctx)"
.Fc
.Ft void
.Fo osmtpd_register_filter_commit
.Fa "void (*cb)(struct osmtpd_ctx *ctx)"
.Fc
.Ft void
.Fo osmtpd_register_report_connect
.Fa "int incoming"
.Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *rdns, enum osmtpd_status fcrdns, struct sockaddr_storage *src, struct sockaddr_storage *dst)"
.Fc
.Ft void
.Fo osmtpd_register_report_disconnect
.Fa "int incoming"
.Fa "void (*ctx)(struct osmtpd_ctx *ctx)"
.Fc
.Ft void
.Fo osmtpd_register_report_identify
.Fa "int incoming"
.Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *identity)"
.Fc
.Ft void
.Fo osmtpd_register_report_tls
.Fa "int incoming"
.Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *ciphers)"
.Fc
.Ft void
.Fo osmtpd_register_report_begin
.Fa "int incoming"
.Fa "void (*cb)(struct osmtpd_ctx *ctx, uint32_t msgid)"
.Fc
.Ft void
.Fo osmtpd_register_report_mail
.Fa "int incoming"
.Fa "void (*cb)(struct osmtpd_ctx *ctx, uint32_t msgid, const char *mailfrom, enum osmtpd_status status)"
.Fc
.Ft void
.Fo osmtpd_register_report_rcpt
.Fa "int incoming"
.Fa "void (*cb)(struct osmtpd_ctx *ctx, uint32_t msgid, const char *rcptto, enum osmtpd_status status)"
.Fc
.Ft void
.Fo osmtpd_register_report_envelope
.Fa "int incoming"
.Fa "void (*cb)(struct osmtpd_ctx *ctx, uint32_t msgid, uint64_t evpid)"
.Fc
.Ft void
.Fo osmtpd_register_report_data
.Fa "int incoming"
.Fa "void (*cb)(struct osmtpd_ctx *ctx, uint32_t msgid, enum osmtpd_status status)"
.Fc
.Ft void
.Fo osmtpd_register_report_commit
.Fa int incoming
.Fa "void (*cb)(struct osmtpd_ctx *ctx, uint32_t msgid, size_t msgsz)"
.Fc
.Ft void
.Fo osmtpd_register_report_rollback
.Fa "int incoming"
.Fa "void (*cb)(struct osmtpd_ctx *ctx, uint32_t msgid)"
.Fc
.Ft void
.Fo osmtpd_register_report_client
.Fa "int incoming"
.Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *cmd)"
.Fc
.Ft void
.Fo osmtpd_register_report_server
.Fa "int incoming"
.Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *resp)"
.Fc
.Ft void
.Fo osmtpd_register_report_response
.Fa "int incoming"
.Fa "void (*cb)(struct osmtpd_ctx *ctx, const char *resp)"
.Fc
.Ft void
.Fo osmtpd_register_report_timeout
.Fa "int incoming"
.Fa "void (*cb)(struct osmtpd_ctx *ctx)"
.Fc
.Ft void
.Fo osmtpd_local_session
.Fa "void *(*oncreate)(struct osmtpd_ctx *ctx)"
.Fa "void (*ondelete)(struct osmtpd_ctx *ctx, void *data)"
.Fc
.Ft void
.Fo osmtpd_local_message
.Fa "void *(*oncreate)(struct osmtpd_ctx *ctx)"
.Fa "void (*ondelete)(struct osmtpd_ctx *ctx, void *data)"
.Fc
.Ft void
.Fn osmtpd_need "int needs"
.Ft void
.Fn osmtpd_filter_proceed "struct osmtpd_ctx *ctx"
.Ft void
.Fn osmtpd_filter_reject "struct osmtpd_ctx *ctx" "int error" "const char *msg" ...
.Ft void
.Fn osmtpd_filter_disconnect "struct osmtpd_ctx *ctx" "const char *msg" ...
.Ft void
.Fn osmtpd_filter_rewrite "struct osmtpd_ctx *ctx" "const char *value" ...
.Ft void
.Fn osmtpd_filter_dataline "struct osmtpd_ctx *ctx" "const char *line" ...
.Ft void
.Fn osmtpd_run void
.Ft void
.Fn osmtpd_err "int eval" "const char *fmt" ...
.Ft void
.Fn osmtpd_errx "int eval" "const char *fmt" ...
.Sh DESCRIPTION
The
.Nm osmtpd
API is an event based interface for writing
.Xr smtpd 8
filters.
Filter and report callbacks are registered via the
.Nm osmtpd_register
class of functions, followed by
.Nm osmtpd_run .
.Pp
.Nm osmtpd_run
starts the communication with the server and transforms network queries to
callbacks.
Internally it uses
.Xr event_dispatch 3 ,
which allows filters to be written fully asynchronously.
.Pp
Each callback
.Fa cb
gets at least a pointer of the type
.Fa struct osmtpd_ctx .
It contains the following elements:
.Bl -tag -width Ds
.It Fa "enum osmtpd_type type"
The type of request being made.
The possible values are
.Dv OSMTPD_TYPE_FILTER
and
.Dv OSMTPD_TYPE_REPORT .
.It Vt "enum osmtpd_phase" Va phase
The phase in the transaction which triggered the callback.
The following values match their respective
.Nm osmtpd_register
function:
.Bl -tag -compact -width OSMTPD_PHASE_LINK_DISCONNECT
.It Dv OSMTPD_PHASE_CONNECT
.Nm osmtpd_register_filter_connect
.It Dv OSMTPD_PHASE_HELO
.Nm osmtpd_register_filter_helo
.It Dv OSMTPD_PHASE_EHLO
.Nm osmtpd_register_filter_ehlo
.It Dv OSMTPD_PHASE_STARTTLS
.Nm osmtpd_register_filter_starttls
.It Dv OSMTPD_PHASE_AUTH
.Nm osmtpd_register_filter_auth
.It Dv OSMTPD_PHASE_MAIL_FROM
.Nm osmtpd_register_filter_mailfrom
.It Dv OSMTPD_PHASE_RCPT_TO
.Nm osmtpd_register_filter_rcptto
.It Dv OSMTPD_PHASE_DATA
.Nm osmtpd_register_filter_data
.It Dv OSMTPD_PHASE_DATA_LINE
.Nm osmtpd_register_filter_dataline
.It Dv OSMTPD_PHASE_RSET
.Nm osmtpd_register_filter_rset
.It Dv OSMTPD_PHASE_QUIT
.Nm osmtpd_register_filter_quit
.It Dv OSMTPD_PHASE_NOOP
.Nm osmtpd_register_filter_noop
.It Dv OSMTPD_PHASE_HELP
.Nm osmtpd_register_filter_help
.It Dv OSMTPD_PHASE_WIZ
.Nm osmtpd_register_filter_wiz
.It Dv OSMTPD_PHASE_COMMIT
.Nm osmtpd_register_filter_commit
.It Dv OSMTPD_PHASE_LINK_CONNECT
.Nm osmtpd_register_report_connect
.It OSMTPD_PHASE_LINK_DISCONNECT
.Nm osmtpd_register_report_disconnect
.It Dv OSMTPD_PHASE_LINK_IDENTIFY
.Nm osmtpd_register_report_identify
.It Dv OSMTPD_PHASE_LINK_TLS
.Nm osmtpd_register_report_tls
.It Dv OSMTPD_PHASE_TX_BEGIN
.Nm osmtpd_register_report_begin
.It Dv OSMTPD_PHASE_TX_MAIL
.Nm osmtpd_register_report_mail
.It Dv OSMTPD_PHASE_TX_RCPT
.Nm osmtpd_register_report_rcpt
.It Dv OSMTPD_PHASE_TX_ENVELOPE
.Nm osmtpd_register_report_envelope
.It Dv OSMTPD_PHASE_TX_DATA
.Nm osmtpd_register_report_data
.It Dv OSMTPD_PHASE_TX_COMMIT
.Nm osmtpd_register_report_commit
.It Dv OSMTPD_PHASE_TX_ROLLBACK
.Nm osmtpd_register_report_rollback
.It Dv OSMTPD_PHASE_PROTOCOL_CLIENT
.Nm osmtpd_register_report_client
.It Dv OSMTPD_PHASE_PROTOCOL_SERVER
.Nm osmtpd_register_report_server
.It Dv OSMTPD_PHASE_FILTER_RESPONSE
.Nm osmtpd_register_report_response
.It Dv OSMTPD_PHASE_TIMEOUT .
.Nm osmtpd_register_report_timeout
.El
.It Vt int Va version_major
The major version number of the protocol.
Most filters don't need this information.
.It Vt int Va version_minor
The minor version number of the protocol.
Most filters don't need this information.
.It Vt "struct timespec" Va tm
The time the event was triggered inside
.Xr smtpd 8 .
.It Vt int Va incoming
Set to 1 if the event was based on an incoming connection, 0 if it's an outgoing
connection.
The
.Nm osmtpd_register_filter
class of functions is always based on incoming connections.
.Nm osmtpd_register_report
can be both incoming and outgoing.
.It Vt uint64_t Va reqid
The request ID of the connection the event was issued on.
This value can be useful for logging.
Filters in need of filter specific data can use
.Nm osmtpd_local_session
and
.Va local_session .
.It Vt uint64_t Va token
The filter specific token.
Most filters don't need this information.
.It Vt "struct sockaddr_storage" Va src
The source address and port of the connection.
This needs to be cast to the appropriate sockaddr type based on the
.Va ss_family
attribute.
It can have the following families:
.Dv AF_INET ,
.Dv AF_INET6
and
.Dv AF_UNIX .
To use this attribute, initialize
.Nm osmtpd_need
with
.Dv OSMTPD_NEED_SRC .
If not available the entire attribute is zeroed out.
.It Vt "struct sockaddr_storage" Va dst
The destination address and port of the connection.
This needs to be cast to the appropriate sockaddr type based on the
.Va ss_family
attribute.
It can have the following families:
.Dv AF_INET ,
.Dv AF_INET6
and
.Dv AF_UNIX .
To use this attribute, initialize
.Nm osmtpd_need
with
.Dv OSMTPD_NEED_DST .
If not available the entire attribute is zeroed out.
.It Vt char Va *rdns
The reverse DNS hostname of the connection.
To use this attribute, initialize
.Nm osmtpd_need
with
.Dv OSMTPD_NEED_RDNS .
If not available the attribute is set to
.Dv NULL .
.It Vt enum osmtpd_status Va fcrdns
Whether the reverse DNS hostname is forward confirmed.
To use this attribute, initialize
.Nm osmtpd_need
with
.Dv OSMTPD_NEED_FCRDNS .
If not available the attribute is set to
.Dv OSMTPD_STATUS_TEMPFAIL .
.It Vt char Va *identity
The identity of the remote host as presented by the HELO or EHLO SMTP command.
To use this attribute, initialize
.Nm osmtpd_need
with
.Dv OSMTPD_NEED_IDENTITY .
If not available the attribute is set to
.Dv NULL .
.It Vt char Va *ciphers
The ciphers used during
.Po start Pc Ns tls .
To use this attribute, initialize
.Nm osmtpd_need
with
.Dv OSMTPD_NEED_CIPHERS .
If not available the attribute is set to
.Dv NULL .
.It Vt uint32_t Va msgid
The message ID of the current message being handled in the SMTP transaction.
This value can be useful for logging.
.Nm osmtpd_need
needs to be initialized with
.Dv OSMTPD_NEED_MSGID .
If not available the attribute is set to
.Dv 0 .
Filters in need of filter specific data can use
.Nm osmtpd_local_message
and
.Va local_message .
.It Vt char Va *mailfrom
The envelope MAIL FROM address in the SMTP transaction.
.Nm osmtpd_need
needs to be initialized with
.Dv OSMTPD_NEED_MAILFROM .
If not available the attribute is set to
.Dv NULL .
.It Vt char Va **rcptto
The envelope RCPT TO address in the SMTP transaction.
.Nm osmtpd_need
needs to be initialized with
.Dv OSMTPD_NEED_RCPTTO .
This attribute is a NULL-terminated array of address strings.
If not available the first element in the array is set to
.Dv NULL .
.It Vt uint64_t Va evpid
The envelope ID we're currently working on.
.Nm osmtpd_need
needs to be initialized with
.Dv OSMTPD_NEED_EVPID .
If not available the attribute is set to
.Dv 0 .
.It Vt void Va *local_session
Any filter specific data that needs to be stored during the session.
This is initialized on
.Fa ctx
creation by calling
.Fa oncreate
argument from
.Nm osmtpd_local_session .
.It Vt void Va *local_message
Any filter specific data that needs to be stored during the message transaction.
This is initialized on
.Fa ctx
creation by calling
.Fa oncreate
argument from
.Nm osmtpd_local_message .
.El
.Pp
The
.Nm osmtpd_register_filter
class of functions must call one of
.Nm osmtpd_filter_proceed ,
.Nm osmtpd_filter_rewrite ,
.Nm osmtpd_filter_reject
and
.Nm osmtpd_filter_disconnect .
This can be done either in the callback function itself, or at a later moment
through another callback.
Note that the session stalls until the
.Nm osmtpd_filter
has been called.
.Pp
Exceptions to the above reply options are:
.Pp
.Bl -bullet -compact -width Ds
.It
.Nm osmtpd_register_filter_connect Ns 's
and
.Nm osmtpd_register_filter
functions' callbacks without argument can't use
.Nm osmtpd_filter_rewrite .
.It
.Nm osmtpd_register_filter_dataline Ns 's
callback can only use osmtpd_filter_dataline.
.El
.Pp
.Nm osmtpd_err
and
.Nm osmtpd_errx
can be used as a standin for
.Xr err 3
and
.Xr errx 3
without printing the program name to stderr.
.Sh SEE ALSO
.Xr event_init 3 ,
.Xr smtpd.conf 5
.Sh HISTORY
The
.Nm osmtpd_run
API first appeared in
.Ox 6.6 .
.Sh AUTHORS
.An Martijn van Duren Aq Mt martijn@openbsd.org