1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * ldapaddent.c
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * Utility to add /etc files into LDAP.
33*0Sstevel@tonic-gate  * Can also be used to dump entries from a ldap container in /etc format.
34*0Sstevel@tonic-gate  */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <stdio.h>
37*0Sstevel@tonic-gate #include <stdlib.h>
38*0Sstevel@tonic-gate #include <libintl.h>
39*0Sstevel@tonic-gate #include <strings.h>
40*0Sstevel@tonic-gate #include <sys/param.h>
41*0Sstevel@tonic-gate #include <ctype.h>
42*0Sstevel@tonic-gate #include <sys/types.h>
43*0Sstevel@tonic-gate #include <sys/socket.h>
44*0Sstevel@tonic-gate #include <netinet/in.h>
45*0Sstevel@tonic-gate #include <arpa/inet.h>
46*0Sstevel@tonic-gate #include <locale.h>
47*0Sstevel@tonic-gate #include <syslog.h>
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #undef opaque
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #include <nss_dbdefs.h>
52*0Sstevel@tonic-gate #include <netdb.h>
53*0Sstevel@tonic-gate #include <rpc/rpcent.h>
54*0Sstevel@tonic-gate #include <grp.h>
55*0Sstevel@tonic-gate #include <pwd.h>
56*0Sstevel@tonic-gate #include <shadow.h>
57*0Sstevel@tonic-gate #include <sys/systeminfo.h>
58*0Sstevel@tonic-gate #include "ns_internal.h"
59*0Sstevel@tonic-gate #include "ldapaddent.h"
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate #define	OP_ADD	0
62*0Sstevel@tonic-gate #define	OP_DUMP	3
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static struct ttypelist_t {
65*0Sstevel@tonic-gate 	char *ttype;		/* type tag */
66*0Sstevel@tonic-gate 	int (*genent)(char *, int(*)());
67*0Sstevel@tonic-gate 				/* routine to turn line into ldap entries */
68*0Sstevel@tonic-gate 	void (*dump)(ns_ldap_result_t *);
69*0Sstevel@tonic-gate 				/* routine to print ldap containers */
70*0Sstevel@tonic-gate 	int (*filedbmline)();	/* routine to turn file line into dbm line */
71*0Sstevel@tonic-gate 	char *objclass;		/* Objectclass for the servicetype */
72*0Sstevel@tonic-gate } *tt;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate char	parse_err_msg [PARSE_ERR_MSG_LEN];
75*0Sstevel@tonic-gate int	continue_onerror = 0;  /* do not exit on error */
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate static int get_basedn(char *service, char **basedn);
78*0Sstevel@tonic-gate static int check_ipaddr(char *addr, char **newaddr);
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate extern	int	optind;
81*0Sstevel@tonic-gate extern	char	*optarg;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate extern	char	*__nis_quote_key(const char *, char *, int);
84*0Sstevel@tonic-gate /* from ns_internal.h */
85*0Sstevel@tonic-gate extern	int __s_api_prepend_automountmapname_to_dn(
86*0Sstevel@tonic-gate 	const char *, char **, ns_ldap_error_t **);
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate static char	*inputbasedn = NULL;
89*0Sstevel@tonic-gate static char	*databasetype = NULL;
90*0Sstevel@tonic-gate static int	exit_val = 0;
91*0Sstevel@tonic-gate static unsigned	nent_add = 0;
92*0Sstevel@tonic-gate static FILE	*etcf = 0;
93*0Sstevel@tonic-gate static ns_cred_t	authority;
94*0Sstevel@tonic-gate unsigned	flags = 0;
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate static void
97*0Sstevel@tonic-gate perr(ns_ldap_error_t *e)
98*0Sstevel@tonic-gate {
99*0Sstevel@tonic-gate 	if (e)
100*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%d: %s\n"),
101*0Sstevel@tonic-gate 				e->status, e->message);
102*0Sstevel@tonic-gate }
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate static int
106*0Sstevel@tonic-gate ascii_to_int(char *str)
107*0Sstevel@tonic-gate {
108*0Sstevel@tonic-gate 	int i;
109*0Sstevel@tonic-gate 	char *c = str;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	if (c == NULL || *c == '\0')
112*0Sstevel@tonic-gate 		return (-1);
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	while (c != '\0' && *c == ' ')
115*0Sstevel@tonic-gate 		c++;
116*0Sstevel@tonic-gate 	if (*c == '\0')
117*0Sstevel@tonic-gate 		return (-1);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	for (i = 0; i < strlen(c); i++)
120*0Sstevel@tonic-gate 		if (!isdigit(c[i]))
121*0Sstevel@tonic-gate 			return (-1);
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	return (atoi(c));
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate /*
127*0Sstevel@tonic-gate  * Internet network address interpretation routine.
128*0Sstevel@tonic-gate  * The library routines call this routine to interpret
129*0Sstevel@tonic-gate  * network numbers.
130*0Sstevel@tonic-gate  */
131*0Sstevel@tonic-gate static in_addr_t
132*0Sstevel@tonic-gate encode_network(const char *cp)
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate 	in_addr_t val;
135*0Sstevel@tonic-gate 	int base;
136*0Sstevel@tonic-gate 	ptrdiff_t n;
137*0Sstevel@tonic-gate 	char c;
138*0Sstevel@tonic-gate 	in_addr_t parts[4], *pp = parts;
139*0Sstevel@tonic-gate 	int i;
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate again:
142*0Sstevel@tonic-gate 	val = 0; base = 10;
143*0Sstevel@tonic-gate 	if (*cp == '0') {
144*0Sstevel@tonic-gate 		if (*++cp == 'x' || *cp == 'X')
145*0Sstevel@tonic-gate 			base = 16, cp++;
146*0Sstevel@tonic-gate 		else
147*0Sstevel@tonic-gate 			base = 8;
148*0Sstevel@tonic-gate 	}
149*0Sstevel@tonic-gate 	while ((c = *cp) != NULL) {
150*0Sstevel@tonic-gate 		if (isdigit(c)) {
151*0Sstevel@tonic-gate 			if ((c - '0') >= base)
152*0Sstevel@tonic-gate 			    break;
153*0Sstevel@tonic-gate 			val = (val * base) + (c - '0');
154*0Sstevel@tonic-gate 			cp++;
155*0Sstevel@tonic-gate 			continue;
156*0Sstevel@tonic-gate 		}
157*0Sstevel@tonic-gate 		if (base == 16 && isxdigit(c)) {
158*0Sstevel@tonic-gate 			val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
159*0Sstevel@tonic-gate 			cp++;
160*0Sstevel@tonic-gate 			continue;
161*0Sstevel@tonic-gate 		}
162*0Sstevel@tonic-gate 		break;
163*0Sstevel@tonic-gate 	}
164*0Sstevel@tonic-gate 	if (*cp == '.') {
165*0Sstevel@tonic-gate 		if (pp >= parts + 4)
166*0Sstevel@tonic-gate 			return ((in_addr_t)-1);
167*0Sstevel@tonic-gate 		*pp++ = val, cp++;
168*0Sstevel@tonic-gate 		goto again;
169*0Sstevel@tonic-gate 	}
170*0Sstevel@tonic-gate 	if (*cp && !isspace(*cp))
171*0Sstevel@tonic-gate 		return ((in_addr_t)-1);
172*0Sstevel@tonic-gate 	*pp++ = val;
173*0Sstevel@tonic-gate 	n = pp - parts;
174*0Sstevel@tonic-gate 	if (n > 4)
175*0Sstevel@tonic-gate 		return ((in_addr_t)-1);
176*0Sstevel@tonic-gate 	for (val = 0, i = 0; i < n; i++) {
177*0Sstevel@tonic-gate 		val <<= 8;
178*0Sstevel@tonic-gate 		val |= parts[i] & 0xff;
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 	for (/* no init */; i < 4; i++)
181*0Sstevel@tonic-gate 		val <<= 8;
182*0Sstevel@tonic-gate 	return (val);
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate static void
186*0Sstevel@tonic-gate replace_tab2space(char *str)
187*0Sstevel@tonic-gate {
188*0Sstevel@tonic-gate 	int i = 0;
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	while ((str) && (str[i])) {
191*0Sstevel@tonic-gate 		if (str[i] == '\t')
192*0Sstevel@tonic-gate 			str[i] = ' ';
193*0Sstevel@tonic-gate 		i++;
194*0Sstevel@tonic-gate 	}
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate static int
198*0Sstevel@tonic-gate blankline(char *line)
199*0Sstevel@tonic-gate {
200*0Sstevel@tonic-gate 	char *p;
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	for (p = line; *p; p++)
203*0Sstevel@tonic-gate 		if (*p != ' ' && *p != '\t')
204*0Sstevel@tonic-gate 			return (0);
205*0Sstevel@tonic-gate 	return (1);
206*0Sstevel@tonic-gate }
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate static void
209*0Sstevel@tonic-gate line_buf_expand(struct line_buf *line)
210*0Sstevel@tonic-gate {
211*0Sstevel@tonic-gate 	line->alloc += BUFSIZ;
212*0Sstevel@tonic-gate 	line->str = (char *)realloc(line->str, line->alloc);
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	if (line->str == NULL) {
215*0Sstevel@tonic-gate 		(void) fprintf(stderr,
216*0Sstevel@tonic-gate 		    gettext("line_buf_expand: out of memory\n"));
217*0Sstevel@tonic-gate 		exit(1);
218*0Sstevel@tonic-gate 	}
219*0Sstevel@tonic-gate }
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate static void
222*0Sstevel@tonic-gate line_buf_init(struct line_buf *line)
223*0Sstevel@tonic-gate {
224*0Sstevel@tonic-gate 	(void) memset((char *)line, 0, sizeof (*line));
225*0Sstevel@tonic-gate 	line_buf_expand(line);
226*0Sstevel@tonic-gate }
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate static int
229*0Sstevel@tonic-gate __s_add_attr(ns_ldap_entry_t *e, char *attrname, char *value)
230*0Sstevel@tonic-gate {
231*0Sstevel@tonic-gate 	ns_ldap_attr_t	*a;
232*0Sstevel@tonic-gate 	char		*v;
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	a = (ns_ldap_attr_t *)calloc(1, sizeof (ns_ldap_attr_t));
235*0Sstevel@tonic-gate 	if (a == NULL)
236*0Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
237*0Sstevel@tonic-gate 	a->attrname = strdup(attrname);
238*0Sstevel@tonic-gate 	if (a->attrname == NULL) {
239*0Sstevel@tonic-gate 		free(a);
240*0Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
241*0Sstevel@tonic-gate 	}
242*0Sstevel@tonic-gate 	a->attrvalue = (char **)calloc(1, sizeof (char **));
243*0Sstevel@tonic-gate 	if (a->attrvalue == NULL) {
244*0Sstevel@tonic-gate 		free(a->attrname);
245*0Sstevel@tonic-gate 		free(a);
246*0Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
247*0Sstevel@tonic-gate 	}
248*0Sstevel@tonic-gate 	a->value_count = 1;
249*0Sstevel@tonic-gate 	a->attrvalue[0] = NULL;
250*0Sstevel@tonic-gate 	v = strdup(value);
251*0Sstevel@tonic-gate 	if (v == NULL) {
252*0Sstevel@tonic-gate 		free(a->attrname);
253*0Sstevel@tonic-gate 		free(a->attrvalue);
254*0Sstevel@tonic-gate 		free(a);
255*0Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
256*0Sstevel@tonic-gate 	}
257*0Sstevel@tonic-gate 	a->attrvalue[0] = v;
258*0Sstevel@tonic-gate 	e->attr_pair[e->attr_count] = a;
259*0Sstevel@tonic-gate 	e->attr_count++;
260*0Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate static int
264*0Sstevel@tonic-gate __s_add_attrlist(ns_ldap_entry_t *e, char *attrname, char **argv)
265*0Sstevel@tonic-gate {
266*0Sstevel@tonic-gate 	ns_ldap_attr_t	*a;
267*0Sstevel@tonic-gate 	char		*v;
268*0Sstevel@tonic-gate 	char		**av;
269*0Sstevel@tonic-gate 	int		i, j;
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 	a = (ns_ldap_attr_t *)calloc(1, sizeof (ns_ldap_attr_t));
272*0Sstevel@tonic-gate 	if (a == NULL)
273*0Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
274*0Sstevel@tonic-gate 	a->attrname = strdup(attrname);
275*0Sstevel@tonic-gate 	if (a->attrname == NULL) {
276*0Sstevel@tonic-gate 		free(a);
277*0Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
278*0Sstevel@tonic-gate 	}
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	for (i = 0, av = argv; *av != NULL; av++, i++)
281*0Sstevel@tonic-gate 		;
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	a->attrvalue = (char **)calloc(i, sizeof (char **));
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	if (a->attrvalue == NULL) {
286*0Sstevel@tonic-gate 		free(a->attrname);
287*0Sstevel@tonic-gate 		free(a);
288*0Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
289*0Sstevel@tonic-gate 	}
290*0Sstevel@tonic-gate 	a->value_count = i;
291*0Sstevel@tonic-gate 	for (j = 0; j < i; j++) {
292*0Sstevel@tonic-gate 		v = strdup(argv[j]);
293*0Sstevel@tonic-gate 		if (v == NULL) {
294*0Sstevel@tonic-gate 			free(a->attrname);
295*0Sstevel@tonic-gate 			free(a->attrvalue);
296*0Sstevel@tonic-gate 			free(a);
297*0Sstevel@tonic-gate 			return (NS_LDAP_MEMORY);
298*0Sstevel@tonic-gate 		}
299*0Sstevel@tonic-gate 		a->attrvalue[j] = v;
300*0Sstevel@tonic-gate 	}
301*0Sstevel@tonic-gate 	e->attr_pair[e->attr_count] = a;
302*0Sstevel@tonic-gate 	e->attr_count++;
303*0Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
304*0Sstevel@tonic-gate }
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate static ns_ldap_entry_t *
307*0Sstevel@tonic-gate __s_mk_entry(char **objclass, int max_attr)
308*0Sstevel@tonic-gate {
309*0Sstevel@tonic-gate 	ns_ldap_entry_t *e;
310*0Sstevel@tonic-gate 	e = (ns_ldap_entry_t *)calloc(1, sizeof (ns_ldap_entry_t));
311*0Sstevel@tonic-gate 	if (e == NULL)
312*0Sstevel@tonic-gate 		return (NULL);
313*0Sstevel@tonic-gate 	e->attr_pair = (ns_ldap_attr_t **)calloc(max_attr+1,
314*0Sstevel@tonic-gate 						sizeof (ns_ldap_attr_t *));
315*0Sstevel@tonic-gate 	if (e->attr_pair == NULL) {
316*0Sstevel@tonic-gate 		free(e);
317*0Sstevel@tonic-gate 		return (NULL);
318*0Sstevel@tonic-gate 	}
319*0Sstevel@tonic-gate 	e->attr_count = 0;
320*0Sstevel@tonic-gate 	if (__s_add_attrlist(e, "objectClass", objclass) != NS_LDAP_SUCCESS) {
321*0Sstevel@tonic-gate 		free(e->attr_pair);
322*0Sstevel@tonic-gate 		free(e);
323*0Sstevel@tonic-gate 		return (NULL);
324*0Sstevel@tonic-gate 	}
325*0Sstevel@tonic-gate 	return (e);
326*0Sstevel@tonic-gate }
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate static void
329*0Sstevel@tonic-gate ldap_freeEntry(ns_ldap_entry_t *ep)
330*0Sstevel@tonic-gate {
331*0Sstevel@tonic-gate 	int		j, k = 0;
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	if (ep == NULL)
334*0Sstevel@tonic-gate 		return;
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	if (ep->attr_pair == NULL) {
337*0Sstevel@tonic-gate 		free(ep);
338*0Sstevel@tonic-gate 		return;
339*0Sstevel@tonic-gate 	}
340*0Sstevel@tonic-gate 	for (j = 0; j < ep->attr_count; j++) {
341*0Sstevel@tonic-gate 		if (ep->attr_pair[j] == NULL)
342*0Sstevel@tonic-gate 			continue;
343*0Sstevel@tonic-gate 		if (ep->attr_pair[j]->attrname)
344*0Sstevel@tonic-gate 			free(ep->attr_pair[j]->attrname);
345*0Sstevel@tonic-gate 		if (ep->attr_pair[j]->attrvalue) {
346*0Sstevel@tonic-gate 			for (k = 0; (k < ep->attr_pair[j]->value_count) &&
347*0Sstevel@tonic-gate 				    (ep->attr_pair[j]->attrvalue[k]); k++) {
348*0Sstevel@tonic-gate 				free(ep->attr_pair[j]->attrvalue[k]);
349*0Sstevel@tonic-gate 			}
350*0Sstevel@tonic-gate 			free(ep->attr_pair[j]->attrvalue);
351*0Sstevel@tonic-gate 		}
352*0Sstevel@tonic-gate 		free(ep->attr_pair[j]);
353*0Sstevel@tonic-gate 	}
354*0Sstevel@tonic-gate 	free(ep->attr_pair);
355*0Sstevel@tonic-gate 	free(ep);
356*0Sstevel@tonic-gate }
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate static int
359*0Sstevel@tonic-gate addentry(void *entry, int mod)
360*0Sstevel@tonic-gate {
361*0Sstevel@tonic-gate 	int		 result = 0;
362*0Sstevel@tonic-gate 	ns_ldap_error_t	 *eres = NULL;
363*0Sstevel@tonic-gate 	int		rc = 1;
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate 	/*  adds entry into the LDAP tree */
367*0Sstevel@tonic-gate 	if (mod)
368*0Sstevel@tonic-gate 		result = __ns_ldap_addTypedEntry(databasetype, inputbasedn,
369*0Sstevel@tonic-gate 		    entry, 0, &authority, NS_LDAP_FOLLOWREF, &eres);
370*0Sstevel@tonic-gate 	else
371*0Sstevel@tonic-gate 		result = __ns_ldap_addTypedEntry(databasetype, inputbasedn,
372*0Sstevel@tonic-gate 		    entry, 1, &authority, NS_LDAP_FOLLOWREF, &eres);
373*0Sstevel@tonic-gate 	/*
374*0Sstevel@tonic-gate 	 *  Return	0 on success
375*0Sstevel@tonic-gate 	 *		LDAP_ALREADY_EXISTS if entry exists already
376*0Sstevel@tonic-gate 	 *		1 for all other non-fatal errors.
377*0Sstevel@tonic-gate 	 *  Exit on fatal errors.
378*0Sstevel@tonic-gate 	 */
379*0Sstevel@tonic-gate 	switch (result) {
380*0Sstevel@tonic-gate 	case NS_LDAP_SUCCESS:
381*0Sstevel@tonic-gate 		nent_add++;
382*0Sstevel@tonic-gate 		rc = 0;
383*0Sstevel@tonic-gate 		break;
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate 	case NS_LDAP_OP_FAILED:
386*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("operation failed.\n"));
387*0Sstevel@tonic-gate 		rc = 1;
388*0Sstevel@tonic-gate 		break;
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 	case NS_LDAP_INVALID_PARAM:
391*0Sstevel@tonic-gate 		(void) fprintf(stderr,
392*0Sstevel@tonic-gate 		    gettext("invalid parameter(s) passed.\n"));
393*0Sstevel@tonic-gate 		rc = 1;
394*0Sstevel@tonic-gate 		break;
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	case NS_LDAP_NOTFOUND:
397*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("entry not found.\n"));
398*0Sstevel@tonic-gate 		rc = 1;
399*0Sstevel@tonic-gate 		break;
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	case NS_LDAP_MEMORY:
402*0Sstevel@tonic-gate 		(void) fprintf(stderr,
403*0Sstevel@tonic-gate 		    gettext("internal memory allocation error.\n"));
404*0Sstevel@tonic-gate 		exit(1);
405*0Sstevel@tonic-gate 		break;
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 	case NS_LDAP_CONFIG:
408*0Sstevel@tonic-gate 		(void) fprintf(stderr,
409*0Sstevel@tonic-gate 		    gettext("LDAP Configuration problem.\n"));
410*0Sstevel@tonic-gate 		perr(eres);
411*0Sstevel@tonic-gate 		exit(1);
412*0Sstevel@tonic-gate 		break;
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	case NS_LDAP_PARTIAL:
415*0Sstevel@tonic-gate 		(void) fprintf(stderr,
416*0Sstevel@tonic-gate 		    gettext("partial result returned\n"));
417*0Sstevel@tonic-gate 		perr(eres);
418*0Sstevel@tonic-gate 		rc = 1;
419*0Sstevel@tonic-gate 		break;
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 	case NS_LDAP_INTERNAL:
422*0Sstevel@tonic-gate 		if (eres->status == LDAP_ALREADY_EXISTS)
423*0Sstevel@tonic-gate 			rc = eres->status;
424*0Sstevel@tonic-gate 		else {
425*0Sstevel@tonic-gate 			rc = 1;
426*0Sstevel@tonic-gate 			perr(eres);
427*0Sstevel@tonic-gate 		}
428*0Sstevel@tonic-gate 		break;
429*0Sstevel@tonic-gate 	}
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 	if (eres)
432*0Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&eres);
433*0Sstevel@tonic-gate 	return (rc);
434*0Sstevel@tonic-gate }
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate /*
438*0Sstevel@tonic-gate  * usage(char *msg)
439*0Sstevel@tonic-gate  * Display usage message to STDERR.
440*0Sstevel@tonic-gate  */
441*0Sstevel@tonic-gate static void
442*0Sstevel@tonic-gate usage(char *msg) {
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 	if (msg)
445*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s\n"), msg);
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
448*0Sstevel@tonic-gate 	    "usage: ldapaddent [ -cpv ] [ -a authenticationMethod ]\n"
449*0Sstevel@tonic-gate 	    "[ -b baseDN ] -D bindDN -w bind_password [ -f file ] database\n\n"
450*0Sstevel@tonic-gate 	    "usage: ldapaddent -d [ -cpv ] [ -a authenticationMethod ]\n"
451*0Sstevel@tonic-gate 	    "[ -b baseDN ] [ -D bindDN ] [ -w bind_password ] database\n"));
452*0Sstevel@tonic-gate 	exit(1);
453*0Sstevel@tonic-gate }
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate /*
456*0Sstevel@tonic-gate  * Determine if the given string is an IP address (IPv4 or IPv6).
457*0Sstevel@tonic-gate  * If so, it's converted to the preferred form (rfc2373) and
458*0Sstevel@tonic-gate  * *newaddr will point to the new address.
459*0Sstevel@tonic-gate  *
460*0Sstevel@tonic-gate  * Returns	-2		: inet_ntop error
461*0Sstevel@tonic-gate  *		-1		: not an IP address
462*0Sstevel@tonic-gate  *		0		: unsupported IP address (future use)
463*0Sstevel@tonic-gate  *		AF_INET		: IPv4
464*0Sstevel@tonic-gate  *		AF_INET6	: IPv6
465*0Sstevel@tonic-gate  */
466*0Sstevel@tonic-gate static int
467*0Sstevel@tonic-gate check_ipaddr(char *addr, char **newaddr) {
468*0Sstevel@tonic-gate 	ipaddr_t	addr_ipv4 = 0;
469*0Sstevel@tonic-gate 	in6_addr_t	addr_ipv6;
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 	/* IPv6 */
472*0Sstevel@tonic-gate 	if (inet_pton(AF_INET6, addr, &addr_ipv6) == 1) {
473*0Sstevel@tonic-gate 		if (newaddr == NULL)
474*0Sstevel@tonic-gate 			return (AF_INET6);
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 		/* Convert IPv4-mapped IPv6 address to IPv4 */
477*0Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&addr_ipv6) ||
478*0Sstevel@tonic-gate 					IN6_IS_ADDR_V4COMPAT(&addr_ipv6)) {
479*0Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&addr_ipv6, addr_ipv4);
480*0Sstevel@tonic-gate 			if ((*newaddr = calloc(1, INET_ADDRSTRLEN)) == NULL) {
481*0Sstevel@tonic-gate 				(void) fprintf(stderr,
482*0Sstevel@tonic-gate 				    gettext("out of memory\n"));
483*0Sstevel@tonic-gate 				exit(1);
484*0Sstevel@tonic-gate 			}
485*0Sstevel@tonic-gate 			if (inet_ntop(AF_INET, &addr_ipv4, *newaddr,
486*0Sstevel@tonic-gate 			    INET_ADDRSTRLEN))
487*0Sstevel@tonic-gate 				return (AF_INET6);
488*0Sstevel@tonic-gate 			free(*newaddr);
489*0Sstevel@tonic-gate 			return (-2);
490*0Sstevel@tonic-gate 		}
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate 		/* Processing general IPv6 addresses */
493*0Sstevel@tonic-gate 		if ((*newaddr = calloc(1, INET6_ADDRSTRLEN)) == NULL) {
494*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
495*0Sstevel@tonic-gate 			exit(1);
496*0Sstevel@tonic-gate 		}
497*0Sstevel@tonic-gate 		if (inet_ntop(AF_INET6, &addr_ipv6, *newaddr, INET6_ADDRSTRLEN))
498*0Sstevel@tonic-gate 			return (AF_INET6);
499*0Sstevel@tonic-gate 		free(*newaddr);
500*0Sstevel@tonic-gate 		return (-2);
501*0Sstevel@tonic-gate 	}
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate 	/* Processing IPv4 addresses of the type d.d.d.d. */
504*0Sstevel@tonic-gate 	if (inet_pton(AF_INET, addr, &addr_ipv4) == 1) {
505*0Sstevel@tonic-gate 		if (newaddr == NULL)
506*0Sstevel@tonic-gate 			return (AF_INET);
507*0Sstevel@tonic-gate 		if ((*newaddr = calloc(1, INET_ADDRSTRLEN)) == NULL) {
508*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
509*0Sstevel@tonic-gate 			exit(1);
510*0Sstevel@tonic-gate 		}
511*0Sstevel@tonic-gate 		if (inet_ntop(AF_INET, &addr_ipv4, *newaddr, INET_ADDRSTRLEN))
512*0Sstevel@tonic-gate 			return (AF_INET);
513*0Sstevel@tonic-gate 		free(*newaddr);
514*0Sstevel@tonic-gate 		return (-2);
515*0Sstevel@tonic-gate 	}
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate 	/* Processing IPv4 addresses d.d.d , d.d and d */
518*0Sstevel@tonic-gate 	if (inet_addr(addr) != (in_addr_t)-1) {
519*0Sstevel@tonic-gate 		if (newaddr == NULL)
520*0Sstevel@tonic-gate 			return (AF_INET);
521*0Sstevel@tonic-gate 		if ((*newaddr = strdup(addr)) == NULL) {
522*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
523*0Sstevel@tonic-gate 			exit(1);
524*0Sstevel@tonic-gate 		}
525*0Sstevel@tonic-gate 		return (AF_INET);
526*0Sstevel@tonic-gate 	}
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 	return (-1);
529*0Sstevel@tonic-gate }
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate static int
532*0Sstevel@tonic-gate genent_hosts(char *line, int (*cback)())
533*0Sstevel@tonic-gate {
534*0Sstevel@tonic-gate 	char buf[BUFSIZ+1];
535*0Sstevel@tonic-gate 	char *t;
536*0Sstevel@tonic-gate 	entry_col ecol[4];
537*0Sstevel@tonic-gate 	char *cname, *pref_addr;
538*0Sstevel@tonic-gate 	int ctr = 0, retval = 1;
539*0Sstevel@tonic-gate 	int rc = GENENT_OK, af;
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 	struct hostent  data;
542*0Sstevel@tonic-gate 	char *alias;
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	/*
545*0Sstevel@tonic-gate 	 * don't clobber our argument
546*0Sstevel@tonic-gate 	 */
547*0Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
548*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
549*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
550*0Sstevel@tonic-gate 	}
551*0Sstevel@tonic-gate 	(void) strcpy(buf, line);
552*0Sstevel@tonic-gate 
553*0Sstevel@tonic-gate 	/*
554*0Sstevel@tonic-gate 	 * clear column data
555*0Sstevel@tonic-gate 	 */
556*0Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 	/*
559*0Sstevel@tonic-gate 	 * comment (col 3)
560*0Sstevel@tonic-gate 	 */
561*0Sstevel@tonic-gate 	t = strchr(buf, '#');
562*0Sstevel@tonic-gate 	if (t) {
563*0Sstevel@tonic-gate 		*t++ = 0;
564*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
565*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
566*0Sstevel@tonic-gate 	} else {
567*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = "";
568*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
569*0Sstevel@tonic-gate 	}
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate 	/*
573*0Sstevel@tonic-gate 	 * addr(col 2)
574*0Sstevel@tonic-gate 	 */
575*0Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
576*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no host");
577*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
578*0Sstevel@tonic-gate 	}
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate 	af = check_ipaddr(t, &pref_addr);
581*0Sstevel@tonic-gate 	if (af == -2) {
582*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "Internal error");
583*0Sstevel@tonic-gate 	} else if (af == -1) {
584*0Sstevel@tonic-gate 		(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
585*0Sstevel@tonic-gate 		    "Invalid IP address: %s", t);
586*0Sstevel@tonic-gate 	} else if (flags & F_VERBOSE) {
587*0Sstevel@tonic-gate 		if ((strncasecmp(t, pref_addr, strlen(t))) != 0) {
588*0Sstevel@tonic-gate 			(void) fprintf(stdout,
589*0Sstevel@tonic-gate 			    gettext("IP address %s converted to %s\n"),
590*0Sstevel@tonic-gate 			    t, pref_addr);
591*0Sstevel@tonic-gate 		}
592*0Sstevel@tonic-gate 	}
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	if (af < 0) {
595*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s\n"), parse_err_msg);
596*0Sstevel@tonic-gate 		if (continue_onerror == 0)
597*0Sstevel@tonic-gate 			return (GENENT_CBERR);
598*0Sstevel@tonic-gate 		else
599*0Sstevel@tonic-gate 			return (rc);
600*0Sstevel@tonic-gate 	}
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = pref_addr;
603*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(pref_addr)+1;
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate 	/*
606*0Sstevel@tonic-gate 	 * cname (col 0)
607*0Sstevel@tonic-gate 	 */
608*0Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
609*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no cname");
610*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
611*0Sstevel@tonic-gate 	}
612*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
613*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
614*0Sstevel@tonic-gate 	cname = t;
615*0Sstevel@tonic-gate 
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 	/* build entry */
618*0Sstevel@tonic-gate 	if ((data.h_addr_list = (char **)calloc(2, sizeof (char **))) == NULL) {
619*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
620*0Sstevel@tonic-gate 		exit(1);
621*0Sstevel@tonic-gate 	}
622*0Sstevel@tonic-gate 	data.h_addr_list[0] = strdup(ecol[2].ec_value.ec_value_val);
623*0Sstevel@tonic-gate 	data.h_addr_list[1] = NULL;
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate 	free(pref_addr);
626*0Sstevel@tonic-gate 	data.h_name = strdup(ecol[0].ec_value.ec_value_val);
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate 	/*
629*0Sstevel@tonic-gate 	 * name (col 1)
630*0Sstevel@tonic-gate 	 */
631*0Sstevel@tonic-gate 
632*0Sstevel@tonic-gate 	data.h_aliases = NULL;
633*0Sstevel@tonic-gate 
634*0Sstevel@tonic-gate 	do {
635*0Sstevel@tonic-gate 		/*
636*0Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
637*0Sstevel@tonic-gate 		 */
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 		/* This call to AddEntry may move out of the loop */
640*0Sstevel@tonic-gate 		/* This is because we have to call the function just once */
641*0Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
642*0Sstevel@tonic-gate 			continue;
643*0Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
644*0Sstevel@tonic-gate 			continue;
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
647*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate 		ctr++;
650*0Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
651*0Sstevel@tonic-gate 		if ((data.h_aliases = (char **)realloc(data.h_aliases,
652*0Sstevel@tonic-gate 			ctr * sizeof (char **))) == NULL) {
653*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
654*0Sstevel@tonic-gate 			exit(1);
655*0Sstevel@tonic-gate 		}
656*0Sstevel@tonic-gate 		data.h_aliases[ctr-1] = alias;
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate 		/*
659*0Sstevel@tonic-gate 		 * only put comment in canonical entry
660*0Sstevel@tonic-gate 		 */
661*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
662*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
667*0Sstevel@tonic-gate 	if ((data.h_aliases = (char **)realloc(data.h_aliases,
668*0Sstevel@tonic-gate 		(ctr + 1) * sizeof (char **))) == NULL) {
669*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
670*0Sstevel@tonic-gate 		exit(1);
671*0Sstevel@tonic-gate 	}
672*0Sstevel@tonic-gate 	data.h_aliases[ctr] = NULL;
673*0Sstevel@tonic-gate 
674*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
675*0Sstevel@tonic-gate 		(void) fprintf(stdout,
676*0Sstevel@tonic-gate 		    gettext("Adding entry : cn=%s+ipHostNumber=%s\n"),
677*0Sstevel@tonic-gate 		    data.h_name, data.h_addr_list[0]);
678*0Sstevel@tonic-gate 
679*0Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
682*0Sstevel@tonic-gate 		if (continue_onerror)
683*0Sstevel@tonic-gate 			(void) fprintf(stderr,
684*0Sstevel@tonic-gate 				gettext("Entry: cn=%s+ipHostNumber=%s "
685*0Sstevel@tonic-gate 					"already Exists -skipping it\n"),
686*0Sstevel@tonic-gate 					data.h_name, data.h_addr_list[0]);
687*0Sstevel@tonic-gate 		else {
688*0Sstevel@tonic-gate 			rc = GENENT_CBERR;
689*0Sstevel@tonic-gate 			(void) fprintf(stderr,
690*0Sstevel@tonic-gate 				gettext("Entry: cn=%s+ipHostNumber=%s"
691*0Sstevel@tonic-gate 					" already Exists\n"),
692*0Sstevel@tonic-gate 					data.h_name, data.h_addr_list[0]);
693*0Sstevel@tonic-gate 		}
694*0Sstevel@tonic-gate 	} else if (retval)
695*0Sstevel@tonic-gate 		rc = GENENT_CBERR;
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate 	free(data.h_name);
698*0Sstevel@tonic-gate 	free(data.h_aliases);
699*0Sstevel@tonic-gate 	free(data.h_addr_list);
700*0Sstevel@tonic-gate 
701*0Sstevel@tonic-gate 	return (rc);
702*0Sstevel@tonic-gate }
703*0Sstevel@tonic-gate 
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate static void
707*0Sstevel@tonic-gate dump_hosts(ns_ldap_result_t *res)
708*0Sstevel@tonic-gate {
709*0Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *iphostnumber = NULL;
710*0Sstevel@tonic-gate 	int		 i, j;
711*0Sstevel@tonic-gate 	char		*name; /* host name */
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
714*0Sstevel@tonic-gate 		return;
715*0Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
716*0Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
717*0Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
718*0Sstevel@tonic-gate 			cn = attrptr;
719*0Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "iphostnumber") == 0)
720*0Sstevel@tonic-gate 			iphostnumber = attrptr;
721*0Sstevel@tonic-gate 	}
722*0Sstevel@tonic-gate 	/* sanity check */
723*0Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
724*0Sstevel@tonic-gate 	    iphostnumber == NULL || iphostnumber->attrvalue == NULL ||
725*0Sstevel@tonic-gate 	    iphostnumber->attrvalue[0] == NULL)
726*0Sstevel@tonic-gate 		return;
727*0Sstevel@tonic-gate 
728*0Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
729*0Sstevel@tonic-gate 		return;
730*0Sstevel@tonic-gate 
731*0Sstevel@tonic-gate 	/* ip host/ipnode number */
732*0Sstevel@tonic-gate 	if (strlen(iphostnumber->attrvalue[0]) <= INET_ADDRSTRLEN)
733*0Sstevel@tonic-gate 		/* IPV4 or IPV6 but <= NET_ADDRSTRLEN */
734*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%-18s", iphostnumber->attrvalue[0]);
735*0Sstevel@tonic-gate 	else
736*0Sstevel@tonic-gate 		/* IPV6 */
737*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%-48s", iphostnumber->attrvalue[0]);
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate 	/* host/ipnode name */
740*0Sstevel@tonic-gate 	(void) fprintf(stdout, "%s ", name);
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 	/* aliases */
743*0Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
744*0Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
745*0Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0)
746*0Sstevel@tonic-gate 				/* skip host name */
747*0Sstevel@tonic-gate 				continue;
748*0Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
749*0Sstevel@tonic-gate 		}
750*0Sstevel@tonic-gate 	}
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 	/* end of line */
753*0Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
754*0Sstevel@tonic-gate }
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate /*
757*0Sstevel@tonic-gate  * /etc/rpc
758*0Sstevel@tonic-gate  */
759*0Sstevel@tonic-gate 
760*0Sstevel@tonic-gate static int
761*0Sstevel@tonic-gate genent_rpc(char *line, int (*cback)())
762*0Sstevel@tonic-gate {
763*0Sstevel@tonic-gate 	char buf[BUFSIZ+1];
764*0Sstevel@tonic-gate 	char *t;
765*0Sstevel@tonic-gate 	entry_col ecol[4];
766*0Sstevel@tonic-gate 	char *cname;
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate 	struct rpcent	data;
769*0Sstevel@tonic-gate 	char *alias;
770*0Sstevel@tonic-gate 	int ctr = 0;
771*0Sstevel@tonic-gate 	int retval = 1;
772*0Sstevel@tonic-gate 	int rc = GENENT_OK;
773*0Sstevel@tonic-gate 
774*0Sstevel@tonic-gate 	/*
775*0Sstevel@tonic-gate 	 * don't clobber our argument
776*0Sstevel@tonic-gate 	 */
777*0Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
778*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
779*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
780*0Sstevel@tonic-gate 	}
781*0Sstevel@tonic-gate 	(void) strcpy(buf, line);
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate 	/*
784*0Sstevel@tonic-gate 	 * clear column data
785*0Sstevel@tonic-gate 	 */
786*0Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	/*
789*0Sstevel@tonic-gate 	 * comment (col 3)
790*0Sstevel@tonic-gate 	 */
791*0Sstevel@tonic-gate 	t = strchr(buf, '#');
792*0Sstevel@tonic-gate 	if (t) {
793*0Sstevel@tonic-gate 		*t++ = 0;
794*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
795*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
796*0Sstevel@tonic-gate 	} else {
797*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
798*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
799*0Sstevel@tonic-gate 	}
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 	/*
802*0Sstevel@tonic-gate 	 * cname(col 0)
803*0Sstevel@tonic-gate 	 */
804*0Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
805*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no number");
806*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
807*0Sstevel@tonic-gate 	}
808*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
809*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
810*0Sstevel@tonic-gate 	cname = t;
811*0Sstevel@tonic-gate 
812*0Sstevel@tonic-gate 	/*
813*0Sstevel@tonic-gate 	 * number (col 2)
814*0Sstevel@tonic-gate 	 */
815*0Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
816*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no number");
817*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
818*0Sstevel@tonic-gate 	}
819*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
820*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
821*0Sstevel@tonic-gate 
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate 	/*
824*0Sstevel@tonic-gate 	 * build entry
825*0Sstevel@tonic-gate 	 */
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate 	data.r_name = strdup(ecol[0].ec_value.ec_value_val);
828*0Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
829*0Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val[0] != '\0') {
830*0Sstevel@tonic-gate 
831*0Sstevel@tonic-gate 		data.r_number = ascii_to_int(ecol[2].ec_value.ec_value_val);
832*0Sstevel@tonic-gate 		if (data.r_number == -1) {
833*0Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
834*0Sstevel@tonic-gate 			    "invalid program number: %s",
835*0Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
836*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
837*0Sstevel@tonic-gate 		}
838*0Sstevel@tonic-gate 	} else
839*0Sstevel@tonic-gate 		data.r_number = -1;
840*0Sstevel@tonic-gate 
841*0Sstevel@tonic-gate 	/*
842*0Sstevel@tonic-gate 	 * name (col 1)
843*0Sstevel@tonic-gate 	 */
844*0Sstevel@tonic-gate 	t = cname;
845*0Sstevel@tonic-gate 	data.r_aliases = NULL;
846*0Sstevel@tonic-gate 	do {
847*0Sstevel@tonic-gate 
848*0Sstevel@tonic-gate 		/*
849*0Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
850*0Sstevel@tonic-gate 		 */
851*0Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
852*0Sstevel@tonic-gate 			continue;
853*0Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
854*0Sstevel@tonic-gate 			continue;
855*0Sstevel@tonic-gate 
856*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
857*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
858*0Sstevel@tonic-gate 
859*0Sstevel@tonic-gate 		ctr++;
860*0Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
861*0Sstevel@tonic-gate 		if ((data.r_aliases = (char **)realloc(data.r_aliases,
862*0Sstevel@tonic-gate 			ctr * sizeof (char **))) == NULL) {
863*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
864*0Sstevel@tonic-gate 			exit(1);
865*0Sstevel@tonic-gate 		}
866*0Sstevel@tonic-gate 		data.r_aliases[ctr-1] = alias;
867*0Sstevel@tonic-gate 
868*0Sstevel@tonic-gate 
869*0Sstevel@tonic-gate 		/*
870*0Sstevel@tonic-gate 		 * only put comment in canonical entry
871*0Sstevel@tonic-gate 		 */
872*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
873*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
874*0Sstevel@tonic-gate 
875*0Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
876*0Sstevel@tonic-gate 
877*0Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
878*0Sstevel@tonic-gate 	if ((data.r_aliases = (char **)realloc(data.r_aliases,
879*0Sstevel@tonic-gate 		(ctr + 1) * sizeof (char **))) == NULL) {
880*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
881*0Sstevel@tonic-gate 		exit(1);
882*0Sstevel@tonic-gate 	}
883*0Sstevel@tonic-gate 	data.r_aliases[ctr] = NULL;
884*0Sstevel@tonic-gate 
885*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
886*0Sstevel@tonic-gate 		(void) fprintf(stdout,
887*0Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.r_name);
888*0Sstevel@tonic-gate 
889*0Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
890*0Sstevel@tonic-gate 
891*0Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
892*0Sstevel@tonic-gate 		if (continue_onerror)
893*0Sstevel@tonic-gate 			(void) fprintf(stderr,
894*0Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
895*0Sstevel@tonic-gate 			data.r_name);
896*0Sstevel@tonic-gate 		else {
897*0Sstevel@tonic-gate 			rc = GENENT_CBERR;
898*0Sstevel@tonic-gate 			(void) fprintf(stderr,
899*0Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
900*0Sstevel@tonic-gate 				data.r_name);
901*0Sstevel@tonic-gate 		}
902*0Sstevel@tonic-gate 	} else if (retval)
903*0Sstevel@tonic-gate 		rc = GENENT_CBERR;
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate 	free(data.r_name);
906*0Sstevel@tonic-gate 	free(data.r_aliases);
907*0Sstevel@tonic-gate 
908*0Sstevel@tonic-gate 	return (rc);
909*0Sstevel@tonic-gate }
910*0Sstevel@tonic-gate 
911*0Sstevel@tonic-gate 
912*0Sstevel@tonic-gate 
913*0Sstevel@tonic-gate static void
914*0Sstevel@tonic-gate dump_rpc(ns_ldap_result_t *res)
915*0Sstevel@tonic-gate {
916*0Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *rpcnumber = NULL;
917*0Sstevel@tonic-gate 	int		 i, j;
918*0Sstevel@tonic-gate 	char		*name; /* rpc name */
919*0Sstevel@tonic-gate 
920*0Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
921*0Sstevel@tonic-gate 		return;
922*0Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
923*0Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
924*0Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
925*0Sstevel@tonic-gate 			cn = attrptr;
926*0Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "oncRpcNumber") == 0)
927*0Sstevel@tonic-gate 			rpcnumber = attrptr;
928*0Sstevel@tonic-gate 	}
929*0Sstevel@tonic-gate 	/* sanity check */
930*0Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
931*0Sstevel@tonic-gate 	    rpcnumber == NULL || rpcnumber->attrvalue == NULL ||
932*0Sstevel@tonic-gate 	    rpcnumber->attrvalue[0] == NULL)
933*0Sstevel@tonic-gate 		return;
934*0Sstevel@tonic-gate 
935*0Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
936*0Sstevel@tonic-gate 		return;
937*0Sstevel@tonic-gate 
938*0Sstevel@tonic-gate 	/* rpc name */
939*0Sstevel@tonic-gate 	if (strlen(name) < 8)
940*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t\t", name);
941*0Sstevel@tonic-gate 	else
942*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t", name);
943*0Sstevel@tonic-gate 
944*0Sstevel@tonic-gate 	/* rpc number */
945*0Sstevel@tonic-gate 	(void) fprintf(stdout, "%-8s", rpcnumber->attrvalue[0]);
946*0Sstevel@tonic-gate 
947*0Sstevel@tonic-gate 
948*0Sstevel@tonic-gate 	/* aliases */
949*0Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
950*0Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
951*0Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0)
952*0Sstevel@tonic-gate 				/* skip rpc name */
953*0Sstevel@tonic-gate 				continue;
954*0Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
955*0Sstevel@tonic-gate 		}
956*0Sstevel@tonic-gate 	}
957*0Sstevel@tonic-gate 
958*0Sstevel@tonic-gate 	/* end of line */
959*0Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
960*0Sstevel@tonic-gate 
961*0Sstevel@tonic-gate }
962*0Sstevel@tonic-gate 
963*0Sstevel@tonic-gate /*
964*0Sstevel@tonic-gate  * /etc/protocols
965*0Sstevel@tonic-gate  *
966*0Sstevel@tonic-gate  */
967*0Sstevel@tonic-gate 
968*0Sstevel@tonic-gate static int
969*0Sstevel@tonic-gate genent_protocols(char *line, int (*cback)())
970*0Sstevel@tonic-gate {
971*0Sstevel@tonic-gate 	char buf[BUFSIZ+1];
972*0Sstevel@tonic-gate 	char *t;
973*0Sstevel@tonic-gate 	entry_col ecol[4];
974*0Sstevel@tonic-gate 	char *cname;
975*0Sstevel@tonic-gate 
976*0Sstevel@tonic-gate 	struct protoent	data;
977*0Sstevel@tonic-gate 	char *alias;
978*0Sstevel@tonic-gate 	int ctr = 0;
979*0Sstevel@tonic-gate 	int retval = 1;
980*0Sstevel@tonic-gate 	int rc = GENENT_OK;
981*0Sstevel@tonic-gate 
982*0Sstevel@tonic-gate 	/*
983*0Sstevel@tonic-gate 	 * don't clobber our argument
984*0Sstevel@tonic-gate 	 */
985*0Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
986*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
987*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
988*0Sstevel@tonic-gate 	}
989*0Sstevel@tonic-gate 	(void) strcpy(buf, line);
990*0Sstevel@tonic-gate 
991*0Sstevel@tonic-gate 	/*
992*0Sstevel@tonic-gate 	 * clear column data
993*0Sstevel@tonic-gate 	 */
994*0Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
995*0Sstevel@tonic-gate 
996*0Sstevel@tonic-gate 	/*
997*0Sstevel@tonic-gate 	 * comment (col 3)
998*0Sstevel@tonic-gate 	 */
999*0Sstevel@tonic-gate 	t = strchr(buf, '#');
1000*0Sstevel@tonic-gate 	if (t) {
1001*0Sstevel@tonic-gate 		*t++ = 0;
1002*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
1003*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
1004*0Sstevel@tonic-gate 	} else {
1005*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
1006*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
1007*0Sstevel@tonic-gate 	}
1008*0Sstevel@tonic-gate 
1009*0Sstevel@tonic-gate 	/*
1010*0Sstevel@tonic-gate 	 * cname(col 0)
1011*0Sstevel@tonic-gate 	 */
1012*0Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
1013*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no number");
1014*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1015*0Sstevel@tonic-gate 	}
1016*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
1017*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
1018*0Sstevel@tonic-gate 	cname = t;
1019*0Sstevel@tonic-gate 
1020*0Sstevel@tonic-gate 	/*
1021*0Sstevel@tonic-gate 	 * number (col 2)
1022*0Sstevel@tonic-gate 	 */
1023*0Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
1024*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no number");
1025*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1026*0Sstevel@tonic-gate 	}
1027*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
1028*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
1029*0Sstevel@tonic-gate 
1030*0Sstevel@tonic-gate 
1031*0Sstevel@tonic-gate 	/*
1032*0Sstevel@tonic-gate 	 * build entry
1033*0Sstevel@tonic-gate 	 */
1034*0Sstevel@tonic-gate 	data.p_name = strdup(ecol[0].ec_value.ec_value_val);
1035*0Sstevel@tonic-gate 
1036*0Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
1037*0Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val[0] != '\0') {
1038*0Sstevel@tonic-gate 
1039*0Sstevel@tonic-gate 		data.p_proto = ascii_to_int(ecol[2].ec_value.ec_value_val);
1040*0Sstevel@tonic-gate 		if (data.p_proto == -1) {
1041*0Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
1042*0Sstevel@tonic-gate 			    "invalid protocol number: %s",
1043*0Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
1044*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1045*0Sstevel@tonic-gate 		}
1046*0Sstevel@tonic-gate 	} else
1047*0Sstevel@tonic-gate 		data.p_proto = -1;
1048*0Sstevel@tonic-gate 
1049*0Sstevel@tonic-gate 	/*
1050*0Sstevel@tonic-gate 	 * name (col 1)
1051*0Sstevel@tonic-gate 	 */
1052*0Sstevel@tonic-gate 	t = cname;
1053*0Sstevel@tonic-gate 	ctr = 0;
1054*0Sstevel@tonic-gate 	data.p_aliases = NULL;
1055*0Sstevel@tonic-gate 
1056*0Sstevel@tonic-gate 	do {
1057*0Sstevel@tonic-gate 		/*
1058*0Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
1059*0Sstevel@tonic-gate 		 */
1060*0Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
1061*0Sstevel@tonic-gate 			continue;
1062*0Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
1063*0Sstevel@tonic-gate 			continue;
1064*0Sstevel@tonic-gate 
1065*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
1066*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
1067*0Sstevel@tonic-gate 
1068*0Sstevel@tonic-gate 		ctr++;
1069*0Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
1070*0Sstevel@tonic-gate 		if ((data.p_aliases = (char **)realloc(data.p_aliases,
1071*0Sstevel@tonic-gate 			ctr * sizeof (char **))) == NULL) {
1072*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
1073*0Sstevel@tonic-gate 			exit(1);
1074*0Sstevel@tonic-gate 		}
1075*0Sstevel@tonic-gate 		data.p_aliases[ctr-1] = alias;
1076*0Sstevel@tonic-gate 
1077*0Sstevel@tonic-gate 		/*
1078*0Sstevel@tonic-gate 		 * only put comment in canonical entry
1079*0Sstevel@tonic-gate 		 */
1080*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
1081*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
1082*0Sstevel@tonic-gate 
1083*0Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
1084*0Sstevel@tonic-gate 
1085*0Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
1086*0Sstevel@tonic-gate 	if ((data.p_aliases = (char **)realloc(data.p_aliases,
1087*0Sstevel@tonic-gate 		(ctr + 1) * sizeof (char **))) == NULL) {
1088*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
1089*0Sstevel@tonic-gate 		exit(1);
1090*0Sstevel@tonic-gate 	}
1091*0Sstevel@tonic-gate 	data.p_aliases[ctr] = NULL;
1092*0Sstevel@tonic-gate 
1093*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
1094*0Sstevel@tonic-gate 		(void) fprintf(stdout,
1095*0Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.p_name);
1096*0Sstevel@tonic-gate 
1097*0Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
1098*0Sstevel@tonic-gate 
1099*0Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
1100*0Sstevel@tonic-gate 		if (continue_onerror)
1101*0Sstevel@tonic-gate 			(void) fprintf(stderr,
1102*0Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
1103*0Sstevel@tonic-gate 			data.p_name);
1104*0Sstevel@tonic-gate 		else {
1105*0Sstevel@tonic-gate 			rc = GENENT_CBERR;
1106*0Sstevel@tonic-gate 			(void) fprintf(stderr,
1107*0Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
1108*0Sstevel@tonic-gate 				data.p_name);
1109*0Sstevel@tonic-gate 		}
1110*0Sstevel@tonic-gate 	} else if (retval)
1111*0Sstevel@tonic-gate 		rc = GENENT_CBERR;
1112*0Sstevel@tonic-gate 
1113*0Sstevel@tonic-gate 	free(data.p_name);
1114*0Sstevel@tonic-gate 	free(data.p_aliases);
1115*0Sstevel@tonic-gate 
1116*0Sstevel@tonic-gate 	return (rc);
1117*0Sstevel@tonic-gate }
1118*0Sstevel@tonic-gate 
1119*0Sstevel@tonic-gate 
1120*0Sstevel@tonic-gate static void
1121*0Sstevel@tonic-gate dump_protocols(ns_ldap_result_t *res)
1122*0Sstevel@tonic-gate {
1123*0Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *protocolnumber = NULL;
1124*0Sstevel@tonic-gate 	int		 i, j;
1125*0Sstevel@tonic-gate 	char		*name, *cp;
1126*0Sstevel@tonic-gate 
1127*0Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
1128*0Sstevel@tonic-gate 		return;
1129*0Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
1130*0Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
1131*0Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
1132*0Sstevel@tonic-gate 			cn = attrptr;
1133*0Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "ipProtocolNumber")
1134*0Sstevel@tonic-gate 									== 0)
1135*0Sstevel@tonic-gate 			protocolnumber = attrptr;
1136*0Sstevel@tonic-gate 	}
1137*0Sstevel@tonic-gate 	/* sanity check */
1138*0Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
1139*0Sstevel@tonic-gate 	    protocolnumber == NULL || protocolnumber->attrvalue == NULL ||
1140*0Sstevel@tonic-gate 	    protocolnumber->attrvalue[0] == NULL)
1141*0Sstevel@tonic-gate 		return;
1142*0Sstevel@tonic-gate 
1143*0Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
1144*0Sstevel@tonic-gate 		return;
1145*0Sstevel@tonic-gate 
1146*0Sstevel@tonic-gate 	/* protocol name */
1147*0Sstevel@tonic-gate 	if (strlen(name) < 8)
1148*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t\t", name);
1149*0Sstevel@tonic-gate 	else
1150*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t", name);
1151*0Sstevel@tonic-gate 
1152*0Sstevel@tonic-gate 	/* protocol number */
1153*0Sstevel@tonic-gate 	(void) fprintf(stdout, "%-16s", protocolnumber->attrvalue[0]);
1154*0Sstevel@tonic-gate 
1155*0Sstevel@tonic-gate 	/* aliases */
1156*0Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
1157*0Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
1158*0Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0) {
1159*0Sstevel@tonic-gate 				if (cn->value_count > 1)
1160*0Sstevel@tonic-gate 					/* Do not replicate */
1161*0Sstevel@tonic-gate 					continue;
1162*0Sstevel@tonic-gate 				/*
1163*0Sstevel@tonic-gate 				 * Replicate name in uppercase as an aliase
1164*0Sstevel@tonic-gate 				 */
1165*0Sstevel@tonic-gate 				for (cp = cn->attrvalue[j]; *cp; cp++)
1166*0Sstevel@tonic-gate 					*cp = toupper(*cp);
1167*0Sstevel@tonic-gate 			}
1168*0Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
1169*0Sstevel@tonic-gate 		}
1170*0Sstevel@tonic-gate 	}
1171*0Sstevel@tonic-gate 
1172*0Sstevel@tonic-gate 	/* end of line */
1173*0Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
1174*0Sstevel@tonic-gate 
1175*0Sstevel@tonic-gate }
1176*0Sstevel@tonic-gate 
1177*0Sstevel@tonic-gate 
1178*0Sstevel@tonic-gate 
1179*0Sstevel@tonic-gate 
1180*0Sstevel@tonic-gate 
1181*0Sstevel@tonic-gate /*
1182*0Sstevel@tonic-gate  * /etc/networks
1183*0Sstevel@tonic-gate  *
1184*0Sstevel@tonic-gate  */
1185*0Sstevel@tonic-gate 
1186*0Sstevel@tonic-gate static int
1187*0Sstevel@tonic-gate genent_networks(char *line, int (*cback)())
1188*0Sstevel@tonic-gate {
1189*0Sstevel@tonic-gate 	char buf[BUFSIZ+1];
1190*0Sstevel@tonic-gate 	char *t;
1191*0Sstevel@tonic-gate 	entry_col ecol[4];
1192*0Sstevel@tonic-gate 	char *cname;
1193*0Sstevel@tonic-gate 
1194*0Sstevel@tonic-gate 	struct netent	data;
1195*0Sstevel@tonic-gate 	char *alias;
1196*0Sstevel@tonic-gate 	int ctr = 0;
1197*0Sstevel@tonic-gate 	int retval = 1;
1198*0Sstevel@tonic-gate 	int enet;
1199*0Sstevel@tonic-gate 	int rc = GENENT_OK;
1200*0Sstevel@tonic-gate 
1201*0Sstevel@tonic-gate 	/*
1202*0Sstevel@tonic-gate 	 * don't clobber our argument
1203*0Sstevel@tonic-gate 	 */
1204*0Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
1205*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
1206*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1207*0Sstevel@tonic-gate 	}
1208*0Sstevel@tonic-gate 	(void) strcpy(buf, line);
1209*0Sstevel@tonic-gate 
1210*0Sstevel@tonic-gate 	/*
1211*0Sstevel@tonic-gate 	 * clear column data
1212*0Sstevel@tonic-gate 	 */
1213*0Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
1214*0Sstevel@tonic-gate 
1215*0Sstevel@tonic-gate 	/*
1216*0Sstevel@tonic-gate 	 * comment (col 3)
1217*0Sstevel@tonic-gate 	 */
1218*0Sstevel@tonic-gate 	t = strchr(buf, '#');
1219*0Sstevel@tonic-gate 	if (t) {
1220*0Sstevel@tonic-gate 		*t++ = 0;
1221*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
1222*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
1223*0Sstevel@tonic-gate 	} else {
1224*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
1225*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
1226*0Sstevel@tonic-gate 	}
1227*0Sstevel@tonic-gate 
1228*0Sstevel@tonic-gate 	/*
1229*0Sstevel@tonic-gate 	 * cname(col 0)
1230*0Sstevel@tonic-gate 	 */
1231*0Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
1232*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no number");
1233*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1234*0Sstevel@tonic-gate 	}
1235*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
1236*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
1237*0Sstevel@tonic-gate 	cname = t;
1238*0Sstevel@tonic-gate 
1239*0Sstevel@tonic-gate 	/*
1240*0Sstevel@tonic-gate 	 * number (col 2)
1241*0Sstevel@tonic-gate 	 */
1242*0Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
1243*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no number");
1244*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1245*0Sstevel@tonic-gate 	}
1246*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
1247*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
1248*0Sstevel@tonic-gate 
1249*0Sstevel@tonic-gate 
1250*0Sstevel@tonic-gate 	/*
1251*0Sstevel@tonic-gate 	 * build entry
1252*0Sstevel@tonic-gate 	 */
1253*0Sstevel@tonic-gate 
1254*0Sstevel@tonic-gate 	data.n_name = strdup(ecol[0].ec_value.ec_value_val);
1255*0Sstevel@tonic-gate 	/*
1256*0Sstevel@tonic-gate 	 * data.n_net is an unsigned field,
1257*0Sstevel@tonic-gate 	 * assign -1 to it, make no sense.
1258*0Sstevel@tonic-gate 	 * Use enet here to avoid lint warning.
1259*0Sstevel@tonic-gate 	 */
1260*0Sstevel@tonic-gate 	enet = encode_network(ecol[2].ec_value.ec_value_val);
1261*0Sstevel@tonic-gate 
1262*0Sstevel@tonic-gate 	if (enet == -1 && continue_onerror == 0) {
1263*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Invalid network number\n"));
1264*0Sstevel@tonic-gate 		if (continue_onerror == 0)
1265*0Sstevel@tonic-gate 			return (GENENT_CBERR);
1266*0Sstevel@tonic-gate 	} else
1267*0Sstevel@tonic-gate 		data.n_net = enet;
1268*0Sstevel@tonic-gate 
1269*0Sstevel@tonic-gate 	/*
1270*0Sstevel@tonic-gate 	 * name (col 1)
1271*0Sstevel@tonic-gate 	 */
1272*0Sstevel@tonic-gate 	t = cname;
1273*0Sstevel@tonic-gate 	data.n_aliases = NULL;
1274*0Sstevel@tonic-gate 
1275*0Sstevel@tonic-gate 	do {
1276*0Sstevel@tonic-gate 		/*
1277*0Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
1278*0Sstevel@tonic-gate 		 */
1279*0Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
1280*0Sstevel@tonic-gate 			continue;
1281*0Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
1282*0Sstevel@tonic-gate 			continue;
1283*0Sstevel@tonic-gate 
1284*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
1285*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
1286*0Sstevel@tonic-gate 
1287*0Sstevel@tonic-gate 		ctr++;
1288*0Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
1289*0Sstevel@tonic-gate 		if ((data.n_aliases = (char **)realloc(data.n_aliases,
1290*0Sstevel@tonic-gate 			ctr * sizeof (char **))) == NULL) {
1291*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
1292*0Sstevel@tonic-gate 			exit(1);
1293*0Sstevel@tonic-gate 		}
1294*0Sstevel@tonic-gate 		data.n_aliases[ctr-1] = alias;
1295*0Sstevel@tonic-gate 
1296*0Sstevel@tonic-gate 		/*
1297*0Sstevel@tonic-gate 		 * only put comment in canonical entry
1298*0Sstevel@tonic-gate 		 */
1299*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
1300*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
1301*0Sstevel@tonic-gate 
1302*0Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
1303*0Sstevel@tonic-gate 
1304*0Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
1305*0Sstevel@tonic-gate 	if ((data.n_aliases = (char **)realloc(data.n_aliases,
1306*0Sstevel@tonic-gate 		(ctr + 1) * sizeof (char **))) == NULL) {
1307*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
1308*0Sstevel@tonic-gate 		exit(1);
1309*0Sstevel@tonic-gate 	}
1310*0Sstevel@tonic-gate 	data.n_aliases[ctr] = NULL;
1311*0Sstevel@tonic-gate 
1312*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
1313*0Sstevel@tonic-gate 		(void) fprintf(stdout,
1314*0Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.n_name);
1315*0Sstevel@tonic-gate 
1316*0Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
1317*0Sstevel@tonic-gate 
1318*0Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
1319*0Sstevel@tonic-gate 		if (continue_onerror)
1320*0Sstevel@tonic-gate 			(void) fprintf(stderr,
1321*0Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
1322*0Sstevel@tonic-gate 			data.n_name);
1323*0Sstevel@tonic-gate 		else {
1324*0Sstevel@tonic-gate 			rc = GENENT_CBERR;
1325*0Sstevel@tonic-gate 			(void) fprintf(stderr,
1326*0Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
1327*0Sstevel@tonic-gate 				data.n_name);
1328*0Sstevel@tonic-gate 		}
1329*0Sstevel@tonic-gate 	} else if (retval)
1330*0Sstevel@tonic-gate 		rc = GENENT_CBERR;
1331*0Sstevel@tonic-gate 
1332*0Sstevel@tonic-gate 	free(data.n_name);
1333*0Sstevel@tonic-gate 	free(data.n_aliases);
1334*0Sstevel@tonic-gate 
1335*0Sstevel@tonic-gate 	return (rc);
1336*0Sstevel@tonic-gate }
1337*0Sstevel@tonic-gate 
1338*0Sstevel@tonic-gate 
1339*0Sstevel@tonic-gate static void
1340*0Sstevel@tonic-gate dump_networks(ns_ldap_result_t *res)
1341*0Sstevel@tonic-gate {
1342*0Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *networknumber = NULL;
1343*0Sstevel@tonic-gate 	int		 i, j;
1344*0Sstevel@tonic-gate 	char		*name;
1345*0Sstevel@tonic-gate 
1346*0Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
1347*0Sstevel@tonic-gate 		return;
1348*0Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
1349*0Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
1350*0Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
1351*0Sstevel@tonic-gate 			cn = attrptr;
1352*0Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "ipNetworkNumber")
1353*0Sstevel@tonic-gate 									== 0)
1354*0Sstevel@tonic-gate 			networknumber = attrptr;
1355*0Sstevel@tonic-gate 	}
1356*0Sstevel@tonic-gate 	/* sanity check */
1357*0Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
1358*0Sstevel@tonic-gate 	    networknumber == NULL || networknumber->attrvalue == NULL ||
1359*0Sstevel@tonic-gate 	    networknumber->attrvalue[0] == NULL)
1360*0Sstevel@tonic-gate 		return;
1361*0Sstevel@tonic-gate 
1362*0Sstevel@tonic-gate 	/*
1363*0Sstevel@tonic-gate 	 * cn can be a MUST attribute(RFC 2307) or MAY attribute(2307bis).
1364*0Sstevel@tonic-gate 	 * If the canonical name can not be found (2307bis), use the 1st
1365*0Sstevel@tonic-gate 	 * value as the official name.
1366*0Sstevel@tonic-gate 	 */
1367*0Sstevel@tonic-gate 
1368*0Sstevel@tonic-gate 	/* network name */
1369*0Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
1370*0Sstevel@tonic-gate 		name = cn->attrvalue[0];
1371*0Sstevel@tonic-gate 
1372*0Sstevel@tonic-gate 	if (strlen(name) < 8)
1373*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t\t", name);
1374*0Sstevel@tonic-gate 	else
1375*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t", name);
1376*0Sstevel@tonic-gate 
1377*0Sstevel@tonic-gate 	/* network number */
1378*0Sstevel@tonic-gate 	(void) fprintf(stdout, "%-16s", networknumber->attrvalue[0]);
1379*0Sstevel@tonic-gate 
1380*0Sstevel@tonic-gate 	/* aliases */
1381*0Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
1382*0Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
1383*0Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0)
1384*0Sstevel@tonic-gate 				/* skip name */
1385*0Sstevel@tonic-gate 				continue;
1386*0Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
1387*0Sstevel@tonic-gate 		}
1388*0Sstevel@tonic-gate 	}
1389*0Sstevel@tonic-gate 
1390*0Sstevel@tonic-gate 	/* end of line */
1391*0Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
1392*0Sstevel@tonic-gate 
1393*0Sstevel@tonic-gate }
1394*0Sstevel@tonic-gate 
1395*0Sstevel@tonic-gate 
1396*0Sstevel@tonic-gate 
1397*0Sstevel@tonic-gate 
1398*0Sstevel@tonic-gate /*
1399*0Sstevel@tonic-gate  * /etc/services
1400*0Sstevel@tonic-gate  *
1401*0Sstevel@tonic-gate  */
1402*0Sstevel@tonic-gate 
1403*0Sstevel@tonic-gate static int
1404*0Sstevel@tonic-gate genent_services(char *line, int (*cback)())
1405*0Sstevel@tonic-gate {
1406*0Sstevel@tonic-gate 	char buf[BUFSIZ+1];
1407*0Sstevel@tonic-gate 	char *t, *p;
1408*0Sstevel@tonic-gate 	entry_col ecol[5];
1409*0Sstevel@tonic-gate 	char *cname;
1410*0Sstevel@tonic-gate 
1411*0Sstevel@tonic-gate 	struct servent	data;
1412*0Sstevel@tonic-gate 	char *alias;
1413*0Sstevel@tonic-gate 	int ctr = 0;
1414*0Sstevel@tonic-gate 	int retval = 1;
1415*0Sstevel@tonic-gate 	int rc = GENENT_OK;
1416*0Sstevel@tonic-gate 
1417*0Sstevel@tonic-gate 	/*
1418*0Sstevel@tonic-gate 	 * don't clobber our argument
1419*0Sstevel@tonic-gate 	 */
1420*0Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
1421*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
1422*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1423*0Sstevel@tonic-gate 	}
1424*0Sstevel@tonic-gate 	(void) strcpy(buf, line);
1425*0Sstevel@tonic-gate 
1426*0Sstevel@tonic-gate 	/*
1427*0Sstevel@tonic-gate 	 * clear column data
1428*0Sstevel@tonic-gate 	 */
1429*0Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
1430*0Sstevel@tonic-gate 
1431*0Sstevel@tonic-gate 	/*
1432*0Sstevel@tonic-gate 	 * comment (col 4)
1433*0Sstevel@tonic-gate 	 */
1434*0Sstevel@tonic-gate 	t = strchr(buf, '#');
1435*0Sstevel@tonic-gate 	if (t) {
1436*0Sstevel@tonic-gate 		*t++ = 0;
1437*0Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_val = t;
1438*0Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_len = strlen(t)+1;
1439*0Sstevel@tonic-gate 	} else {
1440*0Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_val = 0;
1441*0Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_len = 0;
1442*0Sstevel@tonic-gate 	}
1443*0Sstevel@tonic-gate 
1444*0Sstevel@tonic-gate 	/*
1445*0Sstevel@tonic-gate 	 * cname(col 0)
1446*0Sstevel@tonic-gate 	 */
1447*0Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
1448*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no port");
1449*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1450*0Sstevel@tonic-gate 	}
1451*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
1452*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
1453*0Sstevel@tonic-gate 	cname = t;
1454*0Sstevel@tonic-gate 
1455*0Sstevel@tonic-gate 	/*
1456*0Sstevel@tonic-gate 	 * port (col 3)
1457*0Sstevel@tonic-gate 	 */
1458*0Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
1459*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no protocol");
1460*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1461*0Sstevel@tonic-gate 	}
1462*0Sstevel@tonic-gate 	if ((p = strchr(t, '/')) == 0) {
1463*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "bad port/proto");
1464*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1465*0Sstevel@tonic-gate 	}
1466*0Sstevel@tonic-gate 	*(p++) = 0;
1467*0Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
1468*0Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
1469*0Sstevel@tonic-gate 
1470*0Sstevel@tonic-gate 	/*
1471*0Sstevel@tonic-gate 	 * proto (col 2)
1472*0Sstevel@tonic-gate 	 */
1473*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = p;
1474*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(p)+1;
1475*0Sstevel@tonic-gate 
1476*0Sstevel@tonic-gate 
1477*0Sstevel@tonic-gate 	/*
1478*0Sstevel@tonic-gate 	 * build entry
1479*0Sstevel@tonic-gate 	 */
1480*0Sstevel@tonic-gate 
1481*0Sstevel@tonic-gate 	data.s_name = strdup(ecol[0].ec_value.ec_value_val);
1482*0Sstevel@tonic-gate 	data.s_proto = strdup(ecol[2].ec_value.ec_value_val);
1483*0Sstevel@tonic-gate 
1484*0Sstevel@tonic-gate 	if (ecol[3].ec_value.ec_value_val != NULL &&
1485*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val[0] != '\0') {
1486*0Sstevel@tonic-gate 
1487*0Sstevel@tonic-gate 		data.s_port = ascii_to_int(ecol[3].ec_value.ec_value_val);
1488*0Sstevel@tonic-gate 		if (data.s_port == -1) {
1489*0Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
1490*0Sstevel@tonic-gate 			    "invalid port number: %s",
1491*0Sstevel@tonic-gate 			    ecol[3].ec_value.ec_value_val);
1492*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1493*0Sstevel@tonic-gate 		}
1494*0Sstevel@tonic-gate 	} else
1495*0Sstevel@tonic-gate 		data.s_port = -1;
1496*0Sstevel@tonic-gate 
1497*0Sstevel@tonic-gate 	/*
1498*0Sstevel@tonic-gate 	 * name (col 1)
1499*0Sstevel@tonic-gate 	 */
1500*0Sstevel@tonic-gate 	t = cname;
1501*0Sstevel@tonic-gate 	data.s_aliases = NULL;
1502*0Sstevel@tonic-gate 
1503*0Sstevel@tonic-gate 	do {
1504*0Sstevel@tonic-gate 		/*
1505*0Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
1506*0Sstevel@tonic-gate 		 */
1507*0Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
1508*0Sstevel@tonic-gate 			continue;
1509*0Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
1510*0Sstevel@tonic-gate 			continue;
1511*0Sstevel@tonic-gate 
1512*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
1513*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
1514*0Sstevel@tonic-gate 
1515*0Sstevel@tonic-gate 		ctr++;
1516*0Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
1517*0Sstevel@tonic-gate 		if ((data.s_aliases = (char **)realloc(data.s_aliases,
1518*0Sstevel@tonic-gate 			ctr * sizeof (char **))) == NULL) {
1519*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
1520*0Sstevel@tonic-gate 			exit(1);
1521*0Sstevel@tonic-gate 		}
1522*0Sstevel@tonic-gate 		data.s_aliases[ctr-1] = alias;
1523*0Sstevel@tonic-gate 
1524*0Sstevel@tonic-gate 		/*
1525*0Sstevel@tonic-gate 		 * only put comment in canonical entry
1526*0Sstevel@tonic-gate 		 */
1527*0Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_val = 0;
1528*0Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_len = 0;
1529*0Sstevel@tonic-gate 
1530*0Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
1531*0Sstevel@tonic-gate 
1532*0Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
1533*0Sstevel@tonic-gate 	if ((data.s_aliases = (char **)realloc(data.s_aliases,
1534*0Sstevel@tonic-gate 		(ctr + 1) * sizeof (char **))) == NULL) {
1535*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
1536*0Sstevel@tonic-gate 		exit(1);
1537*0Sstevel@tonic-gate 	}
1538*0Sstevel@tonic-gate 	data.s_aliases[ctr] = NULL;
1539*0Sstevel@tonic-gate 
1540*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
1541*0Sstevel@tonic-gate 		(void) fprintf(stdout,
1542*0Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), line);
1543*0Sstevel@tonic-gate 
1544*0Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
1545*0Sstevel@tonic-gate 
1546*0Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
1547*0Sstevel@tonic-gate 		if (continue_onerror)
1548*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
1549*0Sstevel@tonic-gate 					"Entry: cn=%s+ipServiceProtocol=%s"
1550*0Sstevel@tonic-gate 					" already Exists, skipping it.\n"),
1551*0Sstevel@tonic-gate 					data.s_name, data.s_proto);
1552*0Sstevel@tonic-gate 		else {
1553*0Sstevel@tonic-gate 			rc = GENENT_CBERR;
1554*0Sstevel@tonic-gate 			(void) fprintf(stderr,
1555*0Sstevel@tonic-gate 				gettext("Entry: cn=%s+ipServiceProtocol=%s"
1556*0Sstevel@tonic-gate 					" - already Exists\n"),
1557*0Sstevel@tonic-gate 					data.s_name, data.s_proto);
1558*0Sstevel@tonic-gate 		}
1559*0Sstevel@tonic-gate 	} else if (retval)
1560*0Sstevel@tonic-gate 		rc = GENENT_CBERR;
1561*0Sstevel@tonic-gate 
1562*0Sstevel@tonic-gate 	free(data.s_name);
1563*0Sstevel@tonic-gate 	free(data.s_proto);
1564*0Sstevel@tonic-gate 	free(data.s_aliases);
1565*0Sstevel@tonic-gate 
1566*0Sstevel@tonic-gate 	return (rc);
1567*0Sstevel@tonic-gate }
1568*0Sstevel@tonic-gate 
1569*0Sstevel@tonic-gate 
1570*0Sstevel@tonic-gate 
1571*0Sstevel@tonic-gate static void
1572*0Sstevel@tonic-gate dump_services(ns_ldap_result_t *res)
1573*0Sstevel@tonic-gate {
1574*0Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *port = NULL;
1575*0Sstevel@tonic-gate 	ns_ldap_attr_t	*protocol = NULL;
1576*0Sstevel@tonic-gate 	int		i, j, len;
1577*0Sstevel@tonic-gate 	char		*name; /* service name */
1578*0Sstevel@tonic-gate 
1579*0Sstevel@tonic-gate 	/*
1580*0Sstevel@tonic-gate 	 * cn can have multiple values.(service name and its aliases)
1581*0Sstevel@tonic-gate 	 * In order to support RFC 2307, section 5.5, ipserviceprotocol  can
1582*0Sstevel@tonic-gate 	 * have multiple values too.
1583*0Sstevel@tonic-gate 	 * The output format should look like
1584*0Sstevel@tonic-gate 	 *
1585*0Sstevel@tonic-gate 	 * test		2345/udp mytest
1586*0Sstevel@tonic-gate 	 * test		2345/tcp mytest
1587*0Sstevel@tonic-gate 	 */
1588*0Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
1589*0Sstevel@tonic-gate 		return;
1590*0Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
1591*0Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
1592*0Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
1593*0Sstevel@tonic-gate 			cn = attrptr;
1594*0Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "ipServicePort") == 0)
1595*0Sstevel@tonic-gate 			port = attrptr;
1596*0Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname,
1597*0Sstevel@tonic-gate 					"ipServiceProtocol") == 0)
1598*0Sstevel@tonic-gate 			protocol = attrptr;
1599*0Sstevel@tonic-gate 	}
1600*0Sstevel@tonic-gate 	/* sanity check */
1601*0Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
1602*0Sstevel@tonic-gate 	    port == NULL || port->attrvalue == NULL ||
1603*0Sstevel@tonic-gate 	    port->attrvalue[0] == NULL || protocol == NULL ||
1604*0Sstevel@tonic-gate 	    protocol->attrvalue == NULL || protocol->attrvalue[0] == NULL)
1605*0Sstevel@tonic-gate 		return;
1606*0Sstevel@tonic-gate 
1607*0Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
1608*0Sstevel@tonic-gate 		return;
1609*0Sstevel@tonic-gate 	for (i = 0; i < protocol->value_count; i++) {
1610*0Sstevel@tonic-gate 		if (protocol->attrvalue[i] == NULL)
1611*0Sstevel@tonic-gate 			return;
1612*0Sstevel@tonic-gate 		/* service name */
1613*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%-16s", name);
1614*0Sstevel@tonic-gate 
1615*0Sstevel@tonic-gate 		/* port & protocol */
1616*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s/%s%n", port->attrvalue[0],
1617*0Sstevel@tonic-gate 				protocol->attrvalue[i], &len);
1618*0Sstevel@tonic-gate 
1619*0Sstevel@tonic-gate 		if (len < 8)
1620*0Sstevel@tonic-gate 			(void) fprintf(stdout, "\t\t");
1621*0Sstevel@tonic-gate 		else
1622*0Sstevel@tonic-gate 			(void) fprintf(stdout, "\t");
1623*0Sstevel@tonic-gate 
1624*0Sstevel@tonic-gate 		/* aliases */
1625*0Sstevel@tonic-gate 		for (j = 0; j < cn->value_count; j++) {
1626*0Sstevel@tonic-gate 			if (cn->attrvalue[j]) {
1627*0Sstevel@tonic-gate 				if (strcasecmp(name, cn->attrvalue[j]) == 0)
1628*0Sstevel@tonic-gate 					/* skip service name */
1629*0Sstevel@tonic-gate 					continue;
1630*0Sstevel@tonic-gate 				(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
1631*0Sstevel@tonic-gate 			}
1632*0Sstevel@tonic-gate 		}
1633*0Sstevel@tonic-gate 
1634*0Sstevel@tonic-gate 		/* end of line */
1635*0Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
1636*0Sstevel@tonic-gate 	}
1637*0Sstevel@tonic-gate }
1638*0Sstevel@tonic-gate 
1639*0Sstevel@tonic-gate 
1640*0Sstevel@tonic-gate /*
1641*0Sstevel@tonic-gate  * /etc/group
1642*0Sstevel@tonic-gate  */
1643*0Sstevel@tonic-gate 
1644*0Sstevel@tonic-gate static int
1645*0Sstevel@tonic-gate genent_group(char *line, int (*cback)())
1646*0Sstevel@tonic-gate {
1647*0Sstevel@tonic-gate 	char buf[BIGBUF+1];
1648*0Sstevel@tonic-gate 	char *s, *t;
1649*0Sstevel@tonic-gate 	entry_col ecol[5];
1650*0Sstevel@tonic-gate 
1651*0Sstevel@tonic-gate 	struct group	data;
1652*0Sstevel@tonic-gate 	char *memb;
1653*0Sstevel@tonic-gate 	int ctr = 0;
1654*0Sstevel@tonic-gate 	int retval = 1;
1655*0Sstevel@tonic-gate 	int rc = GENENT_OK;
1656*0Sstevel@tonic-gate 
1657*0Sstevel@tonic-gate 	/*
1658*0Sstevel@tonic-gate 	 * don't clobber our argument
1659*0Sstevel@tonic-gate 	 */
1660*0Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
1661*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
1662*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1663*0Sstevel@tonic-gate 	}
1664*0Sstevel@tonic-gate 	(void) strcpy(buf, line);
1665*0Sstevel@tonic-gate 	t = buf;
1666*0Sstevel@tonic-gate 
1667*0Sstevel@tonic-gate 	/* ignore empty entries */
1668*0Sstevel@tonic-gate 	if (*t == '\0')
1669*0Sstevel@tonic-gate 		return (GENENT_OK);
1670*0Sstevel@tonic-gate 
1671*0Sstevel@tonic-gate 	/*
1672*0Sstevel@tonic-gate 	 * clear column data
1673*0Sstevel@tonic-gate 	 */
1674*0Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
1675*0Sstevel@tonic-gate 
1676*0Sstevel@tonic-gate 	/*
1677*0Sstevel@tonic-gate 	 * name (col 0)
1678*0Sstevel@tonic-gate 	 */
1679*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
1680*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no passwd");
1681*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1682*0Sstevel@tonic-gate 	}
1683*0Sstevel@tonic-gate 	*s++ = 0;
1684*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
1685*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
1686*0Sstevel@tonic-gate 	t = s;
1687*0Sstevel@tonic-gate 
1688*0Sstevel@tonic-gate 	/*
1689*0Sstevel@tonic-gate 	 * passwd (col 1)
1690*0Sstevel@tonic-gate 	 */
1691*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
1692*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no gid");
1693*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1694*0Sstevel@tonic-gate 	}
1695*0Sstevel@tonic-gate 	*s++ = 0;
1696*0Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
1697*0Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
1698*0Sstevel@tonic-gate 	t = s;
1699*0Sstevel@tonic-gate 
1700*0Sstevel@tonic-gate 
1701*0Sstevel@tonic-gate 	/*
1702*0Sstevel@tonic-gate 	 * gid (col 2)
1703*0Sstevel@tonic-gate 	 */
1704*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0 || s == t) {
1705*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no members");
1706*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1707*0Sstevel@tonic-gate 	}
1708*0Sstevel@tonic-gate 	*s++ = 0;
1709*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
1710*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
1711*0Sstevel@tonic-gate 	t = s;
1712*0Sstevel@tonic-gate 
1713*0Sstevel@tonic-gate 	/*
1714*0Sstevel@tonic-gate 	 * members (col 3)
1715*0Sstevel@tonic-gate 	 */
1716*0Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
1717*0Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
1718*0Sstevel@tonic-gate 
1719*0Sstevel@tonic-gate 
1720*0Sstevel@tonic-gate 	/*
1721*0Sstevel@tonic-gate 	 * build entry
1722*0Sstevel@tonic-gate 	 */
1723*0Sstevel@tonic-gate 	data.gr_name = strdup(ecol[0].ec_value.ec_value_val);
1724*0Sstevel@tonic-gate 	data.gr_passwd = strdup(ecol[1].ec_value.ec_value_val);
1725*0Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
1726*0Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val[0] != '\0') {
1727*0Sstevel@tonic-gate 
1728*0Sstevel@tonic-gate 		data.gr_gid = ascii_to_int(ecol[2].ec_value.ec_value_val);
1729*0Sstevel@tonic-gate 		if (data.gr_gid == -1) {
1730*0Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
1731*0Sstevel@tonic-gate 			    "invalid group id: %s",
1732*0Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
1733*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1734*0Sstevel@tonic-gate 		}
1735*0Sstevel@tonic-gate 	} else
1736*0Sstevel@tonic-gate 		data.gr_gid = -1;
1737*0Sstevel@tonic-gate 
1738*0Sstevel@tonic-gate 	data.gr_mem = NULL;
1739*0Sstevel@tonic-gate 
1740*0Sstevel@tonic-gate 	while (s = strchr(t, ',')) {
1741*0Sstevel@tonic-gate 
1742*0Sstevel@tonic-gate 		*s++ = 0;
1743*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
1744*0Sstevel@tonic-gate 		t = s;
1745*0Sstevel@tonic-gate 		ctr++;
1746*0Sstevel@tonic-gate 		memb = strdup(ecol[3].ec_value.ec_value_val);
1747*0Sstevel@tonic-gate 		if ((data.gr_mem = (char **)realloc(data.gr_mem,
1748*0Sstevel@tonic-gate 			ctr * sizeof (char **))) == NULL) {
1749*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
1750*0Sstevel@tonic-gate 			exit(1);
1751*0Sstevel@tonic-gate 		}
1752*0Sstevel@tonic-gate 		data.gr_mem[ctr-1] = memb;
1753*0Sstevel@tonic-gate 	}
1754*0Sstevel@tonic-gate 
1755*0Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
1756*0Sstevel@tonic-gate 	if ((data.gr_mem = (char **)realloc(data.gr_mem,
1757*0Sstevel@tonic-gate 		(ctr + 2) * sizeof (char **))) == NULL) {
1758*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
1759*0Sstevel@tonic-gate 		exit(1);
1760*0Sstevel@tonic-gate 	}
1761*0Sstevel@tonic-gate 	data.gr_mem[ctr] = t;
1762*0Sstevel@tonic-gate 	data.gr_mem[ctr+1] = NULL;
1763*0Sstevel@tonic-gate 
1764*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
1765*0Sstevel@tonic-gate 		(void) fprintf(stdout,
1766*0Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.gr_name);
1767*0Sstevel@tonic-gate 
1768*0Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
1769*0Sstevel@tonic-gate 
1770*0Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
1771*0Sstevel@tonic-gate 		if (continue_onerror)
1772*0Sstevel@tonic-gate 			(void) fprintf(stderr,
1773*0Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
1774*0Sstevel@tonic-gate 			data.gr_name);
1775*0Sstevel@tonic-gate 		else {
1776*0Sstevel@tonic-gate 			rc = GENENT_CBERR;
1777*0Sstevel@tonic-gate 			(void) fprintf(stderr,
1778*0Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
1779*0Sstevel@tonic-gate 				data.gr_name);
1780*0Sstevel@tonic-gate 		}
1781*0Sstevel@tonic-gate 	} else if (retval)
1782*0Sstevel@tonic-gate 		rc = GENENT_CBERR;
1783*0Sstevel@tonic-gate 
1784*0Sstevel@tonic-gate 	free(data.gr_name);
1785*0Sstevel@tonic-gate 	free(data.gr_passwd);
1786*0Sstevel@tonic-gate 	free(data.gr_mem);
1787*0Sstevel@tonic-gate 
1788*0Sstevel@tonic-gate 	return (rc);
1789*0Sstevel@tonic-gate }
1790*0Sstevel@tonic-gate 
1791*0Sstevel@tonic-gate static void
1792*0Sstevel@tonic-gate dump_group(ns_ldap_result_t *res)
1793*0Sstevel@tonic-gate {
1794*0Sstevel@tonic-gate 	char    **value = NULL;
1795*0Sstevel@tonic-gate 	char	pnam[256];
1796*0Sstevel@tonic-gate 	int	attr_count = 0;
1797*0Sstevel@tonic-gate 
1798*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
1799*0Sstevel@tonic-gate 	if (value && value[0])
1800*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
1801*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "userPassword");
1802*0Sstevel@tonic-gate 	if (value == NULL || value[0] == NULL)
1803*0Sstevel@tonic-gate 		(void) fprintf(stdout, "*:");
1804*0Sstevel@tonic-gate 	else {
1805*0Sstevel@tonic-gate 		(void) strcpy(pnam, value[0]);
1806*0Sstevel@tonic-gate 		if (strncasecmp(value[0], "{crypt}", 7) == 0)
1807*0Sstevel@tonic-gate 			(void) fprintf(stdout, "%s:", (pnam+7));
1808*0Sstevel@tonic-gate 		else
1809*0Sstevel@tonic-gate 			(void) fprintf(stdout, "*:");
1810*0Sstevel@tonic-gate 	}
1811*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "gidNumber");
1812*0Sstevel@tonic-gate 	if (value && value[0])
1813*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
1814*0Sstevel@tonic-gate 
1815*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "memberUid");
1816*0Sstevel@tonic-gate 	if (value != NULL && value[0] != NULL) {
1817*0Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
1818*0Sstevel@tonic-gate 			if (value[attr_count+1] == NULL)
1819*0Sstevel@tonic-gate 				(void) fprintf(stdout, "%s", value[attr_count]);
1820*0Sstevel@tonic-gate 			else
1821*0Sstevel@tonic-gate 				(void) fprintf(stdout, "%s,",
1822*0Sstevel@tonic-gate 					value[attr_count]);
1823*0Sstevel@tonic-gate 			attr_count++;
1824*0Sstevel@tonic-gate 		}
1825*0Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
1826*0Sstevel@tonic-gate 	}
1827*0Sstevel@tonic-gate 	else
1828*0Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
1829*0Sstevel@tonic-gate }
1830*0Sstevel@tonic-gate 
1831*0Sstevel@tonic-gate 
1832*0Sstevel@tonic-gate 
1833*0Sstevel@tonic-gate 
1834*0Sstevel@tonic-gate 
1835*0Sstevel@tonic-gate /*
1836*0Sstevel@tonic-gate  * /etc/ethers
1837*0Sstevel@tonic-gate  */
1838*0Sstevel@tonic-gate 
1839*0Sstevel@tonic-gate static int
1840*0Sstevel@tonic-gate genent_ethers(char *line, int (*cback)())
1841*0Sstevel@tonic-gate {
1842*0Sstevel@tonic-gate 	char buf[BUFSIZ+1];
1843*0Sstevel@tonic-gate 	char *t;
1844*0Sstevel@tonic-gate 	entry_col ecol[3];
1845*0Sstevel@tonic-gate 	int retval = 1;
1846*0Sstevel@tonic-gate 	struct _ns_ethers	data;
1847*0Sstevel@tonic-gate 	int rc = GENENT_OK;
1848*0Sstevel@tonic-gate 
1849*0Sstevel@tonic-gate 	/*
1850*0Sstevel@tonic-gate 	 * don't clobber our argument
1851*0Sstevel@tonic-gate 	 */
1852*0Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
1853*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
1854*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1855*0Sstevel@tonic-gate 	}
1856*0Sstevel@tonic-gate 	(void) strcpy(buf, line);
1857*0Sstevel@tonic-gate 
1858*0Sstevel@tonic-gate 	/*
1859*0Sstevel@tonic-gate 	 * clear column data
1860*0Sstevel@tonic-gate 	 */
1861*0Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
1862*0Sstevel@tonic-gate 
1863*0Sstevel@tonic-gate 	/*
1864*0Sstevel@tonic-gate 	 * comment (col 2)
1865*0Sstevel@tonic-gate 	 */
1866*0Sstevel@tonic-gate 	t = strchr(buf, '#');
1867*0Sstevel@tonic-gate 	if (t) {
1868*0Sstevel@tonic-gate 		*t++ = 0;
1869*0Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = t;
1870*0Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = strlen(t)+1;
1871*0Sstevel@tonic-gate 	} else {
1872*0Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = 0;
1873*0Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = 0;
1874*0Sstevel@tonic-gate 	}
1875*0Sstevel@tonic-gate 
1876*0Sstevel@tonic-gate 	/*
1877*0Sstevel@tonic-gate 	 * addr(col 0)
1878*0Sstevel@tonic-gate 	 */
1879*0Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
1880*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no name");
1881*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1882*0Sstevel@tonic-gate 	}
1883*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
1884*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
1885*0Sstevel@tonic-gate 
1886*0Sstevel@tonic-gate 	/*
1887*0Sstevel@tonic-gate 	 * name(col 1)
1888*0Sstevel@tonic-gate 	 */
1889*0Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
1890*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no white space allowed in name");
1891*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1892*0Sstevel@tonic-gate 	}
1893*0Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
1894*0Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
1895*0Sstevel@tonic-gate 
1896*0Sstevel@tonic-gate 
1897*0Sstevel@tonic-gate 	/*
1898*0Sstevel@tonic-gate 	 * build entry
1899*0Sstevel@tonic-gate 	 */
1900*0Sstevel@tonic-gate 
1901*0Sstevel@tonic-gate 	data.ether = strdup(ecol[0].ec_value.ec_value_val);
1902*0Sstevel@tonic-gate 	data.name  = strdup(ecol[1].ec_value.ec_value_val);
1903*0Sstevel@tonic-gate 
1904*0Sstevel@tonic-gate 
1905*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
1906*0Sstevel@tonic-gate 		(void) fprintf(stdout,
1907*0Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.name);
1908*0Sstevel@tonic-gate 
1909*0Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
1910*0Sstevel@tonic-gate 
1911*0Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
1912*0Sstevel@tonic-gate 		if (continue_onerror)
1913*0Sstevel@tonic-gate 			(void) fprintf(stderr,
1914*0Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
1915*0Sstevel@tonic-gate 			data.name);
1916*0Sstevel@tonic-gate 		else {
1917*0Sstevel@tonic-gate 			rc = GENENT_CBERR;
1918*0Sstevel@tonic-gate 			(void) fprintf(stderr,
1919*0Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
1920*0Sstevel@tonic-gate 				data.name);
1921*0Sstevel@tonic-gate 		}
1922*0Sstevel@tonic-gate 	} else if (retval)
1923*0Sstevel@tonic-gate 		rc = GENENT_CBERR;
1924*0Sstevel@tonic-gate 
1925*0Sstevel@tonic-gate 	free(data.ether);
1926*0Sstevel@tonic-gate 	free(data.name);
1927*0Sstevel@tonic-gate 
1928*0Sstevel@tonic-gate 	return (rc);
1929*0Sstevel@tonic-gate }
1930*0Sstevel@tonic-gate 
1931*0Sstevel@tonic-gate 
1932*0Sstevel@tonic-gate static void
1933*0Sstevel@tonic-gate dump_ethers(ns_ldap_result_t *res)
1934*0Sstevel@tonic-gate {
1935*0Sstevel@tonic-gate 	char	**value = NULL;
1936*0Sstevel@tonic-gate 
1937*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "macAddress");
1938*0Sstevel@tonic-gate 	if (value && value[0])
1939*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
1940*0Sstevel@tonic-gate 	else
1941*0Sstevel@tonic-gate 		return;
1942*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
1943*0Sstevel@tonic-gate 	if (value && value[0])
1944*0Sstevel@tonic-gate 		(void) fprintf(stdout, "	%s\n", value[0]);
1945*0Sstevel@tonic-gate }
1946*0Sstevel@tonic-gate 
1947*0Sstevel@tonic-gate static int
1948*0Sstevel@tonic-gate genent_aliases(char *line, int (*cback)())
1949*0Sstevel@tonic-gate {
1950*0Sstevel@tonic-gate 	char buf[BUFSIZ+1];
1951*0Sstevel@tonic-gate 	char *t, *aliases;
1952*0Sstevel@tonic-gate 	char *cname;
1953*0Sstevel@tonic-gate 	int ctr = 0;
1954*0Sstevel@tonic-gate 	int retval = 1;
1955*0Sstevel@tonic-gate 	int i;
1956*0Sstevel@tonic-gate 
1957*0Sstevel@tonic-gate 	struct _ns_alias data;
1958*0Sstevel@tonic-gate 	char *alias;
1959*0Sstevel@tonic-gate 	int rc = GENENT_OK;
1960*0Sstevel@tonic-gate 
1961*0Sstevel@tonic-gate 	/*
1962*0Sstevel@tonic-gate 	 * don't clobber our argument
1963*0Sstevel@tonic-gate 	 */
1964*0Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
1965*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
1966*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1967*0Sstevel@tonic-gate 	}
1968*0Sstevel@tonic-gate 
1969*0Sstevel@tonic-gate 	(void) strcpy(buf, line);
1970*0Sstevel@tonic-gate 
1971*0Sstevel@tonic-gate 	if ((t = strchr(buf, ':')) == 0) {
1972*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no alias name");
1973*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1974*0Sstevel@tonic-gate 	}
1975*0Sstevel@tonic-gate 
1976*0Sstevel@tonic-gate 	t[0] = '\0';
1977*0Sstevel@tonic-gate 	if (++t == '\0') {
1978*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no alias value");
1979*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
1980*0Sstevel@tonic-gate 	}
1981*0Sstevel@tonic-gate 
1982*0Sstevel@tonic-gate 	cname = buf;
1983*0Sstevel@tonic-gate 	aliases = t;
1984*0Sstevel@tonic-gate 
1985*0Sstevel@tonic-gate 	/* build entry */
1986*0Sstevel@tonic-gate 	data.alias = strdup(cname);
1987*0Sstevel@tonic-gate 	if (!data.alias) {
1988*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
1989*0Sstevel@tonic-gate 		exit(1);
1990*0Sstevel@tonic-gate 	}
1991*0Sstevel@tonic-gate 
1992*0Sstevel@tonic-gate 	data.member = NULL;
1993*0Sstevel@tonic-gate 	t = strtok(aliases, ",");
1994*0Sstevel@tonic-gate 	do {
1995*0Sstevel@tonic-gate 		ctr++;
1996*0Sstevel@tonic-gate 		while (t[0] == ' ')
1997*0Sstevel@tonic-gate 			t++;
1998*0Sstevel@tonic-gate 		alias = strdup(t);
1999*0Sstevel@tonic-gate 		if ((alias == NULL) ||
2000*0Sstevel@tonic-gate 			((data.member = (char **)realloc(data.member,
2001*0Sstevel@tonic-gate 			(ctr + 1) * sizeof (char **))) == NULL)) {
2002*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
2003*0Sstevel@tonic-gate 			exit(1);
2004*0Sstevel@tonic-gate 		}
2005*0Sstevel@tonic-gate 		data.member[ctr-1] = alias;
2006*0Sstevel@tonic-gate 
2007*0Sstevel@tonic-gate 	} while (t = strtok(NULL, ","));
2008*0Sstevel@tonic-gate 
2009*0Sstevel@tonic-gate 	data.member[ctr] = NULL;
2010*0Sstevel@tonic-gate 
2011*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
2012*0Sstevel@tonic-gate 		(void) fprintf(stdout,
2013*0Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.alias);
2014*0Sstevel@tonic-gate 
2015*0Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
2016*0Sstevel@tonic-gate 
2017*0Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
2018*0Sstevel@tonic-gate 		if (continue_onerror)
2019*0Sstevel@tonic-gate 			(void) fprintf(stderr,
2020*0Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
2021*0Sstevel@tonic-gate 			data.alias);
2022*0Sstevel@tonic-gate 		else {
2023*0Sstevel@tonic-gate 			rc = GENENT_CBERR;
2024*0Sstevel@tonic-gate 			(void) fprintf(stderr,
2025*0Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
2026*0Sstevel@tonic-gate 				data.alias);
2027*0Sstevel@tonic-gate 		}
2028*0Sstevel@tonic-gate 	} else if (retval)
2029*0Sstevel@tonic-gate 		rc = GENENT_CBERR;
2030*0Sstevel@tonic-gate 
2031*0Sstevel@tonic-gate 	free(data.alias);
2032*0Sstevel@tonic-gate 	i = 0;
2033*0Sstevel@tonic-gate 	while (data.member[i])
2034*0Sstevel@tonic-gate 		free(data.member[i++]);
2035*0Sstevel@tonic-gate 	free(data.member);
2036*0Sstevel@tonic-gate 
2037*0Sstevel@tonic-gate 	return (rc);
2038*0Sstevel@tonic-gate }
2039*0Sstevel@tonic-gate 
2040*0Sstevel@tonic-gate 
2041*0Sstevel@tonic-gate static void
2042*0Sstevel@tonic-gate dump_aliases(ns_ldap_result_t *res)
2043*0Sstevel@tonic-gate {
2044*0Sstevel@tonic-gate 
2045*0Sstevel@tonic-gate 	char	**value = NULL;
2046*0Sstevel@tonic-gate 	int 		attr_count = 0;
2047*0Sstevel@tonic-gate 
2048*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "mail");
2049*0Sstevel@tonic-gate 	if (value && value[0])
2050*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
2051*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "mgrpRFC822MailMember");
2052*0Sstevel@tonic-gate 	if (value != NULL)
2053*0Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
2054*0Sstevel@tonic-gate 			(void) fprintf(stdout, "%s,", value[attr_count]);
2055*0Sstevel@tonic-gate 			attr_count++;
2056*0Sstevel@tonic-gate 		}
2057*0Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
2058*0Sstevel@tonic-gate 
2059*0Sstevel@tonic-gate }
2060*0Sstevel@tonic-gate 
2061*0Sstevel@tonic-gate /*
2062*0Sstevel@tonic-gate  * /etc/publickey
2063*0Sstevel@tonic-gate  */
2064*0Sstevel@tonic-gate 
2065*0Sstevel@tonic-gate static int
2066*0Sstevel@tonic-gate genent_publickey(char *line, int (*cback)())
2067*0Sstevel@tonic-gate {
2068*0Sstevel@tonic-gate 	char buf[BUFSIZ+1], tmpbuf[BUFSIZ+1], cname[BUFSIZ+1];
2069*0Sstevel@tonic-gate 	char *t, *p, *tmppubkey, *tmpprivkey;
2070*0Sstevel@tonic-gate 	entry_col ecol[3];
2071*0Sstevel@tonic-gate 	int buflen, uid, retval = 1;
2072*0Sstevel@tonic-gate 	struct passwd *pwd;
2073*0Sstevel@tonic-gate 	char auth_type[BUFSIZ+1];
2074*0Sstevel@tonic-gate 	keylen_t keylen;
2075*0Sstevel@tonic-gate 	algtype_t algtype;
2076*0Sstevel@tonic-gate 	struct _ns_pubkey data;
2077*0Sstevel@tonic-gate 	struct hostent *hp;
2078*0Sstevel@tonic-gate 	struct in_addr in;
2079*0Sstevel@tonic-gate 
2080*0Sstevel@tonic-gate 	/*
2081*0Sstevel@tonic-gate 	 * don't clobber our argument
2082*0Sstevel@tonic-gate 	 */
2083*0Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
2084*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
2085*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2086*0Sstevel@tonic-gate 	}
2087*0Sstevel@tonic-gate 	(void) strcpy(buf, line);
2088*0Sstevel@tonic-gate 
2089*0Sstevel@tonic-gate 	/*
2090*0Sstevel@tonic-gate 	 * clear column data
2091*0Sstevel@tonic-gate 	 */
2092*0Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
2093*0Sstevel@tonic-gate 
2094*0Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
2095*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no cname");
2096*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2097*0Sstevel@tonic-gate 	}
2098*0Sstevel@tonic-gate 
2099*0Sstevel@tonic-gate 	/*
2100*0Sstevel@tonic-gate 	 * Special case:  /etc/publickey usually has an entry
2101*0Sstevel@tonic-gate 	 * for principal "nobody".  We skip it.
2102*0Sstevel@tonic-gate 	 */
2103*0Sstevel@tonic-gate 	if (strcmp(t, "nobody") == 0)
2104*0Sstevel@tonic-gate 		return (GENENT_OK);
2105*0Sstevel@tonic-gate 
2106*0Sstevel@tonic-gate 	/*
2107*0Sstevel@tonic-gate 	 * cname (col 0)
2108*0Sstevel@tonic-gate 	 */
2109*0Sstevel@tonic-gate 	if (strncmp(t, "unix.", 5)) {
2110*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "bad cname");
2111*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2112*0Sstevel@tonic-gate 	}
2113*0Sstevel@tonic-gate 	(void) strcpy(tmpbuf, &(t[5]));
2114*0Sstevel@tonic-gate 	if ((p = strchr(tmpbuf, '@')) == 0) {
2115*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "bad cname");
2116*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2117*0Sstevel@tonic-gate 	}
2118*0Sstevel@tonic-gate 	*(p++) = 0;
2119*0Sstevel@tonic-gate 	if (isdigit(*tmpbuf)) {
2120*0Sstevel@tonic-gate 
2121*0Sstevel@tonic-gate 		uid = atoi(tmpbuf);
2122*0Sstevel@tonic-gate 		/*
2123*0Sstevel@tonic-gate 		 * don't generate entries for uids without passwd entries
2124*0Sstevel@tonic-gate 		 */
2125*0Sstevel@tonic-gate 		if ((pwd = getpwuid(uid)) == 0) {
2126*0Sstevel@tonic-gate 			(void) fprintf(stderr,
2127*0Sstevel@tonic-gate 			gettext("can't map uid %d to username, skipping\n"),
2128*0Sstevel@tonic-gate 				uid);
2129*0Sstevel@tonic-gate 			return (GENENT_OK);
2130*0Sstevel@tonic-gate 		}
2131*0Sstevel@tonic-gate 		(void) strcpy(cname, pwd->pw_name);
2132*0Sstevel@tonic-gate 		data.hostcred = NS_HOSTCRED_FALSE;
2133*0Sstevel@tonic-gate 	} else {
2134*0Sstevel@tonic-gate 		if ((hp = gethostbyname(tmpbuf)) == 0) {
2135*0Sstevel@tonic-gate 			(void) fprintf(stderr,
2136*0Sstevel@tonic-gate 		gettext("can't map hostname %s to hostaddress, skipping\n"),
2137*0Sstevel@tonic-gate 			tmpbuf);
2138*0Sstevel@tonic-gate 			return (GENENT_OK);
2139*0Sstevel@tonic-gate 		}
2140*0Sstevel@tonic-gate 		(void) memcpy((char *)&in.s_addr, hp->h_addr_list[0],
2141*0Sstevel@tonic-gate 		    sizeof (in));
2142*0Sstevel@tonic-gate 		data.hostcred = NS_HOSTCRED_TRUE;
2143*0Sstevel@tonic-gate 		(void) snprintf(cname, sizeof (cname),
2144*0Sstevel@tonic-gate 		    "%s+ipHostNumber=%s", tmpbuf, inet_ntoa(in));
2145*0Sstevel@tonic-gate 	}
2146*0Sstevel@tonic-gate 
2147*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = cname;
2148*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(cname)+1;
2149*0Sstevel@tonic-gate 
2150*0Sstevel@tonic-gate 	/*
2151*0Sstevel@tonic-gate 	 * public_data (col 1)
2152*0Sstevel@tonic-gate 	 */
2153*0Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
2154*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no private_data");
2155*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2156*0Sstevel@tonic-gate 	}
2157*0Sstevel@tonic-gate 	if ((p = strchr(t, ':')) == 0) {
2158*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "bad public_data");
2159*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2160*0Sstevel@tonic-gate 	}
2161*0Sstevel@tonic-gate 	*(p++) = 0;
2162*0Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
2163*0Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
2164*0Sstevel@tonic-gate 	keylen = (strlen(t) / 2) * 8;
2165*0Sstevel@tonic-gate 
2166*0Sstevel@tonic-gate 	/*
2167*0Sstevel@tonic-gate 	 * private_data (col 2) and algtype extraction
2168*0Sstevel@tonic-gate 	 */
2169*0Sstevel@tonic-gate 	if (*p == ':')
2170*0Sstevel@tonic-gate 		p++;
2171*0Sstevel@tonic-gate 	t = p;
2172*0Sstevel@tonic-gate 	if (!(t = strchr(t, ':'))) {
2173*0Sstevel@tonic-gate 		(void) fprintf(stderr,
2174*0Sstevel@tonic-gate 		gettext("WARNING: No algorithm type data found "
2175*0Sstevel@tonic-gate 			"in publickey file, assuming 0\n"));
2176*0Sstevel@tonic-gate 		algtype = 0;
2177*0Sstevel@tonic-gate 	} else {
2178*0Sstevel@tonic-gate 		*t = '\0';
2179*0Sstevel@tonic-gate 		t++;
2180*0Sstevel@tonic-gate 		algtype = atoi(t);
2181*0Sstevel@tonic-gate 	}
2182*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = p;
2183*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(p)+1;
2184*0Sstevel@tonic-gate 
2185*0Sstevel@tonic-gate 	/*
2186*0Sstevel@tonic-gate 	 * auth_type (col 1)
2187*0Sstevel@tonic-gate 	 */
2188*0Sstevel@tonic-gate 	if (!(__nis_keyalg2authtype(keylen, algtype, auth_type,
2189*0Sstevel@tonic-gate 						MECH_MAXATNAME))) {
2190*0Sstevel@tonic-gate 		(void) fprintf(stderr,
2191*0Sstevel@tonic-gate 		gettext("Could not convert algorithm type to "
2192*0Sstevel@tonic-gate 			"corresponding auth type string\n"));
2193*0Sstevel@tonic-gate 		return (GENENT_ERR);
2194*0Sstevel@tonic-gate 	}
2195*0Sstevel@tonic-gate 
2196*0Sstevel@tonic-gate 	/*
2197*0Sstevel@tonic-gate 	 * build entry
2198*0Sstevel@tonic-gate 	 */
2199*0Sstevel@tonic-gate 	data.name = strdup(ecol[0].ec_value.ec_value_val);
2200*0Sstevel@tonic-gate 	if (data.name == NULL) {
2201*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
2202*0Sstevel@tonic-gate 		exit(1);
2203*0Sstevel@tonic-gate 	}
2204*0Sstevel@tonic-gate 
2205*0Sstevel@tonic-gate 	buflen = sizeof (auth_type) + strlen(ecol[1].ec_value.ec_value_val) + 3;
2206*0Sstevel@tonic-gate 	if ((tmppubkey = (char *)malloc(buflen)) == NULL) {
2207*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
2208*0Sstevel@tonic-gate 		exit(1);
2209*0Sstevel@tonic-gate 	}
2210*0Sstevel@tonic-gate 	(void) snprintf(tmppubkey, buflen, "{%s}%s", auth_type,
2211*0Sstevel@tonic-gate 	    ecol[1].ec_value.ec_value_val);
2212*0Sstevel@tonic-gate 	data.pubkey = tmppubkey;
2213*0Sstevel@tonic-gate 
2214*0Sstevel@tonic-gate 	buflen = sizeof (auth_type) + strlen(ecol[2].ec_value.ec_value_val) + 3;
2215*0Sstevel@tonic-gate 	if ((tmpprivkey = (char *)malloc(buflen)) == NULL) {
2216*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
2217*0Sstevel@tonic-gate 		exit(1);
2218*0Sstevel@tonic-gate 	}
2219*0Sstevel@tonic-gate 
2220*0Sstevel@tonic-gate 	(void) snprintf(tmpprivkey, buflen, "{%s}%s", auth_type,
2221*0Sstevel@tonic-gate 	    ecol[2].ec_value.ec_value_val);
2222*0Sstevel@tonic-gate 	data.privkey = tmpprivkey;
2223*0Sstevel@tonic-gate 
2224*0Sstevel@tonic-gate 	retval = (*cback)(&data, 1);
2225*0Sstevel@tonic-gate 
2226*0Sstevel@tonic-gate 	if ((retval != NS_LDAP_SUCCESS) && (continue_onerror == 0))
2227*0Sstevel@tonic-gate 		return (GENENT_CBERR);
2228*0Sstevel@tonic-gate 	else {
2229*0Sstevel@tonic-gate 		free(data.name);
2230*0Sstevel@tonic-gate 		free(data.pubkey);
2231*0Sstevel@tonic-gate 		free(data.privkey);
2232*0Sstevel@tonic-gate 		return (GENENT_OK);
2233*0Sstevel@tonic-gate 	}
2234*0Sstevel@tonic-gate }
2235*0Sstevel@tonic-gate 
2236*0Sstevel@tonic-gate static void
2237*0Sstevel@tonic-gate dump_publickey(ns_ldap_result_t *res, char *container)
2238*0Sstevel@tonic-gate {
2239*0Sstevel@tonic-gate 	char	**value = NULL;
2240*0Sstevel@tonic-gate 	char	buf[BUFSIZ];
2241*0Sstevel@tonic-gate 	char	domainname[BUFSIZ];
2242*0Sstevel@tonic-gate 	char	*pubptr, *prvptr;
2243*0Sstevel@tonic-gate 
2244*0Sstevel@tonic-gate 	if (res == NULL)
2245*0Sstevel@tonic-gate 		return;
2246*0Sstevel@tonic-gate 
2247*0Sstevel@tonic-gate 	if (sysinfo(SI_SRPC_DOMAIN, domainname, BUFSIZ) < 0) {
2248*0Sstevel@tonic-gate 		(void) fprintf(stderr,
2249*0Sstevel@tonic-gate 			gettext("could not obtain domainname\n"));
2250*0Sstevel@tonic-gate 		exit(1);
2251*0Sstevel@tonic-gate 	}
2252*0Sstevel@tonic-gate 
2253*0Sstevel@tonic-gate 	/*
2254*0Sstevel@tonic-gate 	 * Retrieve all the attributes, but don't print
2255*0Sstevel@tonic-gate 	 * until we have all the required ones.
2256*0Sstevel@tonic-gate 	 */
2257*0Sstevel@tonic-gate 
2258*0Sstevel@tonic-gate 	if (strcmp(container, "passwd") == 0)
2259*0Sstevel@tonic-gate 		value = __ns_ldap_getAttr(res->entry, "uidNumber");
2260*0Sstevel@tonic-gate 	else
2261*0Sstevel@tonic-gate 		value = __ns_ldap_getAttr(res->entry, "cn");
2262*0Sstevel@tonic-gate 
2263*0Sstevel@tonic-gate 	if (value && value[0])
2264*0Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "unix.%s@%s",
2265*0Sstevel@tonic-gate 		    value[0], domainname);
2266*0Sstevel@tonic-gate 	else
2267*0Sstevel@tonic-gate 		return;
2268*0Sstevel@tonic-gate 
2269*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "nisPublickey");
2270*0Sstevel@tonic-gate 	if (value != NULL && value[0] != NULL) {
2271*0Sstevel@tonic-gate 		if ((pubptr = strchr(value[0], '}')) == NULL)
2272*0Sstevel@tonic-gate 			return;
2273*0Sstevel@tonic-gate 	}
2274*0Sstevel@tonic-gate 
2275*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "nisSecretkey");
2276*0Sstevel@tonic-gate 	if (value != NULL && value[0] != NULL)
2277*0Sstevel@tonic-gate 		if ((prvptr = strchr(value[0], '}')) == NULL)
2278*0Sstevel@tonic-gate 			return;
2279*0Sstevel@tonic-gate 
2280*0Sstevel@tonic-gate 	/* print the attributes, algorithm type is always 0 */
2281*0Sstevel@tonic-gate 	(void) fprintf(stdout, "%s	%s:%s:0\n", buf, ++pubptr, ++prvptr);
2282*0Sstevel@tonic-gate }
2283*0Sstevel@tonic-gate 
2284*0Sstevel@tonic-gate 
2285*0Sstevel@tonic-gate 
2286*0Sstevel@tonic-gate /*
2287*0Sstevel@tonic-gate  * /etc/netmasks
2288*0Sstevel@tonic-gate  */
2289*0Sstevel@tonic-gate 
2290*0Sstevel@tonic-gate static int
2291*0Sstevel@tonic-gate genent_netmasks(char *line, int (*cback)())
2292*0Sstevel@tonic-gate {
2293*0Sstevel@tonic-gate 	char buf[BUFSIZ+1];
2294*0Sstevel@tonic-gate 	char *t;
2295*0Sstevel@tonic-gate 	entry_col ecol[3];
2296*0Sstevel@tonic-gate 
2297*0Sstevel@tonic-gate 	struct _ns_netmasks data;
2298*0Sstevel@tonic-gate 
2299*0Sstevel@tonic-gate 
2300*0Sstevel@tonic-gate 	/*
2301*0Sstevel@tonic-gate 	 * don't clobber our argument
2302*0Sstevel@tonic-gate 	 */
2303*0Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
2304*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
2305*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2306*0Sstevel@tonic-gate 	}
2307*0Sstevel@tonic-gate 	(void) strcpy(buf, line);
2308*0Sstevel@tonic-gate 
2309*0Sstevel@tonic-gate 	/*
2310*0Sstevel@tonic-gate 	 * clear column data
2311*0Sstevel@tonic-gate 	 */
2312*0Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
2313*0Sstevel@tonic-gate 
2314*0Sstevel@tonic-gate 	/*
2315*0Sstevel@tonic-gate 	 * comment (col 2)
2316*0Sstevel@tonic-gate 	 */
2317*0Sstevel@tonic-gate 	t = strchr(buf, '#');
2318*0Sstevel@tonic-gate 	if (t) {
2319*0Sstevel@tonic-gate 		*t++ = 0;
2320*0Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = t;
2321*0Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = strlen(t)+1;
2322*0Sstevel@tonic-gate 	} else {
2323*0Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = 0;
2324*0Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = 0;
2325*0Sstevel@tonic-gate 	}
2326*0Sstevel@tonic-gate 
2327*0Sstevel@tonic-gate 	/*
2328*0Sstevel@tonic-gate 	 * addr(col 0)
2329*0Sstevel@tonic-gate 	 */
2330*0Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
2331*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no mask");
2332*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2333*0Sstevel@tonic-gate 	}
2334*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
2335*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
2336*0Sstevel@tonic-gate 
2337*0Sstevel@tonic-gate 	/*
2338*0Sstevel@tonic-gate 	 * mask (col 1)
2339*0Sstevel@tonic-gate 	 */
2340*0Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
2341*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no mask");
2342*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2343*0Sstevel@tonic-gate 	}
2344*0Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
2345*0Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
2346*0Sstevel@tonic-gate 
2347*0Sstevel@tonic-gate 	/* build entry */
2348*0Sstevel@tonic-gate 	data.netnumber = ecol[0].ec_value.ec_value_val;
2349*0Sstevel@tonic-gate 	data.netmask = ecol[1].ec_value.ec_value_val;
2350*0Sstevel@tonic-gate 
2351*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
2352*0Sstevel@tonic-gate 		(void) fprintf(stdout,
2353*0Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.netnumber);
2354*0Sstevel@tonic-gate 
2355*0Sstevel@tonic-gate 	if ((*cback)(&data, 1) && continue_onerror == 0)
2356*0Sstevel@tonic-gate 		return (GENENT_CBERR);
2357*0Sstevel@tonic-gate 
2358*0Sstevel@tonic-gate 	return (GENENT_OK);
2359*0Sstevel@tonic-gate }
2360*0Sstevel@tonic-gate 
2361*0Sstevel@tonic-gate static void
2362*0Sstevel@tonic-gate dump_netmasks(ns_ldap_result_t *res)
2363*0Sstevel@tonic-gate {
2364*0Sstevel@tonic-gate 	char	**value = NULL;
2365*0Sstevel@tonic-gate 
2366*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "ipNetworkNumber");
2367*0Sstevel@tonic-gate 	if (value && value[0])
2368*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
2369*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "ipNetmaskNumber");
2370*0Sstevel@tonic-gate 	if (value && value[0])
2371*0Sstevel@tonic-gate 		(void) fprintf(stdout, "	%s\n", value[0]);
2372*0Sstevel@tonic-gate }
2373*0Sstevel@tonic-gate 
2374*0Sstevel@tonic-gate 
2375*0Sstevel@tonic-gate /*
2376*0Sstevel@tonic-gate  * /etc/netgroup
2377*0Sstevel@tonic-gate  * column data format is:
2378*0Sstevel@tonic-gate  *    col 0: netgroup name (or cname)
2379*0Sstevel@tonic-gate  *    col 1: netgroup member, if this is a triplet
2380*0Sstevel@tonic-gate  *    col 2: netgroup member, if not a triplet
2381*0Sstevel@tonic-gate  *    col 3: comment
2382*0Sstevel@tonic-gate  */
2383*0Sstevel@tonic-gate 
2384*0Sstevel@tonic-gate static int
2385*0Sstevel@tonic-gate genent_netgroup(char *line, int (*cback)())
2386*0Sstevel@tonic-gate {
2387*0Sstevel@tonic-gate 	char buf[BIGBUF+1];    /* netgroup entries tend to be big */
2388*0Sstevel@tonic-gate 	char *t;
2389*0Sstevel@tonic-gate 	char *cname = NULL;
2390*0Sstevel@tonic-gate 	entry_col ecol[4];
2391*0Sstevel@tonic-gate 	char *netg_tmp = NULL, *triplet_tmp = NULL;
2392*0Sstevel@tonic-gate 	int netgcount = 0, tripletcount = 0, retval = 1;
2393*0Sstevel@tonic-gate 	struct _ns_netgroups data;
2394*0Sstevel@tonic-gate 	int rc = GENENT_OK;
2395*0Sstevel@tonic-gate 
2396*0Sstevel@tonic-gate 	/* don't clobber our argument */
2397*0Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
2398*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
2399*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2400*0Sstevel@tonic-gate 	}
2401*0Sstevel@tonic-gate 	(void) strcpy(buf, line);
2402*0Sstevel@tonic-gate 
2403*0Sstevel@tonic-gate 	/* clear column data */
2404*0Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
2405*0Sstevel@tonic-gate 
2406*0Sstevel@tonic-gate 	/*
2407*0Sstevel@tonic-gate 	 * process 1st minimal entry, to validate that there is no
2408*0Sstevel@tonic-gate 	 * parsing error.
2409*0Sstevel@tonic-gate 	 * start with comment(col 3)
2410*0Sstevel@tonic-gate 	 */
2411*0Sstevel@tonic-gate 	t = strchr(buf, '#');
2412*0Sstevel@tonic-gate 	if (t) {
2413*0Sstevel@tonic-gate 		*t++ = 0;
2414*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
2415*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
2416*0Sstevel@tonic-gate 	} else {
2417*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = "";
2418*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
2419*0Sstevel@tonic-gate 	}
2420*0Sstevel@tonic-gate 
2421*0Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = NULL;
2422*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = NULL;
2423*0Sstevel@tonic-gate 
2424*0Sstevel@tonic-gate 	/* cname (col 0) */
2425*0Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
2426*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no cname");
2427*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2428*0Sstevel@tonic-gate 	}
2429*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
2430*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
2431*0Sstevel@tonic-gate 	cname = t;
2432*0Sstevel@tonic-gate 
2433*0Sstevel@tonic-gate 	/* addr(col 1 and 2) */
2434*0Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
2435*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no members for netgroup");
2436*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2437*0Sstevel@tonic-gate 	}
2438*0Sstevel@tonic-gate 
2439*0Sstevel@tonic-gate 	if (*t == '(') {
2440*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
2441*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
2442*0Sstevel@tonic-gate 	} else {
2443*0Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = t;
2444*0Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = strlen(t)+1;
2445*0Sstevel@tonic-gate 	}
2446*0Sstevel@tonic-gate 
2447*0Sstevel@tonic-gate 
2448*0Sstevel@tonic-gate 	/*
2449*0Sstevel@tonic-gate 	 * now build entry.
2450*0Sstevel@tonic-gate 	 * start by clearing entry data
2451*0Sstevel@tonic-gate 	 */
2452*0Sstevel@tonic-gate 	(void) memset((struct _ns_netgroups *)&data, 0, sizeof (data));
2453*0Sstevel@tonic-gate 
2454*0Sstevel@tonic-gate 	data.name = strdup(ecol[0].ec_value.ec_value_val);
2455*0Sstevel@tonic-gate 
2456*0Sstevel@tonic-gate 	if (ecol[1].ec_value.ec_value_val != NULL) {
2457*0Sstevel@tonic-gate 		if ((data.triplet = calloc(1, sizeof (char **))) == NULL) {
2458*0Sstevel@tonic-gate 				(void) fprintf(stderr,
2459*0Sstevel@tonic-gate 					gettext("out of memory\n"));
2460*0Sstevel@tonic-gate 				exit(1);
2461*0Sstevel@tonic-gate 		}
2462*0Sstevel@tonic-gate 		data.triplet[tripletcount++] =
2463*0Sstevel@tonic-gate 		    strdup(ecol[1].ec_value.ec_value_val);
2464*0Sstevel@tonic-gate 	} else if (ecol[2].ec_value.ec_value_val != NULL) {
2465*0Sstevel@tonic-gate 			if ((data.netgroup = calloc(1, sizeof (char **)))
2466*0Sstevel@tonic-gate 			    == NULL) {
2467*0Sstevel@tonic-gate 					(void) fprintf(stderr,
2468*0Sstevel@tonic-gate 				    gettext("out of memory\n"));
2469*0Sstevel@tonic-gate 					exit(1);
2470*0Sstevel@tonic-gate 			}
2471*0Sstevel@tonic-gate 			data.netgroup[netgcount++] =
2472*0Sstevel@tonic-gate 			    strdup(ecol[2].ec_value.ec_value_val);
2473*0Sstevel@tonic-gate 	}
2474*0Sstevel@tonic-gate 
2475*0Sstevel@tonic-gate 	/*
2476*0Sstevel@tonic-gate 	 * we now have a valid entry (at least 1 netgroup name and
2477*0Sstevel@tonic-gate 	 * 1 netgroup member), proceed with the rest of the line
2478*0Sstevel@tonic-gate 	 */
2479*0Sstevel@tonic-gate 	while (t = strtok(NULL, " \t")) {
2480*0Sstevel@tonic-gate 
2481*0Sstevel@tonic-gate 		/* if next token is equal to netgroup name, ignore */
2482*0Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
2483*0Sstevel@tonic-gate 			continue;
2484*0Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
2485*0Sstevel@tonic-gate 			continue;
2486*0Sstevel@tonic-gate 
2487*0Sstevel@tonic-gate 		if (*t == '(') {
2488*0Sstevel@tonic-gate 			tripletcount++;
2489*0Sstevel@tonic-gate 			triplet_tmp = strdup(t);
2490*0Sstevel@tonic-gate 			if ((data.triplet = (char **)realloc(data.triplet,
2491*0Sstevel@tonic-gate 				tripletcount * sizeof (char **))) == NULL) {
2492*0Sstevel@tonic-gate 				(void) fprintf(stderr,
2493*0Sstevel@tonic-gate 					gettext("out of memory\n"));
2494*0Sstevel@tonic-gate 				exit(1);
2495*0Sstevel@tonic-gate 			}
2496*0Sstevel@tonic-gate 			data.triplet[tripletcount-1] = triplet_tmp;
2497*0Sstevel@tonic-gate 		} else {
2498*0Sstevel@tonic-gate 			netgcount++;
2499*0Sstevel@tonic-gate 			netg_tmp = strdup(t);
2500*0Sstevel@tonic-gate 			if ((data.netgroup = (char **)realloc(data.netgroup,
2501*0Sstevel@tonic-gate 				netgcount * sizeof (char **))) == NULL) {
2502*0Sstevel@tonic-gate 				(void) fprintf(stderr,
2503*0Sstevel@tonic-gate 				gettext("out of memory\n"));
2504*0Sstevel@tonic-gate 				exit(1);
2505*0Sstevel@tonic-gate 			}
2506*0Sstevel@tonic-gate 			data.netgroup[netgcount-1] = netg_tmp;
2507*0Sstevel@tonic-gate 		}
2508*0Sstevel@tonic-gate 	}
2509*0Sstevel@tonic-gate 
2510*0Sstevel@tonic-gate 
2511*0Sstevel@tonic-gate 	/* End the list with NULL */
2512*0Sstevel@tonic-gate 	if ((data.triplet = (char **)realloc(data.triplet,
2513*0Sstevel@tonic-gate 		(tripletcount + 1) * sizeof (char **))) == NULL) {
2514*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
2515*0Sstevel@tonic-gate 		exit(1);
2516*0Sstevel@tonic-gate 	}
2517*0Sstevel@tonic-gate 	data.triplet[tripletcount] = NULL;
2518*0Sstevel@tonic-gate 	if ((data.netgroup = (char **)realloc(data.netgroup,
2519*0Sstevel@tonic-gate 		(netgcount + 1) * sizeof (char **))) == NULL) {
2520*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
2521*0Sstevel@tonic-gate 		exit(1);
2522*0Sstevel@tonic-gate 	}
2523*0Sstevel@tonic-gate 	data.netgroup[netgcount] = NULL;
2524*0Sstevel@tonic-gate 
2525*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
2526*0Sstevel@tonic-gate 		(void) fprintf(stdout,
2527*0Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.name);
2528*0Sstevel@tonic-gate 
2529*0Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
2530*0Sstevel@tonic-gate 
2531*0Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
2532*0Sstevel@tonic-gate 		if (continue_onerror)
2533*0Sstevel@tonic-gate 			(void) fprintf(stderr,
2534*0Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
2535*0Sstevel@tonic-gate 			data.name);
2536*0Sstevel@tonic-gate 		else {
2537*0Sstevel@tonic-gate 			rc = GENENT_CBERR;
2538*0Sstevel@tonic-gate 			(void) fprintf(stderr,
2539*0Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
2540*0Sstevel@tonic-gate 				data.name);
2541*0Sstevel@tonic-gate 		}
2542*0Sstevel@tonic-gate 	} else if (retval)
2543*0Sstevel@tonic-gate 		rc = GENENT_CBERR;
2544*0Sstevel@tonic-gate 
2545*0Sstevel@tonic-gate 	free(data.name);
2546*0Sstevel@tonic-gate 	free(data.triplet);
2547*0Sstevel@tonic-gate 	free(data.netgroup);
2548*0Sstevel@tonic-gate 
2549*0Sstevel@tonic-gate 	return (rc);
2550*0Sstevel@tonic-gate }
2551*0Sstevel@tonic-gate 
2552*0Sstevel@tonic-gate static void
2553*0Sstevel@tonic-gate dump_netgroup(ns_ldap_result_t *res)
2554*0Sstevel@tonic-gate {
2555*0Sstevel@tonic-gate 	char	**value = NULL;
2556*0Sstevel@tonic-gate 	int	attr_count = 0;
2557*0Sstevel@tonic-gate 
2558*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
2559*0Sstevel@tonic-gate 	if ((value != NULL) && (value[0] != NULL))
2560*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
2561*0Sstevel@tonic-gate 	else
2562*0Sstevel@tonic-gate 		return;
2563*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "nisNetgroupTriple");
2564*0Sstevel@tonic-gate 	if (value != NULL)
2565*0Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
2566*0Sstevel@tonic-gate 			(void) fprintf(stdout, " %s", value[attr_count]);
2567*0Sstevel@tonic-gate 			attr_count++;
2568*0Sstevel@tonic-gate 		}
2569*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "memberNisNetgroup");
2570*0Sstevel@tonic-gate 	if (value != NULL)
2571*0Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
2572*0Sstevel@tonic-gate 			(void) fprintf(stdout, " %s", value[attr_count]);
2573*0Sstevel@tonic-gate 			attr_count++;
2574*0Sstevel@tonic-gate 		}
2575*0Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
2576*0Sstevel@tonic-gate 
2577*0Sstevel@tonic-gate }
2578*0Sstevel@tonic-gate 
2579*0Sstevel@tonic-gate static int
2580*0Sstevel@tonic-gate genent_automount(char *line, int (*cback)())
2581*0Sstevel@tonic-gate {
2582*0Sstevel@tonic-gate 	char buf[BUFSIZ+1];
2583*0Sstevel@tonic-gate 	char *t, *s;
2584*0Sstevel@tonic-gate 	entry_col ecol[2];
2585*0Sstevel@tonic-gate 	struct _ns_automount data;
2586*0Sstevel@tonic-gate 	int retval = 1;
2587*0Sstevel@tonic-gate 	int rc = GENENT_OK;
2588*0Sstevel@tonic-gate 
2589*0Sstevel@tonic-gate 	/*
2590*0Sstevel@tonic-gate 	 * don't clobber our argument
2591*0Sstevel@tonic-gate 	 */
2592*0Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
2593*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
2594*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2595*0Sstevel@tonic-gate 		}
2596*0Sstevel@tonic-gate 
2597*0Sstevel@tonic-gate 	/* replace every tabspace with single space */
2598*0Sstevel@tonic-gate 	replace_tab2space(line);
2599*0Sstevel@tonic-gate 	(void) strcpy(buf, line);
2600*0Sstevel@tonic-gate 
2601*0Sstevel@tonic-gate 	/*
2602*0Sstevel@tonic-gate 	 * clear column data
2603*0Sstevel@tonic-gate 	 */
2604*0Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
2605*0Sstevel@tonic-gate 
2606*0Sstevel@tonic-gate 	/*
2607*0Sstevel@tonic-gate 	 * key (col 0)
2608*0Sstevel@tonic-gate 	 */
2609*0Sstevel@tonic-gate 	t = buf;
2610*0Sstevel@tonic-gate 	while (t[0] == ' ')
2611*0Sstevel@tonic-gate 		t++;
2612*0Sstevel@tonic-gate 
2613*0Sstevel@tonic-gate 	if ((s = strchr(t, ' ')) == 0) {
2614*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2615*0Sstevel@tonic-gate 	}
2616*0Sstevel@tonic-gate 	*s++ = 0;
2617*0Sstevel@tonic-gate 
2618*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
2619*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
2620*0Sstevel@tonic-gate 	t = s;
2621*0Sstevel@tonic-gate 
2622*0Sstevel@tonic-gate 	while (t[0] == ' ')
2623*0Sstevel@tonic-gate 		t++;
2624*0Sstevel@tonic-gate 
2625*0Sstevel@tonic-gate 	/*
2626*0Sstevel@tonic-gate 	 * mapentry (col 1)
2627*0Sstevel@tonic-gate 	 */
2628*0Sstevel@tonic-gate 
2629*0Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
2630*0Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
2631*0Sstevel@tonic-gate 
2632*0Sstevel@tonic-gate 	data.mapname = strdup(databasetype);
2633*0Sstevel@tonic-gate 	data.key = strdup(ecol[0].ec_value.ec_value_val);
2634*0Sstevel@tonic-gate 	data.value = strdup(ecol[1].ec_value.ec_value_val);
2635*0Sstevel@tonic-gate 
2636*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
2637*0Sstevel@tonic-gate 		(void) fprintf(stdout,
2638*0Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.key);
2639*0Sstevel@tonic-gate 
2640*0Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
2641*0Sstevel@tonic-gate 
2642*0Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
2643*0Sstevel@tonic-gate 		if (continue_onerror)
2644*0Sstevel@tonic-gate 			(void) fprintf(stderr,
2645*0Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
2646*0Sstevel@tonic-gate 			data.key);
2647*0Sstevel@tonic-gate 		else {
2648*0Sstevel@tonic-gate 			rc = GENENT_CBERR;
2649*0Sstevel@tonic-gate 			(void) fprintf(stderr,
2650*0Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
2651*0Sstevel@tonic-gate 				data.key);
2652*0Sstevel@tonic-gate 		}
2653*0Sstevel@tonic-gate 	} else if (retval)
2654*0Sstevel@tonic-gate 		rc = GENENT_CBERR;
2655*0Sstevel@tonic-gate 
2656*0Sstevel@tonic-gate 	free(data.mapname);
2657*0Sstevel@tonic-gate 	free(data.key);
2658*0Sstevel@tonic-gate 	free(data.value);
2659*0Sstevel@tonic-gate 	return (rc);
2660*0Sstevel@tonic-gate }
2661*0Sstevel@tonic-gate 
2662*0Sstevel@tonic-gate static void
2663*0Sstevel@tonic-gate dump_automount(ns_ldap_result_t *res)
2664*0Sstevel@tonic-gate {
2665*0Sstevel@tonic-gate 	char	**value = NULL;
2666*0Sstevel@tonic-gate 
2667*0Sstevel@tonic-gate 	if (res == NULL)
2668*0Sstevel@tonic-gate 		return;
2669*0Sstevel@tonic-gate 
2670*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "automountKey");
2671*0Sstevel@tonic-gate 	if (value != NULL) {
2672*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
2673*0Sstevel@tonic-gate 		value = __ns_ldap_getAttr(res->entry, "automountInformation");
2674*0Sstevel@tonic-gate 		if (value != NULL)
2675*0Sstevel@tonic-gate 			(void) fprintf(stdout, "	%s\n", value[0]);
2676*0Sstevel@tonic-gate 		else
2677*0Sstevel@tonic-gate 			(void) fprintf(stdout, "\n");
2678*0Sstevel@tonic-gate 	}
2679*0Sstevel@tonic-gate }
2680*0Sstevel@tonic-gate 
2681*0Sstevel@tonic-gate 
2682*0Sstevel@tonic-gate /*
2683*0Sstevel@tonic-gate  * /etc/passwd
2684*0Sstevel@tonic-gate  *
2685*0Sstevel@tonic-gate  */
2686*0Sstevel@tonic-gate 
2687*0Sstevel@tonic-gate static int
2688*0Sstevel@tonic-gate genent_passwd(char *line, int (*cback)())
2689*0Sstevel@tonic-gate {
2690*0Sstevel@tonic-gate 	char buf[BUFSIZ+1];
2691*0Sstevel@tonic-gate 	char *s, *t;
2692*0Sstevel@tonic-gate 	entry_col ecol[8];
2693*0Sstevel@tonic-gate 	int retval = 1;
2694*0Sstevel@tonic-gate 	char pname[BUFSIZ];
2695*0Sstevel@tonic-gate 
2696*0Sstevel@tonic-gate 	struct passwd	data;
2697*0Sstevel@tonic-gate 	int rc = GENENT_OK;
2698*0Sstevel@tonic-gate 
2699*0Sstevel@tonic-gate 
2700*0Sstevel@tonic-gate 	/*
2701*0Sstevel@tonic-gate 	 * don't clobber our argument
2702*0Sstevel@tonic-gate 	 */
2703*0Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
2704*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
2705*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2706*0Sstevel@tonic-gate 	}
2707*0Sstevel@tonic-gate 	(void) strcpy(buf, line);
2708*0Sstevel@tonic-gate 	t = buf;
2709*0Sstevel@tonic-gate 
2710*0Sstevel@tonic-gate 	/* ignore empty entries */
2711*0Sstevel@tonic-gate 	if (*t == '\0')
2712*0Sstevel@tonic-gate 		return (GENENT_OK);
2713*0Sstevel@tonic-gate 
2714*0Sstevel@tonic-gate 	/*
2715*0Sstevel@tonic-gate 	 * clear column data
2716*0Sstevel@tonic-gate 	 */
2717*0Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
2718*0Sstevel@tonic-gate 
2719*0Sstevel@tonic-gate 	/*
2720*0Sstevel@tonic-gate 	 * name (col 0)
2721*0Sstevel@tonic-gate 	 */
2722*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
2723*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no password");
2724*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2725*0Sstevel@tonic-gate 	}
2726*0Sstevel@tonic-gate 	*s++ = 0;
2727*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
2728*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
2729*0Sstevel@tonic-gate 	t = s;
2730*0Sstevel@tonic-gate 
2731*0Sstevel@tonic-gate 	/*
2732*0Sstevel@tonic-gate 	 * passwd (col 1)
2733*0Sstevel@tonic-gate 	 */
2734*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
2735*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no uid");
2736*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2737*0Sstevel@tonic-gate 	}
2738*0Sstevel@tonic-gate 	*s++ = 0;
2739*0Sstevel@tonic-gate 
2740*0Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
2741*0Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
2742*0Sstevel@tonic-gate 
2743*0Sstevel@tonic-gate 	t = s;
2744*0Sstevel@tonic-gate 
2745*0Sstevel@tonic-gate 	/*
2746*0Sstevel@tonic-gate 	 * uid (col 2)
2747*0Sstevel@tonic-gate 	 */
2748*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0 || s == t) {
2749*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no gid");
2750*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2751*0Sstevel@tonic-gate 	}
2752*0Sstevel@tonic-gate 	*s++ = 0;
2753*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
2754*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
2755*0Sstevel@tonic-gate 	t = s;
2756*0Sstevel@tonic-gate 
2757*0Sstevel@tonic-gate 	/*
2758*0Sstevel@tonic-gate 	 * gid (col 3)
2759*0Sstevel@tonic-gate 	 */
2760*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0 || s == t) {
2761*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no gcos");
2762*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2763*0Sstevel@tonic-gate 	}
2764*0Sstevel@tonic-gate 	*s++ = 0;
2765*0Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
2766*0Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
2767*0Sstevel@tonic-gate 	t = s;
2768*0Sstevel@tonic-gate 
2769*0Sstevel@tonic-gate 	/*
2770*0Sstevel@tonic-gate 	 * gcos (col 4)
2771*0Sstevel@tonic-gate 	 */
2772*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
2773*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no home");
2774*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2775*0Sstevel@tonic-gate 	}
2776*0Sstevel@tonic-gate 	*s++ = 0;
2777*0Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_val = t;
2778*0Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_len = strlen(t)+1;
2779*0Sstevel@tonic-gate 	t = s;
2780*0Sstevel@tonic-gate 
2781*0Sstevel@tonic-gate 	/*
2782*0Sstevel@tonic-gate 	 * home (col 5)
2783*0Sstevel@tonic-gate 	 */
2784*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
2785*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no shell");
2786*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2787*0Sstevel@tonic-gate 	}
2788*0Sstevel@tonic-gate 	*s++ = 0;
2789*0Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_val = t;
2790*0Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_len = strlen(t)+1;
2791*0Sstevel@tonic-gate 	t = s;
2792*0Sstevel@tonic-gate 
2793*0Sstevel@tonic-gate 	/*
2794*0Sstevel@tonic-gate 	 * shell (col 6)
2795*0Sstevel@tonic-gate 	 */
2796*0Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_val = t;
2797*0Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_len = strlen(t)+1;
2798*0Sstevel@tonic-gate 
2799*0Sstevel@tonic-gate 	/*
2800*0Sstevel@tonic-gate 	 * build entry
2801*0Sstevel@tonic-gate 	 */
2802*0Sstevel@tonic-gate 	data.pw_name = strdup(ecol[0].ec_value.ec_value_val);
2803*0Sstevel@tonic-gate 
2804*0Sstevel@tonic-gate 	if (flags & F_PASSWD) {
2805*0Sstevel@tonic-gate 		/* Add {crypt} before passwd entry */
2806*0Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname), "{crypt}%s",
2807*0Sstevel@tonic-gate 		    ecol[1].ec_value.ec_value_val);
2808*0Sstevel@tonic-gate 		data.pw_passwd = strdup(pname);
2809*0Sstevel@tonic-gate 	}
2810*0Sstevel@tonic-gate 	else
2811*0Sstevel@tonic-gate 		data.pw_passwd = NULL;
2812*0Sstevel@tonic-gate 
2813*0Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
2814*0Sstevel@tonic-gate 	    ecol[2].ec_value.ec_value_val[0] != '\0') {
2815*0Sstevel@tonic-gate 		data.pw_uid = ascii_to_int(ecol[2].ec_value.ec_value_val);
2816*0Sstevel@tonic-gate 		if (data.pw_uid == -1) {
2817*0Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
2818*0Sstevel@tonic-gate 			    "invalid uid : %s", ecol[2].ec_value.ec_value_val);
2819*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2820*0Sstevel@tonic-gate 		}
2821*0Sstevel@tonic-gate 	} else
2822*0Sstevel@tonic-gate 		data.pw_uid = -1;
2823*0Sstevel@tonic-gate 
2824*0Sstevel@tonic-gate 	if (ecol[3].ec_value.ec_value_val != NULL &&
2825*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val[0] != '\0') {
2826*0Sstevel@tonic-gate 
2827*0Sstevel@tonic-gate 		data.pw_gid = ascii_to_int(ecol[3].ec_value.ec_value_val);
2828*0Sstevel@tonic-gate 		if (data.pw_gid == -1) {
2829*0Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
2830*0Sstevel@tonic-gate 			    "invalid gid : %s", ecol[3].ec_value.ec_value_val);
2831*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2832*0Sstevel@tonic-gate 		}
2833*0Sstevel@tonic-gate 	} else
2834*0Sstevel@tonic-gate 		data.pw_gid = -1;
2835*0Sstevel@tonic-gate 
2836*0Sstevel@tonic-gate 	data.pw_age = NULL;
2837*0Sstevel@tonic-gate 	data.pw_comment = NULL;
2838*0Sstevel@tonic-gate 	data.pw_gecos = strdup(ecol[4].ec_value.ec_value_val);
2839*0Sstevel@tonic-gate 	data.pw_dir = strdup(ecol[5].ec_value.ec_value_val);
2840*0Sstevel@tonic-gate 	data.pw_shell = strdup(ecol[6].ec_value.ec_value_val);
2841*0Sstevel@tonic-gate 
2842*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
2843*0Sstevel@tonic-gate 		(void) fprintf(stdout,
2844*0Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.pw_name);
2845*0Sstevel@tonic-gate 
2846*0Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
2847*0Sstevel@tonic-gate 
2848*0Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
2849*0Sstevel@tonic-gate 		if (continue_onerror)
2850*0Sstevel@tonic-gate 			(void) fprintf(stderr,
2851*0Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
2852*0Sstevel@tonic-gate 			data.pw_name);
2853*0Sstevel@tonic-gate 		else {
2854*0Sstevel@tonic-gate 			rc = GENENT_CBERR;
2855*0Sstevel@tonic-gate 			(void) fprintf(stderr,
2856*0Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
2857*0Sstevel@tonic-gate 				data.pw_name);
2858*0Sstevel@tonic-gate 		}
2859*0Sstevel@tonic-gate 	} else if (retval)
2860*0Sstevel@tonic-gate 		rc = GENENT_CBERR;
2861*0Sstevel@tonic-gate 
2862*0Sstevel@tonic-gate 	free(data.pw_name);
2863*0Sstevel@tonic-gate 	free(data.pw_gecos);
2864*0Sstevel@tonic-gate 	free(data.pw_dir);
2865*0Sstevel@tonic-gate 	free(data.pw_shell);
2866*0Sstevel@tonic-gate 	return (rc);
2867*0Sstevel@tonic-gate }
2868*0Sstevel@tonic-gate 
2869*0Sstevel@tonic-gate 
2870*0Sstevel@tonic-gate static void
2871*0Sstevel@tonic-gate dump_passwd(ns_ldap_result_t *res)
2872*0Sstevel@tonic-gate {
2873*0Sstevel@tonic-gate 	char    **value = NULL;
2874*0Sstevel@tonic-gate 	char	pnam[256];
2875*0Sstevel@tonic-gate 
2876*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "uid");
2877*0Sstevel@tonic-gate 	if (value == NULL)
2878*0Sstevel@tonic-gate 		return;
2879*0Sstevel@tonic-gate 	else
2880*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
2881*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "userPassword");
2882*0Sstevel@tonic-gate 	if (value == NULL)
2883*0Sstevel@tonic-gate 		(void) fprintf(stdout, "*:");
2884*0Sstevel@tonic-gate 	else {
2885*0Sstevel@tonic-gate 		(void) strcpy(pnam, value[0]);
2886*0Sstevel@tonic-gate 		if (strncasecmp(value[0], "{crypt}", 7) == 0)
2887*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", (pnam+7));
2888*0Sstevel@tonic-gate 		else
2889*0Sstevel@tonic-gate 			(void) fprintf(stdout, "*:");
2890*0Sstevel@tonic-gate 	}
2891*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "uidNumber");
2892*0Sstevel@tonic-gate 	if (value && value[0])
2893*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
2894*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "gidNumber");
2895*0Sstevel@tonic-gate 	if (value && value[0])
2896*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
2897*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "gecos");
2898*0Sstevel@tonic-gate 	if (value == NULL)
2899*0Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
2900*0Sstevel@tonic-gate 	else
2901*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
2902*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "homeDirectory");
2903*0Sstevel@tonic-gate 	if (value == NULL)
2904*0Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
2905*0Sstevel@tonic-gate 	else
2906*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
2907*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "loginShell");
2908*0Sstevel@tonic-gate 	if (value == NULL)
2909*0Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
2910*0Sstevel@tonic-gate 	else
2911*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\n", value[0]);
2912*0Sstevel@tonic-gate 
2913*0Sstevel@tonic-gate }
2914*0Sstevel@tonic-gate 
2915*0Sstevel@tonic-gate /*
2916*0Sstevel@tonic-gate  * /etc/shadow
2917*0Sstevel@tonic-gate  */
2918*0Sstevel@tonic-gate 
2919*0Sstevel@tonic-gate static int
2920*0Sstevel@tonic-gate genent_shadow(char *line, int (*cback)())
2921*0Sstevel@tonic-gate {
2922*0Sstevel@tonic-gate 	char buf[BUFSIZ+1];
2923*0Sstevel@tonic-gate 	char *s, *t;
2924*0Sstevel@tonic-gate 	entry_col ecol[9];
2925*0Sstevel@tonic-gate 	char pname[BUFSIZ];
2926*0Sstevel@tonic-gate 
2927*0Sstevel@tonic-gate 	struct spwd	data;
2928*0Sstevel@tonic-gate 	int spflag;
2929*0Sstevel@tonic-gate 
2930*0Sstevel@tonic-gate 
2931*0Sstevel@tonic-gate 	/*
2932*0Sstevel@tonic-gate 	 * don't clobber our argument
2933*0Sstevel@tonic-gate 	 */
2934*0Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
2935*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
2936*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2937*0Sstevel@tonic-gate 	}
2938*0Sstevel@tonic-gate 	(void) strcpy(buf, line);
2939*0Sstevel@tonic-gate 	t = buf;
2940*0Sstevel@tonic-gate 
2941*0Sstevel@tonic-gate 	/* ignore empty entries */
2942*0Sstevel@tonic-gate 	if (*t == '\0')
2943*0Sstevel@tonic-gate 		return (GENENT_OK);
2944*0Sstevel@tonic-gate 
2945*0Sstevel@tonic-gate 	/*
2946*0Sstevel@tonic-gate 	 * clear column data
2947*0Sstevel@tonic-gate 	 */
2948*0Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
2949*0Sstevel@tonic-gate 
2950*0Sstevel@tonic-gate 	/*
2951*0Sstevel@tonic-gate 	 * name (col 0)
2952*0Sstevel@tonic-gate 	 */
2953*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
2954*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no uid");
2955*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2956*0Sstevel@tonic-gate 	}
2957*0Sstevel@tonic-gate 	*s++ = 0;
2958*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
2959*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
2960*0Sstevel@tonic-gate 	t = s;
2961*0Sstevel@tonic-gate 
2962*0Sstevel@tonic-gate 	/*
2963*0Sstevel@tonic-gate 	 * passwd (col 1)
2964*0Sstevel@tonic-gate 	 */
2965*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
2966*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "Improper format");
2967*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2968*0Sstevel@tonic-gate 	}
2969*0Sstevel@tonic-gate 	*s++ = 0;
2970*0Sstevel@tonic-gate 
2971*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
2972*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
2973*0Sstevel@tonic-gate 
2974*0Sstevel@tonic-gate 	t = s;
2975*0Sstevel@tonic-gate 
2976*0Sstevel@tonic-gate 	/*
2977*0Sstevel@tonic-gate 	 * shadow last change (col 2)
2978*0Sstevel@tonic-gate 	 */
2979*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
2980*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "Improper format");
2981*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2982*0Sstevel@tonic-gate 	}
2983*0Sstevel@tonic-gate 	*s++ = 0;
2984*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
2985*0Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
2986*0Sstevel@tonic-gate 	t = s;
2987*0Sstevel@tonic-gate 
2988*0Sstevel@tonic-gate 	/*
2989*0Sstevel@tonic-gate 	 * shadow min (col 3)
2990*0Sstevel@tonic-gate 	 */
2991*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
2992*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "Improper format");
2993*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2994*0Sstevel@tonic-gate 	}
2995*0Sstevel@tonic-gate 	*s++ = 0;
2996*0Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
2997*0Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
2998*0Sstevel@tonic-gate 	t = s;
2999*0Sstevel@tonic-gate 
3000*0Sstevel@tonic-gate 	/*
3001*0Sstevel@tonic-gate 	 * shadow max (col 4)
3002*0Sstevel@tonic-gate 	 */
3003*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
3004*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "Improper format");
3005*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
3006*0Sstevel@tonic-gate 	}
3007*0Sstevel@tonic-gate 	*s++ = 0;
3008*0Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_val = t;
3009*0Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_len = strlen(t)+1;
3010*0Sstevel@tonic-gate 	t = s;
3011*0Sstevel@tonic-gate 
3012*0Sstevel@tonic-gate 	/*
3013*0Sstevel@tonic-gate 	 * shadow warn (col 5)
3014*0Sstevel@tonic-gate 	 */
3015*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
3016*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "Improper format");
3017*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
3018*0Sstevel@tonic-gate 	}
3019*0Sstevel@tonic-gate 	*s++ = 0;
3020*0Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_val = t;
3021*0Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_len = strlen(t)+1;
3022*0Sstevel@tonic-gate 	t = s;
3023*0Sstevel@tonic-gate 
3024*0Sstevel@tonic-gate 	/*
3025*0Sstevel@tonic-gate 	 * shadow inactive (col 6)
3026*0Sstevel@tonic-gate 	 */
3027*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) != 0) {
3028*0Sstevel@tonic-gate 	*s++ = 0;
3029*0Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_val = t;
3030*0Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_len = strlen(t)+1;
3031*0Sstevel@tonic-gate 	t = s;
3032*0Sstevel@tonic-gate 	}
3033*0Sstevel@tonic-gate 
3034*0Sstevel@tonic-gate 	/*
3035*0Sstevel@tonic-gate 	 * shadow expire  (col 7)
3036*0Sstevel@tonic-gate 	 */
3037*0Sstevel@tonic-gate 	if ((s = strchr(t, ':')) != 0) {
3038*0Sstevel@tonic-gate 	*s++ = 0;
3039*0Sstevel@tonic-gate 	ecol[7].ec_value.ec_value_val = t;
3040*0Sstevel@tonic-gate 	ecol[7].ec_value.ec_value_len = strlen(t)+1;
3041*0Sstevel@tonic-gate 	t = s;
3042*0Sstevel@tonic-gate 
3043*0Sstevel@tonic-gate 	/*
3044*0Sstevel@tonic-gate 	 * flag (col 8)
3045*0Sstevel@tonic-gate 	 */
3046*0Sstevel@tonic-gate 	ecol[8].ec_value.ec_value_val = t;
3047*0Sstevel@tonic-gate 	ecol[8].ec_value.ec_value_len = strlen(t)+1;
3048*0Sstevel@tonic-gate 	}
3049*0Sstevel@tonic-gate 
3050*0Sstevel@tonic-gate 	/*
3051*0Sstevel@tonic-gate 	 * build entry
3052*0Sstevel@tonic-gate 	 */
3053*0Sstevel@tonic-gate 
3054*0Sstevel@tonic-gate 	data.sp_namp = strdup(ecol[0].ec_value.ec_value_val);
3055*0Sstevel@tonic-gate 
3056*0Sstevel@tonic-gate 	if (ecol[1].ec_value.ec_value_val != NULL &&
3057*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val[0] != '\0') {
3058*0Sstevel@tonic-gate 		/* Add {crypt} before passwd entry */
3059*0Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname), "{crypt}%s",
3060*0Sstevel@tonic-gate 		    ecol[1].ec_value.ec_value_val);
3061*0Sstevel@tonic-gate 		data.sp_pwdp = strdup(pname);
3062*0Sstevel@tonic-gate 	} else
3063*0Sstevel@tonic-gate 		data.sp_pwdp = NULL;
3064*0Sstevel@tonic-gate 
3065*0Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
3066*0Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val[0] != '\0') {
3067*0Sstevel@tonic-gate 
3068*0Sstevel@tonic-gate 		data.sp_lstchg = ascii_to_int(ecol[2].ec_value.ec_value_val);
3069*0Sstevel@tonic-gate 		if (data.sp_lstchg < -1) {
3070*0Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3071*0Sstevel@tonic-gate 			    "invalid last changed date: %s",
3072*0Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
3073*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
3074*0Sstevel@tonic-gate 		}
3075*0Sstevel@tonic-gate 	} else
3076*0Sstevel@tonic-gate 		data.sp_lstchg = -1;
3077*0Sstevel@tonic-gate 
3078*0Sstevel@tonic-gate 	if (ecol[3].ec_value.ec_value_val != NULL &&
3079*0Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val[0] != '\0') {
3080*0Sstevel@tonic-gate 
3081*0Sstevel@tonic-gate 		data.sp_min = ascii_to_int(ecol[3].ec_value.ec_value_val);
3082*0Sstevel@tonic-gate 		if (data.sp_min < -1) {
3083*0Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3084*0Sstevel@tonic-gate 			    "invalid sp_min : %s",
3085*0Sstevel@tonic-gate 			    ecol[3].ec_value.ec_value_val);
3086*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
3087*0Sstevel@tonic-gate 		}
3088*0Sstevel@tonic-gate 	} else
3089*0Sstevel@tonic-gate 		data.sp_min = -1;
3090*0Sstevel@tonic-gate 
3091*0Sstevel@tonic-gate 	if (ecol[4].ec_value.ec_value_val != NULL &&
3092*0Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_val[0] != '\0') {
3093*0Sstevel@tonic-gate 
3094*0Sstevel@tonic-gate 		data.sp_max = ascii_to_int(ecol[4].ec_value.ec_value_val);
3095*0Sstevel@tonic-gate 		if (data.sp_max < -1) {
3096*0Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3097*0Sstevel@tonic-gate 			    "invalid sp_max : %s",
3098*0Sstevel@tonic-gate 			    ecol[4].ec_value.ec_value_val);
3099*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
3100*0Sstevel@tonic-gate 		}
3101*0Sstevel@tonic-gate 	} else
3102*0Sstevel@tonic-gate 		data.sp_max = -1;
3103*0Sstevel@tonic-gate 
3104*0Sstevel@tonic-gate 	if (ecol[5].ec_value.ec_value_val != NULL &&
3105*0Sstevel@tonic-gate 		ecol[5].ec_value.ec_value_val[0] != '\0') {
3106*0Sstevel@tonic-gate 
3107*0Sstevel@tonic-gate 		data.sp_warn = ascii_to_int(ecol[5].ec_value.ec_value_val);
3108*0Sstevel@tonic-gate 		if (data.sp_warn < -1) {
3109*0Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3110*0Sstevel@tonic-gate 			    "invalid sp_warn : %s",
3111*0Sstevel@tonic-gate 			    ecol[5].ec_value.ec_value_val);
3112*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
3113*0Sstevel@tonic-gate 		}
3114*0Sstevel@tonic-gate 	} else
3115*0Sstevel@tonic-gate 		data.sp_warn = -1;
3116*0Sstevel@tonic-gate 
3117*0Sstevel@tonic-gate 	if (ecol[6].ec_value.ec_value_val != NULL &&
3118*0Sstevel@tonic-gate 		ecol[6].ec_value.ec_value_val[0] != '\0') {
3119*0Sstevel@tonic-gate 
3120*0Sstevel@tonic-gate 		data.sp_inact = ascii_to_int(ecol[6].ec_value.ec_value_val);
3121*0Sstevel@tonic-gate 		if (data.sp_inact < -1) {
3122*0Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3123*0Sstevel@tonic-gate 			    "invalid sp_inact : %s",
3124*0Sstevel@tonic-gate 			    ecol[6].ec_value.ec_value_val);
3125*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
3126*0Sstevel@tonic-gate 		}
3127*0Sstevel@tonic-gate 	} else
3128*0Sstevel@tonic-gate 		data.sp_inact = -1;
3129*0Sstevel@tonic-gate 
3130*0Sstevel@tonic-gate 	if (ecol[7].ec_value.ec_value_val != NULL &&
3131*0Sstevel@tonic-gate 		ecol[7].ec_value.ec_value_val[0] != '\0') {
3132*0Sstevel@tonic-gate 
3133*0Sstevel@tonic-gate 		data.sp_expire = ascii_to_int(ecol[7].ec_value.ec_value_val);
3134*0Sstevel@tonic-gate 		if (data.sp_expire < -1) {
3135*0Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3136*0Sstevel@tonic-gate 			    "invalid login expiry date : %s",
3137*0Sstevel@tonic-gate 			    ecol[7].ec_value.ec_value_val);
3138*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
3139*0Sstevel@tonic-gate 		}
3140*0Sstevel@tonic-gate 	} else
3141*0Sstevel@tonic-gate 		data.sp_expire = -1;
3142*0Sstevel@tonic-gate 
3143*0Sstevel@tonic-gate 	if (ecol[8].ec_value.ec_value_val != NULL &&
3144*0Sstevel@tonic-gate 		ecol[8].ec_value.ec_value_val[0] != '\0') {
3145*0Sstevel@tonic-gate 
3146*0Sstevel@tonic-gate 		/*
3147*0Sstevel@tonic-gate 		 * data.sp_flag is an unsigned int,
3148*0Sstevel@tonic-gate 		 * assign -1 to it, make no sense.
3149*0Sstevel@tonic-gate 		 * Use spflag here to avoid lint warning.
3150*0Sstevel@tonic-gate 		 */
3151*0Sstevel@tonic-gate 		spflag = ascii_to_int(ecol[8].ec_value.ec_value_val);
3152*0Sstevel@tonic-gate 		if (spflag < 0) {
3153*0Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3154*0Sstevel@tonic-gate 			    "invalid flag value: %s",
3155*0Sstevel@tonic-gate 			    ecol[8].ec_value.ec_value_val);
3156*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
3157*0Sstevel@tonic-gate 		} else
3158*0Sstevel@tonic-gate 			data.sp_flag = spflag;
3159*0Sstevel@tonic-gate 	} else
3160*0Sstevel@tonic-gate 		data.sp_flag = 0;
3161*0Sstevel@tonic-gate 
3162*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
3163*0Sstevel@tonic-gate 		(void) fprintf(stdout,
3164*0Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.sp_namp);
3165*0Sstevel@tonic-gate 
3166*0Sstevel@tonic-gate 	if ((*cback)(&data, 1) && (continue_onerror == 0))
3167*0Sstevel@tonic-gate 		return (GENENT_CBERR);
3168*0Sstevel@tonic-gate 
3169*0Sstevel@tonic-gate 	free(data.sp_namp);
3170*0Sstevel@tonic-gate 	free(data.sp_pwdp);
3171*0Sstevel@tonic-gate 	return (GENENT_OK);
3172*0Sstevel@tonic-gate }
3173*0Sstevel@tonic-gate 
3174*0Sstevel@tonic-gate static void
3175*0Sstevel@tonic-gate dump_shadow(ns_ldap_result_t *res)
3176*0Sstevel@tonic-gate {
3177*0Sstevel@tonic-gate 	char    **value = NULL;
3178*0Sstevel@tonic-gate 	char   pnam[256];
3179*0Sstevel@tonic-gate 
3180*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "uid");
3181*0Sstevel@tonic-gate 	if (value == NULL)
3182*0Sstevel@tonic-gate 		return;
3183*0Sstevel@tonic-gate 	else
3184*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
3185*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "userPassword");
3186*0Sstevel@tonic-gate 	if (value == NULL)
3187*0Sstevel@tonic-gate 		(void) fprintf(stdout, "*:");
3188*0Sstevel@tonic-gate 	else {
3189*0Sstevel@tonic-gate 		(void) strcpy(pnam, value[0]);
3190*0Sstevel@tonic-gate 		if (strncasecmp(value[0], "{crypt}", 7) == 0)
3191*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", (pnam+7));
3192*0Sstevel@tonic-gate 		else
3193*0Sstevel@tonic-gate 			(void) fprintf(stdout, "*:");
3194*0Sstevel@tonic-gate 	}
3195*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "shadowLastChange");
3196*0Sstevel@tonic-gate 	if (value == NULL)
3197*0Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
3198*0Sstevel@tonic-gate 	else
3199*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
3200*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "shadowMin");
3201*0Sstevel@tonic-gate 	if (value == NULL)
3202*0Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
3203*0Sstevel@tonic-gate 	else
3204*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
3205*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "shadowMax");
3206*0Sstevel@tonic-gate 	if (value == NULL)
3207*0Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
3208*0Sstevel@tonic-gate 	else
3209*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
3210*0Sstevel@tonic-gate 
3211*0Sstevel@tonic-gate 	/* ignore shadowWarning, shadowInactive, shadowExpire, shadowFlag */
3212*0Sstevel@tonic-gate 	(void) fprintf(stdout, ":::\n");
3213*0Sstevel@tonic-gate 
3214*0Sstevel@tonic-gate }
3215*0Sstevel@tonic-gate 
3216*0Sstevel@tonic-gate 
3217*0Sstevel@tonic-gate static int
3218*0Sstevel@tonic-gate genent_bootparams(char *line, int (*cback)())
3219*0Sstevel@tonic-gate {
3220*0Sstevel@tonic-gate 	char buf[BUFSIZ+1];
3221*0Sstevel@tonic-gate 	char *t;
3222*0Sstevel@tonic-gate 	entry_col ecol[2];
3223*0Sstevel@tonic-gate 	int ctr = 0, retval = 1;
3224*0Sstevel@tonic-gate 
3225*0Sstevel@tonic-gate 	struct _ns_bootp data;
3226*0Sstevel@tonic-gate 	char *parameter;
3227*0Sstevel@tonic-gate 	int rc = GENENT_OK;
3228*0Sstevel@tonic-gate 
3229*0Sstevel@tonic-gate 	/*
3230*0Sstevel@tonic-gate 	 * don't clobber our argument
3231*0Sstevel@tonic-gate 	 */
3232*0Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
3233*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
3234*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
3235*0Sstevel@tonic-gate 	}
3236*0Sstevel@tonic-gate 	(void) strcpy(buf, line);
3237*0Sstevel@tonic-gate 
3238*0Sstevel@tonic-gate 	/*
3239*0Sstevel@tonic-gate 	 * clear column data
3240*0Sstevel@tonic-gate 	 */
3241*0Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
3242*0Sstevel@tonic-gate 
3243*0Sstevel@tonic-gate 
3244*0Sstevel@tonic-gate 	/*
3245*0Sstevel@tonic-gate 	 * cname (col 0)
3246*0Sstevel@tonic-gate 	 */
3247*0Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
3248*0Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no cname");
3249*0Sstevel@tonic-gate 		return (GENENT_PARSEERR);
3250*0Sstevel@tonic-gate 	}
3251*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
3252*0Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
3253*0Sstevel@tonic-gate 
3254*0Sstevel@tonic-gate 
3255*0Sstevel@tonic-gate 
3256*0Sstevel@tonic-gate 	/* build entry */
3257*0Sstevel@tonic-gate 	data.name = strdup(ecol[0].ec_value.ec_value_val);
3258*0Sstevel@tonic-gate 
3259*0Sstevel@tonic-gate 	/*
3260*0Sstevel@tonic-gate 	 * name (col 1)
3261*0Sstevel@tonic-gate 	 */
3262*0Sstevel@tonic-gate 
3263*0Sstevel@tonic-gate 	data.param = NULL;
3264*0Sstevel@tonic-gate 
3265*0Sstevel@tonic-gate 	do {
3266*0Sstevel@tonic-gate 
3267*0Sstevel@tonic-gate 		/*
3268*0Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
3269*0Sstevel@tonic-gate 		 */
3270*0Sstevel@tonic-gate 
3271*0Sstevel@tonic-gate 
3272*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
3273*0Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
3274*0Sstevel@tonic-gate 
3275*0Sstevel@tonic-gate 		ctr++;
3276*0Sstevel@tonic-gate 		parameter = strdup(ecol[1].ec_value.ec_value_val);
3277*0Sstevel@tonic-gate 		if ((data.param = (char **)realloc(data.param,
3278*0Sstevel@tonic-gate 			(ctr + 1) * sizeof (char **))) == NULL) {
3279*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
3280*0Sstevel@tonic-gate 			exit(1);
3281*0Sstevel@tonic-gate 		}
3282*0Sstevel@tonic-gate 		data.param[ctr-1] = parameter;
3283*0Sstevel@tonic-gate 
3284*0Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
3285*0Sstevel@tonic-gate 
3286*0Sstevel@tonic-gate 
3287*0Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
3288*0Sstevel@tonic-gate 	if ((data.param = (char **)realloc(data.param,
3289*0Sstevel@tonic-gate 		(ctr + 1) * sizeof (char **))) == NULL) {
3290*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
3291*0Sstevel@tonic-gate 		exit(1);
3292*0Sstevel@tonic-gate 	}
3293*0Sstevel@tonic-gate 	data.param[ctr] = NULL;
3294*0Sstevel@tonic-gate 
3295*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
3296*0Sstevel@tonic-gate 		(void) fprintf(stdout,
3297*0Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.name);
3298*0Sstevel@tonic-gate 
3299*0Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
3300*0Sstevel@tonic-gate 
3301*0Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
3302*0Sstevel@tonic-gate 		if (continue_onerror)
3303*0Sstevel@tonic-gate 			(void) fprintf(stderr,
3304*0Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
3305*0Sstevel@tonic-gate 			data.name);
3306*0Sstevel@tonic-gate 		else {
3307*0Sstevel@tonic-gate 			rc = GENENT_CBERR;
3308*0Sstevel@tonic-gate 			(void) fprintf(stderr,
3309*0Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
3310*0Sstevel@tonic-gate 				data.name);
3311*0Sstevel@tonic-gate 		}
3312*0Sstevel@tonic-gate 	} else if (retval)
3313*0Sstevel@tonic-gate 		rc = GENENT_CBERR;
3314*0Sstevel@tonic-gate 
3315*0Sstevel@tonic-gate 	free(data.name);
3316*0Sstevel@tonic-gate 	free(data.param);
3317*0Sstevel@tonic-gate 
3318*0Sstevel@tonic-gate 	return (rc);
3319*0Sstevel@tonic-gate 
3320*0Sstevel@tonic-gate }
3321*0Sstevel@tonic-gate 
3322*0Sstevel@tonic-gate 
3323*0Sstevel@tonic-gate static void
3324*0Sstevel@tonic-gate dump_bootparams(ns_ldap_result_t *res)
3325*0Sstevel@tonic-gate {
3326*0Sstevel@tonic-gate 	char	**value = NULL;
3327*0Sstevel@tonic-gate 	int		attr_count = 0;
3328*0Sstevel@tonic-gate 
3329*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
3330*0Sstevel@tonic-gate 	if (value[0] != NULL)
3331*0Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
3332*0Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "bootParameter");
3333*0Sstevel@tonic-gate 	if (value != NULL)
3334*0Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
3335*0Sstevel@tonic-gate 		(void) fprintf(stdout, "\t%s", value[attr_count]);
3336*0Sstevel@tonic-gate 			attr_count++;
3337*0Sstevel@tonic-gate 		}
3338*0Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
3339*0Sstevel@tonic-gate 
3340*0Sstevel@tonic-gate 
3341*0Sstevel@tonic-gate }
3342*0Sstevel@tonic-gate 
3343*0Sstevel@tonic-gate static char *
3344*0Sstevel@tonic-gate fget_line_at(struct line_buf *line, int n, FILE *fp)
3345*0Sstevel@tonic-gate {
3346*0Sstevel@tonic-gate 	int c;
3347*0Sstevel@tonic-gate 
3348*0Sstevel@tonic-gate 	line->len = n;
3349*0Sstevel@tonic-gate 
3350*0Sstevel@tonic-gate 	for (;;) {
3351*0Sstevel@tonic-gate 		c = fgetc(fp);
3352*0Sstevel@tonic-gate 		if (c == -1)
3353*0Sstevel@tonic-gate 			break;
3354*0Sstevel@tonic-gate 		if (line->len >= line->alloc)
3355*0Sstevel@tonic-gate 			line_buf_expand(line);
3356*0Sstevel@tonic-gate 		line->str[line->len++] = c;
3357*0Sstevel@tonic-gate 
3358*0Sstevel@tonic-gate 		if (c == '\n')
3359*0Sstevel@tonic-gate 			break;
3360*0Sstevel@tonic-gate 	}
3361*0Sstevel@tonic-gate 
3362*0Sstevel@tonic-gate 	/* Null Terminate */
3363*0Sstevel@tonic-gate 	if (line->len >= line->alloc)
3364*0Sstevel@tonic-gate 		line_buf_expand(line);
3365*0Sstevel@tonic-gate 	line->str[line->len++] = 0;
3366*0Sstevel@tonic-gate 
3367*0Sstevel@tonic-gate 	/* if no characters are read, return NULL to indicate EOF */
3368*0Sstevel@tonic-gate 	if (line->str[0] == '\0')
3369*0Sstevel@tonic-gate 		return (0);
3370*0Sstevel@tonic-gate 
3371*0Sstevel@tonic-gate 	return (line->str);
3372*0Sstevel@tonic-gate }
3373*0Sstevel@tonic-gate 
3374*0Sstevel@tonic-gate /*
3375*0Sstevel@tonic-gate  * return a line from the file, discarding comments and blank lines
3376*0Sstevel@tonic-gate  */
3377*0Sstevel@tonic-gate static int
3378*0Sstevel@tonic-gate filedbmline_comment(struct line_buf *line, FILE *etcf, int *lineno,
3379*0Sstevel@tonic-gate     struct file_loc *loc)
3380*0Sstevel@tonic-gate {
3381*0Sstevel@tonic-gate 	int i, len = 0;
3382*0Sstevel@tonic-gate 
3383*0Sstevel@tonic-gate 	loc->offset = ftell(etcf);
3384*0Sstevel@tonic-gate 	for (;;) {
3385*0Sstevel@tonic-gate 		if (fget_line_at(line, len, etcf) == 0)
3386*0Sstevel@tonic-gate 			return (0);
3387*0Sstevel@tonic-gate 
3388*0Sstevel@tonic-gate 		if (lineno)
3389*0Sstevel@tonic-gate 			(*lineno)++;
3390*0Sstevel@tonic-gate 
3391*0Sstevel@tonic-gate 		len = strlen(line->str);
3392*0Sstevel@tonic-gate 		if (len >= 2 &&
3393*0Sstevel@tonic-gate 		    line->str[0] != '#' &&
3394*0Sstevel@tonic-gate 		    line->str[len-2] == '\\' && line->str[len-1] == '\n') {
3395*0Sstevel@tonic-gate 			line->str[len-2] = 0;
3396*0Sstevel@tonic-gate 			len -= 2;
3397*0Sstevel@tonic-gate 			continue;    /* append next line at end */
3398*0Sstevel@tonic-gate 		}
3399*0Sstevel@tonic-gate 
3400*0Sstevel@tonic-gate 		if (line->str[len-1] == '\n') {
3401*0Sstevel@tonic-gate 			line->str[len-1] = 0;
3402*0Sstevel@tonic-gate 			len -= 1;
3403*0Sstevel@tonic-gate 		}
3404*0Sstevel@tonic-gate 
3405*0Sstevel@tonic-gate 		/*
3406*0Sstevel@tonic-gate 		 * Skip lines where '#' is the first non-blank character.
3407*0Sstevel@tonic-gate 		 */
3408*0Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
3409*0Sstevel@tonic-gate 			if (line->str[i] == '#') {
3410*0Sstevel@tonic-gate 				line->str[i] = '\0';
3411*0Sstevel@tonic-gate 				len = i;
3412*0Sstevel@tonic-gate 				break;
3413*0Sstevel@tonic-gate 			}
3414*0Sstevel@tonic-gate 			if (line->str[i] != ' ' && line->str[i] != '\t')
3415*0Sstevel@tonic-gate 				break;
3416*0Sstevel@tonic-gate 		}
3417*0Sstevel@tonic-gate 
3418*0Sstevel@tonic-gate 		/*
3419*0Sstevel@tonic-gate 		 * A line with one or more white space characters followed
3420*0Sstevel@tonic-gate 		 * by a comment will now be blank. The special case of a
3421*0Sstevel@tonic-gate 		 * line with '#' in the first byte will have len == 0.
3422*0Sstevel@tonic-gate 		 */
3423*0Sstevel@tonic-gate 		if (len > 0 && !blankline(line->str))
3424*0Sstevel@tonic-gate 			break;
3425*0Sstevel@tonic-gate 
3426*0Sstevel@tonic-gate 		len = 0;
3427*0Sstevel@tonic-gate 		loc->offset = ftell(etcf);
3428*0Sstevel@tonic-gate 	}
3429*0Sstevel@tonic-gate 
3430*0Sstevel@tonic-gate 	loc->size = len;
3431*0Sstevel@tonic-gate 	return (1);
3432*0Sstevel@tonic-gate }
3433*0Sstevel@tonic-gate 
3434*0Sstevel@tonic-gate /*
3435*0Sstevel@tonic-gate  * return a line from the file, discarding comments, blanks, and '+' lines
3436*0Sstevel@tonic-gate  */
3437*0Sstevel@tonic-gate static int
3438*0Sstevel@tonic-gate filedbmline_plus(struct line_buf *line, FILE *etcf, int *lineno,
3439*0Sstevel@tonic-gate     struct file_loc *loc)
3440*0Sstevel@tonic-gate {
3441*0Sstevel@tonic-gate 	int len = 0;
3442*0Sstevel@tonic-gate 
3443*0Sstevel@tonic-gate 	loc->offset = ftell(etcf);
3444*0Sstevel@tonic-gate 	for (;;) {
3445*0Sstevel@tonic-gate 		if (fget_line_at(line, len, etcf) == 0)
3446*0Sstevel@tonic-gate 			return (0);
3447*0Sstevel@tonic-gate 
3448*0Sstevel@tonic-gate 		if (lineno)
3449*0Sstevel@tonic-gate 			(*lineno)++;
3450*0Sstevel@tonic-gate 
3451*0Sstevel@tonic-gate 		len = strlen(line->str);
3452*0Sstevel@tonic-gate 		if (line->str[len-1] == '\n') {
3453*0Sstevel@tonic-gate 			line->str[len-1] = 0;
3454*0Sstevel@tonic-gate 			len -= 1;
3455*0Sstevel@tonic-gate 		}
3456*0Sstevel@tonic-gate 
3457*0Sstevel@tonic-gate 		if (!blankline(line->str) &&
3458*0Sstevel@tonic-gate 		line->str[0] != '+' && line->str[0] != '-' &&
3459*0Sstevel@tonic-gate 		line->str[0] != '#')
3460*0Sstevel@tonic-gate 			break;
3461*0Sstevel@tonic-gate 
3462*0Sstevel@tonic-gate 		len = 0;
3463*0Sstevel@tonic-gate 		loc->offset = ftell(etcf);
3464*0Sstevel@tonic-gate 	}
3465*0Sstevel@tonic-gate 
3466*0Sstevel@tonic-gate 	loc->size = len;
3467*0Sstevel@tonic-gate 	return (1);
3468*0Sstevel@tonic-gate }
3469*0Sstevel@tonic-gate 
3470*0Sstevel@tonic-gate 
3471*0Sstevel@tonic-gate /* Populating the ttypelist structure */
3472*0Sstevel@tonic-gate 
3473*0Sstevel@tonic-gate static struct ttypelist_t ttypelist[] = {
3474*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_HOSTS, genent_hosts, dump_hosts,
3475*0Sstevel@tonic-gate 		filedbmline_comment, "iphost" },
3476*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_IPNODES, genent_hosts, dump_hosts,
3477*0Sstevel@tonic-gate 		filedbmline_comment, "iphost" },
3478*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_RPC, genent_rpc, dump_rpc,
3479*0Sstevel@tonic-gate 		filedbmline_comment, "oncrpc" },
3480*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PROTOCOLS, genent_protocols, dump_protocols,
3481*0Sstevel@tonic-gate 		filedbmline_comment, "ipprotocol" },
3482*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_NETWORKS, genent_networks, dump_networks,
3483*0Sstevel@tonic-gate 		filedbmline_comment, "ipnetwork"  },
3484*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_SERVICES, genent_services, dump_services,
3485*0Sstevel@tonic-gate 		filedbmline_comment, "ipservice" },
3486*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_GROUP, genent_group, dump_group,
3487*0Sstevel@tonic-gate 		filedbmline_plus, "posixgroup" },
3488*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_NETMASKS, genent_netmasks, dump_netmasks,
3489*0Sstevel@tonic-gate 		filedbmline_comment, "ipnetwork" },
3490*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_ETHERS, genent_ethers, dump_ethers,
3491*0Sstevel@tonic-gate 		filedbmline_comment, "ieee802Device" },
3492*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_NETGROUP, genent_netgroup, dump_netgroup,
3493*0Sstevel@tonic-gate 		filedbmline_comment, "nisnetgroup" },
3494*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_BOOTPARAMS, genent_bootparams, dump_bootparams,
3495*0Sstevel@tonic-gate 		filedbmline_comment, "bootableDevice" },
3496*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PUBLICKEY, genent_publickey, NULL /* dump_publickey */,
3497*0Sstevel@tonic-gate 		filedbmline_comment, "niskeyobject" },
3498*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PASSWD, genent_passwd, dump_passwd,
3499*0Sstevel@tonic-gate 		filedbmline_plus, "posixaccount" },
3500*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_SHADOW, genent_shadow, dump_shadow,
3501*0Sstevel@tonic-gate 		filedbmline_plus, "shadowaccount" },
3502*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_ALIASES, genent_aliases, dump_aliases,
3503*0Sstevel@tonic-gate 		filedbmline_plus, "mailGroup" },
3504*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_AUTOMOUNT, genent_automount, dump_automount,
3505*0Sstevel@tonic-gate 		filedbmline_comment, "automount" },
3506*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_USERATTR, genent_user_attr, dump_user_attr,
3507*0Sstevel@tonic-gate 		filedbmline_comment, "SolarisUserAttr" },
3508*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PROFILE, genent_prof_attr, dump_prof_attr,
3509*0Sstevel@tonic-gate 		filedbmline_comment, "SolarisProfAttr" },
3510*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_EXECATTR, genent_exec_attr, dump_exec_attr,
3511*0Sstevel@tonic-gate 		filedbmline_comment, "SolarisExecAttr" },
3512*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_AUTHATTR, genent_auth_attr, dump_auth_attr,
3513*0Sstevel@tonic-gate 		filedbmline_comment, "SolarisAuthAttr" },
3514*0Sstevel@tonic-gate 	{ NS_LDAP_TYPE_AUUSER, genent_audit_user, dump_audit_user,
3515*0Sstevel@tonic-gate 		filedbmline_comment, "SolarisAuditUser" },
3516*0Sstevel@tonic-gate 	{ 0, 0, 0, 0, 0 }
3517*0Sstevel@tonic-gate };
3518*0Sstevel@tonic-gate 
3519*0Sstevel@tonic-gate 
3520*0Sstevel@tonic-gate 
3521*0Sstevel@tonic-gate 
3522*0Sstevel@tonic-gate static int lineno = 0;
3523*0Sstevel@tonic-gate 
3524*0Sstevel@tonic-gate static	void
3525*0Sstevel@tonic-gate addfile()
3526*0Sstevel@tonic-gate {
3527*0Sstevel@tonic-gate 	struct line_buf line;
3528*0Sstevel@tonic-gate 	struct file_loc loc;
3529*0Sstevel@tonic-gate 
3530*0Sstevel@tonic-gate 	/* Initializing the Line Buffer */
3531*0Sstevel@tonic-gate 	line_buf_init(&line);
3532*0Sstevel@tonic-gate 
3533*0Sstevel@tonic-gate 	/* Loop through all the lines in the file */
3534*0Sstevel@tonic-gate 	while (tt->filedbmline(&line, etcf, &lineno, &loc)) {
3535*0Sstevel@tonic-gate 		switch ((*(tt->genent))(line.str, addentry)) {
3536*0Sstevel@tonic-gate 		case GENENT_OK:
3537*0Sstevel@tonic-gate 			break;
3538*0Sstevel@tonic-gate 		case GENENT_PARSEERR:
3539*0Sstevel@tonic-gate 			(void) fprintf(stderr,
3540*0Sstevel@tonic-gate 			    gettext("parse error: %s (line %d)\n"),
3541*0Sstevel@tonic-gate 			    parse_err_msg, lineno);
3542*0Sstevel@tonic-gate 			exit_val = 1;
3543*0Sstevel@tonic-gate 			break;
3544*0Sstevel@tonic-gate 		case GENENT_CBERR:
3545*0Sstevel@tonic-gate 			(void) fprintf(stderr,
3546*0Sstevel@tonic-gate 			    gettext("Error while adding line: %s\n"),
3547*0Sstevel@tonic-gate 			    line.str);
3548*0Sstevel@tonic-gate 			exit_val = 2;
3549*0Sstevel@tonic-gate 			free(line.str);
3550*0Sstevel@tonic-gate 			return;
3551*0Sstevel@tonic-gate 			break;
3552*0Sstevel@tonic-gate 		case GENENT_ERR:
3553*0Sstevel@tonic-gate 			(void) fprintf(stderr,
3554*0Sstevel@tonic-gate 			    gettext("Internal Error while adding line: %s\n"),
3555*0Sstevel@tonic-gate 			    line.str);
3556*0Sstevel@tonic-gate 			exit_val = 3;
3557*0Sstevel@tonic-gate 			free(line.str);
3558*0Sstevel@tonic-gate 			return;
3559*0Sstevel@tonic-gate 			break;
3560*0Sstevel@tonic-gate 		}
3561*0Sstevel@tonic-gate 	}
3562*0Sstevel@tonic-gate 	free(line.str);
3563*0Sstevel@tonic-gate }
3564*0Sstevel@tonic-gate 
3565*0Sstevel@tonic-gate static void
3566*0Sstevel@tonic-gate dumptable(char *service)
3567*0Sstevel@tonic-gate {
3568*0Sstevel@tonic-gate 
3569*0Sstevel@tonic-gate 	ns_ldap_result_t *eres = NULL;
3570*0Sstevel@tonic-gate 	ns_ldap_error_t *err = NULL;
3571*0Sstevel@tonic-gate 	int	rc = 0, success = 0;
3572*0Sstevel@tonic-gate 	char	filter[BUFSIZ];
3573*0Sstevel@tonic-gate 	int	done = 0;
3574*0Sstevel@tonic-gate 	void	*cookie = NULL;
3575*0Sstevel@tonic-gate 
3576*0Sstevel@tonic-gate 	/* set the appropriate filter */
3577*0Sstevel@tonic-gate 	if (strcmp(tt->ttype, NS_LDAP_TYPE_PROFILE) == 0) {
3578*0Sstevel@tonic-gate 		/*
3579*0Sstevel@tonic-gate 		 * prof_attr entries are SolarisProfAttr
3580*0Sstevel@tonic-gate 		 * without AUXILIARY SolarisExecAttr
3581*0Sstevel@tonic-gate 		 */
3582*0Sstevel@tonic-gate 		(void) snprintf(filter, sizeof (filter),
3583*0Sstevel@tonic-gate 		    "(&(objectclass=%s)(!(objectclass=SolarisExecAttr)))",
3584*0Sstevel@tonic-gate 		    tt->objclass);
3585*0Sstevel@tonic-gate 	} else
3586*0Sstevel@tonic-gate 		(void) snprintf(filter, sizeof (filter),
3587*0Sstevel@tonic-gate 		    "(objectclass=%s)", tt->objclass);
3588*0Sstevel@tonic-gate 
3589*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
3590*0Sstevel@tonic-gate 		(void) fprintf(stdout, gettext("FILTER = %s\n"), filter);
3591*0Sstevel@tonic-gate 
3592*0Sstevel@tonic-gate 	/* Pass cred only if supplied. Cred is not always needed for dump */
3593*0Sstevel@tonic-gate 	if (authority.cred.unix_cred.userID == NULL ||
3594*0Sstevel@tonic-gate 	    authority.cred.unix_cred.passwd == NULL)
3595*0Sstevel@tonic-gate 		rc = __ns_ldap_firstEntry(service, filter, NULL, NULL,
3596*0Sstevel@tonic-gate 		    NULL, NS_LDAP_HARD, &cookie, &eres, &err, NULL);
3597*0Sstevel@tonic-gate 	else
3598*0Sstevel@tonic-gate 		rc = __ns_ldap_firstEntry(service, filter, NULL, NULL,
3599*0Sstevel@tonic-gate 		    &authority, NS_LDAP_HARD, &cookie, &eres, &err, NULL);
3600*0Sstevel@tonic-gate 
3601*0Sstevel@tonic-gate 	switch (rc) {
3602*0Sstevel@tonic-gate 	case NS_LDAP_SUCCESS:
3603*0Sstevel@tonic-gate 		nent_add++;
3604*0Sstevel@tonic-gate 		success = 1;
3605*0Sstevel@tonic-gate 		if (eres != NULL) {
3606*0Sstevel@tonic-gate 			if (strcmp(databasetype, "publickey") == 0)
3607*0Sstevel@tonic-gate 				dump_publickey(eres, service);
3608*0Sstevel@tonic-gate 			else
3609*0Sstevel@tonic-gate 				(*(tt->dump))(eres);
3610*0Sstevel@tonic-gate 		}
3611*0Sstevel@tonic-gate 		else
3612*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("No entries found.\n"));
3613*0Sstevel@tonic-gate 		break;
3614*0Sstevel@tonic-gate 
3615*0Sstevel@tonic-gate 	case NS_LDAP_OP_FAILED:
3616*0Sstevel@tonic-gate 		exit_val = 2;
3617*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("operation failed.\n"));
3618*0Sstevel@tonic-gate 		break;
3619*0Sstevel@tonic-gate 
3620*0Sstevel@tonic-gate 	case NS_LDAP_INVALID_PARAM:
3621*0Sstevel@tonic-gate 		exit_val = 2;
3622*0Sstevel@tonic-gate 		(void) fprintf(stderr,
3623*0Sstevel@tonic-gate 		    gettext("invalid parameter(s) passed.\n"));
3624*0Sstevel@tonic-gate 		break;
3625*0Sstevel@tonic-gate 
3626*0Sstevel@tonic-gate 	case NS_LDAP_NOTFOUND:
3627*0Sstevel@tonic-gate 		exit_val = 2;
3628*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("entry not found.\n"));
3629*0Sstevel@tonic-gate 		break;
3630*0Sstevel@tonic-gate 
3631*0Sstevel@tonic-gate 	case NS_LDAP_MEMORY:
3632*0Sstevel@tonic-gate 		exit_val = 2;
3633*0Sstevel@tonic-gate 		(void) fprintf(stderr,
3634*0Sstevel@tonic-gate 			gettext("internal memory allocation error.\n"));
3635*0Sstevel@tonic-gate 		break;
3636*0Sstevel@tonic-gate 
3637*0Sstevel@tonic-gate 	case NS_LDAP_CONFIG:
3638*0Sstevel@tonic-gate 		exit_val = 2;
3639*0Sstevel@tonic-gate 		(void) fprintf(stderr,
3640*0Sstevel@tonic-gate 			gettext("LDAP Configuration problem.\n"));
3641*0Sstevel@tonic-gate 		perr(err);
3642*0Sstevel@tonic-gate 		break;
3643*0Sstevel@tonic-gate 
3644*0Sstevel@tonic-gate 	case NS_LDAP_PARTIAL:
3645*0Sstevel@tonic-gate 		exit_val = 2;
3646*0Sstevel@tonic-gate 		(void) fprintf(stderr,
3647*0Sstevel@tonic-gate 			gettext("partial result returned\n"));
3648*0Sstevel@tonic-gate 		perr(err);
3649*0Sstevel@tonic-gate 		break;
3650*0Sstevel@tonic-gate 
3651*0Sstevel@tonic-gate 	case NS_LDAP_INTERNAL:
3652*0Sstevel@tonic-gate 		exit_val = 2;
3653*0Sstevel@tonic-gate 		(void) fprintf(stderr,
3654*0Sstevel@tonic-gate 			gettext("internal LDAP error occured.\n"));
3655*0Sstevel@tonic-gate 		perr(err);
3656*0Sstevel@tonic-gate 		break;
3657*0Sstevel@tonic-gate 	}
3658*0Sstevel@tonic-gate 
3659*0Sstevel@tonic-gate 	if (eres != NULL) {
3660*0Sstevel@tonic-gate 		(void) __ns_ldap_freeResult(&eres);
3661*0Sstevel@tonic-gate 		eres = NULL;
3662*0Sstevel@tonic-gate 	}
3663*0Sstevel@tonic-gate 
3664*0Sstevel@tonic-gate 	if (success) {
3665*0Sstevel@tonic-gate 		while (!done) {
3666*0Sstevel@tonic-gate 			rc = __ns_ldap_nextEntry(cookie, &eres, &err);
3667*0Sstevel@tonic-gate 			if (rc != NS_LDAP_SUCCESS || eres  == NULL) {
3668*0Sstevel@tonic-gate 				done = 1;
3669*0Sstevel@tonic-gate 				continue;
3670*0Sstevel@tonic-gate 			}
3671*0Sstevel@tonic-gate 
3672*0Sstevel@tonic-gate 			/* Print the result */
3673*0Sstevel@tonic-gate 			if (eres != NULL) {
3674*0Sstevel@tonic-gate 				if (strcmp(databasetype, "publickey") == 0)
3675*0Sstevel@tonic-gate 					dump_publickey(eres, service);
3676*0Sstevel@tonic-gate 				else
3677*0Sstevel@tonic-gate 					(*(tt->dump))(eres);
3678*0Sstevel@tonic-gate 				(void) __ns_ldap_freeResult(&eres);
3679*0Sstevel@tonic-gate 				eres = NULL;
3680*0Sstevel@tonic-gate 			}
3681*0Sstevel@tonic-gate 		}
3682*0Sstevel@tonic-gate 	}
3683*0Sstevel@tonic-gate }
3684*0Sstevel@tonic-gate 
3685*0Sstevel@tonic-gate void
3686*0Sstevel@tonic-gate main(int argc, char **argv)
3687*0Sstevel@tonic-gate {
3688*0Sstevel@tonic-gate 	char	*password;
3689*0Sstevel@tonic-gate 	int	c;
3690*0Sstevel@tonic-gate 	int	rc;
3691*0Sstevel@tonic-gate 	int	ldaprc;
3692*0Sstevel@tonic-gate 	int		authstried = 0;
3693*0Sstevel@tonic-gate 	int		supportedauth = 0;
3694*0Sstevel@tonic-gate 	int		op = OP_ADD;
3695*0Sstevel@tonic-gate 	char	*ttype, *authmech = 0, *etcfile = 0;
3696*0Sstevel@tonic-gate 	char	ps[LDAP_MAXNAMELEN]; /* Temporary password variable */
3697*0Sstevel@tonic-gate 	char	filter[BUFSIZ];
3698*0Sstevel@tonic-gate 	void	**paramVal = NULL;
3699*0Sstevel@tonic-gate 	ns_auth_t	**app;
3700*0Sstevel@tonic-gate 	ns_auth_t	**authpp = NULL;
3701*0Sstevel@tonic-gate 	ns_auth_t	*authp = NULL;
3702*0Sstevel@tonic-gate 	ns_ldap_error_t	*errorp = NULL;
3703*0Sstevel@tonic-gate 	ns_ldap_result_t *resultp;
3704*0Sstevel@tonic-gate 	ns_ldap_entry_t *e;
3705*0Sstevel@tonic-gate 	int	flag = 0;
3706*0Sstevel@tonic-gate 	int	version1 = 0;
3707*0Sstevel@tonic-gate 
3708*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3709*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3710*0Sstevel@tonic-gate 
3711*0Sstevel@tonic-gate 	openlog("ldapaddent", LOG_PID, LOG_USER);
3712*0Sstevel@tonic-gate 
3713*0Sstevel@tonic-gate 	inputbasedn = NULL;
3714*0Sstevel@tonic-gate 	authority.cred.unix_cred.passwd = NULL;
3715*0Sstevel@tonic-gate 	authority.cred.unix_cred.userID = NULL;
3716*0Sstevel@tonic-gate 	authority.auth.type = NS_LDAP_AUTH_SIMPLE;
3717*0Sstevel@tonic-gate 
3718*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "cdhvpf:D:w:b:a:")) != EOF) {
3719*0Sstevel@tonic-gate 		switch (c) {
3720*0Sstevel@tonic-gate 		case 'd':
3721*0Sstevel@tonic-gate 			if (op)
3722*0Sstevel@tonic-gate 				usage("no other option should be specified");
3723*0Sstevel@tonic-gate 			op = OP_DUMP;
3724*0Sstevel@tonic-gate 			break;
3725*0Sstevel@tonic-gate 		case 'c':
3726*0Sstevel@tonic-gate 			continue_onerror = 1;
3727*0Sstevel@tonic-gate 			break;
3728*0Sstevel@tonic-gate 		case 'v':
3729*0Sstevel@tonic-gate 			flags |= F_VERBOSE;
3730*0Sstevel@tonic-gate 			break;
3731*0Sstevel@tonic-gate 		case 'p':
3732*0Sstevel@tonic-gate 			flags |= F_PASSWD;
3733*0Sstevel@tonic-gate 			break;
3734*0Sstevel@tonic-gate 		case 'f':
3735*0Sstevel@tonic-gate 			etcfile = optarg;
3736*0Sstevel@tonic-gate 			break;
3737*0Sstevel@tonic-gate 		case 'D':
3738*0Sstevel@tonic-gate 			authority.cred.unix_cred.userID = strdup(optarg);
3739*0Sstevel@tonic-gate 			break;
3740*0Sstevel@tonic-gate 		case 'w':
3741*0Sstevel@tonic-gate 			authority.cred.unix_cred.passwd = strdup(optarg);
3742*0Sstevel@tonic-gate 			break;
3743*0Sstevel@tonic-gate 		case 'b':
3744*0Sstevel@tonic-gate 			inputbasedn = strdup(optarg);
3745*0Sstevel@tonic-gate 			break;
3746*0Sstevel@tonic-gate 		case 'a':
3747*0Sstevel@tonic-gate 			authmech = strdup(optarg);
3748*0Sstevel@tonic-gate 			break;
3749*0Sstevel@tonic-gate 
3750*0Sstevel@tonic-gate 		default:
3751*0Sstevel@tonic-gate 			usage(gettext("Invalid option"));
3752*0Sstevel@tonic-gate 		}
3753*0Sstevel@tonic-gate 	}
3754*0Sstevel@tonic-gate 
3755*0Sstevel@tonic-gate 
3756*0Sstevel@tonic-gate 	if (authority.cred.unix_cred.userID == NULL && op != OP_DUMP) {
3757*0Sstevel@tonic-gate 	    /* This is not an optional parameter. Exit */
3758*0Sstevel@tonic-gate 		(void) fprintf(stderr,
3759*0Sstevel@tonic-gate 			gettext("Distinguished Name to bind to directory"
3760*0Sstevel@tonic-gate 				" must be specified. use option -D.\n"));
3761*0Sstevel@tonic-gate 		exit(1);
3762*0Sstevel@tonic-gate 	}
3763*0Sstevel@tonic-gate 
3764*0Sstevel@tonic-gate 	if (authority.cred.unix_cred.passwd == NULL && op != OP_DUMP) {
3765*0Sstevel@tonic-gate 		/* If password is not specified, then prompt user for it. */
3766*0Sstevel@tonic-gate 		password = getpassphrase("Enter password:");
3767*0Sstevel@tonic-gate 		(void) strcpy(ps, password);
3768*0Sstevel@tonic-gate 		authority.cred.unix_cred.passwd = strdup(ps);
3769*0Sstevel@tonic-gate 	}
3770*0Sstevel@tonic-gate 
3771*0Sstevel@tonic-gate 	if (authmech != NULL) {
3772*0Sstevel@tonic-gate 		if (strcasecmp(authmech, "simple") == 0) {
3773*0Sstevel@tonic-gate 		authority.auth.type = NS_LDAP_AUTH_SIMPLE;
3774*0Sstevel@tonic-gate 		authority.auth.tlstype = NS_LDAP_TLS_NONE;
3775*0Sstevel@tonic-gate 		authority.auth.saslmech = NS_LDAP_SASL_NONE;
3776*0Sstevel@tonic-gate 		authority.auth.saslopt = NS_LDAP_SASLOPT_NONE;
3777*0Sstevel@tonic-gate 		supportedauth = 1;
3778*0Sstevel@tonic-gate 		}
3779*0Sstevel@tonic-gate 		if (strcasecmp(authmech, "sasl/CRAM-MD5") == 0) {
3780*0Sstevel@tonic-gate 		authority.auth.type = NS_LDAP_AUTH_SASL;
3781*0Sstevel@tonic-gate 		authority.auth.tlstype = NS_LDAP_TLS_SASL;
3782*0Sstevel@tonic-gate 		authority.auth.saslmech = NS_LDAP_SASL_CRAM_MD5;
3783*0Sstevel@tonic-gate 		authority.auth.saslopt = NS_LDAP_SASLOPT_NONE;
3784*0Sstevel@tonic-gate 		supportedauth = 1;
3785*0Sstevel@tonic-gate 		}
3786*0Sstevel@tonic-gate 		if (strcasecmp(authmech, "sasl/DIGEST-MD5") == 0) {
3787*0Sstevel@tonic-gate 		authority.auth.type = NS_LDAP_AUTH_SASL;
3788*0Sstevel@tonic-gate 		authority.auth.tlstype = NS_LDAP_TLS_SASL;
3789*0Sstevel@tonic-gate 		authority.auth.saslmech = NS_LDAP_SASL_DIGEST_MD5;
3790*0Sstevel@tonic-gate 		authority.auth.saslopt = NS_LDAP_SASLOPT_NONE;
3791*0Sstevel@tonic-gate 		supportedauth = 1;
3792*0Sstevel@tonic-gate 		}
3793*0Sstevel@tonic-gate 		if (strcasecmp(authmech, "tls:simple") == 0) {
3794*0Sstevel@tonic-gate 		authority.auth.type = NS_LDAP_AUTH_TLS;
3795*0Sstevel@tonic-gate 		authority.auth.tlstype = NS_LDAP_TLS_SIMPLE;
3796*0Sstevel@tonic-gate 		authority.auth.saslmech = NS_LDAP_SASL_NONE;
3797*0Sstevel@tonic-gate 		authority.auth.saslopt = NS_LDAP_SASLOPT_NONE;
3798*0Sstevel@tonic-gate 		supportedauth = 1;
3799*0Sstevel@tonic-gate 		}
3800*0Sstevel@tonic-gate 		if (strcasecmp(authmech, "tls:sasl/CRAM-MD5") == 0) {
3801*0Sstevel@tonic-gate 		authority.auth.type = NS_LDAP_AUTH_TLS;
3802*0Sstevel@tonic-gate 		authority.auth.tlstype = NS_LDAP_TLS_SASL;
3803*0Sstevel@tonic-gate 		authority.auth.saslmech = NS_LDAP_SASL_CRAM_MD5;
3804*0Sstevel@tonic-gate 		authority.auth.saslopt = NS_LDAP_SASLOPT_NONE;
3805*0Sstevel@tonic-gate 		supportedauth = 1;
3806*0Sstevel@tonic-gate 		}
3807*0Sstevel@tonic-gate 		if (strcasecmp(authmech, "tls:sasl/DIGEST-MD5") == 0) {
3808*0Sstevel@tonic-gate 		authority.auth.type = NS_LDAP_AUTH_TLS;
3809*0Sstevel@tonic-gate 		authority.auth.tlstype = NS_LDAP_TLS_SASL;
3810*0Sstevel@tonic-gate 		authority.auth.saslmech = NS_LDAP_SASL_DIGEST_MD5;
3811*0Sstevel@tonic-gate 		authority.auth.saslopt = NS_LDAP_SASLOPT_NONE;
3812*0Sstevel@tonic-gate 		supportedauth = 1;
3813*0Sstevel@tonic-gate 		}
3814*0Sstevel@tonic-gate 		if (!supportedauth) {
3815*0Sstevel@tonic-gate 			(void) fprintf(stderr,
3816*0Sstevel@tonic-gate 			gettext("Invalid authentication method specified"));
3817*0Sstevel@tonic-gate 			exit(1);
3818*0Sstevel@tonic-gate 		}
3819*0Sstevel@tonic-gate 	}
3820*0Sstevel@tonic-gate 
3821*0Sstevel@tonic-gate 	if (authmech == NULL) {
3822*0Sstevel@tonic-gate 		ldaprc = __ns_ldap_getParam(NS_LDAP_AUTH_P, (void ***)&authpp,
3823*0Sstevel@tonic-gate 			&errorp);
3824*0Sstevel@tonic-gate 		if (ldaprc != NS_LDAP_SUCCESS ||
3825*0Sstevel@tonic-gate 		    (authpp == NULL && op != OP_DUMP)) {
3826*0Sstevel@tonic-gate 			(void) fprintf(stderr,
3827*0Sstevel@tonic-gate 			    gettext("No legal authentication method "
3828*0Sstevel@tonic-gate 			    "configured.\n"));
3829*0Sstevel@tonic-gate 			(void) fprintf(stderr,
3830*0Sstevel@tonic-gate 			    gettext("Provide a legal authentication method "
3831*0Sstevel@tonic-gate 			    "using -a option\n"));
3832*0Sstevel@tonic-gate 			exit(1);
3833*0Sstevel@tonic-gate 		}
3834*0Sstevel@tonic-gate 
3835*0Sstevel@tonic-gate 		/* Use the first authentication method which is not none */
3836*0Sstevel@tonic-gate 		for (app = authpp; *app; app++) {
3837*0Sstevel@tonic-gate 			authp = *app;
3838*0Sstevel@tonic-gate 			if (authp->type != NS_LDAP_AUTH_NONE) {
3839*0Sstevel@tonic-gate 				authstried++;
3840*0Sstevel@tonic-gate 				authority.auth.type = authp->type;
3841*0Sstevel@tonic-gate 				authority.auth.tlstype = authp->tlstype;
3842*0Sstevel@tonic-gate 				authority.auth.saslmech = authp->saslmech;
3843*0Sstevel@tonic-gate 				authority.auth.saslopt = authp->saslopt;
3844*0Sstevel@tonic-gate 				break;
3845*0Sstevel@tonic-gate 			}
3846*0Sstevel@tonic-gate 		}
3847*0Sstevel@tonic-gate 		if (authstried == 0 && op != OP_DUMP) {
3848*0Sstevel@tonic-gate 			(void) fprintf(stderr,
3849*0Sstevel@tonic-gate 			gettext("No legal authentication method configured.\n"
3850*0Sstevel@tonic-gate 				"Provide a legal authentication method using "
3851*0Sstevel@tonic-gate 				"-a option"));
3852*0Sstevel@tonic-gate 			exit(1);
3853*0Sstevel@tonic-gate 		}
3854*0Sstevel@tonic-gate 	}
3855*0Sstevel@tonic-gate 
3856*0Sstevel@tonic-gate 	ttype = argv[optind++];
3857*0Sstevel@tonic-gate 
3858*0Sstevel@tonic-gate 	if (ttype == NULL) {
3859*0Sstevel@tonic-gate 		usage(gettext("No database type specified"));
3860*0Sstevel@tonic-gate 		exit(1);
3861*0Sstevel@tonic-gate 	}
3862*0Sstevel@tonic-gate 
3863*0Sstevel@tonic-gate 	if (strncasecmp(ttype, "automount", 9) == 0) {
3864*0Sstevel@tonic-gate 		(void) fprintf(stderr,
3865*0Sstevel@tonic-gate 		    gettext("automount is not a valid service for ldapaddent.\n"
3866*0Sstevel@tonic-gate 		    "Please use auto_*.\n"
3867*0Sstevel@tonic-gate 		    "e.g.  auto_home, auto_ws etc.\n "));
3868*0Sstevel@tonic-gate 		exit(1);
3869*0Sstevel@tonic-gate 	}
3870*0Sstevel@tonic-gate 
3871*0Sstevel@tonic-gate 	for (tt = ttypelist; tt->ttype; tt++) {
3872*0Sstevel@tonic-gate 		if (strcmp(tt->ttype, ttype) == 0)
3873*0Sstevel@tonic-gate 			break;
3874*0Sstevel@tonic-gate 		if (strcmp(tt->ttype, NS_LDAP_TYPE_AUTOMOUNT) == 0 &&
3875*0Sstevel@tonic-gate 		    strncmp(ttype, NS_LDAP_TYPE_AUTOMOUNT,
3876*0Sstevel@tonic-gate 		    sizeof (NS_LDAP_TYPE_AUTOMOUNT) - 1) == 0)
3877*0Sstevel@tonic-gate 			break;
3878*0Sstevel@tonic-gate 	}
3879*0Sstevel@tonic-gate 
3880*0Sstevel@tonic-gate 	if (tt->ttype == 0) {
3881*0Sstevel@tonic-gate 		(void) fprintf(stderr,
3882*0Sstevel@tonic-gate 		    gettext("database %s not supported;"
3883*0Sstevel@tonic-gate 		    " supported databases are:\n"), ttype);
3884*0Sstevel@tonic-gate 		for (tt = ttypelist; tt->ttype; tt++)
3885*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("\t%s\n"), tt->ttype);
3886*0Sstevel@tonic-gate 		exit(1);
3887*0Sstevel@tonic-gate 	}
3888*0Sstevel@tonic-gate 
3889*0Sstevel@tonic-gate 	if (flags & F_VERBOSE)
3890*0Sstevel@tonic-gate 		(void) fprintf(stdout, gettext("SERVICE = %s\n"), tt->ttype);
3891*0Sstevel@tonic-gate 
3892*0Sstevel@tonic-gate 	databasetype = ttype;
3893*0Sstevel@tonic-gate 
3894*0Sstevel@tonic-gate 	if (strcmp(tt->ttype, NS_LDAP_TYPE_AUTOMOUNT) == 0) {
3895*0Sstevel@tonic-gate 		paramVal = NULL;
3896*0Sstevel@tonic-gate 		errorp = NULL;
3897*0Sstevel@tonic-gate 		rc = __ns_ldap_getParam(NS_LDAP_FILE_VERSION_P, &paramVal,
3898*0Sstevel@tonic-gate 			&errorp);
3899*0Sstevel@tonic-gate 		if (paramVal && *paramVal &&
3900*0Sstevel@tonic-gate 			strcasecmp(*paramVal, NS_LDAP_VERSION_1) == 0)
3901*0Sstevel@tonic-gate 			version1 = 1;
3902*0Sstevel@tonic-gate 		if (paramVal)
3903*0Sstevel@tonic-gate 			(void) __ns_ldap_freeParam(&paramVal);
3904*0Sstevel@tonic-gate 		if (errorp)
3905*0Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&errorp);
3906*0Sstevel@tonic-gate 	}
3907*0Sstevel@tonic-gate 
3908*0Sstevel@tonic-gate 	/* Check if the container exists in first place */
3909*0Sstevel@tonic-gate 	(void) strcpy(&filter[0], "(objectclass=*)");
3910*0Sstevel@tonic-gate 
3911*0Sstevel@tonic-gate 	rc = __ns_ldap_list(databasetype, filter, NULL, (const char **)NULL,
3912*0Sstevel@tonic-gate 	    NULL, NS_LDAP_SCOPE_BASE, &resultp, &errorp, NULL, NULL);
3913*0Sstevel@tonic-gate 
3914*0Sstevel@tonic-gate 	/* create a container for auto_* if it does not exist already */
3915*0Sstevel@tonic-gate 	if ((rc == NS_LDAP_NOTFOUND) && (op == OP_ADD) &&
3916*0Sstevel@tonic-gate 	    (strcmp(tt->ttype, NS_LDAP_TYPE_AUTOMOUNT) == 0)) {
3917*0Sstevel@tonic-gate 		static	char *oclist[] = {NULL, "top", NULL};
3918*0Sstevel@tonic-gate 		if (version1)
3919*0Sstevel@tonic-gate 			oclist[0] = "nisMap";
3920*0Sstevel@tonic-gate 		else
3921*0Sstevel@tonic-gate 			oclist[0] = "automountMap";
3922*0Sstevel@tonic-gate 		e = __s_mk_entry(oclist, 3);
3923*0Sstevel@tonic-gate 		if (e == NULL) {
3924*0Sstevel@tonic-gate 			(void) fprintf(stderr,
3925*0Sstevel@tonic-gate 			    gettext("internal memory allocation error.\n"));
3926*0Sstevel@tonic-gate 			exit(1);
3927*0Sstevel@tonic-gate 		}
3928*0Sstevel@tonic-gate 		if (__s_add_attr(e,
3929*0Sstevel@tonic-gate 		    version1 ? "nisMapName" : "automountMapName",
3930*0Sstevel@tonic-gate 		    databasetype) != NS_LDAP_SUCCESS) {
3931*0Sstevel@tonic-gate 			(void) fprintf(stderr,
3932*0Sstevel@tonic-gate 			    gettext("internal memory allocation error.\n"));
3933*0Sstevel@tonic-gate 			ldap_freeEntry(e);
3934*0Sstevel@tonic-gate 			exit(1);
3935*0Sstevel@tonic-gate 		}
3936*0Sstevel@tonic-gate 
3937*0Sstevel@tonic-gate 		if (inputbasedn == NULL) {
3938*0Sstevel@tonic-gate 			if (get_basedn(databasetype, &inputbasedn) !=
3939*0Sstevel@tonic-gate 			    NS_LDAP_SUCCESS) {
3940*0Sstevel@tonic-gate 				(void) fprintf(stderr,
3941*0Sstevel@tonic-gate 				    gettext("Could not obtain basedn\n"));
3942*0Sstevel@tonic-gate 				ldap_freeEntry(e);
3943*0Sstevel@tonic-gate 				exit(1);
3944*0Sstevel@tonic-gate 			}
3945*0Sstevel@tonic-gate 		}
3946*0Sstevel@tonic-gate 		if (__ns_ldap_addEntry(databasetype, inputbasedn, e,
3947*0Sstevel@tonic-gate 		    &authority, flag, &errorp) != NS_LDAP_SUCCESS) {
3948*0Sstevel@tonic-gate 			(void) fprintf(stderr,
3949*0Sstevel@tonic-gate 			    gettext("Could not create container for %s\n"),
3950*0Sstevel@tonic-gate 			    databasetype);
3951*0Sstevel@tonic-gate 			ldap_freeEntry(e);
3952*0Sstevel@tonic-gate 		}
3953*0Sstevel@tonic-gate 	} else if (strcmp(databasetype, "publickey") != 0) {
3954*0Sstevel@tonic-gate 		if (rc == NS_LDAP_NOTFOUND) {
3955*0Sstevel@tonic-gate 			(void) fprintf(stderr,
3956*0Sstevel@tonic-gate 			    gettext("Container %s does not exist\n"),
3957*0Sstevel@tonic-gate 			    databasetype);
3958*0Sstevel@tonic-gate 			exit(1);
3959*0Sstevel@tonic-gate 		}
3960*0Sstevel@tonic-gate 	}
3961*0Sstevel@tonic-gate 
3962*0Sstevel@tonic-gate 	if (op == OP_DUMP) {
3963*0Sstevel@tonic-gate 		if (strcmp(databasetype, "publickey") == 0) {
3964*0Sstevel@tonic-gate 			dumptable("hosts");
3965*0Sstevel@tonic-gate 			dumptable("passwd");
3966*0Sstevel@tonic-gate 		} else {
3967*0Sstevel@tonic-gate 			dumptable(databasetype);
3968*0Sstevel@tonic-gate 		}
3969*0Sstevel@tonic-gate 		exit(exit_val);
3970*0Sstevel@tonic-gate 	}
3971*0Sstevel@tonic-gate 
3972*0Sstevel@tonic-gate 	if (etcfile) {
3973*0Sstevel@tonic-gate 		if ((etcf = fopen(etcfile, "r")) == 0) {
3974*0Sstevel@tonic-gate 			(void) fprintf(stderr,
3975*0Sstevel@tonic-gate 			    gettext("can't open file %s\n"), etcfile);
3976*0Sstevel@tonic-gate 			exit(1);
3977*0Sstevel@tonic-gate 		}
3978*0Sstevel@tonic-gate 	} else {
3979*0Sstevel@tonic-gate 		etcfile = "stdin";
3980*0Sstevel@tonic-gate 		etcf = stdin;
3981*0Sstevel@tonic-gate 	}
3982*0Sstevel@tonic-gate 
3983*0Sstevel@tonic-gate 	if (op == OP_ADD) {
3984*0Sstevel@tonic-gate 		(void) addfile();
3985*0Sstevel@tonic-gate 		(void) fprintf(stdout, gettext("%d entries added\n"), nent_add);
3986*0Sstevel@tonic-gate 	}
3987*0Sstevel@tonic-gate 
3988*0Sstevel@tonic-gate 	exit(exit_val);
3989*0Sstevel@tonic-gate }
3990*0Sstevel@tonic-gate 
3991*0Sstevel@tonic-gate 
3992*0Sstevel@tonic-gate /*
3993*0Sstevel@tonic-gate  * This is called when service == auto_*.
3994*0Sstevel@tonic-gate  * It calls __ns_ldap_getSearchDescriptors
3995*0Sstevel@tonic-gate  * to generate the dn from SSD's base dn.
3996*0Sstevel@tonic-gate  * If there is no SSD available,
3997*0Sstevel@tonic-gate  * default base dn will be used
3998*0Sstevel@tonic-gate  * Only the first baseDN in the SSD is used
3999*0Sstevel@tonic-gate  */
4000*0Sstevel@tonic-gate 
4001*0Sstevel@tonic-gate static int get_basedn(char *service, char **basedn) {
4002*0Sstevel@tonic-gate 	int rc = NS_LDAP_SUCCESS;
4003*0Sstevel@tonic-gate 	char *dn = NULL;
4004*0Sstevel@tonic-gate 	ns_ldap_search_desc_t **desc = NULL;
4005*0Sstevel@tonic-gate 	ns_ldap_error_t *errp = NULL;
4006*0Sstevel@tonic-gate 	void		**paramVal = NULL;
4007*0Sstevel@tonic-gate 	int		prepend_automountmapname = FALSE;
4008*0Sstevel@tonic-gate 
4009*0Sstevel@tonic-gate 	/*
4010*0Sstevel@tonic-gate 	 * Get auto_* SSD first
4011*0Sstevel@tonic-gate 	 */
4012*0Sstevel@tonic-gate 
4013*0Sstevel@tonic-gate 	if ((rc = __ns_ldap_getSearchDescriptors(
4014*0Sstevel@tonic-gate 			(const char *) service,
4015*0Sstevel@tonic-gate 			&desc, &errp))  == NS_LDAP_SUCCESS &&
4016*0Sstevel@tonic-gate 		desc != NULL) {
4017*0Sstevel@tonic-gate 
4018*0Sstevel@tonic-gate 		if (desc[0] != NULL && desc[0]->basedn != NULL) {
4019*0Sstevel@tonic-gate 			dn = strdup(desc[0]->basedn);
4020*0Sstevel@tonic-gate 			if (dn == NULL) {
4021*0Sstevel@tonic-gate 				(void) __ns_ldap_freeSearchDescriptors
4022*0Sstevel@tonic-gate 						(&desc);
4023*0Sstevel@tonic-gate 				return (NS_LDAP_MEMORY);
4024*0Sstevel@tonic-gate 			}
4025*0Sstevel@tonic-gate 		}
4026*0Sstevel@tonic-gate 	}
4027*0Sstevel@tonic-gate 
4028*0Sstevel@tonic-gate 	/* clean up */
4029*0Sstevel@tonic-gate 	if (desc) (void) __ns_ldap_freeSearchDescriptors(&desc);
4030*0Sstevel@tonic-gate 	if (errp) (void) __ns_ldap_freeError(&errp);
4031*0Sstevel@tonic-gate 
4032*0Sstevel@tonic-gate 	/*
4033*0Sstevel@tonic-gate 	 * If no dn is duplicated from auto_* SSD, try automount SSD
4034*0Sstevel@tonic-gate 	 */
4035*0Sstevel@tonic-gate 	if (dn == NULL) {
4036*0Sstevel@tonic-gate 		if ((rc = __ns_ldap_getSearchDescriptors(
4037*0Sstevel@tonic-gate 				"automount", &desc, &errp))
4038*0Sstevel@tonic-gate 				== NS_LDAP_SUCCESS && desc != NULL) {
4039*0Sstevel@tonic-gate 
4040*0Sstevel@tonic-gate 			if (desc[0] != NULL && desc[0]->basedn != NULL) {
4041*0Sstevel@tonic-gate 				dn = strdup(desc[0]->basedn);
4042*0Sstevel@tonic-gate 				if (dn == NULL) {
4043*0Sstevel@tonic-gate 					(void) __ns_ldap_freeSearchDescriptors
4044*0Sstevel@tonic-gate 							(&desc);
4045*0Sstevel@tonic-gate 					return (NS_LDAP_MEMORY);
4046*0Sstevel@tonic-gate 				}
4047*0Sstevel@tonic-gate 				prepend_automountmapname = TRUE;
4048*0Sstevel@tonic-gate 			}
4049*0Sstevel@tonic-gate 		}
4050*0Sstevel@tonic-gate 		/* clean up */
4051*0Sstevel@tonic-gate 		if (desc) (void) __ns_ldap_freeSearchDescriptors(&desc);
4052*0Sstevel@tonic-gate 		if (errp) (void) __ns_ldap_freeError(&errp);
4053*0Sstevel@tonic-gate 	}
4054*0Sstevel@tonic-gate 
4055*0Sstevel@tonic-gate 	/*
4056*0Sstevel@tonic-gate 	 * If no dn is duplicated from auto_* or automount SSD,
4057*0Sstevel@tonic-gate 	 * use default DN
4058*0Sstevel@tonic-gate 	 */
4059*0Sstevel@tonic-gate 
4060*0Sstevel@tonic-gate 	if (dn == NULL) {
4061*0Sstevel@tonic-gate 		if ((rc = __ns_ldap_getParam(NS_LDAP_SEARCH_BASEDN_P,
4062*0Sstevel@tonic-gate 			&paramVal, &errp)) == NS_LDAP_SUCCESS) {
4063*0Sstevel@tonic-gate 			dn = strdup((char *)paramVal[0]);
4064*0Sstevel@tonic-gate 			if (dn == NULL) {
4065*0Sstevel@tonic-gate 				(void) __ns_ldap_freeParam(&paramVal);
4066*0Sstevel@tonic-gate 				return (NS_LDAP_MEMORY);
4067*0Sstevel@tonic-gate 			}
4068*0Sstevel@tonic-gate 			prepend_automountmapname = TRUE;
4069*0Sstevel@tonic-gate 		}
4070*0Sstevel@tonic-gate 		if (paramVal) (void) __ns_ldap_freeParam(&paramVal);
4071*0Sstevel@tonic-gate 		if (errp) (void) __ns_ldap_freeError(&errp);
4072*0Sstevel@tonic-gate 	}
4073*0Sstevel@tonic-gate 
4074*0Sstevel@tonic-gate 
4075*0Sstevel@tonic-gate 	if (dn == NULL) {
4076*0Sstevel@tonic-gate 		return (NS_LDAP_OP_FAILED);
4077*0Sstevel@tonic-gate 	} else {
4078*0Sstevel@tonic-gate 		/*
4079*0Sstevel@tonic-gate 		 * If dn is duplicated from
4080*0Sstevel@tonic-gate 		 * automount SSD basedn or
4081*0Sstevel@tonic-gate 		 * default base dn
4082*0Sstevel@tonic-gate 		 * then prepend automountMapName=auto_xxx
4083*0Sstevel@tonic-gate 		 */
4084*0Sstevel@tonic-gate 		if (prepend_automountmapname)
4085*0Sstevel@tonic-gate 			rc = __s_api_prepend_automountmapname_to_dn(
4086*0Sstevel@tonic-gate 				service, &dn, &errp);
4087*0Sstevel@tonic-gate 
4088*0Sstevel@tonic-gate 		if (rc != NS_LDAP_SUCCESS) {
4089*0Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&errp);
4090*0Sstevel@tonic-gate 			free(dn);
4091*0Sstevel@tonic-gate 			return (rc);
4092*0Sstevel@tonic-gate 		}
4093*0Sstevel@tonic-gate 
4094*0Sstevel@tonic-gate 		*basedn = dn;
4095*0Sstevel@tonic-gate 
4096*0Sstevel@tonic-gate 		return (NS_LDAP_SUCCESS);
4097*0Sstevel@tonic-gate 	}
4098*0Sstevel@tonic-gate }
4099