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
|
/*
* Copyright (c) 2001-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: sasl.c,v 8.20 2004/06/02 22:48:06 ca Exp $")
#if SASL
# include <stdlib.h>
# include <sendmail.h>
# include <errno.h>
/*
** In order to ensure that storage leaks are tracked, and to prevent
** conflicts between the sm_heap package and sasl, we tell sasl to
** use the following heap allocation functions. Unfortunately,
** the sasl package incorrectly specifies the size of a block
** using unsigned long: for portability, it should be size_t.
*/
void *sm_sasl_malloc __P((unsigned long));
static void *sm_sasl_calloc __P((unsigned long, unsigned long));
static void *sm_sasl_realloc __P((void *, unsigned long));
void sm_sasl_free __P((void *));
/*
** SASLv1:
** We can't use an rpool for Cyrus-SASL memory management routines,
** since the encryption/decryption routines in Cyrus-SASL
** allocate/deallocate a buffer each time. Since rpool
** don't release memory until the very end, memory consumption is
** proportional to the size of an e-mail, which is unacceptable.
*/
/*
** SM_SASL_MALLOC -- malloc() for SASL
**
** Parameters:
** size -- size of requested memory.
**
** Returns:
** pointer to memory.
*/
void *
sm_sasl_malloc(size)
unsigned long size;
{
return sm_malloc((size_t) size);
}
/*
** SM_SASL_CALLOC -- calloc() for SASL
**
** Parameters:
** nelem -- number of elements.
** elemsize -- size of each element.
**
** Returns:
** pointer to memory.
**
** Notice:
** this isn't currently used by SASL.
*/
static void *
sm_sasl_calloc(nelem, elemsize)
unsigned long nelem;
unsigned long elemsize;
{
size_t size;
void *p;
size = (size_t) nelem * (size_t) elemsize;
p = sm_malloc(size);
if (p == NULL)
return NULL;
memset(p, '\0', size);
return p;
}
/*
** SM_SASL_REALLOC -- realloc() for SASL
**
** Parameters:
** p -- pointer to old memory.
** size -- size of requested memory.
**
** Returns:
** pointer to new memory.
*/
static void *
sm_sasl_realloc(o, size)
void *o;
unsigned long size;
{
return sm_realloc(o, (size_t) size);
}
/*
** SM_SASL_FREE -- free() for SASL
**
** Parameters:
** p -- pointer to free.
**
** Returns:
** none
*/
void
sm_sasl_free(p)
void *p;
{
sm_free(p);
}
/*
** SM_SASL_INIT -- sendmail specific SASL initialization
**
** Parameters:
** none.
**
** Returns:
** none
**
** Side Effects:
** installs memory management routines for SASL.
*/
void
sm_sasl_init()
{
sasl_set_alloc(sm_sasl_malloc, sm_sasl_calloc,
sm_sasl_realloc, sm_sasl_free);
}
/*
** INTERSECT -- create the intersection between two lists
**
** Parameters:
** s1, s2 -- lists of items (separated by single blanks).
** rpool -- resource pool from which result is allocated.
**
** Returns:
** the intersection of both lists.
*/
char *
intersect(s1, s2, rpool)
char *s1, *s2;
SM_RPOOL_T *rpool;
{
char *hr, *h1, *h, *res;
int l1, l2, rl;
if (s1 == NULL || s2 == NULL) /* NULL string(s) -> NULL result */
return NULL;
l1 = strlen(s1);
l2 = strlen(s2);
rl = SM_MIN(l1, l2);
res = (char *) sm_rpool_malloc(rpool, rl + 1);
if (res == NULL)
return NULL;
*res = '\0';
if (rl == 0) /* at least one string empty? */
return res;
hr = res;
h1 = s1;
h = s1;
/* walk through s1 */
while (h != NULL && *h1 != '\0')
{
/* is there something after the current word? */
if ((h = strchr(h1, ' ')) != NULL)
*h = '\0';
l1 = strlen(h1);
/* does the current word appear in s2 ? */
if (iteminlist(h1, s2, " ") != NULL)
{
/* add a blank if not first item */
if (hr != res)
*hr++ = ' ';
/* copy the item */
memcpy(hr, h1, l1);
/* advance pointer in result list */
hr += l1;
*hr = '\0';
}
if (h != NULL)
{
/* there are more items */
*h = ' ';
h1 = h + 1;
}
}
return res;
}
# if SASL >= 20000
/*
** IPTOSTRING -- create string for SASL_IP*PORT property
** (borrowed from lib/iptostring.c in Cyrus-IMAP)
**
** Parameters:
** addr -- (pointer to) socket address
** addrlen -- length of socket address
** out -- output string (result)
** outlen -- maximum length of output string
**
** Returns:
** true iff successful.
**
** Side Effects:
** creates output string if successful.
** sets errno if unsuccessful.
*/
# include <arpa/inet.h>
# ifndef NI_WITHSCOPEID
# define NI_WITHSCOPEID 0
# endif
# ifndef NI_MAXHOST
# define NI_MAXHOST 1025
# endif
# ifndef NI_MAXSERV
# define NI_MAXSERV 32
# endif
bool
iptostring(addr, addrlen, out, outlen)
SOCKADDR *addr;
SOCKADDR_LEN_T addrlen;
char *out;
unsigned outlen;
{
char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
if (addr == NULL || out == NULL)
{
errno = EINVAL;
return false;
}
# if NETINET6
if (getnameinfo((struct sockaddr *) addr, addrlen,
hbuf, sizeof hbuf, pbuf, sizeof pbuf,
NI_NUMERICHOST | NI_WITHSCOPEID | NI_NUMERICSERV) != 0)
return false;
# else /* NETINET6 */
if (addr->sa.sa_family != AF_INET)
{
errno = EINVAL;
return false;
}
if (sm_strlcpy(hbuf, inet_ntoa(addr->sin.sin_addr), sizeof(hbuf))
>= sizeof(hbuf))
{
errno = ENOMEM;
return false;
}
sm_snprintf(pbuf, sizeof pbuf, "%d", ntohs(addr->sin.sin_port));
# endif /* NETINET6 */
if (outlen < strlen(hbuf) + strlen(pbuf) + 2)
{
errno = ENOMEM;
return false;
}
sm_snprintf(out, outlen, "%s;%s", hbuf, pbuf);
return true;
}
# endif /* SASL >= 20000 */
#endif /* SASL */
|