1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2001-2002 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*
7*0Sstevel@tonic-gate  * Copyright(c) 1989, 1993, 1995
8*0Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
9*0Sstevel@tonic-gate  *
10*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
11*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
12*0Sstevel@tonic-gate  * are met:
13*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
14*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
15*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
16*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
17*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
18*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
19*0Sstevel@tonic-gate  *    must display the following acknowledgement:
20*0Sstevel@tonic-gate  *	This product includes software developed by the University of
21*0Sstevel@tonic-gate  *	California, Berkeley and its contributors.
22*0Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
23*0Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
24*0Sstevel@tonic-gate  *    without specific prior written permission.
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36*0Sstevel@tonic-gate  * SUCH DAMAGE.
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /*
40*0Sstevel@tonic-gate  * Portions Copyright (c) 1996 by Internet Software Consortium.
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
43*0Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
44*0Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
45*0Sstevel@tonic-gate  *
46*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
47*0Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
48*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
49*0Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50*0Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51*0Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52*0Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53*0Sstevel@tonic-gate  * SOFTWARE.
54*0Sstevel@tonic-gate  */
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
59*0Sstevel@tonic-gate static const char rcsid[] = "$Id: irpmarshall.c,v 8.7 2001/05/29 05:49:01 marka Exp $";
60*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate #if 0
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate Check values are in approrpriate endian order.
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate Double check memory allocations on unmarhsalling
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate #endif
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /* Extern */
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate #include "port_before.h"
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate #include <sys/types.h>
76*0Sstevel@tonic-gate #include <sys/socket.h>
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate #include <netinet/in.h>
79*0Sstevel@tonic-gate #include <arpa/inet.h>
80*0Sstevel@tonic-gate #include <arpa/nameser.h>
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate #include <stdio.h>
83*0Sstevel@tonic-gate #include <ctype.h>
84*0Sstevel@tonic-gate #include <pwd.h>
85*0Sstevel@tonic-gate #include <stdlib.h>
86*0Sstevel@tonic-gate #include <string.h>
87*0Sstevel@tonic-gate #include <syslog.h>
88*0Sstevel@tonic-gate #include <utmp.h>
89*0Sstevel@tonic-gate #include <unistd.h>
90*0Sstevel@tonic-gate #include <assert.h>
91*0Sstevel@tonic-gate #include <errno.h>
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate #include <irs.h>
94*0Sstevel@tonic-gate #include <isc/memcluster.h>
95*0Sstevel@tonic-gate #include <isc/irpmarshall.h>
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate #include "port_after.h"
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate #ifndef HAVE_STRNDUP
101*0Sstevel@tonic-gate static char    *strndup(const char *str, size_t len);
102*0Sstevel@tonic-gate #endif
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate static char   **splitarray(const char *buffer, const char *buffend, char delim);
105*0Sstevel@tonic-gate static int	joinarray(char * const * argv, char *buffer, char delim);
106*0Sstevel@tonic-gate static char    *getfield(char **res, size_t reslen, char **buffer, char delim);
107*0Sstevel@tonic-gate static size_t	joinlength(char * const *argv);
108*0Sstevel@tonic-gate static void	free_array(char **argv, size_t entries);
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate #define ADDR_T_STR(x) (x == AF_INET ? "AF_INET" :\
111*0Sstevel@tonic-gate 		       (x == AF_INET6 ? "AF_INET6" : "UNKNOWN"))
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate #define MAXPADDRSIZE (sizeof "255.255.255.255" + 1)
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate static char COMMA = ',';
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate static const char *COMMASTR = ",";
118*0Sstevel@tonic-gate static const char *COLONSTR = ":";
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate /* See big comment at bottom of irpmarshall.h for description. */
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate #ifdef WANT_IRS_PW
126*0Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct passwd +++++++++++++++++++++++++ */
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate /*
130*0Sstevel@tonic-gate  * int irp_marshall_pw(const struct passwd *pw, char **buffer, size_t *len)
131*0Sstevel@tonic-gate  *
132*0Sstevel@tonic-gate  * notes:
133*0Sstevel@tonic-gate  *
134*0Sstevel@tonic-gate  *	See above
135*0Sstevel@tonic-gate  *
136*0Sstevel@tonic-gate  * return:
137*0Sstevel@tonic-gate  *
138*0Sstevel@tonic-gate  *	0 on sucess, -1 on failure.
139*0Sstevel@tonic-gate  *
140*0Sstevel@tonic-gate  */
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate int
143*0Sstevel@tonic-gate irp_marshall_pw(const struct passwd *pw, char **buffer, size_t *len) {
144*0Sstevel@tonic-gate 	size_t need = 1 ;		/* for null byte */
145*0Sstevel@tonic-gate 	char pwUid[24];
146*0Sstevel@tonic-gate 	char pwGid[24];
147*0Sstevel@tonic-gate 	char pwChange[24];
148*0Sstevel@tonic-gate 	char pwExpire[24];
149*0Sstevel@tonic-gate 	const char *pwClass;
150*0Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	if (pw == NULL || len == NULL) {
153*0Sstevel@tonic-gate 		errno = EINVAL;
154*0Sstevel@tonic-gate 		return (-1);
155*0Sstevel@tonic-gate 	}
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	sprintf(pwUid, "%ld", (long)pw->pw_uid);
158*0Sstevel@tonic-gate 	sprintf(pwGid, "%ld", (long)pw->pw_gid);
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate #ifdef HAVE_PW_CHANGE
161*0Sstevel@tonic-gate 	sprintf(pwChange, "%ld", (long)pw->pw_change);
162*0Sstevel@tonic-gate #else
163*0Sstevel@tonic-gate 	pwChange[0] = '0';
164*0Sstevel@tonic-gate 	pwChange[1] = '\0';
165*0Sstevel@tonic-gate #endif
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate #ifdef HAVE_PW_EXPIRE
168*0Sstevel@tonic-gate 	sprintf(pwExpire, "%ld", (long)pw->pw_expire);
169*0Sstevel@tonic-gate #else
170*0Sstevel@tonic-gate 	pwExpire[0] = '0';
171*0Sstevel@tonic-gate 	pwExpire[1] = '\0';
172*0Sstevel@tonic-gate #endif
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate #ifdef HAVE_PW_CLASS
175*0Sstevel@tonic-gate 	pwClass = pw->pw_class;
176*0Sstevel@tonic-gate #else
177*0Sstevel@tonic-gate 	pwClass = "";
178*0Sstevel@tonic-gate #endif
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	need += strlen(pw->pw_name)	+ 1; /* one for fieldsep */
181*0Sstevel@tonic-gate 	need += strlen(pw->pw_passwd)	+ 1;
182*0Sstevel@tonic-gate 	need += strlen(pwUid)		+ 1;
183*0Sstevel@tonic-gate 	need += strlen(pwGid)		+ 1;
184*0Sstevel@tonic-gate 	need += strlen(pwClass)		+ 1;
185*0Sstevel@tonic-gate 	need += strlen(pwChange)	+ 1;
186*0Sstevel@tonic-gate 	need += strlen(pwExpire)	+ 1;
187*0Sstevel@tonic-gate 	need += strlen(pw->pw_gecos)	+ 1;
188*0Sstevel@tonic-gate 	need += strlen(pw->pw_dir)	+ 1;
189*0Sstevel@tonic-gate 	need += strlen(pw->pw_shell)	+ 1;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	if (buffer == NULL) {
192*0Sstevel@tonic-gate 		*len = need;
193*0Sstevel@tonic-gate 		return (0);
194*0Sstevel@tonic-gate 	}
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
197*0Sstevel@tonic-gate 		errno = EINVAL;
198*0Sstevel@tonic-gate 		return (-1);
199*0Sstevel@tonic-gate 	}
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	if (*buffer == NULL) {
202*0Sstevel@tonic-gate 		need += 2;		/* for CRLF */
203*0Sstevel@tonic-gate 		*buffer = memget(need);
204*0Sstevel@tonic-gate 		if (*buffer == NULL) {
205*0Sstevel@tonic-gate 			errno = ENOMEM;
206*0Sstevel@tonic-gate 			return (-1);
207*0Sstevel@tonic-gate 		}
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 		*len = need;
210*0Sstevel@tonic-gate 	}
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	strcpy(*buffer, pw->pw_name);		strcat(*buffer, fieldsep);
213*0Sstevel@tonic-gate 	strcat(*buffer, pw->pw_passwd);		strcat(*buffer, fieldsep);
214*0Sstevel@tonic-gate 	strcat(*buffer, pwUid);			strcat(*buffer, fieldsep);
215*0Sstevel@tonic-gate 	strcat(*buffer, pwGid);			strcat(*buffer, fieldsep);
216*0Sstevel@tonic-gate 	strcat(*buffer, pwClass);		strcat(*buffer, fieldsep);
217*0Sstevel@tonic-gate 	strcat(*buffer, pwChange);		strcat(*buffer, fieldsep);
218*0Sstevel@tonic-gate 	strcat(*buffer, pwExpire);		strcat(*buffer, fieldsep);
219*0Sstevel@tonic-gate 	strcat(*buffer, pw->pw_gecos);		strcat(*buffer, fieldsep);
220*0Sstevel@tonic-gate 	strcat(*buffer, pw->pw_dir);		strcat(*buffer, fieldsep);
221*0Sstevel@tonic-gate 	strcat(*buffer, pw->pw_shell);		strcat(*buffer, fieldsep);
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	return (0);
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate /*
231*0Sstevel@tonic-gate  * int irp_unmarshall_pw(struct passwd *pw, char *buffer)
232*0Sstevel@tonic-gate  *
233*0Sstevel@tonic-gate  * notes:
234*0Sstevel@tonic-gate  *
235*0Sstevel@tonic-gate  *	see above
236*0Sstevel@tonic-gate  *
237*0Sstevel@tonic-gate  * return:
238*0Sstevel@tonic-gate  *
239*0Sstevel@tonic-gate  *	0 on success, -1 on failure
240*0Sstevel@tonic-gate  *
241*0Sstevel@tonic-gate  */
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate int
244*0Sstevel@tonic-gate irp_unmarshall_pw(struct passwd *pw, char *buffer) {
245*0Sstevel@tonic-gate 	char *name, *pass, *class, *gecos, *dir, *shell;
246*0Sstevel@tonic-gate 	uid_t pwuid;
247*0Sstevel@tonic-gate 	gid_t pwgid;
248*0Sstevel@tonic-gate 	time_t pwchange;
249*0Sstevel@tonic-gate 	time_t pwexpire;
250*0Sstevel@tonic-gate 	char *p;
251*0Sstevel@tonic-gate 	long t;
252*0Sstevel@tonic-gate 	char tmpbuf[24];
253*0Sstevel@tonic-gate 	char *tb = &tmpbuf[0];
254*0Sstevel@tonic-gate 	char fieldsep = ':';
255*0Sstevel@tonic-gate 	int myerrno = EINVAL;
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	name = pass = class = gecos = dir = shell = NULL;
258*0Sstevel@tonic-gate 	p = buffer;
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	/* pw_name field */
261*0Sstevel@tonic-gate 	name = NULL;
262*0Sstevel@tonic-gate 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0) {
263*0Sstevel@tonic-gate 		goto error;
264*0Sstevel@tonic-gate 	}
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	/* pw_passwd field */
267*0Sstevel@tonic-gate 	pass = NULL;
268*0Sstevel@tonic-gate 	if (getfield(&pass, 0, &p, fieldsep) == NULL) { /* field can be empty */
269*0Sstevel@tonic-gate 		goto error;
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	/* pw_uid field */
274*0Sstevel@tonic-gate 	tb = tmpbuf;
275*0Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
276*0Sstevel@tonic-gate 	    strlen(tb) == 0) {
277*0Sstevel@tonic-gate 		goto error;
278*0Sstevel@tonic-gate 	}
279*0Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
280*0Sstevel@tonic-gate 	if (*tb) {
281*0Sstevel@tonic-gate 		goto error;	/* junk in value */
282*0Sstevel@tonic-gate 	}
283*0Sstevel@tonic-gate 	pwuid = (uid_t)t;
284*0Sstevel@tonic-gate 	if ((long) pwuid != t) {	/* value must have been too big. */
285*0Sstevel@tonic-gate 		goto error;
286*0Sstevel@tonic-gate 	}
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	/* pw_gid field */
291*0Sstevel@tonic-gate 	tb = tmpbuf;
292*0Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
293*0Sstevel@tonic-gate 	    strlen(tb) == 0) {
294*0Sstevel@tonic-gate 		goto error;
295*0Sstevel@tonic-gate 	}
296*0Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
297*0Sstevel@tonic-gate 	if (*tb) {
298*0Sstevel@tonic-gate 		goto error;	/* junk in value */
299*0Sstevel@tonic-gate 	}
300*0Sstevel@tonic-gate 	pwgid = (gid_t)t;
301*0Sstevel@tonic-gate 	if ((long)pwgid != t) {	/* value must have been too big. */
302*0Sstevel@tonic-gate 		goto error;
303*0Sstevel@tonic-gate 	}
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	/* pw_class field */
308*0Sstevel@tonic-gate 	class = NULL;
309*0Sstevel@tonic-gate 	if (getfield(&class, 0, &p, fieldsep) == NULL) {
310*0Sstevel@tonic-gate 		goto error;
311*0Sstevel@tonic-gate 	}
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	/* pw_change field */
316*0Sstevel@tonic-gate 	tb = tmpbuf;
317*0Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
318*0Sstevel@tonic-gate 	    strlen(tb) == 0) {
319*0Sstevel@tonic-gate 		goto error;
320*0Sstevel@tonic-gate 	}
321*0Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
322*0Sstevel@tonic-gate 	if (*tb) {
323*0Sstevel@tonic-gate 		goto error;	/* junk in value */
324*0Sstevel@tonic-gate 	}
325*0Sstevel@tonic-gate 	pwchange = (time_t)t;
326*0Sstevel@tonic-gate 	if ((long)pwchange != t) {	/* value must have been too big. */
327*0Sstevel@tonic-gate 		goto error;
328*0Sstevel@tonic-gate 	}
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 	/* pw_expire field */
333*0Sstevel@tonic-gate 	tb = tmpbuf;
334*0Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
335*0Sstevel@tonic-gate 	    strlen(tb) == 0) {
336*0Sstevel@tonic-gate 		goto error;
337*0Sstevel@tonic-gate 	}
338*0Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
339*0Sstevel@tonic-gate 	if (*tb) {
340*0Sstevel@tonic-gate 		goto error;	/* junk in value */
341*0Sstevel@tonic-gate 	}
342*0Sstevel@tonic-gate 	pwexpire = (time_t)t;
343*0Sstevel@tonic-gate 	if ((long) pwexpire != t) {	/* value must have been too big. */
344*0Sstevel@tonic-gate 		goto error;
345*0Sstevel@tonic-gate 	}
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	/* pw_gecos field */
350*0Sstevel@tonic-gate 	gecos = NULL;
351*0Sstevel@tonic-gate 	if (getfield(&gecos, 0, &p, fieldsep) == NULL) {
352*0Sstevel@tonic-gate 		goto error;
353*0Sstevel@tonic-gate 	}
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	/* pw_dir field */
358*0Sstevel@tonic-gate 	dir = NULL;
359*0Sstevel@tonic-gate 	if (getfield(&dir, 0, &p, fieldsep) == NULL) {
360*0Sstevel@tonic-gate 		goto error;
361*0Sstevel@tonic-gate 	}
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	/* pw_shell field */
366*0Sstevel@tonic-gate 	shell = NULL;
367*0Sstevel@tonic-gate 	if (getfield(&shell, 0, &p, fieldsep) == NULL) {
368*0Sstevel@tonic-gate 		goto error;
369*0Sstevel@tonic-gate 	}
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	pw->pw_name = name;
374*0Sstevel@tonic-gate 	pw->pw_passwd = pass;
375*0Sstevel@tonic-gate 	pw->pw_uid = pwuid;
376*0Sstevel@tonic-gate 	pw->pw_gid = pwgid;
377*0Sstevel@tonic-gate 	pw->pw_gecos = gecos;
378*0Sstevel@tonic-gate 	pw->pw_dir = dir;
379*0Sstevel@tonic-gate 	pw->pw_shell = shell;
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate #ifdef HAVE_PW_CHANGE
382*0Sstevel@tonic-gate 	pw->pw_change = pwchange;
383*0Sstevel@tonic-gate #endif
384*0Sstevel@tonic-gate #ifdef HAVE_PW_CLASS
385*0Sstevel@tonic-gate 	pw->pw_class = class;
386*0Sstevel@tonic-gate #endif
387*0Sstevel@tonic-gate #ifdef HAVE_PW_EXPIRE
388*0Sstevel@tonic-gate 	pw->pw_expire = pwexpire;
389*0Sstevel@tonic-gate #endif
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 	return (0);
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate  error:
394*0Sstevel@tonic-gate 	errno = myerrno;
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	if (name != NULL) free(name);
397*0Sstevel@tonic-gate 	if (pass != NULL) free(pass);
398*0Sstevel@tonic-gate 	if (gecos != NULL) free(gecos);
399*0Sstevel@tonic-gate 	if (dir != NULL) free(dir);
400*0Sstevel@tonic-gate 	if (shell != NULL) free(shell);
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 	return (-1);
403*0Sstevel@tonic-gate }
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate /* ------------------------- struct passwd ------------------------- */
406*0Sstevel@tonic-gate #endif /* WANT_IRS_PW */
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct group +++++++++++++++++++++++++ */
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate /*
415*0Sstevel@tonic-gate  * int irp_marshall_gr(const struct group *gr, char **buffer, size_t *len)
416*0Sstevel@tonic-gate  *
417*0Sstevel@tonic-gate  * notes:
418*0Sstevel@tonic-gate  *
419*0Sstevel@tonic-gate  *	see above.
420*0Sstevel@tonic-gate  *
421*0Sstevel@tonic-gate  * return:
422*0Sstevel@tonic-gate  *
423*0Sstevel@tonic-gate  *	0 on success, -1 on failure
424*0Sstevel@tonic-gate  */
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate int
427*0Sstevel@tonic-gate irp_marshall_gr(const struct group *gr, char **buffer, size_t *len) {
428*0Sstevel@tonic-gate 	size_t need = 1;	/* for null byte */
429*0Sstevel@tonic-gate 	char grGid[24];
430*0Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 	if (gr == NULL || len == NULL) {
433*0Sstevel@tonic-gate 		errno = EINVAL;
434*0Sstevel@tonic-gate 		return (-1);
435*0Sstevel@tonic-gate 	}
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 	sprintf(grGid, "%ld", (long)gr->gr_gid);
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate 	need += strlen(gr->gr_name) + 1;
440*0Sstevel@tonic-gate #ifndef MISSING_GR_PASSWD
441*0Sstevel@tonic-gate 	need += strlen(gr->gr_passwd) + 1;
442*0Sstevel@tonic-gate #else
443*0Sstevel@tonic-gate 	need++;
444*0Sstevel@tonic-gate #endif
445*0Sstevel@tonic-gate 	need += strlen(grGid) + 1;
446*0Sstevel@tonic-gate 	need += joinlength(gr->gr_mem) + 1;
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	if (buffer == NULL) {
449*0Sstevel@tonic-gate 		*len = need;
450*0Sstevel@tonic-gate 		return (0);
451*0Sstevel@tonic-gate 	}
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
454*0Sstevel@tonic-gate 		errno = EINVAL;
455*0Sstevel@tonic-gate 		return (-1);
456*0Sstevel@tonic-gate 	}
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 	if (*buffer == NULL) {
459*0Sstevel@tonic-gate 		need += 2;		/* for CRLF */
460*0Sstevel@tonic-gate 		*buffer = memget(need);
461*0Sstevel@tonic-gate 		if (*buffer == NULL) {
462*0Sstevel@tonic-gate 			errno = ENOMEM;
463*0Sstevel@tonic-gate 			return (-1);
464*0Sstevel@tonic-gate 		}
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 		*len = need;
467*0Sstevel@tonic-gate 	}
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 	strcpy(*buffer, gr->gr_name);		strcat(*buffer, fieldsep);
470*0Sstevel@tonic-gate #ifndef MISSING_GR_PASSWD
471*0Sstevel@tonic-gate 	strcat(*buffer, gr->gr_passwd);
472*0Sstevel@tonic-gate #endif
473*0Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
474*0Sstevel@tonic-gate 	strcat(*buffer, grGid);			strcat(*buffer, fieldsep);
475*0Sstevel@tonic-gate 	joinarray(gr->gr_mem, *buffer, COMMA) ;	strcat(*buffer, fieldsep);
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	return (0);
478*0Sstevel@tonic-gate }
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate /*
484*0Sstevel@tonic-gate  * int irp_unmarshall_gr(struct group *gr, char *buffer)
485*0Sstevel@tonic-gate  *
486*0Sstevel@tonic-gate  * notes:
487*0Sstevel@tonic-gate  *
488*0Sstevel@tonic-gate  *	see above
489*0Sstevel@tonic-gate  *
490*0Sstevel@tonic-gate  * return:
491*0Sstevel@tonic-gate  *
492*0Sstevel@tonic-gate  *	0 on success and -1 on failure.
493*0Sstevel@tonic-gate  *
494*0Sstevel@tonic-gate  */
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate int
497*0Sstevel@tonic-gate irp_unmarshall_gr(struct group *gr, char *buffer) {
498*0Sstevel@tonic-gate 	char *p, *q;
499*0Sstevel@tonic-gate 	gid_t grgid;
500*0Sstevel@tonic-gate 	long t;
501*0Sstevel@tonic-gate 	char *name = NULL;
502*0Sstevel@tonic-gate 	char *pass = NULL;
503*0Sstevel@tonic-gate 	char **members = NULL;
504*0Sstevel@tonic-gate 	char tmpbuf[24];
505*0Sstevel@tonic-gate 	char *tb;
506*0Sstevel@tonic-gate 	char fieldsep = ':';
507*0Sstevel@tonic-gate 	int myerrno = EINVAL;
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate 	if (gr == NULL || buffer == NULL) {
510*0Sstevel@tonic-gate 		errno = EINVAL;
511*0Sstevel@tonic-gate 		return (-1);
512*0Sstevel@tonic-gate 	}
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate 	p = buffer;
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate 	/* gr_name field */
517*0Sstevel@tonic-gate 	name = NULL;
518*0Sstevel@tonic-gate 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0) {
519*0Sstevel@tonic-gate 		goto error;
520*0Sstevel@tonic-gate 	}
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 	/* gr_passwd field */
524*0Sstevel@tonic-gate 	pass = NULL;
525*0Sstevel@tonic-gate 	if (getfield(&pass, 0, &p, fieldsep) == NULL) {
526*0Sstevel@tonic-gate 		goto error;
527*0Sstevel@tonic-gate 	}
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate 	/* gr_gid field */
531*0Sstevel@tonic-gate 	tb = tmpbuf;
532*0Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
533*0Sstevel@tonic-gate 	    strlen(tb) == 0) {
534*0Sstevel@tonic-gate 		goto error;
535*0Sstevel@tonic-gate 	}
536*0Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
537*0Sstevel@tonic-gate 	if (*tb) {
538*0Sstevel@tonic-gate 		goto error;	/* junk in value */
539*0Sstevel@tonic-gate 	}
540*0Sstevel@tonic-gate 	grgid = (gid_t)t;
541*0Sstevel@tonic-gate 	if ((long) grgid != t) {	/* value must have been too big. */
542*0Sstevel@tonic-gate 		goto error;
543*0Sstevel@tonic-gate 	}
544*0Sstevel@tonic-gate 
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate 	/* gr_mem field. Member names are separated by commas */
547*0Sstevel@tonic-gate 	q = strchr(p, fieldsep);
548*0Sstevel@tonic-gate 	if (q == NULL) {
549*0Sstevel@tonic-gate 		goto error;
550*0Sstevel@tonic-gate 	}
551*0Sstevel@tonic-gate 	members = splitarray(p, q, COMMA);
552*0Sstevel@tonic-gate 	if (members == NULL) {
553*0Sstevel@tonic-gate 		myerrno = errno;
554*0Sstevel@tonic-gate 		goto error;
555*0Sstevel@tonic-gate 	}
556*0Sstevel@tonic-gate 	p = q + 1;
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate 	gr->gr_name = name;
560*0Sstevel@tonic-gate #ifndef MISSING_GR_PASSWD
561*0Sstevel@tonic-gate 	gr->gr_passwd = pass;
562*0Sstevel@tonic-gate #endif
563*0Sstevel@tonic-gate 	gr->gr_gid = grgid;
564*0Sstevel@tonic-gate 	gr->gr_mem = members;
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate 	return (0);
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate  error:
569*0Sstevel@tonic-gate 	errno = myerrno;
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate 	if (name != NULL) free(name);
572*0Sstevel@tonic-gate 	if (pass != NULL) free(pass);
573*0Sstevel@tonic-gate 
574*0Sstevel@tonic-gate 	return (-1);
575*0Sstevel@tonic-gate }
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate /* ------------------------- struct group ------------------------- */
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct servent +++++++++++++++++++++++++ */
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate /*
588*0Sstevel@tonic-gate  * int irp_marshall_sv(const struct servent *sv, char **buffer, size_t *len)
589*0Sstevel@tonic-gate  *
590*0Sstevel@tonic-gate  * notes:
591*0Sstevel@tonic-gate  *
592*0Sstevel@tonic-gate  *	see above
593*0Sstevel@tonic-gate  *
594*0Sstevel@tonic-gate  * return:
595*0Sstevel@tonic-gate  *
596*0Sstevel@tonic-gate  *	0 on success, -1 on failure.
597*0Sstevel@tonic-gate  *
598*0Sstevel@tonic-gate  */
599*0Sstevel@tonic-gate 
600*0Sstevel@tonic-gate int
601*0Sstevel@tonic-gate irp_marshall_sv(const struct servent *sv, char **buffer, size_t *len) {
602*0Sstevel@tonic-gate 	size_t need = 1;	/* for null byte */
603*0Sstevel@tonic-gate 	char svPort[24];
604*0Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
605*0Sstevel@tonic-gate 	short realport;
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate 	if (sv == NULL || len == NULL) {
608*0Sstevel@tonic-gate 		errno = EINVAL;
609*0Sstevel@tonic-gate 		return (-1);
610*0Sstevel@tonic-gate 	}
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 	/* the int s_port field is actually a short in network order. We
613*0Sstevel@tonic-gate 	   want host order to make the marshalled data look correct */
614*0Sstevel@tonic-gate 	realport = ntohs((short)sv->s_port);
615*0Sstevel@tonic-gate 	sprintf(svPort, "%d", realport);
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 	need += strlen(sv->s_name) + 1;
618*0Sstevel@tonic-gate 	need += joinlength(sv->s_aliases) + 1;
619*0Sstevel@tonic-gate 	need += strlen(svPort) + 1;
620*0Sstevel@tonic-gate 	need += strlen(sv->s_proto) + 1;
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate 	if (buffer == NULL) {
623*0Sstevel@tonic-gate 		*len = need;
624*0Sstevel@tonic-gate 		return (0);
625*0Sstevel@tonic-gate 	}
626*0Sstevel@tonic-gate 
627*0Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
628*0Sstevel@tonic-gate 		errno = EINVAL;
629*0Sstevel@tonic-gate 		return (-1);
630*0Sstevel@tonic-gate 	}
631*0Sstevel@tonic-gate 
632*0Sstevel@tonic-gate 	if (*buffer == NULL) {
633*0Sstevel@tonic-gate 		need += 2;		/* for CRLF */
634*0Sstevel@tonic-gate 		*buffer = memget(need);
635*0Sstevel@tonic-gate 		if (*buffer == NULL) {
636*0Sstevel@tonic-gate 			errno = ENOMEM;
637*0Sstevel@tonic-gate 			return (-1);
638*0Sstevel@tonic-gate 		}
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate 		*len = need;
641*0Sstevel@tonic-gate 	}
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 	strcpy(*buffer, sv->s_name);		strcat(*buffer, fieldsep);
644*0Sstevel@tonic-gate 	joinarray(sv->s_aliases, *buffer, COMMA); strcat(*buffer, fieldsep);
645*0Sstevel@tonic-gate 	strcat(*buffer, svPort);		strcat(*buffer, fieldsep);
646*0Sstevel@tonic-gate 	strcat(*buffer, sv->s_proto);		strcat(*buffer, fieldsep);
647*0Sstevel@tonic-gate 
648*0Sstevel@tonic-gate 	return (0);
649*0Sstevel@tonic-gate }
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate /*
656*0Sstevel@tonic-gate  * int irp_unmarshall_sv(struct servent *sv, char *buffer)
657*0Sstevel@tonic-gate  *
658*0Sstevel@tonic-gate  * notes:
659*0Sstevel@tonic-gate  *
660*0Sstevel@tonic-gate  *	see above
661*0Sstevel@tonic-gate  *
662*0Sstevel@tonic-gate  * return:
663*0Sstevel@tonic-gate  *
664*0Sstevel@tonic-gate  *	0 on success, -1 on failure.
665*0Sstevel@tonic-gate  *
666*0Sstevel@tonic-gate  */
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate int
669*0Sstevel@tonic-gate irp_unmarshall_sv(struct servent *sv, char *buffer) {
670*0Sstevel@tonic-gate 	char *p, *q;
671*0Sstevel@tonic-gate 	short svport;
672*0Sstevel@tonic-gate 	long t;
673*0Sstevel@tonic-gate 	char *name = NULL;
674*0Sstevel@tonic-gate 	char *proto = NULL;
675*0Sstevel@tonic-gate 	char **aliases = NULL;
676*0Sstevel@tonic-gate 	char tmpbuf[24];
677*0Sstevel@tonic-gate 	char *tb;
678*0Sstevel@tonic-gate 	char fieldsep = ':';
679*0Sstevel@tonic-gate 	int myerrno = EINVAL;
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate 	if (sv == NULL || buffer == NULL)
682*0Sstevel@tonic-gate 		return (-1);
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 	p = buffer;
685*0Sstevel@tonic-gate 
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate 	/* s_name field */
688*0Sstevel@tonic-gate 	name = NULL;
689*0Sstevel@tonic-gate 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0) {
690*0Sstevel@tonic-gate 		goto error;
691*0Sstevel@tonic-gate 	}
692*0Sstevel@tonic-gate 
693*0Sstevel@tonic-gate 
694*0Sstevel@tonic-gate 	/* s_aliases field */
695*0Sstevel@tonic-gate 	q = strchr(p, fieldsep);
696*0Sstevel@tonic-gate 	if (q == NULL) {
697*0Sstevel@tonic-gate 		goto error;
698*0Sstevel@tonic-gate 	}
699*0Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
700*0Sstevel@tonic-gate 	if (aliases == NULL) {
701*0Sstevel@tonic-gate 		myerrno = errno;
702*0Sstevel@tonic-gate 		goto error;
703*0Sstevel@tonic-gate 	}
704*0Sstevel@tonic-gate 	p = q + 1;
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 
707*0Sstevel@tonic-gate 	/* s_port field */
708*0Sstevel@tonic-gate 	tb = tmpbuf;
709*0Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
710*0Sstevel@tonic-gate 	    strlen(tb) == 0) {
711*0Sstevel@tonic-gate 		goto error;
712*0Sstevel@tonic-gate 	}
713*0Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
714*0Sstevel@tonic-gate 	if (*tb) {
715*0Sstevel@tonic-gate 		goto error;	/* junk in value */
716*0Sstevel@tonic-gate 	}
717*0Sstevel@tonic-gate 	svport = (short)t;
718*0Sstevel@tonic-gate 	if ((long) svport != t) {	/* value must have been too big. */
719*0Sstevel@tonic-gate 		goto error;
720*0Sstevel@tonic-gate 	}
721*0Sstevel@tonic-gate 	svport = htons(svport);
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate 	/* s_proto field */
724*0Sstevel@tonic-gate 	proto = NULL;
725*0Sstevel@tonic-gate 	if (getfield(&proto, 0, &p, fieldsep) == NULL) {
726*0Sstevel@tonic-gate 		goto error;
727*0Sstevel@tonic-gate 	}
728*0Sstevel@tonic-gate 
729*0Sstevel@tonic-gate 	sv->s_name = name;
730*0Sstevel@tonic-gate 	sv->s_aliases = aliases;
731*0Sstevel@tonic-gate 	sv->s_port = svport;
732*0Sstevel@tonic-gate 	sv->s_proto = proto;
733*0Sstevel@tonic-gate 
734*0Sstevel@tonic-gate 	return (0);
735*0Sstevel@tonic-gate 
736*0Sstevel@tonic-gate  error:
737*0Sstevel@tonic-gate 	errno = myerrno;
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate 	if (name != NULL) free(name);
740*0Sstevel@tonic-gate 	if (proto != NULL) free(proto);
741*0Sstevel@tonic-gate 	free_array(aliases, 0);
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate 	return (-1);
744*0Sstevel@tonic-gate }
745*0Sstevel@tonic-gate 
746*0Sstevel@tonic-gate 
747*0Sstevel@tonic-gate /* ------------------------- struct servent ------------------------- */
748*0Sstevel@tonic-gate 
749*0Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct protoent +++++++++++++++++++++++++ */
750*0Sstevel@tonic-gate 
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate /*
754*0Sstevel@tonic-gate  * int irp_marshall_pr(struct protoent *pr, char **buffer, size_t *len)
755*0Sstevel@tonic-gate  *
756*0Sstevel@tonic-gate  * notes:
757*0Sstevel@tonic-gate  *
758*0Sstevel@tonic-gate  *	see above
759*0Sstevel@tonic-gate  *
760*0Sstevel@tonic-gate  * return:
761*0Sstevel@tonic-gate  *
762*0Sstevel@tonic-gate  *	0 on success and -1 on failure.
763*0Sstevel@tonic-gate  *
764*0Sstevel@tonic-gate  */
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate int
767*0Sstevel@tonic-gate irp_marshall_pr(struct protoent *pr, char **buffer, size_t *len) {
768*0Sstevel@tonic-gate 	size_t need = 1;	/* for null byte */
769*0Sstevel@tonic-gate 	char prProto[24];
770*0Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate 	if (pr == NULL || len == NULL) {
773*0Sstevel@tonic-gate 		errno = EINVAL;
774*0Sstevel@tonic-gate 		return (-1);
775*0Sstevel@tonic-gate 	}
776*0Sstevel@tonic-gate 
777*0Sstevel@tonic-gate 	sprintf(prProto, "%d", (int)pr->p_proto);
778*0Sstevel@tonic-gate 
779*0Sstevel@tonic-gate 	need += strlen(pr->p_name) + 1;
780*0Sstevel@tonic-gate 	need += joinlength(pr->p_aliases) + 1;
781*0Sstevel@tonic-gate 	need += strlen(prProto) + 1;
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate 	if (buffer == NULL) {
784*0Sstevel@tonic-gate 		*len = need;
785*0Sstevel@tonic-gate 		return (0);
786*0Sstevel@tonic-gate 	}
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
789*0Sstevel@tonic-gate 		errno = EINVAL;
790*0Sstevel@tonic-gate 		return (-1);
791*0Sstevel@tonic-gate 	}
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate 	if (*buffer == NULL) {
794*0Sstevel@tonic-gate 		need += 2;		/* for CRLF */
795*0Sstevel@tonic-gate 		*buffer = memget(need);
796*0Sstevel@tonic-gate 		if (*buffer == NULL) {
797*0Sstevel@tonic-gate 			errno = ENOMEM;
798*0Sstevel@tonic-gate 			return (-1);
799*0Sstevel@tonic-gate 		}
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 		*len = need;
802*0Sstevel@tonic-gate 	}
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate 	strcpy(*buffer, pr->p_name);		strcat(*buffer, fieldsep);
805*0Sstevel@tonic-gate 	joinarray(pr->p_aliases, *buffer, COMMA); strcat(*buffer, fieldsep);
806*0Sstevel@tonic-gate 	strcat(*buffer, prProto);		strcat(*buffer, fieldsep);
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate 	return (0);
809*0Sstevel@tonic-gate 
810*0Sstevel@tonic-gate }
811*0Sstevel@tonic-gate 
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate 
814*0Sstevel@tonic-gate /*
815*0Sstevel@tonic-gate  * int irp_unmarshall_pr(struct protoent *pr, char *buffer)
816*0Sstevel@tonic-gate  *
817*0Sstevel@tonic-gate  * notes:
818*0Sstevel@tonic-gate  *
819*0Sstevel@tonic-gate  *	See above
820*0Sstevel@tonic-gate  *
821*0Sstevel@tonic-gate  * return:
822*0Sstevel@tonic-gate  *
823*0Sstevel@tonic-gate  *	0 on success, -1 on failure
824*0Sstevel@tonic-gate  *
825*0Sstevel@tonic-gate  */
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate int irp_unmarshall_pr(struct protoent *pr, char *buffer) {
828*0Sstevel@tonic-gate 	char *p, *q;
829*0Sstevel@tonic-gate 	int prproto;
830*0Sstevel@tonic-gate 	long t;
831*0Sstevel@tonic-gate 	char *name = NULL;
832*0Sstevel@tonic-gate 	char **aliases = NULL;
833*0Sstevel@tonic-gate 	char tmpbuf[24];
834*0Sstevel@tonic-gate 	char *tb;
835*0Sstevel@tonic-gate 	char fieldsep = ':';
836*0Sstevel@tonic-gate 	int myerrno = EINVAL;
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate 	if (pr == NULL || buffer == NULL) {
839*0Sstevel@tonic-gate 		errno = EINVAL;
840*0Sstevel@tonic-gate 		return (-1);
841*0Sstevel@tonic-gate 	}
842*0Sstevel@tonic-gate 
843*0Sstevel@tonic-gate 	p = buffer;
844*0Sstevel@tonic-gate 
845*0Sstevel@tonic-gate 	/* p_name field */
846*0Sstevel@tonic-gate 	name = NULL;
847*0Sstevel@tonic-gate 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0) {
848*0Sstevel@tonic-gate 		goto error;
849*0Sstevel@tonic-gate 	}
850*0Sstevel@tonic-gate 
851*0Sstevel@tonic-gate 
852*0Sstevel@tonic-gate 	/* p_aliases field */
853*0Sstevel@tonic-gate 	q = strchr(p, fieldsep);
854*0Sstevel@tonic-gate 	if (q == NULL) {
855*0Sstevel@tonic-gate 		goto error;
856*0Sstevel@tonic-gate 	}
857*0Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
858*0Sstevel@tonic-gate 	if (aliases == NULL) {
859*0Sstevel@tonic-gate 		myerrno = errno;
860*0Sstevel@tonic-gate 		goto error;
861*0Sstevel@tonic-gate 	}
862*0Sstevel@tonic-gate 	p = q + 1;
863*0Sstevel@tonic-gate 
864*0Sstevel@tonic-gate 
865*0Sstevel@tonic-gate 	/* p_proto field */
866*0Sstevel@tonic-gate 	tb = tmpbuf;
867*0Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
868*0Sstevel@tonic-gate 	    strlen(tb) == 0) {
869*0Sstevel@tonic-gate 		goto error;
870*0Sstevel@tonic-gate 	}
871*0Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
872*0Sstevel@tonic-gate 	if (*tb) {
873*0Sstevel@tonic-gate 		goto error;	/* junk in value */
874*0Sstevel@tonic-gate 	}
875*0Sstevel@tonic-gate 	prproto = (int)t;
876*0Sstevel@tonic-gate 	if ((long) prproto != t) {	/* value must have been too big. */
877*0Sstevel@tonic-gate 		goto error;
878*0Sstevel@tonic-gate 	}
879*0Sstevel@tonic-gate 
880*0Sstevel@tonic-gate 	pr->p_name = name;
881*0Sstevel@tonic-gate 	pr->p_aliases = aliases;
882*0Sstevel@tonic-gate 	pr->p_proto = prproto;
883*0Sstevel@tonic-gate 
884*0Sstevel@tonic-gate 	return (0);
885*0Sstevel@tonic-gate 
886*0Sstevel@tonic-gate  error:
887*0Sstevel@tonic-gate 	errno = myerrno;
888*0Sstevel@tonic-gate 
889*0Sstevel@tonic-gate 	if (name != NULL) free(name);
890*0Sstevel@tonic-gate 	free_array(aliases, 0);
891*0Sstevel@tonic-gate 
892*0Sstevel@tonic-gate 	return (-1);
893*0Sstevel@tonic-gate }
894*0Sstevel@tonic-gate 
895*0Sstevel@tonic-gate /* ------------------------- struct protoent ------------------------- */
896*0Sstevel@tonic-gate 
897*0Sstevel@tonic-gate 
898*0Sstevel@tonic-gate 
899*0Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct hostent +++++++++++++++++++++++++ */
900*0Sstevel@tonic-gate 
901*0Sstevel@tonic-gate 
902*0Sstevel@tonic-gate /*
903*0Sstevel@tonic-gate  * int irp_marshall_ho(struct hostent *ho, char **buffer, size_t *len)
904*0Sstevel@tonic-gate  *
905*0Sstevel@tonic-gate  * notes:
906*0Sstevel@tonic-gate  *
907*0Sstevel@tonic-gate  *	see above.
908*0Sstevel@tonic-gate  *
909*0Sstevel@tonic-gate  * return:
910*0Sstevel@tonic-gate  *
911*0Sstevel@tonic-gate  *	0 on success, -1 on failure.
912*0Sstevel@tonic-gate  *
913*0Sstevel@tonic-gate  */
914*0Sstevel@tonic-gate 
915*0Sstevel@tonic-gate int
916*0Sstevel@tonic-gate irp_marshall_ho(struct hostent *ho, char **buffer, size_t *len) {
917*0Sstevel@tonic-gate 	size_t need = 1;	/* for null byte */
918*0Sstevel@tonic-gate 	char hoaddrtype[24];
919*0Sstevel@tonic-gate 	char holength[24];
920*0Sstevel@tonic-gate 	char **av;
921*0Sstevel@tonic-gate 	char *p;
922*0Sstevel@tonic-gate 	int addrlen;
923*0Sstevel@tonic-gate 	int malloced = 0;
924*0Sstevel@tonic-gate 	size_t remlen;
925*0Sstevel@tonic-gate 	const char *fieldsep = "@";
926*0Sstevel@tonic-gate 
927*0Sstevel@tonic-gate 	if (ho == NULL || len == NULL) {
928*0Sstevel@tonic-gate 		errno = EINVAL;
929*0Sstevel@tonic-gate 		return (-1);
930*0Sstevel@tonic-gate 	}
931*0Sstevel@tonic-gate 
932*0Sstevel@tonic-gate 	switch(ho->h_addrtype) {
933*0Sstevel@tonic-gate 	case AF_INET:
934*0Sstevel@tonic-gate 		strcpy(hoaddrtype, "AF_INET");
935*0Sstevel@tonic-gate 		break;
936*0Sstevel@tonic-gate 
937*0Sstevel@tonic-gate 	case AF_INET6:
938*0Sstevel@tonic-gate 		strcpy(hoaddrtype, "AF_INET6");
939*0Sstevel@tonic-gate 		break;
940*0Sstevel@tonic-gate 
941*0Sstevel@tonic-gate 	default:
942*0Sstevel@tonic-gate 		errno = EINVAL;
943*0Sstevel@tonic-gate 		return (-1);
944*0Sstevel@tonic-gate 	}
945*0Sstevel@tonic-gate 
946*0Sstevel@tonic-gate 	sprintf(holength, "%d", ho->h_length);
947*0Sstevel@tonic-gate 
948*0Sstevel@tonic-gate 	need += strlen(ho->h_name) + 1;
949*0Sstevel@tonic-gate 	need += joinlength(ho->h_aliases) + 1;
950*0Sstevel@tonic-gate 	need += strlen(hoaddrtype) + 1;
951*0Sstevel@tonic-gate 	need += strlen(holength) + 1;
952*0Sstevel@tonic-gate 
953*0Sstevel@tonic-gate 	/* we determine an upper bound on the string length needed, not an
954*0Sstevel@tonic-gate 	   exact length. */
955*0Sstevel@tonic-gate 	addrlen = (ho->h_addrtype == AF_INET ? 16 : 46) ; /* XX other AF's?? */
956*0Sstevel@tonic-gate 	for (av = ho->h_addr_list; av != NULL && *av != NULL ; av++)
957*0Sstevel@tonic-gate 		need += addrlen;
958*0Sstevel@tonic-gate 
959*0Sstevel@tonic-gate 	if (buffer == NULL) {
960*0Sstevel@tonic-gate 		*len = need;
961*0Sstevel@tonic-gate 		return (0);
962*0Sstevel@tonic-gate 	}
963*0Sstevel@tonic-gate 
964*0Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
965*0Sstevel@tonic-gate 		errno = EINVAL;
966*0Sstevel@tonic-gate 		return (-1);
967*0Sstevel@tonic-gate 	}
968*0Sstevel@tonic-gate 
969*0Sstevel@tonic-gate 	if (*buffer == NULL) {
970*0Sstevel@tonic-gate 		need += 2;		/* for CRLF */
971*0Sstevel@tonic-gate 		*buffer = memget(need);
972*0Sstevel@tonic-gate 		if (*buffer == NULL) {
973*0Sstevel@tonic-gate 			errno = ENOMEM;
974*0Sstevel@tonic-gate 			return (-1);
975*0Sstevel@tonic-gate 		}
976*0Sstevel@tonic-gate 
977*0Sstevel@tonic-gate 		*len = need;
978*0Sstevel@tonic-gate 		malloced = 1;
979*0Sstevel@tonic-gate 	}
980*0Sstevel@tonic-gate 
981*0Sstevel@tonic-gate 	strcpy(*buffer, ho->h_name);		strcat(*buffer, fieldsep);
982*0Sstevel@tonic-gate 	joinarray(ho->h_aliases, *buffer, COMMA); strcat(*buffer, fieldsep);
983*0Sstevel@tonic-gate 	strcat(*buffer, hoaddrtype);		strcat(*buffer, fieldsep);
984*0Sstevel@tonic-gate 	strcat(*buffer, holength);		strcat(*buffer, fieldsep);
985*0Sstevel@tonic-gate 
986*0Sstevel@tonic-gate 	p = *buffer + strlen(*buffer);
987*0Sstevel@tonic-gate 	remlen = need - strlen(*buffer);
988*0Sstevel@tonic-gate 	for (av = ho->h_addr_list ; av != NULL && *av != NULL ; av++) {
989*0Sstevel@tonic-gate 		if (inet_ntop(ho->h_addrtype, *av, p, remlen) == NULL) {
990*0Sstevel@tonic-gate 			goto error;
991*0Sstevel@tonic-gate 		}
992*0Sstevel@tonic-gate 		if (*(av + 1) != NULL)
993*0Sstevel@tonic-gate 			strcat(p, COMMASTR);
994*0Sstevel@tonic-gate 		remlen -= strlen(p);
995*0Sstevel@tonic-gate 		p += strlen(p);
996*0Sstevel@tonic-gate 	}
997*0Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
998*0Sstevel@tonic-gate 
999*0Sstevel@tonic-gate 	return (0);
1000*0Sstevel@tonic-gate 
1001*0Sstevel@tonic-gate  error:
1002*0Sstevel@tonic-gate 	if (malloced) {
1003*0Sstevel@tonic-gate 		memput(*buffer, need);
1004*0Sstevel@tonic-gate 	}
1005*0Sstevel@tonic-gate 
1006*0Sstevel@tonic-gate 	return (-1);
1007*0Sstevel@tonic-gate }
1008*0Sstevel@tonic-gate 
1009*0Sstevel@tonic-gate 
1010*0Sstevel@tonic-gate 
1011*0Sstevel@tonic-gate /*
1012*0Sstevel@tonic-gate  * int irp_unmarshall_ho(struct hostent *ho, char *buffer)
1013*0Sstevel@tonic-gate  *
1014*0Sstevel@tonic-gate  * notes:
1015*0Sstevel@tonic-gate  *
1016*0Sstevel@tonic-gate  *	See above.
1017*0Sstevel@tonic-gate  *
1018*0Sstevel@tonic-gate  * return:
1019*0Sstevel@tonic-gate  *
1020*0Sstevel@tonic-gate  *	0 on success, -1 on failure.
1021*0Sstevel@tonic-gate  *
1022*0Sstevel@tonic-gate  */
1023*0Sstevel@tonic-gate 
1024*0Sstevel@tonic-gate int
1025*0Sstevel@tonic-gate irp_unmarshall_ho(struct hostent *ho, char *buffer) {
1026*0Sstevel@tonic-gate 	char *p, *q, *r;
1027*0Sstevel@tonic-gate 	int hoaddrtype;
1028*0Sstevel@tonic-gate 	int holength;
1029*0Sstevel@tonic-gate 	long t;
1030*0Sstevel@tonic-gate 	char *name = NULL;
1031*0Sstevel@tonic-gate 	char **aliases = NULL;
1032*0Sstevel@tonic-gate 	char **hohaddrlist = NULL;
1033*0Sstevel@tonic-gate 	size_t hoaddrsize;
1034*0Sstevel@tonic-gate 	char tmpbuf[24];
1035*0Sstevel@tonic-gate 	char *tb;
1036*0Sstevel@tonic-gate 	char **alist;
1037*0Sstevel@tonic-gate 	int addrcount;
1038*0Sstevel@tonic-gate 	char fieldsep = '@';
1039*0Sstevel@tonic-gate 	int myerrno = EINVAL;
1040*0Sstevel@tonic-gate 
1041*0Sstevel@tonic-gate 	if (ho == NULL || buffer == NULL) {
1042*0Sstevel@tonic-gate 		errno = EINVAL;
1043*0Sstevel@tonic-gate 		return (-1);
1044*0Sstevel@tonic-gate 	}
1045*0Sstevel@tonic-gate 
1046*0Sstevel@tonic-gate 	p = buffer;
1047*0Sstevel@tonic-gate 
1048*0Sstevel@tonic-gate 	/* h_name field */
1049*0Sstevel@tonic-gate 	name = NULL;
1050*0Sstevel@tonic-gate 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0) {
1051*0Sstevel@tonic-gate 		goto error;
1052*0Sstevel@tonic-gate 	}
1053*0Sstevel@tonic-gate 
1054*0Sstevel@tonic-gate 
1055*0Sstevel@tonic-gate 	/* h_aliases field */
1056*0Sstevel@tonic-gate 	q = strchr(p, fieldsep);
1057*0Sstevel@tonic-gate 	if (q == NULL) {
1058*0Sstevel@tonic-gate 		goto error;
1059*0Sstevel@tonic-gate 	}
1060*0Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
1061*0Sstevel@tonic-gate 	if (aliases == NULL) {
1062*0Sstevel@tonic-gate 		myerrno = errno;
1063*0Sstevel@tonic-gate 		goto error;
1064*0Sstevel@tonic-gate 	}
1065*0Sstevel@tonic-gate 	p = q + 1;
1066*0Sstevel@tonic-gate 
1067*0Sstevel@tonic-gate 
1068*0Sstevel@tonic-gate 	/* h_addrtype field */
1069*0Sstevel@tonic-gate 	tb = tmpbuf;
1070*0Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1071*0Sstevel@tonic-gate 	    strlen(tb) == 0) {
1072*0Sstevel@tonic-gate 		goto error;
1073*0Sstevel@tonic-gate 	}
1074*0Sstevel@tonic-gate 	if (strcmp(tmpbuf, "AF_INET") == 0)
1075*0Sstevel@tonic-gate 		hoaddrtype = AF_INET;
1076*0Sstevel@tonic-gate 	else if (strcmp(tmpbuf, "AF_INET6") == 0)
1077*0Sstevel@tonic-gate 		hoaddrtype = AF_INET6;
1078*0Sstevel@tonic-gate 	else
1079*0Sstevel@tonic-gate 		goto error;
1080*0Sstevel@tonic-gate 
1081*0Sstevel@tonic-gate 
1082*0Sstevel@tonic-gate 	/* h_length field */
1083*0Sstevel@tonic-gate 	tb = tmpbuf;
1084*0Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1085*0Sstevel@tonic-gate 	    strlen(tb) == 0) {
1086*0Sstevel@tonic-gate 		goto error;
1087*0Sstevel@tonic-gate 	}
1088*0Sstevel@tonic-gate 	t = strtol(tmpbuf, &tb, 10);
1089*0Sstevel@tonic-gate 	if (*tb) {
1090*0Sstevel@tonic-gate 		goto error;	/* junk in value */
1091*0Sstevel@tonic-gate 	}
1092*0Sstevel@tonic-gate 	holength = (int)t;
1093*0Sstevel@tonic-gate 	if ((long) holength != t) {	/* value must have been too big. */
1094*0Sstevel@tonic-gate 		goto error;
1095*0Sstevel@tonic-gate 	}
1096*0Sstevel@tonic-gate 
1097*0Sstevel@tonic-gate 
1098*0Sstevel@tonic-gate 	/* h_addr_list field */
1099*0Sstevel@tonic-gate 	q = strchr(p, fieldsep);
1100*0Sstevel@tonic-gate 	if (q == NULL)
1101*0Sstevel@tonic-gate 		goto error;
1102*0Sstevel@tonic-gate 
1103*0Sstevel@tonic-gate 	/* count how many addresss are in there */
1104*0Sstevel@tonic-gate 	if (q > p + 1) {
1105*0Sstevel@tonic-gate 		for (addrcount = 1, r = p ; r != q ; r++) {
1106*0Sstevel@tonic-gate 			if (*r == COMMA)
1107*0Sstevel@tonic-gate 				addrcount++;
1108*0Sstevel@tonic-gate 		}
1109*0Sstevel@tonic-gate 	} else {
1110*0Sstevel@tonic-gate 		addrcount = 0;
1111*0Sstevel@tonic-gate 	}
1112*0Sstevel@tonic-gate 
1113*0Sstevel@tonic-gate 	hoaddrsize = (addrcount + 1) * sizeof (char *);
1114*0Sstevel@tonic-gate 	hohaddrlist = malloc(hoaddrsize);
1115*0Sstevel@tonic-gate 	if (hohaddrlist == NULL) {
1116*0Sstevel@tonic-gate 		myerrno = ENOMEM;
1117*0Sstevel@tonic-gate 		goto error;
1118*0Sstevel@tonic-gate 	}
1119*0Sstevel@tonic-gate 
1120*0Sstevel@tonic-gate 	memset(hohaddrlist, 0x0, hoaddrsize);
1121*0Sstevel@tonic-gate 
1122*0Sstevel@tonic-gate 	alist = hohaddrlist;
1123*0Sstevel@tonic-gate 	for (t = 0, r = p ; r != q ; p = r + 1, t++) {
1124*0Sstevel@tonic-gate 		char saved;
1125*0Sstevel@tonic-gate 		while (r != q && *r != COMMA) r++;
1126*0Sstevel@tonic-gate 		saved = *r;
1127*0Sstevel@tonic-gate 		*r = 0x0;
1128*0Sstevel@tonic-gate 
1129*0Sstevel@tonic-gate 		alist[t] = malloc(hoaddrtype == AF_INET ? 4 : 16);
1130*0Sstevel@tonic-gate 		if (alist[t] == NULL) {
1131*0Sstevel@tonic-gate 			myerrno = ENOMEM;
1132*0Sstevel@tonic-gate 			goto error;
1133*0Sstevel@tonic-gate 		}
1134*0Sstevel@tonic-gate 
1135*0Sstevel@tonic-gate 		if (inet_pton(hoaddrtype, p, alist[t]) == -1)
1136*0Sstevel@tonic-gate 			goto error;
1137*0Sstevel@tonic-gate 		*r = saved;
1138*0Sstevel@tonic-gate 	}
1139*0Sstevel@tonic-gate 	alist[t] = NULL;
1140*0Sstevel@tonic-gate 
1141*0Sstevel@tonic-gate 	ho->h_name = name;
1142*0Sstevel@tonic-gate 	ho->h_aliases = aliases;
1143*0Sstevel@tonic-gate 	ho->h_addrtype = hoaddrtype;
1144*0Sstevel@tonic-gate 	ho->h_length = holength;
1145*0Sstevel@tonic-gate 	ho->h_addr_list = hohaddrlist;
1146*0Sstevel@tonic-gate 
1147*0Sstevel@tonic-gate 	return (0);
1148*0Sstevel@tonic-gate 
1149*0Sstevel@tonic-gate  error:
1150*0Sstevel@tonic-gate 	errno = myerrno;
1151*0Sstevel@tonic-gate 
1152*0Sstevel@tonic-gate 	if (name != NULL) free(name);
1153*0Sstevel@tonic-gate 	free_array(aliases, 0);
1154*0Sstevel@tonic-gate 
1155*0Sstevel@tonic-gate 	return (-1);
1156*0Sstevel@tonic-gate }
1157*0Sstevel@tonic-gate 
1158*0Sstevel@tonic-gate /* ------------------------- struct hostent------------------------- */
1159*0Sstevel@tonic-gate 
1160*0Sstevel@tonic-gate 
1161*0Sstevel@tonic-gate 
1162*0Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct netgrp +++++++++++++++++++++++++ */
1163*0Sstevel@tonic-gate 
1164*0Sstevel@tonic-gate 
1165*0Sstevel@tonic-gate /*
1166*0Sstevel@tonic-gate  * int irp_marshall_ng(const char *host, const char *user,
1167*0Sstevel@tonic-gate  *		       const char *domain, char *buffer, size_t *len)
1168*0Sstevel@tonic-gate  *
1169*0Sstevel@tonic-gate  * notes:
1170*0Sstevel@tonic-gate  *
1171*0Sstevel@tonic-gate  *	See note for irp_marshall_ng_start
1172*0Sstevel@tonic-gate  *
1173*0Sstevel@tonic-gate  * return:
1174*0Sstevel@tonic-gate  *
1175*0Sstevel@tonic-gate  *	0 on success, 0 on failure.
1176*0Sstevel@tonic-gate  *
1177*0Sstevel@tonic-gate  */
1178*0Sstevel@tonic-gate 
1179*0Sstevel@tonic-gate int
1180*0Sstevel@tonic-gate irp_marshall_ng(const char *host, const char *user, const char *domain,
1181*0Sstevel@tonic-gate 		char **buffer, size_t *len) {
1182*0Sstevel@tonic-gate 	size_t need = 1; /* for nul byte */
1183*0Sstevel@tonic-gate 	const char *fieldsep = ",";
1184*0Sstevel@tonic-gate 
1185*0Sstevel@tonic-gate 	if (len == NULL) {
1186*0Sstevel@tonic-gate 		errno = EINVAL;
1187*0Sstevel@tonic-gate 		return (-1);
1188*0Sstevel@tonic-gate 	}
1189*0Sstevel@tonic-gate 
1190*0Sstevel@tonic-gate 	need += 4;		       /* two parens and two commas */
1191*0Sstevel@tonic-gate 	need += (host == NULL ? 0 : strlen(host));
1192*0Sstevel@tonic-gate 	need += (user == NULL ? 0 : strlen(user));
1193*0Sstevel@tonic-gate 	need += (domain == NULL ? 0 : strlen(domain));
1194*0Sstevel@tonic-gate 
1195*0Sstevel@tonic-gate 	if (buffer == NULL) {
1196*0Sstevel@tonic-gate 		*len = need;
1197*0Sstevel@tonic-gate 		return (0);
1198*0Sstevel@tonic-gate 	} else if (*buffer != NULL && need > *len) {
1199*0Sstevel@tonic-gate 		errno = EINVAL;
1200*0Sstevel@tonic-gate 		return (-1);
1201*0Sstevel@tonic-gate 	}
1202*0Sstevel@tonic-gate 
1203*0Sstevel@tonic-gate 	if (*buffer == NULL) {
1204*0Sstevel@tonic-gate 		need += 2;		/* for CRLF */
1205*0Sstevel@tonic-gate 		*buffer = memget(need);
1206*0Sstevel@tonic-gate 		if (*buffer == NULL) {
1207*0Sstevel@tonic-gate 			errno = ENOMEM;
1208*0Sstevel@tonic-gate 			return (-1);
1209*0Sstevel@tonic-gate 		}
1210*0Sstevel@tonic-gate 
1211*0Sstevel@tonic-gate 		*len = need;
1212*0Sstevel@tonic-gate 	}
1213*0Sstevel@tonic-gate 
1214*0Sstevel@tonic-gate 	(*buffer)[0] = '(';
1215*0Sstevel@tonic-gate 	(*buffer)[1] = '\0';
1216*0Sstevel@tonic-gate 
1217*0Sstevel@tonic-gate 	if (host != NULL)
1218*0Sstevel@tonic-gate 		strcat(*buffer, host);
1219*0Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
1220*0Sstevel@tonic-gate 
1221*0Sstevel@tonic-gate 	if (user != NULL)
1222*0Sstevel@tonic-gate 		strcat(*buffer, user);
1223*0Sstevel@tonic-gate 	strcat(*buffer, fieldsep);
1224*0Sstevel@tonic-gate 
1225*0Sstevel@tonic-gate 	if (domain != NULL)
1226*0Sstevel@tonic-gate 		strcat(*buffer, domain);
1227*0Sstevel@tonic-gate 	strcat(*buffer, ")");
1228*0Sstevel@tonic-gate 
1229*0Sstevel@tonic-gate 	return (0);
1230*0Sstevel@tonic-gate }
1231*0Sstevel@tonic-gate 
1232*0Sstevel@tonic-gate 
1233*0Sstevel@tonic-gate 
1234*0Sstevel@tonic-gate /* ---------- */
1235*0Sstevel@tonic-gate 
1236*0Sstevel@tonic-gate 
1237*0Sstevel@tonic-gate /*
1238*0Sstevel@tonic-gate  * int irp_unmarshall_ng(const char **host, const char **user,
1239*0Sstevel@tonic-gate  *			 const char **domain, char *buffer)
1240*0Sstevel@tonic-gate  *
1241*0Sstevel@tonic-gate  * notes:
1242*0Sstevel@tonic-gate  *
1243*0Sstevel@tonic-gate  *	Unpacks the BUFFER into 3 character arrays it allocates and assigns
1244*0Sstevel@tonic-gate  *	to *HOST, *USER and *DOMAIN. If any field of the value is empty,
1245*0Sstevel@tonic-gate  *	then the corresponding paramater value will be set to NULL.
1246*0Sstevel@tonic-gate  *
1247*0Sstevel@tonic-gate  * return:
1248*0Sstevel@tonic-gate  *
1249*0Sstevel@tonic-gate  *	0 on success and -1 on failure.
1250*0Sstevel@tonic-gate  */
1251*0Sstevel@tonic-gate 
1252*0Sstevel@tonic-gate int
1253*0Sstevel@tonic-gate irp_unmarshall_ng(const char **hostp, const char **userp, const char **domainp,
1254*0Sstevel@tonic-gate 		  char *buffer)
1255*0Sstevel@tonic-gate {
1256*0Sstevel@tonic-gate 	char *p, *q;
1257*0Sstevel@tonic-gate 	char fieldsep = ',';
1258*0Sstevel@tonic-gate 	int myerrno = EINVAL;
1259*0Sstevel@tonic-gate 	char *host, *user, *domain;
1260*0Sstevel@tonic-gate 
1261*0Sstevel@tonic-gate 	if (userp == NULL || hostp == NULL ||
1262*0Sstevel@tonic-gate 	    domainp == NULL || buffer == NULL) {
1263*0Sstevel@tonic-gate 		errno = EINVAL;
1264*0Sstevel@tonic-gate 		return (-1);
1265*0Sstevel@tonic-gate 	}
1266*0Sstevel@tonic-gate 
1267*0Sstevel@tonic-gate 	host = user = domain = NULL;
1268*0Sstevel@tonic-gate 
1269*0Sstevel@tonic-gate 	p = buffer;
1270*0Sstevel@tonic-gate 	while (isspace((unsigned char)*p)) {
1271*0Sstevel@tonic-gate 		p++;
1272*0Sstevel@tonic-gate 	}
1273*0Sstevel@tonic-gate 	if (*p != '(') {
1274*0Sstevel@tonic-gate 		goto error;
1275*0Sstevel@tonic-gate 	}
1276*0Sstevel@tonic-gate 
1277*0Sstevel@tonic-gate 	q = p + 1;
1278*0Sstevel@tonic-gate 	while (*q && *q != fieldsep)
1279*0Sstevel@tonic-gate 		q++;
1280*0Sstevel@tonic-gate 	if (!*q) {
1281*0Sstevel@tonic-gate 		goto error;
1282*0Sstevel@tonic-gate 	} else if (q > p + 1) {
1283*0Sstevel@tonic-gate 		host = strndup(p, q - p);
1284*0Sstevel@tonic-gate 	}
1285*0Sstevel@tonic-gate 
1286*0Sstevel@tonic-gate 	p = q + 1;
1287*0Sstevel@tonic-gate 	if (!*p) {
1288*0Sstevel@tonic-gate 		goto error;
1289*0Sstevel@tonic-gate 	} else if (*p != fieldsep) {
1290*0Sstevel@tonic-gate 		q = p + 1;
1291*0Sstevel@tonic-gate 		while (*q && *q != fieldsep)
1292*0Sstevel@tonic-gate 			q++;
1293*0Sstevel@tonic-gate 		if (!*q) {
1294*0Sstevel@tonic-gate 			goto error;
1295*0Sstevel@tonic-gate 		}
1296*0Sstevel@tonic-gate 		user = strndup(p, q - p);
1297*0Sstevel@tonic-gate 	} else {
1298*0Sstevel@tonic-gate 		p++;
1299*0Sstevel@tonic-gate 	}
1300*0Sstevel@tonic-gate 
1301*0Sstevel@tonic-gate 	if (!*p) {
1302*0Sstevel@tonic-gate 		goto error;
1303*0Sstevel@tonic-gate 	} else if (*p != ')') {
1304*0Sstevel@tonic-gate 		q = p + 1;
1305*0Sstevel@tonic-gate 		while (*q && *q != ')')
1306*0Sstevel@tonic-gate 			q++;
1307*0Sstevel@tonic-gate 		if (!*q) {
1308*0Sstevel@tonic-gate 			goto error;
1309*0Sstevel@tonic-gate 		}
1310*0Sstevel@tonic-gate 		domain = strndup(p, q - p);
1311*0Sstevel@tonic-gate 	}
1312*0Sstevel@tonic-gate 	*hostp = host;
1313*0Sstevel@tonic-gate 	*userp = user;
1314*0Sstevel@tonic-gate 	*domainp = domain;
1315*0Sstevel@tonic-gate 
1316*0Sstevel@tonic-gate 	return (0);
1317*0Sstevel@tonic-gate 
1318*0Sstevel@tonic-gate  error:
1319*0Sstevel@tonic-gate 	errno = myerrno;
1320*0Sstevel@tonic-gate 
1321*0Sstevel@tonic-gate 	if (host != NULL) free(host);
1322*0Sstevel@tonic-gate 	if (user != NULL) free(user);
1323*0Sstevel@tonic-gate 	if (domain != NULL) free(domain);
1324*0Sstevel@tonic-gate 
1325*0Sstevel@tonic-gate 	return (-1);
1326*0Sstevel@tonic-gate }
1327*0Sstevel@tonic-gate 
1328*0Sstevel@tonic-gate /* ------------------------- struct netgrp ------------------------- */
1329*0Sstevel@tonic-gate 
1330*0Sstevel@tonic-gate 
1331*0Sstevel@tonic-gate 
1332*0Sstevel@tonic-gate 
1333*0Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct nwent +++++++++++++++++++++++++ */
1334*0Sstevel@tonic-gate 
1335*0Sstevel@tonic-gate 
1336*0Sstevel@tonic-gate /*
1337*0Sstevel@tonic-gate  * int irp_marshall_nw(struct nwent *ne, char **buffer, size_t *len)
1338*0Sstevel@tonic-gate  *
1339*0Sstevel@tonic-gate  * notes:
1340*0Sstevel@tonic-gate  *
1341*0Sstevel@tonic-gate  *	See at top.
1342*0Sstevel@tonic-gate  *
1343*0Sstevel@tonic-gate  * return:
1344*0Sstevel@tonic-gate  *
1345*0Sstevel@tonic-gate  *	0 on success and -1 on failure.
1346*0Sstevel@tonic-gate  *
1347*0Sstevel@tonic-gate  */
1348*0Sstevel@tonic-gate 
1349*0Sstevel@tonic-gate int
1350*0Sstevel@tonic-gate irp_marshall_nw(struct nwent *ne, char **buffer, size_t *len) {
1351*0Sstevel@tonic-gate 	size_t need = 1;	/* for null byte */
1352*0Sstevel@tonic-gate 	char nAddrType[24];
1353*0Sstevel@tonic-gate 	char nNet[MAXPADDRSIZE];
1354*0Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
1355*0Sstevel@tonic-gate 
1356*0Sstevel@tonic-gate 	if (ne == NULL || len == NULL) {
1357*0Sstevel@tonic-gate 		return (-1);
1358*0Sstevel@tonic-gate 	}
1359*0Sstevel@tonic-gate 
1360*0Sstevel@tonic-gate 	strcpy(nAddrType, ADDR_T_STR(ne->n_addrtype));
1361*0Sstevel@tonic-gate 
1362*0Sstevel@tonic-gate 	if (inet_net_ntop(ne->n_addrtype, ne->n_addr, ne->n_length,
1363*0Sstevel@tonic-gate 			  nNet, sizeof nNet) == NULL) {
1364*0Sstevel@tonic-gate 		return (-1);
1365*0Sstevel@tonic-gate 	}
1366*0Sstevel@tonic-gate 
1367*0Sstevel@tonic-gate 
1368*0Sstevel@tonic-gate 	need += strlen(ne->n_name) + 1;
1369*0Sstevel@tonic-gate 	need += joinlength(ne->n_aliases) + 1;
1370*0Sstevel@tonic-gate 	need += strlen(nAddrType) + 1;
1371*0Sstevel@tonic-gate 	need += strlen(nNet) + 1;
1372*0Sstevel@tonic-gate 
1373*0Sstevel@tonic-gate 	if (buffer == NULL) {
1374*0Sstevel@tonic-gate 		*len = need;
1375*0Sstevel@tonic-gate 		return (0);
1376*0Sstevel@tonic-gate 	}
1377*0Sstevel@tonic-gate 
1378*0Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
1379*0Sstevel@tonic-gate 		errno = EINVAL;
1380*0Sstevel@tonic-gate 		return (-1);
1381*0Sstevel@tonic-gate 	}
1382*0Sstevel@tonic-gate 
1383*0Sstevel@tonic-gate 	if (*buffer == NULL) {
1384*0Sstevel@tonic-gate 		need += 2;		/* for CRLF */
1385*0Sstevel@tonic-gate 		*buffer = memget(need);
1386*0Sstevel@tonic-gate 		if (*buffer == NULL) {
1387*0Sstevel@tonic-gate 			errno = ENOMEM;
1388*0Sstevel@tonic-gate 			return (-1);
1389*0Sstevel@tonic-gate 		}
1390*0Sstevel@tonic-gate 
1391*0Sstevel@tonic-gate 		*len = need;
1392*0Sstevel@tonic-gate 	}
1393*0Sstevel@tonic-gate 
1394*0Sstevel@tonic-gate 	strcpy(*buffer, ne->n_name);		strcat(*buffer, fieldsep);
1395*0Sstevel@tonic-gate 	joinarray(ne->n_aliases, *buffer, COMMA) ; strcat(*buffer, fieldsep);
1396*0Sstevel@tonic-gate 	strcat(*buffer, nAddrType);		strcat(*buffer, fieldsep);
1397*0Sstevel@tonic-gate 	strcat(*buffer, nNet);			strcat(*buffer, fieldsep);
1398*0Sstevel@tonic-gate 
1399*0Sstevel@tonic-gate 	return (0);
1400*0Sstevel@tonic-gate }
1401*0Sstevel@tonic-gate 
1402*0Sstevel@tonic-gate 
1403*0Sstevel@tonic-gate 
1404*0Sstevel@tonic-gate /*
1405*0Sstevel@tonic-gate  * int irp_unmarshall_nw(struct nwent *ne, char *buffer)
1406*0Sstevel@tonic-gate  *
1407*0Sstevel@tonic-gate  * notes:
1408*0Sstevel@tonic-gate  *
1409*0Sstevel@tonic-gate  *	See note up top.
1410*0Sstevel@tonic-gate  *
1411*0Sstevel@tonic-gate  * return:
1412*0Sstevel@tonic-gate  *
1413*0Sstevel@tonic-gate  *	0 on success and -1 on failure.
1414*0Sstevel@tonic-gate  *
1415*0Sstevel@tonic-gate  */
1416*0Sstevel@tonic-gate 
1417*0Sstevel@tonic-gate int
1418*0Sstevel@tonic-gate irp_unmarshall_nw(struct nwent *ne, char *buffer) {
1419*0Sstevel@tonic-gate 	char *p, *q;
1420*0Sstevel@tonic-gate 	int naddrtype;
1421*0Sstevel@tonic-gate 	long nnet;
1422*0Sstevel@tonic-gate 	int bits;
1423*0Sstevel@tonic-gate 	char *name = NULL;
1424*0Sstevel@tonic-gate 	char **aliases = NULL;
1425*0Sstevel@tonic-gate 	char tmpbuf[24];
1426*0Sstevel@tonic-gate 	char *tb;
1427*0Sstevel@tonic-gate 	char fieldsep = ':';
1428*0Sstevel@tonic-gate 	int myerrno = EINVAL;
1429*0Sstevel@tonic-gate 
1430*0Sstevel@tonic-gate 	if (ne == NULL || buffer == NULL) {
1431*0Sstevel@tonic-gate 		goto error;
1432*0Sstevel@tonic-gate 	}
1433*0Sstevel@tonic-gate 
1434*0Sstevel@tonic-gate 	p = buffer;
1435*0Sstevel@tonic-gate 
1436*0Sstevel@tonic-gate 	/* n_name field */
1437*0Sstevel@tonic-gate 	name = NULL;
1438*0Sstevel@tonic-gate 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0) {
1439*0Sstevel@tonic-gate 		goto error;
1440*0Sstevel@tonic-gate 	}
1441*0Sstevel@tonic-gate 
1442*0Sstevel@tonic-gate 
1443*0Sstevel@tonic-gate 	/* n_aliases field. Aliases are separated by commas */
1444*0Sstevel@tonic-gate 	q = strchr(p, fieldsep);
1445*0Sstevel@tonic-gate 	if (q == NULL) {
1446*0Sstevel@tonic-gate 		goto error;
1447*0Sstevel@tonic-gate 	}
1448*0Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
1449*0Sstevel@tonic-gate 	if (aliases == NULL) {
1450*0Sstevel@tonic-gate 		myerrno = errno;
1451*0Sstevel@tonic-gate 		goto error;
1452*0Sstevel@tonic-gate 	}
1453*0Sstevel@tonic-gate 	p = q + 1;
1454*0Sstevel@tonic-gate 
1455*0Sstevel@tonic-gate 
1456*0Sstevel@tonic-gate 	/* h_addrtype field */
1457*0Sstevel@tonic-gate 	tb = tmpbuf;
1458*0Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1459*0Sstevel@tonic-gate 	    strlen(tb) == 0) {
1460*0Sstevel@tonic-gate 		goto error;
1461*0Sstevel@tonic-gate 	}
1462*0Sstevel@tonic-gate 	if (strcmp(tmpbuf, "AF_INET") == 0)
1463*0Sstevel@tonic-gate 		naddrtype = AF_INET;
1464*0Sstevel@tonic-gate 	else if (strcmp(tmpbuf, "AF_INET6") == 0)
1465*0Sstevel@tonic-gate 		naddrtype = AF_INET6;
1466*0Sstevel@tonic-gate 	else
1467*0Sstevel@tonic-gate 		goto error;
1468*0Sstevel@tonic-gate 
1469*0Sstevel@tonic-gate 
1470*0Sstevel@tonic-gate 	/* n_net field */
1471*0Sstevel@tonic-gate 	tb = tmpbuf;
1472*0Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1473*0Sstevel@tonic-gate 	    strlen(tb) == 0) {
1474*0Sstevel@tonic-gate 		goto error;
1475*0Sstevel@tonic-gate 	}
1476*0Sstevel@tonic-gate 	nnet = 0;
1477*0Sstevel@tonic-gate 	bits = inet_net_pton(naddrtype, tmpbuf, &nnet, sizeof nnet);
1478*0Sstevel@tonic-gate 	if (bits < 0) {
1479*0Sstevel@tonic-gate 		goto error;
1480*0Sstevel@tonic-gate 	}
1481*0Sstevel@tonic-gate 
1482*0Sstevel@tonic-gate 	/* nnet = ntohl(nnet); */ /* keep in network order for nwent */
1483*0Sstevel@tonic-gate 
1484*0Sstevel@tonic-gate 	ne->n_name = name;
1485*0Sstevel@tonic-gate 	ne->n_aliases = aliases;
1486*0Sstevel@tonic-gate 	ne->n_addrtype = naddrtype;
1487*0Sstevel@tonic-gate 	ne->n_length = bits;
1488*0Sstevel@tonic-gate 	ne->n_addr = malloc(sizeof nnet);
1489*0Sstevel@tonic-gate 	if (ne->n_addr == NULL) {
1490*0Sstevel@tonic-gate 		goto error;
1491*0Sstevel@tonic-gate 	}
1492*0Sstevel@tonic-gate 
1493*0Sstevel@tonic-gate 	memcpy(ne->n_addr, &nnet, sizeof nnet);
1494*0Sstevel@tonic-gate 
1495*0Sstevel@tonic-gate 	return (0);
1496*0Sstevel@tonic-gate 
1497*0Sstevel@tonic-gate  error:
1498*0Sstevel@tonic-gate 	errno = myerrno;
1499*0Sstevel@tonic-gate 
1500*0Sstevel@tonic-gate 	if (name != NULL) free(name);
1501*0Sstevel@tonic-gate 	free_array(aliases, 0);
1502*0Sstevel@tonic-gate 
1503*0Sstevel@tonic-gate 	return (-1);
1504*0Sstevel@tonic-gate }
1505*0Sstevel@tonic-gate 
1506*0Sstevel@tonic-gate 
1507*0Sstevel@tonic-gate /* ------------------------- struct nwent ------------------------- */
1508*0Sstevel@tonic-gate 
1509*0Sstevel@tonic-gate 
1510*0Sstevel@tonic-gate /* +++++++++++++++++++++++++ struct netent +++++++++++++++++++++++++ */
1511*0Sstevel@tonic-gate 
1512*0Sstevel@tonic-gate 
1513*0Sstevel@tonic-gate /*
1514*0Sstevel@tonic-gate  * int irp_marshall_ne(struct netent *ne, char **buffer, size_t *len)
1515*0Sstevel@tonic-gate  *
1516*0Sstevel@tonic-gate  * notes:
1517*0Sstevel@tonic-gate  *
1518*0Sstevel@tonic-gate  *	See at top.
1519*0Sstevel@tonic-gate  *
1520*0Sstevel@tonic-gate  * return:
1521*0Sstevel@tonic-gate  *
1522*0Sstevel@tonic-gate  *	0 on success and -1 on failure.
1523*0Sstevel@tonic-gate  *
1524*0Sstevel@tonic-gate  */
1525*0Sstevel@tonic-gate 
1526*0Sstevel@tonic-gate int
1527*0Sstevel@tonic-gate irp_marshall_ne(struct netent *ne, char **buffer, size_t *len) {
1528*0Sstevel@tonic-gate 	size_t need = 1;	/* for null byte */
1529*0Sstevel@tonic-gate 	char nAddrType[24];
1530*0Sstevel@tonic-gate 	char nNet[MAXPADDRSIZE];
1531*0Sstevel@tonic-gate 	const char *fieldsep = COLONSTR;
1532*0Sstevel@tonic-gate 	long nval;
1533*0Sstevel@tonic-gate 
1534*0Sstevel@tonic-gate 	if (ne == NULL || len == NULL) {
1535*0Sstevel@tonic-gate 		return (-1);
1536*0Sstevel@tonic-gate 	}
1537*0Sstevel@tonic-gate 
1538*0Sstevel@tonic-gate 	strcpy(nAddrType, ADDR_T_STR(ne->n_addrtype));
1539*0Sstevel@tonic-gate 
1540*0Sstevel@tonic-gate 	nval = htonl(ne->n_net);
1541*0Sstevel@tonic-gate 	if (inet_ntop(ne->n_addrtype, &nval, nNet, sizeof nNet) == NULL) {
1542*0Sstevel@tonic-gate 		return (-1);
1543*0Sstevel@tonic-gate 	}
1544*0Sstevel@tonic-gate 
1545*0Sstevel@tonic-gate 	need += strlen(ne->n_name) + 1;
1546*0Sstevel@tonic-gate 	need += joinlength(ne->n_aliases) + 1;
1547*0Sstevel@tonic-gate 	need += strlen(nAddrType) + 1;
1548*0Sstevel@tonic-gate 	need += strlen(nNet) + 1;
1549*0Sstevel@tonic-gate 
1550*0Sstevel@tonic-gate 	if (buffer == NULL) {
1551*0Sstevel@tonic-gate 		*len = need;
1552*0Sstevel@tonic-gate 		return (0);
1553*0Sstevel@tonic-gate 	}
1554*0Sstevel@tonic-gate 
1555*0Sstevel@tonic-gate 	if (*buffer != NULL && need > *len) {
1556*0Sstevel@tonic-gate 		errno = EINVAL;
1557*0Sstevel@tonic-gate 		return (-1);
1558*0Sstevel@tonic-gate 	}
1559*0Sstevel@tonic-gate 
1560*0Sstevel@tonic-gate 	if (*buffer == NULL) {
1561*0Sstevel@tonic-gate 		need += 2;		/* for CRLF */
1562*0Sstevel@tonic-gate 		*buffer = memget(need);
1563*0Sstevel@tonic-gate 		if (*buffer == NULL) {
1564*0Sstevel@tonic-gate 			errno = ENOMEM;
1565*0Sstevel@tonic-gate 			return (-1);
1566*0Sstevel@tonic-gate 		}
1567*0Sstevel@tonic-gate 
1568*0Sstevel@tonic-gate 		*len = need;
1569*0Sstevel@tonic-gate 	}
1570*0Sstevel@tonic-gate 
1571*0Sstevel@tonic-gate 	strcpy(*buffer, ne->n_name);		strcat(*buffer, fieldsep);
1572*0Sstevel@tonic-gate 	joinarray(ne->n_aliases, *buffer, COMMA) ; strcat(*buffer, fieldsep);
1573*0Sstevel@tonic-gate 	strcat(*buffer, nAddrType);		strcat(*buffer, fieldsep);
1574*0Sstevel@tonic-gate 	strcat(*buffer, nNet);			strcat(*buffer, fieldsep);
1575*0Sstevel@tonic-gate 
1576*0Sstevel@tonic-gate 	return (0);
1577*0Sstevel@tonic-gate }
1578*0Sstevel@tonic-gate 
1579*0Sstevel@tonic-gate 
1580*0Sstevel@tonic-gate 
1581*0Sstevel@tonic-gate /*
1582*0Sstevel@tonic-gate  * int irp_unmarshall_ne(struct netent *ne, char *buffer)
1583*0Sstevel@tonic-gate  *
1584*0Sstevel@tonic-gate  * notes:
1585*0Sstevel@tonic-gate  *
1586*0Sstevel@tonic-gate  *	See note up top.
1587*0Sstevel@tonic-gate  *
1588*0Sstevel@tonic-gate  * return:
1589*0Sstevel@tonic-gate  *
1590*0Sstevel@tonic-gate  *	0 on success and -1 on failure.
1591*0Sstevel@tonic-gate  *
1592*0Sstevel@tonic-gate  */
1593*0Sstevel@tonic-gate 
1594*0Sstevel@tonic-gate int
1595*0Sstevel@tonic-gate irp_unmarshall_ne(struct netent *ne, char *buffer) {
1596*0Sstevel@tonic-gate 	char *p, *q;
1597*0Sstevel@tonic-gate 	int naddrtype;
1598*0Sstevel@tonic-gate 	long nnet;
1599*0Sstevel@tonic-gate 	int bits;
1600*0Sstevel@tonic-gate 	char *name = NULL;
1601*0Sstevel@tonic-gate 	char **aliases = NULL;
1602*0Sstevel@tonic-gate 	char tmpbuf[24];
1603*0Sstevel@tonic-gate 	char *tb;
1604*0Sstevel@tonic-gate 	char fieldsep = ':';
1605*0Sstevel@tonic-gate 	int myerrno = EINVAL;
1606*0Sstevel@tonic-gate 
1607*0Sstevel@tonic-gate 	if (ne == NULL || buffer == NULL) {
1608*0Sstevel@tonic-gate 		goto error;
1609*0Sstevel@tonic-gate 	}
1610*0Sstevel@tonic-gate 
1611*0Sstevel@tonic-gate 	p = buffer;
1612*0Sstevel@tonic-gate 
1613*0Sstevel@tonic-gate 	/* n_name field */
1614*0Sstevel@tonic-gate 	name = NULL;
1615*0Sstevel@tonic-gate 	if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0) {
1616*0Sstevel@tonic-gate 		goto error;
1617*0Sstevel@tonic-gate 	}
1618*0Sstevel@tonic-gate 
1619*0Sstevel@tonic-gate 
1620*0Sstevel@tonic-gate 	/* n_aliases field. Aliases are separated by commas */
1621*0Sstevel@tonic-gate 	q = strchr(p, fieldsep);
1622*0Sstevel@tonic-gate 	if (q == NULL) {
1623*0Sstevel@tonic-gate 		goto error;
1624*0Sstevel@tonic-gate 	}
1625*0Sstevel@tonic-gate 	aliases = splitarray(p, q, COMMA);
1626*0Sstevel@tonic-gate 	if (aliases == NULL) {
1627*0Sstevel@tonic-gate 		myerrno = errno;
1628*0Sstevel@tonic-gate 		goto error;
1629*0Sstevel@tonic-gate 	}
1630*0Sstevel@tonic-gate 	p = q + 1;
1631*0Sstevel@tonic-gate 
1632*0Sstevel@tonic-gate 
1633*0Sstevel@tonic-gate 	/* h_addrtype field */
1634*0Sstevel@tonic-gate 	tb = tmpbuf;
1635*0Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1636*0Sstevel@tonic-gate 	    strlen(tb) == 0) {
1637*0Sstevel@tonic-gate 		goto error;
1638*0Sstevel@tonic-gate 	}
1639*0Sstevel@tonic-gate 	if (strcmp(tmpbuf, "AF_INET") == 0)
1640*0Sstevel@tonic-gate 		naddrtype = AF_INET;
1641*0Sstevel@tonic-gate 	else if (strcmp(tmpbuf, "AF_INET6") == 0)
1642*0Sstevel@tonic-gate 		naddrtype = AF_INET6;
1643*0Sstevel@tonic-gate 	else
1644*0Sstevel@tonic-gate 		goto error;
1645*0Sstevel@tonic-gate 
1646*0Sstevel@tonic-gate 
1647*0Sstevel@tonic-gate 	/* n_net field */
1648*0Sstevel@tonic-gate 	tb = tmpbuf;
1649*0Sstevel@tonic-gate 	if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL ||
1650*0Sstevel@tonic-gate 	    strlen(tb) == 0) {
1651*0Sstevel@tonic-gate 		goto error;
1652*0Sstevel@tonic-gate 	}
1653*0Sstevel@tonic-gate 	bits = inet_net_pton(naddrtype, tmpbuf, &nnet, sizeof nnet);
1654*0Sstevel@tonic-gate 	if (bits < 0) {
1655*0Sstevel@tonic-gate 		goto error;
1656*0Sstevel@tonic-gate 	}
1657*0Sstevel@tonic-gate 	nnet = ntohl(nnet);
1658*0Sstevel@tonic-gate 
1659*0Sstevel@tonic-gate 	ne->n_name = name;
1660*0Sstevel@tonic-gate 	ne->n_aliases = aliases;
1661*0Sstevel@tonic-gate 	ne->n_addrtype = naddrtype;
1662*0Sstevel@tonic-gate 	ne->n_net = nnet;
1663*0Sstevel@tonic-gate 
1664*0Sstevel@tonic-gate 	return (0);
1665*0Sstevel@tonic-gate 
1666*0Sstevel@tonic-gate  error:
1667*0Sstevel@tonic-gate 	errno = myerrno;
1668*0Sstevel@tonic-gate 
1669*0Sstevel@tonic-gate 	if (name != NULL) free(name);
1670*0Sstevel@tonic-gate 	free_array(aliases, 0);
1671*0Sstevel@tonic-gate 
1672*0Sstevel@tonic-gate 	return (-1);
1673*0Sstevel@tonic-gate }
1674*0Sstevel@tonic-gate 
1675*0Sstevel@tonic-gate 
1676*0Sstevel@tonic-gate /* ------------------------- struct netent ------------------------- */
1677*0Sstevel@tonic-gate 
1678*0Sstevel@tonic-gate 
1679*0Sstevel@tonic-gate /* =========================================================================== */
1680*0Sstevel@tonic-gate 
1681*0Sstevel@tonic-gate 
1682*0Sstevel@tonic-gate /*
1683*0Sstevel@tonic-gate  * static char ** splitarray(const char *buffer, const char *buffend, char delim)
1684*0Sstevel@tonic-gate  *
1685*0Sstevel@tonic-gate  * notes:
1686*0Sstevel@tonic-gate  *
1687*0Sstevel@tonic-gate  *	Split a delim separated astring. Not allowed
1688*0Sstevel@tonic-gate  *	to have two delims next to each other. BUFFER points to begining of
1689*0Sstevel@tonic-gate  *	string, BUFFEND points to one past the end of the string
1690*0Sstevel@tonic-gate  *	(i.e. points at where the null byte would be if null
1691*0Sstevel@tonic-gate  *	terminated).
1692*0Sstevel@tonic-gate  *
1693*0Sstevel@tonic-gate  * return:
1694*0Sstevel@tonic-gate  *
1695*0Sstevel@tonic-gate  *	Returns a malloced array of pointers, each pointer pointing to a
1696*0Sstevel@tonic-gate  *	malloced string. If BUFEER is an empty string, then return values is
1697*0Sstevel@tonic-gate  *	array of 1 pointer that is NULL. Returns NULL on failure.
1698*0Sstevel@tonic-gate  *
1699*0Sstevel@tonic-gate  */
1700*0Sstevel@tonic-gate 
1701*0Sstevel@tonic-gate static char **
1702*0Sstevel@tonic-gate splitarray(const char *buffer, const char *buffend, char delim) {
1703*0Sstevel@tonic-gate 	const char *p, *q;
1704*0Sstevel@tonic-gate 	int count = 0;
1705*0Sstevel@tonic-gate 	char **arr = NULL;
1706*0Sstevel@tonic-gate 	char **aptr;
1707*0Sstevel@tonic-gate 
1708*0Sstevel@tonic-gate 	if (buffend < buffer)
1709*0Sstevel@tonic-gate 		return (NULL);
1710*0Sstevel@tonic-gate 	else if (buffend > buffer && *buffer == delim)
1711*0Sstevel@tonic-gate 		return (NULL);
1712*0Sstevel@tonic-gate 	else if (buffend > buffer && *(buffend - 1) == delim)
1713*0Sstevel@tonic-gate 		return (NULL);
1714*0Sstevel@tonic-gate 
1715*0Sstevel@tonic-gate 	/* count the number of field and make sure none are empty */
1716*0Sstevel@tonic-gate 	if (buffend > buffer + 1) {
1717*0Sstevel@tonic-gate 		for (count = 1, q = buffer ; q != buffend ; q++) {
1718*0Sstevel@tonic-gate 			if (*q == delim) {
1719*0Sstevel@tonic-gate 				if (q > buffer && (*(q - 1) == delim)) {
1720*0Sstevel@tonic-gate 					errno = EINVAL;
1721*0Sstevel@tonic-gate 					return (NULL);
1722*0Sstevel@tonic-gate 				}
1723*0Sstevel@tonic-gate 				count++;
1724*0Sstevel@tonic-gate 			}
1725*0Sstevel@tonic-gate 		}
1726*0Sstevel@tonic-gate 	}
1727*0Sstevel@tonic-gate 
1728*0Sstevel@tonic-gate 	if (count > 0) {
1729*0Sstevel@tonic-gate 		count++ ;		/* for NULL at end */
1730*0Sstevel@tonic-gate 		aptr = arr = malloc(count * sizeof (char *));
1731*0Sstevel@tonic-gate 		if (aptr == NULL) {
1732*0Sstevel@tonic-gate 			 errno = ENOMEM;
1733*0Sstevel@tonic-gate 			 return (NULL);
1734*0Sstevel@tonic-gate 		 }
1735*0Sstevel@tonic-gate 
1736*0Sstevel@tonic-gate 		memset(arr, 0x0, count * sizeof (char *));
1737*0Sstevel@tonic-gate 		for (p = buffer ; p < buffend ; p++) {
1738*0Sstevel@tonic-gate 			for (q = p ; *q != delim && q != buffend ; q++)
1739*0Sstevel@tonic-gate 				/* nothing */;
1740*0Sstevel@tonic-gate 			*aptr = strndup(p, q - p);
1741*0Sstevel@tonic-gate 
1742*0Sstevel@tonic-gate 			p = q;
1743*0Sstevel@tonic-gate 			aptr++;
1744*0Sstevel@tonic-gate 		}
1745*0Sstevel@tonic-gate 		*aptr = NULL;
1746*0Sstevel@tonic-gate 	} else {
1747*0Sstevel@tonic-gate 		arr = malloc(sizeof (char *));
1748*0Sstevel@tonic-gate 		if (arr == NULL) {
1749*0Sstevel@tonic-gate 			errno = ENOMEM;
1750*0Sstevel@tonic-gate 			return (NULL);
1751*0Sstevel@tonic-gate 		}
1752*0Sstevel@tonic-gate 
1753*0Sstevel@tonic-gate 		*arr = NULL;
1754*0Sstevel@tonic-gate 	}
1755*0Sstevel@tonic-gate 
1756*0Sstevel@tonic-gate 	return (arr);
1757*0Sstevel@tonic-gate }
1758*0Sstevel@tonic-gate 
1759*0Sstevel@tonic-gate 
1760*0Sstevel@tonic-gate 
1761*0Sstevel@tonic-gate 
1762*0Sstevel@tonic-gate /*
1763*0Sstevel@tonic-gate  * static size_t joinlength(char * const *argv)
1764*0Sstevel@tonic-gate  *
1765*0Sstevel@tonic-gate  * return:
1766*0Sstevel@tonic-gate  *
1767*0Sstevel@tonic-gate  *	the number of bytes in all the arrays pointed at
1768*0Sstevel@tonic-gate  *	by argv, including their null bytes(which will usually be turned
1769*0Sstevel@tonic-gate  *	into commas).
1770*0Sstevel@tonic-gate  *
1771*0Sstevel@tonic-gate  *
1772*0Sstevel@tonic-gate  */
1773*0Sstevel@tonic-gate 
1774*0Sstevel@tonic-gate static size_t
1775*0Sstevel@tonic-gate joinlength(char * const *argv) {
1776*0Sstevel@tonic-gate 	int len = 0;
1777*0Sstevel@tonic-gate 
1778*0Sstevel@tonic-gate 	while (argv && *argv) {
1779*0Sstevel@tonic-gate 		len += (strlen(*argv) + 1);
1780*0Sstevel@tonic-gate 		argv++;
1781*0Sstevel@tonic-gate 	}
1782*0Sstevel@tonic-gate 
1783*0Sstevel@tonic-gate 	return (len);
1784*0Sstevel@tonic-gate }
1785*0Sstevel@tonic-gate 
1786*0Sstevel@tonic-gate 
1787*0Sstevel@tonic-gate 
1788*0Sstevel@tonic-gate /*
1789*0Sstevel@tonic-gate  * int joinarray(char * const *argv, char *buffer, char delim)
1790*0Sstevel@tonic-gate  *
1791*0Sstevel@tonic-gate  * notes:
1792*0Sstevel@tonic-gate  *
1793*0Sstevel@tonic-gate  *	Copy all the ARGV strings into the end of BUFFER
1794*0Sstevel@tonic-gate  *	separating them with DELIM.  BUFFER is assumed to have
1795*0Sstevel@tonic-gate  *	enough space to hold everything and to be already null-terminated.
1796*0Sstevel@tonic-gate  *
1797*0Sstevel@tonic-gate  * return:
1798*0Sstevel@tonic-gate  *
1799*0Sstevel@tonic-gate  *	0 unless argv or buffer is NULL.
1800*0Sstevel@tonic-gate  *
1801*0Sstevel@tonic-gate  *
1802*0Sstevel@tonic-gate  */
1803*0Sstevel@tonic-gate 
1804*0Sstevel@tonic-gate static int
1805*0Sstevel@tonic-gate joinarray(char * const *argv, char *buffer, char delim) {
1806*0Sstevel@tonic-gate 	char * const *p;
1807*0Sstevel@tonic-gate 	char sep[2];
1808*0Sstevel@tonic-gate 
1809*0Sstevel@tonic-gate 	if (argv == NULL || buffer == NULL) {
1810*0Sstevel@tonic-gate 		errno = EINVAL;
1811*0Sstevel@tonic-gate 		return (-1);
1812*0Sstevel@tonic-gate 	}
1813*0Sstevel@tonic-gate 
1814*0Sstevel@tonic-gate 	sep[0] = delim;
1815*0Sstevel@tonic-gate 	sep[1] = 0x0;
1816*0Sstevel@tonic-gate 
1817*0Sstevel@tonic-gate 	for (p = argv ; *p != NULL ; p++) {
1818*0Sstevel@tonic-gate 		strcat(buffer, *p);
1819*0Sstevel@tonic-gate 		if (*(p + 1) != NULL) {
1820*0Sstevel@tonic-gate 			strcat(buffer, sep);
1821*0Sstevel@tonic-gate 		}
1822*0Sstevel@tonic-gate 	}
1823*0Sstevel@tonic-gate 
1824*0Sstevel@tonic-gate 	return (0);
1825*0Sstevel@tonic-gate }
1826*0Sstevel@tonic-gate 
1827*0Sstevel@tonic-gate 
1828*0Sstevel@tonic-gate /*
1829*0Sstevel@tonic-gate  * static char * getfield(char **res, size_t reslen, char **ptr, char delim)
1830*0Sstevel@tonic-gate  *
1831*0Sstevel@tonic-gate  * notes:
1832*0Sstevel@tonic-gate  *
1833*0Sstevel@tonic-gate  *	Stores in *RES, which is a buffer of length RESLEN, a
1834*0Sstevel@tonic-gate  *	copy of the bytes from *PTR up to and including the first
1835*0Sstevel@tonic-gate  *	instance of DELIM. If *RES is NULL, then it will be
1836*0Sstevel@tonic-gate  *	assigned a malloced buffer to hold the copy. *PTR is
1837*0Sstevel@tonic-gate  *	modified to point at the found delimiter.
1838*0Sstevel@tonic-gate  *
1839*0Sstevel@tonic-gate  * return:
1840*0Sstevel@tonic-gate  *
1841*0Sstevel@tonic-gate  *	If there was no delimiter, then NULL is returned,
1842*0Sstevel@tonic-gate  *	otherewise *RES is returned.
1843*0Sstevel@tonic-gate  *
1844*0Sstevel@tonic-gate  */
1845*0Sstevel@tonic-gate 
1846*0Sstevel@tonic-gate static char *
1847*0Sstevel@tonic-gate getfield(char **res, size_t reslen, char **ptr, char delim) {
1848*0Sstevel@tonic-gate 	char *q;
1849*0Sstevel@tonic-gate 
1850*0Sstevel@tonic-gate 	if (res == NULL || ptr == NULL || *ptr == NULL) {
1851*0Sstevel@tonic-gate 		errno = EINVAL;
1852*0Sstevel@tonic-gate 		return (NULL);
1853*0Sstevel@tonic-gate 	}
1854*0Sstevel@tonic-gate 
1855*0Sstevel@tonic-gate 	q = strchr(*ptr, delim);
1856*0Sstevel@tonic-gate 
1857*0Sstevel@tonic-gate 	if (q == NULL) {
1858*0Sstevel@tonic-gate 		errno = EINVAL;
1859*0Sstevel@tonic-gate 		return (NULL);
1860*0Sstevel@tonic-gate 	} else {
1861*0Sstevel@tonic-gate 		if (*res == NULL) {
1862*0Sstevel@tonic-gate 			*res = strndup(*ptr, q - *ptr);
1863*0Sstevel@tonic-gate 		} else {
1864*0Sstevel@tonic-gate 			if ((size_t)(q - *ptr + 1) > reslen) { /* to big for res */
1865*0Sstevel@tonic-gate 				errno = EINVAL;
1866*0Sstevel@tonic-gate 				return (NULL);
1867*0Sstevel@tonic-gate 			} else {
1868*0Sstevel@tonic-gate 				strncpy(*res, *ptr, q - *ptr);
1869*0Sstevel@tonic-gate 				(*res)[q - *ptr] = 0x0;
1870*0Sstevel@tonic-gate 			}
1871*0Sstevel@tonic-gate 		}
1872*0Sstevel@tonic-gate 		*ptr = q + 1;
1873*0Sstevel@tonic-gate 	}
1874*0Sstevel@tonic-gate 
1875*0Sstevel@tonic-gate 	return (*res);
1876*0Sstevel@tonic-gate }
1877*0Sstevel@tonic-gate 
1878*0Sstevel@tonic-gate 
1879*0Sstevel@tonic-gate 
1880*0Sstevel@tonic-gate 
1881*0Sstevel@tonic-gate 
1882*0Sstevel@tonic-gate 
1883*0Sstevel@tonic-gate /*
1884*0Sstevel@tonic-gate  * static char * strndup(const char *str, size_t len)
1885*0Sstevel@tonic-gate  *
1886*0Sstevel@tonic-gate  * notes:
1887*0Sstevel@tonic-gate  *
1888*0Sstevel@tonic-gate  *	like strdup, except do len bytes instead of the whole string. Always
1889*0Sstevel@tonic-gate  *	null-terminates.
1890*0Sstevel@tonic-gate  *
1891*0Sstevel@tonic-gate  * return:
1892*0Sstevel@tonic-gate  *
1893*0Sstevel@tonic-gate  *	The newly malloced string.
1894*0Sstevel@tonic-gate  *
1895*0Sstevel@tonic-gate  */
1896*0Sstevel@tonic-gate 
1897*0Sstevel@tonic-gate static char *
1898*0Sstevel@tonic-gate strndup(const char *str, size_t len) {
1899*0Sstevel@tonic-gate 	char *p = malloc(len + 1);
1900*0Sstevel@tonic-gate 
1901*0Sstevel@tonic-gate 	if (p == NULL)
1902*0Sstevel@tonic-gate 		return (NULL);
1903*0Sstevel@tonic-gate 	strncpy(p, str, len);
1904*0Sstevel@tonic-gate 	p[len] = 0x0;
1905*0Sstevel@tonic-gate 	return (p);
1906*0Sstevel@tonic-gate }
1907*0Sstevel@tonic-gate 
1908*0Sstevel@tonic-gate 
1909*0Sstevel@tonic-gate #if WANT_MAIN
1910*0Sstevel@tonic-gate 
1911*0Sstevel@tonic-gate /*
1912*0Sstevel@tonic-gate  * static int strcmp_nws(const char *a, const char *b)
1913*0Sstevel@tonic-gate  *
1914*0Sstevel@tonic-gate  * notes:
1915*0Sstevel@tonic-gate  *
1916*0Sstevel@tonic-gate  *	do a strcmp, except uneven lengths of whitespace compare the same
1917*0Sstevel@tonic-gate  *
1918*0Sstevel@tonic-gate  * return:
1919*0Sstevel@tonic-gate  *
1920*0Sstevel@tonic-gate  */
1921*0Sstevel@tonic-gate 
1922*0Sstevel@tonic-gate static int
1923*0Sstevel@tonic-gate strcmp_nws(const char *a, const char *b) {
1924*0Sstevel@tonic-gate 	while (*a && *b) {
1925*0Sstevel@tonic-gate 		if (isspace(*a) && isspace(*b)) {
1926*0Sstevel@tonic-gate 			do {
1927*0Sstevel@tonic-gate 				a++;
1928*0Sstevel@tonic-gate 			} while (isspace(*a));
1929*0Sstevel@tonic-gate 			do {
1930*0Sstevel@tonic-gate 				b++;
1931*0Sstevel@tonic-gate 			} while (isspace(*b));
1932*0Sstevel@tonic-gate 		}
1933*0Sstevel@tonic-gate 		if (*a < *b)
1934*0Sstevel@tonic-gate 			return (-1);
1935*0Sstevel@tonic-gate 		else if (*a > *b)
1936*0Sstevel@tonic-gate 			return (1);
1937*0Sstevel@tonic-gate 
1938*0Sstevel@tonic-gate 		a++;
1939*0Sstevel@tonic-gate 		b++;;
1940*0Sstevel@tonic-gate 	}
1941*0Sstevel@tonic-gate 
1942*0Sstevel@tonic-gate 	if (*a == *b)
1943*0Sstevel@tonic-gate 		return (0);
1944*0Sstevel@tonic-gate 	else if (*a > *b)
1945*0Sstevel@tonic-gate 		return (1);
1946*0Sstevel@tonic-gate 	else
1947*0Sstevel@tonic-gate 		return (-1);
1948*0Sstevel@tonic-gate }
1949*0Sstevel@tonic-gate 
1950*0Sstevel@tonic-gate #endif
1951*0Sstevel@tonic-gate 
1952*0Sstevel@tonic-gate 
1953*0Sstevel@tonic-gate 
1954*0Sstevel@tonic-gate 
1955*0Sstevel@tonic-gate 
1956*0Sstevel@tonic-gate /*
1957*0Sstevel@tonic-gate  * static void free_array(char **argv, size_t entries)
1958*0Sstevel@tonic-gate  *
1959*0Sstevel@tonic-gate  * notes:
1960*0Sstevel@tonic-gate  *
1961*0Sstevel@tonic-gate  *	Free argv and each of the pointers inside it. The end of
1962*0Sstevel@tonic-gate  *	the array is when a NULL pointer is found inside. If
1963*0Sstevel@tonic-gate  *	entries is > 0, then NULL pointers inside the array do
1964*0Sstevel@tonic-gate  *	not indicate the end of the array.
1965*0Sstevel@tonic-gate  *
1966*0Sstevel@tonic-gate  */
1967*0Sstevel@tonic-gate 
1968*0Sstevel@tonic-gate static void
1969*0Sstevel@tonic-gate free_array(char **argv, size_t entries) {
1970*0Sstevel@tonic-gate 	char **p = argv;
1971*0Sstevel@tonic-gate 	int useEntries = (entries > 0);
1972*0Sstevel@tonic-gate 
1973*0Sstevel@tonic-gate 	if (argv == NULL)
1974*0Sstevel@tonic-gate 		return;
1975*0Sstevel@tonic-gate 
1976*0Sstevel@tonic-gate 	while ((useEntries && entries > 0) || *p) {
1977*0Sstevel@tonic-gate 		if (*p)
1978*0Sstevel@tonic-gate 			free(*p);
1979*0Sstevel@tonic-gate 		p++;
1980*0Sstevel@tonic-gate 		if (useEntries)
1981*0Sstevel@tonic-gate 			entries--;
1982*0Sstevel@tonic-gate 	}
1983*0Sstevel@tonic-gate 	free(argv);
1984*0Sstevel@tonic-gate }
1985*0Sstevel@tonic-gate 
1986*0Sstevel@tonic-gate 
1987*0Sstevel@tonic-gate 
1988*0Sstevel@tonic-gate 
1989*0Sstevel@tonic-gate 
1990*0Sstevel@tonic-gate /* ************************************************** */
1991*0Sstevel@tonic-gate 
1992*0Sstevel@tonic-gate #if WANT_MAIN
1993*0Sstevel@tonic-gate 
1994*0Sstevel@tonic-gate /* takes an option to indicate what sort of marshalling(read the code) and
1995*0Sstevel@tonic-gate    an argument. If the argument looks like a marshalled buffer(has a ':'
1996*0Sstevel@tonic-gate    embedded) then it's unmarshalled and the remarshalled and the new string
1997*0Sstevel@tonic-gate    is compared to the old one.
1998*0Sstevel@tonic-gate */
1999*0Sstevel@tonic-gate 
2000*0Sstevel@tonic-gate int
2001*0Sstevel@tonic-gate main(int argc, char **argv) {
2002*0Sstevel@tonic-gate 	char buffer[1024];
2003*0Sstevel@tonic-gate 	char *b = &buffer[0];
2004*0Sstevel@tonic-gate 	size_t len = sizeof buffer;
2005*0Sstevel@tonic-gate 	char option;
2006*0Sstevel@tonic-gate 
2007*0Sstevel@tonic-gate 	if (argc < 2 || argv[1][0] != '-')
2008*0Sstevel@tonic-gate 		exit(1);
2009*0Sstevel@tonic-gate 
2010*0Sstevel@tonic-gate 	option = argv[1][1];
2011*0Sstevel@tonic-gate 	argv++;
2012*0Sstevel@tonic-gate 	argc--;
2013*0Sstevel@tonic-gate 
2014*0Sstevel@tonic-gate 
2015*0Sstevel@tonic-gate #if 0
2016*0Sstevel@tonic-gate 	{
2017*0Sstevel@tonic-gate 		char buff[10];
2018*0Sstevel@tonic-gate 		char *p = argv[1], *q = &buff[0];
2019*0Sstevel@tonic-gate 
2020*0Sstevel@tonic-gate 		while (getfield(&q, sizeof buff, &p, ':') != NULL) {
2021*0Sstevel@tonic-gate 			printf("field: \"%s\"\n", q);
2022*0Sstevel@tonic-gate 			p++;
2023*0Sstevel@tonic-gate 		}
2024*0Sstevel@tonic-gate 		printf("p is now \"%s\"\n", p);
2025*0Sstevel@tonic-gate 	}
2026*0Sstevel@tonic-gate #endif
2027*0Sstevel@tonic-gate 
2028*0Sstevel@tonic-gate #if 0
2029*0Sstevel@tonic-gate 	{
2030*0Sstevel@tonic-gate 		char **x = splitarray(argv[1], argv[1] + strlen(argv[1]),
2031*0Sstevel@tonic-gate 				      argv[2][0]);
2032*0Sstevel@tonic-gate 		char **p;
2033*0Sstevel@tonic-gate 
2034*0Sstevel@tonic-gate 		if (x == NULL)
2035*0Sstevel@tonic-gate 			printf("split failed\n");
2036*0Sstevel@tonic-gate 
2037*0Sstevel@tonic-gate 		for (p = x ; p != NULL && *p != NULL ; p++) {
2038*0Sstevel@tonic-gate 			printf("\"%s\"\n", *p);
2039*0Sstevel@tonic-gate 		}
2040*0Sstevel@tonic-gate 	}
2041*0Sstevel@tonic-gate #endif
2042*0Sstevel@tonic-gate 
2043*0Sstevel@tonic-gate #if 1
2044*0Sstevel@tonic-gate 	switch(option) {
2045*0Sstevel@tonic-gate 	case 'n': {
2046*0Sstevel@tonic-gate 		struct nwent ne;
2047*0Sstevel@tonic-gate 		int i;
2048*0Sstevel@tonic-gate 
2049*0Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
2050*0Sstevel@tonic-gate 			if (irp_unmarshall_nw(&ne, argv[1]) != 0) {
2051*0Sstevel@tonic-gate 				printf("Unmarhsalling failed\n");
2052*0Sstevel@tonic-gate 				exit(1);
2053*0Sstevel@tonic-gate 			}
2054*0Sstevel@tonic-gate 
2055*0Sstevel@tonic-gate 			printf("Name: \"%s\"\n", ne.n_name);
2056*0Sstevel@tonic-gate 			printf("Aliases:");
2057*0Sstevel@tonic-gate 			for (i = 0 ; ne.n_aliases[i] != NULL ; i++)
2058*0Sstevel@tonic-gate 				printf("\n\t\"%s\"", ne.n_aliases[i]);
2059*0Sstevel@tonic-gate 			printf("\nAddrtype: %s\n", ADDR_T_STR(ne.n_addrtype));
2060*0Sstevel@tonic-gate 			inet_net_ntop(ne.n_addrtype, ne.n_addr, ne.n_length,
2061*0Sstevel@tonic-gate 				      buffer, sizeof buffer);
2062*0Sstevel@tonic-gate 			printf("Net: \"%s\"\n", buffer);
2063*0Sstevel@tonic-gate 			*((long*)ne.n_addr) = htonl(*((long*)ne.n_addr));
2064*0Sstevel@tonic-gate 			inet_net_ntop(ne.n_addrtype, ne.n_addr, ne.n_length,
2065*0Sstevel@tonic-gate 				      buffer, sizeof buffer);
2066*0Sstevel@tonic-gate 			printf("Corrected Net: \"%s\"\n", buffer);
2067*0Sstevel@tonic-gate 		} else {
2068*0Sstevel@tonic-gate 			struct netent *np1 = getnetbyname(argv[1]);
2069*0Sstevel@tonic-gate 			ne.n_name = np1->n_name;
2070*0Sstevel@tonic-gate 			ne.n_aliases = np1->n_aliases;
2071*0Sstevel@tonic-gate 			ne.n_addrtype = np1->n_addrtype;
2072*0Sstevel@tonic-gate 			ne.n_addr = &np1->n_net;
2073*0Sstevel@tonic-gate 			ne.n_length = (IN_CLASSA(np1->n_net) ?
2074*0Sstevel@tonic-gate 				       8 :
2075*0Sstevel@tonic-gate 				       (IN_CLASSB(np1->n_net) ?
2076*0Sstevel@tonic-gate 					16 :
2077*0Sstevel@tonic-gate 					(IN_CLASSC(np1->n_net) ?
2078*0Sstevel@tonic-gate 					 24 : -1)));
2079*0Sstevel@tonic-gate 			np1->n_net = htonl(np1->n_net);
2080*0Sstevel@tonic-gate 			if (irp_marshall_nw(&ne, &b, &len) != 0) {
2081*0Sstevel@tonic-gate 				printf("Marshalling failed\n");
2082*0Sstevel@tonic-gate 			}
2083*0Sstevel@tonic-gate 			printf("%s\n", b);
2084*0Sstevel@tonic-gate 		}
2085*0Sstevel@tonic-gate 		break;
2086*0Sstevel@tonic-gate 	}
2087*0Sstevel@tonic-gate 
2088*0Sstevel@tonic-gate 
2089*0Sstevel@tonic-gate 	case 'r': {
2090*0Sstevel@tonic-gate 		char **hosts, **users, **domains;
2091*0Sstevel@tonic-gate 		size_t entries;
2092*0Sstevel@tonic-gate 		int i;
2093*0Sstevel@tonic-gate 		char *buff;
2094*0Sstevel@tonic-gate 		size_t size;
2095*0Sstevel@tonic-gate 		char *ngname;
2096*0Sstevel@tonic-gate 
2097*0Sstevel@tonic-gate 		if (strchr(argv[1], '(') != NULL) {
2098*0Sstevel@tonic-gate 			if (irp_unmarshall_ng(&ngname, &entries,
2099*0Sstevel@tonic-gate 					      &hosts, &users, &domains,
2100*0Sstevel@tonic-gate 					      argv[1]) != 0) {
2101*0Sstevel@tonic-gate 				printf("unmarshall failed\n");
2102*0Sstevel@tonic-gate 				exit(1);
2103*0Sstevel@tonic-gate 			}
2104*0Sstevel@tonic-gate 
2105*0Sstevel@tonic-gate #define STRVAL(x) (x == NULL ? "*" : x)
2106*0Sstevel@tonic-gate 
2107*0Sstevel@tonic-gate 			printf("%s {\n", ngname);
2108*0Sstevel@tonic-gate 			for (i = 0 ; i < entries ; i++)
2109*0Sstevel@tonic-gate 				printf("\t\"%s\" : \"%s\" : \"%s\"\n",
2110*0Sstevel@tonic-gate 				       STRVAL(hosts[i]),
2111*0Sstevel@tonic-gate 				       STRVAL(users[i]),
2112*0Sstevel@tonic-gate 				       STRVAL(domains[i]));
2113*0Sstevel@tonic-gate 			printf("}\n\n\n");
2114*0Sstevel@tonic-gate 
2115*0Sstevel@tonic-gate 
2116*0Sstevel@tonic-gate 			irp_marshall_ng_start(ngname, NULL, &size);
2117*0Sstevel@tonic-gate 			for (i = 0 ; i < entries ; i++)
2118*0Sstevel@tonic-gate 				irp_marshall_ng_next(hosts[i], users[i],
2119*0Sstevel@tonic-gate 						     domains[i], NULL, &size);
2120*0Sstevel@tonic-gate 			irp_marshall_ng_end(NULL, &size);
2121*0Sstevel@tonic-gate 
2122*0Sstevel@tonic-gate 			buff = malloc(size);
2123*0Sstevel@tonic-gate 
2124*0Sstevel@tonic-gate 			irp_marshall_ng_start(ngname, buff, &size);
2125*0Sstevel@tonic-gate 			for (i = 0 ; i < entries ; i++) {
2126*0Sstevel@tonic-gate 				if (irp_marshall_ng_next(hosts[i], users[i],
2127*0Sstevel@tonic-gate 							 domains[i], buff,
2128*0Sstevel@tonic-gate 							 &size) != 0)
2129*0Sstevel@tonic-gate 					printf("next marshalling failed.\n");
2130*0Sstevel@tonic-gate 			}
2131*0Sstevel@tonic-gate 			irp_marshall_ng_end(buff, &size);
2132*0Sstevel@tonic-gate 
2133*0Sstevel@tonic-gate 			if (strcmp_nws(argv[1], buff) != 0) {
2134*0Sstevel@tonic-gate 				printf("compare failed:\n\t%s\n\t%s\n",
2135*0Sstevel@tonic-gate 				       buffer, argv[1]);
2136*0Sstevel@tonic-gate 			} else {
2137*0Sstevel@tonic-gate 				printf("compare ok\n");
2138*0Sstevel@tonic-gate 			}
2139*0Sstevel@tonic-gate 		} else {
2140*0Sstevel@tonic-gate 			char *h, *u, *d, *buff;
2141*0Sstevel@tonic-gate 			size_t size;
2142*0Sstevel@tonic-gate 
2143*0Sstevel@tonic-gate 			/* run through two times. First to figure out how
2144*0Sstevel@tonic-gate 			   much of a buffer we need. Second to do the
2145*0Sstevel@tonic-gate 			   actual marshalling */
2146*0Sstevel@tonic-gate 
2147*0Sstevel@tonic-gate 			setnetgrent(argv[1]);
2148*0Sstevel@tonic-gate 			irp_marshall_ng_start(argv[1], NULL, &size);
2149*0Sstevel@tonic-gate 			while (getnetgrent(&h, &u, &d) == 1)
2150*0Sstevel@tonic-gate 				irp_marshall_ng_next(h, u, d, NULL, &size);
2151*0Sstevel@tonic-gate 			irp_marshall_ng_end(NULL, &size);
2152*0Sstevel@tonic-gate 			endnetgrent(argv[1]);
2153*0Sstevel@tonic-gate 
2154*0Sstevel@tonic-gate 			buff = malloc(size);
2155*0Sstevel@tonic-gate 
2156*0Sstevel@tonic-gate 			setnetgrent(argv[1]);
2157*0Sstevel@tonic-gate 			if (irp_marshall_ng_start(argv[1], buff, &size) != 0)
2158*0Sstevel@tonic-gate 				printf("Marshalling start failed\n");
2159*0Sstevel@tonic-gate 
2160*0Sstevel@tonic-gate 			while (getnetgrent(&h, &u, &d) == 1) {
2161*0Sstevel@tonic-gate 				if (irp_marshall_ng_next(h, u, d, buff, &size)
2162*0Sstevel@tonic-gate 				    != 0) {
2163*0Sstevel@tonic-gate 					printf("Marshalling failed\n");
2164*0Sstevel@tonic-gate 				}
2165*0Sstevel@tonic-gate 			}
2166*0Sstevel@tonic-gate 
2167*0Sstevel@tonic-gate 			irp_marshall_ng_end(buff, &size);
2168*0Sstevel@tonic-gate 			endnetgrent();
2169*0Sstevel@tonic-gate 
2170*0Sstevel@tonic-gate 			printf("success: %s\n", buff);
2171*0Sstevel@tonic-gate 		}
2172*0Sstevel@tonic-gate 		break;
2173*0Sstevel@tonic-gate 	}
2174*0Sstevel@tonic-gate 
2175*0Sstevel@tonic-gate 
2176*0Sstevel@tonic-gate 
2177*0Sstevel@tonic-gate 	case 'h': {
2178*0Sstevel@tonic-gate 		struct hostent he, *hp;
2179*0Sstevel@tonic-gate 		int i;
2180*0Sstevel@tonic-gate 
2181*0Sstevel@tonic-gate 
2182*0Sstevel@tonic-gate 		if (strchr(argv[1], '@') != NULL) {
2183*0Sstevel@tonic-gate 			if (irp_unmarshall_ho(&he, argv[1]) != 0) {
2184*0Sstevel@tonic-gate 				printf("unmarshall failed\n");
2185*0Sstevel@tonic-gate 				exit(1);
2186*0Sstevel@tonic-gate 			}
2187*0Sstevel@tonic-gate 
2188*0Sstevel@tonic-gate 			printf("Host: \"%s\"\nAliases:", he.h_name);
2189*0Sstevel@tonic-gate 			for (i = 0 ; he.h_aliases[i] != NULL ; i++)
2190*0Sstevel@tonic-gate 				printf("\n\t\t\"%s\"", he.h_aliases[i]);
2191*0Sstevel@tonic-gate 			printf("\nAddr Type: \"%s\"\n",
2192*0Sstevel@tonic-gate 			       ADDR_T_STR(he.h_addrtype));
2193*0Sstevel@tonic-gate 			printf("Length: %d\nAddresses:", he.h_length);
2194*0Sstevel@tonic-gate 			for (i = 0 ; he.h_addr_list[i] != 0 ; i++) {
2195*0Sstevel@tonic-gate 				inet_ntop(he.h_addrtype, he.h_addr_list[i],
2196*0Sstevel@tonic-gate 					  buffer, sizeof buffer);
2197*0Sstevel@tonic-gate 				printf("\n\t\"%s\"\n", buffer);
2198*0Sstevel@tonic-gate 			}
2199*0Sstevel@tonic-gate 			printf("\n\n");
2200*0Sstevel@tonic-gate 
2201*0Sstevel@tonic-gate 			irp_marshall_ho(&he, &b, &len);
2202*0Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
2203*0Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
2204*0Sstevel@tonic-gate 				       buffer, argv[1]);
2205*0Sstevel@tonic-gate 			} else {
2206*0Sstevel@tonic-gate 				printf("compare ok\n");
2207*0Sstevel@tonic-gate 			}
2208*0Sstevel@tonic-gate 		} else {
2209*0Sstevel@tonic-gate 			if ((hp = gethostbyname(argv[1])) == NULL) {
2210*0Sstevel@tonic-gate 				perror("gethostbyname");
2211*0Sstevel@tonic-gate 				printf("\"%s\"\n", argv[1]);
2212*0Sstevel@tonic-gate 				exit(1);
2213*0Sstevel@tonic-gate 			}
2214*0Sstevel@tonic-gate 
2215*0Sstevel@tonic-gate 			if (irp_marshall_ho(hp, &b, &len) != 0) {
2216*0Sstevel@tonic-gate 				printf("irp_marshall_ho failed\n");
2217*0Sstevel@tonic-gate 				exit(1);
2218*0Sstevel@tonic-gate 			}
2219*0Sstevel@tonic-gate 
2220*0Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
2221*0Sstevel@tonic-gate 		}
2222*0Sstevel@tonic-gate 		break;
2223*0Sstevel@tonic-gate 	}
2224*0Sstevel@tonic-gate 
2225*0Sstevel@tonic-gate 
2226*0Sstevel@tonic-gate 	case 's': {
2227*0Sstevel@tonic-gate 		struct servent *sv;
2228*0Sstevel@tonic-gate 		struct servent sv1;
2229*0Sstevel@tonic-gate 
2230*0Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
2231*0Sstevel@tonic-gate 			sv = &sv1;
2232*0Sstevel@tonic-gate 			memset(sv, 0xef, sizeof (struct servent));
2233*0Sstevel@tonic-gate 			if (irp_unmarshall_sv(sv, argv[1]) != 0) {
2234*0Sstevel@tonic-gate 				printf("unmarshall failed\n");
2235*0Sstevel@tonic-gate 
2236*0Sstevel@tonic-gate 			}
2237*0Sstevel@tonic-gate 
2238*0Sstevel@tonic-gate 			irp_marshall_sv(sv, &b, &len);
2239*0Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
2240*0Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
2241*0Sstevel@tonic-gate 				       buffer, argv[1]);
2242*0Sstevel@tonic-gate 			} else {
2243*0Sstevel@tonic-gate 				printf("compare ok\n");
2244*0Sstevel@tonic-gate 			}
2245*0Sstevel@tonic-gate 		} else {
2246*0Sstevel@tonic-gate 			if ((sv = getservbyname(argv[1], argv[2])) == NULL) {
2247*0Sstevel@tonic-gate 				perror("getservent");
2248*0Sstevel@tonic-gate 				exit(1);
2249*0Sstevel@tonic-gate 			}
2250*0Sstevel@tonic-gate 
2251*0Sstevel@tonic-gate 			if (irp_marshall_sv(sv, &b, &len) != 0) {
2252*0Sstevel@tonic-gate 				printf("irp_marshall_sv failed\n");
2253*0Sstevel@tonic-gate 				exit(1);
2254*0Sstevel@tonic-gate 			}
2255*0Sstevel@tonic-gate 
2256*0Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
2257*0Sstevel@tonic-gate 		}
2258*0Sstevel@tonic-gate 		break;
2259*0Sstevel@tonic-gate 	}
2260*0Sstevel@tonic-gate 
2261*0Sstevel@tonic-gate 	case 'g': {
2262*0Sstevel@tonic-gate 		struct group *gr;
2263*0Sstevel@tonic-gate 		struct group gr1;
2264*0Sstevel@tonic-gate 
2265*0Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
2266*0Sstevel@tonic-gate 			gr = &gr1;
2267*0Sstevel@tonic-gate 			memset(gr, 0xef, sizeof (struct group));
2268*0Sstevel@tonic-gate 			if (irp_unmarshall_gr(gr, argv[1]) != 0) {
2269*0Sstevel@tonic-gate 				printf("unmarshall failed\n");
2270*0Sstevel@tonic-gate 
2271*0Sstevel@tonic-gate 			}
2272*0Sstevel@tonic-gate 
2273*0Sstevel@tonic-gate 			irp_marshall_gr(gr, &b, &len);
2274*0Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
2275*0Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
2276*0Sstevel@tonic-gate 				       buffer, argv[1]);
2277*0Sstevel@tonic-gate 			} else {
2278*0Sstevel@tonic-gate 				printf("compare ok\n");
2279*0Sstevel@tonic-gate 			}
2280*0Sstevel@tonic-gate 		} else {
2281*0Sstevel@tonic-gate 			if ((gr = getgrnam(argv[1])) == NULL) {
2282*0Sstevel@tonic-gate 				perror("getgrnam");
2283*0Sstevel@tonic-gate 				exit(1);
2284*0Sstevel@tonic-gate 			}
2285*0Sstevel@tonic-gate 
2286*0Sstevel@tonic-gate 			if (irp_marshall_gr(gr, &b, &len) != 0) {
2287*0Sstevel@tonic-gate 				printf("irp_marshall_gr failed\n");
2288*0Sstevel@tonic-gate 				exit(1);
2289*0Sstevel@tonic-gate 			}
2290*0Sstevel@tonic-gate 
2291*0Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
2292*0Sstevel@tonic-gate 		}
2293*0Sstevel@tonic-gate 		break;
2294*0Sstevel@tonic-gate 	}
2295*0Sstevel@tonic-gate 
2296*0Sstevel@tonic-gate 
2297*0Sstevel@tonic-gate 	case 'p': {
2298*0Sstevel@tonic-gate 		struct passwd *pw;
2299*0Sstevel@tonic-gate 		struct passwd pw1;
2300*0Sstevel@tonic-gate 
2301*0Sstevel@tonic-gate 		if (strchr(argv[1], ':') != NULL) {
2302*0Sstevel@tonic-gate 			pw = &pw1;
2303*0Sstevel@tonic-gate 			memset(pw, 0xef, sizeof (*pw));
2304*0Sstevel@tonic-gate 			if (irp_unmarshall_pw(pw, argv[1]) != 0) {
2305*0Sstevel@tonic-gate 				printf("unmarshall failed\n");
2306*0Sstevel@tonic-gate 				exit(1);
2307*0Sstevel@tonic-gate 			}
2308*0Sstevel@tonic-gate 
2309*0Sstevel@tonic-gate 			printf("User: \"%s\"\nPasswd: \"%s\"\nUid: %ld\nGid: %ld\n",
2310*0Sstevel@tonic-gate 			       pw->pw_name, pw->pw_passwd, (long)pw->pw_uid,
2311*0Sstevel@tonic-gate 			       (long)pw->pw_gid);
2312*0Sstevel@tonic-gate 			printf("Class: \"%s\"\nChange: %ld\nGecos: \"%s\"\n",
2313*0Sstevel@tonic-gate 			       pw->pw_class, (long)pw->pw_change, pw->pw_gecos);
2314*0Sstevel@tonic-gate 			printf("Shell: \"%s\"\nDirectory: \"%s\"\n",
2315*0Sstevel@tonic-gate 			       pw->pw_shell, pw->pw_dir);
2316*0Sstevel@tonic-gate 
2317*0Sstevel@tonic-gate 			pw = getpwnam(pw->pw_name);
2318*0Sstevel@tonic-gate 			irp_marshall_pw(pw, &b, &len);
2319*0Sstevel@tonic-gate 			if (strcmp(argv[1], buffer) != 0) {
2320*0Sstevel@tonic-gate 				printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n",
2321*0Sstevel@tonic-gate 				       buffer, argv[1]);
2322*0Sstevel@tonic-gate 			} else {
2323*0Sstevel@tonic-gate 				printf("compare ok\n");
2324*0Sstevel@tonic-gate 			}
2325*0Sstevel@tonic-gate 		} else {
2326*0Sstevel@tonic-gate 			if ((pw = getpwnam(argv[1])) == NULL) {
2327*0Sstevel@tonic-gate 				perror("getpwnam");
2328*0Sstevel@tonic-gate 				exit(1);
2329*0Sstevel@tonic-gate 			}
2330*0Sstevel@tonic-gate 
2331*0Sstevel@tonic-gate 			if (irp_marshall_pw(pw, &b, &len) != 0) {
2332*0Sstevel@tonic-gate 				printf("irp_marshall_pw failed\n");
2333*0Sstevel@tonic-gate 				exit(1);
2334*0Sstevel@tonic-gate 			}
2335*0Sstevel@tonic-gate 
2336*0Sstevel@tonic-gate 			printf("success: \"%s\"\n", buffer);
2337*0Sstevel@tonic-gate 		}
2338*0Sstevel@tonic-gate 		break;
2339*0Sstevel@tonic-gate 	}
2340*0Sstevel@tonic-gate 
2341*0Sstevel@tonic-gate 	default:
2342*0Sstevel@tonic-gate 		printf("Wrong option: %c\n", option);
2343*0Sstevel@tonic-gate 		break;
2344*0Sstevel@tonic-gate 	}
2345*0Sstevel@tonic-gate 
2346*0Sstevel@tonic-gate #endif
2347*0Sstevel@tonic-gate 
2348*0Sstevel@tonic-gate 	return (0);
2349*0Sstevel@tonic-gate }
2350*0Sstevel@tonic-gate 
2351*0Sstevel@tonic-gate #endif
2352