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 31 #if defined(LIBC_SCCS) && !defined(lint) 32 /*static char *sccsid = "from: @(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro";*/ 33 static char *rcsid = "$Id: getrpcent.c,v 1.6 1996/12/30 14:42:31 peter Exp $"; 34 #endif 35 36 /* 37 * Copyright (c) 1984 by Sun Microsystems, Inc. 38 */ 39 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <sys/types.h> 43 #include <string.h> 44 #include <rpc/rpc.h> 45 #ifdef YP 46 #include <rpcsvc/yp_prot.h> 47 #include <rpcsvc/ypclnt.h> 48 #endif 49 50 /* 51 * Internet version. 52 */ 53 struct rpcdata { 54 FILE *rpcf; 55 int stayopen; 56 #define MAXALIASES 35 57 char *rpc_aliases[MAXALIASES]; 58 struct rpcent rpc; 59 char line[BUFSIZ+1]; 60 #ifdef YP 61 char *domain; 62 char *current; 63 int currentlen; 64 #endif 65 } *rpcdata; 66 67 #ifdef YP 68 static int __yp_nomap = 0; 69 extern int _yp_check(char **); 70 #endif /* YP */ 71 72 static struct rpcent *interpret(); 73 struct hostent *gethostent(); 74 char *inet_ntoa(); 75 76 static char RPCDB[] = "/etc/rpc"; 77 78 static struct rpcdata * 79 _rpcdata() 80 { 81 register struct rpcdata *d = rpcdata; 82 83 if (d == 0) { 84 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata)); 85 rpcdata = d; 86 } 87 return (d); 88 } 89 90 struct rpcent * 91 getrpcbynumber(number) 92 register int number; 93 { 94 register struct rpcdata *d = _rpcdata(); 95 register struct rpcent *p; 96 #ifdef YP 97 int reason; 98 char adrstr[16]; 99 #endif 100 101 if (d == 0) 102 return (0); 103 #ifdef YP 104 if (!__yp_nomap && _yp_check(&d->domain)) { 105 sprintf(adrstr, "%d", number); 106 reason = yp_match(d->domain, "rpc.bynumber", adrstr, strlen(adrstr), 107 &d->current, &d->currentlen); 108 switch(reason) { 109 case 0: 110 break; 111 case YPERR_MAP: 112 __yp_nomap = 1; 113 goto no_yp; 114 break; 115 default: 116 return(0); 117 break; 118 } 119 d->current[d->currentlen] = '\0'; 120 p = interpret(d->current, d->currentlen); 121 (void) free(d->current); 122 return p; 123 } 124 no_yp: 125 #endif /* YP */ 126 setrpcent(0); 127 while ((p = getrpcent())) { 128 if (p->r_number == number) 129 break; 130 } 131 endrpcent(); 132 return (p); 133 } 134 135 struct rpcent * 136 getrpcbyname(name) 137 char *name; 138 { 139 struct rpcent *rpc = NULL; 140 char **rp; 141 142 setrpcent(0); 143 while ((rpc = getrpcent())) { 144 if (strcmp(rpc->r_name, name) == 0) 145 goto done; 146 for (rp = rpc->r_aliases; *rp != NULL; rp++) { 147 if (strcmp(*rp, name) == 0) 148 goto done; 149 } 150 } 151 done: 152 endrpcent(); 153 return (rpc); 154 } 155 156 void 157 setrpcent(f) 158 int f; 159 { 160 register struct rpcdata *d = _rpcdata(); 161 162 if (d == 0) 163 return; 164 #ifdef YP 165 if (!__yp_nomap && _yp_check(NULL)) { 166 if (d->current) 167 free(d->current); 168 d->current = NULL; 169 d->currentlen = 0; 170 return; 171 } 172 __yp_nomap = 0; 173 #endif /* YP */ 174 if (d->rpcf == NULL) 175 d->rpcf = fopen(RPCDB, "r"); 176 else 177 rewind(d->rpcf); 178 d->stayopen |= f; 179 } 180 181 void 182 endrpcent() 183 { 184 register struct rpcdata *d = _rpcdata(); 185 186 if (d == 0) 187 return; 188 #ifdef YP 189 if (!__yp_nomap && _yp_check(NULL)) { 190 if (d->current && !d->stayopen) 191 free(d->current); 192 d->current = NULL; 193 d->currentlen = 0; 194 return; 195 } 196 __yp_nomap = 0; 197 #endif /* YP */ 198 if (d->rpcf && !d->stayopen) { 199 fclose(d->rpcf); 200 d->rpcf = NULL; 201 } 202 } 203 204 struct rpcent * 205 getrpcent() 206 { 207 register struct rpcdata *d = _rpcdata(); 208 #ifdef YP 209 struct rpcent *hp; 210 int reason; 211 char *val = NULL; 212 int vallen; 213 #endif 214 215 if (d == 0) 216 return(NULL); 217 #ifdef YP 218 if (!__yp_nomap && _yp_check(&d->domain)) { 219 if (d->current == NULL && d->currentlen == 0) { 220 reason = yp_first(d->domain, "rpc.bynumber", 221 &d->current, &d->currentlen, 222 &val, &vallen); 223 } else { 224 reason = yp_next(d->domain, "rpc.bynumber", 225 d->current, d->currentlen, 226 &d->current, &d->currentlen, 227 &val, &vallen); 228 } 229 switch(reason) { 230 case 0: 231 break; 232 case YPERR_MAP: 233 __yp_nomap = 1; 234 goto no_yp; 235 break; 236 default: 237 return(0); 238 break; 239 } 240 val[vallen] = '\0'; 241 hp = interpret(val, vallen); 242 (void) free(val); 243 return hp; 244 } 245 no_yp: 246 #endif /* YP */ 247 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL) 248 return (NULL); 249 /* -1 so there is room to append a \n below */ 250 if (fgets(d->line, BUFSIZ - 1, d->rpcf) == NULL) 251 return (NULL); 252 return (interpret(d->line, strlen(d->line))); 253 } 254 255 static struct rpcent * 256 interpret(val, len) 257 char *val; 258 int len; 259 { 260 register struct rpcdata *d = _rpcdata(); 261 char *p; 262 register char *cp, **q; 263 264 if (d == 0) 265 return (0); 266 (void) strncpy(d->line, val, BUFSIZ); 267 d->line[BUFSIZ] = '\0'; 268 p = d->line; 269 p[len] = '\n'; 270 if (*p == '#') 271 return (getrpcent()); 272 cp = strpbrk(p, "#\n"); 273 if (cp == NULL) 274 return (getrpcent()); 275 *cp = '\0'; 276 cp = strpbrk(p, " \t"); 277 if (cp == NULL) 278 return (getrpcent()); 279 *cp++ = '\0'; 280 /* THIS STUFF IS INTERNET SPECIFIC */ 281 d->rpc.r_name = d->line; 282 while (*cp == ' ' || *cp == '\t') 283 cp++; 284 d->rpc.r_number = atoi(cp); 285 q = d->rpc.r_aliases = d->rpc_aliases; 286 cp = strpbrk(cp, " \t"); 287 if (cp != NULL) 288 *cp++ = '\0'; 289 while (cp && *cp) { 290 if (*cp == ' ' || *cp == '\t') { 291 cp++; 292 continue; 293 } 294 if (q < &(d->rpc_aliases[MAXALIASES - 1])) 295 *q++ = cp; 296 cp = strpbrk(cp, " \t"); 297 if (cp != NULL) 298 *cp++ = '\0'; 299 } 300 *q = NULL; 301 return (&d->rpc); 302 } 303 304