Home | History | Annotate | Line # | Download | only in irs
      1      1.1  christos /*	$NetBSD: getnetgrent.c,v 1.1.1.2 2012/09/09 16:07:55 christos Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*
      4      1.1  christos  * Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
      5      1.1  christos  * Copyright (C) 1996-1999, 2001, 2003  Internet Software Consortium.
      6      1.1  christos  *
      7      1.1  christos  * Permission to use, copy, modify, and/or 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 WITH
     12      1.1  christos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
     13      1.1  christos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
     14      1.1  christos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
     15      1.1  christos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
     16      1.1  christos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
     17      1.1  christos  * 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: getnetgrent.c,v 1.6 2008/11/14 02:36:51 marka Exp ";
     22      1.1  christos #endif /* LIBC_SCCS and not lint */
     23      1.1  christos 
     24      1.1  christos /* Imports */
     25      1.1  christos 
     26      1.1  christos #include "port_before.h"
     27      1.1  christos 
     28      1.1  christos #if !defined(__BIND_NOSTATIC)
     29      1.1  christos 
     30      1.1  christos #include <sys/types.h>
     31      1.1  christos 
     32      1.1  christos #include <netinet/in.h>
     33      1.1  christos #include <arpa/nameser.h>
     34      1.1  christos 
     35      1.1  christos #include <errno.h>
     36      1.1  christos #include <resolv.h>
     37      1.1  christos #include <stdio.h>
     38      1.1  christos 
     39      1.1  christos #include <irs.h>
     40      1.1  christos 
     41      1.1  christos #include "port_after.h"
     42      1.1  christos 
     43      1.1  christos #include "irs_data.h"
     44      1.1  christos 
     45      1.1  christos /* Forward */
     46      1.1  christos 
     47      1.1  christos static struct net_data *init(void);
     48      1.1  christos 
     49      1.1  christos 
     50      1.1  christos /* Public */
     51      1.1  christos 
     52      1.1  christos #ifndef SETNETGRENT_ARGS
     53      1.1  christos #define SETNETGRENT_ARGS const char *netgroup
     54      1.1  christos #endif
     55      1.1  christos void
     56      1.1  christos setnetgrent(SETNETGRENT_ARGS) {
     57      1.1  christos 	struct net_data *net_data = init();
     58      1.1  christos 
     59      1.1  christos 	setnetgrent_p(netgroup, net_data);
     60      1.1  christos }
     61      1.1  christos 
     62      1.1  christos void
     63      1.1  christos endnetgrent(void) {
     64      1.1  christos 	struct net_data *net_data = init();
     65      1.1  christos 
     66      1.1  christos 	endnetgrent_p(net_data);
     67      1.1  christos }
     68      1.1  christos 
     69      1.1  christos #ifndef INNETGR_ARGS
     70      1.1  christos #define INNETGR_ARGS const char *netgroup, const char *host, \
     71      1.1  christos 		     const char *user, const char *domain
     72      1.1  christos #endif
     73      1.1  christos int
     74      1.1  christos innetgr(INNETGR_ARGS) {
     75      1.1  christos 	struct net_data *net_data = init();
     76      1.1  christos 
     77      1.1  christos 	return (innetgr_p(netgroup, host, user, domain, net_data));
     78      1.1  christos }
     79      1.1  christos 
     80      1.1  christos int
     81      1.1  christos getnetgrent(NGR_R_CONST char **host, NGR_R_CONST char **user,
     82      1.1  christos 	    NGR_R_CONST char **domain)
     83      1.1  christos {
     84      1.1  christos 	struct net_data *net_data = init();
     85      1.1  christos 	const char *ch, *cu, *cd;
     86      1.1  christos 	int ret;
     87      1.1  christos 
     88      1.1  christos 	ret = getnetgrent_p(&ch, &cu, &cd, net_data);
     89      1.1  christos 	if (ret != 1)
     90      1.1  christos 		return (ret);
     91      1.1  christos 
     92      1.1  christos 	DE_CONST(ch, *host);
     93      1.1  christos 	DE_CONST(cu, *user);
     94      1.1  christos 	DE_CONST(cd, *domain);
     95      1.1  christos 	return (ret);
     96      1.1  christos }
     97      1.1  christos 
     98      1.1  christos /* Shared private. */
     99      1.1  christos 
    100      1.1  christos void
    101      1.1  christos setnetgrent_p(const char *netgroup, struct net_data *net_data) {
    102      1.1  christos 	struct irs_ng *ng;
    103      1.1  christos 
    104      1.1  christos 	if ((net_data != NULL) && ((ng = net_data->ng) != NULL))
    105      1.1  christos 		(*ng->rewind)(ng, netgroup);
    106      1.1  christos }
    107      1.1  christos 
    108      1.1  christos void
    109      1.1  christos endnetgrent_p(struct net_data *net_data) {
    110      1.1  christos 	struct irs_ng *ng;
    111      1.1  christos 
    112      1.1  christos 	if (!net_data)
    113      1.1  christos 		return;
    114      1.1  christos 	if ((ng = net_data->ng) != NULL)
    115      1.1  christos 		(*ng->close)(ng);
    116      1.1  christos 	net_data->ng = NULL;
    117      1.1  christos }
    118      1.1  christos 
    119      1.1  christos int
    120      1.1  christos innetgr_p(const char *netgroup, const char *host,
    121      1.1  christos 	  const char *user, const char *domain,
    122      1.1  christos 	  struct net_data *net_data) {
    123      1.1  christos 	struct irs_ng *ng;
    124      1.1  christos 
    125      1.1  christos 	if (!net_data || !(ng = net_data->ng))
    126      1.1  christos 		return (0);
    127      1.1  christos 	return ((*ng->test)(ng, netgroup, host, user, domain));
    128      1.1  christos }
    129      1.1  christos 
    130      1.1  christos int
    131      1.1  christos getnetgrent_p(const char **host, const char **user, const char **domain,
    132      1.1  christos 	      struct net_data *net_data ) {
    133      1.1  christos 	struct irs_ng *ng;
    134      1.1  christos 
    135      1.1  christos 	if (!net_data || !(ng = net_data->ng))
    136      1.1  christos 		return (0);
    137      1.1  christos 	return ((*ng->next)(ng, host, user, domain));
    138      1.1  christos }
    139      1.1  christos 
    140      1.1  christos /* Private */
    141      1.1  christos 
    142      1.1  christos static struct net_data *
    143      1.1  christos init(void) {
    144      1.1  christos 	struct net_data *net_data;
    145      1.1  christos 
    146      1.1  christos 	if (!(net_data = net_data_init(NULL)))
    147      1.1  christos 		goto error;
    148      1.1  christos 	if (!net_data->ng) {
    149      1.1  christos 		net_data->ng = (*net_data->irs->ng_map)(net_data->irs);
    150      1.1  christos 		if (!net_data->ng) {
    151      1.1  christos   error:
    152      1.1  christos 			errno = EIO;
    153      1.1  christos 			return (NULL);
    154      1.1  christos 		}
    155      1.1  christos 	}
    156      1.1  christos 
    157      1.1  christos 	return (net_data);
    158      1.1  christos }
    159      1.1  christos 
    160      1.1  christos #endif /*__BIND_NOSTATIC*/
    161      1.1  christos 
    162      1.1  christos /*! \file */
    163