xref: /illumos-gate/usr/src/lib/libresolv2/common/irs/irpmarshall.c (revision 458f44a49dc56cd17a39815122214e7a1b4793e3)
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 0
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate Check values are in approrpriate endian order.
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate Double check memory allocations on unmarhsalling
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #endif
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /* Extern */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include "port_before.h"
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <sys/types.h>
657c478bd9Sstevel@tonic-gate #include <sys/socket.h>
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #include <netinet/in.h>
687c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
697c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #include <stdio.h>
727c478bd9Sstevel@tonic-gate #include <ctype.h>
737c478bd9Sstevel@tonic-gate #include <pwd.h>
747c478bd9Sstevel@tonic-gate #include <stdlib.h>
757c478bd9Sstevel@tonic-gate #include <string.h>
767c478bd9Sstevel@tonic-gate #include <syslog.h>
777c478bd9Sstevel@tonic-gate #include <utmp.h>
787c478bd9Sstevel@tonic-gate #include <unistd.h>
797c478bd9Sstevel@tonic-gate #include <assert.h>
807c478bd9Sstevel@tonic-gate #include <errno.h>
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #include <irs.h>
837c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
847c478bd9Sstevel@tonic-gate #include <isc/irpmarshall.h>
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #include "port_after.h"
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #ifndef HAVE_STRNDUP
907c478bd9Sstevel@tonic-gate static char    *strndup(const char *str, size_t len);
917c478bd9Sstevel@tonic-gate #endif
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static char   **splitarray(const char *buffer, const char *buffend, char delim);
947c478bd9Sstevel@tonic-gate static int	joinarray(char * const * argv, char *buffer, char delim);
957c478bd9Sstevel@tonic-gate static char    *getfield(char **res, size_t reslen, char **buffer, char delim);
967c478bd9Sstevel@tonic-gate static size_t	joinlength(char * const *argv);
977c478bd9Sstevel@tonic-gate static void	free_array(char **argv, size_t entries);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #define ADDR_T_STR(x) (x == AF_INET ? "AF_INET" :\
1007c478bd9Sstevel@tonic-gate 		       (x == AF_INET6 ? "AF_INET6" : "UNKNOWN"))
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #define MAXPADDRSIZE (sizeof "255.255.255.255" + 1)
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static char COMMA = ',';
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static const char *COMMASTR = ",";
1077c478bd9Sstevel@tonic-gate static const char *COLONSTR = ":";
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /* See big comment at bottom of irpmarshall.h for description. */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #ifdef WANT_IRS_PW
1157c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct passwd +++++++++++++++++++++++++ */
1167c478bd9Sstevel@tonic-gate 
117*9525b14bSRao Shoaib /*%
1187c478bd9Sstevel@tonic-gate  * int irp_marshall_pw(const struct passwd *pw, char **buffer, size_t *len)
1197c478bd9Sstevel@tonic-gate  *
120*9525b14bSRao Shoaib  * notes: \li
1217c478bd9Sstevel@tonic-gate  *
122*9525b14bSRao Shoaib  *	See irpmarshall.h
1237c478bd9Sstevel@tonic-gate  *
124*9525b14bSRao Shoaib  * return: \li
1257c478bd9Sstevel@tonic-gate  *
1267c478bd9Sstevel@tonic-gate  *	0 on sucess, -1 on failure.
1277c478bd9Sstevel@tonic-gate  *
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate int
irp_marshall_pw(const struct passwd * pw,char ** buffer,size_t * len)1317c478bd9Sstevel@tonic-gate irp_marshall_pw(const struct passwd *pw, char **buffer, size_t *len) {
132*9525b14bSRao Shoaib 	size_t need = 1 ;		/*%< for null byte */
1337c478bd9Sstevel@tonic-gate 	char pwUid[24];
1347c478bd9Sstevel@tonic-gate 	char pwGid[24];
1357c478bd9Sstevel@tonic-gate 	char pwChange[24];
1367c478bd9Sstevel@tonic-gate 	char pwExpire[24];
1377c478bd9Sstevel@tonic-gate 	const char *pwClass;
1387c478bd9Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (pw == NULL || len == NULL) {
1417c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1427c478bd9Sstevel@tonic-gate 		return (-1);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	sprintf(pwUid, "%ld", (long)pw->pw_uid);
1467c478bd9Sstevel@tonic-gate 	sprintf(pwGid, "%ld", (long)pw->pw_gid);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_CHANGE
1497c478bd9Sstevel@tonic-gate 	sprintf(pwChange, "%ld", (long)pw->pw_change);
1507c478bd9Sstevel@tonic-gate #else
1517c478bd9Sstevel@tonic-gate 	pwChange[0] = '0';
1527c478bd9Sstevel@tonic-gate 	pwChange[1] = '\0';
1537c478bd9Sstevel@tonic-gate #endif
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_EXPIRE
1567c478bd9Sstevel@tonic-gate 	sprintf(pwExpire, "%ld", (long)pw->pw_expire);
1577c478bd9Sstevel@tonic-gate #else
1587c478bd9Sstevel@tonic-gate 	pwExpire[0] = '0';
1597c478bd9Sstevel@tonic-gate 	pwExpire[1] = '\0';
1607c478bd9Sstevel@tonic-gate #endif
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_CLASS
1637c478bd9Sstevel@tonic-gate 	pwClass = pw->pw_class;
1647c478bd9Sstevel@tonic-gate #else
1657c478bd9Sstevel@tonic-gate 	pwClass = "";
1667c478bd9Sstevel@tonic-gate #endif
1677c478bd9Sstevel@tonic-gate 
168*9525b14bSRao Shoaib 	need += strlen(pw->pw_name)	+ 1; /*%< one for fieldsep */
1697c478bd9Sstevel@tonic-gate 	need += strlen(pw->pw_passwd)	+ 1;
1707c478bd9Sstevel@tonic-gate 	need += strlen(pwUid)		+ 1;
1717c478bd9Sstevel@tonic-gate 	need += strlen(pwGid)		+ 1;
1727c478bd9Sstevel@tonic-gate 	need += strlen(pwClass)		+ 1;
1737c478bd9Sstevel@tonic-gate 	need += strlen(pwChange)	+ 1;
1747c478bd9Sstevel@tonic-gate 	need += strlen(pwExpire)	+ 1;
1757c478bd9Sstevel@tonic-gate 	need += strlen(pw->pw_gecos)	+ 1;
1767c478bd9Sstevel@tonic-gate 	need += strlen(pw->pw_dir)	+ 1;
1777c478bd9Sstevel@tonic-gate 	need += strlen(pw->pw_shell)	+ 1;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
1807c478bd9Sstevel@tonic-gate 		*len = need;
1817c478bd9Sstevel@tonic-gate 		return (0);
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
1857c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1867c478bd9Sstevel@tonic-gate 		return (-1);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
190*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
1917c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
1927c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
1937c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
1947c478bd9Sstevel@tonic-gate 			return (-1);
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		*len = need;
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	strcpy(*buffer, pw->pw_name);		strcat(*buffer, fieldsep);
2017c478bd9Sstevel@tonic-gate 	strcat(*buffer, pw->pw_passwd);		strcat(*buffer, fieldsep);
2027c478bd9Sstevel@tonic-gate 	strcat(*buffer, pwUid);			strcat(*buffer, fieldsep);
2037c478bd9Sstevel@tonic-gate 	strcat(*buffer, pwGid);			strcat(*buffer, fieldsep);
2047c478bd9Sstevel@tonic-gate 	strcat(*buffer, pwClass);		strcat(*buffer, fieldsep);
2057c478bd9Sstevel@tonic-gate 	strcat(*buffer, pwChange);		strcat(*buffer, fieldsep);
2067c478bd9Sstevel@tonic-gate 	strcat(*buffer, pwExpire);		strcat(*buffer, fieldsep);
2077c478bd9Sstevel@tonic-gate 	strcat(*buffer, pw->pw_gecos);		strcat(*buffer, fieldsep);
2087c478bd9Sstevel@tonic-gate 	strcat(*buffer, pw->pw_dir);		strcat(*buffer, fieldsep);
2097c478bd9Sstevel@tonic-gate 	strcat(*buffer, pw->pw_shell);		strcat(*buffer, fieldsep);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	return (0);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
214*9525b14bSRao Shoaib /*%
2157c478bd9Sstevel@tonic-gate  * int irp_unmarshall_pw(struct passwd *pw, char *buffer)
2167c478bd9Sstevel@tonic-gate  *
217*9525b14bSRao Shoaib  * notes: \li
2187c478bd9Sstevel@tonic-gate  *
219*9525b14bSRao Shoaib  *	See irpmarshall.h
2207c478bd9Sstevel@tonic-gate  *
221*9525b14bSRao Shoaib  * return: \li
2227c478bd9Sstevel@tonic-gate  *
2237c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure
2247c478bd9Sstevel@tonic-gate  *
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate int
irp_unmarshall_pw(struct passwd * pw,char * buffer)2287c478bd9Sstevel@tonic-gate irp_unmarshall_pw(struct passwd *pw, char *buffer) {
2297c478bd9Sstevel@tonic-gate 	char *name, *pass, *class, *gecos, *dir, *shell;
2307c478bd9Sstevel@tonic-gate 	uid_t pwuid;
2317c478bd9Sstevel@tonic-gate 	gid_t pwgid;
2327c478bd9Sstevel@tonic-gate 	time_t pwchange;
2337c478bd9Sstevel@tonic-gate 	time_t pwexpire;
2347c478bd9Sstevel@tonic-gate 	char *p;
2357c478bd9Sstevel@tonic-gate 	long t;
2367c478bd9Sstevel@tonic-gate 	char tmpbuf[24];
2377c478bd9Sstevel@tonic-gate 	char *tb = &tmpbuf[0];
2387c478bd9Sstevel@tonic-gate 	char fieldsep = ':';
2397c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	name = pass = class = gecos = dir = shell = NULL;
2427c478bd9Sstevel@tonic-gate 	p = buffer;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/* pw_name field */
2457c478bd9Sstevel@tonic-gate 	name = NULL;
2467c478bd9Sstevel@tonic-gate 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0) {
2477c478bd9Sstevel@tonic-gate 		goto error;
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/* pw_passwd field */
2517c478bd9Sstevel@tonic-gate 	pass = NULL;
252*9525b14bSRao Shoaib 	if (getfield(&pass, 0, &p, fieldsep) == NULL) { /*%< field can be empty */
2537c478bd9Sstevel@tonic-gate 		goto error;
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/* pw_uid field */
2587c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
2597c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
2607c478bd9Sstevel@tonic-gate 	    strlen(tb) == 0) {
2617c478bd9Sstevel@tonic-gate 		goto error;
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
2647c478bd9Sstevel@tonic-gate 	if (*tb) {
265*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 	pwuid = (uid_t)t;
268*9525b14bSRao Shoaib 	if ((long) pwuid != t) {	/*%< value must have been too big. */
2697c478bd9Sstevel@tonic-gate 		goto error;
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/* pw_gid field */
2757c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
2767c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
2777c478bd9Sstevel@tonic-gate 	    strlen(tb) == 0) {
2787c478bd9Sstevel@tonic-gate 		goto error;
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
2817c478bd9Sstevel@tonic-gate 	if (*tb) {
282*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 	pwgid = (gid_t)t;
285*9525b14bSRao Shoaib 	if ((long)pwgid != t) {	/*%< value must have been too big. */
2867c478bd9Sstevel@tonic-gate 		goto error;
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/* pw_class field */
2927c478bd9Sstevel@tonic-gate 	class = NULL;
2937c478bd9Sstevel@tonic-gate 	if (getfield(&class, 0, &p, fieldsep) == NULL) {
2947c478bd9Sstevel@tonic-gate 		goto error;
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/* pw_change field */
3007c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
3017c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
3027c478bd9Sstevel@tonic-gate 	    strlen(tb) == 0) {
3037c478bd9Sstevel@tonic-gate 		goto error;
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
3067c478bd9Sstevel@tonic-gate 	if (*tb) {
307*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	pwchange = (time_t)t;
310*9525b14bSRao Shoaib 	if ((long)pwchange != t) {	/*%< value must have been too big. */
3117c478bd9Sstevel@tonic-gate 		goto error;
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	/* pw_expire field */
3177c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
3187c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
3197c478bd9Sstevel@tonic-gate 	    strlen(tb) == 0) {
3207c478bd9Sstevel@tonic-gate 		goto error;
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
3237c478bd9Sstevel@tonic-gate 	if (*tb) {
324*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	pwexpire = (time_t)t;
327*9525b14bSRao Shoaib 	if ((long) pwexpire != t) {	/*%< value must have been too big. */
3287c478bd9Sstevel@tonic-gate 		goto error;
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	/* pw_gecos field */
3347c478bd9Sstevel@tonic-gate 	gecos = NULL;
3357c478bd9Sstevel@tonic-gate 	if (getfield(&gecos, 0, &p, fieldsep) == NULL) {
3367c478bd9Sstevel@tonic-gate 		goto error;
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/* pw_dir field */
3427c478bd9Sstevel@tonic-gate 	dir = NULL;
3437c478bd9Sstevel@tonic-gate 	if (getfield(&dir, 0, &p, fieldsep) == NULL) {
3447c478bd9Sstevel@tonic-gate 		goto error;
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/* pw_shell field */
3507c478bd9Sstevel@tonic-gate 	shell = NULL;
3517c478bd9Sstevel@tonic-gate 	if (getfield(&shell, 0, &p, fieldsep) == NULL) {
3527c478bd9Sstevel@tonic-gate 		goto error;
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	pw->pw_name = name;
3587c478bd9Sstevel@tonic-gate 	pw->pw_passwd = pass;
3597c478bd9Sstevel@tonic-gate 	pw->pw_uid = pwuid;
3607c478bd9Sstevel@tonic-gate 	pw->pw_gid = pwgid;
3617c478bd9Sstevel@tonic-gate 	pw->pw_gecos = gecos;
3627c478bd9Sstevel@tonic-gate 	pw->pw_dir = dir;
3637c478bd9Sstevel@tonic-gate 	pw->pw_shell = shell;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_CHANGE
3667c478bd9Sstevel@tonic-gate 	pw->pw_change = pwchange;
3677c478bd9Sstevel@tonic-gate #endif
3687c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_CLASS
3697c478bd9Sstevel@tonic-gate 	pw->pw_class = class;
3707c478bd9Sstevel@tonic-gate #endif
3717c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_EXPIRE
3727c478bd9Sstevel@tonic-gate 	pw->pw_expire = pwexpire;
3737c478bd9Sstevel@tonic-gate #endif
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	return (0);
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate  error:
3787c478bd9Sstevel@tonic-gate 	errno = myerrno;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	if (name != NULL) free(name);
3817c478bd9Sstevel@tonic-gate 	if (pass != NULL) free(pass);
3827c478bd9Sstevel@tonic-gate 	if (gecos != NULL) free(gecos);
3837c478bd9Sstevel@tonic-gate 	if (dir != NULL) free(dir);
3847c478bd9Sstevel@tonic-gate 	if (shell != NULL) free(shell);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	return (-1);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /* ------------------------- struct passwd ------------------------- */
3907c478bd9Sstevel@tonic-gate #endif /* WANT_IRS_PW */
3917c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct group +++++++++++++++++++++++++ */
3927c478bd9Sstevel@tonic-gate 
393*9525b14bSRao Shoaib /*%
3947c478bd9Sstevel@tonic-gate  * int irp_marshall_gr(const struct group *gr, char **buffer, size_t *len)
3957c478bd9Sstevel@tonic-gate  *
396*9525b14bSRao Shoaib  * notes: \li
3977c478bd9Sstevel@tonic-gate  *
398*9525b14bSRao Shoaib  *	See irpmarshall.h.
3997c478bd9Sstevel@tonic-gate  *
400*9525b14bSRao Shoaib  * return: \li
4017c478bd9Sstevel@tonic-gate  *
4027c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure
4037c478bd9Sstevel@tonic-gate  */
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate int
irp_marshall_gr(const struct group * gr,char ** buffer,size_t * len)4067c478bd9Sstevel@tonic-gate irp_marshall_gr(const struct group *gr, char **buffer, size_t *len) {
407*9525b14bSRao Shoaib 	size_t need = 1;	/*%< for null byte */
4087c478bd9Sstevel@tonic-gate 	char grGid[24];
4097c478bd9Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if (gr == NULL || len == NULL) {
4127c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4137c478bd9Sstevel@tonic-gate 		return (-1);
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	sprintf(grGid, "%ld", (long)gr->gr_gid);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	need += strlen(gr->gr_name) + 1;
4197c478bd9Sstevel@tonic-gate #ifndef MISSING_GR_PASSWD
4207c478bd9Sstevel@tonic-gate 	need += strlen(gr->gr_passwd) + 1;
4217c478bd9Sstevel@tonic-gate #else
4227c478bd9Sstevel@tonic-gate 	need++;
4237c478bd9Sstevel@tonic-gate #endif
4247c478bd9Sstevel@tonic-gate 	need += strlen(grGid) + 1;
4257c478bd9Sstevel@tonic-gate 	need += joinlength(gr->gr_mem) + 1;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
4287c478bd9Sstevel@tonic-gate 		*len = need;
4297c478bd9Sstevel@tonic-gate 		return (0);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
4337c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4347c478bd9Sstevel@tonic-gate 		return (-1);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
438*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
4397c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
4407c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
4417c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
4427c478bd9Sstevel@tonic-gate 			return (-1);
4437c478bd9Sstevel@tonic-gate 		}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		*len = need;
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	strcpy(*buffer, gr->gr_name);		strcat(*buffer, fieldsep);
4497c478bd9Sstevel@tonic-gate #ifndef MISSING_GR_PASSWD
4507c478bd9Sstevel@tonic-gate 	strcat(*buffer, gr->gr_passwd);
4517c478bd9Sstevel@tonic-gate #endif
4527c478bd9Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
4537c478bd9Sstevel@tonic-gate 	strcat(*buffer, grGid);			strcat(*buffer, fieldsep);
4547c478bd9Sstevel@tonic-gate 	joinarray(gr->gr_mem, *buffer, COMMA) ;	strcat(*buffer, fieldsep);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	return (0);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
459*9525b14bSRao Shoaib /*%
4607c478bd9Sstevel@tonic-gate  * int irp_unmarshall_gr(struct group *gr, char *buffer)
4617c478bd9Sstevel@tonic-gate  *
462*9525b14bSRao Shoaib  * notes: \li
4637c478bd9Sstevel@tonic-gate  *
464*9525b14bSRao Shoaib  *	See irpmarshall.h
4657c478bd9Sstevel@tonic-gate  *
466*9525b14bSRao Shoaib  * return: \li
4677c478bd9Sstevel@tonic-gate  *
4687c478bd9Sstevel@tonic-gate  *	0 on success and -1 on failure.
4697c478bd9Sstevel@tonic-gate  *
4707c478bd9Sstevel@tonic-gate  */
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate int
irp_unmarshall_gr(struct group * gr,char * buffer)4737c478bd9Sstevel@tonic-gate irp_unmarshall_gr(struct group *gr, char *buffer) {
4747c478bd9Sstevel@tonic-gate 	char *p, *q;
4757c478bd9Sstevel@tonic-gate 	gid_t grgid;
4767c478bd9Sstevel@tonic-gate 	long t;
4777c478bd9Sstevel@tonic-gate 	char *name = NULL;
4787c478bd9Sstevel@tonic-gate 	char *pass = NULL;
4797c478bd9Sstevel@tonic-gate 	char **members = NULL;
4807c478bd9Sstevel@tonic-gate 	char tmpbuf[24];
4817c478bd9Sstevel@tonic-gate 	char *tb;
4827c478bd9Sstevel@tonic-gate 	char fieldsep = ':';
4837c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if (gr == NULL || buffer == NULL) {
4867c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4877c478bd9Sstevel@tonic-gate 		return (-1);
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	p = buffer;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	/* gr_name field */
4937c478bd9Sstevel@tonic-gate 	name = NULL;
494*9525b14bSRao Shoaib 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
4957c478bd9Sstevel@tonic-gate 		goto error;
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	/* gr_passwd field */
5007c478bd9Sstevel@tonic-gate 	pass = NULL;
5017c478bd9Sstevel@tonic-gate 	if (getfield(&pass, 0, &p, fieldsep) == NULL) {
5027c478bd9Sstevel@tonic-gate 		goto error;
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	/* gr_gid field */
5077c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
5087c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
509*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
5107c478bd9Sstevel@tonic-gate 		goto error;
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
5137c478bd9Sstevel@tonic-gate 	if (*tb) {
514*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 	grgid = (gid_t)t;
517*9525b14bSRao Shoaib 	if ((long) grgid != t) {	/*%< value must have been too big. */
5187c478bd9Sstevel@tonic-gate 		goto error;
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	/* gr_mem field. Member names are separated by commas */
5237c478bd9Sstevel@tonic-gate 	q = strchr(p, fieldsep);
5247c478bd9Sstevel@tonic-gate 	if (q == NULL) {
5257c478bd9Sstevel@tonic-gate 		goto error;
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 	members = splitarray(p, q, COMMA);
5287c478bd9Sstevel@tonic-gate 	if (members == NULL) {
5297c478bd9Sstevel@tonic-gate 		myerrno = errno;
5307c478bd9Sstevel@tonic-gate 		goto error;
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 	p = q + 1;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	gr->gr_name = name;
5367c478bd9Sstevel@tonic-gate #ifndef MISSING_GR_PASSWD
5377c478bd9Sstevel@tonic-gate 	gr->gr_passwd = pass;
5387c478bd9Sstevel@tonic-gate #endif
5397c478bd9Sstevel@tonic-gate 	gr->gr_gid = grgid;
5407c478bd9Sstevel@tonic-gate 	gr->gr_mem = members;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	return (0);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate  error:
5457c478bd9Sstevel@tonic-gate 	errno = myerrno;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if (name != NULL) free(name);
5487c478bd9Sstevel@tonic-gate 	if (pass != NULL) free(pass);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	return (-1);
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate /* ------------------------- struct group ------------------------- */
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct servent +++++++++++++++++++++++++ */
5607c478bd9Sstevel@tonic-gate 
561*9525b14bSRao Shoaib /*%
5627c478bd9Sstevel@tonic-gate  * int irp_marshall_sv(const struct servent *sv, char **buffer, size_t *len)
5637c478bd9Sstevel@tonic-gate  *
564*9525b14bSRao Shoaib  * notes: \li
5657c478bd9Sstevel@tonic-gate  *
566*9525b14bSRao Shoaib  *	See irpmarshall.h
5677c478bd9Sstevel@tonic-gate  *
568*9525b14bSRao Shoaib  * return: \li
5697c478bd9Sstevel@tonic-gate  *
5707c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure.
5717c478bd9Sstevel@tonic-gate  *
5727c478bd9Sstevel@tonic-gate  */
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate int
irp_marshall_sv(const struct servent * sv,char ** buffer,size_t * len)5757c478bd9Sstevel@tonic-gate irp_marshall_sv(const struct servent *sv, char **buffer, size_t *len) {
576*9525b14bSRao Shoaib 	size_t need = 1;	/*%< for null byte */
5777c478bd9Sstevel@tonic-gate 	char svPort[24];
5787c478bd9Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
5797c478bd9Sstevel@tonic-gate 	short realport;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	if (sv == NULL || len == NULL) {
5827c478bd9Sstevel@tonic-gate 		errno = EINVAL;
5837c478bd9Sstevel@tonic-gate 		return (-1);
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/* the int s_port field is actually a short in network order. We
5877c478bd9Sstevel@tonic-gate 	   want host order to make the marshalled data look correct */
5887c478bd9Sstevel@tonic-gate 	realport = ntohs((short)sv->s_port);
5897c478bd9Sstevel@tonic-gate 	sprintf(svPort, "%d", realport);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	need += strlen(sv->s_name) + 1;
5927c478bd9Sstevel@tonic-gate 	need += joinlength(sv->s_aliases) + 1;
5937c478bd9Sstevel@tonic-gate 	need += strlen(svPort) + 1;
5947c478bd9Sstevel@tonic-gate 	need += strlen(sv->s_proto) + 1;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
5977c478bd9Sstevel@tonic-gate 		*len = need;
5987c478bd9Sstevel@tonic-gate 		return (0);
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
6027c478bd9Sstevel@tonic-gate 		errno = EINVAL;
6037c478bd9Sstevel@tonic-gate 		return (-1);
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
607*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
6087c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
6097c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
6107c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
6117c478bd9Sstevel@tonic-gate 			return (-1);
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 		*len = need;
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	strcpy(*buffer, sv->s_name);		strcat(*buffer, fieldsep);
6187c478bd9Sstevel@tonic-gate 	joinarray(sv->s_aliases, *buffer, COMMA); strcat(*buffer, fieldsep);
6197c478bd9Sstevel@tonic-gate 	strcat(*buffer, svPort);		strcat(*buffer, fieldsep);
6207c478bd9Sstevel@tonic-gate 	strcat(*buffer, sv->s_proto);		strcat(*buffer, fieldsep);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	return (0);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate 
625*9525b14bSRao Shoaib /*%
6267c478bd9Sstevel@tonic-gate  * int irp_unmarshall_sv(struct servent *sv, char *buffer)
6277c478bd9Sstevel@tonic-gate  *
628*9525b14bSRao Shoaib  * notes: \li
6297c478bd9Sstevel@tonic-gate  *
630*9525b14bSRao Shoaib  *	See irpmarshall.h
6317c478bd9Sstevel@tonic-gate  *
632*9525b14bSRao Shoaib  * return: \li
6337c478bd9Sstevel@tonic-gate  *
6347c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure.
6357c478bd9Sstevel@tonic-gate  *
6367c478bd9Sstevel@tonic-gate  */
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate int
irp_unmarshall_sv(struct servent * sv,char * buffer)6397c478bd9Sstevel@tonic-gate irp_unmarshall_sv(struct servent *sv, char *buffer) {
6407c478bd9Sstevel@tonic-gate 	char *p, *q;
6417c478bd9Sstevel@tonic-gate 	short svport;
6427c478bd9Sstevel@tonic-gate 	long t;
6437c478bd9Sstevel@tonic-gate 	char *name = NULL;
6447c478bd9Sstevel@tonic-gate 	char *proto = NULL;
6457c478bd9Sstevel@tonic-gate 	char **aliases = NULL;
6467c478bd9Sstevel@tonic-gate 	char tmpbuf[24];
6477c478bd9Sstevel@tonic-gate 	char *tb;
6487c478bd9Sstevel@tonic-gate 	char fieldsep = ':';
6497c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	if (sv == NULL || buffer == NULL)
6527c478bd9Sstevel@tonic-gate 		return (-1);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	p = buffer;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	/* s_name field */
6587c478bd9Sstevel@tonic-gate 	name = NULL;
659*9525b14bSRao Shoaib 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
6607c478bd9Sstevel@tonic-gate 		goto error;
6617c478bd9Sstevel@tonic-gate 	}
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	/* s_aliases field */
6657c478bd9Sstevel@tonic-gate 	q = strchr(p, fieldsep);
6667c478bd9Sstevel@tonic-gate 	if (q == NULL) {
6677c478bd9Sstevel@tonic-gate 		goto error;
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
6707c478bd9Sstevel@tonic-gate 	if (aliases == NULL) {
6717c478bd9Sstevel@tonic-gate 		myerrno = errno;
6727c478bd9Sstevel@tonic-gate 		goto error;
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 	p = q + 1;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	/* s_port field */
6787c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
6797c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
680*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
6817c478bd9Sstevel@tonic-gate 		goto error;
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
6847c478bd9Sstevel@tonic-gate 	if (*tb) {
685*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 	svport = (short)t;
688*9525b14bSRao Shoaib 	if ((long) svport != t) {	/*%< value must have been too big. */
6897c478bd9Sstevel@tonic-gate 		goto error;
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 	svport = htons(svport);
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	/* s_proto field */
6947c478bd9Sstevel@tonic-gate 	proto = NULL;
6957c478bd9Sstevel@tonic-gate 	if (getfield(&proto, 0, &p, fieldsep) == NULL) {
6967c478bd9Sstevel@tonic-gate 		goto error;
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	sv->s_name = name;
7007c478bd9Sstevel@tonic-gate 	sv->s_aliases = aliases;
7017c478bd9Sstevel@tonic-gate 	sv->s_port = svport;
7027c478bd9Sstevel@tonic-gate 	sv->s_proto = proto;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	return (0);
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate  error:
7077c478bd9Sstevel@tonic-gate 	errno = myerrno;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	if (name != NULL) free(name);
7107c478bd9Sstevel@tonic-gate 	if (proto != NULL) free(proto);
7117c478bd9Sstevel@tonic-gate 	free_array(aliases, 0);
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	return (-1);
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate /* ------------------------- struct servent ------------------------- */
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct protoent +++++++++++++++++++++++++ */
7207c478bd9Sstevel@tonic-gate 
721*9525b14bSRao Shoaib /*%
7227c478bd9Sstevel@tonic-gate  * int irp_marshall_pr(struct protoent *pr, char **buffer, size_t *len)
7237c478bd9Sstevel@tonic-gate  *
724*9525b14bSRao Shoaib  * notes: \li
7257c478bd9Sstevel@tonic-gate  *
726*9525b14bSRao Shoaib  *	See irpmarshall.h
7277c478bd9Sstevel@tonic-gate  *
728*9525b14bSRao Shoaib  * return: \li
7297c478bd9Sstevel@tonic-gate  *
7307c478bd9Sstevel@tonic-gate  *	0 on success and -1 on failure.
7317c478bd9Sstevel@tonic-gate  *
7327c478bd9Sstevel@tonic-gate  */
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate int
irp_marshall_pr(struct protoent * pr,char ** buffer,size_t * len)7357c478bd9Sstevel@tonic-gate irp_marshall_pr(struct protoent *pr, char **buffer, size_t *len) {
736*9525b14bSRao Shoaib 	size_t need = 1;	/*%< for null byte */
7377c478bd9Sstevel@tonic-gate 	char prProto[24];
7387c478bd9Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	if (pr == NULL || len == NULL) {
7417c478bd9Sstevel@tonic-gate 		errno = EINVAL;
7427c478bd9Sstevel@tonic-gate 		return (-1);
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	sprintf(prProto, "%d", (int)pr->p_proto);
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	need += strlen(pr->p_name) + 1;
7487c478bd9Sstevel@tonic-gate 	need += joinlength(pr->p_aliases) + 1;
7497c478bd9Sstevel@tonic-gate 	need += strlen(prProto) + 1;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
7527c478bd9Sstevel@tonic-gate 		*len = need;
7537c478bd9Sstevel@tonic-gate 		return (0);
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
7577c478bd9Sstevel@tonic-gate 		errno = EINVAL;
7587c478bd9Sstevel@tonic-gate 		return (-1);
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
762*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
7637c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
7647c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
7657c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
7667c478bd9Sstevel@tonic-gate 			return (-1);
7677c478bd9Sstevel@tonic-gate 		}
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 		*len = need;
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	strcpy(*buffer, pr->p_name);		strcat(*buffer, fieldsep);
7737c478bd9Sstevel@tonic-gate 	joinarray(pr->p_aliases, *buffer, COMMA); strcat(*buffer, fieldsep);
7747c478bd9Sstevel@tonic-gate 	strcat(*buffer, prProto);		strcat(*buffer, fieldsep);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	return (0);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate }
7797c478bd9Sstevel@tonic-gate 
780*9525b14bSRao Shoaib /*%
7817c478bd9Sstevel@tonic-gate  * int irp_unmarshall_pr(struct protoent *pr, char *buffer)
7827c478bd9Sstevel@tonic-gate  *
783*9525b14bSRao Shoaib  * notes: \li
7847c478bd9Sstevel@tonic-gate  *
785*9525b14bSRao Shoaib  *	See irpmarshall.h
7867c478bd9Sstevel@tonic-gate  *
787*9525b14bSRao Shoaib  * return: \li
7887c478bd9Sstevel@tonic-gate  *
7897c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure
7907c478bd9Sstevel@tonic-gate  *
7917c478bd9Sstevel@tonic-gate  */
7927c478bd9Sstevel@tonic-gate 
irp_unmarshall_pr(struct protoent * pr,char * buffer)7937c478bd9Sstevel@tonic-gate int irp_unmarshall_pr(struct protoent *pr, char *buffer) {
7947c478bd9Sstevel@tonic-gate 	char *p, *q;
7957c478bd9Sstevel@tonic-gate 	int prproto;
7967c478bd9Sstevel@tonic-gate 	long t;
7977c478bd9Sstevel@tonic-gate 	char *name = NULL;
7987c478bd9Sstevel@tonic-gate 	char **aliases = NULL;
7997c478bd9Sstevel@tonic-gate 	char tmpbuf[24];
8007c478bd9Sstevel@tonic-gate 	char *tb;
8017c478bd9Sstevel@tonic-gate 	char fieldsep = ':';
8027c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	if (pr == NULL || buffer == NULL) {
8057c478bd9Sstevel@tonic-gate 		errno = EINVAL;
8067c478bd9Sstevel@tonic-gate 		return (-1);
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	p = buffer;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	/* p_name field */
8127c478bd9Sstevel@tonic-gate 	name = NULL;
813*9525b14bSRao Shoaib 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
8147c478bd9Sstevel@tonic-gate 		goto error;
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	/* p_aliases field */
8197c478bd9Sstevel@tonic-gate 	q = strchr(p, fieldsep);
8207c478bd9Sstevel@tonic-gate 	if (q == NULL) {
8217c478bd9Sstevel@tonic-gate 		goto error;
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
8247c478bd9Sstevel@tonic-gate 	if (aliases == NULL) {
8257c478bd9Sstevel@tonic-gate 		myerrno = errno;
8267c478bd9Sstevel@tonic-gate 		goto error;
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 	p = q + 1;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	/* p_proto field */
8327c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
8337c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
834*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
8357c478bd9Sstevel@tonic-gate 		goto error;
8367c478bd9Sstevel@tonic-gate 	}
8377c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
8387c478bd9Sstevel@tonic-gate 	if (*tb) {
839*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 	prproto = (int)t;
842*9525b14bSRao Shoaib 	if ((long) prproto != t) {	/*%< value must have been too big. */
8437c478bd9Sstevel@tonic-gate 		goto error;
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	pr->p_name = name;
8477c478bd9Sstevel@tonic-gate 	pr->p_aliases = aliases;
8487c478bd9Sstevel@tonic-gate 	pr->p_proto = prproto;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	return (0);
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate  error:
8537c478bd9Sstevel@tonic-gate 	errno = myerrno;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	if (name != NULL) free(name);
8567c478bd9Sstevel@tonic-gate 	free_array(aliases, 0);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	return (-1);
8597c478bd9Sstevel@tonic-gate }
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate /* ------------------------- struct protoent ------------------------- */
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct hostent +++++++++++++++++++++++++ */
8667c478bd9Sstevel@tonic-gate 
867*9525b14bSRao Shoaib /*%
8687c478bd9Sstevel@tonic-gate  * int irp_marshall_ho(struct hostent *ho, char **buffer, size_t *len)
8697c478bd9Sstevel@tonic-gate  *
870*9525b14bSRao Shoaib  * notes: \li
8717c478bd9Sstevel@tonic-gate  *
872*9525b14bSRao Shoaib  *	See irpmarshall.h.
8737c478bd9Sstevel@tonic-gate  *
874*9525b14bSRao Shoaib  * return: \li
8757c478bd9Sstevel@tonic-gate  *
8767c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure.
8777c478bd9Sstevel@tonic-gate  *
8787c478bd9Sstevel@tonic-gate  */
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate int
irp_marshall_ho(struct hostent * ho,char ** buffer,size_t * len)8817c478bd9Sstevel@tonic-gate irp_marshall_ho(struct hostent *ho, char **buffer, size_t *len) {
882*9525b14bSRao Shoaib 	size_t need = 1;	/*%< for null byte */
8837c478bd9Sstevel@tonic-gate 	char hoaddrtype[24];
8847c478bd9Sstevel@tonic-gate 	char holength[24];
8857c478bd9Sstevel@tonic-gate 	char **av;
8867c478bd9Sstevel@tonic-gate 	char *p;
8877c478bd9Sstevel@tonic-gate 	int addrlen;
8887c478bd9Sstevel@tonic-gate 	int malloced = 0;
8897c478bd9Sstevel@tonic-gate 	size_t remlen;
8907c478bd9Sstevel@tonic-gate 	const char *fieldsep = "@";
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	if (ho == NULL || len == NULL) {
8937c478bd9Sstevel@tonic-gate 		errno = EINVAL;
8947c478bd9Sstevel@tonic-gate 		return (-1);
8957c478bd9Sstevel@tonic-gate 	}
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	switch(ho->h_addrtype) {
8987c478bd9Sstevel@tonic-gate 	case AF_INET:
8997c478bd9Sstevel@tonic-gate 		strcpy(hoaddrtype, "AF_INET");
9007c478bd9Sstevel@tonic-gate 		break;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	case AF_INET6:
9037c478bd9Sstevel@tonic-gate 		strcpy(hoaddrtype, "AF_INET6");
9047c478bd9Sstevel@tonic-gate 		break;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	default:
9077c478bd9Sstevel@tonic-gate 		errno = EINVAL;
9087c478bd9Sstevel@tonic-gate 		return (-1);
9097c478bd9Sstevel@tonic-gate 	}
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	sprintf(holength, "%d", ho->h_length);
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	need += strlen(ho->h_name) + 1;
9147c478bd9Sstevel@tonic-gate 	need += joinlength(ho->h_aliases) + 1;
9157c478bd9Sstevel@tonic-gate 	need += strlen(hoaddrtype) + 1;
9167c478bd9Sstevel@tonic-gate 	need += strlen(holength) + 1;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	/* we determine an upper bound on the string length needed, not an
9197c478bd9Sstevel@tonic-gate 	   exact length. */
920*9525b14bSRao Shoaib 	addrlen = (ho->h_addrtype == AF_INET ? 16 : 46) ; /*%< XX other AF's?? */
9217c478bd9Sstevel@tonic-gate 	for (av = ho->h_addr_list; av != NULL && *av != NULL ; av++)
9227c478bd9Sstevel@tonic-gate 		need += addrlen;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
9257c478bd9Sstevel@tonic-gate 		*len = need;
9267c478bd9Sstevel@tonic-gate 		return (0);
9277c478bd9Sstevel@tonic-gate 	}
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
9307c478bd9Sstevel@tonic-gate 		errno = EINVAL;
9317c478bd9Sstevel@tonic-gate 		return (-1);
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
935*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
9367c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
9377c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
9387c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
9397c478bd9Sstevel@tonic-gate 			return (-1);
9407c478bd9Sstevel@tonic-gate 		}
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 		*len = need;
9437c478bd9Sstevel@tonic-gate 		malloced = 1;
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	strcpy(*buffer, ho->h_name);		strcat(*buffer, fieldsep);
9477c478bd9Sstevel@tonic-gate 	joinarray(ho->h_aliases, *buffer, COMMA); strcat(*buffer, fieldsep);
9487c478bd9Sstevel@tonic-gate 	strcat(*buffer, hoaddrtype);		strcat(*buffer, fieldsep);
9497c478bd9Sstevel@tonic-gate 	strcat(*buffer, holength);		strcat(*buffer, fieldsep);
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	p = *buffer + strlen(*buffer);
9527c478bd9Sstevel@tonic-gate 	remlen = need - strlen(*buffer);
9537c478bd9Sstevel@tonic-gate 	for (av = ho->h_addr_list ; av != NULL && *av != NULL ; av++) {
9547c478bd9Sstevel@tonic-gate 		if (inet_ntop(ho->h_addrtype, *av, p, remlen) == NULL) {
9557c478bd9Sstevel@tonic-gate 			goto error;
9567c478bd9Sstevel@tonic-gate 		}
9577c478bd9Sstevel@tonic-gate 		if (*(av + 1) != NULL)
9587c478bd9Sstevel@tonic-gate 			strcat(p, COMMASTR);
9597c478bd9Sstevel@tonic-gate 		remlen -= strlen(p);
9607c478bd9Sstevel@tonic-gate 		p += strlen(p);
9617c478bd9Sstevel@tonic-gate 	}
9627c478bd9Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	return (0);
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate  error:
9677c478bd9Sstevel@tonic-gate 	if (malloced) {
9687c478bd9Sstevel@tonic-gate 		memput(*buffer, need);
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	return (-1);
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate 
974*9525b14bSRao Shoaib /*%
9757c478bd9Sstevel@tonic-gate  * int irp_unmarshall_ho(struct hostent *ho, char *buffer)
9767c478bd9Sstevel@tonic-gate  *
977*9525b14bSRao Shoaib  * notes: \li
9787c478bd9Sstevel@tonic-gate  *
979*9525b14bSRao Shoaib  *	See irpmarshall.h.
9807c478bd9Sstevel@tonic-gate  *
981*9525b14bSRao Shoaib  * return: \li
9827c478bd9Sstevel@tonic-gate  *
9837c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure.
9847c478bd9Sstevel@tonic-gate  *
9857c478bd9Sstevel@tonic-gate  */
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate int
irp_unmarshall_ho(struct hostent * ho,char * buffer)9887c478bd9Sstevel@tonic-gate irp_unmarshall_ho(struct hostent *ho, char *buffer) {
9897c478bd9Sstevel@tonic-gate 	char *p, *q, *r;
9907c478bd9Sstevel@tonic-gate 	int hoaddrtype;
9917c478bd9Sstevel@tonic-gate 	int holength;
9927c478bd9Sstevel@tonic-gate 	long t;
993*9525b14bSRao Shoaib 	char *name;
9947c478bd9Sstevel@tonic-gate 	char **aliases = NULL;
9957c478bd9Sstevel@tonic-gate 	char **hohaddrlist = NULL;
9967c478bd9Sstevel@tonic-gate 	size_t hoaddrsize;
9977c478bd9Sstevel@tonic-gate 	char tmpbuf[24];
9987c478bd9Sstevel@tonic-gate 	char *tb;
9997c478bd9Sstevel@tonic-gate 	char **alist;
10007c478bd9Sstevel@tonic-gate 	int addrcount;
10017c478bd9Sstevel@tonic-gate 	char fieldsep = '@';
10027c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	if (ho == NULL || buffer == NULL) {
10057c478bd9Sstevel@tonic-gate 		errno = EINVAL;
10067c478bd9Sstevel@tonic-gate 		return (-1);
10077c478bd9Sstevel@tonic-gate 	}
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	p = buffer;
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	/* h_name field */
10127c478bd9Sstevel@tonic-gate 	name = NULL;
1013*9525b14bSRao Shoaib 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
10147c478bd9Sstevel@tonic-gate 		goto error;
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	/* h_aliases field */
10197c478bd9Sstevel@tonic-gate 	q = strchr(p, fieldsep);
10207c478bd9Sstevel@tonic-gate 	if (q == NULL) {
10217c478bd9Sstevel@tonic-gate 		goto error;
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
10247c478bd9Sstevel@tonic-gate 	if (aliases == NULL) {
10257c478bd9Sstevel@tonic-gate 		myerrno = errno;
10267c478bd9Sstevel@tonic-gate 		goto error;
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate 	p = q + 1;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	/* h_addrtype field */
10327c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
10337c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1034*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
10357c478bd9Sstevel@tonic-gate 		goto error;
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 	if (strcmp(tmpbuf, "AF_INET") == 0)
10387c478bd9Sstevel@tonic-gate 		hoaddrtype = AF_INET;
10397c478bd9Sstevel@tonic-gate 	else if (strcmp(tmpbuf, "AF_INET6") == 0)
10407c478bd9Sstevel@tonic-gate 		hoaddrtype = AF_INET6;
10417c478bd9Sstevel@tonic-gate 	else
10427c478bd9Sstevel@tonic-gate 		goto error;
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	/* h_length field */
10467c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
10477c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1048*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
10497c478bd9Sstevel@tonic-gate 		goto error;
10507c478bd9Sstevel@tonic-gate 	}
10517c478bd9Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
10527c478bd9Sstevel@tonic-gate 	if (*tb) {
1053*9525b14bSRao Shoaib 		goto error;	/*%< junk in value */
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 	holength = (int)t;
1056*9525b14bSRao Shoaib 	if ((long) holength != t) {	/*%< value must have been too big. */
10577c478bd9Sstevel@tonic-gate 		goto error;
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	/* h_addr_list field */
10627c478bd9Sstevel@tonic-gate 	q = strchr(p, fieldsep);
10637c478bd9Sstevel@tonic-gate 	if (q == NULL)
10647c478bd9Sstevel@tonic-gate 		goto error;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	/* count how many addresss are in there */
10677c478bd9Sstevel@tonic-gate 	if (q > p + 1) {
10687c478bd9Sstevel@tonic-gate 		for (addrcount = 1, r = p ; r != q ; r++) {
10697c478bd9Sstevel@tonic-gate 			if (*r == COMMA)
10707c478bd9Sstevel@tonic-gate 				addrcount++;
10717c478bd9Sstevel@tonic-gate 		}
10727c478bd9Sstevel@tonic-gate 	} else {
10737c478bd9Sstevel@tonic-gate 		addrcount = 0;
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	hoaddrsize = (addrcount + 1) * sizeof (char *);
10777c478bd9Sstevel@tonic-gate 	hohaddrlist = malloc(hoaddrsize);
10787c478bd9Sstevel@tonic-gate 	if (hohaddrlist == NULL) {
10797c478bd9Sstevel@tonic-gate 		myerrno = ENOMEM;
10807c478bd9Sstevel@tonic-gate 		goto error;
10817c478bd9Sstevel@tonic-gate 	}
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	memset(hohaddrlist, 0x0, hoaddrsize);
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	alist = hohaddrlist;
10867c478bd9Sstevel@tonic-gate 	for (t = 0, r = p ; r != q ; p = r + 1, t++) {
10877c478bd9Sstevel@tonic-gate 		char saved;
10887c478bd9Sstevel@tonic-gate 		while (r != q && *r != COMMA) r++;
10897c478bd9Sstevel@tonic-gate 		saved = *r;
10907c478bd9Sstevel@tonic-gate 		*r = 0x0;
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 		alist[t] = malloc(hoaddrtype == AF_INET ? 4 : 16);
10937c478bd9Sstevel@tonic-gate 		if (alist[t] == NULL) {
10947c478bd9Sstevel@tonic-gate 			myerrno = ENOMEM;
10957c478bd9Sstevel@tonic-gate 			goto error;
10967c478bd9Sstevel@tonic-gate 		}
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 		if (inet_pton(hoaddrtype, p, alist[t]) == -1)
10997c478bd9Sstevel@tonic-gate 			goto error;
11007c478bd9Sstevel@tonic-gate 		*r = saved;
11017c478bd9Sstevel@tonic-gate 	}
11027c478bd9Sstevel@tonic-gate 	alist[t] = NULL;
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	ho->h_name = name;
11057c478bd9Sstevel@tonic-gate 	ho->h_aliases = aliases;
11067c478bd9Sstevel@tonic-gate 	ho->h_addrtype = hoaddrtype;
11077c478bd9Sstevel@tonic-gate 	ho->h_length = holength;
11087c478bd9Sstevel@tonic-gate 	ho->h_addr_list = hohaddrlist;
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	return (0);
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate  error:
11137c478bd9Sstevel@tonic-gate 	errno = myerrno;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	if (name != NULL) free(name);
1116*9525b14bSRao Shoaib 	free_array(hohaddrlist, 0);
11177c478bd9Sstevel@tonic-gate 	free_array(aliases, 0);
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	return (-1);
11207c478bd9Sstevel@tonic-gate }
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate /* ------------------------- struct hostent------------------------- */
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct netgrp +++++++++++++++++++++++++ */
11277c478bd9Sstevel@tonic-gate 
1128*9525b14bSRao Shoaib /*%
11297c478bd9Sstevel@tonic-gate  * int irp_marshall_ng(const char *host, const char *user,
11307c478bd9Sstevel@tonic-gate  *		       const char *domain, char *buffer, size_t *len)
11317c478bd9Sstevel@tonic-gate  *
1132*9525b14bSRao Shoaib  * notes: \li
11337c478bd9Sstevel@tonic-gate  *
11347c478bd9Sstevel@tonic-gate  *	See note for irp_marshall_ng_start
11357c478bd9Sstevel@tonic-gate  *
1136*9525b14bSRao Shoaib  * return: \li
11377c478bd9Sstevel@tonic-gate  *
11387c478bd9Sstevel@tonic-gate  *	0 on success, 0 on failure.
11397c478bd9Sstevel@tonic-gate  *
11407c478bd9Sstevel@tonic-gate  */
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate int
irp_marshall_ng(const char * host,const char * user,const char * domain,char ** buffer,size_t * len)11437c478bd9Sstevel@tonic-gate irp_marshall_ng(const char *host, const char *user, const char *domain,
11447c478bd9Sstevel@tonic-gate 		char **buffer, size_t *len) {
1145*9525b14bSRao Shoaib 	size_t need = 1; /*%< for nul byte */
11467c478bd9Sstevel@tonic-gate 	const char *fieldsep = ",";
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	if (len == NULL) {
11497c478bd9Sstevel@tonic-gate 		errno = EINVAL;
11507c478bd9Sstevel@tonic-gate 		return (-1);
11517c478bd9Sstevel@tonic-gate 	}
11527c478bd9Sstevel@tonic-gate 
1153*9525b14bSRao Shoaib 	need += 4;		       /*%< two parens and two commas */
11547c478bd9Sstevel@tonic-gate 	need += (host == NULL ? 0 : strlen(host));
11557c478bd9Sstevel@tonic-gate 	need += (user == NULL ? 0 : strlen(user));
11567c478bd9Sstevel@tonic-gate 	need += (domain == NULL ? 0 : strlen(domain));
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
11597c478bd9Sstevel@tonic-gate 		*len = need;
11607c478bd9Sstevel@tonic-gate 		return (0);
11617c478bd9Sstevel@tonic-gate 	} else if (*buffer != NULL && need > *len) {
11627c478bd9Sstevel@tonic-gate 		errno = EINVAL;
11637c478bd9Sstevel@tonic-gate 		return (-1);
11647c478bd9Sstevel@tonic-gate 	}
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
1167*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
11687c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
11697c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
11707c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
11717c478bd9Sstevel@tonic-gate 			return (-1);
11727c478bd9Sstevel@tonic-gate 		}
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 		*len = need;
11757c478bd9Sstevel@tonic-gate 	}
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	(*buffer)[0] = '(';
11787c478bd9Sstevel@tonic-gate 	(*buffer)[1] = '\0';
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	if (host != NULL)
11817c478bd9Sstevel@tonic-gate 		strcat(*buffer, host);
11827c478bd9Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	if (user != NULL)
11857c478bd9Sstevel@tonic-gate 		strcat(*buffer, user);
11867c478bd9Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	if (domain != NULL)
11897c478bd9Sstevel@tonic-gate 		strcat(*buffer, domain);
11907c478bd9Sstevel@tonic-gate 	strcat(*buffer, ")");
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	return (0);
11937c478bd9Sstevel@tonic-gate }
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate /* ---------- */
11987c478bd9Sstevel@tonic-gate 
1199*9525b14bSRao Shoaib /*%
12007c478bd9Sstevel@tonic-gate  * int irp_unmarshall_ng(const char **host, const char **user,
12017c478bd9Sstevel@tonic-gate  *			 const char **domain, char *buffer)
12027c478bd9Sstevel@tonic-gate  *
1203*9525b14bSRao Shoaib  * notes: \li
12047c478bd9Sstevel@tonic-gate  *
12057c478bd9Sstevel@tonic-gate  *	Unpacks the BUFFER into 3 character arrays it allocates and assigns
12067c478bd9Sstevel@tonic-gate  *	to *HOST, *USER and *DOMAIN. If any field of the value is empty,
12077c478bd9Sstevel@tonic-gate  *	then the corresponding paramater value will be set to NULL.
12087c478bd9Sstevel@tonic-gate  *
1209*9525b14bSRao Shoaib  * return: \li
12107c478bd9Sstevel@tonic-gate  *
12117c478bd9Sstevel@tonic-gate  *	0 on success and -1 on failure.
12127c478bd9Sstevel@tonic-gate  */
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate int
irp_unmarshall_ng(const char ** hostp,const char ** userp,const char ** domainp,char * buffer)12157c478bd9Sstevel@tonic-gate irp_unmarshall_ng(const char **hostp, const char **userp, const char **domainp,
12167c478bd9Sstevel@tonic-gate 		  char *buffer)
12177c478bd9Sstevel@tonic-gate {
12187c478bd9Sstevel@tonic-gate 	char *p, *q;
12197c478bd9Sstevel@tonic-gate 	char fieldsep = ',';
12207c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
12217c478bd9Sstevel@tonic-gate 	char *host, *user, *domain;
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	if (userp == NULL || hostp == NULL ||
12247c478bd9Sstevel@tonic-gate 	    domainp == NULL || buffer == NULL) {
12257c478bd9Sstevel@tonic-gate 		errno = EINVAL;
12267c478bd9Sstevel@tonic-gate 		return (-1);
12277c478bd9Sstevel@tonic-gate 	}
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	host = user = domain = NULL;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	p = buffer;
12327c478bd9Sstevel@tonic-gate 	while (isspace((unsigned char)*p)) {
12337c478bd9Sstevel@tonic-gate 		p++;
12347c478bd9Sstevel@tonic-gate 	}
12357c478bd9Sstevel@tonic-gate 	if (*p != '(') {
12367c478bd9Sstevel@tonic-gate 		goto error;
12377c478bd9Sstevel@tonic-gate 	}
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	q = p + 1;
12407c478bd9Sstevel@tonic-gate 	while (*q && *q != fieldsep)
12417c478bd9Sstevel@tonic-gate 		q++;
12427c478bd9Sstevel@tonic-gate 	if (!*q) {
12437c478bd9Sstevel@tonic-gate 		goto error;
12447c478bd9Sstevel@tonic-gate 	} else if (q > p + 1) {
12457c478bd9Sstevel@tonic-gate 		host = strndup(p, q - p);
12467c478bd9Sstevel@tonic-gate 	}
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	p = q + 1;
12497c478bd9Sstevel@tonic-gate 	if (!*p) {
12507c478bd9Sstevel@tonic-gate 		goto error;
12517c478bd9Sstevel@tonic-gate 	} else if (*p != fieldsep) {
12527c478bd9Sstevel@tonic-gate 		q = p + 1;
12537c478bd9Sstevel@tonic-gate 		while (*q && *q != fieldsep)
12547c478bd9Sstevel@tonic-gate 			q++;
12557c478bd9Sstevel@tonic-gate 		if (!*q) {
12567c478bd9Sstevel@tonic-gate 			goto error;
12577c478bd9Sstevel@tonic-gate 		}
12587c478bd9Sstevel@tonic-gate 		user = strndup(p, q - p);
12597c478bd9Sstevel@tonic-gate 	} else {
12607c478bd9Sstevel@tonic-gate 		p++;
12617c478bd9Sstevel@tonic-gate 	}
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	if (!*p) {
12647c478bd9Sstevel@tonic-gate 		goto error;
12657c478bd9Sstevel@tonic-gate 	} else if (*p != ')') {
12667c478bd9Sstevel@tonic-gate 		q = p + 1;
12677c478bd9Sstevel@tonic-gate 		while (*q && *q != ')')
12687c478bd9Sstevel@tonic-gate 			q++;
12697c478bd9Sstevel@tonic-gate 		if (!*q) {
12707c478bd9Sstevel@tonic-gate 			goto error;
12717c478bd9Sstevel@tonic-gate 		}
12727c478bd9Sstevel@tonic-gate 		domain = strndup(p, q - p);
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate 	*hostp = host;
12757c478bd9Sstevel@tonic-gate 	*userp = user;
12767c478bd9Sstevel@tonic-gate 	*domainp = domain;
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	return (0);
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate  error:
12817c478bd9Sstevel@tonic-gate 	errno = myerrno;
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	if (host != NULL) free(host);
12847c478bd9Sstevel@tonic-gate 	if (user != NULL) free(user);
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	return (-1);
12877c478bd9Sstevel@tonic-gate }
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate /* ------------------------- struct netgrp ------------------------- */
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct nwent +++++++++++++++++++++++++ */
12957c478bd9Sstevel@tonic-gate 
1296*9525b14bSRao Shoaib /*%
12977c478bd9Sstevel@tonic-gate  * int irp_marshall_nw(struct nwent *ne, char **buffer, size_t *len)
12987c478bd9Sstevel@tonic-gate  *
1299*9525b14bSRao Shoaib  * notes: \li
13007c478bd9Sstevel@tonic-gate  *
13017c478bd9Sstevel@tonic-gate  *	See at top.
13027c478bd9Sstevel@tonic-gate  *
1303*9525b14bSRao Shoaib  * return: \li
13047c478bd9Sstevel@tonic-gate  *
13057c478bd9Sstevel@tonic-gate  *	0 on success and -1 on failure.
13067c478bd9Sstevel@tonic-gate  *
13077c478bd9Sstevel@tonic-gate  */
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate int
irp_marshall_nw(struct nwent * ne,char ** buffer,size_t * len)13107c478bd9Sstevel@tonic-gate irp_marshall_nw(struct nwent *ne, char **buffer, size_t *len) {
1311*9525b14bSRao Shoaib 	size_t need = 1;	/*%< for null byte */
13127c478bd9Sstevel@tonic-gate 	char nAddrType[24];
13137c478bd9Sstevel@tonic-gate 	char nNet[MAXPADDRSIZE];
13147c478bd9Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	if (ne == NULL || len == NULL) {
13177c478bd9Sstevel@tonic-gate 		return (-1);
13187c478bd9Sstevel@tonic-gate 	}
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	strcpy(nAddrType, ADDR_T_STR(ne->n_addrtype));
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	if (inet_net_ntop(ne->n_addrtype, ne->n_addr, ne->n_length,
13237c478bd9Sstevel@tonic-gate 			  nNet, sizeof nNet) == NULL) {
13247c478bd9Sstevel@tonic-gate 		return (-1);
13257c478bd9Sstevel@tonic-gate 	}
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	need += strlen(ne->n_name) + 1;
13297c478bd9Sstevel@tonic-gate 	need += joinlength(ne->n_aliases) + 1;
13307c478bd9Sstevel@tonic-gate 	need += strlen(nAddrType) + 1;
13317c478bd9Sstevel@tonic-gate 	need += strlen(nNet) + 1;
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
13347c478bd9Sstevel@tonic-gate 		*len = need;
13357c478bd9Sstevel@tonic-gate 		return (0);
13367c478bd9Sstevel@tonic-gate 	}
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
13397c478bd9Sstevel@tonic-gate 		errno = EINVAL;
13407c478bd9Sstevel@tonic-gate 		return (-1);
13417c478bd9Sstevel@tonic-gate 	}
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
1344*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
13457c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
13467c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
13477c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
13487c478bd9Sstevel@tonic-gate 			return (-1);
13497c478bd9Sstevel@tonic-gate 		}
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 		*len = need;
13527c478bd9Sstevel@tonic-gate 	}
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 	strcpy(*buffer, ne->n_name);		strcat(*buffer, fieldsep);
13557c478bd9Sstevel@tonic-gate 	joinarray(ne->n_aliases, *buffer, COMMA) ; strcat(*buffer, fieldsep);
13567c478bd9Sstevel@tonic-gate 	strcat(*buffer, nAddrType);		strcat(*buffer, fieldsep);
13577c478bd9Sstevel@tonic-gate 	strcat(*buffer, nNet);			strcat(*buffer, fieldsep);
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	return (0);
13607c478bd9Sstevel@tonic-gate }
13617c478bd9Sstevel@tonic-gate 
1362*9525b14bSRao Shoaib /*%
13637c478bd9Sstevel@tonic-gate  * int irp_unmarshall_nw(struct nwent *ne, char *buffer)
13647c478bd9Sstevel@tonic-gate  *
1365*9525b14bSRao Shoaib  * notes: \li
13667c478bd9Sstevel@tonic-gate  *
13677c478bd9Sstevel@tonic-gate  *	See note up top.
13687c478bd9Sstevel@tonic-gate  *
1369*9525b14bSRao Shoaib  * return: \li
13707c478bd9Sstevel@tonic-gate  *
13717c478bd9Sstevel@tonic-gate  *	0 on success and -1 on failure.
13727c478bd9Sstevel@tonic-gate  *
13737c478bd9Sstevel@tonic-gate  */
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate int
irp_unmarshall_nw(struct nwent * ne,char * buffer)13767c478bd9Sstevel@tonic-gate irp_unmarshall_nw(struct nwent *ne, char *buffer) {
13777c478bd9Sstevel@tonic-gate 	char *p, *q;
13787c478bd9Sstevel@tonic-gate 	int naddrtype;
13797c478bd9Sstevel@tonic-gate 	long nnet;
13807c478bd9Sstevel@tonic-gate 	int bits;
13817c478bd9Sstevel@tonic-gate 	char *name = NULL;
13827c478bd9Sstevel@tonic-gate 	char **aliases = NULL;
13837c478bd9Sstevel@tonic-gate 	char tmpbuf[24];
13847c478bd9Sstevel@tonic-gate 	char *tb;
13857c478bd9Sstevel@tonic-gate 	char fieldsep = ':';
13867c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	if (ne == NULL || buffer == NULL) {
13897c478bd9Sstevel@tonic-gate 		goto error;
13907c478bd9Sstevel@tonic-gate 	}
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	p = buffer;
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	/* n_name field */
13957c478bd9Sstevel@tonic-gate 	name = NULL;
1396*9525b14bSRao Shoaib 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
13977c478bd9Sstevel@tonic-gate 		goto error;
13987c478bd9Sstevel@tonic-gate 	}
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	/* n_aliases field. Aliases are separated by commas */
14027c478bd9Sstevel@tonic-gate 	q = strchr(p, fieldsep);
14037c478bd9Sstevel@tonic-gate 	if (q == NULL) {
14047c478bd9Sstevel@tonic-gate 		goto error;
14057c478bd9Sstevel@tonic-gate 	}
14067c478bd9Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
14077c478bd9Sstevel@tonic-gate 	if (aliases == NULL) {
14087c478bd9Sstevel@tonic-gate 		myerrno = errno;
14097c478bd9Sstevel@tonic-gate 		goto error;
14107c478bd9Sstevel@tonic-gate 	}
14117c478bd9Sstevel@tonic-gate 	p = q + 1;
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	/* h_addrtype field */
14157c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
14167c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1417*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
14187c478bd9Sstevel@tonic-gate 		goto error;
14197c478bd9Sstevel@tonic-gate 	}
14207c478bd9Sstevel@tonic-gate 	if (strcmp(tmpbuf, "AF_INET") == 0)
14217c478bd9Sstevel@tonic-gate 		naddrtype = AF_INET;
14227c478bd9Sstevel@tonic-gate 	else if (strcmp(tmpbuf, "AF_INET6") == 0)
14237c478bd9Sstevel@tonic-gate 		naddrtype = AF_INET6;
14247c478bd9Sstevel@tonic-gate 	else
14257c478bd9Sstevel@tonic-gate 		goto error;
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	/* n_net field */
14297c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
14307c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1431*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
14327c478bd9Sstevel@tonic-gate 		goto error;
14337c478bd9Sstevel@tonic-gate 	}
14347c478bd9Sstevel@tonic-gate 	nnet = 0;
14357c478bd9Sstevel@tonic-gate 	bits = inet_net_pton(naddrtype, tmpbuf, &nnet, sizeof nnet);
14367c478bd9Sstevel@tonic-gate 	if (bits < 0) {
14377c478bd9Sstevel@tonic-gate 		goto error;
14387c478bd9Sstevel@tonic-gate 	}
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	/* nnet = ntohl(nnet); */ /* keep in network order for nwent */
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	ne->n_name = name;
14437c478bd9Sstevel@tonic-gate 	ne->n_aliases = aliases;
14447c478bd9Sstevel@tonic-gate 	ne->n_addrtype = naddrtype;
14457c478bd9Sstevel@tonic-gate 	ne->n_length = bits;
14467c478bd9Sstevel@tonic-gate 	ne->n_addr = malloc(sizeof nnet);
14477c478bd9Sstevel@tonic-gate 	if (ne->n_addr == NULL) {
14487c478bd9Sstevel@tonic-gate 		goto error;
14497c478bd9Sstevel@tonic-gate 	}
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	memcpy(ne->n_addr, &nnet, sizeof nnet);
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	return (0);
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate  error:
14567c478bd9Sstevel@tonic-gate 	errno = myerrno;
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	if (name != NULL) free(name);
14597c478bd9Sstevel@tonic-gate 	free_array(aliases, 0);
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	return (-1);
14627c478bd9Sstevel@tonic-gate }
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate /* ------------------------- struct nwent ------------------------- */
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct netent +++++++++++++++++++++++++ */
14697c478bd9Sstevel@tonic-gate 
1470*9525b14bSRao Shoaib /*%
14717c478bd9Sstevel@tonic-gate  * int irp_marshall_ne(struct netent *ne, char **buffer, size_t *len)
14727c478bd9Sstevel@tonic-gate  *
1473*9525b14bSRao Shoaib  * notes: \li
14747c478bd9Sstevel@tonic-gate  *
14757c478bd9Sstevel@tonic-gate  *	See at top.
14767c478bd9Sstevel@tonic-gate  *
1477*9525b14bSRao Shoaib  * return: \li
14787c478bd9Sstevel@tonic-gate  *
14797c478bd9Sstevel@tonic-gate  *	0 on success and -1 on failure.
14807c478bd9Sstevel@tonic-gate  *
14817c478bd9Sstevel@tonic-gate  */
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate int
irp_marshall_ne(struct netent * ne,char ** buffer,size_t * len)14847c478bd9Sstevel@tonic-gate irp_marshall_ne(struct netent *ne, char **buffer, size_t *len) {
1485*9525b14bSRao Shoaib 	size_t need = 1;	/*%< for null byte */
14867c478bd9Sstevel@tonic-gate 	char nAddrType[24];
14877c478bd9Sstevel@tonic-gate 	char nNet[MAXPADDRSIZE];
14887c478bd9Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
14897c478bd9Sstevel@tonic-gate 	long nval;
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	if (ne == NULL || len == NULL) {
14927c478bd9Sstevel@tonic-gate 		return (-1);
14937c478bd9Sstevel@tonic-gate 	}
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	strcpy(nAddrType, ADDR_T_STR(ne->n_addrtype));
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	nval = htonl(ne->n_net);
14987c478bd9Sstevel@tonic-gate 	if (inet_ntop(ne->n_addrtype, &nval, nNet, sizeof nNet) == NULL) {
14997c478bd9Sstevel@tonic-gate 		return (-1);
15007c478bd9Sstevel@tonic-gate 	}
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	need += strlen(ne->n_name) + 1;
15037c478bd9Sstevel@tonic-gate 	need += joinlength(ne->n_aliases) + 1;
15047c478bd9Sstevel@tonic-gate 	need += strlen(nAddrType) + 1;
15057c478bd9Sstevel@tonic-gate 	need += strlen(nNet) + 1;
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
15087c478bd9Sstevel@tonic-gate 		*len = need;
15097c478bd9Sstevel@tonic-gate 		return (0);
15107c478bd9Sstevel@tonic-gate 	}
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
15137c478bd9Sstevel@tonic-gate 		errno = EINVAL;
15147c478bd9Sstevel@tonic-gate 		return (-1);
15157c478bd9Sstevel@tonic-gate 	}
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 	if (*buffer == NULL) {
1518*9525b14bSRao Shoaib 		need += 2;		/*%< for CRLF */
15197c478bd9Sstevel@tonic-gate 		*buffer = memget(need);
15207c478bd9Sstevel@tonic-gate 		if (*buffer == NULL) {
15217c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
15227c478bd9Sstevel@tonic-gate 			return (-1);
15237c478bd9Sstevel@tonic-gate 		}
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 		*len = need;
15267c478bd9Sstevel@tonic-gate 	}
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	strcpy(*buffer, ne->n_name);		strcat(*buffer, fieldsep);
15297c478bd9Sstevel@tonic-gate 	joinarray(ne->n_aliases, *buffer, COMMA) ; strcat(*buffer, fieldsep);
15307c478bd9Sstevel@tonic-gate 	strcat(*buffer, nAddrType);		strcat(*buffer, fieldsep);
15317c478bd9Sstevel@tonic-gate 	strcat(*buffer, nNet);			strcat(*buffer, fieldsep);
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	return (0);
15347c478bd9Sstevel@tonic-gate }
15357c478bd9Sstevel@tonic-gate 
1536*9525b14bSRao Shoaib /*%
15377c478bd9Sstevel@tonic-gate  * int irp_unmarshall_ne(struct netent *ne, char *buffer)
15387c478bd9Sstevel@tonic-gate  *
1539*9525b14bSRao Shoaib  * notes: \li
15407c478bd9Sstevel@tonic-gate  *
15417c478bd9Sstevel@tonic-gate  *	See note up top.
15427c478bd9Sstevel@tonic-gate  *
1543*9525b14bSRao Shoaib  * return: \li
15447c478bd9Sstevel@tonic-gate  *
15457c478bd9Sstevel@tonic-gate  *	0 on success and -1 on failure.
15467c478bd9Sstevel@tonic-gate  *
15477c478bd9Sstevel@tonic-gate  */
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate int
irp_unmarshall_ne(struct netent * ne,char * buffer)15507c478bd9Sstevel@tonic-gate irp_unmarshall_ne(struct netent *ne, char *buffer) {
15517c478bd9Sstevel@tonic-gate 	char *p, *q;
15527c478bd9Sstevel@tonic-gate 	int naddrtype;
15537c478bd9Sstevel@tonic-gate 	long nnet;
15547c478bd9Sstevel@tonic-gate 	int bits;
15557c478bd9Sstevel@tonic-gate 	char *name = NULL;
15567c478bd9Sstevel@tonic-gate 	char **aliases = NULL;
15577c478bd9Sstevel@tonic-gate 	char tmpbuf[24];
15587c478bd9Sstevel@tonic-gate 	char *tb;
15597c478bd9Sstevel@tonic-gate 	char fieldsep = ':';
15607c478bd9Sstevel@tonic-gate 	int myerrno = EINVAL;
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	if (ne == NULL || buffer == NULL) {
15637c478bd9Sstevel@tonic-gate 		goto error;
15647c478bd9Sstevel@tonic-gate 	}
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	p = buffer;
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	/* n_name field */
15697c478bd9Sstevel@tonic-gate 	name = NULL;
1570*9525b14bSRao Shoaib 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
15717c478bd9Sstevel@tonic-gate 		goto error;
15727c478bd9Sstevel@tonic-gate 	}
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	/* n_aliases field. Aliases are separated by commas */
15767c478bd9Sstevel@tonic-gate 	q = strchr(p, fieldsep);
15777c478bd9Sstevel@tonic-gate 	if (q == NULL) {
15787c478bd9Sstevel@tonic-gate 		goto error;
15797c478bd9Sstevel@tonic-gate 	}
15807c478bd9Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
15817c478bd9Sstevel@tonic-gate 	if (aliases == NULL) {
15827c478bd9Sstevel@tonic-gate 		myerrno = errno;
15837c478bd9Sstevel@tonic-gate 		goto error;
15847c478bd9Sstevel@tonic-gate 	}
15857c478bd9Sstevel@tonic-gate 	p = q + 1;
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	/* h_addrtype field */
15897c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
15907c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1591*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
15927c478bd9Sstevel@tonic-gate 		goto error;
15937c478bd9Sstevel@tonic-gate 	}
15947c478bd9Sstevel@tonic-gate 	if (strcmp(tmpbuf, "AF_INET") == 0)
15957c478bd9Sstevel@tonic-gate 		naddrtype = AF_INET;
15967c478bd9Sstevel@tonic-gate 	else if (strcmp(tmpbuf, "AF_INET6") == 0)
15977c478bd9Sstevel@tonic-gate 		naddrtype = AF_INET6;
15987c478bd9Sstevel@tonic-gate 	else
15997c478bd9Sstevel@tonic-gate 		goto error;
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 	/* n_net field */
16037c478bd9Sstevel@tonic-gate 	tb = tmpbuf;
16047c478bd9Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1605*9525b14bSRao Shoaib 	    strlen(tb) == 0U) {
16067c478bd9Sstevel@tonic-gate 		goto error;
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 	bits = inet_net_pton(naddrtype, tmpbuf, &nnet, sizeof nnet);
16097c478bd9Sstevel@tonic-gate 	if (bits < 0) {
16107c478bd9Sstevel@tonic-gate 		goto error;
16117c478bd9Sstevel@tonic-gate 	}
16127c478bd9Sstevel@tonic-gate 	nnet = ntohl(nnet);
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	ne->n_name = name;
16157c478bd9Sstevel@tonic-gate 	ne->n_aliases = aliases;
16167c478bd9Sstevel@tonic-gate 	ne->n_addrtype = naddrtype;
16177c478bd9Sstevel@tonic-gate 	ne->n_net = nnet;
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	return (0);
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate  error:
16227c478bd9Sstevel@tonic-gate 	errno = myerrno;
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	if (name != NULL) free(name);
16257c478bd9Sstevel@tonic-gate 	free_array(aliases, 0);
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	return (-1);
16287c478bd9Sstevel@tonic-gate }
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate /* ------------------------- struct netent ------------------------- */
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate /* =========================================================================== */
16357c478bd9Sstevel@tonic-gate 
1636*9525b14bSRao Shoaib /*%
16377c478bd9Sstevel@tonic-gate  * static char ** splitarray(const char *buffer, const char *buffend, char delim)
16387c478bd9Sstevel@tonic-gate  *
1639*9525b14bSRao Shoaib  * notes: \li
16407c478bd9Sstevel@tonic-gate  *
16417c478bd9Sstevel@tonic-gate  *	Split a delim separated astring. Not allowed
16427c478bd9Sstevel@tonic-gate  *	to have two delims next to each other. BUFFER points to begining of
16437c478bd9Sstevel@tonic-gate  *	string, BUFFEND points to one past the end of the string
16447c478bd9Sstevel@tonic-gate  *	(i.e. points at where the null byte would be if null
16457c478bd9Sstevel@tonic-gate  *	terminated).
16467c478bd9Sstevel@tonic-gate  *
1647*9525b14bSRao Shoaib  * return: \li
16487c478bd9Sstevel@tonic-gate  *
16497c478bd9Sstevel@tonic-gate  *	Returns a malloced array of pointers, each pointer pointing to a
16507c478bd9Sstevel@tonic-gate  *	malloced string. If BUFEER is an empty string, then return values is
16517c478bd9Sstevel@tonic-gate  *	array of 1 pointer that is NULL. Returns NULL on failure.
16527c478bd9Sstevel@tonic-gate  *
16537c478bd9Sstevel@tonic-gate  */
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate static char **
splitarray(const char * buffer,const char * buffend,char delim)16567c478bd9Sstevel@tonic-gate splitarray(const char *buffer, const char *buffend, char delim) {
16577c478bd9Sstevel@tonic-gate 	const char *p, *q;
16587c478bd9Sstevel@tonic-gate 	int count = 0;
16597c478bd9Sstevel@tonic-gate 	char **arr = NULL;
16607c478bd9Sstevel@tonic-gate 	char **aptr;
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	if (buffend < buffer)
16637c478bd9Sstevel@tonic-gate 		return (NULL);
16647c478bd9Sstevel@tonic-gate 	else if (buffend > buffer && *buffer == delim)
16657c478bd9Sstevel@tonic-gate 		return (NULL);
16667c478bd9Sstevel@tonic-gate 	else if (buffend > buffer && *(buffend - 1) == delim)
16677c478bd9Sstevel@tonic-gate 		return (NULL);
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	/* count the number of field and make sure none are empty */
16707c478bd9Sstevel@tonic-gate 	if (buffend > buffer + 1) {
16717c478bd9Sstevel@tonic-gate 		for (count = 1, q = buffer ; q != buffend ; q++) {
16727c478bd9Sstevel@tonic-gate 			if (*q == delim) {
16737c478bd9Sstevel@tonic-gate 				if (q > buffer && (*(q - 1) == delim)) {
16747c478bd9Sstevel@tonic-gate 					errno = EINVAL;
16757c478bd9Sstevel@tonic-gate 					return (NULL);
16767c478bd9Sstevel@tonic-gate 				}
16777c478bd9Sstevel@tonic-gate 				count++;
16787c478bd9Sstevel@tonic-gate 			}
16797c478bd9Sstevel@tonic-gate 		}
16807c478bd9Sstevel@tonic-gate 	}
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	if (count > 0) {
1683*9525b14bSRao Shoaib 		count++ ;		/*%< for NULL at end */
16847c478bd9Sstevel@tonic-gate 		aptr = arr = malloc(count * sizeof (char *));
16857c478bd9Sstevel@tonic-gate 		if (aptr == NULL) {
16867c478bd9Sstevel@tonic-gate 			 errno = ENOMEM;
16877c478bd9Sstevel@tonic-gate 			 return (NULL);
16887c478bd9Sstevel@tonic-gate 		 }
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 		memset(arr, 0x0, count * sizeof (char *));
16917c478bd9Sstevel@tonic-gate 		for (p = buffer ; p < buffend ; p++) {
16927c478bd9Sstevel@tonic-gate 			for (q = p ; *q != delim && q != buffend ; q++)
16937c478bd9Sstevel@tonic-gate 				/* nothing */;
16947c478bd9Sstevel@tonic-gate 			*aptr = strndup(p, q - p);
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 			p = q;
16977c478bd9Sstevel@tonic-gate 			aptr++;
16987c478bd9Sstevel@tonic-gate 		}
16997c478bd9Sstevel@tonic-gate 		*aptr = NULL;
17007c478bd9Sstevel@tonic-gate 	} else {
17017c478bd9Sstevel@tonic-gate 		arr = malloc(sizeof (char *));
17027c478bd9Sstevel@tonic-gate 		if (arr == NULL) {
17037c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
17047c478bd9Sstevel@tonic-gate 			return (NULL);
17057c478bd9Sstevel@tonic-gate 		}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 		*arr = NULL;
17087c478bd9Sstevel@tonic-gate 	}
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	return (arr);
17117c478bd9Sstevel@tonic-gate }
17127c478bd9Sstevel@tonic-gate 
1713*9525b14bSRao Shoaib /*%
17147c478bd9Sstevel@tonic-gate  * static size_t joinlength(char * const *argv)
17157c478bd9Sstevel@tonic-gate  *
1716*9525b14bSRao Shoaib  * return: \li
17177c478bd9Sstevel@tonic-gate  *
17187c478bd9Sstevel@tonic-gate  *	the number of bytes in all the arrays pointed at
17197c478bd9Sstevel@tonic-gate  *	by argv, including their null bytes(which will usually be turned
17207c478bd9Sstevel@tonic-gate  *	into commas).
17217c478bd9Sstevel@tonic-gate  *
17227c478bd9Sstevel@tonic-gate  *
17237c478bd9Sstevel@tonic-gate  */
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate static size_t
joinlength(char * const * argv)17267c478bd9Sstevel@tonic-gate joinlength(char * const *argv) {
17277c478bd9Sstevel@tonic-gate 	int len = 0;
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	while (argv && *argv) {
17307c478bd9Sstevel@tonic-gate 		len += (strlen(*argv) + 1);
17317c478bd9Sstevel@tonic-gate 		argv++;
17327c478bd9Sstevel@tonic-gate 	}
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	return (len);
17357c478bd9Sstevel@tonic-gate }
17367c478bd9Sstevel@tonic-gate 
1737*9525b14bSRao Shoaib /*%
17387c478bd9Sstevel@tonic-gate  * int joinarray(char * const *argv, char *buffer, char delim)
17397c478bd9Sstevel@tonic-gate  *
1740*9525b14bSRao Shoaib  * notes: \li
17417c478bd9Sstevel@tonic-gate  *
17427c478bd9Sstevel@tonic-gate  *	Copy all the ARGV strings into the end of BUFFER
17437c478bd9Sstevel@tonic-gate  *	separating them with DELIM.  BUFFER is assumed to have
17447c478bd9Sstevel@tonic-gate  *	enough space to hold everything and to be already null-terminated.
17457c478bd9Sstevel@tonic-gate  *
1746*9525b14bSRao Shoaib  * return: \li
17477c478bd9Sstevel@tonic-gate  *
17487c478bd9Sstevel@tonic-gate  *	0 unless argv or buffer is NULL.
17497c478bd9Sstevel@tonic-gate  *
17507c478bd9Sstevel@tonic-gate  *
17517c478bd9Sstevel@tonic-gate  */
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate static int
joinarray(char * const * argv,char * buffer,char delim)17547c478bd9Sstevel@tonic-gate joinarray(char * const *argv, char *buffer, char delim) {
17557c478bd9Sstevel@tonic-gate 	char * const *p;
17567c478bd9Sstevel@tonic-gate 	char sep[2];
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	if (argv == NULL || buffer == NULL) {
17597c478bd9Sstevel@tonic-gate 		errno = EINVAL;
17607c478bd9Sstevel@tonic-gate 		return (-1);
17617c478bd9Sstevel@tonic-gate 	}
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	sep[0] = delim;
17647c478bd9Sstevel@tonic-gate 	sep[1] = 0x0;
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	for (p = argv ; *p != NULL ; p++) {
17677c478bd9Sstevel@tonic-gate 		strcat(buffer, *p);
17687c478bd9Sstevel@tonic-gate 		if (*(p + 1) != NULL) {
17697c478bd9Sstevel@tonic-gate 			strcat(buffer, sep);
17707c478bd9Sstevel@tonic-gate 		}
17717c478bd9Sstevel@tonic-gate 	}
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	return (0);
17747c478bd9Sstevel@tonic-gate }
17757c478bd9Sstevel@tonic-gate 
1776*9525b14bSRao Shoaib /*%
17777c478bd9Sstevel@tonic-gate  * static char * getfield(char **res, size_t reslen, char **ptr, char delim)
17787c478bd9Sstevel@tonic-gate  *
1779*9525b14bSRao Shoaib  * notes: \li
17807c478bd9Sstevel@tonic-gate  *
17817c478bd9Sstevel@tonic-gate  *	Stores in *RES, which is a buffer of length RESLEN, a
17827c478bd9Sstevel@tonic-gate  *	copy of the bytes from *PTR up to and including the first
17837c478bd9Sstevel@tonic-gate  *	instance of DELIM. If *RES is NULL, then it will be
17847c478bd9Sstevel@tonic-gate  *	assigned a malloced buffer to hold the copy. *PTR is
17857c478bd9Sstevel@tonic-gate  *	modified to point at the found delimiter.
17867c478bd9Sstevel@tonic-gate  *
1787*9525b14bSRao Shoaib  * return: \li
17887c478bd9Sstevel@tonic-gate  *
17897c478bd9Sstevel@tonic-gate  *	If there was no delimiter, then NULL is returned,
17907c478bd9Sstevel@tonic-gate  *	otherewise *RES is returned.
17917c478bd9Sstevel@tonic-gate  *
17927c478bd9Sstevel@tonic-gate  */
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate static char *
getfield(char ** res,size_t reslen,char ** ptr,char delim)17957c478bd9Sstevel@tonic-gate getfield(char **res, size_t reslen, char **ptr, char delim) {
17967c478bd9Sstevel@tonic-gate 	char *q;
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 	if (res == NULL || ptr == NULL || *ptr == NULL) {
17997c478bd9Sstevel@tonic-gate 		errno = EINVAL;
18007c478bd9Sstevel@tonic-gate 		return (NULL);
18017c478bd9Sstevel@tonic-gate 	}
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	q = strchr(*ptr, delim);
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	if (q == NULL) {
18067c478bd9Sstevel@tonic-gate 		errno = EINVAL;
18077c478bd9Sstevel@tonic-gate 		return (NULL);
18087c478bd9Sstevel@tonic-gate 	} else {
18097c478bd9Sstevel@tonic-gate 		if (*res == NULL) {
18107c478bd9Sstevel@tonic-gate 			*res = strndup(*ptr, q - *ptr);
18117c478bd9Sstevel@tonic-gate 		} else {
1812*9525b14bSRao Shoaib 			if ((size_t)(q - *ptr + 1) > reslen) { /*%< to big for res */
18137c478bd9Sstevel@tonic-gate 				errno = EINVAL;
18147c478bd9Sstevel@tonic-gate 				return (NULL);
18157c478bd9Sstevel@tonic-gate 			} else {
18167c478bd9Sstevel@tonic-gate 				strncpy(*res, *ptr, q - *ptr);
18177c478bd9Sstevel@tonic-gate 				(*res)[q - *ptr] = 0x0;
18187c478bd9Sstevel@tonic-gate 			}
18197c478bd9Sstevel@tonic-gate 		}
18207c478bd9Sstevel@tonic-gate 		*ptr = q + 1;
18217c478bd9Sstevel@tonic-gate 	}
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	return (*res);
18247c478bd9Sstevel@tonic-gate }
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 
1830*9525b14bSRao Shoaib #ifndef HAVE_STRNDUP
18317c478bd9Sstevel@tonic-gate /*
18327c478bd9Sstevel@tonic-gate  * static char * strndup(const char *str, size_t len)
18337c478bd9Sstevel@tonic-gate  *
1834*9525b14bSRao Shoaib  * notes: \li
18357c478bd9Sstevel@tonic-gate  *
18367c478bd9Sstevel@tonic-gate  *	like strdup, except do len bytes instead of the whole string. Always
18377c478bd9Sstevel@tonic-gate  *	null-terminates.
18387c478bd9Sstevel@tonic-gate  *
1839*9525b14bSRao Shoaib  * return: \li
18407c478bd9Sstevel@tonic-gate  *
18417c478bd9Sstevel@tonic-gate  *	The newly malloced string.
18427c478bd9Sstevel@tonic-gate  *
18437c478bd9Sstevel@tonic-gate  */
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate static char *
strndup(const char * str,size_t len)18467c478bd9Sstevel@tonic-gate strndup(const char *str, size_t len) {
18477c478bd9Sstevel@tonic-gate 	char *p = malloc(len + 1);
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	if (p == NULL)
18507c478bd9Sstevel@tonic-gate 		return (NULL);
18517c478bd9Sstevel@tonic-gate 	strncpy(p, str, len);
18527c478bd9Sstevel@tonic-gate 	p[len] = 0x0;
18537c478bd9Sstevel@tonic-gate 	return (p);
18547c478bd9Sstevel@tonic-gate }
1855*9525b14bSRao Shoaib #endif
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate #if WANT_MAIN
18587c478bd9Sstevel@tonic-gate 
1859*9525b14bSRao Shoaib /*%
18607c478bd9Sstevel@tonic-gate  * static int strcmp_nws(const char *a, const char *b)
18617c478bd9Sstevel@tonic-gate  *
1862*9525b14bSRao Shoaib  * notes: \li
18637c478bd9Sstevel@tonic-gate  *
18647c478bd9Sstevel@tonic-gate  *	do a strcmp, except uneven lengths of whitespace compare the same
18657c478bd9Sstevel@tonic-gate  *
1866*9525b14bSRao Shoaib  * return: \li
18677c478bd9Sstevel@tonic-gate  *
18687c478bd9Sstevel@tonic-gate  */
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate static int
strcmp_nws(const char * a,const char * b)18717c478bd9Sstevel@tonic-gate strcmp_nws(const char *a, const char *b) {
18727c478bd9Sstevel@tonic-gate 	while (*a && *b) {
18737c478bd9Sstevel@tonic-gate 		if (isspace(*a) && isspace(*b)) {
18747c478bd9Sstevel@tonic-gate 			do {
18757c478bd9Sstevel@tonic-gate 				a++;
18767c478bd9Sstevel@tonic-gate 			} while (isspace(*a));
18777c478bd9Sstevel@tonic-gate 			do {
18787c478bd9Sstevel@tonic-gate 				b++;
18797c478bd9Sstevel@tonic-gate 			} while (isspace(*b));
18807c478bd9Sstevel@tonic-gate 		}
18817c478bd9Sstevel@tonic-gate 		if (*a < *b)
18827c478bd9Sstevel@tonic-gate 			return (-1);
18837c478bd9Sstevel@tonic-gate 		else if (*a > *b)
18847c478bd9Sstevel@tonic-gate 			return (1);
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 		a++;
18877c478bd9Sstevel@tonic-gate 		b++;;
18887c478bd9Sstevel@tonic-gate 	}
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	if (*a == *b)
18917c478bd9Sstevel@tonic-gate 		return (0);
18927c478bd9Sstevel@tonic-gate 	else if (*a > *b)
18937c478bd9Sstevel@tonic-gate 		return (1);
18947c478bd9Sstevel@tonic-gate 	else
18957c478bd9Sstevel@tonic-gate 		return (-1);
18967c478bd9Sstevel@tonic-gate }
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate #endif
18997c478bd9Sstevel@tonic-gate 
1900*9525b14bSRao Shoaib /*%
19017c478bd9Sstevel@tonic-gate  * static void free_array(char **argv, size_t entries)
19027c478bd9Sstevel@tonic-gate  *
1903*9525b14bSRao Shoaib  * notes: \li
19047c478bd9Sstevel@tonic-gate  *
19057c478bd9Sstevel@tonic-gate  *	Free argv and each of the pointers inside it. The end of
19067c478bd9Sstevel@tonic-gate  *	the array is when a NULL pointer is found inside. If
19077c478bd9Sstevel@tonic-gate  *	entries is > 0, then NULL pointers inside the array do
19087c478bd9Sstevel@tonic-gate  *	not indicate the end of the array.
19097c478bd9Sstevel@tonic-gate  *
19107c478bd9Sstevel@tonic-gate  */
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate static void
free_array(char ** argv,size_t entries)19137c478bd9Sstevel@tonic-gate free_array(char **argv, size_t entries) {
19147c478bd9Sstevel@tonic-gate 	char **p = argv;
1915*9525b14bSRao Shoaib 	int useEntries = (entries > 0U);
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	if (argv == NULL)
19187c478bd9Sstevel@tonic-gate 		return;
19197c478bd9Sstevel@tonic-gate 
1920*9525b14bSRao Shoaib 	while ((useEntries && entries > 0U) || *p) {
19217c478bd9Sstevel@tonic-gate 		if (*p)
19227c478bd9Sstevel@tonic-gate 			free(*p);
19237c478bd9Sstevel@tonic-gate 		p++;
19247c478bd9Sstevel@tonic-gate 		if (useEntries)
19257c478bd9Sstevel@tonic-gate 			entries--;
19267c478bd9Sstevel@tonic-gate 	}
19277c478bd9Sstevel@tonic-gate 	free(argv);
19287c478bd9Sstevel@tonic-gate }
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate /* ************************************************** */
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate #if WANT_MAIN
19377c478bd9Sstevel@tonic-gate 
1938*9525b14bSRao Shoaib /*% takes an option to indicate what sort of marshalling(read the code) and
19397c478bd9Sstevel@tonic-gate    an argument. If the argument looks like a marshalled buffer(has a ':'
19407c478bd9Sstevel@tonic-gate    embedded) then it's unmarshalled and the remarshalled and the new string
19417c478bd9Sstevel@tonic-gate    is compared to the old one.
19427c478bd9Sstevel@tonic-gate */
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)19457c478bd9Sstevel@tonic-gate main(int argc, char **argv) {
19467c478bd9Sstevel@tonic-gate 	char buffer[1024];
19477c478bd9Sstevel@tonic-gate 	char *b = &buffer[0];
19487c478bd9Sstevel@tonic-gate 	size_t len = sizeof buffer;
19497c478bd9Sstevel@tonic-gate 	char option;
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	if (argc < 2 || argv[1][0] != '-')
19527c478bd9Sstevel@tonic-gate 		exit(1);
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	option = argv[1][1];
19557c478bd9Sstevel@tonic-gate 	argv++;
19567c478bd9Sstevel@tonic-gate 	argc--;
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate #if 0
19607c478bd9Sstevel@tonic-gate 	{
19617c478bd9Sstevel@tonic-gate 		char buff[10];
19627c478bd9Sstevel@tonic-gate 		char *p = argv[1], *q = &buff[0];
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 		while (getfield(&q, sizeof buff, &p, ':') != NULL) {
19657c478bd9Sstevel@tonic-gate 			printf("field: \"%s\"\n", q);
19667c478bd9Sstevel@tonic-gate 			p++;
19677c478bd9Sstevel@tonic-gate 		}
19687c478bd9Sstevel@tonic-gate 		printf("p is now \"%s\"\n", p);
19697c478bd9Sstevel@tonic-gate 	}
19707c478bd9Sstevel@tonic-gate #endif
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate #if 0
19737c478bd9Sstevel@tonic-gate 	{
19747c478bd9Sstevel@tonic-gate 		char **x = splitarray(argv[1], argv[1] + strlen(argv[1]),
19757c478bd9Sstevel@tonic-gate 				      argv[2][0]);
19767c478bd9Sstevel@tonic-gate 		char **p;
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 		if (x == NULL)
19797c478bd9Sstevel@tonic-gate 			printf("split failed\n");
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 		for (p = x ; p != NULL && *p != NULL ; p++) {
19827c478bd9Sstevel@tonic-gate 			printf("\"%s\"\n", *p);
19837c478bd9Sstevel@tonic-gate 		}
19847c478bd9Sstevel@tonic-gate 	}
19857c478bd9Sstevel@tonic-gate #endif
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate #if 1
19887c478bd9Sstevel@tonic-gate 	switch(option) {
19897c478bd9Sstevel@tonic-gate 	case 'n': {
19907c478bd9Sstevel@tonic-gate 		struct nwent ne;
19917c478bd9Sstevel@tonic-gate 		int i;
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
19947c478bd9Sstevel@tonic-gate 			if (irp_unmarshall_nw(&ne, argv[1]) != 0) {
19957c478bd9Sstevel@tonic-gate 				printf("Unmarhsalling failed\n");
19967c478bd9Sstevel@tonic-gate 				exit(1);
19977c478bd9Sstevel@tonic-gate 			}
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 			printf("Name: \"%s\"\n", ne.n_name);
20007c478bd9Sstevel@tonic-gate 			printf("Aliases:");
20017c478bd9Sstevel@tonic-gate 			for (i = 0 ; ne.n_aliases[i] != NULL ; i++)
20027c478bd9Sstevel@tonic-gate 				printf("\n\t\"%s\"", ne.n_aliases[i]);
20037c478bd9Sstevel@tonic-gate 			printf("\nAddrtype: %s\n", ADDR_T_STR(ne.n_addrtype));
20047c478bd9Sstevel@tonic-gate 			inet_net_ntop(ne.n_addrtype, ne.n_addr, ne.n_length,
20057c478bd9Sstevel@tonic-gate 				      buffer, sizeof buffer);
20067c478bd9Sstevel@tonic-gate 			printf("Net: \"%s\"\n", buffer);
20077c478bd9Sstevel@tonic-gate 			*((long*)ne.n_addr) = htonl(*((long*)ne.n_addr));
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("Corrected Net: \"%s\"\n", buffer);
20117c478bd9Sstevel@tonic-gate 		} else {
20127c478bd9Sstevel@tonic-gate 			struct netent *np1 = getnetbyname(argv[1]);
20137c478bd9Sstevel@tonic-gate 			ne.n_name = np1->n_name;
20147c478bd9Sstevel@tonic-gate 			ne.n_aliases = np1->n_aliases;
20157c478bd9Sstevel@tonic-gate 			ne.n_addrtype = np1->n_addrtype;
20167c478bd9Sstevel@tonic-gate 			ne.n_addr = &np1->n_net;
20177c478bd9Sstevel@tonic-gate 			ne.n_length = (IN_CLASSA(np1->n_net) ?
20187c478bd9Sstevel@tonic-gate 				       8 :
20197c478bd9Sstevel@tonic-gate 				       (IN_CLASSB(np1->n_net) ?
20207c478bd9Sstevel@tonic-gate 					16 :
20217c478bd9Sstevel@tonic-gate 					(IN_CLASSC(np1->n_net) ?
20227c478bd9Sstevel@tonic-gate 					 24 : -1)));
20237c478bd9Sstevel@tonic-gate 			np1->n_net = htonl(np1->n_net);
20247c478bd9Sstevel@tonic-gate 			if (irp_marshall_nw(&ne, &b, &len) != 0) {
20257c478bd9Sstevel@tonic-gate 				printf("Marshalling failed\n");
20267c478bd9Sstevel@tonic-gate 			}
20277c478bd9Sstevel@tonic-gate 			printf("%s\n", b);
20287c478bd9Sstevel@tonic-gate 		}
20297c478bd9Sstevel@tonic-gate 		break;
20307c478bd9Sstevel@tonic-gate 	}
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	case 'r': {
20347c478bd9Sstevel@tonic-gate 		char **hosts, **users, **domains;
20357c478bd9Sstevel@tonic-gate 		size_t entries;
20367c478bd9Sstevel@tonic-gate 		int i;
20377c478bd9Sstevel@tonic-gate 		char *buff;
20387c478bd9Sstevel@tonic-gate 		size_t size;
20397c478bd9Sstevel@tonic-gate 		char *ngname;
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 		if (strchr(argv[1], '(') != NULL) {
20427c478bd9Sstevel@tonic-gate 			if (irp_unmarshall_ng(&ngname, &entries,
20437c478bd9Sstevel@tonic-gate 					      &hosts, &users, &domains,
20447c478bd9Sstevel@tonic-gate 					      argv[1]) != 0) {
20457c478bd9Sstevel@tonic-gate 				printf("unmarshall failed\n");
20467c478bd9Sstevel@tonic-gate 				exit(1);
20477c478bd9Sstevel@tonic-gate 			}
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate #define STRVAL(x) (x == NULL ? "*" : x)
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 			printf("%s {\n", ngname);
20527c478bd9Sstevel@tonic-gate 			for (i = 0 ; i < entries ; i++)
20537c478bd9Sstevel@tonic-gate 				printf("\t\"%s\" : \"%s\" : \"%s\"\n",
20547c478bd9Sstevel@tonic-gate 				       STRVAL(hosts[i]),
20557c478bd9Sstevel@tonic-gate 				       STRVAL(users[i]),
20567c478bd9Sstevel@tonic-gate 				       STRVAL(domains[i]));
20577c478bd9Sstevel@tonic-gate 			printf("}\n\n\n");
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 			irp_marshall_ng_start(ngname, NULL, &size);
20617c478bd9Sstevel@tonic-gate 			for (i = 0 ; i < entries ; i++)
20627c478bd9Sstevel@tonic-gate 				irp_marshall_ng_next(hosts[i], users[i],
20637c478bd9Sstevel@tonic-gate 						     domains[i], NULL, &size);
20647c478bd9Sstevel@tonic-gate 			irp_marshall_ng_end(NULL, &size);
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 			buff = malloc(size);
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 			irp_marshall_ng_start(ngname, buff, &size);
20697c478bd9Sstevel@tonic-gate 			for (i = 0 ; i < entries ; i++) {
20707c478bd9Sstevel@tonic-gate 				if (irp_marshall_ng_next(hosts[i], users[i],
20717c478bd9Sstevel@tonic-gate 							 domains[i], buff,
20727c478bd9Sstevel@tonic-gate 							 &size) != 0)
20737c478bd9Sstevel@tonic-gate 					printf("next marshalling failed.\n");
20747c478bd9Sstevel@tonic-gate 			}
20757c478bd9Sstevel@tonic-gate 			irp_marshall_ng_end(buff, &size);
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 			if (strcmp_nws(argv[1], buff) != 0) {
20787c478bd9Sstevel@tonic-gate 				printf("compare failed:\n\t%s\n\t%s\n",
20797c478bd9Sstevel@tonic-gate 				       buffer, argv[1]);
20807c478bd9Sstevel@tonic-gate 			} else {
20817c478bd9Sstevel@tonic-gate 				printf("compare ok\n");
20827c478bd9Sstevel@tonic-gate 			}
20837c478bd9Sstevel@tonic-gate 		} else {
20847c478bd9Sstevel@tonic-gate 			char *h, *u, *d, *buff;
20857c478bd9Sstevel@tonic-gate 			size_t size;
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 			/* run through two times. First to figure out how
20887c478bd9Sstevel@tonic-gate 			   much of a buffer we need. Second to do the
20897c478bd9Sstevel@tonic-gate 			   actual marshalling */
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 			setnetgrent(argv[1]);
20927c478bd9Sstevel@tonic-gate 			irp_marshall_ng_start(argv[1], NULL, &size);
20937c478bd9Sstevel@tonic-gate 			while (getnetgrent(&h, &u, &d) == 1)
20947c478bd9Sstevel@tonic-gate 				irp_marshall_ng_next(h, u, d, NULL, &size);
20957c478bd9Sstevel@tonic-gate 			irp_marshall_ng_end(NULL, &size);
20967c478bd9Sstevel@tonic-gate 			endnetgrent(argv[1]);
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 			buff = malloc(size);
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 			setnetgrent(argv[1]);
21017c478bd9Sstevel@tonic-gate 			if (irp_marshall_ng_start(argv[1], buff, &size) != 0)
21027c478bd9Sstevel@tonic-gate 				printf("Marshalling start failed\n");
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 			while (getnetgrent(&h, &u, &d) == 1) {
21057c478bd9Sstevel@tonic-gate 				if (irp_marshall_ng_next(h, u, d, buff, &size)
21067c478bd9Sstevel@tonic-gate 				    != 0) {
21077c478bd9Sstevel@tonic-gate 					printf("Marshalling failed\n");
21087c478bd9Sstevel@tonic-gate 				}
21097c478bd9Sstevel@tonic-gate 			}
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 			irp_marshall_ng_end(buff, &size);
21127c478bd9Sstevel@tonic-gate 			endnetgrent();
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate 			printf("success: %s\n", buff);
21157c478bd9Sstevel@tonic-gate 		}
21167c478bd9Sstevel@tonic-gate 		break;
21177c478bd9Sstevel@tonic-gate 	}
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	case 'h': {
21227c478bd9Sstevel@tonic-gate 		struct hostent he, *hp;
21237c478bd9Sstevel@tonic-gate 		int i;
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 		if (strchr(argv[1], '@') != NULL) {
21277c478bd9Sstevel@tonic-gate 			if (irp_unmarshall_ho(&he, argv[1]) != 0) {
21287c478bd9Sstevel@tonic-gate 				printf("unmarshall failed\n");
21297c478bd9Sstevel@tonic-gate 				exit(1);
21307c478bd9Sstevel@tonic-gate 			}
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 			printf("Host: \"%s\"\nAliases:", he.h_name);
21337c478bd9Sstevel@tonic-gate 			for (i = 0 ; he.h_aliases[i] != NULL ; i++)
21347c478bd9Sstevel@tonic-gate 				printf("\n\t\t\"%s\"", he.h_aliases[i]);
21357c478bd9Sstevel@tonic-gate 			printf("\nAddr Type: \"%s\"\n",
21367c478bd9Sstevel@tonic-gate 			       ADDR_T_STR(he.h_addrtype));
21377c478bd9Sstevel@tonic-gate 			printf("Length: %d\nAddresses:", he.h_length);
21387c478bd9Sstevel@tonic-gate 			for (i = 0 ; he.h_addr_list[i] != 0 ; i++) {
21397c478bd9Sstevel@tonic-gate 				inet_ntop(he.h_addrtype, he.h_addr_list[i],
21407c478bd9Sstevel@tonic-gate 					  buffer, sizeof buffer);
21417c478bd9Sstevel@tonic-gate 				printf("\n\t\"%s\"\n", buffer);
21427c478bd9Sstevel@tonic-gate 			}
21437c478bd9Sstevel@tonic-gate 			printf("\n\n");
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate 			irp_marshall_ho(&he, &b, &len);
21467c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
21477c478bd9Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
21487c478bd9Sstevel@tonic-gate 				       buffer, argv[1]);
21497c478bd9Sstevel@tonic-gate 			} else {
21507c478bd9Sstevel@tonic-gate 				printf("compare ok\n");
21517c478bd9Sstevel@tonic-gate 			}
21527c478bd9Sstevel@tonic-gate 		} else {
21537c478bd9Sstevel@tonic-gate 			if ((hp = gethostbyname(argv[1])) == NULL) {
21547c478bd9Sstevel@tonic-gate 				perror("gethostbyname");
21557c478bd9Sstevel@tonic-gate 				printf("\"%s\"\n", argv[1]);
21567c478bd9Sstevel@tonic-gate 				exit(1);
21577c478bd9Sstevel@tonic-gate 			}
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 			if (irp_marshall_ho(hp, &b, &len) != 0) {
21607c478bd9Sstevel@tonic-gate 				printf("irp_marshall_ho failed\n");
21617c478bd9Sstevel@tonic-gate 				exit(1);
21627c478bd9Sstevel@tonic-gate 			}
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
21657c478bd9Sstevel@tonic-gate 		}
21667c478bd9Sstevel@tonic-gate 		break;
21677c478bd9Sstevel@tonic-gate 	}
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	case 's': {
21717c478bd9Sstevel@tonic-gate 		struct servent *sv;
21727c478bd9Sstevel@tonic-gate 		struct servent sv1;
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
21757c478bd9Sstevel@tonic-gate 			sv = &sv1;
21767c478bd9Sstevel@tonic-gate 			memset(sv, 0xef, sizeof (struct servent));
21777c478bd9Sstevel@tonic-gate 			if (irp_unmarshall_sv(sv, argv[1]) != 0) {
21787c478bd9Sstevel@tonic-gate 				printf("unmarshall failed\n");
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 			}
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 			irp_marshall_sv(sv, &b, &len);
21837c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
21847c478bd9Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
21857c478bd9Sstevel@tonic-gate 				       buffer, argv[1]);
21867c478bd9Sstevel@tonic-gate 			} else {
21877c478bd9Sstevel@tonic-gate 				printf("compare ok\n");
21887c478bd9Sstevel@tonic-gate 			}
21897c478bd9Sstevel@tonic-gate 		} else {
21907c478bd9Sstevel@tonic-gate 			if ((sv = getservbyname(argv[1], argv[2])) == NULL) {
21917c478bd9Sstevel@tonic-gate 				perror("getservent");
21927c478bd9Sstevel@tonic-gate 				exit(1);
21937c478bd9Sstevel@tonic-gate 			}
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 			if (irp_marshall_sv(sv, &b, &len) != 0) {
21967c478bd9Sstevel@tonic-gate 				printf("irp_marshall_sv failed\n");
21977c478bd9Sstevel@tonic-gate 				exit(1);
21987c478bd9Sstevel@tonic-gate 			}
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
22017c478bd9Sstevel@tonic-gate 		}
22027c478bd9Sstevel@tonic-gate 		break;
22037c478bd9Sstevel@tonic-gate 	}
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	case 'g': {
22067c478bd9Sstevel@tonic-gate 		struct group *gr;
22077c478bd9Sstevel@tonic-gate 		struct group gr1;
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
22107c478bd9Sstevel@tonic-gate 			gr = &gr1;
22117c478bd9Sstevel@tonic-gate 			memset(gr, 0xef, sizeof (struct group));
22127c478bd9Sstevel@tonic-gate 			if (irp_unmarshall_gr(gr, argv[1]) != 0) {
22137c478bd9Sstevel@tonic-gate 				printf("unmarshall failed\n");
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 			}
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 			irp_marshall_gr(gr, &b, &len);
22187c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
22197c478bd9Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
22207c478bd9Sstevel@tonic-gate 				       buffer, argv[1]);
22217c478bd9Sstevel@tonic-gate 			} else {
22227c478bd9Sstevel@tonic-gate 				printf("compare ok\n");
22237c478bd9Sstevel@tonic-gate 			}
22247c478bd9Sstevel@tonic-gate 		} else {
22257c478bd9Sstevel@tonic-gate 			if ((gr = getgrnam(argv[1])) == NULL) {
22267c478bd9Sstevel@tonic-gate 				perror("getgrnam");
22277c478bd9Sstevel@tonic-gate 				exit(1);
22287c478bd9Sstevel@tonic-gate 			}
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 			if (irp_marshall_gr(gr, &b, &len) != 0) {
22317c478bd9Sstevel@tonic-gate 				printf("irp_marshall_gr failed\n");
22327c478bd9Sstevel@tonic-gate 				exit(1);
22337c478bd9Sstevel@tonic-gate 			}
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
22367c478bd9Sstevel@tonic-gate 		}
22377c478bd9Sstevel@tonic-gate 		break;
22387c478bd9Sstevel@tonic-gate 	}
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 	case 'p': {
22427c478bd9Sstevel@tonic-gate 		struct passwd *pw;
22437c478bd9Sstevel@tonic-gate 		struct passwd pw1;
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
22467c478bd9Sstevel@tonic-gate 			pw = &pw1;
22477c478bd9Sstevel@tonic-gate 			memset(pw, 0xef, sizeof (*pw));
22487c478bd9Sstevel@tonic-gate 			if (irp_unmarshall_pw(pw, argv[1]) != 0) {
22497c478bd9Sstevel@tonic-gate 				printf("unmarshall failed\n");
22507c478bd9Sstevel@tonic-gate 				exit(1);
22517c478bd9Sstevel@tonic-gate 			}
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 			printf("User: \"%s\"\nPasswd: \"%s\"\nUid: %ld\nGid: %ld\n",
22547c478bd9Sstevel@tonic-gate 			       pw->pw_name, pw->pw_passwd, (long)pw->pw_uid,
22557c478bd9Sstevel@tonic-gate 			       (long)pw->pw_gid);
22567c478bd9Sstevel@tonic-gate 			printf("Class: \"%s\"\nChange: %ld\nGecos: \"%s\"\n",
22577c478bd9Sstevel@tonic-gate 			       pw->pw_class, (long)pw->pw_change, pw->pw_gecos);
22587c478bd9Sstevel@tonic-gate 			printf("Shell: \"%s\"\nDirectory: \"%s\"\n",
22597c478bd9Sstevel@tonic-gate 			       pw->pw_shell, pw->pw_dir);
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 			pw = getpwnam(pw->pw_name);
22627c478bd9Sstevel@tonic-gate 			irp_marshall_pw(pw, &b, &len);
22637c478bd9Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
22647c478bd9Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
22657c478bd9Sstevel@tonic-gate 				       buffer, argv[1]);
22667c478bd9Sstevel@tonic-gate 			} else {
22677c478bd9Sstevel@tonic-gate 				printf("compare ok\n");
22687c478bd9Sstevel@tonic-gate 			}
22697c478bd9Sstevel@tonic-gate 		} else {
22707c478bd9Sstevel@tonic-gate 			if ((pw = getpwnam(argv[1])) == NULL) {
22717c478bd9Sstevel@tonic-gate 				perror("getpwnam");
22727c478bd9Sstevel@tonic-gate 				exit(1);
22737c478bd9Sstevel@tonic-gate 			}
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 			if (irp_marshall_pw(pw, &b, &len) != 0) {
22767c478bd9Sstevel@tonic-gate 				printf("irp_marshall_pw failed\n");
22777c478bd9Sstevel@tonic-gate 				exit(1);
22787c478bd9Sstevel@tonic-gate 			}
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
22817c478bd9Sstevel@tonic-gate 		}
22827c478bd9Sstevel@tonic-gate 		break;
22837c478bd9Sstevel@tonic-gate 	}
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	default:
22867c478bd9Sstevel@tonic-gate 		printf("Wrong option: %c\n", option);
22877c478bd9Sstevel@tonic-gate 		break;
22887c478bd9Sstevel@tonic-gate 	}
22897c478bd9Sstevel@tonic-gate 
22907c478bd9Sstevel@tonic-gate #endif
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 	return (0);
22937c478bd9Sstevel@tonic-gate }
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate #endif
2296*9525b14bSRao Shoaib 
2297*9525b14bSRao Shoaib /*! \file */
2298