Home | History | Annotate | Line # | Download | only in net
getservent_r.c revision 1.6
      1  1.6  christos /*	$NetBSD: getservent_r.c,v 1.6 2006/07/27 22:03:49 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1983, 1993
      5  1.1  christos  *	The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
     16  1.1  christos  *    may be used to endorse or promote products derived from this software
     17  1.1  christos  *    without specific prior written permission.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1  christos  * SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos 
     32  1.1  christos #include <sys/cdefs.h>
     33  1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     34  1.1  christos #if 0
     35  1.1  christos static char sccsid[] = "@(#)getservent.c	8.1 (Berkeley) 6/4/93";
     36  1.1  christos #else
     37  1.6  christos __RCSID("$NetBSD: getservent_r.c,v 1.6 2006/07/27 22:03:49 christos Exp $");
     38  1.1  christos #endif
     39  1.1  christos #endif /* LIBC_SCCS and not lint */
     40  1.1  christos 
     41  1.1  christos #include "namespace.h"
     42  1.1  christos #include <netdb.h>
     43  1.1  christos #include <errno.h>
     44  1.1  christos #include <stdio.h>
     45  1.1  christos #include <string.h>
     46  1.1  christos #include <stdlib.h>
     47  1.6  christos #include <fcntl.h>
     48  1.6  christos #include <db.h>
     49  1.1  christos 
     50  1.5    kleink #include "servent.h"
     51  1.5    kleink 
     52  1.1  christos #ifdef __weak_alias
     53  1.1  christos __weak_alias(endservent_r,_endservent_r)
     54  1.1  christos __weak_alias(getservent_r,_getservent_r)
     55  1.1  christos __weak_alias(setservent_r,_setservent_r)
     56  1.1  christos #endif
     57  1.1  christos 
     58  1.6  christos int
     59  1.6  christos _servent_open(struct servent_data *sd)
     60  1.1  christos {
     61  1.6  christos 	sd->flags = _SV_FIRST;
     62  1.6  christos 	if (sd->db == NULL) {
     63  1.6  christos 		if ((sd->db = dbopen(_PATH_SERVICES_DB, O_RDONLY, 0,
     64  1.6  christos 		    DB_HASH, NULL)) != NULL)
     65  1.6  christos 			sd->flags |= _SV_DB;
     66  1.6  christos 		else
     67  1.6  christos 			sd->db = fopen(_PATH_SERVICES, "r");
     68  1.6  christos 	}
     69  1.6  christos 	return sd->db ? 0 : -1;
     70  1.1  christos }
     71  1.1  christos 
     72  1.1  christos void
     73  1.6  christos _servent_close(struct servent_data *sd)
     74  1.1  christos {
     75  1.6  christos 	if (sd->db) {
     76  1.6  christos 		if (sd->flags & _SV_DB) {
     77  1.6  christos 			DB *db = sd->db;
     78  1.6  christos 			(*db->close)(db);
     79  1.6  christos 		} else
     80  1.6  christos 			(void)fclose((FILE *)sd->db);
     81  1.6  christos 		sd->db = NULL;
     82  1.1  christos 	}
     83  1.6  christos 	sd->flags &= ~_SV_STAYOPEN;
     84  1.6  christos }
     85  1.6  christos 
     86  1.6  christos 
     87  1.6  christos int
     88  1.6  christos _servent_getline(struct servent_data *sd)
     89  1.6  christos {
     90  1.2  christos 	if (sd->line) {
     91  1.2  christos 		free(sd->line);
     92  1.2  christos 		sd->line = NULL;
     93  1.2  christos 	}
     94  1.6  christos 	if (sd->flags & _SV_DB) {
     95  1.6  christos 		DB *db = sd->db;
     96  1.6  christos 		DBT key, data;
     97  1.6  christos 		int flags  = (sd->flags & _SV_FIRST) ? R_FIRST : R_NEXT;
     98  1.6  christos 
     99  1.6  christos 		while ((*db->seq)(db, &key, &data, flags) == 0) {
    100  1.6  christos 			flags = R_NEXT;
    101  1.6  christos 			switch (((u_char *)key.data)[0]) {
    102  1.6  christos 			case (u_char)'\377':
    103  1.6  christos 			case (u_char)'\376':
    104  1.6  christos 				continue;
    105  1.6  christos 			default:
    106  1.6  christos 				break;
    107  1.6  christos 			}
    108  1.6  christos 			sd->line = strdup(data.data);
    109  1.6  christos 			break;
    110  1.6  christos 		}
    111  1.6  christos 	} else {
    112  1.6  christos 		if (sd->flags & _SV_FIRST)
    113  1.6  christos 			(void)rewind((FILE *)sd->db);
    114  1.6  christos 		sd->line = fparseln((FILE *)sd->db, NULL, NULL, NULL,
    115  1.6  christos 		    FPARSELN_UNESCALL);
    116  1.6  christos 	}
    117  1.6  christos 	sd->flags &= ~_SV_FIRST;
    118  1.6  christos 	return sd->line == NULL ? -1 : 0;
    119  1.1  christos }
    120  1.1  christos 
    121  1.6  christos 
    122  1.1  christos struct servent *
    123  1.6  christos _servent_parseline(struct servent_data *sd, struct servent *sp)
    124  1.1  christos {
    125  1.1  christos 	size_t i = 0;
    126  1.1  christos 	int oerrno;
    127  1.6  christos 	char *p, *cp, **q;
    128  1.1  christos 
    129  1.6  christos 	if (sd->line == NULL)
    130  1.1  christos 		return NULL;
    131  1.1  christos 
    132  1.6  christos 	sp->s_name = p = sd->line;
    133  1.6  christos 	p = strpbrk(p, " \t");
    134  1.6  christos 	if (p == NULL)
    135  1.6  christos 		return NULL;
    136  1.6  christos 	*p++ = '\0';
    137  1.6  christos 	while (*p == ' ' || *p == '\t')
    138  1.6  christos 		p++;
    139  1.6  christos 	cp = strpbrk(p, ",/");
    140  1.6  christos 	if (cp == NULL)
    141  1.6  christos 		return NULL;
    142  1.6  christos 	*cp++ = '\0';
    143  1.6  christos 	sp->s_port = htons((u_short)atoi(p));
    144  1.6  christos 	sp->s_proto = cp;
    145  1.6  christos 	if (sd->aliases == NULL) {
    146  1.6  christos 		sd->maxaliases = 10;
    147  1.6  christos 		sd->aliases = malloc(sd->maxaliases * sizeof(char *));
    148  1.6  christos 		if (sd->aliases == NULL) {
    149  1.6  christos 			oerrno = errno;
    150  1.6  christos 			endservent_r(sd);
    151  1.6  christos 			errno = oerrno;
    152  1.1  christos 			return NULL;
    153  1.6  christos 		}
    154  1.6  christos 	}
    155  1.6  christos 	q = sp->s_aliases = sd->aliases;
    156  1.6  christos 	cp = strpbrk(cp, " \t");
    157  1.6  christos 	if (cp != NULL)
    158  1.6  christos 		*cp++ = '\0';
    159  1.6  christos 	while (cp && *cp) {
    160  1.6  christos 		if (*cp == ' ' || *cp == '\t') {
    161  1.6  christos 			cp++;
    162  1.1  christos 			continue;
    163  1.6  christos 		}
    164  1.6  christos 		if (i == sd->maxaliases - 2) {
    165  1.6  christos 			sd->maxaliases *= 2;
    166  1.6  christos 			q = realloc(q,
    167  1.6  christos 			    sd->maxaliases * sizeof(char *));
    168  1.6  christos 			if (q == NULL) {
    169  1.1  christos 				oerrno = errno;
    170  1.1  christos 				endservent_r(sd);
    171  1.1  christos 				errno = oerrno;
    172  1.1  christos 				return NULL;
    173  1.1  christos 			}
    174  1.6  christos 			sp->s_aliases = sd->aliases = q;
    175  1.1  christos 		}
    176  1.6  christos 		q[i++] = cp;
    177  1.1  christos 		cp = strpbrk(cp, " \t");
    178  1.1  christos 		if (cp != NULL)
    179  1.1  christos 			*cp++ = '\0';
    180  1.6  christos 	}
    181  1.6  christos 	q[i] = NULL;
    182  1.6  christos 	return sp;
    183  1.6  christos }
    184  1.6  christos 
    185  1.6  christos void
    186  1.6  christos setservent_r(int f, struct servent_data *sd)
    187  1.6  christos {
    188  1.6  christos 	(void)_servent_open(sd);
    189  1.6  christos 	sd->flags |= f ? _SV_STAYOPEN : 0;
    190  1.6  christos }
    191  1.6  christos 
    192  1.6  christos void
    193  1.6  christos endservent_r(struct servent_data *sd)
    194  1.6  christos {
    195  1.6  christos 	_servent_close(sd);
    196  1.6  christos 	if (sd->aliases) {
    197  1.6  christos 		free(sd->aliases);
    198  1.6  christos 		sd->aliases = NULL;
    199  1.6  christos 		sd->maxaliases = 0;
    200  1.6  christos 	}
    201  1.6  christos 	if (sd->line) {
    202  1.6  christos 		free(sd->line);
    203  1.6  christos 		sd->line = NULL;
    204  1.6  christos 	}
    205  1.6  christos }
    206  1.6  christos 
    207  1.6  christos struct servent *
    208  1.6  christos getservent_r(struct servent *sp, struct servent_data *sd)
    209  1.6  christos {
    210  1.6  christos 	if (sd->db == NULL && _servent_open(sd) == -1)
    211  1.6  christos 		return NULL;
    212  1.6  christos 
    213  1.6  christos 	for (;;) {
    214  1.6  christos 		if (_servent_getline(sd) == -1)
    215  1.6  christos 			return NULL;
    216  1.6  christos 		if (_servent_parseline(sd, sp) == NULL)
    217  1.6  christos 			continue;
    218  1.1  christos 		return sp;
    219  1.1  christos 	}
    220  1.1  christos }
    221