xref: /onnv-gate/usr/src/cmd/ldap/ns_ldap/ldapaddent.c (revision 12788:c71b0e8f856c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51603Svl199446  * Common Development and Distribution License (the "License").
61603Svl199446  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12758SJulian.Pullen@Sun.COM  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  * ldapaddent.c
270Sstevel@tonic-gate  *
280Sstevel@tonic-gate  * Utility to add /etc files into LDAP.
290Sstevel@tonic-gate  * Can also be used to dump entries from a ldap container in /etc format.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <libintl.h>
350Sstevel@tonic-gate #include <strings.h>
360Sstevel@tonic-gate #include <sys/param.h>
370Sstevel@tonic-gate #include <ctype.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/socket.h>
400Sstevel@tonic-gate #include <netinet/in.h>
410Sstevel@tonic-gate #include <arpa/inet.h>
420Sstevel@tonic-gate #include <locale.h>
430Sstevel@tonic-gate #include <syslog.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #undef opaque
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <nss_dbdefs.h>
480Sstevel@tonic-gate #include <netdb.h>
490Sstevel@tonic-gate #include <rpc/rpcent.h>
500Sstevel@tonic-gate #include <grp.h>
510Sstevel@tonic-gate #include <pwd.h>
526842Sth160488 #include <project.h>
530Sstevel@tonic-gate #include <shadow.h>
540Sstevel@tonic-gate #include <sys/systeminfo.h>
550Sstevel@tonic-gate #include "ns_internal.h"
560Sstevel@tonic-gate #include "ldapaddent.h"
576842Sth160488 #include "standalone.h"
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #define	OP_ADD	0
600Sstevel@tonic-gate #define	OP_DUMP	3
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static struct ttypelist_t {
630Sstevel@tonic-gate 	char *ttype;		/* type tag */
640Sstevel@tonic-gate 	int (*genent)(char *, int(*)());
650Sstevel@tonic-gate 				/* routine to turn line into ldap entries */
660Sstevel@tonic-gate 	void (*dump)(ns_ldap_result_t *);
670Sstevel@tonic-gate 				/* routine to print ldap containers */
680Sstevel@tonic-gate 	int (*filedbmline)();	/* routine to turn file line into dbm line */
690Sstevel@tonic-gate 	char *objclass;		/* Objectclass for the servicetype */
70*12758SJulian.Pullen@Sun.COM 	char *sortattr;		/* Sort attr for enumeration */
710Sstevel@tonic-gate } *tt;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate char	parse_err_msg [PARSE_ERR_MSG_LEN];
740Sstevel@tonic-gate int	continue_onerror = 0;  /* do not exit on error */
750Sstevel@tonic-gate 
760Sstevel@tonic-gate static int get_basedn(char *service, char **basedn);
770Sstevel@tonic-gate static int check_ipaddr(char *addr, char **newaddr);
786842Sth160488 static int check_projname(char *addr);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate extern	int	optind;
810Sstevel@tonic-gate extern	char	*optarg;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate extern	char	*__nis_quote_key(const char *, char *, int);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static char	*inputbasedn = NULL;
860Sstevel@tonic-gate static char	*databasetype = NULL;
870Sstevel@tonic-gate static int	exit_val = 0;
880Sstevel@tonic-gate static unsigned	nent_add = 0;
890Sstevel@tonic-gate static FILE	*etcf = 0;
900Sstevel@tonic-gate static ns_cred_t	authority;
910Sstevel@tonic-gate unsigned	flags = 0;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate static void
perr(ns_ldap_error_t * e)940Sstevel@tonic-gate perr(ns_ldap_error_t *e)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	if (e)
976842Sth160488 		(void) fprintf(stderr, "%d: %s\n",
986842Sth160488 		    e->status, e->message);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate static int
ascii_to_int(char * str)1030Sstevel@tonic-gate ascii_to_int(char *str)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	int i;
1060Sstevel@tonic-gate 	char *c = str;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if (c == NULL || *c == '\0')
1090Sstevel@tonic-gate 		return (-1);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	while (c != '\0' && *c == ' ')
1120Sstevel@tonic-gate 		c++;
1130Sstevel@tonic-gate 	if (*c == '\0')
1140Sstevel@tonic-gate 		return (-1);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	for (i = 0; i < strlen(c); i++)
1170Sstevel@tonic-gate 		if (!isdigit(c[i]))
1180Sstevel@tonic-gate 			return (-1);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	return (atoi(c));
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * Internet network address interpretation routine.
1250Sstevel@tonic-gate  * The library routines call this routine to interpret
1260Sstevel@tonic-gate  * network numbers.
1270Sstevel@tonic-gate  */
1280Sstevel@tonic-gate static in_addr_t
encode_network(const char * cp)1290Sstevel@tonic-gate encode_network(const char *cp)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	in_addr_t val;
1320Sstevel@tonic-gate 	int base;
1330Sstevel@tonic-gate 	ptrdiff_t n;
1340Sstevel@tonic-gate 	char c;
1350Sstevel@tonic-gate 	in_addr_t parts[4], *pp = parts;
1360Sstevel@tonic-gate 	int i;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate again:
1390Sstevel@tonic-gate 	val = 0; base = 10;
1400Sstevel@tonic-gate 	if (*cp == '0') {
1410Sstevel@tonic-gate 		if (*++cp == 'x' || *cp == 'X')
1420Sstevel@tonic-gate 			base = 16, cp++;
1430Sstevel@tonic-gate 		else
1440Sstevel@tonic-gate 			base = 8;
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 	while ((c = *cp) != NULL) {
1470Sstevel@tonic-gate 		if (isdigit(c)) {
1480Sstevel@tonic-gate 			if ((c - '0') >= base)
1496842Sth160488 				break;
1500Sstevel@tonic-gate 			val = (val * base) + (c - '0');
1510Sstevel@tonic-gate 			cp++;
1520Sstevel@tonic-gate 			continue;
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 		if (base == 16 && isxdigit(c)) {
1550Sstevel@tonic-gate 			val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
1560Sstevel@tonic-gate 			cp++;
1570Sstevel@tonic-gate 			continue;
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 		break;
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 	if (*cp == '.') {
1620Sstevel@tonic-gate 		if (pp >= parts + 4)
1630Sstevel@tonic-gate 			return ((in_addr_t)-1);
1640Sstevel@tonic-gate 		*pp++ = val, cp++;
1650Sstevel@tonic-gate 		goto again;
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 	if (*cp && !isspace(*cp))
1680Sstevel@tonic-gate 		return ((in_addr_t)-1);
1690Sstevel@tonic-gate 	*pp++ = val;
1700Sstevel@tonic-gate 	n = pp - parts;
1710Sstevel@tonic-gate 	if (n > 4)
1720Sstevel@tonic-gate 		return ((in_addr_t)-1);
1730Sstevel@tonic-gate 	for (val = 0, i = 0; i < n; i++) {
1740Sstevel@tonic-gate 		val <<= 8;
1750Sstevel@tonic-gate 		val |= parts[i] & 0xff;
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 	for (/* no init */; i < 4; i++)
1780Sstevel@tonic-gate 		val <<= 8;
1790Sstevel@tonic-gate 	return (val);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate static void
replace_tab2space(char * str)1830Sstevel@tonic-gate replace_tab2space(char *str)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	int i = 0;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	while ((str) && (str[i])) {
1880Sstevel@tonic-gate 		if (str[i] == '\t')
1890Sstevel@tonic-gate 			str[i] = ' ';
1900Sstevel@tonic-gate 		i++;
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate static int
blankline(char * line)1950Sstevel@tonic-gate blankline(char *line)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	char *p;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	for (p = line; *p; p++)
2000Sstevel@tonic-gate 		if (*p != ' ' && *p != '\t')
2010Sstevel@tonic-gate 			return (0);
2020Sstevel@tonic-gate 	return (1);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
2051603Svl199446 /*
2061603Svl199446  * check whether the token <tok> is a triplet,
2071603Svl199446  * i. e. <tok> := (<hostname>,<username>,<domainname>)
2081603Svl199446  * where <hostname>, <username>, <domainname> are IA5String
2091603Svl199446  * <tok> supposes to contain NO spaces and start with '('
2101603Svl199446  */
2111603Svl199446 static int
is_triplet(char * tok)2121603Svl199446 is_triplet(char *tok)
2131603Svl199446 {
2141603Svl199446 	char *s;
2151603Svl199446 	return (strchr(++tok, '(') == NULL &&		/* no more '(' */
2166842Sth160488 	    (s = strchr(tok, ')')) != NULL &&		/* find ')' */
2176842Sth160488 	    !*++s &&					/* ')' ends token */
2186842Sth160488 	    (tok = strchr(tok, ',')) != NULL &&		/* host up to ',' */
2196842Sth160488 	    (tok = strchr(++tok, ',')) != NULL &&	/* user up to ',' */
2206842Sth160488 	    strchr(++tok, ',') == NULL);		/* no more ',' */
2211603Svl199446 }
2221603Svl199446 
2230Sstevel@tonic-gate static void
line_buf_expand(struct line_buf * line)2240Sstevel@tonic-gate line_buf_expand(struct line_buf *line)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	line->alloc += BUFSIZ;
2270Sstevel@tonic-gate 	line->str = (char *)realloc(line->str, line->alloc);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	if (line->str == NULL) {
2300Sstevel@tonic-gate 		(void) fprintf(stderr,
2310Sstevel@tonic-gate 		    gettext("line_buf_expand: out of memory\n"));
2320Sstevel@tonic-gate 		exit(1);
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate static void
line_buf_init(struct line_buf * line)2370Sstevel@tonic-gate line_buf_init(struct line_buf *line)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate 	(void) memset((char *)line, 0, sizeof (*line));
2400Sstevel@tonic-gate 	line_buf_expand(line);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate static int
__s_add_attr(ns_ldap_entry_t * e,char * attrname,char * value)2440Sstevel@tonic-gate __s_add_attr(ns_ldap_entry_t *e, char *attrname, char *value)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	ns_ldap_attr_t	*a;
2470Sstevel@tonic-gate 	char		*v;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	a = (ns_ldap_attr_t *)calloc(1, sizeof (ns_ldap_attr_t));
2500Sstevel@tonic-gate 	if (a == NULL)
2510Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2520Sstevel@tonic-gate 	a->attrname = strdup(attrname);
2530Sstevel@tonic-gate 	if (a->attrname == NULL) {
2540Sstevel@tonic-gate 		free(a);
2550Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 	a->attrvalue = (char **)calloc(1, sizeof (char **));
2580Sstevel@tonic-gate 	if (a->attrvalue == NULL) {
2590Sstevel@tonic-gate 		free(a->attrname);
2600Sstevel@tonic-gate 		free(a);
2610Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 	a->value_count = 1;
2640Sstevel@tonic-gate 	a->attrvalue[0] = NULL;
2650Sstevel@tonic-gate 	v = strdup(value);
2660Sstevel@tonic-gate 	if (v == NULL) {
2670Sstevel@tonic-gate 		free(a->attrname);
2680Sstevel@tonic-gate 		free(a->attrvalue);
2690Sstevel@tonic-gate 		free(a);
2700Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 	a->attrvalue[0] = v;
2730Sstevel@tonic-gate 	e->attr_pair[e->attr_count] = a;
2740Sstevel@tonic-gate 	e->attr_count++;
2750Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate static int
__s_add_attrlist(ns_ldap_entry_t * e,char * attrname,char ** argv)2790Sstevel@tonic-gate __s_add_attrlist(ns_ldap_entry_t *e, char *attrname, char **argv)
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate 	ns_ldap_attr_t	*a;
2820Sstevel@tonic-gate 	char		*v;
2830Sstevel@tonic-gate 	char		**av;
2840Sstevel@tonic-gate 	int		i, j;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	a = (ns_ldap_attr_t *)calloc(1, sizeof (ns_ldap_attr_t));
2870Sstevel@tonic-gate 	if (a == NULL)
2880Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2890Sstevel@tonic-gate 	a->attrname = strdup(attrname);
2900Sstevel@tonic-gate 	if (a->attrname == NULL) {
2910Sstevel@tonic-gate 		free(a);
2920Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	for (i = 0, av = argv; *av != NULL; av++, i++)
2960Sstevel@tonic-gate 		;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	a->attrvalue = (char **)calloc(i, sizeof (char **));
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	if (a->attrvalue == NULL) {
3010Sstevel@tonic-gate 		free(a->attrname);
3020Sstevel@tonic-gate 		free(a);
3030Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 	a->value_count = i;
3060Sstevel@tonic-gate 	for (j = 0; j < i; j++) {
3070Sstevel@tonic-gate 		v = strdup(argv[j]);
3080Sstevel@tonic-gate 		if (v == NULL) {
3090Sstevel@tonic-gate 			free(a->attrname);
3100Sstevel@tonic-gate 			free(a->attrvalue);
3110Sstevel@tonic-gate 			free(a);
3120Sstevel@tonic-gate 			return (NS_LDAP_MEMORY);
3130Sstevel@tonic-gate 		}
3140Sstevel@tonic-gate 		a->attrvalue[j] = v;
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 	e->attr_pair[e->attr_count] = a;
3170Sstevel@tonic-gate 	e->attr_count++;
3180Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate static ns_ldap_entry_t *
__s_mk_entry(char ** objclass,int max_attr)3220Sstevel@tonic-gate __s_mk_entry(char **objclass, int max_attr)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate 	ns_ldap_entry_t *e;
3250Sstevel@tonic-gate 	e = (ns_ldap_entry_t *)calloc(1, sizeof (ns_ldap_entry_t));
3260Sstevel@tonic-gate 	if (e == NULL)
3270Sstevel@tonic-gate 		return (NULL);
3280Sstevel@tonic-gate 	e->attr_pair = (ns_ldap_attr_t **)calloc(max_attr+1,
3296842Sth160488 	    sizeof (ns_ldap_attr_t *));
3300Sstevel@tonic-gate 	if (e->attr_pair == NULL) {
3310Sstevel@tonic-gate 		free(e);
3320Sstevel@tonic-gate 		return (NULL);
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 	e->attr_count = 0;
3350Sstevel@tonic-gate 	if (__s_add_attrlist(e, "objectClass", objclass) != NS_LDAP_SUCCESS) {
3360Sstevel@tonic-gate 		free(e->attr_pair);
3370Sstevel@tonic-gate 		free(e);
3380Sstevel@tonic-gate 		return (NULL);
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 	return (e);
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate static void
ldap_freeEntry(ns_ldap_entry_t * ep)3440Sstevel@tonic-gate ldap_freeEntry(ns_ldap_entry_t *ep)
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate 	int		j, k = 0;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if (ep == NULL)
3490Sstevel@tonic-gate 		return;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if (ep->attr_pair == NULL) {
3520Sstevel@tonic-gate 		free(ep);
3530Sstevel@tonic-gate 		return;
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 	for (j = 0; j < ep->attr_count; j++) {
3560Sstevel@tonic-gate 		if (ep->attr_pair[j] == NULL)
3570Sstevel@tonic-gate 			continue;
3580Sstevel@tonic-gate 		if (ep->attr_pair[j]->attrname)
3590Sstevel@tonic-gate 			free(ep->attr_pair[j]->attrname);
3600Sstevel@tonic-gate 		if (ep->attr_pair[j]->attrvalue) {
3610Sstevel@tonic-gate 			for (k = 0; (k < ep->attr_pair[j]->value_count) &&
3626842Sth160488 			    (ep->attr_pair[j]->attrvalue[k]); k++) {
3630Sstevel@tonic-gate 				free(ep->attr_pair[j]->attrvalue[k]);
3640Sstevel@tonic-gate 			}
3650Sstevel@tonic-gate 			free(ep->attr_pair[j]->attrvalue);
3660Sstevel@tonic-gate 		}
3670Sstevel@tonic-gate 		free(ep->attr_pair[j]);
3680Sstevel@tonic-gate 	}
3690Sstevel@tonic-gate 	free(ep->attr_pair);
3700Sstevel@tonic-gate 	free(ep);
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate static int
addentry(void * entry,int mod)3740Sstevel@tonic-gate addentry(void *entry, int mod)
3750Sstevel@tonic-gate {
3760Sstevel@tonic-gate 	int		 result = 0;
3770Sstevel@tonic-gate 	ns_ldap_error_t	 *eres = NULL;
3780Sstevel@tonic-gate 	int		rc = 1;
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	/*  adds entry into the LDAP tree */
3820Sstevel@tonic-gate 	if (mod)
3830Sstevel@tonic-gate 		result = __ns_ldap_addTypedEntry(databasetype, inputbasedn,
3846842Sth160488 		    entry, 0, &authority, NS_LDAP_FOLLOWREF | NS_LDAP_KEEP_CONN,
3856842Sth160488 		    &eres);
3860Sstevel@tonic-gate 	else
3870Sstevel@tonic-gate 		result = __ns_ldap_addTypedEntry(databasetype, inputbasedn,
3886842Sth160488 		    entry, 1, &authority, NS_LDAP_FOLLOWREF | NS_LDAP_KEEP_CONN,
3896842Sth160488 		    &eres);
3900Sstevel@tonic-gate 	/*
3910Sstevel@tonic-gate 	 *  Return	0 on success
3920Sstevel@tonic-gate 	 *		LDAP_ALREADY_EXISTS if entry exists already
3930Sstevel@tonic-gate 	 *		1 for all other non-fatal errors.
3940Sstevel@tonic-gate 	 *  Exit on fatal errors.
3950Sstevel@tonic-gate 	 */
3960Sstevel@tonic-gate 	switch (result) {
3970Sstevel@tonic-gate 	case NS_LDAP_SUCCESS:
3980Sstevel@tonic-gate 		nent_add++;
3990Sstevel@tonic-gate 		rc = 0;
4000Sstevel@tonic-gate 		break;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	case NS_LDAP_OP_FAILED:
4030Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("operation failed.\n"));
4040Sstevel@tonic-gate 		rc = 1;
4050Sstevel@tonic-gate 		break;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	case NS_LDAP_INVALID_PARAM:
4080Sstevel@tonic-gate 		(void) fprintf(stderr,
4090Sstevel@tonic-gate 		    gettext("invalid parameter(s) passed.\n"));
4100Sstevel@tonic-gate 		rc = 1;
4110Sstevel@tonic-gate 		break;
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	case NS_LDAP_NOTFOUND:
4140Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("entry not found.\n"));
4150Sstevel@tonic-gate 		rc = 1;
4160Sstevel@tonic-gate 		break;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	case NS_LDAP_MEMORY:
4190Sstevel@tonic-gate 		(void) fprintf(stderr,
4200Sstevel@tonic-gate 		    gettext("internal memory allocation error.\n"));
4210Sstevel@tonic-gate 		exit(1);
4220Sstevel@tonic-gate 		break;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	case NS_LDAP_CONFIG:
4250Sstevel@tonic-gate 		(void) fprintf(stderr,
4260Sstevel@tonic-gate 		    gettext("LDAP Configuration problem.\n"));
4270Sstevel@tonic-gate 		perr(eres);
4280Sstevel@tonic-gate 		exit(1);
4290Sstevel@tonic-gate 		break;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	case NS_LDAP_PARTIAL:
4320Sstevel@tonic-gate 		(void) fprintf(stderr,
4330Sstevel@tonic-gate 		    gettext("partial result returned\n"));
4340Sstevel@tonic-gate 		perr(eres);
4350Sstevel@tonic-gate 		rc = 1;
4360Sstevel@tonic-gate 		break;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	case NS_LDAP_INTERNAL:
4392277Sjs198686 		if (eres->status == LDAP_ALREADY_EXISTS ||
4406842Sth160488 		    eres->status == LDAP_NO_SUCH_OBJECT)
4410Sstevel@tonic-gate 			rc = eres->status;
4422830Sdjl 		else if (eres->status == LDAP_INSUFFICIENT_ACCESS) {
4432830Sdjl 			(void) fprintf(stderr,
4446842Sth160488 			    gettext("The user does not have permission"
4456842Sth160488 			    " to add/modify entries\n"));
4462830Sdjl 			perr(eres);
4472830Sdjl 			exit(1);
4482830Sdjl 		} else {
4490Sstevel@tonic-gate 			rc = 1;
4500Sstevel@tonic-gate 			perr(eres);
4510Sstevel@tonic-gate 		}
4520Sstevel@tonic-gate 		break;
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if (eres)
4560Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&eres);
4570Sstevel@tonic-gate 	return (rc);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate  * usage(char *msg)
4620Sstevel@tonic-gate  * Display usage message to STDERR.
4630Sstevel@tonic-gate  */
4640Sstevel@tonic-gate static void
usage(char * msg)4650Sstevel@tonic-gate usage(char *msg) {
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	if (msg)
4686842Sth160488 		(void) fprintf(stderr, "%s\n", msg);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
4716842Sth160488 	"usage: ldapaddent [-cpv] [-a authenticationMethod] [-b baseDN]\n"
4726842Sth160488 	"-D bindDN [-w bindPassword] [-j passwdFile] [-f filename]\n"
4736842Sth160488 	"database\n"
4746842Sth160488 	"\n"
4756842Sth160488 	"usage: ldapaddent  [-cpv] -asasl/GSSAPI [-b baseDN] [-f filename]\n"
4766842Sth160488 	"database\n"
4776842Sth160488 	"\n"
4786842Sth160488 	"usage: ldapaddent  -d [-v] [-a authenticationMethod] [-D bindDN]\n"
4796842Sth160488 	"[-w bindPassword] [-j passwdFile] database\n"
4806842Sth160488 	"\n"
4816842Sth160488 	"usage: ldapaddent [-cpv] -h LDAP_server[:serverPort] [-M domainName]\n"
4826842Sth160488 	"[-N  profileName]  [-P certifPath]  [-a authenticationMethod]\n"
4836842Sth160488 	"[-b baseDN] -D bindDN [-w bindPassword] [-f filename]\n"
4846842Sth160488 	"[-j passwdFile] database\n"
4856842Sth160488 	"\n"
4866842Sth160488 	"usage: ldapaddent [-cpv] -h LDAP_server[:serverPort] [-M domainName]\n"
4876842Sth160488 	"[-N  profileName]  [-P certifPath] -asasl/GSSAPI  [-b baseDN]\n"
4886842Sth160488 	"[-f filename] database\n"
4896842Sth160488 	"\n"
4906842Sth160488 	"usage: ldapaddent -d [-v] -h LDAP_server[:serverPort]"
4916842Sth160488 	" [-M domainName]\n"
4926842Sth160488 	"[-N profileName]  [-P certifPath]  [-a authenticationMethod]\n"
4936842Sth160488 	"[-b baseDN] -D bindDN [-w bindPassword] [-j passwdFile]\n"
4946842Sth160488 	"database\n"));
4950Sstevel@tonic-gate 	exit(1);
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate /*
4990Sstevel@tonic-gate  * Determine if the given string is an IP address (IPv4 or IPv6).
5000Sstevel@tonic-gate  * If so, it's converted to the preferred form (rfc2373) and
5010Sstevel@tonic-gate  * *newaddr will point to the new address.
5020Sstevel@tonic-gate  *
5030Sstevel@tonic-gate  * Returns	-2		: inet_ntop error
5040Sstevel@tonic-gate  *		-1		: not an IP address
5050Sstevel@tonic-gate  *		0		: unsupported IP address (future use)
5060Sstevel@tonic-gate  *		AF_INET		: IPv4
5070Sstevel@tonic-gate  *		AF_INET6	: IPv6
5080Sstevel@tonic-gate  */
5090Sstevel@tonic-gate static int
check_ipaddr(char * addr,char ** newaddr)5100Sstevel@tonic-gate check_ipaddr(char *addr, char **newaddr) {
5110Sstevel@tonic-gate 	ipaddr_t	addr_ipv4 = 0;
5120Sstevel@tonic-gate 	in6_addr_t	addr_ipv6;
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	/* IPv6 */
5150Sstevel@tonic-gate 	if (inet_pton(AF_INET6, addr, &addr_ipv6) == 1) {
5160Sstevel@tonic-gate 		if (newaddr == NULL)
5170Sstevel@tonic-gate 			return (AF_INET6);
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 		/* Convert IPv4-mapped IPv6 address to IPv4 */
5200Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&addr_ipv6) ||
5210Sstevel@tonic-gate 					IN6_IS_ADDR_V4COMPAT(&addr_ipv6)) {
5220Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&addr_ipv6, addr_ipv4);
5230Sstevel@tonic-gate 			if ((*newaddr = calloc(1, INET_ADDRSTRLEN)) == NULL) {
5240Sstevel@tonic-gate 				(void) fprintf(stderr,
5250Sstevel@tonic-gate 				    gettext("out of memory\n"));
5260Sstevel@tonic-gate 				exit(1);
5270Sstevel@tonic-gate 			}
5280Sstevel@tonic-gate 			if (inet_ntop(AF_INET, &addr_ipv4, *newaddr,
5290Sstevel@tonic-gate 			    INET_ADDRSTRLEN))
5300Sstevel@tonic-gate 				return (AF_INET6);
5310Sstevel@tonic-gate 			free(*newaddr);
5320Sstevel@tonic-gate 			return (-2);
5330Sstevel@tonic-gate 		}
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 		/* Processing general IPv6 addresses */
5360Sstevel@tonic-gate 		if ((*newaddr = calloc(1, INET6_ADDRSTRLEN)) == NULL) {
5370Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
5380Sstevel@tonic-gate 			exit(1);
5390Sstevel@tonic-gate 		}
5400Sstevel@tonic-gate 		if (inet_ntop(AF_INET6, &addr_ipv6, *newaddr, INET6_ADDRSTRLEN))
5410Sstevel@tonic-gate 			return (AF_INET6);
5420Sstevel@tonic-gate 		free(*newaddr);
5430Sstevel@tonic-gate 		return (-2);
5440Sstevel@tonic-gate 	}
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	/* Processing IPv4 addresses of the type d.d.d.d. */
5470Sstevel@tonic-gate 	if (inet_pton(AF_INET, addr, &addr_ipv4) == 1) {
5480Sstevel@tonic-gate 		if (newaddr == NULL)
5490Sstevel@tonic-gate 			return (AF_INET);
5500Sstevel@tonic-gate 		if ((*newaddr = calloc(1, INET_ADDRSTRLEN)) == NULL) {
5510Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
5520Sstevel@tonic-gate 			exit(1);
5530Sstevel@tonic-gate 		}
5540Sstevel@tonic-gate 		if (inet_ntop(AF_INET, &addr_ipv4, *newaddr, INET_ADDRSTRLEN))
5550Sstevel@tonic-gate 			return (AF_INET);
5560Sstevel@tonic-gate 		free(*newaddr);
5570Sstevel@tonic-gate 		return (-2);
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	/* Processing IPv4 addresses d.d.d , d.d and d */
5610Sstevel@tonic-gate 	if (inet_addr(addr) != (in_addr_t)-1) {
5620Sstevel@tonic-gate 		if (newaddr == NULL)
5630Sstevel@tonic-gate 			return (AF_INET);
5640Sstevel@tonic-gate 		if ((*newaddr = strdup(addr)) == NULL) {
5650Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
5660Sstevel@tonic-gate 			exit(1);
5670Sstevel@tonic-gate 		}
5680Sstevel@tonic-gate 		return (AF_INET);
5690Sstevel@tonic-gate 	}
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	return (-1);
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate 
5746842Sth160488 /*
5756842Sth160488  * Verifies that project name meets the restrictions defined by project(4).
5766842Sth160488  */
5776842Sth160488 static int
check_projname(char * addr)5786842Sth160488 check_projname(char *addr)
5796842Sth160488 {
5806842Sth160488 	int i;
5816842Sth160488 	if (addr == NULL || *addr == '\0')
5826842Sth160488 		return (-1);
5836842Sth160488 
5846842Sth160488 	for (i = 0; i < strlen(addr); i++) {
5856842Sth160488 		if (!isalpha(addr[i]) &&
5866842Sth160488 		    !isdigit(addr[i]) &&
5876842Sth160488 		    addr[i] != '_' &&
5886842Sth160488 		    addr[i] != '-' &&
5896842Sth160488 		    addr[i] != '.')
5906842Sth160488 			return (-1);
5916842Sth160488 	}
5926842Sth160488 
5936842Sth160488 	return (0);
5946842Sth160488 }
5956842Sth160488 
5960Sstevel@tonic-gate static int
genent_hosts(char * line,int (* cback)())5970Sstevel@tonic-gate genent_hosts(char *line, int (*cback)())
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate 	char buf[BUFSIZ+1];
6002689Siz202018 	char *t, *comment;
6010Sstevel@tonic-gate 	entry_col ecol[4];
6020Sstevel@tonic-gate 	char *cname, *pref_addr;
6030Sstevel@tonic-gate 	int ctr = 0, retval = 1;
6040Sstevel@tonic-gate 	int rc = GENENT_OK, af;
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	struct hostent  data;
6070Sstevel@tonic-gate 	char *alias;
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	/*
6100Sstevel@tonic-gate 	 * don't clobber our argument
6110Sstevel@tonic-gate 	 */
6120Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
6136842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
6146842Sth160488 		    PARSE_ERR_MSG_LEN);
6150Sstevel@tonic-gate 		return (GENENT_PARSEERR);
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 	(void) strcpy(buf, line);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	/*
6200Sstevel@tonic-gate 	 * clear column data
6210Sstevel@tonic-gate 	 */
6220Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	/*
6250Sstevel@tonic-gate 	 * comment (col 3)
6262689Siz202018 	 * All leading spaces will be deleted from the comment
6270Sstevel@tonic-gate 	 */
6282689Siz202018 	ecol[3].ec_value.ec_value_val = "";
6292689Siz202018 	ecol[3].ec_value.ec_value_len = 0;
6302689Siz202018 	comment = t = strchr(buf, '#');
6312689Siz202018 	if (comment) {
6322689Siz202018 		do {
6332689Siz202018 			++comment;
6342689Siz202018 		} while (*comment != '\0' && isspace(*comment));
6352689Siz202018 		if (*comment != '\0') {
6362689Siz202018 			*--comment = '#';
6372689Siz202018 			ecol[3].ec_value.ec_value_val = strdup(comment);
6382689Siz202018 			ecol[3].ec_value.ec_value_len = strlen(comment)+1;
6392689Siz202018 		}
6402689Siz202018 
6412689Siz202018 		*t = '\0';
6420Sstevel@tonic-gate 	}
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	/*
6450Sstevel@tonic-gate 	 * addr(col 2)
6460Sstevel@tonic-gate 	 */
6470Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
6486842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no host"),
6496842Sth160488 		    PARSE_ERR_MSG_LEN);
6500Sstevel@tonic-gate 		return (GENENT_PARSEERR);
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	af = check_ipaddr(t, &pref_addr);
6540Sstevel@tonic-gate 	if (af == -2) {
6556842Sth160488 		(void) strlcpy(parse_err_msg, gettext("Internal error"),
6566842Sth160488 		    PARSE_ERR_MSG_LEN);
6570Sstevel@tonic-gate 	} else if (af == -1) {
6580Sstevel@tonic-gate 		(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
6596842Sth160488 		    gettext("Invalid IP address: %s"), t);
6600Sstevel@tonic-gate 	} else if (flags & F_VERBOSE) {
6610Sstevel@tonic-gate 		if ((strncasecmp(t, pref_addr, strlen(t))) != 0) {
6620Sstevel@tonic-gate 			(void) fprintf(stdout,
6630Sstevel@tonic-gate 			    gettext("IP address %s converted to %s\n"),
6640Sstevel@tonic-gate 			    t, pref_addr);
6650Sstevel@tonic-gate 		}
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	if (af < 0) {
6696842Sth160488 		(void) fprintf(stderr, "%s\n", parse_err_msg);
6700Sstevel@tonic-gate 		if (continue_onerror == 0)
6710Sstevel@tonic-gate 			return (GENENT_CBERR);
6720Sstevel@tonic-gate 		else
6730Sstevel@tonic-gate 			return (rc);
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = pref_addr;
6770Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(pref_addr)+1;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	/*
6800Sstevel@tonic-gate 	 * cname (col 0)
6810Sstevel@tonic-gate 	 */
6820Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
6836842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no cname"),
6846842Sth160488 		    PARSE_ERR_MSG_LEN);
6850Sstevel@tonic-gate 		return (GENENT_PARSEERR);
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
6880Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
6890Sstevel@tonic-gate 	cname = t;
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	/* build entry */
6930Sstevel@tonic-gate 	if ((data.h_addr_list = (char **)calloc(2, sizeof (char **))) == NULL) {
6940Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
6950Sstevel@tonic-gate 		exit(1);
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 	data.h_addr_list[0] = strdup(ecol[2].ec_value.ec_value_val);
6980Sstevel@tonic-gate 	data.h_addr_list[1] = NULL;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	free(pref_addr);
7010Sstevel@tonic-gate 	data.h_name = strdup(ecol[0].ec_value.ec_value_val);
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	/*
7040Sstevel@tonic-gate 	 * name (col 1)
7050Sstevel@tonic-gate 	 */
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	data.h_aliases = NULL;
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	do {
7100Sstevel@tonic-gate 		/*
7110Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
7120Sstevel@tonic-gate 		 */
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 		/* This call to AddEntry may move out of the loop */
7150Sstevel@tonic-gate 		/* This is because we have to call the function just once */
7160Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
7170Sstevel@tonic-gate 			continue;
7180Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
7190Sstevel@tonic-gate 			continue;
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
7220Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 		ctr++;
7250Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
7260Sstevel@tonic-gate 		if ((data.h_aliases = (char **)realloc(data.h_aliases,
7276842Sth160488 		    ctr * sizeof (char **))) == NULL) {
7280Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
7290Sstevel@tonic-gate 			exit(1);
7300Sstevel@tonic-gate 		}
7310Sstevel@tonic-gate 		data.h_aliases[ctr-1] = alias;
7320Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
7330Sstevel@tonic-gate 
7342689Siz202018 	/*
7352689Siz202018 	 * End the list of all the aliases by NULL
7362689Siz202018 	 * If there is some comment, it will be stored as the last entry
7372689Siz202018 	 * in the list of the host aliases
7382689Siz202018 	 */
7390Sstevel@tonic-gate 	if ((data.h_aliases = (char **)realloc(data.h_aliases,
7406842Sth160488 	    (ecol[3].ec_value.ec_value_len != 0 ?
7416842Sth160488 	    ctr + 2 : ctr + 1) * sizeof (char **))) == NULL) {
7420Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
7430Sstevel@tonic-gate 		exit(1);
7440Sstevel@tonic-gate 	}
7452689Siz202018 
7462689Siz202018 	if (ecol[3].ec_value.ec_value_len != 0) {
7472689Siz202018 		data.h_aliases[ctr++] = ecol[3].ec_value.ec_value_val;
7482689Siz202018 	}
7490Sstevel@tonic-gate 	data.h_aliases[ctr] = NULL;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	if (flags & F_VERBOSE)
7520Sstevel@tonic-gate 		(void) fprintf(stdout,
7530Sstevel@tonic-gate 		    gettext("Adding entry : cn=%s+ipHostNumber=%s\n"),
7540Sstevel@tonic-gate 		    data.h_name, data.h_addr_list[0]);
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
7570Sstevel@tonic-gate 
7582689Siz202018 	if (ecol[3].ec_value.ec_value_len != 0) {
7592689Siz202018 		free(ecol[3].ec_value.ec_value_val);
7602689Siz202018 	}
7612689Siz202018 
7620Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
7630Sstevel@tonic-gate 		if (continue_onerror)
7640Sstevel@tonic-gate 			(void) fprintf(stderr,
7656842Sth160488 			    gettext("Entry: cn=%s+ipHostNumber=%s "
7666842Sth160488 			    "already Exists -skipping it\n"),
7676842Sth160488 			    data.h_name, data.h_addr_list[0]);
7680Sstevel@tonic-gate 		else {
7690Sstevel@tonic-gate 			rc = GENENT_CBERR;
7700Sstevel@tonic-gate 			(void) fprintf(stderr,
7716842Sth160488 			    gettext("Entry: cn=%s+ipHostNumber=%s"
7726842Sth160488 			    " already Exists\n"),
7736842Sth160488 			    data.h_name, data.h_addr_list[0]);
7740Sstevel@tonic-gate 		}
7750Sstevel@tonic-gate 	} else if (retval)
7760Sstevel@tonic-gate 		rc = GENENT_CBERR;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	free(data.h_name);
7790Sstevel@tonic-gate 	free(data.h_aliases);
7800Sstevel@tonic-gate 	free(data.h_addr_list);
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	return (rc);
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate static void
dump_hosts(ns_ldap_result_t * res)7880Sstevel@tonic-gate dump_hosts(ns_ldap_result_t *res)
7890Sstevel@tonic-gate {
7902689Siz202018 	ns_ldap_attr_t	*attrptr = NULL,
7916842Sth160488 	    *cn = NULL,
7926842Sth160488 	    *iphostnumber = NULL,
7936842Sth160488 	    *desc = NULL;
7940Sstevel@tonic-gate 	int		 i, j;
7950Sstevel@tonic-gate 	char		*name; /* host name */
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
7980Sstevel@tonic-gate 		return;
7990Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
8000Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
8010Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
8020Sstevel@tonic-gate 			cn = attrptr;
8030Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "iphostnumber") == 0)
8040Sstevel@tonic-gate 			iphostnumber = attrptr;
8052689Siz202018 		else if (strcasecmp(attrptr->attrname, "description") == 0) {
8062689Siz202018 			desc = attrptr;
8072689Siz202018 		}
8080Sstevel@tonic-gate 	}
8090Sstevel@tonic-gate 	/* sanity check */
8100Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
8110Sstevel@tonic-gate 	    iphostnumber == NULL || iphostnumber->attrvalue == NULL ||
8120Sstevel@tonic-gate 	    iphostnumber->attrvalue[0] == NULL)
8130Sstevel@tonic-gate 		return;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
8160Sstevel@tonic-gate 		return;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	/* ip host/ipnode number */
8190Sstevel@tonic-gate 	if (strlen(iphostnumber->attrvalue[0]) <= INET_ADDRSTRLEN)
8200Sstevel@tonic-gate 		/* IPV4 or IPV6 but <= NET_ADDRSTRLEN */
8210Sstevel@tonic-gate 		(void) fprintf(stdout, "%-18s", iphostnumber->attrvalue[0]);
8220Sstevel@tonic-gate 	else
8230Sstevel@tonic-gate 		/* IPV6 */
8240Sstevel@tonic-gate 		(void) fprintf(stdout, "%-48s", iphostnumber->attrvalue[0]);
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	/* host/ipnode name */
8270Sstevel@tonic-gate 	(void) fprintf(stdout, "%s ", name);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	/* aliases */
8300Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
8310Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
8320Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0)
8330Sstevel@tonic-gate 				/* skip host name */
8340Sstevel@tonic-gate 				continue;
8350Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
8360Sstevel@tonic-gate 		}
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 
8392689Siz202018 	/* description */
8402689Siz202018 	if (desc != NULL && desc->attrvalue != NULL &&
8412689Siz202018 	    desc->attrvalue[0] != NULL) {
8422689Siz202018 		(void) fprintf(stdout, "#%s", desc->attrvalue[0]);
8432689Siz202018 	}
8442689Siz202018 
8450Sstevel@tonic-gate 	/* end of line */
8460Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
8470Sstevel@tonic-gate }
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate /*
8500Sstevel@tonic-gate  * /etc/rpc
8510Sstevel@tonic-gate  */
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate static int
genent_rpc(char * line,int (* cback)())8540Sstevel@tonic-gate genent_rpc(char *line, int (*cback)())
8550Sstevel@tonic-gate {
8560Sstevel@tonic-gate 	char buf[BUFSIZ+1];
8570Sstevel@tonic-gate 	char *t;
8580Sstevel@tonic-gate 	entry_col ecol[4];
8590Sstevel@tonic-gate 	char *cname;
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	struct rpcent	data;
8620Sstevel@tonic-gate 	char *alias;
8630Sstevel@tonic-gate 	int ctr = 0;
8640Sstevel@tonic-gate 	int retval = 1;
8650Sstevel@tonic-gate 	int rc = GENENT_OK;
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	/*
8680Sstevel@tonic-gate 	 * don't clobber our argument
8690Sstevel@tonic-gate 	 */
8700Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
8716842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
8726842Sth160488 		    PARSE_ERR_MSG_LEN);
8730Sstevel@tonic-gate 		return (GENENT_PARSEERR);
8740Sstevel@tonic-gate 	}
8750Sstevel@tonic-gate 	(void) strcpy(buf, line);
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	/*
8780Sstevel@tonic-gate 	 * clear column data
8790Sstevel@tonic-gate 	 */
8800Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	/*
8830Sstevel@tonic-gate 	 * comment (col 3)
8840Sstevel@tonic-gate 	 */
8850Sstevel@tonic-gate 	t = strchr(buf, '#');
8860Sstevel@tonic-gate 	if (t) {
8870Sstevel@tonic-gate 		*t++ = 0;
8880Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
8890Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
8900Sstevel@tonic-gate 	} else {
8910Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
8920Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	/*
8960Sstevel@tonic-gate 	 * cname(col 0)
8970Sstevel@tonic-gate 	 */
8980Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
8996842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no number"),
9006842Sth160488 		    PARSE_ERR_MSG_LEN);
9010Sstevel@tonic-gate 		return (GENENT_PARSEERR);
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
9040Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
9050Sstevel@tonic-gate 	cname = t;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	/*
9080Sstevel@tonic-gate 	 * number (col 2)
9090Sstevel@tonic-gate 	 */
9100Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
9116842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no number"),
9126842Sth160488 		    PARSE_ERR_MSG_LEN);
9130Sstevel@tonic-gate 		return (GENENT_PARSEERR);
9140Sstevel@tonic-gate 	}
9150Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
9160Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	/*
9200Sstevel@tonic-gate 	 * build entry
9210Sstevel@tonic-gate 	 */
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	data.r_name = strdup(ecol[0].ec_value.ec_value_val);
9240Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
9256842Sth160488 	    ecol[2].ec_value.ec_value_val[0] != '\0') {
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 		data.r_number = ascii_to_int(ecol[2].ec_value.ec_value_val);
9280Sstevel@tonic-gate 		if (data.r_number == -1) {
9290Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
9306842Sth160488 			    gettext("invalid program number: %s"),
9310Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
9320Sstevel@tonic-gate 		return (GENENT_PARSEERR);
9330Sstevel@tonic-gate 		}
9340Sstevel@tonic-gate 	} else
9350Sstevel@tonic-gate 		data.r_number = -1;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	/*
9380Sstevel@tonic-gate 	 * name (col 1)
9390Sstevel@tonic-gate 	 */
9400Sstevel@tonic-gate 	t = cname;
9410Sstevel@tonic-gate 	data.r_aliases = NULL;
9420Sstevel@tonic-gate 	do {
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 		/*
9450Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
9460Sstevel@tonic-gate 		 */
9470Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
9480Sstevel@tonic-gate 			continue;
9490Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
9500Sstevel@tonic-gate 			continue;
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
9530Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 		ctr++;
9560Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
9570Sstevel@tonic-gate 		if ((data.r_aliases = (char **)realloc(data.r_aliases,
9586842Sth160488 		    ctr * sizeof (char **))) == NULL) {
9590Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
9600Sstevel@tonic-gate 			exit(1);
9610Sstevel@tonic-gate 		}
9620Sstevel@tonic-gate 		data.r_aliases[ctr-1] = alias;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 		/*
9660Sstevel@tonic-gate 		 * only put comment in canonical entry
9670Sstevel@tonic-gate 		 */
9680Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
9690Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
9740Sstevel@tonic-gate 	if ((data.r_aliases = (char **)realloc(data.r_aliases,
9756842Sth160488 	    (ctr + 1) * sizeof (char **))) == NULL) {
9760Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
9770Sstevel@tonic-gate 		exit(1);
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 	data.r_aliases[ctr] = NULL;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	if (flags & F_VERBOSE)
9820Sstevel@tonic-gate 		(void) fprintf(stdout,
9830Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.r_name);
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
9880Sstevel@tonic-gate 		if (continue_onerror)
9890Sstevel@tonic-gate 			(void) fprintf(stderr,
9906842Sth160488 			    gettext("Entry: %s - already Exists,"
9916842Sth160488 			    " skipping it.\n"), data.r_name);
9920Sstevel@tonic-gate 		else {
9930Sstevel@tonic-gate 			rc = GENENT_CBERR;
9940Sstevel@tonic-gate 			(void) fprintf(stderr,
9956842Sth160488 			    gettext("Entry: %s - already Exists\n"),
9966842Sth160488 			    data.r_name);
9970Sstevel@tonic-gate 		}
9980Sstevel@tonic-gate 	} else if (retval)
9990Sstevel@tonic-gate 		rc = GENENT_CBERR;
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	free(data.r_name);
10020Sstevel@tonic-gate 	free(data.r_aliases);
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	return (rc);
10050Sstevel@tonic-gate }
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate static void
dump_rpc(ns_ldap_result_t * res)10100Sstevel@tonic-gate dump_rpc(ns_ldap_result_t *res)
10110Sstevel@tonic-gate {
10120Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *rpcnumber = NULL;
10130Sstevel@tonic-gate 	int		 i, j;
10140Sstevel@tonic-gate 	char		*name; /* rpc name */
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
10170Sstevel@tonic-gate 		return;
10180Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
10190Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
10200Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
10210Sstevel@tonic-gate 			cn = attrptr;
10220Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "oncRpcNumber") == 0)
10230Sstevel@tonic-gate 			rpcnumber = attrptr;
10240Sstevel@tonic-gate 	}
10250Sstevel@tonic-gate 	/* sanity check */
10260Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
10270Sstevel@tonic-gate 	    rpcnumber == NULL || rpcnumber->attrvalue == NULL ||
10280Sstevel@tonic-gate 	    rpcnumber->attrvalue[0] == NULL)
10290Sstevel@tonic-gate 		return;
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
10320Sstevel@tonic-gate 		return;
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	/* rpc name */
10350Sstevel@tonic-gate 	if (strlen(name) < 8)
10360Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t\t", name);
10370Sstevel@tonic-gate 	else
10380Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t", name);
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	/* rpc number */
10410Sstevel@tonic-gate 	(void) fprintf(stdout, "%-8s", rpcnumber->attrvalue[0]);
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 	/* aliases */
10450Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
10460Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
10470Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0)
10480Sstevel@tonic-gate 				/* skip rpc name */
10490Sstevel@tonic-gate 				continue;
10500Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
10510Sstevel@tonic-gate 		}
10520Sstevel@tonic-gate 	}
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	/* end of line */
10550Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate }
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate /*
10600Sstevel@tonic-gate  * /etc/protocols
10610Sstevel@tonic-gate  *
10620Sstevel@tonic-gate  */
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate static int
genent_protocols(char * line,int (* cback)())10650Sstevel@tonic-gate genent_protocols(char *line, int (*cback)())
10660Sstevel@tonic-gate {
10670Sstevel@tonic-gate 	char buf[BUFSIZ+1];
10680Sstevel@tonic-gate 	char *t;
10690Sstevel@tonic-gate 	entry_col ecol[4];
10700Sstevel@tonic-gate 	char *cname;
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	struct protoent	data;
10730Sstevel@tonic-gate 	char *alias;
10740Sstevel@tonic-gate 	int ctr = 0;
10750Sstevel@tonic-gate 	int retval = 1;
10760Sstevel@tonic-gate 	int rc = GENENT_OK;
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	/*
10790Sstevel@tonic-gate 	 * don't clobber our argument
10800Sstevel@tonic-gate 	 */
10810Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
10826842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
10836842Sth160488 		    PARSE_ERR_MSG_LEN);
10840Sstevel@tonic-gate 		return (GENENT_PARSEERR);
10850Sstevel@tonic-gate 	}
10860Sstevel@tonic-gate 	(void) strcpy(buf, line);
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	/*
10890Sstevel@tonic-gate 	 * clear column data
10900Sstevel@tonic-gate 	 */
10910Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	/*
10940Sstevel@tonic-gate 	 * comment (col 3)
10950Sstevel@tonic-gate 	 */
10960Sstevel@tonic-gate 	t = strchr(buf, '#');
10970Sstevel@tonic-gate 	if (t) {
10980Sstevel@tonic-gate 		*t++ = 0;
10990Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
11000Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
11010Sstevel@tonic-gate 	} else {
11020Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
11030Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
11040Sstevel@tonic-gate 	}
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	/*
11070Sstevel@tonic-gate 	 * cname(col 0)
11080Sstevel@tonic-gate 	 */
11090Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
11106842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no number"),
11116842Sth160488 		    PARSE_ERR_MSG_LEN);
11120Sstevel@tonic-gate 		return (GENENT_PARSEERR);
11130Sstevel@tonic-gate 	}
11140Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
11150Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
11160Sstevel@tonic-gate 	cname = t;
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	/*
11190Sstevel@tonic-gate 	 * number (col 2)
11200Sstevel@tonic-gate 	 */
11210Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
11226842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no number"),
11236842Sth160488 		    PARSE_ERR_MSG_LEN);
11240Sstevel@tonic-gate 		return (GENENT_PARSEERR);
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
11270Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	/*
11310Sstevel@tonic-gate 	 * build entry
11320Sstevel@tonic-gate 	 */
11330Sstevel@tonic-gate 	data.p_name = strdup(ecol[0].ec_value.ec_value_val);
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
11366842Sth160488 	    ecol[2].ec_value.ec_value_val[0] != '\0') {
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 		data.p_proto = ascii_to_int(ecol[2].ec_value.ec_value_val);
11390Sstevel@tonic-gate 		if (data.p_proto == -1) {
11400Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
11416842Sth160488 			    gettext("invalid protocol number: %s"),
11420Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
11430Sstevel@tonic-gate 		return (GENENT_PARSEERR);
11440Sstevel@tonic-gate 		}
11450Sstevel@tonic-gate 	} else
11460Sstevel@tonic-gate 		data.p_proto = -1;
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	/*
11490Sstevel@tonic-gate 	 * name (col 1)
11500Sstevel@tonic-gate 	 */
11510Sstevel@tonic-gate 	t = cname;
11520Sstevel@tonic-gate 	ctr = 0;
11530Sstevel@tonic-gate 	data.p_aliases = NULL;
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	do {
11560Sstevel@tonic-gate 		/*
11570Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
11580Sstevel@tonic-gate 		 */
11590Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
11600Sstevel@tonic-gate 			continue;
11610Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
11620Sstevel@tonic-gate 			continue;
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
11650Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 		ctr++;
11680Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
11690Sstevel@tonic-gate 		if ((data.p_aliases = (char **)realloc(data.p_aliases,
11706842Sth160488 		    ctr * sizeof (char **))) == NULL) {
11710Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
11720Sstevel@tonic-gate 			exit(1);
11730Sstevel@tonic-gate 		}
11740Sstevel@tonic-gate 		data.p_aliases[ctr-1] = alias;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 		/*
11770Sstevel@tonic-gate 		 * only put comment in canonical entry
11780Sstevel@tonic-gate 		 */
11790Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
11800Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
11850Sstevel@tonic-gate 	if ((data.p_aliases = (char **)realloc(data.p_aliases,
11866842Sth160488 	    (ctr + 1) * sizeof (char **))) == NULL) {
11870Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
11880Sstevel@tonic-gate 		exit(1);
11890Sstevel@tonic-gate 	}
11900Sstevel@tonic-gate 	data.p_aliases[ctr] = NULL;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	if (flags & F_VERBOSE)
11930Sstevel@tonic-gate 		(void) fprintf(stdout,
11940Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.p_name);
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
11990Sstevel@tonic-gate 		if (continue_onerror)
12000Sstevel@tonic-gate 			(void) fprintf(stderr,
12016842Sth160488 			    gettext("Entry: %s - already Exists,"
12026842Sth160488 			    " skipping it.\n"), data.p_name);
12030Sstevel@tonic-gate 		else {
12040Sstevel@tonic-gate 			rc = GENENT_CBERR;
12050Sstevel@tonic-gate 			(void) fprintf(stderr,
12066842Sth160488 			    gettext("Entry: %s - already Exists\n"),
12076842Sth160488 			    data.p_name);
12080Sstevel@tonic-gate 		}
12090Sstevel@tonic-gate 	} else if (retval)
12100Sstevel@tonic-gate 		rc = GENENT_CBERR;
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	free(data.p_name);
12130Sstevel@tonic-gate 	free(data.p_aliases);
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	return (rc);
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate static void
dump_protocols(ns_ldap_result_t * res)12200Sstevel@tonic-gate dump_protocols(ns_ldap_result_t *res)
12210Sstevel@tonic-gate {
12220Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *protocolnumber = NULL;
12230Sstevel@tonic-gate 	int		 i, j;
12240Sstevel@tonic-gate 	char		*name, *cp;
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
12270Sstevel@tonic-gate 		return;
12280Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
12290Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
12300Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
12310Sstevel@tonic-gate 			cn = attrptr;
12320Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "ipProtocolNumber")
12336842Sth160488 		    == 0)
12340Sstevel@tonic-gate 			protocolnumber = attrptr;
12350Sstevel@tonic-gate 	}
12360Sstevel@tonic-gate 	/* sanity check */
12370Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
12380Sstevel@tonic-gate 	    protocolnumber == NULL || protocolnumber->attrvalue == NULL ||
12390Sstevel@tonic-gate 	    protocolnumber->attrvalue[0] == NULL)
12400Sstevel@tonic-gate 		return;
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
12430Sstevel@tonic-gate 		return;
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 	/* protocol name */
12460Sstevel@tonic-gate 	if (strlen(name) < 8)
12470Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t\t", name);
12480Sstevel@tonic-gate 	else
12490Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t", name);
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	/* protocol number */
12520Sstevel@tonic-gate 	(void) fprintf(stdout, "%-16s", protocolnumber->attrvalue[0]);
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	/* aliases */
12550Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
12560Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
12570Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0) {
12580Sstevel@tonic-gate 				if (cn->value_count > 1)
12590Sstevel@tonic-gate 					/* Do not replicate */
12600Sstevel@tonic-gate 					continue;
12610Sstevel@tonic-gate 				/*
12620Sstevel@tonic-gate 				 * Replicate name in uppercase as an aliase
12630Sstevel@tonic-gate 				 */
12640Sstevel@tonic-gate 				for (cp = cn->attrvalue[j]; *cp; cp++)
12650Sstevel@tonic-gate 					*cp = toupper(*cp);
12660Sstevel@tonic-gate 			}
12670Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
12680Sstevel@tonic-gate 		}
12690Sstevel@tonic-gate 	}
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	/* end of line */
12720Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate }
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate /*
12810Sstevel@tonic-gate  * /etc/networks
12820Sstevel@tonic-gate  *
12830Sstevel@tonic-gate  */
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate static int
genent_networks(char * line,int (* cback)())12860Sstevel@tonic-gate genent_networks(char *line, int (*cback)())
12870Sstevel@tonic-gate {
12880Sstevel@tonic-gate 	char buf[BUFSIZ+1];
12890Sstevel@tonic-gate 	char *t;
12900Sstevel@tonic-gate 	entry_col ecol[4];
12910Sstevel@tonic-gate 	char *cname;
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	struct netent	data;
12940Sstevel@tonic-gate 	char *alias;
12950Sstevel@tonic-gate 	int ctr = 0;
12960Sstevel@tonic-gate 	int retval = 1;
12970Sstevel@tonic-gate 	int enet;
12980Sstevel@tonic-gate 	int rc = GENENT_OK;
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	/*
13010Sstevel@tonic-gate 	 * don't clobber our argument
13020Sstevel@tonic-gate 	 */
13030Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
13046842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
13056842Sth160488 		    PARSE_ERR_MSG_LEN);
13060Sstevel@tonic-gate 		return (GENENT_PARSEERR);
13070Sstevel@tonic-gate 	}
13080Sstevel@tonic-gate 	(void) strcpy(buf, line);
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	/*
13110Sstevel@tonic-gate 	 * clear column data
13120Sstevel@tonic-gate 	 */
13130Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	/*
13160Sstevel@tonic-gate 	 * comment (col 3)
13170Sstevel@tonic-gate 	 */
13180Sstevel@tonic-gate 	t = strchr(buf, '#');
13190Sstevel@tonic-gate 	if (t) {
13200Sstevel@tonic-gate 		*t++ = 0;
13210Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
13220Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
13230Sstevel@tonic-gate 	} else {
13240Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
13250Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
13260Sstevel@tonic-gate 	}
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	/*
13290Sstevel@tonic-gate 	 * cname(col 0)
13300Sstevel@tonic-gate 	 */
13310Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
13326842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no number"),
13336842Sth160488 		    PARSE_ERR_MSG_LEN);
13340Sstevel@tonic-gate 		return (GENENT_PARSEERR);
13350Sstevel@tonic-gate 	}
13360Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
13370Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
13380Sstevel@tonic-gate 	cname = t;
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	/*
13410Sstevel@tonic-gate 	 * number (col 2)
13420Sstevel@tonic-gate 	 */
13430Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
13446842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no number"),
13456842Sth160488 		    PARSE_ERR_MSG_LEN);
13460Sstevel@tonic-gate 		return (GENENT_PARSEERR);
13470Sstevel@tonic-gate 	}
13480Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
13490Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	/*
13530Sstevel@tonic-gate 	 * build entry
13540Sstevel@tonic-gate 	 */
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	data.n_name = strdup(ecol[0].ec_value.ec_value_val);
13570Sstevel@tonic-gate 	/*
13580Sstevel@tonic-gate 	 * data.n_net is an unsigned field,
13590Sstevel@tonic-gate 	 * assign -1 to it, make no sense.
13600Sstevel@tonic-gate 	 * Use enet here to avoid lint warning.
13610Sstevel@tonic-gate 	 */
13620Sstevel@tonic-gate 	enet = encode_network(ecol[2].ec_value.ec_value_val);
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	if (enet == -1 && continue_onerror == 0) {
13650Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Invalid network number\n"));
13660Sstevel@tonic-gate 		if (continue_onerror == 0)
13670Sstevel@tonic-gate 			return (GENENT_CBERR);
13680Sstevel@tonic-gate 	} else
13690Sstevel@tonic-gate 		data.n_net = enet;
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 	/*
13720Sstevel@tonic-gate 	 * name (col 1)
13730Sstevel@tonic-gate 	 */
13740Sstevel@tonic-gate 	t = cname;
13750Sstevel@tonic-gate 	data.n_aliases = NULL;
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	do {
13780Sstevel@tonic-gate 		/*
13790Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
13800Sstevel@tonic-gate 		 */
13810Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
13820Sstevel@tonic-gate 			continue;
13830Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
13840Sstevel@tonic-gate 			continue;
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
13870Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 		ctr++;
13900Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
13910Sstevel@tonic-gate 		if ((data.n_aliases = (char **)realloc(data.n_aliases,
13926842Sth160488 		    ctr * sizeof (char **))) == NULL) {
13930Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
13940Sstevel@tonic-gate 			exit(1);
13950Sstevel@tonic-gate 		}
13960Sstevel@tonic-gate 		data.n_aliases[ctr-1] = alias;
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 		/*
13990Sstevel@tonic-gate 		 * only put comment in canonical entry
14000Sstevel@tonic-gate 		 */
14010Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
14020Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
14070Sstevel@tonic-gate 	if ((data.n_aliases = (char **)realloc(data.n_aliases,
14086842Sth160488 	    (ctr + 1) * sizeof (char **))) == NULL) {
14090Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
14100Sstevel@tonic-gate 		exit(1);
14110Sstevel@tonic-gate 	}
14120Sstevel@tonic-gate 	data.n_aliases[ctr] = NULL;
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	if (flags & F_VERBOSE)
14150Sstevel@tonic-gate 		(void) fprintf(stdout,
14160Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.n_name);
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
14210Sstevel@tonic-gate 		if (continue_onerror)
14220Sstevel@tonic-gate 			(void) fprintf(stderr,
14236842Sth160488 			    gettext("Entry: %s - already Exists,"
14246842Sth160488 			    " skipping it.\n"), data.n_name);
14250Sstevel@tonic-gate 		else {
14260Sstevel@tonic-gate 			rc = GENENT_CBERR;
14270Sstevel@tonic-gate 			(void) fprintf(stderr,
14286842Sth160488 			    gettext("Entry: %s - already Exists\n"),
14296842Sth160488 			    data.n_name);
14300Sstevel@tonic-gate 		}
14310Sstevel@tonic-gate 	} else if (retval)
14320Sstevel@tonic-gate 		rc = GENENT_CBERR;
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	free(data.n_name);
14350Sstevel@tonic-gate 	free(data.n_aliases);
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	return (rc);
14380Sstevel@tonic-gate }
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate static void
dump_networks(ns_ldap_result_t * res)14420Sstevel@tonic-gate dump_networks(ns_ldap_result_t *res)
14430Sstevel@tonic-gate {
14440Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *networknumber = NULL;
14450Sstevel@tonic-gate 	int		 i, j;
14460Sstevel@tonic-gate 	char		*name;
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
14490Sstevel@tonic-gate 		return;
14500Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
14510Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
14520Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
14530Sstevel@tonic-gate 			cn = attrptr;
14540Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "ipNetworkNumber")
14556842Sth160488 		    == 0)
14560Sstevel@tonic-gate 			networknumber = attrptr;
14570Sstevel@tonic-gate 	}
14580Sstevel@tonic-gate 	/* sanity check */
14590Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
14600Sstevel@tonic-gate 	    networknumber == NULL || networknumber->attrvalue == NULL ||
14610Sstevel@tonic-gate 	    networknumber->attrvalue[0] == NULL)
14620Sstevel@tonic-gate 		return;
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 	/*
14650Sstevel@tonic-gate 	 * cn can be a MUST attribute(RFC 2307) or MAY attribute(2307bis).
14660Sstevel@tonic-gate 	 * If the canonical name can not be found (2307bis), use the 1st
14670Sstevel@tonic-gate 	 * value as the official name.
14680Sstevel@tonic-gate 	 */
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	/* network name */
14710Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
14720Sstevel@tonic-gate 		name = cn->attrvalue[0];
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	if (strlen(name) < 8)
14750Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t\t", name);
14760Sstevel@tonic-gate 	else
14770Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t", name);
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	/* network number */
14800Sstevel@tonic-gate 	(void) fprintf(stdout, "%-16s", networknumber->attrvalue[0]);
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 	/* aliases */
14830Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
14840Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
14850Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0)
14860Sstevel@tonic-gate 				/* skip name */
14870Sstevel@tonic-gate 				continue;
14880Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
14890Sstevel@tonic-gate 		}
14900Sstevel@tonic-gate 	}
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 	/* end of line */
14930Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate }
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate /*
15010Sstevel@tonic-gate  * /etc/services
15020Sstevel@tonic-gate  *
15030Sstevel@tonic-gate  */
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate static int
genent_services(char * line,int (* cback)())15060Sstevel@tonic-gate genent_services(char *line, int (*cback)())
15070Sstevel@tonic-gate {
15080Sstevel@tonic-gate 	char buf[BUFSIZ+1];
15090Sstevel@tonic-gate 	char *t, *p;
15100Sstevel@tonic-gate 	entry_col ecol[5];
15110Sstevel@tonic-gate 	char *cname;
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	struct servent	data;
15140Sstevel@tonic-gate 	char *alias;
15150Sstevel@tonic-gate 	int ctr = 0;
15160Sstevel@tonic-gate 	int retval = 1;
15170Sstevel@tonic-gate 	int rc = GENENT_OK;
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 	/*
15200Sstevel@tonic-gate 	 * don't clobber our argument
15210Sstevel@tonic-gate 	 */
15220Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
15236842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
15246842Sth160488 		    PARSE_ERR_MSG_LEN);
15250Sstevel@tonic-gate 		return (GENENT_PARSEERR);
15260Sstevel@tonic-gate 	}
15270Sstevel@tonic-gate 	(void) strcpy(buf, line);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	/*
15300Sstevel@tonic-gate 	 * clear column data
15310Sstevel@tonic-gate 	 */
15320Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 	/*
15350Sstevel@tonic-gate 	 * comment (col 4)
15360Sstevel@tonic-gate 	 */
15370Sstevel@tonic-gate 	t = strchr(buf, '#');
15380Sstevel@tonic-gate 	if (t) {
15390Sstevel@tonic-gate 		*t++ = 0;
15400Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_val = t;
15410Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_len = strlen(t)+1;
15420Sstevel@tonic-gate 	} else {
15430Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_val = 0;
15440Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_len = 0;
15450Sstevel@tonic-gate 	}
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	/*
15480Sstevel@tonic-gate 	 * cname(col 0)
15490Sstevel@tonic-gate 	 */
15500Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
15516842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no port"),
15526842Sth160488 		    PARSE_ERR_MSG_LEN);
15530Sstevel@tonic-gate 		return (GENENT_PARSEERR);
15540Sstevel@tonic-gate 	}
15550Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
15560Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
15570Sstevel@tonic-gate 	cname = t;
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	/*
15600Sstevel@tonic-gate 	 * port (col 3)
15610Sstevel@tonic-gate 	 */
15620Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
15636842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no protocol"),
15646842Sth160488 		    PARSE_ERR_MSG_LEN);
15650Sstevel@tonic-gate 		return (GENENT_PARSEERR);
15660Sstevel@tonic-gate 	}
15670Sstevel@tonic-gate 	if ((p = strchr(t, '/')) == 0) {
15686842Sth160488 		(void) strlcpy(parse_err_msg, gettext("bad port/proto"),
15696842Sth160488 		    PARSE_ERR_MSG_LEN);
15700Sstevel@tonic-gate 		return (GENENT_PARSEERR);
15710Sstevel@tonic-gate 	}
15720Sstevel@tonic-gate 	*(p++) = 0;
15730Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
15740Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	/*
15770Sstevel@tonic-gate 	 * proto (col 2)
15780Sstevel@tonic-gate 	 */
15790Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = p;
15800Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(p)+1;
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 	/*
15840Sstevel@tonic-gate 	 * build entry
15850Sstevel@tonic-gate 	 */
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	data.s_name = strdup(ecol[0].ec_value.ec_value_val);
15880Sstevel@tonic-gate 	data.s_proto = strdup(ecol[2].ec_value.ec_value_val);
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate 	if (ecol[3].ec_value.ec_value_val != NULL &&
15916842Sth160488 	    ecol[3].ec_value.ec_value_val[0] != '\0') {
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 		data.s_port = ascii_to_int(ecol[3].ec_value.ec_value_val);
15940Sstevel@tonic-gate 		if (data.s_port == -1) {
15950Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
15966842Sth160488 			    gettext("invalid port number: %s"),
15970Sstevel@tonic-gate 			    ecol[3].ec_value.ec_value_val);
15980Sstevel@tonic-gate 		return (GENENT_PARSEERR);
15990Sstevel@tonic-gate 		}
16000Sstevel@tonic-gate 	} else
16010Sstevel@tonic-gate 		data.s_port = -1;
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 	/*
16040Sstevel@tonic-gate 	 * name (col 1)
16050Sstevel@tonic-gate 	 */
16060Sstevel@tonic-gate 	t = cname;
16070Sstevel@tonic-gate 	data.s_aliases = NULL;
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	do {
16100Sstevel@tonic-gate 		/*
16110Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
16120Sstevel@tonic-gate 		 */
16130Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
16140Sstevel@tonic-gate 			continue;
16150Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
16160Sstevel@tonic-gate 			continue;
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
16190Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 		ctr++;
16220Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
16230Sstevel@tonic-gate 		if ((data.s_aliases = (char **)realloc(data.s_aliases,
16246842Sth160488 		    ctr * sizeof (char **))) == NULL) {
16250Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
16260Sstevel@tonic-gate 			exit(1);
16270Sstevel@tonic-gate 		}
16280Sstevel@tonic-gate 		data.s_aliases[ctr-1] = alias;
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 		/*
16310Sstevel@tonic-gate 		 * only put comment in canonical entry
16320Sstevel@tonic-gate 		 */
16330Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_val = 0;
16340Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_len = 0;
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
16390Sstevel@tonic-gate 	if ((data.s_aliases = (char **)realloc(data.s_aliases,
16406842Sth160488 	    (ctr + 1) * sizeof (char **))) == NULL) {
16410Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
16420Sstevel@tonic-gate 		exit(1);
16430Sstevel@tonic-gate 	}
16440Sstevel@tonic-gate 	data.s_aliases[ctr] = NULL;
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	if (flags & F_VERBOSE)
16470Sstevel@tonic-gate 		(void) fprintf(stdout,
16480Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), line);
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
16530Sstevel@tonic-gate 		if (continue_onerror)
16540Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
16556842Sth160488 			    "Entry: cn=%s+ipServiceProtocol=%s"
16566842Sth160488 			    " already Exists, skipping it.\n"),
16576842Sth160488 			    data.s_name, data.s_proto);
16580Sstevel@tonic-gate 		else {
16590Sstevel@tonic-gate 			rc = GENENT_CBERR;
16600Sstevel@tonic-gate 			(void) fprintf(stderr,
16616842Sth160488 			    gettext("Entry: cn=%s+ipServiceProtocol=%s"
16626842Sth160488 			    " - already Exists\n"),
16636842Sth160488 			    data.s_name, data.s_proto);
16640Sstevel@tonic-gate 		}
16650Sstevel@tonic-gate 	} else if (retval)
16660Sstevel@tonic-gate 		rc = GENENT_CBERR;
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	free(data.s_name);
16690Sstevel@tonic-gate 	free(data.s_proto);
16700Sstevel@tonic-gate 	free(data.s_aliases);
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 	return (rc);
16730Sstevel@tonic-gate }
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate static void
dump_services(ns_ldap_result_t * res)16780Sstevel@tonic-gate dump_services(ns_ldap_result_t *res)
16790Sstevel@tonic-gate {
16800Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *port = NULL;
16810Sstevel@tonic-gate 	ns_ldap_attr_t	*protocol = NULL;
16820Sstevel@tonic-gate 	int		i, j, len;
16830Sstevel@tonic-gate 	char		*name; /* service name */
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 	/*
16860Sstevel@tonic-gate 	 * cn can have multiple values.(service name and its aliases)
16870Sstevel@tonic-gate 	 * In order to support RFC 2307, section 5.5, ipserviceprotocol  can
16880Sstevel@tonic-gate 	 * have multiple values too.
16890Sstevel@tonic-gate 	 * The output format should look like
16900Sstevel@tonic-gate 	 *
16910Sstevel@tonic-gate 	 * test		2345/udp mytest
16920Sstevel@tonic-gate 	 * test		2345/tcp mytest
16930Sstevel@tonic-gate 	 */
16940Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
16950Sstevel@tonic-gate 		return;
16960Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
16970Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
16980Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
16990Sstevel@tonic-gate 			cn = attrptr;
17000Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "ipServicePort") == 0)
17010Sstevel@tonic-gate 			port = attrptr;
17020Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname,
17036842Sth160488 		    "ipServiceProtocol") == 0)
17040Sstevel@tonic-gate 			protocol = attrptr;
17050Sstevel@tonic-gate 	}
17060Sstevel@tonic-gate 	/* sanity check */
17070Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
17080Sstevel@tonic-gate 	    port == NULL || port->attrvalue == NULL ||
17090Sstevel@tonic-gate 	    port->attrvalue[0] == NULL || protocol == NULL ||
17100Sstevel@tonic-gate 	    protocol->attrvalue == NULL || protocol->attrvalue[0] == NULL)
17110Sstevel@tonic-gate 		return;
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
17140Sstevel@tonic-gate 		return;
17150Sstevel@tonic-gate 	for (i = 0; i < protocol->value_count; i++) {
17160Sstevel@tonic-gate 		if (protocol->attrvalue[i] == NULL)
17170Sstevel@tonic-gate 			return;
17180Sstevel@tonic-gate 		/* service name */
17190Sstevel@tonic-gate 		(void) fprintf(stdout, "%-16s", name);
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 		/* port & protocol */
17220Sstevel@tonic-gate 		(void) fprintf(stdout, "%s/%s%n", port->attrvalue[0],
17236842Sth160488 		    protocol->attrvalue[i], &len);
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 		if (len < 8)
17260Sstevel@tonic-gate 			(void) fprintf(stdout, "\t\t");
17270Sstevel@tonic-gate 		else
17280Sstevel@tonic-gate 			(void) fprintf(stdout, "\t");
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 		/* aliases */
17310Sstevel@tonic-gate 		for (j = 0; j < cn->value_count; j++) {
17320Sstevel@tonic-gate 			if (cn->attrvalue[j]) {
17330Sstevel@tonic-gate 				if (strcasecmp(name, cn->attrvalue[j]) == 0)
17340Sstevel@tonic-gate 					/* skip service name */
17350Sstevel@tonic-gate 					continue;
17360Sstevel@tonic-gate 				(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
17370Sstevel@tonic-gate 			}
17380Sstevel@tonic-gate 		}
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 		/* end of line */
17410Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
17420Sstevel@tonic-gate 	}
17430Sstevel@tonic-gate }
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate /*
17470Sstevel@tonic-gate  * /etc/group
17480Sstevel@tonic-gate  */
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate static int
genent_group(char * line,int (* cback)())17510Sstevel@tonic-gate genent_group(char *line, int (*cback)())
17520Sstevel@tonic-gate {
17530Sstevel@tonic-gate 	char buf[BIGBUF+1];
17540Sstevel@tonic-gate 	char *s, *t;
17550Sstevel@tonic-gate 	entry_col ecol[5];
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 	struct group	data;
17580Sstevel@tonic-gate 	int ctr = 0;
17590Sstevel@tonic-gate 	int retval = 1;
17600Sstevel@tonic-gate 	int rc = GENENT_OK;
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	/*
17630Sstevel@tonic-gate 	 * don't clobber our argument
17640Sstevel@tonic-gate 	 */
17650Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
17666842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
17676842Sth160488 		    PARSE_ERR_MSG_LEN);
17680Sstevel@tonic-gate 		return (GENENT_PARSEERR);
17690Sstevel@tonic-gate 	}
17700Sstevel@tonic-gate 	(void) strcpy(buf, line);
17710Sstevel@tonic-gate 	t = buf;
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate 	/* ignore empty entries */
17740Sstevel@tonic-gate 	if (*t == '\0')
17750Sstevel@tonic-gate 		return (GENENT_OK);
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	/*
17780Sstevel@tonic-gate 	 * clear column data
17790Sstevel@tonic-gate 	 */
17800Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 	/*
17830Sstevel@tonic-gate 	 * name (col 0)
17840Sstevel@tonic-gate 	 */
17850Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
17866842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no passwd"),
17876842Sth160488 		    PARSE_ERR_MSG_LEN);
17880Sstevel@tonic-gate 		return (GENENT_PARSEERR);
17890Sstevel@tonic-gate 	}
17900Sstevel@tonic-gate 	*s++ = 0;
17910Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
17920Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
17930Sstevel@tonic-gate 	t = s;
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 	/*
17960Sstevel@tonic-gate 	 * passwd (col 1)
17970Sstevel@tonic-gate 	 */
17980Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
17996842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no gid"),
18006842Sth160488 		    PARSE_ERR_MSG_LEN);
18010Sstevel@tonic-gate 		return (GENENT_PARSEERR);
18020Sstevel@tonic-gate 	}
18030Sstevel@tonic-gate 	*s++ = 0;
18040Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
18050Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
18060Sstevel@tonic-gate 	t = s;
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 	/*
18100Sstevel@tonic-gate 	 * gid (col 2)
18110Sstevel@tonic-gate 	 */
18120Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0 || s == t) {
18136842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no members"),
18146842Sth160488 		    PARSE_ERR_MSG_LEN);
18150Sstevel@tonic-gate 		return (GENENT_PARSEERR);
18160Sstevel@tonic-gate 	}
18170Sstevel@tonic-gate 	*s++ = 0;
18180Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
18190Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
18200Sstevel@tonic-gate 	t = s;
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	/*
18230Sstevel@tonic-gate 	 * members (col 3)
18240Sstevel@tonic-gate 	 */
18250Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
18260Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate 	/*
18300Sstevel@tonic-gate 	 * build entry
18310Sstevel@tonic-gate 	 */
18320Sstevel@tonic-gate 	data.gr_name = strdup(ecol[0].ec_value.ec_value_val);
18330Sstevel@tonic-gate 	data.gr_passwd = strdup(ecol[1].ec_value.ec_value_val);
18340Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
18356842Sth160488 	    ecol[2].ec_value.ec_value_val[0] != '\0') {
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 		data.gr_gid = ascii_to_int(ecol[2].ec_value.ec_value_val);
18386842Sth160488 		if (data.gr_gid == (uid_t)-1) {
18390Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
18406842Sth160488 			    gettext("invalid group id: %s"),
18410Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
18420Sstevel@tonic-gate 		return (GENENT_PARSEERR);
18430Sstevel@tonic-gate 		}
18440Sstevel@tonic-gate 	} else
18456842Sth160488 		data.gr_gid = (uid_t)-1;
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 	data.gr_mem = NULL;
18480Sstevel@tonic-gate 
1849839Smj162486 	/* Compute maximum amount of members */
1850839Smj162486 	s = t;
1851839Smj162486 	while (s = strchr(s, ',')) {
1852839Smj162486 		s++;
1853839Smj162486 		ctr++;
1854839Smj162486 	}
1855839Smj162486 
1856839Smj162486 	/* Allocate memory for all members */
1857839Smj162486 	data.gr_mem = calloc(ctr + 2, sizeof (char **));
1858839Smj162486 	if (data.gr_mem == NULL) {
1859839Smj162486 		(void) fprintf(stderr, gettext("out of memory\n"));
1860839Smj162486 		exit(1);
1861839Smj162486 	}
1862839Smj162486 
1863839Smj162486 	ctr = 0;
18640Sstevel@tonic-gate 	while (s = strchr(t, ',')) {
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate 		*s++ = 0;
18670Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
18680Sstevel@tonic-gate 		t = s;
1869839Smj162486 		/* Send to server only non empty member names */
1870839Smj162486 		if (strlen(ecol[3].ec_value.ec_value_val) != 0)
1871839Smj162486 			data.gr_mem[ctr++] = ecol[3].ec_value.ec_value_val;
18720Sstevel@tonic-gate 	}
18730Sstevel@tonic-gate 
1874839Smj162486 	/* Send to server only non empty member names */
1875839Smj162486 	if (strlen(t) != 0)
1876839Smj162486 		data.gr_mem[ctr++] = t;
1877839Smj162486 
1878839Smj162486 	/* Array of members completed, finished by NULL, see calloc() */
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 	if (flags & F_VERBOSE)
18810Sstevel@tonic-gate 		(void) fprintf(stdout,
18820Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.gr_name);
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
18870Sstevel@tonic-gate 		if (continue_onerror)
18880Sstevel@tonic-gate 			(void) fprintf(stderr,
18896842Sth160488 			    gettext("Entry: %s - already Exists,"
18906842Sth160488 			    " skipping it.\n"), data.gr_name);
18910Sstevel@tonic-gate 		else {
18920Sstevel@tonic-gate 			rc = GENENT_CBERR;
18930Sstevel@tonic-gate 			(void) fprintf(stderr,
18946842Sth160488 			    gettext("Entry: %s - already Exists\n"),
18956842Sth160488 			    data.gr_name);
18960Sstevel@tonic-gate 		}
18970Sstevel@tonic-gate 	} else if (retval)
18980Sstevel@tonic-gate 		rc = GENENT_CBERR;
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	free(data.gr_name);
19010Sstevel@tonic-gate 	free(data.gr_passwd);
19020Sstevel@tonic-gate 	free(data.gr_mem);
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 	return (rc);
19050Sstevel@tonic-gate }
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate static void
dump_group(ns_ldap_result_t * res)19080Sstevel@tonic-gate dump_group(ns_ldap_result_t *res)
19090Sstevel@tonic-gate {
19100Sstevel@tonic-gate 	char    **value = NULL;
19110Sstevel@tonic-gate 	char	pnam[256];
19120Sstevel@tonic-gate 	int	attr_count = 0;
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
19150Sstevel@tonic-gate 	if (value && value[0])
19160Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
19170Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "userPassword");
19180Sstevel@tonic-gate 	if (value == NULL || value[0] == NULL)
19190Sstevel@tonic-gate 		(void) fprintf(stdout, "*:");
19200Sstevel@tonic-gate 	else {
19210Sstevel@tonic-gate 		(void) strcpy(pnam, value[0]);
19220Sstevel@tonic-gate 		if (strncasecmp(value[0], "{crypt}", 7) == 0)
19230Sstevel@tonic-gate 			(void) fprintf(stdout, "%s:", (pnam+7));
19240Sstevel@tonic-gate 		else
19250Sstevel@tonic-gate 			(void) fprintf(stdout, "*:");
19260Sstevel@tonic-gate 	}
19270Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "gidNumber");
19280Sstevel@tonic-gate 	if (value && value[0])
19290Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "memberUid");
19320Sstevel@tonic-gate 	if (value != NULL && value[0] != NULL) {
19330Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
19340Sstevel@tonic-gate 			if (value[attr_count+1] == NULL)
19350Sstevel@tonic-gate 				(void) fprintf(stdout, "%s", value[attr_count]);
19360Sstevel@tonic-gate 			else
19370Sstevel@tonic-gate 				(void) fprintf(stdout, "%s,",
19386842Sth160488 				    value[attr_count]);
19390Sstevel@tonic-gate 			attr_count++;
19400Sstevel@tonic-gate 		}
19410Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
19420Sstevel@tonic-gate 	}
19430Sstevel@tonic-gate 	else
19440Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
19450Sstevel@tonic-gate }
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate /*
19520Sstevel@tonic-gate  * /etc/ethers
19530Sstevel@tonic-gate  */
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate static int
genent_ethers(char * line,int (* cback)())19560Sstevel@tonic-gate genent_ethers(char *line, int (*cback)())
19570Sstevel@tonic-gate {
19580Sstevel@tonic-gate 	char buf[BUFSIZ+1];
19590Sstevel@tonic-gate 	char *t;
19600Sstevel@tonic-gate 	entry_col ecol[3];
19610Sstevel@tonic-gate 	int retval = 1;
19620Sstevel@tonic-gate 	struct _ns_ethers	data;
19630Sstevel@tonic-gate 	int rc = GENENT_OK;
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	/*
19660Sstevel@tonic-gate 	 * don't clobber our argument
19670Sstevel@tonic-gate 	 */
19680Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
19696842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
19706842Sth160488 		    PARSE_ERR_MSG_LEN);
19710Sstevel@tonic-gate 		return (GENENT_PARSEERR);
19720Sstevel@tonic-gate 	}
19730Sstevel@tonic-gate 	(void) strcpy(buf, line);
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 	/*
19760Sstevel@tonic-gate 	 * clear column data
19770Sstevel@tonic-gate 	 */
19780Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate 	/*
19810Sstevel@tonic-gate 	 * comment (col 2)
19820Sstevel@tonic-gate 	 */
19830Sstevel@tonic-gate 	t = strchr(buf, '#');
19840Sstevel@tonic-gate 	if (t) {
19850Sstevel@tonic-gate 		*t++ = 0;
19860Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = t;
19870Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = strlen(t)+1;
19880Sstevel@tonic-gate 	} else {
19890Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = 0;
19900Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = 0;
19910Sstevel@tonic-gate 	}
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 	/*
19940Sstevel@tonic-gate 	 * addr(col 0)
19950Sstevel@tonic-gate 	 */
19960Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
19976842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no name"),
19986842Sth160488 		    PARSE_ERR_MSG_LEN);
19990Sstevel@tonic-gate 		return (GENENT_PARSEERR);
20000Sstevel@tonic-gate 	}
20010Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
20020Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 	/*
20050Sstevel@tonic-gate 	 * name(col 1)
20060Sstevel@tonic-gate 	 */
20070Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
20086842Sth160488 		(void) strlcpy(parse_err_msg,
20096842Sth160488 		    gettext("no white space allowed in name"),
20106842Sth160488 		    PARSE_ERR_MSG_LEN);
20110Sstevel@tonic-gate 		return (GENENT_PARSEERR);
20120Sstevel@tonic-gate 	}
20130Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
20140Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	/*
20180Sstevel@tonic-gate 	 * build entry
20190Sstevel@tonic-gate 	 */
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 	data.ether = strdup(ecol[0].ec_value.ec_value_val);
20220Sstevel@tonic-gate 	data.name  = strdup(ecol[1].ec_value.ec_value_val);
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate 	if (flags & F_VERBOSE)
20260Sstevel@tonic-gate 		(void) fprintf(stdout,
20270Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.name);
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
20320Sstevel@tonic-gate 		if (continue_onerror)
20330Sstevel@tonic-gate 			(void) fprintf(stderr,
20346842Sth160488 			    gettext("Entry: %s - already Exists,"
20356842Sth160488 			    " skipping it.\n"), data.name);
20360Sstevel@tonic-gate 		else {
20370Sstevel@tonic-gate 			rc = GENENT_CBERR;
20380Sstevel@tonic-gate 			(void) fprintf(stderr,
20396842Sth160488 			    gettext("Entry: %s - already Exists\n"),
20406842Sth160488 			    data.name);
20410Sstevel@tonic-gate 		}
20420Sstevel@tonic-gate 	} else if (retval)
20430Sstevel@tonic-gate 		rc = GENENT_CBERR;
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 	free(data.ether);
20460Sstevel@tonic-gate 	free(data.name);
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 	return (rc);
20490Sstevel@tonic-gate }
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 
20520Sstevel@tonic-gate static void
dump_ethers(ns_ldap_result_t * res)20530Sstevel@tonic-gate dump_ethers(ns_ldap_result_t *res)
20540Sstevel@tonic-gate {
20550Sstevel@tonic-gate 	char	**value = NULL;
20560Sstevel@tonic-gate 
20570Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "macAddress");
20580Sstevel@tonic-gate 	if (value && value[0])
20590Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
20600Sstevel@tonic-gate 	else
20610Sstevel@tonic-gate 		return;
20620Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
20630Sstevel@tonic-gate 	if (value && value[0])
20640Sstevel@tonic-gate 		(void) fprintf(stdout, "	%s\n", value[0]);
20650Sstevel@tonic-gate }
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate static int
genent_aliases(char * line,int (* cback)())20680Sstevel@tonic-gate genent_aliases(char *line, int (*cback)())
20690Sstevel@tonic-gate {
20700Sstevel@tonic-gate 	char buf[BUFSIZ+1];
20710Sstevel@tonic-gate 	char *t, *aliases;
20720Sstevel@tonic-gate 	char *cname;
20730Sstevel@tonic-gate 	int ctr = 0;
20740Sstevel@tonic-gate 	int retval = 1;
20750Sstevel@tonic-gate 	int i;
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 	struct _ns_alias data;
20780Sstevel@tonic-gate 	char *alias;
20790Sstevel@tonic-gate 	int rc = GENENT_OK;
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 	/*
20820Sstevel@tonic-gate 	 * don't clobber our argument
20830Sstevel@tonic-gate 	 */
20840Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
20856842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
20866842Sth160488 		    PARSE_ERR_MSG_LEN);
20870Sstevel@tonic-gate 		return (GENENT_PARSEERR);
20880Sstevel@tonic-gate 	}
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 	(void) strcpy(buf, line);
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 	if ((t = strchr(buf, ':')) == 0) {
20936842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no alias name"),
20946842Sth160488 		    PARSE_ERR_MSG_LEN);
20950Sstevel@tonic-gate 		return (GENENT_PARSEERR);
20960Sstevel@tonic-gate 	}
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 	t[0] = '\0';
20990Sstevel@tonic-gate 	if (++t == '\0') {
21006842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no alias value"),
21016842Sth160488 		    PARSE_ERR_MSG_LEN);
21020Sstevel@tonic-gate 		return (GENENT_PARSEERR);
21030Sstevel@tonic-gate 	}
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	cname = buf;
21060Sstevel@tonic-gate 	aliases = t;
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 	/* build entry */
21090Sstevel@tonic-gate 	data.alias = strdup(cname);
21100Sstevel@tonic-gate 	if (!data.alias) {
21110Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
21120Sstevel@tonic-gate 		exit(1);
21130Sstevel@tonic-gate 	}
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate 	data.member = NULL;
21160Sstevel@tonic-gate 	t = strtok(aliases, ",");
21170Sstevel@tonic-gate 	do {
21180Sstevel@tonic-gate 		ctr++;
21190Sstevel@tonic-gate 		while (t[0] == ' ')
21200Sstevel@tonic-gate 			t++;
21210Sstevel@tonic-gate 		alias = strdup(t);
21220Sstevel@tonic-gate 		if ((alias == NULL) ||
21236842Sth160488 		    ((data.member = (char **)realloc(data.member,
21246842Sth160488 		    (ctr + 1) * sizeof (char **))) == NULL)) {
21250Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
21260Sstevel@tonic-gate 			exit(1);
21270Sstevel@tonic-gate 		}
21280Sstevel@tonic-gate 		data.member[ctr-1] = alias;
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 	} while (t = strtok(NULL, ","));
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 	data.member[ctr] = NULL;
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 	if (flags & F_VERBOSE)
21350Sstevel@tonic-gate 		(void) fprintf(stdout,
21360Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.alias);
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
21410Sstevel@tonic-gate 		if (continue_onerror)
21420Sstevel@tonic-gate 			(void) fprintf(stderr,
21436842Sth160488 			    gettext("Entry: %s - already Exists,"
21446842Sth160488 			    " skipping it.\n"), data.alias);
21450Sstevel@tonic-gate 		else {
21460Sstevel@tonic-gate 			rc = GENENT_CBERR;
21470Sstevel@tonic-gate 			(void) fprintf(stderr,
21486842Sth160488 			    gettext("Entry: %s - already Exists\n"),
21496842Sth160488 			    data.alias);
21500Sstevel@tonic-gate 		}
21510Sstevel@tonic-gate 	} else if (retval)
21520Sstevel@tonic-gate 		rc = GENENT_CBERR;
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate 	free(data.alias);
21550Sstevel@tonic-gate 	i = 0;
21560Sstevel@tonic-gate 	while (data.member[i])
21570Sstevel@tonic-gate 		free(data.member[i++]);
21580Sstevel@tonic-gate 	free(data.member);
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate 	return (rc);
21610Sstevel@tonic-gate }
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate static void
dump_aliases(ns_ldap_result_t * res)21650Sstevel@tonic-gate dump_aliases(ns_ldap_result_t *res)
21660Sstevel@tonic-gate {
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 	char	**value = NULL;
21690Sstevel@tonic-gate 	int 		attr_count = 0;
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "mail");
21720Sstevel@tonic-gate 	if (value && value[0])
21730Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
21740Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "mgrpRFC822MailMember");
21750Sstevel@tonic-gate 	if (value != NULL)
21760Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
21770Sstevel@tonic-gate 			(void) fprintf(stdout, "%s,", value[attr_count]);
21780Sstevel@tonic-gate 			attr_count++;
21790Sstevel@tonic-gate 		}
21800Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate }
21830Sstevel@tonic-gate 
21840Sstevel@tonic-gate /*
21850Sstevel@tonic-gate  * /etc/publickey
21860Sstevel@tonic-gate  */
21870Sstevel@tonic-gate 
21882830Sdjl static char *h_errno2str(int h_errno);
21892830Sdjl 
21900Sstevel@tonic-gate static int
genent_publickey(char * line,int (* cback)())21910Sstevel@tonic-gate genent_publickey(char *line, int (*cback)())
21920Sstevel@tonic-gate {
21930Sstevel@tonic-gate 	char buf[BUFSIZ+1], tmpbuf[BUFSIZ+1], cname[BUFSIZ+1];
21940Sstevel@tonic-gate 	char *t, *p, *tmppubkey, *tmpprivkey;
21950Sstevel@tonic-gate 	entry_col ecol[3];
21962830Sdjl 	int buflen, uid, retval = 1, errnum = 0;
21970Sstevel@tonic-gate 	struct passwd *pwd;
21982830Sdjl 	char auth_type[BUFSIZ+1], *dot;
21990Sstevel@tonic-gate 	keylen_t keylen;
22000Sstevel@tonic-gate 	algtype_t algtype;
22010Sstevel@tonic-gate 	struct _ns_pubkey data;
22020Sstevel@tonic-gate 	struct hostent *hp;
22030Sstevel@tonic-gate 	struct in_addr in;
22042830Sdjl 	struct in6_addr in6;
22052830Sdjl 	char abuf[INET6_ADDRSTRLEN];
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 	/*
22080Sstevel@tonic-gate 	 * don't clobber our argument
22090Sstevel@tonic-gate 	 */
22100Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
22116842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
22126842Sth160488 		    PARSE_ERR_MSG_LEN);
22130Sstevel@tonic-gate 		return (GENENT_PARSEERR);
22140Sstevel@tonic-gate 	}
22150Sstevel@tonic-gate 	(void) strcpy(buf, line);
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 	/*
22180Sstevel@tonic-gate 	 * clear column data
22190Sstevel@tonic-gate 	 */
22200Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
22236842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no cname"),
22246842Sth160488 		    PARSE_ERR_MSG_LEN);
22250Sstevel@tonic-gate 		return (GENENT_PARSEERR);
22260Sstevel@tonic-gate 	}
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate 	/*
22290Sstevel@tonic-gate 	 * Special case:  /etc/publickey usually has an entry
22300Sstevel@tonic-gate 	 * for principal "nobody".  We skip it.
22310Sstevel@tonic-gate 	 */
22320Sstevel@tonic-gate 	if (strcmp(t, "nobody") == 0)
22330Sstevel@tonic-gate 		return (GENENT_OK);
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate 	/*
22360Sstevel@tonic-gate 	 * cname (col 0)
22370Sstevel@tonic-gate 	 */
22380Sstevel@tonic-gate 	if (strncmp(t, "unix.", 5)) {
22396842Sth160488 		(void) strlcpy(parse_err_msg, gettext("bad cname"),
22406842Sth160488 		    PARSE_ERR_MSG_LEN);
22410Sstevel@tonic-gate 		return (GENENT_PARSEERR);
22420Sstevel@tonic-gate 	}
22430Sstevel@tonic-gate 	(void) strcpy(tmpbuf, &(t[5]));
22440Sstevel@tonic-gate 	if ((p = strchr(tmpbuf, '@')) == 0) {
22456842Sth160488 		(void) strlcpy(parse_err_msg, gettext("bad cname"),
22466842Sth160488 		    PARSE_ERR_MSG_LEN);
22470Sstevel@tonic-gate 		return (GENENT_PARSEERR);
22480Sstevel@tonic-gate 	}
22490Sstevel@tonic-gate 	*(p++) = 0;
22500Sstevel@tonic-gate 	if (isdigit(*tmpbuf)) {
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 		uid = atoi(tmpbuf);
22530Sstevel@tonic-gate 		/*
22540Sstevel@tonic-gate 		 * don't generate entries for uids without passwd entries
22550Sstevel@tonic-gate 		 */
22560Sstevel@tonic-gate 		if ((pwd = getpwuid(uid)) == 0) {
22570Sstevel@tonic-gate 			(void) fprintf(stderr,
22580Sstevel@tonic-gate 			gettext("can't map uid %d to username, skipping\n"),
22596842Sth160488 			    uid);
22600Sstevel@tonic-gate 			return (GENENT_OK);
22610Sstevel@tonic-gate 		}
22620Sstevel@tonic-gate 		(void) strcpy(cname, pwd->pw_name);
22630Sstevel@tonic-gate 		data.hostcred = NS_HOSTCRED_FALSE;
22640Sstevel@tonic-gate 	} else {
22652830Sdjl 		if ((hp = getipnodebyname(tmpbuf, AF_INET6,
22666842Sth160488 		    AI_ALL | AI_V4MAPPED, &errnum)) == NULL) {
22670Sstevel@tonic-gate 			(void) fprintf(stderr,
22686842Sth160488 			    gettext("can't map hostname %s to hostaddress, "
22696842Sth160488 			    "errnum %d %s skipping\n"), tmpbuf, errnum,
22706842Sth160488 			    h_errno2str(errnum));
22710Sstevel@tonic-gate 			return (GENENT_OK);
22720Sstevel@tonic-gate 		}
22732830Sdjl 		(void) memcpy((char *)&in6.s6_addr, hp->h_addr_list[0],
22746842Sth160488 		    hp->h_length);
22752830Sdjl 		if (IN6_IS_ADDR_V4MAPPED(&in6) ||
22766842Sth160488 		    IN6_IS_ADDR_V4COMPAT(&in6)) {
22772830Sdjl 			IN6_V4MAPPED_TO_INADDR(&in6, &in);
22782830Sdjl 			if (inet_ntop(AF_INET, (const void *)&in, abuf,
22796842Sth160488 			    INET6_ADDRSTRLEN) == NULL) {
22802830Sdjl 				(void) fprintf(stderr,
22816842Sth160488 				    gettext("can't convert IPV4 address of"
22826842Sth160488 				    " hostname %s to string, "
22836842Sth160488 				    "skipping\n"), tmpbuf);
22842830Sdjl 					return (GENENT_OK);
22852830Sdjl 			}
22862830Sdjl 		} else {
22872830Sdjl 			if (inet_ntop(AF_INET6, (const void *)&in6, abuf,
22886842Sth160488 			    INET6_ADDRSTRLEN) == NULL) {
22892830Sdjl 				(void) fprintf(stderr,
22906842Sth160488 				    gettext("can't convert IPV6 address of"
22916842Sth160488 				    " hostname %s to string, "
22926842Sth160488 				    "skipping\n"), tmpbuf);
22932830Sdjl 					return (GENENT_OK);
22942830Sdjl 			}
22952830Sdjl 		}
22960Sstevel@tonic-gate 		data.hostcred = NS_HOSTCRED_TRUE;
22972830Sdjl 		/*
22982830Sdjl 		 * tmpbuf could be an alias, use hp->h_name instead.
22992830Sdjl 		 * hp->h_name is in FQDN format, so extract 1st field.
23002830Sdjl 		 */
23012830Sdjl 		if ((dot = strchr(hp->h_name, '.')) != NULL)
23022830Sdjl 			*dot = '\0';
23030Sstevel@tonic-gate 		(void) snprintf(cname, sizeof (cname),
23042830Sdjl 		    "%s+ipHostNumber=%s", hp->h_name, abuf);
23052830Sdjl 		if (dot)
23062830Sdjl 			*dot = '.';
23070Sstevel@tonic-gate 	}
23080Sstevel@tonic-gate 
23090Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = cname;
23100Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(cname)+1;
23110Sstevel@tonic-gate 
23120Sstevel@tonic-gate 	/*
23130Sstevel@tonic-gate 	 * public_data (col 1)
23140Sstevel@tonic-gate 	 */
23150Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
23166842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no private_data"),
23176842Sth160488 		    PARSE_ERR_MSG_LEN);
23180Sstevel@tonic-gate 		return (GENENT_PARSEERR);
23190Sstevel@tonic-gate 	}
23200Sstevel@tonic-gate 	if ((p = strchr(t, ':')) == 0) {
23216842Sth160488 		(void) strlcpy(parse_err_msg, gettext("bad public_data"),
23226842Sth160488 		    PARSE_ERR_MSG_LEN);
23230Sstevel@tonic-gate 		return (GENENT_PARSEERR);
23240Sstevel@tonic-gate 	}
23250Sstevel@tonic-gate 	*(p++) = 0;
23260Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
23270Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
23280Sstevel@tonic-gate 	keylen = (strlen(t) / 2) * 8;
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 	/*
23310Sstevel@tonic-gate 	 * private_data (col 2) and algtype extraction
23320Sstevel@tonic-gate 	 */
23330Sstevel@tonic-gate 	if (*p == ':')
23340Sstevel@tonic-gate 		p++;
23350Sstevel@tonic-gate 	t = p;
23360Sstevel@tonic-gate 	if (!(t = strchr(t, ':'))) {
23370Sstevel@tonic-gate 		(void) fprintf(stderr,
23386842Sth160488 		    gettext("WARNING: No algorithm type data found "
23396842Sth160488 		    "in publickey file, assuming 0\n"));
23400Sstevel@tonic-gate 		algtype = 0;
23410Sstevel@tonic-gate 	} else {
23420Sstevel@tonic-gate 		*t = '\0';
23430Sstevel@tonic-gate 		t++;
23440Sstevel@tonic-gate 		algtype = atoi(t);
23450Sstevel@tonic-gate 	}
23460Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = p;
23470Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(p)+1;
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate 	/*
23500Sstevel@tonic-gate 	 * auth_type (col 1)
23510Sstevel@tonic-gate 	 */
23522830Sdjl 	if (AUTH_DES_KEY(keylen, algtype))
23532830Sdjl 		/*
23542830Sdjl 		 * {DES} and {DH192-0} means same thing.
23552830Sdjl 		 * However, nisplus uses "DES" and ldap uses "DH192-0"
23562830Sdjl 		 * internally.
23572830Sdjl 		 * See newkey(1M), __nis_mechalias2authtype() which is
23582830Sdjl 		 * called by __nis_keyalg2authtype() and getkey_ldap_g()
23592830Sdjl 		 */
23602830Sdjl 		(void) strlcpy(auth_type, "DH192-0", BUFSIZ+1);
23612830Sdjl 	else if (!(__nis_keyalg2authtype(keylen, algtype, auth_type,
23626842Sth160488 	    MECH_MAXATNAME))) {
23630Sstevel@tonic-gate 		(void) fprintf(stderr,
23646842Sth160488 		    gettext("Could not convert algorithm type to "
23656842Sth160488 		    "corresponding auth type string\n"));
23660Sstevel@tonic-gate 		return (GENENT_ERR);
23670Sstevel@tonic-gate 	}
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate 	/*
23700Sstevel@tonic-gate 	 * build entry
23710Sstevel@tonic-gate 	 */
23720Sstevel@tonic-gate 	data.name = strdup(ecol[0].ec_value.ec_value_val);
23730Sstevel@tonic-gate 	if (data.name == NULL) {
23740Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
23750Sstevel@tonic-gate 		exit(1);
23760Sstevel@tonic-gate 	}
23770Sstevel@tonic-gate 
23780Sstevel@tonic-gate 	buflen = sizeof (auth_type) + strlen(ecol[1].ec_value.ec_value_val) + 3;
23790Sstevel@tonic-gate 	if ((tmppubkey = (char *)malloc(buflen)) == NULL) {
23800Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
23810Sstevel@tonic-gate 		exit(1);
23820Sstevel@tonic-gate 	}
23830Sstevel@tonic-gate 	(void) snprintf(tmppubkey, buflen, "{%s}%s", auth_type,
23840Sstevel@tonic-gate 	    ecol[1].ec_value.ec_value_val);
23850Sstevel@tonic-gate 	data.pubkey = tmppubkey;
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate 	buflen = sizeof (auth_type) + strlen(ecol[2].ec_value.ec_value_val) + 3;
23880Sstevel@tonic-gate 	if ((tmpprivkey = (char *)malloc(buflen)) == NULL) {
23890Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
23900Sstevel@tonic-gate 		exit(1);
23910Sstevel@tonic-gate 	}
23920Sstevel@tonic-gate 
23930Sstevel@tonic-gate 	(void) snprintf(tmpprivkey, buflen, "{%s}%s", auth_type,
23940Sstevel@tonic-gate 	    ecol[2].ec_value.ec_value_val);
23950Sstevel@tonic-gate 	data.privkey = tmpprivkey;
23960Sstevel@tonic-gate 
23970Sstevel@tonic-gate 	retval = (*cback)(&data, 1);
23982277Sjs198686 	if (retval != NS_LDAP_SUCCESS) {
23992277Sjs198686 		if (retval == LDAP_NO_SUCH_OBJECT) {
24002277Sjs198686 			if (data.hostcred == NS_HOSTCRED_TRUE)
24012277Sjs198686 				(void) fprintf(stdout,
24026842Sth160488 				    gettext("Cannot add publickey entry"" (%s),"
24036842Sth160488 				    " add host entry first\n"),
24046842Sth160488 				    tmpbuf);
24052277Sjs198686 			else
24062277Sjs198686 				(void) fprintf(stdout,
24076842Sth160488 				    gettext("Cannot add publickey entry (%s), "
24086842Sth160488 				    "add passwd entry first\n"),
24096842Sth160488 				    data.name);
24102277Sjs198686 		}
24112277Sjs198686 		if (continue_onerror == 0)
24122277Sjs198686 			return (GENENT_CBERR);
24130Sstevel@tonic-gate 	}
24142277Sjs198686 
24152277Sjs198686 	free(data.name);
24162277Sjs198686 	free(data.pubkey);
24172277Sjs198686 	free(data.privkey);
24182277Sjs198686 	return (GENENT_OK);
24190Sstevel@tonic-gate }
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate static void
dump_publickey(ns_ldap_result_t * res,char * container)24220Sstevel@tonic-gate dump_publickey(ns_ldap_result_t *res, char *container)
24230Sstevel@tonic-gate {
24240Sstevel@tonic-gate 	char	**value = NULL;
24250Sstevel@tonic-gate 	char	buf[BUFSIZ];
24260Sstevel@tonic-gate 	char	domainname[BUFSIZ];
24270Sstevel@tonic-gate 	char	*pubptr, *prvptr;
24280Sstevel@tonic-gate 
24290Sstevel@tonic-gate 	if (res == NULL)
24300Sstevel@tonic-gate 		return;
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 	if (sysinfo(SI_SRPC_DOMAIN, domainname, BUFSIZ) < 0) {
24330Sstevel@tonic-gate 		(void) fprintf(stderr,
24346842Sth160488 		    gettext("could not obtain domainname\n"));
24350Sstevel@tonic-gate 		exit(1);
24360Sstevel@tonic-gate 	}
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate 	/*
24390Sstevel@tonic-gate 	 * Retrieve all the attributes, but don't print
24400Sstevel@tonic-gate 	 * until we have all the required ones.
24410Sstevel@tonic-gate 	 */
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate 	if (strcmp(container, "passwd") == 0)
24440Sstevel@tonic-gate 		value = __ns_ldap_getAttr(res->entry, "uidNumber");
24450Sstevel@tonic-gate 	else
24460Sstevel@tonic-gate 		value = __ns_ldap_getAttr(res->entry, "cn");
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate 	if (value && value[0])
24490Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "unix.%s@%s",
24500Sstevel@tonic-gate 		    value[0], domainname);
24510Sstevel@tonic-gate 	else
24520Sstevel@tonic-gate 		return;
24530Sstevel@tonic-gate 
24540Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "nisPublickey");
24550Sstevel@tonic-gate 	if (value != NULL && value[0] != NULL) {
24560Sstevel@tonic-gate 		if ((pubptr = strchr(value[0], '}')) == NULL)
24570Sstevel@tonic-gate 			return;
24580Sstevel@tonic-gate 	}
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "nisSecretkey");
24610Sstevel@tonic-gate 	if (value != NULL && value[0] != NULL)
24620Sstevel@tonic-gate 		if ((prvptr = strchr(value[0], '}')) == NULL)
24630Sstevel@tonic-gate 			return;
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate 	/* print the attributes, algorithm type is always 0 */
24660Sstevel@tonic-gate 	(void) fprintf(stdout, "%s	%s:%s:0\n", buf, ++pubptr, ++prvptr);
24670Sstevel@tonic-gate }
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate /*
24720Sstevel@tonic-gate  * /etc/netmasks
24730Sstevel@tonic-gate  */
24740Sstevel@tonic-gate 
24750Sstevel@tonic-gate static int
genent_netmasks(char * line,int (* cback)())24760Sstevel@tonic-gate genent_netmasks(char *line, int (*cback)())
24770Sstevel@tonic-gate {
24780Sstevel@tonic-gate 	char buf[BUFSIZ+1];
24790Sstevel@tonic-gate 	char *t;
24800Sstevel@tonic-gate 	entry_col ecol[3];
24812277Sjs198686 	int retval;
24820Sstevel@tonic-gate 
24830Sstevel@tonic-gate 	struct _ns_netmasks data;
24840Sstevel@tonic-gate 
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate 	/*
24870Sstevel@tonic-gate 	 * don't clobber our argument
24880Sstevel@tonic-gate 	 */
24890Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
24906842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
24916842Sth160488 		    PARSE_ERR_MSG_LEN);
24920Sstevel@tonic-gate 		return (GENENT_PARSEERR);
24930Sstevel@tonic-gate 	}
24940Sstevel@tonic-gate 	(void) strcpy(buf, line);
24950Sstevel@tonic-gate 
24960Sstevel@tonic-gate 	/*
24970Sstevel@tonic-gate 	 * clear column data
24980Sstevel@tonic-gate 	 */
24990Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
25000Sstevel@tonic-gate 
25010Sstevel@tonic-gate 	/*
25020Sstevel@tonic-gate 	 * comment (col 2)
25030Sstevel@tonic-gate 	 */
25040Sstevel@tonic-gate 	t = strchr(buf, '#');
25050Sstevel@tonic-gate 	if (t) {
25060Sstevel@tonic-gate 		*t++ = 0;
25070Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = t;
25080Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = strlen(t)+1;
25090Sstevel@tonic-gate 	} else {
25100Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = 0;
25110Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = 0;
25120Sstevel@tonic-gate 	}
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate 	/*
25150Sstevel@tonic-gate 	 * addr(col 0)
25160Sstevel@tonic-gate 	 */
25170Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
25186842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no mask"),
25196842Sth160488 		    PARSE_ERR_MSG_LEN);
25200Sstevel@tonic-gate 		return (GENENT_PARSEERR);
25210Sstevel@tonic-gate 	}
25220Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
25230Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
25240Sstevel@tonic-gate 
25250Sstevel@tonic-gate 	/*
25260Sstevel@tonic-gate 	 * mask (col 1)
25270Sstevel@tonic-gate 	 */
25280Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
25296842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no mask"),
25306842Sth160488 		    PARSE_ERR_MSG_LEN);
25310Sstevel@tonic-gate 		return (GENENT_PARSEERR);
25320Sstevel@tonic-gate 	}
25330Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
25340Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
25350Sstevel@tonic-gate 
25360Sstevel@tonic-gate 	/* build entry */
25370Sstevel@tonic-gate 	data.netnumber = ecol[0].ec_value.ec_value_val;
25380Sstevel@tonic-gate 	data.netmask = ecol[1].ec_value.ec_value_val;
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 	if (flags & F_VERBOSE)
25410Sstevel@tonic-gate 		(void) fprintf(stdout,
25420Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.netnumber);
25430Sstevel@tonic-gate 
25442277Sjs198686 	retval = (*cback)(&data, 1);
25452277Sjs198686 	if (retval != NS_LDAP_SUCCESS) {
25462277Sjs198686 		if (retval == LDAP_NO_SUCH_OBJECT)
25472277Sjs198686 			(void) fprintf(stdout,
25486842Sth160488 			    gettext("Cannot add netmask entry (%s), "
25496842Sth160488 			    "add network entry first\n"), data.netnumber);
25502277Sjs198686 		if (continue_onerror == 0)
25512277Sjs198686 			return (GENENT_CBERR);
25522277Sjs198686 	}
25530Sstevel@tonic-gate 
25540Sstevel@tonic-gate 	return (GENENT_OK);
25550Sstevel@tonic-gate }
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate static void
dump_netmasks(ns_ldap_result_t * res)25580Sstevel@tonic-gate dump_netmasks(ns_ldap_result_t *res)
25590Sstevel@tonic-gate {
25600Sstevel@tonic-gate 	char	**value = NULL;
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "ipNetworkNumber");
25630Sstevel@tonic-gate 	if (value && value[0])
25640Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
25650Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "ipNetmaskNumber");
25660Sstevel@tonic-gate 	if (value && value[0])
25670Sstevel@tonic-gate 		(void) fprintf(stdout, "	%s\n", value[0]);
25680Sstevel@tonic-gate }
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate /*
25720Sstevel@tonic-gate  * /etc/netgroup
25730Sstevel@tonic-gate  * column data format is:
25740Sstevel@tonic-gate  *    col 0: netgroup name (or cname)
25750Sstevel@tonic-gate  *    col 1: netgroup member, if this is a triplet
25760Sstevel@tonic-gate  *    col 2: netgroup member, if not a triplet
25770Sstevel@tonic-gate  *    col 3: comment
25780Sstevel@tonic-gate  */
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate static int
genent_netgroup(char * line,int (* cback)())25810Sstevel@tonic-gate genent_netgroup(char *line, int (*cback)())
25820Sstevel@tonic-gate {
25830Sstevel@tonic-gate 	char buf[BIGBUF+1];    /* netgroup entries tend to be big */
25840Sstevel@tonic-gate 	char *t;
25850Sstevel@tonic-gate 	char *cname = NULL;
25860Sstevel@tonic-gate 	entry_col ecol[4];
25870Sstevel@tonic-gate 	char *netg_tmp = NULL, *triplet_tmp = NULL;
25881603Svl199446 	int netgcount = 0, tripletcount = 0, retval = 1, i;
25890Sstevel@tonic-gate 	struct _ns_netgroups data;
25900Sstevel@tonic-gate 	int rc = GENENT_OK;
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate 	/* don't clobber our argument */
25930Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
25946842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
25956842Sth160488 		    PARSE_ERR_MSG_LEN);
25960Sstevel@tonic-gate 		return (GENENT_PARSEERR);
25970Sstevel@tonic-gate 	}
25980Sstevel@tonic-gate 	(void) strcpy(buf, line);
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate 	/* clear column data */
26010Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate 	/*
26040Sstevel@tonic-gate 	 * process 1st minimal entry, to validate that there is no
26050Sstevel@tonic-gate 	 * parsing error.
26060Sstevel@tonic-gate 	 * start with comment(col 3)
26070Sstevel@tonic-gate 	 */
26080Sstevel@tonic-gate 	t = strchr(buf, '#');
26090Sstevel@tonic-gate 	if (t) {
26100Sstevel@tonic-gate 		*t++ = 0;
26110Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
26120Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
26130Sstevel@tonic-gate 	} else {
26140Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = "";
26150Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
26160Sstevel@tonic-gate 	}
26170Sstevel@tonic-gate 
26180Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = NULL;
26190Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = NULL;
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate 	/* cname (col 0) */
26220Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
26236842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no cname"),
26246842Sth160488 		    PARSE_ERR_MSG_LEN);
26250Sstevel@tonic-gate 		return (GENENT_PARSEERR);
26260Sstevel@tonic-gate 	}
26271603Svl199446 
26280Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
26290Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
26300Sstevel@tonic-gate 	cname = t;
26310Sstevel@tonic-gate 
26320Sstevel@tonic-gate 	/* addr(col 1 and 2) */
26330Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
26346842Sth160488 		(void) strlcpy(parse_err_msg,
26356842Sth160488 		    gettext("no members for netgroup"), PARSE_ERR_MSG_LEN);
26360Sstevel@tonic-gate 		return (GENENT_PARSEERR);
26370Sstevel@tonic-gate 	}
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate 	if (*t == '(') {
26401603Svl199446 		/* if token starts with '(' it must be a valid triplet */
26411603Svl199446 		if (is_triplet(t)) {
26421603Svl199446 			ecol[1].ec_value.ec_value_val = t;
26431603Svl199446 			ecol[1].ec_value.ec_value_len = strlen(t)+1;
26441603Svl199446 		} else {
26456842Sth160488 			(void) strlcpy(parse_err_msg,
26466842Sth160488 			    gettext("invalid triplet"), PARSE_ERR_MSG_LEN);
26471603Svl199446 			return (GENENT_PARSEERR);
26481603Svl199446 		}
26490Sstevel@tonic-gate 	} else {
26500Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = t;
26510Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = strlen(t)+1;
26520Sstevel@tonic-gate 	}
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate 	/*
26550Sstevel@tonic-gate 	 * now build entry.
26560Sstevel@tonic-gate 	 * start by clearing entry data
26570Sstevel@tonic-gate 	 */
26580Sstevel@tonic-gate 	(void) memset((struct _ns_netgroups *)&data, 0, sizeof (data));
26590Sstevel@tonic-gate 
26600Sstevel@tonic-gate 	data.name = strdup(ecol[0].ec_value.ec_value_val);
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate 	if (ecol[1].ec_value.ec_value_val != NULL) {
26630Sstevel@tonic-gate 		if ((data.triplet = calloc(1, sizeof (char **))) == NULL) {
26640Sstevel@tonic-gate 				(void) fprintf(stderr,
26656842Sth160488 				    gettext("out of memory\n"));
26660Sstevel@tonic-gate 				exit(1);
26670Sstevel@tonic-gate 		}
26680Sstevel@tonic-gate 		data.triplet[tripletcount++] =
26690Sstevel@tonic-gate 		    strdup(ecol[1].ec_value.ec_value_val);
26700Sstevel@tonic-gate 	} else if (ecol[2].ec_value.ec_value_val != NULL) {
26710Sstevel@tonic-gate 			if ((data.netgroup = calloc(1, sizeof (char **)))
26720Sstevel@tonic-gate 			    == NULL) {
26730Sstevel@tonic-gate 					(void) fprintf(stderr,
26740Sstevel@tonic-gate 				    gettext("out of memory\n"));
26750Sstevel@tonic-gate 					exit(1);
26760Sstevel@tonic-gate 			}
26770Sstevel@tonic-gate 			data.netgroup[netgcount++] =
26780Sstevel@tonic-gate 			    strdup(ecol[2].ec_value.ec_value_val);
26790Sstevel@tonic-gate 	}
26800Sstevel@tonic-gate 
26810Sstevel@tonic-gate 	/*
26820Sstevel@tonic-gate 	 * we now have a valid entry (at least 1 netgroup name and
26830Sstevel@tonic-gate 	 * 1 netgroup member), proceed with the rest of the line
26840Sstevel@tonic-gate 	 */
26851603Svl199446 	while (rc == GENENT_OK && (t = strtok(NULL, " \t"))) {
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 		/* if next token is equal to netgroup name, ignore */
26880Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
26890Sstevel@tonic-gate 			continue;
26900Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
26910Sstevel@tonic-gate 			continue;
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate 		if (*t == '(') {
26941603Svl199446 			if (is_triplet(t)) {
26951603Svl199446 				/* skip a triplet if it is added already */
26961603Svl199446 				for (i = 0; i < tripletcount &&
26976842Sth160488 				    strcmp(t, data.triplet[i]); i++)
26981603Svl199446 					;
26991603Svl199446 				if (i < tripletcount)
27001603Svl199446 					continue;
27011603Svl199446 
27021603Svl199446 				tripletcount++;
27031603Svl199446 				triplet_tmp = strdup(t);
27041603Svl199446 				if ((data.triplet = (char **)realloc(
27056842Sth160488 				    data.triplet,
27066842Sth160488 				    tripletcount * sizeof (char **))) == NULL) {
27071603Svl199446 					(void) fprintf(stderr,
27086842Sth160488 					    gettext("out of memory\n"));
27091603Svl199446 					exit(1);
27101603Svl199446 				}
27111603Svl199446 				data.triplet[tripletcount-1] = triplet_tmp;
27121603Svl199446 			} else {
27136842Sth160488 				(void) strlcpy(parse_err_msg,
27146842Sth160488 				    gettext("invalid triplet"),
27156842Sth160488 				    PARSE_ERR_MSG_LEN);
27161603Svl199446 				rc = GENENT_PARSEERR;
27170Sstevel@tonic-gate 			}
27180Sstevel@tonic-gate 		} else {
27191603Svl199446 			/* skip a netgroup if it is added already */
27201603Svl199446 			for (i = 0; i < netgcount &&
27216842Sth160488 			    strcmp(t, data.netgroup[i]); i++)
27221603Svl199446 				;
27231603Svl199446 			if (i < netgcount)
27241603Svl199446 				continue;
27251603Svl199446 
27260Sstevel@tonic-gate 			netgcount++;
27270Sstevel@tonic-gate 			netg_tmp = strdup(t);
27280Sstevel@tonic-gate 			if ((data.netgroup = (char **)realloc(data.netgroup,
27296842Sth160488 			    netgcount * sizeof (char **))) == NULL) {
27300Sstevel@tonic-gate 				(void) fprintf(stderr,
27310Sstevel@tonic-gate 				gettext("out of memory\n"));
27320Sstevel@tonic-gate 				exit(1);
27330Sstevel@tonic-gate 			}
27340Sstevel@tonic-gate 			data.netgroup[netgcount-1] = netg_tmp;
27350Sstevel@tonic-gate 		}
27360Sstevel@tonic-gate 	}
27370Sstevel@tonic-gate 
27380Sstevel@tonic-gate 	/* End the list with NULL */
27390Sstevel@tonic-gate 	if ((data.triplet = (char **)realloc(data.triplet,
27406842Sth160488 	    (tripletcount + 1) * sizeof (char **))) == NULL) {
27410Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
27420Sstevel@tonic-gate 		exit(1);
27430Sstevel@tonic-gate 	}
27440Sstevel@tonic-gate 	data.triplet[tripletcount] = NULL;
27450Sstevel@tonic-gate 	if ((data.netgroup = (char **)realloc(data.netgroup,
27466842Sth160488 	    (netgcount + 1) * sizeof (char **))) == NULL) {
27470Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
27480Sstevel@tonic-gate 		exit(1);
27490Sstevel@tonic-gate 	}
27500Sstevel@tonic-gate 	data.netgroup[netgcount] = NULL;
27510Sstevel@tonic-gate 
27521603Svl199446 	if (rc == GENENT_OK) {
27531603Svl199446 		if (flags & F_VERBOSE)
27541603Svl199446 			(void) fprintf(stdout,
27551603Svl199446 			    gettext("Adding entry : %s\n"), data.name);
27561603Svl199446 
27571603Svl199446 		retval = (*cback)(&data, 0);
27581603Svl199446 
27591603Svl199446 		if (retval == LDAP_ALREADY_EXISTS) {
27601603Svl199446 			if (continue_onerror)
27611603Svl199446 				(void) fprintf(stderr, gettext(
27626842Sth160488 				    "Entry: %s - already Exists,"
27636842Sth160488 				    " skipping it.\n"), data.name);
27641603Svl199446 			else {
27651603Svl199446 				rc = GENENT_CBERR;
27661603Svl199446 				(void) fprintf(stderr,
27676842Sth160488 				    gettext("Entry: %s - already Exists\n"),
27686842Sth160488 				    data.name);
27691603Svl199446 			}
27701603Svl199446 		} else if (retval)
27710Sstevel@tonic-gate 			rc = GENENT_CBERR;
27721603Svl199446 	}
27731603Svl199446 
27741603Svl199446 	/* release memory allocated by strdup() */
27751603Svl199446 	for (i = 0; i < tripletcount; i++) {
27761603Svl199446 		free(data.triplet[i]);
27771603Svl199446 	}
27781603Svl199446 	for (i = 0; i < netgcount; i++) {
27791603Svl199446 		free(data.netgroup[i]);
27801603Svl199446 	}
27810Sstevel@tonic-gate 
27820Sstevel@tonic-gate 	free(data.name);
27830Sstevel@tonic-gate 	free(data.triplet);
27840Sstevel@tonic-gate 	free(data.netgroup);
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate 	return (rc);
27870Sstevel@tonic-gate }
27880Sstevel@tonic-gate 
27890Sstevel@tonic-gate static void
dump_netgroup(ns_ldap_result_t * res)27900Sstevel@tonic-gate dump_netgroup(ns_ldap_result_t *res)
27910Sstevel@tonic-gate {
27920Sstevel@tonic-gate 	char	**value = NULL;
27930Sstevel@tonic-gate 	int	attr_count = 0;
27940Sstevel@tonic-gate 
27950Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
27960Sstevel@tonic-gate 	if ((value != NULL) && (value[0] != NULL))
27970Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
27980Sstevel@tonic-gate 	else
27990Sstevel@tonic-gate 		return;
28000Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "nisNetgroupTriple");
28010Sstevel@tonic-gate 	if (value != NULL)
28020Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
28030Sstevel@tonic-gate 			(void) fprintf(stdout, " %s", value[attr_count]);
28040Sstevel@tonic-gate 			attr_count++;
28050Sstevel@tonic-gate 		}
28062300Sjs198686 	attr_count = 0;
28070Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "memberNisNetgroup");
28080Sstevel@tonic-gate 	if (value != NULL)
28090Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
28100Sstevel@tonic-gate 			(void) fprintf(stdout, " %s", value[attr_count]);
28110Sstevel@tonic-gate 			attr_count++;
28120Sstevel@tonic-gate 		}
28130Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
28140Sstevel@tonic-gate 
28150Sstevel@tonic-gate }
28160Sstevel@tonic-gate 
28170Sstevel@tonic-gate static int
genent_automount(char * line,int (* cback)())28180Sstevel@tonic-gate genent_automount(char *line, int (*cback)())
28190Sstevel@tonic-gate {
28200Sstevel@tonic-gate 	char buf[BUFSIZ+1];
28210Sstevel@tonic-gate 	char *t, *s;
28220Sstevel@tonic-gate 	entry_col ecol[2];
28230Sstevel@tonic-gate 	struct _ns_automount data;
28240Sstevel@tonic-gate 	int retval = 1;
28250Sstevel@tonic-gate 	int rc = GENENT_OK;
28260Sstevel@tonic-gate 
28270Sstevel@tonic-gate 	/*
28280Sstevel@tonic-gate 	 * don't clobber our argument
28290Sstevel@tonic-gate 	 */
28300Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
28316842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
28326842Sth160488 		    PARSE_ERR_MSG_LEN);
28330Sstevel@tonic-gate 		return (GENENT_PARSEERR);
28346842Sth160488 	}
28350Sstevel@tonic-gate 
28360Sstevel@tonic-gate 	/* replace every tabspace with single space */
28370Sstevel@tonic-gate 	replace_tab2space(line);
28380Sstevel@tonic-gate 	(void) strcpy(buf, line);
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 	/*
28410Sstevel@tonic-gate 	 * clear column data
28420Sstevel@tonic-gate 	 */
28430Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
28440Sstevel@tonic-gate 
28450Sstevel@tonic-gate 	/*
28460Sstevel@tonic-gate 	 * key (col 0)
28470Sstevel@tonic-gate 	 */
28480Sstevel@tonic-gate 	t = buf;
28490Sstevel@tonic-gate 	while (t[0] == ' ')
28500Sstevel@tonic-gate 		t++;
28510Sstevel@tonic-gate 
28520Sstevel@tonic-gate 	if ((s = strchr(t, ' ')) == 0) {
28530Sstevel@tonic-gate 		return (GENENT_PARSEERR);
28540Sstevel@tonic-gate 	}
28550Sstevel@tonic-gate 	*s++ = 0;
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
28580Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
28590Sstevel@tonic-gate 	t = s;
28600Sstevel@tonic-gate 
28610Sstevel@tonic-gate 	while (t[0] == ' ')
28620Sstevel@tonic-gate 		t++;
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate 	/*
28650Sstevel@tonic-gate 	 * mapentry (col 1)
28660Sstevel@tonic-gate 	 */
28670Sstevel@tonic-gate 
28680Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
28690Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
28700Sstevel@tonic-gate 
28710Sstevel@tonic-gate 	data.mapname = strdup(databasetype);
28720Sstevel@tonic-gate 	data.key = strdup(ecol[0].ec_value.ec_value_val);
28730Sstevel@tonic-gate 	data.value = strdup(ecol[1].ec_value.ec_value_val);
28740Sstevel@tonic-gate 
28750Sstevel@tonic-gate 	if (flags & F_VERBOSE)
28760Sstevel@tonic-gate 		(void) fprintf(stdout,
28770Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.key);
28780Sstevel@tonic-gate 
28790Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
28820Sstevel@tonic-gate 		if (continue_onerror)
28830Sstevel@tonic-gate 			(void) fprintf(stderr,
28846842Sth160488 			    gettext("Entry: %s - already Exists,"
28856842Sth160488 			    " skipping it.\n"), data.key);
28860Sstevel@tonic-gate 		else {
28870Sstevel@tonic-gate 			rc = GENENT_CBERR;
28880Sstevel@tonic-gate 			(void) fprintf(stderr,
28896842Sth160488 			    gettext("Entry: %s - already Exists\n"),
28906842Sth160488 			    data.key);
28910Sstevel@tonic-gate 		}
28920Sstevel@tonic-gate 	} else if (retval)
28930Sstevel@tonic-gate 		rc = GENENT_CBERR;
28940Sstevel@tonic-gate 
28950Sstevel@tonic-gate 	free(data.mapname);
28960Sstevel@tonic-gate 	free(data.key);
28970Sstevel@tonic-gate 	free(data.value);
28980Sstevel@tonic-gate 	return (rc);
28990Sstevel@tonic-gate }
29000Sstevel@tonic-gate 
29010Sstevel@tonic-gate static void
dump_automount(ns_ldap_result_t * res)29020Sstevel@tonic-gate dump_automount(ns_ldap_result_t *res)
29030Sstevel@tonic-gate {
29040Sstevel@tonic-gate 	char	**value = NULL;
29050Sstevel@tonic-gate 
29060Sstevel@tonic-gate 	if (res == NULL)
29070Sstevel@tonic-gate 		return;
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "automountKey");
29100Sstevel@tonic-gate 	if (value != NULL) {
29110Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
29120Sstevel@tonic-gate 		value = __ns_ldap_getAttr(res->entry, "automountInformation");
29130Sstevel@tonic-gate 		if (value != NULL)
29140Sstevel@tonic-gate 			(void) fprintf(stdout, "	%s\n", value[0]);
29150Sstevel@tonic-gate 		else
29160Sstevel@tonic-gate 			(void) fprintf(stdout, "\n");
29170Sstevel@tonic-gate 	}
29180Sstevel@tonic-gate }
29190Sstevel@tonic-gate 
29200Sstevel@tonic-gate 
29210Sstevel@tonic-gate /*
29220Sstevel@tonic-gate  * /etc/passwd
29230Sstevel@tonic-gate  *
29240Sstevel@tonic-gate  */
29250Sstevel@tonic-gate 
29260Sstevel@tonic-gate static int
genent_passwd(char * line,int (* cback)())29270Sstevel@tonic-gate genent_passwd(char *line, int (*cback)())
29280Sstevel@tonic-gate {
29290Sstevel@tonic-gate 	char buf[BUFSIZ+1];
29300Sstevel@tonic-gate 	char *s, *t;
29310Sstevel@tonic-gate 	entry_col ecol[8];
29320Sstevel@tonic-gate 	int retval = 1;
29330Sstevel@tonic-gate 	char pname[BUFSIZ];
29340Sstevel@tonic-gate 
29350Sstevel@tonic-gate 	struct passwd	data;
29360Sstevel@tonic-gate 	int rc = GENENT_OK;
29370Sstevel@tonic-gate 
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate 	/*
29400Sstevel@tonic-gate 	 * don't clobber our argument
29410Sstevel@tonic-gate 	 */
29420Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
29436842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
29446842Sth160488 		    PARSE_ERR_MSG_LEN);
29450Sstevel@tonic-gate 		return (GENENT_PARSEERR);
29460Sstevel@tonic-gate 	}
29470Sstevel@tonic-gate 	(void) strcpy(buf, line);
29480Sstevel@tonic-gate 	t = buf;
29490Sstevel@tonic-gate 
29500Sstevel@tonic-gate 	/* ignore empty entries */
29510Sstevel@tonic-gate 	if (*t == '\0')
29520Sstevel@tonic-gate 		return (GENENT_OK);
29530Sstevel@tonic-gate 
29540Sstevel@tonic-gate 	/*
29550Sstevel@tonic-gate 	 * clear column data
29560Sstevel@tonic-gate 	 */
29570Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
29580Sstevel@tonic-gate 
29590Sstevel@tonic-gate 	/*
29600Sstevel@tonic-gate 	 * name (col 0)
29610Sstevel@tonic-gate 	 */
29620Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
29636842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no password"),
29646842Sth160488 		    PARSE_ERR_MSG_LEN);
29650Sstevel@tonic-gate 		return (GENENT_PARSEERR);
29660Sstevel@tonic-gate 	}
29670Sstevel@tonic-gate 	*s++ = 0;
29680Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
29690Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
29700Sstevel@tonic-gate 	t = s;
29710Sstevel@tonic-gate 
29720Sstevel@tonic-gate 	/*
29730Sstevel@tonic-gate 	 * passwd (col 1)
29740Sstevel@tonic-gate 	 */
29750Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
29766842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no uid"),
29776842Sth160488 		    PARSE_ERR_MSG_LEN);
29780Sstevel@tonic-gate 		return (GENENT_PARSEERR);
29790Sstevel@tonic-gate 	}
29800Sstevel@tonic-gate 	*s++ = 0;
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
29830Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
29840Sstevel@tonic-gate 
29850Sstevel@tonic-gate 	t = s;
29860Sstevel@tonic-gate 
29870Sstevel@tonic-gate 	/*
29880Sstevel@tonic-gate 	 * uid (col 2)
29890Sstevel@tonic-gate 	 */
29900Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0 || s == t) {
29916842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no gid"),
29926842Sth160488 		    PARSE_ERR_MSG_LEN);
29930Sstevel@tonic-gate 		return (GENENT_PARSEERR);
29940Sstevel@tonic-gate 	}
29950Sstevel@tonic-gate 	*s++ = 0;
29960Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
29970Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
29980Sstevel@tonic-gate 	t = s;
29990Sstevel@tonic-gate 
30000Sstevel@tonic-gate 	/*
30010Sstevel@tonic-gate 	 * gid (col 3)
30020Sstevel@tonic-gate 	 */
30030Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0 || s == t) {
30046842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no gcos"),
30056842Sth160488 		    PARSE_ERR_MSG_LEN);
30060Sstevel@tonic-gate 		return (GENENT_PARSEERR);
30070Sstevel@tonic-gate 	}
30080Sstevel@tonic-gate 	*s++ = 0;
30090Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
30100Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
30110Sstevel@tonic-gate 	t = s;
30120Sstevel@tonic-gate 
30130Sstevel@tonic-gate 	/*
30140Sstevel@tonic-gate 	 * gcos (col 4)
30150Sstevel@tonic-gate 	 */
30160Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
30176842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no home"),
30186842Sth160488 		    PARSE_ERR_MSG_LEN);
30190Sstevel@tonic-gate 		return (GENENT_PARSEERR);
30200Sstevel@tonic-gate 	}
30210Sstevel@tonic-gate 	*s++ = 0;
30220Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_val = t;
30230Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_len = strlen(t)+1;
30240Sstevel@tonic-gate 	t = s;
30250Sstevel@tonic-gate 
30260Sstevel@tonic-gate 	/*
30270Sstevel@tonic-gate 	 * home (col 5)
30280Sstevel@tonic-gate 	 */
30290Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
30306842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no shell"),
30316842Sth160488 		    PARSE_ERR_MSG_LEN);
30320Sstevel@tonic-gate 		return (GENENT_PARSEERR);
30330Sstevel@tonic-gate 	}
30340Sstevel@tonic-gate 	*s++ = 0;
30350Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_val = t;
30360Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_len = strlen(t)+1;
30370Sstevel@tonic-gate 	t = s;
30380Sstevel@tonic-gate 
30390Sstevel@tonic-gate 	/*
30400Sstevel@tonic-gate 	 * shell (col 6)
30410Sstevel@tonic-gate 	 */
30420Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_val = t;
30430Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_len = strlen(t)+1;
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate 	/*
30460Sstevel@tonic-gate 	 * build entry
30470Sstevel@tonic-gate 	 */
30480Sstevel@tonic-gate 	data.pw_name = strdup(ecol[0].ec_value.ec_value_val);
30490Sstevel@tonic-gate 
30500Sstevel@tonic-gate 	if (flags & F_PASSWD) {
30510Sstevel@tonic-gate 		/* Add {crypt} before passwd entry */
30520Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname), "{crypt}%s",
30530Sstevel@tonic-gate 		    ecol[1].ec_value.ec_value_val);
30540Sstevel@tonic-gate 		data.pw_passwd = strdup(pname);
30550Sstevel@tonic-gate 	}
30560Sstevel@tonic-gate 	else
30570Sstevel@tonic-gate 		data.pw_passwd = NULL;
30580Sstevel@tonic-gate 
30590Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
30600Sstevel@tonic-gate 	    ecol[2].ec_value.ec_value_val[0] != '\0') {
30610Sstevel@tonic-gate 		data.pw_uid = ascii_to_int(ecol[2].ec_value.ec_value_val);
30626842Sth160488 		if (data.pw_uid == (uid_t)-1) {
30630Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
30646842Sth160488 			    gettext("invalid uid : %s"),
30656842Sth160488 			    ecol[2].ec_value.ec_value_val);
30660Sstevel@tonic-gate 		return (GENENT_PARSEERR);
30670Sstevel@tonic-gate 		}
30680Sstevel@tonic-gate 	} else
30696842Sth160488 		data.pw_uid = (uid_t)-1;
30700Sstevel@tonic-gate 
30710Sstevel@tonic-gate 	if (ecol[3].ec_value.ec_value_val != NULL &&
30726842Sth160488 	    ecol[3].ec_value.ec_value_val[0] != '\0') {
30730Sstevel@tonic-gate 
30740Sstevel@tonic-gate 		data.pw_gid = ascii_to_int(ecol[3].ec_value.ec_value_val);
30756842Sth160488 		if (data.pw_gid == (uid_t)-1) {
30760Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
30776842Sth160488 			    gettext("invalid gid : %s"),
30786842Sth160488 			    ecol[3].ec_value.ec_value_val);
30790Sstevel@tonic-gate 		return (GENENT_PARSEERR);
30800Sstevel@tonic-gate 		}
30810Sstevel@tonic-gate 	} else
30826842Sth160488 		data.pw_gid = (uid_t)-1;
30830Sstevel@tonic-gate 
30840Sstevel@tonic-gate 	data.pw_age = NULL;
30850Sstevel@tonic-gate 	data.pw_comment = NULL;
30860Sstevel@tonic-gate 	data.pw_gecos = strdup(ecol[4].ec_value.ec_value_val);
30870Sstevel@tonic-gate 	data.pw_dir = strdup(ecol[5].ec_value.ec_value_val);
30880Sstevel@tonic-gate 	data.pw_shell = strdup(ecol[6].ec_value.ec_value_val);
30890Sstevel@tonic-gate 
30900Sstevel@tonic-gate 	if (flags & F_VERBOSE)
30910Sstevel@tonic-gate 		(void) fprintf(stdout,
30920Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.pw_name);
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
30970Sstevel@tonic-gate 		if (continue_onerror)
30980Sstevel@tonic-gate 			(void) fprintf(stderr,
30996842Sth160488 			    gettext("Entry: %s - already Exists,"
31006842Sth160488 			    " skipping it.\n"), data.pw_name);
31010Sstevel@tonic-gate 		else {
31020Sstevel@tonic-gate 			rc = GENENT_CBERR;
31030Sstevel@tonic-gate 			(void) fprintf(stderr,
31046842Sth160488 			    gettext("Entry: %s - already Exists\n"),
31056842Sth160488 			    data.pw_name);
31060Sstevel@tonic-gate 		}
31070Sstevel@tonic-gate 	} else if (retval)
31080Sstevel@tonic-gate 		rc = GENENT_CBERR;
31090Sstevel@tonic-gate 
31100Sstevel@tonic-gate 	free(data.pw_name);
31110Sstevel@tonic-gate 	free(data.pw_gecos);
31120Sstevel@tonic-gate 	free(data.pw_dir);
31130Sstevel@tonic-gate 	free(data.pw_shell);
31140Sstevel@tonic-gate 	return (rc);
31150Sstevel@tonic-gate }
31160Sstevel@tonic-gate 
31170Sstevel@tonic-gate 
31180Sstevel@tonic-gate static void
dump_passwd(ns_ldap_result_t * res)31190Sstevel@tonic-gate dump_passwd(ns_ldap_result_t *res)
31200Sstevel@tonic-gate {
31210Sstevel@tonic-gate 	char    **value = NULL;
31220Sstevel@tonic-gate 
31230Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "uid");
31240Sstevel@tonic-gate 	if (value == NULL)
31250Sstevel@tonic-gate 		return;
31260Sstevel@tonic-gate 	else
31270Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
31280Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "userPassword");
31298821SMichen.Chang@Sun.COM 
31308821SMichen.Chang@Sun.COM 	/*
31318821SMichen.Chang@Sun.COM 	 * Don't print the encrypted password, Use x to
31328821SMichen.Chang@Sun.COM 	 * indicate it is in the shadow database.
31338821SMichen.Chang@Sun.COM 	 */
31348821SMichen.Chang@Sun.COM 	(void) fprintf(stdout, "x:");
31358821SMichen.Chang@Sun.COM 
31360Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "uidNumber");
31370Sstevel@tonic-gate 	if (value && value[0])
31380Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
31390Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "gidNumber");
31400Sstevel@tonic-gate 	if (value && value[0])
31410Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
31420Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "gecos");
31430Sstevel@tonic-gate 	if (value == NULL)
31440Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
31450Sstevel@tonic-gate 	else
31460Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
31470Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "homeDirectory");
31480Sstevel@tonic-gate 	if (value == NULL)
31490Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
31500Sstevel@tonic-gate 	else
31510Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
31520Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "loginShell");
31530Sstevel@tonic-gate 	if (value == NULL)
31540Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
31550Sstevel@tonic-gate 	else
31560Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\n", value[0]);
31570Sstevel@tonic-gate 
31580Sstevel@tonic-gate }
31590Sstevel@tonic-gate 
31600Sstevel@tonic-gate /*
31610Sstevel@tonic-gate  * /etc/shadow
31620Sstevel@tonic-gate  */
31630Sstevel@tonic-gate 
31640Sstevel@tonic-gate static int
genent_shadow(char * line,int (* cback)())31650Sstevel@tonic-gate genent_shadow(char *line, int (*cback)())
31660Sstevel@tonic-gate {
31670Sstevel@tonic-gate 	char buf[BUFSIZ+1];
31680Sstevel@tonic-gate 	char *s, *t;
31690Sstevel@tonic-gate 	entry_col ecol[9];
31700Sstevel@tonic-gate 	char pname[BUFSIZ];
31710Sstevel@tonic-gate 
31720Sstevel@tonic-gate 	struct spwd	data;
31730Sstevel@tonic-gate 	int spflag;
31742277Sjs198686 	int retval;
31750Sstevel@tonic-gate 
31760Sstevel@tonic-gate 
31770Sstevel@tonic-gate 	/*
31780Sstevel@tonic-gate 	 * don't clobber our argument
31790Sstevel@tonic-gate 	 */
31800Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
31816842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
31826842Sth160488 		    PARSE_ERR_MSG_LEN);
31830Sstevel@tonic-gate 		return (GENENT_PARSEERR);
31840Sstevel@tonic-gate 	}
31850Sstevel@tonic-gate 	(void) strcpy(buf, line);
31860Sstevel@tonic-gate 	t = buf;
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate 	/* ignore empty entries */
31890Sstevel@tonic-gate 	if (*t == '\0')
31900Sstevel@tonic-gate 		return (GENENT_OK);
31910Sstevel@tonic-gate 
31920Sstevel@tonic-gate 	/*
31930Sstevel@tonic-gate 	 * clear column data
31940Sstevel@tonic-gate 	 */
31950Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
31960Sstevel@tonic-gate 
31970Sstevel@tonic-gate 	/*
31980Sstevel@tonic-gate 	 * name (col 0)
31990Sstevel@tonic-gate 	 */
32000Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
32016842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no uid"),
32026842Sth160488 		    PARSE_ERR_MSG_LEN);
32030Sstevel@tonic-gate 		return (GENENT_PARSEERR);
32040Sstevel@tonic-gate 	}
32050Sstevel@tonic-gate 	*s++ = 0;
32060Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
32070Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
32080Sstevel@tonic-gate 	t = s;
32090Sstevel@tonic-gate 
32100Sstevel@tonic-gate 	/*
32110Sstevel@tonic-gate 	 * passwd (col 1)
32120Sstevel@tonic-gate 	 */
32130Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
32146842Sth160488 		(void) strlcpy(parse_err_msg, gettext("Improper format"),
32156842Sth160488 		    PARSE_ERR_MSG_LEN);
32160Sstevel@tonic-gate 		return (GENENT_PARSEERR);
32170Sstevel@tonic-gate 	}
32180Sstevel@tonic-gate 	*s++ = 0;
32190Sstevel@tonic-gate 
32200Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
32210Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
32220Sstevel@tonic-gate 
32230Sstevel@tonic-gate 	t = s;
32240Sstevel@tonic-gate 
32250Sstevel@tonic-gate 	/*
32260Sstevel@tonic-gate 	 * shadow last change (col 2)
32270Sstevel@tonic-gate 	 */
32280Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
32296842Sth160488 		(void) strlcpy(parse_err_msg, gettext("Improper format"),
32306842Sth160488 		    PARSE_ERR_MSG_LEN);
32310Sstevel@tonic-gate 		return (GENENT_PARSEERR);
32320Sstevel@tonic-gate 	}
32330Sstevel@tonic-gate 	*s++ = 0;
32340Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
32350Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
32360Sstevel@tonic-gate 	t = s;
32370Sstevel@tonic-gate 
32380Sstevel@tonic-gate 	/*
32390Sstevel@tonic-gate 	 * shadow min (col 3)
32400Sstevel@tonic-gate 	 */
32410Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
32426842Sth160488 		(void) strlcpy(parse_err_msg, gettext("Improper format"),
32436842Sth160488 		    PARSE_ERR_MSG_LEN);
32440Sstevel@tonic-gate 		return (GENENT_PARSEERR);
32450Sstevel@tonic-gate 	}
32460Sstevel@tonic-gate 	*s++ = 0;
32470Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
32480Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
32490Sstevel@tonic-gate 	t = s;
32500Sstevel@tonic-gate 
32510Sstevel@tonic-gate 	/*
32520Sstevel@tonic-gate 	 * shadow max (col 4)
32530Sstevel@tonic-gate 	 */
32540Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
32556842Sth160488 		(void) strlcpy(parse_err_msg, gettext("Improper format"),
32566842Sth160488 		    PARSE_ERR_MSG_LEN);
32570Sstevel@tonic-gate 		return (GENENT_PARSEERR);
32580Sstevel@tonic-gate 	}
32590Sstevel@tonic-gate 	*s++ = 0;
32600Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_val = t;
32610Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_len = strlen(t)+1;
32620Sstevel@tonic-gate 	t = s;
32630Sstevel@tonic-gate 
32640Sstevel@tonic-gate 	/*
32650Sstevel@tonic-gate 	 * shadow warn (col 5)
32660Sstevel@tonic-gate 	 */
32670Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
32686842Sth160488 		(void) strlcpy(parse_err_msg, gettext("Improper format"),
32696842Sth160488 		    PARSE_ERR_MSG_LEN);
32700Sstevel@tonic-gate 		return (GENENT_PARSEERR);
32710Sstevel@tonic-gate 	}
32720Sstevel@tonic-gate 	*s++ = 0;
32730Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_val = t;
32740Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_len = strlen(t)+1;
32750Sstevel@tonic-gate 	t = s;
32760Sstevel@tonic-gate 
32770Sstevel@tonic-gate 	/*
32780Sstevel@tonic-gate 	 * shadow inactive (col 6)
32790Sstevel@tonic-gate 	 */
32800Sstevel@tonic-gate 	if ((s = strchr(t, ':')) != 0) {
32810Sstevel@tonic-gate 	*s++ = 0;
32820Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_val = t;
32830Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_len = strlen(t)+1;
32840Sstevel@tonic-gate 	t = s;
32850Sstevel@tonic-gate 	}
32860Sstevel@tonic-gate 
32870Sstevel@tonic-gate 	/*
32880Sstevel@tonic-gate 	 * shadow expire  (col 7)
32890Sstevel@tonic-gate 	 */
32900Sstevel@tonic-gate 	if ((s = strchr(t, ':')) != 0) {
32910Sstevel@tonic-gate 	*s++ = 0;
32920Sstevel@tonic-gate 	ecol[7].ec_value.ec_value_val = t;
32930Sstevel@tonic-gate 	ecol[7].ec_value.ec_value_len = strlen(t)+1;
32940Sstevel@tonic-gate 	t = s;
32950Sstevel@tonic-gate 
32960Sstevel@tonic-gate 	/*
32970Sstevel@tonic-gate 	 * flag (col 8)
32980Sstevel@tonic-gate 	 */
32990Sstevel@tonic-gate 	ecol[8].ec_value.ec_value_val = t;
33000Sstevel@tonic-gate 	ecol[8].ec_value.ec_value_len = strlen(t)+1;
33010Sstevel@tonic-gate 	}
33020Sstevel@tonic-gate 
33030Sstevel@tonic-gate 	/*
33040Sstevel@tonic-gate 	 * build entry
33050Sstevel@tonic-gate 	 */
33060Sstevel@tonic-gate 
33070Sstevel@tonic-gate 	data.sp_namp = strdup(ecol[0].ec_value.ec_value_val);
33080Sstevel@tonic-gate 
33090Sstevel@tonic-gate 	if (ecol[1].ec_value.ec_value_val != NULL &&
33106842Sth160488 	    ecol[1].ec_value.ec_value_val[0] != '\0') {
33110Sstevel@tonic-gate 		/* Add {crypt} before passwd entry */
33120Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname), "{crypt}%s",
33130Sstevel@tonic-gate 		    ecol[1].ec_value.ec_value_val);
33140Sstevel@tonic-gate 		data.sp_pwdp = strdup(pname);
33158821SMichen.Chang@Sun.COM 	} else {
33168821SMichen.Chang@Sun.COM 		/*
33178821SMichen.Chang@Sun.COM 		 * no password (e.g., deleted by "passwd -d"):
33188821SMichen.Chang@Sun.COM 		 * use the special value NS_LDAP_NO_UNIX_PASSWORD
33198821SMichen.Chang@Sun.COM 		 * instead.
33208821SMichen.Chang@Sun.COM 		 */
33218821SMichen.Chang@Sun.COM 		(void) snprintf(pname, sizeof (pname), "{crypt}%s",
33228821SMichen.Chang@Sun.COM 		    NS_LDAP_NO_UNIX_PASSWORD);
33238821SMichen.Chang@Sun.COM 		data.sp_pwdp = strdup(pname);
33248821SMichen.Chang@Sun.COM 	}
33250Sstevel@tonic-gate 
33260Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
33276842Sth160488 	    ecol[2].ec_value.ec_value_val[0] != '\0') {
33280Sstevel@tonic-gate 
33290Sstevel@tonic-gate 		data.sp_lstchg = ascii_to_int(ecol[2].ec_value.ec_value_val);
33300Sstevel@tonic-gate 		if (data.sp_lstchg < -1) {
33310Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
33326842Sth160488 			    gettext("invalid last changed date: %s"),
33330Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
33340Sstevel@tonic-gate 		return (GENENT_PARSEERR);
33350Sstevel@tonic-gate 		}
33360Sstevel@tonic-gate 	} else
33370Sstevel@tonic-gate 		data.sp_lstchg = -1;
33380Sstevel@tonic-gate 
33390Sstevel@tonic-gate 	if (ecol[3].ec_value.ec_value_val != NULL &&
33406842Sth160488 	    ecol[3].ec_value.ec_value_val[0] != '\0') {
33410Sstevel@tonic-gate 
33420Sstevel@tonic-gate 		data.sp_min = ascii_to_int(ecol[3].ec_value.ec_value_val);
33430Sstevel@tonic-gate 		if (data.sp_min < -1) {
33440Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
33456842Sth160488 			    gettext("invalid sp_min : %s"),
33460Sstevel@tonic-gate 			    ecol[3].ec_value.ec_value_val);
33470Sstevel@tonic-gate 		return (GENENT_PARSEERR);
33480Sstevel@tonic-gate 		}
33490Sstevel@tonic-gate 	} else
33500Sstevel@tonic-gate 		data.sp_min = -1;
33510Sstevel@tonic-gate 
33520Sstevel@tonic-gate 	if (ecol[4].ec_value.ec_value_val != NULL &&
33536842Sth160488 	    ecol[4].ec_value.ec_value_val[0] != '\0') {
33540Sstevel@tonic-gate 
33550Sstevel@tonic-gate 		data.sp_max = ascii_to_int(ecol[4].ec_value.ec_value_val);
33560Sstevel@tonic-gate 		if (data.sp_max < -1) {
33570Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
33586842Sth160488 			    gettext("invalid sp_max : %s"),
33590Sstevel@tonic-gate 			    ecol[4].ec_value.ec_value_val);
33600Sstevel@tonic-gate 		return (GENENT_PARSEERR);
33610Sstevel@tonic-gate 		}
33620Sstevel@tonic-gate 	} else
33630Sstevel@tonic-gate 		data.sp_max = -1;
33640Sstevel@tonic-gate 
33650Sstevel@tonic-gate 	if (ecol[5].ec_value.ec_value_val != NULL &&
33666842Sth160488 	    ecol[5].ec_value.ec_value_val[0] != '\0') {
33670Sstevel@tonic-gate 
33680Sstevel@tonic-gate 		data.sp_warn = ascii_to_int(ecol[5].ec_value.ec_value_val);
33690Sstevel@tonic-gate 		if (data.sp_warn < -1) {
33700Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
33716842Sth160488 			    gettext("invalid sp_warn : %s"),
33720Sstevel@tonic-gate 			    ecol[5].ec_value.ec_value_val);
33730Sstevel@tonic-gate 		return (GENENT_PARSEERR);
33740Sstevel@tonic-gate 		}
33750Sstevel@tonic-gate 	} else
33760Sstevel@tonic-gate 		data.sp_warn = -1;
33770Sstevel@tonic-gate 
33780Sstevel@tonic-gate 	if (ecol[6].ec_value.ec_value_val != NULL &&
33796842Sth160488 	    ecol[6].ec_value.ec_value_val[0] != '\0') {
33800Sstevel@tonic-gate 
33810Sstevel@tonic-gate 		data.sp_inact = ascii_to_int(ecol[6].ec_value.ec_value_val);
33820Sstevel@tonic-gate 		if (data.sp_inact < -1) {
33830Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
33846842Sth160488 			    gettext("invalid sp_inact : %s"),
33850Sstevel@tonic-gate 			    ecol[6].ec_value.ec_value_val);
33860Sstevel@tonic-gate 		return (GENENT_PARSEERR);
33870Sstevel@tonic-gate 		}
33880Sstevel@tonic-gate 	} else
33890Sstevel@tonic-gate 		data.sp_inact = -1;
33900Sstevel@tonic-gate 
33910Sstevel@tonic-gate 	if (ecol[7].ec_value.ec_value_val != NULL &&
33926842Sth160488 	    ecol[7].ec_value.ec_value_val[0] != '\0') {
33930Sstevel@tonic-gate 
33940Sstevel@tonic-gate 		data.sp_expire = ascii_to_int(ecol[7].ec_value.ec_value_val);
33950Sstevel@tonic-gate 		if (data.sp_expire < -1) {
33960Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
33976842Sth160488 			    gettext("invalid login expiry date : %s"),
33980Sstevel@tonic-gate 			    ecol[7].ec_value.ec_value_val);
33990Sstevel@tonic-gate 		return (GENENT_PARSEERR);
34000Sstevel@tonic-gate 		}
34010Sstevel@tonic-gate 	} else
34020Sstevel@tonic-gate 		data.sp_expire = -1;
34030Sstevel@tonic-gate 
34040Sstevel@tonic-gate 	if (ecol[8].ec_value.ec_value_val != NULL &&
34056842Sth160488 	    ecol[8].ec_value.ec_value_val[0] != '\0') {
34060Sstevel@tonic-gate 
34070Sstevel@tonic-gate 		/*
34080Sstevel@tonic-gate 		 * data.sp_flag is an unsigned int,
34090Sstevel@tonic-gate 		 * assign -1 to it, make no sense.
34100Sstevel@tonic-gate 		 * Use spflag here to avoid lint warning.
34110Sstevel@tonic-gate 		 */
34120Sstevel@tonic-gate 		spflag = ascii_to_int(ecol[8].ec_value.ec_value_val);
34130Sstevel@tonic-gate 		if (spflag < 0) {
34140Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
34156842Sth160488 			    gettext("invalid flag value: %s"),
34160Sstevel@tonic-gate 			    ecol[8].ec_value.ec_value_val);
34170Sstevel@tonic-gate 		return (GENENT_PARSEERR);
34180Sstevel@tonic-gate 		} else
34190Sstevel@tonic-gate 			data.sp_flag = spflag;
34200Sstevel@tonic-gate 	} else
34210Sstevel@tonic-gate 		data.sp_flag = 0;
34220Sstevel@tonic-gate 
34230Sstevel@tonic-gate 	if (flags & F_VERBOSE)
34240Sstevel@tonic-gate 		(void) fprintf(stdout,
34250Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.sp_namp);
34260Sstevel@tonic-gate 
34272277Sjs198686 	retval = (*cback)(&data, 1);
34282277Sjs198686 	if (retval != NS_LDAP_SUCCESS) {
34292277Sjs198686 		if (retval == LDAP_NO_SUCH_OBJECT)
34302277Sjs198686 			(void) fprintf(stdout,
34316842Sth160488 			    gettext("Cannot add shadow entry (%s), "
34326842Sth160488 			    "add passwd entry first\n"), data.sp_namp);
34332277Sjs198686 		if (continue_onerror == 0)
34342277Sjs198686 			return (GENENT_CBERR);
34352277Sjs198686 	}
34360Sstevel@tonic-gate 
34370Sstevel@tonic-gate 	free(data.sp_namp);
34380Sstevel@tonic-gate 	free(data.sp_pwdp);
34390Sstevel@tonic-gate 	return (GENENT_OK);
34400Sstevel@tonic-gate }
34410Sstevel@tonic-gate 
34420Sstevel@tonic-gate static void
dump_shadow(ns_ldap_result_t * res)34430Sstevel@tonic-gate dump_shadow(ns_ldap_result_t *res)
34440Sstevel@tonic-gate {
34450Sstevel@tonic-gate 	char    **value = NULL;
34460Sstevel@tonic-gate 	char   pnam[256];
34470Sstevel@tonic-gate 
34480Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "uid");
34490Sstevel@tonic-gate 	if (value == NULL)
34500Sstevel@tonic-gate 		return;
34510Sstevel@tonic-gate 	else
34520Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
34530Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "userPassword");
34540Sstevel@tonic-gate 	if (value == NULL)
34550Sstevel@tonic-gate 		(void) fprintf(stdout, "*:");
34560Sstevel@tonic-gate 	else {
34570Sstevel@tonic-gate 		(void) strcpy(pnam, value[0]);
34588821SMichen.Chang@Sun.COM 		if (strncasecmp(value[0], "{crypt}", 7) == 0) {
34598821SMichen.Chang@Sun.COM 			if (strcmp(pnam + 7, NS_LDAP_NO_UNIX_PASSWORD) == 0)
34608821SMichen.Chang@Sun.COM 				(void) fprintf(stdout, ":");
34618821SMichen.Chang@Sun.COM 			else
34628821SMichen.Chang@Sun.COM 				(void) fprintf(stdout, "%s:", (pnam+7));
34638821SMichen.Chang@Sun.COM 		} else
34640Sstevel@tonic-gate 			(void) fprintf(stdout, "*:");
34650Sstevel@tonic-gate 	}
34660Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "shadowLastChange");
34670Sstevel@tonic-gate 	if (value == NULL)
34680Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
34690Sstevel@tonic-gate 	else
34700Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
34710Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "shadowMin");
34720Sstevel@tonic-gate 	if (value == NULL)
34730Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
34740Sstevel@tonic-gate 	else
34750Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
34760Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "shadowMax");
34770Sstevel@tonic-gate 	if (value == NULL)
34780Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
34790Sstevel@tonic-gate 	else
34800Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
34810Sstevel@tonic-gate 
34828821SMichen.Chang@Sun.COM 	value = __ns_ldap_getAttr(res->entry, "shadowWarning");
34838821SMichen.Chang@Sun.COM 	if (value == NULL)
34848821SMichen.Chang@Sun.COM 		(void) fprintf(stdout, ":");
34858821SMichen.Chang@Sun.COM 	else
34868821SMichen.Chang@Sun.COM 		(void) fprintf(stdout, "%s:", value[0]);
34878821SMichen.Chang@Sun.COM 
34888821SMichen.Chang@Sun.COM 	value = __ns_ldap_getAttr(res->entry, "shadowInactive");
34898821SMichen.Chang@Sun.COM 	if (value == NULL)
34908821SMichen.Chang@Sun.COM 		(void) fprintf(stdout, ":");
34918821SMichen.Chang@Sun.COM 	else
34928821SMichen.Chang@Sun.COM 		(void) fprintf(stdout, "%s:", value[0]);
34938821SMichen.Chang@Sun.COM 
34948821SMichen.Chang@Sun.COM 	value = __ns_ldap_getAttr(res->entry, "shadowExpire");
34958821SMichen.Chang@Sun.COM 	if (value == NULL)
34968821SMichen.Chang@Sun.COM 		(void) fprintf(stdout, ":");
34978821SMichen.Chang@Sun.COM 	else
34988821SMichen.Chang@Sun.COM 		(void) fprintf(stdout, "%s:", value[0]);
34998821SMichen.Chang@Sun.COM 
35008821SMichen.Chang@Sun.COM 	value = __ns_ldap_getAttr(res->entry, "shadowFlag");
35018821SMichen.Chang@Sun.COM 	if (value == NULL || value[0] == NULL || strcmp(value[0], "0") == 0)
35028821SMichen.Chang@Sun.COM 		(void) fprintf(stdout, "\n");
35038821SMichen.Chang@Sun.COM 	else
35048821SMichen.Chang@Sun.COM 		(void) fprintf(stdout, "%s\n", value[0]);
35050Sstevel@tonic-gate }
35060Sstevel@tonic-gate 
35070Sstevel@tonic-gate static int
genent_bootparams(char * line,int (* cback)())35080Sstevel@tonic-gate genent_bootparams(char *line, int (*cback)())
35090Sstevel@tonic-gate {
35100Sstevel@tonic-gate 	char buf[BUFSIZ+1];
35110Sstevel@tonic-gate 	char *t;
35120Sstevel@tonic-gate 	entry_col ecol[2];
35130Sstevel@tonic-gate 	int ctr = 0, retval = 1;
35140Sstevel@tonic-gate 
35150Sstevel@tonic-gate 	struct _ns_bootp data;
35160Sstevel@tonic-gate 	char *parameter;
35170Sstevel@tonic-gate 	int rc = GENENT_OK;
35180Sstevel@tonic-gate 
35190Sstevel@tonic-gate 	/*
35200Sstevel@tonic-gate 	 * don't clobber our argument
35210Sstevel@tonic-gate 	 */
35220Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
35236842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
35246842Sth160488 		    PARSE_ERR_MSG_LEN);
35250Sstevel@tonic-gate 		return (GENENT_PARSEERR);
35260Sstevel@tonic-gate 	}
35270Sstevel@tonic-gate 	(void) strcpy(buf, line);
35280Sstevel@tonic-gate 
35290Sstevel@tonic-gate 	/*
35300Sstevel@tonic-gate 	 * clear column data
35310Sstevel@tonic-gate 	 */
35320Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
35330Sstevel@tonic-gate 
35340Sstevel@tonic-gate 
35350Sstevel@tonic-gate 	/*
35360Sstevel@tonic-gate 	 * cname (col 0)
35370Sstevel@tonic-gate 	 */
35380Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
35396842Sth160488 		(void) strlcpy(parse_err_msg, gettext("no cname"),
35406842Sth160488 		    PARSE_ERR_MSG_LEN);
35410Sstevel@tonic-gate 		return (GENENT_PARSEERR);
35420Sstevel@tonic-gate 	}
35430Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
35440Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
35450Sstevel@tonic-gate 
35460Sstevel@tonic-gate 
35470Sstevel@tonic-gate 
35480Sstevel@tonic-gate 	/* build entry */
35490Sstevel@tonic-gate 	data.name = strdup(ecol[0].ec_value.ec_value_val);
35500Sstevel@tonic-gate 
35510Sstevel@tonic-gate 	/*
35520Sstevel@tonic-gate 	 * name (col 1)
35530Sstevel@tonic-gate 	 */
35540Sstevel@tonic-gate 
35550Sstevel@tonic-gate 	data.param = NULL;
35560Sstevel@tonic-gate 
35571831Sdm199847 	while (t = strtok(NULL, " \t"))  {
35580Sstevel@tonic-gate 
35590Sstevel@tonic-gate 		/*
35600Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
35610Sstevel@tonic-gate 		 */
35620Sstevel@tonic-gate 
35630Sstevel@tonic-gate 
35640Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
35650Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
35660Sstevel@tonic-gate 
35670Sstevel@tonic-gate 		ctr++;
35680Sstevel@tonic-gate 		parameter = strdup(ecol[1].ec_value.ec_value_val);
35690Sstevel@tonic-gate 		if ((data.param = (char **)realloc(data.param,
35706842Sth160488 		    (ctr + 1) * sizeof (char **))) == NULL) {
35710Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
35720Sstevel@tonic-gate 			exit(1);
35730Sstevel@tonic-gate 		}
35740Sstevel@tonic-gate 		data.param[ctr-1] = parameter;
35750Sstevel@tonic-gate 
35761831Sdm199847 	}
35770Sstevel@tonic-gate 
35780Sstevel@tonic-gate 
35790Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
35800Sstevel@tonic-gate 	if ((data.param = (char **)realloc(data.param,
35816842Sth160488 	    (ctr + 1) * sizeof (char **))) == NULL) {
35820Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
35830Sstevel@tonic-gate 		exit(1);
35840Sstevel@tonic-gate 	}
35850Sstevel@tonic-gate 	data.param[ctr] = NULL;
35860Sstevel@tonic-gate 
35870Sstevel@tonic-gate 	if (flags & F_VERBOSE)
35880Sstevel@tonic-gate 		(void) fprintf(stdout,
35890Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.name);
35900Sstevel@tonic-gate 
35910Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
35920Sstevel@tonic-gate 
35930Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
35940Sstevel@tonic-gate 		if (continue_onerror)
35950Sstevel@tonic-gate 			(void) fprintf(stderr,
35966842Sth160488 			    gettext("Entry: %s - already Exists,"
35976842Sth160488 			    " skipping it.\n"), data.name);
35980Sstevel@tonic-gate 		else {
35990Sstevel@tonic-gate 			rc = GENENT_CBERR;
36000Sstevel@tonic-gate 			(void) fprintf(stderr,
36016842Sth160488 			    gettext("Entry: %s - already Exists\n"),
36026842Sth160488 			    data.name);
36030Sstevel@tonic-gate 		}
36040Sstevel@tonic-gate 	} else if (retval)
36050Sstevel@tonic-gate 		rc = GENENT_CBERR;
36060Sstevel@tonic-gate 
36070Sstevel@tonic-gate 	free(data.name);
36080Sstevel@tonic-gate 	free(data.param);
36090Sstevel@tonic-gate 
36100Sstevel@tonic-gate 	return (rc);
36110Sstevel@tonic-gate 
36120Sstevel@tonic-gate }
36130Sstevel@tonic-gate 
36146842Sth160488 /*
36156842Sth160488  * Count number of tokens in string which has tokens separated by colons.
36166842Sth160488  *
36176842Sth160488  * NULL or "" - 0 tokens
36186842Sth160488  * "foo" - 1 token
36196842Sth160488  * "foo:bar" - 2 tokens
36206842Sth160488  * ":bar" - 2 tokens, first empty
36216842Sth160488  * "::" - 3 tokens, all empty
36226842Sth160488  */
36236842Sth160488 static int
count_tokens(char * string,char delim)36246842Sth160488 count_tokens(char *string, char delim)
36256842Sth160488 {
36266842Sth160488 	int i = 0;
36276842Sth160488 	char *s = string;
36286842Sth160488 
36296842Sth160488 	if (string == NULL || *string == '\0')
36306842Sth160488 		return (0);
36316842Sth160488 
36326842Sth160488 	/* Count delimiters */
36336842Sth160488 	while ((s = strchr(s, delim)) != NULL && *s != '\0') {
36346842Sth160488 		i++;
36356842Sth160488 		s++;
36366842Sth160488 	}
36376842Sth160488 
36386842Sth160488 	return (i + 1);
36396842Sth160488 }
36406842Sth160488 
36416842Sth160488 static int
genent_project(char * line,int (* cback)())36426842Sth160488 genent_project(char *line, int (*cback)())
36436842Sth160488 {
36446842Sth160488 	char buf[BUFSIZ+1];
36456842Sth160488 	char *b = buf;
36466842Sth160488 	char *s;
36476842Sth160488 	int rc = GENENT_OK, retval;
36486842Sth160488 	int index = 0;
36496842Sth160488 	struct project data;
36506842Sth160488 
36516842Sth160488 	(void) memset(&data, 0, sizeof (struct project));
36526842Sth160488 
36536842Sth160488 	/*
36546842Sth160488 	 * don't clobber our argument
36556842Sth160488 	 */
36566842Sth160488 	if (strlen(line) >= sizeof (buf)) {
36576842Sth160488 		(void) strlcpy(parse_err_msg, gettext("line too long"),
36586842Sth160488 		    PARSE_ERR_MSG_LEN);
36596842Sth160488 		return (GENENT_PARSEERR);
36606842Sth160488 	}
36616842Sth160488 
36626842Sth160488 	if (count_tokens(line, ':') != 6) {
36636842Sth160488 		(void) strlcpy(parse_err_msg, gettext("Improper format"),
36646842Sth160488 		    PARSE_ERR_MSG_LEN);
36656842Sth160488 		return (GENENT_PARSEERR);
36666842Sth160488 	}
36676842Sth160488 
36686842Sth160488 	(void) strcpy(buf, line);
36696842Sth160488 
36706842Sth160488 	s = strsep(&b, ":");
36716842Sth160488 	while (s != NULL) {
36726842Sth160488 		switch (index) {
36736842Sth160488 		/* Project name */
36746842Sth160488 		case 0:
36756842Sth160488 			if (check_projname(s) != 0) {
36766842Sth160488 				(void) strlcpy(parse_err_msg,
36776842Sth160488 				    gettext("invalid project name"),
36786842Sth160488 				    PARSE_ERR_MSG_LEN);
36796842Sth160488 				return (GENENT_PARSEERR);
36806842Sth160488 			} else {
36816842Sth160488 				data.pj_name = strdup(s);
36826842Sth160488 			}
36836842Sth160488 			break;
36846842Sth160488 
36856842Sth160488 		/* Project ID */
36866842Sth160488 		case 1:
36876842Sth160488 		{
36886842Sth160488 			char *endptr = NULL;
36896842Sth160488 			int projid = strtoul(s, &endptr, 10);
36906842Sth160488 
36916842Sth160488 			if (*s == '\0' || strlen(endptr) != 0 || projid < 0 ||
36926842Sth160488 			    projid > MAXPROJID) {
36936842Sth160488 				(void) strlcpy(parse_err_msg,
36946842Sth160488 				    gettext("invalid project id"),
36956842Sth160488 				    PARSE_ERR_MSG_LEN);
36966842Sth160488 				return (GENENT_PARSEERR);
36976842Sth160488 			} else {
36986842Sth160488 				data.pj_projid = projid;
36996842Sth160488 			}
37006842Sth160488 			break;
37016842Sth160488 		}
37026842Sth160488 
37036842Sth160488 		/* Project description */
37046842Sth160488 		case 2:
37056842Sth160488 			if (*s != '\0')
37066842Sth160488 				data.pj_comment = strdup(s);
37076842Sth160488 			break;
37086842Sth160488 
37096842Sth160488 		/* Project users */
37106842Sth160488 		case 3:
37116842Sth160488 		{
37126842Sth160488 			if (*s == '\0')
37136842Sth160488 				break;
37146842Sth160488 
37156842Sth160488 			char *usrlist = strdup(s);
37166842Sth160488 			int   i = 0;
37176842Sth160488 			int   usr_count = count_tokens(usrlist, ',');
37186842Sth160488 			char *u = strsep(&usrlist, ",");
37196842Sth160488 
37206842Sth160488 			if (usr_count == 0) {
37216842Sth160488 				free(usrlist);
37226842Sth160488 				break;
37236842Sth160488 			}
37246842Sth160488 
37256842Sth160488 			/* +1 to NULL-terminate the array */
37266842Sth160488 			data.pj_users = (char **)calloc(usr_count + 1,
37276842Sth160488 			    sizeof (char *));
37286842Sth160488 
37296842Sth160488 			while (u != NULL) {
37306842Sth160488 				data.pj_users[i++] = strdup(u);
37316842Sth160488 				u = strsep(&usrlist, ",");
37326842Sth160488 			}
37336842Sth160488 
37346842Sth160488 			free(usrlist);
37356842Sth160488 			break;
37366842Sth160488 		}
37376842Sth160488 
37386842Sth160488 		/* Project groups */
37396842Sth160488 		case 4:
37406842Sth160488 		{
37416842Sth160488 			if (*s == '\0')
37426842Sth160488 				break;
37436842Sth160488 
37446842Sth160488 			char *grouplist = strdup(s);
37456842Sth160488 			int   i = 0;
37466842Sth160488 			int   grp_count = count_tokens(grouplist, ',');
37476842Sth160488 			char *g = strsep(&grouplist, ",");
37486842Sth160488 
37496842Sth160488 			if (grp_count == 0) {
37506842Sth160488 				free(grouplist);
37516842Sth160488 				break;
37526842Sth160488 			}
37536842Sth160488 
37546842Sth160488 			/* +1 to NULL-terminate the array */
37556842Sth160488 			data.pj_groups = (char **)calloc(grp_count + 1,
37566842Sth160488 			    sizeof (char *));
37576842Sth160488 
37586842Sth160488 			while (g != NULL) {
37596842Sth160488 				data.pj_groups[i++] = strdup(g);
37606842Sth160488 				g = strsep(&grouplist, ",");
37616842Sth160488 			}
37626842Sth160488 
37636842Sth160488 			free(grouplist);
37646842Sth160488 			break;
37656842Sth160488 		}
37666842Sth160488 
37676842Sth160488 		/* Attributes */
37686842Sth160488 		case 5:
37696842Sth160488 			if (*s != '\0')
37706842Sth160488 				data.pj_attr = strdup(s);
37716842Sth160488 
37726842Sth160488 			break;
37736842Sth160488 		}
37746842Sth160488 
37756842Sth160488 		/* Next token */
37766842Sth160488 		s = strsep(&b, ":");
37776842Sth160488 		index++;
37786842Sth160488 	}
37796842Sth160488 
37806842Sth160488 	if (flags & F_VERBOSE)
37816842Sth160488 		(void) fprintf(stdout,
37826842Sth160488 		    gettext("Adding entry : %s\n"), data.pj_name);
37836842Sth160488 
37846842Sth160488 	retval = (*cback)(&data, 0);
37856842Sth160488 
37866842Sth160488 	if (retval == LDAP_ALREADY_EXISTS) {
37876842Sth160488 		if (continue_onerror)
37886842Sth160488 			(void) fprintf(stderr,
37896842Sth160488 			    gettext("Entry: %s - already Exists,"
37906842Sth160488 			    " skipping it.\n"), data.pj_name);
37916842Sth160488 		else {
37926842Sth160488 			rc = GENENT_CBERR;
37936842Sth160488 			(void) fprintf(stderr,
37946842Sth160488 			    gettext("Entry: %s - already Exists\n"),
37956842Sth160488 			    data.pj_name);
37966842Sth160488 		}
37976842Sth160488 	} else if (retval)
37986842Sth160488 		rc = GENENT_CBERR;
37996842Sth160488 
38006842Sth160488 	/* Clean up */
38016842Sth160488 	free(data.pj_name);
38026842Sth160488 	free(data.pj_attr);
38036842Sth160488 	if (data.pj_users != NULL) {
38046842Sth160488 		for (index = 0; data.pj_users[index] != NULL; index++)
38056842Sth160488 			free(data.pj_users[index]);
38066842Sth160488 		free(data.pj_users);
38076842Sth160488 	}
38086842Sth160488 	if (data.pj_groups != NULL) {
38096842Sth160488 		for (index = 0; data.pj_groups[index] != NULL; index++)
38106842Sth160488 			free(data.pj_groups[index]);
38116842Sth160488 		free(data.pj_groups);
38126842Sth160488 	}
38136842Sth160488 
38146842Sth160488 	return (rc);
38156842Sth160488 }
38166842Sth160488 
38176842Sth160488 static void
dump_project(ns_ldap_result_t * res)38186842Sth160488 dump_project(ns_ldap_result_t *res)
38196842Sth160488 {
38206842Sth160488 	char    **value = NULL;
38216842Sth160488 	char 	*endptr = NULL;
38226842Sth160488 	int 	projid;
38236842Sth160488 
38246842Sth160488 	if (res == NULL || res->entry == NULL)
38256842Sth160488 		return;
38266842Sth160488 
38276842Sth160488 	/* Sanity checking */
38286842Sth160488 	value = __ns_ldap_getAttr(res->entry, "SolarisProjectID");
38296842Sth160488 
38306842Sth160488 	if (value[0] == NULL)
38316842Sth160488 		return;
38326842Sth160488 
38336842Sth160488 	projid = strtoul(value[0], &endptr, 10);
38346842Sth160488 	if (*value[0] == '\0' || strlen(endptr) != 0 || projid < 0 ||
38356842Sth160488 	    projid > MAXPROJID)
38366842Sth160488 		return;
38376842Sth160488 
38386842Sth160488 	value = __ns_ldap_getAttr(res->entry, "SolarisProjectName");
38396842Sth160488 	if (value && value[0] && check_projname(value[0]) == 0)
38406842Sth160488 		(void) fprintf(stdout, "%s:", value[0]);
38416842Sth160488 	else
38426842Sth160488 		return;
38436842Sth160488 
38446842Sth160488 	(void) fprintf(stdout, "%d:", projid);
38456842Sth160488 
38466842Sth160488 	value = __ns_ldap_getAttr(res->entry, "description");
38476842Sth160488 	if (value && value[0])
38486842Sth160488 		(void) fprintf(stdout, "%s:", value[0]);
38496842Sth160488 	else
38506842Sth160488 		(void) fprintf(stdout, ":");
38516842Sth160488 
38526842Sth160488 	value = __ns_ldap_getAttr(res->entry, "memberUid");
38536842Sth160488 	if (value) {
38546842Sth160488 		int i;
38556842Sth160488 		for (i = 0; value[i] != NULL; i++)
38566842Sth160488 			if (value[i+1] != NULL)
38576842Sth160488 				(void) fprintf(stdout, "%s,", value[i]);
38586842Sth160488 			else
38596842Sth160488 				(void) fprintf(stdout, "%s:", value[i]);
38606842Sth160488 	} else {
38616842Sth160488 		(void) fprintf(stdout, ":");
38626842Sth160488 	}
38636842Sth160488 
38646842Sth160488 	value = __ns_ldap_getAttr(res->entry, "memberGid");
38656842Sth160488 	if (value) {
38666842Sth160488 		int i;
38676842Sth160488 		for (i = 0; value[i] != NULL; i++)
38686842Sth160488 			if (value[i+1] != NULL)
38696842Sth160488 				(void) fprintf(stdout, "%s,", value[i]);
38706842Sth160488 			else
38716842Sth160488 				(void) fprintf(stdout, "%s:", value[i]);
38726842Sth160488 	} else {
38736842Sth160488 		(void) fprintf(stdout, ":");
38746842Sth160488 	}
38756842Sth160488 
38766842Sth160488 	value = __ns_ldap_getAttr(res->entry, "SolarisProjectAttr");
38776842Sth160488 	if (value && value[0])
38786842Sth160488 		(void) fprintf(stdout, "%s\n", value[0]);
38796842Sth160488 	else
38806842Sth160488 		(void) fprintf(stdout, "\n");
38816842Sth160488 
38826842Sth160488 }
38830Sstevel@tonic-gate 
38840Sstevel@tonic-gate static void
dump_bootparams(ns_ldap_result_t * res)38850Sstevel@tonic-gate dump_bootparams(ns_ldap_result_t *res)
38860Sstevel@tonic-gate {
38870Sstevel@tonic-gate 	char	**value = NULL;
38880Sstevel@tonic-gate 	int		attr_count = 0;
38890Sstevel@tonic-gate 
38900Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
38910Sstevel@tonic-gate 	if (value[0] != NULL)
38920Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
38930Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "bootParameter");
38940Sstevel@tonic-gate 	if (value != NULL)
38950Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
38960Sstevel@tonic-gate 		(void) fprintf(stdout, "\t%s", value[attr_count]);
38970Sstevel@tonic-gate 			attr_count++;
38980Sstevel@tonic-gate 		}
38990Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
39000Sstevel@tonic-gate 
39010Sstevel@tonic-gate 
39020Sstevel@tonic-gate }
39030Sstevel@tonic-gate 
39040Sstevel@tonic-gate static char *
fget_line_at(struct line_buf * line,int n,FILE * fp)39050Sstevel@tonic-gate fget_line_at(struct line_buf *line, int n, FILE *fp)
39060Sstevel@tonic-gate {
39070Sstevel@tonic-gate 	int c;
39080Sstevel@tonic-gate 
39090Sstevel@tonic-gate 	line->len = n;
39100Sstevel@tonic-gate 
39110Sstevel@tonic-gate 	for (;;) {
39120Sstevel@tonic-gate 		c = fgetc(fp);
39130Sstevel@tonic-gate 		if (c == -1)
39140Sstevel@tonic-gate 			break;
39150Sstevel@tonic-gate 		if (line->len >= line->alloc)
39160Sstevel@tonic-gate 			line_buf_expand(line);
39170Sstevel@tonic-gate 		line->str[line->len++] = c;
39180Sstevel@tonic-gate 
39190Sstevel@tonic-gate 		if (c == '\n')
39200Sstevel@tonic-gate 			break;
39210Sstevel@tonic-gate 	}
39220Sstevel@tonic-gate 
39230Sstevel@tonic-gate 	/* Null Terminate */
39240Sstevel@tonic-gate 	if (line->len >= line->alloc)
39250Sstevel@tonic-gate 		line_buf_expand(line);
39260Sstevel@tonic-gate 	line->str[line->len++] = 0;
39270Sstevel@tonic-gate 
39280Sstevel@tonic-gate 	/* if no characters are read, return NULL to indicate EOF */
39290Sstevel@tonic-gate 	if (line->str[0] == '\0')
39300Sstevel@tonic-gate 		return (0);
39310Sstevel@tonic-gate 
39320Sstevel@tonic-gate 	return (line->str);
39330Sstevel@tonic-gate }
39340Sstevel@tonic-gate 
39350Sstevel@tonic-gate /*
39360Sstevel@tonic-gate  * return a line from the file, discarding comments and blank lines
39370Sstevel@tonic-gate  */
39380Sstevel@tonic-gate static int
filedbmline_comment(struct line_buf * line,FILE * etcf,int * lineno,struct file_loc * loc)39390Sstevel@tonic-gate filedbmline_comment(struct line_buf *line, FILE *etcf, int *lineno,
39400Sstevel@tonic-gate     struct file_loc *loc)
39410Sstevel@tonic-gate {
39420Sstevel@tonic-gate 	int i, len = 0;
39430Sstevel@tonic-gate 
39440Sstevel@tonic-gate 	loc->offset = ftell(etcf);
39450Sstevel@tonic-gate 	for (;;) {
39460Sstevel@tonic-gate 		if (fget_line_at(line, len, etcf) == 0)
39470Sstevel@tonic-gate 			return (0);
39480Sstevel@tonic-gate 
39490Sstevel@tonic-gate 		if (lineno)
39500Sstevel@tonic-gate 			(*lineno)++;
39510Sstevel@tonic-gate 
39520Sstevel@tonic-gate 		len = strlen(line->str);
39530Sstevel@tonic-gate 		if (len >= 2 &&
39540Sstevel@tonic-gate 		    line->str[0] != '#' &&
39550Sstevel@tonic-gate 		    line->str[len-2] == '\\' && line->str[len-1] == '\n') {
39560Sstevel@tonic-gate 			line->str[len-2] = 0;
39570Sstevel@tonic-gate 			len -= 2;
39580Sstevel@tonic-gate 			continue;    /* append next line at end */
39590Sstevel@tonic-gate 		}
39600Sstevel@tonic-gate 
39610Sstevel@tonic-gate 		if (line->str[len-1] == '\n') {
39620Sstevel@tonic-gate 			line->str[len-1] = 0;
39630Sstevel@tonic-gate 			len -= 1;
39640Sstevel@tonic-gate 		}
39650Sstevel@tonic-gate 
39660Sstevel@tonic-gate 		/*
39670Sstevel@tonic-gate 		 * Skip lines where '#' is the first non-blank character.
39680Sstevel@tonic-gate 		 */
39690Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
39700Sstevel@tonic-gate 			if (line->str[i] == '#') {
39710Sstevel@tonic-gate 				line->str[i] = '\0';
39720Sstevel@tonic-gate 				len = i;
39730Sstevel@tonic-gate 				break;
39740Sstevel@tonic-gate 			}
39750Sstevel@tonic-gate 			if (line->str[i] != ' ' && line->str[i] != '\t')
39760Sstevel@tonic-gate 				break;
39770Sstevel@tonic-gate 		}
39780Sstevel@tonic-gate 
39790Sstevel@tonic-gate 		/*
39800Sstevel@tonic-gate 		 * A line with one or more white space characters followed
39810Sstevel@tonic-gate 		 * by a comment will now be blank. The special case of a
39820Sstevel@tonic-gate 		 * line with '#' in the first byte will have len == 0.
39830Sstevel@tonic-gate 		 */
39840Sstevel@tonic-gate 		if (len > 0 && !blankline(line->str))
39850Sstevel@tonic-gate 			break;
39860Sstevel@tonic-gate 
39870Sstevel@tonic-gate 		len = 0;
39880Sstevel@tonic-gate 		loc->offset = ftell(etcf);
39890Sstevel@tonic-gate 	}
39900Sstevel@tonic-gate 
39910Sstevel@tonic-gate 	loc->size = len;
39920Sstevel@tonic-gate 	return (1);
39930Sstevel@tonic-gate }
39940Sstevel@tonic-gate 
39950Sstevel@tonic-gate /*
39960Sstevel@tonic-gate  * return a line from the file, discarding comments, blanks, and '+' lines
39970Sstevel@tonic-gate  */
39980Sstevel@tonic-gate static int
filedbmline_plus(struct line_buf * line,FILE * etcf,int * lineno,struct file_loc * loc)39990Sstevel@tonic-gate filedbmline_plus(struct line_buf *line, FILE *etcf, int *lineno,
40000Sstevel@tonic-gate     struct file_loc *loc)
40010Sstevel@tonic-gate {
40020Sstevel@tonic-gate 	int len = 0;
40030Sstevel@tonic-gate 
40040Sstevel@tonic-gate 	loc->offset = ftell(etcf);
40050Sstevel@tonic-gate 	for (;;) {
40060Sstevel@tonic-gate 		if (fget_line_at(line, len, etcf) == 0)
40070Sstevel@tonic-gate 			return (0);
40080Sstevel@tonic-gate 
40090Sstevel@tonic-gate 		if (lineno)
40100Sstevel@tonic-gate 			(*lineno)++;
40110Sstevel@tonic-gate 
40120Sstevel@tonic-gate 		len = strlen(line->str);
40130Sstevel@tonic-gate 		if (line->str[len-1] == '\n') {
40140Sstevel@tonic-gate 			line->str[len-1] = 0;
40150Sstevel@tonic-gate 			len -= 1;
40160Sstevel@tonic-gate 		}
40170Sstevel@tonic-gate 
40180Sstevel@tonic-gate 		if (!blankline(line->str) &&
40196842Sth160488 		    line->str[0] != '+' && line->str[0] != '-' &&
40206842Sth160488 		    line->str[0] != '#')
40210Sstevel@tonic-gate 			break;
40220Sstevel@tonic-gate 
40230Sstevel@tonic-gate 		len = 0;
40240Sstevel@tonic-gate 		loc->offset = ftell(etcf);
40250Sstevel@tonic-gate 	}
40260Sstevel@tonic-gate 
40270Sstevel@tonic-gate 	loc->size = len;
40280Sstevel@tonic-gate 	return (1);
40290Sstevel@tonic-gate }
40300Sstevel@tonic-gate 
40310Sstevel@tonic-gate 
40320Sstevel@tonic-gate /* Populating the ttypelist structure */
40330Sstevel@tonic-gate 
40340Sstevel@tonic-gate static struct ttypelist_t ttypelist[] = {
40350Sstevel@tonic-gate 	{ NS_LDAP_TYPE_HOSTS, genent_hosts, dump_hosts,
4036*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "iphost", "cn" },
40370Sstevel@tonic-gate 	{ NS_LDAP_TYPE_IPNODES, genent_hosts, dump_hosts,
4038*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "iphost", "cn" },
40390Sstevel@tonic-gate 	{ NS_LDAP_TYPE_RPC, genent_rpc, dump_rpc,
4040*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "oncrpc", "cn" },
40410Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PROTOCOLS, genent_protocols, dump_protocols,
4042*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "ipprotocol", "cn" },
40430Sstevel@tonic-gate 	{ NS_LDAP_TYPE_NETWORKS, genent_networks, dump_networks,
4044*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "ipnetwork", "ipnetworknumber" },
40450Sstevel@tonic-gate 	{ NS_LDAP_TYPE_SERVICES, genent_services, dump_services,
4046*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "ipservice", "cn" },
40470Sstevel@tonic-gate 	{ NS_LDAP_TYPE_GROUP, genent_group, dump_group,
4048*12758SJulian.Pullen@Sun.COM 		filedbmline_plus, "posixgroup", "gidnumber" },
40490Sstevel@tonic-gate 	{ NS_LDAP_TYPE_NETMASKS, genent_netmasks, dump_netmasks,
4050*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "ipnetwork", "ipnetworknumber"},
40510Sstevel@tonic-gate 	{ NS_LDAP_TYPE_ETHERS, genent_ethers, dump_ethers,
4052*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "ieee802Device", "cn" },
40530Sstevel@tonic-gate 	{ NS_LDAP_TYPE_NETGROUP, genent_netgroup, dump_netgroup,
4054*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "nisnetgroup", "cn" },
40550Sstevel@tonic-gate 	{ NS_LDAP_TYPE_BOOTPARAMS, genent_bootparams, dump_bootparams,
4056*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "bootableDevice", "cn" },
40570Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PUBLICKEY, genent_publickey, NULL /* dump_publickey */,
4058*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "niskeyobject", "cn" },
40590Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PASSWD, genent_passwd, dump_passwd,
4060*12758SJulian.Pullen@Sun.COM 		filedbmline_plus, "posixaccount", "uid" },
40610Sstevel@tonic-gate 	{ NS_LDAP_TYPE_SHADOW, genent_shadow, dump_shadow,
4062*12758SJulian.Pullen@Sun.COM 		filedbmline_plus, "shadowaccount", "uid" },
40630Sstevel@tonic-gate 	{ NS_LDAP_TYPE_ALIASES, genent_aliases, dump_aliases,
4064*12758SJulian.Pullen@Sun.COM 		filedbmline_plus, "mailGroup", "cn" },
40650Sstevel@tonic-gate 	{ NS_LDAP_TYPE_AUTOMOUNT, genent_automount, dump_automount,
4066*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "automount", "automountKey" },
40670Sstevel@tonic-gate 	{ NS_LDAP_TYPE_USERATTR, genent_user_attr, dump_user_attr,
4068*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "SolarisUserAttr", "uid" },
40690Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PROFILE, genent_prof_attr, dump_prof_attr,
4070*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "SolarisProfAttr", "cn" },
40710Sstevel@tonic-gate 	{ NS_LDAP_TYPE_EXECATTR, genent_exec_attr, dump_exec_attr,
4072*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "SolarisExecAttr", "cn" },
40730Sstevel@tonic-gate 	{ NS_LDAP_TYPE_AUTHATTR, genent_auth_attr, dump_auth_attr,
4074*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "SolarisAuthAttr", "cn" },
40751676Sjpk 	{ NS_LDAP_TYPE_TNRHDB, genent_tnrhdb, dump_tnrhdb,
4076*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "ipTnetHost", "ipTnetNumber" },
40771676Sjpk 	{ NS_LDAP_TYPE_TNRHTP, genent_tnrhtp, dump_tnrhtp,
4078*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "ipTnetTemplate", "ipTnetTemplateName" },
40796842Sth160488 	{ NS_LDAP_TYPE_PROJECT, genent_project, dump_project,
4080*12758SJulian.Pullen@Sun.COM 		filedbmline_comment, "SolarisProject", "SolarisProjectName" },
4081*12758SJulian.Pullen@Sun.COM 	{ 0, 0, 0, 0, 0, 0 }
40820Sstevel@tonic-gate };
40830Sstevel@tonic-gate 
40840Sstevel@tonic-gate 
40850Sstevel@tonic-gate 
40860Sstevel@tonic-gate 
40870Sstevel@tonic-gate static int lineno = 0;
40880Sstevel@tonic-gate 
40890Sstevel@tonic-gate static	void
addfile()40900Sstevel@tonic-gate addfile()
40910Sstevel@tonic-gate {
40920Sstevel@tonic-gate 	struct line_buf line;
40930Sstevel@tonic-gate 	struct file_loc loc;
40940Sstevel@tonic-gate 
40950Sstevel@tonic-gate 	/* Initializing the Line Buffer */
40960Sstevel@tonic-gate 	line_buf_init(&line);
40970Sstevel@tonic-gate 
40980Sstevel@tonic-gate 	/* Loop through all the lines in the file */
40990Sstevel@tonic-gate 	while (tt->filedbmline(&line, etcf, &lineno, &loc)) {
41000Sstevel@tonic-gate 		switch ((*(tt->genent))(line.str, addentry)) {
41010Sstevel@tonic-gate 		case GENENT_OK:
41020Sstevel@tonic-gate 			break;
41030Sstevel@tonic-gate 		case GENENT_PARSEERR:
41040Sstevel@tonic-gate 			(void) fprintf(stderr,
41050Sstevel@tonic-gate 			    gettext("parse error: %s (line %d)\n"),
41060Sstevel@tonic-gate 			    parse_err_msg, lineno);
41070Sstevel@tonic-gate 			exit_val = 1;
41080Sstevel@tonic-gate 			break;
41090Sstevel@tonic-gate 		case GENENT_CBERR:
41100Sstevel@tonic-gate 			(void) fprintf(stderr,
41110Sstevel@tonic-gate 			    gettext("Error while adding line: %s\n"),
41120Sstevel@tonic-gate 			    line.str);
41130Sstevel@tonic-gate 			exit_val = 2;
41140Sstevel@tonic-gate 			free(line.str);
41150Sstevel@tonic-gate 			return;
41160Sstevel@tonic-gate 			break;
41170Sstevel@tonic-gate 		case GENENT_ERR:
41180Sstevel@tonic-gate 			(void) fprintf(stderr,
41190Sstevel@tonic-gate 			    gettext("Internal Error while adding line: %s\n"),
41200Sstevel@tonic-gate 			    line.str);
41210Sstevel@tonic-gate 			exit_val = 3;
41220Sstevel@tonic-gate 			free(line.str);
41230Sstevel@tonic-gate 			return;
41240Sstevel@tonic-gate 			break;
41250Sstevel@tonic-gate 		}
41260Sstevel@tonic-gate 	}
41270Sstevel@tonic-gate 	free(line.str);
41280Sstevel@tonic-gate }
41290Sstevel@tonic-gate 
41300Sstevel@tonic-gate static void
dumptable(char * service)41310Sstevel@tonic-gate dumptable(char *service)
41320Sstevel@tonic-gate {
41330Sstevel@tonic-gate 
41340Sstevel@tonic-gate 	ns_ldap_result_t *eres = NULL;
41350Sstevel@tonic-gate 	ns_ldap_error_t *err = NULL;
41360Sstevel@tonic-gate 	int	rc = 0, success = 0;
41370Sstevel@tonic-gate 	char	filter[BUFSIZ];
41380Sstevel@tonic-gate 	int	done = 0;
41390Sstevel@tonic-gate 	void	*cookie = NULL;
41400Sstevel@tonic-gate 
41410Sstevel@tonic-gate 	/* set the appropriate filter */
41420Sstevel@tonic-gate 	if (strcmp(tt->ttype, NS_LDAP_TYPE_PROFILE) == 0) {
41430Sstevel@tonic-gate 		/*
41440Sstevel@tonic-gate 		 * prof_attr entries are SolarisProfAttr
41450Sstevel@tonic-gate 		 * without AUXILIARY SolarisExecAttr
41460Sstevel@tonic-gate 		 */
41470Sstevel@tonic-gate 		(void) snprintf(filter, sizeof (filter),
41480Sstevel@tonic-gate 		    "(&(objectclass=%s)(!(objectclass=SolarisExecAttr)))",
41490Sstevel@tonic-gate 		    tt->objclass);
41501676Sjpk 	} else if (strcmp(tt->ttype, NS_LDAP_TYPE_TNRHDB) == 0) {
41511676Sjpk 		/*
41521676Sjpk 		 * tnrhtp entries are ipTnet entries with SolarisAttrKeyValue
41531676Sjpk 		 */
41541676Sjpk 		(void) snprintf(filter, sizeof (filter),
41551676Sjpk 		    "(&(objectclass=%s)(SolarisAttrKeyValue=*)))",
41561676Sjpk 		    tt->objclass);
41571676Sjpk 	} else {
41580Sstevel@tonic-gate 		(void) snprintf(filter, sizeof (filter),
41590Sstevel@tonic-gate 		    "(objectclass=%s)", tt->objclass);
41601676Sjpk 	}
41610Sstevel@tonic-gate 
41620Sstevel@tonic-gate 	if (flags & F_VERBOSE)
41630Sstevel@tonic-gate 		(void) fprintf(stdout, gettext("FILTER = %s\n"), filter);
41640Sstevel@tonic-gate 
41650Sstevel@tonic-gate 	/* Pass cred only if supplied. Cred is not always needed for dump */
41660Sstevel@tonic-gate 	if (authority.cred.unix_cred.userID == NULL ||
41670Sstevel@tonic-gate 	    authority.cred.unix_cred.passwd == NULL)
4168*12758SJulian.Pullen@Sun.COM 		rc = __ns_ldap_firstEntry(service, filter, tt->sortattr, NULL,
4169*12758SJulian.Pullen@Sun.COM 		    NULL, NULL, NS_LDAP_HARD, &cookie, &eres, &err, NULL);
41700Sstevel@tonic-gate 	else
4171*12758SJulian.Pullen@Sun.COM 		rc = __ns_ldap_firstEntry(service, filter, tt->sortattr, NULL,
4172*12758SJulian.Pullen@Sun.COM 		    NULL, &authority, NS_LDAP_HARD, &cookie, &eres, &err, NULL);
41730Sstevel@tonic-gate 
41740Sstevel@tonic-gate 	switch (rc) {
41750Sstevel@tonic-gate 	case NS_LDAP_SUCCESS:
41760Sstevel@tonic-gate 		nent_add++;
41770Sstevel@tonic-gate 		success = 1;
41780Sstevel@tonic-gate 		if (eres != NULL) {
41790Sstevel@tonic-gate 			if (strcmp(databasetype, "publickey") == 0)
41800Sstevel@tonic-gate 				dump_publickey(eres, service);
41810Sstevel@tonic-gate 			else
41820Sstevel@tonic-gate 				(*(tt->dump))(eres);
41830Sstevel@tonic-gate 		}
41840Sstevel@tonic-gate 		else
41850Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("No entries found.\n"));
41860Sstevel@tonic-gate 		break;
41870Sstevel@tonic-gate 
41880Sstevel@tonic-gate 	case NS_LDAP_OP_FAILED:
41890Sstevel@tonic-gate 		exit_val = 2;
41900Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("operation failed.\n"));
41910Sstevel@tonic-gate 		break;
41920Sstevel@tonic-gate 
41930Sstevel@tonic-gate 	case NS_LDAP_INVALID_PARAM:
41940Sstevel@tonic-gate 		exit_val = 2;
41950Sstevel@tonic-gate 		(void) fprintf(stderr,
41960Sstevel@tonic-gate 		    gettext("invalid parameter(s) passed.\n"));
41970Sstevel@tonic-gate 		break;
41980Sstevel@tonic-gate 
41990Sstevel@tonic-gate 	case NS_LDAP_NOTFOUND:
42000Sstevel@tonic-gate 		exit_val = 2;
42010Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("entry not found.\n"));
42020Sstevel@tonic-gate 		break;
42030Sstevel@tonic-gate 
42040Sstevel@tonic-gate 	case NS_LDAP_MEMORY:
42050Sstevel@tonic-gate 		exit_val = 2;
42060Sstevel@tonic-gate 		(void) fprintf(stderr,
42076842Sth160488 		    gettext("internal memory allocation error.\n"));
42080Sstevel@tonic-gate 		break;
42090Sstevel@tonic-gate 
42100Sstevel@tonic-gate 	case NS_LDAP_CONFIG:
42110Sstevel@tonic-gate 		exit_val = 2;
42120Sstevel@tonic-gate 		(void) fprintf(stderr,
42136842Sth160488 		    gettext("LDAP Configuration problem.\n"));
42140Sstevel@tonic-gate 		perr(err);
42150Sstevel@tonic-gate 		break;
42160Sstevel@tonic-gate 
42170Sstevel@tonic-gate 	case NS_LDAP_PARTIAL:
42180Sstevel@tonic-gate 		exit_val = 2;
42190Sstevel@tonic-gate 		(void) fprintf(stderr,
42206842Sth160488 		    gettext("partial result returned\n"));
42210Sstevel@tonic-gate 		perr(err);
42220Sstevel@tonic-gate 		break;
42230Sstevel@tonic-gate 
42240Sstevel@tonic-gate 	case NS_LDAP_INTERNAL:
42250Sstevel@tonic-gate 		exit_val = 2;
42260Sstevel@tonic-gate 		(void) fprintf(stderr,
42276842Sth160488 		    gettext("internal LDAP error occured.\n"));
42280Sstevel@tonic-gate 		perr(err);
42290Sstevel@tonic-gate 		break;
42300Sstevel@tonic-gate 	}
42310Sstevel@tonic-gate 
42320Sstevel@tonic-gate 	if (eres != NULL) {
42330Sstevel@tonic-gate 		(void) __ns_ldap_freeResult(&eres);
42340Sstevel@tonic-gate 		eres = NULL;
42350Sstevel@tonic-gate 	}
42360Sstevel@tonic-gate 
42370Sstevel@tonic-gate 	if (success) {
42380Sstevel@tonic-gate 		while (!done) {
42390Sstevel@tonic-gate 			rc = __ns_ldap_nextEntry(cookie, &eres, &err);
42400Sstevel@tonic-gate 			if (rc != NS_LDAP_SUCCESS || eres  == NULL) {
42410Sstevel@tonic-gate 				done = 1;
42420Sstevel@tonic-gate 				continue;
42430Sstevel@tonic-gate 			}
42440Sstevel@tonic-gate 
42450Sstevel@tonic-gate 			/* Print the result */
42460Sstevel@tonic-gate 			if (eres != NULL) {
42470Sstevel@tonic-gate 				if (strcmp(databasetype, "publickey") == 0)
42480Sstevel@tonic-gate 					dump_publickey(eres, service);
42490Sstevel@tonic-gate 				else
42500Sstevel@tonic-gate 					(*(tt->dump))(eres);
42510Sstevel@tonic-gate 				(void) __ns_ldap_freeResult(&eres);
42520Sstevel@tonic-gate 				eres = NULL;
42530Sstevel@tonic-gate 			}
42540Sstevel@tonic-gate 		}
42550Sstevel@tonic-gate 	}
42560Sstevel@tonic-gate }
42570Sstevel@tonic-gate 
4258702Sth160488 int
main(int argc,char ** argv)42590Sstevel@tonic-gate main(int argc, char **argv)
42600Sstevel@tonic-gate {
42616842Sth160488 	char			*password;
42626842Sth160488 	ns_standalone_conf_t	standalone_cfg = standaloneDefaults;
42636842Sth160488 	int			c;
42646842Sth160488 	int			rc;
42656842Sth160488 	int			ldaprc;
42666842Sth160488 	int			authstried = 0;
42676842Sth160488 	int			op = OP_ADD;
42686842Sth160488 	char			*ttype, *authmech = 0, *etcfile = 0;
42696842Sth160488 	/* Temporary password variable */
42706842Sth160488 	char			ps[LDAP_MAXNAMELEN];
42716842Sth160488 	char			filter[BUFSIZ];
42726842Sth160488 	void			**paramVal = NULL;
42736842Sth160488 	ns_auth_t		**app;
42746842Sth160488 	ns_auth_t		**authpp = NULL;
42756842Sth160488 	ns_auth_t		*authp = NULL;
42766842Sth160488 	ns_ldap_error_t		*errorp = NULL;
42776842Sth160488 	ns_ldap_result_t	*resultp;
42786842Sth160488 	ns_ldap_entry_t		*e;
42796842Sth160488 	int			flag = 0;
42806842Sth160488 	int			version1 = 0;
42810Sstevel@tonic-gate 
42820Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
42830Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
42840Sstevel@tonic-gate 
42850Sstevel@tonic-gate 	openlog("ldapaddent", LOG_PID, LOG_USER);
42860Sstevel@tonic-gate 
42870Sstevel@tonic-gate 	inputbasedn = NULL;
42880Sstevel@tonic-gate 	authority.cred.unix_cred.passwd = NULL;
42890Sstevel@tonic-gate 	authority.cred.unix_cred.userID = NULL;
42900Sstevel@tonic-gate 	authority.auth.type = NS_LDAP_AUTH_SIMPLE;
42910Sstevel@tonic-gate 
42926842Sth160488 	while ((c = getopt(argc, argv, "cdh:N:M:vpf:D:w:j:b:a:P:r:")) != EOF) {
42930Sstevel@tonic-gate 		switch (c) {
42940Sstevel@tonic-gate 		case 'd':
42950Sstevel@tonic-gate 			if (op)
42966842Sth160488 				usage(gettext(
42976842Sth160488 				    "no other option should be specified"));
42980Sstevel@tonic-gate 			op = OP_DUMP;
42990Sstevel@tonic-gate 			break;
43000Sstevel@tonic-gate 		case 'c':
43010Sstevel@tonic-gate 			continue_onerror = 1;
43020Sstevel@tonic-gate 			break;
43030Sstevel@tonic-gate 		case 'v':
43040Sstevel@tonic-gate 			flags |= F_VERBOSE;
43050Sstevel@tonic-gate 			break;
43060Sstevel@tonic-gate 		case 'p':
43070Sstevel@tonic-gate 			flags |= F_PASSWD;
43080Sstevel@tonic-gate 			break;
43096842Sth160488 		case 'M':
43106842Sth160488 			standalone_cfg.type = NS_LDAP_SERVER;
43116842Sth160488 			standalone_cfg.SA_DOMAIN = optarg;
43126842Sth160488 			break;
43136842Sth160488 		case 'h':
43146842Sth160488 			standalone_cfg.type = NS_LDAP_SERVER;
43156842Sth160488 			if (separatePort(optarg,
43166842Sth160488 			    &standalone_cfg.SA_SERVER,
43176842Sth160488 			    &standalone_cfg.SA_PORT) > 0) {
43186842Sth160488 				exit(1);
43196842Sth160488 			}
43206842Sth160488 			break;
43216842Sth160488 		case 'P':
43226842Sth160488 			standalone_cfg.type = NS_LDAP_SERVER;
43236842Sth160488 			authority.hostcertpath = optarg;
43246842Sth160488 			break;
43256842Sth160488 		case 'N':
43266842Sth160488 			standalone_cfg.type = NS_LDAP_SERVER;
43276842Sth160488 			standalone_cfg.SA_PROFILE_NAME = optarg;
43286842Sth160488 			break;
43290Sstevel@tonic-gate 		case 'f':
43300Sstevel@tonic-gate 			etcfile = optarg;
43310Sstevel@tonic-gate 			break;
43320Sstevel@tonic-gate 		case 'D':
43330Sstevel@tonic-gate 			authority.cred.unix_cred.userID = strdup(optarg);
43340Sstevel@tonic-gate 			break;
43350Sstevel@tonic-gate 		case 'w':
43366842Sth160488 			if (authority.cred.unix_cred.passwd) {
43376842Sth160488 				(void) fprintf(stderr,
43386842Sth160488 				    gettext("Warning: The -w option is mutually"
43396842Sth160488 				    " exclusive of -j. -w is ignored.\n"));
43406842Sth160488 				break;
43416842Sth160488 			}
43426842Sth160488 
43436842Sth160488 			if (optarg != NULL &&
43446842Sth160488 			    optarg[0] == '-' && optarg[1] == '\0') {
43456842Sth160488 				/* Ask for a password later */
43466842Sth160488 				break;
43476842Sth160488 			}
43486842Sth160488 
43490Sstevel@tonic-gate 			authority.cred.unix_cred.passwd = strdup(optarg);
43500Sstevel@tonic-gate 			break;
43516842Sth160488 		case 'j':
43526842Sth160488 			if (authority.cred.unix_cred.passwd != NULL) {
43536842Sth160488 				(void) fprintf(stderr,
43546842Sth160488 				    gettext("The -w option is mutually "
43556842Sth160488 				    "exclusive of -j. -w is ignored.\n"));
43566842Sth160488 				free(authority.cred.unix_cred.passwd);
43576842Sth160488 			}
43586842Sth160488 			authority.cred.unix_cred.passwd = readPwd(optarg);
43596842Sth160488 			if (authority.cred.unix_cred.passwd == NULL) {
43606842Sth160488 				exit(1);
43616842Sth160488 			}
43626842Sth160488 			break;
43630Sstevel@tonic-gate 		case 'b':
43640Sstevel@tonic-gate 			inputbasedn = strdup(optarg);
43650Sstevel@tonic-gate 			break;
43660Sstevel@tonic-gate 		case 'a':
43670Sstevel@tonic-gate 			authmech = strdup(optarg);
43680Sstevel@tonic-gate 			break;
43690Sstevel@tonic-gate 		default:
43700Sstevel@tonic-gate 			usage(gettext("Invalid option"));
43710Sstevel@tonic-gate 		}
43720Sstevel@tonic-gate 	}
43730Sstevel@tonic-gate 
43746842Sth160488 	if (standalone_cfg.type == NS_LDAP_SERVER &&
43756842Sth160488 	    standalone_cfg.SA_SERVER == NULL) {
43766842Sth160488 		(void) fprintf(stderr,
43776842Sth160488 		    gettext("Please specify an LDAP server you want "
43786842Sth160488 		    "to connect to. \n"));
43796842Sth160488 		exit(1);
43806842Sth160488 	}
43816842Sth160488 
43826842Sth160488 	if (authmech != NULL) {
43836842Sth160488 		if (__ns_ldap_initAuth(authmech, &authority.auth, &errorp) !=
43846842Sth160488 		    NS_LDAP_SUCCESS) {
43856842Sth160488 			if (errorp) {
43866842Sth160488 				(void) fprintf(stderr, "%s", errorp->message);
43876842Sth160488 				(void) __ns_ldap_freeError(&errorp);
43886842Sth160488 			}
43896842Sth160488 			exit(1);
43906842Sth160488 		}
43916842Sth160488 	}
43926842Sth160488 
43936842Sth160488 	if (authority.auth.saslmech != NS_LDAP_SASL_GSSAPI &&
43946842Sth160488 	    authority.cred.unix_cred.userID == NULL &&
43956842Sth160488 	    op != OP_DUMP) {
43960Sstevel@tonic-gate 	    /* This is not an optional parameter. Exit */
43970Sstevel@tonic-gate 		(void) fprintf(stderr,
43986842Sth160488 		    gettext("DN must be specified unless SASL/GSSAPI is used."
43996842Sth160488 		    " Use option -D.\n"));
44000Sstevel@tonic-gate 		exit(1);
44010Sstevel@tonic-gate 	}
44020Sstevel@tonic-gate 
44036842Sth160488 	if (authority.auth.saslmech != NS_LDAP_SASL_GSSAPI &&
44046842Sth160488 	    authority.cred.unix_cred.passwd == NULL &&
44056842Sth160488 	    (op != OP_DUMP ||
44066842Sth160488 	    standalone_cfg.type != NS_CACHEMGR &&
44076842Sth160488 	    authority.cred.unix_cred.userID != NULL)) {
44080Sstevel@tonic-gate 		/* If password is not specified, then prompt user for it. */
44090Sstevel@tonic-gate 		password = getpassphrase("Enter password:");
44100Sstevel@tonic-gate 		(void) strcpy(ps, password);
44110Sstevel@tonic-gate 		authority.cred.unix_cred.passwd = strdup(ps);
44120Sstevel@tonic-gate 	}
44130Sstevel@tonic-gate 
44146842Sth160488 	standalone_cfg.SA_AUTH = authmech == NULL ? NULL : &authority.auth;
44156842Sth160488 	standalone_cfg.SA_CERT_PATH = authority.hostcertpath;
44166842Sth160488 	standalone_cfg.SA_BIND_DN = authority.cred.unix_cred.userID;
44176842Sth160488 	standalone_cfg.SA_BIND_PWD = authority.cred.unix_cred.passwd;
44186842Sth160488 
44196842Sth160488 	if (__ns_ldap_initStandalone(&standalone_cfg,
44206842Sth160488 	    &errorp) != NS_LDAP_SUCCESS) {
44216842Sth160488 		if (errorp) {
44226842Sth160488 			(void) fprintf(stderr, "%s", errorp->message);
44232830Sdjl 		}
44242830Sdjl 		exit(1);
44252830Sdjl 	}
44262830Sdjl 
44270Sstevel@tonic-gate 	if (authmech == NULL) {
44280Sstevel@tonic-gate 		ldaprc = __ns_ldap_getParam(NS_LDAP_AUTH_P, (void ***)&authpp,
44296842Sth160488 		    &errorp);
44300Sstevel@tonic-gate 		if (ldaprc != NS_LDAP_SUCCESS ||
44310Sstevel@tonic-gate 		    (authpp == NULL && op != OP_DUMP)) {
44320Sstevel@tonic-gate 			(void) fprintf(stderr,
44330Sstevel@tonic-gate 			    gettext("No legal authentication method "
44340Sstevel@tonic-gate 			    "configured.\n"));
44350Sstevel@tonic-gate 			(void) fprintf(stderr,
44360Sstevel@tonic-gate 			    gettext("Provide a legal authentication method "
44370Sstevel@tonic-gate 			    "using -a option\n"));
44380Sstevel@tonic-gate 			exit(1);
44390Sstevel@tonic-gate 		}
44400Sstevel@tonic-gate 
44410Sstevel@tonic-gate 		/* Use the first authentication method which is not none */
44420Sstevel@tonic-gate 		for (app = authpp; *app; app++) {
44430Sstevel@tonic-gate 			authp = *app;
44440Sstevel@tonic-gate 			if (authp->type != NS_LDAP_AUTH_NONE) {
44450Sstevel@tonic-gate 				authstried++;
44460Sstevel@tonic-gate 				authority.auth.type = authp->type;
44470Sstevel@tonic-gate 				authority.auth.tlstype = authp->tlstype;
44480Sstevel@tonic-gate 				authority.auth.saslmech = authp->saslmech;
44490Sstevel@tonic-gate 				authority.auth.saslopt = authp->saslopt;
44500Sstevel@tonic-gate 				break;
44510Sstevel@tonic-gate 			}
44520Sstevel@tonic-gate 		}
44530Sstevel@tonic-gate 		if (authstried == 0 && op != OP_DUMP) {
44540Sstevel@tonic-gate 			(void) fprintf(stderr,
44556842Sth160488 			    gettext("No legal authentication method configured."
44566842Sth160488 			    "\nProvide a legal authentication method using "
44576842Sth160488 			    "-a option"));
44580Sstevel@tonic-gate 			exit(1);
44590Sstevel@tonic-gate 		}
44602830Sdjl 		if (authority.auth.saslmech == NS_LDAP_SASL_GSSAPI &&
44616842Sth160488 		    authority.cred.unix_cred.passwd != NULL &&
44626842Sth160488 		    authority.cred.unix_cred.userID != NULL) {
44632830Sdjl 			/*
44642830Sdjl 			 * -a is not specified and the auth method sasl/GSSAPI
44652830Sdjl 			 * is defined in the configuration of the ldap profile.
44662830Sdjl 			 * Even -D and -w is provided it's not valid usage.
44676842Sth160488 			 * Drop them on the floor.
44682830Sdjl 			 */
44692830Sdjl 
44702830Sdjl 			(void) fprintf(stderr,
44716842Sth160488 			    gettext("The default authentication is "
44726842Sth160488 			    "sasl/GSSAPI.\n"
44736842Sth160488 			    "The bind DN and password will be ignored.\n"));
44746842Sth160488 			authority.cred.unix_cred.passwd = NULL;
44756842Sth160488 			authority.cred.unix_cred.userID = NULL;
44762830Sdjl 		}
44770Sstevel@tonic-gate 	}
44780Sstevel@tonic-gate 
44790Sstevel@tonic-gate 	ttype = argv[optind++];
44800Sstevel@tonic-gate 
44810Sstevel@tonic-gate 	if (ttype == NULL) {
44820Sstevel@tonic-gate 		usage(gettext("No database type specified"));
44830Sstevel@tonic-gate 		exit(1);
44840Sstevel@tonic-gate 	}
44850Sstevel@tonic-gate 
44860Sstevel@tonic-gate 	if (strncasecmp(ttype, "automount", 9) == 0) {
44870Sstevel@tonic-gate 		(void) fprintf(stderr,
44880Sstevel@tonic-gate 		    gettext("automount is not a valid service for ldapaddent.\n"
44890Sstevel@tonic-gate 		    "Please use auto_*.\n"
44900Sstevel@tonic-gate 		    "e.g.  auto_home, auto_ws etc.\n "));
44910Sstevel@tonic-gate 		exit(1);
44920Sstevel@tonic-gate 	}
44930Sstevel@tonic-gate 
44940Sstevel@tonic-gate 	for (tt = ttypelist; tt->ttype; tt++) {
44950Sstevel@tonic-gate 		if (strcmp(tt->ttype, ttype) == 0)
44960Sstevel@tonic-gate 			break;
44970Sstevel@tonic-gate 		if (strcmp(tt->ttype, NS_LDAP_TYPE_AUTOMOUNT) == 0 &&
44980Sstevel@tonic-gate 		    strncmp(ttype, NS_LDAP_TYPE_AUTOMOUNT,
44990Sstevel@tonic-gate 		    sizeof (NS_LDAP_TYPE_AUTOMOUNT) - 1) == 0)
45000Sstevel@tonic-gate 			break;
45010Sstevel@tonic-gate 	}
45020Sstevel@tonic-gate 
45030Sstevel@tonic-gate 	if (tt->ttype == 0) {
45040Sstevel@tonic-gate 		(void) fprintf(stderr,
45050Sstevel@tonic-gate 		    gettext("database %s not supported;"
45060Sstevel@tonic-gate 		    " supported databases are:\n"), ttype);
45070Sstevel@tonic-gate 		for (tt = ttypelist; tt->ttype; tt++)
45080Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("\t%s\n"), tt->ttype);
45090Sstevel@tonic-gate 		exit(1);
45100Sstevel@tonic-gate 	}
45110Sstevel@tonic-gate 
45120Sstevel@tonic-gate 	if (flags & F_VERBOSE)
45130Sstevel@tonic-gate 		(void) fprintf(stdout, gettext("SERVICE = %s\n"), tt->ttype);
45140Sstevel@tonic-gate 
45150Sstevel@tonic-gate 	databasetype = ttype;
45160Sstevel@tonic-gate 
45170Sstevel@tonic-gate 	if (strcmp(tt->ttype, NS_LDAP_TYPE_AUTOMOUNT) == 0) {
45180Sstevel@tonic-gate 		paramVal = NULL;
45190Sstevel@tonic-gate 		errorp = NULL;
45200Sstevel@tonic-gate 		rc = __ns_ldap_getParam(NS_LDAP_FILE_VERSION_P, &paramVal,
45216842Sth160488 		    &errorp);
45220Sstevel@tonic-gate 		if (paramVal && *paramVal &&
45236842Sth160488 		    strcasecmp(*paramVal, NS_LDAP_VERSION_1) == 0)
45240Sstevel@tonic-gate 			version1 = 1;
45250Sstevel@tonic-gate 		if (paramVal)
45260Sstevel@tonic-gate 			(void) __ns_ldap_freeParam(&paramVal);
45270Sstevel@tonic-gate 		if (errorp)
45280Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&errorp);
45290Sstevel@tonic-gate 	}
45300Sstevel@tonic-gate 
45310Sstevel@tonic-gate 	/* Check if the container exists in first place */
45320Sstevel@tonic-gate 	(void) strcpy(&filter[0], "(objectclass=*)");
45330Sstevel@tonic-gate 
45340Sstevel@tonic-gate 	rc = __ns_ldap_list(databasetype, filter, NULL, (const char **)NULL,
45350Sstevel@tonic-gate 	    NULL, NS_LDAP_SCOPE_BASE, &resultp, &errorp, NULL, NULL);
45360Sstevel@tonic-gate 
45370Sstevel@tonic-gate 	/* create a container for auto_* if it does not exist already */
45380Sstevel@tonic-gate 	if ((rc == NS_LDAP_NOTFOUND) && (op == OP_ADD) &&
45390Sstevel@tonic-gate 	    (strcmp(tt->ttype, NS_LDAP_TYPE_AUTOMOUNT) == 0)) {
45400Sstevel@tonic-gate 		static	char *oclist[] = {NULL, "top", NULL};
45410Sstevel@tonic-gate 		if (version1)
45420Sstevel@tonic-gate 			oclist[0] = "nisMap";
45430Sstevel@tonic-gate 		else
45440Sstevel@tonic-gate 			oclist[0] = "automountMap";
45450Sstevel@tonic-gate 		e = __s_mk_entry(oclist, 3);
45460Sstevel@tonic-gate 		if (e == NULL) {
45470Sstevel@tonic-gate 			(void) fprintf(stderr,
45480Sstevel@tonic-gate 			    gettext("internal memory allocation error.\n"));
45490Sstevel@tonic-gate 			exit(1);
45500Sstevel@tonic-gate 		}
45510Sstevel@tonic-gate 		if (__s_add_attr(e,
45520Sstevel@tonic-gate 		    version1 ? "nisMapName" : "automountMapName",
45530Sstevel@tonic-gate 		    databasetype) != NS_LDAP_SUCCESS) {
45540Sstevel@tonic-gate 			(void) fprintf(stderr,
45550Sstevel@tonic-gate 			    gettext("internal memory allocation error.\n"));
45560Sstevel@tonic-gate 			ldap_freeEntry(e);
45570Sstevel@tonic-gate 			exit(1);
45580Sstevel@tonic-gate 		}
45590Sstevel@tonic-gate 
45600Sstevel@tonic-gate 		if (inputbasedn == NULL) {
45610Sstevel@tonic-gate 			if (get_basedn(databasetype, &inputbasedn) !=
45620Sstevel@tonic-gate 			    NS_LDAP_SUCCESS) {
45630Sstevel@tonic-gate 				(void) fprintf(stderr,
45640Sstevel@tonic-gate 				    gettext("Could not obtain basedn\n"));
45650Sstevel@tonic-gate 				ldap_freeEntry(e);
45660Sstevel@tonic-gate 				exit(1);
45670Sstevel@tonic-gate 			}
45680Sstevel@tonic-gate 		}
45690Sstevel@tonic-gate 		if (__ns_ldap_addEntry(databasetype, inputbasedn, e,
45700Sstevel@tonic-gate 		    &authority, flag, &errorp) != NS_LDAP_SUCCESS) {
45710Sstevel@tonic-gate 			(void) fprintf(stderr,
45720Sstevel@tonic-gate 			    gettext("Could not create container for %s\n"),
45730Sstevel@tonic-gate 			    databasetype);
45740Sstevel@tonic-gate 			ldap_freeEntry(e);
45750Sstevel@tonic-gate 		}
45760Sstevel@tonic-gate 	} else if (strcmp(databasetype, "publickey") != 0) {
45770Sstevel@tonic-gate 		if (rc == NS_LDAP_NOTFOUND) {
45780Sstevel@tonic-gate 			(void) fprintf(stderr,
45790Sstevel@tonic-gate 			    gettext("Container %s does not exist\n"),
45800Sstevel@tonic-gate 			    databasetype);
45810Sstevel@tonic-gate 			exit(1);
45820Sstevel@tonic-gate 		}
45830Sstevel@tonic-gate 	}
45840Sstevel@tonic-gate 
45850Sstevel@tonic-gate 	if (op == OP_DUMP) {
45860Sstevel@tonic-gate 		if (strcmp(databasetype, "publickey") == 0) {
45870Sstevel@tonic-gate 			dumptable("hosts");
45880Sstevel@tonic-gate 			dumptable("passwd");
45890Sstevel@tonic-gate 		} else {
45900Sstevel@tonic-gate 			dumptable(databasetype);
45910Sstevel@tonic-gate 		}
45920Sstevel@tonic-gate 		exit(exit_val);
45930Sstevel@tonic-gate 	}
45940Sstevel@tonic-gate 
45950Sstevel@tonic-gate 	if (etcfile) {
45960Sstevel@tonic-gate 		if ((etcf = fopen(etcfile, "r")) == 0) {
45970Sstevel@tonic-gate 			(void) fprintf(stderr,
45980Sstevel@tonic-gate 			    gettext("can't open file %s\n"), etcfile);
45990Sstevel@tonic-gate 			exit(1);
46000Sstevel@tonic-gate 		}
46010Sstevel@tonic-gate 	} else {
46020Sstevel@tonic-gate 		etcfile = "stdin";
46030Sstevel@tonic-gate 		etcf = stdin;
46040Sstevel@tonic-gate 	}
46050Sstevel@tonic-gate 
46060Sstevel@tonic-gate 	if (op == OP_ADD) {
46070Sstevel@tonic-gate 		(void) addfile();
46080Sstevel@tonic-gate 		(void) fprintf(stdout, gettext("%d entries added\n"), nent_add);
46090Sstevel@tonic-gate 	}
46100Sstevel@tonic-gate 
46116842Sth160488 	__ns_ldap_cancelStandalone();
4612839Smj162486 	/* exit() -> return for make lint */
4613839Smj162486 	return (exit_val);
46140Sstevel@tonic-gate }
46150Sstevel@tonic-gate 
46160Sstevel@tonic-gate 
46170Sstevel@tonic-gate /*
46180Sstevel@tonic-gate  * This is called when service == auto_*.
46190Sstevel@tonic-gate  * It calls __ns_ldap_getSearchDescriptors
46200Sstevel@tonic-gate  * to generate the dn from SSD's base dn.
46210Sstevel@tonic-gate  * If there is no SSD available,
46220Sstevel@tonic-gate  * default base dn will be used
46230Sstevel@tonic-gate  * Only the first baseDN in the SSD is used
46240Sstevel@tonic-gate  */
46250Sstevel@tonic-gate 
get_basedn(char * service,char ** basedn)46260Sstevel@tonic-gate static int get_basedn(char *service, char **basedn) {
46270Sstevel@tonic-gate 	int rc = NS_LDAP_SUCCESS;
46280Sstevel@tonic-gate 	char *dn = NULL;
46290Sstevel@tonic-gate 	ns_ldap_search_desc_t **desc = NULL;
46300Sstevel@tonic-gate 	ns_ldap_error_t *errp = NULL;
46310Sstevel@tonic-gate 	void		**paramVal = NULL;
46320Sstevel@tonic-gate 	int		prepend_automountmapname = FALSE;
46330Sstevel@tonic-gate 
46340Sstevel@tonic-gate 	/*
46350Sstevel@tonic-gate 	 * Get auto_* SSD first
46360Sstevel@tonic-gate 	 */
46370Sstevel@tonic-gate 
46380Sstevel@tonic-gate 	if ((rc = __ns_ldap_getSearchDescriptors(
46390Sstevel@tonic-gate 			(const char *) service,
46400Sstevel@tonic-gate 			&desc, &errp))  == NS_LDAP_SUCCESS &&
46410Sstevel@tonic-gate 		desc != NULL) {
46420Sstevel@tonic-gate 
46430Sstevel@tonic-gate 		if (desc[0] != NULL && desc[0]->basedn != NULL) {
46440Sstevel@tonic-gate 			dn = strdup(desc[0]->basedn);
46450Sstevel@tonic-gate 			if (dn == NULL) {
46460Sstevel@tonic-gate 				(void) __ns_ldap_freeSearchDescriptors
46470Sstevel@tonic-gate 						(&desc);
46480Sstevel@tonic-gate 				return (NS_LDAP_MEMORY);
46490Sstevel@tonic-gate 			}
46500Sstevel@tonic-gate 		}
46510Sstevel@tonic-gate 	}
46520Sstevel@tonic-gate 
46530Sstevel@tonic-gate 	/* clean up */
46540Sstevel@tonic-gate 	if (desc) (void) __ns_ldap_freeSearchDescriptors(&desc);
46550Sstevel@tonic-gate 	if (errp) (void) __ns_ldap_freeError(&errp);
46560Sstevel@tonic-gate 
46570Sstevel@tonic-gate 	/*
46580Sstevel@tonic-gate 	 * If no dn is duplicated from auto_* SSD, try automount SSD
46590Sstevel@tonic-gate 	 */
46600Sstevel@tonic-gate 	if (dn == NULL) {
46610Sstevel@tonic-gate 		if ((rc = __ns_ldap_getSearchDescriptors(
46620Sstevel@tonic-gate 				"automount", &desc, &errp))
46630Sstevel@tonic-gate 				== NS_LDAP_SUCCESS && desc != NULL) {
46640Sstevel@tonic-gate 
46650Sstevel@tonic-gate 			if (desc[0] != NULL && desc[0]->basedn != NULL) {
46660Sstevel@tonic-gate 				dn = strdup(desc[0]->basedn);
46670Sstevel@tonic-gate 				if (dn == NULL) {
46680Sstevel@tonic-gate 					(void) __ns_ldap_freeSearchDescriptors
46690Sstevel@tonic-gate 							(&desc);
46700Sstevel@tonic-gate 					return (NS_LDAP_MEMORY);
46710Sstevel@tonic-gate 				}
46720Sstevel@tonic-gate 				prepend_automountmapname = TRUE;
46730Sstevel@tonic-gate 			}
46740Sstevel@tonic-gate 		}
46750Sstevel@tonic-gate 		/* clean up */
46760Sstevel@tonic-gate 		if (desc) (void) __ns_ldap_freeSearchDescriptors(&desc);
46770Sstevel@tonic-gate 		if (errp) (void) __ns_ldap_freeError(&errp);
46780Sstevel@tonic-gate 	}
46790Sstevel@tonic-gate 
46800Sstevel@tonic-gate 	/*
46810Sstevel@tonic-gate 	 * If no dn is duplicated from auto_* or automount SSD,
46820Sstevel@tonic-gate 	 * use default DN
46830Sstevel@tonic-gate 	 */
46840Sstevel@tonic-gate 
46850Sstevel@tonic-gate 	if (dn == NULL) {
46860Sstevel@tonic-gate 		if ((rc = __ns_ldap_getParam(NS_LDAP_SEARCH_BASEDN_P,
46870Sstevel@tonic-gate 			&paramVal, &errp)) == NS_LDAP_SUCCESS) {
46880Sstevel@tonic-gate 			dn = strdup((char *)paramVal[0]);
46890Sstevel@tonic-gate 			if (dn == NULL) {
46900Sstevel@tonic-gate 				(void) __ns_ldap_freeParam(&paramVal);
46910Sstevel@tonic-gate 				return (NS_LDAP_MEMORY);
46920Sstevel@tonic-gate 			}
46930Sstevel@tonic-gate 			prepend_automountmapname = TRUE;
46940Sstevel@tonic-gate 		}
46950Sstevel@tonic-gate 		if (paramVal) (void) __ns_ldap_freeParam(&paramVal);
46960Sstevel@tonic-gate 		if (errp) (void) __ns_ldap_freeError(&errp);
46970Sstevel@tonic-gate 	}
46980Sstevel@tonic-gate 
46990Sstevel@tonic-gate 
47000Sstevel@tonic-gate 	if (dn == NULL) {
47010Sstevel@tonic-gate 		return (NS_LDAP_OP_FAILED);
47020Sstevel@tonic-gate 	} else {
47030Sstevel@tonic-gate 		/*
47040Sstevel@tonic-gate 		 * If dn is duplicated from
47050Sstevel@tonic-gate 		 * automount SSD basedn or
47060Sstevel@tonic-gate 		 * default base dn
47070Sstevel@tonic-gate 		 * then prepend automountMapName=auto_xxx
47080Sstevel@tonic-gate 		 */
47090Sstevel@tonic-gate 		if (prepend_automountmapname)
47100Sstevel@tonic-gate 			rc = __s_api_prepend_automountmapname_to_dn(
47110Sstevel@tonic-gate 				service, &dn, &errp);
47120Sstevel@tonic-gate 
47130Sstevel@tonic-gate 		if (rc != NS_LDAP_SUCCESS) {
47140Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&errp);
47150Sstevel@tonic-gate 			free(dn);
47160Sstevel@tonic-gate 			return (rc);
47170Sstevel@tonic-gate 		}
47180Sstevel@tonic-gate 
47190Sstevel@tonic-gate 		*basedn = dn;
47200Sstevel@tonic-gate 
47210Sstevel@tonic-gate 		return (NS_LDAP_SUCCESS);
47220Sstevel@tonic-gate 	}
47230Sstevel@tonic-gate }
47242830Sdjl static char *
h_errno2str(int h_errno)47252830Sdjl h_errno2str(int h_errno) {
47262830Sdjl 	switch (h_errno) {
47272830Sdjl 	case HOST_NOT_FOUND:
47282830Sdjl 		return ("HOST_NOT_FOUND");
47292830Sdjl 		break;
47302830Sdjl 	case TRY_AGAIN:
47312830Sdjl 		return ("TRY_AGAIN");
47322830Sdjl 		break;
47332830Sdjl 	case NO_RECOVERY:
47342830Sdjl 		return ("NO_RECOVERY");
47352830Sdjl 		break;
47362830Sdjl 	case NO_DATA:
47372830Sdjl 		return ("NO_DATA");
47382830Sdjl 		break;
47392830Sdjl 	default:
47402830Sdjl 		break;
47412830Sdjl 	}
47422830Sdjl 	return ("UNKNOWN_ERROR");
47432830Sdjl }
4744