Home | History | Annotate | Line # | Download | only in netgroup_mkdb
netgroup_mkdb.c revision 1.8
      1  1.8     lukem /*	$NetBSD: netgroup_mkdb.c,v 1.8 1997/10/17 11:49:05 lukem Exp $	*/
      2  1.5  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1994 Christos Zoulas
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     16  1.1  christos  *    must display the following acknowledgement:
     17  1.1  christos  *	This product includes software developed by Christos Zoulas.
     18  1.1  christos  * 4. The name of the author may not be used to endorse or promote products
     19  1.1  christos  *    derived from this software without specific prior written permission.
     20  1.1  christos  *
     21  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  1.1  christos  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  1.1  christos  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  1.1  christos  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  christos  * SUCH DAMAGE.
     32  1.1  christos  */
     33  1.8     lukem #include <sys/cdefs.h>
     34  1.1  christos #ifndef lint
     35  1.8     lukem __RCSID("$NetBSD: netgroup_mkdb.c,v 1.8 1997/10/17 11:49:05 lukem Exp $");
     36  1.1  christos #endif
     37  1.1  christos 
     38  1.1  christos #include <sys/types.h>
     39  1.2  christos #include <sys/param.h>
     40  1.1  christos #include <sys/stat.h>
     41  1.1  christos #include <stdlib.h>
     42  1.1  christos #include <stddef.h>
     43  1.1  christos #include <unistd.h>
     44  1.1  christos #include <fcntl.h>
     45  1.1  christos #include <db.h>
     46  1.1  christos #include <err.h>
     47  1.1  christos #include <errno.h>
     48  1.1  christos #include <stdio.h>
     49  1.1  christos #include <string.h>
     50  1.6     lukem #include <stringlist.h>
     51  1.4  christos #define _NETGROUP_PRIVATE
     52  1.1  christos #include <netgroup.h>
     53  1.1  christos #include <assert.h>
     54  1.1  christos 
     55  1.1  christos #include "str.h"
     56  1.1  christos #include "util.h"
     57  1.1  christos 
     58  1.4  christos #define DEBUG_NG
     59  1.4  christos 
     60  1.1  christos #define NEW(a)	(a *) emalloc(sizeof(a))
     61  1.1  christos 
     62  1.1  christos struct nentry {
     63  1.1  christos 	int             n_type;
     64  1.1  christos 	size_t          n_size;	/* Buffer size required for printing */
     65  1.1  christos 	union {
     66  1.1  christos 		char            *_name;
     67  1.1  christos 		struct netgroup *_group;
     68  1.1  christos 	} _n;
     69  1.1  christos #define n_name	_n._name
     70  1.1  christos #define n_group _n._group
     71  1.1  christos 	struct nentry  *n_next;
     72  1.1  christos };
     73  1.1  christos 
     74  1.1  christos 
     75  1.8     lukem static	void	 cleanup __P((void));
     76  1.8     lukem 	int	 main __P((int, char **));
     77  1.8     lukem static	DB      *ng_insert __P((DB *, const char *));
     78  1.8     lukem static	void	 ng_reventry __P((DB *, DB *, struct nentry *, char *,
     79  1.6     lukem 				  size_t, StringList *));
     80  1.1  christos 
     81  1.8     lukem static	void	 ng_print __P((struct nentry *, struct string *));
     82  1.8     lukem static	void	 ng_rprint __P((DB *, struct string *));
     83  1.8     lukem static	DB	*ng_reverse __P((DB *, size_t));
     84  1.8     lukem static	DB	*ng_load __P((const char *));
     85  1.8     lukem static	void	 ng_write __P((DB *, DB *, int));
     86  1.8     lukem static	void	 ng_rwrite __P((DB *, DB *, int));
     87  1.8     lukem static	void	 usage __P((void));
     88  1.1  christos 
     89  1.1  christos #ifdef DEBUG_NG
     90  1.8     lukem static	int 	 debug = 0;
     91  1.8     lukem static	void	 ng_dump __P((DB *));
     92  1.8     lukem static	void	 ng_rdump __P((DB *));
     93  1.1  christos #endif /* DEBUG_NG */
     94  1.1  christos 
     95  1.4  christos 
     96  1.3  christos static const char ng_empty[] = "";
     97  1.3  christos #define NG_EMPTY(a)	((a) ? (a) : ng_empty)
     98  1.3  christos 
     99  1.1  christos static char    *dbname = _PATH_NETGROUP_DB;
    100  1.1  christos 
    101  1.1  christos int
    102  1.1  christos main(argc, argv)
    103  1.2  christos 	int		  argc;
    104  1.2  christos 	char		**argv;
    105  1.1  christos {
    106  1.2  christos 	DB		 *db, *ndb, *hdb, *udb;
    107  1.2  christos 	int               ch;
    108  1.2  christos 	char		  buf[MAXPATHLEN];
    109  1.2  christos 	char		 *fname = _PATH_NETGROUP;
    110  1.2  christos 
    111  1.1  christos 
    112  1.8     lukem 	while ((ch = getopt(argc, argv, "do:")) != -1)
    113  1.1  christos 		switch (ch) {
    114  1.4  christos #ifdef DEBUG_NG
    115  1.4  christos 		case 'd':
    116  1.4  christos 			debug++;
    117  1.4  christos 			break;
    118  1.4  christos #endif
    119  1.1  christos 		case 'o':
    120  1.1  christos 			dbname = optarg;
    121  1.1  christos 			break;
    122  1.1  christos 
    123  1.1  christos 		case '?':
    124  1.1  christos 		default:
    125  1.1  christos 			usage();
    126  1.1  christos 		}
    127  1.1  christos 
    128  1.1  christos 	argc -= optind;
    129  1.1  christos 	argv += optind;
    130  1.1  christos 
    131  1.2  christos 	if (argc == 1)
    132  1.2  christos 		fname = *argv;
    133  1.2  christos 	else if (argc > 1)
    134  1.1  christos 		usage();
    135  1.1  christos 
    136  1.2  christos 	if (atexit(cleanup))
    137  1.2  christos 		err(1, "Cannot install exit handler");
    138  1.2  christos 
    139  1.1  christos 	/* Read and parse the netgroup file */
    140  1.2  christos 	ndb = ng_load(fname);
    141  1.1  christos #ifdef DEBUG_NG
    142  1.4  christos 	if (debug) {
    143  1.4  christos 		(void) fprintf(stderr, "#### Database\n");
    144  1.4  christos 		ng_dump(ndb);
    145  1.4  christos 	}
    146  1.1  christos #endif
    147  1.1  christos 
    148  1.1  christos 	/* Reverse the database by host */
    149  1.1  christos 	hdb = ng_reverse(ndb, offsetof(struct netgroup, ng_host));
    150  1.1  christos #ifdef DEBUG_NG
    151  1.4  christos 	if (debug) {
    152  1.4  christos 		(void) fprintf(stderr, "#### Reverse by host\n");
    153  1.4  christos 		ng_rdump(hdb);
    154  1.4  christos 	}
    155  1.1  christos #endif
    156  1.1  christos 
    157  1.1  christos 	/* Reverse the database by user */
    158  1.1  christos 	udb = ng_reverse(ndb, offsetof(struct netgroup, ng_user));
    159  1.1  christos #ifdef DEBUG_NG
    160  1.4  christos 	if (debug) {
    161  1.4  christos 		(void) fprintf(stderr, "#### Reverse by user\n");
    162  1.4  christos 		ng_rdump(udb);
    163  1.4  christos 	}
    164  1.1  christos #endif
    165  1.1  christos 
    166  1.2  christos 	(void) snprintf(buf, sizeof(buf), "%s.tmp", dbname);
    167  1.2  christos 
    168  1.2  christos 	db = dbopen(buf, O_RDWR | O_CREAT | O_EXCL,
    169  1.1  christos 		    (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, NULL);
    170  1.1  christos 	if (!db)
    171  1.2  christos 		err(1, buf);
    172  1.1  christos 
    173  1.1  christos 	ng_write(db, ndb, _NG_KEYBYNAME);
    174  1.1  christos 	ng_rwrite(db, udb, _NG_KEYBYUSER);
    175  1.1  christos 	ng_rwrite(db, hdb, _NG_KEYBYHOST);
    176  1.2  christos 
    177  1.2  christos 	if ((db->close)(db))
    178  1.2  christos 		err(1, "Error closing database");
    179  1.2  christos 
    180  1.2  christos 	if (rename(buf, dbname) == -1)
    181  1.2  christos 		err(1, "Cannot rename `%s' to `%s'", buf, dbname);
    182  1.2  christos 
    183  1.1  christos 	return 0;
    184  1.1  christos }
    185  1.1  christos 
    186  1.1  christos 
    187  1.1  christos /*
    188  1.2  christos  * cleanup(): Remove temporary files upon exit
    189  1.2  christos  */
    190  1.2  christos static void
    191  1.2  christos cleanup()
    192  1.2  christos {
    193  1.2  christos 	char buf[MAXPATHLEN];
    194  1.2  christos 	(void) snprintf(buf, sizeof(buf), "%s.tmp", dbname);
    195  1.2  christos 	(void) unlink(buf);
    196  1.2  christos }
    197  1.2  christos 
    198  1.2  christos 
    199  1.2  christos 
    200  1.2  christos /*
    201  1.1  christos  * ng_load(): Load the netgroup database from a file
    202  1.1  christos  */
    203  1.2  christos static DB *
    204  1.1  christos ng_load(fname)
    205  1.1  christos 	const char     *fname;
    206  1.1  christos {
    207  1.1  christos 	FILE           *fp;
    208  1.1  christos 	DB             *db;
    209  1.1  christos 	char           *buf;
    210  1.1  christos 	size_t          size;
    211  1.1  christos 	struct nentry  *tail, *head, *e;
    212  1.1  christos 	char           *p, *name;
    213  1.1  christos 	struct netgroup *ng;
    214  1.1  christos 	DBT             data, key;
    215  1.1  christos 
    216  1.1  christos 	/* Open the netgroup file */
    217  1.1  christos 	if ((fp = fopen(fname, "r")) == NULL)
    218  1.1  christos 		err(1, fname);
    219  1.1  christos 
    220  1.1  christos 	db = dbopen(NULL, O_RDWR | O_CREAT | O_EXCL, 0, DB_HASH, NULL);
    221  1.1  christos 
    222  1.1  christos 	if (db == NULL)
    223  1.1  christos 		err(1, "dbopen");
    224  1.1  christos 
    225  1.1  christos 	while ((buf = getline(fp, &size)) != NULL) {
    226  1.1  christos 		tail = head = NULL;
    227  1.1  christos 		p = buf;
    228  1.1  christos 
    229  1.1  christos 		while (p != NULL) {
    230  1.1  christos 			switch (_ng_parse(&p, &name, &ng)) {
    231  1.1  christos 			case _NG_NONE:
    232  1.1  christos 				/* done with this one */
    233  1.1  christos 				p = NULL;
    234  1.1  christos 				free(buf);
    235  1.1  christos 				if (head == NULL)
    236  1.1  christos 					break;
    237  1.1  christos 
    238  1.1  christos 				key.data = (u_char *) head->n_name;
    239  1.1  christos 				key.size = strlen(head->n_name) + 1;
    240  1.1  christos 				data.data = (u_char *) & head;
    241  1.1  christos 				data.size = sizeof(head);
    242  1.1  christos 				switch ((db->put)(db, &key, &data,
    243  1.1  christos 						  R_NOOVERWRITE)) {
    244  1.1  christos 				case 0:
    245  1.1  christos 					break;
    246  1.1  christos 
    247  1.1  christos 				case 1:
    248  1.1  christos 					warnx("Duplicate entry netgroup `%s'\n",
    249  1.1  christos 					      head->n_name);
    250  1.1  christos 					break;
    251  1.1  christos 
    252  1.1  christos 				case -1:
    253  1.1  christos 					err(1, "put");
    254  1.1  christos 					break;
    255  1.1  christos 
    256  1.1  christos 				default:
    257  1.1  christos 					abort();
    258  1.1  christos 					break;
    259  1.1  christos 				}
    260  1.1  christos 				break;
    261  1.1  christos 
    262  1.1  christos 			case _NG_NAME:
    263  1.1  christos 				e = NEW(struct nentry);
    264  1.1  christos 				e->n_type = _NG_NAME;
    265  1.1  christos 				e->n_name = name;
    266  1.1  christos 				e->n_next = NULL;
    267  1.1  christos 				e->n_size = size;
    268  1.1  christos 				if (tail == NULL)
    269  1.1  christos 					head = tail = e;
    270  1.1  christos 				else {
    271  1.1  christos 					tail->n_next = e;
    272  1.1  christos 					tail = e;
    273  1.1  christos 				}
    274  1.1  christos 				break;
    275  1.1  christos 
    276  1.1  christos 			case _NG_GROUP:
    277  1.4  christos 				if (tail == NULL) {
    278  1.4  christos 					char fmt[BUFSIZ];
    279  1.4  christos 					_ng_print(fmt, sizeof(fmt), ng);
    280  1.4  christos 					errx(1, "no netgroup key for %s", fmt);
    281  1.4  christos 				}
    282  1.1  christos 				else {
    283  1.1  christos 					e = NEW(struct nentry);
    284  1.1  christos 					e->n_type = _NG_GROUP;
    285  1.1  christos 					e->n_group = ng;
    286  1.1  christos 					e->n_next = NULL;
    287  1.1  christos 					e->n_size = size;
    288  1.1  christos 					tail->n_next = e;
    289  1.1  christos 					tail = e;
    290  1.1  christos 				}
    291  1.1  christos 				break;
    292  1.1  christos 
    293  1.1  christos 			default:
    294  1.1  christos 				abort();
    295  1.1  christos 				break;
    296  1.1  christos 			}
    297  1.1  christos 		}
    298  1.1  christos 	}
    299  1.1  christos 	(void) fclose(fp);
    300  1.1  christos 	return db;
    301  1.1  christos }
    302  1.1  christos 
    303  1.1  christos 
    304  1.1  christos /*
    305  1.1  christos  * ng_insert(): Insert named key into the database, and return its associated
    306  1.1  christos  * string database
    307  1.1  christos  */
    308  1.1  christos static DB *
    309  1.1  christos ng_insert(db, name)
    310  1.1  christos 	DB             *db;
    311  1.1  christos 	const char     *name;
    312  1.1  christos {
    313  1.1  christos 	DB             *xdb = NULL;
    314  1.1  christos 	DBT             key, data;
    315  1.1  christos 
    316  1.1  christos 	key.data = (u_char *) name;
    317  1.1  christos 	key.size = strlen(name) + 1;
    318  1.1  christos 
    319  1.1  christos 	switch ((db->get)(db, &key, &data, 0)) {
    320  1.1  christos 	case 0:
    321  1.1  christos 		memcpy(&xdb, data.data, sizeof(xdb));
    322  1.1  christos 		break;
    323  1.1  christos 
    324  1.1  christos 	case 1:
    325  1.1  christos 		xdb = dbopen(NULL, O_RDWR | O_CREAT | O_EXCL, 0, DB_HASH, NULL);
    326  1.1  christos 		if (xdb == NULL)
    327  1.1  christos 			err(1, "dbopen");
    328  1.1  christos 
    329  1.1  christos 		data.data = (u_char *) & xdb;
    330  1.1  christos 		data.size = sizeof(xdb);
    331  1.1  christos 		switch ((db->put)(db, &key, &data, R_NOOVERWRITE)) {
    332  1.1  christos 		case 0:
    333  1.1  christos 			break;
    334  1.1  christos 
    335  1.1  christos 		case -1:
    336  1.1  christos 			err(1, "db put `%s'", name);
    337  1.1  christos 			break;
    338  1.1  christos 
    339  1.1  christos 		case 1:
    340  1.1  christos 		default:
    341  1.1  christos 			abort();
    342  1.1  christos 		}
    343  1.1  christos 		break;
    344  1.1  christos 
    345  1.1  christos 	case -1:
    346  1.1  christos 		err(1, "db get `%s'", name);
    347  1.1  christos 		break;
    348  1.1  christos 
    349  1.1  christos 	default:
    350  1.1  christos 		abort();
    351  1.1  christos 		break;
    352  1.1  christos 	}
    353  1.1  christos 
    354  1.1  christos 	return xdb;
    355  1.1  christos }
    356  1.1  christos 
    357  1.1  christos 
    358  1.1  christos /*
    359  1.1  christos  * ng_reventry(): Recursively add all the netgroups to the group entry.
    360  1.1  christos  */
    361  1.1  christos static void
    362  1.1  christos ng_reventry(db, udb, fe, name, s, ss)
    363  1.1  christos 	DB             *db, *udb;
    364  1.1  christos 	struct nentry  *fe;
    365  1.1  christos 	char           *name;
    366  1.1  christos 	size_t          s;
    367  1.6     lukem 	StringList	*ss;
    368  1.1  christos {
    369  1.1  christos 	DBT             key, data;
    370  1.1  christos 	struct nentry  *e;
    371  1.1  christos 	struct netgroup *ng;
    372  1.1  christos 	char           *p;
    373  1.1  christos 	DB             *xdb;
    374  1.1  christos 
    375  1.7  christos 	if (sl_find(ss, fe->n_name) != NULL) {
    376  1.1  christos 		warnx("Cycle in netgroup `%s'", name);
    377  1.1  christos 		return;
    378  1.1  christos 	}
    379  1.7  christos 	sl_add(ss, fe->n_name);
    380  1.1  christos 
    381  1.1  christos 	for (e = fe->n_next; e != NULL; e = e->n_next)
    382  1.1  christos 		switch (e->n_type) {
    383  1.1  christos 		case _NG_GROUP:
    384  1.1  christos 			ng = e->n_group;
    385  1.1  christos 			p = _ng_makekey(*((char **)(((char *) ng) + s)),
    386  1.1  christos 					ng->ng_domain, e->n_size);
    387  1.1  christos 			xdb = ng_insert(udb, p);
    388  1.1  christos 			key.data = (u_char *) name;
    389  1.1  christos 			key.size = strlen(name) + 1;
    390  1.1  christos 			data.data = NULL;
    391  1.1  christos 			data.size = 0;
    392  1.1  christos 			switch ((xdb->put)(xdb, &key, &data, R_NOOVERWRITE)) {
    393  1.1  christos 			case 0:
    394  1.1  christos 			case 1:
    395  1.1  christos 				break;
    396  1.1  christos 
    397  1.1  christos 			case -1:
    398  1.1  christos 				err(1, "db put `%s'", name);
    399  1.1  christos 				return;
    400  1.1  christos 
    401  1.1  christos 			default:
    402  1.1  christos 				abort();
    403  1.1  christos 				break;
    404  1.1  christos 			}
    405  1.1  christos 			free(p);
    406  1.1  christos 			break;
    407  1.1  christos 
    408  1.1  christos 		case _NG_NAME:
    409  1.1  christos 			key.data = (u_char *) e->n_name;
    410  1.1  christos 			key.size = strlen(e->n_name) + 1;
    411  1.1  christos 			switch ((db->get)(db, &key, &data, 0)) {
    412  1.7  christos 				struct nentry *rfe;
    413  1.1  christos 			case 0:
    414  1.7  christos 				(void) memcpy(&rfe, data.data, sizeof(rfe));
    415  1.7  christos 				ng_reventry(db, udb, rfe, name, s, ss);
    416  1.1  christos 				break;
    417  1.1  christos 
    418  1.1  christos 			case 1:
    419  1.1  christos 				break;
    420  1.1  christos 
    421  1.1  christos 			case -1:
    422  1.1  christos 				err(1, "db get `%s'", e->n_name);
    423  1.1  christos 				return;
    424  1.1  christos 
    425  1.1  christos 			default:
    426  1.1  christos 				abort();
    427  1.1  christos 				return;
    428  1.1  christos 			}
    429  1.1  christos 			break;
    430  1.1  christos 
    431  1.1  christos 		default:
    432  1.1  christos 			abort();
    433  1.1  christos 			break;
    434  1.1  christos 		}
    435  1.1  christos }
    436  1.1  christos 
    437  1.1  christos 
    438  1.1  christos /*
    439  1.1  christos  * ng_reverse(): Reverse the database
    440  1.1  christos  */
    441  1.1  christos static DB *
    442  1.1  christos ng_reverse(db, s)
    443  1.1  christos 	DB             *db;
    444  1.1  christos 	size_t          s;
    445  1.1  christos {
    446  1.1  christos 	int             pos;
    447  1.6     lukem 	StringList	*sl;
    448  1.1  christos 	DBT             key, data;
    449  1.1  christos 	struct nentry  *fe;
    450  1.1  christos 	DB             *udb = dbopen(NULL, O_RDWR | O_CREAT | O_EXCL, 0,
    451  1.1  christos 				     DB_HASH, NULL);
    452  1.1  christos 
    453  1.1  christos 	if (udb == NULL)
    454  1.1  christos 		err(1, "dbopen");
    455  1.1  christos 
    456  1.1  christos 	for (pos = R_FIRST;; pos = R_NEXT)
    457  1.1  christos 		switch ((db->seq)(db, &key, &data, pos)) {
    458  1.1  christos 		case 0:
    459  1.6     lukem 			sl = sl_init();
    460  1.1  christos 			memcpy(&fe, data.data, sizeof(fe));
    461  1.1  christos 			ng_reventry(db, udb, fe, (char *) key.data, s, sl);
    462  1.6     lukem 			sl_free(sl, 0);
    463  1.1  christos 			break;
    464  1.1  christos 
    465  1.1  christos 		case 1:
    466  1.1  christos 			return udb;
    467  1.1  christos 
    468  1.1  christos 		case -1:
    469  1.1  christos 			err(1, "seq");
    470  1.1  christos 			return udb;
    471  1.1  christos 		}
    472  1.1  christos 
    473  1.1  christos 	return udb;
    474  1.1  christos }
    475  1.1  christos 
    476  1.1  christos 
    477  1.1  christos /*
    478  1.1  christos  * ng_print(): Pretty print a netgroup entry
    479  1.1  christos  */
    480  1.1  christos static void
    481  1.1  christos ng_print(e, str)
    482  1.1  christos 	struct nentry  *e;
    483  1.1  christos 	struct string  *str;
    484  1.1  christos {
    485  1.1  christos 	char           *ptr = emalloc(e->n_size);
    486  1.4  christos 
    487  1.4  christos 	if (e->n_next == NULL) {
    488  1.4  christos 		str_append(str, "", ' ');
    489  1.4  christos 		return;
    490  1.4  christos 	}
    491  1.1  christos 
    492  1.1  christos 	for (e = e->n_next; e != NULL; e = e->n_next) {
    493  1.1  christos 		switch (e->n_type) {
    494  1.1  christos 		case _NG_NAME:
    495  1.1  christos 			(void) snprintf(ptr, e->n_size, "%s", e->n_name);
    496  1.1  christos 			break;
    497  1.1  christos 
    498  1.1  christos 		case _NG_GROUP:
    499  1.1  christos 			(void) snprintf(ptr, e->n_size, "(%s,%s,%s)",
    500  1.3  christos 					NG_EMPTY(e->n_group->ng_host),
    501  1.3  christos 					NG_EMPTY(e->n_group->ng_user),
    502  1.3  christos 					NG_EMPTY(e->n_group->ng_domain));
    503  1.1  christos 			break;
    504  1.1  christos 
    505  1.1  christos 		default:
    506  1.1  christos 			errx(1, "Internal error: Bad netgroup type\n");
    507  1.1  christos 			break;
    508  1.1  christos 		}
    509  1.1  christos 		str_append(str, ptr, ' ');
    510  1.1  christos 	}
    511  1.1  christos 	free(ptr);
    512  1.1  christos }
    513  1.1  christos 
    514  1.1  christos 
    515  1.1  christos /*
    516  1.1  christos  * ng_rprint(): Pretty print all reverse netgroup mappings in the given entry
    517  1.1  christos  */
    518  1.1  christos static void
    519  1.1  christos ng_rprint(db, str)
    520  1.1  christos 	DB             *db;
    521  1.1  christos 	struct string  *str;
    522  1.1  christos {
    523  1.1  christos 	int             pos;
    524  1.1  christos 	DBT             key, data;
    525  1.1  christos 
    526  1.1  christos 	for (pos = R_FIRST;; pos = R_NEXT)
    527  1.1  christos 		switch ((db->seq)(db, &key, &data, pos)) {
    528  1.1  christos 		case 0:
    529  1.1  christos 			str_append(str, (char *) key.data, ',');
    530  1.1  christos 			break;
    531  1.1  christos 
    532  1.1  christos 		case 1:
    533  1.1  christos 			return;
    534  1.1  christos 
    535  1.1  christos 		default:
    536  1.1  christos 			err(1, "seq");
    537  1.1  christos 			break;
    538  1.1  christos 		}
    539  1.1  christos }
    540  1.1  christos 
    541  1.1  christos 
    542  1.1  christos #ifdef DEBUG_NG
    543  1.1  christos /*
    544  1.1  christos  * ng_dump(): Pretty print all netgroups in the given database
    545  1.1  christos  */
    546  1.1  christos static void
    547  1.1  christos ng_dump(db)
    548  1.1  christos 	DB             *db;
    549  1.1  christos {
    550  1.1  christos 	int             pos;
    551  1.1  christos 	DBT             key, data;
    552  1.1  christos 	struct nentry  *e;
    553  1.1  christos 	struct string   buf;
    554  1.1  christos 
    555  1.1  christos 	for (pos = R_FIRST;; pos = R_NEXT)
    556  1.1  christos 		switch ((db->seq)(db, &key, &data, pos)) {
    557  1.1  christos 		case 0:
    558  1.1  christos 			memcpy(&e, data.data, sizeof(e));
    559  1.1  christos 			str_init(&buf);
    560  1.1  christos 			assert(e->n_type == _NG_NAME);
    561  1.1  christos 
    562  1.1  christos 			ng_print(e, &buf);
    563  1.1  christos 			(void) fprintf(stderr, "%s\t%s\n", e->n_name,
    564  1.1  christos 				       buf.s_str ? buf.s_str : "");
    565  1.1  christos 			str_free(&buf);
    566  1.1  christos 			break;
    567  1.1  christos 
    568  1.1  christos 		case 1:
    569  1.1  christos 			return;
    570  1.1  christos 
    571  1.1  christos 		default:
    572  1.1  christos 			err(1, "seq");
    573  1.1  christos 			return;
    574  1.1  christos 		}
    575  1.1  christos }
    576  1.1  christos 
    577  1.1  christos 
    578  1.1  christos /*
    579  1.1  christos  * ng_rdump(): Pretty print all reverse mappings in the given database
    580  1.1  christos  */
    581  1.1  christos static void
    582  1.1  christos ng_rdump(db)
    583  1.1  christos 	DB             *db;
    584  1.1  christos {
    585  1.1  christos 	int             pos;
    586  1.1  christos 	DBT             key, data;
    587  1.1  christos 	DB             *xdb;
    588  1.1  christos 	struct string   buf;
    589  1.1  christos 
    590  1.1  christos 	for (pos = R_FIRST;; pos = R_NEXT)
    591  1.1  christos 		switch ((db->seq)(db, &key, &data, pos)) {
    592  1.1  christos 		case 0:
    593  1.1  christos 			memcpy(&xdb, data.data, sizeof(xdb));
    594  1.1  christos 			str_init(&buf);
    595  1.1  christos 			ng_rprint(xdb, &buf);
    596  1.1  christos 			(void) fprintf(stderr, "%s\t%s\n",
    597  1.1  christos 				       (char *) key.data,
    598  1.1  christos 				       buf.s_str ? buf.s_str : "");
    599  1.1  christos 			str_free(&buf);
    600  1.1  christos 			break;
    601  1.1  christos 
    602  1.1  christos 		case 1:
    603  1.1  christos 			return;
    604  1.1  christos 
    605  1.1  christos 		default:
    606  1.1  christos 			err(1, "seq");
    607  1.1  christos 			return;
    608  1.1  christos 		}
    609  1.1  christos }
    610  1.1  christos #endif /* DEBUG_NG */
    611  1.1  christos 
    612  1.1  christos 
    613  1.1  christos /*
    614  1.1  christos  * ng_write(): Dump the database into a file.
    615  1.1  christos  */
    616  1.1  christos static void
    617  1.1  christos ng_write(odb, idb, k)
    618  1.1  christos 	DB             *odb, *idb;
    619  1.1  christos 	int             k;
    620  1.1  christos {
    621  1.1  christos 	int             pos;
    622  1.1  christos 	DBT             key, data;
    623  1.1  christos 	struct nentry  *e;
    624  1.1  christos 	struct string   skey, sdata;
    625  1.1  christos 
    626  1.1  christos 	for (pos = R_FIRST;; pos = R_NEXT)
    627  1.1  christos 		switch ((idb->seq)(idb, &key, &data, pos)) {
    628  1.1  christos 		case 0:
    629  1.1  christos 			memcpy(&e, data.data, sizeof(e));
    630  1.1  christos 			str_init(&skey);
    631  1.1  christos 			str_init(&sdata);
    632  1.1  christos 			assert(e->n_type == _NG_NAME);
    633  1.1  christos 
    634  1.1  christos 			str_prepend(&skey, e->n_name, k);
    635  1.1  christos 			ng_print(e, &sdata);
    636  1.1  christos 			key.data = (u_char *) skey.s_str;
    637  1.1  christos 			key.size = skey.s_len + 1;
    638  1.1  christos 			data.data = (u_char *) sdata.s_str;
    639  1.1  christos 			data.size = sdata.s_len + 1;
    640  1.1  christos 
    641  1.1  christos 			switch ((odb->put)(odb, &key, &data, R_NOOVERWRITE)) {
    642  1.1  christos 			case 0:
    643  1.1  christos 				break;
    644  1.1  christos 
    645  1.1  christos 			case -1:
    646  1.1  christos 				err(1, "put");
    647  1.1  christos 				break;
    648  1.1  christos 
    649  1.1  christos 			case 1:
    650  1.1  christos 			default:
    651  1.1  christos 				abort();
    652  1.1  christos 				break;
    653  1.1  christos 			}
    654  1.1  christos 
    655  1.1  christos 			str_free(&skey);
    656  1.1  christos 			str_free(&sdata);
    657  1.1  christos 			break;
    658  1.1  christos 
    659  1.1  christos 		case 1:
    660  1.1  christos 			return;
    661  1.1  christos 
    662  1.1  christos 		default:
    663  1.1  christos 			err(1, "seq");
    664  1.1  christos 			return;
    665  1.1  christos 		}
    666  1.1  christos }
    667  1.1  christos 
    668  1.1  christos 
    669  1.1  christos /*
    670  1.1  christos  * ng_rwrite(): Write the database
    671  1.1  christos  */
    672  1.1  christos static void
    673  1.1  christos ng_rwrite(odb, idb, k)
    674  1.1  christos 	DB             *odb;
    675  1.1  christos 	DB             *idb;
    676  1.1  christos 	int             k;
    677  1.1  christos {
    678  1.1  christos 	int             pos;
    679  1.1  christos 	DBT             key, data;
    680  1.1  christos 	DB             *xdb;
    681  1.1  christos 	struct string   skey, sdata;
    682  1.1  christos 
    683  1.1  christos 	for (pos = R_FIRST;; pos = R_NEXT)
    684  1.1  christos 		switch ((idb->seq)(idb, &key, &data, pos)) {
    685  1.1  christos 		case 0:
    686  1.1  christos 			memcpy(&xdb, data.data, sizeof(xdb));
    687  1.1  christos 			str_init(&skey);
    688  1.1  christos 			str_init(&sdata);
    689  1.1  christos 
    690  1.1  christos 			str_prepend(&skey, (char *) key.data, k);
    691  1.1  christos 			ng_rprint(xdb, &sdata);
    692  1.1  christos 			key.data = (u_char *) skey.s_str;
    693  1.1  christos 			key.size = skey.s_len + 1;
    694  1.1  christos 			data.data = (u_char *) sdata.s_str;
    695  1.1  christos 			data.size = sdata.s_len + 1;
    696  1.1  christos 
    697  1.1  christos 			switch ((odb->put)(odb, &key, &data, R_NOOVERWRITE)) {
    698  1.1  christos 			case 0:
    699  1.1  christos 				break;
    700  1.1  christos 
    701  1.1  christos 			case -1:
    702  1.1  christos 				err(1, "put");
    703  1.1  christos 				break;
    704  1.1  christos 
    705  1.1  christos 			case 1:
    706  1.1  christos 			default:
    707  1.1  christos 				abort();
    708  1.1  christos 				break;
    709  1.1  christos 			}
    710  1.1  christos 
    711  1.1  christos 			str_free(&skey);
    712  1.1  christos 			str_free(&sdata);
    713  1.1  christos 			break;
    714  1.1  christos 
    715  1.1  christos 		case 1:
    716  1.1  christos 			return;
    717  1.1  christos 
    718  1.1  christos 		default:
    719  1.1  christos 			err(1, "seq");
    720  1.1  christos 			return;
    721  1.1  christos 		}
    722  1.1  christos }
    723  1.1  christos 
    724  1.1  christos 
    725  1.1  christos /*
    726  1.1  christos  * usage(): Print usage message and exit
    727  1.1  christos  */
    728  1.1  christos static void
    729  1.1  christos usage()
    730  1.1  christos {
    731  1.1  christos 	extern const char *__progname;
    732  1.1  christos 	fprintf(stderr, "usage: %s [-o db] file\n", __progname);
    733  1.1  christos 	exit(1);
    734  1.1  christos }
    735