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.2 1995/05/30 05:41:21 rgrimes 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 #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() 79 { 80 register 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(number) 91 register int number; 92 { 93 register struct rpcdata *d = _rpcdata(); 94 register struct rpcent *p; 95 #ifdef YP 96 int reason; 97 char adrstr[16]; 98 #endif 99 100 if (d == 0) 101 return (0); 102 #ifdef YP 103 if (!__yp_nomap && _yp_check(&d->domain)) { 104 sprintf(adrstr, "%d", number); 105 reason = yp_match(d->domain, "rpc.bynumber", adrstr, strlen(adrstr), 106 &d->current, &d->currentlen); 107 switch(reason) { 108 case 0: 109 break; 110 case YPERR_MAP: 111 __yp_nomap = 1; 112 goto no_yp; 113 break; 114 default: 115 return(0); 116 break; 117 } 118 d->current[d->currentlen] = '\0'; 119 p = interpret(d->current, d->currentlen); 120 (void) free(d->current); 121 return p; 122 } 123 no_yp: 124 #endif /* YP */ 125 setrpcent(0); 126 while (p = getrpcent()) { 127 if (p->r_number == number) 128 break; 129 } 130 endrpcent(); 131 return (p); 132 } 133 134 struct rpcent * 135 getrpcbyname(name) 136 char *name; 137 { 138 struct rpcent *rpc; 139 char **rp; 140 141 setrpcent(0); 142 while (rpc = getrpcent()) { 143 if (strcmp(rpc->r_name, name) == 0) 144 return (rpc); 145 for (rp = rpc->r_aliases; *rp != NULL; rp++) { 146 if (strcmp(*rp, name) == 0) 147 return (rpc); 148 } 149 } 150 endrpcent(); 151 return (NULL); 152 } 153 154 void 155 setrpcent(f) 156 int f; 157 { 158 register struct rpcdata *d = _rpcdata(); 159 160 if (d == 0) 161 return; 162 #ifdef YP 163 if (!__yp_nomap && _yp_check(NULL)) { 164 if (d->current) 165 free(d->current); 166 d->current = NULL; 167 d->currentlen = 0; 168 return; 169 } 170 __yp_nomap = 0; 171 #endif /* YP */ 172 if (d->rpcf == NULL) 173 d->rpcf = fopen(RPCDB, "r"); 174 else 175 rewind(d->rpcf); 176 d->stayopen |= f; 177 } 178 179 void 180 endrpcent() 181 { 182 register struct rpcdata *d = _rpcdata(); 183 184 if (d == 0) 185 return; 186 #ifdef YP 187 if (!__yp_nomap && _yp_check(NULL)) { 188 if (d->current && !d->stayopen) 189 free(d->current); 190 d->current = NULL; 191 d->currentlen = 0; 192 return; 193 } 194 __yp_nomap = 0; 195 #endif /* YP */ 196 if (d->rpcf && !d->stayopen) { 197 fclose(d->rpcf); 198 d->rpcf = NULL; 199 } 200 } 201 202 struct rpcent * 203 getrpcent() 204 { 205 struct rpcent *hp; 206 int reason; 207 register struct rpcdata *d = _rpcdata(); 208 #ifdef YP 209 char *val = NULL; 210 int vallen; 211 #endif 212 213 if (d == 0) 214 return(NULL); 215 #ifdef YP 216 if (!__yp_nomap && _yp_check(&d->domain)) { 217 if (d->current == NULL && d->currentlen == 0) { 218 reason = yp_first(d->domain, "rpc.bynumber", 219 &d->current, &d->currentlen, 220 &val, &vallen); 221 } else { 222 reason = yp_next(d->domain, "rpc.bynumber", 223 d->current, d->currentlen, 224 &d->current, &d->currentlen, 225 &val, &vallen); 226 } 227 switch(reason) { 228 case 0: 229 break; 230 case YPERR_MAP: 231 __yp_nomap = 1; 232 goto no_yp; 233 break; 234 default: 235 return(0); 236 break; 237 } 238 val[vallen] = '\0'; 239 hp = interpret(val, vallen); 240 (void) free(val); 241 return hp; 242 } 243 no_yp: 244 #endif /* YP */ 245 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL) 246 return (NULL); 247 if (fgets(d->line, BUFSIZ, d->rpcf) == NULL) 248 return (NULL); 249 return (interpret(d->line, strlen(d->line))); 250 } 251 252 static struct rpcent * 253 interpret(val, len) 254 char *val; 255 int len; 256 { 257 register struct rpcdata *d = _rpcdata(); 258 char *p; 259 register char *cp, **q; 260 261 if (d == 0) 262 return (0); 263 (void) strncpy(d->line, val, len); 264 p = d->line; 265 d->line[len] = '\n'; 266 if (*p == '#') 267 return (getrpcent()); 268 cp = strpbrk(p, "#\n"); 269 if (cp == NULL) 270 return (getrpcent()); 271 *cp = '\0'; 272 cp = strpbrk(p, " \t"); 273 if (cp == NULL) 274 return (getrpcent()); 275 *cp++ = '\0'; 276 /* THIS STUFF IS INTERNET SPECIFIC */ 277 d->rpc.r_name = d->line; 278 while (*cp == ' ' || *cp == '\t') 279 cp++; 280 d->rpc.r_number = atoi(cp); 281 q = d->rpc.r_aliases = d->rpc_aliases; 282 cp = strpbrk(cp, " \t"); 283 if (cp != NULL) 284 *cp++ = '\0'; 285 while (cp && *cp) { 286 if (*cp == ' ' || *cp == '\t') { 287 cp++; 288 continue; 289 } 290 if (q < &(d->rpc_aliases[MAXALIASES - 1])) 291 *q++ = cp; 292 cp = strpbrk(cp, " \t"); 293 if (cp != NULL) 294 *cp++ = '\0'; 295 } 296 *q = NULL; 297 return (&d->rpc); 298 } 299 300