Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*	$NetBSD: gen.c,v 1.1.1.2 2012/09/09 16:07:55 christos Exp $	*/

/*
 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
 * Copyright (c) 1996-1999 by Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#if !defined(LINT) && !defined(CODECENTER)
static const char rcsid[] = "Id: gen.c,v 1.7 2005/04/27 04:56:23 sra Exp ";
#endif

/*! \file
 * \brief
 * this is the top level dispatcher
 *
 * The dispatcher is implemented as an accessor class; it is an
 * accessor class that calls other accessor classes, as controlled by a
 * configuration file.
 * 
 * A big difference between this accessor class and others is that the
 * map class initializers are NULL, and the map classes are already
 * filled in with method functions that will do the right thing.
 */

/* Imports */

#include "port_before.h"

#include <isc/assertions.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <netinet/in.h> 
#include <arpa/nameser.h>
#include <resolv.h>

#include <isc/memcluster.h>
#include <irs.h>

#include "port_after.h"

#include "irs_p.h"
#include "gen_p.h"

/* Definitions */

struct nameval {
	const char *	name;
	int		val;
};

static const struct nameval acc_names[irs_nacc+1] = {
	{ "local", irs_lcl },
	{ "dns", irs_dns },
	{ "nis", irs_nis },
	{ "irp", irs_irp },
	{ NULL, irs_nacc }
};

typedef struct irs_acc *(*accinit) __P((const char *options));

static const accinit accs[irs_nacc+1] = {
	irs_lcl_acc,
	irs_dns_acc,
#ifdef WANT_IRS_NIS
	irs_nis_acc,
#else
	NULL,
#endif
	irs_irp_acc,
	NULL
};

static const struct nameval map_names[irs_nmap+1] = {
	{ "group", irs_gr },
	{ "passwd", irs_pw },
	{ "services", irs_sv },
	{ "protocols", irs_pr },
	{ "hosts", irs_ho },
	{ "networks", irs_nw },
	{ "netgroup", irs_ng },
	{ NULL, irs_nmap }
};

static const struct nameval option_names[] = {
	{ "merge", IRS_MERGE },
	{ "continue", IRS_CONTINUE },
	{ NULL, 0 }
};

/* Forward */

static void		gen_close(struct irs_acc *);
static struct __res_state * gen_res_get(struct irs_acc *);
static void		gen_res_set(struct irs_acc *, struct __res_state *,
				    void (*)(void *));
static int		find_name(const char *, const struct nameval nv[]);
static void 		init_map_rules(struct gen_p *, const char *conf_file);
static struct irs_rule *release_rule(struct irs_rule *);
static int		add_rule(struct gen_p *,
				 enum irs_map_id, enum irs_acc_id,
				 const char *);

/* Public */

struct irs_acc *
irs_gen_acc(const char *options, const char *conf_file) {
	struct irs_acc *acc;
	struct gen_p *irs;
		
	if (!(acc = memget(sizeof *acc))) {
		errno = ENOMEM;
		return (NULL);
	}
	memset(acc, 0x5e, sizeof *acc);
	if (!(irs = memget(sizeof *irs))) {
		errno = ENOMEM;
		memput(acc, sizeof *acc);
		return (NULL);
	}
	memset(irs, 0x5e, sizeof *irs);
	irs->options = strdup(options);
	irs->res = NULL;
	irs->free_res = NULL;
	memset(irs->accessors, 0, sizeof irs->accessors);
	memset(irs->map_rules, 0, sizeof irs->map_rules);
	init_map_rules(irs, conf_file);
	acc->private = irs;
#ifdef WANT_IRS_GR
	acc->gr_map = irs_gen_gr;
#else
	acc->gr_map = NULL;
#endif
#ifdef WANT_IRS_PW
	acc->pw_map = irs_gen_pw;
#else
	acc->pw_map = NULL;
#endif
	acc->sv_map = irs_gen_sv;
	acc->pr_map = irs_gen_pr;
	acc->ho_map = irs_gen_ho;
	acc->nw_map = irs_gen_nw;
	acc->ng_map = irs_gen_ng;
	acc->res_get = gen_res_get;
	acc->res_set = gen_res_set;
	acc->close = gen_close;
	return (acc);
}

/* Methods */

static struct __res_state *
gen_res_get(struct irs_acc *this) {
	struct gen_p *irs = (struct gen_p *)this->private;

	if (irs->res == NULL) {
		struct __res_state *res;
		res = (struct __res_state *)malloc(sizeof *res);
		if (res == NULL)
			return (NULL);
		memset(res, 0, sizeof *res);
		gen_res_set(this, res, free);
	}

	if (((irs->res->options & RES_INIT) == 0U) && res_ninit(irs->res) < 0)
		return (NULL);

	return (irs->res);
}

static void
gen_res_set(struct irs_acc *this, struct __res_state *res,
	    void (*free_res)(void *)) {
	struct gen_p *irs = (struct gen_p *)this->private;
#if 0
	struct irs_rule *rule;
	struct irs_ho *ho;
	struct irs_nw *nw;
#endif

	if (irs->res && irs->free_res) {
		res_nclose(irs->res);
		(*irs->free_res)(irs->res);
	}

	irs->res = res;
	irs->free_res = free_res;

#if 0
	for (rule = irs->map_rules[irs_ho]; rule; rule = rule->next) {
                ho = rule->inst->ho;

                (*ho->res_set)(ho, res, NULL);
	}
	for (rule = irs->map_rules[irs_nw]; rule; rule = rule->next) {
                nw = rule->inst->nw;

                (*nw->res_set)(nw, res, NULL);
	}
#endif
}

static void
gen_close(struct irs_acc *this) {
	struct gen_p *irs = (struct gen_p *)this->private;
	int n;
	
	/* Search rules. */
	for (n = 0; n < irs_nmap; n++)
		while (irs->map_rules[n] != NULL)
			irs->map_rules[n] = release_rule(irs->map_rules[n]);

	/* Access methods. */
	for (n = 0; n < irs_nacc; n++) {
		/* Map objects. */
		if (irs->accessors[n].gr != NULL)
			(*irs->accessors[n].gr->close)(irs->accessors[n].gr);
		if (irs->accessors[n].pw != NULL)
			(*irs->accessors[n].pw->close)(irs->accessors[n].pw);
		if (irs->accessors[n].sv != NULL)
			(*irs->accessors[n].sv->close)(irs->accessors[n].sv);
		if (irs->accessors[n].pr != NULL)
			(*irs->accessors[n].pr->close)(irs->accessors[n].pr);
		if (irs->accessors[n].ho != NULL)
			(*irs->accessors[n].ho->close)(irs->accessors[n].ho);
		if (irs->accessors[n].nw != NULL)
			(*irs->accessors[n].nw->close)(irs->accessors[n].nw);
		if (irs->accessors[n].ng != NULL)
			(*irs->accessors[n].ng->close)(irs->accessors[n].ng);
		/* Enclosing accessor. */
		if (irs->accessors[n].acc != NULL)
			(*irs->accessors[n].acc->close)(irs->accessors[n].acc);
	}

	/* The options string was strdup'd. */
	free((void*)irs->options);

	if (irs->res && irs->free_res)
		(*irs->free_res)(irs->res);

	/* The private data container. */
	memput(irs, sizeof *irs);

	/* The object. */
	memput(this, sizeof *this);
}

/* Private */

static int
find_name(const char *name, const struct nameval names[]) {
	int n;

	for (n = 0; names[n].name != NULL; n++)
		if (strcmp(name, names[n].name) == 0)
			return (names[n].val);
	return (-1);
}

static struct irs_rule *
release_rule(struct irs_rule *rule) {
	struct irs_rule *next = rule->next;

	memput(rule, sizeof *rule);
	return (next);
}

static int
add_rule(struct gen_p *irs,
	 enum irs_map_id map, enum irs_acc_id acc,
	 const char *options)
{
	struct irs_rule **rules, *last, *tmp, *new;
	struct irs_inst *inst;
	const char *cp;
	int n;

#ifndef WANT_IRS_GR
	if (map == irs_gr)
		return (-1);
#endif
#ifndef WANT_IRS_PW
	if (map == irs_pw)
		return (-1);
#endif
#ifndef WANT_IRS_NIS
	if (acc == irs_nis)
		return (-1);
#endif
	new = memget(sizeof *new);
	if (new == NULL)
		return (-1);
	memset(new, 0x5e, sizeof *new);
	new->next = NULL;

	new->inst = &irs->accessors[acc];

	new->flags = 0;
	cp = options;
	while (cp && *cp) {
		char option[50], *next;

		next = strchr(cp, ',');
		if (next)
			n = next++ - cp;
		else
			n = strlen(cp);
		if ((size_t)n > sizeof option - 1)
			n = sizeof option - 1;
		strncpy(option, cp, n);
		option[n] = '\0';

		n = find_name(option, option_names);
		if (n >= 0)
			new->flags |= n;

		cp = next;
	}

	rules = &irs->map_rules[map];
	for (last = NULL, tmp = *rules;
	     tmp != NULL;
	     last = tmp, tmp = tmp->next)
		(void)NULL;
	if (last == NULL)
		*rules = new;
	else
		last->next = new;

	/* Try to instantiate map accessors for this if necessary & approp. */
	inst = &irs->accessors[acc];
	if (inst->acc == NULL && accs[acc] != NULL)
		inst->acc = (*accs[acc])(irs->options);
	if (inst->acc != NULL) {
		if (inst->gr == NULL && inst->acc->gr_map != NULL)
			inst->gr = (*inst->acc->gr_map)(inst->acc);
		if (inst->pw == NULL && inst->acc->pw_map != NULL)
			inst->pw = (*inst->acc->pw_map)(inst->acc);
		if (inst->sv == NULL && inst->acc->sv_map != NULL)
			inst->sv = (*inst->acc->sv_map)(inst->acc);
		if (inst->pr == NULL && inst->acc->pr_map != NULL)
			inst->pr = (*inst->acc->pr_map)(inst->acc);
		if (inst->ho == NULL && inst->acc->ho_map != NULL)
			inst->ho = (*inst->acc->ho_map)(inst->acc);
		if (inst->nw == NULL && inst->acc->nw_map != NULL)
			inst->nw = (*inst->acc->nw_map)(inst->acc);
		if (inst->ng == NULL && inst->acc->ng_map != NULL)
			inst->ng = (*inst->acc->ng_map)(inst->acc);
	}

	return (0);
}

static void
default_map_rules(struct gen_p *irs) {
	/* Install time honoured and proved BSD style rules as default. */
	add_rule(irs, irs_gr, irs_lcl, "");
	add_rule(irs, irs_pw, irs_lcl, "");
	add_rule(irs, irs_sv, irs_lcl, "");
	add_rule(irs, irs_pr, irs_lcl, "");
	add_rule(irs, irs_ho, irs_dns, "continue");
	add_rule(irs, irs_ho, irs_lcl, "");
	add_rule(irs, irs_nw, irs_dns, "continue");
	add_rule(irs, irs_nw, irs_lcl, "");
	add_rule(irs, irs_ng, irs_lcl, "");
}

static void
init_map_rules(struct gen_p *irs, const char *conf_file) {
	char line[1024], pattern[40], mapname[20], accname[20], options[100];
	FILE *conf;

	if (conf_file == NULL) 
		conf_file = _PATH_IRS_CONF ;

	/* A conf file of "" means compiled in defaults. Irpd wants this */
	if (conf_file[0] == '\0' || (conf = fopen(conf_file, "r")) == NULL) {
		default_map_rules(irs);
		return;
	}
	(void) sprintf(pattern, "%%%lus %%%lus %%%lus\n",
		       (unsigned long)sizeof mapname,
		       (unsigned long)sizeof accname,
		       (unsigned long)sizeof options);
	while (fgets(line, sizeof line, conf)) {
		enum irs_map_id map;
		enum irs_acc_id acc;
		char *tmp;
		int n;

		for (tmp = line;
		     isascii((unsigned char)*tmp) &&
		     isspace((unsigned char)*tmp);
		     tmp++)
			(void)NULL;
		if (*tmp == '#' || *tmp == '\n' || *tmp == '\0')
			continue;
		n = sscanf(tmp, pattern, mapname, accname, options);
		if (n < 2)
			continue;
		if (n < 3)
			options[0] = '\0';

		n = find_name(mapname, map_names);
		INSIST(n < irs_nmap);
		if (n < 0)
			continue;
		map = (enum irs_map_id) n;

		n = find_name(accname, acc_names);
		INSIST(n < irs_nacc);
		if (n < 0)
			continue;
		acc = (enum irs_acc_id) n;

		add_rule(irs, map, acc, options);
	}
	fclose(conf);
}