nis_sv.c revision 1.1.1.2 1 1.1 christos /* $NetBSD: nis_sv.c,v 1.1.1.2 2012/09/09 16:07:51 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos * Copyright (c) 1996,1999 by Internet Software Consortium.
6 1.1 christos *
7 1.1 christos * Permission to use, copy, modify, and 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
12 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 1.1 christos * OF OR IN CONNECTION WITH THE USE OR 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: nis_sv.c,v 1.4 2005/04/27 04:56:34 sra 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 #ifndef WANT_IRS_NIS
29 1.1 christos static int __bind_irs_nis_unneeded;
30 1.1 christos #else
31 1.1 christos
32 1.1 christos #include <sys/types.h>
33 1.1 christos #include <netinet/in.h>
34 1.1 christos #include <arpa/nameser.h>
35 1.1 christos #include <resolv.h>
36 1.1 christos #include <sys/socket.h>
37 1.1 christos #ifdef T_NULL
38 1.1 christos #undef T_NULL /* Silence re-definition warning of T_NULL. */
39 1.1 christos #endif
40 1.1 christos #include <rpc/rpc.h>
41 1.1 christos #include <rpc/xdr.h>
42 1.1 christos #include <rpcsvc/yp_prot.h>
43 1.1 christos #include <rpcsvc/ypclnt.h>
44 1.1 christos
45 1.1 christos #include <ctype.h>
46 1.1 christos #include <errno.h>
47 1.1 christos #include <stdio.h>
48 1.1 christos #include <stdlib.h>
49 1.1 christos #include <string.h>
50 1.1 christos
51 1.1 christos #include <isc/memcluster.h>
52 1.1 christos #include <irs.h>
53 1.1 christos
54 1.1 christos #include "port_after.h"
55 1.1 christos
56 1.1 christos #include "irs_p.h"
57 1.1 christos #include "nis_p.h"
58 1.1 christos
59 1.1 christos /* Definitions */
60 1.1 christos
61 1.1 christos struct pvt {
62 1.1 christos int needrewind;
63 1.1 christos char * nis_domain;
64 1.1 christos char * curkey_data;
65 1.1 christos int curkey_len;
66 1.1 christos char * curval_data;
67 1.1 christos int curval_len;
68 1.1 christos char line[BUFSIZ+1];
69 1.1 christos struct servent serv;
70 1.1 christos char * svbuf;
71 1.1 christos };
72 1.1 christos
73 1.1 christos enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 };
74 1.1 christos
75 1.1 christos static /*const*/ char services_byname[] = "services.byname";
76 1.1 christos
77 1.1 christos /* Forward */
78 1.1 christos
79 1.1 christos static void sv_close(struct irs_sv*);
80 1.1 christos static struct servent * sv_next(struct irs_sv *);
81 1.1 christos static struct servent * sv_byname(struct irs_sv *, const char *,
82 1.1 christos const char *);
83 1.1 christos static struct servent * sv_byport(struct irs_sv *, int, const char *);
84 1.1 christos static void sv_rewind(struct irs_sv *);
85 1.1 christos static void sv_minimize(struct irs_sv *);
86 1.1 christos
87 1.1 christos static struct servent * makeservent(struct irs_sv *this);
88 1.1 christos static void nisfree(struct pvt *, enum do_what);
89 1.1 christos
90 1.1 christos /* Public */
91 1.1 christos
92 1.1 christos struct irs_sv *
93 1.1 christos irs_nis_sv(struct irs_acc *this) {
94 1.1 christos struct irs_sv *sv;
95 1.1 christos struct pvt *pvt;
96 1.1 christos
97 1.1 christos if (!(sv = memget(sizeof *sv))) {
98 1.1 christos errno = ENOMEM;
99 1.1 christos return (NULL);
100 1.1 christos }
101 1.1 christos memset(sv, 0x5e, sizeof *sv);
102 1.1 christos if (!(pvt = memget(sizeof *pvt))) {
103 1.1 christos memput(sv, sizeof *sv);
104 1.1 christos errno = ENOMEM;
105 1.1 christos return (NULL);
106 1.1 christos }
107 1.1 christos memset(pvt, 0, sizeof *pvt);
108 1.1 christos pvt->needrewind = 1;
109 1.1 christos pvt->nis_domain = ((struct nis_p *)this->private)->domain;
110 1.1 christos sv->private = pvt;
111 1.1 christos sv->close = sv_close;
112 1.1 christos sv->next = sv_next;
113 1.1 christos sv->byname = sv_byname;
114 1.1 christos sv->byport = sv_byport;
115 1.1 christos sv->rewind = sv_rewind;
116 1.1 christos sv->minimize = sv_minimize;
117 1.1 christos sv->res_get = NULL;
118 1.1 christos sv->res_set = NULL;
119 1.1 christos return (sv);
120 1.1 christos }
121 1.1 christos
122 1.1 christos /* Methods */
123 1.1 christos
124 1.1 christos static void
125 1.1 christos sv_close(struct irs_sv *this) {
126 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
127 1.1 christos
128 1.1 christos nisfree(pvt, do_all);
129 1.1 christos if (pvt->serv.s_aliases)
130 1.1 christos free(pvt->serv.s_aliases);
131 1.1 christos if (pvt->svbuf)
132 1.1 christos free(pvt->svbuf);
133 1.1 christos memput(pvt, sizeof *pvt);
134 1.1 christos memput(this, sizeof *this);
135 1.1 christos }
136 1.1 christos
137 1.1 christos static struct servent *
138 1.1 christos sv_byname(struct irs_sv *this, const char *name, const char *proto) {
139 1.1 christos struct servent *serv;
140 1.1 christos char **sap;
141 1.1 christos
142 1.1 christos sv_rewind(this);
143 1.1 christos while ((serv = sv_next(this)) != NULL) {
144 1.1 christos if (proto != NULL && strcmp(proto, serv->s_proto))
145 1.1 christos continue;
146 1.1 christos if (!strcmp(name, serv->s_name))
147 1.1 christos break;
148 1.1 christos for (sap = serv->s_aliases; sap && *sap; sap++)
149 1.1 christos if (!strcmp(name, *sap))
150 1.1 christos break;
151 1.1 christos }
152 1.1 christos return (serv);
153 1.1 christos }
154 1.1 christos
155 1.1 christos static struct servent *
156 1.1 christos sv_byport(struct irs_sv *this, int port, const char *proto) {
157 1.1 christos struct servent *serv;
158 1.1 christos
159 1.1 christos sv_rewind(this);
160 1.1 christos while ((serv = sv_next(this)) != NULL) {
161 1.1 christos if (proto != NULL && strcmp(proto, serv->s_proto))
162 1.1 christos continue;
163 1.1 christos if (serv->s_port == port)
164 1.1 christos break;
165 1.1 christos }
166 1.1 christos return (serv);
167 1.1 christos }
168 1.1 christos
169 1.1 christos static void
170 1.1 christos sv_rewind(struct irs_sv *this) {
171 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
172 1.1 christos
173 1.1 christos pvt->needrewind = 1;
174 1.1 christos }
175 1.1 christos
176 1.1 christos static struct servent *
177 1.1 christos sv_next(struct irs_sv *this) {
178 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
179 1.1 christos struct servent *rval;
180 1.1 christos int r;
181 1.1 christos
182 1.1 christos do {
183 1.1 christos if (pvt->needrewind) {
184 1.1 christos nisfree(pvt, do_all);
185 1.1 christos r = yp_first(pvt->nis_domain, services_byname,
186 1.1 christos &pvt->curkey_data, &pvt->curkey_len,
187 1.1 christos &pvt->curval_data, &pvt->curval_len);
188 1.1 christos pvt->needrewind = 0;
189 1.1 christos } else {
190 1.1 christos char *newkey_data;
191 1.1 christos int newkey_len;
192 1.1 christos
193 1.1 christos nisfree(pvt, do_val);
194 1.1 christos r = yp_next(pvt->nis_domain, services_byname,
195 1.1 christos pvt->curkey_data, pvt->curkey_len,
196 1.1 christos &newkey_data, &newkey_len,
197 1.1 christos &pvt->curval_data, &pvt->curval_len);
198 1.1 christos nisfree(pvt, do_key);
199 1.1 christos pvt->curkey_data = newkey_data;
200 1.1 christos pvt->curkey_len = newkey_len;
201 1.1 christos }
202 1.1 christos if (r != 0) {
203 1.1 christos errno = ENOENT;
204 1.1 christos return (NULL);
205 1.1 christos }
206 1.1 christos rval = makeservent(this);
207 1.1 christos } while (rval == NULL);
208 1.1 christos return (rval);
209 1.1 christos }
210 1.1 christos
211 1.1 christos static void
212 1.1 christos sv_minimize(struct irs_sv *this) {
213 1.1 christos UNUSED(this);
214 1.1 christos /* NOOP */
215 1.1 christos }
216 1.1 christos
217 1.1 christos /* Private */
218 1.1 christos
219 1.1 christos static struct servent *
220 1.1 christos makeservent(struct irs_sv *this) {
221 1.1 christos struct pvt *pvt = (struct pvt *)this->private;
222 1.1 christos static const char spaces[] = " \t";
223 1.1 christos char *p, **t;
224 1.1 christos int n, m;
225 1.1 christos
226 1.1 christos if (pvt->svbuf)
227 1.1 christos free(pvt->svbuf);
228 1.1 christos pvt->svbuf = pvt->curval_data;
229 1.1 christos pvt->curval_data = NULL;
230 1.1 christos
231 1.1 christos if (pvt->serv.s_aliases) {
232 1.1 christos free(pvt->serv.s_aliases);
233 1.1 christos pvt->serv.s_aliases = NULL;
234 1.1 christos }
235 1.1 christos
236 1.1 christos if ((p = strpbrk(pvt->svbuf, "#\n")))
237 1.1 christos *p = '\0';
238 1.1 christos
239 1.1 christos p = pvt->svbuf;
240 1.1 christos
241 1.1 christos pvt->serv.s_name = p;
242 1.1 christos p += strcspn(p, spaces);
243 1.1 christos if (!*p)
244 1.1 christos goto cleanup;
245 1.1 christos *p++ = '\0';
246 1.1 christos p += strspn(p, spaces);
247 1.1 christos
248 1.1 christos pvt->serv.s_port = htons((u_short) atoi(p));
249 1.1 christos pvt->serv.s_proto = NULL;
250 1.1 christos
251 1.1 christos while (*p && !isspace((unsigned char)*p))
252 1.1 christos if (*p++ == '/')
253 1.1 christos pvt->serv.s_proto = p;
254 1.1 christos if (!pvt->serv.s_proto)
255 1.1 christos goto cleanup;
256 1.1 christos if (*p) {
257 1.1 christos *p++ = '\0';
258 1.1 christos p += strspn(p, spaces);
259 1.1 christos }
260 1.1 christos
261 1.1 christos n = m = 0;
262 1.1 christos while (*p) {
263 1.1 christos if ((n + 1) >= m || !pvt->serv.s_aliases) {
264 1.1 christos m += 10;
265 1.1 christos t = realloc(pvt->serv.s_aliases, m * sizeof(char *));
266 1.1 christos if (!t) {
267 1.1 christos errno = ENOMEM;
268 1.1 christos goto cleanup;
269 1.1 christos }
270 1.1 christos pvt->serv.s_aliases = t;
271 1.1 christos }
272 1.1 christos pvt->serv.s_aliases[n++] = p;
273 1.1 christos p += strcspn(p, spaces);
274 1.1 christos if (!*p)
275 1.1 christos break;
276 1.1 christos *p++ = '\0';
277 1.1 christos p += strspn(p, spaces);
278 1.1 christos }
279 1.1 christos if (!pvt->serv.s_aliases)
280 1.1 christos pvt->serv.s_aliases = malloc(sizeof(char *));
281 1.1 christos if (!pvt->serv.s_aliases)
282 1.1 christos goto cleanup;
283 1.1 christos pvt->serv.s_aliases[n] = NULL;
284 1.1 christos return (&pvt->serv);
285 1.1 christos
286 1.1 christos cleanup:
287 1.1 christos if (pvt->serv.s_aliases) {
288 1.1 christos free(pvt->serv.s_aliases);
289 1.1 christos pvt->serv.s_aliases = NULL;
290 1.1 christos }
291 1.1 christos if (pvt->svbuf) {
292 1.1 christos free(pvt->svbuf);
293 1.1 christos pvt->svbuf = NULL;
294 1.1 christos }
295 1.1 christos return (NULL);
296 1.1 christos }
297 1.1 christos
298 1.1 christos static void
299 1.1 christos nisfree(struct pvt *pvt, enum do_what do_what) {
300 1.1 christos if ((do_what & do_key) && pvt->curkey_data) {
301 1.1 christos free(pvt->curkey_data);
302 1.1 christos pvt->curkey_data = NULL;
303 1.1 christos }
304 1.1 christos if ((do_what & do_val) && pvt->curval_data) {
305 1.1 christos free(pvt->curval_data);
306 1.1 christos pvt->curval_data = NULL;
307 1.1 christos }
308 1.1 christos }
309 1.1 christos
310 1.1 christos #endif /*WANT_IRS_NIS*/
311 1.1 christos
312 1.1 christos /*! \file */
313