Home | History | Annotate | Download | only in net
      1 /*	$NetBSD: nsdispatch.c,v 1.30 2005/11/29 03:11:59 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999, 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Luke Mewburn; and by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 2003 Networks Associates Technology, Inc.
     41  * All rights reserved.
     42  *
     43  * Portions of this software were developed for the FreeBSD Project by
     44  * Jacques A. Vidrine, Safeport Network Services, and Network
     45  * Associates Laboratories, the Security Research Division of Network
     46  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
     47  * ("CBOSS"), as part of the DARPA CHATS research program.
     48  *
     49  * Redistribution and use in source and binary forms, with or without
     50  * modification, are permitted provided that the following conditions
     51  * are met:
     52  * 1. Redistributions of source code must retain the above copyright
     53  *    notice, this list of conditions and the following disclaimer.
     54  * 2. Redistributions in binary form must reproduce the above copyright
     55  *    notice, this list of conditions and the following disclaimer in the
     56  *    documentation and/or other materials provided with the distribution.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  */
     70 
     71 #include <sys/cdefs.h>
     72 
     73 #include <assert.h>
     74 #include <nsswitch.h>
     75 #include <stdarg.h>
     76 #include <strings.h>
     77 
     78 static nss_method
     79 _nsmethod(const char *source, const char *database __unused, const char *method __unused,
     80     const ns_dtab disp_tab[], void **cb_data)
     81 {
     82 	int	curdisp;
     83 
     84 	if (disp_tab != NULL) {
     85 		for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++) {
     86 			if (strcasecmp(source, disp_tab[curdisp].src) == 0) {
     87 				*cb_data = disp_tab[curdisp].cb_data;
     88 				return (disp_tab[curdisp].callback);
     89 			}
     90 		}
     91 	}
     92 
     93 	*cb_data = NULL;
     94 	return (NULL);
     95 }
     96 
     97 int
     98 /*ARGSUSED*/
     99 nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database,
    100 	    const char *method, const ns_src defaults[], ...)
    101 {
    102 	va_list		 ap;
    103 	int		 i, result;
    104 	const ns_src	*srclist;
    105 	int		 srclistsize;
    106 	nss_method	 cb;
    107 	void		*cb_data;
    108 
    109 	/* retval may be NULL */
    110 	/* disp_tab may be NULL */
    111 	assert(database != NULL);
    112 	assert(method != NULL);
    113 	assert(defaults != NULL);
    114 	if (database == NULL || method == NULL || defaults == NULL)
    115 		return (NS_UNAVAIL);
    116 
    117         srclist = defaults;
    118         srclistsize = 0;
    119         while (srclist[srclistsize].name != NULL)
    120                 srclistsize++;
    121 
    122 	result = 0;
    123 
    124 	for (i = 0; i < srclistsize; i++) {
    125 		cb = _nsmethod(srclist[i].name, database, method,
    126 		    disp_tab, &cb_data);
    127 		result = 0;
    128 		if (cb != NULL) {
    129 			va_start(ap, defaults);
    130 			result = (*cb)(retval, cb_data, ap);
    131 			va_end(ap);
    132 			if (defaults[0].flags & NS_FORCEALL)
    133 				continue;
    134 			if (result & srclist[i].flags)
    135 				break;
    136 		}
    137 	}
    138 	result &= NS_STATUSMASK;	/* clear private flags in result */
    139 
    140 	return (result ? result : NS_NOTFOUND);
    141 }
    142