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
|
/*
* Copyright (c) 1999-2002 Sendmail, Inc. and its suppliers.
* All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*
*/
#include <sm/gen.h>
SM_RCSID("@(#)$Id: comm.c,v 8.54.2.4 2002/12/03 17:32:45 ca Exp $")
#include "libmilter.h"
#include <sm/errstring.h>
#define FD_Z FD_ZERO(&readset); \
FD_SET((unsigned int) sd, &readset); \
FD_ZERO(&excset); \
FD_SET((unsigned int) sd, &excset)
/*
** MI_RD_CMD -- read a command
**
** Parameters:
** sd -- socket descriptor
** timeout -- maximum time to wait
** cmd -- single character command read from sd
** rlen -- pointer to length of result
** name -- name of milter
**
** Returns:
** buffer with rest of command
** (malloc()ed here, should be free()d)
** hack: encode error in cmd
*/
char *
mi_rd_cmd(sd, timeout, cmd, rlen, name)
socket_t sd;
struct timeval *timeout;
char *cmd;
size_t *rlen;
char *name;
{
ssize_t len;
mi_int32 expl;
ssize_t i;
fd_set readset, excset;
int ret;
int save_errno;
char *buf;
char data[MILTER_LEN_BYTES + 1];
*cmd = '\0';
*rlen = 0;
i = 0;
for (;;)
{
FD_Z;
ret = select(sd + 1, &readset, NULL, &excset, timeout);
if (ret == 0)
break;
else if (ret < 0)
{
if (errno == EINTR)
continue;
break;
}
if (FD_ISSET(sd, &excset))
{
*cmd = SMFIC_SELECT;
return NULL;
}
len = MI_SOCK_READ(sd, data + i, sizeof data - i);
if (MI_SOCK_READ_FAIL(len))
{
smi_log(SMI_LOG_ERR,
"%s, mi_rd_cmd: read returned %d: %s",
name, (int) len, sm_errstring(errno));
*cmd = SMFIC_RECVERR;
return NULL;
}
if (len == 0)
{
*cmd = SMFIC_EOF;
return NULL;
}
if (len >= (ssize_t) sizeof data - i)
break;
i += len;
}
if (ret == 0)
{
*cmd = SMFIC_TIMEOUT;
return NULL;
}
else if (ret < 0)
{
smi_log(SMI_LOG_ERR,
"%s: mi_rd_cmd: select returned %d: %s",
name, ret, sm_errstring(errno));
*cmd = SMFIC_RECVERR;
return NULL;
}
*cmd = data[MILTER_LEN_BYTES];
data[MILTER_LEN_BYTES] = '\0';
(void) memcpy((void *) &expl, (void *) &(data[0]), MILTER_LEN_BYTES);
expl = ntohl(expl) - 1;
if (expl <= 0)
return NULL;
if (expl > MILTER_CHUNK_SIZE)
{
*cmd = SMFIC_TOOBIG;
return NULL;
}
#if _FFR_ADD_NULL
buf = malloc(expl + 1);
#else /* _FFR_ADD_NULL */
buf = malloc(expl);
#endif /* _FFR_ADD_NULL */
if (buf == NULL)
{
*cmd = SMFIC_MALLOC;
return NULL;
}
i = 0;
for (;;)
{
FD_Z;
ret = select(sd + 1, &readset, NULL, &excset, timeout);
if (ret == 0)
break;
else if (ret < 0)
{
if (errno == EINTR)
continue;
break;
}
if (FD_ISSET(sd, &excset))
{
*cmd = SMFIC_SELECT;
free(buf);
return NULL;
}
len = MI_SOCK_READ(sd, buf + i, expl - i);
if (MI_SOCK_READ_FAIL(len))
{
smi_log(SMI_LOG_ERR,
"%s: mi_rd_cmd: read returned %d: %s",
name, (int) len, sm_errstring(errno));
ret = -1;
break;
}
if (len == 0)
{
*cmd = SMFIC_EOF;
free(buf);
return NULL;
}
if (len > expl - i)
{
*cmd = SMFIC_RECVERR;
free(buf);
return NULL;
}
if (len >= expl - i)
{
*rlen = expl;
#if _FFR_ADD_NULL
/* makes life simpler for common string routines */
buf[expl] = '\0';
#endif /* _FFR_ADD_NULL */
return buf;
}
i += len;
}
save_errno = errno;
free(buf);
/* select returned 0 (timeout) or < 0 (error) */
if (ret == 0)
{
*cmd = SMFIC_TIMEOUT;
return NULL;
}
if (ret < 0)
{
smi_log(SMI_LOG_ERR,
"%s: mi_rd_cmd: select returned %d: %s",
name, ret, sm_errstring(save_errno));
*cmd = SMFIC_RECVERR;
return NULL;
}
*cmd = SMFIC_UNKNERR;
return NULL;
}
/*
** MI_WR_CMD -- write a cmd to sd
**
** Parameters:
** sd -- socket descriptor
** timeout -- maximum time to wait (currently unused)
** cmd -- single character command to write
** buf -- buffer with further data
** len -- length of buffer (without cmd!)
**
** Returns:
** MI_SUCCESS/MI_FAILURE
*/
/*
** we don't care much about the timeout here, it's very long anyway
** FD_SETSIZE is checked when socket is created.
** XXX l == 0 ?
*/
#define MI_WR(data) \
while (sl > 0) \
{ \
FD_ZERO(&wrtset); \
FD_SET((unsigned int) sd, &wrtset); \
ret = select(sd + 1, NULL, &wrtset, NULL, timeout); \
if (ret == 0) \
return MI_FAILURE; \
if (ret < 0) \
{ \
if (errno == EINTR) \
continue; \
else \
return MI_FAILURE; \
} \
l = MI_SOCK_WRITE(sd, (void *) ((data) + i), sl); \
if (l < 0) \
{ \
if (errno == EINTR) \
continue; \
else \
return MI_FAILURE; \
} \
i += l; \
sl -= l; \
}
int
mi_wr_cmd(sd, timeout, cmd, buf, len)
socket_t sd;
struct timeval *timeout;
int cmd;
char *buf;
size_t len;
{
size_t sl, i;
ssize_t l;
mi_int32 nl;
int ret;
fd_set wrtset;
char data[MILTER_LEN_BYTES + 1];
if (len > MILTER_CHUNK_SIZE)
return MI_FAILURE;
nl = htonl(len + 1); /* add 1 for the cmd char */
(void) memcpy(data, (void *) &nl, MILTER_LEN_BYTES);
data[MILTER_LEN_BYTES] = (char) cmd;
i = 0;
sl = MILTER_LEN_BYTES + 1;
/* use writev() instead to send the whole stuff at once? */
MI_WR(data);
if (len > 0 && buf == NULL)
return MI_FAILURE;
if (len == 0 || buf == NULL)
return MI_SUCCESS;
i = 0;
sl = len;
MI_WR(buf);
return MI_SUCCESS;
}
|