17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /* Generic SASL plugin utility functions
77c478bd9Sstevel@tonic-gate  * Rob Siemborski
87c478bd9Sstevel@tonic-gate  * $Id: plugin_common.c,v 1.13 2003/02/13 19:56:05 rjs3 Exp $
97c478bd9Sstevel@tonic-gate  */
10*1da57d55SToomas Soome /*
117c478bd9Sstevel@tonic-gate  * Copyright (c) 1998-2003 Carnegie Mellon University.  All rights reserved.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
147c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
157c478bd9Sstevel@tonic-gate  * are met:
167c478bd9Sstevel@tonic-gate  *
177c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
18*1da57d55SToomas Soome  *    notice, this list of conditions and the following disclaimer.
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
217c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
227c478bd9Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
237c478bd9Sstevel@tonic-gate  *    distribution.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * 3. The name "Carnegie Mellon University" must not be used to
267c478bd9Sstevel@tonic-gate  *    endorse or promote products derived from this software without
277c478bd9Sstevel@tonic-gate  *    prior written permission. For permission or any other legal
28*1da57d55SToomas Soome  *    details, please contact
297c478bd9Sstevel@tonic-gate  *      Office of Technology Transfer
307c478bd9Sstevel@tonic-gate  *      Carnegie Mellon University
317c478bd9Sstevel@tonic-gate  *      5000 Forbes Avenue
327c478bd9Sstevel@tonic-gate  *      Pittsburgh, PA  15213-3890
337c478bd9Sstevel@tonic-gate  *      (412) 268-4387, fax: (412) 268-7395
347c478bd9Sstevel@tonic-gate  *      tech-transfer@andrew.cmu.edu
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * 4. Redistributions of any form whatsoever must retain the following
377c478bd9Sstevel@tonic-gate  *    acknowledgment:
387c478bd9Sstevel@tonic-gate  *    "This product includes software developed by Computing Services
397c478bd9Sstevel@tonic-gate  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
427c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
437c478bd9Sstevel@tonic-gate  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
447c478bd9Sstevel@tonic-gate  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
457c478bd9Sstevel@tonic-gate  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
467c478bd9Sstevel@tonic-gate  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
477c478bd9Sstevel@tonic-gate  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <config.h>
517c478bd9Sstevel@tonic-gate #ifndef macintosh
527c478bd9Sstevel@tonic-gate #ifdef WIN32
537c478bd9Sstevel@tonic-gate # include <winsock.h>
547c478bd9Sstevel@tonic-gate #else
557c478bd9Sstevel@tonic-gate # include <sys/socket.h>
567c478bd9Sstevel@tonic-gate # include <netinet/in.h>
577c478bd9Sstevel@tonic-gate # include <arpa/inet.h>
587c478bd9Sstevel@tonic-gate # include <netdb.h>
597c478bd9Sstevel@tonic-gate #endif /* WIN32 */
607c478bd9Sstevel@tonic-gate #endif /* macintosh */
617c478bd9Sstevel@tonic-gate #ifdef HAVE_UNISTD_H
627c478bd9Sstevel@tonic-gate #include <unistd.h>
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate #include <fcntl.h>
657c478bd9Sstevel@tonic-gate #include <sasl.h>
667c478bd9Sstevel@tonic-gate #include <saslutil.h>
677c478bd9Sstevel@tonic-gate #include <saslplug.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #include <errno.h>
707c478bd9Sstevel@tonic-gate #include <ctype.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #ifdef HAVE_INTTYPES_H
737c478bd9Sstevel@tonic-gate #include <inttypes.h>
747c478bd9Sstevel@tonic-gate #endif
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #include "plugin_common.h"
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /* translate IPv4 mapped IPv6 address to IPv4 address */
sockaddr_unmapped(struct sockaddr * sa,socklen_t * len)797c478bd9Sstevel@tonic-gate static void sockaddr_unmapped(
807c478bd9Sstevel@tonic-gate #ifdef IN6_IS_ADDR_V4MAPPED
817c478bd9Sstevel@tonic-gate   struct sockaddr *sa, socklen_t *len
827c478bd9Sstevel@tonic-gate #else
837c478bd9Sstevel@tonic-gate   struct sockaddr *sa __attribute__((unused)),
847c478bd9Sstevel@tonic-gate   socklen_t *len __attribute__((unused))
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate )
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate #ifdef IN6_IS_ADDR_V4MAPPED
897c478bd9Sstevel@tonic-gate     struct sockaddr_in6 *sin6;
907c478bd9Sstevel@tonic-gate     struct sockaddr_in *sin4;
917c478bd9Sstevel@tonic-gate     uint32_t addr;
927c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
937c478bd9Sstevel@tonic-gate     in_port_t port;
947c478bd9Sstevel@tonic-gate #else
957c478bd9Sstevel@tonic-gate     int port;
967c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate     if (sa->sa_family != AF_INET6)
997c478bd9Sstevel@tonic-gate 	return;
100*1da57d55SToomas Soome /* LINTED pointer alignment */
1017c478bd9Sstevel@tonic-gate     sin6 = (struct sockaddr_in6 *)sa;
1027c478bd9Sstevel@tonic-gate     if (!IN6_IS_ADDR_V4MAPPED((&sin6->sin6_addr)))
1037c478bd9Sstevel@tonic-gate 	return;
104*1da57d55SToomas Soome /* LINTED pointer alignment */
1057c478bd9Sstevel@tonic-gate     sin4 = (struct sockaddr_in *)sa;
106*1da57d55SToomas Soome /* LINTED pointer alignment */
1077c478bd9Sstevel@tonic-gate     addr = *(uint32_t *)&sin6->sin6_addr.s6_addr[12];
1087c478bd9Sstevel@tonic-gate     port = sin6->sin6_port;
1097c478bd9Sstevel@tonic-gate     memset(sin4, 0, sizeof(struct sockaddr_in));
1107c478bd9Sstevel@tonic-gate     sin4->sin_addr.s_addr = addr;
1117c478bd9Sstevel@tonic-gate     sin4->sin_port = port;
1127c478bd9Sstevel@tonic-gate     sin4->sin_family = AF_INET;
1137c478bd9Sstevel@tonic-gate #ifdef HAVE_SOCKADDR_SA_LEN
1147c478bd9Sstevel@tonic-gate     sin4->sin_len = sizeof(struct sockaddr_in);
1157c478bd9Sstevel@tonic-gate #endif
1167c478bd9Sstevel@tonic-gate     *len = sizeof(struct sockaddr_in);
1177c478bd9Sstevel@tonic-gate #else
1187c478bd9Sstevel@tonic-gate     return;
1197c478bd9Sstevel@tonic-gate #endif
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
_plug_ipfromstring(const sasl_utils_t * utils,const char * addr,struct sockaddr * out,socklen_t outlen)1227c478bd9Sstevel@tonic-gate int _plug_ipfromstring(const sasl_utils_t *utils, const char *addr,
123*1da57d55SToomas Soome 		       struct sockaddr *out, socklen_t outlen)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate     int i, j;
1267c478bd9Sstevel@tonic-gate     socklen_t len;
1277c478bd9Sstevel@tonic-gate #ifdef WINNT /* _SUN_SDK_ */
1287c478bd9Sstevel@tonic-gate     struct sockaddr_in ss;
1297c478bd9Sstevel@tonic-gate #else
1307c478bd9Sstevel@tonic-gate     struct sockaddr_storage ss;
1317c478bd9Sstevel@tonic-gate #endif	/* _SUN_SDK_ */
1327c478bd9Sstevel@tonic-gate     struct addrinfo hints, *ai = NULL;
1337c478bd9Sstevel@tonic-gate     char hbuf[NI_MAXHOST];
1347c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
1357c478bd9Sstevel@tonic-gate     const char *start, *end, *p;
1367c478bd9Sstevel@tonic-gate #endif	/* _SUN_SDK_ */
137*1da57d55SToomas Soome 
1387c478bd9Sstevel@tonic-gate     if(!utils || !addr || !out) {
1397c478bd9Sstevel@tonic-gate 	if(utils) PARAMERROR( utils );
1407c478bd9Sstevel@tonic-gate 	return SASL_BADPARAM;
1417c478bd9Sstevel@tonic-gate     }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
1447c478bd9Sstevel@tonic-gate     end = strchr(addr, ']');
1457c478bd9Sstevel@tonic-gate     if (end != NULL) {
1467c478bd9Sstevel@tonic-gate 	/* This an rfc 2732 ipv6 address */
1477c478bd9Sstevel@tonic-gate 	start = strchr(addr, '[');
1487c478bd9Sstevel@tonic-gate 	if (start >= end || start == NULL) {
1497c478bd9Sstevel@tonic-gate 	    if(utils) PARAMERROR( utils );
1507c478bd9Sstevel@tonic-gate 	    return SASL_BADPARAM;
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	for (i = 0, p = start + 1; p < end; p++) {
1537c478bd9Sstevel@tonic-gate 	    hbuf[i++] = *p;
1547c478bd9Sstevel@tonic-gate 	    if (i >= NI_MAXHOST)
1557c478bd9Sstevel@tonic-gate 		break;
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 	p = strchr(end, ':');
1587c478bd9Sstevel@tonic-gate 	if (p == NULL)
1597c478bd9Sstevel@tonic-gate 		p = end + 1;
1607c478bd9Sstevel@tonic-gate 	else
1617c478bd9Sstevel@tonic-gate 		p = p + 1;
1627c478bd9Sstevel@tonic-gate     } else {
1637c478bd9Sstevel@tonic-gate 	for (i = 0; addr[i] != '\0' && addr[i] != ';'; ) {
1647c478bd9Sstevel@tonic-gate 	    hbuf[i] = addr[i];
1657c478bd9Sstevel@tonic-gate 	    if (++i >= NI_MAXHOST)
1667c478bd9Sstevel@tonic-gate 		break;
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	if (addr[i] == ';')
1697c478bd9Sstevel@tonic-gate 	     p = &addr[i+1];
1707c478bd9Sstevel@tonic-gate 	else
1717c478bd9Sstevel@tonic-gate 	     p = &addr[i];
1727c478bd9Sstevel@tonic-gate     }
1737c478bd9Sstevel@tonic-gate     if (i >= NI_MAXHOST) {
1747c478bd9Sstevel@tonic-gate 	if(utils) PARAMERROR( utils );
1757c478bd9Sstevel@tonic-gate 	return SASL_BADPARAM;
1767c478bd9Sstevel@tonic-gate     }
1777c478bd9Sstevel@tonic-gate     hbuf[i] = '\0';
1787c478bd9Sstevel@tonic-gate     for (j = 0; p[j] != '\0'; j++)
1797c478bd9Sstevel@tonic-gate 	if (!isdigit((int)(p[j]))) {
1807c478bd9Sstevel@tonic-gate 	    PARAMERROR( utils );
1817c478bd9Sstevel@tonic-gate 	    return SASL_BADPARAM;
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate #else
1847c478bd9Sstevel@tonic-gate     /* Parse the address */
1857c478bd9Sstevel@tonic-gate     for (i = 0; addr[i] != '\0' && addr[i] != ';'; i++) {
1867c478bd9Sstevel@tonic-gate 	if (i >= NI_MAXHOST) {
1877c478bd9Sstevel@tonic-gate 	    if(utils) PARAMERROR( utils );
1887c478bd9Sstevel@tonic-gate 	    return SASL_BADPARAM;
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 	hbuf[i] = addr[i];
1917c478bd9Sstevel@tonic-gate     }
1927c478bd9Sstevel@tonic-gate     hbuf[i] = '\0';
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate     if (addr[i] == ';')
1957c478bd9Sstevel@tonic-gate 	i++;
1967c478bd9Sstevel@tonic-gate     /* XXX/FIXME: Do we need this check? */
1977c478bd9Sstevel@tonic-gate     for (j = i; addr[j] != '\0'; j++)
1987c478bd9Sstevel@tonic-gate 	if (!isdigit((int)(addr[j]))) {
1997c478bd9Sstevel@tonic-gate 	    PARAMERROR( utils );
2007c478bd9Sstevel@tonic-gate 	    return SASL_BADPARAM;
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate     memset(&hints, 0, sizeof(hints));
2057c478bd9Sstevel@tonic-gate     hints.ai_family = PF_UNSPEC;
2067c478bd9Sstevel@tonic-gate     hints.ai_socktype = SOCK_STREAM;
2077c478bd9Sstevel@tonic-gate     hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
210*1da57d55SToomas Soome     if (getaddrinfo(hbuf, p, &hints, &ai) != 0) {
2117c478bd9Sstevel@tonic-gate #else
212*1da57d55SToomas Soome     if (getaddrinfo(hbuf, &addr[i], &hints, &ai) != 0) {
2137c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
2147c478bd9Sstevel@tonic-gate 	PARAMERROR( utils );
2157c478bd9Sstevel@tonic-gate 	return SASL_BADPARAM;
2167c478bd9Sstevel@tonic-gate     }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate     len = ai->ai_addrlen;
2197c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
2207c478bd9Sstevel@tonic-gate     if (len > sizeof(ss))
2217c478bd9Sstevel@tonic-gate 	return (SASL_BUFOVER);
2227c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
2237c478bd9Sstevel@tonic-gate     memcpy(&ss, ai->ai_addr, len);
2247c478bd9Sstevel@tonic-gate     freeaddrinfo(ai);
2257c478bd9Sstevel@tonic-gate     sockaddr_unmapped((struct sockaddr *)&ss, &len);
2267c478bd9Sstevel@tonic-gate     if (outlen < len) {
2277c478bd9Sstevel@tonic-gate 	PARAMERROR( utils );
2287c478bd9Sstevel@tonic-gate 	return SASL_BUFOVER;
2297c478bd9Sstevel@tonic-gate     }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate     memcpy(out, &ss, len);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate     return SASL_OK;
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate int _plug_iovec_to_buf(const sasl_utils_t *utils, const struct iovec *vec,
237*1da57d55SToomas Soome 		       unsigned numiov, buffer_info_t **output)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate     unsigned i;
2407c478bd9Sstevel@tonic-gate     int ret;
2417c478bd9Sstevel@tonic-gate     buffer_info_t *out;
2427c478bd9Sstevel@tonic-gate     char *pos;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate     if(!utils || !vec || !output) {
2457c478bd9Sstevel@tonic-gate 	if(utils) PARAMERROR( utils );
2467c478bd9Sstevel@tonic-gate 	return SASL_BADPARAM;
2477c478bd9Sstevel@tonic-gate     }
248*1da57d55SToomas Soome 
2497c478bd9Sstevel@tonic-gate     if(!(*output)) {
2507c478bd9Sstevel@tonic-gate 	*output = utils->malloc(sizeof(buffer_info_t));
2517c478bd9Sstevel@tonic-gate 	if(!*output) {
2527c478bd9Sstevel@tonic-gate 	    MEMERROR(utils);
2537c478bd9Sstevel@tonic-gate 	    return SASL_NOMEM;
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 	memset(*output,0,sizeof(buffer_info_t));
2567c478bd9Sstevel@tonic-gate     }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate     out = *output;
259*1da57d55SToomas Soome 
2607c478bd9Sstevel@tonic-gate     out->curlen = 0;
2617c478bd9Sstevel@tonic-gate     for(i=0; i<numiov; i++)
2627c478bd9Sstevel@tonic-gate 	out->curlen += vec[i].iov_len;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate     ret = _plug_buf_alloc(utils, &out->data, &out->reallen, out->curlen);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate     if(ret != SASL_OK) {
2677c478bd9Sstevel@tonic-gate 	MEMERROR(utils);
2687c478bd9Sstevel@tonic-gate 	return SASL_NOMEM;
2697c478bd9Sstevel@tonic-gate     }
270*1da57d55SToomas Soome 
2717c478bd9Sstevel@tonic-gate     memset(out->data, 0, out->reallen);
2727c478bd9Sstevel@tonic-gate     pos = out->data;
273*1da57d55SToomas Soome 
2747c478bd9Sstevel@tonic-gate     for(i=0; i<numiov; i++) {
2757c478bd9Sstevel@tonic-gate 	memcpy(pos, vec[i].iov_base, vec[i].iov_len);
2767c478bd9Sstevel@tonic-gate 	pos += vec[i].iov_len;
2777c478bd9Sstevel@tonic-gate     }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate     return SASL_OK;
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /* Basically a conditional call to realloc(), if we need more */
2837c478bd9Sstevel@tonic-gate int _plug_buf_alloc(const sasl_utils_t *utils, char **rwbuf,
284*1da57d55SToomas Soome 		    unsigned *curlen, unsigned newlen)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate     if(!utils || !rwbuf || !curlen) {
2877c478bd9Sstevel@tonic-gate 	PARAMERROR(utils);
2887c478bd9Sstevel@tonic-gate 	return SASL_BADPARAM;
2897c478bd9Sstevel@tonic-gate     }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate     if(!(*rwbuf)) {
2927c478bd9Sstevel@tonic-gate 	*rwbuf = utils->malloc(newlen);
2937c478bd9Sstevel@tonic-gate 	if (*rwbuf == NULL) {
2947c478bd9Sstevel@tonic-gate 	    *curlen = 0;
2957c478bd9Sstevel@tonic-gate 	    MEMERROR(utils);
2967c478bd9Sstevel@tonic-gate 	    return SASL_NOMEM;
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 	*curlen = newlen;
2997c478bd9Sstevel@tonic-gate     } else if(*rwbuf && *curlen < newlen) {
3007c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
3017c478bd9Sstevel@tonic-gate 	unsigned needed = 2*(*curlen);
3027c478bd9Sstevel@tonic-gate #else
3037c478bd9Sstevel@tonic-gate 	size_t needed = 2*(*curlen);
3047c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	while(needed < newlen)
3077c478bd9Sstevel@tonic-gate 	    needed *= 2;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	*rwbuf = utils->realloc(*rwbuf, needed);
3107c478bd9Sstevel@tonic-gate 	if (*rwbuf == NULL) {
3117c478bd9Sstevel@tonic-gate 	    *curlen = 0;
3127c478bd9Sstevel@tonic-gate 	    MEMERROR(utils);
3137c478bd9Sstevel@tonic-gate 	    return SASL_NOMEM;
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 	*curlen = needed;
316*1da57d55SToomas Soome     }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate     return SASL_OK;
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate /* copy a string */
3227c478bd9Sstevel@tonic-gate int _plug_strdup(const sasl_utils_t * utils, const char *in,
3237c478bd9Sstevel@tonic-gate 		 char **out, int *outlen)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
3267c478bd9Sstevel@tonic-gate   int len;
3277c478bd9Sstevel@tonic-gate #else
3287c478bd9Sstevel@tonic-gate   size_t len = strlen(in);
3297c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate   if(!utils || !in || !out) {
3327c478bd9Sstevel@tonic-gate       if(utils) PARAMERROR(utils);
3337c478bd9Sstevel@tonic-gate       return SASL_BADPARAM;
3347c478bd9Sstevel@tonic-gate   }
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
3377c478bd9Sstevel@tonic-gate   len = strlen(in);
3387c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
3397c478bd9Sstevel@tonic-gate   *out = utils->malloc(len + 1);
3407c478bd9Sstevel@tonic-gate   if (!*out) {
3417c478bd9Sstevel@tonic-gate       MEMERROR(utils);
3427c478bd9Sstevel@tonic-gate       return SASL_NOMEM;
3437c478bd9Sstevel@tonic-gate   }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate   strcpy((char *) *out, in);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate   if (outlen)
3487c478bd9Sstevel@tonic-gate       *outlen = len;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate   return SASL_OK;
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate void _plug_free_string(const sasl_utils_t *utils, char **str)
3547c478bd9Sstevel@tonic-gate {
3557c478bd9Sstevel@tonic-gate   size_t len;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate   if (!utils || !str || !(*str)) return;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate   len = strlen(*str);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate   utils->erasebuffer(*str, len);
3627c478bd9Sstevel@tonic-gate   utils->free(*str);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate   *str=NULL;
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
367*1da57d55SToomas Soome void _plug_free_secret(const sasl_utils_t *utils, sasl_secret_t **secret)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate     if(!utils || !secret || !(*secret)) return;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
3727c478bd9Sstevel@tonic-gate     utils->erasebuffer((char *)(*secret)->data, (*secret)->len);
3737c478bd9Sstevel@tonic-gate #else
3747c478bd9Sstevel@tonic-gate     utils->erasebuffer((*secret)->data, (*secret)->len);
3757c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
3767c478bd9Sstevel@tonic-gate     utils->free(*secret);
3777c478bd9Sstevel@tonic-gate     *secret = NULL;
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
380*1da57d55SToomas Soome /*
3817c478bd9Sstevel@tonic-gate  * Trys to find the prompt with the lookingfor id in the prompt list
3827c478bd9Sstevel@tonic-gate  * Returns it if found. NULL otherwise
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate sasl_interact_t *_plug_find_prompt(sasl_interact_t **promptlist,
3857c478bd9Sstevel@tonic-gate 				   unsigned int lookingfor)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate     sasl_interact_t *prompt;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate     if (promptlist && *promptlist) {
3907c478bd9Sstevel@tonic-gate 	for (prompt = *promptlist; prompt->id != SASL_CB_LIST_END; ++prompt) {
3917c478bd9Sstevel@tonic-gate 	    if (prompt->id==lookingfor)
3927c478bd9Sstevel@tonic-gate 		return prompt;
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate     }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate     return NULL;
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate /*
4007c478bd9Sstevel@tonic-gate  * Retrieve the simple string given by the callback id.
4017c478bd9Sstevel@tonic-gate  */
4027c478bd9Sstevel@tonic-gate int _plug_get_simple(const sasl_utils_t *utils, unsigned int id, int required,
4037c478bd9Sstevel@tonic-gate 		     const char **result, sasl_interact_t **prompt_need)
4047c478bd9Sstevel@tonic-gate {
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate     int ret = SASL_FAIL;
4077c478bd9Sstevel@tonic-gate     sasl_getsimple_t *simple_cb;
4087c478bd9Sstevel@tonic-gate     void *simple_context;
4097c478bd9Sstevel@tonic-gate     sasl_interact_t *prompt;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate     *result = NULL;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate     /* see if we were given the result in the prompt */
4147c478bd9Sstevel@tonic-gate     prompt = _plug_find_prompt(prompt_need, id);
4157c478bd9Sstevel@tonic-gate     if (prompt != NULL) {
4167c478bd9Sstevel@tonic-gate 	/* We prompted, and got.*/
417*1da57d55SToomas Soome 
4187c478bd9Sstevel@tonic-gate 	if (required && !prompt->result) {
4197c478bd9Sstevel@tonic-gate 	    SETERROR(utils, "Unexpectedly missing a prompt result");
4207c478bd9Sstevel@tonic-gate 	    return SASL_BADPARAM;
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	*result = prompt->result;
4247c478bd9Sstevel@tonic-gate 	return SASL_OK;
4257c478bd9Sstevel@tonic-gate     }
426*1da57d55SToomas Soome 
4277c478bd9Sstevel@tonic-gate     /* Try to get the callback... */
4287c478bd9Sstevel@tonic-gate     ret = utils->getcallback(utils->conn, id, &simple_cb, &simple_context);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate     if (ret == SASL_FAIL && !required)
4317c478bd9Sstevel@tonic-gate 	return SASL_OK;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate     if (ret == SASL_OK && simple_cb) {
4347c478bd9Sstevel@tonic-gate 	ret = simple_cb(simple_context, id, result, NULL);
4357c478bd9Sstevel@tonic-gate 	if (ret != SASL_OK)
4367c478bd9Sstevel@tonic-gate 	    return ret;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	if (required && !*result) {
4397c478bd9Sstevel@tonic-gate 	    PARAMERROR(utils);
4407c478bd9Sstevel@tonic-gate 	    return SASL_BADPARAM;
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate     }
443*1da57d55SToomas Soome 
4447c478bd9Sstevel@tonic-gate     return ret;
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate /*
4487c478bd9Sstevel@tonic-gate  * Retrieve the user password.
4497c478bd9Sstevel@tonic-gate  */
4507c478bd9Sstevel@tonic-gate int _plug_get_password(const sasl_utils_t *utils, sasl_secret_t **password,
4517c478bd9Sstevel@tonic-gate 		       unsigned int *iscopy, sasl_interact_t **prompt_need)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate     int ret = SASL_FAIL;
4547c478bd9Sstevel@tonic-gate     sasl_getsecret_t *pass_cb;
4557c478bd9Sstevel@tonic-gate     void *pass_context;
4567c478bd9Sstevel@tonic-gate     sasl_interact_t *prompt;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate     *password = NULL;
4597c478bd9Sstevel@tonic-gate     *iscopy = 0;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate     /* see if we were given the password in the prompt */
4627c478bd9Sstevel@tonic-gate     prompt = _plug_find_prompt(prompt_need, SASL_CB_PASS);
4637c478bd9Sstevel@tonic-gate     if (prompt != NULL) {
4647c478bd9Sstevel@tonic-gate 	/* We prompted, and got.*/
465*1da57d55SToomas Soome 
4667c478bd9Sstevel@tonic-gate 	if (!prompt->result) {
4677c478bd9Sstevel@tonic-gate 	    SETERROR(utils, "Unexpectedly missing a prompt result");
4687c478bd9Sstevel@tonic-gate 	    return SASL_BADPARAM;
4697c478bd9Sstevel@tonic-gate 	}
470*1da57d55SToomas Soome 
4717c478bd9Sstevel@tonic-gate 	/* copy what we got into a secret_t */
4727c478bd9Sstevel@tonic-gate 	*password = (sasl_secret_t *) utils->malloc(sizeof(sasl_secret_t) +
4737c478bd9Sstevel@tonic-gate 						    prompt->len + 1);
4747c478bd9Sstevel@tonic-gate 	if (!*password) {
4757c478bd9Sstevel@tonic-gate 	    MEMERROR(utils);
4767c478bd9Sstevel@tonic-gate 	    return SASL_NOMEM;
4777c478bd9Sstevel@tonic-gate 	}
478*1da57d55SToomas Soome 
4797c478bd9Sstevel@tonic-gate 	(*password)->len=prompt->len;
4807c478bd9Sstevel@tonic-gate 	memcpy((*password)->data, prompt->result, prompt->len);
4817c478bd9Sstevel@tonic-gate 	(*password)->data[(*password)->len]=0;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	*iscopy = 1;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	return SASL_OK;
4867c478bd9Sstevel@tonic-gate     }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate     /* Try to get the callback... */
4897c478bd9Sstevel@tonic-gate     ret = utils->getcallback(utils->conn, SASL_CB_PASS,
4907c478bd9Sstevel@tonic-gate 			     &pass_cb, &pass_context);
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate     if (ret == SASL_OK && pass_cb) {
4937c478bd9Sstevel@tonic-gate 	ret = pass_cb(utils->conn, pass_context, SASL_CB_PASS, password);
4947c478bd9Sstevel@tonic-gate 	if (ret != SASL_OK)
4957c478bd9Sstevel@tonic-gate 	    return ret;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if (!*password) {
4987c478bd9Sstevel@tonic-gate 	    PARAMERROR(utils);
4997c478bd9Sstevel@tonic-gate 	    return SASL_BADPARAM;
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate     }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate     return ret;
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate /*
5077c478bd9Sstevel@tonic-gate  * Retrieve the string given by the challenge prompt id.
5087c478bd9Sstevel@tonic-gate  */
5097c478bd9Sstevel@tonic-gate int _plug_challenge_prompt(const sasl_utils_t *utils, unsigned int id,
5107c478bd9Sstevel@tonic-gate 			   const char *challenge, const char *promptstr,
5117c478bd9Sstevel@tonic-gate 			   const char **result, sasl_interact_t **prompt_need)
5127c478bd9Sstevel@tonic-gate {
5137c478bd9Sstevel@tonic-gate     int ret = SASL_FAIL;
5147c478bd9Sstevel@tonic-gate     sasl_chalprompt_t *chalprompt_cb;
5157c478bd9Sstevel@tonic-gate     void *chalprompt_context;
5167c478bd9Sstevel@tonic-gate     sasl_interact_t *prompt;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate     *result = NULL;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate     /* see if we were given the password in the prompt */
5217c478bd9Sstevel@tonic-gate     prompt = _plug_find_prompt(prompt_need, id);
5227c478bd9Sstevel@tonic-gate     if (prompt != NULL) {
5237c478bd9Sstevel@tonic-gate 	/* We prompted, and got.*/
524*1da57d55SToomas Soome 
5257c478bd9Sstevel@tonic-gate 	if (!prompt->result) {
5267c478bd9Sstevel@tonic-gate 	    SETERROR(utils, "Unexpectedly missing a prompt result");
5277c478bd9Sstevel@tonic-gate 	    return SASL_BADPARAM;
5287c478bd9Sstevel@tonic-gate 	}
529*1da57d55SToomas Soome 
5307c478bd9Sstevel@tonic-gate 	*result = prompt->result;
5317c478bd9Sstevel@tonic-gate 	return SASL_OK;
5327c478bd9Sstevel@tonic-gate     }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate     /* Try to get the callback... */
5357c478bd9Sstevel@tonic-gate     ret = utils->getcallback(utils->conn, id,
5367c478bd9Sstevel@tonic-gate 			     &chalprompt_cb, &chalprompt_context);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate     if (ret == SASL_OK && chalprompt_cb) {
5397c478bd9Sstevel@tonic-gate 	ret = chalprompt_cb(chalprompt_context, id,
5407c478bd9Sstevel@tonic-gate 			    challenge, promptstr, NULL, result, NULL);
5417c478bd9Sstevel@tonic-gate 	if (ret != SASL_OK)
5427c478bd9Sstevel@tonic-gate 	    return ret;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (!*result) {
5457c478bd9Sstevel@tonic-gate 	    PARAMERROR(utils);
5467c478bd9Sstevel@tonic-gate 	    return SASL_BADPARAM;
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate     }
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate     return ret;
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate /*
5547c478bd9Sstevel@tonic-gate  * Retrieve the client realm.
5557c478bd9Sstevel@tonic-gate  */
5567c478bd9Sstevel@tonic-gate int _plug_get_realm(const sasl_utils_t *utils, const char **availrealms,
5577c478bd9Sstevel@tonic-gate 		    const char **realm, sasl_interact_t **prompt_need)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate     int ret = SASL_FAIL;
5607c478bd9Sstevel@tonic-gate     sasl_getrealm_t *realm_cb;
5617c478bd9Sstevel@tonic-gate     void *realm_context;
5627c478bd9Sstevel@tonic-gate     sasl_interact_t *prompt;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate     *realm = NULL;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate     /* see if we were given the result in the prompt */
5677c478bd9Sstevel@tonic-gate     prompt = _plug_find_prompt(prompt_need, SASL_CB_GETREALM);
5687c478bd9Sstevel@tonic-gate     if (prompt != NULL) {
5697c478bd9Sstevel@tonic-gate 	/* We prompted, and got.*/
570*1da57d55SToomas Soome 
5717c478bd9Sstevel@tonic-gate 	if (!prompt->result) {
5727c478bd9Sstevel@tonic-gate 	    SETERROR(utils, "Unexpectedly missing a prompt result");
5737c478bd9Sstevel@tonic-gate 	    return SASL_BADPARAM;
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	*realm = prompt->result;
5777c478bd9Sstevel@tonic-gate 	return SASL_OK;
5787c478bd9Sstevel@tonic-gate     }
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate     /* Try to get the callback... */
5817c478bd9Sstevel@tonic-gate     ret = utils->getcallback(utils->conn, SASL_CB_GETREALM,
5827c478bd9Sstevel@tonic-gate 			     &realm_cb, &realm_context);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate     if (ret == SASL_OK && realm_cb) {
5857c478bd9Sstevel@tonic-gate 	ret = realm_cb(realm_context, SASL_CB_GETREALM, availrealms, realm);
5867c478bd9Sstevel@tonic-gate 	if (ret != SASL_OK)
5877c478bd9Sstevel@tonic-gate 	    return ret;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if (!*realm) {
5907c478bd9Sstevel@tonic-gate 	    PARAMERROR(utils);
5917c478bd9Sstevel@tonic-gate 	    return SASL_BADPARAM;
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate     }
594*1da57d55SToomas Soome 
5957c478bd9Sstevel@tonic-gate     return ret;
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate /*
5997c478bd9Sstevel@tonic-gate  * Make the requested prompts. (prompt==NULL means we don't want it)
6007c478bd9Sstevel@tonic-gate  */
6017c478bd9Sstevel@tonic-gate int _plug_make_prompts(const sasl_utils_t *utils,
6027c478bd9Sstevel@tonic-gate #ifdef _INTEGRATED_SOLARIS_
6037c478bd9Sstevel@tonic-gate 		      void **h,
6047c478bd9Sstevel@tonic-gate #endif /* _INTEGRATED_SOLARIS_ */
6057c478bd9Sstevel@tonic-gate 		       sasl_interact_t **prompts_res,
6067c478bd9Sstevel@tonic-gate 		       const char *user_prompt, const char *user_def,
6077c478bd9Sstevel@tonic-gate 		       const char *auth_prompt, const char *auth_def,
6087c478bd9Sstevel@tonic-gate 		       const char *pass_prompt, const char *pass_def,
6097c478bd9Sstevel@tonic-gate 		       const char *echo_chal,
6107c478bd9Sstevel@tonic-gate 		       const char *echo_prompt, const char *echo_def,
6117c478bd9Sstevel@tonic-gate 		       const char *realm_chal,
6127c478bd9Sstevel@tonic-gate 		       const char *realm_prompt, const char *realm_def)
6137c478bd9Sstevel@tonic-gate {
6147c478bd9Sstevel@tonic-gate     int num = 1;
6157c478bd9Sstevel@tonic-gate     int alloc_size;
6167c478bd9Sstevel@tonic-gate     sasl_interact_t *prompts;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate     if (user_prompt) num++;
6197c478bd9Sstevel@tonic-gate     if (auth_prompt) num++;
6207c478bd9Sstevel@tonic-gate     if (pass_prompt) num++;
6217c478bd9Sstevel@tonic-gate     if (echo_prompt) num++;
6227c478bd9Sstevel@tonic-gate     if (realm_prompt) num++;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate     if (num == 1) {
6257c478bd9Sstevel@tonic-gate 	SETERROR( utils, "make_prompts() called with no actual prompts" );
6267c478bd9Sstevel@tonic-gate 	return SASL_FAIL;
6277c478bd9Sstevel@tonic-gate     }
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate     alloc_size = sizeof(sasl_interact_t)*num;
6307c478bd9Sstevel@tonic-gate     prompts = utils->malloc(alloc_size);
6317c478bd9Sstevel@tonic-gate     if (!prompts) {
6327c478bd9Sstevel@tonic-gate 	MEMERROR( utils );
6337c478bd9Sstevel@tonic-gate 	return SASL_NOMEM;
6347c478bd9Sstevel@tonic-gate     }
6357c478bd9Sstevel@tonic-gate     memset(prompts, 0, alloc_size);
636*1da57d55SToomas Soome 
6377c478bd9Sstevel@tonic-gate     *prompts_res = prompts;
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate     if (user_prompt) {
6407c478bd9Sstevel@tonic-gate 	(prompts)->id = SASL_CB_USER;
6417c478bd9Sstevel@tonic-gate #ifdef _INTEGRATED_SOLARIS_
6427c478bd9Sstevel@tonic-gate 	(prompts)->challenge = convert_prompt(utils, h,
6437c478bd9Sstevel@tonic-gate 		gettext("Authorization Name"));
6447c478bd9Sstevel@tonic-gate #else
6457c478bd9Sstevel@tonic-gate 	(prompts)->challenge = "Authorization Name";
6467c478bd9Sstevel@tonic-gate #endif /* _INTEGRATED_SOLARIS_ */
6477c478bd9Sstevel@tonic-gate 	(prompts)->prompt = user_prompt;
6487c478bd9Sstevel@tonic-gate 	(prompts)->defresult = user_def;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	prompts++;
6517c478bd9Sstevel@tonic-gate     }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate     if (auth_prompt) {
6547c478bd9Sstevel@tonic-gate 	(prompts)->id = SASL_CB_AUTHNAME;
6557c478bd9Sstevel@tonic-gate #ifdef _INTEGRATED_SOLARIS_
6567c478bd9Sstevel@tonic-gate 	(prompts)->challenge = convert_prompt(utils, h,
6577c478bd9Sstevel@tonic-gate 		gettext( "Authentication Name"));
6587c478bd9Sstevel@tonic-gate #else
6597c478bd9Sstevel@tonic-gate 	(prompts)->challenge = "Authentication Name";
6607c478bd9Sstevel@tonic-gate #endif /* _INTEGRATED_SOLARIS_ */
6617c478bd9Sstevel@tonic-gate 	(prompts)->prompt = auth_prompt;
6627c478bd9Sstevel@tonic-gate 	(prompts)->defresult = auth_def;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	prompts++;
6657c478bd9Sstevel@tonic-gate     }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate     if (pass_prompt) {
6687c478bd9Sstevel@tonic-gate 	(prompts)->id = SASL_CB_PASS;
6697c478bd9Sstevel@tonic-gate #ifdef _INTEGRATED_SOLARIS_
6707c478bd9Sstevel@tonic-gate 	(prompts)->challenge = convert_prompt(utils, h, gettext("Password"));
6717c478bd9Sstevel@tonic-gate #else
6727c478bd9Sstevel@tonic-gate 	(prompts)->challenge = "Password";
6737c478bd9Sstevel@tonic-gate #endif /* _INTEGRATED_SOLARIS_ */
6747c478bd9Sstevel@tonic-gate 	(prompts)->prompt = pass_prompt;
6757c478bd9Sstevel@tonic-gate 	(prompts)->defresult = pass_def;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	prompts++;
6787c478bd9Sstevel@tonic-gate     }
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate     if (echo_prompt) {
6817c478bd9Sstevel@tonic-gate 	(prompts)->id = SASL_CB_ECHOPROMPT;
6827c478bd9Sstevel@tonic-gate 	(prompts)->challenge = echo_chal;
6837c478bd9Sstevel@tonic-gate 	(prompts)->prompt = echo_prompt;
6847c478bd9Sstevel@tonic-gate 	(prompts)->defresult = echo_def;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	prompts++;
6877c478bd9Sstevel@tonic-gate     }
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate     if (realm_prompt) {
6907c478bd9Sstevel@tonic-gate 	(prompts)->id = SASL_CB_GETREALM;
6917c478bd9Sstevel@tonic-gate 	(prompts)->challenge = realm_chal;
6927c478bd9Sstevel@tonic-gate 	(prompts)->prompt = realm_prompt;
6937c478bd9Sstevel@tonic-gate 	(prompts)->defresult = realm_def;
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	prompts++;
6967c478bd9Sstevel@tonic-gate     }
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate     /* add the ending one */
6997c478bd9Sstevel@tonic-gate     (prompts)->id = SASL_CB_LIST_END;
7007c478bd9Sstevel@tonic-gate     (prompts)->challenge = NULL;
7017c478bd9Sstevel@tonic-gate     (prompts)->prompt = NULL;
7027c478bd9Sstevel@tonic-gate     (prompts)->defresult = NULL;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate     return SASL_OK;
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate /*
7087c478bd9Sstevel@tonic-gate  * Decode and concatenate multiple packets using the given function
7097c478bd9Sstevel@tonic-gate  * to decode each packet.
7107c478bd9Sstevel@tonic-gate  */
7117c478bd9Sstevel@tonic-gate int _plug_decode(const sasl_utils_t *utils,
7127c478bd9Sstevel@tonic-gate 		 void *context,
7137c478bd9Sstevel@tonic-gate 		 const char *input, unsigned inputlen,
7147c478bd9Sstevel@tonic-gate 		 char **output,		/* output buffer */
7157c478bd9Sstevel@tonic-gate 		 unsigned *outputsize,	/* current size of output buffer */
7167c478bd9Sstevel@tonic-gate 		 unsigned *outputlen,	/* length of data in output buffer */
7177c478bd9Sstevel@tonic-gate 		 int (*decode_pkt)(void *context,
7187c478bd9Sstevel@tonic-gate 				   const char **input, unsigned *inputlen,
7197c478bd9Sstevel@tonic-gate 				   char **output, unsigned *outputlen))
7207c478bd9Sstevel@tonic-gate {
7217c478bd9Sstevel@tonic-gate     char *tmp = NULL;
7227c478bd9Sstevel@tonic-gate     unsigned tmplen = 0;
7237c478bd9Sstevel@tonic-gate     int ret;
724*1da57d55SToomas Soome 
7257c478bd9Sstevel@tonic-gate     *outputlen = 0;
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate     while (inputlen!=0)
7287c478bd9Sstevel@tonic-gate     {
7297c478bd9Sstevel@tonic-gate 	/* no need to free tmp */
7307c478bd9Sstevel@tonic-gate       ret = decode_pkt(context, &input, &inputlen, &tmp, &tmplen);
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate       if(ret != SASL_OK) return ret;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate       if (tmp!=NULL) /* if received 2 packets merge them together */
7357c478bd9Sstevel@tonic-gate       {
7367c478bd9Sstevel@tonic-gate 	  ret = _plug_buf_alloc(utils, output, outputsize,
7377c478bd9Sstevel@tonic-gate 				*outputlen + tmplen + 1);
7387c478bd9Sstevel@tonic-gate 	  if(ret != SASL_OK) return ret;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	  memcpy(*output + *outputlen, tmp, tmplen);
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	  /* Protect stupid clients */
7437c478bd9Sstevel@tonic-gate 	  *(*output + *outputlen + tmplen) = '\0';
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	  *outputlen+=tmplen;
7467c478bd9Sstevel@tonic-gate       }
7477c478bd9Sstevel@tonic-gate     }
7487c478bd9Sstevel@tonic-gate 
749*1da57d55SToomas Soome     return SASL_OK;
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate /* returns the realm we should pretend to be in */
7537c478bd9Sstevel@tonic-gate int _plug_parseuser(const sasl_utils_t *utils,
754*1da57d55SToomas Soome 		    char **user, char **realm, const char *user_realm,
7557c478bd9Sstevel@tonic-gate 		    const char *serverFQDN, const char *input)
7567c478bd9Sstevel@tonic-gate {
7577c478bd9Sstevel@tonic-gate     int ret;
7587c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
7597c478bd9Sstevel@tonic-gate     const char *r;
7607c478bd9Sstevel@tonic-gate #else
7617c478bd9Sstevel@tonic-gate     char *r;
7627c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate     if(!user || !serverFQDN) {
7657c478bd9Sstevel@tonic-gate 	PARAMERROR( utils );
7667c478bd9Sstevel@tonic-gate 	return SASL_BADPARAM;
7677c478bd9Sstevel@tonic-gate     }
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate     r = strchr(input, '@');
7707c478bd9Sstevel@tonic-gate     if (!r) {
7717c478bd9Sstevel@tonic-gate 	/* hmmm, the user didn't specify a realm */
7727c478bd9Sstevel@tonic-gate 	if(user_realm && user_realm[0]) {
7737c478bd9Sstevel@tonic-gate 	    ret = _plug_strdup(utils, user_realm, realm, NULL);
7747c478bd9Sstevel@tonic-gate 	} else {
7757c478bd9Sstevel@tonic-gate 	    /* Default to serverFQDN */
7767c478bd9Sstevel@tonic-gate 	    ret = _plug_strdup(utils, serverFQDN, realm, NULL);
7777c478bd9Sstevel@tonic-gate 	}
778*1da57d55SToomas Soome 
7797c478bd9Sstevel@tonic-gate 	if (ret == SASL_OK) {
7807c478bd9Sstevel@tonic-gate 	    ret = _plug_strdup(utils, input, user, NULL);
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate     } else {
7837c478bd9Sstevel@tonic-gate 	r++;
7847c478bd9Sstevel@tonic-gate 	ret = _plug_strdup(utils, r, realm, NULL);
7857c478bd9Sstevel@tonic-gate #ifdef _SUN_SDK_
7867c478bd9Sstevel@tonic-gate 	if (ret == SASL_OK) {
7877c478bd9Sstevel@tonic-gate 	    *user = utils->malloc(r - input);
7887c478bd9Sstevel@tonic-gate 	    if (*user) {
7897c478bd9Sstevel@tonic-gate 		memcpy(*user, input, r - input - 1);
7907c478bd9Sstevel@tonic-gate 		(*user)[r - input - 1] = '\0';
7917c478bd9Sstevel@tonic-gate 	    } else {
7927c478bd9Sstevel@tonic-gate 		MEMERROR( utils );
7937c478bd9Sstevel@tonic-gate 		ret = SASL_NOMEM;
7947c478bd9Sstevel@tonic-gate 	    }
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate #else
7977c478bd9Sstevel@tonic-gate 	*--r = '\0';
7987c478bd9Sstevel@tonic-gate 	*user = utils->malloc(r - input + 1);
7997c478bd9Sstevel@tonic-gate 	if (*user) {
8007c478bd9Sstevel@tonic-gate 	    strncpy(*user, input, r - input +1);
8017c478bd9Sstevel@tonic-gate 	} else {
8027c478bd9Sstevel@tonic-gate 	    MEMERROR( utils );
8037c478bd9Sstevel@tonic-gate 	    ret = SASL_NOMEM;
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 	*r = '@';
8067c478bd9Sstevel@tonic-gate #endif /* _SUN_SDK_ */
8077c478bd9Sstevel@tonic-gate     }
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate     return ret;
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate #ifdef _INTEGRATED_SOLARIS_
8137c478bd9Sstevel@tonic-gate int
8147c478bd9Sstevel@tonic-gate use_locale(const char *lang_list, int is_client)
8157c478bd9Sstevel@tonic-gate {
8167c478bd9Sstevel@tonic-gate     const char *s;
8177c478bd9Sstevel@tonic-gate     const char *begin;
8187c478bd9Sstevel@tonic-gate     const char *end;
8197c478bd9Sstevel@tonic-gate     const char *i_default = "i-default";
8207c478bd9Sstevel@tonic-gate     const int i_default_len = 9;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate     if (lang_list == NULL)
8237c478bd9Sstevel@tonic-gate 	return is_client;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate     begin = lang_list;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate     for (;;) {
8287c478bd9Sstevel@tonic-gate 	/* skip over leading whitespace and commas */
8297c478bd9Sstevel@tonic-gate 	while (isspace(*begin) || *begin == ',')
8307c478bd9Sstevel@tonic-gate 	    begin++;
8317c478bd9Sstevel@tonic-gate 	if (*begin == '\0')
8327c478bd9Sstevel@tonic-gate 	    break;
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	/* Find the end of the language tag */
8357c478bd9Sstevel@tonic-gate 	for (end = begin; end[1] != ',' && end[1] != '\0'; end++) {}
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	for (s = end; isspace(*s); s--) {}
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	if (s == begin && *begin == '*')
8407c478bd9Sstevel@tonic-gate 	    return 1;
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	if (s - begin == (i_default_len - 1) &&
8437c478bd9Sstevel@tonic-gate 		strncasecmp(begin, i_default, i_default_len) == 0)
8447c478bd9Sstevel@tonic-gate 	    return 0;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	begin = end + 1;
8477c478bd9Sstevel@tonic-gate     }
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate     return is_client;
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate typedef struct prompt_list {
8537c478bd9Sstevel@tonic-gate     char *prompt;
8547c478bd9Sstevel@tonic-gate     struct prompt_list *next;
8557c478bd9Sstevel@tonic-gate } prompt_list;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate const char *
8587c478bd9Sstevel@tonic-gate convert_prompt(const sasl_utils_t *utils, void **h, const char *s)
8597c478bd9Sstevel@tonic-gate {
8607c478bd9Sstevel@tonic-gate     sasl_getsimple_t *simple_cb;
8617c478bd9Sstevel@tonic-gate     void *simple_context;
8627c478bd9Sstevel@tonic-gate     const char *result = NULL;
8637c478bd9Sstevel@tonic-gate     const char *s_locale;
8647c478bd9Sstevel@tonic-gate     int ret;
8657c478bd9Sstevel@tonic-gate     char *buf;
8667c478bd9Sstevel@tonic-gate     const char *ret_buf;
8677c478bd9Sstevel@tonic-gate     prompt_list *list;
8687c478bd9Sstevel@tonic-gate     prompt_list *next;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate     if (utils == NULL || utils->conn == NULL)
8717c478bd9Sstevel@tonic-gate 	return s;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate     if (s == NULL) {
8747c478bd9Sstevel@tonic-gate 	for (list = (prompt_list *)*h; list != NULL; list = next) {
8757c478bd9Sstevel@tonic-gate 	    if (list->prompt)
8767c478bd9Sstevel@tonic-gate 		utils->free(list->prompt);
8777c478bd9Sstevel@tonic-gate 	    next = list->next;
8787c478bd9Sstevel@tonic-gate 	    utils->free(list);
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 	*h = NULL;
8817c478bd9Sstevel@tonic-gate 	return NULL;
8827c478bd9Sstevel@tonic-gate     }
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate     ret = utils->getcallback(utils->conn, SASL_CB_LANGUAGE, &simple_cb,
8857c478bd9Sstevel@tonic-gate 	&simple_context);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate     if (ret == SASL_OK && simple_cb) {
8887c478bd9Sstevel@tonic-gate 	ret = simple_cb(simple_context, SASL_CB_LANGUAGE, &result, NULL);
8897c478bd9Sstevel@tonic-gate     } else
8907c478bd9Sstevel@tonic-gate 	ret = SASL_FAIL;
8917c478bd9Sstevel@tonic-gate     if (ret == SASL_OK && !use_locale(result, 1))
8927c478bd9Sstevel@tonic-gate 	return s;
893*1da57d55SToomas Soome 
8947c478bd9Sstevel@tonic-gate     s_locale = dgettext(TEXT_DOMAIN, s);
8957c478bd9Sstevel@tonic-gate     if (s == s_locale) {
8967c478bd9Sstevel@tonic-gate 	return s;
8977c478bd9Sstevel@tonic-gate     }
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate     buf = local_to_utf(utils, s_locale);
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate     if (buf != NULL) {
9027c478bd9Sstevel@tonic-gate 	list = utils->malloc(sizeof (prompt_list));
9037c478bd9Sstevel@tonic-gate 	if (list == NULL) {
9047c478bd9Sstevel@tonic-gate 	    utils->free(buf);
9057c478bd9Sstevel@tonic-gate 	    buf = NULL;
9067c478bd9Sstevel@tonic-gate 	} else {
9077c478bd9Sstevel@tonic-gate 	    list->prompt = buf;
9087c478bd9Sstevel@tonic-gate 	    list->next = *h;
9097c478bd9Sstevel@tonic-gate 	    *h = list;
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate     }
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate     ret_buf = (buf == NULL) ? s : buf;
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate     return ret_buf;
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate #include <iconv.h>
9197c478bd9Sstevel@tonic-gate #include <langinfo.h>
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate /*
9227c478bd9Sstevel@tonic-gate  * local_to_utf converts a string in the current codeset to utf-8.
9237c478bd9Sstevel@tonic-gate  * If no codeset is specified, then codeset 646 will be used.
9247c478bd9Sstevel@tonic-gate  * Upon successful completion, this function will return a non-NULL buffer
9257c478bd9Sstevel@tonic-gate  * that is allocated by local_to_utf.
9267c478bd9Sstevel@tonic-gate  *
9277c478bd9Sstevel@tonic-gate  * If utils is NULL, local_to_utf will use the standard memory allocation
9287c478bd9Sstevel@tonic-gate  * functions, otherwise the memory functions defined in sasl_utils_t will
9297c478bd9Sstevel@tonic-gate  * be used.
9307c478bd9Sstevel@tonic-gate  *
9317c478bd9Sstevel@tonic-gate  * local_to_utf will return NULL in the case of any error
9327c478bd9Sstevel@tonic-gate  */
9337c478bd9Sstevel@tonic-gate char *
9347c478bd9Sstevel@tonic-gate local_to_utf(const sasl_utils_t *utils, const char *s)
9357c478bd9Sstevel@tonic-gate {
9367c478bd9Sstevel@tonic-gate 	const char *code_set = nl_langinfo(CODESET);
9377c478bd9Sstevel@tonic-gate 	iconv_t cd;
9387c478bd9Sstevel@tonic-gate 	char *buf, *tmp;
9397c478bd9Sstevel@tonic-gate 	size_t in_len;
9407c478bd9Sstevel@tonic-gate 	size_t buf_size;
9417c478bd9Sstevel@tonic-gate 	size_t ileft, oleft;
9427c478bd9Sstevel@tonic-gate 	const char *inptr;
9437c478bd9Sstevel@tonic-gate 	char *outptr;
9447c478bd9Sstevel@tonic-gate 	size_t ret;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	if (s == NULL)
9477c478bd9Sstevel@tonic-gate 	    return NULL;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	if (code_set == NULL)
9507c478bd9Sstevel@tonic-gate 	    code_set = "646";
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	if (strcasecmp(code_set, "UTF-8") == 0) {
9537c478bd9Sstevel@tonic-gate 	    if (utils == NULL)
9547c478bd9Sstevel@tonic-gate 		buf = strdup(s);
9557c478bd9Sstevel@tonic-gate 	    else {
9567c478bd9Sstevel@tonic-gate 		if (_plug_strdup(utils, s, &buf, NULL) != SASL_OK)
9577c478bd9Sstevel@tonic-gate 			buf = NULL;
9587c478bd9Sstevel@tonic-gate 	    }
9597c478bd9Sstevel@tonic-gate 	    return buf;
9607c478bd9Sstevel@tonic-gate 	}
9617c478bd9Sstevel@tonic-gate 	cd = iconv_open("UTF-8", code_set);
9627c478bd9Sstevel@tonic-gate 	if (cd == (iconv_t)-1)
9637c478bd9Sstevel@tonic-gate 	    return NULL;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	in_len = strlen(s);
9667c478bd9Sstevel@tonic-gate 	buf_size = 4 * (in_len + 1);	/* guess */
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	if (utils == NULL)
9697c478bd9Sstevel@tonic-gate 	    buf = malloc(buf_size);
9707c478bd9Sstevel@tonic-gate 	else
9717c478bd9Sstevel@tonic-gate 	    buf = utils->malloc(buf_size);
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
9747c478bd9Sstevel@tonic-gate 	    (void) iconv_close(cd);
9757c478bd9Sstevel@tonic-gate 	    return NULL;
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate 	inptr = s;
9787c478bd9Sstevel@tonic-gate 	ileft = in_len;
9797c478bd9Sstevel@tonic-gate 	outptr = buf;
9807c478bd9Sstevel@tonic-gate 	oleft = buf_size;
9817c478bd9Sstevel@tonic-gate 	for (;;) {
9827c478bd9Sstevel@tonic-gate 	    ret = iconv(cd, &inptr, &ileft, &outptr, &oleft);
9837c478bd9Sstevel@tonic-gate 	    if (ret == (size_t)(-1)) {
9847c478bd9Sstevel@tonic-gate 		if (errno == E2BIG) {
9857c478bd9Sstevel@tonic-gate 		    oleft += buf_size;
9867c478bd9Sstevel@tonic-gate 		    buf_size *= 2;
9877c478bd9Sstevel@tonic-gate 		    if (utils == NULL)
9887c478bd9Sstevel@tonic-gate 			tmp = realloc(buf, buf_size);
9897c478bd9Sstevel@tonic-gate 		    else
9907c478bd9Sstevel@tonic-gate 			tmp = utils->realloc(buf, buf_size);
9917c478bd9Sstevel@tonic-gate 		    if (tmp == NULL) {
9927c478bd9Sstevel@tonic-gate 			oleft = (size_t)(-1);
9937c478bd9Sstevel@tonic-gate 			break;
9947c478bd9Sstevel@tonic-gate 		    }
9957c478bd9Sstevel@tonic-gate 		    outptr = tmp + (outptr-buf);
9967c478bd9Sstevel@tonic-gate 		    buf = tmp;
9977c478bd9Sstevel@tonic-gate 		    continue;
9987c478bd9Sstevel@tonic-gate 		}
9997c478bd9Sstevel@tonic-gate 		oleft = (size_t)(-1);
10007c478bd9Sstevel@tonic-gate 		break;
10017c478bd9Sstevel@tonic-gate 	    }
10027c478bd9Sstevel@tonic-gate 	    if (inptr == NULL)
10037c478bd9Sstevel@tonic-gate 		break;
10047c478bd9Sstevel@tonic-gate 	    inptr = NULL;
10057c478bd9Sstevel@tonic-gate 	    ileft = 0;
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 	if (oleft > 0) {
10087c478bd9Sstevel@tonic-gate 	    *outptr = '\0';
10097c478bd9Sstevel@tonic-gate 	} else if (oleft != (size_t)(-1)) {
10107c478bd9Sstevel@tonic-gate 	    if (utils == NULL)
10117c478bd9Sstevel@tonic-gate 		tmp = realloc(buf, buf_size + 1);
10127c478bd9Sstevel@tonic-gate 	    else
10137c478bd9Sstevel@tonic-gate 		tmp = utils->realloc(buf, buf_size + 1);
10147c478bd9Sstevel@tonic-gate 	    if (tmp == NULL) {
10157c478bd9Sstevel@tonic-gate 		oleft = (size_t)(-1);
10167c478bd9Sstevel@tonic-gate 	    } else {
10177c478bd9Sstevel@tonic-gate 		buf = tmp;
10187c478bd9Sstevel@tonic-gate 		buf[buf_size] = '\0';
10197c478bd9Sstevel@tonic-gate 	    }
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 	if (oleft == (size_t)(-1)) {
10227c478bd9Sstevel@tonic-gate 	    if (utils == NULL)
10237c478bd9Sstevel@tonic-gate 		free(buf);
10247c478bd9Sstevel@tonic-gate 	    else
10257c478bd9Sstevel@tonic-gate 		utils->free(buf);
10267c478bd9Sstevel@tonic-gate 	    buf = NULL;
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	(void) iconv_close(cd);
10307c478bd9Sstevel@tonic-gate 	return buf;
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate #endif /* _INTEGRATED_SOLARIS_ */
1033