xref: /titanic_51/usr/src/lib/libresolv2/common/irs/irpmarshall.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright(c) 1989, 1993, 1995
37c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
67c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
77c478bd9Sstevel@tonic-gate  * are met:
87c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
97c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
107c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
117c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
127c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
137c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
147c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
157c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
167c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
177c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
187c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
197c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
227c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
257c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
35*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
367c478bd9Sstevel@tonic-gate  * Portions Copyright (c) 1996 by Internet Software Consortium.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
397c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
407c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
417c478bd9Sstevel@tonic-gate  *
42*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
43*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
45*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
48*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
52*9525b14bSRao Shoaib static const char rcsid[] = "$Id: irpmarshall.c,v 1.7 2006/03/09 23:57:56 marka Exp $";
537c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #if 0
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate Check values are in approrpriate endian order.
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate Double check memory allocations on unmarhsalling
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #endif
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* Extern */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include "port_before.h"
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #include <sys/types.h>
697c478bd9Sstevel@tonic-gate #include <sys/socket.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #include <netinet/in.h>
727c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
737c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #include <stdio.h>
767c478bd9Sstevel@tonic-gate #include <ctype.h>
777c478bd9Sstevel@tonic-gate #include <pwd.h>
787c478bd9Sstevel@tonic-gate #include <stdlib.h>
797c478bd9Sstevel@tonic-gate #include <string.h>
807c478bd9Sstevel@tonic-gate #include <syslog.h>
817c478bd9Sstevel@tonic-gate #include <utmp.h>
827c478bd9Sstevel@tonic-gate #include <unistd.h>
837c478bd9Sstevel@tonic-gate #include <assert.h>
847c478bd9Sstevel@tonic-gate #include <errno.h>
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #include <irs.h>
877c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
887c478bd9Sstevel@tonic-gate #include <isc/irpmarshall.h>
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #include "port_after.h"
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #ifndef HAVE_STRNDUP
947c478bd9Sstevel@tonic-gate static char    *strndup(const char *str, size_t len);
957c478bd9Sstevel@tonic-gate #endif
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static char   **splitarray(const char *buffer, const char *buffend, char delim);
987c478bd9Sstevel@tonic-gate static int	joinarray(char * const * argv, char *buffer, char delim);
997c478bd9Sstevel@tonic-gate static char    *getfield(char **res, size_t reslen, char **buffer, char delim);
1007c478bd9Sstevel@tonic-gate static size_t	joinlength(char * const *argv);
1017c478bd9Sstevel@tonic-gate static void	free_array(char **argv, size_t entries);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #define ADDR_T_STR(x) (x == AF_INET ? "AF_INET" :\
1047c478bd9Sstevel@tonic-gate 		       (x == AF_INET6 ? "AF_INET6" : "UNKNOWN"))
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #define MAXPADDRSIZE (sizeof "255.255.255.255" + 1)
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate static char COMMA = ',';
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static const char *COMMASTR = ",";
1117c478bd9Sstevel@tonic-gate static const char *COLONSTR = ":";
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /* See big comment at bottom of irpmarshall.h for description. */
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #ifdef WANT_IRS_PW
1197c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct passwd +++++++++++++++++++++++++ */
1207c478bd9Sstevel@tonic-gate 
121*9525b14bSRao Shoaib /*%
1227c478bd9Sstevel@tonic-gate  * int irp_marshall_pw(const struct passwd *pw, char **buffer, size_t *len)
1237c478bd9Sstevel@tonic-gate  *
124*9525b14bSRao Shoaib  * notes: \li
1257c478bd9Sstevel@tonic-gate  *
126*9525b14bSRao Shoaib  *	See irpmarshall.h
1277c478bd9Sstevel@tonic-gate  *
128*9525b14bSRao Shoaib  * return: \li
1297c478bd9Sstevel@tonic-gate  *
1307c478bd9Sstevel@tonic-gate  *	0 on sucess, -1 on failure.
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate int
1357c478bd9Sstevel@tonic-gate irp_marshall_pw(const struct passwd *pw, char **buffer, size_t *len) {
136*9525b14bSRao Shoaib 	size_t need = 1 ;		/*%< for null byte */
1377c478bd9Sstevel@tonic-gate 	char pwUid[24];
1387c478bd9Sstevel@tonic-gate 	char pwGid[24];
1397c478bd9Sstevel@tonic-gate 	char pwChange[24];
1407c478bd9Sstevel@tonic-gate 	char pwExpire[24];
1417c478bd9Sstevel@tonic-gate 	const char *pwClass;
1427c478bd9Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (pw == NULL || len == NULL) {
1457c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1467c478bd9Sstevel@tonic-gate 		return (-1);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	sprintf(pwUid, "%ld", (long)pw->pw_uid);
1507c478bd9Sstevel@tonic-gate 	sprintf(pwGid, "%ld", (long)pw->pw_gid);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_CHANGE
1537c478bd9Sstevel@tonic-gate 	sprintf(pwChange, "%ld", (long)pw->pw_change);
1547c478bd9Sstevel@tonic-gate #else
1557c478bd9Sstevel@tonic-gate 	pwChange[0] = '0';
1567c478bd9Sstevel@tonic-gate 	pwChange[1] = '\0';
1577c478bd9Sstevel@tonic-gate #endif
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_EXPIRE
1607c478bd9Sstevel@tonic-gate 	sprintf(pwExpire, "%ld", (long)pw->pw_expire);
1617c478bd9Sstevel@tonic-gate #else
1627c478bd9Sstevel@tonic-gate 	pwExpire[0] = '0';
1637c478bd9Sstevel@tonic-gate 	pwExpire[1] = '\0';
1647c478bd9Sstevel@tonic-gate #endif
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_CLASS
1677c478bd9Sstevel@tonic-gate 	pwClass = pw->pw_class;
1687c478bd9Sstevel@tonic-gate #else
1697c478bd9Sstevel@tonic-gate 	pwClass = "";
1707c478bd9Sstevel@tonic-gate #endif
1717c478bd9Sstevel@tonic-gate 
172*9525b14bSRao Shoaib 	need += strlen(pw->pw_name)	+ 1; /*%< one for fieldsep */
1737c478bd9Sstevel@tonic-gate 	need += strlen(pw->pw_passwd)	+ 1;
1747c478bd9Sstevel@tonic-gate 	need += strlen(pwUid)		+ 1;
1757c478bd9Sstevel@tonic-gate 	need += strlen(pwGid)		+ 1;
1767c478bd9Sstevel@tonic-gate 	need += strlen(pwClass)		+ 1;
1777c478bd9Sstevel@tonic-gate 	need += strlen(pwChange)	+ 1;
1787c478bd9Sstevel@tonic-gate 	need += strlen(pwExpire)	+ 1;
1797c478bd9Sstevel@tonic-gate 	need += strlen(pw->pw_gecos)	+ 1;
1807c478bd9Sstevel@tonic-gate 	need += strlen(pw->pw_dir)	+ 1;
1817c478bd9Sstevel@tonic-gate 	need += strlen(pw->pw_shell)	+ 1;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
1847c478bd9Sstevel@tonic-gate 		*len = need;
1857c478bd9Sstevel@tonic-gate 		return (0);
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
1897c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1907c478bd9Sstevel@tonic-gate 		return (-1);
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
194*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
1957c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
1967c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
1977c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
1987c478bd9Sstevel@tonic-gate 			return (-1);
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 		*len = need;
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	strcpy(*buffer, pw->pw_name);		strcat(*buffer, fieldsep);
2057c478bd9Sstevel@tonic-gate 	strcat(*buffer, pw->pw_passwd);		strcat(*buffer, fieldsep);
2067c478bd9Sstevel@tonic-gate 	strcat(*buffer, pwUid);			strcat(*buffer, fieldsep);
2077c478bd9Sstevel@tonic-gate 	strcat(*buffer, pwGid);			strcat(*buffer, fieldsep);
2087c478bd9Sstevel@tonic-gate 	strcat(*buffer, pwClass);		strcat(*buffer, fieldsep);
2097c478bd9Sstevel@tonic-gate 	strcat(*buffer, pwChange);		strcat(*buffer, fieldsep);
2107c478bd9Sstevel@tonic-gate 	strcat(*buffer, pwExpire);		strcat(*buffer, fieldsep);
2117c478bd9Sstevel@tonic-gate 	strcat(*buffer, pw->pw_gecos);		strcat(*buffer, fieldsep);
2127c478bd9Sstevel@tonic-gate 	strcat(*buffer, pw->pw_dir);		strcat(*buffer, fieldsep);
2137c478bd9Sstevel@tonic-gate 	strcat(*buffer, pw->pw_shell);		strcat(*buffer, fieldsep);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	return (0);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
218*9525b14bSRao Shoaib /*%
2197c478bd9Sstevel@tonic-gate  * int irp_unmarshall_pw(struct passwd *pw, char *buffer)
2207c478bd9Sstevel@tonic-gate  *
221*9525b14bSRao Shoaib  * notes: \li
2227c478bd9Sstevel@tonic-gate  *
223*9525b14bSRao Shoaib  *	See irpmarshall.h
2247c478bd9Sstevel@tonic-gate  *
225*9525b14bSRao Shoaib  * return: \li
2267c478bd9Sstevel@tonic-gate  *
2277c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure
2287c478bd9Sstevel@tonic-gate  *
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate int
2327c478bd9Sstevel@tonic-gate irp_unmarshall_pw(struct passwd *pw, char *buffer) {
2337c478bd9Sstevel@tonic-gate 	char *name, *pass, *class, *gecos, *dir, *shell;
2347c478bd9Sstevel@tonic-gate 	uid_t pwuid;
2357c478bd9Sstevel@tonic-gate 	gid_t pwgid;
2367c478bd9Sstevel@tonic-gate 	time_t pwchange;
2377c478bd9Sstevel@tonic-gate 	time_t pwexpire;
2387c478bd9Sstevel@tonic-gate 	char *p;
2397c478bd9Sstevel@tonic-gate 	long t;
2407c478bd9Sstevel@tonic-gate 	char tmpbuf[24];
2417c478bd9Sstevel@tonic-gate 	char *tb = &tmpbuf[0];
2427c478bd9Sstevel@tonic-gate 	char fieldsep = ':';
2437c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	name = pass = class = gecos = dir = shell = NULL;
2467c478bd9Sstevel@tonic-gate 	p = buffer;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/* pw_name field */
2497c478bd9Sstevel@tonic-gate 	name = NULL;
2507c478bd9Sstevel@tonic-gate 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0) {
2517c478bd9Sstevel@tonic-gate 		goto error;
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/* pw_passwd field */
2557c478bd9Sstevel@tonic-gate 	pass = NULL;
256*9525b14bSRao Shoaib 	if (getfield(&pass, 0, &p, fieldsep) == NULL) { /*%< field can be empty */
2577c478bd9Sstevel@tonic-gate 		goto error;
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/* pw_uid field */
2627c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
2637c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
2647c478bd9Sstevel@tonic-gate 	    strlen(tb) == 0) {
2657c478bd9Sstevel@tonic-gate 		goto error;
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
2687c478bd9Sstevel@tonic-gate 	if (*tb) {
269*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 	pwuid = (uid_t)t;
272*9525b14bSRao Shoaib 	if ((long) pwuid != t) {	/*%< value must have been too big. */
2737c478bd9Sstevel@tonic-gate 		goto error;
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/* pw_gid field */
2797c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
2807c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
2817c478bd9Sstevel@tonic-gate 	    strlen(tb) == 0) {
2827c478bd9Sstevel@tonic-gate 		goto error;
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
2857c478bd9Sstevel@tonic-gate 	if (*tb) {
286*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 	pwgid = (gid_t)t;
289*9525b14bSRao Shoaib 	if ((long)pwgid != t) {	/*%< value must have been too big. */
2907c478bd9Sstevel@tonic-gate 		goto error;
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	/* pw_class field */
2967c478bd9Sstevel@tonic-gate 	class = NULL;
2977c478bd9Sstevel@tonic-gate 	if (getfield(&class, 0, &p, fieldsep) == NULL) {
2987c478bd9Sstevel@tonic-gate 		goto error;
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/* pw_change field */
3047c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
3057c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
3067c478bd9Sstevel@tonic-gate 	    strlen(tb) == 0) {
3077c478bd9Sstevel@tonic-gate 		goto error;
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
3107c478bd9Sstevel@tonic-gate 	if (*tb) {
311*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 	pwchange = (time_t)t;
314*9525b14bSRao Shoaib 	if ((long)pwchange != t) {	/*%< value must have been too big. */
3157c478bd9Sstevel@tonic-gate 		goto error;
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/* pw_expire field */
3217c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
3227c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
3237c478bd9Sstevel@tonic-gate 	    strlen(tb) == 0) {
3247c478bd9Sstevel@tonic-gate 		goto error;
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
3277c478bd9Sstevel@tonic-gate 	if (*tb) {
328*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 	pwexpire = (time_t)t;
331*9525b14bSRao Shoaib 	if ((long) pwexpire != t) {	/*%< value must have been too big. */
3327c478bd9Sstevel@tonic-gate 		goto error;
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/* pw_gecos field */
3387c478bd9Sstevel@tonic-gate 	gecos = NULL;
3397c478bd9Sstevel@tonic-gate 	if (getfield(&gecos, 0, &p, fieldsep) == NULL) {
3407c478bd9Sstevel@tonic-gate 		goto error;
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	/* pw_dir field */
3467c478bd9Sstevel@tonic-gate 	dir = NULL;
3477c478bd9Sstevel@tonic-gate 	if (getfield(&dir, 0, &p, fieldsep) == NULL) {
3487c478bd9Sstevel@tonic-gate 		goto error;
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	/* pw_shell field */
3547c478bd9Sstevel@tonic-gate 	shell = NULL;
3557c478bd9Sstevel@tonic-gate 	if (getfield(&shell, 0, &p, fieldsep) == NULL) {
3567c478bd9Sstevel@tonic-gate 		goto error;
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	pw->pw_name = name;
3627c478bd9Sstevel@tonic-gate 	pw->pw_passwd = pass;
3637c478bd9Sstevel@tonic-gate 	pw->pw_uid = pwuid;
3647c478bd9Sstevel@tonic-gate 	pw->pw_gid = pwgid;
3657c478bd9Sstevel@tonic-gate 	pw->pw_gecos = gecos;
3667c478bd9Sstevel@tonic-gate 	pw->pw_dir = dir;
3677c478bd9Sstevel@tonic-gate 	pw->pw_shell = shell;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_CHANGE
3707c478bd9Sstevel@tonic-gate 	pw->pw_change = pwchange;
3717c478bd9Sstevel@tonic-gate #endif
3727c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_CLASS
3737c478bd9Sstevel@tonic-gate 	pw->pw_class = class;
3747c478bd9Sstevel@tonic-gate #endif
3757c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_EXPIRE
3767c478bd9Sstevel@tonic-gate 	pw->pw_expire = pwexpire;
3777c478bd9Sstevel@tonic-gate #endif
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	return (0);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate  error:
3827c478bd9Sstevel@tonic-gate 	errno = myerrno;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (name != NULL) free(name);
3857c478bd9Sstevel@tonic-gate 	if (pass != NULL) free(pass);
3867c478bd9Sstevel@tonic-gate 	if (gecos != NULL) free(gecos);
3877c478bd9Sstevel@tonic-gate 	if (dir != NULL) free(dir);
3887c478bd9Sstevel@tonic-gate 	if (shell != NULL) free(shell);
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	return (-1);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate /* ------------------------- struct passwd ------------------------- */
3947c478bd9Sstevel@tonic-gate #endif /* WANT_IRS_PW */
3957c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct group +++++++++++++++++++++++++ */
3967c478bd9Sstevel@tonic-gate 
397*9525b14bSRao Shoaib /*%
3987c478bd9Sstevel@tonic-gate  * int irp_marshall_gr(const struct group *gr, char **buffer, size_t *len)
3997c478bd9Sstevel@tonic-gate  *
400*9525b14bSRao Shoaib  * notes: \li
4017c478bd9Sstevel@tonic-gate  *
402*9525b14bSRao Shoaib  *	See irpmarshall.h.
4037c478bd9Sstevel@tonic-gate  *
404*9525b14bSRao Shoaib  * return: \li
4057c478bd9Sstevel@tonic-gate  *
4067c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure
4077c478bd9Sstevel@tonic-gate  */
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate int
4107c478bd9Sstevel@tonic-gate irp_marshall_gr(const struct group *gr, char **buffer, size_t *len) {
411*9525b14bSRao Shoaib 	size_t need = 1;	/*%< for null byte */
4127c478bd9Sstevel@tonic-gate 	char grGid[24];
4137c478bd9Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if (gr == NULL || len == NULL) {
4167c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4177c478bd9Sstevel@tonic-gate 		return (-1);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	sprintf(grGid, "%ld", (long)gr->gr_gid);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	need += strlen(gr->gr_name) + 1;
4237c478bd9Sstevel@tonic-gate #ifndef MISSING_GR_PASSWD
4247c478bd9Sstevel@tonic-gate 	need += strlen(gr->gr_passwd) + 1;
4257c478bd9Sstevel@tonic-gate #else
4267c478bd9Sstevel@tonic-gate 	need++;
4277c478bd9Sstevel@tonic-gate #endif
4287c478bd9Sstevel@tonic-gate 	need += strlen(grGid) + 1;
4297c478bd9Sstevel@tonic-gate 	need += joinlength(gr->gr_mem) + 1;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
4327c478bd9Sstevel@tonic-gate 		*len = need;
4337c478bd9Sstevel@tonic-gate 		return (0);
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
4377c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4387c478bd9Sstevel@tonic-gate 		return (-1);
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
442*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
4437c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
4447c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
4457c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
4467c478bd9Sstevel@tonic-gate 			return (-1);
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 		*len = need;
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	strcpy(*buffer, gr->gr_name);		strcat(*buffer, fieldsep);
4537c478bd9Sstevel@tonic-gate #ifndef MISSING_GR_PASSWD
4547c478bd9Sstevel@tonic-gate 	strcat(*buffer, gr->gr_passwd);
4557c478bd9Sstevel@tonic-gate #endif
4567c478bd9Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
4577c478bd9Sstevel@tonic-gate 	strcat(*buffer, grGid);			strcat(*buffer, fieldsep);
4587c478bd9Sstevel@tonic-gate 	joinarray(gr->gr_mem, *buffer, COMMA) ;	strcat(*buffer, fieldsep);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	return (0);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
463*9525b14bSRao Shoaib /*%
4647c478bd9Sstevel@tonic-gate  * int irp_unmarshall_gr(struct group *gr, char *buffer)
4657c478bd9Sstevel@tonic-gate  *
466*9525b14bSRao Shoaib  * notes: \li
4677c478bd9Sstevel@tonic-gate  *
468*9525b14bSRao Shoaib  *	See irpmarshall.h
4697c478bd9Sstevel@tonic-gate  *
470*9525b14bSRao Shoaib  * return: \li
4717c478bd9Sstevel@tonic-gate  *
4727c478bd9Sstevel@tonic-gate  *	0 on success and -1 on failure.
4737c478bd9Sstevel@tonic-gate  *
4747c478bd9Sstevel@tonic-gate  */
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate int
4777c478bd9Sstevel@tonic-gate irp_unmarshall_gr(struct group *gr, char *buffer) {
4787c478bd9Sstevel@tonic-gate 	char *p, *q;
4797c478bd9Sstevel@tonic-gate 	gid_t grgid;
4807c478bd9Sstevel@tonic-gate 	long t;
4817c478bd9Sstevel@tonic-gate 	char *name = NULL;
4827c478bd9Sstevel@tonic-gate 	char *pass = NULL;
4837c478bd9Sstevel@tonic-gate 	char **members = NULL;
4847c478bd9Sstevel@tonic-gate 	char tmpbuf[24];
4857c478bd9Sstevel@tonic-gate 	char *tb;
4867c478bd9Sstevel@tonic-gate 	char fieldsep = ':';
4877c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	if (gr == NULL || buffer == NULL) {
4907c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4917c478bd9Sstevel@tonic-gate 		return (-1);
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	p = buffer;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	/* gr_name field */
4977c478bd9Sstevel@tonic-gate 	name = NULL;
498*9525b14bSRao Shoaib 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
4997c478bd9Sstevel@tonic-gate 		goto error;
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	/* gr_passwd field */
5047c478bd9Sstevel@tonic-gate 	pass = NULL;
5057c478bd9Sstevel@tonic-gate 	if (getfield(&pass, 0, &p, fieldsep) == NULL) {
5067c478bd9Sstevel@tonic-gate 		goto error;
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	/* gr_gid field */
5117c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
5127c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
513*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
5147c478bd9Sstevel@tonic-gate 		goto error;
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
5177c478bd9Sstevel@tonic-gate 	if (*tb) {
518*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 	grgid = (gid_t)t;
521*9525b14bSRao Shoaib 	if ((long) grgid != t) {	/*%< value must have been too big. */
5227c478bd9Sstevel@tonic-gate 		goto error;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	/* gr_mem field. Member names are separated by commas */
5277c478bd9Sstevel@tonic-gate 	q = strchr(p, fieldsep);
5287c478bd9Sstevel@tonic-gate 	if (q == NULL) {
5297c478bd9Sstevel@tonic-gate 		goto error;
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 	members = splitarray(p, q, COMMA);
5327c478bd9Sstevel@tonic-gate 	if (members == NULL) {
5337c478bd9Sstevel@tonic-gate 		myerrno = errno;
5347c478bd9Sstevel@tonic-gate 		goto error;
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 	p = q + 1;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	gr->gr_name = name;
5407c478bd9Sstevel@tonic-gate #ifndef MISSING_GR_PASSWD
5417c478bd9Sstevel@tonic-gate 	gr->gr_passwd = pass;
5427c478bd9Sstevel@tonic-gate #endif
5437c478bd9Sstevel@tonic-gate 	gr->gr_gid = grgid;
5447c478bd9Sstevel@tonic-gate 	gr->gr_mem = members;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	return (0);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate  error:
5497c478bd9Sstevel@tonic-gate 	errno = myerrno;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	if (name != NULL) free(name);
5527c478bd9Sstevel@tonic-gate 	if (pass != NULL) free(pass);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	return (-1);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate /* ------------------------- struct group ------------------------- */
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct servent +++++++++++++++++++++++++ */
5647c478bd9Sstevel@tonic-gate 
565*9525b14bSRao Shoaib /*%
5667c478bd9Sstevel@tonic-gate  * int irp_marshall_sv(const struct servent *sv, char **buffer, size_t *len)
5677c478bd9Sstevel@tonic-gate  *
568*9525b14bSRao Shoaib  * notes: \li
5697c478bd9Sstevel@tonic-gate  *
570*9525b14bSRao Shoaib  *	See irpmarshall.h
5717c478bd9Sstevel@tonic-gate  *
572*9525b14bSRao Shoaib  * return: \li
5737c478bd9Sstevel@tonic-gate  *
5747c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure.
5757c478bd9Sstevel@tonic-gate  *
5767c478bd9Sstevel@tonic-gate  */
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate int
5797c478bd9Sstevel@tonic-gate irp_marshall_sv(const struct servent *sv, char **buffer, size_t *len) {
580*9525b14bSRao Shoaib 	size_t need = 1;	/*%< for null byte */
5817c478bd9Sstevel@tonic-gate 	char svPort[24];
5827c478bd9Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
5837c478bd9Sstevel@tonic-gate 	short realport;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (sv == NULL || len == NULL) {
5867c478bd9Sstevel@tonic-gate 		errno = EINVAL;
5877c478bd9Sstevel@tonic-gate 		return (-1);
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	/* the int s_port field is actually a short in network order. We
5917c478bd9Sstevel@tonic-gate 	   want host order to make the marshalled data look correct */
5927c478bd9Sstevel@tonic-gate 	realport = ntohs((short)sv->s_port);
5937c478bd9Sstevel@tonic-gate 	sprintf(svPort, "%d", realport);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	need += strlen(sv->s_name) + 1;
5967c478bd9Sstevel@tonic-gate 	need += joinlength(sv->s_aliases) + 1;
5977c478bd9Sstevel@tonic-gate 	need += strlen(svPort) + 1;
5987c478bd9Sstevel@tonic-gate 	need += strlen(sv->s_proto) + 1;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
6017c478bd9Sstevel@tonic-gate 		*len = need;
6027c478bd9Sstevel@tonic-gate 		return (0);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
6067c478bd9Sstevel@tonic-gate 		errno = EINVAL;
6077c478bd9Sstevel@tonic-gate 		return (-1);
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
611*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
6127c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
6137c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
6147c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
6157c478bd9Sstevel@tonic-gate 			return (-1);
6167c478bd9Sstevel@tonic-gate 		}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 		*len = need;
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	strcpy(*buffer, sv->s_name);		strcat(*buffer, fieldsep);
6227c478bd9Sstevel@tonic-gate 	joinarray(sv->s_aliases, *buffer, COMMA); strcat(*buffer, fieldsep);
6237c478bd9Sstevel@tonic-gate 	strcat(*buffer, svPort);		strcat(*buffer, fieldsep);
6247c478bd9Sstevel@tonic-gate 	strcat(*buffer, sv->s_proto);		strcat(*buffer, fieldsep);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	return (0);
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate 
629*9525b14bSRao Shoaib /*%
6307c478bd9Sstevel@tonic-gate  * int irp_unmarshall_sv(struct servent *sv, char *buffer)
6317c478bd9Sstevel@tonic-gate  *
632*9525b14bSRao Shoaib  * notes: \li
6337c478bd9Sstevel@tonic-gate  *
634*9525b14bSRao Shoaib  *	See irpmarshall.h
6357c478bd9Sstevel@tonic-gate  *
636*9525b14bSRao Shoaib  * return: \li
6377c478bd9Sstevel@tonic-gate  *
6387c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure.
6397c478bd9Sstevel@tonic-gate  *
6407c478bd9Sstevel@tonic-gate  */
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate int
6437c478bd9Sstevel@tonic-gate irp_unmarshall_sv(struct servent *sv, char *buffer) {
6447c478bd9Sstevel@tonic-gate 	char *p, *q;
6457c478bd9Sstevel@tonic-gate 	short svport;
6467c478bd9Sstevel@tonic-gate 	long t;
6477c478bd9Sstevel@tonic-gate 	char *name = NULL;
6487c478bd9Sstevel@tonic-gate 	char *proto = NULL;
6497c478bd9Sstevel@tonic-gate 	char **aliases = NULL;
6507c478bd9Sstevel@tonic-gate 	char tmpbuf[24];
6517c478bd9Sstevel@tonic-gate 	char *tb;
6527c478bd9Sstevel@tonic-gate 	char fieldsep = ':';
6537c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	if (sv == NULL || buffer == NULL)
6567c478bd9Sstevel@tonic-gate 		return (-1);
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	p = buffer;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	/* s_name field */
6627c478bd9Sstevel@tonic-gate 	name = NULL;
663*9525b14bSRao Shoaib 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
6647c478bd9Sstevel@tonic-gate 		goto error;
6657c478bd9Sstevel@tonic-gate 	}
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	/* s_aliases field */
6697c478bd9Sstevel@tonic-gate 	q = strchr(p, fieldsep);
6707c478bd9Sstevel@tonic-gate 	if (q == NULL) {
6717c478bd9Sstevel@tonic-gate 		goto error;
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
6747c478bd9Sstevel@tonic-gate 	if (aliases == NULL) {
6757c478bd9Sstevel@tonic-gate 		myerrno = errno;
6767c478bd9Sstevel@tonic-gate 		goto error;
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 	p = q + 1;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	/* s_port field */
6827c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
6837c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
684*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
6857c478bd9Sstevel@tonic-gate 		goto error;
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
6887c478bd9Sstevel@tonic-gate 	if (*tb) {
689*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 	svport = (short)t;
692*9525b14bSRao Shoaib 	if ((long) svport != t) {	/*%< value must have been too big. */
6937c478bd9Sstevel@tonic-gate 		goto error;
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 	svport = htons(svport);
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	/* s_proto field */
6987c478bd9Sstevel@tonic-gate 	proto = NULL;
6997c478bd9Sstevel@tonic-gate 	if (getfield(&proto, 0, &p, fieldsep) == NULL) {
7007c478bd9Sstevel@tonic-gate 		goto error;
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	sv->s_name = name;
7047c478bd9Sstevel@tonic-gate 	sv->s_aliases = aliases;
7057c478bd9Sstevel@tonic-gate 	sv->s_port = svport;
7067c478bd9Sstevel@tonic-gate 	sv->s_proto = proto;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	return (0);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate  error:
7117c478bd9Sstevel@tonic-gate 	errno = myerrno;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	if (name != NULL) free(name);
7147c478bd9Sstevel@tonic-gate 	if (proto != NULL) free(proto);
7157c478bd9Sstevel@tonic-gate 	free_array(aliases, 0);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	return (-1);
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate /* ------------------------- struct servent ------------------------- */
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct protoent +++++++++++++++++++++++++ */
7247c478bd9Sstevel@tonic-gate 
725*9525b14bSRao Shoaib /*%
7267c478bd9Sstevel@tonic-gate  * int irp_marshall_pr(struct protoent *pr, char **buffer, size_t *len)
7277c478bd9Sstevel@tonic-gate  *
728*9525b14bSRao Shoaib  * notes: \li
7297c478bd9Sstevel@tonic-gate  *
730*9525b14bSRao Shoaib  *	See irpmarshall.h
7317c478bd9Sstevel@tonic-gate  *
732*9525b14bSRao Shoaib  * return: \li
7337c478bd9Sstevel@tonic-gate  *
7347c478bd9Sstevel@tonic-gate  *	0 on success and -1 on failure.
7357c478bd9Sstevel@tonic-gate  *
7367c478bd9Sstevel@tonic-gate  */
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate int
7397c478bd9Sstevel@tonic-gate irp_marshall_pr(struct protoent *pr, char **buffer, size_t *len) {
740*9525b14bSRao Shoaib 	size_t need = 1;	/*%< for null byte */
7417c478bd9Sstevel@tonic-gate 	char prProto[24];
7427c478bd9Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	if (pr == NULL || len == NULL) {
7457c478bd9Sstevel@tonic-gate 		errno = EINVAL;
7467c478bd9Sstevel@tonic-gate 		return (-1);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	sprintf(prProto, "%d", (int)pr->p_proto);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	need += strlen(pr->p_name) + 1;
7527c478bd9Sstevel@tonic-gate 	need += joinlength(pr->p_aliases) + 1;
7537c478bd9Sstevel@tonic-gate 	need += strlen(prProto) + 1;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
7567c478bd9Sstevel@tonic-gate 		*len = need;
7577c478bd9Sstevel@tonic-gate 		return (0);
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
7617c478bd9Sstevel@tonic-gate 		errno = EINVAL;
7627c478bd9Sstevel@tonic-gate 		return (-1);
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
766*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
7677c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
7687c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
7697c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
7707c478bd9Sstevel@tonic-gate 			return (-1);
7717c478bd9Sstevel@tonic-gate 		}
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 		*len = need;
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	strcpy(*buffer, pr->p_name);		strcat(*buffer, fieldsep);
7777c478bd9Sstevel@tonic-gate 	joinarray(pr->p_aliases, *buffer, COMMA); strcat(*buffer, fieldsep);
7787c478bd9Sstevel@tonic-gate 	strcat(*buffer, prProto);		strcat(*buffer, fieldsep);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	return (0);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate 
784*9525b14bSRao Shoaib /*%
7857c478bd9Sstevel@tonic-gate  * int irp_unmarshall_pr(struct protoent *pr, char *buffer)
7867c478bd9Sstevel@tonic-gate  *
787*9525b14bSRao Shoaib  * notes: \li
7887c478bd9Sstevel@tonic-gate  *
789*9525b14bSRao Shoaib  *	See irpmarshall.h
7907c478bd9Sstevel@tonic-gate  *
791*9525b14bSRao Shoaib  * return: \li
7927c478bd9Sstevel@tonic-gate  *
7937c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure
7947c478bd9Sstevel@tonic-gate  *
7957c478bd9Sstevel@tonic-gate  */
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate int irp_unmarshall_pr(struct protoent *pr, char *buffer) {
7987c478bd9Sstevel@tonic-gate 	char *p, *q;
7997c478bd9Sstevel@tonic-gate 	int prproto;
8007c478bd9Sstevel@tonic-gate 	long t;
8017c478bd9Sstevel@tonic-gate 	char *name = NULL;
8027c478bd9Sstevel@tonic-gate 	char **aliases = NULL;
8037c478bd9Sstevel@tonic-gate 	char tmpbuf[24];
8047c478bd9Sstevel@tonic-gate 	char *tb;
8057c478bd9Sstevel@tonic-gate 	char fieldsep = ':';
8067c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	if (pr == NULL || buffer == NULL) {
8097c478bd9Sstevel@tonic-gate 		errno = EINVAL;
8107c478bd9Sstevel@tonic-gate 		return (-1);
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	p = buffer;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	/* p_name field */
8167c478bd9Sstevel@tonic-gate 	name = NULL;
817*9525b14bSRao Shoaib 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
8187c478bd9Sstevel@tonic-gate 		goto error;
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	/* p_aliases field */
8237c478bd9Sstevel@tonic-gate 	q = strchr(p, fieldsep);
8247c478bd9Sstevel@tonic-gate 	if (q == NULL) {
8257c478bd9Sstevel@tonic-gate 		goto error;
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
8287c478bd9Sstevel@tonic-gate 	if (aliases == NULL) {
8297c478bd9Sstevel@tonic-gate 		myerrno = errno;
8307c478bd9Sstevel@tonic-gate 		goto error;
8317c478bd9Sstevel@tonic-gate 	}
8327c478bd9Sstevel@tonic-gate 	p = q + 1;
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	/* p_proto field */
8367c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
8377c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
838*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
8397c478bd9Sstevel@tonic-gate 		goto error;
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
8427c478bd9Sstevel@tonic-gate 	if (*tb) {
843*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate 	prproto = (int)t;
846*9525b14bSRao Shoaib 	if ((long) prproto != t) {	/*%< value must have been too big. */
8477c478bd9Sstevel@tonic-gate 		goto error;
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	pr->p_name = name;
8517c478bd9Sstevel@tonic-gate 	pr->p_aliases = aliases;
8527c478bd9Sstevel@tonic-gate 	pr->p_proto = prproto;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	return (0);
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate  error:
8577c478bd9Sstevel@tonic-gate 	errno = myerrno;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	if (name != NULL) free(name);
8607c478bd9Sstevel@tonic-gate 	free_array(aliases, 0);
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	return (-1);
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate /* ------------------------- struct protoent ------------------------- */
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct hostent +++++++++++++++++++++++++ */
8707c478bd9Sstevel@tonic-gate 
871*9525b14bSRao Shoaib /*%
8727c478bd9Sstevel@tonic-gate  * int irp_marshall_ho(struct hostent *ho, char **buffer, size_t *len)
8737c478bd9Sstevel@tonic-gate  *
874*9525b14bSRao Shoaib  * notes: \li
8757c478bd9Sstevel@tonic-gate  *
876*9525b14bSRao Shoaib  *	See irpmarshall.h.
8777c478bd9Sstevel@tonic-gate  *
878*9525b14bSRao Shoaib  * return: \li
8797c478bd9Sstevel@tonic-gate  *
8807c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure.
8817c478bd9Sstevel@tonic-gate  *
8827c478bd9Sstevel@tonic-gate  */
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate int
8857c478bd9Sstevel@tonic-gate irp_marshall_ho(struct hostent *ho, char **buffer, size_t *len) {
886*9525b14bSRao Shoaib 	size_t need = 1;	/*%< for null byte */
8877c478bd9Sstevel@tonic-gate 	char hoaddrtype[24];
8887c478bd9Sstevel@tonic-gate 	char holength[24];
8897c478bd9Sstevel@tonic-gate 	char **av;
8907c478bd9Sstevel@tonic-gate 	char *p;
8917c478bd9Sstevel@tonic-gate 	int addrlen;
8927c478bd9Sstevel@tonic-gate 	int malloced = 0;
8937c478bd9Sstevel@tonic-gate 	size_t remlen;
8947c478bd9Sstevel@tonic-gate 	const char *fieldsep = "@";
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	if (ho == NULL || len == NULL) {
8977c478bd9Sstevel@tonic-gate 		errno = EINVAL;
8987c478bd9Sstevel@tonic-gate 		return (-1);
8997c478bd9Sstevel@tonic-gate 	}
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	switch(ho->h_addrtype) {
9027c478bd9Sstevel@tonic-gate 	case AF_INET:
9037c478bd9Sstevel@tonic-gate 		strcpy(hoaddrtype, "AF_INET");
9047c478bd9Sstevel@tonic-gate 		break;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	case AF_INET6:
9077c478bd9Sstevel@tonic-gate 		strcpy(hoaddrtype, "AF_INET6");
9087c478bd9Sstevel@tonic-gate 		break;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	default:
9117c478bd9Sstevel@tonic-gate 		errno = EINVAL;
9127c478bd9Sstevel@tonic-gate 		return (-1);
9137c478bd9Sstevel@tonic-gate 	}
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	sprintf(holength, "%d", ho->h_length);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	need += strlen(ho->h_name) + 1;
9187c478bd9Sstevel@tonic-gate 	need += joinlength(ho->h_aliases) + 1;
9197c478bd9Sstevel@tonic-gate 	need += strlen(hoaddrtype) + 1;
9207c478bd9Sstevel@tonic-gate 	need += strlen(holength) + 1;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	/* we determine an upper bound on the string length needed, not an
9237c478bd9Sstevel@tonic-gate 	   exact length. */
924*9525b14bSRao Shoaib 	addrlen = (ho->h_addrtype == AF_INET ? 16 : 46) ; /*%< XX other AF's?? */
9257c478bd9Sstevel@tonic-gate 	for (av = ho->h_addr_list; av != NULL && *av != NULL ; av++)
9267c478bd9Sstevel@tonic-gate 		need += addrlen;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
9297c478bd9Sstevel@tonic-gate 		*len = need;
9307c478bd9Sstevel@tonic-gate 		return (0);
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
9347c478bd9Sstevel@tonic-gate 		errno = EINVAL;
9357c478bd9Sstevel@tonic-gate 		return (-1);
9367c478bd9Sstevel@tonic-gate 	}
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
939*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
9407c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
9417c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
9427c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
9437c478bd9Sstevel@tonic-gate 			return (-1);
9447c478bd9Sstevel@tonic-gate 		}
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 		*len = need;
9477c478bd9Sstevel@tonic-gate 		malloced = 1;
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	strcpy(*buffer, ho->h_name);		strcat(*buffer, fieldsep);
9517c478bd9Sstevel@tonic-gate 	joinarray(ho->h_aliases, *buffer, COMMA); strcat(*buffer, fieldsep);
9527c478bd9Sstevel@tonic-gate 	strcat(*buffer, hoaddrtype);		strcat(*buffer, fieldsep);
9537c478bd9Sstevel@tonic-gate 	strcat(*buffer, holength);		strcat(*buffer, fieldsep);
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	p = *buffer + strlen(*buffer);
9567c478bd9Sstevel@tonic-gate 	remlen = need - strlen(*buffer);
9577c478bd9Sstevel@tonic-gate 	for (av = ho->h_addr_list ; av != NULL && *av != NULL ; av++) {
9587c478bd9Sstevel@tonic-gate 		if (inet_ntop(ho->h_addrtype, *av, p, remlen) == NULL) {
9597c478bd9Sstevel@tonic-gate 			goto error;
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 		if (*(av + 1) != NULL)
9627c478bd9Sstevel@tonic-gate 			strcat(p, COMMASTR);
9637c478bd9Sstevel@tonic-gate 		remlen -= strlen(p);
9647c478bd9Sstevel@tonic-gate 		p += strlen(p);
9657c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	return (0);
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate  error:
9717c478bd9Sstevel@tonic-gate 	if (malloced) {
9727c478bd9Sstevel@tonic-gate 		memput(*buffer, need);
9737c478bd9Sstevel@tonic-gate 	}
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	return (-1);
9767c478bd9Sstevel@tonic-gate }
9777c478bd9Sstevel@tonic-gate 
978*9525b14bSRao Shoaib /*%
9797c478bd9Sstevel@tonic-gate  * int irp_unmarshall_ho(struct hostent *ho, char *buffer)
9807c478bd9Sstevel@tonic-gate  *
981*9525b14bSRao Shoaib  * notes: \li
9827c478bd9Sstevel@tonic-gate  *
983*9525b14bSRao Shoaib  *	See irpmarshall.h.
9847c478bd9Sstevel@tonic-gate  *
985*9525b14bSRao Shoaib  * return: \li
9867c478bd9Sstevel@tonic-gate  *
9877c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure.
9887c478bd9Sstevel@tonic-gate  *
9897c478bd9Sstevel@tonic-gate  */
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate int
9927c478bd9Sstevel@tonic-gate irp_unmarshall_ho(struct hostent *ho, char *buffer) {
9937c478bd9Sstevel@tonic-gate 	char *p, *q, *r;
9947c478bd9Sstevel@tonic-gate 	int hoaddrtype;
9957c478bd9Sstevel@tonic-gate 	int holength;
9967c478bd9Sstevel@tonic-gate 	long t;
997*9525b14bSRao Shoaib 	char *name;
9987c478bd9Sstevel@tonic-gate 	char **aliases = NULL;
9997c478bd9Sstevel@tonic-gate 	char **hohaddrlist = NULL;
10007c478bd9Sstevel@tonic-gate 	size_t hoaddrsize;
10017c478bd9Sstevel@tonic-gate 	char tmpbuf[24];
10027c478bd9Sstevel@tonic-gate 	char *tb;
10037c478bd9Sstevel@tonic-gate 	char **alist;
10047c478bd9Sstevel@tonic-gate 	int addrcount;
10057c478bd9Sstevel@tonic-gate 	char fieldsep = '@';
10067c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	if (ho == NULL || buffer == NULL) {
10097c478bd9Sstevel@tonic-gate 		errno = EINVAL;
10107c478bd9Sstevel@tonic-gate 		return (-1);
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	p = buffer;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	/* h_name field */
10167c478bd9Sstevel@tonic-gate 	name = NULL;
1017*9525b14bSRao Shoaib 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
10187c478bd9Sstevel@tonic-gate 		goto error;
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	/* h_aliases field */
10237c478bd9Sstevel@tonic-gate 	q = strchr(p, fieldsep);
10247c478bd9Sstevel@tonic-gate 	if (q == NULL) {
10257c478bd9Sstevel@tonic-gate 		goto error;
10267c478bd9Sstevel@tonic-gate 	}
10277c478bd9Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
10287c478bd9Sstevel@tonic-gate 	if (aliases == NULL) {
10297c478bd9Sstevel@tonic-gate 		myerrno = errno;
10307c478bd9Sstevel@tonic-gate 		goto error;
10317c478bd9Sstevel@tonic-gate 	}
10327c478bd9Sstevel@tonic-gate 	p = q + 1;
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	/* h_addrtype field */
10367c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
10377c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1038*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
10397c478bd9Sstevel@tonic-gate 		goto error;
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 	if (strcmp(tmpbuf, "AF_INET") == 0)
10427c478bd9Sstevel@tonic-gate 		hoaddrtype = AF_INET;
10437c478bd9Sstevel@tonic-gate 	else if (strcmp(tmpbuf, "AF_INET6") == 0)
10447c478bd9Sstevel@tonic-gate 		hoaddrtype = AF_INET6;
10457c478bd9Sstevel@tonic-gate 	else
10467c478bd9Sstevel@tonic-gate 		goto error;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	/* h_length field */
10507c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
10517c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1052*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
10537c478bd9Sstevel@tonic-gate 		goto error;
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
10567c478bd9Sstevel@tonic-gate 	if (*tb) {
1057*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 	holength = (int)t;
1060*9525b14bSRao Shoaib 	if ((long) holength != t) {	/*%< value must have been too big. */
10617c478bd9Sstevel@tonic-gate 		goto error;
10627c478bd9Sstevel@tonic-gate 	}
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	/* h_addr_list field */
10667c478bd9Sstevel@tonic-gate 	q = strchr(p, fieldsep);
10677c478bd9Sstevel@tonic-gate 	if (q == NULL)
10687c478bd9Sstevel@tonic-gate 		goto error;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	/* count how many addresss are in there */
10717c478bd9Sstevel@tonic-gate 	if (q > p + 1) {
10727c478bd9Sstevel@tonic-gate 		for (addrcount = 1, r = p ; r != q ; r++) {
10737c478bd9Sstevel@tonic-gate 			if (*r == COMMA)
10747c478bd9Sstevel@tonic-gate 				addrcount++;
10757c478bd9Sstevel@tonic-gate 		}
10767c478bd9Sstevel@tonic-gate 	} else {
10777c478bd9Sstevel@tonic-gate 		addrcount = 0;
10787c478bd9Sstevel@tonic-gate 	}
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	hoaddrsize = (addrcount + 1) * sizeof (char *);
10817c478bd9Sstevel@tonic-gate 	hohaddrlist = malloc(hoaddrsize);
10827c478bd9Sstevel@tonic-gate 	if (hohaddrlist == NULL) {
10837c478bd9Sstevel@tonic-gate 		myerrno = ENOMEM;
10847c478bd9Sstevel@tonic-gate 		goto error;
10857c478bd9Sstevel@tonic-gate 	}
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	memset(hohaddrlist, 0x0, hoaddrsize);
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	alist = hohaddrlist;
10907c478bd9Sstevel@tonic-gate 	for (t = 0, r = p ; r != q ; p = r + 1, t++) {
10917c478bd9Sstevel@tonic-gate 		char saved;
10927c478bd9Sstevel@tonic-gate 		while (r != q && *r != COMMA) r++;
10937c478bd9Sstevel@tonic-gate 		saved = *r;
10947c478bd9Sstevel@tonic-gate 		*r = 0x0;
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 		alist[t] = malloc(hoaddrtype == AF_INET ? 4 : 16);
10977c478bd9Sstevel@tonic-gate 		if (alist[t] == NULL) {
10987c478bd9Sstevel@tonic-gate 			myerrno = ENOMEM;
10997c478bd9Sstevel@tonic-gate 			goto error;
11007c478bd9Sstevel@tonic-gate 		}
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 		if (inet_pton(hoaddrtype, p, alist[t]) == -1)
11037c478bd9Sstevel@tonic-gate 			goto error;
11047c478bd9Sstevel@tonic-gate 		*r = saved;
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 	alist[t] = NULL;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	ho->h_name = name;
11097c478bd9Sstevel@tonic-gate 	ho->h_aliases = aliases;
11107c478bd9Sstevel@tonic-gate 	ho->h_addrtype = hoaddrtype;
11117c478bd9Sstevel@tonic-gate 	ho->h_length = holength;
11127c478bd9Sstevel@tonic-gate 	ho->h_addr_list = hohaddrlist;
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	return (0);
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate  error:
11177c478bd9Sstevel@tonic-gate 	errno = myerrno;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	if (name != NULL) free(name);
1120*9525b14bSRao Shoaib 	free_array(hohaddrlist, 0);
11217c478bd9Sstevel@tonic-gate 	free_array(aliases, 0);
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	return (-1);
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate /* ------------------------- struct hostent------------------------- */
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct netgrp +++++++++++++++++++++++++ */
11317c478bd9Sstevel@tonic-gate 
1132*9525b14bSRao Shoaib /*%
11337c478bd9Sstevel@tonic-gate  * int irp_marshall_ng(const char *host, const char *user,
11347c478bd9Sstevel@tonic-gate  *		       const char *domain, char *buffer, size_t *len)
11357c478bd9Sstevel@tonic-gate  *
1136*9525b14bSRao Shoaib  * notes: \li
11377c478bd9Sstevel@tonic-gate  *
11387c478bd9Sstevel@tonic-gate  *	See note for irp_marshall_ng_start
11397c478bd9Sstevel@tonic-gate  *
1140*9525b14bSRao Shoaib  * return: \li
11417c478bd9Sstevel@tonic-gate  *
11427c478bd9Sstevel@tonic-gate  *	0 on success, 0 on failure.
11437c478bd9Sstevel@tonic-gate  *
11447c478bd9Sstevel@tonic-gate  */
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate int
11477c478bd9Sstevel@tonic-gate irp_marshall_ng(const char *host, const char *user, const char *domain,
11487c478bd9Sstevel@tonic-gate 		char **buffer, size_t *len) {
1149*9525b14bSRao Shoaib 	size_t need = 1; /*%< for nul byte */
11507c478bd9Sstevel@tonic-gate 	const char *fieldsep = ",";
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	if (len == NULL) {
11537c478bd9Sstevel@tonic-gate 		errno = EINVAL;
11547c478bd9Sstevel@tonic-gate 		return (-1);
11557c478bd9Sstevel@tonic-gate 	}
11567c478bd9Sstevel@tonic-gate 
1157*9525b14bSRao Shoaib 	need += 4;		       /*%< two parens and two commas */
11587c478bd9Sstevel@tonic-gate 	need += (host == NULL ? 0 : strlen(host));
11597c478bd9Sstevel@tonic-gate 	need += (user == NULL ? 0 : strlen(user));
11607c478bd9Sstevel@tonic-gate 	need += (domain == NULL ? 0 : strlen(domain));
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
11637c478bd9Sstevel@tonic-gate 		*len = need;
11647c478bd9Sstevel@tonic-gate 		return (0);
11657c478bd9Sstevel@tonic-gate 	} else if (*buffer != NULL && need > *len) {
11667c478bd9Sstevel@tonic-gate 		errno = EINVAL;
11677c478bd9Sstevel@tonic-gate 		return (-1);
11687c478bd9Sstevel@tonic-gate 	}
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
1171*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
11727c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
11737c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
11747c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
11757c478bd9Sstevel@tonic-gate 			return (-1);
11767c478bd9Sstevel@tonic-gate 		}
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 		*len = need;
11797c478bd9Sstevel@tonic-gate 	}
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	(*buffer)[0] = '(';
11827c478bd9Sstevel@tonic-gate 	(*buffer)[1] = '\0';
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	if (host != NULL)
11857c478bd9Sstevel@tonic-gate 		strcat(*buffer, host);
11867c478bd9Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	if (user != NULL)
11897c478bd9Sstevel@tonic-gate 		strcat(*buffer, user);
11907c478bd9Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	if (domain != NULL)
11937c478bd9Sstevel@tonic-gate 		strcat(*buffer, domain);
11947c478bd9Sstevel@tonic-gate 	strcat(*buffer, ")");
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	return (0);
11977c478bd9Sstevel@tonic-gate }
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate /* ---------- */
12027c478bd9Sstevel@tonic-gate 
1203*9525b14bSRao Shoaib /*%
12047c478bd9Sstevel@tonic-gate  * int irp_unmarshall_ng(const char **host, const char **user,
12057c478bd9Sstevel@tonic-gate  *			 const char **domain, char *buffer)
12067c478bd9Sstevel@tonic-gate  *
1207*9525b14bSRao Shoaib  * notes: \li
12087c478bd9Sstevel@tonic-gate  *
12097c478bd9Sstevel@tonic-gate  *	Unpacks the BUFFER into 3 character arrays it allocates and assigns
12107c478bd9Sstevel@tonic-gate  *	to *HOST, *USER and *DOMAIN. If any field of the value is empty,
12117c478bd9Sstevel@tonic-gate  *	then the corresponding paramater value will be set to NULL.
12127c478bd9Sstevel@tonic-gate  *
1213*9525b14bSRao Shoaib  * return: \li
12147c478bd9Sstevel@tonic-gate  *
12157c478bd9Sstevel@tonic-gate  *	0 on success and -1 on failure.
12167c478bd9Sstevel@tonic-gate  */
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate int
12197c478bd9Sstevel@tonic-gate irp_unmarshall_ng(const char **hostp, const char **userp, const char **domainp,
12207c478bd9Sstevel@tonic-gate 		  char *buffer)
12217c478bd9Sstevel@tonic-gate {
12227c478bd9Sstevel@tonic-gate 	char *p, *q;
12237c478bd9Sstevel@tonic-gate 	char fieldsep = ',';
12247c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
12257c478bd9Sstevel@tonic-gate 	char *host, *user, *domain;
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	if (userp == NULL || hostp == NULL ||
12287c478bd9Sstevel@tonic-gate 	    domainp == NULL || buffer == NULL) {
12297c478bd9Sstevel@tonic-gate 		errno = EINVAL;
12307c478bd9Sstevel@tonic-gate 		return (-1);
12317c478bd9Sstevel@tonic-gate 	}
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	host = user = domain = NULL;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	p = buffer;
12367c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*p)) {
12377c478bd9Sstevel@tonic-gate 		p++;
12387c478bd9Sstevel@tonic-gate 	}
12397c478bd9Sstevel@tonic-gate 	if (*p != '(') {
12407c478bd9Sstevel@tonic-gate 		goto error;
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	q = p + 1;
12447c478bd9Sstevel@tonic-gate 	while (*q && *q != fieldsep)
12457c478bd9Sstevel@tonic-gate 		q++;
12467c478bd9Sstevel@tonic-gate 	if (!*q) {
12477c478bd9Sstevel@tonic-gate 		goto error;
12487c478bd9Sstevel@tonic-gate 	} else if (q > p + 1) {
12497c478bd9Sstevel@tonic-gate 		host = strndup(p, q - p);
12507c478bd9Sstevel@tonic-gate 	}
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	p = q + 1;
12537c478bd9Sstevel@tonic-gate 	if (!*p) {
12547c478bd9Sstevel@tonic-gate 		goto error;
12557c478bd9Sstevel@tonic-gate 	} else if (*p != fieldsep) {
12567c478bd9Sstevel@tonic-gate 		q = p + 1;
12577c478bd9Sstevel@tonic-gate 		while (*q && *q != fieldsep)
12587c478bd9Sstevel@tonic-gate 			q++;
12597c478bd9Sstevel@tonic-gate 		if (!*q) {
12607c478bd9Sstevel@tonic-gate 			goto error;
12617c478bd9Sstevel@tonic-gate 		}
12627c478bd9Sstevel@tonic-gate 		user = strndup(p, q - p);
12637c478bd9Sstevel@tonic-gate 	} else {
12647c478bd9Sstevel@tonic-gate 		p++;
12657c478bd9Sstevel@tonic-gate 	}
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	if (!*p) {
12687c478bd9Sstevel@tonic-gate 		goto error;
12697c478bd9Sstevel@tonic-gate 	} else if (*p != ')') {
12707c478bd9Sstevel@tonic-gate 		q = p + 1;
12717c478bd9Sstevel@tonic-gate 		while (*q && *q != ')')
12727c478bd9Sstevel@tonic-gate 			q++;
12737c478bd9Sstevel@tonic-gate 		if (!*q) {
12747c478bd9Sstevel@tonic-gate 			goto error;
12757c478bd9Sstevel@tonic-gate 		}
12767c478bd9Sstevel@tonic-gate 		domain = strndup(p, q - p);
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 	*hostp = host;
12797c478bd9Sstevel@tonic-gate 	*userp = user;
12807c478bd9Sstevel@tonic-gate 	*domainp = domain;
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	return (0);
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate  error:
12857c478bd9Sstevel@tonic-gate 	errno = myerrno;
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	if (host != NULL) free(host);
12887c478bd9Sstevel@tonic-gate 	if (user != NULL) free(user);
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	return (-1);
12917c478bd9Sstevel@tonic-gate }
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate /* ------------------------- struct netgrp ------------------------- */
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct nwent +++++++++++++++++++++++++ */
12997c478bd9Sstevel@tonic-gate 
1300*9525b14bSRao Shoaib /*%
13017c478bd9Sstevel@tonic-gate  * int irp_marshall_nw(struct nwent *ne, char **buffer, size_t *len)
13027c478bd9Sstevel@tonic-gate  *
1303*9525b14bSRao Shoaib  * notes: \li
13047c478bd9Sstevel@tonic-gate  *
13057c478bd9Sstevel@tonic-gate  *	See at top.
13067c478bd9Sstevel@tonic-gate  *
1307*9525b14bSRao Shoaib  * return: \li
13087c478bd9Sstevel@tonic-gate  *
13097c478bd9Sstevel@tonic-gate  *	0 on success and -1 on failure.
13107c478bd9Sstevel@tonic-gate  *
13117c478bd9Sstevel@tonic-gate  */
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate int
13147c478bd9Sstevel@tonic-gate irp_marshall_nw(struct nwent *ne, char **buffer, size_t *len) {
1315*9525b14bSRao Shoaib 	size_t need = 1;	/*%< for null byte */
13167c478bd9Sstevel@tonic-gate 	char nAddrType[24];
13177c478bd9Sstevel@tonic-gate 	char nNet[MAXPADDRSIZE];
13187c478bd9Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	if (ne == NULL || len == NULL) {
13217c478bd9Sstevel@tonic-gate 		return (-1);
13227c478bd9Sstevel@tonic-gate 	}
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 	strcpy(nAddrType, ADDR_T_STR(ne->n_addrtype));
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	if (inet_net_ntop(ne->n_addrtype, ne->n_addr, ne->n_length,
13277c478bd9Sstevel@tonic-gate 			  nNet, sizeof nNet) == NULL) {
13287c478bd9Sstevel@tonic-gate 		return (-1);
13297c478bd9Sstevel@tonic-gate 	}
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	need += strlen(ne->n_name) + 1;
13337c478bd9Sstevel@tonic-gate 	need += joinlength(ne->n_aliases) + 1;
13347c478bd9Sstevel@tonic-gate 	need += strlen(nAddrType) + 1;
13357c478bd9Sstevel@tonic-gate 	need += strlen(nNet) + 1;
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
13387c478bd9Sstevel@tonic-gate 		*len = need;
13397c478bd9Sstevel@tonic-gate 		return (0);
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
13437c478bd9Sstevel@tonic-gate 		errno = EINVAL;
13447c478bd9Sstevel@tonic-gate 		return (-1);
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
1348*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
13497c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
13507c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
13517c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
13527c478bd9Sstevel@tonic-gate 			return (-1);
13537c478bd9Sstevel@tonic-gate 		}
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 		*len = need;
13567c478bd9Sstevel@tonic-gate 	}
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	strcpy(*buffer, ne->n_name);		strcat(*buffer, fieldsep);
13597c478bd9Sstevel@tonic-gate 	joinarray(ne->n_aliases, *buffer, COMMA) ; strcat(*buffer, fieldsep);
13607c478bd9Sstevel@tonic-gate 	strcat(*buffer, nAddrType);		strcat(*buffer, fieldsep);
13617c478bd9Sstevel@tonic-gate 	strcat(*buffer, nNet);			strcat(*buffer, fieldsep);
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	return (0);
13647c478bd9Sstevel@tonic-gate }
13657c478bd9Sstevel@tonic-gate 
1366*9525b14bSRao Shoaib /*%
13677c478bd9Sstevel@tonic-gate  * int irp_unmarshall_nw(struct nwent *ne, char *buffer)
13687c478bd9Sstevel@tonic-gate  *
1369*9525b14bSRao Shoaib  * notes: \li
13707c478bd9Sstevel@tonic-gate  *
13717c478bd9Sstevel@tonic-gate  *	See note up top.
13727c478bd9Sstevel@tonic-gate  *
1373*9525b14bSRao Shoaib  * return: \li
13747c478bd9Sstevel@tonic-gate  *
13757c478bd9Sstevel@tonic-gate  *	0 on success and -1 on failure.
13767c478bd9Sstevel@tonic-gate  *
13777c478bd9Sstevel@tonic-gate  */
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate int
13807c478bd9Sstevel@tonic-gate irp_unmarshall_nw(struct nwent *ne, char *buffer) {
13817c478bd9Sstevel@tonic-gate 	char *p, *q;
13827c478bd9Sstevel@tonic-gate 	int naddrtype;
13837c478bd9Sstevel@tonic-gate 	long nnet;
13847c478bd9Sstevel@tonic-gate 	int bits;
13857c478bd9Sstevel@tonic-gate 	char *name = NULL;
13867c478bd9Sstevel@tonic-gate 	char **aliases = NULL;
13877c478bd9Sstevel@tonic-gate 	char tmpbuf[24];
13887c478bd9Sstevel@tonic-gate 	char *tb;
13897c478bd9Sstevel@tonic-gate 	char fieldsep = ':';
13907c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	if (ne == NULL || buffer == NULL) {
13937c478bd9Sstevel@tonic-gate 		goto error;
13947c478bd9Sstevel@tonic-gate 	}
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	p = buffer;
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	/* n_name field */
13997c478bd9Sstevel@tonic-gate 	name = NULL;
1400*9525b14bSRao Shoaib 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
14017c478bd9Sstevel@tonic-gate 		goto error;
14027c478bd9Sstevel@tonic-gate 	}
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	/* n_aliases field. Aliases are separated by commas */
14067c478bd9Sstevel@tonic-gate 	q = strchr(p, fieldsep);
14077c478bd9Sstevel@tonic-gate 	if (q == NULL) {
14087c478bd9Sstevel@tonic-gate 		goto error;
14097c478bd9Sstevel@tonic-gate 	}
14107c478bd9Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
14117c478bd9Sstevel@tonic-gate 	if (aliases == NULL) {
14127c478bd9Sstevel@tonic-gate 		myerrno = errno;
14137c478bd9Sstevel@tonic-gate 		goto error;
14147c478bd9Sstevel@tonic-gate 	}
14157c478bd9Sstevel@tonic-gate 	p = q + 1;
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	/* h_addrtype field */
14197c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
14207c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1421*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
14227c478bd9Sstevel@tonic-gate 		goto error;
14237c478bd9Sstevel@tonic-gate 	}
14247c478bd9Sstevel@tonic-gate 	if (strcmp(tmpbuf, "AF_INET") == 0)
14257c478bd9Sstevel@tonic-gate 		naddrtype = AF_INET;
14267c478bd9Sstevel@tonic-gate 	else if (strcmp(tmpbuf, "AF_INET6") == 0)
14277c478bd9Sstevel@tonic-gate 		naddrtype = AF_INET6;
14287c478bd9Sstevel@tonic-gate 	else
14297c478bd9Sstevel@tonic-gate 		goto error;
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	/* n_net field */
14337c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
14347c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1435*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
14367c478bd9Sstevel@tonic-gate 		goto error;
14377c478bd9Sstevel@tonic-gate 	}
14387c478bd9Sstevel@tonic-gate 	nnet = 0;
14397c478bd9Sstevel@tonic-gate 	bits = inet_net_pton(naddrtype, tmpbuf, &nnet, sizeof nnet);
14407c478bd9Sstevel@tonic-gate 	if (bits < 0) {
14417c478bd9Sstevel@tonic-gate 		goto error;
14427c478bd9Sstevel@tonic-gate 	}
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	/* nnet = ntohl(nnet); */ /* keep in network order for nwent */
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	ne->n_name = name;
14477c478bd9Sstevel@tonic-gate 	ne->n_aliases = aliases;
14487c478bd9Sstevel@tonic-gate 	ne->n_addrtype = naddrtype;
14497c478bd9Sstevel@tonic-gate 	ne->n_length = bits;
14507c478bd9Sstevel@tonic-gate 	ne->n_addr = malloc(sizeof nnet);
14517c478bd9Sstevel@tonic-gate 	if (ne->n_addr == NULL) {
14527c478bd9Sstevel@tonic-gate 		goto error;
14537c478bd9Sstevel@tonic-gate 	}
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	memcpy(ne->n_addr, &nnet, sizeof nnet);
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	return (0);
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate  error:
14607c478bd9Sstevel@tonic-gate 	errno = myerrno;
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	if (name != NULL) free(name);
14637c478bd9Sstevel@tonic-gate 	free_array(aliases, 0);
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	return (-1);
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate /* ------------------------- struct nwent ------------------------- */
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct netent +++++++++++++++++++++++++ */
14737c478bd9Sstevel@tonic-gate 
1474*9525b14bSRao Shoaib /*%
14757c478bd9Sstevel@tonic-gate  * int irp_marshall_ne(struct netent *ne, char **buffer, size_t *len)
14767c478bd9Sstevel@tonic-gate  *
1477*9525b14bSRao Shoaib  * notes: \li
14787c478bd9Sstevel@tonic-gate  *
14797c478bd9Sstevel@tonic-gate  *	See at top.
14807c478bd9Sstevel@tonic-gate  *
1481*9525b14bSRao Shoaib  * return: \li
14827c478bd9Sstevel@tonic-gate  *
14837c478bd9Sstevel@tonic-gate  *	0 on success and -1 on failure.
14847c478bd9Sstevel@tonic-gate  *
14857c478bd9Sstevel@tonic-gate  */
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate int
14887c478bd9Sstevel@tonic-gate irp_marshall_ne(struct netent *ne, char **buffer, size_t *len) {
1489*9525b14bSRao Shoaib 	size_t need = 1;	/*%< for null byte */
14907c478bd9Sstevel@tonic-gate 	char nAddrType[24];
14917c478bd9Sstevel@tonic-gate 	char nNet[MAXPADDRSIZE];
14927c478bd9Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
14937c478bd9Sstevel@tonic-gate 	long nval;
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	if (ne == NULL || len == NULL) {
14967c478bd9Sstevel@tonic-gate 		return (-1);
14977c478bd9Sstevel@tonic-gate 	}
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	strcpy(nAddrType, ADDR_T_STR(ne->n_addrtype));
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	nval = htonl(ne->n_net);
15027c478bd9Sstevel@tonic-gate 	if (inet_ntop(ne->n_addrtype, &nval, nNet, sizeof nNet) == NULL) {
15037c478bd9Sstevel@tonic-gate 		return (-1);
15047c478bd9Sstevel@tonic-gate 	}
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	need += strlen(ne->n_name) + 1;
15077c478bd9Sstevel@tonic-gate 	need += joinlength(ne->n_aliases) + 1;
15087c478bd9Sstevel@tonic-gate 	need += strlen(nAddrType) + 1;
15097c478bd9Sstevel@tonic-gate 	need += strlen(nNet) + 1;
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
15127c478bd9Sstevel@tonic-gate 		*len = need;
15137c478bd9Sstevel@tonic-gate 		return (0);
15147c478bd9Sstevel@tonic-gate 	}
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
15177c478bd9Sstevel@tonic-gate 		errno = EINVAL;
15187c478bd9Sstevel@tonic-gate 		return (-1);
15197c478bd9Sstevel@tonic-gate 	}
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
1522*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
15237c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
15247c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
15257c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
15267c478bd9Sstevel@tonic-gate 			return (-1);
15277c478bd9Sstevel@tonic-gate 		}
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 		*len = need;
15307c478bd9Sstevel@tonic-gate 	}
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	strcpy(*buffer, ne->n_name);		strcat(*buffer, fieldsep);
15337c478bd9Sstevel@tonic-gate 	joinarray(ne->n_aliases, *buffer, COMMA) ; strcat(*buffer, fieldsep);
15347c478bd9Sstevel@tonic-gate 	strcat(*buffer, nAddrType);		strcat(*buffer, fieldsep);
15357c478bd9Sstevel@tonic-gate 	strcat(*buffer, nNet);			strcat(*buffer, fieldsep);
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	return (0);
15387c478bd9Sstevel@tonic-gate }
15397c478bd9Sstevel@tonic-gate 
1540*9525b14bSRao Shoaib /*%
15417c478bd9Sstevel@tonic-gate  * int irp_unmarshall_ne(struct netent *ne, char *buffer)
15427c478bd9Sstevel@tonic-gate  *
1543*9525b14bSRao Shoaib  * notes: \li
15447c478bd9Sstevel@tonic-gate  *
15457c478bd9Sstevel@tonic-gate  *	See note up top.
15467c478bd9Sstevel@tonic-gate  *
1547*9525b14bSRao Shoaib  * return: \li
15487c478bd9Sstevel@tonic-gate  *
15497c478bd9Sstevel@tonic-gate  *	0 on success and -1 on failure.
15507c478bd9Sstevel@tonic-gate  *
15517c478bd9Sstevel@tonic-gate  */
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate int
15547c478bd9Sstevel@tonic-gate irp_unmarshall_ne(struct netent *ne, char *buffer) {
15557c478bd9Sstevel@tonic-gate 	char *p, *q;
15567c478bd9Sstevel@tonic-gate 	int naddrtype;
15577c478bd9Sstevel@tonic-gate 	long nnet;
15587c478bd9Sstevel@tonic-gate 	int bits;
15597c478bd9Sstevel@tonic-gate 	char *name = NULL;
15607c478bd9Sstevel@tonic-gate 	char **aliases = NULL;
15617c478bd9Sstevel@tonic-gate 	char tmpbuf[24];
15627c478bd9Sstevel@tonic-gate 	char *tb;
15637c478bd9Sstevel@tonic-gate 	char fieldsep = ':';
15647c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	if (ne == NULL || buffer == NULL) {
15677c478bd9Sstevel@tonic-gate 		goto error;
15687c478bd9Sstevel@tonic-gate 	}
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 	p = buffer;
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	/* n_name field */
15737c478bd9Sstevel@tonic-gate 	name = NULL;
1574*9525b14bSRao Shoaib 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
15757c478bd9Sstevel@tonic-gate 		goto error;
15767c478bd9Sstevel@tonic-gate 	}
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	/* n_aliases field. Aliases are separated by commas */
15807c478bd9Sstevel@tonic-gate 	q = strchr(p, fieldsep);
15817c478bd9Sstevel@tonic-gate 	if (q == NULL) {
15827c478bd9Sstevel@tonic-gate 		goto error;
15837c478bd9Sstevel@tonic-gate 	}
15847c478bd9Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
15857c478bd9Sstevel@tonic-gate 	if (aliases == NULL) {
15867c478bd9Sstevel@tonic-gate 		myerrno = errno;
15877c478bd9Sstevel@tonic-gate 		goto error;
15887c478bd9Sstevel@tonic-gate 	}
15897c478bd9Sstevel@tonic-gate 	p = q + 1;
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	/* h_addrtype field */
15937c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
15947c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1595*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
15967c478bd9Sstevel@tonic-gate 		goto error;
15977c478bd9Sstevel@tonic-gate 	}
15987c478bd9Sstevel@tonic-gate 	if (strcmp(tmpbuf, "AF_INET") == 0)
15997c478bd9Sstevel@tonic-gate 		naddrtype = AF_INET;
16007c478bd9Sstevel@tonic-gate 	else if (strcmp(tmpbuf, "AF_INET6") == 0)
16017c478bd9Sstevel@tonic-gate 		naddrtype = AF_INET6;
16027c478bd9Sstevel@tonic-gate 	else
16037c478bd9Sstevel@tonic-gate 		goto error;
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	/* n_net field */
16077c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
16087c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1609*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
16107c478bd9Sstevel@tonic-gate 		goto error;
16117c478bd9Sstevel@tonic-gate 	}
16127c478bd9Sstevel@tonic-gate 	bits = inet_net_pton(naddrtype, tmpbuf, &nnet, sizeof nnet);
16137c478bd9Sstevel@tonic-gate 	if (bits < 0) {
16147c478bd9Sstevel@tonic-gate 		goto error;
16157c478bd9Sstevel@tonic-gate 	}
16167c478bd9Sstevel@tonic-gate 	nnet = ntohl(nnet);
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	ne->n_name = name;
16197c478bd9Sstevel@tonic-gate 	ne->n_aliases = aliases;
16207c478bd9Sstevel@tonic-gate 	ne->n_addrtype = naddrtype;
16217c478bd9Sstevel@tonic-gate 	ne->n_net = nnet;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	return (0);
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate  error:
16267c478bd9Sstevel@tonic-gate 	errno = myerrno;
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	if (name != NULL) free(name);
16297c478bd9Sstevel@tonic-gate 	free_array(aliases, 0);
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	return (-1);
16327c478bd9Sstevel@tonic-gate }
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate /* ------------------------- struct netent ------------------------- */
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate /* =========================================================================== */
16397c478bd9Sstevel@tonic-gate 
1640*9525b14bSRao Shoaib /*%
16417c478bd9Sstevel@tonic-gate  * static char ** splitarray(const char *buffer, const char *buffend, char delim)
16427c478bd9Sstevel@tonic-gate  *
1643*9525b14bSRao Shoaib  * notes: \li
16447c478bd9Sstevel@tonic-gate  *
16457c478bd9Sstevel@tonic-gate  *	Split a delim separated astring. Not allowed
16467c478bd9Sstevel@tonic-gate  *	to have two delims next to each other. BUFFER points to begining of
16477c478bd9Sstevel@tonic-gate  *	string, BUFFEND points to one past the end of the string
16487c478bd9Sstevel@tonic-gate  *	(i.e. points at where the null byte would be if null
16497c478bd9Sstevel@tonic-gate  *	terminated).
16507c478bd9Sstevel@tonic-gate  *
1651*9525b14bSRao Shoaib  * return: \li
16527c478bd9Sstevel@tonic-gate  *
16537c478bd9Sstevel@tonic-gate  *	Returns a malloced array of pointers, each pointer pointing to a
16547c478bd9Sstevel@tonic-gate  *	malloced string. If BUFEER is an empty string, then return values is
16557c478bd9Sstevel@tonic-gate  *	array of 1 pointer that is NULL. Returns NULL on failure.
16567c478bd9Sstevel@tonic-gate  *
16577c478bd9Sstevel@tonic-gate  */
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate static char **
16607c478bd9Sstevel@tonic-gate splitarray(const char *buffer, const char *buffend, char delim) {
16617c478bd9Sstevel@tonic-gate 	const char *p, *q;
16627c478bd9Sstevel@tonic-gate 	int count = 0;
16637c478bd9Sstevel@tonic-gate 	char **arr = NULL;
16647c478bd9Sstevel@tonic-gate 	char **aptr;
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	if (buffend < buffer)
16677c478bd9Sstevel@tonic-gate 		return (NULL);
16687c478bd9Sstevel@tonic-gate 	else if (buffend > buffer && *buffer == delim)
16697c478bd9Sstevel@tonic-gate 		return (NULL);
16707c478bd9Sstevel@tonic-gate 	else if (buffend > buffer && *(buffend - 1) == delim)
16717c478bd9Sstevel@tonic-gate 		return (NULL);
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	/* count the number of field and make sure none are empty */
16747c478bd9Sstevel@tonic-gate 	if (buffend > buffer + 1) {
16757c478bd9Sstevel@tonic-gate 		for (count = 1, q = buffer ; q != buffend ; q++) {
16767c478bd9Sstevel@tonic-gate 			if (*q == delim) {
16777c478bd9Sstevel@tonic-gate 				if (q > buffer && (*(q - 1) == delim)) {
16787c478bd9Sstevel@tonic-gate 					errno = EINVAL;
16797c478bd9Sstevel@tonic-gate 					return (NULL);
16807c478bd9Sstevel@tonic-gate 				}
16817c478bd9Sstevel@tonic-gate 				count++;
16827c478bd9Sstevel@tonic-gate 			}
16837c478bd9Sstevel@tonic-gate 		}
16847c478bd9Sstevel@tonic-gate 	}
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	if (count > 0) {
1687*9525b14bSRao Shoaib 		count++ ;		/*%< for NULL at end */
16887c478bd9Sstevel@tonic-gate 		aptr = arr = malloc(count * sizeof (char *));
16897c478bd9Sstevel@tonic-gate 		if (aptr == NULL) {
16907c478bd9Sstevel@tonic-gate 			 errno = ENOMEM;
16917c478bd9Sstevel@tonic-gate 			 return (NULL);
16927c478bd9Sstevel@tonic-gate 		 }
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 		memset(arr, 0x0, count * sizeof (char *));
16957c478bd9Sstevel@tonic-gate 		for (p = buffer ; p < buffend ; p++) {
16967c478bd9Sstevel@tonic-gate 			for (q = p ; *q != delim && q != buffend ; q++)
16977c478bd9Sstevel@tonic-gate 				/* nothing */;
16987c478bd9Sstevel@tonic-gate 			*aptr = strndup(p, q - p);
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 			p = q;
17017c478bd9Sstevel@tonic-gate 			aptr++;
17027c478bd9Sstevel@tonic-gate 		}
17037c478bd9Sstevel@tonic-gate 		*aptr = NULL;
17047c478bd9Sstevel@tonic-gate 	} else {
17057c478bd9Sstevel@tonic-gate 		arr = malloc(sizeof (char *));
17067c478bd9Sstevel@tonic-gate 		if (arr == NULL) {
17077c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
17087c478bd9Sstevel@tonic-gate 			return (NULL);
17097c478bd9Sstevel@tonic-gate 		}
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 		*arr = NULL;
17127c478bd9Sstevel@tonic-gate 	}
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	return (arr);
17157c478bd9Sstevel@tonic-gate }
17167c478bd9Sstevel@tonic-gate 
1717*9525b14bSRao Shoaib /*%
17187c478bd9Sstevel@tonic-gate  * static size_t joinlength(char * const *argv)
17197c478bd9Sstevel@tonic-gate  *
1720*9525b14bSRao Shoaib  * return: \li
17217c478bd9Sstevel@tonic-gate  *
17227c478bd9Sstevel@tonic-gate  *	the number of bytes in all the arrays pointed at
17237c478bd9Sstevel@tonic-gate  *	by argv, including their null bytes(which will usually be turned
17247c478bd9Sstevel@tonic-gate  *	into commas).
17257c478bd9Sstevel@tonic-gate  *
17267c478bd9Sstevel@tonic-gate  *
17277c478bd9Sstevel@tonic-gate  */
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate static size_t
17307c478bd9Sstevel@tonic-gate joinlength(char * const *argv) {
17317c478bd9Sstevel@tonic-gate 	int len = 0;
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 	while (argv && *argv) {
17347c478bd9Sstevel@tonic-gate 		len += (strlen(*argv) + 1);
17357c478bd9Sstevel@tonic-gate 		argv++;
17367c478bd9Sstevel@tonic-gate 	}
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	return (len);
17397c478bd9Sstevel@tonic-gate }
17407c478bd9Sstevel@tonic-gate 
1741*9525b14bSRao Shoaib /*%
17427c478bd9Sstevel@tonic-gate  * int joinarray(char * const *argv, char *buffer, char delim)
17437c478bd9Sstevel@tonic-gate  *
1744*9525b14bSRao Shoaib  * notes: \li
17457c478bd9Sstevel@tonic-gate  *
17467c478bd9Sstevel@tonic-gate  *	Copy all the ARGV strings into the end of BUFFER
17477c478bd9Sstevel@tonic-gate  *	separating them with DELIM.  BUFFER is assumed to have
17487c478bd9Sstevel@tonic-gate  *	enough space to hold everything and to be already null-terminated.
17497c478bd9Sstevel@tonic-gate  *
1750*9525b14bSRao Shoaib  * return: \li
17517c478bd9Sstevel@tonic-gate  *
17527c478bd9Sstevel@tonic-gate  *	0 unless argv or buffer is NULL.
17537c478bd9Sstevel@tonic-gate  *
17547c478bd9Sstevel@tonic-gate  *
17557c478bd9Sstevel@tonic-gate  */
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate static int
17587c478bd9Sstevel@tonic-gate joinarray(char * const *argv, char *buffer, char delim) {
17597c478bd9Sstevel@tonic-gate 	char * const *p;
17607c478bd9Sstevel@tonic-gate 	char sep[2];
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	if (argv == NULL || buffer == NULL) {
17637c478bd9Sstevel@tonic-gate 		errno = EINVAL;
17647c478bd9Sstevel@tonic-gate 		return (-1);
17657c478bd9Sstevel@tonic-gate 	}
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	sep[0] = delim;
17687c478bd9Sstevel@tonic-gate 	sep[1] = 0x0;
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	for (p = argv ; *p != NULL ; p++) {
17717c478bd9Sstevel@tonic-gate 		strcat(buffer, *p);
17727c478bd9Sstevel@tonic-gate 		if (*(p + 1) != NULL) {
17737c478bd9Sstevel@tonic-gate 			strcat(buffer, sep);
17747c478bd9Sstevel@tonic-gate 		}
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	return (0);
17787c478bd9Sstevel@tonic-gate }
17797c478bd9Sstevel@tonic-gate 
1780*9525b14bSRao Shoaib /*%
17817c478bd9Sstevel@tonic-gate  * static char * getfield(char **res, size_t reslen, char **ptr, char delim)
17827c478bd9Sstevel@tonic-gate  *
1783*9525b14bSRao Shoaib  * notes: \li
17847c478bd9Sstevel@tonic-gate  *
17857c478bd9Sstevel@tonic-gate  *	Stores in *RES, which is a buffer of length RESLEN, a
17867c478bd9Sstevel@tonic-gate  *	copy of the bytes from *PTR up to and including the first
17877c478bd9Sstevel@tonic-gate  *	instance of DELIM. If *RES is NULL, then it will be
17887c478bd9Sstevel@tonic-gate  *	assigned a malloced buffer to hold the copy. *PTR is
17897c478bd9Sstevel@tonic-gate  *	modified to point at the found delimiter.
17907c478bd9Sstevel@tonic-gate  *
1791*9525b14bSRao Shoaib  * return: \li
17927c478bd9Sstevel@tonic-gate  *
17937c478bd9Sstevel@tonic-gate  *	If there was no delimiter, then NULL is returned,
17947c478bd9Sstevel@tonic-gate  *	otherewise *RES is returned.
17957c478bd9Sstevel@tonic-gate  *
17967c478bd9Sstevel@tonic-gate  */
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate static char *
17997c478bd9Sstevel@tonic-gate getfield(char **res, size_t reslen, char **ptr, char delim) {
18007c478bd9Sstevel@tonic-gate 	char *q;
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	if (res == NULL || ptr == NULL || *ptr == NULL) {
18037c478bd9Sstevel@tonic-gate 		errno = EINVAL;
18047c478bd9Sstevel@tonic-gate 		return (NULL);
18057c478bd9Sstevel@tonic-gate 	}
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	q = strchr(*ptr, delim);
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	if (q == NULL) {
18107c478bd9Sstevel@tonic-gate 		errno = EINVAL;
18117c478bd9Sstevel@tonic-gate 		return (NULL);
18127c478bd9Sstevel@tonic-gate 	} else {
18137c478bd9Sstevel@tonic-gate 		if (*res == NULL) {
18147c478bd9Sstevel@tonic-gate 			*res = strndup(*ptr, q - *ptr);
18157c478bd9Sstevel@tonic-gate 		} else {
1816*9525b14bSRao Shoaib 			if ((size_t)(q - *ptr + 1) > reslen) { /*%< to big for res */
18177c478bd9Sstevel@tonic-gate 				errno = EINVAL;
18187c478bd9Sstevel@tonic-gate 				return (NULL);
18197c478bd9Sstevel@tonic-gate 			} else {
18207c478bd9Sstevel@tonic-gate 				strncpy(*res, *ptr, q - *ptr);
18217c478bd9Sstevel@tonic-gate 				(*res)[q - *ptr] = 0x0;
18227c478bd9Sstevel@tonic-gate 			}
18237c478bd9Sstevel@tonic-gate 		}
18247c478bd9Sstevel@tonic-gate 		*ptr = q + 1;
18257c478bd9Sstevel@tonic-gate 	}
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	return (*res);
18287c478bd9Sstevel@tonic-gate }
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 
1834*9525b14bSRao Shoaib #ifndef HAVE_STRNDUP
18357c478bd9Sstevel@tonic-gate /*
18367c478bd9Sstevel@tonic-gate  * static char * strndup(const char *str, size_t len)
18377c478bd9Sstevel@tonic-gate  *
1838*9525b14bSRao Shoaib  * notes: \li
18397c478bd9Sstevel@tonic-gate  *
18407c478bd9Sstevel@tonic-gate  *	like strdup, except do len bytes instead of the whole string. Always
18417c478bd9Sstevel@tonic-gate  *	null-terminates.
18427c478bd9Sstevel@tonic-gate  *
1843*9525b14bSRao Shoaib  * return: \li
18447c478bd9Sstevel@tonic-gate  *
18457c478bd9Sstevel@tonic-gate  *	The newly malloced string.
18467c478bd9Sstevel@tonic-gate  *
18477c478bd9Sstevel@tonic-gate  */
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate static char *
18507c478bd9Sstevel@tonic-gate strndup(const char *str, size_t len) {
18517c478bd9Sstevel@tonic-gate 	char *p = malloc(len + 1);
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	if (p == NULL)
18547c478bd9Sstevel@tonic-gate 		return (NULL);
18557c478bd9Sstevel@tonic-gate 	strncpy(p, str, len);
18567c478bd9Sstevel@tonic-gate 	p[len] = 0x0;
18577c478bd9Sstevel@tonic-gate 	return (p);
18587c478bd9Sstevel@tonic-gate }
1859*9525b14bSRao Shoaib #endif
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate #if WANT_MAIN
18627c478bd9Sstevel@tonic-gate 
1863*9525b14bSRao Shoaib /*%
18647c478bd9Sstevel@tonic-gate  * static int strcmp_nws(const char *a, const char *b)
18657c478bd9Sstevel@tonic-gate  *
1866*9525b14bSRao Shoaib  * notes: \li
18677c478bd9Sstevel@tonic-gate  *
18687c478bd9Sstevel@tonic-gate  *	do a strcmp, except uneven lengths of whitespace compare the same
18697c478bd9Sstevel@tonic-gate  *
1870*9525b14bSRao Shoaib  * return: \li
18717c478bd9Sstevel@tonic-gate  *
18727c478bd9Sstevel@tonic-gate  */
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate static int
18757c478bd9Sstevel@tonic-gate strcmp_nws(const char *a, const char *b) {
18767c478bd9Sstevel@tonic-gate 	while (*a && *b) {
18777c478bd9Sstevel@tonic-gate 		if (isspace(*a) && isspace(*b)) {
18787c478bd9Sstevel@tonic-gate 			do {
18797c478bd9Sstevel@tonic-gate 				a++;
18807c478bd9Sstevel@tonic-gate 			} while (isspace(*a));
18817c478bd9Sstevel@tonic-gate 			do {
18827c478bd9Sstevel@tonic-gate 				b++;
18837c478bd9Sstevel@tonic-gate 			} while (isspace(*b));
18847c478bd9Sstevel@tonic-gate 		}
18857c478bd9Sstevel@tonic-gate 		if (*a < *b)
18867c478bd9Sstevel@tonic-gate 			return (-1);
18877c478bd9Sstevel@tonic-gate 		else if (*a > *b)
18887c478bd9Sstevel@tonic-gate 			return (1);
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 		a++;
18917c478bd9Sstevel@tonic-gate 		b++;;
18927c478bd9Sstevel@tonic-gate 	}
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	if (*a == *b)
18957c478bd9Sstevel@tonic-gate 		return (0);
18967c478bd9Sstevel@tonic-gate 	else if (*a > *b)
18977c478bd9Sstevel@tonic-gate 		return (1);
18987c478bd9Sstevel@tonic-gate 	else
18997c478bd9Sstevel@tonic-gate 		return (-1);
19007c478bd9Sstevel@tonic-gate }
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate #endif
19037c478bd9Sstevel@tonic-gate 
1904*9525b14bSRao Shoaib /*%
19057c478bd9Sstevel@tonic-gate  * static void free_array(char **argv, size_t entries)
19067c478bd9Sstevel@tonic-gate  *
1907*9525b14bSRao Shoaib  * notes: \li
19087c478bd9Sstevel@tonic-gate  *
19097c478bd9Sstevel@tonic-gate  *	Free argv and each of the pointers inside it. The end of
19107c478bd9Sstevel@tonic-gate  *	the array is when a NULL pointer is found inside. If
19117c478bd9Sstevel@tonic-gate  *	entries is > 0, then NULL pointers inside the array do
19127c478bd9Sstevel@tonic-gate  *	not indicate the end of the array.
19137c478bd9Sstevel@tonic-gate  *
19147c478bd9Sstevel@tonic-gate  */
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate static void
19177c478bd9Sstevel@tonic-gate free_array(char **argv, size_t entries) {
19187c478bd9Sstevel@tonic-gate 	char **p = argv;
1919*9525b14bSRao Shoaib 	int useEntries = (entries > 0U);
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	if (argv == NULL)
19227c478bd9Sstevel@tonic-gate 		return;
19237c478bd9Sstevel@tonic-gate 
1924*9525b14bSRao Shoaib 	while ((useEntries && entries > 0U) || *p) {
19257c478bd9Sstevel@tonic-gate 		if (*p)
19267c478bd9Sstevel@tonic-gate 			free(*p);
19277c478bd9Sstevel@tonic-gate 		p++;
19287c478bd9Sstevel@tonic-gate 		if (useEntries)
19297c478bd9Sstevel@tonic-gate 			entries--;
19307c478bd9Sstevel@tonic-gate 	}
19317c478bd9Sstevel@tonic-gate 	free(argv);
19327c478bd9Sstevel@tonic-gate }
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate /* ************************************************** */
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate #if WANT_MAIN
19417c478bd9Sstevel@tonic-gate 
1942*9525b14bSRao Shoaib /*% takes an option to indicate what sort of marshalling(read the code) and
19437c478bd9Sstevel@tonic-gate    an argument. If the argument looks like a marshalled buffer(has a ':'
19447c478bd9Sstevel@tonic-gate    embedded) then it's unmarshalled and the remarshalled and the new string
19457c478bd9Sstevel@tonic-gate    is compared to the old one.
19467c478bd9Sstevel@tonic-gate */
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate int
19497c478bd9Sstevel@tonic-gate main(int argc, char **argv) {
19507c478bd9Sstevel@tonic-gate 	char buffer[1024];
19517c478bd9Sstevel@tonic-gate 	char *b = &buffer[0];
19527c478bd9Sstevel@tonic-gate 	size_t len = sizeof buffer;
19537c478bd9Sstevel@tonic-gate 	char option;
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	if (argc < 2 || argv[1][0] != '-')
19567c478bd9Sstevel@tonic-gate 		exit(1);
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	option = argv[1][1];
19597c478bd9Sstevel@tonic-gate 	argv++;
19607c478bd9Sstevel@tonic-gate 	argc--;
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate #if 0
19647c478bd9Sstevel@tonic-gate 	{
19657c478bd9Sstevel@tonic-gate 		char buff[10];
19667c478bd9Sstevel@tonic-gate 		char *p = argv[1], *q = &buff[0];
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 		while (getfield(&q, sizeof buff, &p, ':') != NULL) {
19697c478bd9Sstevel@tonic-gate 			printf("field: \"%s\"\n", q);
19707c478bd9Sstevel@tonic-gate 			p++;
19717c478bd9Sstevel@tonic-gate 		}
19727c478bd9Sstevel@tonic-gate 		printf("p is now \"%s\"\n", p);
19737c478bd9Sstevel@tonic-gate 	}
19747c478bd9Sstevel@tonic-gate #endif
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate #if 0
19777c478bd9Sstevel@tonic-gate 	{
19787c478bd9Sstevel@tonic-gate 		char **x = splitarray(argv[1], argv[1] + strlen(argv[1]),
19797c478bd9Sstevel@tonic-gate 				      argv[2][0]);
19807c478bd9Sstevel@tonic-gate 		char **p;
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 		if (x == NULL)
19837c478bd9Sstevel@tonic-gate 			printf("split failed\n");
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 		for (p = x ; p != NULL && *p != NULL ; p++) {
19867c478bd9Sstevel@tonic-gate 			printf("\"%s\"\n", *p);
19877c478bd9Sstevel@tonic-gate 		}
19887c478bd9Sstevel@tonic-gate 	}
19897c478bd9Sstevel@tonic-gate #endif
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate #if 1
19927c478bd9Sstevel@tonic-gate 	switch(option) {
19937c478bd9Sstevel@tonic-gate 	case 'n': {
19947c478bd9Sstevel@tonic-gate 		struct nwent ne;
19957c478bd9Sstevel@tonic-gate 		int i;
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
19987c478bd9Sstevel@tonic-gate 			if (irp_unmarshall_nw(&ne, argv[1]) != 0) {
19997c478bd9Sstevel@tonic-gate 				printf("Unmarhsalling failed\n");
20007c478bd9Sstevel@tonic-gate 				exit(1);
20017c478bd9Sstevel@tonic-gate 			}
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 			printf("Name: \"%s\"\n", ne.n_name);
20047c478bd9Sstevel@tonic-gate 			printf("Aliases:");
20057c478bd9Sstevel@tonic-gate 			for (i = 0 ; ne.n_aliases[i] != NULL ; i++)
20067c478bd9Sstevel@tonic-gate 				printf("\n\t\"%s\"", ne.n_aliases[i]);
20077c478bd9Sstevel@tonic-gate 			printf("\nAddrtype: %s\n", ADDR_T_STR(ne.n_addrtype));
20087c478bd9Sstevel@tonic-gate 			inet_net_ntop(ne.n_addrtype, ne.n_addr, ne.n_length,
20097c478bd9Sstevel@tonic-gate 				      buffer, sizeof buffer);
20107c478bd9Sstevel@tonic-gate 			printf("Net: \"%s\"\n", buffer);
20117c478bd9Sstevel@tonic-gate 			*((long*)ne.n_addr) = htonl(*((long*)ne.n_addr));
20127c478bd9Sstevel@tonic-gate 			inet_net_ntop(ne.n_addrtype, ne.n_addr, ne.n_length,
20137c478bd9Sstevel@tonic-gate 				      buffer, sizeof buffer);
20147c478bd9Sstevel@tonic-gate 			printf("Corrected Net: \"%s\"\n", buffer);
20157c478bd9Sstevel@tonic-gate 		} else {
20167c478bd9Sstevel@tonic-gate 			struct netent *np1 = getnetbyname(argv[1]);
20177c478bd9Sstevel@tonic-gate 			ne.n_name = np1->n_name;
20187c478bd9Sstevel@tonic-gate 			ne.n_aliases = np1->n_aliases;
20197c478bd9Sstevel@tonic-gate 			ne.n_addrtype = np1->n_addrtype;
20207c478bd9Sstevel@tonic-gate 			ne.n_addr = &np1->n_net;
20217c478bd9Sstevel@tonic-gate 			ne.n_length = (IN_CLASSA(np1->n_net) ?
20227c478bd9Sstevel@tonic-gate 				       8 :
20237c478bd9Sstevel@tonic-gate 				       (IN_CLASSB(np1->n_net) ?
20247c478bd9Sstevel@tonic-gate 					16 :
20257c478bd9Sstevel@tonic-gate 					(IN_CLASSC(np1->n_net) ?
20267c478bd9Sstevel@tonic-gate 					 24 : -1)));
20277c478bd9Sstevel@tonic-gate 			np1->n_net = htonl(np1->n_net);
20287c478bd9Sstevel@tonic-gate 			if (irp_marshall_nw(&ne, &b, &len) != 0) {
20297c478bd9Sstevel@tonic-gate 				printf("Marshalling failed\n");
20307c478bd9Sstevel@tonic-gate 			}
20317c478bd9Sstevel@tonic-gate 			printf("%s\n", b);
20327c478bd9Sstevel@tonic-gate 		}
20337c478bd9Sstevel@tonic-gate 		break;
20347c478bd9Sstevel@tonic-gate 	}
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	case 'r': {
20387c478bd9Sstevel@tonic-gate 		char **hosts, **users, **domains;
20397c478bd9Sstevel@tonic-gate 		size_t entries;
20407c478bd9Sstevel@tonic-gate 		int i;
20417c478bd9Sstevel@tonic-gate 		char *buff;
20427c478bd9Sstevel@tonic-gate 		size_t size;
20437c478bd9Sstevel@tonic-gate 		char *ngname;
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 		if (strchr(argv[1], '(') != NULL) {
20467c478bd9Sstevel@tonic-gate 			if (irp_unmarshall_ng(&ngname, &entries,
20477c478bd9Sstevel@tonic-gate 					      &hosts, &users, &domains,
20487c478bd9Sstevel@tonic-gate 					      argv[1]) != 0) {
20497c478bd9Sstevel@tonic-gate 				printf("unmarshall failed\n");
20507c478bd9Sstevel@tonic-gate 				exit(1);
20517c478bd9Sstevel@tonic-gate 			}
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate #define STRVAL(x) (x == NULL ? "*" : x)
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 			printf("%s {\n", ngname);
20567c478bd9Sstevel@tonic-gate 			for (i = 0 ; i < entries ; i++)
20577c478bd9Sstevel@tonic-gate 				printf("\t\"%s\" : \"%s\" : \"%s\"\n",
20587c478bd9Sstevel@tonic-gate 				       STRVAL(hosts[i]),
20597c478bd9Sstevel@tonic-gate 				       STRVAL(users[i]),
20607c478bd9Sstevel@tonic-gate 				       STRVAL(domains[i]));
20617c478bd9Sstevel@tonic-gate 			printf("}\n\n\n");
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 			irp_marshall_ng_start(ngname, NULL, &size);
20657c478bd9Sstevel@tonic-gate 			for (i = 0 ; i < entries ; i++)
20667c478bd9Sstevel@tonic-gate 				irp_marshall_ng_next(hosts[i], users[i],
20677c478bd9Sstevel@tonic-gate 						     domains[i], NULL, &size);
20687c478bd9Sstevel@tonic-gate 			irp_marshall_ng_end(NULL, &size);
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 			buff = malloc(size);
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 			irp_marshall_ng_start(ngname, buff, &size);
20737c478bd9Sstevel@tonic-gate 			for (i = 0 ; i < entries ; i++) {
20747c478bd9Sstevel@tonic-gate 				if (irp_marshall_ng_next(hosts[i], users[i],
20757c478bd9Sstevel@tonic-gate 							 domains[i], buff,
20767c478bd9Sstevel@tonic-gate 							 &size) != 0)
20777c478bd9Sstevel@tonic-gate 					printf("next marshalling failed.\n");
20787c478bd9Sstevel@tonic-gate 			}
20797c478bd9Sstevel@tonic-gate 			irp_marshall_ng_end(buff, &size);
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 			if (strcmp_nws(argv[1], buff) != 0) {
20827c478bd9Sstevel@tonic-gate 				printf("compare failed:\n\t%s\n\t%s\n",
20837c478bd9Sstevel@tonic-gate 				       buffer, argv[1]);
20847c478bd9Sstevel@tonic-gate 			} else {
20857c478bd9Sstevel@tonic-gate 				printf("compare ok\n");
20867c478bd9Sstevel@tonic-gate 			}
20877c478bd9Sstevel@tonic-gate 		} else {
20887c478bd9Sstevel@tonic-gate 			char *h, *u, *d, *buff;
20897c478bd9Sstevel@tonic-gate 			size_t size;
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 			/* run through two times. First to figure out how
20927c478bd9Sstevel@tonic-gate 			   much of a buffer we need. Second to do the
20937c478bd9Sstevel@tonic-gate 			   actual marshalling */
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 			setnetgrent(argv[1]);
20967c478bd9Sstevel@tonic-gate 			irp_marshall_ng_start(argv[1], NULL, &size);
20977c478bd9Sstevel@tonic-gate 			while (getnetgrent(&h, &u, &d) == 1)
20987c478bd9Sstevel@tonic-gate 				irp_marshall_ng_next(h, u, d, NULL, &size);
20997c478bd9Sstevel@tonic-gate 			irp_marshall_ng_end(NULL, &size);
21007c478bd9Sstevel@tonic-gate 			endnetgrent(argv[1]);
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 			buff = malloc(size);
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 			setnetgrent(argv[1]);
21057c478bd9Sstevel@tonic-gate 			if (irp_marshall_ng_start(argv[1], buff, &size) != 0)
21067c478bd9Sstevel@tonic-gate 				printf("Marshalling start failed\n");
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 			while (getnetgrent(&h, &u, &d) == 1) {
21097c478bd9Sstevel@tonic-gate 				if (irp_marshall_ng_next(h, u, d, buff, &size)
21107c478bd9Sstevel@tonic-gate 				    != 0) {
21117c478bd9Sstevel@tonic-gate 					printf("Marshalling failed\n");
21127c478bd9Sstevel@tonic-gate 				}
21137c478bd9Sstevel@tonic-gate 			}
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 			irp_marshall_ng_end(buff, &size);
21167c478bd9Sstevel@tonic-gate 			endnetgrent();
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 			printf("success: %s\n", buff);
21197c478bd9Sstevel@tonic-gate 		}
21207c478bd9Sstevel@tonic-gate 		break;
21217c478bd9Sstevel@tonic-gate 	}
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 	case 'h': {
21267c478bd9Sstevel@tonic-gate 		struct hostent he, *hp;
21277c478bd9Sstevel@tonic-gate 		int i;
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 		if (strchr(argv[1], '@') != NULL) {
21317c478bd9Sstevel@tonic-gate 			if (irp_unmarshall_ho(&he, argv[1]) != 0) {
21327c478bd9Sstevel@tonic-gate 				printf("unmarshall failed\n");
21337c478bd9Sstevel@tonic-gate 				exit(1);
21347c478bd9Sstevel@tonic-gate 			}
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 			printf("Host: \"%s\"\nAliases:", he.h_name);
21377c478bd9Sstevel@tonic-gate 			for (i = 0 ; he.h_aliases[i] != NULL ; i++)
21387c478bd9Sstevel@tonic-gate 				printf("\n\t\t\"%s\"", he.h_aliases[i]);
21397c478bd9Sstevel@tonic-gate 			printf("\nAddr Type: \"%s\"\n",
21407c478bd9Sstevel@tonic-gate 			       ADDR_T_STR(he.h_addrtype));
21417c478bd9Sstevel@tonic-gate 			printf("Length: %d\nAddresses:", he.h_length);
21427c478bd9Sstevel@tonic-gate 			for (i = 0 ; he.h_addr_list[i] != 0 ; i++) {
21437c478bd9Sstevel@tonic-gate 				inet_ntop(he.h_addrtype, he.h_addr_list[i],
21447c478bd9Sstevel@tonic-gate 					  buffer, sizeof buffer);
21457c478bd9Sstevel@tonic-gate 				printf("\n\t\"%s\"\n", buffer);
21467c478bd9Sstevel@tonic-gate 			}
21477c478bd9Sstevel@tonic-gate 			printf("\n\n");
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 			irp_marshall_ho(&he, &b, &len);
21507c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
21517c478bd9Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
21527c478bd9Sstevel@tonic-gate 				       buffer, argv[1]);
21537c478bd9Sstevel@tonic-gate 			} else {
21547c478bd9Sstevel@tonic-gate 				printf("compare ok\n");
21557c478bd9Sstevel@tonic-gate 			}
21567c478bd9Sstevel@tonic-gate 		} else {
21577c478bd9Sstevel@tonic-gate 			if ((hp = gethostbyname(argv[1])) == NULL) {
21587c478bd9Sstevel@tonic-gate 				perror("gethostbyname");
21597c478bd9Sstevel@tonic-gate 				printf("\"%s\"\n", argv[1]);
21607c478bd9Sstevel@tonic-gate 				exit(1);
21617c478bd9Sstevel@tonic-gate 			}
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 			if (irp_marshall_ho(hp, &b, &len) != 0) {
21647c478bd9Sstevel@tonic-gate 				printf("irp_marshall_ho failed\n");
21657c478bd9Sstevel@tonic-gate 				exit(1);
21667c478bd9Sstevel@tonic-gate 			}
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
21697c478bd9Sstevel@tonic-gate 		}
21707c478bd9Sstevel@tonic-gate 		break;
21717c478bd9Sstevel@tonic-gate 	}
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	case 's': {
21757c478bd9Sstevel@tonic-gate 		struct servent *sv;
21767c478bd9Sstevel@tonic-gate 		struct servent sv1;
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
21797c478bd9Sstevel@tonic-gate 			sv = &sv1;
21807c478bd9Sstevel@tonic-gate 			memset(sv, 0xef, sizeof (struct servent));
21817c478bd9Sstevel@tonic-gate 			if (irp_unmarshall_sv(sv, argv[1]) != 0) {
21827c478bd9Sstevel@tonic-gate 				printf("unmarshall failed\n");
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 			}
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 			irp_marshall_sv(sv, &b, &len);
21877c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
21887c478bd9Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
21897c478bd9Sstevel@tonic-gate 				       buffer, argv[1]);
21907c478bd9Sstevel@tonic-gate 			} else {
21917c478bd9Sstevel@tonic-gate 				printf("compare ok\n");
21927c478bd9Sstevel@tonic-gate 			}
21937c478bd9Sstevel@tonic-gate 		} else {
21947c478bd9Sstevel@tonic-gate 			if ((sv = getservbyname(argv[1], argv[2])) == NULL) {
21957c478bd9Sstevel@tonic-gate 				perror("getservent");
21967c478bd9Sstevel@tonic-gate 				exit(1);
21977c478bd9Sstevel@tonic-gate 			}
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 			if (irp_marshall_sv(sv, &b, &len) != 0) {
22007c478bd9Sstevel@tonic-gate 				printf("irp_marshall_sv failed\n");
22017c478bd9Sstevel@tonic-gate 				exit(1);
22027c478bd9Sstevel@tonic-gate 			}
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
22057c478bd9Sstevel@tonic-gate 		}
22067c478bd9Sstevel@tonic-gate 		break;
22077c478bd9Sstevel@tonic-gate 	}
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 	case 'g': {
22107c478bd9Sstevel@tonic-gate 		struct group *gr;
22117c478bd9Sstevel@tonic-gate 		struct group gr1;
22127c478bd9Sstevel@tonic-gate 
22137c478bd9Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
22147c478bd9Sstevel@tonic-gate 			gr = &gr1;
22157c478bd9Sstevel@tonic-gate 			memset(gr, 0xef, sizeof (struct group));
22167c478bd9Sstevel@tonic-gate 			if (irp_unmarshall_gr(gr, argv[1]) != 0) {
22177c478bd9Sstevel@tonic-gate 				printf("unmarshall failed\n");
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 			}
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 			irp_marshall_gr(gr, &b, &len);
22227c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
22237c478bd9Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
22247c478bd9Sstevel@tonic-gate 				       buffer, argv[1]);
22257c478bd9Sstevel@tonic-gate 			} else {
22267c478bd9Sstevel@tonic-gate 				printf("compare ok\n");
22277c478bd9Sstevel@tonic-gate 			}
22287c478bd9Sstevel@tonic-gate 		} else {
22297c478bd9Sstevel@tonic-gate 			if ((gr = getgrnam(argv[1])) == NULL) {
22307c478bd9Sstevel@tonic-gate 				perror("getgrnam");
22317c478bd9Sstevel@tonic-gate 				exit(1);
22327c478bd9Sstevel@tonic-gate 			}
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate 			if (irp_marshall_gr(gr, &b, &len) != 0) {
22357c478bd9Sstevel@tonic-gate 				printf("irp_marshall_gr failed\n");
22367c478bd9Sstevel@tonic-gate 				exit(1);
22377c478bd9Sstevel@tonic-gate 			}
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
22407c478bd9Sstevel@tonic-gate 		}
22417c478bd9Sstevel@tonic-gate 		break;
22427c478bd9Sstevel@tonic-gate 	}
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	case 'p': {
22467c478bd9Sstevel@tonic-gate 		struct passwd *pw;
22477c478bd9Sstevel@tonic-gate 		struct passwd pw1;
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
22507c478bd9Sstevel@tonic-gate 			pw = &pw1;
22517c478bd9Sstevel@tonic-gate 			memset(pw, 0xef, sizeof (*pw));
22527c478bd9Sstevel@tonic-gate 			if (irp_unmarshall_pw(pw, argv[1]) != 0) {
22537c478bd9Sstevel@tonic-gate 				printf("unmarshall failed\n");
22547c478bd9Sstevel@tonic-gate 				exit(1);
22557c478bd9Sstevel@tonic-gate 			}
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 			printf("User: \"%s\"\nPasswd: \"%s\"\nUid: %ld\nGid: %ld\n",
22587c478bd9Sstevel@tonic-gate 			       pw->pw_name, pw->pw_passwd, (long)pw->pw_uid,
22597c478bd9Sstevel@tonic-gate 			       (long)pw->pw_gid);
22607c478bd9Sstevel@tonic-gate 			printf("Class: \"%s\"\nChange: %ld\nGecos: \"%s\"\n",
22617c478bd9Sstevel@tonic-gate 			       pw->pw_class, (long)pw->pw_change, pw->pw_gecos);
22627c478bd9Sstevel@tonic-gate 			printf("Shell: \"%s\"\nDirectory: \"%s\"\n",
22637c478bd9Sstevel@tonic-gate 			       pw->pw_shell, pw->pw_dir);
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 			pw = getpwnam(pw->pw_name);
22667c478bd9Sstevel@tonic-gate 			irp_marshall_pw(pw, &b, &len);
22677c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
22687c478bd9Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
22697c478bd9Sstevel@tonic-gate 				       buffer, argv[1]);
22707c478bd9Sstevel@tonic-gate 			} else {
22717c478bd9Sstevel@tonic-gate 				printf("compare ok\n");
22727c478bd9Sstevel@tonic-gate 			}
22737c478bd9Sstevel@tonic-gate 		} else {
22747c478bd9Sstevel@tonic-gate 			if ((pw = getpwnam(argv[1])) == NULL) {
22757c478bd9Sstevel@tonic-gate 				perror("getpwnam");
22767c478bd9Sstevel@tonic-gate 				exit(1);
22777c478bd9Sstevel@tonic-gate 			}
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 			if (irp_marshall_pw(pw, &b, &len) != 0) {
22807c478bd9Sstevel@tonic-gate 				printf("irp_marshall_pw failed\n");
22817c478bd9Sstevel@tonic-gate 				exit(1);
22827c478bd9Sstevel@tonic-gate 			}
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
22857c478bd9Sstevel@tonic-gate 		}
22867c478bd9Sstevel@tonic-gate 		break;
22877c478bd9Sstevel@tonic-gate 	}
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 	default:
22907c478bd9Sstevel@tonic-gate 		printf("Wrong option: %c\n", option);
22917c478bd9Sstevel@tonic-gate 		break;
22927c478bd9Sstevel@tonic-gate 	}
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate #endif
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	return (0);
22977c478bd9Sstevel@tonic-gate }
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate #endif
2300*9525b14bSRao Shoaib 
2301*9525b14bSRao Shoaib /*! \file */
2302