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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23702Sth160488  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * ldapaddent.c
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * Utility to add /etc files into LDAP.
330Sstevel@tonic-gate  * Can also be used to dump entries from a ldap container in /etc format.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <libintl.h>
390Sstevel@tonic-gate #include <strings.h>
400Sstevel@tonic-gate #include <sys/param.h>
410Sstevel@tonic-gate #include <ctype.h>
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <sys/socket.h>
440Sstevel@tonic-gate #include <netinet/in.h>
450Sstevel@tonic-gate #include <arpa/inet.h>
460Sstevel@tonic-gate #include <locale.h>
470Sstevel@tonic-gate #include <syslog.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #undef opaque
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include <nss_dbdefs.h>
520Sstevel@tonic-gate #include <netdb.h>
530Sstevel@tonic-gate #include <rpc/rpcent.h>
540Sstevel@tonic-gate #include <grp.h>
550Sstevel@tonic-gate #include <pwd.h>
560Sstevel@tonic-gate #include <shadow.h>
570Sstevel@tonic-gate #include <sys/systeminfo.h>
580Sstevel@tonic-gate #include "ns_internal.h"
590Sstevel@tonic-gate #include "ldapaddent.h"
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	OP_ADD	0
620Sstevel@tonic-gate #define	OP_DUMP	3
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static struct ttypelist_t {
650Sstevel@tonic-gate 	char *ttype;		/* type tag */
660Sstevel@tonic-gate 	int (*genent)(char *, int(*)());
670Sstevel@tonic-gate 				/* routine to turn line into ldap entries */
680Sstevel@tonic-gate 	void (*dump)(ns_ldap_result_t *);
690Sstevel@tonic-gate 				/* routine to print ldap containers */
700Sstevel@tonic-gate 	int (*filedbmline)();	/* routine to turn file line into dbm line */
710Sstevel@tonic-gate 	char *objclass;		/* Objectclass for the servicetype */
720Sstevel@tonic-gate } *tt;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate char	parse_err_msg [PARSE_ERR_MSG_LEN];
750Sstevel@tonic-gate int	continue_onerror = 0;  /* do not exit on error */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static int get_basedn(char *service, char **basedn);
780Sstevel@tonic-gate static int check_ipaddr(char *addr, char **newaddr);
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 /* from ns_internal.h */
850Sstevel@tonic-gate extern	int __s_api_prepend_automountmapname_to_dn(
860Sstevel@tonic-gate 	const char *, char **, ns_ldap_error_t **);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static char	*inputbasedn = NULL;
890Sstevel@tonic-gate static char	*databasetype = NULL;
900Sstevel@tonic-gate static int	exit_val = 0;
910Sstevel@tonic-gate static unsigned	nent_add = 0;
920Sstevel@tonic-gate static FILE	*etcf = 0;
930Sstevel@tonic-gate static ns_cred_t	authority;
940Sstevel@tonic-gate unsigned	flags = 0;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static void
970Sstevel@tonic-gate perr(ns_ldap_error_t *e)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	if (e)
1000Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%d: %s\n"),
1010Sstevel@tonic-gate 				e->status, e->message);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate static int
1060Sstevel@tonic-gate ascii_to_int(char *str)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	int i;
1090Sstevel@tonic-gate 	char *c = str;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	if (c == NULL || *c == '\0')
1120Sstevel@tonic-gate 		return (-1);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	while (c != '\0' && *c == ' ')
1150Sstevel@tonic-gate 		c++;
1160Sstevel@tonic-gate 	if (*c == '\0')
1170Sstevel@tonic-gate 		return (-1);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	for (i = 0; i < strlen(c); i++)
1200Sstevel@tonic-gate 		if (!isdigit(c[i]))
1210Sstevel@tonic-gate 			return (-1);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	return (atoi(c));
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate  * Internet network address interpretation routine.
1280Sstevel@tonic-gate  * The library routines call this routine to interpret
1290Sstevel@tonic-gate  * network numbers.
1300Sstevel@tonic-gate  */
1310Sstevel@tonic-gate static in_addr_t
1320Sstevel@tonic-gate encode_network(const char *cp)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	in_addr_t val;
1350Sstevel@tonic-gate 	int base;
1360Sstevel@tonic-gate 	ptrdiff_t n;
1370Sstevel@tonic-gate 	char c;
1380Sstevel@tonic-gate 	in_addr_t parts[4], *pp = parts;
1390Sstevel@tonic-gate 	int i;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate again:
1420Sstevel@tonic-gate 	val = 0; base = 10;
1430Sstevel@tonic-gate 	if (*cp == '0') {
1440Sstevel@tonic-gate 		if (*++cp == 'x' || *cp == 'X')
1450Sstevel@tonic-gate 			base = 16, cp++;
1460Sstevel@tonic-gate 		else
1470Sstevel@tonic-gate 			base = 8;
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 	while ((c = *cp) != NULL) {
1500Sstevel@tonic-gate 		if (isdigit(c)) {
1510Sstevel@tonic-gate 			if ((c - '0') >= base)
1520Sstevel@tonic-gate 			    break;
1530Sstevel@tonic-gate 			val = (val * base) + (c - '0');
1540Sstevel@tonic-gate 			cp++;
1550Sstevel@tonic-gate 			continue;
1560Sstevel@tonic-gate 		}
1570Sstevel@tonic-gate 		if (base == 16 && isxdigit(c)) {
1580Sstevel@tonic-gate 			val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
1590Sstevel@tonic-gate 			cp++;
1600Sstevel@tonic-gate 			continue;
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 		break;
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 	if (*cp == '.') {
1650Sstevel@tonic-gate 		if (pp >= parts + 4)
1660Sstevel@tonic-gate 			return ((in_addr_t)-1);
1670Sstevel@tonic-gate 		*pp++ = val, cp++;
1680Sstevel@tonic-gate 		goto again;
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 	if (*cp && !isspace(*cp))
1710Sstevel@tonic-gate 		return ((in_addr_t)-1);
1720Sstevel@tonic-gate 	*pp++ = val;
1730Sstevel@tonic-gate 	n = pp - parts;
1740Sstevel@tonic-gate 	if (n > 4)
1750Sstevel@tonic-gate 		return ((in_addr_t)-1);
1760Sstevel@tonic-gate 	for (val = 0, i = 0; i < n; i++) {
1770Sstevel@tonic-gate 		val <<= 8;
1780Sstevel@tonic-gate 		val |= parts[i] & 0xff;
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 	for (/* no init */; i < 4; i++)
1810Sstevel@tonic-gate 		val <<= 8;
1820Sstevel@tonic-gate 	return (val);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate static void
1860Sstevel@tonic-gate replace_tab2space(char *str)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate 	int i = 0;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	while ((str) && (str[i])) {
1910Sstevel@tonic-gate 		if (str[i] == '\t')
1920Sstevel@tonic-gate 			str[i] = ' ';
1930Sstevel@tonic-gate 		i++;
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate static int
1980Sstevel@tonic-gate blankline(char *line)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate 	char *p;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	for (p = line; *p; p++)
2030Sstevel@tonic-gate 		if (*p != ' ' && *p != '\t')
2040Sstevel@tonic-gate 			return (0);
2050Sstevel@tonic-gate 	return (1);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate static void
2090Sstevel@tonic-gate line_buf_expand(struct line_buf *line)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	line->alloc += BUFSIZ;
2120Sstevel@tonic-gate 	line->str = (char *)realloc(line->str, line->alloc);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	if (line->str == NULL) {
2150Sstevel@tonic-gate 		(void) fprintf(stderr,
2160Sstevel@tonic-gate 		    gettext("line_buf_expand: out of memory\n"));
2170Sstevel@tonic-gate 		exit(1);
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate static void
2220Sstevel@tonic-gate line_buf_init(struct line_buf *line)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 	(void) memset((char *)line, 0, sizeof (*line));
2250Sstevel@tonic-gate 	line_buf_expand(line);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate static int
2290Sstevel@tonic-gate __s_add_attr(ns_ldap_entry_t *e, char *attrname, char *value)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate 	ns_ldap_attr_t	*a;
2320Sstevel@tonic-gate 	char		*v;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	a = (ns_ldap_attr_t *)calloc(1, sizeof (ns_ldap_attr_t));
2350Sstevel@tonic-gate 	if (a == NULL)
2360Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2370Sstevel@tonic-gate 	a->attrname = strdup(attrname);
2380Sstevel@tonic-gate 	if (a->attrname == NULL) {
2390Sstevel@tonic-gate 		free(a);
2400Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 	a->attrvalue = (char **)calloc(1, sizeof (char **));
2430Sstevel@tonic-gate 	if (a->attrvalue == NULL) {
2440Sstevel@tonic-gate 		free(a->attrname);
2450Sstevel@tonic-gate 		free(a);
2460Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 	a->value_count = 1;
2490Sstevel@tonic-gate 	a->attrvalue[0] = NULL;
2500Sstevel@tonic-gate 	v = strdup(value);
2510Sstevel@tonic-gate 	if (v == NULL) {
2520Sstevel@tonic-gate 		free(a->attrname);
2530Sstevel@tonic-gate 		free(a->attrvalue);
2540Sstevel@tonic-gate 		free(a);
2550Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 	a->attrvalue[0] = v;
2580Sstevel@tonic-gate 	e->attr_pair[e->attr_count] = a;
2590Sstevel@tonic-gate 	e->attr_count++;
2600Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate static int
2640Sstevel@tonic-gate __s_add_attrlist(ns_ldap_entry_t *e, char *attrname, char **argv)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate 	ns_ldap_attr_t	*a;
2670Sstevel@tonic-gate 	char		*v;
2680Sstevel@tonic-gate 	char		**av;
2690Sstevel@tonic-gate 	int		i, j;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	a = (ns_ldap_attr_t *)calloc(1, sizeof (ns_ldap_attr_t));
2720Sstevel@tonic-gate 	if (a == NULL)
2730Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2740Sstevel@tonic-gate 	a->attrname = strdup(attrname);
2750Sstevel@tonic-gate 	if (a->attrname == NULL) {
2760Sstevel@tonic-gate 		free(a);
2770Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	for (i = 0, av = argv; *av != NULL; av++, i++)
2810Sstevel@tonic-gate 		;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	a->attrvalue = (char **)calloc(i, sizeof (char **));
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	if (a->attrvalue == NULL) {
2860Sstevel@tonic-gate 		free(a->attrname);
2870Sstevel@tonic-gate 		free(a);
2880Sstevel@tonic-gate 		return (NS_LDAP_MEMORY);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 	a->value_count = i;
2910Sstevel@tonic-gate 	for (j = 0; j < i; j++) {
2920Sstevel@tonic-gate 		v = strdup(argv[j]);
2930Sstevel@tonic-gate 		if (v == NULL) {
2940Sstevel@tonic-gate 			free(a->attrname);
2950Sstevel@tonic-gate 			free(a->attrvalue);
2960Sstevel@tonic-gate 			free(a);
2970Sstevel@tonic-gate 			return (NS_LDAP_MEMORY);
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 		a->attrvalue[j] = v;
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 	e->attr_pair[e->attr_count] = a;
3020Sstevel@tonic-gate 	e->attr_count++;
3030Sstevel@tonic-gate 	return (NS_LDAP_SUCCESS);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate static ns_ldap_entry_t *
3070Sstevel@tonic-gate __s_mk_entry(char **objclass, int max_attr)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	ns_ldap_entry_t *e;
3100Sstevel@tonic-gate 	e = (ns_ldap_entry_t *)calloc(1, sizeof (ns_ldap_entry_t));
3110Sstevel@tonic-gate 	if (e == NULL)
3120Sstevel@tonic-gate 		return (NULL);
3130Sstevel@tonic-gate 	e->attr_pair = (ns_ldap_attr_t **)calloc(max_attr+1,
3140Sstevel@tonic-gate 						sizeof (ns_ldap_attr_t *));
3150Sstevel@tonic-gate 	if (e->attr_pair == NULL) {
3160Sstevel@tonic-gate 		free(e);
3170Sstevel@tonic-gate 		return (NULL);
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 	e->attr_count = 0;
3200Sstevel@tonic-gate 	if (__s_add_attrlist(e, "objectClass", objclass) != NS_LDAP_SUCCESS) {
3210Sstevel@tonic-gate 		free(e->attr_pair);
3220Sstevel@tonic-gate 		free(e);
3230Sstevel@tonic-gate 		return (NULL);
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 	return (e);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate static void
3290Sstevel@tonic-gate ldap_freeEntry(ns_ldap_entry_t *ep)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	int		j, k = 0;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	if (ep == NULL)
3340Sstevel@tonic-gate 		return;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	if (ep->attr_pair == NULL) {
3370Sstevel@tonic-gate 		free(ep);
3380Sstevel@tonic-gate 		return;
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 	for (j = 0; j < ep->attr_count; j++) {
3410Sstevel@tonic-gate 		if (ep->attr_pair[j] == NULL)
3420Sstevel@tonic-gate 			continue;
3430Sstevel@tonic-gate 		if (ep->attr_pair[j]->attrname)
3440Sstevel@tonic-gate 			free(ep->attr_pair[j]->attrname);
3450Sstevel@tonic-gate 		if (ep->attr_pair[j]->attrvalue) {
3460Sstevel@tonic-gate 			for (k = 0; (k < ep->attr_pair[j]->value_count) &&
3470Sstevel@tonic-gate 				    (ep->attr_pair[j]->attrvalue[k]); k++) {
3480Sstevel@tonic-gate 				free(ep->attr_pair[j]->attrvalue[k]);
3490Sstevel@tonic-gate 			}
3500Sstevel@tonic-gate 			free(ep->attr_pair[j]->attrvalue);
3510Sstevel@tonic-gate 		}
3520Sstevel@tonic-gate 		free(ep->attr_pair[j]);
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 	free(ep->attr_pair);
3550Sstevel@tonic-gate 	free(ep);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate static int
3590Sstevel@tonic-gate addentry(void *entry, int mod)
3600Sstevel@tonic-gate {
3610Sstevel@tonic-gate 	int		 result = 0;
3620Sstevel@tonic-gate 	ns_ldap_error_t	 *eres = NULL;
3630Sstevel@tonic-gate 	int		rc = 1;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/*  adds entry into the LDAP tree */
3670Sstevel@tonic-gate 	if (mod)
3680Sstevel@tonic-gate 		result = __ns_ldap_addTypedEntry(databasetype, inputbasedn,
3690Sstevel@tonic-gate 		    entry, 0, &authority, NS_LDAP_FOLLOWREF, &eres);
3700Sstevel@tonic-gate 	else
3710Sstevel@tonic-gate 		result = __ns_ldap_addTypedEntry(databasetype, inputbasedn,
3720Sstevel@tonic-gate 		    entry, 1, &authority, NS_LDAP_FOLLOWREF, &eres);
3730Sstevel@tonic-gate 	/*
3740Sstevel@tonic-gate 	 *  Return	0 on success
3750Sstevel@tonic-gate 	 *		LDAP_ALREADY_EXISTS if entry exists already
3760Sstevel@tonic-gate 	 *		1 for all other non-fatal errors.
3770Sstevel@tonic-gate 	 *  Exit on fatal errors.
3780Sstevel@tonic-gate 	 */
3790Sstevel@tonic-gate 	switch (result) {
3800Sstevel@tonic-gate 	case NS_LDAP_SUCCESS:
3810Sstevel@tonic-gate 		nent_add++;
3820Sstevel@tonic-gate 		rc = 0;
3830Sstevel@tonic-gate 		break;
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	case NS_LDAP_OP_FAILED:
3860Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("operation failed.\n"));
3870Sstevel@tonic-gate 		rc = 1;
3880Sstevel@tonic-gate 		break;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	case NS_LDAP_INVALID_PARAM:
3910Sstevel@tonic-gate 		(void) fprintf(stderr,
3920Sstevel@tonic-gate 		    gettext("invalid parameter(s) passed.\n"));
3930Sstevel@tonic-gate 		rc = 1;
3940Sstevel@tonic-gate 		break;
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	case NS_LDAP_NOTFOUND:
3970Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("entry not found.\n"));
3980Sstevel@tonic-gate 		rc = 1;
3990Sstevel@tonic-gate 		break;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	case NS_LDAP_MEMORY:
4020Sstevel@tonic-gate 		(void) fprintf(stderr,
4030Sstevel@tonic-gate 		    gettext("internal memory allocation error.\n"));
4040Sstevel@tonic-gate 		exit(1);
4050Sstevel@tonic-gate 		break;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	case NS_LDAP_CONFIG:
4080Sstevel@tonic-gate 		(void) fprintf(stderr,
4090Sstevel@tonic-gate 		    gettext("LDAP Configuration problem.\n"));
4100Sstevel@tonic-gate 		perr(eres);
4110Sstevel@tonic-gate 		exit(1);
4120Sstevel@tonic-gate 		break;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	case NS_LDAP_PARTIAL:
4150Sstevel@tonic-gate 		(void) fprintf(stderr,
4160Sstevel@tonic-gate 		    gettext("partial result returned\n"));
4170Sstevel@tonic-gate 		perr(eres);
4180Sstevel@tonic-gate 		rc = 1;
4190Sstevel@tonic-gate 		break;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	case NS_LDAP_INTERNAL:
4220Sstevel@tonic-gate 		if (eres->status == LDAP_ALREADY_EXISTS)
4230Sstevel@tonic-gate 			rc = eres->status;
4240Sstevel@tonic-gate 		else {
4250Sstevel@tonic-gate 			rc = 1;
4260Sstevel@tonic-gate 			perr(eres);
4270Sstevel@tonic-gate 		}
4280Sstevel@tonic-gate 		break;
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	if (eres)
4320Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&eres);
4330Sstevel@tonic-gate 	return (rc);
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate /*
4380Sstevel@tonic-gate  * usage(char *msg)
4390Sstevel@tonic-gate  * Display usage message to STDERR.
4400Sstevel@tonic-gate  */
4410Sstevel@tonic-gate static void
4420Sstevel@tonic-gate usage(char *msg) {
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	if (msg)
4450Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s\n"), msg);
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
4480Sstevel@tonic-gate 	    "usage: ldapaddent [ -cpv ] [ -a authenticationMethod ]\n"
4490Sstevel@tonic-gate 	    "[ -b baseDN ] -D bindDN -w bind_password [ -f file ] database\n\n"
4500Sstevel@tonic-gate 	    "usage: ldapaddent -d [ -cpv ] [ -a authenticationMethod ]\n"
4510Sstevel@tonic-gate 	    "[ -b baseDN ] [ -D bindDN ] [ -w bind_password ] database\n"));
4520Sstevel@tonic-gate 	exit(1);
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate  * Determine if the given string is an IP address (IPv4 or IPv6).
4570Sstevel@tonic-gate  * If so, it's converted to the preferred form (rfc2373) and
4580Sstevel@tonic-gate  * *newaddr will point to the new address.
4590Sstevel@tonic-gate  *
4600Sstevel@tonic-gate  * Returns	-2		: inet_ntop error
4610Sstevel@tonic-gate  *		-1		: not an IP address
4620Sstevel@tonic-gate  *		0		: unsupported IP address (future use)
4630Sstevel@tonic-gate  *		AF_INET		: IPv4
4640Sstevel@tonic-gate  *		AF_INET6	: IPv6
4650Sstevel@tonic-gate  */
4660Sstevel@tonic-gate static int
4670Sstevel@tonic-gate check_ipaddr(char *addr, char **newaddr) {
4680Sstevel@tonic-gate 	ipaddr_t	addr_ipv4 = 0;
4690Sstevel@tonic-gate 	in6_addr_t	addr_ipv6;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	/* IPv6 */
4720Sstevel@tonic-gate 	if (inet_pton(AF_INET6, addr, &addr_ipv6) == 1) {
4730Sstevel@tonic-gate 		if (newaddr == NULL)
4740Sstevel@tonic-gate 			return (AF_INET6);
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 		/* Convert IPv4-mapped IPv6 address to IPv4 */
4770Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED(&addr_ipv6) ||
4780Sstevel@tonic-gate 					IN6_IS_ADDR_V4COMPAT(&addr_ipv6)) {
4790Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&addr_ipv6, addr_ipv4);
4800Sstevel@tonic-gate 			if ((*newaddr = calloc(1, INET_ADDRSTRLEN)) == NULL) {
4810Sstevel@tonic-gate 				(void) fprintf(stderr,
4820Sstevel@tonic-gate 				    gettext("out of memory\n"));
4830Sstevel@tonic-gate 				exit(1);
4840Sstevel@tonic-gate 			}
4850Sstevel@tonic-gate 			if (inet_ntop(AF_INET, &addr_ipv4, *newaddr,
4860Sstevel@tonic-gate 			    INET_ADDRSTRLEN))
4870Sstevel@tonic-gate 				return (AF_INET6);
4880Sstevel@tonic-gate 			free(*newaddr);
4890Sstevel@tonic-gate 			return (-2);
4900Sstevel@tonic-gate 		}
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 		/* Processing general IPv6 addresses */
4930Sstevel@tonic-gate 		if ((*newaddr = calloc(1, INET6_ADDRSTRLEN)) == NULL) {
4940Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
4950Sstevel@tonic-gate 			exit(1);
4960Sstevel@tonic-gate 		}
4970Sstevel@tonic-gate 		if (inet_ntop(AF_INET6, &addr_ipv6, *newaddr, INET6_ADDRSTRLEN))
4980Sstevel@tonic-gate 			return (AF_INET6);
4990Sstevel@tonic-gate 		free(*newaddr);
5000Sstevel@tonic-gate 		return (-2);
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	/* Processing IPv4 addresses of the type d.d.d.d. */
5040Sstevel@tonic-gate 	if (inet_pton(AF_INET, addr, &addr_ipv4) == 1) {
5050Sstevel@tonic-gate 		if (newaddr == NULL)
5060Sstevel@tonic-gate 			return (AF_INET);
5070Sstevel@tonic-gate 		if ((*newaddr = calloc(1, INET_ADDRSTRLEN)) == NULL) {
5080Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
5090Sstevel@tonic-gate 			exit(1);
5100Sstevel@tonic-gate 		}
5110Sstevel@tonic-gate 		if (inet_ntop(AF_INET, &addr_ipv4, *newaddr, INET_ADDRSTRLEN))
5120Sstevel@tonic-gate 			return (AF_INET);
5130Sstevel@tonic-gate 		free(*newaddr);
5140Sstevel@tonic-gate 		return (-2);
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	/* Processing IPv4 addresses d.d.d , d.d and d */
5180Sstevel@tonic-gate 	if (inet_addr(addr) != (in_addr_t)-1) {
5190Sstevel@tonic-gate 		if (newaddr == NULL)
5200Sstevel@tonic-gate 			return (AF_INET);
5210Sstevel@tonic-gate 		if ((*newaddr = strdup(addr)) == NULL) {
5220Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
5230Sstevel@tonic-gate 			exit(1);
5240Sstevel@tonic-gate 		}
5250Sstevel@tonic-gate 		return (AF_INET);
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	return (-1);
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate static int
5320Sstevel@tonic-gate genent_hosts(char *line, int (*cback)())
5330Sstevel@tonic-gate {
5340Sstevel@tonic-gate 	char buf[BUFSIZ+1];
5350Sstevel@tonic-gate 	char *t;
5360Sstevel@tonic-gate 	entry_col ecol[4];
5370Sstevel@tonic-gate 	char *cname, *pref_addr;
5380Sstevel@tonic-gate 	int ctr = 0, retval = 1;
5390Sstevel@tonic-gate 	int rc = GENENT_OK, af;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	struct hostent  data;
5420Sstevel@tonic-gate 	char *alias;
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	/*
5450Sstevel@tonic-gate 	 * don't clobber our argument
5460Sstevel@tonic-gate 	 */
5470Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
5480Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
5490Sstevel@tonic-gate 		return (GENENT_PARSEERR);
5500Sstevel@tonic-gate 	}
5510Sstevel@tonic-gate 	(void) strcpy(buf, line);
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	/*
5540Sstevel@tonic-gate 	 * clear column data
5550Sstevel@tonic-gate 	 */
5560Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	/*
5590Sstevel@tonic-gate 	 * comment (col 3)
5600Sstevel@tonic-gate 	 */
5610Sstevel@tonic-gate 	t = strchr(buf, '#');
5620Sstevel@tonic-gate 	if (t) {
5630Sstevel@tonic-gate 		*t++ = 0;
5640Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
5650Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
5660Sstevel@tonic-gate 	} else {
5670Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = "";
5680Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
5690Sstevel@tonic-gate 	}
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	/*
5730Sstevel@tonic-gate 	 * addr(col 2)
5740Sstevel@tonic-gate 	 */
5750Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
5760Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no host");
5770Sstevel@tonic-gate 		return (GENENT_PARSEERR);
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	af = check_ipaddr(t, &pref_addr);
5810Sstevel@tonic-gate 	if (af == -2) {
5820Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "Internal error");
5830Sstevel@tonic-gate 	} else if (af == -1) {
5840Sstevel@tonic-gate 		(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
5850Sstevel@tonic-gate 		    "Invalid IP address: %s", t);
5860Sstevel@tonic-gate 	} else if (flags & F_VERBOSE) {
5870Sstevel@tonic-gate 		if ((strncasecmp(t, pref_addr, strlen(t))) != 0) {
5880Sstevel@tonic-gate 			(void) fprintf(stdout,
5890Sstevel@tonic-gate 			    gettext("IP address %s converted to %s\n"),
5900Sstevel@tonic-gate 			    t, pref_addr);
5910Sstevel@tonic-gate 		}
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	if (af < 0) {
5950Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s\n"), parse_err_msg);
5960Sstevel@tonic-gate 		if (continue_onerror == 0)
5970Sstevel@tonic-gate 			return (GENENT_CBERR);
5980Sstevel@tonic-gate 		else
5990Sstevel@tonic-gate 			return (rc);
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = pref_addr;
6030Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(pref_addr)+1;
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	/*
6060Sstevel@tonic-gate 	 * cname (col 0)
6070Sstevel@tonic-gate 	 */
6080Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
6090Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no cname");
6100Sstevel@tonic-gate 		return (GENENT_PARSEERR);
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
6130Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
6140Sstevel@tonic-gate 	cname = t;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	/* build entry */
6180Sstevel@tonic-gate 	if ((data.h_addr_list = (char **)calloc(2, sizeof (char **))) == NULL) {
6190Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
6200Sstevel@tonic-gate 		exit(1);
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 	data.h_addr_list[0] = strdup(ecol[2].ec_value.ec_value_val);
6230Sstevel@tonic-gate 	data.h_addr_list[1] = NULL;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	free(pref_addr);
6260Sstevel@tonic-gate 	data.h_name = strdup(ecol[0].ec_value.ec_value_val);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	/*
6290Sstevel@tonic-gate 	 * name (col 1)
6300Sstevel@tonic-gate 	 */
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	data.h_aliases = NULL;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	do {
6350Sstevel@tonic-gate 		/*
6360Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
6370Sstevel@tonic-gate 		 */
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 		/* This call to AddEntry may move out of the loop */
6400Sstevel@tonic-gate 		/* This is because we have to call the function just once */
6410Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
6420Sstevel@tonic-gate 			continue;
6430Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
6440Sstevel@tonic-gate 			continue;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
6470Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		ctr++;
6500Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
6510Sstevel@tonic-gate 		if ((data.h_aliases = (char **)realloc(data.h_aliases,
6520Sstevel@tonic-gate 			ctr * sizeof (char **))) == NULL) {
6530Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
6540Sstevel@tonic-gate 			exit(1);
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 		data.h_aliases[ctr-1] = alias;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 		/*
6590Sstevel@tonic-gate 		 * only put comment in canonical entry
6600Sstevel@tonic-gate 		 */
6610Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
6620Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
6670Sstevel@tonic-gate 	if ((data.h_aliases = (char **)realloc(data.h_aliases,
6680Sstevel@tonic-gate 		(ctr + 1) * sizeof (char **))) == NULL) {
6690Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
6700Sstevel@tonic-gate 		exit(1);
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 	data.h_aliases[ctr] = NULL;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	if (flags & F_VERBOSE)
6750Sstevel@tonic-gate 		(void) fprintf(stdout,
6760Sstevel@tonic-gate 		    gettext("Adding entry : cn=%s+ipHostNumber=%s\n"),
6770Sstevel@tonic-gate 		    data.h_name, data.h_addr_list[0]);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
6820Sstevel@tonic-gate 		if (continue_onerror)
6830Sstevel@tonic-gate 			(void) fprintf(stderr,
6840Sstevel@tonic-gate 				gettext("Entry: cn=%s+ipHostNumber=%s "
6850Sstevel@tonic-gate 					"already Exists -skipping it\n"),
6860Sstevel@tonic-gate 					data.h_name, data.h_addr_list[0]);
6870Sstevel@tonic-gate 		else {
6880Sstevel@tonic-gate 			rc = GENENT_CBERR;
6890Sstevel@tonic-gate 			(void) fprintf(stderr,
6900Sstevel@tonic-gate 				gettext("Entry: cn=%s+ipHostNumber=%s"
6910Sstevel@tonic-gate 					" already Exists\n"),
6920Sstevel@tonic-gate 					data.h_name, data.h_addr_list[0]);
6930Sstevel@tonic-gate 		}
6940Sstevel@tonic-gate 	} else if (retval)
6950Sstevel@tonic-gate 		rc = GENENT_CBERR;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	free(data.h_name);
6980Sstevel@tonic-gate 	free(data.h_aliases);
6990Sstevel@tonic-gate 	free(data.h_addr_list);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	return (rc);
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate static void
7070Sstevel@tonic-gate dump_hosts(ns_ldap_result_t *res)
7080Sstevel@tonic-gate {
7090Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *iphostnumber = NULL;
7100Sstevel@tonic-gate 	int		 i, j;
7110Sstevel@tonic-gate 	char		*name; /* host name */
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
7140Sstevel@tonic-gate 		return;
7150Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
7160Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
7170Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
7180Sstevel@tonic-gate 			cn = attrptr;
7190Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "iphostnumber") == 0)
7200Sstevel@tonic-gate 			iphostnumber = attrptr;
7210Sstevel@tonic-gate 	}
7220Sstevel@tonic-gate 	/* sanity check */
7230Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
7240Sstevel@tonic-gate 	    iphostnumber == NULL || iphostnumber->attrvalue == NULL ||
7250Sstevel@tonic-gate 	    iphostnumber->attrvalue[0] == NULL)
7260Sstevel@tonic-gate 		return;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
7290Sstevel@tonic-gate 		return;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	/* ip host/ipnode number */
7320Sstevel@tonic-gate 	if (strlen(iphostnumber->attrvalue[0]) <= INET_ADDRSTRLEN)
7330Sstevel@tonic-gate 		/* IPV4 or IPV6 but <= NET_ADDRSTRLEN */
7340Sstevel@tonic-gate 		(void) fprintf(stdout, "%-18s", iphostnumber->attrvalue[0]);
7350Sstevel@tonic-gate 	else
7360Sstevel@tonic-gate 		/* IPV6 */
7370Sstevel@tonic-gate 		(void) fprintf(stdout, "%-48s", iphostnumber->attrvalue[0]);
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	/* host/ipnode name */
7400Sstevel@tonic-gate 	(void) fprintf(stdout, "%s ", name);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	/* aliases */
7430Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
7440Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
7450Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0)
7460Sstevel@tonic-gate 				/* skip host name */
7470Sstevel@tonic-gate 				continue;
7480Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
7490Sstevel@tonic-gate 		}
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	/* end of line */
7530Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate /*
7570Sstevel@tonic-gate  * /etc/rpc
7580Sstevel@tonic-gate  */
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate static int
7610Sstevel@tonic-gate genent_rpc(char *line, int (*cback)())
7620Sstevel@tonic-gate {
7630Sstevel@tonic-gate 	char buf[BUFSIZ+1];
7640Sstevel@tonic-gate 	char *t;
7650Sstevel@tonic-gate 	entry_col ecol[4];
7660Sstevel@tonic-gate 	char *cname;
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	struct rpcent	data;
7690Sstevel@tonic-gate 	char *alias;
7700Sstevel@tonic-gate 	int ctr = 0;
7710Sstevel@tonic-gate 	int retval = 1;
7720Sstevel@tonic-gate 	int rc = GENENT_OK;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	/*
7750Sstevel@tonic-gate 	 * don't clobber our argument
7760Sstevel@tonic-gate 	 */
7770Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
7780Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
7790Sstevel@tonic-gate 		return (GENENT_PARSEERR);
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 	(void) strcpy(buf, line);
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	/*
7840Sstevel@tonic-gate 	 * clear column data
7850Sstevel@tonic-gate 	 */
7860Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	/*
7890Sstevel@tonic-gate 	 * comment (col 3)
7900Sstevel@tonic-gate 	 */
7910Sstevel@tonic-gate 	t = strchr(buf, '#');
7920Sstevel@tonic-gate 	if (t) {
7930Sstevel@tonic-gate 		*t++ = 0;
7940Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
7950Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
7960Sstevel@tonic-gate 	} else {
7970Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
7980Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	/*
8020Sstevel@tonic-gate 	 * cname(col 0)
8030Sstevel@tonic-gate 	 */
8040Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
8050Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no number");
8060Sstevel@tonic-gate 		return (GENENT_PARSEERR);
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
8090Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
8100Sstevel@tonic-gate 	cname = t;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	/*
8130Sstevel@tonic-gate 	 * number (col 2)
8140Sstevel@tonic-gate 	 */
8150Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
8160Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no number");
8170Sstevel@tonic-gate 		return (GENENT_PARSEERR);
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
8200Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	/*
8240Sstevel@tonic-gate 	 * build entry
8250Sstevel@tonic-gate 	 */
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	data.r_name = strdup(ecol[0].ec_value.ec_value_val);
8280Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
8290Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val[0] != '\0') {
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 		data.r_number = ascii_to_int(ecol[2].ec_value.ec_value_val);
8320Sstevel@tonic-gate 		if (data.r_number == -1) {
8330Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
8340Sstevel@tonic-gate 			    "invalid program number: %s",
8350Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
8360Sstevel@tonic-gate 		return (GENENT_PARSEERR);
8370Sstevel@tonic-gate 		}
8380Sstevel@tonic-gate 	} else
8390Sstevel@tonic-gate 		data.r_number = -1;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	/*
8420Sstevel@tonic-gate 	 * name (col 1)
8430Sstevel@tonic-gate 	 */
8440Sstevel@tonic-gate 	t = cname;
8450Sstevel@tonic-gate 	data.r_aliases = NULL;
8460Sstevel@tonic-gate 	do {
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 		/*
8490Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
8500Sstevel@tonic-gate 		 */
8510Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
8520Sstevel@tonic-gate 			continue;
8530Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
8540Sstevel@tonic-gate 			continue;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
8570Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 		ctr++;
8600Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
8610Sstevel@tonic-gate 		if ((data.r_aliases = (char **)realloc(data.r_aliases,
8620Sstevel@tonic-gate 			ctr * sizeof (char **))) == NULL) {
8630Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
8640Sstevel@tonic-gate 			exit(1);
8650Sstevel@tonic-gate 		}
8660Sstevel@tonic-gate 		data.r_aliases[ctr-1] = alias;
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 		/*
8700Sstevel@tonic-gate 		 * only put comment in canonical entry
8710Sstevel@tonic-gate 		 */
8720Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
8730Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
8780Sstevel@tonic-gate 	if ((data.r_aliases = (char **)realloc(data.r_aliases,
8790Sstevel@tonic-gate 		(ctr + 1) * sizeof (char **))) == NULL) {
8800Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
8810Sstevel@tonic-gate 		exit(1);
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 	data.r_aliases[ctr] = NULL;
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	if (flags & F_VERBOSE)
8860Sstevel@tonic-gate 		(void) fprintf(stdout,
8870Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.r_name);
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
8920Sstevel@tonic-gate 		if (continue_onerror)
8930Sstevel@tonic-gate 			(void) fprintf(stderr,
8940Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
8950Sstevel@tonic-gate 			data.r_name);
8960Sstevel@tonic-gate 		else {
8970Sstevel@tonic-gate 			rc = GENENT_CBERR;
8980Sstevel@tonic-gate 			(void) fprintf(stderr,
8990Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
9000Sstevel@tonic-gate 				data.r_name);
9010Sstevel@tonic-gate 		}
9020Sstevel@tonic-gate 	} else if (retval)
9030Sstevel@tonic-gate 		rc = GENENT_CBERR;
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	free(data.r_name);
9060Sstevel@tonic-gate 	free(data.r_aliases);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	return (rc);
9090Sstevel@tonic-gate }
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate static void
9140Sstevel@tonic-gate dump_rpc(ns_ldap_result_t *res)
9150Sstevel@tonic-gate {
9160Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *rpcnumber = NULL;
9170Sstevel@tonic-gate 	int		 i, j;
9180Sstevel@tonic-gate 	char		*name; /* rpc name */
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
9210Sstevel@tonic-gate 		return;
9220Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
9230Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
9240Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
9250Sstevel@tonic-gate 			cn = attrptr;
9260Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "oncRpcNumber") == 0)
9270Sstevel@tonic-gate 			rpcnumber = attrptr;
9280Sstevel@tonic-gate 	}
9290Sstevel@tonic-gate 	/* sanity check */
9300Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
9310Sstevel@tonic-gate 	    rpcnumber == NULL || rpcnumber->attrvalue == NULL ||
9320Sstevel@tonic-gate 	    rpcnumber->attrvalue[0] == NULL)
9330Sstevel@tonic-gate 		return;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
9360Sstevel@tonic-gate 		return;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	/* rpc name */
9390Sstevel@tonic-gate 	if (strlen(name) < 8)
9400Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t\t", name);
9410Sstevel@tonic-gate 	else
9420Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t", name);
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	/* rpc number */
9450Sstevel@tonic-gate 	(void) fprintf(stdout, "%-8s", rpcnumber->attrvalue[0]);
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	/* aliases */
9490Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
9500Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
9510Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0)
9520Sstevel@tonic-gate 				/* skip rpc name */
9530Sstevel@tonic-gate 				continue;
9540Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
9550Sstevel@tonic-gate 		}
9560Sstevel@tonic-gate 	}
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	/* end of line */
9590Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate }
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate /*
9640Sstevel@tonic-gate  * /etc/protocols
9650Sstevel@tonic-gate  *
9660Sstevel@tonic-gate  */
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate static int
9690Sstevel@tonic-gate genent_protocols(char *line, int (*cback)())
9700Sstevel@tonic-gate {
9710Sstevel@tonic-gate 	char buf[BUFSIZ+1];
9720Sstevel@tonic-gate 	char *t;
9730Sstevel@tonic-gate 	entry_col ecol[4];
9740Sstevel@tonic-gate 	char *cname;
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	struct protoent	data;
9770Sstevel@tonic-gate 	char *alias;
9780Sstevel@tonic-gate 	int ctr = 0;
9790Sstevel@tonic-gate 	int retval = 1;
9800Sstevel@tonic-gate 	int rc = GENENT_OK;
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	/*
9830Sstevel@tonic-gate 	 * don't clobber our argument
9840Sstevel@tonic-gate 	 */
9850Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
9860Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
9870Sstevel@tonic-gate 		return (GENENT_PARSEERR);
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate 	(void) strcpy(buf, line);
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	/*
9920Sstevel@tonic-gate 	 * clear column data
9930Sstevel@tonic-gate 	 */
9940Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	/*
9970Sstevel@tonic-gate 	 * comment (col 3)
9980Sstevel@tonic-gate 	 */
9990Sstevel@tonic-gate 	t = strchr(buf, '#');
10000Sstevel@tonic-gate 	if (t) {
10010Sstevel@tonic-gate 		*t++ = 0;
10020Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
10030Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
10040Sstevel@tonic-gate 	} else {
10050Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
10060Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	/*
10100Sstevel@tonic-gate 	 * cname(col 0)
10110Sstevel@tonic-gate 	 */
10120Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
10130Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no number");
10140Sstevel@tonic-gate 		return (GENENT_PARSEERR);
10150Sstevel@tonic-gate 	}
10160Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
10170Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
10180Sstevel@tonic-gate 	cname = t;
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	/*
10210Sstevel@tonic-gate 	 * number (col 2)
10220Sstevel@tonic-gate 	 */
10230Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
10240Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no number");
10250Sstevel@tonic-gate 		return (GENENT_PARSEERR);
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
10280Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	/*
10320Sstevel@tonic-gate 	 * build entry
10330Sstevel@tonic-gate 	 */
10340Sstevel@tonic-gate 	data.p_name = strdup(ecol[0].ec_value.ec_value_val);
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
10370Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val[0] != '\0') {
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 		data.p_proto = ascii_to_int(ecol[2].ec_value.ec_value_val);
10400Sstevel@tonic-gate 		if (data.p_proto == -1) {
10410Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
10420Sstevel@tonic-gate 			    "invalid protocol number: %s",
10430Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
10440Sstevel@tonic-gate 		return (GENENT_PARSEERR);
10450Sstevel@tonic-gate 		}
10460Sstevel@tonic-gate 	} else
10470Sstevel@tonic-gate 		data.p_proto = -1;
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	/*
10500Sstevel@tonic-gate 	 * name (col 1)
10510Sstevel@tonic-gate 	 */
10520Sstevel@tonic-gate 	t = cname;
10530Sstevel@tonic-gate 	ctr = 0;
10540Sstevel@tonic-gate 	data.p_aliases = NULL;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	do {
10570Sstevel@tonic-gate 		/*
10580Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
10590Sstevel@tonic-gate 		 */
10600Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
10610Sstevel@tonic-gate 			continue;
10620Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
10630Sstevel@tonic-gate 			continue;
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
10660Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 		ctr++;
10690Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
10700Sstevel@tonic-gate 		if ((data.p_aliases = (char **)realloc(data.p_aliases,
10710Sstevel@tonic-gate 			ctr * sizeof (char **))) == NULL) {
10720Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
10730Sstevel@tonic-gate 			exit(1);
10740Sstevel@tonic-gate 		}
10750Sstevel@tonic-gate 		data.p_aliases[ctr-1] = alias;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 		/*
10780Sstevel@tonic-gate 		 * only put comment in canonical entry
10790Sstevel@tonic-gate 		 */
10800Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
10810Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
10860Sstevel@tonic-gate 	if ((data.p_aliases = (char **)realloc(data.p_aliases,
10870Sstevel@tonic-gate 		(ctr + 1) * sizeof (char **))) == NULL) {
10880Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
10890Sstevel@tonic-gate 		exit(1);
10900Sstevel@tonic-gate 	}
10910Sstevel@tonic-gate 	data.p_aliases[ctr] = NULL;
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	if (flags & F_VERBOSE)
10940Sstevel@tonic-gate 		(void) fprintf(stdout,
10950Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.p_name);
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
11000Sstevel@tonic-gate 		if (continue_onerror)
11010Sstevel@tonic-gate 			(void) fprintf(stderr,
11020Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
11030Sstevel@tonic-gate 			data.p_name);
11040Sstevel@tonic-gate 		else {
11050Sstevel@tonic-gate 			rc = GENENT_CBERR;
11060Sstevel@tonic-gate 			(void) fprintf(stderr,
11070Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
11080Sstevel@tonic-gate 				data.p_name);
11090Sstevel@tonic-gate 		}
11100Sstevel@tonic-gate 	} else if (retval)
11110Sstevel@tonic-gate 		rc = GENENT_CBERR;
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	free(data.p_name);
11140Sstevel@tonic-gate 	free(data.p_aliases);
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	return (rc);
11170Sstevel@tonic-gate }
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate static void
11210Sstevel@tonic-gate dump_protocols(ns_ldap_result_t *res)
11220Sstevel@tonic-gate {
11230Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *protocolnumber = NULL;
11240Sstevel@tonic-gate 	int		 i, j;
11250Sstevel@tonic-gate 	char		*name, *cp;
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
11280Sstevel@tonic-gate 		return;
11290Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
11300Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
11310Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
11320Sstevel@tonic-gate 			cn = attrptr;
11330Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "ipProtocolNumber")
11340Sstevel@tonic-gate 									== 0)
11350Sstevel@tonic-gate 			protocolnumber = attrptr;
11360Sstevel@tonic-gate 	}
11370Sstevel@tonic-gate 	/* sanity check */
11380Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
11390Sstevel@tonic-gate 	    protocolnumber == NULL || protocolnumber->attrvalue == NULL ||
11400Sstevel@tonic-gate 	    protocolnumber->attrvalue[0] == NULL)
11410Sstevel@tonic-gate 		return;
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
11440Sstevel@tonic-gate 		return;
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	/* protocol name */
11470Sstevel@tonic-gate 	if (strlen(name) < 8)
11480Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t\t", name);
11490Sstevel@tonic-gate 	else
11500Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t", name);
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	/* protocol number */
11530Sstevel@tonic-gate 	(void) fprintf(stdout, "%-16s", protocolnumber->attrvalue[0]);
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	/* aliases */
11560Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
11570Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
11580Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0) {
11590Sstevel@tonic-gate 				if (cn->value_count > 1)
11600Sstevel@tonic-gate 					/* Do not replicate */
11610Sstevel@tonic-gate 					continue;
11620Sstevel@tonic-gate 				/*
11630Sstevel@tonic-gate 				 * Replicate name in uppercase as an aliase
11640Sstevel@tonic-gate 				 */
11650Sstevel@tonic-gate 				for (cp = cn->attrvalue[j]; *cp; cp++)
11660Sstevel@tonic-gate 					*cp = toupper(*cp);
11670Sstevel@tonic-gate 			}
11680Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
11690Sstevel@tonic-gate 		}
11700Sstevel@tonic-gate 	}
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	/* end of line */
11730Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate }
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate /*
11820Sstevel@tonic-gate  * /etc/networks
11830Sstevel@tonic-gate  *
11840Sstevel@tonic-gate  */
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate static int
11870Sstevel@tonic-gate genent_networks(char *line, int (*cback)())
11880Sstevel@tonic-gate {
11890Sstevel@tonic-gate 	char buf[BUFSIZ+1];
11900Sstevel@tonic-gate 	char *t;
11910Sstevel@tonic-gate 	entry_col ecol[4];
11920Sstevel@tonic-gate 	char *cname;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	struct netent	data;
11950Sstevel@tonic-gate 	char *alias;
11960Sstevel@tonic-gate 	int ctr = 0;
11970Sstevel@tonic-gate 	int retval = 1;
11980Sstevel@tonic-gate 	int enet;
11990Sstevel@tonic-gate 	int rc = GENENT_OK;
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	/*
12020Sstevel@tonic-gate 	 * don't clobber our argument
12030Sstevel@tonic-gate 	 */
12040Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
12050Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
12060Sstevel@tonic-gate 		return (GENENT_PARSEERR);
12070Sstevel@tonic-gate 	}
12080Sstevel@tonic-gate 	(void) strcpy(buf, line);
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	/*
12110Sstevel@tonic-gate 	 * clear column data
12120Sstevel@tonic-gate 	 */
12130Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	/*
12160Sstevel@tonic-gate 	 * comment (col 3)
12170Sstevel@tonic-gate 	 */
12180Sstevel@tonic-gate 	t = strchr(buf, '#');
12190Sstevel@tonic-gate 	if (t) {
12200Sstevel@tonic-gate 		*t++ = 0;
12210Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
12220Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
12230Sstevel@tonic-gate 	} else {
12240Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
12250Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
12260Sstevel@tonic-gate 	}
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	/*
12290Sstevel@tonic-gate 	 * cname(col 0)
12300Sstevel@tonic-gate 	 */
12310Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
12320Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no number");
12330Sstevel@tonic-gate 		return (GENENT_PARSEERR);
12340Sstevel@tonic-gate 	}
12350Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
12360Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
12370Sstevel@tonic-gate 	cname = t;
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	/*
12400Sstevel@tonic-gate 	 * number (col 2)
12410Sstevel@tonic-gate 	 */
12420Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
12430Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no number");
12440Sstevel@tonic-gate 		return (GENENT_PARSEERR);
12450Sstevel@tonic-gate 	}
12460Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
12470Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 	/*
12510Sstevel@tonic-gate 	 * build entry
12520Sstevel@tonic-gate 	 */
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	data.n_name = strdup(ecol[0].ec_value.ec_value_val);
12550Sstevel@tonic-gate 	/*
12560Sstevel@tonic-gate 	 * data.n_net is an unsigned field,
12570Sstevel@tonic-gate 	 * assign -1 to it, make no sense.
12580Sstevel@tonic-gate 	 * Use enet here to avoid lint warning.
12590Sstevel@tonic-gate 	 */
12600Sstevel@tonic-gate 	enet = encode_network(ecol[2].ec_value.ec_value_val);
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	if (enet == -1 && continue_onerror == 0) {
12630Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Invalid network number\n"));
12640Sstevel@tonic-gate 		if (continue_onerror == 0)
12650Sstevel@tonic-gate 			return (GENENT_CBERR);
12660Sstevel@tonic-gate 	} else
12670Sstevel@tonic-gate 		data.n_net = enet;
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	/*
12700Sstevel@tonic-gate 	 * name (col 1)
12710Sstevel@tonic-gate 	 */
12720Sstevel@tonic-gate 	t = cname;
12730Sstevel@tonic-gate 	data.n_aliases = NULL;
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	do {
12760Sstevel@tonic-gate 		/*
12770Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
12780Sstevel@tonic-gate 		 */
12790Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
12800Sstevel@tonic-gate 			continue;
12810Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
12820Sstevel@tonic-gate 			continue;
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
12850Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 		ctr++;
12880Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
12890Sstevel@tonic-gate 		if ((data.n_aliases = (char **)realloc(data.n_aliases,
12900Sstevel@tonic-gate 			ctr * sizeof (char **))) == NULL) {
12910Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
12920Sstevel@tonic-gate 			exit(1);
12930Sstevel@tonic-gate 		}
12940Sstevel@tonic-gate 		data.n_aliases[ctr-1] = alias;
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 		/*
12970Sstevel@tonic-gate 		 * only put comment in canonical entry
12980Sstevel@tonic-gate 		 */
12990Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = 0;
13000Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
13050Sstevel@tonic-gate 	if ((data.n_aliases = (char **)realloc(data.n_aliases,
13060Sstevel@tonic-gate 		(ctr + 1) * sizeof (char **))) == NULL) {
13070Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
13080Sstevel@tonic-gate 		exit(1);
13090Sstevel@tonic-gate 	}
13100Sstevel@tonic-gate 	data.n_aliases[ctr] = NULL;
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	if (flags & F_VERBOSE)
13130Sstevel@tonic-gate 		(void) fprintf(stdout,
13140Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.n_name);
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
13190Sstevel@tonic-gate 		if (continue_onerror)
13200Sstevel@tonic-gate 			(void) fprintf(stderr,
13210Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
13220Sstevel@tonic-gate 			data.n_name);
13230Sstevel@tonic-gate 		else {
13240Sstevel@tonic-gate 			rc = GENENT_CBERR;
13250Sstevel@tonic-gate 			(void) fprintf(stderr,
13260Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
13270Sstevel@tonic-gate 				data.n_name);
13280Sstevel@tonic-gate 		}
13290Sstevel@tonic-gate 	} else if (retval)
13300Sstevel@tonic-gate 		rc = GENENT_CBERR;
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	free(data.n_name);
13330Sstevel@tonic-gate 	free(data.n_aliases);
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	return (rc);
13360Sstevel@tonic-gate }
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate static void
13400Sstevel@tonic-gate dump_networks(ns_ldap_result_t *res)
13410Sstevel@tonic-gate {
13420Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *networknumber = NULL;
13430Sstevel@tonic-gate 	int		 i, j;
13440Sstevel@tonic-gate 	char		*name;
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
13470Sstevel@tonic-gate 		return;
13480Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
13490Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
13500Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
13510Sstevel@tonic-gate 			cn = attrptr;
13520Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "ipNetworkNumber")
13530Sstevel@tonic-gate 									== 0)
13540Sstevel@tonic-gate 			networknumber = attrptr;
13550Sstevel@tonic-gate 	}
13560Sstevel@tonic-gate 	/* sanity check */
13570Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
13580Sstevel@tonic-gate 	    networknumber == NULL || networknumber->attrvalue == NULL ||
13590Sstevel@tonic-gate 	    networknumber->attrvalue[0] == NULL)
13600Sstevel@tonic-gate 		return;
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 	/*
13630Sstevel@tonic-gate 	 * cn can be a MUST attribute(RFC 2307) or MAY attribute(2307bis).
13640Sstevel@tonic-gate 	 * If the canonical name can not be found (2307bis), use the 1st
13650Sstevel@tonic-gate 	 * value as the official name.
13660Sstevel@tonic-gate 	 */
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	/* network name */
13690Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
13700Sstevel@tonic-gate 		name = cn->attrvalue[0];
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	if (strlen(name) < 8)
13730Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t\t", name);
13740Sstevel@tonic-gate 	else
13750Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\t", name);
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	/* network number */
13780Sstevel@tonic-gate 	(void) fprintf(stdout, "%-16s", networknumber->attrvalue[0]);
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 	/* aliases */
13810Sstevel@tonic-gate 	for (j = 0; j < cn->value_count; j++) {
13820Sstevel@tonic-gate 		if (cn->attrvalue[j]) {
13830Sstevel@tonic-gate 			if (strcasecmp(name, cn->attrvalue[j]) == 0)
13840Sstevel@tonic-gate 				/* skip name */
13850Sstevel@tonic-gate 				continue;
13860Sstevel@tonic-gate 			(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
13870Sstevel@tonic-gate 		}
13880Sstevel@tonic-gate 	}
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	/* end of line */
13910Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate }
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate /*
13990Sstevel@tonic-gate  * /etc/services
14000Sstevel@tonic-gate  *
14010Sstevel@tonic-gate  */
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate static int
14040Sstevel@tonic-gate genent_services(char *line, int (*cback)())
14050Sstevel@tonic-gate {
14060Sstevel@tonic-gate 	char buf[BUFSIZ+1];
14070Sstevel@tonic-gate 	char *t, *p;
14080Sstevel@tonic-gate 	entry_col ecol[5];
14090Sstevel@tonic-gate 	char *cname;
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	struct servent	data;
14120Sstevel@tonic-gate 	char *alias;
14130Sstevel@tonic-gate 	int ctr = 0;
14140Sstevel@tonic-gate 	int retval = 1;
14150Sstevel@tonic-gate 	int rc = GENENT_OK;
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	/*
14180Sstevel@tonic-gate 	 * don't clobber our argument
14190Sstevel@tonic-gate 	 */
14200Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
14210Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
14220Sstevel@tonic-gate 		return (GENENT_PARSEERR);
14230Sstevel@tonic-gate 	}
14240Sstevel@tonic-gate 	(void) strcpy(buf, line);
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	/*
14270Sstevel@tonic-gate 	 * clear column data
14280Sstevel@tonic-gate 	 */
14290Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	/*
14320Sstevel@tonic-gate 	 * comment (col 4)
14330Sstevel@tonic-gate 	 */
14340Sstevel@tonic-gate 	t = strchr(buf, '#');
14350Sstevel@tonic-gate 	if (t) {
14360Sstevel@tonic-gate 		*t++ = 0;
14370Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_val = t;
14380Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_len = strlen(t)+1;
14390Sstevel@tonic-gate 	} else {
14400Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_val = 0;
14410Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_len = 0;
14420Sstevel@tonic-gate 	}
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 	/*
14450Sstevel@tonic-gate 	 * cname(col 0)
14460Sstevel@tonic-gate 	 */
14470Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
14480Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no port");
14490Sstevel@tonic-gate 		return (GENENT_PARSEERR);
14500Sstevel@tonic-gate 	}
14510Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
14520Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
14530Sstevel@tonic-gate 	cname = t;
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	/*
14560Sstevel@tonic-gate 	 * port (col 3)
14570Sstevel@tonic-gate 	 */
14580Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
14590Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no protocol");
14600Sstevel@tonic-gate 		return (GENENT_PARSEERR);
14610Sstevel@tonic-gate 	}
14620Sstevel@tonic-gate 	if ((p = strchr(t, '/')) == 0) {
14630Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "bad port/proto");
14640Sstevel@tonic-gate 		return (GENENT_PARSEERR);
14650Sstevel@tonic-gate 	}
14660Sstevel@tonic-gate 	*(p++) = 0;
14670Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
14680Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	/*
14710Sstevel@tonic-gate 	 * proto (col 2)
14720Sstevel@tonic-gate 	 */
14730Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = p;
14740Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(p)+1;
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	/*
14780Sstevel@tonic-gate 	 * build entry
14790Sstevel@tonic-gate 	 */
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 	data.s_name = strdup(ecol[0].ec_value.ec_value_val);
14820Sstevel@tonic-gate 	data.s_proto = strdup(ecol[2].ec_value.ec_value_val);
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 	if (ecol[3].ec_value.ec_value_val != NULL &&
14850Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val[0] != '\0') {
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 		data.s_port = ascii_to_int(ecol[3].ec_value.ec_value_val);
14880Sstevel@tonic-gate 		if (data.s_port == -1) {
14890Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
14900Sstevel@tonic-gate 			    "invalid port number: %s",
14910Sstevel@tonic-gate 			    ecol[3].ec_value.ec_value_val);
14920Sstevel@tonic-gate 		return (GENENT_PARSEERR);
14930Sstevel@tonic-gate 		}
14940Sstevel@tonic-gate 	} else
14950Sstevel@tonic-gate 		data.s_port = -1;
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 	/*
14980Sstevel@tonic-gate 	 * name (col 1)
14990Sstevel@tonic-gate 	 */
15000Sstevel@tonic-gate 	t = cname;
15010Sstevel@tonic-gate 	data.s_aliases = NULL;
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 	do {
15040Sstevel@tonic-gate 		/*
15050Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
15060Sstevel@tonic-gate 		 */
15070Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
15080Sstevel@tonic-gate 			continue;
15090Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
15100Sstevel@tonic-gate 			continue;
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
15130Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 		ctr++;
15160Sstevel@tonic-gate 		alias = strdup(ecol[1].ec_value.ec_value_val);
15170Sstevel@tonic-gate 		if ((data.s_aliases = (char **)realloc(data.s_aliases,
15180Sstevel@tonic-gate 			ctr * sizeof (char **))) == NULL) {
15190Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
15200Sstevel@tonic-gate 			exit(1);
15210Sstevel@tonic-gate 		}
15220Sstevel@tonic-gate 		data.s_aliases[ctr-1] = alias;
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 		/*
15250Sstevel@tonic-gate 		 * only put comment in canonical entry
15260Sstevel@tonic-gate 		 */
15270Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_val = 0;
15280Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_len = 0;
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
15330Sstevel@tonic-gate 	if ((data.s_aliases = (char **)realloc(data.s_aliases,
15340Sstevel@tonic-gate 		(ctr + 1) * sizeof (char **))) == NULL) {
15350Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
15360Sstevel@tonic-gate 		exit(1);
15370Sstevel@tonic-gate 	}
15380Sstevel@tonic-gate 	data.s_aliases[ctr] = NULL;
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	if (flags & F_VERBOSE)
15410Sstevel@tonic-gate 		(void) fprintf(stdout,
15420Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), line);
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
15470Sstevel@tonic-gate 		if (continue_onerror)
15480Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
15490Sstevel@tonic-gate 					"Entry: cn=%s+ipServiceProtocol=%s"
15500Sstevel@tonic-gate 					" already Exists, skipping it.\n"),
15510Sstevel@tonic-gate 					data.s_name, data.s_proto);
15520Sstevel@tonic-gate 		else {
15530Sstevel@tonic-gate 			rc = GENENT_CBERR;
15540Sstevel@tonic-gate 			(void) fprintf(stderr,
15550Sstevel@tonic-gate 				gettext("Entry: cn=%s+ipServiceProtocol=%s"
15560Sstevel@tonic-gate 					" - already Exists\n"),
15570Sstevel@tonic-gate 					data.s_name, data.s_proto);
15580Sstevel@tonic-gate 		}
15590Sstevel@tonic-gate 	} else if (retval)
15600Sstevel@tonic-gate 		rc = GENENT_CBERR;
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	free(data.s_name);
15630Sstevel@tonic-gate 	free(data.s_proto);
15640Sstevel@tonic-gate 	free(data.s_aliases);
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	return (rc);
15670Sstevel@tonic-gate }
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate static void
15720Sstevel@tonic-gate dump_services(ns_ldap_result_t *res)
15730Sstevel@tonic-gate {
15740Sstevel@tonic-gate 	ns_ldap_attr_t	*attrptr = NULL, *cn = NULL, *port = NULL;
15750Sstevel@tonic-gate 	ns_ldap_attr_t	*protocol = NULL;
15760Sstevel@tonic-gate 	int		i, j, len;
15770Sstevel@tonic-gate 	char		*name; /* service name */
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	/*
15800Sstevel@tonic-gate 	 * cn can have multiple values.(service name and its aliases)
15810Sstevel@tonic-gate 	 * In order to support RFC 2307, section 5.5, ipserviceprotocol  can
15820Sstevel@tonic-gate 	 * have multiple values too.
15830Sstevel@tonic-gate 	 * The output format should look like
15840Sstevel@tonic-gate 	 *
15850Sstevel@tonic-gate 	 * test		2345/udp mytest
15860Sstevel@tonic-gate 	 * test		2345/tcp mytest
15870Sstevel@tonic-gate 	 */
15880Sstevel@tonic-gate 	if (res == NULL || res->entry == NULL)
15890Sstevel@tonic-gate 		return;
15900Sstevel@tonic-gate 	for (i = 0; i < res->entry->attr_count; i++) {
15910Sstevel@tonic-gate 		attrptr = res->entry->attr_pair[i];
15920Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, "cn") == 0)
15930Sstevel@tonic-gate 			cn = attrptr;
15940Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname, "ipServicePort") == 0)
15950Sstevel@tonic-gate 			port = attrptr;
15960Sstevel@tonic-gate 		else if (strcasecmp(attrptr->attrname,
15970Sstevel@tonic-gate 					"ipServiceProtocol") == 0)
15980Sstevel@tonic-gate 			protocol = attrptr;
15990Sstevel@tonic-gate 	}
16000Sstevel@tonic-gate 	/* sanity check */
16010Sstevel@tonic-gate 	if (cn == NULL || cn->attrvalue == NULL || cn->attrvalue[0] == NULL ||
16020Sstevel@tonic-gate 	    port == NULL || port->attrvalue == NULL ||
16030Sstevel@tonic-gate 	    port->attrvalue[0] == NULL || protocol == NULL ||
16040Sstevel@tonic-gate 	    protocol->attrvalue == NULL || protocol->attrvalue[0] == NULL)
16050Sstevel@tonic-gate 		return;
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 	if ((name = __s_api_get_canonical_name(res->entry, cn, 1)) == NULL)
16080Sstevel@tonic-gate 		return;
16090Sstevel@tonic-gate 	for (i = 0; i < protocol->value_count; i++) {
16100Sstevel@tonic-gate 		if (protocol->attrvalue[i] == NULL)
16110Sstevel@tonic-gate 			return;
16120Sstevel@tonic-gate 		/* service name */
16130Sstevel@tonic-gate 		(void) fprintf(stdout, "%-16s", name);
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 		/* port & protocol */
16160Sstevel@tonic-gate 		(void) fprintf(stdout, "%s/%s%n", port->attrvalue[0],
16170Sstevel@tonic-gate 				protocol->attrvalue[i], &len);
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 		if (len < 8)
16200Sstevel@tonic-gate 			(void) fprintf(stdout, "\t\t");
16210Sstevel@tonic-gate 		else
16220Sstevel@tonic-gate 			(void) fprintf(stdout, "\t");
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 		/* aliases */
16250Sstevel@tonic-gate 		for (j = 0; j < cn->value_count; j++) {
16260Sstevel@tonic-gate 			if (cn->attrvalue[j]) {
16270Sstevel@tonic-gate 				if (strcasecmp(name, cn->attrvalue[j]) == 0)
16280Sstevel@tonic-gate 					/* skip service name */
16290Sstevel@tonic-gate 					continue;
16300Sstevel@tonic-gate 				(void) fprintf(stdout, "%s ", cn->attrvalue[j]);
16310Sstevel@tonic-gate 			}
16320Sstevel@tonic-gate 		}
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 		/* end of line */
16350Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
16360Sstevel@tonic-gate 	}
16370Sstevel@tonic-gate }
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate /*
16410Sstevel@tonic-gate  * /etc/group
16420Sstevel@tonic-gate  */
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate static int
16450Sstevel@tonic-gate genent_group(char *line, int (*cback)())
16460Sstevel@tonic-gate {
16470Sstevel@tonic-gate 	char buf[BIGBUF+1];
16480Sstevel@tonic-gate 	char *s, *t;
16490Sstevel@tonic-gate 	entry_col ecol[5];
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate 	struct group	data;
16520Sstevel@tonic-gate 	int ctr = 0;
16530Sstevel@tonic-gate 	int retval = 1;
16540Sstevel@tonic-gate 	int rc = GENENT_OK;
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 	/*
16570Sstevel@tonic-gate 	 * don't clobber our argument
16580Sstevel@tonic-gate 	 */
16590Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
16600Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
16610Sstevel@tonic-gate 		return (GENENT_PARSEERR);
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 	(void) strcpy(buf, line);
16640Sstevel@tonic-gate 	t = buf;
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	/* ignore empty entries */
16670Sstevel@tonic-gate 	if (*t == '\0')
16680Sstevel@tonic-gate 		return (GENENT_OK);
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	/*
16710Sstevel@tonic-gate 	 * clear column data
16720Sstevel@tonic-gate 	 */
16730Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 	/*
16760Sstevel@tonic-gate 	 * name (col 0)
16770Sstevel@tonic-gate 	 */
16780Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
16790Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no passwd");
16800Sstevel@tonic-gate 		return (GENENT_PARSEERR);
16810Sstevel@tonic-gate 	}
16820Sstevel@tonic-gate 	*s++ = 0;
16830Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
16840Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
16850Sstevel@tonic-gate 	t = s;
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	/*
16880Sstevel@tonic-gate 	 * passwd (col 1)
16890Sstevel@tonic-gate 	 */
16900Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
16910Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no gid");
16920Sstevel@tonic-gate 		return (GENENT_PARSEERR);
16930Sstevel@tonic-gate 	}
16940Sstevel@tonic-gate 	*s++ = 0;
16950Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
16960Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
16970Sstevel@tonic-gate 	t = s;
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	/*
17010Sstevel@tonic-gate 	 * gid (col 2)
17020Sstevel@tonic-gate 	 */
17030Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0 || s == t) {
17040Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no members");
17050Sstevel@tonic-gate 		return (GENENT_PARSEERR);
17060Sstevel@tonic-gate 	}
17070Sstevel@tonic-gate 	*s++ = 0;
17080Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
17090Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
17100Sstevel@tonic-gate 	t = s;
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 	/*
17130Sstevel@tonic-gate 	 * members (col 3)
17140Sstevel@tonic-gate 	 */
17150Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
17160Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate 	/*
17200Sstevel@tonic-gate 	 * build entry
17210Sstevel@tonic-gate 	 */
17220Sstevel@tonic-gate 	data.gr_name = strdup(ecol[0].ec_value.ec_value_val);
17230Sstevel@tonic-gate 	data.gr_passwd = strdup(ecol[1].ec_value.ec_value_val);
17240Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
17250Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val[0] != '\0') {
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 		data.gr_gid = ascii_to_int(ecol[2].ec_value.ec_value_val);
17280Sstevel@tonic-gate 		if (data.gr_gid == -1) {
17290Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
17300Sstevel@tonic-gate 			    "invalid group id: %s",
17310Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
17320Sstevel@tonic-gate 		return (GENENT_PARSEERR);
17330Sstevel@tonic-gate 		}
17340Sstevel@tonic-gate 	} else
17350Sstevel@tonic-gate 		data.gr_gid = -1;
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 	data.gr_mem = NULL;
17380Sstevel@tonic-gate 
1739*839Smj162486 	/* Compute maximum amount of members */
1740*839Smj162486 	s = t;
1741*839Smj162486 	while (s = strchr(s, ',')) {
1742*839Smj162486 		s++;
1743*839Smj162486 		ctr++;
1744*839Smj162486 	}
1745*839Smj162486 
1746*839Smj162486 	/* Allocate memory for all members */
1747*839Smj162486 	data.gr_mem = calloc(ctr + 2, sizeof (char **));
1748*839Smj162486 	if (data.gr_mem == NULL) {
1749*839Smj162486 		(void) fprintf(stderr, gettext("out of memory\n"));
1750*839Smj162486 		exit(1);
1751*839Smj162486 	}
1752*839Smj162486 
1753*839Smj162486 	ctr = 0;
17540Sstevel@tonic-gate 	while (s = strchr(t, ',')) {
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 		*s++ = 0;
17570Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
17580Sstevel@tonic-gate 		t = s;
1759*839Smj162486 		/* Send to server only non empty member names */
1760*839Smj162486 		if (strlen(ecol[3].ec_value.ec_value_val) != 0)
1761*839Smj162486 			data.gr_mem[ctr++] = ecol[3].ec_value.ec_value_val;
17620Sstevel@tonic-gate 	}
17630Sstevel@tonic-gate 
1764*839Smj162486 	/* Send to server only non empty member names */
1765*839Smj162486 	if (strlen(t) != 0)
1766*839Smj162486 		data.gr_mem[ctr++] = t;
1767*839Smj162486 
1768*839Smj162486 	/* Array of members completed, finished by NULL, see calloc() */
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 	if (flags & F_VERBOSE)
17710Sstevel@tonic-gate 		(void) fprintf(stdout,
17720Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.gr_name);
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
17770Sstevel@tonic-gate 		if (continue_onerror)
17780Sstevel@tonic-gate 			(void) fprintf(stderr,
17790Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
17800Sstevel@tonic-gate 			data.gr_name);
17810Sstevel@tonic-gate 		else {
17820Sstevel@tonic-gate 			rc = GENENT_CBERR;
17830Sstevel@tonic-gate 			(void) fprintf(stderr,
17840Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
17850Sstevel@tonic-gate 				data.gr_name);
17860Sstevel@tonic-gate 		}
17870Sstevel@tonic-gate 	} else if (retval)
17880Sstevel@tonic-gate 		rc = GENENT_CBERR;
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 	free(data.gr_name);
17910Sstevel@tonic-gate 	free(data.gr_passwd);
17920Sstevel@tonic-gate 	free(data.gr_mem);
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	return (rc);
17950Sstevel@tonic-gate }
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate static void
17980Sstevel@tonic-gate dump_group(ns_ldap_result_t *res)
17990Sstevel@tonic-gate {
18000Sstevel@tonic-gate 	char    **value = NULL;
18010Sstevel@tonic-gate 	char	pnam[256];
18020Sstevel@tonic-gate 	int	attr_count = 0;
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
18050Sstevel@tonic-gate 	if (value && value[0])
18060Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
18070Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "userPassword");
18080Sstevel@tonic-gate 	if (value == NULL || value[0] == NULL)
18090Sstevel@tonic-gate 		(void) fprintf(stdout, "*:");
18100Sstevel@tonic-gate 	else {
18110Sstevel@tonic-gate 		(void) strcpy(pnam, value[0]);
18120Sstevel@tonic-gate 		if (strncasecmp(value[0], "{crypt}", 7) == 0)
18130Sstevel@tonic-gate 			(void) fprintf(stdout, "%s:", (pnam+7));
18140Sstevel@tonic-gate 		else
18150Sstevel@tonic-gate 			(void) fprintf(stdout, "*:");
18160Sstevel@tonic-gate 	}
18170Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "gidNumber");
18180Sstevel@tonic-gate 	if (value && value[0])
18190Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "memberUid");
18220Sstevel@tonic-gate 	if (value != NULL && value[0] != NULL) {
18230Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
18240Sstevel@tonic-gate 			if (value[attr_count+1] == NULL)
18250Sstevel@tonic-gate 				(void) fprintf(stdout, "%s", value[attr_count]);
18260Sstevel@tonic-gate 			else
18270Sstevel@tonic-gate 				(void) fprintf(stdout, "%s,",
18280Sstevel@tonic-gate 					value[attr_count]);
18290Sstevel@tonic-gate 			attr_count++;
18300Sstevel@tonic-gate 		}
18310Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
18320Sstevel@tonic-gate 	}
18330Sstevel@tonic-gate 	else
18340Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
18350Sstevel@tonic-gate }
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate /*
18420Sstevel@tonic-gate  * /etc/ethers
18430Sstevel@tonic-gate  */
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate static int
18460Sstevel@tonic-gate genent_ethers(char *line, int (*cback)())
18470Sstevel@tonic-gate {
18480Sstevel@tonic-gate 	char buf[BUFSIZ+1];
18490Sstevel@tonic-gate 	char *t;
18500Sstevel@tonic-gate 	entry_col ecol[3];
18510Sstevel@tonic-gate 	int retval = 1;
18520Sstevel@tonic-gate 	struct _ns_ethers	data;
18530Sstevel@tonic-gate 	int rc = GENENT_OK;
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 	/*
18560Sstevel@tonic-gate 	 * don't clobber our argument
18570Sstevel@tonic-gate 	 */
18580Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
18590Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
18600Sstevel@tonic-gate 		return (GENENT_PARSEERR);
18610Sstevel@tonic-gate 	}
18620Sstevel@tonic-gate 	(void) strcpy(buf, line);
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 	/*
18650Sstevel@tonic-gate 	 * clear column data
18660Sstevel@tonic-gate 	 */
18670Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 	/*
18700Sstevel@tonic-gate 	 * comment (col 2)
18710Sstevel@tonic-gate 	 */
18720Sstevel@tonic-gate 	t = strchr(buf, '#');
18730Sstevel@tonic-gate 	if (t) {
18740Sstevel@tonic-gate 		*t++ = 0;
18750Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = t;
18760Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = strlen(t)+1;
18770Sstevel@tonic-gate 	} else {
18780Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = 0;
18790Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = 0;
18800Sstevel@tonic-gate 	}
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 	/*
18830Sstevel@tonic-gate 	 * addr(col 0)
18840Sstevel@tonic-gate 	 */
18850Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
18860Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no name");
18870Sstevel@tonic-gate 		return (GENENT_PARSEERR);
18880Sstevel@tonic-gate 	}
18890Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
18900Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate 	/*
18930Sstevel@tonic-gate 	 * name(col 1)
18940Sstevel@tonic-gate 	 */
18950Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
18960Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no white space allowed in name");
18970Sstevel@tonic-gate 		return (GENENT_PARSEERR);
18980Sstevel@tonic-gate 	}
18990Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
19000Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 	/*
19040Sstevel@tonic-gate 	 * build entry
19050Sstevel@tonic-gate 	 */
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 	data.ether = strdup(ecol[0].ec_value.ec_value_val);
19080Sstevel@tonic-gate 	data.name  = strdup(ecol[1].ec_value.ec_value_val);
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate 	if (flags & F_VERBOSE)
19120Sstevel@tonic-gate 		(void) fprintf(stdout,
19130Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.name);
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
19180Sstevel@tonic-gate 		if (continue_onerror)
19190Sstevel@tonic-gate 			(void) fprintf(stderr,
19200Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
19210Sstevel@tonic-gate 			data.name);
19220Sstevel@tonic-gate 		else {
19230Sstevel@tonic-gate 			rc = GENENT_CBERR;
19240Sstevel@tonic-gate 			(void) fprintf(stderr,
19250Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
19260Sstevel@tonic-gate 				data.name);
19270Sstevel@tonic-gate 		}
19280Sstevel@tonic-gate 	} else if (retval)
19290Sstevel@tonic-gate 		rc = GENENT_CBERR;
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 	free(data.ether);
19320Sstevel@tonic-gate 	free(data.name);
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 	return (rc);
19350Sstevel@tonic-gate }
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate static void
19390Sstevel@tonic-gate dump_ethers(ns_ldap_result_t *res)
19400Sstevel@tonic-gate {
19410Sstevel@tonic-gate 	char	**value = NULL;
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "macAddress");
19440Sstevel@tonic-gate 	if (value && value[0])
19450Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
19460Sstevel@tonic-gate 	else
19470Sstevel@tonic-gate 		return;
19480Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
19490Sstevel@tonic-gate 	if (value && value[0])
19500Sstevel@tonic-gate 		(void) fprintf(stdout, "	%s\n", value[0]);
19510Sstevel@tonic-gate }
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate static int
19540Sstevel@tonic-gate genent_aliases(char *line, int (*cback)())
19550Sstevel@tonic-gate {
19560Sstevel@tonic-gate 	char buf[BUFSIZ+1];
19570Sstevel@tonic-gate 	char *t, *aliases;
19580Sstevel@tonic-gate 	char *cname;
19590Sstevel@tonic-gate 	int ctr = 0;
19600Sstevel@tonic-gate 	int retval = 1;
19610Sstevel@tonic-gate 	int i;
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 	struct _ns_alias data;
19640Sstevel@tonic-gate 	char *alias;
19650Sstevel@tonic-gate 	int rc = GENENT_OK;
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 	/*
19680Sstevel@tonic-gate 	 * don't clobber our argument
19690Sstevel@tonic-gate 	 */
19700Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
19710Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
19720Sstevel@tonic-gate 		return (GENENT_PARSEERR);
19730Sstevel@tonic-gate 	}
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 	(void) strcpy(buf, line);
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 	if ((t = strchr(buf, ':')) == 0) {
19780Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no alias name");
19790Sstevel@tonic-gate 		return (GENENT_PARSEERR);
19800Sstevel@tonic-gate 	}
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 	t[0] = '\0';
19830Sstevel@tonic-gate 	if (++t == '\0') {
19840Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no alias value");
19850Sstevel@tonic-gate 		return (GENENT_PARSEERR);
19860Sstevel@tonic-gate 	}
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 	cname = buf;
19890Sstevel@tonic-gate 	aliases = t;
19900Sstevel@tonic-gate 
19910Sstevel@tonic-gate 	/* build entry */
19920Sstevel@tonic-gate 	data.alias = strdup(cname);
19930Sstevel@tonic-gate 	if (!data.alias) {
19940Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
19950Sstevel@tonic-gate 		exit(1);
19960Sstevel@tonic-gate 	}
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate 	data.member = NULL;
19990Sstevel@tonic-gate 	t = strtok(aliases, ",");
20000Sstevel@tonic-gate 	do {
20010Sstevel@tonic-gate 		ctr++;
20020Sstevel@tonic-gate 		while (t[0] == ' ')
20030Sstevel@tonic-gate 			t++;
20040Sstevel@tonic-gate 		alias = strdup(t);
20050Sstevel@tonic-gate 		if ((alias == NULL) ||
20060Sstevel@tonic-gate 			((data.member = (char **)realloc(data.member,
20070Sstevel@tonic-gate 			(ctr + 1) * sizeof (char **))) == NULL)) {
20080Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
20090Sstevel@tonic-gate 			exit(1);
20100Sstevel@tonic-gate 		}
20110Sstevel@tonic-gate 		data.member[ctr-1] = alias;
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 	} while (t = strtok(NULL, ","));
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate 	data.member[ctr] = NULL;
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	if (flags & F_VERBOSE)
20180Sstevel@tonic-gate 		(void) fprintf(stdout,
20190Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.alias);
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
20220Sstevel@tonic-gate 
20230Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
20240Sstevel@tonic-gate 		if (continue_onerror)
20250Sstevel@tonic-gate 			(void) fprintf(stderr,
20260Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
20270Sstevel@tonic-gate 			data.alias);
20280Sstevel@tonic-gate 		else {
20290Sstevel@tonic-gate 			rc = GENENT_CBERR;
20300Sstevel@tonic-gate 			(void) fprintf(stderr,
20310Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
20320Sstevel@tonic-gate 				data.alias);
20330Sstevel@tonic-gate 		}
20340Sstevel@tonic-gate 	} else if (retval)
20350Sstevel@tonic-gate 		rc = GENENT_CBERR;
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 	free(data.alias);
20380Sstevel@tonic-gate 	i = 0;
20390Sstevel@tonic-gate 	while (data.member[i])
20400Sstevel@tonic-gate 		free(data.member[i++]);
20410Sstevel@tonic-gate 	free(data.member);
20420Sstevel@tonic-gate 
20430Sstevel@tonic-gate 	return (rc);
20440Sstevel@tonic-gate }
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate static void
20480Sstevel@tonic-gate dump_aliases(ns_ldap_result_t *res)
20490Sstevel@tonic-gate {
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 	char	**value = NULL;
20520Sstevel@tonic-gate 	int 		attr_count = 0;
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "mail");
20550Sstevel@tonic-gate 	if (value && value[0])
20560Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
20570Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "mgrpRFC822MailMember");
20580Sstevel@tonic-gate 	if (value != NULL)
20590Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
20600Sstevel@tonic-gate 			(void) fprintf(stdout, "%s,", value[attr_count]);
20610Sstevel@tonic-gate 			attr_count++;
20620Sstevel@tonic-gate 		}
20630Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate }
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate /*
20680Sstevel@tonic-gate  * /etc/publickey
20690Sstevel@tonic-gate  */
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate static int
20720Sstevel@tonic-gate genent_publickey(char *line, int (*cback)())
20730Sstevel@tonic-gate {
20740Sstevel@tonic-gate 	char buf[BUFSIZ+1], tmpbuf[BUFSIZ+1], cname[BUFSIZ+1];
20750Sstevel@tonic-gate 	char *t, *p, *tmppubkey, *tmpprivkey;
20760Sstevel@tonic-gate 	entry_col ecol[3];
20770Sstevel@tonic-gate 	int buflen, uid, retval = 1;
20780Sstevel@tonic-gate 	struct passwd *pwd;
20790Sstevel@tonic-gate 	char auth_type[BUFSIZ+1];
20800Sstevel@tonic-gate 	keylen_t keylen;
20810Sstevel@tonic-gate 	algtype_t algtype;
20820Sstevel@tonic-gate 	struct _ns_pubkey data;
20830Sstevel@tonic-gate 	struct hostent *hp;
20840Sstevel@tonic-gate 	struct in_addr in;
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	/*
20870Sstevel@tonic-gate 	 * don't clobber our argument
20880Sstevel@tonic-gate 	 */
20890Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
20900Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
20910Sstevel@tonic-gate 		return (GENENT_PARSEERR);
20920Sstevel@tonic-gate 	}
20930Sstevel@tonic-gate 	(void) strcpy(buf, line);
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate 	/*
20960Sstevel@tonic-gate 	 * clear column data
20970Sstevel@tonic-gate 	 */
20980Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
21010Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no cname");
21020Sstevel@tonic-gate 		return (GENENT_PARSEERR);
21030Sstevel@tonic-gate 	}
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	/*
21060Sstevel@tonic-gate 	 * Special case:  /etc/publickey usually has an entry
21070Sstevel@tonic-gate 	 * for principal "nobody".  We skip it.
21080Sstevel@tonic-gate 	 */
21090Sstevel@tonic-gate 	if (strcmp(t, "nobody") == 0)
21100Sstevel@tonic-gate 		return (GENENT_OK);
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 	/*
21130Sstevel@tonic-gate 	 * cname (col 0)
21140Sstevel@tonic-gate 	 */
21150Sstevel@tonic-gate 	if (strncmp(t, "unix.", 5)) {
21160Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "bad cname");
21170Sstevel@tonic-gate 		return (GENENT_PARSEERR);
21180Sstevel@tonic-gate 	}
21190Sstevel@tonic-gate 	(void) strcpy(tmpbuf, &(t[5]));
21200Sstevel@tonic-gate 	if ((p = strchr(tmpbuf, '@')) == 0) {
21210Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "bad cname");
21220Sstevel@tonic-gate 		return (GENENT_PARSEERR);
21230Sstevel@tonic-gate 	}
21240Sstevel@tonic-gate 	*(p++) = 0;
21250Sstevel@tonic-gate 	if (isdigit(*tmpbuf)) {
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 		uid = atoi(tmpbuf);
21280Sstevel@tonic-gate 		/*
21290Sstevel@tonic-gate 		 * don't generate entries for uids without passwd entries
21300Sstevel@tonic-gate 		 */
21310Sstevel@tonic-gate 		if ((pwd = getpwuid(uid)) == 0) {
21320Sstevel@tonic-gate 			(void) fprintf(stderr,
21330Sstevel@tonic-gate 			gettext("can't map uid %d to username, skipping\n"),
21340Sstevel@tonic-gate 				uid);
21350Sstevel@tonic-gate 			return (GENENT_OK);
21360Sstevel@tonic-gate 		}
21370Sstevel@tonic-gate 		(void) strcpy(cname, pwd->pw_name);
21380Sstevel@tonic-gate 		data.hostcred = NS_HOSTCRED_FALSE;
21390Sstevel@tonic-gate 	} else {
21400Sstevel@tonic-gate 		if ((hp = gethostbyname(tmpbuf)) == 0) {
21410Sstevel@tonic-gate 			(void) fprintf(stderr,
21420Sstevel@tonic-gate 		gettext("can't map hostname %s to hostaddress, skipping\n"),
21430Sstevel@tonic-gate 			tmpbuf);
21440Sstevel@tonic-gate 			return (GENENT_OK);
21450Sstevel@tonic-gate 		}
21460Sstevel@tonic-gate 		(void) memcpy((char *)&in.s_addr, hp->h_addr_list[0],
21470Sstevel@tonic-gate 		    sizeof (in));
21480Sstevel@tonic-gate 		data.hostcred = NS_HOSTCRED_TRUE;
21490Sstevel@tonic-gate 		(void) snprintf(cname, sizeof (cname),
21500Sstevel@tonic-gate 		    "%s+ipHostNumber=%s", tmpbuf, inet_ntoa(in));
21510Sstevel@tonic-gate 	}
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = cname;
21540Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(cname)+1;
21550Sstevel@tonic-gate 
21560Sstevel@tonic-gate 	/*
21570Sstevel@tonic-gate 	 * public_data (col 1)
21580Sstevel@tonic-gate 	 */
21590Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
21600Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no private_data");
21610Sstevel@tonic-gate 		return (GENENT_PARSEERR);
21620Sstevel@tonic-gate 	}
21630Sstevel@tonic-gate 	if ((p = strchr(t, ':')) == 0) {
21640Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "bad public_data");
21650Sstevel@tonic-gate 		return (GENENT_PARSEERR);
21660Sstevel@tonic-gate 	}
21670Sstevel@tonic-gate 	*(p++) = 0;
21680Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
21690Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
21700Sstevel@tonic-gate 	keylen = (strlen(t) / 2) * 8;
21710Sstevel@tonic-gate 
21720Sstevel@tonic-gate 	/*
21730Sstevel@tonic-gate 	 * private_data (col 2) and algtype extraction
21740Sstevel@tonic-gate 	 */
21750Sstevel@tonic-gate 	if (*p == ':')
21760Sstevel@tonic-gate 		p++;
21770Sstevel@tonic-gate 	t = p;
21780Sstevel@tonic-gate 	if (!(t = strchr(t, ':'))) {
21790Sstevel@tonic-gate 		(void) fprintf(stderr,
21800Sstevel@tonic-gate 		gettext("WARNING: No algorithm type data found "
21810Sstevel@tonic-gate 			"in publickey file, assuming 0\n"));
21820Sstevel@tonic-gate 		algtype = 0;
21830Sstevel@tonic-gate 	} else {
21840Sstevel@tonic-gate 		*t = '\0';
21850Sstevel@tonic-gate 		t++;
21860Sstevel@tonic-gate 		algtype = atoi(t);
21870Sstevel@tonic-gate 	}
21880Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = p;
21890Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(p)+1;
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	/*
21920Sstevel@tonic-gate 	 * auth_type (col 1)
21930Sstevel@tonic-gate 	 */
21940Sstevel@tonic-gate 	if (!(__nis_keyalg2authtype(keylen, algtype, auth_type,
21950Sstevel@tonic-gate 						MECH_MAXATNAME))) {
21960Sstevel@tonic-gate 		(void) fprintf(stderr,
21970Sstevel@tonic-gate 		gettext("Could not convert algorithm type to "
21980Sstevel@tonic-gate 			"corresponding auth type string\n"));
21990Sstevel@tonic-gate 		return (GENENT_ERR);
22000Sstevel@tonic-gate 	}
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	/*
22030Sstevel@tonic-gate 	 * build entry
22040Sstevel@tonic-gate 	 */
22050Sstevel@tonic-gate 	data.name = strdup(ecol[0].ec_value.ec_value_val);
22060Sstevel@tonic-gate 	if (data.name == NULL) {
22070Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
22080Sstevel@tonic-gate 		exit(1);
22090Sstevel@tonic-gate 	}
22100Sstevel@tonic-gate 
22110Sstevel@tonic-gate 	buflen = sizeof (auth_type) + strlen(ecol[1].ec_value.ec_value_val) + 3;
22120Sstevel@tonic-gate 	if ((tmppubkey = (char *)malloc(buflen)) == NULL) {
22130Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
22140Sstevel@tonic-gate 		exit(1);
22150Sstevel@tonic-gate 	}
22160Sstevel@tonic-gate 	(void) snprintf(tmppubkey, buflen, "{%s}%s", auth_type,
22170Sstevel@tonic-gate 	    ecol[1].ec_value.ec_value_val);
22180Sstevel@tonic-gate 	data.pubkey = tmppubkey;
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 	buflen = sizeof (auth_type) + strlen(ecol[2].ec_value.ec_value_val) + 3;
22210Sstevel@tonic-gate 	if ((tmpprivkey = (char *)malloc(buflen)) == NULL) {
22220Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
22230Sstevel@tonic-gate 		exit(1);
22240Sstevel@tonic-gate 	}
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate 	(void) snprintf(tmpprivkey, buflen, "{%s}%s", auth_type,
22270Sstevel@tonic-gate 	    ecol[2].ec_value.ec_value_val);
22280Sstevel@tonic-gate 	data.privkey = tmpprivkey;
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 	retval = (*cback)(&data, 1);
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 	if ((retval != NS_LDAP_SUCCESS) && (continue_onerror == 0))
22330Sstevel@tonic-gate 		return (GENENT_CBERR);
22340Sstevel@tonic-gate 	else {
22350Sstevel@tonic-gate 		free(data.name);
22360Sstevel@tonic-gate 		free(data.pubkey);
22370Sstevel@tonic-gate 		free(data.privkey);
22380Sstevel@tonic-gate 		return (GENENT_OK);
22390Sstevel@tonic-gate 	}
22400Sstevel@tonic-gate }
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate static void
22430Sstevel@tonic-gate dump_publickey(ns_ldap_result_t *res, char *container)
22440Sstevel@tonic-gate {
22450Sstevel@tonic-gate 	char	**value = NULL;
22460Sstevel@tonic-gate 	char	buf[BUFSIZ];
22470Sstevel@tonic-gate 	char	domainname[BUFSIZ];
22480Sstevel@tonic-gate 	char	*pubptr, *prvptr;
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate 	if (res == NULL)
22510Sstevel@tonic-gate 		return;
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate 	if (sysinfo(SI_SRPC_DOMAIN, domainname, BUFSIZ) < 0) {
22540Sstevel@tonic-gate 		(void) fprintf(stderr,
22550Sstevel@tonic-gate 			gettext("could not obtain domainname\n"));
22560Sstevel@tonic-gate 		exit(1);
22570Sstevel@tonic-gate 	}
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	/*
22600Sstevel@tonic-gate 	 * Retrieve all the attributes, but don't print
22610Sstevel@tonic-gate 	 * until we have all the required ones.
22620Sstevel@tonic-gate 	 */
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 	if (strcmp(container, "passwd") == 0)
22650Sstevel@tonic-gate 		value = __ns_ldap_getAttr(res->entry, "uidNumber");
22660Sstevel@tonic-gate 	else
22670Sstevel@tonic-gate 		value = __ns_ldap_getAttr(res->entry, "cn");
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 	if (value && value[0])
22700Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "unix.%s@%s",
22710Sstevel@tonic-gate 		    value[0], domainname);
22720Sstevel@tonic-gate 	else
22730Sstevel@tonic-gate 		return;
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "nisPublickey");
22760Sstevel@tonic-gate 	if (value != NULL && value[0] != NULL) {
22770Sstevel@tonic-gate 		if ((pubptr = strchr(value[0], '}')) == NULL)
22780Sstevel@tonic-gate 			return;
22790Sstevel@tonic-gate 	}
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "nisSecretkey");
22820Sstevel@tonic-gate 	if (value != NULL && value[0] != NULL)
22830Sstevel@tonic-gate 		if ((prvptr = strchr(value[0], '}')) == NULL)
22840Sstevel@tonic-gate 			return;
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate 	/* print the attributes, algorithm type is always 0 */
22870Sstevel@tonic-gate 	(void) fprintf(stdout, "%s	%s:%s:0\n", buf, ++pubptr, ++prvptr);
22880Sstevel@tonic-gate }
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate /*
22930Sstevel@tonic-gate  * /etc/netmasks
22940Sstevel@tonic-gate  */
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate static int
22970Sstevel@tonic-gate genent_netmasks(char *line, int (*cback)())
22980Sstevel@tonic-gate {
22990Sstevel@tonic-gate 	char buf[BUFSIZ+1];
23000Sstevel@tonic-gate 	char *t;
23010Sstevel@tonic-gate 	entry_col ecol[3];
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate 	struct _ns_netmasks data;
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate 	/*
23070Sstevel@tonic-gate 	 * don't clobber our argument
23080Sstevel@tonic-gate 	 */
23090Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
23100Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
23110Sstevel@tonic-gate 		return (GENENT_PARSEERR);
23120Sstevel@tonic-gate 	}
23130Sstevel@tonic-gate 	(void) strcpy(buf, line);
23140Sstevel@tonic-gate 
23150Sstevel@tonic-gate 	/*
23160Sstevel@tonic-gate 	 * clear column data
23170Sstevel@tonic-gate 	 */
23180Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 	/*
23210Sstevel@tonic-gate 	 * comment (col 2)
23220Sstevel@tonic-gate 	 */
23230Sstevel@tonic-gate 	t = strchr(buf, '#');
23240Sstevel@tonic-gate 	if (t) {
23250Sstevel@tonic-gate 		*t++ = 0;
23260Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = t;
23270Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = strlen(t)+1;
23280Sstevel@tonic-gate 	} else {
23290Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = 0;
23300Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = 0;
23310Sstevel@tonic-gate 	}
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 	/*
23340Sstevel@tonic-gate 	 * addr(col 0)
23350Sstevel@tonic-gate 	 */
23360Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
23370Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no mask");
23380Sstevel@tonic-gate 		return (GENENT_PARSEERR);
23390Sstevel@tonic-gate 	}
23400Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
23410Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 	/*
23440Sstevel@tonic-gate 	 * mask (col 1)
23450Sstevel@tonic-gate 	 */
23460Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
23470Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no mask");
23480Sstevel@tonic-gate 		return (GENENT_PARSEERR);
23490Sstevel@tonic-gate 	}
23500Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
23510Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate 	/* build entry */
23540Sstevel@tonic-gate 	data.netnumber = ecol[0].ec_value.ec_value_val;
23550Sstevel@tonic-gate 	data.netmask = ecol[1].ec_value.ec_value_val;
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate 	if (flags & F_VERBOSE)
23580Sstevel@tonic-gate 		(void) fprintf(stdout,
23590Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.netnumber);
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate 	if ((*cback)(&data, 1) && continue_onerror == 0)
23620Sstevel@tonic-gate 		return (GENENT_CBERR);
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate 	return (GENENT_OK);
23650Sstevel@tonic-gate }
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate static void
23680Sstevel@tonic-gate dump_netmasks(ns_ldap_result_t *res)
23690Sstevel@tonic-gate {
23700Sstevel@tonic-gate 	char	**value = NULL;
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "ipNetworkNumber");
23730Sstevel@tonic-gate 	if (value && value[0])
23740Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
23750Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "ipNetmaskNumber");
23760Sstevel@tonic-gate 	if (value && value[0])
23770Sstevel@tonic-gate 		(void) fprintf(stdout, "	%s\n", value[0]);
23780Sstevel@tonic-gate }
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate /*
23820Sstevel@tonic-gate  * /etc/netgroup
23830Sstevel@tonic-gate  * column data format is:
23840Sstevel@tonic-gate  *    col 0: netgroup name (or cname)
23850Sstevel@tonic-gate  *    col 1: netgroup member, if this is a triplet
23860Sstevel@tonic-gate  *    col 2: netgroup member, if not a triplet
23870Sstevel@tonic-gate  *    col 3: comment
23880Sstevel@tonic-gate  */
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate static int
23910Sstevel@tonic-gate genent_netgroup(char *line, int (*cback)())
23920Sstevel@tonic-gate {
23930Sstevel@tonic-gate 	char buf[BIGBUF+1];    /* netgroup entries tend to be big */
23940Sstevel@tonic-gate 	char *t;
23950Sstevel@tonic-gate 	char *cname = NULL;
23960Sstevel@tonic-gate 	entry_col ecol[4];
23970Sstevel@tonic-gate 	char *netg_tmp = NULL, *triplet_tmp = NULL;
23980Sstevel@tonic-gate 	int netgcount = 0, tripletcount = 0, retval = 1;
23990Sstevel@tonic-gate 	struct _ns_netgroups data;
24000Sstevel@tonic-gate 	int rc = GENENT_OK;
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate 	/* don't clobber our argument */
24030Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
24040Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
24050Sstevel@tonic-gate 		return (GENENT_PARSEERR);
24060Sstevel@tonic-gate 	}
24070Sstevel@tonic-gate 	(void) strcpy(buf, line);
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 	/* clear column data */
24100Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 	/*
24130Sstevel@tonic-gate 	 * process 1st minimal entry, to validate that there is no
24140Sstevel@tonic-gate 	 * parsing error.
24150Sstevel@tonic-gate 	 * start with comment(col 3)
24160Sstevel@tonic-gate 	 */
24170Sstevel@tonic-gate 	t = strchr(buf, '#');
24180Sstevel@tonic-gate 	if (t) {
24190Sstevel@tonic-gate 		*t++ = 0;
24200Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = t;
24210Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = strlen(t)+1;
24220Sstevel@tonic-gate 	} else {
24230Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val = "";
24240Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_len = 0;
24250Sstevel@tonic-gate 	}
24260Sstevel@tonic-gate 
24270Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = NULL;
24280Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = NULL;
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate 	/* cname (col 0) */
24310Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
24320Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no cname");
24330Sstevel@tonic-gate 		return (GENENT_PARSEERR);
24340Sstevel@tonic-gate 	}
24350Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
24360Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
24370Sstevel@tonic-gate 	cname = t;
24380Sstevel@tonic-gate 
24390Sstevel@tonic-gate 	/* addr(col 1 and 2) */
24400Sstevel@tonic-gate 	if ((t = strtok(NULL, " \t")) == 0) {
24410Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no members for netgroup");
24420Sstevel@tonic-gate 		return (GENENT_PARSEERR);
24430Sstevel@tonic-gate 	}
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate 	if (*t == '(') {
24460Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
24470Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
24480Sstevel@tonic-gate 	} else {
24490Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val = t;
24500Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_len = strlen(t)+1;
24510Sstevel@tonic-gate 	}
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate 
24540Sstevel@tonic-gate 	/*
24550Sstevel@tonic-gate 	 * now build entry.
24560Sstevel@tonic-gate 	 * start by clearing entry data
24570Sstevel@tonic-gate 	 */
24580Sstevel@tonic-gate 	(void) memset((struct _ns_netgroups *)&data, 0, sizeof (data));
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 	data.name = strdup(ecol[0].ec_value.ec_value_val);
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 	if (ecol[1].ec_value.ec_value_val != NULL) {
24630Sstevel@tonic-gate 		if ((data.triplet = calloc(1, sizeof (char **))) == NULL) {
24640Sstevel@tonic-gate 				(void) fprintf(stderr,
24650Sstevel@tonic-gate 					gettext("out of memory\n"));
24660Sstevel@tonic-gate 				exit(1);
24670Sstevel@tonic-gate 		}
24680Sstevel@tonic-gate 		data.triplet[tripletcount++] =
24690Sstevel@tonic-gate 		    strdup(ecol[1].ec_value.ec_value_val);
24700Sstevel@tonic-gate 	} else if (ecol[2].ec_value.ec_value_val != NULL) {
24710Sstevel@tonic-gate 			if ((data.netgroup = calloc(1, sizeof (char **)))
24720Sstevel@tonic-gate 			    == NULL) {
24730Sstevel@tonic-gate 					(void) fprintf(stderr,
24740Sstevel@tonic-gate 				    gettext("out of memory\n"));
24750Sstevel@tonic-gate 					exit(1);
24760Sstevel@tonic-gate 			}
24770Sstevel@tonic-gate 			data.netgroup[netgcount++] =
24780Sstevel@tonic-gate 			    strdup(ecol[2].ec_value.ec_value_val);
24790Sstevel@tonic-gate 	}
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 	/*
24820Sstevel@tonic-gate 	 * we now have a valid entry (at least 1 netgroup name and
24830Sstevel@tonic-gate 	 * 1 netgroup member), proceed with the rest of the line
24840Sstevel@tonic-gate 	 */
24850Sstevel@tonic-gate 	while (t = strtok(NULL, " \t")) {
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate 		/* if next token is equal to netgroup name, ignore */
24880Sstevel@tonic-gate 		if (t != cname && strcasecmp(t, cname) == 0)
24890Sstevel@tonic-gate 			continue;
24900Sstevel@tonic-gate 		if (strcasecmp(t, ecol[0].ec_value.ec_value_val) == 0)
24910Sstevel@tonic-gate 			continue;
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate 		if (*t == '(') {
24940Sstevel@tonic-gate 			tripletcount++;
24950Sstevel@tonic-gate 			triplet_tmp = strdup(t);
24960Sstevel@tonic-gate 			if ((data.triplet = (char **)realloc(data.triplet,
24970Sstevel@tonic-gate 				tripletcount * sizeof (char **))) == NULL) {
24980Sstevel@tonic-gate 				(void) fprintf(stderr,
24990Sstevel@tonic-gate 					gettext("out of memory\n"));
25000Sstevel@tonic-gate 				exit(1);
25010Sstevel@tonic-gate 			}
25020Sstevel@tonic-gate 			data.triplet[tripletcount-1] = triplet_tmp;
25030Sstevel@tonic-gate 		} else {
25040Sstevel@tonic-gate 			netgcount++;
25050Sstevel@tonic-gate 			netg_tmp = strdup(t);
25060Sstevel@tonic-gate 			if ((data.netgroup = (char **)realloc(data.netgroup,
25070Sstevel@tonic-gate 				netgcount * sizeof (char **))) == NULL) {
25080Sstevel@tonic-gate 				(void) fprintf(stderr,
25090Sstevel@tonic-gate 				gettext("out of memory\n"));
25100Sstevel@tonic-gate 				exit(1);
25110Sstevel@tonic-gate 			}
25120Sstevel@tonic-gate 			data.netgroup[netgcount-1] = netg_tmp;
25130Sstevel@tonic-gate 		}
25140Sstevel@tonic-gate 	}
25150Sstevel@tonic-gate 
25160Sstevel@tonic-gate 
25170Sstevel@tonic-gate 	/* End the list with NULL */
25180Sstevel@tonic-gate 	if ((data.triplet = (char **)realloc(data.triplet,
25190Sstevel@tonic-gate 		(tripletcount + 1) * sizeof (char **))) == NULL) {
25200Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
25210Sstevel@tonic-gate 		exit(1);
25220Sstevel@tonic-gate 	}
25230Sstevel@tonic-gate 	data.triplet[tripletcount] = NULL;
25240Sstevel@tonic-gate 	if ((data.netgroup = (char **)realloc(data.netgroup,
25250Sstevel@tonic-gate 		(netgcount + 1) * sizeof (char **))) == NULL) {
25260Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
25270Sstevel@tonic-gate 		exit(1);
25280Sstevel@tonic-gate 	}
25290Sstevel@tonic-gate 	data.netgroup[netgcount] = NULL;
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate 	if (flags & F_VERBOSE)
25320Sstevel@tonic-gate 		(void) fprintf(stdout,
25330Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.name);
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
25380Sstevel@tonic-gate 		if (continue_onerror)
25390Sstevel@tonic-gate 			(void) fprintf(stderr,
25400Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
25410Sstevel@tonic-gate 			data.name);
25420Sstevel@tonic-gate 		else {
25430Sstevel@tonic-gate 			rc = GENENT_CBERR;
25440Sstevel@tonic-gate 			(void) fprintf(stderr,
25450Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
25460Sstevel@tonic-gate 				data.name);
25470Sstevel@tonic-gate 		}
25480Sstevel@tonic-gate 	} else if (retval)
25490Sstevel@tonic-gate 		rc = GENENT_CBERR;
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate 	free(data.name);
25520Sstevel@tonic-gate 	free(data.triplet);
25530Sstevel@tonic-gate 	free(data.netgroup);
25540Sstevel@tonic-gate 
25550Sstevel@tonic-gate 	return (rc);
25560Sstevel@tonic-gate }
25570Sstevel@tonic-gate 
25580Sstevel@tonic-gate static void
25590Sstevel@tonic-gate dump_netgroup(ns_ldap_result_t *res)
25600Sstevel@tonic-gate {
25610Sstevel@tonic-gate 	char	**value = NULL;
25620Sstevel@tonic-gate 	int	attr_count = 0;
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
25650Sstevel@tonic-gate 	if ((value != NULL) && (value[0] != NULL))
25660Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
25670Sstevel@tonic-gate 	else
25680Sstevel@tonic-gate 		return;
25690Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "nisNetgroupTriple");
25700Sstevel@tonic-gate 	if (value != NULL)
25710Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
25720Sstevel@tonic-gate 			(void) fprintf(stdout, " %s", value[attr_count]);
25730Sstevel@tonic-gate 			attr_count++;
25740Sstevel@tonic-gate 		}
25750Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "memberNisNetgroup");
25760Sstevel@tonic-gate 	if (value != NULL)
25770Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
25780Sstevel@tonic-gate 			(void) fprintf(stdout, " %s", value[attr_count]);
25790Sstevel@tonic-gate 			attr_count++;
25800Sstevel@tonic-gate 		}
25810Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate }
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate static int
25860Sstevel@tonic-gate genent_automount(char *line, int (*cback)())
25870Sstevel@tonic-gate {
25880Sstevel@tonic-gate 	char buf[BUFSIZ+1];
25890Sstevel@tonic-gate 	char *t, *s;
25900Sstevel@tonic-gate 	entry_col ecol[2];
25910Sstevel@tonic-gate 	struct _ns_automount data;
25920Sstevel@tonic-gate 	int retval = 1;
25930Sstevel@tonic-gate 	int rc = GENENT_OK;
25940Sstevel@tonic-gate 
25950Sstevel@tonic-gate 	/*
25960Sstevel@tonic-gate 	 * don't clobber our argument
25970Sstevel@tonic-gate 	 */
25980Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
25990Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
26000Sstevel@tonic-gate 		return (GENENT_PARSEERR);
26010Sstevel@tonic-gate 		}
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate 	/* replace every tabspace with single space */
26040Sstevel@tonic-gate 	replace_tab2space(line);
26050Sstevel@tonic-gate 	(void) strcpy(buf, line);
26060Sstevel@tonic-gate 
26070Sstevel@tonic-gate 	/*
26080Sstevel@tonic-gate 	 * clear column data
26090Sstevel@tonic-gate 	 */
26100Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
26110Sstevel@tonic-gate 
26120Sstevel@tonic-gate 	/*
26130Sstevel@tonic-gate 	 * key (col 0)
26140Sstevel@tonic-gate 	 */
26150Sstevel@tonic-gate 	t = buf;
26160Sstevel@tonic-gate 	while (t[0] == ' ')
26170Sstevel@tonic-gate 		t++;
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate 	if ((s = strchr(t, ' ')) == 0) {
26200Sstevel@tonic-gate 		return (GENENT_PARSEERR);
26210Sstevel@tonic-gate 	}
26220Sstevel@tonic-gate 	*s++ = 0;
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
26250Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
26260Sstevel@tonic-gate 	t = s;
26270Sstevel@tonic-gate 
26280Sstevel@tonic-gate 	while (t[0] == ' ')
26290Sstevel@tonic-gate 		t++;
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate 	/*
26320Sstevel@tonic-gate 	 * mapentry (col 1)
26330Sstevel@tonic-gate 	 */
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
26360Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate 	data.mapname = strdup(databasetype);
26390Sstevel@tonic-gate 	data.key = strdup(ecol[0].ec_value.ec_value_val);
26400Sstevel@tonic-gate 	data.value = strdup(ecol[1].ec_value.ec_value_val);
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	if (flags & F_VERBOSE)
26430Sstevel@tonic-gate 		(void) fprintf(stdout,
26440Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.key);
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
26470Sstevel@tonic-gate 
26480Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
26490Sstevel@tonic-gate 		if (continue_onerror)
26500Sstevel@tonic-gate 			(void) fprintf(stderr,
26510Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
26520Sstevel@tonic-gate 			data.key);
26530Sstevel@tonic-gate 		else {
26540Sstevel@tonic-gate 			rc = GENENT_CBERR;
26550Sstevel@tonic-gate 			(void) fprintf(stderr,
26560Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
26570Sstevel@tonic-gate 				data.key);
26580Sstevel@tonic-gate 		}
26590Sstevel@tonic-gate 	} else if (retval)
26600Sstevel@tonic-gate 		rc = GENENT_CBERR;
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate 	free(data.mapname);
26630Sstevel@tonic-gate 	free(data.key);
26640Sstevel@tonic-gate 	free(data.value);
26650Sstevel@tonic-gate 	return (rc);
26660Sstevel@tonic-gate }
26670Sstevel@tonic-gate 
26680Sstevel@tonic-gate static void
26690Sstevel@tonic-gate dump_automount(ns_ldap_result_t *res)
26700Sstevel@tonic-gate {
26710Sstevel@tonic-gate 	char	**value = NULL;
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate 	if (res == NULL)
26740Sstevel@tonic-gate 		return;
26750Sstevel@tonic-gate 
26760Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "automountKey");
26770Sstevel@tonic-gate 	if (value != NULL) {
26780Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
26790Sstevel@tonic-gate 		value = __ns_ldap_getAttr(res->entry, "automountInformation");
26800Sstevel@tonic-gate 		if (value != NULL)
26810Sstevel@tonic-gate 			(void) fprintf(stdout, "	%s\n", value[0]);
26820Sstevel@tonic-gate 		else
26830Sstevel@tonic-gate 			(void) fprintf(stdout, "\n");
26840Sstevel@tonic-gate 	}
26850Sstevel@tonic-gate }
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate /*
26890Sstevel@tonic-gate  * /etc/passwd
26900Sstevel@tonic-gate  *
26910Sstevel@tonic-gate  */
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate static int
26940Sstevel@tonic-gate genent_passwd(char *line, int (*cback)())
26950Sstevel@tonic-gate {
26960Sstevel@tonic-gate 	char buf[BUFSIZ+1];
26970Sstevel@tonic-gate 	char *s, *t;
26980Sstevel@tonic-gate 	entry_col ecol[8];
26990Sstevel@tonic-gate 	int retval = 1;
27000Sstevel@tonic-gate 	char pname[BUFSIZ];
27010Sstevel@tonic-gate 
27020Sstevel@tonic-gate 	struct passwd	data;
27030Sstevel@tonic-gate 	int rc = GENENT_OK;
27040Sstevel@tonic-gate 
27050Sstevel@tonic-gate 
27060Sstevel@tonic-gate 	/*
27070Sstevel@tonic-gate 	 * don't clobber our argument
27080Sstevel@tonic-gate 	 */
27090Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
27100Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
27110Sstevel@tonic-gate 		return (GENENT_PARSEERR);
27120Sstevel@tonic-gate 	}
27130Sstevel@tonic-gate 	(void) strcpy(buf, line);
27140Sstevel@tonic-gate 	t = buf;
27150Sstevel@tonic-gate 
27160Sstevel@tonic-gate 	/* ignore empty entries */
27170Sstevel@tonic-gate 	if (*t == '\0')
27180Sstevel@tonic-gate 		return (GENENT_OK);
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate 	/*
27210Sstevel@tonic-gate 	 * clear column data
27220Sstevel@tonic-gate 	 */
27230Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate 	/*
27260Sstevel@tonic-gate 	 * name (col 0)
27270Sstevel@tonic-gate 	 */
27280Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
27290Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no password");
27300Sstevel@tonic-gate 		return (GENENT_PARSEERR);
27310Sstevel@tonic-gate 	}
27320Sstevel@tonic-gate 	*s++ = 0;
27330Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
27340Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
27350Sstevel@tonic-gate 	t = s;
27360Sstevel@tonic-gate 
27370Sstevel@tonic-gate 	/*
27380Sstevel@tonic-gate 	 * passwd (col 1)
27390Sstevel@tonic-gate 	 */
27400Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
27410Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no uid");
27420Sstevel@tonic-gate 		return (GENENT_PARSEERR);
27430Sstevel@tonic-gate 	}
27440Sstevel@tonic-gate 	*s++ = 0;
27450Sstevel@tonic-gate 
27460Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_val = t;
27470Sstevel@tonic-gate 	ecol[1].ec_value.ec_value_len = strlen(t)+1;
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate 	t = s;
27500Sstevel@tonic-gate 
27510Sstevel@tonic-gate 	/*
27520Sstevel@tonic-gate 	 * uid (col 2)
27530Sstevel@tonic-gate 	 */
27540Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0 || s == t) {
27550Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no gid");
27560Sstevel@tonic-gate 		return (GENENT_PARSEERR);
27570Sstevel@tonic-gate 	}
27580Sstevel@tonic-gate 	*s++ = 0;
27590Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
27600Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
27610Sstevel@tonic-gate 	t = s;
27620Sstevel@tonic-gate 
27630Sstevel@tonic-gate 	/*
27640Sstevel@tonic-gate 	 * gid (col 3)
27650Sstevel@tonic-gate 	 */
27660Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0 || s == t) {
27670Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no gcos");
27680Sstevel@tonic-gate 		return (GENENT_PARSEERR);
27690Sstevel@tonic-gate 	}
27700Sstevel@tonic-gate 	*s++ = 0;
27710Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
27720Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
27730Sstevel@tonic-gate 	t = s;
27740Sstevel@tonic-gate 
27750Sstevel@tonic-gate 	/*
27760Sstevel@tonic-gate 	 * gcos (col 4)
27770Sstevel@tonic-gate 	 */
27780Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
27790Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no home");
27800Sstevel@tonic-gate 		return (GENENT_PARSEERR);
27810Sstevel@tonic-gate 	}
27820Sstevel@tonic-gate 	*s++ = 0;
27830Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_val = t;
27840Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_len = strlen(t)+1;
27850Sstevel@tonic-gate 	t = s;
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate 	/*
27880Sstevel@tonic-gate 	 * home (col 5)
27890Sstevel@tonic-gate 	 */
27900Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
27910Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no shell");
27920Sstevel@tonic-gate 		return (GENENT_PARSEERR);
27930Sstevel@tonic-gate 	}
27940Sstevel@tonic-gate 	*s++ = 0;
27950Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_val = t;
27960Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_len = strlen(t)+1;
27970Sstevel@tonic-gate 	t = s;
27980Sstevel@tonic-gate 
27990Sstevel@tonic-gate 	/*
28000Sstevel@tonic-gate 	 * shell (col 6)
28010Sstevel@tonic-gate 	 */
28020Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_val = t;
28030Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_len = strlen(t)+1;
28040Sstevel@tonic-gate 
28050Sstevel@tonic-gate 	/*
28060Sstevel@tonic-gate 	 * build entry
28070Sstevel@tonic-gate 	 */
28080Sstevel@tonic-gate 	data.pw_name = strdup(ecol[0].ec_value.ec_value_val);
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate 	if (flags & F_PASSWD) {
28110Sstevel@tonic-gate 		/* Add {crypt} before passwd entry */
28120Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname), "{crypt}%s",
28130Sstevel@tonic-gate 		    ecol[1].ec_value.ec_value_val);
28140Sstevel@tonic-gate 		data.pw_passwd = strdup(pname);
28150Sstevel@tonic-gate 	}
28160Sstevel@tonic-gate 	else
28170Sstevel@tonic-gate 		data.pw_passwd = NULL;
28180Sstevel@tonic-gate 
28190Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
28200Sstevel@tonic-gate 	    ecol[2].ec_value.ec_value_val[0] != '\0') {
28210Sstevel@tonic-gate 		data.pw_uid = ascii_to_int(ecol[2].ec_value.ec_value_val);
28220Sstevel@tonic-gate 		if (data.pw_uid == -1) {
28230Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
28240Sstevel@tonic-gate 			    "invalid uid : %s", ecol[2].ec_value.ec_value_val);
28250Sstevel@tonic-gate 		return (GENENT_PARSEERR);
28260Sstevel@tonic-gate 		}
28270Sstevel@tonic-gate 	} else
28280Sstevel@tonic-gate 		data.pw_uid = -1;
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate 	if (ecol[3].ec_value.ec_value_val != NULL &&
28310Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val[0] != '\0') {
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate 		data.pw_gid = ascii_to_int(ecol[3].ec_value.ec_value_val);
28340Sstevel@tonic-gate 		if (data.pw_gid == -1) {
28350Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
28360Sstevel@tonic-gate 			    "invalid gid : %s", ecol[3].ec_value.ec_value_val);
28370Sstevel@tonic-gate 		return (GENENT_PARSEERR);
28380Sstevel@tonic-gate 		}
28390Sstevel@tonic-gate 	} else
28400Sstevel@tonic-gate 		data.pw_gid = -1;
28410Sstevel@tonic-gate 
28420Sstevel@tonic-gate 	data.pw_age = NULL;
28430Sstevel@tonic-gate 	data.pw_comment = NULL;
28440Sstevel@tonic-gate 	data.pw_gecos = strdup(ecol[4].ec_value.ec_value_val);
28450Sstevel@tonic-gate 	data.pw_dir = strdup(ecol[5].ec_value.ec_value_val);
28460Sstevel@tonic-gate 	data.pw_shell = strdup(ecol[6].ec_value.ec_value_val);
28470Sstevel@tonic-gate 
28480Sstevel@tonic-gate 	if (flags & F_VERBOSE)
28490Sstevel@tonic-gate 		(void) fprintf(stdout,
28500Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.pw_name);
28510Sstevel@tonic-gate 
28520Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
28530Sstevel@tonic-gate 
28540Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
28550Sstevel@tonic-gate 		if (continue_onerror)
28560Sstevel@tonic-gate 			(void) fprintf(stderr,
28570Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
28580Sstevel@tonic-gate 			data.pw_name);
28590Sstevel@tonic-gate 		else {
28600Sstevel@tonic-gate 			rc = GENENT_CBERR;
28610Sstevel@tonic-gate 			(void) fprintf(stderr,
28620Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
28630Sstevel@tonic-gate 				data.pw_name);
28640Sstevel@tonic-gate 		}
28650Sstevel@tonic-gate 	} else if (retval)
28660Sstevel@tonic-gate 		rc = GENENT_CBERR;
28670Sstevel@tonic-gate 
28680Sstevel@tonic-gate 	free(data.pw_name);
28690Sstevel@tonic-gate 	free(data.pw_gecos);
28700Sstevel@tonic-gate 	free(data.pw_dir);
28710Sstevel@tonic-gate 	free(data.pw_shell);
28720Sstevel@tonic-gate 	return (rc);
28730Sstevel@tonic-gate }
28740Sstevel@tonic-gate 
28750Sstevel@tonic-gate 
28760Sstevel@tonic-gate static void
28770Sstevel@tonic-gate dump_passwd(ns_ldap_result_t *res)
28780Sstevel@tonic-gate {
28790Sstevel@tonic-gate 	char    **value = NULL;
28800Sstevel@tonic-gate 	char	pnam[256];
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "uid");
28830Sstevel@tonic-gate 	if (value == NULL)
28840Sstevel@tonic-gate 		return;
28850Sstevel@tonic-gate 	else
28860Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
28870Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "userPassword");
28880Sstevel@tonic-gate 	if (value == NULL)
28890Sstevel@tonic-gate 		(void) fprintf(stdout, "*:");
28900Sstevel@tonic-gate 	else {
28910Sstevel@tonic-gate 		(void) strcpy(pnam, value[0]);
28920Sstevel@tonic-gate 		if (strncasecmp(value[0], "{crypt}", 7) == 0)
28930Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", (pnam+7));
28940Sstevel@tonic-gate 		else
28950Sstevel@tonic-gate 			(void) fprintf(stdout, "*:");
28960Sstevel@tonic-gate 	}
28970Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "uidNumber");
28980Sstevel@tonic-gate 	if (value && value[0])
28990Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
29000Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "gidNumber");
29010Sstevel@tonic-gate 	if (value && value[0])
29020Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
29030Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "gecos");
29040Sstevel@tonic-gate 	if (value == NULL)
29050Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
29060Sstevel@tonic-gate 	else
29070Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
29080Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "homeDirectory");
29090Sstevel@tonic-gate 	if (value == NULL)
29100Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
29110Sstevel@tonic-gate 	else
29120Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
29130Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "loginShell");
29140Sstevel@tonic-gate 	if (value == NULL)
29150Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
29160Sstevel@tonic-gate 	else
29170Sstevel@tonic-gate 		(void) fprintf(stdout, "%s\n", value[0]);
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate }
29200Sstevel@tonic-gate 
29210Sstevel@tonic-gate /*
29220Sstevel@tonic-gate  * /etc/shadow
29230Sstevel@tonic-gate  */
29240Sstevel@tonic-gate 
29250Sstevel@tonic-gate static int
29260Sstevel@tonic-gate genent_shadow(char *line, int (*cback)())
29270Sstevel@tonic-gate {
29280Sstevel@tonic-gate 	char buf[BUFSIZ+1];
29290Sstevel@tonic-gate 	char *s, *t;
29300Sstevel@tonic-gate 	entry_col ecol[9];
29310Sstevel@tonic-gate 	char pname[BUFSIZ];
29320Sstevel@tonic-gate 
29330Sstevel@tonic-gate 	struct spwd	data;
29340Sstevel@tonic-gate 	int spflag;
29350Sstevel@tonic-gate 
29360Sstevel@tonic-gate 
29370Sstevel@tonic-gate 	/*
29380Sstevel@tonic-gate 	 * don't clobber our argument
29390Sstevel@tonic-gate 	 */
29400Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
29410Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
29420Sstevel@tonic-gate 		return (GENENT_PARSEERR);
29430Sstevel@tonic-gate 	}
29440Sstevel@tonic-gate 	(void) strcpy(buf, line);
29450Sstevel@tonic-gate 	t = buf;
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate 	/* ignore empty entries */
29480Sstevel@tonic-gate 	if (*t == '\0')
29490Sstevel@tonic-gate 		return (GENENT_OK);
29500Sstevel@tonic-gate 
29510Sstevel@tonic-gate 	/*
29520Sstevel@tonic-gate 	 * clear column data
29530Sstevel@tonic-gate 	 */
29540Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
29550Sstevel@tonic-gate 
29560Sstevel@tonic-gate 	/*
29570Sstevel@tonic-gate 	 * name (col 0)
29580Sstevel@tonic-gate 	 */
29590Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
29600Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no uid");
29610Sstevel@tonic-gate 		return (GENENT_PARSEERR);
29620Sstevel@tonic-gate 	}
29630Sstevel@tonic-gate 	*s++ = 0;
29640Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
29650Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
29660Sstevel@tonic-gate 	t = s;
29670Sstevel@tonic-gate 
29680Sstevel@tonic-gate 	/*
29690Sstevel@tonic-gate 	 * passwd (col 1)
29700Sstevel@tonic-gate 	 */
29710Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
29720Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "Improper format");
29730Sstevel@tonic-gate 		return (GENENT_PARSEERR);
29740Sstevel@tonic-gate 	}
29750Sstevel@tonic-gate 	*s++ = 0;
29760Sstevel@tonic-gate 
29770Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
29780Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
29790Sstevel@tonic-gate 
29800Sstevel@tonic-gate 	t = s;
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 	/*
29830Sstevel@tonic-gate 	 * shadow last change (col 2)
29840Sstevel@tonic-gate 	 */
29850Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
29860Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "Improper format");
29870Sstevel@tonic-gate 		return (GENENT_PARSEERR);
29880Sstevel@tonic-gate 	}
29890Sstevel@tonic-gate 	*s++ = 0;
29900Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_val = t;
29910Sstevel@tonic-gate 	ecol[2].ec_value.ec_value_len = strlen(t)+1;
29920Sstevel@tonic-gate 	t = s;
29930Sstevel@tonic-gate 
29940Sstevel@tonic-gate 	/*
29950Sstevel@tonic-gate 	 * shadow min (col 3)
29960Sstevel@tonic-gate 	 */
29970Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
29980Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "Improper format");
29990Sstevel@tonic-gate 		return (GENENT_PARSEERR);
30000Sstevel@tonic-gate 	}
30010Sstevel@tonic-gate 	*s++ = 0;
30020Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_val = t;
30030Sstevel@tonic-gate 	ecol[3].ec_value.ec_value_len = strlen(t)+1;
30040Sstevel@tonic-gate 	t = s;
30050Sstevel@tonic-gate 
30060Sstevel@tonic-gate 	/*
30070Sstevel@tonic-gate 	 * shadow max (col 4)
30080Sstevel@tonic-gate 	 */
30090Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
30100Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "Improper format");
30110Sstevel@tonic-gate 		return (GENENT_PARSEERR);
30120Sstevel@tonic-gate 	}
30130Sstevel@tonic-gate 	*s++ = 0;
30140Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_val = t;
30150Sstevel@tonic-gate 	ecol[4].ec_value.ec_value_len = strlen(t)+1;
30160Sstevel@tonic-gate 	t = s;
30170Sstevel@tonic-gate 
30180Sstevel@tonic-gate 	/*
30190Sstevel@tonic-gate 	 * shadow warn (col 5)
30200Sstevel@tonic-gate 	 */
30210Sstevel@tonic-gate 	if ((s = strchr(t, ':')) == 0) {
30220Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "Improper format");
30230Sstevel@tonic-gate 		return (GENENT_PARSEERR);
30240Sstevel@tonic-gate 	}
30250Sstevel@tonic-gate 	*s++ = 0;
30260Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_val = t;
30270Sstevel@tonic-gate 	ecol[5].ec_value.ec_value_len = strlen(t)+1;
30280Sstevel@tonic-gate 	t = s;
30290Sstevel@tonic-gate 
30300Sstevel@tonic-gate 	/*
30310Sstevel@tonic-gate 	 * shadow inactive (col 6)
30320Sstevel@tonic-gate 	 */
30330Sstevel@tonic-gate 	if ((s = strchr(t, ':')) != 0) {
30340Sstevel@tonic-gate 	*s++ = 0;
30350Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_val = t;
30360Sstevel@tonic-gate 	ecol[6].ec_value.ec_value_len = strlen(t)+1;
30370Sstevel@tonic-gate 	t = s;
30380Sstevel@tonic-gate 	}
30390Sstevel@tonic-gate 
30400Sstevel@tonic-gate 	/*
30410Sstevel@tonic-gate 	 * shadow expire  (col 7)
30420Sstevel@tonic-gate 	 */
30430Sstevel@tonic-gate 	if ((s = strchr(t, ':')) != 0) {
30440Sstevel@tonic-gate 	*s++ = 0;
30450Sstevel@tonic-gate 	ecol[7].ec_value.ec_value_val = t;
30460Sstevel@tonic-gate 	ecol[7].ec_value.ec_value_len = strlen(t)+1;
30470Sstevel@tonic-gate 	t = s;
30480Sstevel@tonic-gate 
30490Sstevel@tonic-gate 	/*
30500Sstevel@tonic-gate 	 * flag (col 8)
30510Sstevel@tonic-gate 	 */
30520Sstevel@tonic-gate 	ecol[8].ec_value.ec_value_val = t;
30530Sstevel@tonic-gate 	ecol[8].ec_value.ec_value_len = strlen(t)+1;
30540Sstevel@tonic-gate 	}
30550Sstevel@tonic-gate 
30560Sstevel@tonic-gate 	/*
30570Sstevel@tonic-gate 	 * build entry
30580Sstevel@tonic-gate 	 */
30590Sstevel@tonic-gate 
30600Sstevel@tonic-gate 	data.sp_namp = strdup(ecol[0].ec_value.ec_value_val);
30610Sstevel@tonic-gate 
30620Sstevel@tonic-gate 	if (ecol[1].ec_value.ec_value_val != NULL &&
30630Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val[0] != '\0') {
30640Sstevel@tonic-gate 		/* Add {crypt} before passwd entry */
30650Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname), "{crypt}%s",
30660Sstevel@tonic-gate 		    ecol[1].ec_value.ec_value_val);
30670Sstevel@tonic-gate 		data.sp_pwdp = strdup(pname);
30680Sstevel@tonic-gate 	} else
30690Sstevel@tonic-gate 		data.sp_pwdp = NULL;
30700Sstevel@tonic-gate 
30710Sstevel@tonic-gate 	if (ecol[2].ec_value.ec_value_val != NULL &&
30720Sstevel@tonic-gate 		ecol[2].ec_value.ec_value_val[0] != '\0') {
30730Sstevel@tonic-gate 
30740Sstevel@tonic-gate 		data.sp_lstchg = ascii_to_int(ecol[2].ec_value.ec_value_val);
30750Sstevel@tonic-gate 		if (data.sp_lstchg < -1) {
30760Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
30770Sstevel@tonic-gate 			    "invalid last changed date: %s",
30780Sstevel@tonic-gate 			    ecol[2].ec_value.ec_value_val);
30790Sstevel@tonic-gate 		return (GENENT_PARSEERR);
30800Sstevel@tonic-gate 		}
30810Sstevel@tonic-gate 	} else
30820Sstevel@tonic-gate 		data.sp_lstchg = -1;
30830Sstevel@tonic-gate 
30840Sstevel@tonic-gate 	if (ecol[3].ec_value.ec_value_val != NULL &&
30850Sstevel@tonic-gate 		ecol[3].ec_value.ec_value_val[0] != '\0') {
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 		data.sp_min = ascii_to_int(ecol[3].ec_value.ec_value_val);
30880Sstevel@tonic-gate 		if (data.sp_min < -1) {
30890Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
30900Sstevel@tonic-gate 			    "invalid sp_min : %s",
30910Sstevel@tonic-gate 			    ecol[3].ec_value.ec_value_val);
30920Sstevel@tonic-gate 		return (GENENT_PARSEERR);
30930Sstevel@tonic-gate 		}
30940Sstevel@tonic-gate 	} else
30950Sstevel@tonic-gate 		data.sp_min = -1;
30960Sstevel@tonic-gate 
30970Sstevel@tonic-gate 	if (ecol[4].ec_value.ec_value_val != NULL &&
30980Sstevel@tonic-gate 		ecol[4].ec_value.ec_value_val[0] != '\0') {
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate 		data.sp_max = ascii_to_int(ecol[4].ec_value.ec_value_val);
31010Sstevel@tonic-gate 		if (data.sp_max < -1) {
31020Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
31030Sstevel@tonic-gate 			    "invalid sp_max : %s",
31040Sstevel@tonic-gate 			    ecol[4].ec_value.ec_value_val);
31050Sstevel@tonic-gate 		return (GENENT_PARSEERR);
31060Sstevel@tonic-gate 		}
31070Sstevel@tonic-gate 	} else
31080Sstevel@tonic-gate 		data.sp_max = -1;
31090Sstevel@tonic-gate 
31100Sstevel@tonic-gate 	if (ecol[5].ec_value.ec_value_val != NULL &&
31110Sstevel@tonic-gate 		ecol[5].ec_value.ec_value_val[0] != '\0') {
31120Sstevel@tonic-gate 
31130Sstevel@tonic-gate 		data.sp_warn = ascii_to_int(ecol[5].ec_value.ec_value_val);
31140Sstevel@tonic-gate 		if (data.sp_warn < -1) {
31150Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
31160Sstevel@tonic-gate 			    "invalid sp_warn : %s",
31170Sstevel@tonic-gate 			    ecol[5].ec_value.ec_value_val);
31180Sstevel@tonic-gate 		return (GENENT_PARSEERR);
31190Sstevel@tonic-gate 		}
31200Sstevel@tonic-gate 	} else
31210Sstevel@tonic-gate 		data.sp_warn = -1;
31220Sstevel@tonic-gate 
31230Sstevel@tonic-gate 	if (ecol[6].ec_value.ec_value_val != NULL &&
31240Sstevel@tonic-gate 		ecol[6].ec_value.ec_value_val[0] != '\0') {
31250Sstevel@tonic-gate 
31260Sstevel@tonic-gate 		data.sp_inact = ascii_to_int(ecol[6].ec_value.ec_value_val);
31270Sstevel@tonic-gate 		if (data.sp_inact < -1) {
31280Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
31290Sstevel@tonic-gate 			    "invalid sp_inact : %s",
31300Sstevel@tonic-gate 			    ecol[6].ec_value.ec_value_val);
31310Sstevel@tonic-gate 		return (GENENT_PARSEERR);
31320Sstevel@tonic-gate 		}
31330Sstevel@tonic-gate 	} else
31340Sstevel@tonic-gate 		data.sp_inact = -1;
31350Sstevel@tonic-gate 
31360Sstevel@tonic-gate 	if (ecol[7].ec_value.ec_value_val != NULL &&
31370Sstevel@tonic-gate 		ecol[7].ec_value.ec_value_val[0] != '\0') {
31380Sstevel@tonic-gate 
31390Sstevel@tonic-gate 		data.sp_expire = ascii_to_int(ecol[7].ec_value.ec_value_val);
31400Sstevel@tonic-gate 		if (data.sp_expire < -1) {
31410Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
31420Sstevel@tonic-gate 			    "invalid login expiry date : %s",
31430Sstevel@tonic-gate 			    ecol[7].ec_value.ec_value_val);
31440Sstevel@tonic-gate 		return (GENENT_PARSEERR);
31450Sstevel@tonic-gate 		}
31460Sstevel@tonic-gate 	} else
31470Sstevel@tonic-gate 		data.sp_expire = -1;
31480Sstevel@tonic-gate 
31490Sstevel@tonic-gate 	if (ecol[8].ec_value.ec_value_val != NULL &&
31500Sstevel@tonic-gate 		ecol[8].ec_value.ec_value_val[0] != '\0') {
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate 		/*
31530Sstevel@tonic-gate 		 * data.sp_flag is an unsigned int,
31540Sstevel@tonic-gate 		 * assign -1 to it, make no sense.
31550Sstevel@tonic-gate 		 * Use spflag here to avoid lint warning.
31560Sstevel@tonic-gate 		 */
31570Sstevel@tonic-gate 		spflag = ascii_to_int(ecol[8].ec_value.ec_value_val);
31580Sstevel@tonic-gate 		if (spflag < 0) {
31590Sstevel@tonic-gate 			(void) snprintf(parse_err_msg, sizeof (parse_err_msg),
31600Sstevel@tonic-gate 			    "invalid flag value: %s",
31610Sstevel@tonic-gate 			    ecol[8].ec_value.ec_value_val);
31620Sstevel@tonic-gate 		return (GENENT_PARSEERR);
31630Sstevel@tonic-gate 		} else
31640Sstevel@tonic-gate 			data.sp_flag = spflag;
31650Sstevel@tonic-gate 	} else
31660Sstevel@tonic-gate 		data.sp_flag = 0;
31670Sstevel@tonic-gate 
31680Sstevel@tonic-gate 	if (flags & F_VERBOSE)
31690Sstevel@tonic-gate 		(void) fprintf(stdout,
31700Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.sp_namp);
31710Sstevel@tonic-gate 
31720Sstevel@tonic-gate 	if ((*cback)(&data, 1) && (continue_onerror == 0))
31730Sstevel@tonic-gate 		return (GENENT_CBERR);
31740Sstevel@tonic-gate 
31750Sstevel@tonic-gate 	free(data.sp_namp);
31760Sstevel@tonic-gate 	free(data.sp_pwdp);
31770Sstevel@tonic-gate 	return (GENENT_OK);
31780Sstevel@tonic-gate }
31790Sstevel@tonic-gate 
31800Sstevel@tonic-gate static void
31810Sstevel@tonic-gate dump_shadow(ns_ldap_result_t *res)
31820Sstevel@tonic-gate {
31830Sstevel@tonic-gate 	char    **value = NULL;
31840Sstevel@tonic-gate 	char   pnam[256];
31850Sstevel@tonic-gate 
31860Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "uid");
31870Sstevel@tonic-gate 	if (value == NULL)
31880Sstevel@tonic-gate 		return;
31890Sstevel@tonic-gate 	else
31900Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
31910Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "userPassword");
31920Sstevel@tonic-gate 	if (value == NULL)
31930Sstevel@tonic-gate 		(void) fprintf(stdout, "*:");
31940Sstevel@tonic-gate 	else {
31950Sstevel@tonic-gate 		(void) strcpy(pnam, value[0]);
31960Sstevel@tonic-gate 		if (strncasecmp(value[0], "{crypt}", 7) == 0)
31970Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", (pnam+7));
31980Sstevel@tonic-gate 		else
31990Sstevel@tonic-gate 			(void) fprintf(stdout, "*:");
32000Sstevel@tonic-gate 	}
32010Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "shadowLastChange");
32020Sstevel@tonic-gate 	if (value == NULL)
32030Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
32040Sstevel@tonic-gate 	else
32050Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
32060Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "shadowMin");
32070Sstevel@tonic-gate 	if (value == NULL)
32080Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
32090Sstevel@tonic-gate 	else
32100Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
32110Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "shadowMax");
32120Sstevel@tonic-gate 	if (value == NULL)
32130Sstevel@tonic-gate 		(void) fprintf(stdout, ":");
32140Sstevel@tonic-gate 	else
32150Sstevel@tonic-gate 		(void) fprintf(stdout, "%s:", value[0]);
32160Sstevel@tonic-gate 
32170Sstevel@tonic-gate 	/* ignore shadowWarning, shadowInactive, shadowExpire, shadowFlag */
32180Sstevel@tonic-gate 	(void) fprintf(stdout, ":::\n");
32190Sstevel@tonic-gate 
32200Sstevel@tonic-gate }
32210Sstevel@tonic-gate 
32220Sstevel@tonic-gate 
32230Sstevel@tonic-gate static int
32240Sstevel@tonic-gate genent_bootparams(char *line, int (*cback)())
32250Sstevel@tonic-gate {
32260Sstevel@tonic-gate 	char buf[BUFSIZ+1];
32270Sstevel@tonic-gate 	char *t;
32280Sstevel@tonic-gate 	entry_col ecol[2];
32290Sstevel@tonic-gate 	int ctr = 0, retval = 1;
32300Sstevel@tonic-gate 
32310Sstevel@tonic-gate 	struct _ns_bootp data;
32320Sstevel@tonic-gate 	char *parameter;
32330Sstevel@tonic-gate 	int rc = GENENT_OK;
32340Sstevel@tonic-gate 
32350Sstevel@tonic-gate 	/*
32360Sstevel@tonic-gate 	 * don't clobber our argument
32370Sstevel@tonic-gate 	 */
32380Sstevel@tonic-gate 	if (strlen(line) >= sizeof (buf)) {
32390Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "line too long");
32400Sstevel@tonic-gate 		return (GENENT_PARSEERR);
32410Sstevel@tonic-gate 	}
32420Sstevel@tonic-gate 	(void) strcpy(buf, line);
32430Sstevel@tonic-gate 
32440Sstevel@tonic-gate 	/*
32450Sstevel@tonic-gate 	 * clear column data
32460Sstevel@tonic-gate 	 */
32470Sstevel@tonic-gate 	(void) memset((char *)ecol, 0, sizeof (ecol));
32480Sstevel@tonic-gate 
32490Sstevel@tonic-gate 
32500Sstevel@tonic-gate 	/*
32510Sstevel@tonic-gate 	 * cname (col 0)
32520Sstevel@tonic-gate 	 */
32530Sstevel@tonic-gate 	if ((t = strtok(buf, " \t")) == 0) {
32540Sstevel@tonic-gate 		(void) strcpy(parse_err_msg, "no cname");
32550Sstevel@tonic-gate 		return (GENENT_PARSEERR);
32560Sstevel@tonic-gate 	}
32570Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_val = t;
32580Sstevel@tonic-gate 	ecol[0].ec_value.ec_value_len = strlen(t)+1;
32590Sstevel@tonic-gate 
32600Sstevel@tonic-gate 
32610Sstevel@tonic-gate 
32620Sstevel@tonic-gate 	/* build entry */
32630Sstevel@tonic-gate 	data.name = strdup(ecol[0].ec_value.ec_value_val);
32640Sstevel@tonic-gate 
32650Sstevel@tonic-gate 	/*
32660Sstevel@tonic-gate 	 * name (col 1)
32670Sstevel@tonic-gate 	 */
32680Sstevel@tonic-gate 
32690Sstevel@tonic-gate 	data.param = NULL;
32700Sstevel@tonic-gate 
32710Sstevel@tonic-gate 	do {
32720Sstevel@tonic-gate 
32730Sstevel@tonic-gate 		/*
32740Sstevel@tonic-gate 		 * don't clobber comment in canonical entry
32750Sstevel@tonic-gate 		 */
32760Sstevel@tonic-gate 
32770Sstevel@tonic-gate 
32780Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_val = t;
32790Sstevel@tonic-gate 		ecol[1].ec_value.ec_value_len = strlen(t)+1;
32800Sstevel@tonic-gate 
32810Sstevel@tonic-gate 		ctr++;
32820Sstevel@tonic-gate 		parameter = strdup(ecol[1].ec_value.ec_value_val);
32830Sstevel@tonic-gate 		if ((data.param = (char **)realloc(data.param,
32840Sstevel@tonic-gate 			(ctr + 1) * sizeof (char **))) == NULL) {
32850Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("out of memory\n"));
32860Sstevel@tonic-gate 			exit(1);
32870Sstevel@tonic-gate 		}
32880Sstevel@tonic-gate 		data.param[ctr-1] = parameter;
32890Sstevel@tonic-gate 
32900Sstevel@tonic-gate 	} while (t = strtok(NULL, " \t"));
32910Sstevel@tonic-gate 
32920Sstevel@tonic-gate 
32930Sstevel@tonic-gate 	/* End the list of all the aliases by NULL */
32940Sstevel@tonic-gate 	if ((data.param = (char **)realloc(data.param,
32950Sstevel@tonic-gate 		(ctr + 1) * sizeof (char **))) == NULL) {
32960Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
32970Sstevel@tonic-gate 		exit(1);
32980Sstevel@tonic-gate 	}
32990Sstevel@tonic-gate 	data.param[ctr] = NULL;
33000Sstevel@tonic-gate 
33010Sstevel@tonic-gate 	if (flags & F_VERBOSE)
33020Sstevel@tonic-gate 		(void) fprintf(stdout,
33030Sstevel@tonic-gate 		    gettext("Adding entry : %s\n"), data.name);
33040Sstevel@tonic-gate 
33050Sstevel@tonic-gate 	retval = (*cback)(&data, 0);
33060Sstevel@tonic-gate 
33070Sstevel@tonic-gate 	if (retval == LDAP_ALREADY_EXISTS) {
33080Sstevel@tonic-gate 		if (continue_onerror)
33090Sstevel@tonic-gate 			(void) fprintf(stderr,
33100Sstevel@tonic-gate 			gettext("Entry: %s - already Exists, skipping it.\n"),
33110Sstevel@tonic-gate 			data.name);
33120Sstevel@tonic-gate 		else {
33130Sstevel@tonic-gate 			rc = GENENT_CBERR;
33140Sstevel@tonic-gate 			(void) fprintf(stderr,
33150Sstevel@tonic-gate 				gettext("Entry: %s - already Exists\n"),
33160Sstevel@tonic-gate 				data.name);
33170Sstevel@tonic-gate 		}
33180Sstevel@tonic-gate 	} else if (retval)
33190Sstevel@tonic-gate 		rc = GENENT_CBERR;
33200Sstevel@tonic-gate 
33210Sstevel@tonic-gate 	free(data.name);
33220Sstevel@tonic-gate 	free(data.param);
33230Sstevel@tonic-gate 
33240Sstevel@tonic-gate 	return (rc);
33250Sstevel@tonic-gate 
33260Sstevel@tonic-gate }
33270Sstevel@tonic-gate 
33280Sstevel@tonic-gate 
33290Sstevel@tonic-gate static void
33300Sstevel@tonic-gate dump_bootparams(ns_ldap_result_t *res)
33310Sstevel@tonic-gate {
33320Sstevel@tonic-gate 	char	**value = NULL;
33330Sstevel@tonic-gate 	int		attr_count = 0;
33340Sstevel@tonic-gate 
33350Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "cn");
33360Sstevel@tonic-gate 	if (value[0] != NULL)
33370Sstevel@tonic-gate 		(void) fprintf(stdout, "%s", value[0]);
33380Sstevel@tonic-gate 	value = __ns_ldap_getAttr(res->entry, "bootParameter");
33390Sstevel@tonic-gate 	if (value != NULL)
33400Sstevel@tonic-gate 		while (value[attr_count] != NULL) {
33410Sstevel@tonic-gate 		(void) fprintf(stdout, "\t%s", value[attr_count]);
33420Sstevel@tonic-gate 			attr_count++;
33430Sstevel@tonic-gate 		}
33440Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
33450Sstevel@tonic-gate 
33460Sstevel@tonic-gate 
33470Sstevel@tonic-gate }
33480Sstevel@tonic-gate 
33490Sstevel@tonic-gate static char *
33500Sstevel@tonic-gate fget_line_at(struct line_buf *line, int n, FILE *fp)
33510Sstevel@tonic-gate {
33520Sstevel@tonic-gate 	int c;
33530Sstevel@tonic-gate 
33540Sstevel@tonic-gate 	line->len = n;
33550Sstevel@tonic-gate 
33560Sstevel@tonic-gate 	for (;;) {
33570Sstevel@tonic-gate 		c = fgetc(fp);
33580Sstevel@tonic-gate 		if (c == -1)
33590Sstevel@tonic-gate 			break;
33600Sstevel@tonic-gate 		if (line->len >= line->alloc)
33610Sstevel@tonic-gate 			line_buf_expand(line);
33620Sstevel@tonic-gate 		line->str[line->len++] = c;
33630Sstevel@tonic-gate 
33640Sstevel@tonic-gate 		if (c == '\n')
33650Sstevel@tonic-gate 			break;
33660Sstevel@tonic-gate 	}
33670Sstevel@tonic-gate 
33680Sstevel@tonic-gate 	/* Null Terminate */
33690Sstevel@tonic-gate 	if (line->len >= line->alloc)
33700Sstevel@tonic-gate 		line_buf_expand(line);
33710Sstevel@tonic-gate 	line->str[line->len++] = 0;
33720Sstevel@tonic-gate 
33730Sstevel@tonic-gate 	/* if no characters are read, return NULL to indicate EOF */
33740Sstevel@tonic-gate 	if (line->str[0] == '\0')
33750Sstevel@tonic-gate 		return (0);
33760Sstevel@tonic-gate 
33770Sstevel@tonic-gate 	return (line->str);
33780Sstevel@tonic-gate }
33790Sstevel@tonic-gate 
33800Sstevel@tonic-gate /*
33810Sstevel@tonic-gate  * return a line from the file, discarding comments and blank lines
33820Sstevel@tonic-gate  */
33830Sstevel@tonic-gate static int
33840Sstevel@tonic-gate filedbmline_comment(struct line_buf *line, FILE *etcf, int *lineno,
33850Sstevel@tonic-gate     struct file_loc *loc)
33860Sstevel@tonic-gate {
33870Sstevel@tonic-gate 	int i, len = 0;
33880Sstevel@tonic-gate 
33890Sstevel@tonic-gate 	loc->offset = ftell(etcf);
33900Sstevel@tonic-gate 	for (;;) {
33910Sstevel@tonic-gate 		if (fget_line_at(line, len, etcf) == 0)
33920Sstevel@tonic-gate 			return (0);
33930Sstevel@tonic-gate 
33940Sstevel@tonic-gate 		if (lineno)
33950Sstevel@tonic-gate 			(*lineno)++;
33960Sstevel@tonic-gate 
33970Sstevel@tonic-gate 		len = strlen(line->str);
33980Sstevel@tonic-gate 		if (len >= 2 &&
33990Sstevel@tonic-gate 		    line->str[0] != '#' &&
34000Sstevel@tonic-gate 		    line->str[len-2] == '\\' && line->str[len-1] == '\n') {
34010Sstevel@tonic-gate 			line->str[len-2] = 0;
34020Sstevel@tonic-gate 			len -= 2;
34030Sstevel@tonic-gate 			continue;    /* append next line at end */
34040Sstevel@tonic-gate 		}
34050Sstevel@tonic-gate 
34060Sstevel@tonic-gate 		if (line->str[len-1] == '\n') {
34070Sstevel@tonic-gate 			line->str[len-1] = 0;
34080Sstevel@tonic-gate 			len -= 1;
34090Sstevel@tonic-gate 		}
34100Sstevel@tonic-gate 
34110Sstevel@tonic-gate 		/*
34120Sstevel@tonic-gate 		 * Skip lines where '#' is the first non-blank character.
34130Sstevel@tonic-gate 		 */
34140Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
34150Sstevel@tonic-gate 			if (line->str[i] == '#') {
34160Sstevel@tonic-gate 				line->str[i] = '\0';
34170Sstevel@tonic-gate 				len = i;
34180Sstevel@tonic-gate 				break;
34190Sstevel@tonic-gate 			}
34200Sstevel@tonic-gate 			if (line->str[i] != ' ' && line->str[i] != '\t')
34210Sstevel@tonic-gate 				break;
34220Sstevel@tonic-gate 		}
34230Sstevel@tonic-gate 
34240Sstevel@tonic-gate 		/*
34250Sstevel@tonic-gate 		 * A line with one or more white space characters followed
34260Sstevel@tonic-gate 		 * by a comment will now be blank. The special case of a
34270Sstevel@tonic-gate 		 * line with '#' in the first byte will have len == 0.
34280Sstevel@tonic-gate 		 */
34290Sstevel@tonic-gate 		if (len > 0 && !blankline(line->str))
34300Sstevel@tonic-gate 			break;
34310Sstevel@tonic-gate 
34320Sstevel@tonic-gate 		len = 0;
34330Sstevel@tonic-gate 		loc->offset = ftell(etcf);
34340Sstevel@tonic-gate 	}
34350Sstevel@tonic-gate 
34360Sstevel@tonic-gate 	loc->size = len;
34370Sstevel@tonic-gate 	return (1);
34380Sstevel@tonic-gate }
34390Sstevel@tonic-gate 
34400Sstevel@tonic-gate /*
34410Sstevel@tonic-gate  * return a line from the file, discarding comments, blanks, and '+' lines
34420Sstevel@tonic-gate  */
34430Sstevel@tonic-gate static int
34440Sstevel@tonic-gate filedbmline_plus(struct line_buf *line, FILE *etcf, int *lineno,
34450Sstevel@tonic-gate     struct file_loc *loc)
34460Sstevel@tonic-gate {
34470Sstevel@tonic-gate 	int len = 0;
34480Sstevel@tonic-gate 
34490Sstevel@tonic-gate 	loc->offset = ftell(etcf);
34500Sstevel@tonic-gate 	for (;;) {
34510Sstevel@tonic-gate 		if (fget_line_at(line, len, etcf) == 0)
34520Sstevel@tonic-gate 			return (0);
34530Sstevel@tonic-gate 
34540Sstevel@tonic-gate 		if (lineno)
34550Sstevel@tonic-gate 			(*lineno)++;
34560Sstevel@tonic-gate 
34570Sstevel@tonic-gate 		len = strlen(line->str);
34580Sstevel@tonic-gate 		if (line->str[len-1] == '\n') {
34590Sstevel@tonic-gate 			line->str[len-1] = 0;
34600Sstevel@tonic-gate 			len -= 1;
34610Sstevel@tonic-gate 		}
34620Sstevel@tonic-gate 
34630Sstevel@tonic-gate 		if (!blankline(line->str) &&
34640Sstevel@tonic-gate 		line->str[0] != '+' && line->str[0] != '-' &&
34650Sstevel@tonic-gate 		line->str[0] != '#')
34660Sstevel@tonic-gate 			break;
34670Sstevel@tonic-gate 
34680Sstevel@tonic-gate 		len = 0;
34690Sstevel@tonic-gate 		loc->offset = ftell(etcf);
34700Sstevel@tonic-gate 	}
34710Sstevel@tonic-gate 
34720Sstevel@tonic-gate 	loc->size = len;
34730Sstevel@tonic-gate 	return (1);
34740Sstevel@tonic-gate }
34750Sstevel@tonic-gate 
34760Sstevel@tonic-gate 
34770Sstevel@tonic-gate /* Populating the ttypelist structure */
34780Sstevel@tonic-gate 
34790Sstevel@tonic-gate static struct ttypelist_t ttypelist[] = {
34800Sstevel@tonic-gate 	{ NS_LDAP_TYPE_HOSTS, genent_hosts, dump_hosts,
34810Sstevel@tonic-gate 		filedbmline_comment, "iphost" },
34820Sstevel@tonic-gate 	{ NS_LDAP_TYPE_IPNODES, genent_hosts, dump_hosts,
34830Sstevel@tonic-gate 		filedbmline_comment, "iphost" },
34840Sstevel@tonic-gate 	{ NS_LDAP_TYPE_RPC, genent_rpc, dump_rpc,
34850Sstevel@tonic-gate 		filedbmline_comment, "oncrpc" },
34860Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PROTOCOLS, genent_protocols, dump_protocols,
34870Sstevel@tonic-gate 		filedbmline_comment, "ipprotocol" },
34880Sstevel@tonic-gate 	{ NS_LDAP_TYPE_NETWORKS, genent_networks, dump_networks,
34890Sstevel@tonic-gate 		filedbmline_comment, "ipnetwork"  },
34900Sstevel@tonic-gate 	{ NS_LDAP_TYPE_SERVICES, genent_services, dump_services,
34910Sstevel@tonic-gate 		filedbmline_comment, "ipservice" },
34920Sstevel@tonic-gate 	{ NS_LDAP_TYPE_GROUP, genent_group, dump_group,
34930Sstevel@tonic-gate 		filedbmline_plus, "posixgroup" },
34940Sstevel@tonic-gate 	{ NS_LDAP_TYPE_NETMASKS, genent_netmasks, dump_netmasks,
34950Sstevel@tonic-gate 		filedbmline_comment, "ipnetwork" },
34960Sstevel@tonic-gate 	{ NS_LDAP_TYPE_ETHERS, genent_ethers, dump_ethers,
34970Sstevel@tonic-gate 		filedbmline_comment, "ieee802Device" },
34980Sstevel@tonic-gate 	{ NS_LDAP_TYPE_NETGROUP, genent_netgroup, dump_netgroup,
34990Sstevel@tonic-gate 		filedbmline_comment, "nisnetgroup" },
35000Sstevel@tonic-gate 	{ NS_LDAP_TYPE_BOOTPARAMS, genent_bootparams, dump_bootparams,
35010Sstevel@tonic-gate 		filedbmline_comment, "bootableDevice" },
35020Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PUBLICKEY, genent_publickey, NULL /* dump_publickey */,
35030Sstevel@tonic-gate 		filedbmline_comment, "niskeyobject" },
35040Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PASSWD, genent_passwd, dump_passwd,
35050Sstevel@tonic-gate 		filedbmline_plus, "posixaccount" },
35060Sstevel@tonic-gate 	{ NS_LDAP_TYPE_SHADOW, genent_shadow, dump_shadow,
35070Sstevel@tonic-gate 		filedbmline_plus, "shadowaccount" },
35080Sstevel@tonic-gate 	{ NS_LDAP_TYPE_ALIASES, genent_aliases, dump_aliases,
35090Sstevel@tonic-gate 		filedbmline_plus, "mailGroup" },
35100Sstevel@tonic-gate 	{ NS_LDAP_TYPE_AUTOMOUNT, genent_automount, dump_automount,
35110Sstevel@tonic-gate 		filedbmline_comment, "automount" },
35120Sstevel@tonic-gate 	{ NS_LDAP_TYPE_USERATTR, genent_user_attr, dump_user_attr,
35130Sstevel@tonic-gate 		filedbmline_comment, "SolarisUserAttr" },
35140Sstevel@tonic-gate 	{ NS_LDAP_TYPE_PROFILE, genent_prof_attr, dump_prof_attr,
35150Sstevel@tonic-gate 		filedbmline_comment, "SolarisProfAttr" },
35160Sstevel@tonic-gate 	{ NS_LDAP_TYPE_EXECATTR, genent_exec_attr, dump_exec_attr,
35170Sstevel@tonic-gate 		filedbmline_comment, "SolarisExecAttr" },
35180Sstevel@tonic-gate 	{ NS_LDAP_TYPE_AUTHATTR, genent_auth_attr, dump_auth_attr,
35190Sstevel@tonic-gate 		filedbmline_comment, "SolarisAuthAttr" },
35200Sstevel@tonic-gate 	{ NS_LDAP_TYPE_AUUSER, genent_audit_user, dump_audit_user,
35210Sstevel@tonic-gate 		filedbmline_comment, "SolarisAuditUser" },
35220Sstevel@tonic-gate 	{ 0, 0, 0, 0, 0 }
35230Sstevel@tonic-gate };
35240Sstevel@tonic-gate 
35250Sstevel@tonic-gate 
35260Sstevel@tonic-gate 
35270Sstevel@tonic-gate 
35280Sstevel@tonic-gate static int lineno = 0;
35290Sstevel@tonic-gate 
35300Sstevel@tonic-gate static	void
35310Sstevel@tonic-gate addfile()
35320Sstevel@tonic-gate {
35330Sstevel@tonic-gate 	struct line_buf line;
35340Sstevel@tonic-gate 	struct file_loc loc;
35350Sstevel@tonic-gate 
35360Sstevel@tonic-gate 	/* Initializing the Line Buffer */
35370Sstevel@tonic-gate 	line_buf_init(&line);
35380Sstevel@tonic-gate 
35390Sstevel@tonic-gate 	/* Loop through all the lines in the file */
35400Sstevel@tonic-gate 	while (tt->filedbmline(&line, etcf, &lineno, &loc)) {
35410Sstevel@tonic-gate 		switch ((*(tt->genent))(line.str, addentry)) {
35420Sstevel@tonic-gate 		case GENENT_OK:
35430Sstevel@tonic-gate 			break;
35440Sstevel@tonic-gate 		case GENENT_PARSEERR:
35450Sstevel@tonic-gate 			(void) fprintf(stderr,
35460Sstevel@tonic-gate 			    gettext("parse error: %s (line %d)\n"),
35470Sstevel@tonic-gate 			    parse_err_msg, lineno);
35480Sstevel@tonic-gate 			exit_val = 1;
35490Sstevel@tonic-gate 			break;
35500Sstevel@tonic-gate 		case GENENT_CBERR:
35510Sstevel@tonic-gate 			(void) fprintf(stderr,
35520Sstevel@tonic-gate 			    gettext("Error while adding line: %s\n"),
35530Sstevel@tonic-gate 			    line.str);
35540Sstevel@tonic-gate 			exit_val = 2;
35550Sstevel@tonic-gate 			free(line.str);
35560Sstevel@tonic-gate 			return;
35570Sstevel@tonic-gate 			break;
35580Sstevel@tonic-gate 		case GENENT_ERR:
35590Sstevel@tonic-gate 			(void) fprintf(stderr,
35600Sstevel@tonic-gate 			    gettext("Internal Error while adding line: %s\n"),
35610Sstevel@tonic-gate 			    line.str);
35620Sstevel@tonic-gate 			exit_val = 3;
35630Sstevel@tonic-gate 			free(line.str);
35640Sstevel@tonic-gate 			return;
35650Sstevel@tonic-gate 			break;
35660Sstevel@tonic-gate 		}
35670Sstevel@tonic-gate 	}
35680Sstevel@tonic-gate 	free(line.str);
35690Sstevel@tonic-gate }
35700Sstevel@tonic-gate 
35710Sstevel@tonic-gate static void
35720Sstevel@tonic-gate dumptable(char *service)
35730Sstevel@tonic-gate {
35740Sstevel@tonic-gate 
35750Sstevel@tonic-gate 	ns_ldap_result_t *eres = NULL;
35760Sstevel@tonic-gate 	ns_ldap_error_t *err = NULL;
35770Sstevel@tonic-gate 	int	rc = 0, success = 0;
35780Sstevel@tonic-gate 	char	filter[BUFSIZ];
35790Sstevel@tonic-gate 	int	done = 0;
35800Sstevel@tonic-gate 	void	*cookie = NULL;
35810Sstevel@tonic-gate 
35820Sstevel@tonic-gate 	/* set the appropriate filter */
35830Sstevel@tonic-gate 	if (strcmp(tt->ttype, NS_LDAP_TYPE_PROFILE) == 0) {
35840Sstevel@tonic-gate 		/*
35850Sstevel@tonic-gate 		 * prof_attr entries are SolarisProfAttr
35860Sstevel@tonic-gate 		 * without AUXILIARY SolarisExecAttr
35870Sstevel@tonic-gate 		 */
35880Sstevel@tonic-gate 		(void) snprintf(filter, sizeof (filter),
35890Sstevel@tonic-gate 		    "(&(objectclass=%s)(!(objectclass=SolarisExecAttr)))",
35900Sstevel@tonic-gate 		    tt->objclass);
35910Sstevel@tonic-gate 	} else
35920Sstevel@tonic-gate 		(void) snprintf(filter, sizeof (filter),
35930Sstevel@tonic-gate 		    "(objectclass=%s)", tt->objclass);
35940Sstevel@tonic-gate 
35950Sstevel@tonic-gate 	if (flags & F_VERBOSE)
35960Sstevel@tonic-gate 		(void) fprintf(stdout, gettext("FILTER = %s\n"), filter);
35970Sstevel@tonic-gate 
35980Sstevel@tonic-gate 	/* Pass cred only if supplied. Cred is not always needed for dump */
35990Sstevel@tonic-gate 	if (authority.cred.unix_cred.userID == NULL ||
36000Sstevel@tonic-gate 	    authority.cred.unix_cred.passwd == NULL)
36010Sstevel@tonic-gate 		rc = __ns_ldap_firstEntry(service, filter, NULL, NULL,
36020Sstevel@tonic-gate 		    NULL, NS_LDAP_HARD, &cookie, &eres, &err, NULL);
36030Sstevel@tonic-gate 	else
36040Sstevel@tonic-gate 		rc = __ns_ldap_firstEntry(service, filter, NULL, NULL,
36050Sstevel@tonic-gate 		    &authority, NS_LDAP_HARD, &cookie, &eres, &err, NULL);
36060Sstevel@tonic-gate 
36070Sstevel@tonic-gate 	switch (rc) {
36080Sstevel@tonic-gate 	case NS_LDAP_SUCCESS:
36090Sstevel@tonic-gate 		nent_add++;
36100Sstevel@tonic-gate 		success = 1;
36110Sstevel@tonic-gate 		if (eres != NULL) {
36120Sstevel@tonic-gate 			if (strcmp(databasetype, "publickey") == 0)
36130Sstevel@tonic-gate 				dump_publickey(eres, service);
36140Sstevel@tonic-gate 			else
36150Sstevel@tonic-gate 				(*(tt->dump))(eres);
36160Sstevel@tonic-gate 		}
36170Sstevel@tonic-gate 		else
36180Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("No entries found.\n"));
36190Sstevel@tonic-gate 		break;
36200Sstevel@tonic-gate 
36210Sstevel@tonic-gate 	case NS_LDAP_OP_FAILED:
36220Sstevel@tonic-gate 		exit_val = 2;
36230Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("operation failed.\n"));
36240Sstevel@tonic-gate 		break;
36250Sstevel@tonic-gate 
36260Sstevel@tonic-gate 	case NS_LDAP_INVALID_PARAM:
36270Sstevel@tonic-gate 		exit_val = 2;
36280Sstevel@tonic-gate 		(void) fprintf(stderr,
36290Sstevel@tonic-gate 		    gettext("invalid parameter(s) passed.\n"));
36300Sstevel@tonic-gate 		break;
36310Sstevel@tonic-gate 
36320Sstevel@tonic-gate 	case NS_LDAP_NOTFOUND:
36330Sstevel@tonic-gate 		exit_val = 2;
36340Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("entry not found.\n"));
36350Sstevel@tonic-gate 		break;
36360Sstevel@tonic-gate 
36370Sstevel@tonic-gate 	case NS_LDAP_MEMORY:
36380Sstevel@tonic-gate 		exit_val = 2;
36390Sstevel@tonic-gate 		(void) fprintf(stderr,
36400Sstevel@tonic-gate 			gettext("internal memory allocation error.\n"));
36410Sstevel@tonic-gate 		break;
36420Sstevel@tonic-gate 
36430Sstevel@tonic-gate 	case NS_LDAP_CONFIG:
36440Sstevel@tonic-gate 		exit_val = 2;
36450Sstevel@tonic-gate 		(void) fprintf(stderr,
36460Sstevel@tonic-gate 			gettext("LDAP Configuration problem.\n"));
36470Sstevel@tonic-gate 		perr(err);
36480Sstevel@tonic-gate 		break;
36490Sstevel@tonic-gate 
36500Sstevel@tonic-gate 	case NS_LDAP_PARTIAL:
36510Sstevel@tonic-gate 		exit_val = 2;
36520Sstevel@tonic-gate 		(void) fprintf(stderr,
36530Sstevel@tonic-gate 			gettext("partial result returned\n"));
36540Sstevel@tonic-gate 		perr(err);
36550Sstevel@tonic-gate 		break;
36560Sstevel@tonic-gate 
36570Sstevel@tonic-gate 	case NS_LDAP_INTERNAL:
36580Sstevel@tonic-gate 		exit_val = 2;
36590Sstevel@tonic-gate 		(void) fprintf(stderr,
36600Sstevel@tonic-gate 			gettext("internal LDAP error occured.\n"));
36610Sstevel@tonic-gate 		perr(err);
36620Sstevel@tonic-gate 		break;
36630Sstevel@tonic-gate 	}
36640Sstevel@tonic-gate 
36650Sstevel@tonic-gate 	if (eres != NULL) {
36660Sstevel@tonic-gate 		(void) __ns_ldap_freeResult(&eres);
36670Sstevel@tonic-gate 		eres = NULL;
36680Sstevel@tonic-gate 	}
36690Sstevel@tonic-gate 
36700Sstevel@tonic-gate 	if (success) {
36710Sstevel@tonic-gate 		while (!done) {
36720Sstevel@tonic-gate 			rc = __ns_ldap_nextEntry(cookie, &eres, &err);
36730Sstevel@tonic-gate 			if (rc != NS_LDAP_SUCCESS || eres  == NULL) {
36740Sstevel@tonic-gate 				done = 1;
36750Sstevel@tonic-gate 				continue;
36760Sstevel@tonic-gate 			}
36770Sstevel@tonic-gate 
36780Sstevel@tonic-gate 			/* Print the result */
36790Sstevel@tonic-gate 			if (eres != NULL) {
36800Sstevel@tonic-gate 				if (strcmp(databasetype, "publickey") == 0)
36810Sstevel@tonic-gate 					dump_publickey(eres, service);
36820Sstevel@tonic-gate 				else
36830Sstevel@tonic-gate 					(*(tt->dump))(eres);
36840Sstevel@tonic-gate 				(void) __ns_ldap_freeResult(&eres);
36850Sstevel@tonic-gate 				eres = NULL;
36860Sstevel@tonic-gate 			}
36870Sstevel@tonic-gate 		}
36880Sstevel@tonic-gate 	}
36890Sstevel@tonic-gate }
36900Sstevel@tonic-gate 
3691702Sth160488 int
36920Sstevel@tonic-gate main(int argc, char **argv)
36930Sstevel@tonic-gate {
36940Sstevel@tonic-gate 	char	*password;
36950Sstevel@tonic-gate 	int	c;
36960Sstevel@tonic-gate 	int	rc;
36970Sstevel@tonic-gate 	int	ldaprc;
36980Sstevel@tonic-gate 	int		authstried = 0;
36990Sstevel@tonic-gate 	int		supportedauth = 0;
37000Sstevel@tonic-gate 	int		op = OP_ADD;
37010Sstevel@tonic-gate 	char	*ttype, *authmech = 0, *etcfile = 0;
37020Sstevel@tonic-gate 	char	ps[LDAP_MAXNAMELEN]; /* Temporary password variable */
37030Sstevel@tonic-gate 	char	filter[BUFSIZ];
37040Sstevel@tonic-gate 	void	**paramVal = NULL;
37050Sstevel@tonic-gate 	ns_auth_t	**app;
37060Sstevel@tonic-gate 	ns_auth_t	**authpp = NULL;
37070Sstevel@tonic-gate 	ns_auth_t	*authp = NULL;
37080Sstevel@tonic-gate 	ns_ldap_error_t	*errorp = NULL;
37090Sstevel@tonic-gate 	ns_ldap_result_t *resultp;
37100Sstevel@tonic-gate 	ns_ldap_entry_t *e;
37110Sstevel@tonic-gate 	int	flag = 0;
37120Sstevel@tonic-gate 	int	version1 = 0;
37130Sstevel@tonic-gate 
37140Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
37150Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
37160Sstevel@tonic-gate 
37170Sstevel@tonic-gate 	openlog("ldapaddent", LOG_PID, LOG_USER);
37180Sstevel@tonic-gate 
37190Sstevel@tonic-gate 	inputbasedn = NULL;
37200Sstevel@tonic-gate 	authority.cred.unix_cred.passwd = NULL;
37210Sstevel@tonic-gate 	authority.cred.unix_cred.userID = NULL;
37220Sstevel@tonic-gate 	authority.auth.type = NS_LDAP_AUTH_SIMPLE;
37230Sstevel@tonic-gate 
37240Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "cdhvpf:D:w:b:a:")) != EOF) {
37250Sstevel@tonic-gate 		switch (c) {
37260Sstevel@tonic-gate 		case 'd':
37270Sstevel@tonic-gate 			if (op)
37280Sstevel@tonic-gate 				usage("no other option should be specified");
37290Sstevel@tonic-gate 			op = OP_DUMP;
37300Sstevel@tonic-gate 			break;
37310Sstevel@tonic-gate 		case 'c':
37320Sstevel@tonic-gate 			continue_onerror = 1;
37330Sstevel@tonic-gate 			break;
37340Sstevel@tonic-gate 		case 'v':
37350Sstevel@tonic-gate 			flags |= F_VERBOSE;
37360Sstevel@tonic-gate 			break;
37370Sstevel@tonic-gate 		case 'p':
37380Sstevel@tonic-gate 			flags |= F_PASSWD;
37390Sstevel@tonic-gate 			break;
37400Sstevel@tonic-gate 		case 'f':
37410Sstevel@tonic-gate 			etcfile = optarg;
37420Sstevel@tonic-gate 			break;
37430Sstevel@tonic-gate 		case 'D':
37440Sstevel@tonic-gate 			authority.cred.unix_cred.userID = strdup(optarg);
37450Sstevel@tonic-gate 			break;
37460Sstevel@tonic-gate 		case 'w':
37470Sstevel@tonic-gate 			authority.cred.unix_cred.passwd = strdup(optarg);
37480Sstevel@tonic-gate 			break;
37490Sstevel@tonic-gate 		case 'b':
37500Sstevel@tonic-gate 			inputbasedn = strdup(optarg);
37510Sstevel@tonic-gate 			break;
37520Sstevel@tonic-gate 		case 'a':
37530Sstevel@tonic-gate 			authmech = strdup(optarg);
37540Sstevel@tonic-gate 			break;
37550Sstevel@tonic-gate 
37560Sstevel@tonic-gate 		default:
37570Sstevel@tonic-gate 			usage(gettext("Invalid option"));
37580Sstevel@tonic-gate 		}
37590Sstevel@tonic-gate 	}
37600Sstevel@tonic-gate 
37610Sstevel@tonic-gate 
37620Sstevel@tonic-gate 	if (authority.cred.unix_cred.userID == NULL && op != OP_DUMP) {
37630Sstevel@tonic-gate 	    /* This is not an optional parameter. Exit */
37640Sstevel@tonic-gate 		(void) fprintf(stderr,
37650Sstevel@tonic-gate 			gettext("Distinguished Name to bind to directory"
37660Sstevel@tonic-gate 				" must be specified. use option -D.\n"));
37670Sstevel@tonic-gate 		exit(1);
37680Sstevel@tonic-gate 	}
37690Sstevel@tonic-gate 
37700Sstevel@tonic-gate 	if (authority.cred.unix_cred.passwd == NULL && op != OP_DUMP) {
37710Sstevel@tonic-gate 		/* If password is not specified, then prompt user for it. */
37720Sstevel@tonic-gate 		password = getpassphrase("Enter password:");
37730Sstevel@tonic-gate 		(void) strcpy(ps, password);
37740Sstevel@tonic-gate 		authority.cred.unix_cred.passwd = strdup(ps);
37750Sstevel@tonic-gate 	}
37760Sstevel@tonic-gate 
37770Sstevel@tonic-gate 	if (authmech != NULL) {
37780Sstevel@tonic-gate 		if (strcasecmp(authmech, "simple") == 0) {
37790Sstevel@tonic-gate 		authority.auth.type = NS_LDAP_AUTH_SIMPLE;
37800Sstevel@tonic-gate 		authority.auth.tlstype = NS_LDAP_TLS_NONE;
37810Sstevel@tonic-gate 		authority.auth.saslmech = NS_LDAP_SASL_NONE;
37820Sstevel@tonic-gate 		authority.auth.saslopt = NS_LDAP_SASLOPT_NONE;
37830Sstevel@tonic-gate 		supportedauth = 1;
37840Sstevel@tonic-gate 		}
37850Sstevel@tonic-gate 		if (strcasecmp(authmech, "sasl/CRAM-MD5") == 0) {
37860Sstevel@tonic-gate 		authority.auth.type = NS_LDAP_AUTH_SASL;
37870Sstevel@tonic-gate 		authority.auth.tlstype = NS_LDAP_TLS_SASL;
37880Sstevel@tonic-gate 		authority.auth.saslmech = NS_LDAP_SASL_CRAM_MD5;
37890Sstevel@tonic-gate 		authority.auth.saslopt = NS_LDAP_SASLOPT_NONE;
37900Sstevel@tonic-gate 		supportedauth = 1;
37910Sstevel@tonic-gate 		}
37920Sstevel@tonic-gate 		if (strcasecmp(authmech, "sasl/DIGEST-MD5") == 0) {
37930Sstevel@tonic-gate 		authority.auth.type = NS_LDAP_AUTH_SASL;
37940Sstevel@tonic-gate 		authority.auth.tlstype = NS_LDAP_TLS_SASL;
37950Sstevel@tonic-gate 		authority.auth.saslmech = NS_LDAP_SASL_DIGEST_MD5;
37960Sstevel@tonic-gate 		authority.auth.saslopt = NS_LDAP_SASLOPT_NONE;
37970Sstevel@tonic-gate 		supportedauth = 1;
37980Sstevel@tonic-gate 		}
37990Sstevel@tonic-gate 		if (strcasecmp(authmech, "tls:simple") == 0) {
38000Sstevel@tonic-gate 		authority.auth.type = NS_LDAP_AUTH_TLS;
38010Sstevel@tonic-gate 		authority.auth.tlstype = NS_LDAP_TLS_SIMPLE;
38020Sstevel@tonic-gate 		authority.auth.saslmech = NS_LDAP_SASL_NONE;
38030Sstevel@tonic-gate 		authority.auth.saslopt = NS_LDAP_SASLOPT_NONE;
38040Sstevel@tonic-gate 		supportedauth = 1;
38050Sstevel@tonic-gate 		}
38060Sstevel@tonic-gate 		if (strcasecmp(authmech, "tls:sasl/CRAM-MD5") == 0) {
38070Sstevel@tonic-gate 		authority.auth.type = NS_LDAP_AUTH_TLS;
38080Sstevel@tonic-gate 		authority.auth.tlstype = NS_LDAP_TLS_SASL;
38090Sstevel@tonic-gate 		authority.auth.saslmech = NS_LDAP_SASL_CRAM_MD5;
38100Sstevel@tonic-gate 		authority.auth.saslopt = NS_LDAP_SASLOPT_NONE;
38110Sstevel@tonic-gate 		supportedauth = 1;
38120Sstevel@tonic-gate 		}
38130Sstevel@tonic-gate 		if (strcasecmp(authmech, "tls:sasl/DIGEST-MD5") == 0) {
38140Sstevel@tonic-gate 		authority.auth.type = NS_LDAP_AUTH_TLS;
38150Sstevel@tonic-gate 		authority.auth.tlstype = NS_LDAP_TLS_SASL;
38160Sstevel@tonic-gate 		authority.auth.saslmech = NS_LDAP_SASL_DIGEST_MD5;
38170Sstevel@tonic-gate 		authority.auth.saslopt = NS_LDAP_SASLOPT_NONE;
38180Sstevel@tonic-gate 		supportedauth = 1;
38190Sstevel@tonic-gate 		}
38200Sstevel@tonic-gate 		if (!supportedauth) {
38210Sstevel@tonic-gate 			(void) fprintf(stderr,
38220Sstevel@tonic-gate 			gettext("Invalid authentication method specified"));
38230Sstevel@tonic-gate 			exit(1);
38240Sstevel@tonic-gate 		}
38250Sstevel@tonic-gate 	}
38260Sstevel@tonic-gate 
38270Sstevel@tonic-gate 	if (authmech == NULL) {
38280Sstevel@tonic-gate 		ldaprc = __ns_ldap_getParam(NS_LDAP_AUTH_P, (void ***)&authpp,
38290Sstevel@tonic-gate 			&errorp);
38300Sstevel@tonic-gate 		if (ldaprc != NS_LDAP_SUCCESS ||
38310Sstevel@tonic-gate 		    (authpp == NULL && op != OP_DUMP)) {
38320Sstevel@tonic-gate 			(void) fprintf(stderr,
38330Sstevel@tonic-gate 			    gettext("No legal authentication method "
38340Sstevel@tonic-gate 			    "configured.\n"));
38350Sstevel@tonic-gate 			(void) fprintf(stderr,
38360Sstevel@tonic-gate 			    gettext("Provide a legal authentication method "
38370Sstevel@tonic-gate 			    "using -a option\n"));
38380Sstevel@tonic-gate 			exit(1);
38390Sstevel@tonic-gate 		}
38400Sstevel@tonic-gate 
38410Sstevel@tonic-gate 		/* Use the first authentication method which is not none */
38420Sstevel@tonic-gate 		for (app = authpp; *app; app++) {
38430Sstevel@tonic-gate 			authp = *app;
38440Sstevel@tonic-gate 			if (authp->type != NS_LDAP_AUTH_NONE) {
38450Sstevel@tonic-gate 				authstried++;
38460Sstevel@tonic-gate 				authority.auth.type = authp->type;
38470Sstevel@tonic-gate 				authority.auth.tlstype = authp->tlstype;
38480Sstevel@tonic-gate 				authority.auth.saslmech = authp->saslmech;
38490Sstevel@tonic-gate 				authority.auth.saslopt = authp->saslopt;
38500Sstevel@tonic-gate 				break;
38510Sstevel@tonic-gate 			}
38520Sstevel@tonic-gate 		}
38530Sstevel@tonic-gate 		if (authstried == 0 && op != OP_DUMP) {
38540Sstevel@tonic-gate 			(void) fprintf(stderr,
38550Sstevel@tonic-gate 			gettext("No legal authentication method configured.\n"
38560Sstevel@tonic-gate 				"Provide a legal authentication method using "
38570Sstevel@tonic-gate 				"-a option"));
38580Sstevel@tonic-gate 			exit(1);
38590Sstevel@tonic-gate 		}
38600Sstevel@tonic-gate 	}
38610Sstevel@tonic-gate 
38620Sstevel@tonic-gate 	ttype = argv[optind++];
38630Sstevel@tonic-gate 
38640Sstevel@tonic-gate 	if (ttype == NULL) {
38650Sstevel@tonic-gate 		usage(gettext("No database type specified"));
38660Sstevel@tonic-gate 		exit(1);
38670Sstevel@tonic-gate 	}
38680Sstevel@tonic-gate 
38690Sstevel@tonic-gate 	if (strncasecmp(ttype, "automount", 9) == 0) {
38700Sstevel@tonic-gate 		(void) fprintf(stderr,
38710Sstevel@tonic-gate 		    gettext("automount is not a valid service for ldapaddent.\n"
38720Sstevel@tonic-gate 		    "Please use auto_*.\n"
38730Sstevel@tonic-gate 		    "e.g.  auto_home, auto_ws etc.\n "));
38740Sstevel@tonic-gate 		exit(1);
38750Sstevel@tonic-gate 	}
38760Sstevel@tonic-gate 
38770Sstevel@tonic-gate 	for (tt = ttypelist; tt->ttype; tt++) {
38780Sstevel@tonic-gate 		if (strcmp(tt->ttype, ttype) == 0)
38790Sstevel@tonic-gate 			break;
38800Sstevel@tonic-gate 		if (strcmp(tt->ttype, NS_LDAP_TYPE_AUTOMOUNT) == 0 &&
38810Sstevel@tonic-gate 		    strncmp(ttype, NS_LDAP_TYPE_AUTOMOUNT,
38820Sstevel@tonic-gate 		    sizeof (NS_LDAP_TYPE_AUTOMOUNT) - 1) == 0)
38830Sstevel@tonic-gate 			break;
38840Sstevel@tonic-gate 	}
38850Sstevel@tonic-gate 
38860Sstevel@tonic-gate 	if (tt->ttype == 0) {
38870Sstevel@tonic-gate 		(void) fprintf(stderr,
38880Sstevel@tonic-gate 		    gettext("database %s not supported;"
38890Sstevel@tonic-gate 		    " supported databases are:\n"), ttype);
38900Sstevel@tonic-gate 		for (tt = ttypelist; tt->ttype; tt++)
38910Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("\t%s\n"), tt->ttype);
38920Sstevel@tonic-gate 		exit(1);
38930Sstevel@tonic-gate 	}
38940Sstevel@tonic-gate 
38950Sstevel@tonic-gate 	if (flags & F_VERBOSE)
38960Sstevel@tonic-gate 		(void) fprintf(stdout, gettext("SERVICE = %s\n"), tt->ttype);
38970Sstevel@tonic-gate 
38980Sstevel@tonic-gate 	databasetype = ttype;
38990Sstevel@tonic-gate 
39000Sstevel@tonic-gate 	if (strcmp(tt->ttype, NS_LDAP_TYPE_AUTOMOUNT) == 0) {
39010Sstevel@tonic-gate 		paramVal = NULL;
39020Sstevel@tonic-gate 		errorp = NULL;
39030Sstevel@tonic-gate 		rc = __ns_ldap_getParam(NS_LDAP_FILE_VERSION_P, &paramVal,
39040Sstevel@tonic-gate 			&errorp);
39050Sstevel@tonic-gate 		if (paramVal && *paramVal &&
39060Sstevel@tonic-gate 			strcasecmp(*paramVal, NS_LDAP_VERSION_1) == 0)
39070Sstevel@tonic-gate 			version1 = 1;
39080Sstevel@tonic-gate 		if (paramVal)
39090Sstevel@tonic-gate 			(void) __ns_ldap_freeParam(&paramVal);
39100Sstevel@tonic-gate 		if (errorp)
39110Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&errorp);
39120Sstevel@tonic-gate 	}
39130Sstevel@tonic-gate 
39140Sstevel@tonic-gate 	/* Check if the container exists in first place */
39150Sstevel@tonic-gate 	(void) strcpy(&filter[0], "(objectclass=*)");
39160Sstevel@tonic-gate 
39170Sstevel@tonic-gate 	rc = __ns_ldap_list(databasetype, filter, NULL, (const char **)NULL,
39180Sstevel@tonic-gate 	    NULL, NS_LDAP_SCOPE_BASE, &resultp, &errorp, NULL, NULL);
39190Sstevel@tonic-gate 
39200Sstevel@tonic-gate 	/* create a container for auto_* if it does not exist already */
39210Sstevel@tonic-gate 	if ((rc == NS_LDAP_NOTFOUND) && (op == OP_ADD) &&
39220Sstevel@tonic-gate 	    (strcmp(tt->ttype, NS_LDAP_TYPE_AUTOMOUNT) == 0)) {
39230Sstevel@tonic-gate 		static	char *oclist[] = {NULL, "top", NULL};
39240Sstevel@tonic-gate 		if (version1)
39250Sstevel@tonic-gate 			oclist[0] = "nisMap";
39260Sstevel@tonic-gate 		else
39270Sstevel@tonic-gate 			oclist[0] = "automountMap";
39280Sstevel@tonic-gate 		e = __s_mk_entry(oclist, 3);
39290Sstevel@tonic-gate 		if (e == NULL) {
39300Sstevel@tonic-gate 			(void) fprintf(stderr,
39310Sstevel@tonic-gate 			    gettext("internal memory allocation error.\n"));
39320Sstevel@tonic-gate 			exit(1);
39330Sstevel@tonic-gate 		}
39340Sstevel@tonic-gate 		if (__s_add_attr(e,
39350Sstevel@tonic-gate 		    version1 ? "nisMapName" : "automountMapName",
39360Sstevel@tonic-gate 		    databasetype) != NS_LDAP_SUCCESS) {
39370Sstevel@tonic-gate 			(void) fprintf(stderr,
39380Sstevel@tonic-gate 			    gettext("internal memory allocation error.\n"));
39390Sstevel@tonic-gate 			ldap_freeEntry(e);
39400Sstevel@tonic-gate 			exit(1);
39410Sstevel@tonic-gate 		}
39420Sstevel@tonic-gate 
39430Sstevel@tonic-gate 		if (inputbasedn == NULL) {
39440Sstevel@tonic-gate 			if (get_basedn(databasetype, &inputbasedn) !=
39450Sstevel@tonic-gate 			    NS_LDAP_SUCCESS) {
39460Sstevel@tonic-gate 				(void) fprintf(stderr,
39470Sstevel@tonic-gate 				    gettext("Could not obtain basedn\n"));
39480Sstevel@tonic-gate 				ldap_freeEntry(e);
39490Sstevel@tonic-gate 				exit(1);
39500Sstevel@tonic-gate 			}
39510Sstevel@tonic-gate 		}
39520Sstevel@tonic-gate 		if (__ns_ldap_addEntry(databasetype, inputbasedn, e,
39530Sstevel@tonic-gate 		    &authority, flag, &errorp) != NS_LDAP_SUCCESS) {
39540Sstevel@tonic-gate 			(void) fprintf(stderr,
39550Sstevel@tonic-gate 			    gettext("Could not create container for %s\n"),
39560Sstevel@tonic-gate 			    databasetype);
39570Sstevel@tonic-gate 			ldap_freeEntry(e);
39580Sstevel@tonic-gate 		}
39590Sstevel@tonic-gate 	} else if (strcmp(databasetype, "publickey") != 0) {
39600Sstevel@tonic-gate 		if (rc == NS_LDAP_NOTFOUND) {
39610Sstevel@tonic-gate 			(void) fprintf(stderr,
39620Sstevel@tonic-gate 			    gettext("Container %s does not exist\n"),
39630Sstevel@tonic-gate 			    databasetype);
39640Sstevel@tonic-gate 			exit(1);
39650Sstevel@tonic-gate 		}
39660Sstevel@tonic-gate 	}
39670Sstevel@tonic-gate 
39680Sstevel@tonic-gate 	if (op == OP_DUMP) {
39690Sstevel@tonic-gate 		if (strcmp(databasetype, "publickey") == 0) {
39700Sstevel@tonic-gate 			dumptable("hosts");
39710Sstevel@tonic-gate 			dumptable("passwd");
39720Sstevel@tonic-gate 		} else {
39730Sstevel@tonic-gate 			dumptable(databasetype);
39740Sstevel@tonic-gate 		}
39750Sstevel@tonic-gate 		exit(exit_val);
39760Sstevel@tonic-gate 	}
39770Sstevel@tonic-gate 
39780Sstevel@tonic-gate 	if (etcfile) {
39790Sstevel@tonic-gate 		if ((etcf = fopen(etcfile, "r")) == 0) {
39800Sstevel@tonic-gate 			(void) fprintf(stderr,
39810Sstevel@tonic-gate 			    gettext("can't open file %s\n"), etcfile);
39820Sstevel@tonic-gate 			exit(1);
39830Sstevel@tonic-gate 		}
39840Sstevel@tonic-gate 	} else {
39850Sstevel@tonic-gate 		etcfile = "stdin";
39860Sstevel@tonic-gate 		etcf = stdin;
39870Sstevel@tonic-gate 	}
39880Sstevel@tonic-gate 
39890Sstevel@tonic-gate 	if (op == OP_ADD) {
39900Sstevel@tonic-gate 		(void) addfile();
39910Sstevel@tonic-gate 		(void) fprintf(stdout, gettext("%d entries added\n"), nent_add);
39920Sstevel@tonic-gate 	}
39930Sstevel@tonic-gate 
3994*839Smj162486 	/* exit() -> return for make lint */
3995*839Smj162486 	return (exit_val);
39960Sstevel@tonic-gate }
39970Sstevel@tonic-gate 
39980Sstevel@tonic-gate 
39990Sstevel@tonic-gate /*
40000Sstevel@tonic-gate  * This is called when service == auto_*.
40010Sstevel@tonic-gate  * It calls __ns_ldap_getSearchDescriptors
40020Sstevel@tonic-gate  * to generate the dn from SSD's base dn.
40030Sstevel@tonic-gate  * If there is no SSD available,
40040Sstevel@tonic-gate  * default base dn will be used
40050Sstevel@tonic-gate  * Only the first baseDN in the SSD is used
40060Sstevel@tonic-gate  */
40070Sstevel@tonic-gate 
40080Sstevel@tonic-gate static int get_basedn(char *service, char **basedn) {
40090Sstevel@tonic-gate 	int rc = NS_LDAP_SUCCESS;
40100Sstevel@tonic-gate 	char *dn = NULL;
40110Sstevel@tonic-gate 	ns_ldap_search_desc_t **desc = NULL;
40120Sstevel@tonic-gate 	ns_ldap_error_t *errp = NULL;
40130Sstevel@tonic-gate 	void		**paramVal = NULL;
40140Sstevel@tonic-gate 	int		prepend_automountmapname = FALSE;
40150Sstevel@tonic-gate 
40160Sstevel@tonic-gate 	/*
40170Sstevel@tonic-gate 	 * Get auto_* SSD first
40180Sstevel@tonic-gate 	 */
40190Sstevel@tonic-gate 
40200Sstevel@tonic-gate 	if ((rc = __ns_ldap_getSearchDescriptors(
40210Sstevel@tonic-gate 			(const char *) service,
40220Sstevel@tonic-gate 			&desc, &errp))  == NS_LDAP_SUCCESS &&
40230Sstevel@tonic-gate 		desc != NULL) {
40240Sstevel@tonic-gate 
40250Sstevel@tonic-gate 		if (desc[0] != NULL && desc[0]->basedn != NULL) {
40260Sstevel@tonic-gate 			dn = strdup(desc[0]->basedn);
40270Sstevel@tonic-gate 			if (dn == NULL) {
40280Sstevel@tonic-gate 				(void) __ns_ldap_freeSearchDescriptors
40290Sstevel@tonic-gate 						(&desc);
40300Sstevel@tonic-gate 				return (NS_LDAP_MEMORY);
40310Sstevel@tonic-gate 			}
40320Sstevel@tonic-gate 		}
40330Sstevel@tonic-gate 	}
40340Sstevel@tonic-gate 
40350Sstevel@tonic-gate 	/* clean up */
40360Sstevel@tonic-gate 	if (desc) (void) __ns_ldap_freeSearchDescriptors(&desc);
40370Sstevel@tonic-gate 	if (errp) (void) __ns_ldap_freeError(&errp);
40380Sstevel@tonic-gate 
40390Sstevel@tonic-gate 	/*
40400Sstevel@tonic-gate 	 * If no dn is duplicated from auto_* SSD, try automount SSD
40410Sstevel@tonic-gate 	 */
40420Sstevel@tonic-gate 	if (dn == NULL) {
40430Sstevel@tonic-gate 		if ((rc = __ns_ldap_getSearchDescriptors(
40440Sstevel@tonic-gate 				"automount", &desc, &errp))
40450Sstevel@tonic-gate 				== NS_LDAP_SUCCESS && desc != NULL) {
40460Sstevel@tonic-gate 
40470Sstevel@tonic-gate 			if (desc[0] != NULL && desc[0]->basedn != NULL) {
40480Sstevel@tonic-gate 				dn = strdup(desc[0]->basedn);
40490Sstevel@tonic-gate 				if (dn == NULL) {
40500Sstevel@tonic-gate 					(void) __ns_ldap_freeSearchDescriptors
40510Sstevel@tonic-gate 							(&desc);
40520Sstevel@tonic-gate 					return (NS_LDAP_MEMORY);
40530Sstevel@tonic-gate 				}
40540Sstevel@tonic-gate 				prepend_automountmapname = TRUE;
40550Sstevel@tonic-gate 			}
40560Sstevel@tonic-gate 		}
40570Sstevel@tonic-gate 		/* clean up */
40580Sstevel@tonic-gate 		if (desc) (void) __ns_ldap_freeSearchDescriptors(&desc);
40590Sstevel@tonic-gate 		if (errp) (void) __ns_ldap_freeError(&errp);
40600Sstevel@tonic-gate 	}
40610Sstevel@tonic-gate 
40620Sstevel@tonic-gate 	/*
40630Sstevel@tonic-gate 	 * If no dn is duplicated from auto_* or automount SSD,
40640Sstevel@tonic-gate 	 * use default DN
40650Sstevel@tonic-gate 	 */
40660Sstevel@tonic-gate 
40670Sstevel@tonic-gate 	if (dn == NULL) {
40680Sstevel@tonic-gate 		if ((rc = __ns_ldap_getParam(NS_LDAP_SEARCH_BASEDN_P,
40690Sstevel@tonic-gate 			&paramVal, &errp)) == NS_LDAP_SUCCESS) {
40700Sstevel@tonic-gate 			dn = strdup((char *)paramVal[0]);
40710Sstevel@tonic-gate 			if (dn == NULL) {
40720Sstevel@tonic-gate 				(void) __ns_ldap_freeParam(&paramVal);
40730Sstevel@tonic-gate 				return (NS_LDAP_MEMORY);
40740Sstevel@tonic-gate 			}
40750Sstevel@tonic-gate 			prepend_automountmapname = TRUE;
40760Sstevel@tonic-gate 		}
40770Sstevel@tonic-gate 		if (paramVal) (void) __ns_ldap_freeParam(&paramVal);
40780Sstevel@tonic-gate 		if (errp) (void) __ns_ldap_freeError(&errp);
40790Sstevel@tonic-gate 	}
40800Sstevel@tonic-gate 
40810Sstevel@tonic-gate 
40820Sstevel@tonic-gate 	if (dn == NULL) {
40830Sstevel@tonic-gate 		return (NS_LDAP_OP_FAILED);
40840Sstevel@tonic-gate 	} else {
40850Sstevel@tonic-gate 		/*
40860Sstevel@tonic-gate 		 * If dn is duplicated from
40870Sstevel@tonic-gate 		 * automount SSD basedn or
40880Sstevel@tonic-gate 		 * default base dn
40890Sstevel@tonic-gate 		 * then prepend automountMapName=auto_xxx
40900Sstevel@tonic-gate 		 */
40910Sstevel@tonic-gate 		if (prepend_automountmapname)
40920Sstevel@tonic-gate 			rc = __s_api_prepend_automountmapname_to_dn(
40930Sstevel@tonic-gate 				service, &dn, &errp);
40940Sstevel@tonic-gate 
40950Sstevel@tonic-gate 		if (rc != NS_LDAP_SUCCESS) {
40960Sstevel@tonic-gate 			(void) __ns_ldap_freeError(&errp);
40970Sstevel@tonic-gate 			free(dn);
40980Sstevel@tonic-gate 			return (rc);
40990Sstevel@tonic-gate 		}
41000Sstevel@tonic-gate 
41010Sstevel@tonic-gate 		*basedn = dn;
41020Sstevel@tonic-gate 
41030Sstevel@tonic-gate 		return (NS_LDAP_SUCCESS);
41040Sstevel@tonic-gate 	}
41050Sstevel@tonic-gate }
4106