2 * Copyright (c) 2001-2002 Sendmail, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
12 SM_RCSID("@(#)$Id: sasl.c,v 8.22 2006/08/15 23:24:57 ca Exp $")
16 # include <sendmail.h>
20 ** In order to ensure that storage leaks are tracked, and to prevent
21 ** conflicts between the sm_heap package and sasl, we tell sasl to
22 ** use the following heap allocation functions. Unfortunately,
23 ** the sasl package incorrectly specifies the size of a block
24 ** using unsigned long: for portability, it should be size_t.
27 void *sm_sasl_malloc __P((unsigned long));
28 static void *sm_sasl_calloc __P((unsigned long, unsigned long));
29 static void *sm_sasl_realloc __P((void *, unsigned long));
30 void sm_sasl_free __P((void *));
34 ** We can't use an rpool for Cyrus-SASL memory management routines,
35 ** since the encryption/decryption routines in Cyrus-SASL
36 ** allocate/deallocate a buffer each time. Since rpool
37 ** don't release memory until the very end, memory consumption is
38 ** proportional to the size of an e-mail, which is unacceptable.
42 ** SM_SASL_MALLOC -- malloc() for SASL
45 ** size -- size of requested memory.
55 return sm_malloc((size_t) size);
59 ** SM_SASL_CALLOC -- calloc() for SASL
62 ** nelem -- number of elements.
63 ** elemsize -- size of each element.
69 ** this isn't currently used by SASL.
73 sm_sasl_calloc(nelem, elemsize)
75 unsigned long elemsize;
80 size = (size_t) nelem * (size_t) elemsize;
84 memset(p, '\0', size);
89 ** SM_SASL_REALLOC -- realloc() for SASL
92 ** p -- pointer to old memory.
93 ** size -- size of requested memory.
96 ** pointer to new memory.
100 sm_sasl_realloc(o, size)
104 return sm_realloc(o, (size_t) size);
108 ** SM_SASL_FREE -- free() for SASL
111 ** p -- pointer to free.
125 ** SM_SASL_INIT -- sendmail specific SASL initialization
134 ** installs memory management routines for SASL.
140 sasl_set_alloc(sm_sasl_malloc, sm_sasl_calloc,
141 sm_sasl_realloc, sm_sasl_free);
144 ** INTERSECT -- create the intersection between two lists
147 ** s1, s2 -- lists of items (separated by single blanks).
148 ** rpool -- resource pool from which result is allocated.
151 ** the intersection of both lists.
155 intersect(s1, s2, rpool)
159 char *hr, *h1, *h, *res;
162 if (s1 == NULL || s2 == NULL) /* NULL string(s) -> NULL result */
167 res = (char *) sm_rpool_malloc(rpool, rl + 1);
171 if (rl == 0) /* at least one string empty? */
177 /* walk through s1 */
178 while (h != NULL && *h1 != '\0')
180 /* is there something after the current word? */
181 if ((h = strchr(h1, ' ')) != NULL)
185 /* does the current word appear in s2 ? */
186 if (iteminlist(h1, s2, " ") != NULL)
188 /* add a blank if not first item */
195 /* advance pointer in result list */
201 /* there are more items */
210 ** IPTOSTRING -- create string for SASL_IP*PORT property
211 ** (borrowed from lib/iptostring.c in Cyrus-IMAP)
214 ** addr -- (pointer to) socket address
215 ** addrlen -- length of socket address
216 ** out -- output string (result)
217 ** outlen -- maximum length of output string
220 ** true iff successful.
223 ** creates output string if successful.
224 ** sets errno if unsuccessful.
227 # include <arpa/inet.h>
230 # define NI_MAXHOST 1025
233 # define NI_MAXSERV 32
237 iptostring(addr, addrlen, out, outlen)
239 SOCKADDR_LEN_T addrlen;
243 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
246 # endif /* NETINET6 */
248 if (addr == NULL || out == NULL)
255 niflags = (NI_NUMERICHOST | NI_NUMERICSERV);
256 # ifdef NI_WITHSCOPEID
257 if (addr->sa.sa_family == AF_INET6)
258 niflags |= NI_WITHSCOPEID;
259 # endif /* NI_WITHSCOPEID */
260 if (getnameinfo((struct sockaddr *) addr, addrlen,
261 hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), niflags) != 0)
263 # else /* NETINET6 */
264 if (addr->sa.sa_family != AF_INET)
269 if (sm_strlcpy(hbuf, inet_ntoa(addr->sin.sin_addr), sizeof(hbuf))
275 sm_snprintf(pbuf, sizeof(pbuf), "%d", ntohs(addr->sin.sin_port));
276 # endif /* NETINET6 */
278 if (outlen < strlen(hbuf) + strlen(pbuf) + 2)
283 sm_snprintf(out, outlen, "%s;%s", hbuf, pbuf);
286 # endif /* SASL >= 20000 */