2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)getservent.c 8.1 (Berkeley) 6/4/93
36 #include <sys/types.h>
37 #include <sys/socket.h>
44 #include <rpcsvc/yp_prot.h>
45 #include <rpcsvc/ypclnt.h>
46 static int serv_stepping_yp = 0;
47 extern int _yp_check __P(( char ** ));
53 static FILE *servf = NULL;
54 static char line[BUFSIZ+1];
55 static struct servent serv;
56 static char *serv_aliases[MAXALIASES];
60 char *___getservbyname_yp = NULL;
61 char *___getservbyproto_yp = NULL;
62 int ___getservbyport_yp = 0;
63 static char *yp_domain = NULL;
66 _getservbyport_yp(line)
71 char buf[YPMAXRECORD + 2];
74 snprintf(buf, sizeof(buf), "%d/%s", ntohs(___getservbyport_yp),
75 ___getservbyproto_yp);
77 ___getservbyport_yp = 0;
78 ___getservbyproto_yp = NULL;
81 if(yp_get_default_domain(&yp_domain))
86 * We have to be a little flexible here. Ideally you're supposed
87 * to have both a services.byname and a services.byport map, but
88 * some systems have only services.byname. FreeBSD cheats a little
89 * by putting the services.byport information in the same map as
90 * services.byname so that either case will work. We allow for both
91 * possibilities here: if there is no services.byport map, we try
92 * services.byname instead.
94 if ((rv = yp_match(yp_domain, "services.byport", buf, strlen(buf),
95 &result, &resultlen))) {
96 if (rv == YPERR_MAP) {
97 if (yp_match(yp_domain, "services.byname", buf,
98 strlen(buf), &result, &resultlen))
104 /* getservent() expects lines terminated with \n -- make it happy */
105 snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
112 _getservbyname_yp(line)
117 char buf[YPMAXRECORD + 2];
120 if(yp_get_default_domain(&yp_domain))
124 snprintf(buf, sizeof(buf), "%s/%s", ___getservbyname_yp,
125 ___getservbyproto_yp);
127 ___getservbyname_yp = 0;
128 ___getservbyproto_yp = NULL;
130 if (yp_match(yp_domain, "services.byname", buf, strlen(buf),
131 &result, &resultlen)) {
135 /* getservent() expects lines terminated with \n -- make it happy */
136 snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
146 static char *key = NULL;
148 char *lastkey, *result;
153 if(yp_get_default_domain(&yp_domain))
157 if (!serv_stepping_yp) {
160 if ((rv = yp_first(yp_domain, "services.byname", &key, &keylen,
161 &result, &resultlen))) {
162 serv_stepping_yp = 0;
165 serv_stepping_yp = 1;
168 rv = yp_next(yp_domain, "services.byname", key, keylen, &key,
169 &keylen, &result, &resultlen);
172 serv_stepping_yp = 0;
177 /* getservent() expects lines terminated with \n -- make it happy */
178 snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
191 servf = fopen(_PATH_SERVICES, "r" );
211 register char *cp, **q;
214 if (serv_stepping_yp && _getservent_yp(line)) {
220 if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
223 if ((p = fgets(line, BUFSIZ, servf)) == NULL)
226 if (*p == '+' && _yp_check(NULL)) {
227 if (___getservbyname_yp != NULL) {
228 if (!_getservbyname_yp(line))
231 else if (___getservbyport_yp != 0) {
232 if (!_getservbyport_yp(line))
235 else if (!_getservent_yp(line))
242 cp = strpbrk(p, "#\n");
247 p = strpbrk(p, " \t");
251 while (*p == ' ' || *p == '\t')
253 cp = strpbrk(p, ",/");
257 serv.s_port = htons((u_short)atoi(p));
259 q = serv.s_aliases = serv_aliases;
260 cp = strpbrk(cp, " \t");
264 if (*cp == ' ' || *cp == '\t') {
268 if (q < &serv_aliases[MAXALIASES - 1])
270 cp = strpbrk(cp, " \t");