1 /* $OpenBSD: getrpcent.c,v 1.14 2007/02/20 01:47:03 ray Exp $ */ 2 /* 3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 4 * unrestricted use provided that this legend is included on all tape 5 * media and as a part of the software program in whole or part. Users 6 * may copy or modify Sun RPC without charge, but are not authorized 7 * to license or distribute it to anyone else except as part of a product or 8 * program developed by the user or with the express written consent of 9 * Sun Microsystems, Inc. 10 * 11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 14 * 15 * Sun RPC is provided with no support and without any obligation on the 16 * part of Sun Microsystems, Inc. to assist in its use, correction, 17 * modification or enhancement. 18 * 19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 21 * OR ANY PART THEREOF. 22 * 23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 24 * or profits or other special, indirect and consequential damages, even if 25 * Sun has been advised of the possibility of such damages. 26 * 27 * Sun Microsystems, Inc. 28 * 2550 Garcia Avenue 29 * Mountain View, California 94043 30 */ 31 32 /* 33 * Copyright (c) 1984 by Sun Microsystems, Inc. 34 */ 35 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <sys/types.h> 39 #include <string.h> 40 #include <rpc/rpc.h> 41 42 /* 43 * Internet version. 44 */ 45 struct rpcdata { 46 FILE *rpcf; 47 int stayopen; 48 #define MAXALIASES 35 49 char *rpc_aliases[MAXALIASES]; 50 struct rpcent rpc; 51 char line[BUFSIZ+1]; 52 } *rpcdata; 53 54 static struct rpcent *interpret(char *val, int len); 55 56 static char RPCDB[] = "/etc/rpc"; 57 58 static struct rpcdata * 59 _rpcdata(void) 60 { 61 struct rpcdata *d = rpcdata; 62 63 if (d == NULL) { 64 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata)); 65 rpcdata = d; 66 } 67 return (d); 68 } 69 70 struct rpcent * 71 getrpcbynumber(int number) 72 { 73 struct rpcdata *d = _rpcdata(); 74 struct rpcent *p; 75 76 if (d == NULL) 77 return (0); 78 setrpcent(0); 79 while ((p = getrpcent())) { 80 if (p->r_number == number) 81 break; 82 } 83 endrpcent(); 84 return (p); 85 } 86 87 struct rpcent * 88 getrpcbyname(char *name) 89 { 90 struct rpcent *rpc; 91 char **rp; 92 93 setrpcent(0); 94 while ((rpc = getrpcent())) { 95 if (strcmp(rpc->r_name, name) == 0) 96 goto done; 97 for (rp = rpc->r_aliases; *rp != NULL; rp++) { 98 if (strcmp(*rp, name) == 0) 99 goto done; 100 } 101 } 102 done: 103 endrpcent(); 104 return (rpc); 105 } 106 107 void 108 setrpcent(int f) 109 { 110 struct rpcdata *d = _rpcdata(); 111 112 if (d == NULL) 113 return; 114 if (d->rpcf == NULL) 115 d->rpcf = fopen(RPCDB, "r"); 116 else 117 rewind(d->rpcf); 118 d->stayopen |= f; 119 } 120 121 void 122 endrpcent(void) 123 { 124 struct rpcdata *d = _rpcdata(); 125 126 if (d == NULL) 127 return; 128 if (d->rpcf && !d->stayopen) { 129 fclose(d->rpcf); 130 d->rpcf = NULL; 131 } 132 } 133 134 struct rpcent * 135 getrpcent(void) 136 { 137 struct rpcdata *d = _rpcdata(); 138 139 if (d == NULL) 140 return(NULL); 141 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL) 142 return (NULL); 143 /* -1 so there is room to append a \n below */ 144 if (fgets(d->line, sizeof(d->line) - 1, d->rpcf) == NULL) 145 return (NULL); 146 return (interpret(d->line, strlen(d->line))); 147 } 148 149 static struct rpcent * 150 interpret(char *val, int len) 151 { 152 struct rpcdata *d = _rpcdata(); 153 char *p; 154 char *cp, **q; 155 156 if (d == NULL) 157 return (0); 158 strlcpy(d->line, val, sizeof(d->line)); 159 p = d->line; 160 p[len] = '\n'; 161 if (*p == '#') 162 return (getrpcent()); 163 cp = strpbrk(p, "#\n"); 164 if (cp == NULL) 165 return (getrpcent()); 166 *cp = '\0'; 167 cp = strpbrk(p, " \t"); 168 if (cp == NULL) 169 return (getrpcent()); 170 *cp++ = '\0'; 171 /* THIS STUFF IS INTERNET SPECIFIC */ 172 d->rpc.r_name = d->line; 173 while (*cp == ' ' || *cp == '\t') 174 cp++; 175 d->rpc.r_number = atoi(cp); 176 q = d->rpc.r_aliases = d->rpc_aliases; 177 cp = strpbrk(cp, " \t"); 178 if (cp != NULL) 179 *cp++ = '\0'; 180 while (cp && *cp) { 181 if (*cp == ' ' || *cp == '\t') { 182 cp++; 183 continue; 184 } 185 if (q < &(d->rpc_aliases[MAXALIASES - 1])) 186 *q++ = cp; 187 cp = strpbrk(cp, " \t"); 188 if (cp != NULL) 189 *cp++ = '\0'; 190 } 191 *q = NULL; 192 return (&d->rpc); 193 } 194 195