17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <syslog.h>
297c478bd9Sstevel@tonic-gate #include <slp-internal.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate struct attr_node {
327c478bd9Sstevel@tonic-gate 	char *tag, *val;
337c478bd9Sstevel@tonic-gate };
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate static SLPError slp_packAttrRqst(slp_handle_impl_t *, const char *,
367c478bd9Sstevel@tonic-gate 					const char *);
377c478bd9Sstevel@tonic-gate static int compare_tags(const void *, const void *);
387c478bd9Sstevel@tonic-gate static void collate_attrs(char *, void **, int *, int);
397c478bd9Sstevel@tonic-gate static void parens_attr(char *, void **, int *);
407c478bd9Sstevel@tonic-gate static void merge_attrs(struct attr_node *, char *);
417c478bd9Sstevel@tonic-gate static char *build_attrs_list(void *collator);
427c478bd9Sstevel@tonic-gate static void collect_attrs(void *, VISIT, int, void *);
437c478bd9Sstevel@tonic-gate static SLPBoolean unpackDAAdvert_attr(slp_handle_impl_t *, char *,
447c478bd9Sstevel@tonic-gate 					SLPAttrCallback, void *,
457c478bd9Sstevel@tonic-gate 					void **, int *);
467c478bd9Sstevel@tonic-gate static SLPBoolean unpackSAAdvert_attr(slp_handle_impl_t *, char *,
477c478bd9Sstevel@tonic-gate 					SLPAttrCallback, void *,
487c478bd9Sstevel@tonic-gate 					void **, int *);
497c478bd9Sstevel@tonic-gate 
SLPFindAttrs(SLPHandle hSLP,const char * pcURL,const char * pcScope,const char * pcAttrIds,SLPAttrCallback callback,void * pvUser)507c478bd9Sstevel@tonic-gate SLPError SLPFindAttrs(SLPHandle hSLP, const char *pcURL, const char *pcScope,
517c478bd9Sstevel@tonic-gate 			const char *pcAttrIds,
527c478bd9Sstevel@tonic-gate 			SLPAttrCallback callback, void *pvUser) {
537c478bd9Sstevel@tonic-gate 	SLPError err;
547c478bd9Sstevel@tonic-gate 	int wantSAAdvert =
557c478bd9Sstevel@tonic-gate 		strcasecmp(pcURL, "service:service-agent") == 0;
567c478bd9Sstevel@tonic-gate 	int wantDAAdvert =
577c478bd9Sstevel@tonic-gate 		strcasecmp(pcURL, "service:directory-agent") == 0;
587c478bd9Sstevel@tonic-gate 	int isSpecial = wantSAAdvert || wantDAAdvert;
597c478bd9Sstevel@tonic-gate 	SLPMsgReplyCB *unpack_cb;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	if (!hSLP || !pcURL || !pcScope || (!*pcScope && !isSpecial) ||
637c478bd9Sstevel@tonic-gate 	    !pcAttrIds || !callback) {
647c478bd9Sstevel@tonic-gate 		return (SLP_PARAMETER_BAD);
657c478bd9Sstevel@tonic-gate 	}
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	if ((strlen(pcURL) > SLP_MAX_STRINGLEN) ||
687c478bd9Sstevel@tonic-gate 	    (strlen(pcScope) > SLP_MAX_STRINGLEN) ||
697c478bd9Sstevel@tonic-gate 	    (strlen(pcAttrIds) > SLP_MAX_STRINGLEN)) {
707c478bd9Sstevel@tonic-gate 	    return (SLP_PARAMETER_BAD);
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	if ((err = slp_start_call(hSLP)) != SLP_OK)
747c478bd9Sstevel@tonic-gate 		return (err);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/* Special packer and unpacker for DA and SA solicitations */
777c478bd9Sstevel@tonic-gate 	if (wantDAAdvert) {
787c478bd9Sstevel@tonic-gate 		unpack_cb = (SLPMsgReplyCB *)unpackDAAdvert_attr;
797c478bd9Sstevel@tonic-gate 		err = slp_packSrvRqst(pcURL, "", hSLP);
807c478bd9Sstevel@tonic-gate 		((slp_handle_impl_t *)hSLP)->force_multicast = SLP_TRUE;
817c478bd9Sstevel@tonic-gate 	} else if (wantSAAdvert) {
827c478bd9Sstevel@tonic-gate 		unpack_cb = (SLPMsgReplyCB *)unpackSAAdvert_attr;
837c478bd9Sstevel@tonic-gate 		err = slp_packSrvRqst(pcURL, "", hSLP);
847c478bd9Sstevel@tonic-gate 		((slp_handle_impl_t *)hSLP)->force_multicast = SLP_TRUE;
857c478bd9Sstevel@tonic-gate 	} else {
867c478bd9Sstevel@tonic-gate 		/* normal service request */
877c478bd9Sstevel@tonic-gate 		unpack_cb = (SLPMsgReplyCB *)slp_UnpackAttrReply;
887c478bd9Sstevel@tonic-gate 		/* format params into msgBuf */
897c478bd9Sstevel@tonic-gate 		err = slp_packAttrRqst(hSLP, pcURL, pcAttrIds);
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if (err == SLP_OK)
937c478bd9Sstevel@tonic-gate 		err = slp_ua_common(hSLP, pcScope,
94*48d1bcbbSToomas Soome 		    (SLPGenericAppCB *)(uintptr_t)callback, pvUser, unpack_cb);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (err != SLP_OK)
977c478bd9Sstevel@tonic-gate 		slp_end_call(hSLP);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	return (err);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
slp_UnpackAttrReply(slp_handle_impl_t * hp,char * reply,SLPAttrCallback cb,void * cookie,void ** collator,int * numResults)1027c478bd9Sstevel@tonic-gate SLPBoolean slp_UnpackAttrReply(slp_handle_impl_t *hp, char *reply,
1037c478bd9Sstevel@tonic-gate 				SLPAttrCallback cb, void *cookie,
1047c478bd9Sstevel@tonic-gate 				void **collator, int *numResults) {
1057c478bd9Sstevel@tonic-gate 	char *pcAttrList;
1067c478bd9Sstevel@tonic-gate 	SLPError errCode;
1077c478bd9Sstevel@tonic-gate 	unsigned short protoErrCode;
1087c478bd9Sstevel@tonic-gate 	size_t len, off;
1097c478bd9Sstevel@tonic-gate 	int maxResults = slp_get_maxResults();
1107c478bd9Sstevel@tonic-gate 	SLPBoolean cont = SLP_TRUE;
1117c478bd9Sstevel@tonic-gate 	int auth_cnt;
1127c478bd9Sstevel@tonic-gate 	size_t tbv_len;
1137c478bd9Sstevel@tonic-gate 	char *attr_tbv;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (!reply) {
1167c478bd9Sstevel@tonic-gate 		/* no more results */
1177c478bd9Sstevel@tonic-gate 		if (!hp->async) {
1187c478bd9Sstevel@tonic-gate 		    pcAttrList = build_attrs_list(*collator);
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		if (!hp->async && pcAttrList) {
1227c478bd9Sstevel@tonic-gate 		    cb(hp, pcAttrList, SLP_OK, cookie);
1237c478bd9Sstevel@tonic-gate 		    free(pcAttrList);
1247c478bd9Sstevel@tonic-gate 		}
1257c478bd9Sstevel@tonic-gate 		cb(hp, NULL, SLP_LAST_CALL, cookie);
1267c478bd9Sstevel@tonic-gate 		return (SLP_FALSE);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/* parse reply into params */
1307c478bd9Sstevel@tonic-gate 	len = slp_get_length(reply);
1317c478bd9Sstevel@tonic-gate 	off = SLP_HDRLEN + slp_get_langlen(reply);
1327c478bd9Sstevel@tonic-gate 	/* err code */
1337c478bd9Sstevel@tonic-gate 	if (slp_get_sht(reply, len, &off, &protoErrCode) != SLP_OK)
1347c478bd9Sstevel@tonic-gate 		return (SLP_TRUE);
1357c478bd9Sstevel@tonic-gate 	/* internal errors should have been filtered out by the net code */
1367c478bd9Sstevel@tonic-gate 	if ((errCode = slp_map_err(protoErrCode)) != SLP_OK) {
1377c478bd9Sstevel@tonic-gate 		return (cb(hp, NULL, errCode, cookie));
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	/* attr list */
1417c478bd9Sstevel@tonic-gate 	attr_tbv = reply + off;
1427c478bd9Sstevel@tonic-gate 	tbv_len = off;
1437c478bd9Sstevel@tonic-gate 	if (slp_get_string(reply, len, &off, &pcAttrList) != SLP_OK)
1447c478bd9Sstevel@tonic-gate 		return (SLP_TRUE);
1457c478bd9Sstevel@tonic-gate 	tbv_len = off - tbv_len;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/* number of attr auths */
1487c478bd9Sstevel@tonic-gate 	if (slp_get_byte(reply, len, &off, &auth_cnt) != SLP_OK) {
1497c478bd9Sstevel@tonic-gate 	    goto cleanup;
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/* get and verify auth blocks */
1537c478bd9Sstevel@tonic-gate 	if ((!hp->internal_call && slp_get_security_on()) || auth_cnt > 0) {
1547c478bd9Sstevel@tonic-gate 		size_t abLen = 0;
1557c478bd9Sstevel@tonic-gate 		struct iovec iov[1];
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		iov[0].iov_base = attr_tbv;
1587c478bd9Sstevel@tonic-gate 		iov[0].iov_len = tbv_len;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		if (slp_verify(iov, 1,
1617c478bd9Sstevel@tonic-gate 				reply + off,
1627c478bd9Sstevel@tonic-gate 				len - off,
1637c478bd9Sstevel@tonic-gate 				auth_cnt,
1647c478bd9Sstevel@tonic-gate 				&abLen) != SLP_OK) {
1657c478bd9Sstevel@tonic-gate 		    goto cleanup;
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/* collate */
1707c478bd9Sstevel@tonic-gate 	if (!hp->async) {
1717c478bd9Sstevel@tonic-gate 		collate_attrs(pcAttrList, collator, numResults, maxResults);
1727c478bd9Sstevel@tonic-gate 	} else {
1737c478bd9Sstevel@tonic-gate 		/* async: invoke cb */
1747c478bd9Sstevel@tonic-gate 		cont = cb((SLPHandle) hp, pcAttrList, errCode, cookie);
1757c478bd9Sstevel@tonic-gate 		(*numResults)++;
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate cleanup:
1797c478bd9Sstevel@tonic-gate 	free(pcAttrList);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/* check maxResults */
1827c478bd9Sstevel@tonic-gate 	if (!hp->internal_call && *numResults == maxResults) {
1837c478bd9Sstevel@tonic-gate 		return (SLP_FALSE);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	return (cont);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * unpackDAAdvert_attr follows the same logic stream as UnpackAttrReply,
1917c478bd9Sstevel@tonic-gate  * except that reply contains a DAAdvert.
1927c478bd9Sstevel@tonic-gate  */
unpackDAAdvert_attr(slp_handle_impl_t * hp,char * reply,SLPAttrCallback cb,void * cookie,void ** collator,int * numResults)1937c478bd9Sstevel@tonic-gate static SLPBoolean unpackDAAdvert_attr(slp_handle_impl_t *hp, char *reply,
1947c478bd9Sstevel@tonic-gate 					SLPAttrCallback cb, void *cookie,
1957c478bd9Sstevel@tonic-gate 					void **collator, int *numResults) {
1967c478bd9Sstevel@tonic-gate 	char *surl, *scopes, *attrs, *spis;
1977c478bd9Sstevel@tonic-gate 	SLPBoolean cont = SLP_TRUE;
1987c478bd9Sstevel@tonic-gate 	SLPError errCode;
1997c478bd9Sstevel@tonic-gate 	int maxResults = slp_get_maxResults();
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (!reply) {
2027c478bd9Sstevel@tonic-gate 		/* no more results */
2037c478bd9Sstevel@tonic-gate 		if (!hp->async) {
2047c478bd9Sstevel@tonic-gate 		    attrs = build_attrs_list(*collator);
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		if (!hp->async && attrs) {
2087c478bd9Sstevel@tonic-gate 			cb(hp, attrs, SLP_OK, cookie);
2097c478bd9Sstevel@tonic-gate 			free(attrs);
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 		cb(hp, NULL, SLP_LAST_CALL, cookie);
2127c478bd9Sstevel@tonic-gate 		return (SLP_FALSE);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (slp_unpackDAAdvert(reply, &surl, &scopes, &attrs, &spis, &errCode)
2167c478bd9Sstevel@tonic-gate 	    != SLP_OK) {
2177c478bd9Sstevel@tonic-gate 		return (SLP_TRUE);
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 	if (errCode != SLP_OK) {
2207c478bd9Sstevel@tonic-gate 		return (cb(hp, NULL, errCode, cookie));
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/* collate */
2247c478bd9Sstevel@tonic-gate 	if (!hp->async) {
2257c478bd9Sstevel@tonic-gate 		collate_attrs(attrs, collator, numResults, maxResults);
2267c478bd9Sstevel@tonic-gate 	} else {
2277c478bd9Sstevel@tonic-gate 		/* async: invoke cb */
2287c478bd9Sstevel@tonic-gate 		cont = cb((SLPHandle) hp, attrs, errCode, cookie);
2297c478bd9Sstevel@tonic-gate 		(*numResults)++;
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/* cleanup */
2337c478bd9Sstevel@tonic-gate 	free(surl);
2347c478bd9Sstevel@tonic-gate 	free(scopes);
2357c478bd9Sstevel@tonic-gate 	free(attrs);
2367c478bd9Sstevel@tonic-gate 	free(spis);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	/* check maxResults */
2397c478bd9Sstevel@tonic-gate 	if (!hp->internal_call && *numResults == maxResults) {
2407c478bd9Sstevel@tonic-gate 		return (SLP_FALSE);
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	return (cont);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * unpackSAAdvert_attr follows the same logic stream as UnpackAttrReply,
2487c478bd9Sstevel@tonic-gate  * except that reply contains an SAAdvert.
2497c478bd9Sstevel@tonic-gate  */
unpackSAAdvert_attr(slp_handle_impl_t * hp,char * reply,SLPAttrCallback cb,void * cookie,void ** collator,int * numResults)2507c478bd9Sstevel@tonic-gate static SLPBoolean unpackSAAdvert_attr(slp_handle_impl_t *hp, char *reply,
2517c478bd9Sstevel@tonic-gate 					SLPAttrCallback cb, void *cookie,
2527c478bd9Sstevel@tonic-gate 					void **collator, int *numResults) {
2537c478bd9Sstevel@tonic-gate 	char *surl, *scopes, *attrs;
2547c478bd9Sstevel@tonic-gate 	SLPBoolean cont = SLP_TRUE;
2557c478bd9Sstevel@tonic-gate 	int maxResults = slp_get_maxResults();
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (!reply) {
2587c478bd9Sstevel@tonic-gate 		/* no more results */
2597c478bd9Sstevel@tonic-gate 		if (!hp->async) {
2607c478bd9Sstevel@tonic-gate 		    attrs = build_attrs_list(*collator);
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 		if (!hp->async && attrs) {
2647c478bd9Sstevel@tonic-gate 			cb(hp, attrs, SLP_OK, cookie);
2657c478bd9Sstevel@tonic-gate 			free(attrs);
2667c478bd9Sstevel@tonic-gate 		}
2677c478bd9Sstevel@tonic-gate 		cb(hp, NULL, SLP_LAST_CALL, cookie);
2687c478bd9Sstevel@tonic-gate 		return (SLP_FALSE);
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if (slp_unpackSAAdvert(reply, &surl, &scopes, &attrs) != SLP_OK) {
2727c478bd9Sstevel@tonic-gate 		return (SLP_TRUE);
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/* collate */
2767c478bd9Sstevel@tonic-gate 	if (!hp->async) {
2777c478bd9Sstevel@tonic-gate 		collate_attrs(attrs, collator, numResults, maxResults);
2787c478bd9Sstevel@tonic-gate 	} else {
2797c478bd9Sstevel@tonic-gate 		/* async: invoke cb */
2807c478bd9Sstevel@tonic-gate 		cont = cb((SLPHandle) hp, attrs, SLP_OK, cookie);
2817c478bd9Sstevel@tonic-gate 		(*numResults)++;
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	/* cleanup */
2857c478bd9Sstevel@tonic-gate 	free(surl);
2867c478bd9Sstevel@tonic-gate 	free(scopes);
2877c478bd9Sstevel@tonic-gate 	free(attrs);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/* check maxResults */
2907c478bd9Sstevel@tonic-gate 	if (!hp->internal_call && *numResults == maxResults) {
2917c478bd9Sstevel@tonic-gate 		return (SLP_FALSE);
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	return (cont);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
slp_packAttrRqst(slp_handle_impl_t * hp,const char * url,const char * ids)2977c478bd9Sstevel@tonic-gate static SLPError slp_packAttrRqst(slp_handle_impl_t *hp, const char *url,
2987c478bd9Sstevel@tonic-gate 					const char *ids) {
2997c478bd9Sstevel@tonic-gate 	SLPError err;
3007c478bd9Sstevel@tonic-gate 	size_t len, tmplen, msgLen;
3017c478bd9Sstevel@tonic-gate 	slp_msg_t *msg = &(hp->msg);
3027c478bd9Sstevel@tonic-gate 	char *spi = NULL;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	if (slp_get_security_on()) {
3057c478bd9Sstevel@tonic-gate 	    spi = (char *)SLPGetProperty(SLP_CONFIG_SPI);
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (!spi || !*spi) {
3097c478bd9Sstevel@tonic-gate 		spi = "";
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/*
3137c478bd9Sstevel@tonic-gate 	 * Allocate iovec for the messge. An AttrRqst is layed out thus:
3147c478bd9Sstevel@tonic-gate 	 *  0: header
3157c478bd9Sstevel@tonic-gate 	 *  1: prlist length
3167c478bd9Sstevel@tonic-gate 	 *  2: prlist (filled in later by networking code)
3177c478bd9Sstevel@tonic-gate 	 *  3: URL string
3187c478bd9Sstevel@tonic-gate 	 *  4: scopes length
3197c478bd9Sstevel@tonic-gate 	 *  5: scopes (filled in later by networking code)
3207c478bd9Sstevel@tonic-gate 	 *  6: tag list string and SPI string
3217c478bd9Sstevel@tonic-gate 	 */
3227c478bd9Sstevel@tonic-gate 	if (!(msg->iov = calloc(7, sizeof (*(msg->iov))))) {
3237c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "slp_packAttrRqst", "out of memory");
3247c478bd9Sstevel@tonic-gate 		return (SLP_MEMORY_ALLOC_FAILED);
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	msg->iovlen = 7;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	/* calculate msg length */
3297c478bd9Sstevel@tonic-gate 	msgLen = 2 +		/* prlist length */
3307c478bd9Sstevel@tonic-gate 	    2 + strlen(url) +	/* URL */
3317c478bd9Sstevel@tonic-gate 	    2 +			/* scope list length */
3327c478bd9Sstevel@tonic-gate 	    2 + strlen(ids) +	/* tag list */
3337c478bd9Sstevel@tonic-gate 	    2 + strlen(spi);	/* SPI string */
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if (!(msg->msg = calloc(1, msgLen))) {
3367c478bd9Sstevel@tonic-gate 		free(msg->iov);
3377c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "slp_packAttrRqst", "out of memory");
3387c478bd9Sstevel@tonic-gate 		return (SLP_MEMORY_ALLOC_FAILED);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/* set pointer to PR list and scope list length spaces */
3427c478bd9Sstevel@tonic-gate 	msg->prlistlen.iov_base = msg->msg;
3437c478bd9Sstevel@tonic-gate 	msg->prlistlen.iov_len = 2;
3447c478bd9Sstevel@tonic-gate 	msg->iov[1].iov_base = msg->msg;
3457c478bd9Sstevel@tonic-gate 	msg->iov[1].iov_len = 2;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	msg->scopeslen.iov_base = msg->msg + 2;
3487c478bd9Sstevel@tonic-gate 	msg->scopeslen.iov_len = 2;
3497c478bd9Sstevel@tonic-gate 	msg->iov[4].iov_base = msg->msg + 2;
3507c478bd9Sstevel@tonic-gate 	msg->iov[4].iov_len = 2;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	/* set up the scopes and prlist pointers into iov */
3537c478bd9Sstevel@tonic-gate 	msg->prlist = &(msg->iov[2]);
3547c478bd9Sstevel@tonic-gate 	msg->scopes = &(msg->iov[5]);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	len = 4;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/* Add URL string */
3597c478bd9Sstevel@tonic-gate 	msg->iov[3].iov_base = msg->msg + len;
3607c478bd9Sstevel@tonic-gate 	tmplen = len;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	err = slp_add_string(msg->msg, msgLen, url, &len);
3637c478bd9Sstevel@tonic-gate 	msg->iov[3].iov_len = len - tmplen;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if (err != SLP_OK)
3667c478bd9Sstevel@tonic-gate 		goto error;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/* Add tag list */
3697c478bd9Sstevel@tonic-gate 	msg->iov[6].iov_base = msg->msg + len;
3707c478bd9Sstevel@tonic-gate 	tmplen = len;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	err = slp_add_string(msg->msg, msgLen, ids, &len);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (err != SLP_OK)
3757c478bd9Sstevel@tonic-gate 		goto error;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	/* SPI string */
3787c478bd9Sstevel@tonic-gate 	err = slp_add_string(msg->msg, msgLen, spi, &len);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	msg->iov[6].iov_len = len - tmplen;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	hp->fid = ATTRRQST;
3837c478bd9Sstevel@tonic-gate 	if (err == SLP_OK) {
3847c478bd9Sstevel@tonic-gate 		return (SLP_OK);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/* else error */
3887c478bd9Sstevel@tonic-gate error:
3897c478bd9Sstevel@tonic-gate 	free(msg->iov);
3907c478bd9Sstevel@tonic-gate 	free(msg->msg);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	return (err);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate 
slp_packAttrRqst_single(const char * url,const char * scopes,const char * ids,char ** msg,const char * lang)3957c478bd9Sstevel@tonic-gate SLPError slp_packAttrRqst_single(const char *url,
3967c478bd9Sstevel@tonic-gate 				const char *scopes,
3977c478bd9Sstevel@tonic-gate 				const char *ids,
3987c478bd9Sstevel@tonic-gate 				char **msg,
3997c478bd9Sstevel@tonic-gate 				const char *lang) {
4007c478bd9Sstevel@tonic-gate 	SLPError err;
4017c478bd9Sstevel@tonic-gate 	size_t len, msgLen;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	msgLen =
4047c478bd9Sstevel@tonic-gate 		SLP_HDRLEN + strlen(lang) + 2 +
4057c478bd9Sstevel@tonic-gate 		2 + strlen(url) +
4067c478bd9Sstevel@tonic-gate 		2 + strlen(scopes) +
4077c478bd9Sstevel@tonic-gate 		2 + strlen(ids) +
4087c478bd9Sstevel@tonic-gate 		2; /* No SPI string for internal calls */
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if (!(*msg = calloc(msgLen, 1))) {
4117c478bd9Sstevel@tonic-gate 	    slp_err(LOG_CRIT, 0, "slp_packAttrRqst_single", "out of memory");
4127c478bd9Sstevel@tonic-gate 	    return (SLP_MEMORY_ALLOC_FAILED);
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	len = 0;
4167c478bd9Sstevel@tonic-gate 	err = slp_add_header(lang, *msg, msgLen, ATTRRQST, msgLen, &len);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	len += 2;	/* empty PR list */
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if (err == SLP_OK) {
4217c478bd9Sstevel@tonic-gate 	    err = slp_add_string(*msg, msgLen, url, &len);
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 	if (err == SLP_OK) {
4247c478bd9Sstevel@tonic-gate 	    err = slp_add_string(*msg, msgLen, scopes, &len);
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 	if (err == SLP_OK) {
4277c478bd9Sstevel@tonic-gate 	    err = slp_add_string(*msg, msgLen, ids, &len);
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	/* empty SPI */
4307c478bd9Sstevel@tonic-gate 	if (err == SLP_OK) {
4317c478bd9Sstevel@tonic-gate 	    err = slp_add_string(*msg, msgLen, "", &len);
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	return (err);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
compare_tags(const void * n1,const void * n2)4377c478bd9Sstevel@tonic-gate static int compare_tags(const void *n1, const void *n2) {
4387c478bd9Sstevel@tonic-gate 	return slp_strcasecmp(
4397c478bd9Sstevel@tonic-gate 		((struct attr_node *)n1)->tag,
4407c478bd9Sstevel@tonic-gate 		((struct attr_node *)n2)->tag);
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate 
merge_attrs(struct attr_node * n,char * vals)4437c478bd9Sstevel@tonic-gate static void merge_attrs(struct attr_node *n, char *vals) {
4447c478bd9Sstevel@tonic-gate 	char *p, *v;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	for (p = v = vals; p; v = p) {
4477c478bd9Sstevel@tonic-gate 		p = slp_utf_strchr(v, ',');
4487c478bd9Sstevel@tonic-gate 		if (p)
4497c478bd9Sstevel@tonic-gate 			*p++ = 0;
4507c478bd9Sstevel@tonic-gate 		slp_add2list(v, &(n->val), SLP_TRUE);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
parens_attr(char * attr,void ** collator,int * numResults)4547c478bd9Sstevel@tonic-gate static void parens_attr(char *attr, void **collator, int *numResults) {
4557c478bd9Sstevel@tonic-gate 	char *open_paren, *close_paren, *equals;
4567c478bd9Sstevel@tonic-gate 	struct attr_node *n, **res;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	open_paren = attr + 1;
4597c478bd9Sstevel@tonic-gate 	close_paren = slp_utf_strchr(open_paren, ')');
4607c478bd9Sstevel@tonic-gate 	if (!close_paren)
4617c478bd9Sstevel@tonic-gate 		return;	/* skip bad attr list */
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	*close_paren = 0;
4647c478bd9Sstevel@tonic-gate 	if (!(equals = slp_utf_strchr(open_paren, '=')))
4657c478bd9Sstevel@tonic-gate 		return;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	*equals++ = 0;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if (!(n = malloc(sizeof (*n)))) {
4707c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "collate_attrs", "out of memory");
4717c478bd9Sstevel@tonic-gate 		return;
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	if (!(n->tag = strdup(open_paren))) {
4757c478bd9Sstevel@tonic-gate 		free(n);
4767c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "collate_attrs", "out of memory");
4777c478bd9Sstevel@tonic-gate 		return;
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 	n->val = NULL;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	res = slp_tsearch(n, collator, compare_tags);
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (*res != n) {
4847c478bd9Sstevel@tonic-gate 		merge_attrs(*res, equals);
4857c478bd9Sstevel@tonic-gate 		free(n->tag); free(n);
4867c478bd9Sstevel@tonic-gate 	} else {
4877c478bd9Sstevel@tonic-gate 		/* not found; populate new attr node */
4887c478bd9Sstevel@tonic-gate 		(*numResults)++;
4897c478bd9Sstevel@tonic-gate 		if (!(n->val = strdup(equals))) {
4907c478bd9Sstevel@tonic-gate 			slp_err(LOG_CRIT, 0, "collate_attrs", "out of memory");
4917c478bd9Sstevel@tonic-gate 			return;
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
collate_attrs(char * attrs,void ** collator,int * numResults,int maxResults)4967c478bd9Sstevel@tonic-gate static void collate_attrs(char *attrs, void **collator,
4977c478bd9Sstevel@tonic-gate 				int *numResults, int maxResults) {
4987c478bd9Sstevel@tonic-gate 	char *start, *end;
4997c478bd9Sstevel@tonic-gate 	struct attr_node *n, **res;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	for (start = attrs;
5027c478bd9Sstevel@tonic-gate 			start &&
5037c478bd9Sstevel@tonic-gate 			*start &&
5047c478bd9Sstevel@tonic-gate 			*numResults != maxResults;
5057c478bd9Sstevel@tonic-gate 						start = end) {
5067c478bd9Sstevel@tonic-gate 		if (*start == ',') start++;
5077c478bd9Sstevel@tonic-gate 		if (*start == '(') {
5087c478bd9Sstevel@tonic-gate 			/* form of (tag=val,val) */
5097c478bd9Sstevel@tonic-gate 			if (!(end = slp_utf_strchr(start, ')')))
5107c478bd9Sstevel@tonic-gate 				return;		/* skip bad attr */
5117c478bd9Sstevel@tonic-gate 			parens_attr(start, collator, numResults);
5127c478bd9Sstevel@tonic-gate 			end++;
5137c478bd9Sstevel@tonic-gate 			continue;
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 		end = slp_utf_strchr(start, ',');
5167c478bd9Sstevel@tonic-gate 		if (end)
5177c478bd9Sstevel@tonic-gate 			*end++ = 0;
5187c478bd9Sstevel@tonic-gate 		/* create a new node with the tag only */
5197c478bd9Sstevel@tonic-gate 		if (!(n = malloc(sizeof (*n)))) {
5207c478bd9Sstevel@tonic-gate 			slp_err(LOG_CRIT, 0, "collate_attrs", "out of memory");
5217c478bd9Sstevel@tonic-gate 			return;
5227c478bd9Sstevel@tonic-gate 		}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 		if (!(n->tag = strdup(start))) {
5257c478bd9Sstevel@tonic-gate 			free(n);
5267c478bd9Sstevel@tonic-gate 			slp_err(LOG_CRIT, 0, "collate_attrs", "out of memory");
5277c478bd9Sstevel@tonic-gate 			return;
5287c478bd9Sstevel@tonic-gate 		}
5297c478bd9Sstevel@tonic-gate 		n->val = NULL;
5307c478bd9Sstevel@tonic-gate 		res = slp_tsearch(n, collator, compare_tags);
5317c478bd9Sstevel@tonic-gate 		if (*res != n) {
5327c478bd9Sstevel@tonic-gate 			/* already in the tree, so just free resources */
5337c478bd9Sstevel@tonic-gate 			free(n->tag); free(n);
5347c478bd9Sstevel@tonic-gate 		}
5357c478bd9Sstevel@tonic-gate 		(*numResults)++;
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
build_attrs_list(void * collator)5397c478bd9Sstevel@tonic-gate static char *build_attrs_list(void *collator) {
5407c478bd9Sstevel@tonic-gate 	char *answer = NULL;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	if (!collator)
5437c478bd9Sstevel@tonic-gate 		return (NULL);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	slp_twalk(collator, collect_attrs, 0, &answer);
5467c478bd9Sstevel@tonic-gate 	return (answer);
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
collect_attrs(void * node,VISIT order,int level,void * cookie)5507c478bd9Sstevel@tonic-gate static void collect_attrs(void *node, VISIT order, int level, void *cookie) {
5517c478bd9Sstevel@tonic-gate 	struct attr_node *n;
5527c478bd9Sstevel@tonic-gate 	char *attr, *p, **answer = (char **)cookie;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (order == endorder || order == leaf) {
5557c478bd9Sstevel@tonic-gate 		n = *(struct attr_node **)node;
5567c478bd9Sstevel@tonic-gate 		if (!n->val) {
5577c478bd9Sstevel@tonic-gate 			/* no values, so no parens */
5587c478bd9Sstevel@tonic-gate 			if (!(attr = malloc(strlen(n->tag) + 1))) {
5597c478bd9Sstevel@tonic-gate 				slp_err(LOG_CRIT, 0, "collect_attrs",
5607c478bd9Sstevel@tonic-gate 					"out of memory");
5617c478bd9Sstevel@tonic-gate 				return;
5627c478bd9Sstevel@tonic-gate 			}
5637c478bd9Sstevel@tonic-gate 			(void) strcpy(attr, n->tag);
5647c478bd9Sstevel@tonic-gate 		} else {
5657c478bd9Sstevel@tonic-gate 			if (!(attr = malloc(1 + strlen(n->tag) + 1 +
5667c478bd9Sstevel@tonic-gate 					    strlen(n->val) + 2))) {
5677c478bd9Sstevel@tonic-gate 				slp_err(LOG_CRIT, 0, "collect_attrs",
5687c478bd9Sstevel@tonic-gate 					"out of memory");
5697c478bd9Sstevel@tonic-gate 				return;
5707c478bd9Sstevel@tonic-gate 			}
5717c478bd9Sstevel@tonic-gate 			/* build attr string */
5727c478bd9Sstevel@tonic-gate 			p = attr;
5737c478bd9Sstevel@tonic-gate 			*p++ = '(';
5747c478bd9Sstevel@tonic-gate 			(void) strcpy(p, n->tag); p += strlen(n->tag);
5757c478bd9Sstevel@tonic-gate 			*p++ = '=';
5767c478bd9Sstevel@tonic-gate 			(void) strcpy(p, n->val); p += strlen(n->val);
5777c478bd9Sstevel@tonic-gate 			*p++ = ')'; *p = 0;
5787c478bd9Sstevel@tonic-gate 		}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		slp_add2list(attr, answer, SLP_FALSE);
5817c478bd9Sstevel@tonic-gate 		free(attr);
5827c478bd9Sstevel@tonic-gate 		free(n->tag); if (n->val) free(n->val); free(n);
5837c478bd9Sstevel@tonic-gate 		free(node);
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate }
586