Home | History | Annotate | Line # | Download | only in irs
      1      1.1  christos /*	$NetBSD: dns_gr.c,v 1.1.1.2 2012/09/09 16:07:52 christos Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*
      4      1.1  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5      1.1  christos  * Copyright (c) 1996-1999 by Internet Software Consortium.
      6      1.1  christos  *
      7      1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      8      1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      9      1.1  christos  * copyright notice and this permission notice appear in all copies.
     10      1.1  christos  *
     11      1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12      1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13      1.1  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14      1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15      1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16      1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17      1.1  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18      1.1  christos  */
     19      1.1  christos 
     20      1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     21  1.1.1.2  christos static const char rcsid[] = "Id: dns_gr.c,v 1.4 2005/04/27 04:56:21 sra Exp ";
     22      1.1  christos #endif
     23      1.1  christos 
     24      1.1  christos /*! \file
     25      1.1  christos  * \brief
     26      1.1  christos  * dns_gr.c --- this file contains the functions for accessing
     27      1.1  christos  * 	group information from Hesiod.
     28      1.1  christos  */
     29      1.1  christos 
     30      1.1  christos #include "port_before.h"
     31      1.1  christos 
     32      1.1  christos #ifndef WANT_IRS_GR
     33      1.1  christos static int __bind_irs_gr_unneeded;
     34      1.1  christos #else
     35      1.1  christos 
     36      1.1  christos #include <sys/param.h>
     37      1.1  christos #include <sys/types.h>
     38      1.1  christos 
     39      1.1  christos #include <stdio.h>
     40      1.1  christos #include <stdlib.h>
     41      1.1  christos #include <string.h>
     42      1.1  christos #include <errno.h>
     43      1.1  christos #include <unistd.h>
     44      1.1  christos 
     45      1.1  christos #include <sys/types.h>
     46      1.1  christos #include <netinet/in.h>
     47      1.1  christos #include <arpa/nameser.h>
     48      1.1  christos #include <resolv.h>
     49      1.1  christos 
     50      1.1  christos #include <isc/memcluster.h>
     51      1.1  christos 
     52      1.1  christos #include <irs.h>
     53      1.1  christos 
     54      1.1  christos #include "port_after.h"
     55      1.1  christos 
     56      1.1  christos #include "irs_p.h"
     57      1.1  christos #include "hesiod.h"
     58      1.1  christos #include "dns_p.h"
     59      1.1  christos 
     60      1.1  christos /* Types. */
     61      1.1  christos 
     62      1.1  christos struct pvt {
     63      1.1  christos 	/*
     64      1.1  christos 	 * This is our private accessor data.  It has a shared hesiod context.
     65      1.1  christos 	 */
     66      1.1  christos 	struct dns_p *	dns;
     67      1.1  christos 	/*
     68      1.1  christos 	 * Need space to store the entries read from the group file.
     69      1.1  christos 	 * The members list also needs space per member, and the
     70      1.1  christos 	 * strings making up the user names must be allocated
     71      1.1  christos 	 * somewhere.  Rather than doing lots of small allocations,
     72      1.1  christos 	 * we keep one buffer and resize it as needed.
     73      1.1  christos 	 */
     74      1.1  christos 	struct group	group;
     75      1.1  christos 	size_t		nmemb;		/*%< Malloc'd max index of gr_mem[]. */
     76      1.1  christos 	char *		membuf;
     77      1.1  christos 	size_t		membufsize;
     78      1.1  christos };
     79      1.1  christos 
     80      1.1  christos /* Forward. */
     81      1.1  christos 
     82      1.1  christos static struct group *	gr_next(struct irs_gr *);
     83      1.1  christos static struct group *	gr_byname(struct irs_gr *, const char *);
     84      1.1  christos static struct group *	gr_bygid(struct irs_gr *, gid_t);
     85      1.1  christos static void		gr_rewind(struct irs_gr *);
     86      1.1  christos static void		gr_close(struct irs_gr *);
     87      1.1  christos static int		gr_list(struct irs_gr *, const char *,
     88      1.1  christos 				gid_t, gid_t *, int *);
     89      1.1  christos static void		gr_minimize(struct irs_gr *);
     90      1.1  christos static struct __res_state * gr_res_get(struct irs_gr *);
     91      1.1  christos static void		gr_res_set(struct irs_gr *,
     92      1.1  christos 				   struct __res_state *,
     93      1.1  christos 				   void (*)(void *));
     94      1.1  christos 
     95      1.1  christos static struct group *	get_hes_group(struct irs_gr *this,
     96      1.1  christos 				      const char *name,
     97      1.1  christos 				      const char *type);
     98      1.1  christos 
     99      1.1  christos /* Public. */
    100      1.1  christos 
    101      1.1  christos struct irs_gr *
    102      1.1  christos irs_dns_gr(struct irs_acc *this) {
    103      1.1  christos 	struct dns_p *dns = (struct dns_p *)this->private;
    104      1.1  christos 	struct irs_gr *gr;
    105      1.1  christos 	struct pvt *pvt;
    106      1.1  christos 
    107      1.1  christos 	if (!dns || !dns->hes_ctx) {
    108      1.1  christos 		errno = ENODEV;
    109      1.1  christos 		return (NULL);
    110      1.1  christos 	}
    111      1.1  christos 	if (!(pvt = memget(sizeof *pvt))) {
    112      1.1  christos 		errno = ENOMEM;
    113      1.1  christos 		return (NULL);
    114      1.1  christos 	}
    115      1.1  christos 	memset(pvt, 0, sizeof *pvt);
    116      1.1  christos 	pvt->dns = dns;
    117      1.1  christos 	if (!(gr = memget(sizeof *gr))) {
    118      1.1  christos 		memput(pvt, sizeof *pvt);
    119      1.1  christos 		errno = ENOMEM;
    120      1.1  christos 		return (NULL);
    121      1.1  christos 	}
    122      1.1  christos 	memset(gr, 0x5e, sizeof *gr);
    123      1.1  christos 	gr->private = pvt;
    124      1.1  christos 	gr->next = gr_next;
    125      1.1  christos 	gr->byname = gr_byname;
    126      1.1  christos 	gr->bygid = gr_bygid;
    127      1.1  christos 	gr->rewind = gr_rewind;
    128      1.1  christos 	gr->close = gr_close;
    129      1.1  christos 	gr->list = gr_list;
    130      1.1  christos 	gr->minimize = gr_minimize;
    131      1.1  christos 	gr->res_get = gr_res_get;
    132      1.1  christos 	gr->res_set = gr_res_set;
    133      1.1  christos 	return (gr);
    134      1.1  christos }
    135      1.1  christos 
    136      1.1  christos /* methods */
    137      1.1  christos 
    138      1.1  christos static void
    139      1.1  christos gr_close(struct irs_gr *this) {
    140      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    141      1.1  christos 
    142      1.1  christos 	if (pvt->group.gr_mem)
    143      1.1  christos 		free(pvt->group.gr_mem);
    144      1.1  christos 	if (pvt->membuf)
    145      1.1  christos 		free(pvt->membuf);
    146      1.1  christos 	memput(pvt, sizeof *pvt);
    147      1.1  christos 	memput(this, sizeof *this);
    148      1.1  christos }
    149      1.1  christos 
    150      1.1  christos static struct group *
    151      1.1  christos gr_next(struct irs_gr *this) {
    152      1.1  christos 
    153      1.1  christos 	UNUSED(this);
    154      1.1  christos 
    155      1.1  christos 	return (NULL);
    156      1.1  christos }
    157      1.1  christos 
    158      1.1  christos static struct group *
    159      1.1  christos gr_byname(struct irs_gr *this, const char *name) {
    160      1.1  christos 	return (get_hes_group(this, name, "group"));
    161      1.1  christos }
    162      1.1  christos 
    163      1.1  christos static struct group *
    164      1.1  christos gr_bygid(struct irs_gr *this, gid_t gid) {
    165      1.1  christos 	char name[32];
    166      1.1  christos 
    167      1.1  christos 	sprintf(name, "%ld", (long)gid);
    168      1.1  christos 	return (get_hes_group(this, name, "gid"));
    169      1.1  christos }
    170      1.1  christos 
    171      1.1  christos static void
    172      1.1  christos gr_rewind(struct irs_gr *this) {
    173      1.1  christos 
    174      1.1  christos 	UNUSED(this);
    175      1.1  christos 
    176      1.1  christos 	/* NOOP */
    177      1.1  christos }
    178      1.1  christos 
    179      1.1  christos static int
    180      1.1  christos gr_list(struct irs_gr *this, const char *name,
    181      1.1  christos 	gid_t basegid, gid_t *groups, int *ngroups)
    182      1.1  christos {
    183      1.1  christos 	UNUSED(this);
    184      1.1  christos 	UNUSED(name);
    185      1.1  christos 	UNUSED(basegid);
    186      1.1  christos 	UNUSED(groups);
    187      1.1  christos 
    188      1.1  christos 	*ngroups = 0;
    189      1.1  christos 	/* There's some way to do this in Hesiod. */
    190      1.1  christos 	return (-1);
    191      1.1  christos }
    192      1.1  christos 
    193      1.1  christos static void
    194      1.1  christos gr_minimize(struct irs_gr *this) {
    195      1.1  christos 
    196      1.1  christos 	UNUSED(this);
    197      1.1  christos 	/* NOOP */
    198      1.1  christos }
    199      1.1  christos 
    200      1.1  christos /* Private. */
    201      1.1  christos 
    202      1.1  christos static struct group *
    203      1.1  christos get_hes_group(struct irs_gr *this, const char *name, const char *type) {
    204      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    205      1.1  christos 	char **hes_list, *cp, **new;
    206      1.1  christos 	size_t num_members = 0;
    207      1.1  christos 	u_long t;
    208      1.1  christos 
    209      1.1  christos 	hes_list = hesiod_resolve(pvt->dns->hes_ctx, name, type);
    210      1.1  christos 	if (!hes_list)
    211      1.1  christos 		return (NULL);
    212      1.1  christos 
    213      1.1  christos 	/*
    214      1.1  christos 	 * Copy the returned hesiod string into storage space.
    215      1.1  christos 	 */
    216      1.1  christos 	if (pvt->membuf)
    217      1.1  christos 		free(pvt->membuf);
    218      1.1  christos 	pvt->membuf = strdup(*hes_list);
    219      1.1  christos 	hesiod_free_list(pvt->dns->hes_ctx, hes_list);
    220      1.1  christos 
    221      1.1  christos 	cp = pvt->membuf;
    222      1.1  christos 	pvt->group.gr_name = cp;
    223      1.1  christos 	if (!(cp = strchr(cp, ':')))
    224      1.1  christos 		goto cleanup;
    225      1.1  christos 	*cp++ = '\0';
    226      1.1  christos 
    227      1.1  christos 	pvt->group.gr_passwd = cp;
    228      1.1  christos 	if (!(cp = strchr(cp, ':')))
    229      1.1  christos 		goto cleanup;
    230      1.1  christos 	*cp++ = '\0';
    231      1.1  christos 
    232      1.1  christos 	errno = 0;
    233      1.1  christos 	t = strtoul(cp, NULL, 10);
    234      1.1  christos 	if (errno == ERANGE)
    235      1.1  christos 		goto cleanup;
    236      1.1  christos 	pvt->group.gr_gid = (gid_t) t;
    237      1.1  christos 	if (!(cp = strchr(cp, ':')))
    238      1.1  christos 		goto cleanup;
    239      1.1  christos 	cp++;
    240      1.1  christos 
    241      1.1  christos 	/*
    242      1.1  christos 	 * Parse the members out.
    243      1.1  christos 	 */
    244      1.1  christos 	while (*cp) {
    245      1.1  christos 		if (num_members+1 >= pvt->nmemb || pvt->group.gr_mem == NULL) {
    246      1.1  christos 			pvt->nmemb += 10;
    247      1.1  christos 			new = realloc(pvt->group.gr_mem,
    248      1.1  christos 				      pvt->nmemb * sizeof(char *));
    249      1.1  christos 			if (new == NULL)
    250      1.1  christos 				goto cleanup;
    251      1.1  christos 			pvt->group.gr_mem = new;
    252      1.1  christos 		}
    253      1.1  christos 		pvt->group.gr_mem[num_members++] = cp;
    254      1.1  christos 		if (!(cp = strchr(cp, ',')))
    255      1.1  christos 			break;
    256      1.1  christos 		*cp++ = '\0';
    257      1.1  christos 	}
    258      1.1  christos 	if (!pvt->group.gr_mem) {
    259      1.1  christos 		pvt->group.gr_mem = malloc(sizeof(char*));
    260      1.1  christos 		if (!pvt->group.gr_mem)
    261      1.1  christos 			goto cleanup;
    262      1.1  christos 	}
    263      1.1  christos 	pvt->group.gr_mem[num_members] = NULL;
    264      1.1  christos 
    265      1.1  christos 	return (&pvt->group);
    266      1.1  christos 
    267      1.1  christos  cleanup:
    268      1.1  christos 	if (pvt->group.gr_mem) {
    269      1.1  christos 		free(pvt->group.gr_mem);
    270      1.1  christos 		pvt->group.gr_mem = NULL;
    271      1.1  christos 	}
    272      1.1  christos 	if (pvt->membuf) {
    273      1.1  christos 		free(pvt->membuf);
    274      1.1  christos 		pvt->membuf = NULL;
    275      1.1  christos 	}
    276      1.1  christos 	return (NULL);
    277      1.1  christos }
    278      1.1  christos 
    279      1.1  christos static struct __res_state *
    280      1.1  christos gr_res_get(struct irs_gr *this) {
    281      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    282      1.1  christos 	struct dns_p *dns = pvt->dns;
    283      1.1  christos 
    284      1.1  christos 	return (__hesiod_res_get(dns->hes_ctx));
    285      1.1  christos }
    286      1.1  christos 
    287      1.1  christos static void
    288      1.1  christos gr_res_set(struct irs_gr *this, struct __res_state * res,
    289      1.1  christos 	   void (*free_res)(void *)) {
    290      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    291      1.1  christos 	struct dns_p *dns = pvt->dns;
    292      1.1  christos 
    293      1.1  christos 	__hesiod_res_set(dns->hes_ctx, res, free_res);
    294      1.1  christos }
    295      1.1  christos 
    296      1.1  christos #endif /* WANT_IRS_GR */
    297