1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)getservent.c 8.1 (Berkeley) 6/4/93 30 * $DragonFly: src/lib/libc/net/getservent.c,v 1.7 2005/11/13 02:04:47 swildner Exp $ 31 */ 32 33 #include <sys/types.h> 34 #include <sys/socket.h> 35 #include <netdb.h> 36 #include <stdio.h> 37 #include <string.h> 38 #include <stdlib.h> 39 #ifdef YP 40 #include <rpc/rpc.h> 41 #include <rpcsvc/yp_prot.h> 42 #include <rpcsvc/ypclnt.h> 43 static int serv_stepping_yp = 0; 44 extern int _yp_check ( char ** ); 45 #endif 46 47 48 #define MAXALIASES 35 49 50 static FILE *servf = NULL; 51 static char line[BUFSIZ+1]; 52 static struct servent serv; 53 static char *serv_aliases[MAXALIASES]; 54 int _serv_stayopen; 55 56 #ifdef YP 57 char *___getservbyname_yp = NULL; 58 char *___getservbyproto_yp = NULL; 59 int ___getservbyport_yp = 0; 60 static char *yp_domain = NULL; 61 62 static int 63 _getservbyport_yp(char *line) 64 { 65 char *result; 66 int resultlen; 67 char buf[YPMAXRECORD + 2]; 68 int rv; 69 70 snprintf(buf, sizeof(buf), "%d/%s", ntohs(___getservbyport_yp), 71 ___getservbyproto_yp); 72 73 ___getservbyport_yp = 0; 74 ___getservbyproto_yp = NULL; 75 76 if(!yp_domain) { 77 if(yp_get_default_domain(&yp_domain)) 78 return (0); 79 } 80 81 /* 82 * We have to be a little flexible here. Ideally you're supposed 83 * to have both a services.byname and a services.byport map, but 84 * some systems have only services.byname. FreeBSD cheats a little 85 * by putting the services.byport information in the same map as 86 * services.byname so that either case will work. We allow for both 87 * possibilities here: if there is no services.byport map, we try 88 * services.byname instead. 89 */ 90 if ((rv = yp_match(yp_domain, "services.byport", buf, strlen(buf), 91 &result, &resultlen))) { 92 if (rv == YPERR_MAP) { 93 if (yp_match(yp_domain, "services.byname", buf, 94 strlen(buf), &result, &resultlen)) 95 return(0); 96 } else 97 return(0); 98 } 99 100 /* getservent() expects lines terminated with \n -- make it happy */ 101 snprintf(line, BUFSIZ, "%.*s\n", resultlen, result); 102 103 free(result); 104 return(1); 105 } 106 107 static int 108 _getservbyname_yp(char *line) 109 { 110 char *result; 111 int resultlen; 112 char buf[YPMAXRECORD + 2]; 113 114 if(!yp_domain) { 115 if(yp_get_default_domain(&yp_domain)) 116 return (0); 117 } 118 119 snprintf(buf, sizeof(buf), "%s/%s", ___getservbyname_yp, 120 ___getservbyproto_yp); 121 122 ___getservbyname_yp = 0; 123 ___getservbyproto_yp = NULL; 124 125 if (yp_match(yp_domain, "services.byname", buf, strlen(buf), 126 &result, &resultlen)) { 127 return(0); 128 } 129 130 /* getservent() expects lines terminated with \n -- make it happy */ 131 snprintf(line, BUFSIZ, "%.*s\n", resultlen, result); 132 133 free(result); 134 return(1); 135 } 136 137 static int 138 _getservent_yp(char *line) 139 { 140 static char *key = NULL; 141 static int keylen; 142 char *lastkey, *result; 143 int resultlen; 144 int rv; 145 146 if(!yp_domain) { 147 if(yp_get_default_domain(&yp_domain)) 148 return (0); 149 } 150 151 if (!serv_stepping_yp) { 152 if (key) 153 free(key); 154 if ((rv = yp_first(yp_domain, "services.byname", &key, &keylen, 155 &result, &resultlen))) { 156 serv_stepping_yp = 0; 157 return(0); 158 } 159 serv_stepping_yp = 1; 160 } else { 161 lastkey = key; 162 rv = yp_next(yp_domain, "services.byname", key, keylen, &key, 163 &keylen, &result, &resultlen); 164 free(lastkey); 165 if (rv) { 166 serv_stepping_yp = 0; 167 return (0); 168 } 169 } 170 171 /* getservent() expects lines terminated with \n -- make it happy */ 172 snprintf(line, BUFSIZ, "%.*s\n", resultlen, result); 173 174 free(result); 175 176 return(1); 177 } 178 #endif 179 180 void 181 setservent(int f) 182 { 183 if (servf == NULL) 184 servf = fopen(_PATH_SERVICES, "r" ); 185 else 186 rewind(servf); 187 _serv_stayopen |= f; 188 } 189 190 void 191 endservent(void) 192 { 193 if (servf) { 194 fclose(servf); 195 servf = NULL; 196 } 197 _serv_stayopen = 0; 198 } 199 200 struct servent * 201 getservent(void) 202 { 203 char *p; 204 char *cp, **q; 205 206 #ifdef YP 207 if (serv_stepping_yp && _getservent_yp(line)) { 208 p = (char *)&line; 209 goto unpack; 210 } 211 tryagain: 212 #endif 213 if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL) 214 return (NULL); 215 again: 216 if ((p = fgets(line, BUFSIZ, servf)) == NULL) 217 return (NULL); 218 #ifdef YP 219 if (*p == '+' && _yp_check(NULL)) { 220 if (___getservbyname_yp != NULL) { 221 if (!_getservbyname_yp(line)) 222 goto tryagain; 223 } 224 else if (___getservbyport_yp != 0) { 225 if (!_getservbyport_yp(line)) 226 goto tryagain; 227 } 228 else if (!_getservent_yp(line)) 229 goto tryagain; 230 } 231 unpack: 232 #endif 233 if (*p == '#') 234 goto again; 235 cp = strpbrk(p, "#\n"); 236 if (cp == NULL) 237 goto again; 238 *cp = '\0'; 239 serv.s_name = p; 240 p = strpbrk(p, " \t"); 241 if (p == NULL) 242 goto again; 243 *p++ = '\0'; 244 while (*p == ' ' || *p == '\t') 245 p++; 246 cp = strpbrk(p, ",/"); 247 if (cp == NULL) 248 goto again; 249 *cp++ = '\0'; 250 serv.s_port = htons((u_short)atoi(p)); 251 serv.s_proto = cp; 252 q = serv.s_aliases = serv_aliases; 253 cp = strpbrk(cp, " \t"); 254 if (cp != NULL) 255 *cp++ = '\0'; 256 while (cp && *cp) { 257 if (*cp == ' ' || *cp == '\t') { 258 cp++; 259 continue; 260 } 261 if (q < &serv_aliases[MAXALIASES - 1]) 262 *q++ = cp; 263 cp = strpbrk(cp, " \t"); 264 if (cp != NULL) 265 *cp++ = '\0'; 266 } 267 *q = NULL; 268 return (&serv); 269 } 270