1 1.1 christos /* $NetBSD: lcl_sv.c,v 1.1.1.2 2012/09/09 16:07:52 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Copyright (c) 1989, 1993, 1995 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. All advertising materials mentioning features or use of this software 16 1.1 christos * must display the following acknowledgement: 17 1.1 christos * This product includes software developed by the University of 18 1.1 christos * California, Berkeley and its contributors. 19 1.1 christos * 4. Neither the name of the University nor the names of its contributors 20 1.1 christos * may be used to endorse or promote products derived from this software 21 1.1 christos * without specific prior written permission. 22 1.1 christos * 23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 1.1 christos * SUCH DAMAGE. 34 1.1 christos */ 35 1.1 christos 36 1.1 christos /* 37 1.1 christos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 38 1.1 christos * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 39 1.1 christos * 40 1.1 christos * Permission to use, copy, modify, and distribute this software for any 41 1.1 christos * purpose with or without fee is hereby granted, provided that the above 42 1.1 christos * copyright notice and this permission notice appear in all copies. 43 1.1 christos * 44 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 45 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 46 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 47 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 48 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 49 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 50 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 51 1.1 christos */ 52 1.1 christos 53 1.1 christos #if defined(LIBC_SCCS) && !defined(lint) 54 1.1.1.2 christos static const char rcsid[] = "Id: lcl_sv.c,v 1.4 2005/04/27 04:56:31 sra Exp "; 55 1.1 christos #endif /* LIBC_SCCS and not lint */ 56 1.1 christos 57 1.1 christos /* extern */ 58 1.1 christos 59 1.1 christos #include "port_before.h" 60 1.1 christos 61 1.1 christos #include <sys/types.h> 62 1.1 christos #include <sys/socket.h> 63 1.1 christos #include <netinet/in.h> 64 1.1 christos #include <arpa/nameser.h> 65 1.1 christos #include <resolv.h> 66 1.1 christos 67 1.1 christos #ifdef IRS_LCL_SV_DB 68 1.1 christos #include <db.h> 69 1.1 christos #endif 70 1.1 christos #include <errno.h> 71 1.1 christos #include <fcntl.h> 72 1.1 christos #include <limits.h> 73 1.1 christos #include <stdio.h> 74 1.1 christos #include <string.h> 75 1.1 christos #include <stdlib.h> 76 1.1 christos 77 1.1 christos #include <irs.h> 78 1.1 christos #include <isc/memcluster.h> 79 1.1 christos 80 1.1 christos #include "port_after.h" 81 1.1 christos 82 1.1 christos #include "irs_p.h" 83 1.1 christos #include "lcl_p.h" 84 1.1 christos 85 1.1 christos #ifdef SPRINTF_CHAR 86 1.1 christos # define SPRINTF(x) strlen(sprintf/**/x) 87 1.1 christos #else 88 1.1 christos # define SPRINTF(x) ((size_t)sprintf x) 89 1.1 christos #endif 90 1.1 christos 91 1.1 christos /* Types */ 92 1.1 christos 93 1.1 christos struct pvt { 94 1.1 christos #ifdef IRS_LCL_SV_DB 95 1.1 christos DB * dbh; 96 1.1 christos int dbf; 97 1.1 christos #endif 98 1.1 christos struct lcl_sv sv; 99 1.1 christos }; 100 1.1 christos 101 1.1 christos /* Forward */ 102 1.1 christos 103 1.1 christos static void sv_close(struct irs_sv*); 104 1.1 christos static struct servent * sv_next(struct irs_sv *); 105 1.1 christos static struct servent * sv_byname(struct irs_sv *, const char *, 106 1.1 christos const char *); 107 1.1 christos static struct servent * sv_byport(struct irs_sv *, int, const char *); 108 1.1 christos static void sv_rewind(struct irs_sv *); 109 1.1 christos static void sv_minimize(struct irs_sv *); 110 1.1 christos /*global*/ struct servent * irs_lclsv_fnxt(struct lcl_sv *); 111 1.1 christos #ifdef IRS_LCL_SV_DB 112 1.1 christos static struct servent * sv_db_rec(struct lcl_sv *, DBT *, DBT *); 113 1.1 christos #endif 114 1.1 christos 115 1.1 christos /* Portability */ 116 1.1 christos 117 1.1 christos #ifndef SEEK_SET 118 1.1 christos # define SEEK_SET 0 119 1.1 christos #endif 120 1.1 christos 121 1.1 christos /* Public */ 122 1.1 christos 123 1.1 christos struct irs_sv * 124 1.1 christos irs_lcl_sv(struct irs_acc *this) { 125 1.1 christos struct irs_sv *sv; 126 1.1 christos struct pvt *pvt; 127 1.1 christos 128 1.1 christos UNUSED(this); 129 1.1 christos 130 1.1 christos if ((sv = memget(sizeof *sv)) == NULL) { 131 1.1 christos errno = ENOMEM; 132 1.1 christos return (NULL); 133 1.1 christos } 134 1.1 christos memset(sv, 0x5e, sizeof *sv); 135 1.1 christos if ((pvt = memget(sizeof *pvt)) == NULL) { 136 1.1 christos memput(sv, sizeof *sv); 137 1.1 christos errno = ENOMEM; 138 1.1 christos return (NULL); 139 1.1 christos } 140 1.1 christos memset(pvt, 0, sizeof *pvt); 141 1.1 christos sv->private = pvt; 142 1.1 christos sv->close = sv_close; 143 1.1 christos sv->next = sv_next; 144 1.1 christos sv->byname = sv_byname; 145 1.1 christos sv->byport = sv_byport; 146 1.1 christos sv->rewind = sv_rewind; 147 1.1 christos sv->minimize = sv_minimize; 148 1.1 christos sv->res_get = NULL; 149 1.1 christos sv->res_set = NULL; 150 1.1 christos #ifdef IRS_LCL_SV_DB 151 1.1 christos pvt->dbf = R_FIRST; 152 1.1 christos #endif 153 1.1 christos return (sv); 154 1.1 christos } 155 1.1 christos 156 1.1 christos /* Methods */ 157 1.1 christos 158 1.1 christos static void 159 1.1 christos sv_close(struct irs_sv *this) { 160 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 161 1.1 christos 162 1.1 christos #ifdef IRS_LCL_SV_DB 163 1.1 christos if (pvt->dbh != NULL) 164 1.1 christos (*pvt->dbh->close)(pvt->dbh); 165 1.1 christos #endif 166 1.1 christos if (pvt->sv.fp) 167 1.1 christos fclose(pvt->sv.fp); 168 1.1 christos memput(pvt, sizeof *pvt); 169 1.1 christos memput(this, sizeof *this); 170 1.1 christos } 171 1.1 christos 172 1.1 christos static struct servent * 173 1.1 christos sv_byname(struct irs_sv *this, const char *name, const char *proto) { 174 1.1 christos #ifdef IRS_LCL_SV_DB 175 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 176 1.1 christos #endif 177 1.1 christos struct servent *p; 178 1.1 christos char **cp; 179 1.1 christos 180 1.1 christos sv_rewind(this); 181 1.1 christos #ifdef IRS_LCL_SV_DB 182 1.1 christos if (pvt->dbh != NULL) { 183 1.1 christos DBT key, data; 184 1.1 christos 185 1.1 christos /* Note that (sizeof "/") == 2. */ 186 1.1 christos if ((strlen(name) + sizeof "/" + proto ? strlen(proto) : 0) 187 1.1 christos > sizeof pvt->sv.line) 188 1.1 christos goto try_local; 189 1.1 christos key.data = pvt->sv.line; 190 1.1 christos key.size = SPRINTF((pvt->sv.line, "%s/%s", name, 191 1.1 christos proto ? proto : "")) + 1; 192 1.1 christos if (proto != NULL) { 193 1.1 christos if ((*pvt->dbh->get)(pvt->dbh, &key, &data, 0) != 0) 194 1.1 christos return (NULL); 195 1.1 christos } else if ((*pvt->dbh->seq)(pvt->dbh, &key, &data, R_CURSOR) 196 1.1 christos != 0) 197 1.1 christos return (NULL); 198 1.1 christos return (sv_db_rec(&pvt->sv, &key, &data)); 199 1.1 christos } 200 1.1 christos try_local: 201 1.1 christos #endif 202 1.1 christos 203 1.1 christos while ((p = sv_next(this))) { 204 1.1 christos if (strcmp(name, p->s_name) == 0) 205 1.1 christos goto gotname; 206 1.1 christos for (cp = p->s_aliases; *cp; cp++) 207 1.1 christos if (strcmp(name, *cp) == 0) 208 1.1 christos goto gotname; 209 1.1 christos continue; 210 1.1 christos gotname: 211 1.1 christos if (proto == NULL || strcmp(p->s_proto, proto) == 0) 212 1.1 christos break; 213 1.1 christos } 214 1.1 christos return (p); 215 1.1 christos } 216 1.1 christos 217 1.1 christos static struct servent * 218 1.1 christos sv_byport(struct irs_sv *this, int port, const char *proto) { 219 1.1 christos #ifdef IRS_LCL_SV_DB 220 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 221 1.1 christos #endif 222 1.1 christos struct servent *p; 223 1.1 christos 224 1.1 christos sv_rewind(this); 225 1.1 christos #ifdef IRS_LCL_SV_DB 226 1.1 christos if (pvt->dbh != NULL) { 227 1.1 christos DBT key, data; 228 1.1 christos u_short *ports; 229 1.1 christos 230 1.1 christos ports = (u_short *)pvt->sv.line; 231 1.1 christos ports[0] = 0; 232 1.1 christos ports[1] = port; 233 1.1 christos key.data = ports; 234 1.1 christos key.size = sizeof(u_short) * 2; 235 1.1 christos if (proto && *proto) { 236 1.1 christos strncpy((char *)ports + key.size, proto, 237 1.1 christos BUFSIZ - key.size); 238 1.1 christos key.size += strlen((char *)ports + key.size) + 1; 239 1.1 christos if ((*pvt->dbh->get)(pvt->dbh, &key, &data, 0) != 0) 240 1.1 christos return (NULL); 241 1.1 christos } else { 242 1.1 christos if ((*pvt->dbh->seq)(pvt->dbh, &key, &data, R_CURSOR) 243 1.1 christos != 0) 244 1.1 christos return (NULL); 245 1.1 christos } 246 1.1 christos return (sv_db_rec(&pvt->sv, &key, &data)); 247 1.1 christos } 248 1.1 christos #endif 249 1.1 christos while ((p = sv_next(this))) { 250 1.1 christos if (p->s_port != port) 251 1.1 christos continue; 252 1.1 christos if (proto == NULL || strcmp(p->s_proto, proto) == 0) 253 1.1 christos break; 254 1.1 christos } 255 1.1 christos return (p); 256 1.1 christos } 257 1.1 christos 258 1.1 christos static void 259 1.1 christos sv_rewind(struct irs_sv *this) { 260 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 261 1.1 christos 262 1.1 christos if (pvt->sv.fp) { 263 1.1 christos if (fseek(pvt->sv.fp, 0L, SEEK_SET) == 0) 264 1.1 christos return; 265 1.1 christos (void)fclose(pvt->sv.fp); 266 1.1 christos pvt->sv.fp = NULL; 267 1.1 christos } 268 1.1 christos #ifdef IRS_LCL_SV_DB 269 1.1 christos pvt->dbf = R_FIRST; 270 1.1 christos if (pvt->dbh != NULL) 271 1.1 christos return; 272 1.1 christos pvt->dbh = dbopen(_PATH_SERVICES_DB, O_RDONLY,O_RDONLY,DB_BTREE, NULL); 273 1.1 christos if (pvt->dbh != NULL) { 274 1.1 christos if (fcntl((*pvt->dbh->fd)(pvt->dbh), F_SETFD, 1) < 0) { 275 1.1 christos (*pvt->dbh->close)(pvt->dbh); 276 1.1 christos pvt->dbh = NULL; 277 1.1 christos } 278 1.1 christos return; 279 1.1 christos } 280 1.1 christos #endif 281 1.1 christos if ((pvt->sv.fp = fopen(_PATH_SERVICES, "r")) == NULL) 282 1.1 christos return; 283 1.1 christos if (fcntl(fileno(pvt->sv.fp), F_SETFD, 1) < 0) { 284 1.1 christos (void)fclose(pvt->sv.fp); 285 1.1 christos pvt->sv.fp = NULL; 286 1.1 christos } 287 1.1 christos } 288 1.1 christos 289 1.1 christos static struct servent * 290 1.1 christos sv_next(struct irs_sv *this) { 291 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 292 1.1 christos 293 1.1 christos #ifdef IRS_LCL_SV_DB 294 1.1 christos if (pvt->dbh == NULL && pvt->sv.fp == NULL) 295 1.1 christos #else 296 1.1 christos if (pvt->sv.fp == NULL) 297 1.1 christos #endif 298 1.1 christos sv_rewind(this); 299 1.1 christos 300 1.1 christos #ifdef IRS_LCL_SV_DB 301 1.1 christos if (pvt->dbh != NULL) { 302 1.1 christos DBT key, data; 303 1.1 christos 304 1.1 christos while ((*pvt->dbh->seq)(pvt->dbh, &key, &data, pvt->dbf) == 0){ 305 1.1 christos pvt->dbf = R_NEXT; 306 1.1 christos if (((char *)key.data)[0]) 307 1.1 christos continue; 308 1.1 christos return (sv_db_rec(&pvt->sv, &key, &data)); 309 1.1 christos } 310 1.1 christos } 311 1.1 christos #endif 312 1.1 christos 313 1.1 christos if (pvt->sv.fp == NULL) 314 1.1 christos return (NULL); 315 1.1 christos return (irs_lclsv_fnxt(&pvt->sv)); 316 1.1 christos } 317 1.1 christos 318 1.1 christos static void 319 1.1 christos sv_minimize(struct irs_sv *this) { 320 1.1 christos struct pvt *pvt = (struct pvt *)this->private; 321 1.1 christos 322 1.1 christos #ifdef IRS_LCL_SV_DB 323 1.1 christos if (pvt->dbh != NULL) { 324 1.1 christos (*pvt->dbh->close)(pvt->dbh); 325 1.1 christos pvt->dbh = NULL; 326 1.1 christos } 327 1.1 christos #endif 328 1.1 christos if (pvt->sv.fp != NULL) { 329 1.1 christos (void)fclose(pvt->sv.fp); 330 1.1 christos pvt->sv.fp = NULL; 331 1.1 christos } 332 1.1 christos } 333 1.1 christos 334 1.1 christos /* Quasipublic. */ 335 1.1 christos 336 1.1 christos struct servent * 337 1.1 christos irs_lclsv_fnxt(struct lcl_sv *sv) { 338 1.1 christos char *p, *cp, **q; 339 1.1 christos 340 1.1 christos again: 341 1.1 christos if ((p = fgets(sv->line, BUFSIZ, sv->fp)) == NULL) 342 1.1 christos return (NULL); 343 1.1 christos if (*p == '#') 344 1.1 christos goto again; 345 1.1 christos sv->serv.s_name = p; 346 1.1 christos while (*p && *p != '\n' && *p != ' ' && *p != '\t' && *p != '#') 347 1.1 christos ++p; 348 1.1 christos if (*p == '\0' || *p == '#' || *p == '\n') 349 1.1 christos goto again; 350 1.1 christos *p++ = '\0'; 351 1.1 christos while (*p == ' ' || *p == '\t') 352 1.1 christos p++; 353 1.1 christos if (*p == '\0' || *p == '#' || *p == '\n') 354 1.1 christos goto again; 355 1.1 christos sv->serv.s_port = htons((u_short)strtol(p, &cp, 10)); 356 1.1 christos if (cp == p || (*cp != '/' && *cp != ',')) 357 1.1 christos goto again; 358 1.1 christos p = cp + 1; 359 1.1 christos sv->serv.s_proto = p; 360 1.1 christos 361 1.1 christos q = sv->serv.s_aliases = sv->serv_aliases; 362 1.1 christos 363 1.1 christos while (*p && *p != '\n' && *p != ' ' && *p != '\t' && *p != '#') 364 1.1 christos ++p; 365 1.1 christos 366 1.1 christos while (*p == ' ' || *p == '\t') { 367 1.1 christos *p++ = '\0'; 368 1.1 christos while (*p == ' ' || *p == '\t') 369 1.1 christos ++p; 370 1.1 christos if (*p == '\0' || *p == '#' || *p == '\n') 371 1.1 christos break; 372 1.1 christos if (q < &sv->serv_aliases[IRS_SV_MAXALIASES - 1]) 373 1.1 christos *q++ = p; 374 1.1 christos while (*p && *p != '\n' && *p != ' ' && *p != '\t' && *p != '#') 375 1.1 christos ++p; 376 1.1 christos } 377 1.1 christos 378 1.1 christos *p = '\0'; 379 1.1 christos *q = NULL; 380 1.1 christos return (&sv->serv); 381 1.1 christos } 382 1.1 christos 383 1.1 christos /* Private. */ 384 1.1 christos 385 1.1 christos #ifdef IRS_LCL_SV_DB 386 1.1 christos static struct servent * 387 1.1 christos sv_db_rec(struct lcl_sv *sv, DBT *key, DBT *data) { 388 1.1 christos char *p, **q; 389 1.1 christos int n; 390 1.1 christos 391 1.1 christos p = data->data; 392 1.1 christos p[data->size - 1] = '\0'; /*%< should be, but we depend on it */ 393 1.1 christos if (((char *)key->data)[0] == '\0') { 394 1.1 christos if (key->size < sizeof(u_short)*2 || data->size < 2) 395 1.1 christos return (NULL); 396 1.1 christos sv->serv.s_port = ((u_short *)key->data)[1]; 397 1.1 christos n = strlen(p) + 1; 398 1.1 christos if ((size_t)n > sizeof(sv->line)) { 399 1.1 christos n = sizeof(sv->line); 400 1.1 christos } 401 1.1 christos memcpy(sv->line, p, n); 402 1.1 christos sv->serv.s_name = sv->line; 403 1.1 christos if ((sv->serv.s_proto = strchr(sv->line, '/')) != NULL) 404 1.1 christos *(sv->serv.s_proto)++ = '\0'; 405 1.1 christos p += n; 406 1.1 christos data->size -= n; 407 1.1 christos } else { 408 1.1 christos if (data->size < sizeof(u_short) + 1) 409 1.1 christos return (NULL); 410 1.1 christos if (key->size > sizeof(sv->line)) 411 1.1 christos key->size = sizeof(sv->line); 412 1.1 christos ((char *)key->data)[key->size - 1] = '\0'; 413 1.1 christos memcpy(sv->line, key->data, key->size); 414 1.1 christos sv->serv.s_name = sv->line; 415 1.1 christos if ((sv->serv.s_proto = strchr(sv->line, '/')) != NULL) 416 1.1 christos *(sv->serv.s_proto)++ = '\0'; 417 1.1 christos sv->serv.s_port = *(u_short *)data->data; 418 1.1 christos p += sizeof(u_short); 419 1.1 christos data->size -= sizeof(u_short); 420 1.1 christos } 421 1.1 christos q = sv->serv.s_aliases = sv->serv_aliases; 422 1.1 christos while (data->size > 0 && q < &sv->serv_aliases[IRS_SV_MAXALIASES - 1]) { 423 1.1 christos 424 1.1 christos *q++ = p; 425 1.1 christos n = strlen(p) + 1; 426 1.1 christos data->size -= n; 427 1.1 christos p += n; 428 1.1 christos } 429 1.1 christos *q = NULL; 430 1.1 christos return (&sv->serv); 431 1.1 christos } 432 1.1 christos #endif 433 1.1 christos 434 1.1 christos /*! \file */ 435