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
|
.\" $OpenBSD: filter_api.3,v 1.3 2015/12/14 08:24:33 jmc Exp $
.\"
.\" Copyright (c) 2015, Joerg Jung <jung@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: December 14 2015 $
.Dt FILTER_API 3
.Os
.Sh NAME
.Nm filter_api_loop ,
.Nm filter_api_accept ,
.Nm filter_api_reject ,
.Nm filter_api_reject_code ,
.Nm filter_api_writeln ,
.Nm filter_api_on_connect ,
.Nm filter_api_on_helo ,
.Nm filter_api_on_mail ,
.Nm filter_api_on_rcpt ,
.Nm filter_api_on_data ,
.Nm filter_api_on_dataline ,
.Nm filter_api_on_eom ,
.Nm filter_api_on_reset ,
.Nm filter_api_on_disconnect ,
.Nm filter_api_on_commit ,
.Nm filter_api_on_rollback ,
.Nm filter_api_setugid ,
.Nm filter_api_set_chroot ,
.Nm filter_api_no_chroot ,
.Nm filter_api_set_udata ,
.Nm filter_api_get_udata ,
.Nm filter_api_sockaddr_to_text ,
.Nm filter_api_mailaddr_to_text
.Nd filter API for smtpd
.Sh SYNOPSIS
.In smtpd-defines.h
.In smtpd-api.h
.Ft void
.Fn filter_api_loop "void"
.Ft int
.Fn filter_api_accept "uint64_t id"
.Ft int
.Fn filter_api_reject "uint64_t id" "enum filter_status status"
.Ft int
.Fn filter_api_reject_code "uint64_t id" "enum filter_status status" \
"uint32_t code" "const char *line"
.Ft void
.Fn filter_api_writeln "uint64_t id" "const char * line"
.Ft void
.Fn filter_api_on_connect "int(*cb)(uint64_t, struct filter_connect *)"
.Ft void
.Fn filter_api_on_helo "int(*cb)(uint64_t, const char *)"
.Ft void
.Fn filter_api_on_mail "int(*cb)(uint64_t, struct mailaddr *)"
.Ft void
.Fn filter_api_on_rcpt "int(*cb)(uint64_t, struct mailaddr *)"
.Ft void
.Fn filter_api_on_data "int(*cb)(uint64_t)"
.Ft void
.Fn filter_api_on_dataline "void(*cb)(uint64_t, const char *)"
.Ft void
.Fn filter_api_on_eom "int(*cb)(uint64_t, size_t)"
.Ft void
.Fn filter_api_on_reset "void(*cb)(uint64_t)"
.Ft void
.Fn filter_api_on_disconnect "void(*cb)(uint64_t)"
.Ft void
.Fn filter_api_on_commit "void(*cb)(uint64_t)"
.Ft void
.Fn filter_api_on_rollback "void(*cb)(uint64_t)"
.Ft void
.Fn filter_api_setugid "uid_t uid" "gid_t gid"
.Ft void
.Fn filter_api_set_chroot "const char *rootpath"
.Ft void
.Fn filter_api_no_chroot "void"
.Ft void
.Fn filter_api_set_udata "uint64_t id" "void *data"
.Ft void *
.Fn filter_api_get_udata "uint64_t id"
.Ft const char *
.Fn filter_api_sockaddr_to_text "const struct sockaddr *sa"
.Ft const char *
.Fn filter_api_mailaddr_to_text "const struct mailaddr *maddr"
.Sh DESCRIPTION
.Nm filter_api
is an interface intended to provide filter capabilities for the
.Xr smtpd 8
daemon.
.Nm filter_api
is used in standalone filter programs, which set up callback functions to hook
into the SMTP dialog.
For example, each SMTP command can be intercepted with callback functions.
.Pp
The function
.Fn filter_api_loop
is used to enter the filter loop, waiting for callback functions to be called.
.Pp
The function
.Fn filter_api_accept
can be called to accept the current message with the given
.Fa id
from within a callback function.
.Pp
The functions
.Fn filter_api_reject
and
.Fn filter_api_reject_code
can be called to reject the current message with the given
.Fa id
from within a callback function.
Both expect the
.Fa status
to be specified, which should be one of the following values:
.Bl -tag -width -Ds
.It Dv FILTER_FAIL
The filter failed and the message should not be accepted.
.It Dv FILTER_CLOSE
The filter is done and the message should not be accepted.
.El
.Pp
Moreover, with the function
.Fn filter_api_reject_code
a
.Fa code
and
.Fa line
can be specified, to reject the current message with a custom SMTP reply code
and error message.
.Pp
The function
.Fn filter_api_writeln
is intended to write (received) SMTP DATA command lines (back) from within a
callback function.
This might be used by a filter to add or modifiy DATA lines.
.Sh CALLBACK FUNCTIONS
The function
.Fn filter_api_on_connect
is called on new SMTP connections.
The callback can be used to accept or reject messages based on the given
connection information.
.Pp
The function
.Fn filter_api_on_helo
is called on SMTP HELO command.
The callback can be used to accept or reject messages based on the given HELO
string.
.Pp
The function
.Fn filter_api_on_mail
is called on SMTP MAIL FROM command.
The callback can be used to accept or reject messages based on the given MAIL
FROM address.
.Pp
The function
.Fn filter_api_on_rcpt
is called on SMTP RCPT TO command.
The callback can be used to accept or reject messages based on the given RCPT
TO address.
.Pp
The function
.Fn filter_api_on_data
is called on SMTP DATA command.
The callback might be used to set up data structures for filtering the upcoming
DATA lines.
.Pp
The function
.Fn filter_api_on_dataline
is called on each SMTP DATA line.
The callback can be used to accept or reject messaged based on the (header or
body) content of the current message.
.Pp
The function
.Fn filter_api_on_eom
is called on the end (of DATA command) of the current message.
The callback might be used to clean up data structures and finalize the decision
of accepting or rejecting the message.
.Pp
The function
.Fn filter_api_on_reset
is called on SMTP RSET command.
The callback might be used to reset the current filter state.
.Pp
The function
.Fn filter_api_on_disconnect
is called on closed SMTP connections.
The callback might be used to clean up earlier setup data structures.
.Pp
The function
.Fn filter_api_on_commit
is called on commit of the current message.
.\"The callback might be used to trigger further operations.
.Pp
The function
.Fn filter_api_on_rollback
is called on rollback of the SMTP session.
The callback might be used to clean up earlier setup data structures.
.Sh HELPER FUNCTIONS
The function
.Fn filter_api_setugid
can be called using
.Fa uid
and
.Fa gid
to set the running user and group of the filter.
.Pp
The function
.Fn filter_api_set_chroot
can be called using
.Fa rootpath
to set the chroot path of the filter.
.Pp
The function
.Fn filter_api_no_chroot
can be called to disable chroot for the filter.
.Pp
The function
.Fn filter_api_set_udata
can be called for the current message with the given
.Fa id
to set a user data pointer
.Fa data
from within a callback function.
.Pp
The function
.Fn filter_api_get_udata
can be called to get the user data pointer for the current message with the
given
.Fa id
from within a callback function.
.Pp
The function
.Fn filter_api_sockaddr_to_text
can be called to convert a socket address
.Fa sa
to a string.
.Pp
The function
.Fn filter_api_mailaddr_to_text
can be called to convert a mail address
.Fa maddr
to a string.
.Sh RETURN VALUES
The functions
.Fn filter_api_accept ,
.Fn filter_api_reject ,
and
.Fn filter_api_reject_code ,
return 1, intended to be used in a return statement of a callback function.
.Pp
The function
.Fn filter_api_get_udata
returns a pointer to the user data set for the current message.
.Pp
The functions
.Fn filter_api_sockaddr_to_text
and
.Fn filter_api_mailaddr_to_text
return a pointer to a static buffer containing the string representation.
The buffer remains valid until the next call to the conversion functions.
In case of error, the function
.Fn filter_api_sockaddr_to_text
returns the static string
.Dq Li (unknown)
and the function
.Fn filter_api_mailaddr_to_text
returns NULL.
.Sh EXAMPLES
.Xr filter-stub 8 ,
which ships with the OpenSMTPD-extras package,
is intended to provide an example code template.
.Sh ERRORS
The function
.Fn filter_api_sockaddr_to_text
may fail for any of the errors specified for the routine
.Xr getnameinfo 3
and listed in
.Xr gai_strerror 3 .
.Sh SEE ALSO
.Xr smtpd.conf 5 ,
.Xr filter-stub 8 ,
.Xr smtpd 8
.Sh HISTORY
The
.Nm filter_api
first appeared in
.Ox 5.9 .
|