2006-09-14 23:58:48 +04:00
|
|
|
/* $NetBSD: getservent_r.c,v 1.8 2006/09/14 19:58:48 christos Exp $ */
|
2004-02-19 22:21:44 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)getservent.c 8.1 (Berkeley) 6/4/93";
|
|
|
|
#else
|
2006-09-14 23:58:48 +04:00
|
|
|
__RCSID("$NetBSD: getservent_r.c,v 1.8 2006/09/14 19:58:48 christos Exp $");
|
2004-02-19 22:21:44 +03:00
|
|
|
#endif
|
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
|
|
|
#include "namespace.h"
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2006-07-28 02:03:49 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <db.h>
|
2004-02-19 22:21:44 +03:00
|
|
|
|
2005-04-18 23:39:45 +04:00
|
|
|
#include "servent.h"
|
|
|
|
|
2004-02-19 22:21:44 +03:00
|
|
|
#ifdef __weak_alias
|
|
|
|
__weak_alias(endservent_r,_endservent_r)
|
|
|
|
__weak_alias(getservent_r,_getservent_r)
|
|
|
|
__weak_alias(setservent_r,_setservent_r)
|
|
|
|
#endif
|
|
|
|
|
2006-07-28 02:03:49 +04:00
|
|
|
int
|
|
|
|
_servent_open(struct servent_data *sd)
|
2004-02-19 22:21:44 +03:00
|
|
|
{
|
2006-09-14 23:58:48 +04:00
|
|
|
sd->flags |= _SV_FIRST;
|
2006-07-28 02:03:49 +04:00
|
|
|
if (sd->db == NULL) {
|
|
|
|
if ((sd->db = dbopen(_PATH_SERVICES_DB, O_RDONLY, 0,
|
|
|
|
DB_HASH, NULL)) != NULL)
|
|
|
|
sd->flags |= _SV_DB;
|
|
|
|
else
|
|
|
|
sd->db = fopen(_PATH_SERVICES, "r");
|
|
|
|
}
|
|
|
|
return sd->db ? 0 : -1;
|
2004-02-19 22:21:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-07-28 02:03:49 +04:00
|
|
|
_servent_close(struct servent_data *sd)
|
2004-02-19 22:21:44 +03:00
|
|
|
{
|
2006-07-28 02:03:49 +04:00
|
|
|
if (sd->db) {
|
|
|
|
if (sd->flags & _SV_DB) {
|
|
|
|
DB *db = sd->db;
|
|
|
|
(*db->close)(db);
|
|
|
|
} else
|
|
|
|
(void)fclose((FILE *)sd->db);
|
|
|
|
sd->db = NULL;
|
2004-02-19 22:21:44 +03:00
|
|
|
}
|
2006-07-28 02:03:49 +04:00
|
|
|
sd->flags &= ~_SV_STAYOPEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
_servent_getline(struct servent_data *sd)
|
|
|
|
{
|
2004-02-23 19:06:52 +03:00
|
|
|
if (sd->line) {
|
|
|
|
free(sd->line);
|
|
|
|
sd->line = NULL;
|
|
|
|
}
|
2006-07-28 02:03:49 +04:00
|
|
|
if (sd->flags & _SV_DB) {
|
|
|
|
DB *db = sd->db;
|
|
|
|
DBT key, data;
|
2006-07-28 05:24:24 +04:00
|
|
|
u_int flags = (sd->flags & _SV_FIRST) ? R_FIRST : R_NEXT;
|
2006-07-28 02:03:49 +04:00
|
|
|
|
|
|
|
while ((*db->seq)(db, &key, &data, flags) == 0) {
|
|
|
|
flags = R_NEXT;
|
|
|
|
switch (((u_char *)key.data)[0]) {
|
|
|
|
case (u_char)'\377':
|
|
|
|
case (u_char)'\376':
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sd->line = strdup(data.data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (sd->flags & _SV_FIRST)
|
|
|
|
(void)rewind((FILE *)sd->db);
|
|
|
|
sd->line = fparseln((FILE *)sd->db, NULL, NULL, NULL,
|
|
|
|
FPARSELN_UNESCALL);
|
|
|
|
}
|
|
|
|
sd->flags &= ~_SV_FIRST;
|
|
|
|
return sd->line == NULL ? -1 : 0;
|
2004-02-19 22:21:44 +03:00
|
|
|
}
|
|
|
|
|
2006-07-28 02:03:49 +04:00
|
|
|
|
2004-02-19 22:21:44 +03:00
|
|
|
struct servent *
|
2006-07-28 02:03:49 +04:00
|
|
|
_servent_parseline(struct servent_data *sd, struct servent *sp)
|
2004-02-19 22:21:44 +03:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
int oerrno;
|
2006-07-28 02:03:49 +04:00
|
|
|
char *p, *cp, **q;
|
2004-02-19 22:21:44 +03:00
|
|
|
|
2006-07-28 02:03:49 +04:00
|
|
|
if (sd->line == NULL)
|
2004-02-19 22:21:44 +03:00
|
|
|
return NULL;
|
|
|
|
|
2006-07-28 02:03:49 +04:00
|
|
|
sp->s_name = p = sd->line;
|
|
|
|
p = strpbrk(p, " \t");
|
|
|
|
if (p == NULL)
|
|
|
|
return NULL;
|
|
|
|
*p++ = '\0';
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
cp = strpbrk(p, ",/");
|
|
|
|
if (cp == NULL)
|
|
|
|
return NULL;
|
|
|
|
*cp++ = '\0';
|
|
|
|
sp->s_port = htons((u_short)atoi(p));
|
|
|
|
sp->s_proto = cp;
|
|
|
|
if (sd->aliases == NULL) {
|
|
|
|
sd->maxaliases = 10;
|
|
|
|
sd->aliases = malloc(sd->maxaliases * sizeof(char *));
|
|
|
|
if (sd->aliases == NULL) {
|
|
|
|
oerrno = errno;
|
|
|
|
endservent_r(sd);
|
|
|
|
errno = oerrno;
|
2004-02-19 22:21:44 +03:00
|
|
|
return NULL;
|
2006-07-28 02:03:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
q = sp->s_aliases = sd->aliases;
|
|
|
|
cp = strpbrk(cp, " \t");
|
|
|
|
if (cp != NULL)
|
2004-02-19 22:21:44 +03:00
|
|
|
*cp++ = '\0';
|
2006-07-28 02:03:49 +04:00
|
|
|
while (cp && *cp) {
|
|
|
|
if (*cp == ' ' || *cp == '\t') {
|
|
|
|
cp++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (i == sd->maxaliases - 2) {
|
|
|
|
sd->maxaliases *= 2;
|
|
|
|
q = realloc(q,
|
|
|
|
sd->maxaliases * sizeof(char *));
|
|
|
|
if (q == NULL) {
|
2004-02-19 22:21:44 +03:00
|
|
|
oerrno = errno;
|
|
|
|
endservent_r(sd);
|
|
|
|
errno = oerrno;
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-07-28 02:03:49 +04:00
|
|
|
sp->s_aliases = sd->aliases = q;
|
2004-02-19 22:21:44 +03:00
|
|
|
}
|
2006-07-28 02:03:49 +04:00
|
|
|
q[i++] = cp;
|
2004-02-19 22:21:44 +03:00
|
|
|
cp = strpbrk(cp, " \t");
|
|
|
|
if (cp != NULL)
|
|
|
|
*cp++ = '\0';
|
2006-07-28 02:03:49 +04:00
|
|
|
}
|
|
|
|
q[i] = NULL;
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
setservent_r(int f, struct servent_data *sd)
|
|
|
|
{
|
|
|
|
(void)_servent_open(sd);
|
|
|
|
sd->flags |= f ? _SV_STAYOPEN : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
endservent_r(struct servent_data *sd)
|
|
|
|
{
|
|
|
|
_servent_close(sd);
|
|
|
|
if (sd->aliases) {
|
|
|
|
free(sd->aliases);
|
|
|
|
sd->aliases = NULL;
|
|
|
|
sd->maxaliases = 0;
|
|
|
|
}
|
|
|
|
if (sd->line) {
|
|
|
|
free(sd->line);
|
|
|
|
sd->line = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct servent *
|
|
|
|
getservent_r(struct servent *sp, struct servent_data *sd)
|
|
|
|
{
|
|
|
|
if (sd->db == NULL && _servent_open(sd) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (_servent_getline(sd) == -1)
|
|
|
|
return NULL;
|
|
|
|
if (_servent_parseline(sd, sp) == NULL)
|
|
|
|
continue;
|
2004-02-19 22:21:44 +03:00
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
}
|