getservent_r.c revision 1.8.8.1 1 1.8.8.1 matt /* $NetBSD: getservent_r.c,v 1.8.8.1 2008/01/09 01:34:12 matt 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.8.8.1 matt __RCSID("$NetBSD: getservent_r.c,v 1.8.8.1 2008/01/09 01:34:12 matt 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.8 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.8.8.1 matt
95 1.8.8.1 matt if (sd->db == NULL)
96 1.8.8.1 matt return -1;
97 1.8.8.1 matt
98 1.6 christos if (sd->flags & _SV_DB) {
99 1.6 christos DB *db = sd->db;
100 1.6 christos DBT key, data;
101 1.7 joerg u_int flags = (sd->flags & _SV_FIRST) ? R_FIRST : R_NEXT;
102 1.6 christos
103 1.6 christos while ((*db->seq)(db, &key, &data, flags) == 0) {
104 1.6 christos flags = R_NEXT;
105 1.6 christos switch (((u_char *)key.data)[0]) {
106 1.6 christos case (u_char)'\377':
107 1.6 christos case (u_char)'\376':
108 1.6 christos continue;
109 1.6 christos default:
110 1.6 christos break;
111 1.6 christos }
112 1.6 christos sd->line = strdup(data.data);
113 1.6 christos break;
114 1.6 christos }
115 1.6 christos } else {
116 1.6 christos if (sd->flags & _SV_FIRST)
117 1.6 christos (void)rewind((FILE *)sd->db);
118 1.6 christos sd->line = fparseln((FILE *)sd->db, NULL, NULL, NULL,
119 1.6 christos FPARSELN_UNESCALL);
120 1.6 christos }
121 1.6 christos sd->flags &= ~_SV_FIRST;
122 1.6 christos return sd->line == NULL ? -1 : 0;
123 1.1 christos }
124 1.1 christos
125 1.6 christos
126 1.1 christos struct servent *
127 1.6 christos _servent_parseline(struct servent_data *sd, struct servent *sp)
128 1.1 christos {
129 1.1 christos size_t i = 0;
130 1.1 christos int oerrno;
131 1.6 christos char *p, *cp, **q;
132 1.1 christos
133 1.6 christos if (sd->line == NULL)
134 1.1 christos return NULL;
135 1.1 christos
136 1.6 christos sp->s_name = p = sd->line;
137 1.6 christos p = strpbrk(p, " \t");
138 1.6 christos if (p == NULL)
139 1.6 christos return NULL;
140 1.6 christos *p++ = '\0';
141 1.6 christos while (*p == ' ' || *p == '\t')
142 1.6 christos p++;
143 1.6 christos cp = strpbrk(p, ",/");
144 1.6 christos if (cp == NULL)
145 1.6 christos return NULL;
146 1.6 christos *cp++ = '\0';
147 1.6 christos sp->s_port = htons((u_short)atoi(p));
148 1.6 christos sp->s_proto = cp;
149 1.6 christos if (sd->aliases == NULL) {
150 1.6 christos sd->maxaliases = 10;
151 1.6 christos sd->aliases = malloc(sd->maxaliases * sizeof(char *));
152 1.6 christos if (sd->aliases == NULL) {
153 1.6 christos oerrno = errno;
154 1.6 christos endservent_r(sd);
155 1.6 christos errno = oerrno;
156 1.1 christos return NULL;
157 1.6 christos }
158 1.6 christos }
159 1.6 christos q = sp->s_aliases = sd->aliases;
160 1.6 christos cp = strpbrk(cp, " \t");
161 1.6 christos if (cp != NULL)
162 1.6 christos *cp++ = '\0';
163 1.6 christos while (cp && *cp) {
164 1.6 christos if (*cp == ' ' || *cp == '\t') {
165 1.6 christos cp++;
166 1.1 christos continue;
167 1.6 christos }
168 1.6 christos if (i == sd->maxaliases - 2) {
169 1.6 christos sd->maxaliases *= 2;
170 1.6 christos q = realloc(q,
171 1.6 christos sd->maxaliases * sizeof(char *));
172 1.6 christos if (q == NULL) {
173 1.1 christos oerrno = errno;
174 1.1 christos endservent_r(sd);
175 1.1 christos errno = oerrno;
176 1.1 christos return NULL;
177 1.1 christos }
178 1.6 christos sp->s_aliases = sd->aliases = q;
179 1.1 christos }
180 1.6 christos q[i++] = cp;
181 1.1 christos cp = strpbrk(cp, " \t");
182 1.1 christos if (cp != NULL)
183 1.1 christos *cp++ = '\0';
184 1.6 christos }
185 1.6 christos q[i] = NULL;
186 1.6 christos return sp;
187 1.6 christos }
188 1.6 christos
189 1.6 christos void
190 1.6 christos setservent_r(int f, struct servent_data *sd)
191 1.6 christos {
192 1.6 christos (void)_servent_open(sd);
193 1.6 christos sd->flags |= f ? _SV_STAYOPEN : 0;
194 1.6 christos }
195 1.6 christos
196 1.6 christos void
197 1.6 christos endservent_r(struct servent_data *sd)
198 1.6 christos {
199 1.6 christos _servent_close(sd);
200 1.6 christos if (sd->aliases) {
201 1.6 christos free(sd->aliases);
202 1.6 christos sd->aliases = NULL;
203 1.6 christos sd->maxaliases = 0;
204 1.6 christos }
205 1.6 christos if (sd->line) {
206 1.6 christos free(sd->line);
207 1.6 christos sd->line = NULL;
208 1.6 christos }
209 1.6 christos }
210 1.6 christos
211 1.6 christos struct servent *
212 1.6 christos getservent_r(struct servent *sp, struct servent_data *sd)
213 1.6 christos {
214 1.6 christos if (sd->db == NULL && _servent_open(sd) == -1)
215 1.6 christos return NULL;
216 1.6 christos
217 1.6 christos for (;;) {
218 1.6 christos if (_servent_getline(sd) == -1)
219 1.6 christos return NULL;
220 1.6 christos if (_servent_parseline(sd, sp) == NULL)
221 1.6 christos continue;
222 1.1 christos return sp;
223 1.1 christos }
224 1.1 christos }
225