xref: /onnv-gate/usr/src/lib/libresolv2/common/irs/irpmarshall.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright(c) 1989, 1993, 1995
30Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
60Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
70Sstevel@tonic-gate  * are met:
80Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
90Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
100Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
110Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
120Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
130Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
140Sstevel@tonic-gate  *    must display the following acknowledgement:
150Sstevel@tonic-gate  *	This product includes software developed by the University of
160Sstevel@tonic-gate  *	California, Berkeley and its contributors.
170Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
180Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
190Sstevel@tonic-gate  *    without specific prior written permission.
200Sstevel@tonic-gate  *
210Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
220Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
230Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
240Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
250Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
260Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
270Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
280Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
290Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
300Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
310Sstevel@tonic-gate  * SUCH DAMAGE.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
35*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
360Sstevel@tonic-gate  * Portions Copyright (c) 1996 by Internet Software Consortium.
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
390Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
400Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
410Sstevel@tonic-gate  *
42*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
43*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
45*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
48*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
52*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: irpmarshall.c,v 1.7 2006/03/09 23:57:56 marka Exp $";
530Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #if 0
560Sstevel@tonic-gate 
570Sstevel@tonic-gate Check values are in approrpriate endian order.
580Sstevel@tonic-gate 
590Sstevel@tonic-gate Double check memory allocations on unmarhsalling
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #endif
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /* Extern */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include "port_before.h"
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include <sys/types.h>
690Sstevel@tonic-gate #include <sys/socket.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #include <netinet/in.h>
720Sstevel@tonic-gate #include <arpa/inet.h>
730Sstevel@tonic-gate #include <arpa/nameser.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #include <stdio.h>
760Sstevel@tonic-gate #include <ctype.h>
770Sstevel@tonic-gate #include <pwd.h>
780Sstevel@tonic-gate #include <stdlib.h>
790Sstevel@tonic-gate #include <string.h>
800Sstevel@tonic-gate #include <syslog.h>
810Sstevel@tonic-gate #include <utmp.h>
820Sstevel@tonic-gate #include <unistd.h>
830Sstevel@tonic-gate #include <assert.h>
840Sstevel@tonic-gate #include <errno.h>
850Sstevel@tonic-gate 
860Sstevel@tonic-gate #include <irs.h>
870Sstevel@tonic-gate #include <isc/memcluster.h>
880Sstevel@tonic-gate #include <isc/irpmarshall.h>
890Sstevel@tonic-gate 
900Sstevel@tonic-gate #include "port_after.h"
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 
930Sstevel@tonic-gate #ifndef HAVE_STRNDUP
940Sstevel@tonic-gate static char    *strndup(const char *str, size_t len);
950Sstevel@tonic-gate #endif
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static char   **splitarray(const char *buffer, const char *buffend, char delim);
980Sstevel@tonic-gate static int	joinarray(char * const * argv, char *buffer, char delim);
990Sstevel@tonic-gate static char    *getfield(char **res, size_t reslen, char **buffer, char delim);
1000Sstevel@tonic-gate static size_t	joinlength(char * const *argv);
1010Sstevel@tonic-gate static void	free_array(char **argv, size_t entries);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #define ADDR_T_STR(x) (x == AF_INET ? "AF_INET" :\
1040Sstevel@tonic-gate 		       (x == AF_INET6 ? "AF_INET6" : "UNKNOWN"))
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #define MAXPADDRSIZE (sizeof "255.255.255.255" + 1)
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static char COMMA = ',';
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static const char *COMMASTR = ",";
1110Sstevel@tonic-gate static const char *COLONSTR = ":";
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /* See big comment at bottom of irpmarshall.h for description. */
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #ifdef WANT_IRS_PW
1190Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct passwd +++++++++++++++++++++++++ */
1200Sstevel@tonic-gate 
121*11038SRao.Shoaib@Sun.COM /*%
1220Sstevel@tonic-gate  * int irp_marshall_pw(const struct passwd *pw, char **buffer, size_t *len)
1230Sstevel@tonic-gate  *
124*11038SRao.Shoaib@Sun.COM  * notes: \li
1250Sstevel@tonic-gate  *
126*11038SRao.Shoaib@Sun.COM  *	See irpmarshall.h
1270Sstevel@tonic-gate  *
128*11038SRao.Shoaib@Sun.COM  * return: \li
1290Sstevel@tonic-gate  *
1300Sstevel@tonic-gate  *	0 on sucess, -1 on failure.
1310Sstevel@tonic-gate  *
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate int
irp_marshall_pw(const struct passwd * pw,char ** buffer,size_t * len)1350Sstevel@tonic-gate irp_marshall_pw(const struct passwd *pw, char **buffer, size_t *len) {
136*11038SRao.Shoaib@Sun.COM 	size_t need = 1 ;		/*%< for null byte */
1370Sstevel@tonic-gate 	char pwUid[24];
1380Sstevel@tonic-gate 	char pwGid[24];
1390Sstevel@tonic-gate 	char pwChange[24];
1400Sstevel@tonic-gate 	char pwExpire[24];
1410Sstevel@tonic-gate 	const char *pwClass;
1420Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (pw == NULL || len == NULL) {
1450Sstevel@tonic-gate 		errno = EINVAL;
1460Sstevel@tonic-gate 		return (-1);
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	sprintf(pwUid, "%ld", (long)pw->pw_uid);
1500Sstevel@tonic-gate 	sprintf(pwGid, "%ld", (long)pw->pw_gid);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate #ifdef HAVE_PW_CHANGE
1530Sstevel@tonic-gate 	sprintf(pwChange, "%ld", (long)pw->pw_change);
1540Sstevel@tonic-gate #else
1550Sstevel@tonic-gate 	pwChange[0] = '0';
1560Sstevel@tonic-gate 	pwChange[1] = '\0';
1570Sstevel@tonic-gate #endif
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate #ifdef HAVE_PW_EXPIRE
1600Sstevel@tonic-gate 	sprintf(pwExpire, "%ld", (long)pw->pw_expire);
1610Sstevel@tonic-gate #else
1620Sstevel@tonic-gate 	pwExpire[0] = '0';
1630Sstevel@tonic-gate 	pwExpire[1] = '\0';
1640Sstevel@tonic-gate #endif
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate #ifdef HAVE_PW_CLASS
1670Sstevel@tonic-gate 	pwClass = pw->pw_class;
1680Sstevel@tonic-gate #else
1690Sstevel@tonic-gate 	pwClass = "";
1700Sstevel@tonic-gate #endif
1710Sstevel@tonic-gate 
172*11038SRao.Shoaib@Sun.COM 	need += strlen(pw->pw_name)	+ 1; /*%< one for fieldsep */
1730Sstevel@tonic-gate 	need += strlen(pw->pw_passwd)	+ 1;
1740Sstevel@tonic-gate 	need += strlen(pwUid)		+ 1;
1750Sstevel@tonic-gate 	need += strlen(pwGid)		+ 1;
1760Sstevel@tonic-gate 	need += strlen(pwClass)		+ 1;
1770Sstevel@tonic-gate 	need += strlen(pwChange)	+ 1;
1780Sstevel@tonic-gate 	need += strlen(pwExpire)	+ 1;
1790Sstevel@tonic-gate 	need += strlen(pw->pw_gecos)	+ 1;
1800Sstevel@tonic-gate 	need += strlen(pw->pw_dir)	+ 1;
1810Sstevel@tonic-gate 	need += strlen(pw->pw_shell)	+ 1;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if (buffer == NULL) {
1840Sstevel@tonic-gate 		*len = need;
1850Sstevel@tonic-gate 		return (0);
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
1890Sstevel@tonic-gate 		errno = EINVAL;
1900Sstevel@tonic-gate 		return (-1);
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	if (*buffer == NULL) {
194*11038SRao.Shoaib@Sun.COM 		need += 2;		/*%< for CRLF */
1950Sstevel@tonic-gate 		*buffer = memget(need);
1960Sstevel@tonic-gate 		if (*buffer == NULL) {
1970Sstevel@tonic-gate 			errno = ENOMEM;
1980Sstevel@tonic-gate 			return (-1);
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 		*len = need;
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	strcpy(*buffer, pw->pw_name);		strcat(*buffer, fieldsep);
2050Sstevel@tonic-gate 	strcat(*buffer, pw->pw_passwd);		strcat(*buffer, fieldsep);
2060Sstevel@tonic-gate 	strcat(*buffer, pwUid);			strcat(*buffer, fieldsep);
2070Sstevel@tonic-gate 	strcat(*buffer, pwGid);			strcat(*buffer, fieldsep);
2080Sstevel@tonic-gate 	strcat(*buffer, pwClass);		strcat(*buffer, fieldsep);
2090Sstevel@tonic-gate 	strcat(*buffer, pwChange);		strcat(*buffer, fieldsep);
2100Sstevel@tonic-gate 	strcat(*buffer, pwExpire);		strcat(*buffer, fieldsep);
2110Sstevel@tonic-gate 	strcat(*buffer, pw->pw_gecos);		strcat(*buffer, fieldsep);
2120Sstevel@tonic-gate 	strcat(*buffer, pw->pw_dir);		strcat(*buffer, fieldsep);
2130Sstevel@tonic-gate 	strcat(*buffer, pw->pw_shell);		strcat(*buffer, fieldsep);
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	return (0);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate 
218*11038SRao.Shoaib@Sun.COM /*%
2190Sstevel@tonic-gate  * int irp_unmarshall_pw(struct passwd *pw, char *buffer)
2200Sstevel@tonic-gate  *
221*11038SRao.Shoaib@Sun.COM  * notes: \li
2220Sstevel@tonic-gate  *
223*11038SRao.Shoaib@Sun.COM  *	See irpmarshall.h
2240Sstevel@tonic-gate  *
225*11038SRao.Shoaib@Sun.COM  * return: \li
2260Sstevel@tonic-gate  *
2270Sstevel@tonic-gate  *	0 on success, -1 on failure
2280Sstevel@tonic-gate  *
2290Sstevel@tonic-gate  */
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate int
irp_unmarshall_pw(struct passwd * pw,char * buffer)2320Sstevel@tonic-gate irp_unmarshall_pw(struct passwd *pw, char *buffer) {
2330Sstevel@tonic-gate 	char *name, *pass, *class, *gecos, *dir, *shell;
2340Sstevel@tonic-gate 	uid_t pwuid;
2350Sstevel@tonic-gate 	gid_t pwgid;
2360Sstevel@tonic-gate 	time_t pwchange;
2370Sstevel@tonic-gate 	time_t pwexpire;
2380Sstevel@tonic-gate 	char *p;
2390Sstevel@tonic-gate 	long t;
2400Sstevel@tonic-gate 	char tmpbuf[24];
2410Sstevel@tonic-gate 	char *tb = &tmpbuf[0];
2420Sstevel@tonic-gate 	char fieldsep = ':';
2430Sstevel@tonic-gate 	int myerrno = EINVAL;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	name = pass = class = gecos = dir = shell = NULL;
2460Sstevel@tonic-gate 	p = buffer;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/* pw_name field */
2490Sstevel@tonic-gate 	name = NULL;
2500Sstevel@tonic-gate 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0) {
2510Sstevel@tonic-gate 		goto error;
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/* pw_passwd field */
2550Sstevel@tonic-gate 	pass = NULL;
256*11038SRao.Shoaib@Sun.COM 	if (getfield(&pass, 0, &p, fieldsep) == NULL) { /*%< field can be empty */
2570Sstevel@tonic-gate 		goto error;
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	/* pw_uid field */
2620Sstevel@tonic-gate 	tb = tmpbuf;
2630Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
2640Sstevel@tonic-gate 	    strlen(tb) == 0) {
2650Sstevel@tonic-gate 		goto error;
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
2680Sstevel@tonic-gate 	if (*tb) {
269*11038SRao.Shoaib@Sun.COM 		goto error;	/*%< junk in value */
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate 	pwuid = (uid_t)t;
272*11038SRao.Shoaib@Sun.COM 	if ((long) pwuid != t) {	/*%< value must have been too big. */
2730Sstevel@tonic-gate 		goto error;
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/* pw_gid field */
2790Sstevel@tonic-gate 	tb = tmpbuf;
2800Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
2810Sstevel@tonic-gate 	    strlen(tb) == 0) {
2820Sstevel@tonic-gate 		goto error;
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
2850Sstevel@tonic-gate 	if (*tb) {
286*11038SRao.Shoaib@Sun.COM 		goto error;	/*%< junk in value */
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 	pwgid = (gid_t)t;
289*11038SRao.Shoaib@Sun.COM 	if ((long)pwgid != t) {	/*%< value must have been too big. */
2900Sstevel@tonic-gate 		goto error;
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	/* pw_class field */
2960Sstevel@tonic-gate 	class = NULL;
2970Sstevel@tonic-gate 	if (getfield(&class, 0, &p, fieldsep) == NULL) {
2980Sstevel@tonic-gate 		goto error;
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	/* pw_change field */
3040Sstevel@tonic-gate 	tb = tmpbuf;
3050Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
3060Sstevel@tonic-gate 	    strlen(tb) == 0) {
3070Sstevel@tonic-gate 		goto error;
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
3100Sstevel@tonic-gate 	if (*tb) {
311*11038SRao.Shoaib@Sun.COM 		goto error;	/*%< junk in value */
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 	pwchange = (time_t)t;
314*11038SRao.Shoaib@Sun.COM 	if ((long)pwchange != t) {	/*%< value must have been too big. */
3150Sstevel@tonic-gate 		goto error;
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/* pw_expire field */
3210Sstevel@tonic-gate 	tb = tmpbuf;
3220Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
3230Sstevel@tonic-gate 	    strlen(tb) == 0) {
3240Sstevel@tonic-gate 		goto error;
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
3270Sstevel@tonic-gate 	if (*tb) {
328*11038SRao.Shoaib@Sun.COM 		goto error;	/*%< junk in value */
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 	pwexpire = (time_t)t;
331*11038SRao.Shoaib@Sun.COM 	if ((long) pwexpire != t) {	/*%< value must have been too big. */
3320Sstevel@tonic-gate 		goto error;
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	/* pw_gecos field */
3380Sstevel@tonic-gate 	gecos = NULL;
3390Sstevel@tonic-gate 	if (getfield(&gecos, 0, &p, fieldsep) == NULL) {
3400Sstevel@tonic-gate 		goto error;
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	/* pw_dir field */
3460Sstevel@tonic-gate 	dir = NULL;
3470Sstevel@tonic-gate 	if (getfield(&dir, 0, &p, fieldsep) == NULL) {
3480Sstevel@tonic-gate 		goto error;
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	/* pw_shell field */
3540Sstevel@tonic-gate 	shell = NULL;
3550Sstevel@tonic-gate 	if (getfield(&shell, 0, &p, fieldsep) == NULL) {
3560Sstevel@tonic-gate 		goto error;
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	pw->pw_name = name;
3620Sstevel@tonic-gate 	pw->pw_passwd = pass;
3630Sstevel@tonic-gate 	pw->pw_uid = pwuid;
3640Sstevel@tonic-gate 	pw->pw_gid = pwgid;
3650Sstevel@tonic-gate 	pw->pw_gecos = gecos;
3660Sstevel@tonic-gate 	pw->pw_dir = dir;
3670Sstevel@tonic-gate 	pw->pw_shell = shell;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate #ifdef HAVE_PW_CHANGE
3700Sstevel@tonic-gate 	pw->pw_change = pwchange;
3710Sstevel@tonic-gate #endif
3720Sstevel@tonic-gate #ifdef HAVE_PW_CLASS
3730Sstevel@tonic-gate 	pw->pw_class = class;
3740Sstevel@tonic-gate #endif
3750Sstevel@tonic-gate #ifdef HAVE_PW_EXPIRE
3760Sstevel@tonic-gate 	pw->pw_expire = pwexpire;
3770Sstevel@tonic-gate #endif
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	return (0);
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate  error:
3820Sstevel@tonic-gate 	errno = myerrno;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	if (name != NULL) free(name);
3850Sstevel@tonic-gate 	if (pass != NULL) free(pass);
3860Sstevel@tonic-gate 	if (gecos != NULL) free(gecos);
3870Sstevel@tonic-gate 	if (dir != NULL) free(dir);
3880Sstevel@tonic-gate 	if (shell != NULL) free(shell);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	return (-1);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate /* ------------------------- struct passwd ------------------------- */
3940Sstevel@tonic-gate #endif /* WANT_IRS_PW */
3950Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct group +++++++++++++++++++++++++ */
3960Sstevel@tonic-gate 
397*11038SRao.Shoaib@Sun.COM /*%
3980Sstevel@tonic-gate  * int irp_marshall_gr(const struct group *gr, char **buffer, size_t *len)
3990Sstevel@tonic-gate  *
400*11038SRao.Shoaib@Sun.COM  * notes: \li
4010Sstevel@tonic-gate  *
402*11038SRao.Shoaib@Sun.COM  *	See irpmarshall.h.
4030Sstevel@tonic-gate  *
404*11038SRao.Shoaib@Sun.COM  * return: \li
4050Sstevel@tonic-gate  *
4060Sstevel@tonic-gate  *	0 on success, -1 on failure
4070Sstevel@tonic-gate  */
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate int
irp_marshall_gr(const struct group * gr,char ** buffer,size_t * len)4100Sstevel@tonic-gate irp_marshall_gr(const struct group *gr, char **buffer, size_t *len) {
411*11038SRao.Shoaib@Sun.COM 	size_t need = 1;	/*%< for null byte */
4120Sstevel@tonic-gate 	char grGid[24];
4130Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if (gr == NULL || len == NULL) {
4160Sstevel@tonic-gate 		errno = EINVAL;
4170Sstevel@tonic-gate 		return (-1);
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	sprintf(grGid, "%ld", (long)gr->gr_gid);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	need += strlen(gr->gr_name) + 1;
4230Sstevel@tonic-gate #ifndef MISSING_GR_PASSWD
4240Sstevel@tonic-gate 	need += strlen(gr->gr_passwd) + 1;
4250Sstevel@tonic-gate #else
4260Sstevel@tonic-gate 	need++;
4270Sstevel@tonic-gate #endif
4280Sstevel@tonic-gate 	need += strlen(grGid) + 1;
4290Sstevel@tonic-gate 	need += joinlength(gr->gr_mem) + 1;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	if (buffer == NULL) {
4320Sstevel@tonic-gate 		*len = need;
4330Sstevel@tonic-gate 		return (0);
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
4370Sstevel@tonic-gate 		errno = EINVAL;
4380Sstevel@tonic-gate 		return (-1);
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	if (*buffer == NULL) {
442*11038SRao.Shoaib@Sun.COM 		need += 2;		/*%< for CRLF */
4430Sstevel@tonic-gate 		*buffer = memget(need);
4440Sstevel@tonic-gate 		if (*buffer == NULL) {
4450Sstevel@tonic-gate 			errno = ENOMEM;
4460Sstevel@tonic-gate 			return (-1);
4470Sstevel@tonic-gate 		}
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 		*len = need;
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	strcpy(*buffer, gr->gr_name);		strcat(*buffer, fieldsep);
4530Sstevel@tonic-gate #ifndef MISSING_GR_PASSWD
4540Sstevel@tonic-gate 	strcat(*buffer, gr->gr_passwd);
4550Sstevel@tonic-gate #endif
4560Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
4570Sstevel@tonic-gate 	strcat(*buffer, grGid);			strcat(*buffer, fieldsep);
4580Sstevel@tonic-gate 	joinarray(gr->gr_mem, *buffer, COMMA) ;	strcat(*buffer, fieldsep);
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	return (0);
4610Sstevel@tonic-gate }
4620Sstevel@tonic-gate 
463*11038SRao.Shoaib@Sun.COM /*%
4640Sstevel@tonic-gate  * int irp_unmarshall_gr(struct group *gr, char *buffer)
4650Sstevel@tonic-gate  *
466*11038SRao.Shoaib@Sun.COM  * notes: \li
4670Sstevel@tonic-gate  *
468*11038SRao.Shoaib@Sun.COM  *	See irpmarshall.h
4690Sstevel@tonic-gate  *
470*11038SRao.Shoaib@Sun.COM  * return: \li
4710Sstevel@tonic-gate  *
4720Sstevel@tonic-gate  *	0 on success and -1 on failure.
4730Sstevel@tonic-gate  *
4740Sstevel@tonic-gate  */
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate int
irp_unmarshall_gr(struct group * gr,char * buffer)4770Sstevel@tonic-gate irp_unmarshall_gr(struct group *gr, char *buffer) {
4780Sstevel@tonic-gate 	char *p, *q;
4790Sstevel@tonic-gate 	gid_t grgid;
4800Sstevel@tonic-gate 	long t;
4810Sstevel@tonic-gate 	char *name = NULL;
4820Sstevel@tonic-gate 	char *pass = NULL;
4830Sstevel@tonic-gate 	char **members = NULL;
4840Sstevel@tonic-gate 	char tmpbuf[24];
4850Sstevel@tonic-gate 	char *tb;
4860Sstevel@tonic-gate 	char fieldsep = ':';
4870Sstevel@tonic-gate 	int myerrno = EINVAL;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	if (gr == NULL || buffer == NULL) {
4900Sstevel@tonic-gate 		errno = EINVAL;
4910Sstevel@tonic-gate 		return (-1);
4920Sstevel@tonic-gate 	}
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	p = buffer;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	/* gr_name field */
4970Sstevel@tonic-gate 	name = NULL;
498*11038SRao.Shoaib@Sun.COM 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
4990Sstevel@tonic-gate 		goto error;
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	/* gr_passwd field */
5040Sstevel@tonic-gate 	pass = NULL;
5050Sstevel@tonic-gate 	if (getfield(&pass, 0, &p, fieldsep) == NULL) {
5060Sstevel@tonic-gate 		goto error;
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	/* gr_gid field */
5110Sstevel@tonic-gate 	tb = tmpbuf;
5120Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
513*11038SRao.Shoaib@Sun.COM 	    strlen(tb) == 0U) {
5140Sstevel@tonic-gate 		goto error;
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
5170Sstevel@tonic-gate 	if (*tb) {
518*11038SRao.Shoaib@Sun.COM 		goto error;	/*%< junk in value */
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 	grgid = (gid_t)t;
521*11038SRao.Shoaib@Sun.COM 	if ((long) grgid != t) {	/*%< value must have been too big. */
5220Sstevel@tonic-gate 		goto error;
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	/* gr_mem field. Member names are separated by commas */
5270Sstevel@tonic-gate 	q = strchr(p, fieldsep);
5280Sstevel@tonic-gate 	if (q == NULL) {
5290Sstevel@tonic-gate 		goto error;
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 	members = splitarray(p, q, COMMA);
5320Sstevel@tonic-gate 	if (members == NULL) {
5330Sstevel@tonic-gate 		myerrno = errno;
5340Sstevel@tonic-gate 		goto error;
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 	p = q + 1;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	gr->gr_name = name;
5400Sstevel@tonic-gate #ifndef MISSING_GR_PASSWD
5410Sstevel@tonic-gate 	gr->gr_passwd = pass;
5420Sstevel@tonic-gate #endif
5430Sstevel@tonic-gate 	gr->gr_gid = grgid;
5440Sstevel@tonic-gate 	gr->gr_mem = members;
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	return (0);
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate  error:
5490Sstevel@tonic-gate 	errno = myerrno;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	if (name != NULL) free(name);
5520Sstevel@tonic-gate 	if (pass != NULL) free(pass);
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	return (-1);
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate /* ------------------------- struct group ------------------------- */
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct servent +++++++++++++++++++++++++ */
5640Sstevel@tonic-gate 
565*11038SRao.Shoaib@Sun.COM /*%
5660Sstevel@tonic-gate  * int irp_marshall_sv(const struct servent *sv, char **buffer, size_t *len)
5670Sstevel@tonic-gate  *
568*11038SRao.Shoaib@Sun.COM  * notes: \li
5690Sstevel@tonic-gate  *
570*11038SRao.Shoaib@Sun.COM  *	See irpmarshall.h
5710Sstevel@tonic-gate  *
572*11038SRao.Shoaib@Sun.COM  * return: \li
5730Sstevel@tonic-gate  *
5740Sstevel@tonic-gate  *	0 on success, -1 on failure.
5750Sstevel@tonic-gate  *
5760Sstevel@tonic-gate  */
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate int
irp_marshall_sv(const struct servent * sv,char ** buffer,size_t * len)5790Sstevel@tonic-gate irp_marshall_sv(const struct servent *sv, char **buffer, size_t *len) {
580*11038SRao.Shoaib@Sun.COM 	size_t need = 1;	/*%< for null byte */
5810Sstevel@tonic-gate 	char svPort[24];
5820Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
5830Sstevel@tonic-gate 	short realport;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	if (sv == NULL || len == NULL) {
5860Sstevel@tonic-gate 		errno = EINVAL;
5870Sstevel@tonic-gate 		return (-1);
5880Sstevel@tonic-gate 	}
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	/* the int s_port field is actually a short in network order. We
5910Sstevel@tonic-gate 	   want host order to make the marshalled data look correct */
5920Sstevel@tonic-gate 	realport = ntohs((short)sv->s_port);
5930Sstevel@tonic-gate 	sprintf(svPort, "%d", realport);
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	need += strlen(sv->s_name) + 1;
5960Sstevel@tonic-gate 	need += joinlength(sv->s_aliases) + 1;
5970Sstevel@tonic-gate 	need += strlen(svPort) + 1;
5980Sstevel@tonic-gate 	need += strlen(sv->s_proto) + 1;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	if (buffer == NULL) {
6010Sstevel@tonic-gate 		*len = need;
6020Sstevel@tonic-gate 		return (0);
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
6060Sstevel@tonic-gate 		errno = EINVAL;
6070Sstevel@tonic-gate 		return (-1);
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	if (*buffer == NULL) {
611*11038SRao.Shoaib@Sun.COM 		need += 2;		/*%< for CRLF */
6120Sstevel@tonic-gate 		*buffer = memget(need);
6130Sstevel@tonic-gate 		if (*buffer == NULL) {
6140Sstevel@tonic-gate 			errno = ENOMEM;
6150Sstevel@tonic-gate 			return (-1);
6160Sstevel@tonic-gate 		}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 		*len = need;
6190Sstevel@tonic-gate 	}
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	strcpy(*buffer, sv->s_name);		strcat(*buffer, fieldsep);
6220Sstevel@tonic-gate 	joinarray(sv->s_aliases, *buffer, COMMA); strcat(*buffer, fieldsep);
6230Sstevel@tonic-gate 	strcat(*buffer, svPort);		strcat(*buffer, fieldsep);
6240Sstevel@tonic-gate 	strcat(*buffer, sv->s_proto);		strcat(*buffer, fieldsep);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	return (0);
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate 
629*11038SRao.Shoaib@Sun.COM /*%
6300Sstevel@tonic-gate  * int irp_unmarshall_sv(struct servent *sv, char *buffer)
6310Sstevel@tonic-gate  *
632*11038SRao.Shoaib@Sun.COM  * notes: \li
6330Sstevel@tonic-gate  *
634*11038SRao.Shoaib@Sun.COM  *	See irpmarshall.h
6350Sstevel@tonic-gate  *
636*11038SRao.Shoaib@Sun.COM  * return: \li
6370Sstevel@tonic-gate  *
6380Sstevel@tonic-gate  *	0 on success, -1 on failure.
6390Sstevel@tonic-gate  *
6400Sstevel@tonic-gate  */
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate int
irp_unmarshall_sv(struct servent * sv,char * buffer)6430Sstevel@tonic-gate irp_unmarshall_sv(struct servent *sv, char *buffer) {
6440Sstevel@tonic-gate 	char *p, *q;
6450Sstevel@tonic-gate 	short svport;
6460Sstevel@tonic-gate 	long t;
6470Sstevel@tonic-gate 	char *name = NULL;
6480Sstevel@tonic-gate 	char *proto = NULL;
6490Sstevel@tonic-gate 	char **aliases = NULL;
6500Sstevel@tonic-gate 	char tmpbuf[24];
6510Sstevel@tonic-gate 	char *tb;
6520Sstevel@tonic-gate 	char fieldsep = ':';
6530Sstevel@tonic-gate 	int myerrno = EINVAL;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	if (sv == NULL || buffer == NULL)
6560Sstevel@tonic-gate 		return (-1);
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	p = buffer;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	/* s_name field */
6620Sstevel@tonic-gate 	name = NULL;
663*11038SRao.Shoaib@Sun.COM 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
6640Sstevel@tonic-gate 		goto error;
6650Sstevel@tonic-gate 	}
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	/* s_aliases field */
6690Sstevel@tonic-gate 	q = strchr(p, fieldsep);
6700Sstevel@tonic-gate 	if (q == NULL) {
6710Sstevel@tonic-gate 		goto error;
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
6740Sstevel@tonic-gate 	if (aliases == NULL) {
6750Sstevel@tonic-gate 		myerrno = errno;
6760Sstevel@tonic-gate 		goto error;
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 	p = q + 1;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	/* s_port field */
6820Sstevel@tonic-gate 	tb = tmpbuf;
6830Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
684*11038SRao.Shoaib@Sun.COM 	    strlen(tb) == 0U) {
6850Sstevel@tonic-gate 		goto error;
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
6880Sstevel@tonic-gate 	if (*tb) {
689*11038SRao.Shoaib@Sun.COM 		goto error;	/*%< junk in value */
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 	svport = (short)t;
692*11038SRao.Shoaib@Sun.COM 	if ((long) svport != t) {	/*%< value must have been too big. */
6930Sstevel@tonic-gate 		goto error;
6940Sstevel@tonic-gate 	}
6950Sstevel@tonic-gate 	svport = htons(svport);
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	/* s_proto field */
6980Sstevel@tonic-gate 	proto = NULL;
6990Sstevel@tonic-gate 	if (getfield(&proto, 0, &p, fieldsep) == NULL) {
7000Sstevel@tonic-gate 		goto error;
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	sv->s_name = name;
7040Sstevel@tonic-gate 	sv->s_aliases = aliases;
7050Sstevel@tonic-gate 	sv->s_port = svport;
7060Sstevel@tonic-gate 	sv->s_proto = proto;
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	return (0);
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate  error:
7110Sstevel@tonic-gate 	errno = myerrno;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	if (name != NULL) free(name);
7140Sstevel@tonic-gate 	if (proto != NULL) free(proto);
7150Sstevel@tonic-gate 	free_array(aliases, 0);
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	return (-1);
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate /* ------------------------- struct servent ------------------------- */
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct protoent +++++++++++++++++++++++++ */
7240Sstevel@tonic-gate 
725*11038SRao.Shoaib@Sun.COM /*%
7260Sstevel@tonic-gate  * int irp_marshall_pr(struct protoent *pr, char **buffer, size_t *len)
7270Sstevel@tonic-gate  *
728*11038SRao.Shoaib@Sun.COM  * notes: \li
7290Sstevel@tonic-gate  *
730*11038SRao.Shoaib@Sun.COM  *	See irpmarshall.h
7310Sstevel@tonic-gate  *
732*11038SRao.Shoaib@Sun.COM  * return: \li
7330Sstevel@tonic-gate  *
7340Sstevel@tonic-gate  *	0 on success and -1 on failure.
7350Sstevel@tonic-gate  *
7360Sstevel@tonic-gate  */
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate int
irp_marshall_pr(struct protoent * pr,char ** buffer,size_t * len)7390Sstevel@tonic-gate irp_marshall_pr(struct protoent *pr, char **buffer, size_t *len) {
740*11038SRao.Shoaib@Sun.COM 	size_t need = 1;	/*%< for null byte */
7410Sstevel@tonic-gate 	char prProto[24];
7420Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	if (pr == NULL || len == NULL) {
7450Sstevel@tonic-gate 		errno = EINVAL;
7460Sstevel@tonic-gate 		return (-1);
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	sprintf(prProto, "%d", (int)pr->p_proto);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	need += strlen(pr->p_name) + 1;
7520Sstevel@tonic-gate 	need += joinlength(pr->p_aliases) + 1;
7530Sstevel@tonic-gate 	need += strlen(prProto) + 1;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	if (buffer == NULL) {
7560Sstevel@tonic-gate 		*len = need;
7570Sstevel@tonic-gate 		return (0);
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
7610Sstevel@tonic-gate 		errno = EINVAL;
7620Sstevel@tonic-gate 		return (-1);
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	if (*buffer == NULL) {
766*11038SRao.Shoaib@Sun.COM 		need += 2;		/*%< for CRLF */
7670Sstevel@tonic-gate 		*buffer = memget(need);
7680Sstevel@tonic-gate 		if (*buffer == NULL) {
7690Sstevel@tonic-gate 			errno = ENOMEM;
7700Sstevel@tonic-gate 			return (-1);
7710Sstevel@tonic-gate 		}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 		*len = need;
7740Sstevel@tonic-gate 	}
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	strcpy(*buffer, pr->p_name);		strcat(*buffer, fieldsep);
7770Sstevel@tonic-gate 	joinarray(pr->p_aliases, *buffer, COMMA); strcat(*buffer, fieldsep);
7780Sstevel@tonic-gate 	strcat(*buffer, prProto);		strcat(*buffer, fieldsep);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	return (0);
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate 
784*11038SRao.Shoaib@Sun.COM /*%
7850Sstevel@tonic-gate  * int irp_unmarshall_pr(struct protoent *pr, char *buffer)
7860Sstevel@tonic-gate  *
787*11038SRao.Shoaib@Sun.COM  * notes: \li
7880Sstevel@tonic-gate  *
789*11038SRao.Shoaib@Sun.COM  *	See irpmarshall.h
7900Sstevel@tonic-gate  *
791*11038SRao.Shoaib@Sun.COM  * return: \li
7920Sstevel@tonic-gate  *
7930Sstevel@tonic-gate  *	0 on success, -1 on failure
7940Sstevel@tonic-gate  *
7950Sstevel@tonic-gate  */
7960Sstevel@tonic-gate 
irp_unmarshall_pr(struct protoent * pr,char * buffer)7970Sstevel@tonic-gate int irp_unmarshall_pr(struct protoent *pr, char *buffer) {
7980Sstevel@tonic-gate 	char *p, *q;
7990Sstevel@tonic-gate 	int prproto;
8000Sstevel@tonic-gate 	long t;
8010Sstevel@tonic-gate 	char *name = NULL;
8020Sstevel@tonic-gate 	char **aliases = NULL;
8030Sstevel@tonic-gate 	char tmpbuf[24];
8040Sstevel@tonic-gate 	char *tb;
8050Sstevel@tonic-gate 	char fieldsep = ':';
8060Sstevel@tonic-gate 	int myerrno = EINVAL;
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	if (pr == NULL || buffer == NULL) {
8090Sstevel@tonic-gate 		errno = EINVAL;
8100Sstevel@tonic-gate 		return (-1);
8110Sstevel@tonic-gate 	}
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	p = buffer;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	/* p_name field */
8160Sstevel@tonic-gate 	name = NULL;
817*11038SRao.Shoaib@Sun.COM 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
8180Sstevel@tonic-gate 		goto error;
8190Sstevel@tonic-gate 	}
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	/* p_aliases field */
8230Sstevel@tonic-gate 	q = strchr(p, fieldsep);
8240Sstevel@tonic-gate 	if (q == NULL) {
8250Sstevel@tonic-gate 		goto error;
8260Sstevel@tonic-gate 	}
8270Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
8280Sstevel@tonic-gate 	if (aliases == NULL) {
8290Sstevel@tonic-gate 		myerrno = errno;
8300Sstevel@tonic-gate 		goto error;
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 	p = q + 1;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	/* p_proto field */
8360Sstevel@tonic-gate 	tb = tmpbuf;
8370Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
838*11038SRao.Shoaib@Sun.COM 	    strlen(tb) == 0U) {
8390Sstevel@tonic-gate 		goto error;
8400Sstevel@tonic-gate 	}
8410Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
8420Sstevel@tonic-gate 	if (*tb) {
843*11038SRao.Shoaib@Sun.COM 		goto error;	/*%< junk in value */
8440Sstevel@tonic-gate 	}
8450Sstevel@tonic-gate 	prproto = (int)t;
846*11038SRao.Shoaib@Sun.COM 	if ((long) prproto != t) {	/*%< value must have been too big. */
8470Sstevel@tonic-gate 		goto error;
8480Sstevel@tonic-gate 	}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	pr->p_name = name;
8510Sstevel@tonic-gate 	pr->p_aliases = aliases;
8520Sstevel@tonic-gate 	pr->p_proto = prproto;
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	return (0);
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate  error:
8570Sstevel@tonic-gate 	errno = myerrno;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	if (name != NULL) free(name);
8600Sstevel@tonic-gate 	free_array(aliases, 0);
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	return (-1);
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate /* ------------------------- struct protoent ------------------------- */
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct hostent +++++++++++++++++++++++++ */
8700Sstevel@tonic-gate 
871*11038SRao.Shoaib@Sun.COM /*%
8720Sstevel@tonic-gate  * int irp_marshall_ho(struct hostent *ho, char **buffer, size_t *len)
8730Sstevel@tonic-gate  *
874*11038SRao.Shoaib@Sun.COM  * notes: \li
8750Sstevel@tonic-gate  *
876*11038SRao.Shoaib@Sun.COM  *	See irpmarshall.h.
8770Sstevel@tonic-gate  *
878*11038SRao.Shoaib@Sun.COM  * return: \li
8790Sstevel@tonic-gate  *
8800Sstevel@tonic-gate  *	0 on success, -1 on failure.
8810Sstevel@tonic-gate  *
8820Sstevel@tonic-gate  */
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate int
irp_marshall_ho(struct hostent * ho,char ** buffer,size_t * len)8850Sstevel@tonic-gate irp_marshall_ho(struct hostent *ho, char **buffer, size_t *len) {
886*11038SRao.Shoaib@Sun.COM 	size_t need = 1;	/*%< for null byte */
8870Sstevel@tonic-gate 	char hoaddrtype[24];
8880Sstevel@tonic-gate 	char holength[24];
8890Sstevel@tonic-gate 	char **av;
8900Sstevel@tonic-gate 	char *p;
8910Sstevel@tonic-gate 	int addrlen;
8920Sstevel@tonic-gate 	int malloced = 0;
8930Sstevel@tonic-gate 	size_t remlen;
8940Sstevel@tonic-gate 	const char *fieldsep = "@";
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	if (ho == NULL || len == NULL) {
8970Sstevel@tonic-gate 		errno = EINVAL;
8980Sstevel@tonic-gate 		return (-1);
8990Sstevel@tonic-gate 	}
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	switch(ho->h_addrtype) {
9020Sstevel@tonic-gate 	case AF_INET:
9030Sstevel@tonic-gate 		strcpy(hoaddrtype, "AF_INET");
9040Sstevel@tonic-gate 		break;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	case AF_INET6:
9070Sstevel@tonic-gate 		strcpy(hoaddrtype, "AF_INET6");
9080Sstevel@tonic-gate 		break;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	default:
9110Sstevel@tonic-gate 		errno = EINVAL;
9120Sstevel@tonic-gate 		return (-1);
9130Sstevel@tonic-gate 	}
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	sprintf(holength, "%d", ho->h_length);
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	need += strlen(ho->h_name) + 1;
9180Sstevel@tonic-gate 	need += joinlength(ho->h_aliases) + 1;
9190Sstevel@tonic-gate 	need += strlen(hoaddrtype) + 1;
9200Sstevel@tonic-gate 	need += strlen(holength) + 1;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	/* we determine an upper bound on the string length needed, not an
9230Sstevel@tonic-gate 	   exact length. */
924*11038SRao.Shoaib@Sun.COM 	addrlen = (ho->h_addrtype == AF_INET ? 16 : 46) ; /*%< XX other AF's?? */
9250Sstevel@tonic-gate 	for (av = ho->h_addr_list; av != NULL && *av != NULL ; av++)
9260Sstevel@tonic-gate 		need += addrlen;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	if (buffer == NULL) {
9290Sstevel@tonic-gate 		*len = need;
9300Sstevel@tonic-gate 		return (0);
9310Sstevel@tonic-gate 	}
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
9340Sstevel@tonic-gate 		errno = EINVAL;
9350Sstevel@tonic-gate 		return (-1);
9360Sstevel@tonic-gate 	}
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	if (*buffer == NULL) {
939*11038SRao.Shoaib@Sun.COM 		need += 2;		/*%< for CRLF */
9400Sstevel@tonic-gate 		*buffer = memget(need);
9410Sstevel@tonic-gate 		if (*buffer == NULL) {
9420Sstevel@tonic-gate 			errno = ENOMEM;
9430Sstevel@tonic-gate 			return (-1);
9440Sstevel@tonic-gate 		}
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 		*len = need;
9470Sstevel@tonic-gate 		malloced = 1;
9480Sstevel@tonic-gate 	}
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	strcpy(*buffer, ho->h_name);		strcat(*buffer, fieldsep);
9510Sstevel@tonic-gate 	joinarray(ho->h_aliases, *buffer, COMMA); strcat(*buffer, fieldsep);
9520Sstevel@tonic-gate 	strcat(*buffer, hoaddrtype);		strcat(*buffer, fieldsep);
9530Sstevel@tonic-gate 	strcat(*buffer, holength);		strcat(*buffer, fieldsep);
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	p = *buffer + strlen(*buffer);
9560Sstevel@tonic-gate 	remlen = need - strlen(*buffer);
9570Sstevel@tonic-gate 	for (av = ho->h_addr_list ; av != NULL && *av != NULL ; av++) {
9580Sstevel@tonic-gate 		if (inet_ntop(ho->h_addrtype, *av, p, remlen) == NULL) {
9590Sstevel@tonic-gate 			goto error;
9600Sstevel@tonic-gate 		}
9610Sstevel@tonic-gate 		if (*(av + 1) != NULL)
9620Sstevel@tonic-gate 			strcat(p, COMMASTR);
9630Sstevel@tonic-gate 		remlen -= strlen(p);
9640Sstevel@tonic-gate 		p += strlen(p);
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	return (0);
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate  error:
9710Sstevel@tonic-gate 	if (malloced) {
9720Sstevel@tonic-gate 		memput(*buffer, need);
9730Sstevel@tonic-gate 	}
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	return (-1);
9760Sstevel@tonic-gate }
9770Sstevel@tonic-gate 
978*11038SRao.Shoaib@Sun.COM /*%
9790Sstevel@tonic-gate  * int irp_unmarshall_ho(struct hostent *ho, char *buffer)
9800Sstevel@tonic-gate  *
981*11038SRao.Shoaib@Sun.COM  * notes: \li
9820Sstevel@tonic-gate  *
983*11038SRao.Shoaib@Sun.COM  *	See irpmarshall.h.
9840Sstevel@tonic-gate  *
985*11038SRao.Shoaib@Sun.COM  * return: \li
9860Sstevel@tonic-gate  *
9870Sstevel@tonic-gate  *	0 on success, -1 on failure.
9880Sstevel@tonic-gate  *
9890Sstevel@tonic-gate  */
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate int
irp_unmarshall_ho(struct hostent * ho,char * buffer)9920Sstevel@tonic-gate irp_unmarshall_ho(struct hostent *ho, char *buffer) {
9930Sstevel@tonic-gate 	char *p, *q, *r;
9940Sstevel@tonic-gate 	int hoaddrtype;
9950Sstevel@tonic-gate 	int holength;
9960Sstevel@tonic-gate 	long t;
997*11038SRao.Shoaib@Sun.COM 	char *name;
9980Sstevel@tonic-gate 	char **aliases = NULL;
9990Sstevel@tonic-gate 	char **hohaddrlist = NULL;
10000Sstevel@tonic-gate 	size_t hoaddrsize;
10010Sstevel@tonic-gate 	char tmpbuf[24];
10020Sstevel@tonic-gate 	char *tb;
10030Sstevel@tonic-gate 	char **alist;
10040Sstevel@tonic-gate 	int addrcount;
10050Sstevel@tonic-gate 	char fieldsep = '@';
10060Sstevel@tonic-gate 	int myerrno = EINVAL;
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	if (ho == NULL || buffer == NULL) {
10090Sstevel@tonic-gate 		errno = EINVAL;
10100Sstevel@tonic-gate 		return (-1);
10110Sstevel@tonic-gate 	}
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	p = buffer;
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	/* h_name field */
10160Sstevel@tonic-gate 	name = NULL;
1017*11038SRao.Shoaib@Sun.COM 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
10180Sstevel@tonic-gate 		goto error;
10190Sstevel@tonic-gate 	}
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	/* h_aliases field */
10230Sstevel@tonic-gate 	q = strchr(p, fieldsep);
10240Sstevel@tonic-gate 	if (q == NULL) {
10250Sstevel@tonic-gate 		goto error;
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
10280Sstevel@tonic-gate 	if (aliases == NULL) {
10290Sstevel@tonic-gate 		myerrno = errno;
10300Sstevel@tonic-gate 		goto error;
10310Sstevel@tonic-gate 	}
10320Sstevel@tonic-gate 	p = q + 1;
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	/* h_addrtype field */
10360Sstevel@tonic-gate 	tb = tmpbuf;
10370Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1038*11038SRao.Shoaib@Sun.COM 	    strlen(tb) == 0U) {
10390Sstevel@tonic-gate 		goto error;
10400Sstevel@tonic-gate 	}
10410Sstevel@tonic-gate 	if (strcmp(tmpbuf, "AF_INET") == 0)
10420Sstevel@tonic-gate 		hoaddrtype = AF_INET;
10430Sstevel@tonic-gate 	else if (strcmp(tmpbuf, "AF_INET6") == 0)
10440Sstevel@tonic-gate 		hoaddrtype = AF_INET6;
10450Sstevel@tonic-gate 	else
10460Sstevel@tonic-gate 		goto error;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	/* h_length field */
10500Sstevel@tonic-gate 	tb = tmpbuf;
10510Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1052*11038SRao.Shoaib@Sun.COM 	    strlen(tb) == 0U) {
10530Sstevel@tonic-gate 		goto error;
10540Sstevel@tonic-gate 	}
10550Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
10560Sstevel@tonic-gate 	if (*tb) {
1057*11038SRao.Shoaib@Sun.COM 		goto error;	/*%< junk in value */
10580Sstevel@tonic-gate 	}
10590Sstevel@tonic-gate 	holength = (int)t;
1060*11038SRao.Shoaib@Sun.COM 	if ((long) holength != t) {	/*%< value must have been too big. */
10610Sstevel@tonic-gate 		goto error;
10620Sstevel@tonic-gate 	}
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	/* h_addr_list field */
10660Sstevel@tonic-gate 	q = strchr(p, fieldsep);
10670Sstevel@tonic-gate 	if (q == NULL)
10680Sstevel@tonic-gate 		goto error;
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	/* count how many addresss are in there */
10710Sstevel@tonic-gate 	if (q > p + 1) {
10720Sstevel@tonic-gate 		for (addrcount = 1, r = p ; r != q ; r++) {
10730Sstevel@tonic-gate 			if (*r == COMMA)
10740Sstevel@tonic-gate 				addrcount++;
10750Sstevel@tonic-gate 		}
10760Sstevel@tonic-gate 	} else {
10770Sstevel@tonic-gate 		addrcount = 0;
10780Sstevel@tonic-gate 	}
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	hoaddrsize = (addrcount + 1) * sizeof (char *);
10810Sstevel@tonic-gate 	hohaddrlist = malloc(hoaddrsize);
10820Sstevel@tonic-gate 	if (hohaddrlist == NULL) {
10830Sstevel@tonic-gate 		myerrno = ENOMEM;
10840Sstevel@tonic-gate 		goto error;
10850Sstevel@tonic-gate 	}
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	memset(hohaddrlist, 0x0, hoaddrsize);
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	alist = hohaddrlist;
10900Sstevel@tonic-gate 	for (t = 0, r = p ; r != q ; p = r + 1, t++) {
10910Sstevel@tonic-gate 		char saved;
10920Sstevel@tonic-gate 		while (r != q && *r != COMMA) r++;
10930Sstevel@tonic-gate 		saved = *r;
10940Sstevel@tonic-gate 		*r = 0x0;
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 		alist[t] = malloc(hoaddrtype == AF_INET ? 4 : 16);
10970Sstevel@tonic-gate 		if (alist[t] == NULL) {
10980Sstevel@tonic-gate 			myerrno = ENOMEM;
10990Sstevel@tonic-gate 			goto error;
11000Sstevel@tonic-gate 		}
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 		if (inet_pton(hoaddrtype, p, alist[t]) == -1)
11030Sstevel@tonic-gate 			goto error;
11040Sstevel@tonic-gate 		*r = saved;
11050Sstevel@tonic-gate 	}
11060Sstevel@tonic-gate 	alist[t] = NULL;
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	ho->h_name = name;
11090Sstevel@tonic-gate 	ho->h_aliases = aliases;
11100Sstevel@tonic-gate 	ho->h_addrtype = hoaddrtype;
11110Sstevel@tonic-gate 	ho->h_length = holength;
11120Sstevel@tonic-gate 	ho->h_addr_list = hohaddrlist;
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	return (0);
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate  error:
11170Sstevel@tonic-gate 	errno = myerrno;
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	if (name != NULL) free(name);
1120*11038SRao.Shoaib@Sun.COM 	free_array(hohaddrlist, 0);
11210Sstevel@tonic-gate 	free_array(aliases, 0);
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	return (-1);
11240Sstevel@tonic-gate }
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate /* ------------------------- struct hostent------------------------- */
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct netgrp +++++++++++++++++++++++++ */
11310Sstevel@tonic-gate 
1132*11038SRao.Shoaib@Sun.COM /*%
11330Sstevel@tonic-gate  * int irp_marshall_ng(const char *host, const char *user,
11340Sstevel@tonic-gate  *		       const char *domain, char *buffer, size_t *len)
11350Sstevel@tonic-gate  *
1136*11038SRao.Shoaib@Sun.COM  * notes: \li
11370Sstevel@tonic-gate  *
11380Sstevel@tonic-gate  *	See note for irp_marshall_ng_start
11390Sstevel@tonic-gate  *
1140*11038SRao.Shoaib@Sun.COM  * return: \li
11410Sstevel@tonic-gate  *
11420Sstevel@tonic-gate  *	0 on success, 0 on failure.
11430Sstevel@tonic-gate  *
11440Sstevel@tonic-gate  */
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate int
irp_marshall_ng(const char * host,const char * user,const char * domain,char ** buffer,size_t * len)11470Sstevel@tonic-gate irp_marshall_ng(const char *host, const char *user, const char *domain,
11480Sstevel@tonic-gate 		char **buffer, size_t *len) {
1149*11038SRao.Shoaib@Sun.COM 	size_t need = 1; /*%< for nul byte */
11500Sstevel@tonic-gate 	const char *fieldsep = ",";
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	if (len == NULL) {
11530Sstevel@tonic-gate 		errno = EINVAL;
11540Sstevel@tonic-gate 		return (-1);
11550Sstevel@tonic-gate 	}
11560Sstevel@tonic-gate 
1157*11038SRao.Shoaib@Sun.COM 	need += 4;		       /*%< two parens and two commas */
11580Sstevel@tonic-gate 	need += (host == NULL ? 0 : strlen(host));
11590Sstevel@tonic-gate 	need += (user == NULL ? 0 : strlen(user));
11600Sstevel@tonic-gate 	need += (domain == NULL ? 0 : strlen(domain));
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	if (buffer == NULL) {
11630Sstevel@tonic-gate 		*len = need;
11640Sstevel@tonic-gate 		return (0);
11650Sstevel@tonic-gate 	} else if (*buffer != NULL && need > *len) {
11660Sstevel@tonic-gate 		errno = EINVAL;
11670Sstevel@tonic-gate 		return (-1);
11680Sstevel@tonic-gate 	}
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	if (*buffer == NULL) {
1171*11038SRao.Shoaib@Sun.COM 		need += 2;		/*%< for CRLF */
11720Sstevel@tonic-gate 		*buffer = memget(need);
11730Sstevel@tonic-gate 		if (*buffer == NULL) {
11740Sstevel@tonic-gate 			errno = ENOMEM;
11750Sstevel@tonic-gate 			return (-1);
11760Sstevel@tonic-gate 		}
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 		*len = need;
11790Sstevel@tonic-gate 	}
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	(*buffer)[0] = '(';
11820Sstevel@tonic-gate 	(*buffer)[1] = '\0';
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	if (host != NULL)
11850Sstevel@tonic-gate 		strcat(*buffer, host);
11860Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	if (user != NULL)
11890Sstevel@tonic-gate 		strcat(*buffer, user);
11900Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	if (domain != NULL)
11930Sstevel@tonic-gate 		strcat(*buffer, domain);
11940Sstevel@tonic-gate 	strcat(*buffer, ")");
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	return (0);
11970Sstevel@tonic-gate }
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate /* ---------- */
12020Sstevel@tonic-gate 
1203*11038SRao.Shoaib@Sun.COM /*%
12040Sstevel@tonic-gate  * int irp_unmarshall_ng(const char **host, const char **user,
12050Sstevel@tonic-gate  *			 const char **domain, char *buffer)
12060Sstevel@tonic-gate  *
1207*11038SRao.Shoaib@Sun.COM  * notes: \li
12080Sstevel@tonic-gate  *
12090Sstevel@tonic-gate  *	Unpacks the BUFFER into 3 character arrays it allocates and assigns
12100Sstevel@tonic-gate  *	to *HOST, *USER and *DOMAIN. If any field of the value is empty,
12110Sstevel@tonic-gate  *	then the corresponding paramater value will be set to NULL.
12120Sstevel@tonic-gate  *
1213*11038SRao.Shoaib@Sun.COM  * return: \li
12140Sstevel@tonic-gate  *
12150Sstevel@tonic-gate  *	0 on success and -1 on failure.
12160Sstevel@tonic-gate  */
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate int
irp_unmarshall_ng(const char ** hostp,const char ** userp,const char ** domainp,char * buffer)12190Sstevel@tonic-gate irp_unmarshall_ng(const char **hostp, const char **userp, const char **domainp,
12200Sstevel@tonic-gate 		  char *buffer)
12210Sstevel@tonic-gate {
12220Sstevel@tonic-gate 	char *p, *q;
12230Sstevel@tonic-gate 	char fieldsep = ',';
12240Sstevel@tonic-gate 	int myerrno = EINVAL;
12250Sstevel@tonic-gate 	char *host, *user, *domain;
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	if (userp == NULL || hostp == NULL ||
12280Sstevel@tonic-gate 	    domainp == NULL || buffer == NULL) {
12290Sstevel@tonic-gate 		errno = EINVAL;
12300Sstevel@tonic-gate 		return (-1);
12310Sstevel@tonic-gate 	}
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	host = user = domain = NULL;
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	p = buffer;
12360Sstevel@tonic-gate 	while (isspace((unsigned char)*p)) {
12370Sstevel@tonic-gate 		p++;
12380Sstevel@tonic-gate 	}
12390Sstevel@tonic-gate 	if (*p != '(') {
12400Sstevel@tonic-gate 		goto error;
12410Sstevel@tonic-gate 	}
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	q = p + 1;
12440Sstevel@tonic-gate 	while (*q && *q != fieldsep)
12450Sstevel@tonic-gate 		q++;
12460Sstevel@tonic-gate 	if (!*q) {
12470Sstevel@tonic-gate 		goto error;
12480Sstevel@tonic-gate 	} else if (q > p + 1) {
12490Sstevel@tonic-gate 		host = strndup(p, q - p);
12500Sstevel@tonic-gate 	}
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	p = q + 1;
12530Sstevel@tonic-gate 	if (!*p) {
12540Sstevel@tonic-gate 		goto error;
12550Sstevel@tonic-gate 	} else if (*p != fieldsep) {
12560Sstevel@tonic-gate 		q = p + 1;
12570Sstevel@tonic-gate 		while (*q && *q != fieldsep)
12580Sstevel@tonic-gate 			q++;
12590Sstevel@tonic-gate 		if (!*q) {
12600Sstevel@tonic-gate 			goto error;
12610Sstevel@tonic-gate 		}
12620Sstevel@tonic-gate 		user = strndup(p, q - p);
12630Sstevel@tonic-gate 	} else {
12640Sstevel@tonic-gate 		p++;
12650Sstevel@tonic-gate 	}
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	if (!*p) {
12680Sstevel@tonic-gate 		goto error;
12690Sstevel@tonic-gate 	} else if (*p != ')') {
12700Sstevel@tonic-gate 		q = p + 1;
12710Sstevel@tonic-gate 		while (*q && *q != ')')
12720Sstevel@tonic-gate 			q++;
12730Sstevel@tonic-gate 		if (!*q) {
12740Sstevel@tonic-gate 			goto error;
12750Sstevel@tonic-gate 		}
12760Sstevel@tonic-gate 		domain = strndup(p, q - p);
12770Sstevel@tonic-gate 	}
12780Sstevel@tonic-gate 	*hostp = host;
12790Sstevel@tonic-gate 	*userp = user;
12800Sstevel@tonic-gate 	*domainp = domain;
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	return (0);
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate  error:
12850Sstevel@tonic-gate 	errno = myerrno;
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 	if (host != NULL) free(host);
12880Sstevel@tonic-gate 	if (user != NULL) free(user);
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	return (-1);
12910Sstevel@tonic-gate }
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate /* ------------------------- struct netgrp ------------------------- */
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct nwent +++++++++++++++++++++++++ */
12990Sstevel@tonic-gate 
1300*11038SRao.Shoaib@Sun.COM /*%
13010Sstevel@tonic-gate  * int irp_marshall_nw(struct nwent *ne, char **buffer, size_t *len)
13020Sstevel@tonic-gate  *
1303*11038SRao.Shoaib@Sun.COM  * notes: \li
13040Sstevel@tonic-gate  *
13050Sstevel@tonic-gate  *	See at top.
13060Sstevel@tonic-gate  *
1307*11038SRao.Shoaib@Sun.COM  * return: \li
13080Sstevel@tonic-gate  *
13090Sstevel@tonic-gate  *	0 on success and -1 on failure.
13100Sstevel@tonic-gate  *
13110Sstevel@tonic-gate  */
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate int
irp_marshall_nw(struct nwent * ne,char ** buffer,size_t * len)13140Sstevel@tonic-gate irp_marshall_nw(struct nwent *ne, char **buffer, size_t *len) {
1315*11038SRao.Shoaib@Sun.COM 	size_t need = 1;	/*%< for null byte */
13160Sstevel@tonic-gate 	char nAddrType[24];
13170Sstevel@tonic-gate 	char nNet[MAXPADDRSIZE];
13180Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	if (ne == NULL || len == NULL) {
13210Sstevel@tonic-gate 		return (-1);
13220Sstevel@tonic-gate 	}
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	strcpy(nAddrType, ADDR_T_STR(ne->n_addrtype));
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	if (inet_net_ntop(ne->n_addrtype, ne->n_addr, ne->n_length,
13270Sstevel@tonic-gate 			  nNet, sizeof nNet) == NULL) {
13280Sstevel@tonic-gate 		return (-1);
13290Sstevel@tonic-gate 	}
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	need += strlen(ne->n_name) + 1;
13330Sstevel@tonic-gate 	need += joinlength(ne->n_aliases) + 1;
13340Sstevel@tonic-gate 	need += strlen(nAddrType) + 1;
13350Sstevel@tonic-gate 	need += strlen(nNet) + 1;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 	if (buffer == NULL) {
13380Sstevel@tonic-gate 		*len = need;
13390Sstevel@tonic-gate 		return (0);
13400Sstevel@tonic-gate 	}
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
13430Sstevel@tonic-gate 		errno = EINVAL;
13440Sstevel@tonic-gate 		return (-1);
13450Sstevel@tonic-gate 	}
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	if (*buffer == NULL) {
1348*11038SRao.Shoaib@Sun.COM 		need += 2;		/*%< for CRLF */
13490Sstevel@tonic-gate 		*buffer = memget(need);
13500Sstevel@tonic-gate 		if (*buffer == NULL) {
13510Sstevel@tonic-gate 			errno = ENOMEM;
13520Sstevel@tonic-gate 			return (-1);
13530Sstevel@tonic-gate 		}
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 		*len = need;
13560Sstevel@tonic-gate 	}
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	strcpy(*buffer, ne->n_name);		strcat(*buffer, fieldsep);
13590Sstevel@tonic-gate 	joinarray(ne->n_aliases, *buffer, COMMA) ; strcat(*buffer, fieldsep);
13600Sstevel@tonic-gate 	strcat(*buffer, nAddrType);		strcat(*buffer, fieldsep);
13610Sstevel@tonic-gate 	strcat(*buffer, nNet);			strcat(*buffer, fieldsep);
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	return (0);
13640Sstevel@tonic-gate }
13650Sstevel@tonic-gate 
1366*11038SRao.Shoaib@Sun.COM /*%
13670Sstevel@tonic-gate  * int irp_unmarshall_nw(struct nwent *ne, char *buffer)
13680Sstevel@tonic-gate  *
1369*11038SRao.Shoaib@Sun.COM  * notes: \li
13700Sstevel@tonic-gate  *
13710Sstevel@tonic-gate  *	See note up top.
13720Sstevel@tonic-gate  *
1373*11038SRao.Shoaib@Sun.COM  * return: \li
13740Sstevel@tonic-gate  *
13750Sstevel@tonic-gate  *	0 on success and -1 on failure.
13760Sstevel@tonic-gate  *
13770Sstevel@tonic-gate  */
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate int
irp_unmarshall_nw(struct nwent * ne,char * buffer)13800Sstevel@tonic-gate irp_unmarshall_nw(struct nwent *ne, char *buffer) {
13810Sstevel@tonic-gate 	char *p, *q;
13820Sstevel@tonic-gate 	int naddrtype;
13830Sstevel@tonic-gate 	long nnet;
13840Sstevel@tonic-gate 	int bits;
13850Sstevel@tonic-gate 	char *name = NULL;
13860Sstevel@tonic-gate 	char **aliases = NULL;
13870Sstevel@tonic-gate 	char tmpbuf[24];
13880Sstevel@tonic-gate 	char *tb;
13890Sstevel@tonic-gate 	char fieldsep = ':';
13900Sstevel@tonic-gate 	int myerrno = EINVAL;
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	if (ne == NULL || buffer == NULL) {
13930Sstevel@tonic-gate 		goto error;
13940Sstevel@tonic-gate 	}
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	p = buffer;
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	/* n_name field */
13990Sstevel@tonic-gate 	name = NULL;
1400*11038SRao.Shoaib@Sun.COM 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
14010Sstevel@tonic-gate 		goto error;
14020Sstevel@tonic-gate 	}
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	/* n_aliases field. Aliases are separated by commas */
14060Sstevel@tonic-gate 	q = strchr(p, fieldsep);
14070Sstevel@tonic-gate 	if (q == NULL) {
14080Sstevel@tonic-gate 		goto error;
14090Sstevel@tonic-gate 	}
14100Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
14110Sstevel@tonic-gate 	if (aliases == NULL) {
14120Sstevel@tonic-gate 		myerrno = errno;
14130Sstevel@tonic-gate 		goto error;
14140Sstevel@tonic-gate 	}
14150Sstevel@tonic-gate 	p = q + 1;
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 	/* h_addrtype field */
14190Sstevel@tonic-gate 	tb = tmpbuf;
14200Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1421*11038SRao.Shoaib@Sun.COM 	    strlen(tb) == 0U) {
14220Sstevel@tonic-gate 		goto error;
14230Sstevel@tonic-gate 	}
14240Sstevel@tonic-gate 	if (strcmp(tmpbuf, "AF_INET") == 0)
14250Sstevel@tonic-gate 		naddrtype = AF_INET;
14260Sstevel@tonic-gate 	else if (strcmp(tmpbuf, "AF_INET6") == 0)
14270Sstevel@tonic-gate 		naddrtype = AF_INET6;
14280Sstevel@tonic-gate 	else
14290Sstevel@tonic-gate 		goto error;
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	/* n_net field */
14330Sstevel@tonic-gate 	tb = tmpbuf;
14340Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1435*11038SRao.Shoaib@Sun.COM 	    strlen(tb) == 0U) {
14360Sstevel@tonic-gate 		goto error;
14370Sstevel@tonic-gate 	}
14380Sstevel@tonic-gate 	nnet = 0;
14390Sstevel@tonic-gate 	bits = inet_net_pton(naddrtype, tmpbuf, &nnet, sizeof nnet);
14400Sstevel@tonic-gate 	if (bits < 0) {
14410Sstevel@tonic-gate 		goto error;
14420Sstevel@tonic-gate 	}
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 	/* nnet = ntohl(nnet); */ /* keep in network order for nwent */
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 	ne->n_name = name;
14470Sstevel@tonic-gate 	ne->n_aliases = aliases;
14480Sstevel@tonic-gate 	ne->n_addrtype = naddrtype;
14490Sstevel@tonic-gate 	ne->n_length = bits;
14500Sstevel@tonic-gate 	ne->n_addr = malloc(sizeof nnet);
14510Sstevel@tonic-gate 	if (ne->n_addr == NULL) {
14520Sstevel@tonic-gate 		goto error;
14530Sstevel@tonic-gate 	}
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	memcpy(ne->n_addr, &nnet, sizeof nnet);
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 	return (0);
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate  error:
14600Sstevel@tonic-gate 	errno = myerrno;
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 	if (name != NULL) free(name);
14630Sstevel@tonic-gate 	free_array(aliases, 0);
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	return (-1);
14660Sstevel@tonic-gate }
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate /* ------------------------- struct nwent ------------------------- */
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct netent +++++++++++++++++++++++++ */
14730Sstevel@tonic-gate 
1474*11038SRao.Shoaib@Sun.COM /*%
14750Sstevel@tonic-gate  * int irp_marshall_ne(struct netent *ne, char **buffer, size_t *len)
14760Sstevel@tonic-gate  *
1477*11038SRao.Shoaib@Sun.COM  * notes: \li
14780Sstevel@tonic-gate  *
14790Sstevel@tonic-gate  *	See at top.
14800Sstevel@tonic-gate  *
1481*11038SRao.Shoaib@Sun.COM  * return: \li
14820Sstevel@tonic-gate  *
14830Sstevel@tonic-gate  *	0 on success and -1 on failure.
14840Sstevel@tonic-gate  *
14850Sstevel@tonic-gate  */
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate int
irp_marshall_ne(struct netent * ne,char ** buffer,size_t * len)14880Sstevel@tonic-gate irp_marshall_ne(struct netent *ne, char **buffer, size_t *len) {
1489*11038SRao.Shoaib@Sun.COM 	size_t need = 1;	/*%< for null byte */
14900Sstevel@tonic-gate 	char nAddrType[24];
14910Sstevel@tonic-gate 	char nNet[MAXPADDRSIZE];
14920Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
14930Sstevel@tonic-gate 	long nval;
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	if (ne == NULL || len == NULL) {
14960Sstevel@tonic-gate 		return (-1);
14970Sstevel@tonic-gate 	}
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	strcpy(nAddrType, ADDR_T_STR(ne->n_addrtype));
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	nval = htonl(ne->n_net);
15020Sstevel@tonic-gate 	if (inet_ntop(ne->n_addrtype, &nval, nNet, sizeof nNet) == NULL) {
15030Sstevel@tonic-gate 		return (-1);
15040Sstevel@tonic-gate 	}
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 	need += strlen(ne->n_name) + 1;
15070Sstevel@tonic-gate 	need += joinlength(ne->n_aliases) + 1;
15080Sstevel@tonic-gate 	need += strlen(nAddrType) + 1;
15090Sstevel@tonic-gate 	need += strlen(nNet) + 1;
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	if (buffer == NULL) {
15120Sstevel@tonic-gate 		*len = need;
15130Sstevel@tonic-gate 		return (0);
15140Sstevel@tonic-gate 	}
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
15170Sstevel@tonic-gate 		errno = EINVAL;
15180Sstevel@tonic-gate 		return (-1);
15190Sstevel@tonic-gate 	}
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 	if (*buffer == NULL) {
1522*11038SRao.Shoaib@Sun.COM 		need += 2;		/*%< for CRLF */
15230Sstevel@tonic-gate 		*buffer = memget(need);
15240Sstevel@tonic-gate 		if (*buffer == NULL) {
15250Sstevel@tonic-gate 			errno = ENOMEM;
15260Sstevel@tonic-gate 			return (-1);
15270Sstevel@tonic-gate 		}
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 		*len = need;
15300Sstevel@tonic-gate 	}
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	strcpy(*buffer, ne->n_name);		strcat(*buffer, fieldsep);
15330Sstevel@tonic-gate 	joinarray(ne->n_aliases, *buffer, COMMA) ; strcat(*buffer, fieldsep);
15340Sstevel@tonic-gate 	strcat(*buffer, nAddrType);		strcat(*buffer, fieldsep);
15350Sstevel@tonic-gate 	strcat(*buffer, nNet);			strcat(*buffer, fieldsep);
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	return (0);
15380Sstevel@tonic-gate }
15390Sstevel@tonic-gate 
1540*11038SRao.Shoaib@Sun.COM /*%
15410Sstevel@tonic-gate  * int irp_unmarshall_ne(struct netent *ne, char *buffer)
15420Sstevel@tonic-gate  *
1543*11038SRao.Shoaib@Sun.COM  * notes: \li
15440Sstevel@tonic-gate  *
15450Sstevel@tonic-gate  *	See note up top.
15460Sstevel@tonic-gate  *
1547*11038SRao.Shoaib@Sun.COM  * return: \li
15480Sstevel@tonic-gate  *
15490Sstevel@tonic-gate  *	0 on success and -1 on failure.
15500Sstevel@tonic-gate  *
15510Sstevel@tonic-gate  */
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate int
irp_unmarshall_ne(struct netent * ne,char * buffer)15540Sstevel@tonic-gate irp_unmarshall_ne(struct netent *ne, char *buffer) {
15550Sstevel@tonic-gate 	char *p, *q;
15560Sstevel@tonic-gate 	int naddrtype;
15570Sstevel@tonic-gate 	long nnet;
15580Sstevel@tonic-gate 	int bits;
15590Sstevel@tonic-gate 	char *name = NULL;
15600Sstevel@tonic-gate 	char **aliases = NULL;
15610Sstevel@tonic-gate 	char tmpbuf[24];
15620Sstevel@tonic-gate 	char *tb;
15630Sstevel@tonic-gate 	char fieldsep = ':';
15640Sstevel@tonic-gate 	int myerrno = EINVAL;
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	if (ne == NULL || buffer == NULL) {
15670Sstevel@tonic-gate 		goto error;
15680Sstevel@tonic-gate 	}
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	p = buffer;
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	/* n_name field */
15730Sstevel@tonic-gate 	name = NULL;
1574*11038SRao.Shoaib@Sun.COM 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) {
15750Sstevel@tonic-gate 		goto error;
15760Sstevel@tonic-gate 	}
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	/* n_aliases field. Aliases are separated by commas */
15800Sstevel@tonic-gate 	q = strchr(p, fieldsep);
15810Sstevel@tonic-gate 	if (q == NULL) {
15820Sstevel@tonic-gate 		goto error;
15830Sstevel@tonic-gate 	}
15840Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
15850Sstevel@tonic-gate 	if (aliases == NULL) {
15860Sstevel@tonic-gate 		myerrno = errno;
15870Sstevel@tonic-gate 		goto error;
15880Sstevel@tonic-gate 	}
15890Sstevel@tonic-gate 	p = q + 1;
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	/* h_addrtype field */
15930Sstevel@tonic-gate 	tb = tmpbuf;
15940Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1595*11038SRao.Shoaib@Sun.COM 	    strlen(tb) == 0U) {
15960Sstevel@tonic-gate 		goto error;
15970Sstevel@tonic-gate 	}
15980Sstevel@tonic-gate 	if (strcmp(tmpbuf, "AF_INET") == 0)
15990Sstevel@tonic-gate 		naddrtype = AF_INET;
16000Sstevel@tonic-gate 	else if (strcmp(tmpbuf, "AF_INET6") == 0)
16010Sstevel@tonic-gate 		naddrtype = AF_INET6;
16020Sstevel@tonic-gate 	else
16030Sstevel@tonic-gate 		goto error;
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 	/* n_net field */
16070Sstevel@tonic-gate 	tb = tmpbuf;
16080Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1609*11038SRao.Shoaib@Sun.COM 	    strlen(tb) == 0U) {
16100Sstevel@tonic-gate 		goto error;
16110Sstevel@tonic-gate 	}
16120Sstevel@tonic-gate 	bits = inet_net_pton(naddrtype, tmpbuf, &nnet, sizeof nnet);
16130Sstevel@tonic-gate 	if (bits < 0) {
16140Sstevel@tonic-gate 		goto error;
16150Sstevel@tonic-gate 	}
16160Sstevel@tonic-gate 	nnet = ntohl(nnet);
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	ne->n_name = name;
16190Sstevel@tonic-gate 	ne->n_aliases = aliases;
16200Sstevel@tonic-gate 	ne->n_addrtype = naddrtype;
16210Sstevel@tonic-gate 	ne->n_net = nnet;
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	return (0);
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate  error:
16260Sstevel@tonic-gate 	errno = myerrno;
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	if (name != NULL) free(name);
16290Sstevel@tonic-gate 	free_array(aliases, 0);
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	return (-1);
16320Sstevel@tonic-gate }
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate /* ------------------------- struct netent ------------------------- */
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate /* =========================================================================== */
16390Sstevel@tonic-gate 
1640*11038SRao.Shoaib@Sun.COM /*%
16410Sstevel@tonic-gate  * static char ** splitarray(const char *buffer, const char *buffend, char delim)
16420Sstevel@tonic-gate  *
1643*11038SRao.Shoaib@Sun.COM  * notes: \li
16440Sstevel@tonic-gate  *
16450Sstevel@tonic-gate  *	Split a delim separated astring. Not allowed
16460Sstevel@tonic-gate  *	to have two delims next to each other. BUFFER points to begining of
16470Sstevel@tonic-gate  *	string, BUFFEND points to one past the end of the string
16480Sstevel@tonic-gate  *	(i.e. points at where the null byte would be if null
16490Sstevel@tonic-gate  *	terminated).
16500Sstevel@tonic-gate  *
1651*11038SRao.Shoaib@Sun.COM  * return: \li
16520Sstevel@tonic-gate  *
16530Sstevel@tonic-gate  *	Returns a malloced array of pointers, each pointer pointing to a
16540Sstevel@tonic-gate  *	malloced string. If BUFEER is an empty string, then return values is
16550Sstevel@tonic-gate  *	array of 1 pointer that is NULL. Returns NULL on failure.
16560Sstevel@tonic-gate  *
16570Sstevel@tonic-gate  */
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate static char **
splitarray(const char * buffer,const char * buffend,char delim)16600Sstevel@tonic-gate splitarray(const char *buffer, const char *buffend, char delim) {
16610Sstevel@tonic-gate 	const char *p, *q;
16620Sstevel@tonic-gate 	int count = 0;
16630Sstevel@tonic-gate 	char **arr = NULL;
16640Sstevel@tonic-gate 	char **aptr;
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	if (buffend < buffer)
16670Sstevel@tonic-gate 		return (NULL);
16680Sstevel@tonic-gate 	else if (buffend > buffer && *buffer == delim)
16690Sstevel@tonic-gate 		return (NULL);
16700Sstevel@tonic-gate 	else if (buffend > buffer && *(buffend - 1) == delim)
16710Sstevel@tonic-gate 		return (NULL);
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	/* count the number of field and make sure none are empty */
16740Sstevel@tonic-gate 	if (buffend > buffer + 1) {
16750Sstevel@tonic-gate 		for (count = 1, q = buffer ; q != buffend ; q++) {
16760Sstevel@tonic-gate 			if (*q == delim) {
16770Sstevel@tonic-gate 				if (q > buffer && (*(q - 1) == delim)) {
16780Sstevel@tonic-gate 					errno = EINVAL;
16790Sstevel@tonic-gate 					return (NULL);
16800Sstevel@tonic-gate 				}
16810Sstevel@tonic-gate 				count++;
16820Sstevel@tonic-gate 			}
16830Sstevel@tonic-gate 		}
16840Sstevel@tonic-gate 	}
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	if (count > 0) {
1687*11038SRao.Shoaib@Sun.COM 		count++ ;		/*%< for NULL at end */
16880Sstevel@tonic-gate 		aptr = arr = malloc(count * sizeof (char *));
16890Sstevel@tonic-gate 		if (aptr == NULL) {
16900Sstevel@tonic-gate 			 errno = ENOMEM;
16910Sstevel@tonic-gate 			 return (NULL);
16920Sstevel@tonic-gate 		 }
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 		memset(arr, 0x0, count * sizeof (char *));
16950Sstevel@tonic-gate 		for (p = buffer ; p < buffend ; p++) {
16960Sstevel@tonic-gate 			for (q = p ; *q != delim && q != buffend ; q++)
16970Sstevel@tonic-gate 				/* nothing */;
16980Sstevel@tonic-gate 			*aptr = strndup(p, q - p);
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 			p = q;
17010Sstevel@tonic-gate 			aptr++;
17020Sstevel@tonic-gate 		}
17030Sstevel@tonic-gate 		*aptr = NULL;
17040Sstevel@tonic-gate 	} else {
17050Sstevel@tonic-gate 		arr = malloc(sizeof (char *));
17060Sstevel@tonic-gate 		if (arr == NULL) {
17070Sstevel@tonic-gate 			errno = ENOMEM;
17080Sstevel@tonic-gate 			return (NULL);
17090Sstevel@tonic-gate 		}
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 		*arr = NULL;
17120Sstevel@tonic-gate 	}
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	return (arr);
17150Sstevel@tonic-gate }
17160Sstevel@tonic-gate 
1717*11038SRao.Shoaib@Sun.COM /*%
17180Sstevel@tonic-gate  * static size_t joinlength(char * const *argv)
17190Sstevel@tonic-gate  *
1720*11038SRao.Shoaib@Sun.COM  * return: \li
17210Sstevel@tonic-gate  *
17220Sstevel@tonic-gate  *	the number of bytes in all the arrays pointed at
17230Sstevel@tonic-gate  *	by argv, including their null bytes(which will usually be turned
17240Sstevel@tonic-gate  *	into commas).
17250Sstevel@tonic-gate  *
17260Sstevel@tonic-gate  *
17270Sstevel@tonic-gate  */
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate static size_t
joinlength(char * const * argv)17300Sstevel@tonic-gate joinlength(char * const *argv) {
17310Sstevel@tonic-gate 	int len = 0;
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 	while (argv && *argv) {
17340Sstevel@tonic-gate 		len += (strlen(*argv) + 1);
17350Sstevel@tonic-gate 		argv++;
17360Sstevel@tonic-gate 	}
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 	return (len);
17390Sstevel@tonic-gate }
17400Sstevel@tonic-gate 
1741*11038SRao.Shoaib@Sun.COM /*%
17420Sstevel@tonic-gate  * int joinarray(char * const *argv, char *buffer, char delim)
17430Sstevel@tonic-gate  *
1744*11038SRao.Shoaib@Sun.COM  * notes: \li
17450Sstevel@tonic-gate  *
17460Sstevel@tonic-gate  *	Copy all the ARGV strings into the end of BUFFER
17470Sstevel@tonic-gate  *	separating them with DELIM.  BUFFER is assumed to have
17480Sstevel@tonic-gate  *	enough space to hold everything and to be already null-terminated.
17490Sstevel@tonic-gate  *
1750*11038SRao.Shoaib@Sun.COM  * return: \li
17510Sstevel@tonic-gate  *
17520Sstevel@tonic-gate  *	0 unless argv or buffer is NULL.
17530Sstevel@tonic-gate  *
17540Sstevel@tonic-gate  *
17550Sstevel@tonic-gate  */
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate static int
joinarray(char * const * argv,char * buffer,char delim)17580Sstevel@tonic-gate joinarray(char * const *argv, char *buffer, char delim) {
17590Sstevel@tonic-gate 	char * const *p;
17600Sstevel@tonic-gate 	char sep[2];
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	if (argv == NULL || buffer == NULL) {
17630Sstevel@tonic-gate 		errno = EINVAL;
17640Sstevel@tonic-gate 		return (-1);
17650Sstevel@tonic-gate 	}
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 	sep[0] = delim;
17680Sstevel@tonic-gate 	sep[1] = 0x0;
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 	for (p = argv ; *p != NULL ; p++) {
17710Sstevel@tonic-gate 		strcat(buffer, *p);
17720Sstevel@tonic-gate 		if (*(p + 1) != NULL) {
17730Sstevel@tonic-gate 			strcat(buffer, sep);
17740Sstevel@tonic-gate 		}
17750Sstevel@tonic-gate 	}
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	return (0);
17780Sstevel@tonic-gate }
17790Sstevel@tonic-gate 
1780*11038SRao.Shoaib@Sun.COM /*%
17810Sstevel@tonic-gate  * static char * getfield(char **res, size_t reslen, char **ptr, char delim)
17820Sstevel@tonic-gate  *
1783*11038SRao.Shoaib@Sun.COM  * notes: \li
17840Sstevel@tonic-gate  *
17850Sstevel@tonic-gate  *	Stores in *RES, which is a buffer of length RESLEN, a
17860Sstevel@tonic-gate  *	copy of the bytes from *PTR up to and including the first
17870Sstevel@tonic-gate  *	instance of DELIM. If *RES is NULL, then it will be
17880Sstevel@tonic-gate  *	assigned a malloced buffer to hold the copy. *PTR is
17890Sstevel@tonic-gate  *	modified to point at the found delimiter.
17900Sstevel@tonic-gate  *
1791*11038SRao.Shoaib@Sun.COM  * return: \li
17920Sstevel@tonic-gate  *
17930Sstevel@tonic-gate  *	If there was no delimiter, then NULL is returned,
17940Sstevel@tonic-gate  *	otherewise *RES is returned.
17950Sstevel@tonic-gate  *
17960Sstevel@tonic-gate  */
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate static char *
getfield(char ** res,size_t reslen,char ** ptr,char delim)17990Sstevel@tonic-gate getfield(char **res, size_t reslen, char **ptr, char delim) {
18000Sstevel@tonic-gate 	char *q;
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 	if (res == NULL || ptr == NULL || *ptr == NULL) {
18030Sstevel@tonic-gate 		errno = EINVAL;
18040Sstevel@tonic-gate 		return (NULL);
18050Sstevel@tonic-gate 	}
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate 	q = strchr(*ptr, delim);
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 	if (q == NULL) {
18100Sstevel@tonic-gate 		errno = EINVAL;
18110Sstevel@tonic-gate 		return (NULL);
18120Sstevel@tonic-gate 	} else {
18130Sstevel@tonic-gate 		if (*res == NULL) {
18140Sstevel@tonic-gate 			*res = strndup(*ptr, q - *ptr);
18150Sstevel@tonic-gate 		} else {
1816*11038SRao.Shoaib@Sun.COM 			if ((size_t)(q - *ptr + 1) > reslen) { /*%< to big for res */
18170Sstevel@tonic-gate 				errno = EINVAL;
18180Sstevel@tonic-gate 				return (NULL);
18190Sstevel@tonic-gate 			} else {
18200Sstevel@tonic-gate 				strncpy(*res, *ptr, q - *ptr);
18210Sstevel@tonic-gate 				(*res)[q - *ptr] = 0x0;
18220Sstevel@tonic-gate 			}
18230Sstevel@tonic-gate 		}
18240Sstevel@tonic-gate 		*ptr = q + 1;
18250Sstevel@tonic-gate 	}
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 	return (*res);
18280Sstevel@tonic-gate }
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate 
1834*11038SRao.Shoaib@Sun.COM #ifndef HAVE_STRNDUP
18350Sstevel@tonic-gate /*
18360Sstevel@tonic-gate  * static char * strndup(const char *str, size_t len)
18370Sstevel@tonic-gate  *
1838*11038SRao.Shoaib@Sun.COM  * notes: \li
18390Sstevel@tonic-gate  *
18400Sstevel@tonic-gate  *	like strdup, except do len bytes instead of the whole string. Always
18410Sstevel@tonic-gate  *	null-terminates.
18420Sstevel@tonic-gate  *
1843*11038SRao.Shoaib@Sun.COM  * return: \li
18440Sstevel@tonic-gate  *
18450Sstevel@tonic-gate  *	The newly malloced string.
18460Sstevel@tonic-gate  *
18470Sstevel@tonic-gate  */
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate static char *
strndup(const char * str,size_t len)18500Sstevel@tonic-gate strndup(const char *str, size_t len) {
18510Sstevel@tonic-gate 	char *p = malloc(len + 1);
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	if (p == NULL)
18540Sstevel@tonic-gate 		return (NULL);
18550Sstevel@tonic-gate 	strncpy(p, str, len);
18560Sstevel@tonic-gate 	p[len] = 0x0;
18570Sstevel@tonic-gate 	return (p);
18580Sstevel@tonic-gate }
1859*11038SRao.Shoaib@Sun.COM #endif
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate #if WANT_MAIN
18620Sstevel@tonic-gate 
1863*11038SRao.Shoaib@Sun.COM /*%
18640Sstevel@tonic-gate  * static int strcmp_nws(const char *a, const char *b)
18650Sstevel@tonic-gate  *
1866*11038SRao.Shoaib@Sun.COM  * notes: \li
18670Sstevel@tonic-gate  *
18680Sstevel@tonic-gate  *	do a strcmp, except uneven lengths of whitespace compare the same
18690Sstevel@tonic-gate  *
1870*11038SRao.Shoaib@Sun.COM  * return: \li
18710Sstevel@tonic-gate  *
18720Sstevel@tonic-gate  */
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate static int
strcmp_nws(const char * a,const char * b)18750Sstevel@tonic-gate strcmp_nws(const char *a, const char *b) {
18760Sstevel@tonic-gate 	while (*a && *b) {
18770Sstevel@tonic-gate 		if (isspace(*a) && isspace(*b)) {
18780Sstevel@tonic-gate 			do {
18790Sstevel@tonic-gate 				a++;
18800Sstevel@tonic-gate 			} while (isspace(*a));
18810Sstevel@tonic-gate 			do {
18820Sstevel@tonic-gate 				b++;
18830Sstevel@tonic-gate 			} while (isspace(*b));
18840Sstevel@tonic-gate 		}
18850Sstevel@tonic-gate 		if (*a < *b)
18860Sstevel@tonic-gate 			return (-1);
18870Sstevel@tonic-gate 		else if (*a > *b)
18880Sstevel@tonic-gate 			return (1);
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 		a++;
18910Sstevel@tonic-gate 		b++;;
18920Sstevel@tonic-gate 	}
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 	if (*a == *b)
18950Sstevel@tonic-gate 		return (0);
18960Sstevel@tonic-gate 	else if (*a > *b)
18970Sstevel@tonic-gate 		return (1);
18980Sstevel@tonic-gate 	else
18990Sstevel@tonic-gate 		return (-1);
19000Sstevel@tonic-gate }
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate #endif
19030Sstevel@tonic-gate 
1904*11038SRao.Shoaib@Sun.COM /*%
19050Sstevel@tonic-gate  * static void free_array(char **argv, size_t entries)
19060Sstevel@tonic-gate  *
1907*11038SRao.Shoaib@Sun.COM  * notes: \li
19080Sstevel@tonic-gate  *
19090Sstevel@tonic-gate  *	Free argv and each of the pointers inside it. The end of
19100Sstevel@tonic-gate  *	the array is when a NULL pointer is found inside. If
19110Sstevel@tonic-gate  *	entries is > 0, then NULL pointers inside the array do
19120Sstevel@tonic-gate  *	not indicate the end of the array.
19130Sstevel@tonic-gate  *
19140Sstevel@tonic-gate  */
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate static void
free_array(char ** argv,size_t entries)19170Sstevel@tonic-gate free_array(char **argv, size_t entries) {
19180Sstevel@tonic-gate 	char **p = argv;
1919*11038SRao.Shoaib@Sun.COM 	int useEntries = (entries > 0U);
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	if (argv == NULL)
19220Sstevel@tonic-gate 		return;
19230Sstevel@tonic-gate 
1924*11038SRao.Shoaib@Sun.COM 	while ((useEntries && entries > 0U) || *p) {
19250Sstevel@tonic-gate 		if (*p)
19260Sstevel@tonic-gate 			free(*p);
19270Sstevel@tonic-gate 		p++;
19280Sstevel@tonic-gate 		if (useEntries)
19290Sstevel@tonic-gate 			entries--;
19300Sstevel@tonic-gate 	}
19310Sstevel@tonic-gate 	free(argv);
19320Sstevel@tonic-gate }
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate /* ************************************************** */
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate #if WANT_MAIN
19410Sstevel@tonic-gate 
1942*11038SRao.Shoaib@Sun.COM /*% takes an option to indicate what sort of marshalling(read the code) and
19430Sstevel@tonic-gate    an argument. If the argument looks like a marshalled buffer(has a ':'
19440Sstevel@tonic-gate    embedded) then it's unmarshalled and the remarshalled and the new string
19450Sstevel@tonic-gate    is compared to the old one.
19460Sstevel@tonic-gate */
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate int
main(int argc,char ** argv)19490Sstevel@tonic-gate main(int argc, char **argv) {
19500Sstevel@tonic-gate 	char buffer[1024];
19510Sstevel@tonic-gate 	char *b = &buffer[0];
19520Sstevel@tonic-gate 	size_t len = sizeof buffer;
19530Sstevel@tonic-gate 	char option;
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 	if (argc < 2 || argv[1][0] != '-')
19560Sstevel@tonic-gate 		exit(1);
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 	option = argv[1][1];
19590Sstevel@tonic-gate 	argv++;
19600Sstevel@tonic-gate 	argc--;
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate #if 0
19640Sstevel@tonic-gate 	{
19650Sstevel@tonic-gate 		char buff[10];
19660Sstevel@tonic-gate 		char *p = argv[1], *q = &buff[0];
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 		while (getfield(&q, sizeof buff, &p, ':') != NULL) {
19690Sstevel@tonic-gate 			printf("field: \"%s\"\n", q);
19700Sstevel@tonic-gate 			p++;
19710Sstevel@tonic-gate 		}
19720Sstevel@tonic-gate 		printf("p is now \"%s\"\n", p);
19730Sstevel@tonic-gate 	}
19740Sstevel@tonic-gate #endif
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate #if 0
19770Sstevel@tonic-gate 	{
19780Sstevel@tonic-gate 		char **x = splitarray(argv[1], argv[1] + strlen(argv[1]),
19790Sstevel@tonic-gate 				      argv[2][0]);
19800Sstevel@tonic-gate 		char **p;
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 		if (x == NULL)
19830Sstevel@tonic-gate 			printf("split failed\n");
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 		for (p = x ; p != NULL && *p != NULL ; p++) {
19860Sstevel@tonic-gate 			printf("\"%s\"\n", *p);
19870Sstevel@tonic-gate 		}
19880Sstevel@tonic-gate 	}
19890Sstevel@tonic-gate #endif
19900Sstevel@tonic-gate 
19910Sstevel@tonic-gate #if 1
19920Sstevel@tonic-gate 	switch(option) {
19930Sstevel@tonic-gate 	case 'n': {
19940Sstevel@tonic-gate 		struct nwent ne;
19950Sstevel@tonic-gate 		int i;
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
19980Sstevel@tonic-gate 			if (irp_unmarshall_nw(&ne, argv[1]) != 0) {
19990Sstevel@tonic-gate 				printf("Unmarhsalling failed\n");
20000Sstevel@tonic-gate 				exit(1);
20010Sstevel@tonic-gate 			}
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 			printf("Name: \"%s\"\n", ne.n_name);
20040Sstevel@tonic-gate 			printf("Aliases:");
20050Sstevel@tonic-gate 			for (i = 0 ; ne.n_aliases[i] != NULL ; i++)
20060Sstevel@tonic-gate 				printf("\n\t\"%s\"", ne.n_aliases[i]);
20070Sstevel@tonic-gate 			printf("\nAddrtype: %s\n", ADDR_T_STR(ne.n_addrtype));
20080Sstevel@tonic-gate 			inet_net_ntop(ne.n_addrtype, ne.n_addr, ne.n_length,
20090Sstevel@tonic-gate 				      buffer, sizeof buffer);
20100Sstevel@tonic-gate 			printf("Net: \"%s\"\n", buffer);
20110Sstevel@tonic-gate 			*((long*)ne.n_addr) = htonl(*((long*)ne.n_addr));
20120Sstevel@tonic-gate 			inet_net_ntop(ne.n_addrtype, ne.n_addr, ne.n_length,
20130Sstevel@tonic-gate 				      buffer, sizeof buffer);
20140Sstevel@tonic-gate 			printf("Corrected Net: \"%s\"\n", buffer);
20150Sstevel@tonic-gate 		} else {
20160Sstevel@tonic-gate 			struct netent *np1 = getnetbyname(argv[1]);
20170Sstevel@tonic-gate 			ne.n_name = np1->n_name;
20180Sstevel@tonic-gate 			ne.n_aliases = np1->n_aliases;
20190Sstevel@tonic-gate 			ne.n_addrtype = np1->n_addrtype;
20200Sstevel@tonic-gate 			ne.n_addr = &np1->n_net;
20210Sstevel@tonic-gate 			ne.n_length = (IN_CLASSA(np1->n_net) ?
20220Sstevel@tonic-gate 				       8 :
20230Sstevel@tonic-gate 				       (IN_CLASSB(np1->n_net) ?
20240Sstevel@tonic-gate 					16 :
20250Sstevel@tonic-gate 					(IN_CLASSC(np1->n_net) ?
20260Sstevel@tonic-gate 					 24 : -1)));
20270Sstevel@tonic-gate 			np1->n_net = htonl(np1->n_net);
20280Sstevel@tonic-gate 			if (irp_marshall_nw(&ne, &b, &len) != 0) {
20290Sstevel@tonic-gate 				printf("Marshalling failed\n");
20300Sstevel@tonic-gate 			}
20310Sstevel@tonic-gate 			printf("%s\n", b);
20320Sstevel@tonic-gate 		}
20330Sstevel@tonic-gate 		break;
20340Sstevel@tonic-gate 	}
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 	case 'r': {
20380Sstevel@tonic-gate 		char **hosts, **users, **domains;
20390Sstevel@tonic-gate 		size_t entries;
20400Sstevel@tonic-gate 		int i;
20410Sstevel@tonic-gate 		char *buff;
20420Sstevel@tonic-gate 		size_t size;
20430Sstevel@tonic-gate 		char *ngname;
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 		if (strchr(argv[1], '(') != NULL) {
20460Sstevel@tonic-gate 			if (irp_unmarshall_ng(&ngname, &entries,
20470Sstevel@tonic-gate 					      &hosts, &users, &domains,
20480Sstevel@tonic-gate 					      argv[1]) != 0) {
20490Sstevel@tonic-gate 				printf("unmarshall failed\n");
20500Sstevel@tonic-gate 				exit(1);
20510Sstevel@tonic-gate 			}
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate #define STRVAL(x) (x == NULL ? "*" : x)
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 			printf("%s {\n", ngname);
20560Sstevel@tonic-gate 			for (i = 0 ; i < entries ; i++)
20570Sstevel@tonic-gate 				printf("\t\"%s\" : \"%s\" : \"%s\"\n",
20580Sstevel@tonic-gate 				       STRVAL(hosts[i]),
20590Sstevel@tonic-gate 				       STRVAL(users[i]),
20600Sstevel@tonic-gate 				       STRVAL(domains[i]));
20610Sstevel@tonic-gate 			printf("}\n\n\n");
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 			irp_marshall_ng_start(ngname, NULL, &size);
20650Sstevel@tonic-gate 			for (i = 0 ; i < entries ; i++)
20660Sstevel@tonic-gate 				irp_marshall_ng_next(hosts[i], users[i],
20670Sstevel@tonic-gate 						     domains[i], NULL, &size);
20680Sstevel@tonic-gate 			irp_marshall_ng_end(NULL, &size);
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 			buff = malloc(size);
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 			irp_marshall_ng_start(ngname, buff, &size);
20730Sstevel@tonic-gate 			for (i = 0 ; i < entries ; i++) {
20740Sstevel@tonic-gate 				if (irp_marshall_ng_next(hosts[i], users[i],
20750Sstevel@tonic-gate 							 domains[i], buff,
20760Sstevel@tonic-gate 							 &size) != 0)
20770Sstevel@tonic-gate 					printf("next marshalling failed.\n");
20780Sstevel@tonic-gate 			}
20790Sstevel@tonic-gate 			irp_marshall_ng_end(buff, &size);
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 			if (strcmp_nws(argv[1], buff) != 0) {
20820Sstevel@tonic-gate 				printf("compare failed:\n\t%s\n\t%s\n",
20830Sstevel@tonic-gate 				       buffer, argv[1]);
20840Sstevel@tonic-gate 			} else {
20850Sstevel@tonic-gate 				printf("compare ok\n");
20860Sstevel@tonic-gate 			}
20870Sstevel@tonic-gate 		} else {
20880Sstevel@tonic-gate 			char *h, *u, *d, *buff;
20890Sstevel@tonic-gate 			size_t size;
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 			/* run through two times. First to figure out how
20920Sstevel@tonic-gate 			   much of a buffer we need. Second to do the
20930Sstevel@tonic-gate 			   actual marshalling */
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate 			setnetgrent(argv[1]);
20960Sstevel@tonic-gate 			irp_marshall_ng_start(argv[1], NULL, &size);
20970Sstevel@tonic-gate 			while (getnetgrent(&h, &u, &d) == 1)
20980Sstevel@tonic-gate 				irp_marshall_ng_next(h, u, d, NULL, &size);
20990Sstevel@tonic-gate 			irp_marshall_ng_end(NULL, &size);
21000Sstevel@tonic-gate 			endnetgrent(argv[1]);
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 			buff = malloc(size);
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 			setnetgrent(argv[1]);
21050Sstevel@tonic-gate 			if (irp_marshall_ng_start(argv[1], buff, &size) != 0)
21060Sstevel@tonic-gate 				printf("Marshalling start failed\n");
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 			while (getnetgrent(&h, &u, &d) == 1) {
21090Sstevel@tonic-gate 				if (irp_marshall_ng_next(h, u, d, buff, &size)
21100Sstevel@tonic-gate 				    != 0) {
21110Sstevel@tonic-gate 					printf("Marshalling failed\n");
21120Sstevel@tonic-gate 				}
21130Sstevel@tonic-gate 			}
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate 			irp_marshall_ng_end(buff, &size);
21160Sstevel@tonic-gate 			endnetgrent();
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 			printf("success: %s\n", buff);
21190Sstevel@tonic-gate 		}
21200Sstevel@tonic-gate 		break;
21210Sstevel@tonic-gate 	}
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 	case 'h': {
21260Sstevel@tonic-gate 		struct hostent he, *hp;
21270Sstevel@tonic-gate 		int i;
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 		if (strchr(argv[1], '@') != NULL) {
21310Sstevel@tonic-gate 			if (irp_unmarshall_ho(&he, argv[1]) != 0) {
21320Sstevel@tonic-gate 				printf("unmarshall failed\n");
21330Sstevel@tonic-gate 				exit(1);
21340Sstevel@tonic-gate 			}
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 			printf("Host: \"%s\"\nAliases:", he.h_name);
21370Sstevel@tonic-gate 			for (i = 0 ; he.h_aliases[i] != NULL ; i++)
21380Sstevel@tonic-gate 				printf("\n\t\t\"%s\"", he.h_aliases[i]);
21390Sstevel@tonic-gate 			printf("\nAddr Type: \"%s\"\n",
21400Sstevel@tonic-gate 			       ADDR_T_STR(he.h_addrtype));
21410Sstevel@tonic-gate 			printf("Length: %d\nAddresses:", he.h_length);
21420Sstevel@tonic-gate 			for (i = 0 ; he.h_addr_list[i] != 0 ; i++) {
21430Sstevel@tonic-gate 				inet_ntop(he.h_addrtype, he.h_addr_list[i],
21440Sstevel@tonic-gate 					  buffer, sizeof buffer);
21450Sstevel@tonic-gate 				printf("\n\t\"%s\"\n", buffer);
21460Sstevel@tonic-gate 			}
21470Sstevel@tonic-gate 			printf("\n\n");
21480Sstevel@tonic-gate 
21490Sstevel@tonic-gate 			irp_marshall_ho(&he, &b, &len);
21500Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
21510Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
21520Sstevel@tonic-gate 				       buffer, argv[1]);
21530Sstevel@tonic-gate 			} else {
21540Sstevel@tonic-gate 				printf("compare ok\n");
21550Sstevel@tonic-gate 			}
21560Sstevel@tonic-gate 		} else {
21570Sstevel@tonic-gate 			if ((hp = gethostbyname(argv[1])) == NULL) {
21580Sstevel@tonic-gate 				perror("gethostbyname");
21590Sstevel@tonic-gate 				printf("\"%s\"\n", argv[1]);
21600Sstevel@tonic-gate 				exit(1);
21610Sstevel@tonic-gate 			}
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 			if (irp_marshall_ho(hp, &b, &len) != 0) {
21640Sstevel@tonic-gate 				printf("irp_marshall_ho failed\n");
21650Sstevel@tonic-gate 				exit(1);
21660Sstevel@tonic-gate 			}
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
21690Sstevel@tonic-gate 		}
21700Sstevel@tonic-gate 		break;
21710Sstevel@tonic-gate 	}
21720Sstevel@tonic-gate 
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate 	case 's': {
21750Sstevel@tonic-gate 		struct servent *sv;
21760Sstevel@tonic-gate 		struct servent sv1;
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
21790Sstevel@tonic-gate 			sv = &sv1;
21800Sstevel@tonic-gate 			memset(sv, 0xef, sizeof (struct servent));
21810Sstevel@tonic-gate 			if (irp_unmarshall_sv(sv, argv[1]) != 0) {
21820Sstevel@tonic-gate 				printf("unmarshall failed\n");
21830Sstevel@tonic-gate 
21840Sstevel@tonic-gate 			}
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate 			irp_marshall_sv(sv, &b, &len);
21870Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
21880Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
21890Sstevel@tonic-gate 				       buffer, argv[1]);
21900Sstevel@tonic-gate 			} else {
21910Sstevel@tonic-gate 				printf("compare ok\n");
21920Sstevel@tonic-gate 			}
21930Sstevel@tonic-gate 		} else {
21940Sstevel@tonic-gate 			if ((sv = getservbyname(argv[1], argv[2])) == NULL) {
21950Sstevel@tonic-gate 				perror("getservent");
21960Sstevel@tonic-gate 				exit(1);
21970Sstevel@tonic-gate 			}
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate 			if (irp_marshall_sv(sv, &b, &len) != 0) {
22000Sstevel@tonic-gate 				printf("irp_marshall_sv failed\n");
22010Sstevel@tonic-gate 				exit(1);
22020Sstevel@tonic-gate 			}
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
22050Sstevel@tonic-gate 		}
22060Sstevel@tonic-gate 		break;
22070Sstevel@tonic-gate 	}
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 	case 'g': {
22100Sstevel@tonic-gate 		struct group *gr;
22110Sstevel@tonic-gate 		struct group gr1;
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
22140Sstevel@tonic-gate 			gr = &gr1;
22150Sstevel@tonic-gate 			memset(gr, 0xef, sizeof (struct group));
22160Sstevel@tonic-gate 			if (irp_unmarshall_gr(gr, argv[1]) != 0) {
22170Sstevel@tonic-gate 				printf("unmarshall failed\n");
22180Sstevel@tonic-gate 
22190Sstevel@tonic-gate 			}
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate 			irp_marshall_gr(gr, &b, &len);
22220Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
22230Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
22240Sstevel@tonic-gate 				       buffer, argv[1]);
22250Sstevel@tonic-gate 			} else {
22260Sstevel@tonic-gate 				printf("compare ok\n");
22270Sstevel@tonic-gate 			}
22280Sstevel@tonic-gate 		} else {
22290Sstevel@tonic-gate 			if ((gr = getgrnam(argv[1])) == NULL) {
22300Sstevel@tonic-gate 				perror("getgrnam");
22310Sstevel@tonic-gate 				exit(1);
22320Sstevel@tonic-gate 			}
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate 			if (irp_marshall_gr(gr, &b, &len) != 0) {
22350Sstevel@tonic-gate 				printf("irp_marshall_gr failed\n");
22360Sstevel@tonic-gate 				exit(1);
22370Sstevel@tonic-gate 			}
22380Sstevel@tonic-gate 
22390Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
22400Sstevel@tonic-gate 		}
22410Sstevel@tonic-gate 		break;
22420Sstevel@tonic-gate 	}
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	case 'p': {
22460Sstevel@tonic-gate 		struct passwd *pw;
22470Sstevel@tonic-gate 		struct passwd pw1;
22480Sstevel@tonic-gate 
22490Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
22500Sstevel@tonic-gate 			pw = &pw1;
22510Sstevel@tonic-gate 			memset(pw, 0xef, sizeof (*pw));
22520Sstevel@tonic-gate 			if (irp_unmarshall_pw(pw, argv[1]) != 0) {
22530Sstevel@tonic-gate 				printf("unmarshall failed\n");
22540Sstevel@tonic-gate 				exit(1);
22550Sstevel@tonic-gate 			}
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 			printf("User: \"%s\"\nPasswd: \"%s\"\nUid: %ld\nGid: %ld\n",
22580Sstevel@tonic-gate 			       pw->pw_name, pw->pw_passwd, (long)pw->pw_uid,
22590Sstevel@tonic-gate 			       (long)pw->pw_gid);
22600Sstevel@tonic-gate 			printf("Class: \"%s\"\nChange: %ld\nGecos: \"%s\"\n",
22610Sstevel@tonic-gate 			       pw->pw_class, (long)pw->pw_change, pw->pw_gecos);
22620Sstevel@tonic-gate 			printf("Shell: \"%s\"\nDirectory: \"%s\"\n",
22630Sstevel@tonic-gate 			       pw->pw_shell, pw->pw_dir);
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 			pw = getpwnam(pw->pw_name);
22660Sstevel@tonic-gate 			irp_marshall_pw(pw, &b, &len);
22670Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
22680Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
22690Sstevel@tonic-gate 				       buffer, argv[1]);
22700Sstevel@tonic-gate 			} else {
22710Sstevel@tonic-gate 				printf("compare ok\n");
22720Sstevel@tonic-gate 			}
22730Sstevel@tonic-gate 		} else {
22740Sstevel@tonic-gate 			if ((pw = getpwnam(argv[1])) == NULL) {
22750Sstevel@tonic-gate 				perror("getpwnam");
22760Sstevel@tonic-gate 				exit(1);
22770Sstevel@tonic-gate 			}
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 			if (irp_marshall_pw(pw, &b, &len) != 0) {
22800Sstevel@tonic-gate 				printf("irp_marshall_pw failed\n");
22810Sstevel@tonic-gate 				exit(1);
22820Sstevel@tonic-gate 			}
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
22850Sstevel@tonic-gate 		}
22860Sstevel@tonic-gate 		break;
22870Sstevel@tonic-gate 	}
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 	default:
22900Sstevel@tonic-gate 		printf("Wrong option: %c\n", option);
22910Sstevel@tonic-gate 		break;
22920Sstevel@tonic-gate 	}
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate #endif
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate 	return (0);
22970Sstevel@tonic-gate }
22980Sstevel@tonic-gate 
22990Sstevel@tonic-gate #endif
2300*11038SRao.Shoaib@Sun.COM 
2301*11038SRao.Shoaib@Sun.COM /*! \file */
2302