1 /* 2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 3 * unrestricted use provided that this legend is included on all tape 4 * media and as a part of the software program in whole or part. Users 5 * may copy or modify Sun RPC without charge, but are not authorized 6 * to license or distribute it to anyone else except as part of a product or 7 * program developed by the user or with the express written consent of 8 * Sun Microsystems, Inc. 9 * 10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 13 * 14 * Sun RPC is provided with no support and without any obligation on the 15 * part of Sun Microsystems, Inc. to assist in its use, correction, 16 * modification or enhancement. 17 * 18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 20 * OR ANY PART THEREOF. 21 * 22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 23 * or profits or other special, indirect and consequential damages, even if 24 * Sun has been advised of the possibility of such damages. 25 * 26 * Sun Microsystems, Inc. 27 * 2550 Garcia Avenue 28 * Mountain View, California 94043 29 * 30 * @(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro 31 * $FreeBSD: src/lib/libc/rpc/getrpcent.c,v 1.10 1999/08/28 00:00:39 peter Exp $ 32 * $DragonFly: src/lib/libc/rpc/getrpcent.c,v 1.4 2005/11/13 12:27:04 swildner Exp $ 33 */ 34 35 /* 36 * Copyright (c) 1984 by Sun Microsystems, Inc. 37 */ 38 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <sys/types.h> 42 #include <string.h> 43 #include <rpc/rpc.h> 44 #ifdef YP 45 #include <rpcsvc/yp_prot.h> 46 #include <rpcsvc/ypclnt.h> 47 #endif 48 49 /* 50 * Internet version. 51 */ 52 struct rpcdata { 53 FILE *rpcf; 54 int stayopen; 55 #define MAXALIASES 35 56 char *rpc_aliases[MAXALIASES]; 57 struct rpcent rpc; 58 char line[BUFSIZ+1]; 59 #ifdef YP 60 char *domain; 61 char *current; 62 int currentlen; 63 #endif 64 } *rpcdata; 65 66 #ifdef YP 67 static int __yp_nomap = 0; 68 extern int _yp_check(char **); 69 #endif /* YP */ 70 71 static struct rpcent *interpret(); 72 struct hostent *gethostent(); 73 char *inet_ntoa(); 74 75 static char RPCDB[] = "/etc/rpc"; 76 77 static struct rpcdata * 78 _rpcdata(void) 79 { 80 struct rpcdata *d = rpcdata; 81 82 if (d == 0) { 83 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata)); 84 rpcdata = d; 85 } 86 return (d); 87 } 88 89 struct rpcent * 90 getrpcbynumber(int number) 91 { 92 struct rpcdata *d = _rpcdata(); 93 struct rpcent *p; 94 #ifdef YP 95 int reason; 96 char adrstr[16]; 97 #endif 98 99 if (d == 0) 100 return (0); 101 #ifdef YP 102 if (!__yp_nomap && _yp_check(&d->domain)) { 103 sprintf(adrstr, "%d", number); 104 reason = yp_match(d->domain, "rpc.bynumber", adrstr, strlen(adrstr), 105 &d->current, &d->currentlen); 106 switch(reason) { 107 case 0: 108 break; 109 case YPERR_MAP: 110 __yp_nomap = 1; 111 goto no_yp; 112 break; 113 default: 114 return(0); 115 break; 116 } 117 d->current[d->currentlen] = '\0'; 118 p = interpret(d->current, d->currentlen); 119 free(d->current); 120 return p; 121 } 122 no_yp: 123 #endif /* YP */ 124 setrpcent(0); 125 while ((p = getrpcent())) { 126 if (p->r_number == number) 127 break; 128 } 129 endrpcent(); 130 return (p); 131 } 132 133 struct rpcent * 134 getrpcbyname(char *name) 135 { 136 struct rpcent *rpc = NULL; 137 char **rp; 138 139 setrpcent(0); 140 while ((rpc = getrpcent())) { 141 if (strcmp(rpc->r_name, name) == 0) 142 goto done; 143 for (rp = rpc->r_aliases; *rp != NULL; rp++) { 144 if (strcmp(*rp, name) == 0) 145 goto done; 146 } 147 } 148 done: 149 endrpcent(); 150 return (rpc); 151 } 152 153 void 154 setrpcent(int f) 155 { 156 struct rpcdata *d = _rpcdata(); 157 158 if (d == 0) 159 return; 160 #ifdef YP 161 if (!__yp_nomap && _yp_check(NULL)) { 162 if (d->current) 163 free(d->current); 164 d->current = NULL; 165 d->currentlen = 0; 166 return; 167 } 168 __yp_nomap = 0; 169 #endif /* YP */ 170 if (d->rpcf == NULL) 171 d->rpcf = fopen(RPCDB, "r"); 172 else 173 rewind(d->rpcf); 174 d->stayopen |= f; 175 } 176 177 void 178 endrpcent(void) 179 { 180 struct rpcdata *d = _rpcdata(); 181 182 if (d == 0) 183 return; 184 #ifdef YP 185 if (!__yp_nomap && _yp_check(NULL)) { 186 if (d->current && !d->stayopen) 187 free(d->current); 188 d->current = NULL; 189 d->currentlen = 0; 190 return; 191 } 192 __yp_nomap = 0; 193 #endif /* YP */ 194 if (d->rpcf && !d->stayopen) { 195 fclose(d->rpcf); 196 d->rpcf = NULL; 197 } 198 } 199 200 struct rpcent * 201 getrpcent(void) 202 { 203 struct rpcdata *d = _rpcdata(); 204 #ifdef YP 205 struct rpcent *hp; 206 int reason; 207 char *val = NULL; 208 int vallen; 209 #endif 210 211 if (d == 0) 212 return(NULL); 213 #ifdef YP 214 if (!__yp_nomap && _yp_check(&d->domain)) { 215 if (d->current == NULL && d->currentlen == 0) { 216 reason = yp_first(d->domain, "rpc.bynumber", 217 &d->current, &d->currentlen, 218 &val, &vallen); 219 } else { 220 reason = yp_next(d->domain, "rpc.bynumber", 221 d->current, d->currentlen, 222 &d->current, &d->currentlen, 223 &val, &vallen); 224 } 225 switch(reason) { 226 case 0: 227 break; 228 case YPERR_MAP: 229 __yp_nomap = 1; 230 goto no_yp; 231 break; 232 default: 233 return(0); 234 break; 235 } 236 val[vallen] = '\0'; 237 hp = interpret(val, vallen); 238 free(val); 239 return hp; 240 } 241 no_yp: 242 #endif /* YP */ 243 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL) 244 return (NULL); 245 /* -1 so there is room to append a \n below */ 246 if (fgets(d->line, BUFSIZ - 1, d->rpcf) == NULL) 247 return (NULL); 248 return (interpret(d->line, strlen(d->line))); 249 } 250 251 static struct rpcent * 252 interpret(char *val, int len) 253 { 254 struct rpcdata *d = _rpcdata(); 255 char *p; 256 char *cp, **q; 257 258 if (d == 0) 259 return (0); 260 strncpy(d->line, val, BUFSIZ); 261 d->line[BUFSIZ] = '\0'; 262 p = d->line; 263 p[len] = '\n'; 264 if (*p == '#') 265 return (getrpcent()); 266 cp = strpbrk(p, "#\n"); 267 if (cp == NULL) 268 return (getrpcent()); 269 *cp = '\0'; 270 cp = strpbrk(p, " \t"); 271 if (cp == NULL) 272 return (getrpcent()); 273 *cp++ = '\0'; 274 /* THIS STUFF IS INTERNET SPECIFIC */ 275 d->rpc.r_name = d->line; 276 while (*cp == ' ' || *cp == '\t') 277 cp++; 278 d->rpc.r_number = atoi(cp); 279 q = d->rpc.r_aliases = d->rpc_aliases; 280 cp = strpbrk(cp, " \t"); 281 if (cp != NULL) 282 *cp++ = '\0'; 283 while (cp && *cp) { 284 if (*cp == ' ' || *cp == '\t') { 285 cp++; 286 continue; 287 } 288 if (q < &(d->rpc_aliases[MAXALIASES - 1])) 289 *q++ = cp; 290 cp = strpbrk(cp, " \t"); 291 if (cp != NULL) 292 *cp++ = '\0'; 293 } 294 *q = NULL; 295 return (&d->rpc); 296 } 297 298