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