17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f31b640dSvl  * Common Development and Distribution License (the "License").
6f31b640dSvl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
229f2fd570SJulian Pullen  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
2333f5ff17SMilan Jurik  * Copyright 2012 Milan Jurik. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * ldapaddent.c
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * Utility to add /etc files into LDAP.
307c478bd9Sstevel@tonic-gate  * Can also be used to dump entries from a ldap container in /etc format.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <libintl.h>
367c478bd9Sstevel@tonic-gate #include <strings.h>
377c478bd9Sstevel@tonic-gate #include <sys/param.h>
387c478bd9Sstevel@tonic-gate #include <ctype.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/socket.h>
417c478bd9Sstevel@tonic-gate #include <netinet/in.h>
427c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
437c478bd9Sstevel@tonic-gate #include <locale.h>
447c478bd9Sstevel@tonic-gate #include <syslog.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #undef opaque
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
497c478bd9Sstevel@tonic-gate #include <netdb.h>
507c478bd9Sstevel@tonic-gate #include <rpc/rpcent.h>
517c478bd9Sstevel@tonic-gate #include <grp.h>
527c478bd9Sstevel@tonic-gate #include <pwd.h>
53e1dd0a2fSth #include <project.h>
547c478bd9Sstevel@tonic-gate #include <shadow.h>
557c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
567c478bd9Sstevel@tonic-gate #include "ns_internal.h"
577c478bd9Sstevel@tonic-gate #include "ldapaddent.h"
58e1dd0a2fSth #include "standalone.h"
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	OP_ADD	0
617c478bd9Sstevel@tonic-gate #define	OP_DUMP	3
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static struct ttypelist_t {
647c478bd9Sstevel@tonic-gate 	char *ttype;		/* type tag */
657c478bd9Sstevel@tonic-gate 	int (*genent)(char *, int(*)());
667c478bd9Sstevel@tonic-gate 				/* routine to turn line into ldap entries */
677c478bd9Sstevel@tonic-gate 	void (*dump)(ns_ldap_result_t *);
687c478bd9Sstevel@tonic-gate 				/* routine to print ldap containers */
697c478bd9Sstevel@tonic-gate 	int (*filedbmline)();	/* routine to turn file line into dbm line */
707c478bd9Sstevel@tonic-gate 	char *objclass;		/* Objectclass for the servicetype */
719f2fd570SJulian Pullen 	char *sortattr;		/* Sort attr for enumeration */
727c478bd9Sstevel@tonic-gate } *tt;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate char	parse_err_msg [PARSE_ERR_MSG_LEN];
757c478bd9Sstevel@tonic-gate int	continue_onerror = 0;  /* do not exit on error */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static int get_basedn(char *service, char **basedn);
787c478bd9Sstevel@tonic-gate static int check_ipaddr(char *addr, char **newaddr);
79e1dd0a2fSth static int check_projname(char *addr);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate extern	int	optind;
827c478bd9Sstevel@tonic-gate extern	char	*optarg;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate extern	char	*__nis_quote_key(const char *, char *, int);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static char	*inputbasedn = NULL;
877c478bd9Sstevel@tonic-gate static char	*databasetype = NULL;
887c478bd9Sstevel@tonic-gate static int	exit_val = 0;
897c478bd9Sstevel@tonic-gate static unsigned	nent_add = 0;
907c478bd9Sstevel@tonic-gate static FILE	*etcf = 0;
917c478bd9Sstevel@tonic-gate static ns_cred_t	authority;
927c478bd9Sstevel@tonic-gate unsigned	flags = 0;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static void
957c478bd9Sstevel@tonic-gate perr(ns_ldap_error_t *e)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	if (e)
98e1dd0a2fSth 		(void) fprintf(stderr, "%d: %s\n",
99e1dd0a2fSth 		    e->status, e->message);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static int
1047c478bd9Sstevel@tonic-gate ascii_to_int(char *str)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	int i;
1077c478bd9Sstevel@tonic-gate 	char *c = str;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if (c == NULL || *c == '\0')
1107c478bd9Sstevel@tonic-gate 		return (-1);
1117c478bd9Sstevel@tonic-gate 
1123718f283SToomas Soome 	while (*c == ' ')
1137c478bd9Sstevel@tonic-gate 		c++;
1147c478bd9Sstevel@tonic-gate 	if (*c == '\0')
1157c478bd9Sstevel@tonic-gate 		return (-1);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	for (i = 0; i < strlen(c); i++)
1187c478bd9Sstevel@tonic-gate 		if (!isdigit(c[i]))
1197c478bd9Sstevel@tonic-gate 			return (-1);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	return (atoi(c));
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * Internet network address interpretation routine.
1267c478bd9Sstevel@tonic-gate  * The library routines call this routine to interpret
1277c478bd9Sstevel@tonic-gate  * network numbers.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate static in_addr_t
1307c478bd9Sstevel@tonic-gate encode_network(const char *cp)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	in_addr_t val;
1337c478bd9Sstevel@tonic-gate 	int base;
1347c478bd9Sstevel@tonic-gate 	ptrdiff_t n;
1357c478bd9Sstevel@tonic-gate 	char c;
1367c478bd9Sstevel@tonic-gate 	in_addr_t parts[4], *pp = parts;
1377c478bd9Sstevel@tonic-gate 	int i;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate again:
1407c478bd9Sstevel@tonic-gate 	val = 0; base = 10;
1417c478bd9Sstevel@tonic-gate 	if (*cp == '0') {
1427c478bd9Sstevel@tonic-gate 		if (*++cp == 'x' || *cp == 'X')
1437c478bd9Sstevel@tonic-gate 			base = 16, cp++;
1447c478bd9Sstevel@tonic-gate 		else
1457c478bd9Sstevel@tonic-gate 			base = 8;
1467c478bd9Sstevel@tonic-gate 	}
147*c59d9dffSToomas Soome 	while ((c = *cp) != '\0') {
1487c478bd9Sstevel@tonic-gate 		if (isdigit(c)) {
1497c478bd9Sstevel@tonic-gate 			if ((c - '0') >= base)
150e1dd0a2fSth 				break;
1517c478bd9Sstevel@tonic-gate 			val = (val * base) + (c - '0');
1527c478bd9Sstevel@tonic-gate 			cp++;
1537c478bd9Sstevel@tonic-gate 			continue;
1547c478bd9Sstevel@tonic-gate 		}
1557c478bd9Sstevel@tonic-gate 		if (base == 16 && isxdigit(c)) {
1567c478bd9Sstevel@tonic-gate 			val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
1577c478bd9Sstevel@tonic-gate 			cp++;
1587c478bd9Sstevel@tonic-gate 			continue;
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 		break;
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 	if (*cp == '.') {
1637c478bd9Sstevel@tonic-gate 		if (pp >= parts + 4)
1647c478bd9Sstevel@tonic-gate 			return ((in_addr_t)-1);
1657c478bd9Sstevel@tonic-gate 		*pp++ = val, cp++;
1667c478bd9Sstevel@tonic-gate 		goto again;
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	if (*cp && !isspace(*cp))
1697c478bd9Sstevel@tonic-gate 		return ((in_addr_t)-1);
1707c478bd9Sstevel@tonic-gate 	*pp++ = val;
1717c478bd9Sstevel@tonic-gate 	n = pp - parts;
1727c478bd9Sstevel@tonic-gate 	if (n > 4)
1737c478bd9Sstevel@tonic-gate 		return ((in_addr_t)-1);
1747c478bd9Sstevel@tonic-gate 	for (val = 0, i = 0; i < n; i++) {
1757c478bd9Sstevel@tonic-gate 		val <<= 8;
1767c478bd9Sstevel@tonic-gate 		val |= parts[i] & 0xff;
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 	for (/* no init */; i < 4; i++)
1797c478bd9Sstevel@tonic-gate 		val <<= 8;
1807c478bd9Sstevel@tonic-gate 	return (val);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static void
1847c478bd9Sstevel@tonic-gate replace_tab2space(char *str)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	int i = 0;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	while ((str) && (str[i])) {
1897c478bd9Sstevel@tonic-gate 		if (str[i] == '\t')
1907c478bd9Sstevel@tonic-gate 			str[i] = ' ';
1917c478bd9Sstevel@tonic-gate 		i++;
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate static int
1967c478bd9Sstevel@tonic-gate blankline(char *line)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	char *p;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	for (p = line; *p; p++)
2017c478bd9Sstevel@tonic-gate 		if (*p != ' ' && *p != '\t')
2027c478bd9Sstevel@tonic-gate 			return (0);
2037c478bd9Sstevel@tonic-gate 	return (1);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
206f31b640dSvl /*
207f31b640dSvl  * check whether the token <tok> is a triplet,
208f31b640dSvl  * i. e. <tok> := (<hostname>,<username>,<domainname>)
209f31b640dSvl  * where <hostname>, <username>, <domainname> are IA5String
210f31b640dSvl  * <tok> supposes to contain NO spaces and start with '('
211f31b640dSvl  */
212f31b640dSvl static int
213f31b640dSvl is_triplet(char *tok)
214f31b640dSvl {
215f31b640dSvl 	char *s;
216f31b640dSvl 	return (strchr(++tok, '(') == NULL &&		/* no more '(' */
217e1dd0a2fSth 	    (s = strchr(tok, ')')) != NULL &&		/* find ')' */
218e1dd0a2fSth 	    !*++s &&					/* ')' ends token */
219e1dd0a2fSth 	    (tok = strchr(tok, ',')) != NULL &&		/* host up to ',' */
220e1dd0a2fSth 	    (tok = strchr(++tok, ',')) != NULL &&	/* user up to ',' */
221e1dd0a2fSth 	    strchr(++tok, ',') == NULL);		/* no more ',' */
222f31b640dSvl }
223f31b640dSvl 
2247c478bd9Sstevel@tonic-gate static void
2257c478bd9Sstevel@tonic-gate line_buf_expand(struct line_buf *line)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	line->alloc += BUFSIZ;
2287c478bd9Sstevel@tonic-gate 	line->str = (char *)realloc(line->str, line->alloc);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if (line->str == NULL) {
2317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2327c478bd9Sstevel@tonic-gate 		    gettext("line_buf_expand: out of memory\n"));
2337c478bd9Sstevel@tonic-gate 		exit(1);
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate static void
2387c478bd9Sstevel@tonic-gate line_buf_init(struct line_buf *line)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	(void) memset((char *)line, 0, sizeof (*line));
2417c478bd9Sstevel@tonic-gate 	line_buf_expand(line);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate static int
2457c478bd9Sstevel@tonic-gate __s_add_attr(ns_ldap_entry_t *e, char *attrname, char *value)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	ns_ldap_attr_t	*a;
2487c478bd9Sstevel@tonic-gate 	char		*v;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	a = (ns_ldap_attr_t *)calloc(1, sizeof (ns_ldap_attr_t));
2517c478bd9Sstevel@tonic-gate 	if (a == NULL)
2527c478bd9Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2537c478bd9Sstevel@tonic-gate 	a->attrname = strdup(attrname);
2547c478bd9Sstevel@tonic-gate 	if (a->attrname == NULL) {
2557c478bd9Sstevel@tonic-gate 		free(a);
2567c478bd9Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 	a->attrvalue = (char **)calloc(1, sizeof (char **));
2597c478bd9Sstevel@tonic-gate 	if (a->attrvalue == NULL) {
2607c478bd9Sstevel@tonic-gate 		free(a->attrname);
2617c478bd9Sstevel@tonic-gate 		free(a);
2627c478bd9Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 	a->value_count = 1;
2657c478bd9Sstevel@tonic-gate 	a->attrvalue[0] = NULL;
2667c478bd9Sstevel@tonic-gate 	v = strdup(value);
2677c478bd9Sstevel@tonic-gate 	if (v == NULL) {
2687c478bd9Sstevel@tonic-gate 		free(a->attrname);
2697c478bd9Sstevel@tonic-gate 		free(a->attrvalue);
2707c478bd9Sstevel@tonic-gate 		free(a);
2717c478bd9Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 	a->attrvalue[0] = v;
2747c478bd9Sstevel@tonic-gate 	e->attr_pair[e->attr_count] = a;
2757c478bd9Sstevel@tonic-gate 	e->attr_count++;
2767c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate static int
2807c478bd9Sstevel@tonic-gate __s_add_attrlist(ns_ldap_entry_t *e, char *attrname, char **argv)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	ns_ldap_attr_t	*a;
2837c478bd9Sstevel@tonic-gate 	char		*v;
2847c478bd9Sstevel@tonic-gate 	char		**av;
2857c478bd9Sstevel@tonic-gate 	int		i, j;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	a = (ns_ldap_attr_t *)calloc(1, sizeof (ns_ldap_attr_t));
2887c478bd9Sstevel@tonic-gate 	if (a == NULL)
2897c478bd9Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2907c478bd9Sstevel@tonic-gate 	a->attrname = strdup(attrname);
2917c478bd9Sstevel@tonic-gate 	if (a->attrname == NULL) {
2927c478bd9Sstevel@tonic-gate 		free(a);
2937c478bd9Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	for (i = 0, av = argv; *av != NULL; av++, i++)
2977c478bd9Sstevel@tonic-gate 		;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	a->attrvalue = (char **)calloc(i, sizeof (char **));
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (a->attrvalue == NULL) {
3027c478bd9Sstevel@tonic-gate 		free(a->attrname);
3037c478bd9Sstevel@tonic-gate 		free(a);
3047c478bd9Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 	a->value_count = i;
3077c478bd9Sstevel@tonic-gate 	for (j = 0; j < i; j++) {
3087c478bd9Sstevel@tonic-gate 		v = strdup(argv[j]);
3097c478bd9Sstevel@tonic-gate 		if (v == NULL) {
3107c478bd9Sstevel@tonic-gate 			free(a->attrname);
3117c478bd9Sstevel@tonic-gate 			free(a->attrvalue);
3127c478bd9Sstevel@tonic-gate 			free(a);
3137c478bd9Sstevel@tonic-gate 			return (NS_LDAP_MEMORY);
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 		a->attrvalue[j] = v;
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 	e->attr_pair[e->attr_count] = a;
3187c478bd9Sstevel@tonic-gate 	e->attr_count++;
3197c478bd9Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate static ns_ldap_entry_t *
3237c478bd9Sstevel@tonic-gate __s_mk_entry(char **objclass, int max_attr)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	ns_ldap_entry_t *e;
3267c478bd9Sstevel@tonic-gate 	e = (ns_ldap_entry_t *)calloc(1, sizeof (ns_ldap_entry_t));
3277c478bd9Sstevel@tonic-gate 	if (e == NULL)
3287c478bd9Sstevel@tonic-gate 		return (NULL);
3297c478bd9Sstevel@tonic-gate 	e->attr_pair = (ns_ldap_attr_t **)calloc(max_attr+1,
330e1dd0a2fSth 	    sizeof (ns_ldap_attr_t *));
3317c478bd9Sstevel@tonic-gate 	if (e->attr_pair == NULL) {
3327c478bd9Sstevel@tonic-gate 		free(e);
3337c478bd9Sstevel@tonic-gate 		return (NULL);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 	e->attr_count = 0;
3367c478bd9Sstevel@tonic-gate 	if (__s_add_attrlist(e, "objectClass", objclass) != NS_LDAP_SUCCESS) {
3377c478bd9Sstevel@tonic-gate 		free(e->attr_pair);
3387c478bd9Sstevel@tonic-gate 		free(e);
3397c478bd9Sstevel@tonic-gate 		return (NULL);
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 	return (e);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate static void
3457c478bd9Sstevel@tonic-gate ldap_freeEntry(ns_ldap_entry_t *ep)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	int		j, k = 0;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	if (ep == NULL)
3507c478bd9Sstevel@tonic-gate 		return;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	if (ep->attr_pair == NULL) {
3537c478bd9Sstevel@tonic-gate 		free(ep);
3547c478bd9Sstevel@tonic-gate 		return;
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 	for (j = 0; j < ep->attr_count; j++) {
3577c478bd9Sstevel@tonic-gate 		if (ep->attr_pair[j] == NULL)
3587c478bd9Sstevel@tonic-gate 			continue;
3597c478bd9Sstevel@tonic-gate 		if (ep->attr_pair[j]->attrname)
3607c478bd9Sstevel@tonic-gate 			free(ep->attr_pair[j]->attrname);
3617c478bd9Sstevel@tonic-gate 		if (ep->attr_pair[j]->attrvalue) {
3627c478bd9Sstevel@tonic-gate 			for (k = 0; (k < ep->attr_pair[j]->value_count) &&
363e1dd0a2fSth 			    (ep->attr_pair[j]->attrvalue[k]); k++) {
3647c478bd9Sstevel@tonic-gate 				free(ep->attr_pair[j]->attrvalue[k]);
3657c478bd9Sstevel@tonic-gate 			}
3667c478bd9Sstevel@tonic-gate 			free(ep->attr_pair[j]->attrvalue);
3677c478bd9Sstevel@tonic-gate 		}
3687c478bd9Sstevel@tonic-gate 		free(ep->attr_pair[j]);
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 	free(ep->attr_pair);
3717c478bd9Sstevel@tonic-gate 	free(ep);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate static int
3757c478bd9Sstevel@tonic-gate addentry(void *entry, int mod)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	int		 result = 0;
3787c478bd9Sstevel@tonic-gate 	ns_ldap_error_t	 *eres = NULL;
3797c478bd9Sstevel@tonic-gate 	int		rc = 1;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	/*  adds entry into the LDAP tree */
3837c478bd9Sstevel@tonic-gate 	if (mod)
3847c478bd9Sstevel@tonic-gate 		result = __ns_ldap_addTypedEntry(databasetype, inputbasedn,
385e1dd0a2fSth 		    entry, 0, &authority, NS_LDAP_FOLLOWREF | NS_LDAP_KEEP_CONN,
386e1dd0a2fSth 		    &eres);
3877c478bd9Sstevel@tonic-gate 	else
3887c478bd9Sstevel@tonic-gate 		result = __ns_ldap_addTypedEntry(databasetype, inputbasedn,
389e1dd0a2fSth 		    entry, 1, &authority, NS_LDAP_FOLLOWREF | NS_LDAP_KEEP_CONN,
390e1dd0a2fSth 		    &eres);
3917c478bd9Sstevel@tonic-gate 	/*
3927c478bd9Sstevel@tonic-gate 	 *  Return	0 on success
3937c478bd9Sstevel@tonic-gate 	 *		LDAP_ALREADY_EXISTS if entry exists already
3947c478bd9Sstevel@tonic-gate 	 *		1 for all other non-fatal errors.
3957c478bd9Sstevel@tonic-gate 	 *  Exit on fatal errors.
3967c478bd9Sstevel@tonic-gate 	 */
3977c478bd9Sstevel@tonic-gate 	switch (result) {
3987c478bd9Sstevel@tonic-gate 	case NS_LDAP_SUCCESS:
3997c478bd9Sstevel@tonic-gate 		nent_add++;
4007c478bd9Sstevel@tonic-gate 		rc = 0;
4017c478bd9Sstevel@tonic-gate 		break;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	case NS_LDAP_OP_FAILED:
4047c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("operation failed.\n"));
4057c478bd9Sstevel@tonic-gate 		rc = 1;
4067c478bd9Sstevel@tonic-gate 		break;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	case NS_LDAP_INVALID_PARAM:
4097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4107c478bd9Sstevel@tonic-gate 		    gettext("invalid parameter(s) passed.\n"));
4117c478bd9Sstevel@tonic-gate 		rc = 1;
4127c478bd9Sstevel@tonic-gate 		break;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	case NS_LDAP_NOTFOUND:
4157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("entry not found.\n"));
4167c478bd9Sstevel@tonic-gate 		rc = 1;
4177c478bd9Sstevel@tonic-gate 		break;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	case NS_LDAP_MEMORY:
4207c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4217c478bd9Sstevel@tonic-gate 		    gettext("internal memory allocation error.\n"));
4227c478bd9Sstevel@tonic-gate 		exit(1);
4237c478bd9Sstevel@tonic-gate 		break;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	case NS_LDAP_CONFIG:
4267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4277c478bd9Sstevel@tonic-gate 		    gettext("LDAP Configuration problem.\n"));
4287c478bd9Sstevel@tonic-gate 		perr(eres);
4297c478bd9Sstevel@tonic-gate 		exit(1);
4307c478bd9Sstevel@tonic-gate 		break;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	case NS_LDAP_PARTIAL:
4337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4347c478bd9Sstevel@tonic-gate 		    gettext("partial result returned\n"));
4357c478bd9Sstevel@tonic-gate 		perr(eres);
4367c478bd9Sstevel@tonic-gate 		rc = 1;
4377c478bd9Sstevel@tonic-gate 		break;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	case NS_LDAP_INTERNAL:
44012fbe00aSjs 		if (eres->status == LDAP_ALREADY_EXISTS ||
441e1dd0a2fSth 		    eres->status == LDAP_NO_SUCH_OBJECT)
4427c478bd9Sstevel@tonic-gate 			rc = eres->status;
443cb5caa98Sdjl 		else if (eres->status == LDAP_INSUFFICIENT_ACCESS) {
444cb5caa98Sdjl 			(void) fprintf(stderr,
445e1dd0a2fSth 			    gettext("The user does not have permission"
446e1dd0a2fSth 			    " to add/modify entries\n"));
447cb5caa98Sdjl 			perr(eres);
448cb5caa98Sdjl 			exit(1);
449cb5caa98Sdjl 		} else {
4507c478bd9Sstevel@tonic-gate 			rc = 1;
4517c478bd9Sstevel@tonic-gate 			perr(eres);
4527c478bd9Sstevel@tonic-gate 		}
4537c478bd9Sstevel@tonic-gate 		break;
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	if (eres)
4577c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&eres);
4587c478bd9Sstevel@tonic-gate 	return (rc);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate /*
4627c478bd9Sstevel@tonic-gate  * usage(char *msg)
4637c478bd9Sstevel@tonic-gate  * Display usage message to STDERR.
4647c478bd9Sstevel@tonic-gate  */
4657c478bd9Sstevel@tonic-gate static void
466*c59d9dffSToomas Soome usage(char *msg)
467*c59d9dffSToomas Soome {
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if (msg)
470e1dd0a2fSth 		(void) fprintf(stderr, "%s\n", msg);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
473e1dd0a2fSth 	"usage: ldapaddent [-cpv] [-a authenticationMethod] [-b baseDN]\n"
474e1dd0a2fSth 	"-D bindDN [-w bindPassword] [-j passwdFile] [-f filename]\n"
475e1dd0a2fSth 	"database\n"
476e1dd0a2fSth 	"\n"
477e1dd0a2fSth 	"usage: ldapaddent  [-cpv] -asasl/GSSAPI [-b baseDN] [-f filename]\n"
478e1dd0a2fSth 	"database\n"
479e1dd0a2fSth 	"\n"
480e1dd0a2fSth 	"usage: ldapaddent  -d [-v] [-a authenticationMethod] [-D bindDN]\n"
481e1dd0a2fSth 	"[-w bindPassword] [-j passwdFile] database\n"
482e1dd0a2fSth 	"\n"
483e1dd0a2fSth 	"usage: ldapaddent [-cpv] -h LDAP_server[:serverPort] [-M domainName]\n"
484e1dd0a2fSth 	"[-N  profileName]  [-P certifPath]  [-a authenticationMethod]\n"
485e1dd0a2fSth 	"[-b baseDN] -D bindDN [-w bindPassword] [-f filename]\n"
486e1dd0a2fSth 	"[-j passwdFile] database\n"
487e1dd0a2fSth 	"\n"
488e1dd0a2fSth 	"usage: ldapaddent [-cpv] -h LDAP_server[:serverPort] [-M domainName]\n"
489e1dd0a2fSth 	"[-N  profileName]  [-P certifPath] -asasl/GSSAPI  [-b baseDN]\n"
490e1dd0a2fSth 	"[-f filename] database\n"
491e1dd0a2fSth 	"\n"
492e1dd0a2fSth 	"usage: ldapaddent -d [-v] -h LDAP_server[:serverPort]"
493e1dd0a2fSth 	" [-M domainName]\n"
494e1dd0a2fSth 	"[-N profileName]  [-P certifPath]  [-a authenticationMethod]\n"
495e1dd0a2fSth 	"[-b baseDN] -D bindDN [-w bindPassword] [-j passwdFile]\n"
496e1dd0a2fSth 	"database\n"));
4977c478bd9Sstevel@tonic-gate 	exit(1);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate  * Determine if the given string is an IP address (IPv4 or IPv6).
5027c478bd9Sstevel@tonic-gate  * If so, it's converted to the preferred form (rfc2373) and
5037c478bd9Sstevel@tonic-gate  * *newaddr will point to the new address.
5047c478bd9Sstevel@tonic-gate  *
5057c478bd9Sstevel@tonic-gate  * Returns	-2		: inet_ntop error
5067c478bd9Sstevel@tonic-gate  *		-1		: not an IP address
5077c478bd9Sstevel@tonic-gate  *		0		: unsupported IP address (future use)
5087c478bd9Sstevel@tonic-gate  *		AF_INET		: IPv4
5097c478bd9Sstevel@tonic-gate  *		AF_INET6	: IPv6
5107c478bd9Sstevel@tonic-gate  */
5117c478bd9Sstevel@tonic-gate static int
512*c59d9dffSToomas Soome check_ipaddr(char *addr, char **newaddr)
513*c59d9dffSToomas Soome {
5147c478bd9Sstevel@tonic-gate 	ipaddr_t	addr_ipv4 = 0;
5157c478bd9Sstevel@tonic-gate 	in6_addr_t	addr_ipv6;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	/* IPv6 */
5187c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET6, addr, &addr_ipv6) == 1) {
5197c478bd9Sstevel@tonic-gate 		if (newaddr == NULL)
5207c478bd9Sstevel@tonic-gate 			return (AF_INET6);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 		/* Convert IPv4-mapped IPv6 address to IPv4 */
5237c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&addr_ipv6) ||
524*c59d9dffSToomas Soome 		    IN6_IS_ADDR_V4COMPAT(&addr_ipv6)) {
5257c478bd9Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&addr_ipv6, addr_ipv4);
5267c478bd9Sstevel@tonic-gate 			if ((*newaddr = calloc(1, INET_ADDRSTRLEN)) == NULL) {
5277c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5287c478bd9Sstevel@tonic-gate 				    gettext("out of memory\n"));
5297c478bd9Sstevel@tonic-gate 				exit(1);
5307c478bd9Sstevel@tonic-gate 			}
5317c478bd9Sstevel@tonic-gate 			if (inet_ntop(AF_INET, &addr_ipv4, *newaddr,
5327c478bd9Sstevel@tonic-gate 			    INET_ADDRSTRLEN))
5337c478bd9Sstevel@tonic-gate 				return (AF_INET6);
5347c478bd9Sstevel@tonic-gate 			free(*newaddr);
5357c478bd9Sstevel@tonic-gate 			return (-2);
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		/* Processing general IPv6 addresses */
5397c478bd9Sstevel@tonic-gate 		if ((*newaddr = calloc(1, INET6_ADDRSTRLEN)) == NULL) {
5407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
5417c478bd9Sstevel@tonic-gate 			exit(1);
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 		if (inet_ntop(AF_INET6, &addr_ipv6, *newaddr, INET6_ADDRSTRLEN))
5447c478bd9Sstevel@tonic-gate 			return (AF_INET6);
5457c478bd9Sstevel@tonic-gate 		free(*newaddr);
5467c478bd9Sstevel@tonic-gate 		return (-2);
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	/* Processing IPv4 addresses of the type d.d.d.d. */
5507c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET, addr, &addr_ipv4) == 1) {
5517c478bd9Sstevel@tonic-gate 		if (newaddr == NULL)
5527c478bd9Sstevel@tonic-gate 			return (AF_INET);
5537c478bd9Sstevel@tonic-gate 		if ((*newaddr = calloc(1, INET_ADDRSTRLEN)) == NULL) {
5547c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
5557c478bd9Sstevel@tonic-gate 			exit(1);
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 		if (inet_ntop(AF_INET, &addr_ipv4, *newaddr, INET_ADDRSTRLEN))
5587c478bd9Sstevel@tonic-gate 			return (AF_INET);
5597c478bd9Sstevel@tonic-gate 		free(*newaddr);
5607c478bd9Sstevel@tonic-gate 		return (-2);
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	/* Processing IPv4 addresses d.d.d , d.d and d */
5647c478bd9Sstevel@tonic-gate 	if (inet_addr(addr) != (in_addr_t)-1) {
5657c478bd9Sstevel@tonic-gate 		if (newaddr == NULL)
5667c478bd9Sstevel@tonic-gate 			return (AF_INET);
5677c478bd9Sstevel@tonic-gate 		if ((*newaddr = strdup(addr)) == NULL) {
5687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
5697c478bd9Sstevel@tonic-gate 			exit(1);
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 		return (AF_INET);
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	return (-1);
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
577e1dd0a2fSth /*
578e1dd0a2fSth  * Verifies that project name meets the restrictions defined by project(4).
579e1dd0a2fSth  */
580e1dd0a2fSth static int
581e1dd0a2fSth check_projname(char *addr)
582e1dd0a2fSth {
583e1dd0a2fSth 	int i;
584e1dd0a2fSth 	if (addr == NULL || *addr == '\0')
585e1dd0a2fSth 		return (-1);
586e1dd0a2fSth 
587e1dd0a2fSth 	for (i = 0; i < strlen(addr); i++) {
588e1dd0a2fSth 		if (!isalpha(addr[i]) &&
589e1dd0a2fSth 		    !isdigit(addr[i]) &&
590e1dd0a2fSth 		    addr[i] != '_' &&
591e1dd0a2fSth 		    addr[i] != '-' &&
592e1dd0a2fSth 		    addr[i] != '.')
593e1dd0a2fSth 			return (-1);
594e1dd0a2fSth 	}
595e1dd0a2fSth 
596e1dd0a2fSth 	return (0);
597e1dd0a2fSth }
598e1dd0a2fSth 
5997c478bd9Sstevel@tonic-gate static int
6007c478bd9Sstevel@tonic-gate genent_hosts(char *line, int (*cback)())
6017c478bd9Sstevel@tonic-gate {
6027c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ+1];
6036d3c3c6aSiz 	char *t, *comment;
6047c478bd9Sstevel@tonic-gate 	entry_col ecol[4];
6057c478bd9Sstevel@tonic-gate 	char *cname, *pref_addr;
6067c478bd9Sstevel@tonic-gate 	int ctr = 0, retval = 1;
6077c478bd9Sstevel@tonic-gate 	int rc = GENENT_OK, af;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	struct hostent  data;
6107c478bd9Sstevel@tonic-gate 	char *alias;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	/*
6137c478bd9Sstevel@tonic-gate 	 * don't clobber our argument
6147c478bd9Sstevel@tonic-gate 	 */
6157c478bd9Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
616e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
617e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
6187c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, line);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	/*
6237c478bd9Sstevel@tonic-gate 	 * clear column data
6247c478bd9Sstevel@tonic-gate 	 */
6257c478bd9Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	/*
6287c478bd9Sstevel@tonic-gate 	 * comment (col 3)
6296d3c3c6aSiz 	 * All leading spaces will be deleted from the comment
6307c478bd9Sstevel@tonic-gate 	 */
6316d3c3c6aSiz 	ecol[3].ec_value.ec_value_val = "";
6326d3c3c6aSiz 	ecol[3].ec_value.ec_value_len = 0;
6336d3c3c6aSiz 	comment = t = strchr(buf, '#');
6346d3c3c6aSiz 	if (comment) {
6356d3c3c6aSiz 		do {
6366d3c3c6aSiz 			++comment;
6376d3c3c6aSiz 		} while (*comment != '\0' && isspace(*comment));
6386d3c3c6aSiz 		if (*comment != '\0') {
6396d3c3c6aSiz 			*--comment = '#';
6406d3c3c6aSiz 			ecol[3].ec_value.ec_value_val = strdup(comment);
6416d3c3c6aSiz 			ecol[3].ec_value.ec_value_len = strlen(comment)+1;
6426d3c3c6aSiz 		}
6437c478bd9Sstevel@tonic-gate 
6446d3c3c6aSiz 		*t = '\0';
6456d3c3c6aSiz 	}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	/*
6487c478bd9Sstevel@tonic-gate 	 * addr(col 2)
6497c478bd9Sstevel@tonic-gate 	 */
6507c478bd9Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
651e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no host"),
652e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
6537c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	af = check_ipaddr(t, &pref_addr);
6577c478bd9Sstevel@tonic-gate 	if (af == -2) {
658e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("Internal error"),
659e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
6607c478bd9Sstevel@tonic-gate 	} else if (af == -1) {
6617c478bd9Sstevel@tonic-gate 		(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
662e1dd0a2fSth 		    gettext("Invalid IP address: %s"), t);
6637c478bd9Sstevel@tonic-gate 	} else if (flags & F_VERBOSE) {
6647c478bd9Sstevel@tonic-gate 		if ((strncasecmp(t, pref_addr, strlen(t))) != 0) {
6657c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout,
6667c478bd9Sstevel@tonic-gate 			    gettext("IP address %s converted to %s\n"),
6677c478bd9Sstevel@tonic-gate 			    t, pref_addr);
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	if (af < 0) {
672e1dd0a2fSth 		(void) fprintf(stderr, "%s\n", parse_err_msg);
6737c478bd9Sstevel@tonic-gate 		if (continue_onerror == 0)
6747c478bd9Sstevel@tonic-gate 			return (GENENT_CBERR);
6757c478bd9Sstevel@tonic-gate 		else
6767c478bd9Sstevel@tonic-gate 			return (rc);
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = pref_addr;
6807c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(pref_addr)+1;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	/*
6837c478bd9Sstevel@tonic-gate 	 * cname (col 0)
6847c478bd9Sstevel@tonic-gate 	 */
6857c478bd9Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
686e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no cname"),
687e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
6887c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
6917c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
6927c478bd9Sstevel@tonic-gate 	cname = t;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	/* build entry */
6967c478bd9Sstevel@tonic-gate 	if ((data.h_addr_list = (char **)calloc(2, sizeof (char **))) == NULL) {
6977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
6987c478bd9Sstevel@tonic-gate 		exit(1);
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 	data.h_addr_list[0] = strdup(ecol[2].ec_value.ec_value_val);
7017c478bd9Sstevel@tonic-gate 	data.h_addr_list[1] = NULL;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	free(pref_addr);
7047c478bd9Sstevel@tonic-gate 	data.h_name = strdup(ecol[0].ec_value.ec_value_val);
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	/*
7077c478bd9Sstevel@tonic-gate 	 * name (col 1)
7087c478bd9Sstevel@tonic-gate 	 */
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	data.h_aliases = NULL;
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	do {
7137c478bd9Sstevel@tonic-gate 		/*
7147c478bd9Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
7157c478bd9Sstevel@tonic-gate 		 */
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 		/* This call to AddEntry may move out of the loop */
7187c478bd9Sstevel@tonic-gate 		/* This is because we have to call the function just once */
7197c478bd9Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
7207c478bd9Sstevel@tonic-gate 			continue;
7217c478bd9Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
7227c478bd9Sstevel@tonic-gate 			continue;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
7257c478bd9Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 		ctr++;
7287c478bd9Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
7297c478bd9Sstevel@tonic-gate 		if ((data.h_aliases = (char **)realloc(data.h_aliases,
730e1dd0a2fSth 		    ctr * sizeof (char **))) == NULL) {
7317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
7327c478bd9Sstevel@tonic-gate 			exit(1);
7337c478bd9Sstevel@tonic-gate 		}
7347c478bd9Sstevel@tonic-gate 		data.h_aliases[ctr-1] = alias;
7357c478bd9Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
7367c478bd9Sstevel@tonic-gate 
7376d3c3c6aSiz 	/*
7386d3c3c6aSiz 	 * End the list of all the aliases by NULL
7396d3c3c6aSiz 	 * If there is some comment, it will be stored as the last entry
7406d3c3c6aSiz 	 * in the list of the host aliases
7416d3c3c6aSiz 	 */
7427c478bd9Sstevel@tonic-gate 	if ((data.h_aliases = (char **)realloc(data.h_aliases,
743e1dd0a2fSth 	    (ecol[3].ec_value.ec_value_len != 0 ?
744e1dd0a2fSth 	    ctr + 2 : ctr + 1) * sizeof (char **))) == NULL) {
7457c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
7467c478bd9Sstevel@tonic-gate 		exit(1);
7477c478bd9Sstevel@tonic-gate 	}
7486d3c3c6aSiz 
7496d3c3c6aSiz 	if (ecol[3].ec_value.ec_value_len != 0) {
7506d3c3c6aSiz 		data.h_aliases[ctr++] = ecol[3].ec_value.ec_value_val;
7516d3c3c6aSiz 	}
7527c478bd9Sstevel@tonic-gate 	data.h_aliases[ctr] = NULL;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	if (flags & F_VERBOSE)
7557c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout,
7567c478bd9Sstevel@tonic-gate 		    gettext("Adding entry : cn=%s+ipHostNumber=%s\n"),
7577c478bd9Sstevel@tonic-gate 		    data.h_name, data.h_addr_list[0]);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
7607c478bd9Sstevel@tonic-gate 
7616d3c3c6aSiz 	if (ecol[3].ec_value.ec_value_len != 0) {
7626d3c3c6aSiz 		free(ecol[3].ec_value.ec_value_val);
7636d3c3c6aSiz 	}
7646d3c3c6aSiz 
7657c478bd9Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
7667c478bd9Sstevel@tonic-gate 		if (continue_onerror)
7677c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
768e1dd0a2fSth 			    gettext("Entry: cn=%s+ipHostNumber=%s "
769e1dd0a2fSth 			    "already Exists -skipping it\n"),
770e1dd0a2fSth 			    data.h_name, data.h_addr_list[0]);
7717c478bd9Sstevel@tonic-gate 		else {
7727c478bd9Sstevel@tonic-gate 			rc = GENENT_CBERR;
7737c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
774e1dd0a2fSth 			    gettext("Entry: cn=%s+ipHostNumber=%s"
775e1dd0a2fSth 			    " already Exists\n"),
776e1dd0a2fSth 			    data.h_name, data.h_addr_list[0]);
7777c478bd9Sstevel@tonic-gate 		}
7787c478bd9Sstevel@tonic-gate 	} else if (retval)
7797c478bd9Sstevel@tonic-gate 		rc = GENENT_CBERR;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	free(data.h_name);
7827c478bd9Sstevel@tonic-gate 	free(data.h_aliases);
7837c478bd9Sstevel@tonic-gate 	free(data.h_addr_list);
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	return (rc);
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate static void
7917c478bd9Sstevel@tonic-gate dump_hosts(ns_ldap_result_t *res)
7927c478bd9Sstevel@tonic-gate {
7936d3c3c6aSiz 	ns_ldap_attr_t	*attrptr = NULL,
794e1dd0a2fSth 	    *cn = NULL,
795e1dd0a2fSth 	    *iphostnumber = NULL,
796e1dd0a2fSth 	    *desc = NULL;
7977c478bd9Sstevel@tonic-gate 	int		 i, j;
7987c478bd9Sstevel@tonic-gate 	char		*name; /* host name */
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
8017c478bd9Sstevel@tonic-gate 		return;
8027c478bd9Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
8037c478bd9Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
8047c478bd9Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
8057c478bd9Sstevel@tonic-gate 			cn = attrptr;
8067c478bd9Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "iphostnumber") == 0)
8077c478bd9Sstevel@tonic-gate 			iphostnumber = attrptr;
8086d3c3c6aSiz 		else if (strcasecmp(attrptr->attrname, "description") == 0) {
8096d3c3c6aSiz 			desc = attrptr;
8106d3c3c6aSiz 		}
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 	/* sanity check */
8137c478bd9Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
8147c478bd9Sstevel@tonic-gate 	    iphostnumber == NULL || iphostnumber->attrvalue == NULL ||
8157c478bd9Sstevel@tonic-gate 	    iphostnumber->attrvalue[0] == NULL)
8167c478bd9Sstevel@tonic-gate 		return;
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
8197c478bd9Sstevel@tonic-gate 		return;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	/* ip host/ipnode number */
8227c478bd9Sstevel@tonic-gate 	if (strlen(iphostnumber->attrvalue[0]) <= INET_ADDRSTRLEN)
8237c478bd9Sstevel@tonic-gate 		/* IPV4 or IPV6 but <= NET_ADDRSTRLEN */
8247c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%-18s", iphostnumber->attrvalue[0]);
8257c478bd9Sstevel@tonic-gate 	else
8267c478bd9Sstevel@tonic-gate 		/* IPV6 */
8277c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%-48s", iphostnumber->attrvalue[0]);
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	/* host/ipnode name */
8307c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "%s ", name);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	/* aliases */
8337c478bd9Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
8347c478bd9Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
8357c478bd9Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0)
8367c478bd9Sstevel@tonic-gate 				/* skip host name */
8377c478bd9Sstevel@tonic-gate 				continue;
8387c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
8397c478bd9Sstevel@tonic-gate 		}
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 
8426d3c3c6aSiz 	/* description */
8436d3c3c6aSiz 	if (desc != NULL && desc->attrvalue != NULL &&
8446d3c3c6aSiz 	    desc->attrvalue[0] != NULL) {
8456d3c3c6aSiz 		(void) fprintf(stdout, "#%s", desc->attrvalue[0]);
8466d3c3c6aSiz 	}
8476d3c3c6aSiz 
8487c478bd9Sstevel@tonic-gate 	/* end of line */
8497c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate /*
8537c478bd9Sstevel@tonic-gate  * /etc/rpc
8547c478bd9Sstevel@tonic-gate  */
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate static int
8577c478bd9Sstevel@tonic-gate genent_rpc(char *line, int (*cback)())
8587c478bd9Sstevel@tonic-gate {
8597c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ+1];
8607c478bd9Sstevel@tonic-gate 	char *t;
8617c478bd9Sstevel@tonic-gate 	entry_col ecol[4];
8627c478bd9Sstevel@tonic-gate 	char *cname;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	struct rpcent	data;
8657c478bd9Sstevel@tonic-gate 	char *alias;
8667c478bd9Sstevel@tonic-gate 	int ctr = 0;
8677c478bd9Sstevel@tonic-gate 	int retval = 1;
8687c478bd9Sstevel@tonic-gate 	int rc = GENENT_OK;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	/*
8717c478bd9Sstevel@tonic-gate 	 * don't clobber our argument
8727c478bd9Sstevel@tonic-gate 	 */
8737c478bd9Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
874e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
875e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
8767c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, line);
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	/*
8817c478bd9Sstevel@tonic-gate 	 * clear column data
8827c478bd9Sstevel@tonic-gate 	 */
8837c478bd9Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	/*
8867c478bd9Sstevel@tonic-gate 	 * comment (col 3)
8877c478bd9Sstevel@tonic-gate 	 */
8887c478bd9Sstevel@tonic-gate 	t = strchr(buf, '#');
8897c478bd9Sstevel@tonic-gate 	if (t) {
8907c478bd9Sstevel@tonic-gate 		*t++ = 0;
8917c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
8927c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
8937c478bd9Sstevel@tonic-gate 	} else {
8947c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
8957c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
8967c478bd9Sstevel@tonic-gate 	}
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	/*
8997c478bd9Sstevel@tonic-gate 	 * cname(col 0)
9007c478bd9Sstevel@tonic-gate 	 */
9017c478bd9Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
902e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no number"),
903e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
9047c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
9077c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
9087c478bd9Sstevel@tonic-gate 	cname = t;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	/*
9117c478bd9Sstevel@tonic-gate 	 * number (col 2)
9127c478bd9Sstevel@tonic-gate 	 */
9137c478bd9Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
914e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no number"),
915e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
9167c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
9177c478bd9Sstevel@tonic-gate 	}
9187c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
9197c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	/*
9237c478bd9Sstevel@tonic-gate 	 * build entry
9247c478bd9Sstevel@tonic-gate 	 */
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	data.r_name = strdup(ecol[0].ec_value.ec_value_val);
9277c478bd9Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
928e1dd0a2fSth 	    ecol[2].ec_value.ec_value_val[0] != '\0') {
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 		data.r_number = ascii_to_int(ecol[2].ec_value.ec_value_val);
9317c478bd9Sstevel@tonic-gate 		if (data.r_number == -1) {
9327c478bd9Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
933e1dd0a2fSth 			    gettext("invalid program number: %s"),
9347c478bd9Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
9357c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
9367c478bd9Sstevel@tonic-gate 		}
9377c478bd9Sstevel@tonic-gate 	} else
9387c478bd9Sstevel@tonic-gate 		data.r_number = -1;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	/*
9417c478bd9Sstevel@tonic-gate 	 * name (col 1)
9427c478bd9Sstevel@tonic-gate 	 */
9437c478bd9Sstevel@tonic-gate 	t = cname;
9447c478bd9Sstevel@tonic-gate 	data.r_aliases = NULL;
9457c478bd9Sstevel@tonic-gate 	do {
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 		/*
9487c478bd9Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
9497c478bd9Sstevel@tonic-gate 		 */
9507c478bd9Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
9517c478bd9Sstevel@tonic-gate 			continue;
9527c478bd9Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
9537c478bd9Sstevel@tonic-gate 			continue;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
9567c478bd9Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 		ctr++;
9597c478bd9Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
9607c478bd9Sstevel@tonic-gate 		if ((data.r_aliases = (char **)realloc(data.r_aliases,
961e1dd0a2fSth 		    ctr * sizeof (char **))) == NULL) {
9627c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
9637c478bd9Sstevel@tonic-gate 			exit(1);
9647c478bd9Sstevel@tonic-gate 		}
9657c478bd9Sstevel@tonic-gate 		data.r_aliases[ctr-1] = alias;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 		/*
9697c478bd9Sstevel@tonic-gate 		 * only put comment in canonical entry
9707c478bd9Sstevel@tonic-gate 		 */
9717c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
9727c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
9777c478bd9Sstevel@tonic-gate 	if ((data.r_aliases = (char **)realloc(data.r_aliases,
978e1dd0a2fSth 	    (ctr + 1) * sizeof (char **))) == NULL) {
9797c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
9807c478bd9Sstevel@tonic-gate 		exit(1);
9817c478bd9Sstevel@tonic-gate 	}
9827c478bd9Sstevel@tonic-gate 	data.r_aliases[ctr] = NULL;
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	if (flags & F_VERBOSE)
9857c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout,
9867c478bd9Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.r_name);
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
9917c478bd9Sstevel@tonic-gate 		if (continue_onerror)
9927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
993e1dd0a2fSth 			    gettext("Entry: %s - already Exists,"
994e1dd0a2fSth 			    " skipping it.\n"), data.r_name);
9957c478bd9Sstevel@tonic-gate 		else {
9967c478bd9Sstevel@tonic-gate 			rc = GENENT_CBERR;
9977c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
998e1dd0a2fSth 			    gettext("Entry: %s - already Exists\n"),
999e1dd0a2fSth 			    data.r_name);
10007c478bd9Sstevel@tonic-gate 		}
10017c478bd9Sstevel@tonic-gate 	} else if (retval)
10027c478bd9Sstevel@tonic-gate 		rc = GENENT_CBERR;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	free(data.r_name);
10057c478bd9Sstevel@tonic-gate 	free(data.r_aliases);
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	return (rc);
10087c478bd9Sstevel@tonic-gate }
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate static void
10137c478bd9Sstevel@tonic-gate dump_rpc(ns_ldap_result_t *res)
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *rpcnumber = NULL;
10167c478bd9Sstevel@tonic-gate 	int		 i, j;
10177c478bd9Sstevel@tonic-gate 	char		*name; /* rpc name */
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
10207c478bd9Sstevel@tonic-gate 		return;
10217c478bd9Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
10227c478bd9Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
10237c478bd9Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
10247c478bd9Sstevel@tonic-gate 			cn = attrptr;
10257c478bd9Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "oncRpcNumber") == 0)
10267c478bd9Sstevel@tonic-gate 			rpcnumber = attrptr;
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate 	/* sanity check */
10297c478bd9Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
10307c478bd9Sstevel@tonic-gate 	    rpcnumber == NULL || rpcnumber->attrvalue == NULL ||
10317c478bd9Sstevel@tonic-gate 	    rpcnumber->attrvalue[0] == NULL)
10327c478bd9Sstevel@tonic-gate 		return;
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
10357c478bd9Sstevel@tonic-gate 		return;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	/* rpc name */
10387c478bd9Sstevel@tonic-gate 	if (strlen(name) < 8)
10397c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t\t", name);
10407c478bd9Sstevel@tonic-gate 	else
10417c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t", name);
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	/* rpc number */
10447c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "%-8s", rpcnumber->attrvalue[0]);
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	/* aliases */
10487c478bd9Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
10497c478bd9Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
10507c478bd9Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0)
10517c478bd9Sstevel@tonic-gate 				/* skip rpc name */
10527c478bd9Sstevel@tonic-gate 				continue;
10537c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
10547c478bd9Sstevel@tonic-gate 		}
10557c478bd9Sstevel@tonic-gate 	}
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	/* end of line */
10587c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate /*
10637c478bd9Sstevel@tonic-gate  * /etc/protocols
10647c478bd9Sstevel@tonic-gate  *
10657c478bd9Sstevel@tonic-gate  */
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate static int
10687c478bd9Sstevel@tonic-gate genent_protocols(char *line, int (*cback)())
10697c478bd9Sstevel@tonic-gate {
10707c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ+1];
10717c478bd9Sstevel@tonic-gate 	char *t;
10727c478bd9Sstevel@tonic-gate 	entry_col ecol[4];
10737c478bd9Sstevel@tonic-gate 	char *cname;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	struct protoent	data;
10767c478bd9Sstevel@tonic-gate 	char *alias;
10777c478bd9Sstevel@tonic-gate 	int ctr = 0;
10787c478bd9Sstevel@tonic-gate 	int retval = 1;
10797c478bd9Sstevel@tonic-gate 	int rc = GENENT_OK;
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	/*
10827c478bd9Sstevel@tonic-gate 	 * don't clobber our argument
10837c478bd9Sstevel@tonic-gate 	 */
10847c478bd9Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
1085e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
1086e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
10877c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, line);
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	/*
10927c478bd9Sstevel@tonic-gate 	 * clear column data
10937c478bd9Sstevel@tonic-gate 	 */
10947c478bd9Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	/*
10977c478bd9Sstevel@tonic-gate 	 * comment (col 3)
10987c478bd9Sstevel@tonic-gate 	 */
10997c478bd9Sstevel@tonic-gate 	t = strchr(buf, '#');
11007c478bd9Sstevel@tonic-gate 	if (t) {
11017c478bd9Sstevel@tonic-gate 		*t++ = 0;
11027c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
11037c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
11047c478bd9Sstevel@tonic-gate 	} else {
11057c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
11067c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
11077c478bd9Sstevel@tonic-gate 	}
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	/*
11107c478bd9Sstevel@tonic-gate 	 * cname(col 0)
11117c478bd9Sstevel@tonic-gate 	 */
11127c478bd9Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
1113e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no number"),
1114e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
11157c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
11187c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
11197c478bd9Sstevel@tonic-gate 	cname = t;
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	/*
11227c478bd9Sstevel@tonic-gate 	 * number (col 2)
11237c478bd9Sstevel@tonic-gate 	 */
11247c478bd9Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
1125e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no number"),
1126e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
11277c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
11307c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	/*
11347c478bd9Sstevel@tonic-gate 	 * build entry
11357c478bd9Sstevel@tonic-gate 	 */
11367c478bd9Sstevel@tonic-gate 	data.p_name = strdup(ecol[0].ec_value.ec_value_val);
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
1139e1dd0a2fSth 	    ecol[2].ec_value.ec_value_val[0] != '\0') {
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 		data.p_proto = ascii_to_int(ecol[2].ec_value.ec_value_val);
11427c478bd9Sstevel@tonic-gate 		if (data.p_proto == -1) {
11437c478bd9Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
1144e1dd0a2fSth 			    gettext("invalid protocol number: %s"),
11457c478bd9Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
11467c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
11477c478bd9Sstevel@tonic-gate 		}
11487c478bd9Sstevel@tonic-gate 	} else
11497c478bd9Sstevel@tonic-gate 		data.p_proto = -1;
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	/*
11527c478bd9Sstevel@tonic-gate 	 * name (col 1)
11537c478bd9Sstevel@tonic-gate 	 */
11547c478bd9Sstevel@tonic-gate 	t = cname;
11557c478bd9Sstevel@tonic-gate 	ctr = 0;
11567c478bd9Sstevel@tonic-gate 	data.p_aliases = NULL;
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	do {
11597c478bd9Sstevel@tonic-gate 		/*
11607c478bd9Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
11617c478bd9Sstevel@tonic-gate 		 */
11627c478bd9Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
11637c478bd9Sstevel@tonic-gate 			continue;
11647c478bd9Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
11657c478bd9Sstevel@tonic-gate 			continue;
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
11687c478bd9Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 		ctr++;
11717c478bd9Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
11727c478bd9Sstevel@tonic-gate 		if ((data.p_aliases = (char **)realloc(data.p_aliases,
1173e1dd0a2fSth 		    ctr * sizeof (char **))) == NULL) {
11747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
11757c478bd9Sstevel@tonic-gate 			exit(1);
11767c478bd9Sstevel@tonic-gate 		}
11777c478bd9Sstevel@tonic-gate 		data.p_aliases[ctr-1] = alias;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 		/*
11807c478bd9Sstevel@tonic-gate 		 * only put comment in canonical entry
11817c478bd9Sstevel@tonic-gate 		 */
11827c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
11837c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
11887c478bd9Sstevel@tonic-gate 	if ((data.p_aliases = (char **)realloc(data.p_aliases,
1189e1dd0a2fSth 	    (ctr + 1) * sizeof (char **))) == NULL) {
11907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
11917c478bd9Sstevel@tonic-gate 		exit(1);
11927c478bd9Sstevel@tonic-gate 	}
11937c478bd9Sstevel@tonic-gate 	data.p_aliases[ctr] = NULL;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	if (flags & F_VERBOSE)
11967c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout,
11977c478bd9Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.p_name);
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
12027c478bd9Sstevel@tonic-gate 		if (continue_onerror)
12037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1204e1dd0a2fSth 			    gettext("Entry: %s - already Exists,"
1205e1dd0a2fSth 			    " skipping it.\n"), data.p_name);
12067c478bd9Sstevel@tonic-gate 		else {
12077c478bd9Sstevel@tonic-gate 			rc = GENENT_CBERR;
12087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1209e1dd0a2fSth 			    gettext("Entry: %s - already Exists\n"),
1210e1dd0a2fSth 			    data.p_name);
12117c478bd9Sstevel@tonic-gate 		}
12127c478bd9Sstevel@tonic-gate 	} else if (retval)
12137c478bd9Sstevel@tonic-gate 		rc = GENENT_CBERR;
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	free(data.p_name);
12167c478bd9Sstevel@tonic-gate 	free(data.p_aliases);
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	return (rc);
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate static void
12237c478bd9Sstevel@tonic-gate dump_protocols(ns_ldap_result_t *res)
12247c478bd9Sstevel@tonic-gate {
12257c478bd9Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *protocolnumber = NULL;
12267c478bd9Sstevel@tonic-gate 	int		 i, j;
12277c478bd9Sstevel@tonic-gate 	char		*name, *cp;
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
12307c478bd9Sstevel@tonic-gate 		return;
12317c478bd9Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
12327c478bd9Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
12337c478bd9Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
12347c478bd9Sstevel@tonic-gate 			cn = attrptr;
12357c478bd9Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "ipProtocolNumber")
1236e1dd0a2fSth 		    == 0)
12377c478bd9Sstevel@tonic-gate 			protocolnumber = attrptr;
12387c478bd9Sstevel@tonic-gate 	}
12397c478bd9Sstevel@tonic-gate 	/* sanity check */
12407c478bd9Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
12417c478bd9Sstevel@tonic-gate 	    protocolnumber == NULL || protocolnumber->attrvalue == NULL ||
12427c478bd9Sstevel@tonic-gate 	    protocolnumber->attrvalue[0] == NULL)
12437c478bd9Sstevel@tonic-gate 		return;
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
12467c478bd9Sstevel@tonic-gate 		return;
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	/* protocol name */
12497c478bd9Sstevel@tonic-gate 	if (strlen(name) < 8)
12507c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t\t", name);
12517c478bd9Sstevel@tonic-gate 	else
12527c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t", name);
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	/* protocol number */
12557c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "%-16s", protocolnumber->attrvalue[0]);
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	/* aliases */
12587c478bd9Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
12597c478bd9Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
12607c478bd9Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0) {
12617c478bd9Sstevel@tonic-gate 				if (cn->value_count > 1)
12627c478bd9Sstevel@tonic-gate 					/* Do not replicate */
12637c478bd9Sstevel@tonic-gate 					continue;
12647c478bd9Sstevel@tonic-gate 				/*
12657c478bd9Sstevel@tonic-gate 				 * Replicate name in uppercase as an aliase
12667c478bd9Sstevel@tonic-gate 				 */
12677c478bd9Sstevel@tonic-gate 				for (cp = cn->attrvalue[j]; *cp; cp++)
12687c478bd9Sstevel@tonic-gate 					*cp = toupper(*cp);
12697c478bd9Sstevel@tonic-gate 			}
12707c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
12717c478bd9Sstevel@tonic-gate 		}
12727c478bd9Sstevel@tonic-gate 	}
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	/* end of line */
12757c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate }
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate /*
12847c478bd9Sstevel@tonic-gate  * /etc/networks
12857c478bd9Sstevel@tonic-gate  *
12867c478bd9Sstevel@tonic-gate  */
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate static int
12897c478bd9Sstevel@tonic-gate genent_networks(char *line, int (*cback)())
12907c478bd9Sstevel@tonic-gate {
12917c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ+1];
12927c478bd9Sstevel@tonic-gate 	char *t;
12937c478bd9Sstevel@tonic-gate 	entry_col ecol[4];
12947c478bd9Sstevel@tonic-gate 	char *cname;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	struct netent	data;
12977c478bd9Sstevel@tonic-gate 	char *alias;
12987c478bd9Sstevel@tonic-gate 	int ctr = 0;
12997c478bd9Sstevel@tonic-gate 	int retval = 1;
13007c478bd9Sstevel@tonic-gate 	int enet;
13017c478bd9Sstevel@tonic-gate 	int rc = GENENT_OK;
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	/*
13047c478bd9Sstevel@tonic-gate 	 * don't clobber our argument
13057c478bd9Sstevel@tonic-gate 	 */
13067c478bd9Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
1307e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
1308e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
13097c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
13107c478bd9Sstevel@tonic-gate 	}
13117c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, line);
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	/*
13147c478bd9Sstevel@tonic-gate 	 * clear column data
13157c478bd9Sstevel@tonic-gate 	 */
13167c478bd9Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	/*
13197c478bd9Sstevel@tonic-gate 	 * comment (col 3)
13207c478bd9Sstevel@tonic-gate 	 */
13217c478bd9Sstevel@tonic-gate 	t = strchr(buf, '#');
13227c478bd9Sstevel@tonic-gate 	if (t) {
13237c478bd9Sstevel@tonic-gate 		*t++ = 0;
13247c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
13257c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
13267c478bd9Sstevel@tonic-gate 	} else {
13277c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
13287c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
13297c478bd9Sstevel@tonic-gate 	}
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	/*
13327c478bd9Sstevel@tonic-gate 	 * cname(col 0)
13337c478bd9Sstevel@tonic-gate 	 */
13347c478bd9Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
1335e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no number"),
1336e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
13377c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
13387c478bd9Sstevel@tonic-gate 	}
13397c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
13407c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
13417c478bd9Sstevel@tonic-gate 	cname = t;
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	/*
13447c478bd9Sstevel@tonic-gate 	 * number (col 2)
13457c478bd9Sstevel@tonic-gate 	 */
13467c478bd9Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
1347e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no number"),
1348e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
13497c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
13507c478bd9Sstevel@tonic-gate 	}
13517c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
13527c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	/*
13567c478bd9Sstevel@tonic-gate 	 * build entry
13577c478bd9Sstevel@tonic-gate 	 */
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	data.n_name = strdup(ecol[0].ec_value.ec_value_val);
13607c478bd9Sstevel@tonic-gate 	/*
13617c478bd9Sstevel@tonic-gate 	 * data.n_net is an unsigned field,
13627c478bd9Sstevel@tonic-gate 	 * assign -1 to it, make no sense.
13637c478bd9Sstevel@tonic-gate 	 * Use enet here to avoid lint warning.
13647c478bd9Sstevel@tonic-gate 	 */
13657c478bd9Sstevel@tonic-gate 	enet = encode_network(ecol[2].ec_value.ec_value_val);
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	if (enet == -1 && continue_onerror == 0) {
13687c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Invalid network number\n"));
13697c478bd9Sstevel@tonic-gate 		if (continue_onerror == 0)
13707c478bd9Sstevel@tonic-gate 			return (GENENT_CBERR);
13717c478bd9Sstevel@tonic-gate 	} else
13727c478bd9Sstevel@tonic-gate 		data.n_net = enet;
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	/*
13757c478bd9Sstevel@tonic-gate 	 * name (col 1)
13767c478bd9Sstevel@tonic-gate 	 */
13777c478bd9Sstevel@tonic-gate 	t = cname;
13787c478bd9Sstevel@tonic-gate 	data.n_aliases = NULL;
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	do {
13817c478bd9Sstevel@tonic-gate 		/*
13827c478bd9Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
13837c478bd9Sstevel@tonic-gate 		 */
13847c478bd9Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
13857c478bd9Sstevel@tonic-gate 			continue;
13867c478bd9Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
13877c478bd9Sstevel@tonic-gate 			continue;
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
13907c478bd9Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 		ctr++;
13937c478bd9Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
13947c478bd9Sstevel@tonic-gate 		if ((data.n_aliases = (char **)realloc(data.n_aliases,
1395e1dd0a2fSth 		    ctr * sizeof (char **))) == NULL) {
13967c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
13977c478bd9Sstevel@tonic-gate 			exit(1);
13987c478bd9Sstevel@tonic-gate 		}
13997c478bd9Sstevel@tonic-gate 		data.n_aliases[ctr-1] = alias;
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 		/*
14027c478bd9Sstevel@tonic-gate 		 * only put comment in canonical entry
14037c478bd9Sstevel@tonic-gate 		 */
14047c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
14057c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
14107c478bd9Sstevel@tonic-gate 	if ((data.n_aliases = (char **)realloc(data.n_aliases,
1411e1dd0a2fSth 	    (ctr + 1) * sizeof (char **))) == NULL) {
14127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
14137c478bd9Sstevel@tonic-gate 		exit(1);
14147c478bd9Sstevel@tonic-gate 	}
14157c478bd9Sstevel@tonic-gate 	data.n_aliases[ctr] = NULL;
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	if (flags & F_VERBOSE)
14187c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout,
14197c478bd9Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.n_name);
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
14247c478bd9Sstevel@tonic-gate 		if (continue_onerror)
14257c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1426e1dd0a2fSth 			    gettext("Entry: %s - already Exists,"
1427e1dd0a2fSth 			    " skipping it.\n"), data.n_name);
14287c478bd9Sstevel@tonic-gate 		else {
14297c478bd9Sstevel@tonic-gate 			rc = GENENT_CBERR;
14307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1431e1dd0a2fSth 			    gettext("Entry: %s - already Exists\n"),
1432e1dd0a2fSth 			    data.n_name);
14337c478bd9Sstevel@tonic-gate 		}
14347c478bd9Sstevel@tonic-gate 	} else if (retval)
14357c478bd9Sstevel@tonic-gate 		rc = GENENT_CBERR;
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	free(data.n_name);
14387c478bd9Sstevel@tonic-gate 	free(data.n_aliases);
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	return (rc);
14417c478bd9Sstevel@tonic-gate }
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate static void
14457c478bd9Sstevel@tonic-gate dump_networks(ns_ldap_result_t *res)
14467c478bd9Sstevel@tonic-gate {
14477c478bd9Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *networknumber = NULL;
14487c478bd9Sstevel@tonic-gate 	int		 i, j;
14497c478bd9Sstevel@tonic-gate 	char		*name;
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
14527c478bd9Sstevel@tonic-gate 		return;
14537c478bd9Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
14547c478bd9Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
14557c478bd9Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
14567c478bd9Sstevel@tonic-gate 			cn = attrptr;
14577c478bd9Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "ipNetworkNumber")
1458e1dd0a2fSth 		    == 0)
14597c478bd9Sstevel@tonic-gate 			networknumber = attrptr;
14607c478bd9Sstevel@tonic-gate 	}
14617c478bd9Sstevel@tonic-gate 	/* sanity check */
14627c478bd9Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
14637c478bd9Sstevel@tonic-gate 	    networknumber == NULL || networknumber->attrvalue == NULL ||
14647c478bd9Sstevel@tonic-gate 	    networknumber->attrvalue[0] == NULL)
14657c478bd9Sstevel@tonic-gate 		return;
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	/*
14687c478bd9Sstevel@tonic-gate 	 * cn can be a MUST attribute(RFC 2307) or MAY attribute(2307bis).
14697c478bd9Sstevel@tonic-gate 	 * If the canonical name can not be found (2307bis), use the 1st
14707c478bd9Sstevel@tonic-gate 	 * value as the official name.
14717c478bd9Sstevel@tonic-gate 	 */
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	/* network name */
14747c478bd9Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
14757c478bd9Sstevel@tonic-gate 		name = cn->attrvalue[0];
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 	if (strlen(name) < 8)
14787c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t\t", name);
14797c478bd9Sstevel@tonic-gate 	else
14807c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t", name);
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	/* network number */
14837c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "%-16s", networknumber->attrvalue[0]);
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	/* aliases */
14867c478bd9Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
14877c478bd9Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
14887c478bd9Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0)
14897c478bd9Sstevel@tonic-gate 				/* skip name */
14907c478bd9Sstevel@tonic-gate 				continue;
14917c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
14927c478bd9Sstevel@tonic-gate 		}
14937c478bd9Sstevel@tonic-gate 	}
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	/* end of line */
14967c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate }
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate /*
15047c478bd9Sstevel@tonic-gate  * /etc/services
15057c478bd9Sstevel@tonic-gate  *
15067c478bd9Sstevel@tonic-gate  */
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate static int
15097c478bd9Sstevel@tonic-gate genent_services(char *line, int (*cback)())
15107c478bd9Sstevel@tonic-gate {
15117c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ+1];
15127c478bd9Sstevel@tonic-gate 	char *t, *p;
15137c478bd9Sstevel@tonic-gate 	entry_col ecol[5];
15147c478bd9Sstevel@tonic-gate 	char *cname;
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	struct servent	data;
15177c478bd9Sstevel@tonic-gate 	char *alias;
15187c478bd9Sstevel@tonic-gate 	int ctr = 0;
15197c478bd9Sstevel@tonic-gate 	int retval = 1;
15207c478bd9Sstevel@tonic-gate 	int rc = GENENT_OK;
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	/*
15237c478bd9Sstevel@tonic-gate 	 * don't clobber our argument
15247c478bd9Sstevel@tonic-gate 	 */
15257c478bd9Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
1526e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
1527e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
15287c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
15297c478bd9Sstevel@tonic-gate 	}
15307c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, line);
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	/*
15337c478bd9Sstevel@tonic-gate 	 * clear column data
15347c478bd9Sstevel@tonic-gate 	 */
15357c478bd9Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	/*
15387c478bd9Sstevel@tonic-gate 	 * comment (col 4)
15397c478bd9Sstevel@tonic-gate 	 */
15407c478bd9Sstevel@tonic-gate 	t = strchr(buf, '#');
15417c478bd9Sstevel@tonic-gate 	if (t) {
15427c478bd9Sstevel@tonic-gate 		*t++ = 0;
15437c478bd9Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_val = t;
15447c478bd9Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_len = strlen(t)+1;
15457c478bd9Sstevel@tonic-gate 	} else {
15467c478bd9Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_val = 0;
15477c478bd9Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_len = 0;
15487c478bd9Sstevel@tonic-gate 	}
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 	/*
15517c478bd9Sstevel@tonic-gate 	 * cname(col 0)
15527c478bd9Sstevel@tonic-gate 	 */
15537c478bd9Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
1554e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no port"),
1555e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
15567c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
15577c478bd9Sstevel@tonic-gate 	}
15587c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
15597c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
15607c478bd9Sstevel@tonic-gate 	cname = t;
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	/*
15637c478bd9Sstevel@tonic-gate 	 * port (col 3)
15647c478bd9Sstevel@tonic-gate 	 */
15657c478bd9Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
1566e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no protocol"),
1567e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
15687c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
15697c478bd9Sstevel@tonic-gate 	}
15707c478bd9Sstevel@tonic-gate 	if ((p = strchr(t, '/')) == 0) {
1571e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("bad port/proto"),
1572e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
15737c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
15747c478bd9Sstevel@tonic-gate 	}
15757c478bd9Sstevel@tonic-gate 	*(p++) = 0;
15767c478bd9Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
15777c478bd9Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	/*
15807c478bd9Sstevel@tonic-gate 	 * proto (col 2)
15817c478bd9Sstevel@tonic-gate 	 */
15827c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = p;
15837c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(p)+1;
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	/*
15877c478bd9Sstevel@tonic-gate 	 * build entry
15887c478bd9Sstevel@tonic-gate 	 */
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	data.s_name = strdup(ecol[0].ec_value.ec_value_val);
15917c478bd9Sstevel@tonic-gate 	data.s_proto = strdup(ecol[2].ec_value.ec_value_val);
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	if (ecol[3].ec_value.ec_value_val != NULL &&
1594e1dd0a2fSth 	    ecol[3].ec_value.ec_value_val[0] != '\0') {
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 		data.s_port = ascii_to_int(ecol[3].ec_value.ec_value_val);
15977c478bd9Sstevel@tonic-gate 		if (data.s_port == -1) {
15987c478bd9Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
1599e1dd0a2fSth 			    gettext("invalid port number: %s"),
16007c478bd9Sstevel@tonic-gate 			    ecol[3].ec_value.ec_value_val);
16017c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
16027c478bd9Sstevel@tonic-gate 		}
16037c478bd9Sstevel@tonic-gate 	} else
16047c478bd9Sstevel@tonic-gate 		data.s_port = -1;
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	/*
16077c478bd9Sstevel@tonic-gate 	 * name (col 1)
16087c478bd9Sstevel@tonic-gate 	 */
16097c478bd9Sstevel@tonic-gate 	t = cname;
16107c478bd9Sstevel@tonic-gate 	data.s_aliases = NULL;
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	do {
16137c478bd9Sstevel@tonic-gate 		/*
16147c478bd9Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
16157c478bd9Sstevel@tonic-gate 		 */
16167c478bd9Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
16177c478bd9Sstevel@tonic-gate 			continue;
16187c478bd9Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
16197c478bd9Sstevel@tonic-gate 			continue;
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
16227c478bd9Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 		ctr++;
16257c478bd9Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
16267c478bd9Sstevel@tonic-gate 		if ((data.s_aliases = (char **)realloc(data.s_aliases,
1627e1dd0a2fSth 		    ctr * sizeof (char **))) == NULL) {
16287c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
16297c478bd9Sstevel@tonic-gate 			exit(1);
16307c478bd9Sstevel@tonic-gate 		}
16317c478bd9Sstevel@tonic-gate 		data.s_aliases[ctr-1] = alias;
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 		/*
16347c478bd9Sstevel@tonic-gate 		 * only put comment in canonical entry
16357c478bd9Sstevel@tonic-gate 		 */
16367c478bd9Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_val = 0;
16377c478bd9Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_len = 0;
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
16427c478bd9Sstevel@tonic-gate 	if ((data.s_aliases = (char **)realloc(data.s_aliases,
1643e1dd0a2fSth 	    (ctr + 1) * sizeof (char **))) == NULL) {
16447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
16457c478bd9Sstevel@tonic-gate 		exit(1);
16467c478bd9Sstevel@tonic-gate 	}
16477c478bd9Sstevel@tonic-gate 	data.s_aliases[ctr] = NULL;
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	if (flags & F_VERBOSE)
16507c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout,
16517c478bd9Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), line);
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
16567c478bd9Sstevel@tonic-gate 		if (continue_onerror)
16577c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
1658e1dd0a2fSth 			    "Entry: cn=%s+ipServiceProtocol=%s"
1659e1dd0a2fSth 			    " already Exists, skipping it.\n"),
1660e1dd0a2fSth 			    data.s_name, data.s_proto);
16617c478bd9Sstevel@tonic-gate 		else {
16627c478bd9Sstevel@tonic-gate 			rc = GENENT_CBERR;
16637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1664e1dd0a2fSth 			    gettext("Entry: cn=%s+ipServiceProtocol=%s"
1665e1dd0a2fSth 			    " - already Exists\n"),
1666e1dd0a2fSth 			    data.s_name, data.s_proto);
16677c478bd9Sstevel@tonic-gate 		}
16687c478bd9Sstevel@tonic-gate 	} else if (retval)
16697c478bd9Sstevel@tonic-gate 		rc = GENENT_CBERR;
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 	free(data.s_name);
16727c478bd9Sstevel@tonic-gate 	free(data.s_proto);
16737c478bd9Sstevel@tonic-gate 	free(data.s_aliases);
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 	return (rc);
16767c478bd9Sstevel@tonic-gate }
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate static void
16817c478bd9Sstevel@tonic-gate dump_services(ns_ldap_result_t *res)
16827c478bd9Sstevel@tonic-gate {
16837c478bd9Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *port = NULL;
16847c478bd9Sstevel@tonic-gate 	ns_ldap_attr_t	*protocol = NULL;
16857c478bd9Sstevel@tonic-gate 	int		i, j, len;
16867c478bd9Sstevel@tonic-gate 	char		*name; /* service name */
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 	/*
16897c478bd9Sstevel@tonic-gate 	 * cn can have multiple values.(service name and its aliases)
16907c478bd9Sstevel@tonic-gate 	 * In order to support RFC 2307, section 5.5, ipserviceprotocol  can
16917c478bd9Sstevel@tonic-gate 	 * have multiple values too.
16927c478bd9Sstevel@tonic-gate 	 * The output format should look like
16937c478bd9Sstevel@tonic-gate 	 *
16947c478bd9Sstevel@tonic-gate 	 * test		2345/udp mytest
16957c478bd9Sstevel@tonic-gate 	 * test		2345/tcp mytest
16967c478bd9Sstevel@tonic-gate 	 */
16977c478bd9Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
16987c478bd9Sstevel@tonic-gate 		return;
16997c478bd9Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
17007c478bd9Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
17017c478bd9Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
17027c478bd9Sstevel@tonic-gate 			cn = attrptr;
17037c478bd9Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "ipServicePort") == 0)
17047c478bd9Sstevel@tonic-gate 			port = attrptr;
17057c478bd9Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname,
1706e1dd0a2fSth 		    "ipServiceProtocol") == 0)
17077c478bd9Sstevel@tonic-gate 			protocol = attrptr;
17087c478bd9Sstevel@tonic-gate 	}
17097c478bd9Sstevel@tonic-gate 	/* sanity check */
17107c478bd9Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
17117c478bd9Sstevel@tonic-gate 	    port == NULL || port->attrvalue == NULL ||
17127c478bd9Sstevel@tonic-gate 	    port->attrvalue[0] == NULL || protocol == NULL ||
17137c478bd9Sstevel@tonic-gate 	    protocol->attrvalue == NULL || protocol->attrvalue[0] == NULL)
17147c478bd9Sstevel@tonic-gate 		return;
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
17177c478bd9Sstevel@tonic-gate 		return;
17187c478bd9Sstevel@tonic-gate 	for (i = 0; i < protocol->value_count; i++) {
17197c478bd9Sstevel@tonic-gate 		if (protocol->attrvalue[i] == NULL)
17207c478bd9Sstevel@tonic-gate 			return;
17217c478bd9Sstevel@tonic-gate 		/* service name */
17227c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%-16s", name);
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 		/* port & protocol */
17257c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s/%s%n", port->attrvalue[0],
1726e1dd0a2fSth 		    protocol->attrvalue[i], &len);
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 		if (len < 8)
17297c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "\t\t");
17307c478bd9Sstevel@tonic-gate 		else
17317c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "\t");
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 		/* aliases */
17347c478bd9Sstevel@tonic-gate 		for (j = 0; j < cn->value_count; j++) {
17357c478bd9Sstevel@tonic-gate 			if (cn->attrvalue[j]) {
17367c478bd9Sstevel@tonic-gate 				if (strcasecmp(name, cn->attrvalue[j]) == 0)
17377c478bd9Sstevel@tonic-gate 					/* skip service name */
17387c478bd9Sstevel@tonic-gate 					continue;
17397c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
17407c478bd9Sstevel@tonic-gate 			}
17417c478bd9Sstevel@tonic-gate 		}
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 		/* end of line */
17447c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
17457c478bd9Sstevel@tonic-gate 	}
17467c478bd9Sstevel@tonic-gate }
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate /*
17507c478bd9Sstevel@tonic-gate  * /etc/group
17517c478bd9Sstevel@tonic-gate  */
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate static int
17547c478bd9Sstevel@tonic-gate genent_group(char *line, int (*cback)())
17557c478bd9Sstevel@tonic-gate {
17567c478bd9Sstevel@tonic-gate 	char buf[BIGBUF+1];
17577c478bd9Sstevel@tonic-gate 	char *s, *t;
17587c478bd9Sstevel@tonic-gate 	entry_col ecol[5];
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	struct group	data;
17617c478bd9Sstevel@tonic-gate 	int ctr = 0;
17627c478bd9Sstevel@tonic-gate 	int retval = 1;
17637c478bd9Sstevel@tonic-gate 	int rc = GENENT_OK;
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	/*
17667c478bd9Sstevel@tonic-gate 	 * don't clobber our argument
17677c478bd9Sstevel@tonic-gate 	 */
17687c478bd9Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
1769e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
1770e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
17717c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
17727c478bd9Sstevel@tonic-gate 	}
17737c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, line);
17747c478bd9Sstevel@tonic-gate 	t = buf;
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	/* ignore empty entries */
17777c478bd9Sstevel@tonic-gate 	if (*t == '\0')
17787c478bd9Sstevel@tonic-gate 		return (GENENT_OK);
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	/*
17817c478bd9Sstevel@tonic-gate 	 * clear column data
17827c478bd9Sstevel@tonic-gate 	 */
17837c478bd9Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	/*
17867c478bd9Sstevel@tonic-gate 	 * name (col 0)
17877c478bd9Sstevel@tonic-gate 	 */
17887c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
1789e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no passwd"),
1790e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
17917c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
17927c478bd9Sstevel@tonic-gate 	}
17937c478bd9Sstevel@tonic-gate 	*s++ = 0;
17947c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
17957c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
17967c478bd9Sstevel@tonic-gate 	t = s;
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 	/*
17997c478bd9Sstevel@tonic-gate 	 * passwd (col 1)
18007c478bd9Sstevel@tonic-gate 	 */
18017c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
1802e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no gid"),
1803e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
18047c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
18057c478bd9Sstevel@tonic-gate 	}
18067c478bd9Sstevel@tonic-gate 	*s++ = 0;
18077c478bd9Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
18087c478bd9Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
18097c478bd9Sstevel@tonic-gate 	t = s;
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	/*
18137c478bd9Sstevel@tonic-gate 	 * gid (col 2)
18147c478bd9Sstevel@tonic-gate 	 */
18157c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0 || s == t) {
1816e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no members"),
1817e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
18187c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
18197c478bd9Sstevel@tonic-gate 	}
18207c478bd9Sstevel@tonic-gate 	*s++ = 0;
18217c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
18227c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
18237c478bd9Sstevel@tonic-gate 	t = s;
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	/*
18267c478bd9Sstevel@tonic-gate 	 * members (col 3)
18277c478bd9Sstevel@tonic-gate 	 */
18287c478bd9Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
18297c478bd9Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 	/*
18337c478bd9Sstevel@tonic-gate 	 * build entry
18347c478bd9Sstevel@tonic-gate 	 */
18357c478bd9Sstevel@tonic-gate 	data.gr_name = strdup(ecol[0].ec_value.ec_value_val);
18367c478bd9Sstevel@tonic-gate 	data.gr_passwd = strdup(ecol[1].ec_value.ec_value_val);
18377c478bd9Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
1838e1dd0a2fSth 	    ecol[2].ec_value.ec_value_val[0] != '\0') {
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 		data.gr_gid = ascii_to_int(ecol[2].ec_value.ec_value_val);
1841e1dd0a2fSth 		if (data.gr_gid == (uid_t)-1) {
18427c478bd9Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
1843e1dd0a2fSth 			    gettext("invalid group id: %s"),
18447c478bd9Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
18457c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
18467c478bd9Sstevel@tonic-gate 		}
18477c478bd9Sstevel@tonic-gate 	} else
1848e1dd0a2fSth 		data.gr_gid = (uid_t)-1;
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	data.gr_mem = NULL;
18517c478bd9Sstevel@tonic-gate 
185284ad75deSmj 	/* Compute maximum amount of members */
185384ad75deSmj 	s = t;
185484ad75deSmj 	while (s = strchr(s, ',')) {
185584ad75deSmj 		s++;
185684ad75deSmj 		ctr++;
185784ad75deSmj 	}
185884ad75deSmj 
185984ad75deSmj 	/* Allocate memory for all members */
186084ad75deSmj 	data.gr_mem = calloc(ctr + 2, sizeof (char **));
186184ad75deSmj 	if (data.gr_mem == NULL) {
186284ad75deSmj 		(void) fprintf(stderr, gettext("out of memory\n"));
186384ad75deSmj 		exit(1);
186484ad75deSmj 	}
186584ad75deSmj 
186684ad75deSmj 	ctr = 0;
18677c478bd9Sstevel@tonic-gate 	while (s = strchr(t, ',')) {
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 		*s++ = 0;
18707c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
18717c478bd9Sstevel@tonic-gate 		t = s;
187284ad75deSmj 		/* Send to server only non empty member names */
187384ad75deSmj 		if (strlen(ecol[3].ec_value.ec_value_val) != 0)
187484ad75deSmj 			data.gr_mem[ctr++] = ecol[3].ec_value.ec_value_val;
18757c478bd9Sstevel@tonic-gate 	}
18767c478bd9Sstevel@tonic-gate 
187784ad75deSmj 	/* Send to server only non empty member names */
187884ad75deSmj 	if (strlen(t) != 0)
187984ad75deSmj 		data.gr_mem[ctr++] = t;
188084ad75deSmj 
188184ad75deSmj 	/* Array of members completed, finished by NULL, see calloc() */
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 	if (flags & F_VERBOSE)
18847c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout,
18857c478bd9Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.gr_name);
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
18907c478bd9Sstevel@tonic-gate 		if (continue_onerror)
18917c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1892e1dd0a2fSth 			    gettext("Entry: %s - already Exists,"
1893e1dd0a2fSth 			    " skipping it.\n"), data.gr_name);
18947c478bd9Sstevel@tonic-gate 		else {
18957c478bd9Sstevel@tonic-gate 			rc = GENENT_CBERR;
18967c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1897e1dd0a2fSth 			    gettext("Entry: %s - already Exists\n"),
1898e1dd0a2fSth 			    data.gr_name);
18997c478bd9Sstevel@tonic-gate 		}
19007c478bd9Sstevel@tonic-gate 	} else if (retval)
19017c478bd9Sstevel@tonic-gate 		rc = GENENT_CBERR;
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 	free(data.gr_name);
19047c478bd9Sstevel@tonic-gate 	free(data.gr_passwd);
19057c478bd9Sstevel@tonic-gate 	free(data.gr_mem);
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	return (rc);
19087c478bd9Sstevel@tonic-gate }
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate static void
19117c478bd9Sstevel@tonic-gate dump_group(ns_ldap_result_t *res)
19127c478bd9Sstevel@tonic-gate {
19137c478bd9Sstevel@tonic-gate 	char    **value = NULL;
19147c478bd9Sstevel@tonic-gate 	char	pnam[256];
19157c478bd9Sstevel@tonic-gate 	int	attr_count = 0;
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
19187c478bd9Sstevel@tonic-gate 	if (value && value[0])
19197c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
19207c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "userPassword");
19217c478bd9Sstevel@tonic-gate 	if (value == NULL || value[0] == NULL)
19227c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "*:");
19237c478bd9Sstevel@tonic-gate 	else {
19247c478bd9Sstevel@tonic-gate 		(void) strcpy(pnam, value[0]);
19257c478bd9Sstevel@tonic-gate 		if (strncasecmp(value[0], "{crypt}", 7) == 0)
19267c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%s:", (pnam+7));
19277c478bd9Sstevel@tonic-gate 		else
19287c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "*:");
19297c478bd9Sstevel@tonic-gate 	}
19307c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "gidNumber");
19317c478bd9Sstevel@tonic-gate 	if (value && value[0])
19327c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "memberUid");
19357c478bd9Sstevel@tonic-gate 	if (value != NULL && value[0] != NULL) {
19367c478bd9Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
19377c478bd9Sstevel@tonic-gate 			if (value[attr_count+1] == NULL)
19387c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "%s", value[attr_count]);
19397c478bd9Sstevel@tonic-gate 			else
19407c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "%s,",
1941e1dd0a2fSth 				    value[attr_count]);
19427c478bd9Sstevel@tonic-gate 			attr_count++;
19437c478bd9Sstevel@tonic-gate 		}
19447c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
19457c478bd9Sstevel@tonic-gate 	}
19467c478bd9Sstevel@tonic-gate 	else
19477c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
19487c478bd9Sstevel@tonic-gate }
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate /*
19557c478bd9Sstevel@tonic-gate  * /etc/ethers
19567c478bd9Sstevel@tonic-gate  */
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate static int
19597c478bd9Sstevel@tonic-gate genent_ethers(char *line, int (*cback)())
19607c478bd9Sstevel@tonic-gate {
19617c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ+1];
19627c478bd9Sstevel@tonic-gate 	char *t;
19637c478bd9Sstevel@tonic-gate 	entry_col ecol[3];
19647c478bd9Sstevel@tonic-gate 	int retval = 1;
19657c478bd9Sstevel@tonic-gate 	struct _ns_ethers	data;
19667c478bd9Sstevel@tonic-gate 	int rc = GENENT_OK;
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 	/*
19697c478bd9Sstevel@tonic-gate 	 * don't clobber our argument
19707c478bd9Sstevel@tonic-gate 	 */
19717c478bd9Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
1972e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
1973e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
19747c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
19757c478bd9Sstevel@tonic-gate 	}
19767c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, line);
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 	/*
19797c478bd9Sstevel@tonic-gate 	 * clear column data
19807c478bd9Sstevel@tonic-gate 	 */
19817c478bd9Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	/*
19847c478bd9Sstevel@tonic-gate 	 * comment (col 2)
19857c478bd9Sstevel@tonic-gate 	 */
19867c478bd9Sstevel@tonic-gate 	t = strchr(buf, '#');
19877c478bd9Sstevel@tonic-gate 	if (t) {
19887c478bd9Sstevel@tonic-gate 		*t++ = 0;
19897c478bd9Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = t;
19907c478bd9Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = strlen(t)+1;
19917c478bd9Sstevel@tonic-gate 	} else {
19927c478bd9Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = 0;
19937c478bd9Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = 0;
19947c478bd9Sstevel@tonic-gate 	}
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	/*
19977c478bd9Sstevel@tonic-gate 	 * addr(col 0)
19987c478bd9Sstevel@tonic-gate 	 */
19997c478bd9Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
2000e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no name"),
2001e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
20027c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
20037c478bd9Sstevel@tonic-gate 	}
20047c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
20057c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	/*
20087c478bd9Sstevel@tonic-gate 	 * name(col 1)
20097c478bd9Sstevel@tonic-gate 	 */
20107c478bd9Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
2011e1dd0a2fSth 		(void) strlcpy(parse_err_msg,
2012e1dd0a2fSth 		    gettext("no white space allowed in name"),
2013e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
20147c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
20157c478bd9Sstevel@tonic-gate 	}
20167c478bd9Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
20177c478bd9Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	/*
20217c478bd9Sstevel@tonic-gate 	 * build entry
20227c478bd9Sstevel@tonic-gate 	 */
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	data.ether = strdup(ecol[0].ec_value.ec_value_val);
20257c478bd9Sstevel@tonic-gate 	data.name  = strdup(ecol[1].ec_value.ec_value_val);
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	if (flags & F_VERBOSE)
20297c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout,
20307c478bd9Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.name);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
20357c478bd9Sstevel@tonic-gate 		if (continue_onerror)
20367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2037e1dd0a2fSth 			    gettext("Entry: %s - already Exists,"
2038e1dd0a2fSth 			    " skipping it.\n"), data.name);
20397c478bd9Sstevel@tonic-gate 		else {
20407c478bd9Sstevel@tonic-gate 			rc = GENENT_CBERR;
20417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2042e1dd0a2fSth 			    gettext("Entry: %s - already Exists\n"),
2043e1dd0a2fSth 			    data.name);
20447c478bd9Sstevel@tonic-gate 		}
20457c478bd9Sstevel@tonic-gate 	} else if (retval)
20467c478bd9Sstevel@tonic-gate 		rc = GENENT_CBERR;
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 	free(data.ether);
20497c478bd9Sstevel@tonic-gate 	free(data.name);
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	return (rc);
20527c478bd9Sstevel@tonic-gate }
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate static void
20567c478bd9Sstevel@tonic-gate dump_ethers(ns_ldap_result_t *res)
20577c478bd9Sstevel@tonic-gate {
20587c478bd9Sstevel@tonic-gate 	char	**value = NULL;
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "macAddress");
20617c478bd9Sstevel@tonic-gate 	if (value && value[0])
20627c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
20637c478bd9Sstevel@tonic-gate 	else
20647c478bd9Sstevel@tonic-gate 		return;
20657c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
20667c478bd9Sstevel@tonic-gate 	if (value && value[0])
20677c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "	%s\n", value[0]);
20687c478bd9Sstevel@tonic-gate }
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate static int
20717c478bd9Sstevel@tonic-gate genent_aliases(char *line, int (*cback)())
20727c478bd9Sstevel@tonic-gate {
20737c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ+1];
20747c478bd9Sstevel@tonic-gate 	char *t, *aliases;
20757c478bd9Sstevel@tonic-gate 	char *cname;
20767c478bd9Sstevel@tonic-gate 	int ctr = 0;
20777c478bd9Sstevel@tonic-gate 	int retval = 1;
20787c478bd9Sstevel@tonic-gate 	int i;
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 	struct _ns_alias data;
20817c478bd9Sstevel@tonic-gate 	char *alias;
20827c478bd9Sstevel@tonic-gate 	int rc = GENENT_OK;
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	/*
20857c478bd9Sstevel@tonic-gate 	 * don't clobber our argument
20867c478bd9Sstevel@tonic-gate 	 */
20877c478bd9Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
2088e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
2089e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
20907c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
20917c478bd9Sstevel@tonic-gate 	}
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, line);
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 	if ((t = strchr(buf, ':')) == 0) {
2096e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no alias name"),
2097e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
20987c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
20997c478bd9Sstevel@tonic-gate 	}
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	t[0] = '\0';
21023718f283SToomas Soome 	if ((++t)[0] == '\0') {
2103e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no alias value"),
2104e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
21057c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
21067c478bd9Sstevel@tonic-gate 	}
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 	cname = buf;
21097c478bd9Sstevel@tonic-gate 	aliases = t;
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 	/* build entry */
21127c478bd9Sstevel@tonic-gate 	data.alias = strdup(cname);
21137c478bd9Sstevel@tonic-gate 	if (!data.alias) {
21147c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
21157c478bd9Sstevel@tonic-gate 		exit(1);
21167c478bd9Sstevel@tonic-gate 	}
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 	data.member = NULL;
21197c478bd9Sstevel@tonic-gate 	t = strtok(aliases, ",");
21207c478bd9Sstevel@tonic-gate 	do {
21217c478bd9Sstevel@tonic-gate 		ctr++;
21227c478bd9Sstevel@tonic-gate 		while (t[0] == ' ')
21237c478bd9Sstevel@tonic-gate 			t++;
21247c478bd9Sstevel@tonic-gate 		alias = strdup(t);
21257c478bd9Sstevel@tonic-gate 		if ((alias == NULL) ||
2126e1dd0a2fSth 		    ((data.member = (char **)realloc(data.member,
2127e1dd0a2fSth 		    (ctr + 1) * sizeof (char **))) == NULL)) {
21287c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
21297c478bd9Sstevel@tonic-gate 			exit(1);
21307c478bd9Sstevel@tonic-gate 		}
21317c478bd9Sstevel@tonic-gate 		data.member[ctr-1] = alias;
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 	} while (t = strtok(NULL, ","));
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	data.member[ctr] = NULL;
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 	if (flags & F_VERBOSE)
21387c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout,
21397c478bd9Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.alias);
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
21447c478bd9Sstevel@tonic-gate 		if (continue_onerror)
21457c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2146e1dd0a2fSth 			    gettext("Entry: %s - already Exists,"
2147e1dd0a2fSth 			    " skipping it.\n"), data.alias);
21487c478bd9Sstevel@tonic-gate 		else {
21497c478bd9Sstevel@tonic-gate 			rc = GENENT_CBERR;
21507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2151e1dd0a2fSth 			    gettext("Entry: %s - already Exists\n"),
2152e1dd0a2fSth 			    data.alias);
21537c478bd9Sstevel@tonic-gate 		}
21547c478bd9Sstevel@tonic-gate 	} else if (retval)
21557c478bd9Sstevel@tonic-gate 		rc = GENENT_CBERR;
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 	free(data.alias);
21587c478bd9Sstevel@tonic-gate 	i = 0;
21597c478bd9Sstevel@tonic-gate 	while (data.member[i])
21607c478bd9Sstevel@tonic-gate 		free(data.member[i++]);
21617c478bd9Sstevel@tonic-gate 	free(data.member);
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	return (rc);
21647c478bd9Sstevel@tonic-gate }
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate static void
21687c478bd9Sstevel@tonic-gate dump_aliases(ns_ldap_result_t *res)
21697c478bd9Sstevel@tonic-gate {
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate 	char	**value = NULL;
2172*c59d9dffSToomas Soome 	int		attr_count = 0;
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "mail");
21757c478bd9Sstevel@tonic-gate 	if (value && value[0])
21767c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
21777c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "mgrpRFC822MailMember");
21787c478bd9Sstevel@tonic-gate 	if (value != NULL)
21797c478bd9Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
21807c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "%s,", value[attr_count]);
21817c478bd9Sstevel@tonic-gate 			attr_count++;
21827c478bd9Sstevel@tonic-gate 		}
21837c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate }
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate /*
21887c478bd9Sstevel@tonic-gate  * /etc/publickey
21897c478bd9Sstevel@tonic-gate  */
21907c478bd9Sstevel@tonic-gate 
2191cb5caa98Sdjl static char *h_errno2str(int h_errno);
2192cb5caa98Sdjl 
21937c478bd9Sstevel@tonic-gate static int
21947c478bd9Sstevel@tonic-gate genent_publickey(char *line, int (*cback)())
21957c478bd9Sstevel@tonic-gate {
21967c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ+1], tmpbuf[BUFSIZ+1], cname[BUFSIZ+1];
21977c478bd9Sstevel@tonic-gate 	char *t, *p, *tmppubkey, *tmpprivkey;
21987c478bd9Sstevel@tonic-gate 	entry_col ecol[3];
2199cb5caa98Sdjl 	int buflen, uid, retval = 1, errnum = 0;
22007c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
2201cb5caa98Sdjl 	char auth_type[BUFSIZ+1], *dot;
22027c478bd9Sstevel@tonic-gate 	keylen_t keylen;
22037c478bd9Sstevel@tonic-gate 	algtype_t algtype;
22047c478bd9Sstevel@tonic-gate 	struct _ns_pubkey data;
22057c478bd9Sstevel@tonic-gate 	struct hostent *hp;
22067c478bd9Sstevel@tonic-gate 	struct in_addr in;
2207cb5caa98Sdjl 	struct in6_addr in6;
2208cb5caa98Sdjl 	char abuf[INET6_ADDRSTRLEN];
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 	/*
22117c478bd9Sstevel@tonic-gate 	 * don't clobber our argument
22127c478bd9Sstevel@tonic-gate 	 */
22137c478bd9Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
2214e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
2215e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
22167c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
22177c478bd9Sstevel@tonic-gate 	}
22187c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, line);
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	/*
22217c478bd9Sstevel@tonic-gate 	 * clear column data
22227c478bd9Sstevel@tonic-gate 	 */
22237c478bd9Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
2226e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no cname"),
2227e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
22287c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
22297c478bd9Sstevel@tonic-gate 	}
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 	/*
22327c478bd9Sstevel@tonic-gate 	 * Special case:  /etc/publickey usually has an entry
22337c478bd9Sstevel@tonic-gate 	 * for principal "nobody".  We skip it.
22347c478bd9Sstevel@tonic-gate 	 */
22357c478bd9Sstevel@tonic-gate 	if (strcmp(t, "nobody") == 0)
22367c478bd9Sstevel@tonic-gate 		return (GENENT_OK);
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 	/*
22397c478bd9Sstevel@tonic-gate 	 * cname (col 0)
22407c478bd9Sstevel@tonic-gate 	 */
22417c478bd9Sstevel@tonic-gate 	if (strncmp(t, "unix.", 5)) {
2242e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("bad cname"),
2243e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
22447c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
22457c478bd9Sstevel@tonic-gate 	}
22467c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpbuf, &(t[5]));
22477c478bd9Sstevel@tonic-gate 	if ((p = strchr(tmpbuf, '@')) == 0) {
2248e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("bad cname"),
2249e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
22507c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
22517c478bd9Sstevel@tonic-gate 	}
22527c478bd9Sstevel@tonic-gate 	*(p++) = 0;
22537c478bd9Sstevel@tonic-gate 	if (isdigit(*tmpbuf)) {
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 		uid = atoi(tmpbuf);
22567c478bd9Sstevel@tonic-gate 		/*
22577c478bd9Sstevel@tonic-gate 		 * don't generate entries for uids without passwd entries
22587c478bd9Sstevel@tonic-gate 		 */
22597c478bd9Sstevel@tonic-gate 		if ((pwd = getpwuid(uid)) == 0) {
22607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
22617c478bd9Sstevel@tonic-gate 			gettext("can't map uid %d to username, skipping\n"),
2262e1dd0a2fSth 			    uid);
22637c478bd9Sstevel@tonic-gate 			return (GENENT_OK);
22647c478bd9Sstevel@tonic-gate 		}
22657c478bd9Sstevel@tonic-gate 		(void) strcpy(cname, pwd->pw_name);
22667c478bd9Sstevel@tonic-gate 		data.hostcred = NS_HOSTCRED_FALSE;
22677c478bd9Sstevel@tonic-gate 	} else {
2268cb5caa98Sdjl 		if ((hp = getipnodebyname(tmpbuf, AF_INET6,
2269e1dd0a2fSth 		    AI_ALL | AI_V4MAPPED, &errnum)) == NULL) {
22707c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2271e1dd0a2fSth 			    gettext("can't map hostname %s to hostaddress, "
2272e1dd0a2fSth 			    "errnum %d %s skipping\n"), tmpbuf, errnum,
2273e1dd0a2fSth 			    h_errno2str(errnum));
22747c478bd9Sstevel@tonic-gate 			return (GENENT_OK);
22757c478bd9Sstevel@tonic-gate 		}
2276cb5caa98Sdjl 		(void) memcpy((char *)&in6.s6_addr, hp->h_addr_list[0],
2277e1dd0a2fSth 		    hp->h_length);
2278cb5caa98Sdjl 		if (IN6_IS_ADDR_V4MAPPED(&in6) ||
2279e1dd0a2fSth 		    IN6_IS_ADDR_V4COMPAT(&in6)) {
2280cb5caa98Sdjl 			IN6_V4MAPPED_TO_INADDR(&in6, &in);
2281cb5caa98Sdjl 			if (inet_ntop(AF_INET, (const void *)&in, abuf,
2282e1dd0a2fSth 			    INET6_ADDRSTRLEN) == NULL) {
2283cb5caa98Sdjl 				(void) fprintf(stderr,
2284e1dd0a2fSth 				    gettext("can't convert IPV4 address of"
2285e1dd0a2fSth 				    " hostname %s to string, "
2286e1dd0a2fSth 				    "skipping\n"), tmpbuf);
2287cb5caa98Sdjl 					return (GENENT_OK);
2288cb5caa98Sdjl 			}
2289cb5caa98Sdjl 		} else {
2290cb5caa98Sdjl 			if (inet_ntop(AF_INET6, (const void *)&in6, abuf,
2291e1dd0a2fSth 			    INET6_ADDRSTRLEN) == NULL) {
2292cb5caa98Sdjl 				(void) fprintf(stderr,
2293e1dd0a2fSth 				    gettext("can't convert IPV6 address of"
2294e1dd0a2fSth 				    " hostname %s to string, "
2295e1dd0a2fSth 				    "skipping\n"), tmpbuf);
2296cb5caa98Sdjl 					return (GENENT_OK);
2297cb5caa98Sdjl 			}
2298cb5caa98Sdjl 		}
22997c478bd9Sstevel@tonic-gate 		data.hostcred = NS_HOSTCRED_TRUE;
2300cb5caa98Sdjl 		/*
2301cb5caa98Sdjl 		 * tmpbuf could be an alias, use hp->h_name instead.
2302cb5caa98Sdjl 		 * hp->h_name is in FQDN format, so extract 1st field.
2303cb5caa98Sdjl 		 */
2304cb5caa98Sdjl 		if ((dot = strchr(hp->h_name, '.')) != NULL)
2305cb5caa98Sdjl 			*dot = '\0';
23067c478bd9Sstevel@tonic-gate 		(void) snprintf(cname, sizeof (cname),
2307cb5caa98Sdjl 		    "%s+ipHostNumber=%s", hp->h_name, abuf);
2308cb5caa98Sdjl 		if (dot)
2309cb5caa98Sdjl 			*dot = '.';
23107c478bd9Sstevel@tonic-gate 	}
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = cname;
23137c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(cname)+1;
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 	/*
23167c478bd9Sstevel@tonic-gate 	 * public_data (col 1)
23177c478bd9Sstevel@tonic-gate 	 */
23187c478bd9Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
2319e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no private_data"),
2320e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
23217c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
23227c478bd9Sstevel@tonic-gate 	}
23237c478bd9Sstevel@tonic-gate 	if ((p = strchr(t, ':')) == 0) {
2324e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("bad public_data"),
2325e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
23267c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
23277c478bd9Sstevel@tonic-gate 	}
23287c478bd9Sstevel@tonic-gate 	*(p++) = 0;
23297c478bd9Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
23307c478bd9Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
23317c478bd9Sstevel@tonic-gate 	keylen = (strlen(t) / 2) * 8;
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	/*
23347c478bd9Sstevel@tonic-gate 	 * private_data (col 2) and algtype extraction
23357c478bd9Sstevel@tonic-gate 	 */
23367c478bd9Sstevel@tonic-gate 	if (*p == ':')
23377c478bd9Sstevel@tonic-gate 		p++;
23387c478bd9Sstevel@tonic-gate 	t = p;
23397c478bd9Sstevel@tonic-gate 	if (!(t = strchr(t, ':'))) {
23407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2341e1dd0a2fSth 		    gettext("WARNING: No algorithm type data found "
2342e1dd0a2fSth 		    "in publickey file, assuming 0\n"));
23437c478bd9Sstevel@tonic-gate 		algtype = 0;
23447c478bd9Sstevel@tonic-gate 	} else {
23457c478bd9Sstevel@tonic-gate 		*t = '\0';
23467c478bd9Sstevel@tonic-gate 		t++;
23477c478bd9Sstevel@tonic-gate 		algtype = atoi(t);
23487c478bd9Sstevel@tonic-gate 	}
23497c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = p;
23507c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(p)+1;
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 	/*
23537c478bd9Sstevel@tonic-gate 	 * auth_type (col 1)
23547c478bd9Sstevel@tonic-gate 	 */
2355cb5caa98Sdjl 	if (AUTH_DES_KEY(keylen, algtype))
2356cb5caa98Sdjl 		/*
2357cb5caa98Sdjl 		 * {DES} and {DH192-0} means same thing.
2358cb5caa98Sdjl 		 * However, nisplus uses "DES" and ldap uses "DH192-0"
2359cb5caa98Sdjl 		 * internally.
2360cb5caa98Sdjl 		 * See newkey(1M), __nis_mechalias2authtype() which is
2361cb5caa98Sdjl 		 * called by __nis_keyalg2authtype() and getkey_ldap_g()
2362cb5caa98Sdjl 		 */
2363cb5caa98Sdjl 		(void) strlcpy(auth_type, "DH192-0", BUFSIZ+1);
2364cb5caa98Sdjl 	else if (!(__nis_keyalg2authtype(keylen, algtype, auth_type,
2365e1dd0a2fSth 	    MECH_MAXATNAME))) {
23667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2367e1dd0a2fSth 		    gettext("Could not convert algorithm type to "
2368e1dd0a2fSth 		    "corresponding auth type string\n"));
23697c478bd9Sstevel@tonic-gate 		return (GENENT_ERR);
23707c478bd9Sstevel@tonic-gate 	}
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate 	/*
23737c478bd9Sstevel@tonic-gate 	 * build entry
23747c478bd9Sstevel@tonic-gate 	 */
23757c478bd9Sstevel@tonic-gate 	data.name = strdup(ecol[0].ec_value.ec_value_val);
23767c478bd9Sstevel@tonic-gate 	if (data.name == NULL) {
23777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
23787c478bd9Sstevel@tonic-gate 		exit(1);
23797c478bd9Sstevel@tonic-gate 	}
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 	buflen = sizeof (auth_type) + strlen(ecol[1].ec_value.ec_value_val) + 3;
23827c478bd9Sstevel@tonic-gate 	if ((tmppubkey = (char *)malloc(buflen)) == NULL) {
23837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
23847c478bd9Sstevel@tonic-gate 		exit(1);
23857c478bd9Sstevel@tonic-gate 	}
23867c478bd9Sstevel@tonic-gate 	(void) snprintf(tmppubkey, buflen, "{%s}%s", auth_type,
23877c478bd9Sstevel@tonic-gate 	    ecol[1].ec_value.ec_value_val);
23887c478bd9Sstevel@tonic-gate 	data.pubkey = tmppubkey;
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate 	buflen = sizeof (auth_type) + strlen(ecol[2].ec_value.ec_value_val) + 3;
23917c478bd9Sstevel@tonic-gate 	if ((tmpprivkey = (char *)malloc(buflen)) == NULL) {
23927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
23937c478bd9Sstevel@tonic-gate 		exit(1);
23947c478bd9Sstevel@tonic-gate 	}
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpprivkey, buflen, "{%s}%s", auth_type,
23977c478bd9Sstevel@tonic-gate 	    ecol[2].ec_value.ec_value_val);
23987c478bd9Sstevel@tonic-gate 	data.privkey = tmpprivkey;
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 	retval = (*cback)(&data, 1);
240112fbe00aSjs 	if (retval != NS_LDAP_SUCCESS) {
240212fbe00aSjs 		if (retval == LDAP_NO_SUCH_OBJECT) {
240312fbe00aSjs 			if (data.hostcred == NS_HOSTCRED_TRUE)
240412fbe00aSjs 				(void) fprintf(stdout,
2405e1dd0a2fSth 				    gettext("Cannot add publickey entry"" (%s),"
2406e1dd0a2fSth 				    " add host entry first\n"),
2407e1dd0a2fSth 				    tmpbuf);
240812fbe00aSjs 			else
240912fbe00aSjs 				(void) fprintf(stdout,
2410e1dd0a2fSth 				    gettext("Cannot add publickey entry (%s), "
2411e1dd0a2fSth 				    "add passwd entry first\n"),
2412e1dd0a2fSth 				    data.name);
241312fbe00aSjs 		}
241412fbe00aSjs 		if (continue_onerror == 0)
241512fbe00aSjs 			return (GENENT_CBERR);
24167c478bd9Sstevel@tonic-gate 	}
241712fbe00aSjs 
241812fbe00aSjs 	free(data.name);
241912fbe00aSjs 	free(data.pubkey);
242012fbe00aSjs 	free(data.privkey);
242112fbe00aSjs 	return (GENENT_OK);
24227c478bd9Sstevel@tonic-gate }
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate static void
24257c478bd9Sstevel@tonic-gate dump_publickey(ns_ldap_result_t *res, char *container)
24267c478bd9Sstevel@tonic-gate {
24277c478bd9Sstevel@tonic-gate 	char	**value = NULL;
24287c478bd9Sstevel@tonic-gate 	char	buf[BUFSIZ];
24297c478bd9Sstevel@tonic-gate 	char	domainname[BUFSIZ];
24307c478bd9Sstevel@tonic-gate 	char	*pubptr, *prvptr;
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate 	if (res == NULL)
24337c478bd9Sstevel@tonic-gate 		return;
24347c478bd9Sstevel@tonic-gate 
24357c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_SRPC_DOMAIN, domainname, BUFSIZ) < 0) {
24367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2437e1dd0a2fSth 		    gettext("could not obtain domainname\n"));
24387c478bd9Sstevel@tonic-gate 		exit(1);
24397c478bd9Sstevel@tonic-gate 	}
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	/*
24427c478bd9Sstevel@tonic-gate 	 * Retrieve all the attributes, but don't print
24437c478bd9Sstevel@tonic-gate 	 * until we have all the required ones.
24447c478bd9Sstevel@tonic-gate 	 */
24457c478bd9Sstevel@tonic-gate 
24467c478bd9Sstevel@tonic-gate 	if (strcmp(container, "passwd") == 0)
24477c478bd9Sstevel@tonic-gate 		value = __ns_ldap_getAttr(res->entry, "uidNumber");
24487c478bd9Sstevel@tonic-gate 	else
24497c478bd9Sstevel@tonic-gate 		value = __ns_ldap_getAttr(res->entry, "cn");
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate 	if (value && value[0])
24527c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "unix.%s@%s",
24537c478bd9Sstevel@tonic-gate 		    value[0], domainname);
24547c478bd9Sstevel@tonic-gate 	else
24557c478bd9Sstevel@tonic-gate 		return;
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "nisPublickey");
24587c478bd9Sstevel@tonic-gate 	if (value != NULL && value[0] != NULL) {
24597c478bd9Sstevel@tonic-gate 		if ((pubptr = strchr(value[0], '}')) == NULL)
24607c478bd9Sstevel@tonic-gate 			return;
24617c478bd9Sstevel@tonic-gate 	}
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "nisSecretkey");
24647c478bd9Sstevel@tonic-gate 	if (value != NULL && value[0] != NULL)
24657c478bd9Sstevel@tonic-gate 		if ((prvptr = strchr(value[0], '}')) == NULL)
24667c478bd9Sstevel@tonic-gate 			return;
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 	/* print the attributes, algorithm type is always 0 */
24697c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "%s	%s:%s:0\n", buf, ++pubptr, ++prvptr);
24707c478bd9Sstevel@tonic-gate }
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate /*
24757c478bd9Sstevel@tonic-gate  * /etc/netmasks
24767c478bd9Sstevel@tonic-gate  */
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate static int
24797c478bd9Sstevel@tonic-gate genent_netmasks(char *line, int (*cback)())
24807c478bd9Sstevel@tonic-gate {
24817c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ+1];
24827c478bd9Sstevel@tonic-gate 	char *t;
24837c478bd9Sstevel@tonic-gate 	entry_col ecol[3];
248412fbe00aSjs 	int retval;
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate 	struct _ns_netmasks data;
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 	/*
24907c478bd9Sstevel@tonic-gate 	 * don't clobber our argument
24917c478bd9Sstevel@tonic-gate 	 */
24927c478bd9Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
2493e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
2494e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
24957c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
24967c478bd9Sstevel@tonic-gate 	}
24977c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, line);
24987c478bd9Sstevel@tonic-gate 
24997c478bd9Sstevel@tonic-gate 	/*
25007c478bd9Sstevel@tonic-gate 	 * clear column data
25017c478bd9Sstevel@tonic-gate 	 */
25027c478bd9Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate 	/*
25057c478bd9Sstevel@tonic-gate 	 * comment (col 2)
25067c478bd9Sstevel@tonic-gate 	 */
25077c478bd9Sstevel@tonic-gate 	t = strchr(buf, '#');
25087c478bd9Sstevel@tonic-gate 	if (t) {
25097c478bd9Sstevel@tonic-gate 		*t++ = 0;
25107c478bd9Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = t;
25117c478bd9Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = strlen(t)+1;
25127c478bd9Sstevel@tonic-gate 	} else {
25137c478bd9Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = 0;
25147c478bd9Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = 0;
25157c478bd9Sstevel@tonic-gate 	}
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 	/*
25187c478bd9Sstevel@tonic-gate 	 * addr(col 0)
25197c478bd9Sstevel@tonic-gate 	 */
25207c478bd9Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
2521e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no mask"),
2522e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
25237c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
25247c478bd9Sstevel@tonic-gate 	}
25257c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
25267c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 	/*
25297c478bd9Sstevel@tonic-gate 	 * mask (col 1)
25307c478bd9Sstevel@tonic-gate 	 */
25317c478bd9Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
2532e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no mask"),
2533e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
25347c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
25357c478bd9Sstevel@tonic-gate 	}
25367c478bd9Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
25377c478bd9Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 	/* build entry */
25407c478bd9Sstevel@tonic-gate 	data.netnumber = ecol[0].ec_value.ec_value_val;
25417c478bd9Sstevel@tonic-gate 	data.netmask = ecol[1].ec_value.ec_value_val;
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate 	if (flags & F_VERBOSE)
25447c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout,
25457c478bd9Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.netnumber);
25467c478bd9Sstevel@tonic-gate 
254712fbe00aSjs 	retval = (*cback)(&data, 1);
254812fbe00aSjs 	if (retval != NS_LDAP_SUCCESS) {
254912fbe00aSjs 		if (retval == LDAP_NO_SUCH_OBJECT)
255012fbe00aSjs 			(void) fprintf(stdout,
2551e1dd0a2fSth 			    gettext("Cannot add netmask entry (%s), "
2552e1dd0a2fSth 			    "add network entry first\n"), data.netnumber);
255312fbe00aSjs 		if (continue_onerror == 0)
255412fbe00aSjs 			return (GENENT_CBERR);
255512fbe00aSjs 	}
25567c478bd9Sstevel@tonic-gate 
25577c478bd9Sstevel@tonic-gate 	return (GENENT_OK);
25587c478bd9Sstevel@tonic-gate }
25597c478bd9Sstevel@tonic-gate 
25607c478bd9Sstevel@tonic-gate static void
25617c478bd9Sstevel@tonic-gate dump_netmasks(ns_ldap_result_t *res)
25627c478bd9Sstevel@tonic-gate {
25637c478bd9Sstevel@tonic-gate 	char	**value = NULL;
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "ipNetworkNumber");
25667c478bd9Sstevel@tonic-gate 	if (value && value[0])
25677c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
25687c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "ipNetmaskNumber");
25697c478bd9Sstevel@tonic-gate 	if (value && value[0])
25707c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "	%s\n", value[0]);
25717c478bd9Sstevel@tonic-gate }
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 
25747c478bd9Sstevel@tonic-gate /*
25757c478bd9Sstevel@tonic-gate  * /etc/netgroup
25767c478bd9Sstevel@tonic-gate  * column data format is:
25777c478bd9Sstevel@tonic-gate  *    col 0: netgroup name (or cname)
25787c478bd9Sstevel@tonic-gate  *    col 1: netgroup member, if this is a triplet
25797c478bd9Sstevel@tonic-gate  *    col 2: netgroup member, if not a triplet
25807c478bd9Sstevel@tonic-gate  *    col 3: comment
25817c478bd9Sstevel@tonic-gate  */
25827c478bd9Sstevel@tonic-gate 
25837c478bd9Sstevel@tonic-gate static int
25847c478bd9Sstevel@tonic-gate genent_netgroup(char *line, int (*cback)())
25857c478bd9Sstevel@tonic-gate {
25867c478bd9Sstevel@tonic-gate 	char buf[BIGBUF+1];    /* netgroup entries tend to be big */
25877c478bd9Sstevel@tonic-gate 	char *t;
25887c478bd9Sstevel@tonic-gate 	char *cname = NULL;
25897c478bd9Sstevel@tonic-gate 	entry_col ecol[4];
25907c478bd9Sstevel@tonic-gate 	char *netg_tmp = NULL, *triplet_tmp = NULL;
2591f31b640dSvl 	int netgcount = 0, tripletcount = 0, retval = 1, i;
25927c478bd9Sstevel@tonic-gate 	struct _ns_netgroups data;
25937c478bd9Sstevel@tonic-gate 	int rc = GENENT_OK;
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 	/* don't clobber our argument */
25967c478bd9Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
2597e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
2598e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
25997c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
26007c478bd9Sstevel@tonic-gate 	}
26017c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, line);
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 	/* clear column data */
26047c478bd9Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
26057c478bd9Sstevel@tonic-gate 
26067c478bd9Sstevel@tonic-gate 	/*
26077c478bd9Sstevel@tonic-gate 	 * process 1st minimal entry, to validate that there is no
26087c478bd9Sstevel@tonic-gate 	 * parsing error.
26097c478bd9Sstevel@tonic-gate 	 * start with comment(col 3)
26107c478bd9Sstevel@tonic-gate 	 */
26117c478bd9Sstevel@tonic-gate 	t = strchr(buf, '#');
26127c478bd9Sstevel@tonic-gate 	if (t) {
26137c478bd9Sstevel@tonic-gate 		*t++ = 0;
26147c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
26157c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
26167c478bd9Sstevel@tonic-gate 	} else {
26177c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = "";
26187c478bd9Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
26197c478bd9Sstevel@tonic-gate 	}
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = NULL;
26227c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = NULL;
26237c478bd9Sstevel@tonic-gate 
26247c478bd9Sstevel@tonic-gate 	/* cname (col 0) */
26257c478bd9Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
2626e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no cname"),
2627e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
26287c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
26297c478bd9Sstevel@tonic-gate 	}
2630f31b640dSvl 
26317c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
26327c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
26337c478bd9Sstevel@tonic-gate 	cname = t;
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 	/* addr(col 1 and 2) */
26367c478bd9Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
2637e1dd0a2fSth 		(void) strlcpy(parse_err_msg,
2638e1dd0a2fSth 		    gettext("no members for netgroup"), PARSE_ERR_MSG_LEN);
26397c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
26407c478bd9Sstevel@tonic-gate 	}
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate 	if (*t == '(') {
2643f31b640dSvl 		/* if token starts with '(' it must be a valid triplet */
2644f31b640dSvl 		if (is_triplet(t)) {
2645f31b640dSvl 			ecol[1].ec_value.ec_value_val = t;
2646f31b640dSvl 			ecol[1].ec_value.ec_value_len = strlen(t)+1;
2647f31b640dSvl 		} else {
2648e1dd0a2fSth 			(void) strlcpy(parse_err_msg,
2649e1dd0a2fSth 			    gettext("invalid triplet"), PARSE_ERR_MSG_LEN);
2650f31b640dSvl 			return (GENENT_PARSEERR);
2651f31b640dSvl 		}
26527c478bd9Sstevel@tonic-gate 	} else {
26537c478bd9Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = t;
26547c478bd9Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = strlen(t)+1;
26557c478bd9Sstevel@tonic-gate 	}
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate 	/*
26587c478bd9Sstevel@tonic-gate 	 * now build entry.
26597c478bd9Sstevel@tonic-gate 	 * start by clearing entry data
26607c478bd9Sstevel@tonic-gate 	 */
26617c478bd9Sstevel@tonic-gate 	(void) memset((struct _ns_netgroups *)&data, 0, sizeof (data));
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 	data.name = strdup(ecol[0].ec_value.ec_value_val);
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate 	if (ecol[1].ec_value.ec_value_val != NULL) {
26667c478bd9Sstevel@tonic-gate 		if ((data.triplet = calloc(1, sizeof (char **))) == NULL) {
26677c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2668e1dd0a2fSth 				    gettext("out of memory\n"));
26697c478bd9Sstevel@tonic-gate 				exit(1);
26707c478bd9Sstevel@tonic-gate 		}
26717c478bd9Sstevel@tonic-gate 		data.triplet[tripletcount++] =
26727c478bd9Sstevel@tonic-gate 		    strdup(ecol[1].ec_value.ec_value_val);
26737c478bd9Sstevel@tonic-gate 	} else if (ecol[2].ec_value.ec_value_val != NULL) {
26747c478bd9Sstevel@tonic-gate 			if ((data.netgroup = calloc(1, sizeof (char **)))
26757c478bd9Sstevel@tonic-gate 			    == NULL) {
26767c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
26777c478bd9Sstevel@tonic-gate 				    gettext("out of memory\n"));
26787c478bd9Sstevel@tonic-gate 					exit(1);
26797c478bd9Sstevel@tonic-gate 			}
26807c478bd9Sstevel@tonic-gate 			data.netgroup[netgcount++] =
26817c478bd9Sstevel@tonic-gate 			    strdup(ecol[2].ec_value.ec_value_val);
26827c478bd9Sstevel@tonic-gate 	}
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	/*
26857c478bd9Sstevel@tonic-gate 	 * we now have a valid entry (at least 1 netgroup name and
26867c478bd9Sstevel@tonic-gate 	 * 1 netgroup member), proceed with the rest of the line
26877c478bd9Sstevel@tonic-gate 	 */
2688f31b640dSvl 	while (rc == GENENT_OK && (t = strtok(NULL, " \t"))) {
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate 		/* if next token is equal to netgroup name, ignore */
26917c478bd9Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
26927c478bd9Sstevel@tonic-gate 			continue;
26937c478bd9Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
26947c478bd9Sstevel@tonic-gate 			continue;
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate 		if (*t == '(') {
2697f31b640dSvl 			if (is_triplet(t)) {
2698f31b640dSvl 				/* skip a triplet if it is added already */
2699f31b640dSvl 				for (i = 0; i < tripletcount &&
2700e1dd0a2fSth 				    strcmp(t, data.triplet[i]); i++)
2701f31b640dSvl 					;
2702f31b640dSvl 				if (i < tripletcount)
2703f31b640dSvl 					continue;
2704f31b640dSvl 
2705f31b640dSvl 				tripletcount++;
2706f31b640dSvl 				triplet_tmp = strdup(t);
2707f31b640dSvl 				if ((data.triplet = (char **)realloc(
2708e1dd0a2fSth 				    data.triplet,
2709e1dd0a2fSth 				    tripletcount * sizeof (char **))) == NULL) {
2710f31b640dSvl 					(void) fprintf(stderr,
2711e1dd0a2fSth 					    gettext("out of memory\n"));
2712f31b640dSvl 					exit(1);
2713f31b640dSvl 				}
2714f31b640dSvl 				data.triplet[tripletcount-1] = triplet_tmp;
2715f31b640dSvl 			} else {
2716e1dd0a2fSth 				(void) strlcpy(parse_err_msg,
2717e1dd0a2fSth 				    gettext("invalid triplet"),
2718e1dd0a2fSth 				    PARSE_ERR_MSG_LEN);
2719f31b640dSvl 				rc = GENENT_PARSEERR;
27207c478bd9Sstevel@tonic-gate 			}
27217c478bd9Sstevel@tonic-gate 		} else {
2722f31b640dSvl 			/* skip a netgroup if it is added already */
2723f31b640dSvl 			for (i = 0; i < netgcount &&
2724e1dd0a2fSth 			    strcmp(t, data.netgroup[i]); i++)
2725f31b640dSvl 				;
2726f31b640dSvl 			if (i < netgcount)
2727f31b640dSvl 				continue;
2728f31b640dSvl 
27297c478bd9Sstevel@tonic-gate 			netgcount++;
27307c478bd9Sstevel@tonic-gate 			netg_tmp = strdup(t);
27317c478bd9Sstevel@tonic-gate 			if ((data.netgroup = (char **)realloc(data.netgroup,
2732e1dd0a2fSth 			    netgcount * sizeof (char **))) == NULL) {
27337c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
27347c478bd9Sstevel@tonic-gate 				gettext("out of memory\n"));
27357c478bd9Sstevel@tonic-gate 				exit(1);
27367c478bd9Sstevel@tonic-gate 			}
27377c478bd9Sstevel@tonic-gate 			data.netgroup[netgcount-1] = netg_tmp;
27387c478bd9Sstevel@tonic-gate 		}
27397c478bd9Sstevel@tonic-gate 	}
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	/* End the list with NULL */
27427c478bd9Sstevel@tonic-gate 	if ((data.triplet = (char **)realloc(data.triplet,
2743e1dd0a2fSth 	    (tripletcount + 1) * sizeof (char **))) == NULL) {
27447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
27457c478bd9Sstevel@tonic-gate 		exit(1);
27467c478bd9Sstevel@tonic-gate 	}
27477c478bd9Sstevel@tonic-gate 	data.triplet[tripletcount] = NULL;
27487c478bd9Sstevel@tonic-gate 	if ((data.netgroup = (char **)realloc(data.netgroup,
2749e1dd0a2fSth 	    (netgcount + 1) * sizeof (char **))) == NULL) {
27507c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
27517c478bd9Sstevel@tonic-gate 		exit(1);
27527c478bd9Sstevel@tonic-gate 	}
27537c478bd9Sstevel@tonic-gate 	data.netgroup[netgcount] = NULL;
27547c478bd9Sstevel@tonic-gate 
2755f31b640dSvl 	if (rc == GENENT_OK) {
2756f31b640dSvl 		if (flags & F_VERBOSE)
2757f31b640dSvl 			(void) fprintf(stdout,
2758f31b640dSvl 			    gettext("Adding entry : %s\n"), data.name);
27597c478bd9Sstevel@tonic-gate 
2760f31b640dSvl 		retval = (*cback)(&data, 0);
27617c478bd9Sstevel@tonic-gate 
2762f31b640dSvl 		if (retval == LDAP_ALREADY_EXISTS) {
2763f31b640dSvl 			if (continue_onerror)
2764f31b640dSvl 				(void) fprintf(stderr, gettext(
2765e1dd0a2fSth 				    "Entry: %s - already Exists,"
2766e1dd0a2fSth 				    " skipping it.\n"), data.name);
2767f31b640dSvl 			else {
2768f31b640dSvl 				rc = GENENT_CBERR;
2769f31b640dSvl 				(void) fprintf(stderr,
2770e1dd0a2fSth 				    gettext("Entry: %s - already Exists\n"),
2771e1dd0a2fSth 				    data.name);
2772f31b640dSvl 			}
2773f31b640dSvl 		} else if (retval)
2774f31b640dSvl 			rc = GENENT_CBERR;
2775f31b640dSvl 	}
2776f31b640dSvl 
2777f31b640dSvl 	/* release memory allocated by strdup() */
2778f31b640dSvl 	for (i = 0; i < tripletcount; i++) {
2779f31b640dSvl 		free(data.triplet[i]);
2780f31b640dSvl 	}
2781f31b640dSvl 	for (i = 0; i < netgcount; i++) {
2782f31b640dSvl 		free(data.netgroup[i]);
2783f31b640dSvl 	}
27847c478bd9Sstevel@tonic-gate 
27857c478bd9Sstevel@tonic-gate 	free(data.name);
27867c478bd9Sstevel@tonic-gate 	free(data.triplet);
27877c478bd9Sstevel@tonic-gate 	free(data.netgroup);
27887c478bd9Sstevel@tonic-gate 
27897c478bd9Sstevel@tonic-gate 	return (rc);
27907c478bd9Sstevel@tonic-gate }
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate static void
27937c478bd9Sstevel@tonic-gate dump_netgroup(ns_ldap_result_t *res)
27947c478bd9Sstevel@tonic-gate {
27957c478bd9Sstevel@tonic-gate 	char	**value = NULL;
27967c478bd9Sstevel@tonic-gate 	int	attr_count = 0;
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
27997c478bd9Sstevel@tonic-gate 	if ((value != NULL) && (value[0] != NULL))
28007c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
28017c478bd9Sstevel@tonic-gate 	else
28027c478bd9Sstevel@tonic-gate 		return;
28037c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "nisNetgroupTriple");
28047c478bd9Sstevel@tonic-gate 	if (value != NULL)
28057c478bd9Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
28067c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, " %s", value[attr_count]);
28077c478bd9Sstevel@tonic-gate 			attr_count++;
28087c478bd9Sstevel@tonic-gate 		}
28092ba040d7Sjs 	attr_count = 0;
28107c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "memberNisNetgroup");
28117c478bd9Sstevel@tonic-gate 	if (value != NULL)
28127c478bd9Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
28137c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, " %s", value[attr_count]);
28147c478bd9Sstevel@tonic-gate 			attr_count++;
28157c478bd9Sstevel@tonic-gate 		}
28167c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate }
28197c478bd9Sstevel@tonic-gate 
28207c478bd9Sstevel@tonic-gate static int
28217c478bd9Sstevel@tonic-gate genent_automount(char *line, int (*cback)())
28227c478bd9Sstevel@tonic-gate {
28237c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ+1];
28247c478bd9Sstevel@tonic-gate 	char *t, *s;
28257c478bd9Sstevel@tonic-gate 	entry_col ecol[2];
28267c478bd9Sstevel@tonic-gate 	struct _ns_automount data;
28277c478bd9Sstevel@tonic-gate 	int retval = 1;
28287c478bd9Sstevel@tonic-gate 	int rc = GENENT_OK;
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate 	/*
28317c478bd9Sstevel@tonic-gate 	 * don't clobber our argument
28327c478bd9Sstevel@tonic-gate 	 */
28337c478bd9Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
2834e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
2835e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
28367c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
2837e1dd0a2fSth 	}
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate 	/* replace every tabspace with single space */
28407c478bd9Sstevel@tonic-gate 	replace_tab2space(line);
28417c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, line);
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 	/*
28447c478bd9Sstevel@tonic-gate 	 * clear column data
28457c478bd9Sstevel@tonic-gate 	 */
28467c478bd9Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 	/*
28497c478bd9Sstevel@tonic-gate 	 * key (col 0)
28507c478bd9Sstevel@tonic-gate 	 */
28517c478bd9Sstevel@tonic-gate 	t = buf;
28527c478bd9Sstevel@tonic-gate 	while (t[0] == ' ')
28537c478bd9Sstevel@tonic-gate 		t++;
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ' ')) == 0) {
28567c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
28577c478bd9Sstevel@tonic-gate 	}
28587c478bd9Sstevel@tonic-gate 	*s++ = 0;
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
28617c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
28627c478bd9Sstevel@tonic-gate 	t = s;
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate 	while (t[0] == ' ')
28657c478bd9Sstevel@tonic-gate 		t++;
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate 	/*
28687c478bd9Sstevel@tonic-gate 	 * mapentry (col 1)
28697c478bd9Sstevel@tonic-gate 	 */
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
28727c478bd9Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 	data.mapname = strdup(databasetype);
28757c478bd9Sstevel@tonic-gate 	data.key = strdup(ecol[0].ec_value.ec_value_val);
28767c478bd9Sstevel@tonic-gate 	data.value = strdup(ecol[1].ec_value.ec_value_val);
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate 	if (flags & F_VERBOSE)
28797c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout,
28807c478bd9Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.key);
28817c478bd9Sstevel@tonic-gate 
28827c478bd9Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
28837c478bd9Sstevel@tonic-gate 
28847c478bd9Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
28857c478bd9Sstevel@tonic-gate 		if (continue_onerror)
28867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2887e1dd0a2fSth 			    gettext("Entry: %s - already Exists,"
2888e1dd0a2fSth 			    " skipping it.\n"), data.key);
28897c478bd9Sstevel@tonic-gate 		else {
28907c478bd9Sstevel@tonic-gate 			rc = GENENT_CBERR;
28917c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2892e1dd0a2fSth 			    gettext("Entry: %s - already Exists\n"),
2893e1dd0a2fSth 			    data.key);
28947c478bd9Sstevel@tonic-gate 		}
28957c478bd9Sstevel@tonic-gate 	} else if (retval)
28967c478bd9Sstevel@tonic-gate 		rc = GENENT_CBERR;
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 	free(data.mapname);
28997c478bd9Sstevel@tonic-gate 	free(data.key);
29007c478bd9Sstevel@tonic-gate 	free(data.value);
29017c478bd9Sstevel@tonic-gate 	return (rc);
29027c478bd9Sstevel@tonic-gate }
29037c478bd9Sstevel@tonic-gate 
29047c478bd9Sstevel@tonic-gate static void
29057c478bd9Sstevel@tonic-gate dump_automount(ns_ldap_result_t *res)
29067c478bd9Sstevel@tonic-gate {
29077c478bd9Sstevel@tonic-gate 	char	**value = NULL;
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 	if (res == NULL)
29107c478bd9Sstevel@tonic-gate 		return;
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "automountKey");
29137c478bd9Sstevel@tonic-gate 	if (value != NULL) {
29147c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
29157c478bd9Sstevel@tonic-gate 		value = __ns_ldap_getAttr(res->entry, "automountInformation");
29167c478bd9Sstevel@tonic-gate 		if (value != NULL)
29177c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "	%s\n", value[0]);
29187c478bd9Sstevel@tonic-gate 		else
29197c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "\n");
29207c478bd9Sstevel@tonic-gate 	}
29217c478bd9Sstevel@tonic-gate }
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate /*
29257c478bd9Sstevel@tonic-gate  * /etc/passwd
29267c478bd9Sstevel@tonic-gate  *
29277c478bd9Sstevel@tonic-gate  */
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate static int
29307c478bd9Sstevel@tonic-gate genent_passwd(char *line, int (*cback)())
29317c478bd9Sstevel@tonic-gate {
29327c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ+1];
29337c478bd9Sstevel@tonic-gate 	char *s, *t;
29347c478bd9Sstevel@tonic-gate 	entry_col ecol[8];
29357c478bd9Sstevel@tonic-gate 	int retval = 1;
29367c478bd9Sstevel@tonic-gate 	char pname[BUFSIZ];
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 	struct passwd	data;
29397c478bd9Sstevel@tonic-gate 	int rc = GENENT_OK;
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 	/*
29437c478bd9Sstevel@tonic-gate 	 * don't clobber our argument
29447c478bd9Sstevel@tonic-gate 	 */
29457c478bd9Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
2946e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
2947e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
29487c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
29497c478bd9Sstevel@tonic-gate 	}
29507c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, line);
29517c478bd9Sstevel@tonic-gate 	t = buf;
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate 	/* ignore empty entries */
29547c478bd9Sstevel@tonic-gate 	if (*t == '\0')
29557c478bd9Sstevel@tonic-gate 		return (GENENT_OK);
29567c478bd9Sstevel@tonic-gate 
29577c478bd9Sstevel@tonic-gate 	/*
29587c478bd9Sstevel@tonic-gate 	 * clear column data
29597c478bd9Sstevel@tonic-gate 	 */
29607c478bd9Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
29617c478bd9Sstevel@tonic-gate 
29627c478bd9Sstevel@tonic-gate 	/*
29637c478bd9Sstevel@tonic-gate 	 * name (col 0)
29647c478bd9Sstevel@tonic-gate 	 */
29657c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
2966e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no password"),
2967e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
29687c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
29697c478bd9Sstevel@tonic-gate 	}
29707c478bd9Sstevel@tonic-gate 	*s++ = 0;
29717c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
29727c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
29737c478bd9Sstevel@tonic-gate 	t = s;
29747c478bd9Sstevel@tonic-gate 
29757c478bd9Sstevel@tonic-gate 	/*
29767c478bd9Sstevel@tonic-gate 	 * passwd (col 1)
29777c478bd9Sstevel@tonic-gate 	 */
29787c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
2979e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no uid"),
2980e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
29817c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
29827c478bd9Sstevel@tonic-gate 	}
29837c478bd9Sstevel@tonic-gate 	*s++ = 0;
29847c478bd9Sstevel@tonic-gate 
29857c478bd9Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
29867c478bd9Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate 	t = s;
29897c478bd9Sstevel@tonic-gate 
29907c478bd9Sstevel@tonic-gate 	/*
29917c478bd9Sstevel@tonic-gate 	 * uid (col 2)
29927c478bd9Sstevel@tonic-gate 	 */
29937c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0 || s == t) {
2994e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no gid"),
2995e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
29967c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
29977c478bd9Sstevel@tonic-gate 	}
29987c478bd9Sstevel@tonic-gate 	*s++ = 0;
29997c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
30007c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
30017c478bd9Sstevel@tonic-gate 	t = s;
30027c478bd9Sstevel@tonic-gate 
30037c478bd9Sstevel@tonic-gate 	/*
30047c478bd9Sstevel@tonic-gate 	 * gid (col 3)
30057c478bd9Sstevel@tonic-gate 	 */
30067c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0 || s == t) {
3007e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no gcos"),
3008e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
30097c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
30107c478bd9Sstevel@tonic-gate 	}
30117c478bd9Sstevel@tonic-gate 	*s++ = 0;
30127c478bd9Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
30137c478bd9Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
30147c478bd9Sstevel@tonic-gate 	t = s;
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 	/*
30177c478bd9Sstevel@tonic-gate 	 * gcos (col 4)
30187c478bd9Sstevel@tonic-gate 	 */
30197c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
3020e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no home"),
3021e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
30227c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
30237c478bd9Sstevel@tonic-gate 	}
30247c478bd9Sstevel@tonic-gate 	*s++ = 0;
30257c478bd9Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_val = t;
30267c478bd9Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_len = strlen(t)+1;
30277c478bd9Sstevel@tonic-gate 	t = s;
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate 	/*
30307c478bd9Sstevel@tonic-gate 	 * home (col 5)
30317c478bd9Sstevel@tonic-gate 	 */
30327c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
3033e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no shell"),
3034e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
30357c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
30367c478bd9Sstevel@tonic-gate 	}
30377c478bd9Sstevel@tonic-gate 	*s++ = 0;
30387c478bd9Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_val = t;
30397c478bd9Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_len = strlen(t)+1;
30407c478bd9Sstevel@tonic-gate 	t = s;
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate 	/*
30437c478bd9Sstevel@tonic-gate 	 * shell (col 6)
30447c478bd9Sstevel@tonic-gate 	 */
30457c478bd9Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_val = t;
30467c478bd9Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_len = strlen(t)+1;
30477c478bd9Sstevel@tonic-gate 
30487c478bd9Sstevel@tonic-gate 	/*
30497c478bd9Sstevel@tonic-gate 	 * build entry
30507c478bd9Sstevel@tonic-gate 	 */
30517c478bd9Sstevel@tonic-gate 	data.pw_name = strdup(ecol[0].ec_value.ec_value_val);
30527c478bd9Sstevel@tonic-gate 
30537c478bd9Sstevel@tonic-gate 	if (flags & F_PASSWD) {
30547c478bd9Sstevel@tonic-gate 		/* Add {crypt} before passwd entry */
30557c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname), "{crypt}%s",
30567c478bd9Sstevel@tonic-gate 		    ecol[1].ec_value.ec_value_val);
30577c478bd9Sstevel@tonic-gate 		data.pw_passwd = strdup(pname);
30587c478bd9Sstevel@tonic-gate 	}
30597c478bd9Sstevel@tonic-gate 	else
30607c478bd9Sstevel@tonic-gate 		data.pw_passwd = NULL;
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
30637c478bd9Sstevel@tonic-gate 	    ecol[2].ec_value.ec_value_val[0] != '\0') {
30647c478bd9Sstevel@tonic-gate 		data.pw_uid = ascii_to_int(ecol[2].ec_value.ec_value_val);
3065e1dd0a2fSth 		if (data.pw_uid == (uid_t)-1) {
30667c478bd9Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3067e1dd0a2fSth 			    gettext("invalid uid : %s"),
3068e1dd0a2fSth 			    ecol[2].ec_value.ec_value_val);
30697c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
30707c478bd9Sstevel@tonic-gate 		}
30717c478bd9Sstevel@tonic-gate 	} else
3072e1dd0a2fSth 		data.pw_uid = (uid_t)-1;
30737c478bd9Sstevel@tonic-gate 
30747c478bd9Sstevel@tonic-gate 	if (ecol[3].ec_value.ec_value_val != NULL &&
3075e1dd0a2fSth 	    ecol[3].ec_value.ec_value_val[0] != '\0') {
30767c478bd9Sstevel@tonic-gate 
30777c478bd9Sstevel@tonic-gate 		data.pw_gid = ascii_to_int(ecol[3].ec_value.ec_value_val);
3078e1dd0a2fSth 		if (data.pw_gid == (uid_t)-1) {
30797c478bd9Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3080e1dd0a2fSth 			    gettext("invalid gid : %s"),
3081e1dd0a2fSth 			    ecol[3].ec_value.ec_value_val);
30827c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
30837c478bd9Sstevel@tonic-gate 		}
30847c478bd9Sstevel@tonic-gate 	} else
3085e1dd0a2fSth 		data.pw_gid = (uid_t)-1;
30867c478bd9Sstevel@tonic-gate 
30877c478bd9Sstevel@tonic-gate 	data.pw_age = NULL;
30887c478bd9Sstevel@tonic-gate 	data.pw_comment = NULL;
30897c478bd9Sstevel@tonic-gate 	data.pw_gecos = strdup(ecol[4].ec_value.ec_value_val);
30907c478bd9Sstevel@tonic-gate 	data.pw_dir = strdup(ecol[5].ec_value.ec_value_val);
30917c478bd9Sstevel@tonic-gate 	data.pw_shell = strdup(ecol[6].ec_value.ec_value_val);
30927c478bd9Sstevel@tonic-gate 
30937c478bd9Sstevel@tonic-gate 	if (flags & F_VERBOSE)
30947c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout,
30957c478bd9Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.pw_name);
30967c478bd9Sstevel@tonic-gate 
30977c478bd9Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
30987c478bd9Sstevel@tonic-gate 
30997c478bd9Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
31007c478bd9Sstevel@tonic-gate 		if (continue_onerror)
31017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3102e1dd0a2fSth 			    gettext("Entry: %s - already Exists,"
3103e1dd0a2fSth 			    " skipping it.\n"), data.pw_name);
31047c478bd9Sstevel@tonic-gate 		else {
31057c478bd9Sstevel@tonic-gate 			rc = GENENT_CBERR;
31067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3107e1dd0a2fSth 			    gettext("Entry: %s - already Exists\n"),
3108e1dd0a2fSth 			    data.pw_name);
31097c478bd9Sstevel@tonic-gate 		}
31107c478bd9Sstevel@tonic-gate 	} else if (retval)
31117c478bd9Sstevel@tonic-gate 		rc = GENENT_CBERR;
31127c478bd9Sstevel@tonic-gate 
31137c478bd9Sstevel@tonic-gate 	free(data.pw_name);
31147c478bd9Sstevel@tonic-gate 	free(data.pw_gecos);
31157c478bd9Sstevel@tonic-gate 	free(data.pw_dir);
31167c478bd9Sstevel@tonic-gate 	free(data.pw_shell);
31177c478bd9Sstevel@tonic-gate 	return (rc);
31187c478bd9Sstevel@tonic-gate }
31197c478bd9Sstevel@tonic-gate 
31207c478bd9Sstevel@tonic-gate 
31217c478bd9Sstevel@tonic-gate static void
31227c478bd9Sstevel@tonic-gate dump_passwd(ns_ldap_result_t *res)
31237c478bd9Sstevel@tonic-gate {
31247c478bd9Sstevel@tonic-gate 	char    **value = NULL;
31257c478bd9Sstevel@tonic-gate 
31267c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "uid");
31277c478bd9Sstevel@tonic-gate 	if (value == NULL)
31287c478bd9Sstevel@tonic-gate 		return;
31297c478bd9Sstevel@tonic-gate 	else
31307c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
31317c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "userPassword");
3132dd1104fbSMichen Chang 
3133dd1104fbSMichen Chang 	/*
3134dd1104fbSMichen Chang 	 * Don't print the encrypted password, Use x to
3135dd1104fbSMichen Chang 	 * indicate it is in the shadow database.
3136dd1104fbSMichen Chang 	 */
3137dd1104fbSMichen Chang 	(void) fprintf(stdout, "x:");
3138dd1104fbSMichen Chang 
31397c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "uidNumber");
31407c478bd9Sstevel@tonic-gate 	if (value && value[0])
31417c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
31427c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "gidNumber");
31437c478bd9Sstevel@tonic-gate 	if (value && value[0])
31447c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
31457c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "gecos");
31467c478bd9Sstevel@tonic-gate 	if (value == NULL)
31477c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
31487c478bd9Sstevel@tonic-gate 	else
31497c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
31507c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "homeDirectory");
31517c478bd9Sstevel@tonic-gate 	if (value == NULL)
31527c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
31537c478bd9Sstevel@tonic-gate 	else
31547c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
31557c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "loginShell");
31567c478bd9Sstevel@tonic-gate 	if (value == NULL)
31577c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
31587c478bd9Sstevel@tonic-gate 	else
31597c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\n", value[0]);
31607c478bd9Sstevel@tonic-gate 
31617c478bd9Sstevel@tonic-gate }
31627c478bd9Sstevel@tonic-gate 
31637c478bd9Sstevel@tonic-gate /*
31647c478bd9Sstevel@tonic-gate  * /etc/shadow
31657c478bd9Sstevel@tonic-gate  */
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate static int
31687c478bd9Sstevel@tonic-gate genent_shadow(char *line, int (*cback)())
31697c478bd9Sstevel@tonic-gate {
31707c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ+1];
31717c478bd9Sstevel@tonic-gate 	char *s, *t;
31727c478bd9Sstevel@tonic-gate 	entry_col ecol[9];
31737c478bd9Sstevel@tonic-gate 	char pname[BUFSIZ];
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate 	struct spwd	data;
31767c478bd9Sstevel@tonic-gate 	int spflag;
317712fbe00aSjs 	int retval;
31787c478bd9Sstevel@tonic-gate 
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate 	/*
31817c478bd9Sstevel@tonic-gate 	 * don't clobber our argument
31827c478bd9Sstevel@tonic-gate 	 */
31837c478bd9Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
3184e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
3185e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
31867c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
31877c478bd9Sstevel@tonic-gate 	}
31887c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, line);
31897c478bd9Sstevel@tonic-gate 	t = buf;
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate 	/* ignore empty entries */
31927c478bd9Sstevel@tonic-gate 	if (*t == '\0')
31937c478bd9Sstevel@tonic-gate 		return (GENENT_OK);
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate 	/*
31967c478bd9Sstevel@tonic-gate 	 * clear column data
31977c478bd9Sstevel@tonic-gate 	 */
31987c478bd9Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
31997c478bd9Sstevel@tonic-gate 
32007c478bd9Sstevel@tonic-gate 	/*
32017c478bd9Sstevel@tonic-gate 	 * name (col 0)
32027c478bd9Sstevel@tonic-gate 	 */
32037c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
3204e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no uid"),
3205e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
32067c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
32077c478bd9Sstevel@tonic-gate 	}
32087c478bd9Sstevel@tonic-gate 	*s++ = 0;
32097c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
32107c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
32117c478bd9Sstevel@tonic-gate 	t = s;
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 	/*
32147c478bd9Sstevel@tonic-gate 	 * passwd (col 1)
32157c478bd9Sstevel@tonic-gate 	 */
32167c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
3217e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("Improper format"),
3218e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
32197c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
32207c478bd9Sstevel@tonic-gate 	}
32217c478bd9Sstevel@tonic-gate 	*s++ = 0;
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
32247c478bd9Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
32257c478bd9Sstevel@tonic-gate 
32267c478bd9Sstevel@tonic-gate 	t = s;
32277c478bd9Sstevel@tonic-gate 
32287c478bd9Sstevel@tonic-gate 	/*
32297c478bd9Sstevel@tonic-gate 	 * shadow last change (col 2)
32307c478bd9Sstevel@tonic-gate 	 */
32317c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
3232e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("Improper format"),
3233e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
32347c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
32357c478bd9Sstevel@tonic-gate 	}
32367c478bd9Sstevel@tonic-gate 	*s++ = 0;
32377c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
32387c478bd9Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
32397c478bd9Sstevel@tonic-gate 	t = s;
32407c478bd9Sstevel@tonic-gate 
32417c478bd9Sstevel@tonic-gate 	/*
32427c478bd9Sstevel@tonic-gate 	 * shadow min (col 3)
32437c478bd9Sstevel@tonic-gate 	 */
32447c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
3245e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("Improper format"),
3246e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
32477c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
32487c478bd9Sstevel@tonic-gate 	}
32497c478bd9Sstevel@tonic-gate 	*s++ = 0;
32507c478bd9Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
32517c478bd9Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
32527c478bd9Sstevel@tonic-gate 	t = s;
32537c478bd9Sstevel@tonic-gate 
32547c478bd9Sstevel@tonic-gate 	/*
32557c478bd9Sstevel@tonic-gate 	 * shadow max (col 4)
32567c478bd9Sstevel@tonic-gate 	 */
32577c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
3258e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("Improper format"),
3259e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
32607c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
32617c478bd9Sstevel@tonic-gate 	}
32627c478bd9Sstevel@tonic-gate 	*s++ = 0;
32637c478bd9Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_val = t;
32647c478bd9Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_len = strlen(t)+1;
32657c478bd9Sstevel@tonic-gate 	t = s;
32667c478bd9Sstevel@tonic-gate 
32677c478bd9Sstevel@tonic-gate 	/*
32687c478bd9Sstevel@tonic-gate 	 * shadow warn (col 5)
32697c478bd9Sstevel@tonic-gate 	 */
32707c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
3271e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("Improper format"),
3272e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
32737c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
32747c478bd9Sstevel@tonic-gate 	}
32757c478bd9Sstevel@tonic-gate 	*s++ = 0;
32767c478bd9Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_val = t;
32777c478bd9Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_len = strlen(t)+1;
32787c478bd9Sstevel@tonic-gate 	t = s;
32797c478bd9Sstevel@tonic-gate 
32807c478bd9Sstevel@tonic-gate 	/*
32817c478bd9Sstevel@tonic-gate 	 * shadow inactive (col 6)
32827c478bd9Sstevel@tonic-gate 	 */
32837c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) != 0) {
32847c478bd9Sstevel@tonic-gate 	*s++ = 0;
32857c478bd9Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_val = t;
32867c478bd9Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_len = strlen(t)+1;
32877c478bd9Sstevel@tonic-gate 	t = s;
32887c478bd9Sstevel@tonic-gate 	}
32897c478bd9Sstevel@tonic-gate 
32907c478bd9Sstevel@tonic-gate 	/*
32917c478bd9Sstevel@tonic-gate 	 * shadow expire  (col 7)
32927c478bd9Sstevel@tonic-gate 	 */
32937c478bd9Sstevel@tonic-gate 	if ((s = strchr(t, ':')) != 0) {
32947c478bd9Sstevel@tonic-gate 	*s++ = 0;
32957c478bd9Sstevel@tonic-gate 	ecol[7].ec_value.ec_value_val = t;
32967c478bd9Sstevel@tonic-gate 	ecol[7].ec_value.ec_value_len = strlen(t)+1;
32977c478bd9Sstevel@tonic-gate 	t = s;
32987c478bd9Sstevel@tonic-gate 
32997c478bd9Sstevel@tonic-gate 	/*
33007c478bd9Sstevel@tonic-gate 	 * flag (col 8)
33017c478bd9Sstevel@tonic-gate 	 */
33027c478bd9Sstevel@tonic-gate 	ecol[8].ec_value.ec_value_val = t;
33037c478bd9Sstevel@tonic-gate 	ecol[8].ec_value.ec_value_len = strlen(t)+1;
33047c478bd9Sstevel@tonic-gate 	}
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate 	/*
33077c478bd9Sstevel@tonic-gate 	 * build entry
33087c478bd9Sstevel@tonic-gate 	 */
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate 	data.sp_namp = strdup(ecol[0].ec_value.ec_value_val);
33117c478bd9Sstevel@tonic-gate 
33127c478bd9Sstevel@tonic-gate 	if (ecol[1].ec_value.ec_value_val != NULL &&
3313e1dd0a2fSth 	    ecol[1].ec_value.ec_value_val[0] != '\0') {
33147c478bd9Sstevel@tonic-gate 		/* Add {crypt} before passwd entry */
33157c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname), "{crypt}%s",
33167c478bd9Sstevel@tonic-gate 		    ecol[1].ec_value.ec_value_val);
33177c478bd9Sstevel@tonic-gate 		data.sp_pwdp = strdup(pname);
3318dd1104fbSMichen Chang 	} else {
3319dd1104fbSMichen Chang 		/*
3320dd1104fbSMichen Chang 		 * no password (e.g., deleted by "passwd -d"):
3321dd1104fbSMichen Chang 		 * use the special value NS_LDAP_NO_UNIX_PASSWORD
3322dd1104fbSMichen Chang 		 * instead.
3323dd1104fbSMichen Chang 		 */
3324dd1104fbSMichen Chang 		(void) snprintf(pname, sizeof (pname), "{crypt}%s",
3325dd1104fbSMichen Chang 		    NS_LDAP_NO_UNIX_PASSWORD);
3326dd1104fbSMichen Chang 		data.sp_pwdp = strdup(pname);
3327dd1104fbSMichen Chang 	}
33287c478bd9Sstevel@tonic-gate 
33297c478bd9Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
3330e1dd0a2fSth 	    ecol[2].ec_value.ec_value_val[0] != '\0') {
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 		data.sp_lstchg = ascii_to_int(ecol[2].ec_value.ec_value_val);
33337c478bd9Sstevel@tonic-gate 		if (data.sp_lstchg < -1) {
33347c478bd9Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3335e1dd0a2fSth 			    gettext("invalid last changed date: %s"),
33367c478bd9Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
33377c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
33387c478bd9Sstevel@tonic-gate 		}
33397c478bd9Sstevel@tonic-gate 	} else
33407c478bd9Sstevel@tonic-gate 		data.sp_lstchg = -1;
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate 	if (ecol[3].ec_value.ec_value_val != NULL &&
3343e1dd0a2fSth 	    ecol[3].ec_value.ec_value_val[0] != '\0') {
33447c478bd9Sstevel@tonic-gate 
33457c478bd9Sstevel@tonic-gate 		data.sp_min = ascii_to_int(ecol[3].ec_value.ec_value_val);
33467c478bd9Sstevel@tonic-gate 		if (data.sp_min < -1) {
33477c478bd9Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3348e1dd0a2fSth 			    gettext("invalid sp_min : %s"),
33497c478bd9Sstevel@tonic-gate 			    ecol[3].ec_value.ec_value_val);
33507c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
33517c478bd9Sstevel@tonic-gate 		}
33527c478bd9Sstevel@tonic-gate 	} else
33537c478bd9Sstevel@tonic-gate 		data.sp_min = -1;
33547c478bd9Sstevel@tonic-gate 
33557c478bd9Sstevel@tonic-gate 	if (ecol[4].ec_value.ec_value_val != NULL &&
3356e1dd0a2fSth 	    ecol[4].ec_value.ec_value_val[0] != '\0') {
33577c478bd9Sstevel@tonic-gate 
33587c478bd9Sstevel@tonic-gate 		data.sp_max = ascii_to_int(ecol[4].ec_value.ec_value_val);
33597c478bd9Sstevel@tonic-gate 		if (data.sp_max < -1) {
33607c478bd9Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3361e1dd0a2fSth 			    gettext("invalid sp_max : %s"),
33627c478bd9Sstevel@tonic-gate 			    ecol[4].ec_value.ec_value_val);
33637c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
33647c478bd9Sstevel@tonic-gate 		}
33657c478bd9Sstevel@tonic-gate 	} else
33667c478bd9Sstevel@tonic-gate 		data.sp_max = -1;
33677c478bd9Sstevel@tonic-gate 
33687c478bd9Sstevel@tonic-gate 	if (ecol[5].ec_value.ec_value_val != NULL &&
3369e1dd0a2fSth 	    ecol[5].ec_value.ec_value_val[0] != '\0') {
33707c478bd9Sstevel@tonic-gate 
33717c478bd9Sstevel@tonic-gate 		data.sp_warn = ascii_to_int(ecol[5].ec_value.ec_value_val);
33727c478bd9Sstevel@tonic-gate 		if (data.sp_warn < -1) {
33737c478bd9Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3374e1dd0a2fSth 			    gettext("invalid sp_warn : %s"),
33757c478bd9Sstevel@tonic-gate 			    ecol[5].ec_value.ec_value_val);
33767c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
33777c478bd9Sstevel@tonic-gate 		}
33787c478bd9Sstevel@tonic-gate 	} else
33797c478bd9Sstevel@tonic-gate 		data.sp_warn = -1;
33807c478bd9Sstevel@tonic-gate 
33817c478bd9Sstevel@tonic-gate 	if (ecol[6].ec_value.ec_value_val != NULL &&
3382e1dd0a2fSth 	    ecol[6].ec_value.ec_value_val[0] != '\0') {
33837c478bd9Sstevel@tonic-gate 
33847c478bd9Sstevel@tonic-gate 		data.sp_inact = ascii_to_int(ecol[6].ec_value.ec_value_val);
33857c478bd9Sstevel@tonic-gate 		if (data.sp_inact < -1) {
33867c478bd9Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3387e1dd0a2fSth 			    gettext("invalid sp_inact : %s"),
33887c478bd9Sstevel@tonic-gate 			    ecol[6].ec_value.ec_value_val);
33897c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
33907c478bd9Sstevel@tonic-gate 		}
33917c478bd9Sstevel@tonic-gate 	} else
33927c478bd9Sstevel@tonic-gate 		data.sp_inact = -1;
33937c478bd9Sstevel@tonic-gate 
33947c478bd9Sstevel@tonic-gate 	if (ecol[7].ec_value.ec_value_val != NULL &&
3395e1dd0a2fSth 	    ecol[7].ec_value.ec_value_val[0] != '\0') {
33967c478bd9Sstevel@tonic-gate 
33977c478bd9Sstevel@tonic-gate 		data.sp_expire = ascii_to_int(ecol[7].ec_value.ec_value_val);
33987c478bd9Sstevel@tonic-gate 		if (data.sp_expire < -1) {
33997c478bd9Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3400e1dd0a2fSth 			    gettext("invalid login expiry date : %s"),
34017c478bd9Sstevel@tonic-gate 			    ecol[7].ec_value.ec_value_val);
34027c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
34037c478bd9Sstevel@tonic-gate 		}
34047c478bd9Sstevel@tonic-gate 	} else
34057c478bd9Sstevel@tonic-gate 		data.sp_expire = -1;
34067c478bd9Sstevel@tonic-gate 
34077c478bd9Sstevel@tonic-gate 	if (ecol[8].ec_value.ec_value_val != NULL &&
3408e1dd0a2fSth 	    ecol[8].ec_value.ec_value_val[0] != '\0') {
34097c478bd9Sstevel@tonic-gate 
34107c478bd9Sstevel@tonic-gate 		/*
34117c478bd9Sstevel@tonic-gate 		 * data.sp_flag is an unsigned int,
34127c478bd9Sstevel@tonic-gate 		 * assign -1 to it, make no sense.
34137c478bd9Sstevel@tonic-gate 		 * Use spflag here to avoid lint warning.
34147c478bd9Sstevel@tonic-gate 		 */
34157c478bd9Sstevel@tonic-gate 		spflag = ascii_to_int(ecol[8].ec_value.ec_value_val);
34167c478bd9Sstevel@tonic-gate 		if (spflag < 0) {
34177c478bd9Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
3418e1dd0a2fSth 			    gettext("invalid flag value: %s"),
34197c478bd9Sstevel@tonic-gate 			    ecol[8].ec_value.ec_value_val);
34207c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
34217c478bd9Sstevel@tonic-gate 		} else
34227c478bd9Sstevel@tonic-gate 			data.sp_flag = spflag;
34237c478bd9Sstevel@tonic-gate 	} else
34247c478bd9Sstevel@tonic-gate 		data.sp_flag = 0;
34257c478bd9Sstevel@tonic-gate 
34267c478bd9Sstevel@tonic-gate 	if (flags & F_VERBOSE)
34277c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout,
34287c478bd9Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.sp_namp);
34297c478bd9Sstevel@tonic-gate 
343012fbe00aSjs 	retval = (*cback)(&data, 1);
343112fbe00aSjs 	if (retval != NS_LDAP_SUCCESS) {
343212fbe00aSjs 		if (retval == LDAP_NO_SUCH_OBJECT)
343312fbe00aSjs 			(void) fprintf(stdout,
3434e1dd0a2fSth 			    gettext("Cannot add shadow entry (%s), "
3435e1dd0a2fSth 			    "add passwd entry first\n"), data.sp_namp);
343612fbe00aSjs 		if (continue_onerror == 0)
343712fbe00aSjs 			return (GENENT_CBERR);
343812fbe00aSjs 	}
34397c478bd9Sstevel@tonic-gate 
34407c478bd9Sstevel@tonic-gate 	free(data.sp_namp);
34417c478bd9Sstevel@tonic-gate 	free(data.sp_pwdp);
34427c478bd9Sstevel@tonic-gate 	return (GENENT_OK);
34437c478bd9Sstevel@tonic-gate }
34447c478bd9Sstevel@tonic-gate 
34457c478bd9Sstevel@tonic-gate static void
34467c478bd9Sstevel@tonic-gate dump_shadow(ns_ldap_result_t *res)
34477c478bd9Sstevel@tonic-gate {
34487c478bd9Sstevel@tonic-gate 	char    **value = NULL;
34497c478bd9Sstevel@tonic-gate 	char   pnam[256];
34507c478bd9Sstevel@tonic-gate 
34517c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "uid");
34527c478bd9Sstevel@tonic-gate 	if (value == NULL)
34537c478bd9Sstevel@tonic-gate 		return;
34547c478bd9Sstevel@tonic-gate 	else
34557c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
34567c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "userPassword");
34577c478bd9Sstevel@tonic-gate 	if (value == NULL)
34587c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "*:");
34597c478bd9Sstevel@tonic-gate 	else {
34607c478bd9Sstevel@tonic-gate 		(void) strcpy(pnam, value[0]);
3461dd1104fbSMichen Chang 		if (strncasecmp(value[0], "{crypt}", 7) == 0) {
3462dd1104fbSMichen Chang 			if (strcmp(pnam + 7, NS_LDAP_NO_UNIX_PASSWORD) == 0)
3463dd1104fbSMichen Chang 				(void) fprintf(stdout, ":");
3464dd1104fbSMichen Chang 			else
3465dd1104fbSMichen Chang 				(void) fprintf(stdout, "%s:", (pnam+7));
3466dd1104fbSMichen Chang 		} else
34677c478bd9Sstevel@tonic-gate 			(void) fprintf(stdout, "*:");
34687c478bd9Sstevel@tonic-gate 	}
34697c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "shadowLastChange");
34707c478bd9Sstevel@tonic-gate 	if (value == NULL)
34717c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
34727c478bd9Sstevel@tonic-gate 	else
34737c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
34747c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "shadowMin");
34757c478bd9Sstevel@tonic-gate 	if (value == NULL)
34767c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
34777c478bd9Sstevel@tonic-gate 	else
34787c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
34797c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "shadowMax");
34807c478bd9Sstevel@tonic-gate 	if (value == NULL)
34817c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
34827c478bd9Sstevel@tonic-gate 	else
34837c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
34847c478bd9Sstevel@tonic-gate 
3485dd1104fbSMichen Chang 	value = __ns_ldap_getAttr(res->entry, "shadowWarning");
3486dd1104fbSMichen Chang 	if (value == NULL)
3487dd1104fbSMichen Chang 		(void) fprintf(stdout, ":");
3488dd1104fbSMichen Chang 	else
3489dd1104fbSMichen Chang 		(void) fprintf(stdout, "%s:", value[0]);
34907c478bd9Sstevel@tonic-gate 
3491dd1104fbSMichen Chang 	value = __ns_ldap_getAttr(res->entry, "shadowInactive");
3492dd1104fbSMichen Chang 	if (value == NULL)
3493dd1104fbSMichen Chang 		(void) fprintf(stdout, ":");
3494dd1104fbSMichen Chang 	else
3495dd1104fbSMichen Chang 		(void) fprintf(stdout, "%s:", value[0]);
3496dd1104fbSMichen Chang 
3497dd1104fbSMichen Chang 	value = __ns_ldap_getAttr(res->entry, "shadowExpire");
3498dd1104fbSMichen Chang 	if (value == NULL)
3499dd1104fbSMichen Chang 		(void) fprintf(stdout, ":");
3500dd1104fbSMichen Chang 	else
3501dd1104fbSMichen Chang 		(void) fprintf(stdout, "%s:", value[0]);
35027c478bd9Sstevel@tonic-gate 
3503dd1104fbSMichen Chang 	value = __ns_ldap_getAttr(res->entry, "shadowFlag");
3504dd1104fbSMichen Chang 	if (value == NULL || value[0] == NULL || strcmp(value[0], "0") == 0)
3505dd1104fbSMichen Chang 		(void) fprintf(stdout, "\n");
3506dd1104fbSMichen Chang 	else
3507dd1104fbSMichen Chang 		(void) fprintf(stdout, "%s\n", value[0]);
3508dd1104fbSMichen Chang }
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate static int
35117c478bd9Sstevel@tonic-gate genent_bootparams(char *line, int (*cback)())
35127c478bd9Sstevel@tonic-gate {
35137c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ+1];
35147c478bd9Sstevel@tonic-gate 	char *t;
35157c478bd9Sstevel@tonic-gate 	entry_col ecol[2];
35167c478bd9Sstevel@tonic-gate 	int ctr = 0, retval = 1;
35177c478bd9Sstevel@tonic-gate 
35187c478bd9Sstevel@tonic-gate 	struct _ns_bootp data;
35197c478bd9Sstevel@tonic-gate 	char *parameter;
35207c478bd9Sstevel@tonic-gate 	int rc = GENENT_OK;
35217c478bd9Sstevel@tonic-gate 
35227c478bd9Sstevel@tonic-gate 	/*
35237c478bd9Sstevel@tonic-gate 	 * don't clobber our argument
35247c478bd9Sstevel@tonic-gate 	 */
35257c478bd9Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
3526e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
3527e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
35287c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
35297c478bd9Sstevel@tonic-gate 	}
35307c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, line);
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate 	/*
35337c478bd9Sstevel@tonic-gate 	 * clear column data
35347c478bd9Sstevel@tonic-gate 	 */
35357c478bd9Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
35367c478bd9Sstevel@tonic-gate 
35377c478bd9Sstevel@tonic-gate 
35387c478bd9Sstevel@tonic-gate 	/*
35397c478bd9Sstevel@tonic-gate 	 * cname (col 0)
35407c478bd9Sstevel@tonic-gate 	 */
35417c478bd9Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
3542e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("no cname"),
3543e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
35447c478bd9Sstevel@tonic-gate 		return (GENENT_PARSEERR);
35457c478bd9Sstevel@tonic-gate 	}
35467c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
35477c478bd9Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate 
35507c478bd9Sstevel@tonic-gate 
35517c478bd9Sstevel@tonic-gate 	/* build entry */
35527c478bd9Sstevel@tonic-gate 	data.name = strdup(ecol[0].ec_value.ec_value_val);
35537c478bd9Sstevel@tonic-gate 
35547c478bd9Sstevel@tonic-gate 	/*
35557c478bd9Sstevel@tonic-gate 	 * name (col 1)
35567c478bd9Sstevel@tonic-gate 	 */
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate 	data.param = NULL;
35597c478bd9Sstevel@tonic-gate 
3560f07af016Sdm 	while (t = strtok(NULL, " \t"))  {
35617c478bd9Sstevel@tonic-gate 
35627c478bd9Sstevel@tonic-gate 		/*
35637c478bd9Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
35647c478bd9Sstevel@tonic-gate 		 */
35657c478bd9Sstevel@tonic-gate 
35667c478bd9Sstevel@tonic-gate 
35677c478bd9Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
35687c478bd9Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
35697c478bd9Sstevel@tonic-gate 
35707c478bd9Sstevel@tonic-gate 		ctr++;
35717c478bd9Sstevel@tonic-gate 		parameter = strdup(ecol[1].ec_value.ec_value_val);
35727c478bd9Sstevel@tonic-gate 		if ((data.param = (char **)realloc(data.param,
3573e1dd0a2fSth 		    (ctr + 1) * sizeof (char **))) == NULL) {
35747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
35757c478bd9Sstevel@tonic-gate 			exit(1);
35767c478bd9Sstevel@tonic-gate 		}
35777c478bd9Sstevel@tonic-gate 		data.param[ctr-1] = parameter;
35787c478bd9Sstevel@tonic-gate 
3579f07af016Sdm 	}
35807c478bd9Sstevel@tonic-gate 
35817c478bd9Sstevel@tonic-gate 
35827c478bd9Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
35837c478bd9Sstevel@tonic-gate 	if ((data.param = (char **)realloc(data.param,
3584e1dd0a2fSth 	    (ctr + 1) * sizeof (char **))) == NULL) {
35857c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
35867c478bd9Sstevel@tonic-gate 		exit(1);
35877c478bd9Sstevel@tonic-gate 	}
35887c478bd9Sstevel@tonic-gate 	data.param[ctr] = NULL;
35897c478bd9Sstevel@tonic-gate 
35907c478bd9Sstevel@tonic-gate 	if (flags & F_VERBOSE)
35917c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout,
35927c478bd9Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.name);
35937c478bd9Sstevel@tonic-gate 
35947c478bd9Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
35957c478bd9Sstevel@tonic-gate 
35967c478bd9Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
35977c478bd9Sstevel@tonic-gate 		if (continue_onerror)
35987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3599e1dd0a2fSth 			    gettext("Entry: %s - already Exists,"
3600e1dd0a2fSth 			    " skipping it.\n"), data.name);
36017c478bd9Sstevel@tonic-gate 		else {
36027c478bd9Sstevel@tonic-gate 			rc = GENENT_CBERR;
36037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3604e1dd0a2fSth 			    gettext("Entry: %s - already Exists\n"),
3605e1dd0a2fSth 			    data.name);
36067c478bd9Sstevel@tonic-gate 		}
36077c478bd9Sstevel@tonic-gate 	} else if (retval)
36087c478bd9Sstevel@tonic-gate 		rc = GENENT_CBERR;
36097c478bd9Sstevel@tonic-gate 
36107c478bd9Sstevel@tonic-gate 	free(data.name);
36117c478bd9Sstevel@tonic-gate 	free(data.param);
36127c478bd9Sstevel@tonic-gate 
36137c478bd9Sstevel@tonic-gate 	return (rc);
36147c478bd9Sstevel@tonic-gate 
36157c478bd9Sstevel@tonic-gate }
36167c478bd9Sstevel@tonic-gate 
3617e1dd0a2fSth /*
3618e1dd0a2fSth  * Count number of tokens in string which has tokens separated by colons.
3619e1dd0a2fSth  *
3620e1dd0a2fSth  * NULL or "" - 0 tokens
3621e1dd0a2fSth  * "foo" - 1 token
3622e1dd0a2fSth  * "foo:bar" - 2 tokens
3623e1dd0a2fSth  * ":bar" - 2 tokens, first empty
3624e1dd0a2fSth  * "::" - 3 tokens, all empty
3625e1dd0a2fSth  */
3626e1dd0a2fSth static int
3627e1dd0a2fSth count_tokens(char *string, char delim)
3628e1dd0a2fSth {
3629e1dd0a2fSth 	int i = 0;
3630e1dd0a2fSth 	char *s = string;
3631e1dd0a2fSth 
3632e1dd0a2fSth 	if (string == NULL || *string == '\0')
3633e1dd0a2fSth 		return (0);
3634e1dd0a2fSth 
3635e1dd0a2fSth 	/* Count delimiters */
3636e1dd0a2fSth 	while ((s = strchr(s, delim)) != NULL && *s != '\0') {
3637e1dd0a2fSth 		i++;
3638e1dd0a2fSth 		s++;
3639e1dd0a2fSth 	}
3640e1dd0a2fSth 
3641e1dd0a2fSth 	return (i + 1);
3642e1dd0a2fSth }
3643e1dd0a2fSth 
3644e1dd0a2fSth static int
3645e1dd0a2fSth genent_project(char *line, int (*cback)())
3646e1dd0a2fSth {
3647e1dd0a2fSth 	char buf[BUFSIZ+1];
3648e1dd0a2fSth 	char *b = buf;
3649e1dd0a2fSth 	char *s;
3650e1dd0a2fSth 	int rc = GENENT_OK, retval;
3651e1dd0a2fSth 	int index = 0;
3652e1dd0a2fSth 	struct project data;
3653e1dd0a2fSth 
3654e1dd0a2fSth 	(void) memset(&data, 0, sizeof (struct project));
3655e1dd0a2fSth 
3656e1dd0a2fSth 	/*
3657e1dd0a2fSth 	 * don't clobber our argument
3658e1dd0a2fSth 	 */
3659e1dd0a2fSth 	if (strlen(line) >= sizeof (buf)) {
3660e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("line too long"),
3661e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
3662e1dd0a2fSth 		return (GENENT_PARSEERR);
3663e1dd0a2fSth 	}
3664e1dd0a2fSth 
3665e1dd0a2fSth 	if (count_tokens(line, ':') != 6) {
3666e1dd0a2fSth 		(void) strlcpy(parse_err_msg, gettext("Improper format"),
3667e1dd0a2fSth 		    PARSE_ERR_MSG_LEN);
3668e1dd0a2fSth 		return (GENENT_PARSEERR);
3669e1dd0a2fSth 	}
3670e1dd0a2fSth 
3671e1dd0a2fSth 	(void) strcpy(buf, line);
3672e1dd0a2fSth 
3673e1dd0a2fSth 	s = strsep(&b, ":");
3674e1dd0a2fSth 	while (s != NULL) {
3675e1dd0a2fSth 		switch (index) {
3676e1dd0a2fSth 		/* Project name */
3677e1dd0a2fSth 		case 0:
3678e1dd0a2fSth 			if (check_projname(s) != 0) {
3679e1dd0a2fSth 				(void) strlcpy(parse_err_msg,
3680e1dd0a2fSth 				    gettext("invalid project name"),
3681e1dd0a2fSth 				    PARSE_ERR_MSG_LEN);
3682e1dd0a2fSth 				return (GENENT_PARSEERR);
3683e1dd0a2fSth 			} else {
3684e1dd0a2fSth 				data.pj_name = strdup(s);
3685e1dd0a2fSth 			}
3686e1dd0a2fSth 			break;
3687e1dd0a2fSth 
3688e1dd0a2fSth 		/* Project ID */
3689e1dd0a2fSth 		case 1:
3690e1dd0a2fSth 		{
3691e1dd0a2fSth 			char *endptr = NULL;
3692e1dd0a2fSth 			int projid = strtoul(s, &endptr, 10);
3693e1dd0a2fSth 
3694e1dd0a2fSth 			if (*s == '\0' || strlen(endptr) != 0 || projid < 0 ||
3695e1dd0a2fSth 			    projid > MAXPROJID) {
3696e1dd0a2fSth 				(void) strlcpy(parse_err_msg,
3697e1dd0a2fSth 				    gettext("invalid project id"),
3698e1dd0a2fSth 				    PARSE_ERR_MSG_LEN);
3699e1dd0a2fSth 				return (GENENT_PARSEERR);
3700e1dd0a2fSth 			} else {
3701e1dd0a2fSth 				data.pj_projid = projid;
3702e1dd0a2fSth 			}
3703e1dd0a2fSth 			break;
3704e1dd0a2fSth 		}
3705e1dd0a2fSth 
3706e1dd0a2fSth 		/* Project description */
3707e1dd0a2fSth 		case 2:
3708e1dd0a2fSth 			if (*s != '\0')
3709e1dd0a2fSth 				data.pj_comment = strdup(s);
3710e1dd0a2fSth 			break;
3711e1dd0a2fSth 
3712e1dd0a2fSth 		/* Project users */
3713e1dd0a2fSth 		case 3:
3714e1dd0a2fSth 		{
3715e1dd0a2fSth 			if (*s == '\0')
3716e1dd0a2fSth 				break;
3717e1dd0a2fSth 
3718e1dd0a2fSth 			char *usrlist = strdup(s);
3719e1dd0a2fSth 			int   i = 0;
3720e1dd0a2fSth 			int   usr_count = count_tokens(usrlist, ',');
3721e1dd0a2fSth 			char *u = strsep(&usrlist, ",");
3722e1dd0a2fSth 
3723e1dd0a2fSth 			if (usr_count == 0) {
3724e1dd0a2fSth 				free(usrlist);
3725e1dd0a2fSth 				break;
3726e1dd0a2fSth 			}
3727e1dd0a2fSth 
3728e1dd0a2fSth 			/* +1 to NULL-terminate the array */
3729e1dd0a2fSth 			data.pj_users = (char **)calloc(usr_count + 1,
3730e1dd0a2fSth 			    sizeof (char *));
3731e1dd0a2fSth 
3732e1dd0a2fSth 			while (u != NULL) {
3733e1dd0a2fSth 				data.pj_users[i++] = strdup(u);
3734e1dd0a2fSth 				u = strsep(&usrlist, ",");
3735e1dd0a2fSth 			}
3736e1dd0a2fSth 
3737e1dd0a2fSth 			free(usrlist);
3738e1dd0a2fSth 			break;
3739e1dd0a2fSth 		}
3740e1dd0a2fSth 
3741e1dd0a2fSth 		/* Project groups */
3742e1dd0a2fSth 		case 4:
3743e1dd0a2fSth 		{
3744e1dd0a2fSth 			if (*s == '\0')
3745e1dd0a2fSth 				break;
3746e1dd0a2fSth 
3747e1dd0a2fSth 			char *grouplist = strdup(s);
3748e1dd0a2fSth 			int   i = 0;
3749e1dd0a2fSth 			int   grp_count = count_tokens(grouplist, ',');
3750e1dd0a2fSth 			char *g = strsep(&grouplist, ",");
3751e1dd0a2fSth 
3752e1dd0a2fSth 			if (grp_count == 0) {
3753e1dd0a2fSth 				free(grouplist);
3754e1dd0a2fSth 				break;
3755e1dd0a2fSth 			}
3756e1dd0a2fSth 
3757e1dd0a2fSth 			/* +1 to NULL-terminate the array */
3758e1dd0a2fSth 			data.pj_groups = (char **)calloc(grp_count + 1,
3759e1dd0a2fSth 			    sizeof (char *));
3760e1dd0a2fSth 
3761e1dd0a2fSth 			while (g != NULL) {
3762e1dd0a2fSth 				data.pj_groups[i++] = strdup(g);
3763e1dd0a2fSth 				g = strsep(&grouplist, ",");
3764e1dd0a2fSth 			}
3765e1dd0a2fSth 
3766e1dd0a2fSth 			free(grouplist);
3767e1dd0a2fSth 			break;
3768e1dd0a2fSth 		}
3769e1dd0a2fSth 
3770e1dd0a2fSth 		/* Attributes */
3771e1dd0a2fSth 		case 5:
3772e1dd0a2fSth 			if (*s != '\0')
3773e1dd0a2fSth 				data.pj_attr = strdup(s);
3774e1dd0a2fSth 
3775e1dd0a2fSth 			break;
3776e1dd0a2fSth 		}
3777e1dd0a2fSth 
3778e1dd0a2fSth 		/* Next token */
3779e1dd0a2fSth 		s = strsep(&b, ":");
3780e1dd0a2fSth 		index++;
3781e1dd0a2fSth 	}
3782e1dd0a2fSth 
3783e1dd0a2fSth 	if (flags & F_VERBOSE)
3784e1dd0a2fSth 		(void) fprintf(stdout,
3785e1dd0a2fSth 		    gettext("Adding entry : %s\n"), data.pj_name);
3786e1dd0a2fSth 
3787e1dd0a2fSth 	retval = (*cback)(&data, 0);
3788e1dd0a2fSth 
3789e1dd0a2fSth 	if (retval == LDAP_ALREADY_EXISTS) {
3790e1dd0a2fSth 		if (continue_onerror)
3791e1dd0a2fSth 			(void) fprintf(stderr,
3792e1dd0a2fSth 			    gettext("Entry: %s - already Exists,"
3793e1dd0a2fSth 			    " skipping it.\n"), data.pj_name);
3794e1dd0a2fSth 		else {
3795e1dd0a2fSth 			rc = GENENT_CBERR;
3796e1dd0a2fSth 			(void) fprintf(stderr,
3797e1dd0a2fSth 			    gettext("Entry: %s - already Exists\n"),
3798e1dd0a2fSth 			    data.pj_name);
3799e1dd0a2fSth 		}
3800e1dd0a2fSth 	} else if (retval)
3801e1dd0a2fSth 		rc = GENENT_CBERR;
3802e1dd0a2fSth 
3803e1dd0a2fSth 	/* Clean up */
3804e1dd0a2fSth 	free(data.pj_name);
3805e1dd0a2fSth 	free(data.pj_attr);
3806e1dd0a2fSth 	if (data.pj_users != NULL) {
3807e1dd0a2fSth 		for (index = 0; data.pj_users[index] != NULL; index++)
3808e1dd0a2fSth 			free(data.pj_users[index]);
3809e1dd0a2fSth 		free(data.pj_users);
3810e1dd0a2fSth 	}
3811e1dd0a2fSth 	if (data.pj_groups != NULL) {
3812e1dd0a2fSth 		for (index = 0; data.pj_groups[index] != NULL; index++)
3813e1dd0a2fSth 			free(data.pj_groups[index]);
3814e1dd0a2fSth 		free(data.pj_groups);
3815e1dd0a2fSth 	}
3816e1dd0a2fSth 
3817e1dd0a2fSth 	return (rc);
3818e1dd0a2fSth }
3819e1dd0a2fSth 
3820e1dd0a2fSth static void
3821e1dd0a2fSth dump_project(ns_ldap_result_t *res)
3822e1dd0a2fSth {
3823e1dd0a2fSth 	char    **value = NULL;
3824*c59d9dffSToomas Soome 	char	*endptr = NULL;
3825*c59d9dffSToomas Soome 	int	projid;
3826e1dd0a2fSth 
3827e1dd0a2fSth 	if (res == NULL || res->entry == NULL)
3828e1dd0a2fSth 		return;
3829e1dd0a2fSth 
3830e1dd0a2fSth 	/* Sanity checking */
3831e1dd0a2fSth 	value = __ns_ldap_getAttr(res->entry, "SolarisProjectID");
3832e1dd0a2fSth 
3833e1dd0a2fSth 	if (value[0] == NULL)
3834e1dd0a2fSth 		return;
3835e1dd0a2fSth 
3836e1dd0a2fSth 	projid = strtoul(value[0], &endptr, 10);
3837e1dd0a2fSth 	if (*value[0] == '\0' || strlen(endptr) != 0 || projid < 0 ||
3838e1dd0a2fSth 	    projid > MAXPROJID)
3839e1dd0a2fSth 		return;
3840e1dd0a2fSth 
3841e1dd0a2fSth 	value = __ns_ldap_getAttr(res->entry, "SolarisProjectName");
3842e1dd0a2fSth 	if (value && value[0] && check_projname(value[0]) == 0)
3843e1dd0a2fSth 		(void) fprintf(stdout, "%s:", value[0]);
3844e1dd0a2fSth 	else
3845e1dd0a2fSth 		return;
3846e1dd0a2fSth 
3847e1dd0a2fSth 	(void) fprintf(stdout, "%d:", projid);
3848e1dd0a2fSth 
3849e1dd0a2fSth 	value = __ns_ldap_getAttr(res->entry, "description");
3850e1dd0a2fSth 	if (value && value[0])
3851e1dd0a2fSth 		(void) fprintf(stdout, "%s:", value[0]);
3852e1dd0a2fSth 	else
3853e1dd0a2fSth 		(void) fprintf(stdout, ":");
3854e1dd0a2fSth 
3855e1dd0a2fSth 	value = __ns_ldap_getAttr(res->entry, "memberUid");
3856e1dd0a2fSth 	if (value) {
3857e1dd0a2fSth 		int i;
3858e1dd0a2fSth 		for (i = 0; value[i] != NULL; i++)
3859e1dd0a2fSth 			if (value[i+1] != NULL)
3860e1dd0a2fSth 				(void) fprintf(stdout, "%s,", value[i]);
3861e1dd0a2fSth 			else
3862e1dd0a2fSth 				(void) fprintf(stdout, "%s:", value[i]);
3863e1dd0a2fSth 	} else {
3864e1dd0a2fSth 		(void) fprintf(stdout, ":");
3865e1dd0a2fSth 	}
3866e1dd0a2fSth 
3867e1dd0a2fSth 	value = __ns_ldap_getAttr(res->entry, "memberGid");
3868e1dd0a2fSth 	if (value) {
3869e1dd0a2fSth 		int i;
3870e1dd0a2fSth 		for (i = 0; value[i] != NULL; i++)
3871e1dd0a2fSth 			if (value[i+1] != NULL)
3872e1dd0a2fSth 				(void) fprintf(stdout, "%s,", value[i]);
3873e1dd0a2fSth 			else
3874e1dd0a2fSth 				(void) fprintf(stdout, "%s:", value[i]);
3875e1dd0a2fSth 	} else {
3876e1dd0a2fSth 		(void) fprintf(stdout, ":");
3877e1dd0a2fSth 	}
3878e1dd0a2fSth 
3879e1dd0a2fSth 	value = __ns_ldap_getAttr(res->entry, "SolarisProjectAttr");
3880e1dd0a2fSth 	if (value && value[0])
3881e1dd0a2fSth 		(void) fprintf(stdout, "%s\n", value[0]);
3882e1dd0a2fSth 	else
3883e1dd0a2fSth 		(void) fprintf(stdout, "\n");
3884e1dd0a2fSth 
3885e1dd0a2fSth }
38867c478bd9Sstevel@tonic-gate 
38877c478bd9Sstevel@tonic-gate static void
38887c478bd9Sstevel@tonic-gate dump_bootparams(ns_ldap_result_t *res)
38897c478bd9Sstevel@tonic-gate {
38907c478bd9Sstevel@tonic-gate 	char	**value = NULL;
38917c478bd9Sstevel@tonic-gate 	int		attr_count = 0;
38927c478bd9Sstevel@tonic-gate 
38937c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
38947c478bd9Sstevel@tonic-gate 	if (value[0] != NULL)
38957c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
38967c478bd9Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "bootParameter");
38977c478bd9Sstevel@tonic-gate 	if (value != NULL)
38987c478bd9Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
38997c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "\t%s", value[attr_count]);
39007c478bd9Sstevel@tonic-gate 			attr_count++;
39017c478bd9Sstevel@tonic-gate 		}
39027c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
39037c478bd9Sstevel@tonic-gate 
39047c478bd9Sstevel@tonic-gate 
39057c478bd9Sstevel@tonic-gate }
39067c478bd9Sstevel@tonic-gate 
39077c478bd9Sstevel@tonic-gate static char *
39087c478bd9Sstevel@tonic-gate fget_line_at(struct line_buf *line, int n, FILE *fp)
39097c478bd9Sstevel@tonic-gate {
39107c478bd9Sstevel@tonic-gate 	int c;
39117c478bd9Sstevel@tonic-gate 
39127c478bd9Sstevel@tonic-gate 	line->len = n;
39137c478bd9Sstevel@tonic-gate 
39147c478bd9Sstevel@tonic-gate 	for (;;) {
39157c478bd9Sstevel@tonic-gate 		c = fgetc(fp);
39167c478bd9Sstevel@tonic-gate 		if (c == -1)
39177c478bd9Sstevel@tonic-gate 			break;
39187c478bd9Sstevel@tonic-gate 		if (line->len >= line->alloc)
39197c478bd9Sstevel@tonic-gate 			line_buf_expand(line);
39207c478bd9Sstevel@tonic-gate 		line->str[line->len++] = c;
39217c478bd9Sstevel@tonic-gate 
39227c478bd9Sstevel@tonic-gate 		if (c == '\n')
39237c478bd9Sstevel@tonic-gate 			break;
39247c478bd9Sstevel@tonic-gate 	}
39257c478bd9Sstevel@tonic-gate 
39267c478bd9Sstevel@tonic-gate 	/* Null Terminate */
39277c478bd9Sstevel@tonic-gate 	if (line->len >= line->alloc)
39287c478bd9Sstevel@tonic-gate 		line_buf_expand(line);
39297c478bd9Sstevel@tonic-gate 	line->str[line->len++] = 0;
39307c478bd9Sstevel@tonic-gate 
39317c478bd9Sstevel@tonic-gate 	/* if no characters are read, return NULL to indicate EOF */
39327c478bd9Sstevel@tonic-gate 	if (line->str[0] == '\0')
39337c478bd9Sstevel@tonic-gate 		return (0);
39347c478bd9Sstevel@tonic-gate 
39357c478bd9Sstevel@tonic-gate 	return (line->str);
39367c478bd9Sstevel@tonic-gate }
39377c478bd9Sstevel@tonic-gate 
39387c478bd9Sstevel@tonic-gate /*
39397c478bd9Sstevel@tonic-gate  * return a line from the file, discarding comments and blank lines
39407c478bd9Sstevel@tonic-gate  */
39417c478bd9Sstevel@tonic-gate static int
39427c478bd9Sstevel@tonic-gate filedbmline_comment(struct line_buf *line, FILE *etcf, int *lineno,
39437c478bd9Sstevel@tonic-gate     struct file_loc *loc)
39447c478bd9Sstevel@tonic-gate {
39457c478bd9Sstevel@tonic-gate 	int i, len = 0;
39467c478bd9Sstevel@tonic-gate 
39477c478bd9Sstevel@tonic-gate 	loc->offset = ftell(etcf);
39487c478bd9Sstevel@tonic-gate 	for (;;) {
39497c478bd9Sstevel@tonic-gate 		if (fget_line_at(line, len, etcf) == 0)
39507c478bd9Sstevel@tonic-gate 			return (0);
39517c478bd9Sstevel@tonic-gate 
39527c478bd9Sstevel@tonic-gate 		if (lineno)
39537c478bd9Sstevel@tonic-gate 			(*lineno)++;
39547c478bd9Sstevel@tonic-gate 
39557c478bd9Sstevel@tonic-gate 		len = strlen(line->str);
39567c478bd9Sstevel@tonic-gate 		if (len >= 2 &&
39577c478bd9Sstevel@tonic-gate 		    line->str[0] != '#' &&
39587c478bd9Sstevel@tonic-gate 		    line->str[len-2] == '\\' && line->str[len-1] == '\n') {
39597c478bd9Sstevel@tonic-gate 			line->str[len-2] = 0;
39607c478bd9Sstevel@tonic-gate 			len -= 2;
39617c478bd9Sstevel@tonic-gate 			continue;    /* append next line at end */
39627c478bd9Sstevel@tonic-gate 		}
39637c478bd9Sstevel@tonic-gate 
39647c478bd9Sstevel@tonic-gate 		if (line->str[len-1] == '\n') {
39657c478bd9Sstevel@tonic-gate 			line->str[len-1] = 0;
39667c478bd9Sstevel@tonic-gate 			len -= 1;
39677c478bd9Sstevel@tonic-gate 		}
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate 		/*
39707c478bd9Sstevel@tonic-gate 		 * Skip lines where '#' is the first non-blank character.
39717c478bd9Sstevel@tonic-gate 		 */
39727c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
39737c478bd9Sstevel@tonic-gate 			if (line->str[i] == '#') {
39747c478bd9Sstevel@tonic-gate 				line->str[i] = '\0';
39757c478bd9Sstevel@tonic-gate 				len = i;
39767c478bd9Sstevel@tonic-gate 				break;
39777c478bd9Sstevel@tonic-gate 			}
39787c478bd9Sstevel@tonic-gate 			if (line->str[i] != ' ' && line->str[i] != '\t')
39797c478bd9Sstevel@tonic-gate 				break;
39807c478bd9Sstevel@tonic-gate 		}
39817c478bd9Sstevel@tonic-gate 
39827c478bd9Sstevel@tonic-gate 		/*
39837c478bd9Sstevel@tonic-gate 		 * A line with one or more white space characters followed
39847c478bd9Sstevel@tonic-gate 		 * by a comment will now be blank. The special case of a
39857c478bd9Sstevel@tonic-gate 		 * line with '#' in the first byte will have len == 0.
39867c478bd9Sstevel@tonic-gate 		 */
39877c478bd9Sstevel@tonic-gate 		if (len > 0 && !blankline(line->str))
39887c478bd9Sstevel@tonic-gate 			break;
39897c478bd9Sstevel@tonic-gate 
39907c478bd9Sstevel@tonic-gate 		len = 0;
39917c478bd9Sstevel@tonic-gate 		loc->offset = ftell(etcf);
39927c478bd9Sstevel@tonic-gate 	}
39937c478bd9Sstevel@tonic-gate 
39947c478bd9Sstevel@tonic-gate 	loc->size = len;
39957c478bd9Sstevel@tonic-gate 	return (1);
39967c478bd9Sstevel@tonic-gate }
39977c478bd9Sstevel@tonic-gate 
39987c478bd9Sstevel@tonic-gate /*
39997c478bd9Sstevel@tonic-gate  * return a line from the file, discarding comments, blanks, and '+' lines
40007c478bd9Sstevel@tonic-gate  */
40017c478bd9Sstevel@tonic-gate static int
40027c478bd9Sstevel@tonic-gate filedbmline_plus(struct line_buf *line, FILE *etcf, int *lineno,
40037c478bd9Sstevel@tonic-gate     struct file_loc *loc)
40047c478bd9Sstevel@tonic-gate {
40057c478bd9Sstevel@tonic-gate 	int len = 0;
40067c478bd9Sstevel@tonic-gate 
40077c478bd9Sstevel@tonic-gate 	loc->offset = ftell(etcf);
40087c478bd9Sstevel@tonic-gate 	for (;;) {
40097c478bd9Sstevel@tonic-gate 		if (fget_line_at(line, len, etcf) == 0)
40107c478bd9Sstevel@tonic-gate 			return (0);
40117c478bd9Sstevel@tonic-gate 
40127c478bd9Sstevel@tonic-gate 		if (lineno)
40137c478bd9Sstevel@tonic-gate 			(*lineno)++;
40147c478bd9Sstevel@tonic-gate 
40157c478bd9Sstevel@tonic-gate 		len = strlen(line->str);
40167c478bd9Sstevel@tonic-gate 		if (line->str[len-1] == '\n') {
40177c478bd9Sstevel@tonic-gate 			line->str[len-1] = 0;
40187c478bd9Sstevel@tonic-gate 			len -= 1;
40197c478bd9Sstevel@tonic-gate 		}
40207c478bd9Sstevel@tonic-gate 
40217c478bd9Sstevel@tonic-gate 		if (!blankline(line->str) &&
4022e1dd0a2fSth 		    line->str[0] != '+' && line->str[0] != '-' &&
4023e1dd0a2fSth 		    line->str[0] != '#')
40247c478bd9Sstevel@tonic-gate 			break;
40257c478bd9Sstevel@tonic-gate 
40267c478bd9Sstevel@tonic-gate 		len = 0;
40277c478bd9Sstevel@tonic-gate 		loc->offset = ftell(etcf);
40287c478bd9Sstevel@tonic-gate 	}
40297c478bd9Sstevel@tonic-gate 
40307c478bd9Sstevel@tonic-gate 	loc->size = len;
40317c478bd9Sstevel@tonic-gate 	return (1);
40327c478bd9Sstevel@tonic-gate }
40337c478bd9Sstevel@tonic-gate 
40347c478bd9Sstevel@tonic-gate 
40357c478bd9Sstevel@tonic-gate /* Populating the ttypelist structure */
40367c478bd9Sstevel@tonic-gate 
40377c478bd9Sstevel@tonic-gate static struct ttypelist_t ttypelist[] = {
40387c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_HOSTS, genent_hosts, dump_hosts,
40399f2fd570SJulian Pullen 		filedbmline_comment, "iphost", "cn" },
40407c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_IPNODES, genent_hosts, dump_hosts,
40419f2fd570SJulian Pullen 		filedbmline_comment, "iphost", "cn" },
40427c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_RPC, genent_rpc, dump_rpc,
40439f2fd570SJulian Pullen 		filedbmline_comment, "oncrpc", "cn" },
40447c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PROTOCOLS, genent_protocols, dump_protocols,
40459f2fd570SJulian Pullen 		filedbmline_comment, "ipprotocol", "cn" },
40467c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_NETWORKS, genent_networks, dump_networks,
40479f2fd570SJulian Pullen 		filedbmline_comment, "ipnetwork", "ipnetworknumber" },
40487c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_SERVICES, genent_services, dump_services,
40499f2fd570SJulian Pullen 		filedbmline_comment, "ipservice", "cn" },
40507c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_GROUP, genent_group, dump_group,
40519f2fd570SJulian Pullen 		filedbmline_plus, "posixgroup", "gidnumber" },
40527c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_NETMASKS, genent_netmasks, dump_netmasks,
40539f2fd570SJulian Pullen 		filedbmline_comment, "ipnetwork", "ipnetworknumber"},
40547c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_ETHERS, genent_ethers, dump_ethers,
40559f2fd570SJulian Pullen 		filedbmline_comment, "ieee802Device", "cn" },
40567c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_NETGROUP, genent_netgroup, dump_netgroup,
40579f2fd570SJulian Pullen 		filedbmline_comment, "nisnetgroup", "cn" },
40587c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_BOOTPARAMS, genent_bootparams, dump_bootparams,
40599f2fd570SJulian Pullen 		filedbmline_comment, "bootableDevice", "cn" },
40607c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PUBLICKEY, genent_publickey, NULL /* dump_publickey */,
40619f2fd570SJulian Pullen 		filedbmline_comment, "niskeyobject", "cn" },
40627c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PASSWD, genent_passwd, dump_passwd,
40639f2fd570SJulian Pullen 		filedbmline_plus, "posixaccount", "uid" },
40647c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_SHADOW, genent_shadow, dump_shadow,
40659f2fd570SJulian Pullen 		filedbmline_plus, "shadowaccount", "uid" },
40667c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_ALIASES, genent_aliases, dump_aliases,
40679f2fd570SJulian Pullen 		filedbmline_plus, "mailGroup", "cn" },
40687c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_AUTOMOUNT, genent_automount, dump_automount,
40699f2fd570SJulian Pullen 		filedbmline_comment, "automount", "automountKey" },
40707c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_USERATTR, genent_user_attr, dump_user_attr,
40719f2fd570SJulian Pullen 		filedbmline_comment, "SolarisUserAttr", "uid" },
40727c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PROFILE, genent_prof_attr, dump_prof_attr,
40739f2fd570SJulian Pullen 		filedbmline_comment, "SolarisProfAttr", "cn" },
40747c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_EXECATTR, genent_exec_attr, dump_exec_attr,
40759f2fd570SJulian Pullen 		filedbmline_comment, "SolarisExecAttr", "cn" },
40767c478bd9Sstevel@tonic-gate 	{ NS_LDAP_TYPE_AUTHATTR, genent_auth_attr, dump_auth_attr,
40779f2fd570SJulian Pullen 		filedbmline_comment, "SolarisAuthAttr", "cn" },
407845916cd2Sjpk 	{ NS_LDAP_TYPE_TNRHDB, genent_tnrhdb, dump_tnrhdb,
40799f2fd570SJulian Pullen 		filedbmline_comment, "ipTnetHost", "ipTnetNumber" },
408045916cd2Sjpk 	{ NS_LDAP_TYPE_TNRHTP, genent_tnrhtp, dump_tnrhtp,
40819f2fd570SJulian Pullen 		filedbmline_comment, "ipTnetTemplate", "ipTnetTemplateName" },
4082e1dd0a2fSth 	{ NS_LDAP_TYPE_PROJECT, genent_project, dump_project,
40839f2fd570SJulian Pullen 		filedbmline_comment, "SolarisProject", "SolarisProjectName" },
40849f2fd570SJulian Pullen 	{ 0, 0, 0, 0, 0, 0 }
40857c478bd9Sstevel@tonic-gate };
40867c478bd9Sstevel@tonic-gate 
40877c478bd9Sstevel@tonic-gate 
40887c478bd9Sstevel@tonic-gate 
40897c478bd9Sstevel@tonic-gate 
40907c478bd9Sstevel@tonic-gate static int lineno = 0;
40917c478bd9Sstevel@tonic-gate 
40927c478bd9Sstevel@tonic-gate static	void
40937c478bd9Sstevel@tonic-gate addfile()
40947c478bd9Sstevel@tonic-gate {
40957c478bd9Sstevel@tonic-gate 	struct line_buf line;
40967c478bd9Sstevel@tonic-gate 	struct file_loc loc;
40977c478bd9Sstevel@tonic-gate 
40987c478bd9Sstevel@tonic-gate 	/* Initializing the Line Buffer */
40997c478bd9Sstevel@tonic-gate 	line_buf_init(&line);
41007c478bd9Sstevel@tonic-gate 
41017c478bd9Sstevel@tonic-gate 	/* Loop through all the lines in the file */
41027c478bd9Sstevel@tonic-gate 	while (tt->filedbmline(&line, etcf, &lineno, &loc)) {
41037c478bd9Sstevel@tonic-gate 		switch ((*(tt->genent))(line.str, addentry)) {
41047c478bd9Sstevel@tonic-gate 		case GENENT_OK:
41057c478bd9Sstevel@tonic-gate 			break;
41067c478bd9Sstevel@tonic-gate 		case GENENT_PARSEERR:
41077c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
41087c478bd9Sstevel@tonic-gate 			    gettext("parse error: %s (line %d)\n"),
41097c478bd9Sstevel@tonic-gate 			    parse_err_msg, lineno);
41107c478bd9Sstevel@tonic-gate 			exit_val = 1;
41117c478bd9Sstevel@tonic-gate 			break;
41127c478bd9Sstevel@tonic-gate 		case GENENT_CBERR:
41137c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
41147c478bd9Sstevel@tonic-gate 			    gettext("Error while adding line: %s\n"),
41157c478bd9Sstevel@tonic-gate 			    line.str);
41167c478bd9Sstevel@tonic-gate 			exit_val = 2;
41177c478bd9Sstevel@tonic-gate 			free(line.str);
41187c478bd9Sstevel@tonic-gate 			return;
41197c478bd9Sstevel@tonic-gate 		case GENENT_ERR:
41207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
41217c478bd9Sstevel@tonic-gate 			    gettext("Internal Error while adding line: %s\n"),
41227c478bd9Sstevel@tonic-gate 			    line.str);
41237c478bd9Sstevel@tonic-gate 			exit_val = 3;
41247c478bd9Sstevel@tonic-gate 			free(line.str);
41257c478bd9Sstevel@tonic-gate 			return;
41267c478bd9Sstevel@tonic-gate 		}
41277c478bd9Sstevel@tonic-gate 	}
41287c478bd9Sstevel@tonic-gate 	free(line.str);
41297c478bd9Sstevel@tonic-gate }
41307c478bd9Sstevel@tonic-gate 
41317c478bd9Sstevel@tonic-gate static void
41327c478bd9Sstevel@tonic-gate dumptable(char *service)
41337c478bd9Sstevel@tonic-gate {
41347c478bd9Sstevel@tonic-gate 
41357c478bd9Sstevel@tonic-gate 	ns_ldap_result_t *eres = NULL;
41367c478bd9Sstevel@tonic-gate 	ns_ldap_error_t *err = NULL;
41377c478bd9Sstevel@tonic-gate 	int	rc = 0, success = 0;
41387c478bd9Sstevel@tonic-gate 	char	filter[BUFSIZ];
41397c478bd9Sstevel@tonic-gate 	int	done = 0;
41407c478bd9Sstevel@tonic-gate 	void	*cookie = NULL;
41417c478bd9Sstevel@tonic-gate 
41427c478bd9Sstevel@tonic-gate 	/* set the appropriate filter */
41437c478bd9Sstevel@tonic-gate 	if (strcmp(tt->ttype, NS_LDAP_TYPE_PROFILE) == 0) {
41447c478bd9Sstevel@tonic-gate 		/*
41457c478bd9Sstevel@tonic-gate 		 * prof_attr entries are SolarisProfAttr
41467c478bd9Sstevel@tonic-gate 		 * without AUXILIARY SolarisExecAttr
41477c478bd9Sstevel@tonic-gate 		 */
41487c478bd9Sstevel@tonic-gate 		(void) snprintf(filter, sizeof (filter),
41497c478bd9Sstevel@tonic-gate 		    "(&(objectclass=%s)(!(objectclass=SolarisExecAttr)))",
41507c478bd9Sstevel@tonic-gate 		    tt->objclass);
415145916cd2Sjpk 	} else if (strcmp(tt->ttype, NS_LDAP_TYPE_TNRHDB) == 0) {
415245916cd2Sjpk 		/*
415345916cd2Sjpk 		 * tnrhtp entries are ipTnet entries with SolarisAttrKeyValue
415445916cd2Sjpk 		 */
415545916cd2Sjpk 		(void) snprintf(filter, sizeof (filter),
415645916cd2Sjpk 		    "(&(objectclass=%s)(SolarisAttrKeyValue=*)))",
415745916cd2Sjpk 		    tt->objclass);
415845916cd2Sjpk 	} else {
41597c478bd9Sstevel@tonic-gate 		(void) snprintf(filter, sizeof (filter),
41607c478bd9Sstevel@tonic-gate 		    "(objectclass=%s)", tt->objclass);
416145916cd2Sjpk 	}
41627c478bd9Sstevel@tonic-gate 
41637c478bd9Sstevel@tonic-gate 	if (flags & F_VERBOSE)
41647c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, gettext("FILTER = %s\n"), filter);
41657c478bd9Sstevel@tonic-gate 
41667c478bd9Sstevel@tonic-gate 	/* Pass cred only if supplied. Cred is not always needed for dump */
41677c478bd9Sstevel@tonic-gate 	if (authority.cred.unix_cred.userID == NULL ||
41687c478bd9Sstevel@tonic-gate 	    authority.cred.unix_cred.passwd == NULL)
41699f2fd570SJulian Pullen 		rc = __ns_ldap_firstEntry(service, filter, tt->sortattr, NULL,
41709f2fd570SJulian Pullen 		    NULL, NULL, NS_LDAP_HARD, &cookie, &eres, &err, NULL);
41717c478bd9Sstevel@tonic-gate 	else
41729f2fd570SJulian Pullen 		rc = __ns_ldap_firstEntry(service, filter, tt->sortattr, NULL,
41739f2fd570SJulian Pullen 		    NULL, &authority, NS_LDAP_HARD, &cookie, &eres, &err, NULL);
41747c478bd9Sstevel@tonic-gate 
41757c478bd9Sstevel@tonic-gate 	switch (rc) {
41767c478bd9Sstevel@tonic-gate 	case NS_LDAP_SUCCESS:
41777c478bd9Sstevel@tonic-gate 		nent_add++;
41787c478bd9Sstevel@tonic-gate 		success = 1;
41797c478bd9Sstevel@tonic-gate 		if (eres != NULL) {
41807c478bd9Sstevel@tonic-gate 			if (strcmp(databasetype, "publickey") == 0)
41817c478bd9Sstevel@tonic-gate 				dump_publickey(eres, service);
41827c478bd9Sstevel@tonic-gate 			else
41837c478bd9Sstevel@tonic-gate 				(*(tt->dump))(eres);
41847c478bd9Sstevel@tonic-gate 		}
41857c478bd9Sstevel@tonic-gate 		else
41867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("No entries found.\n"));
41877c478bd9Sstevel@tonic-gate 		break;
41887c478bd9Sstevel@tonic-gate 
41897c478bd9Sstevel@tonic-gate 	case NS_LDAP_OP_FAILED:
41907c478bd9Sstevel@tonic-gate 		exit_val = 2;
41917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("operation failed.\n"));
41927c478bd9Sstevel@tonic-gate 		break;
41937c478bd9Sstevel@tonic-gate 
41947c478bd9Sstevel@tonic-gate 	case NS_LDAP_INVALID_PARAM:
41957c478bd9Sstevel@tonic-gate 		exit_val = 2;
41967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
41977c478bd9Sstevel@tonic-gate 		    gettext("invalid parameter(s) passed.\n"));
41987c478bd9Sstevel@tonic-gate 		break;
41997c478bd9Sstevel@tonic-gate 
42007c478bd9Sstevel@tonic-gate 	case NS_LDAP_NOTFOUND:
42017c478bd9Sstevel@tonic-gate 		exit_val = 2;
42027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("entry not found.\n"));
42037c478bd9Sstevel@tonic-gate 		break;
42047c478bd9Sstevel@tonic-gate 
42057c478bd9Sstevel@tonic-gate 	case NS_LDAP_MEMORY:
42067c478bd9Sstevel@tonic-gate 		exit_val = 2;
42077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4208e1dd0a2fSth 		    gettext("internal memory allocation error.\n"));
42097c478bd9Sstevel@tonic-gate 		break;
42107c478bd9Sstevel@tonic-gate 
42117c478bd9Sstevel@tonic-gate 	case NS_LDAP_CONFIG:
42127c478bd9Sstevel@tonic-gate 		exit_val = 2;
42137c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4214e1dd0a2fSth 		    gettext("LDAP Configuration problem.\n"));
42157c478bd9Sstevel@tonic-gate 		perr(err);
42167c478bd9Sstevel@tonic-gate 		break;
42177c478bd9Sstevel@tonic-gate 
42187c478bd9Sstevel@tonic-gate 	case NS_LDAP_PARTIAL:
42197c478bd9Sstevel@tonic-gate 		exit_val = 2;
42207c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4221e1dd0a2fSth 		    gettext("partial result returned\n"));
42227c478bd9Sstevel@tonic-gate 		perr(err);
42237c478bd9Sstevel@tonic-gate 		break;
42247c478bd9Sstevel@tonic-gate 
42257c478bd9Sstevel@tonic-gate 	case NS_LDAP_INTERNAL:
42267c478bd9Sstevel@tonic-gate 		exit_val = 2;
42277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4228e1dd0a2fSth 		    gettext("internal LDAP error occured.\n"));
42297c478bd9Sstevel@tonic-gate 		perr(err);
42307c478bd9Sstevel@tonic-gate 		break;
42317c478bd9Sstevel@tonic-gate 	}
42327c478bd9Sstevel@tonic-gate 
42337c478bd9Sstevel@tonic-gate 	if (eres != NULL) {
42347c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeResult(&eres);
42357c478bd9Sstevel@tonic-gate 		eres = NULL;
42367c478bd9Sstevel@tonic-gate 	}
42377c478bd9Sstevel@tonic-gate 
42387c478bd9Sstevel@tonic-gate 	if (success) {
42397c478bd9Sstevel@tonic-gate 		while (!done) {
42407c478bd9Sstevel@tonic-gate 			rc = __ns_ldap_nextEntry(cookie, &eres, &err);
42417c478bd9Sstevel@tonic-gate 			if (rc != NS_LDAP_SUCCESS || eres  == NULL) {
42427c478bd9Sstevel@tonic-gate 				done = 1;
42437c478bd9Sstevel@tonic-gate 				continue;
42447c478bd9Sstevel@tonic-gate 			}
42457c478bd9Sstevel@tonic-gate 
42467c478bd9Sstevel@tonic-gate 			/* Print the result */
42477c478bd9Sstevel@tonic-gate 			if (eres != NULL) {
42487c478bd9Sstevel@tonic-gate 				if (strcmp(databasetype, "publickey") == 0)
42497c478bd9Sstevel@tonic-gate 					dump_publickey(eres, service);
42507c478bd9Sstevel@tonic-gate 				else
42517c478bd9Sstevel@tonic-gate 					(*(tt->dump))(eres);
42527c478bd9Sstevel@tonic-gate 				(void) __ns_ldap_freeResult(&eres);
42537c478bd9Sstevel@tonic-gate 				eres = NULL;
42547c478bd9Sstevel@tonic-gate 			}
42557c478bd9Sstevel@tonic-gate 		}
42567c478bd9Sstevel@tonic-gate 	}
42577c478bd9Sstevel@tonic-gate }
42587c478bd9Sstevel@tonic-gate 
4259a506a34cSth int
42607c478bd9Sstevel@tonic-gate main(int argc, char **argv)
42617c478bd9Sstevel@tonic-gate {
4262e1dd0a2fSth 	char			*password;
4263e1dd0a2fSth 	ns_standalone_conf_t	standalone_cfg = standaloneDefaults;
4264e1dd0a2fSth 	int			c;
4265e1dd0a2fSth 	int			rc;
4266e1dd0a2fSth 	int			ldaprc;
4267e1dd0a2fSth 	int			authstried = 0;
4268e1dd0a2fSth 	int			op = OP_ADD;
4269e1dd0a2fSth 	char			*ttype, *authmech = 0, *etcfile = 0;
4270e1dd0a2fSth 	/* Temporary password variable */
4271e1dd0a2fSth 	char			ps[LDAP_MAXNAMELEN];
4272e1dd0a2fSth 	char			filter[BUFSIZ];
4273e1dd0a2fSth 	void			**paramVal = NULL;
4274e1dd0a2fSth 	ns_auth_t		**app;
4275e1dd0a2fSth 	ns_auth_t		**authpp = NULL;
4276e1dd0a2fSth 	ns_auth_t		*authp = NULL;
4277e1dd0a2fSth 	ns_ldap_error_t		*errorp = NULL;
4278e1dd0a2fSth 	ns_ldap_result_t	*resultp;
4279e1dd0a2fSth 	ns_ldap_entry_t		*e;
4280e1dd0a2fSth 	int			flag = 0;
4281e1dd0a2fSth 	int			version1 = 0;
42827c478bd9Sstevel@tonic-gate 
42837c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
42847c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
42857c478bd9Sstevel@tonic-gate 
42867c478bd9Sstevel@tonic-gate 	openlog("ldapaddent", LOG_PID, LOG_USER);
42877c478bd9Sstevel@tonic-gate 
42887c478bd9Sstevel@tonic-gate 	inputbasedn = NULL;
42897c478bd9Sstevel@tonic-gate 	authority.cred.unix_cred.passwd = NULL;
42907c478bd9Sstevel@tonic-gate 	authority.cred.unix_cred.userID = NULL;
42917c478bd9Sstevel@tonic-gate 	authority.auth.type = NS_LDAP_AUTH_SIMPLE;
42927c478bd9Sstevel@tonic-gate 
4293e1dd0a2fSth 	while ((c = getopt(argc, argv, "cdh:N:M:vpf:D:w:j:b:a:P:r:")) != EOF) {
42947c478bd9Sstevel@tonic-gate 		switch (c) {
42957c478bd9Sstevel@tonic-gate 		case 'd':
42967c478bd9Sstevel@tonic-gate 			if (op)
4297e1dd0a2fSth 				usage(gettext(
4298e1dd0a2fSth 				    "no other option should be specified"));
42997c478bd9Sstevel@tonic-gate 			op = OP_DUMP;
43007c478bd9Sstevel@tonic-gate 			break;
43017c478bd9Sstevel@tonic-gate 		case 'c':
43027c478bd9Sstevel@tonic-gate 			continue_onerror = 1;
43037c478bd9Sstevel@tonic-gate 			break;
43047c478bd9Sstevel@tonic-gate 		case 'v':
43057c478bd9Sstevel@tonic-gate 			flags |= F_VERBOSE;
43067c478bd9Sstevel@tonic-gate 			break;
43077c478bd9Sstevel@tonic-gate 		case 'p':
43087c478bd9Sstevel@tonic-gate 			flags |= F_PASSWD;
43097c478bd9Sstevel@tonic-gate 			break;
4310e1dd0a2fSth 		case 'M':
4311e1dd0a2fSth 			standalone_cfg.type = NS_LDAP_SERVER;
4312e1dd0a2fSth 			standalone_cfg.SA_DOMAIN = optarg;
4313e1dd0a2fSth 			break;
4314e1dd0a2fSth 		case 'h':
4315e1dd0a2fSth 			standalone_cfg.type = NS_LDAP_SERVER;
4316e1dd0a2fSth 			if (separatePort(optarg,
4317e1dd0a2fSth 			    &standalone_cfg.SA_SERVER,
4318e1dd0a2fSth 			    &standalone_cfg.SA_PORT) > 0) {
4319e1dd0a2fSth 				exit(1);
4320e1dd0a2fSth 			}
4321e1dd0a2fSth 			break;
4322e1dd0a2fSth 		case 'P':
4323e1dd0a2fSth 			standalone_cfg.type = NS_LDAP_SERVER;
4324e1dd0a2fSth 			authority.hostcertpath = optarg;
4325e1dd0a2fSth 			break;
4326e1dd0a2fSth 		case 'N':
4327e1dd0a2fSth 			standalone_cfg.type = NS_LDAP_SERVER;
4328e1dd0a2fSth 			standalone_cfg.SA_PROFILE_NAME = optarg;
4329e1dd0a2fSth 			break;
43307c478bd9Sstevel@tonic-gate 		case 'f':
43317c478bd9Sstevel@tonic-gate 			etcfile = optarg;
43327c478bd9Sstevel@tonic-gate 			break;
43337c478bd9Sstevel@tonic-gate 		case 'D':
43347c478bd9Sstevel@tonic-gate 			authority.cred.unix_cred.userID = strdup(optarg);
43357c478bd9Sstevel@tonic-gate 			break;
43367c478bd9Sstevel@tonic-gate 		case 'w':
4337e1dd0a2fSth 			if (authority.cred.unix_cred.passwd) {
4338e1dd0a2fSth 				(void) fprintf(stderr,
4339e1dd0a2fSth 				    gettext("Warning: The -w option is mutually"
4340e1dd0a2fSth 				    " exclusive of -j. -w is ignored.\n"));
4341e1dd0a2fSth 				break;
4342e1dd0a2fSth 			}
4343e1dd0a2fSth 
4344e1dd0a2fSth 			if (optarg != NULL &&
4345e1dd0a2fSth 			    optarg[0] == '-' && optarg[1] == '\0') {
4346e1dd0a2fSth 				/* Ask for a password later */
4347e1dd0a2fSth 				break;
4348e1dd0a2fSth 			}
4349e1dd0a2fSth 
43507c478bd9Sstevel@tonic-gate 			authority.cred.unix_cred.passwd = strdup(optarg);
43517c478bd9Sstevel@tonic-gate 			break;
4352e1dd0a2fSth 		case 'j':
4353e1dd0a2fSth 			if (authority.cred.unix_cred.passwd != NULL) {
4354e1dd0a2fSth 				(void) fprintf(stderr,
4355e1dd0a2fSth 				    gettext("The -w option is mutually "
4356e1dd0a2fSth 				    "exclusive of -j. -w is ignored.\n"));
4357e1dd0a2fSth 				free(authority.cred.unix_cred.passwd);
4358e1dd0a2fSth 			}
4359e1dd0a2fSth 			authority.cred.unix_cred.passwd = readPwd(optarg);
4360e1dd0a2fSth 			if (authority.cred.unix_cred.passwd == NULL) {
4361e1dd0a2fSth 				exit(1);
4362e1dd0a2fSth 			}
4363e1dd0a2fSth 			break;
43647c478bd9Sstevel@tonic-gate 		case 'b':
43657c478bd9Sstevel@tonic-gate 			inputbasedn = strdup(optarg);
43667c478bd9Sstevel@tonic-gate 			break;
43677c478bd9Sstevel@tonic-gate 		case 'a':
43687c478bd9Sstevel@tonic-gate 			authmech = strdup(optarg);
43697c478bd9Sstevel@tonic-gate 			break;
43707c478bd9Sstevel@tonic-gate 		default:
43717c478bd9Sstevel@tonic-gate 			usage(gettext("Invalid option"));
43727c478bd9Sstevel@tonic-gate 		}
43737c478bd9Sstevel@tonic-gate 	}
43747c478bd9Sstevel@tonic-gate 
4375e1dd0a2fSth 	if (standalone_cfg.type == NS_LDAP_SERVER &&
4376e1dd0a2fSth 	    standalone_cfg.SA_SERVER == NULL) {
43777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4378e1dd0a2fSth 		    gettext("Please specify an LDAP server you want "
4379e1dd0a2fSth 		    "to connect to. \n"));
43807c478bd9Sstevel@tonic-gate 		exit(1);
43817c478bd9Sstevel@tonic-gate 	}
43827c478bd9Sstevel@tonic-gate 
43837c478bd9Sstevel@tonic-gate 	if (authmech != NULL) {
4384e1dd0a2fSth 		if (__ns_ldap_initAuth(authmech, &authority.auth, &errorp) !=
4385e1dd0a2fSth 		    NS_LDAP_SUCCESS) {
4386e1dd0a2fSth 			if (errorp) {
4387e1dd0a2fSth 				(void) fprintf(stderr, "%s", errorp->message);
4388e1dd0a2fSth 				(void) __ns_ldap_freeError(&errorp);
4389e1dd0a2fSth 			}
43907c478bd9Sstevel@tonic-gate 			exit(1);
43917c478bd9Sstevel@tonic-gate 		}
43927c478bd9Sstevel@tonic-gate 	}
43937c478bd9Sstevel@tonic-gate 
4394e1dd0a2fSth 	if (authority.auth.saslmech != NS_LDAP_SASL_GSSAPI &&
4395e1dd0a2fSth 	    authority.cred.unix_cred.userID == NULL &&
4396e1dd0a2fSth 	    op != OP_DUMP) {
4397cb5caa98Sdjl 	    /* This is not an optional parameter. Exit */
4398cb5caa98Sdjl 		(void) fprintf(stderr,
4399e1dd0a2fSth 		    gettext("DN must be specified unless SASL/GSSAPI is used."
4400e1dd0a2fSth 		    " Use option -D.\n"));
4401cb5caa98Sdjl 		exit(1);
4402cb5caa98Sdjl 	}
4403cb5caa98Sdjl 
4404e1dd0a2fSth 	if (authority.auth.saslmech != NS_LDAP_SASL_GSSAPI &&
4405e1dd0a2fSth 	    authority.cred.unix_cred.passwd == NULL &&
4406e1dd0a2fSth 	    (op != OP_DUMP ||
4407e1dd0a2fSth 	    standalone_cfg.type != NS_CACHEMGR &&
4408e1dd0a2fSth 	    authority.cred.unix_cred.userID != NULL)) {
4409cb5caa98Sdjl 		/* If password is not specified, then prompt user for it. */
4410cb5caa98Sdjl 		password = getpassphrase("Enter password:");
4411cb5caa98Sdjl 		(void) strcpy(ps, password);
4412cb5caa98Sdjl 		authority.cred.unix_cred.passwd = strdup(ps);
4413cb5caa98Sdjl 	}
4414cb5caa98Sdjl 
4415e1dd0a2fSth 	standalone_cfg.SA_AUTH = authmech == NULL ? NULL : &authority.auth;
4416e1dd0a2fSth 	standalone_cfg.SA_CERT_PATH = authority.hostcertpath;
4417e1dd0a2fSth 	standalone_cfg.SA_BIND_DN = authority.cred.unix_cred.userID;
4418e1dd0a2fSth 	standalone_cfg.SA_BIND_PWD = authority.cred.unix_cred.passwd;
4419e1dd0a2fSth 
4420e1dd0a2fSth 	if (__ns_ldap_initStandalone(&standalone_cfg,
4421e1dd0a2fSth 	    &errorp) != NS_LDAP_SUCCESS) {
4422e1dd0a2fSth 		if (errorp) {
4423e1dd0a2fSth 			(void) fprintf(stderr, "%s", errorp->message);
4424e1dd0a2fSth 		}
4425e1dd0a2fSth 		exit(1);
4426e1dd0a2fSth 	}
4427e1dd0a2fSth 
44287c478bd9Sstevel@tonic-gate 	if (authmech == NULL) {
44297c478bd9Sstevel@tonic-gate 		ldaprc = __ns_ldap_getParam(NS_LDAP_AUTH_P, (void ***)&authpp,
4430e1dd0a2fSth 		    &errorp);
44317c478bd9Sstevel@tonic-gate 		if (ldaprc != NS_LDAP_SUCCESS ||
44327c478bd9Sstevel@tonic-gate 		    (authpp == NULL && op != OP_DUMP)) {
44337c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
44347c478bd9Sstevel@tonic-gate 			    gettext("No legal authentication method "
44357c478bd9Sstevel@tonic-gate 			    "configured.\n"));
44367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
44377c478bd9Sstevel@tonic-gate 			    gettext("Provide a legal authentication method "
44387c478bd9Sstevel@tonic-gate 			    "using -a option\n"));
44397c478bd9Sstevel@tonic-gate 			exit(1);
44407c478bd9Sstevel@tonic-gate 		}
44417c478bd9Sstevel@tonic-gate 
44427c478bd9Sstevel@tonic-gate 		/* Use the first authentication method which is not none */
44437c478bd9Sstevel@tonic-gate 		for (app = authpp; *app; app++) {
44447c478bd9Sstevel@tonic-gate 			authp = *app;
44457c478bd9Sstevel@tonic-gate 			if (authp->type != NS_LDAP_AUTH_NONE) {
44467c478bd9Sstevel@tonic-gate 				authstried++;
44477c478bd9Sstevel@tonic-gate 				authority.auth.type = authp->type;
44487c478bd9Sstevel@tonic-gate 				authority.auth.tlstype = authp->tlstype;
44497c478bd9Sstevel@tonic-gate 				authority.auth.saslmech = authp->saslmech;
44507c478bd9Sstevel@tonic-gate 				authority.auth.saslopt = authp->saslopt;
44517c478bd9Sstevel@tonic-gate 				break;
44527c478bd9Sstevel@tonic-gate 			}
44537c478bd9Sstevel@tonic-gate 		}
44547c478bd9Sstevel@tonic-gate 		if (authstried == 0 && op != OP_DUMP) {
44557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4456e1dd0a2fSth 			    gettext("No legal authentication method configured."
4457e1dd0a2fSth 			    "\nProvide a legal authentication method using "
4458e1dd0a2fSth 			    "-a option"));
44597c478bd9Sstevel@tonic-gate 			exit(1);
44607c478bd9Sstevel@tonic-gate 		}
4461cb5caa98Sdjl 		if (authority.auth.saslmech == NS_LDAP_SASL_GSSAPI &&
4462e1dd0a2fSth 		    authority.cred.unix_cred.passwd != NULL &&
4463e1dd0a2fSth 		    authority.cred.unix_cred.userID != NULL) {
4464cb5caa98Sdjl 			/*
4465cb5caa98Sdjl 			 * -a is not specified and the auth method sasl/GSSAPI
4466cb5caa98Sdjl 			 * is defined in the configuration of the ldap profile.
4467cb5caa98Sdjl 			 * Even -D and -w is provided it's not valid usage.
4468e1dd0a2fSth 			 * Drop them on the floor.
4469cb5caa98Sdjl 			 */
4470cb5caa98Sdjl 
4471cb5caa98Sdjl 			(void) fprintf(stderr,
4472e1dd0a2fSth 			    gettext("The default authentication is "
4473e1dd0a2fSth 			    "sasl/GSSAPI.\n"
4474e1dd0a2fSth 			    "The bind DN and password will be ignored.\n"));
4475e1dd0a2fSth 			authority.cred.unix_cred.passwd = NULL;
4476e1dd0a2fSth 			authority.cred.unix_cred.userID = NULL;
4477cb5caa98Sdjl 		}
44787c478bd9Sstevel@tonic-gate 	}
44797c478bd9Sstevel@tonic-gate 
44807c478bd9Sstevel@tonic-gate 	ttype = argv[optind++];
44817c478bd9Sstevel@tonic-gate 
44827c478bd9Sstevel@tonic-gate 	if (ttype == NULL) {
44837c478bd9Sstevel@tonic-gate 		usage(gettext("No database type specified"));
44847c478bd9Sstevel@tonic-gate 		exit(1);
44857c478bd9Sstevel@tonic-gate 	}
44867c478bd9Sstevel@tonic-gate 
44877c478bd9Sstevel@tonic-gate 	if (strncasecmp(ttype, "automount", 9) == 0) {
44887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
44897c478bd9Sstevel@tonic-gate 		    gettext("automount is not a valid service for ldapaddent.\n"
44907c478bd9Sstevel@tonic-gate 		    "Please use auto_*.\n"
44917c478bd9Sstevel@tonic-gate 		    "e.g.  auto_home, auto_ws etc.\n "));
44927c478bd9Sstevel@tonic-gate 		exit(1);
44937c478bd9Sstevel@tonic-gate 	}
44947c478bd9Sstevel@tonic-gate 
44957c478bd9Sstevel@tonic-gate 	for (tt = ttypelist; tt->ttype; tt++) {
44967c478bd9Sstevel@tonic-gate 		if (strcmp(tt->ttype, ttype) == 0)
44977c478bd9Sstevel@tonic-gate 			break;
44987c478bd9Sstevel@tonic-gate 		if (strcmp(tt->ttype, NS_LDAP_TYPE_AUTOMOUNT) == 0 &&
44997c478bd9Sstevel@tonic-gate 		    strncmp(ttype, NS_LDAP_TYPE_AUTOMOUNT,
45007c478bd9Sstevel@tonic-gate 		    sizeof (NS_LDAP_TYPE_AUTOMOUNT) - 1) == 0)
45017c478bd9Sstevel@tonic-gate 			break;
45027c478bd9Sstevel@tonic-gate 	}
45037c478bd9Sstevel@tonic-gate 
45047c478bd9Sstevel@tonic-gate 	if (tt->ttype == 0) {
45057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
45067c478bd9Sstevel@tonic-gate 		    gettext("database %s not supported;"
45077c478bd9Sstevel@tonic-gate 		    " supported databases are:\n"), ttype);
45087c478bd9Sstevel@tonic-gate 		for (tt = ttypelist; tt->ttype; tt++)
45097c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("\t%s\n"), tt->ttype);
45107c478bd9Sstevel@tonic-gate 		exit(1);
45117c478bd9Sstevel@tonic-gate 	}
45127c478bd9Sstevel@tonic-gate 
45137c478bd9Sstevel@tonic-gate 	if (flags & F_VERBOSE)
45147c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, gettext("SERVICE = %s\n"), tt->ttype);
45157c478bd9Sstevel@tonic-gate 
45167c478bd9Sstevel@tonic-gate 	databasetype = ttype;
45177c478bd9Sstevel@tonic-gate 
45187c478bd9Sstevel@tonic-gate 	if (strcmp(tt->ttype, NS_LDAP_TYPE_AUTOMOUNT) == 0) {
45197c478bd9Sstevel@tonic-gate 		paramVal = NULL;
45207c478bd9Sstevel@tonic-gate 		errorp = NULL;
45217c478bd9Sstevel@tonic-gate 		rc = __ns_ldap_getParam(NS_LDAP_FILE_VERSION_P, &paramVal,
4522e1dd0a2fSth 		    &errorp);
45237c478bd9Sstevel@tonic-gate 		if (paramVal && *paramVal &&
4524e1dd0a2fSth 		    strcasecmp(*paramVal, NS_LDAP_VERSION_1) == 0)
45257c478bd9Sstevel@tonic-gate 			version1 = 1;
45267c478bd9Sstevel@tonic-gate 		if (paramVal)
45277c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeParam(&paramVal);
45287c478bd9Sstevel@tonic-gate 		if (errorp)
45297c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&errorp);
45307c478bd9Sstevel@tonic-gate 	}
45317c478bd9Sstevel@tonic-gate 
45327c478bd9Sstevel@tonic-gate 	/* Check if the container exists in first place */
45337c478bd9Sstevel@tonic-gate 	(void) strcpy(&filter[0], "(objectclass=*)");
45347c478bd9Sstevel@tonic-gate 
45357c478bd9Sstevel@tonic-gate 	rc = __ns_ldap_list(databasetype, filter, NULL, (const char **)NULL,
45367c478bd9Sstevel@tonic-gate 	    NULL, NS_LDAP_SCOPE_BASE, &resultp, &errorp, NULL, NULL);
45377c478bd9Sstevel@tonic-gate 
45387c478bd9Sstevel@tonic-gate 	/* create a container for auto_* if it does not exist already */
45397c478bd9Sstevel@tonic-gate 	if ((rc == NS_LDAP_NOTFOUND) && (op == OP_ADD) &&
45407c478bd9Sstevel@tonic-gate 	    (strcmp(tt->ttype, NS_LDAP_TYPE_AUTOMOUNT) == 0)) {
45417c478bd9Sstevel@tonic-gate 		static	char *oclist[] = {NULL, "top", NULL};
45427c478bd9Sstevel@tonic-gate 		if (version1)
45437c478bd9Sstevel@tonic-gate 			oclist[0] = "nisMap";
45447c478bd9Sstevel@tonic-gate 		else
45457c478bd9Sstevel@tonic-gate 			oclist[0] = "automountMap";
45467c478bd9Sstevel@tonic-gate 		e = __s_mk_entry(oclist, 3);
45477c478bd9Sstevel@tonic-gate 		if (e == NULL) {
45487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
45497c478bd9Sstevel@tonic-gate 			    gettext("internal memory allocation error.\n"));
45507c478bd9Sstevel@tonic-gate 			exit(1);
45517c478bd9Sstevel@tonic-gate 		}
45527c478bd9Sstevel@tonic-gate 		if (__s_add_attr(e,
45537c478bd9Sstevel@tonic-gate 		    version1 ? "nisMapName" : "automountMapName",
45547c478bd9Sstevel@tonic-gate 		    databasetype) != NS_LDAP_SUCCESS) {
45557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
45567c478bd9Sstevel@tonic-gate 			    gettext("internal memory allocation error.\n"));
45577c478bd9Sstevel@tonic-gate 			ldap_freeEntry(e);
45587c478bd9Sstevel@tonic-gate 			exit(1);
45597c478bd9Sstevel@tonic-gate 		}
45607c478bd9Sstevel@tonic-gate 
45617c478bd9Sstevel@tonic-gate 		if (inputbasedn == NULL) {
45627c478bd9Sstevel@tonic-gate 			if (get_basedn(databasetype, &inputbasedn) !=
45637c478bd9Sstevel@tonic-gate 			    NS_LDAP_SUCCESS) {
45647c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
45657c478bd9Sstevel@tonic-gate 				    gettext("Could not obtain basedn\n"));
45667c478bd9Sstevel@tonic-gate 				ldap_freeEntry(e);
45677c478bd9Sstevel@tonic-gate 				exit(1);
45687c478bd9Sstevel@tonic-gate 			}
45697c478bd9Sstevel@tonic-gate 		}
45707c478bd9Sstevel@tonic-gate 		if (__ns_ldap_addEntry(databasetype, inputbasedn, e,
45717c478bd9Sstevel@tonic-gate 		    &authority, flag, &errorp) != NS_LDAP_SUCCESS) {
45727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
45737c478bd9Sstevel@tonic-gate 			    gettext("Could not create container for %s\n"),
45747c478bd9Sstevel@tonic-gate 			    databasetype);
45757c478bd9Sstevel@tonic-gate 			ldap_freeEntry(e);
45767c478bd9Sstevel@tonic-gate 		}
45777c478bd9Sstevel@tonic-gate 	} else if (strcmp(databasetype, "publickey") != 0) {
45787c478bd9Sstevel@tonic-gate 		if (rc == NS_LDAP_NOTFOUND) {
45797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
45807c478bd9Sstevel@tonic-gate 			    gettext("Container %s does not exist\n"),
45817c478bd9Sstevel@tonic-gate 			    databasetype);
45827c478bd9Sstevel@tonic-gate 			exit(1);
45837c478bd9Sstevel@tonic-gate 		}
45847c478bd9Sstevel@tonic-gate 	}
45857c478bd9Sstevel@tonic-gate 
45867c478bd9Sstevel@tonic-gate 	if (op == OP_DUMP) {
45877c478bd9Sstevel@tonic-gate 		if (strcmp(databasetype, "publickey") == 0) {
45887c478bd9Sstevel@tonic-gate 			dumptable("hosts");
45897c478bd9Sstevel@tonic-gate 			dumptable("passwd");
45907c478bd9Sstevel@tonic-gate 		} else {
45917c478bd9Sstevel@tonic-gate 			dumptable(databasetype);
45927c478bd9Sstevel@tonic-gate 		}
45937c478bd9Sstevel@tonic-gate 		exit(exit_val);
45947c478bd9Sstevel@tonic-gate 	}
45957c478bd9Sstevel@tonic-gate 
45967c478bd9Sstevel@tonic-gate 	if (etcfile) {
45977c478bd9Sstevel@tonic-gate 		if ((etcf = fopen(etcfile, "r")) == 0) {
45987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
45997c478bd9Sstevel@tonic-gate 			    gettext("can't open file %s\n"), etcfile);
46007c478bd9Sstevel@tonic-gate 			exit(1);
46017c478bd9Sstevel@tonic-gate 		}
46027c478bd9Sstevel@tonic-gate 	} else {
46037c478bd9Sstevel@tonic-gate 		etcfile = "stdin";
46047c478bd9Sstevel@tonic-gate 		etcf = stdin;
46057c478bd9Sstevel@tonic-gate 	}
46067c478bd9Sstevel@tonic-gate 
46077c478bd9Sstevel@tonic-gate 	if (op == OP_ADD) {
46087c478bd9Sstevel@tonic-gate 		(void) addfile();
46097c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, gettext("%d entries added\n"), nent_add);
46107c478bd9Sstevel@tonic-gate 	}
46117c478bd9Sstevel@tonic-gate 
4612e1dd0a2fSth 	__ns_ldap_cancelStandalone();
461384ad75deSmj 	/* exit() -> return for make lint */
461484ad75deSmj 	return (exit_val);
46157c478bd9Sstevel@tonic-gate }
46167c478bd9Sstevel@tonic-gate 
46177c478bd9Sstevel@tonic-gate 
46187c478bd9Sstevel@tonic-gate /*
46197c478bd9Sstevel@tonic-gate  * This is called when service == auto_*.
46207c478bd9Sstevel@tonic-gate  * It calls __ns_ldap_getSearchDescriptors
46217c478bd9Sstevel@tonic-gate  * to generate the dn from SSD's base dn.
46227c478bd9Sstevel@tonic-gate  * If there is no SSD available,
46237c478bd9Sstevel@tonic-gate  * default base dn will be used
46247c478bd9Sstevel@tonic-gate  * Only the first baseDN in the SSD is used
46257c478bd9Sstevel@tonic-gate  */
46267c478bd9Sstevel@tonic-gate 
46277c478bd9Sstevel@tonic-gate static int get_basedn(char *service, char **basedn) {
46287c478bd9Sstevel@tonic-gate 	int rc = NS_LDAP_SUCCESS;
46297c478bd9Sstevel@tonic-gate 	char *dn = NULL;
46307c478bd9Sstevel@tonic-gate 	ns_ldap_search_desc_t **desc = NULL;
46317c478bd9Sstevel@tonic-gate 	ns_ldap_error_t *errp = NULL;
46327c478bd9Sstevel@tonic-gate 	void		**paramVal = NULL;
46337c478bd9Sstevel@tonic-gate 	int		prepend_automountmapname = FALSE;
46347c478bd9Sstevel@tonic-gate 
46357c478bd9Sstevel@tonic-gate 	/*
46367c478bd9Sstevel@tonic-gate 	 * Get auto_* SSD first
46377c478bd9Sstevel@tonic-gate 	 */
46387c478bd9Sstevel@tonic-gate 
46397c478bd9Sstevel@tonic-gate 	if ((rc = __ns_ldap_getSearchDescriptors(
46407c478bd9Sstevel@tonic-gate 			(const char *) service,
46417c478bd9Sstevel@tonic-gate 			&desc, &errp))  == NS_LDAP_SUCCESS &&
46427c478bd9Sstevel@tonic-gate 		desc != NULL) {
46437c478bd9Sstevel@tonic-gate 
46447c478bd9Sstevel@tonic-gate 		if (desc[0] != NULL && desc[0]->basedn != NULL) {
46457c478bd9Sstevel@tonic-gate 			dn = strdup(desc[0]->basedn);
46467c478bd9Sstevel@tonic-gate 			if (dn == NULL) {
46477c478bd9Sstevel@tonic-gate 				(void) __ns_ldap_freeSearchDescriptors
46487c478bd9Sstevel@tonic-gate 						(&desc);
46497c478bd9Sstevel@tonic-gate 				return (NS_LDAP_MEMORY);
46507c478bd9Sstevel@tonic-gate 			}
46517c478bd9Sstevel@tonic-gate 		}
46527c478bd9Sstevel@tonic-gate 	}
46537c478bd9Sstevel@tonic-gate 
46547c478bd9Sstevel@tonic-gate 	/* clean up */
46557c478bd9Sstevel@tonic-gate 	if (desc) (void) __ns_ldap_freeSearchDescriptors(&desc);
46567c478bd9Sstevel@tonic-gate 	if (errp) (void) __ns_ldap_freeError(&errp);
46577c478bd9Sstevel@tonic-gate 
46587c478bd9Sstevel@tonic-gate 	/*
46597c478bd9Sstevel@tonic-gate 	 * If no dn is duplicated from auto_* SSD, try automount SSD
46607c478bd9Sstevel@tonic-gate 	 */
46617c478bd9Sstevel@tonic-gate 	if (dn == NULL) {
46627c478bd9Sstevel@tonic-gate 		if ((rc = __ns_ldap_getSearchDescriptors(
46637c478bd9Sstevel@tonic-gate 				"automount", &desc, &errp))
46647c478bd9Sstevel@tonic-gate 				== NS_LDAP_SUCCESS && desc != NULL) {
46657c478bd9Sstevel@tonic-gate 
46667c478bd9Sstevel@tonic-gate 			if (desc[0] != NULL && desc[0]->basedn != NULL) {
46677c478bd9Sstevel@tonic-gate 				dn = strdup(desc[0]->basedn);
46687c478bd9Sstevel@tonic-gate 				if (dn == NULL) {
46697c478bd9Sstevel@tonic-gate 					(void) __ns_ldap_freeSearchDescriptors
46707c478bd9Sstevel@tonic-gate 							(&desc);
46717c478bd9Sstevel@tonic-gate 					return (NS_LDAP_MEMORY);
46727c478bd9Sstevel@tonic-gate 				}
46737c478bd9Sstevel@tonic-gate 				prepend_automountmapname = TRUE;
46747c478bd9Sstevel@tonic-gate 			}
46757c478bd9Sstevel@tonic-gate 		}
46767c478bd9Sstevel@tonic-gate 		/* clean up */
46777c478bd9Sstevel@tonic-gate 		if (desc) (void) __ns_ldap_freeSearchDescriptors(&desc);
46787c478bd9Sstevel@tonic-gate 		if (errp) (void) __ns_ldap_freeError(&errp);
46797c478bd9Sstevel@tonic-gate 	}
46807c478bd9Sstevel@tonic-gate 
46817c478bd9Sstevel@tonic-gate 	/*
46827c478bd9Sstevel@tonic-gate 	 * If no dn is duplicated from auto_* or automount SSD,
46837c478bd9Sstevel@tonic-gate 	 * use default DN
46847c478bd9Sstevel@tonic-gate 	 */
46857c478bd9Sstevel@tonic-gate 
46867c478bd9Sstevel@tonic-gate 	if (dn == NULL) {
46877c478bd9Sstevel@tonic-gate 		if ((rc = __ns_ldap_getParam(NS_LDAP_SEARCH_BASEDN_P,
46887c478bd9Sstevel@tonic-gate 			&paramVal, &errp)) == NS_LDAP_SUCCESS) {
46897c478bd9Sstevel@tonic-gate 			dn = strdup((char *)paramVal[0]);
46907c478bd9Sstevel@tonic-gate 			if (dn == NULL) {
46917c478bd9Sstevel@tonic-gate 				(void) __ns_ldap_freeParam(&paramVal);
46927c478bd9Sstevel@tonic-gate 				return (NS_LDAP_MEMORY);
46937c478bd9Sstevel@tonic-gate 			}
46947c478bd9Sstevel@tonic-gate 			prepend_automountmapname = TRUE;
46957c478bd9Sstevel@tonic-gate 		}
46967c478bd9Sstevel@tonic-gate 		if (paramVal) (void) __ns_ldap_freeParam(&paramVal);
46977c478bd9Sstevel@tonic-gate 		if (errp) (void) __ns_ldap_freeError(&errp);
46987c478bd9Sstevel@tonic-gate 	}
46997c478bd9Sstevel@tonic-gate 
47007c478bd9Sstevel@tonic-gate 
47017c478bd9Sstevel@tonic-gate 	if (dn == NULL) {
47027c478bd9Sstevel@tonic-gate 		return (NS_LDAP_OP_FAILED);
47037c478bd9Sstevel@tonic-gate 	} else {
47047c478bd9Sstevel@tonic-gate 		/*
47057c478bd9Sstevel@tonic-gate 		 * If dn is duplicated from
47067c478bd9Sstevel@tonic-gate 		 * automount SSD basedn or
47077c478bd9Sstevel@tonic-gate 		 * default base dn
47087c478bd9Sstevel@tonic-gate 		 * then prepend automountMapName=auto_xxx
47097c478bd9Sstevel@tonic-gate 		 */
47107c478bd9Sstevel@tonic-gate 		if (prepend_automountmapname)
47117c478bd9Sstevel@tonic-gate 			rc = __s_api_prepend_automountmapname_to_dn(
47127c478bd9Sstevel@tonic-gate 				service, &dn, &errp);
47137c478bd9Sstevel@tonic-gate 
47147c478bd9Sstevel@tonic-gate 		if (rc != NS_LDAP_SUCCESS) {
47157c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&errp);
47167c478bd9Sstevel@tonic-gate 			free(dn);
47177c478bd9Sstevel@tonic-gate 			return (rc);
47187c478bd9Sstevel@tonic-gate 		}
47197c478bd9Sstevel@tonic-gate 
47207c478bd9Sstevel@tonic-gate 		*basedn = dn;
47217c478bd9Sstevel@tonic-gate 
47227c478bd9Sstevel@tonic-gate 		return (NS_LDAP_SUCCESS);
47237c478bd9Sstevel@tonic-gate 	}
47247c478bd9Sstevel@tonic-gate }
4725cb5caa98Sdjl static char *
4726*c59d9dffSToomas Soome h_errno2str(int h_errno)
4727*c59d9dffSToomas Soome {
4728cb5caa98Sdjl 	switch (h_errno) {
4729cb5caa98Sdjl 	case HOST_NOT_FOUND:
4730cb5caa98Sdjl 		return ("HOST_NOT_FOUND");
4731cb5caa98Sdjl 	case TRY_AGAIN:
4732cb5caa98Sdjl 		return ("TRY_AGAIN");
4733cb5caa98Sdjl 	case NO_RECOVERY:
4734cb5caa98Sdjl 		return ("NO_RECOVERY");
4735cb5caa98Sdjl 	case NO_DATA:
4736cb5caa98Sdjl 		return ("NO_DATA");
4737cb5caa98Sdjl 	default:
4738cb5caa98Sdjl 		break;
4739cb5caa98Sdjl 	}
4740cb5caa98Sdjl 	return ("UNKNOWN_ERROR");
4741cb5caa98Sdjl }
4742