getrpcent.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* @(#)getrpcent.c 2.2 88/07/29 4.0 RPCSRC */
  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.
  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. * Copyright (c) 1985 by Sun Microsystems, Inc.
  32. */
  33. #define __FORCE_GLIBC
  34. #include <features.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <sys/types.h>
  38. #include <rpc/rpc.h>
  39. #include <netdb.h>
  40. #include <sys/socket.h>
  41. #include <arpa/inet.h>
  42. #include <errno.h>
  43. libc_hidden_proto(memcpy)
  44. libc_hidden_proto(memset)
  45. libc_hidden_proto(strchr)
  46. libc_hidden_proto(strcmp)
  47. libc_hidden_proto(strlen)
  48. libc_hidden_proto(fopen)
  49. libc_hidden_proto(fclose)
  50. libc_hidden_proto(atoi)
  51. libc_hidden_proto(rewind)
  52. libc_hidden_proto(fgets)
  53. /*
  54. * Internet version.
  55. */
  56. static struct rpcdata {
  57. FILE *rpcf;
  58. char *current;
  59. int currentlen;
  60. int stayopen;
  61. #define MAXALIASES 35
  62. char *rpc_aliases[MAXALIASES];
  63. struct rpcent rpc;
  64. char line[BUFSIZ + 1];
  65. char *domain;
  66. } *rpcdata;
  67. static char RPCDB[] = "/etc/rpc";
  68. static struct rpcdata *_rpcdata(void)
  69. {
  70. register struct rpcdata *d = rpcdata;
  71. if (d == NULL) {
  72. d = (struct rpcdata *) calloc(1, sizeof(struct rpcdata));
  73. rpcdata = d;
  74. }
  75. return d;
  76. }
  77. libc_hidden_proto(endrpcent)
  78. void endrpcent(void)
  79. {
  80. register struct rpcdata *d = _rpcdata();
  81. if (d == NULL)
  82. return;
  83. if (d->stayopen)
  84. return;
  85. if (d->current) {
  86. free(d->current);
  87. d->current = NULL;
  88. }
  89. if (d->rpcf) {
  90. fclose(d->rpcf);
  91. d->rpcf = NULL;
  92. }
  93. }
  94. libc_hidden_def(endrpcent)
  95. libc_hidden_proto(setrpcent)
  96. void setrpcent(int f)
  97. {
  98. register struct rpcdata *d = _rpcdata();
  99. if (d == NULL)
  100. return;
  101. if (d->rpcf == NULL)
  102. d->rpcf = fopen(RPCDB, "r");
  103. else
  104. rewind(d->rpcf);
  105. if (d->current)
  106. free(d->current);
  107. d->current = NULL;
  108. d->stayopen |= f;
  109. }
  110. libc_hidden_def(setrpcent)
  111. static struct rpcent *interpret(struct rpcdata *);
  112. static struct rpcent *__get_next_rpcent(struct rpcdata *d)
  113. {
  114. if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
  115. return NULL;
  116. return interpret(d);
  117. }
  118. libc_hidden_proto(getrpcent)
  119. struct rpcent *getrpcent(void)
  120. {
  121. register struct rpcdata *d = _rpcdata();
  122. if (d == NULL)
  123. return NULL;
  124. if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
  125. return NULL;
  126. return __get_next_rpcent(d);
  127. }
  128. libc_hidden_def(getrpcent)
  129. libc_hidden_proto(getrpcbynumber)
  130. struct rpcent *getrpcbynumber(register int number)
  131. {
  132. register struct rpcdata *d = _rpcdata();
  133. register struct rpcent *rpc;
  134. if (d == NULL)
  135. return NULL;
  136. setrpcent(0);
  137. while ((rpc = getrpcent())) {
  138. if (rpc->r_number == number)
  139. break;
  140. }
  141. endrpcent();
  142. return rpc;
  143. }
  144. libc_hidden_def(getrpcbynumber)
  145. libc_hidden_proto(getrpcbyname)
  146. struct rpcent *getrpcbyname(const char *name)
  147. {
  148. struct rpcent *rpc;
  149. char **rp;
  150. setrpcent(0);
  151. while ((rpc = getrpcent())) {
  152. if (strcmp(rpc->r_name, name) == 0)
  153. return rpc;
  154. for (rp = rpc->r_aliases; *rp != NULL; rp++) {
  155. if (strcmp(*rp, name) == 0)
  156. return rpc;
  157. }
  158. }
  159. endrpcent();
  160. return NULL;
  161. }
  162. libc_hidden_def(getrpcbyname)
  163. #ifdef __linux__
  164. static char *firstwhite(char *s)
  165. {
  166. char *s1, *s2;
  167. s1 = strchr(s, ' ');
  168. s2 = strchr(s, '\t');
  169. if (s1) {
  170. if (s2)
  171. return (s1 < s2) ? s1 : s2;
  172. else
  173. return s1;
  174. } else
  175. return s2;
  176. }
  177. #endif
  178. static struct rpcent *interpret(register struct rpcdata *d)
  179. {
  180. char *p;
  181. register char *cp, **q;
  182. p = d->line;
  183. d->line[strlen(p)-1] = '\n';
  184. if (*p == '#')
  185. return __get_next_rpcent(d);
  186. cp = strchr(p, '#');
  187. if (cp == NULL) {
  188. cp = strchr(p, '\n');
  189. if (cp == NULL)
  190. return __get_next_rpcent(d);
  191. }
  192. *cp = '\0';
  193. #ifdef __linux__
  194. if ((cp = firstwhite(p)))
  195. *cp++ = 0;
  196. else
  197. return __get_next_rpcent(d);
  198. #else
  199. cp = strchr(p, ' ');
  200. if (cp == NULL) {
  201. cp = strchr(p, '\t');
  202. if (cp == NULL)
  203. return __get_next_rpcent(d);
  204. }
  205. *cp++ = '\0';
  206. #endif
  207. /* THIS STUFF IS INTERNET SPECIFIC */
  208. d->rpc.r_name = d->line;
  209. while (*cp == ' ' || *cp == '\t')
  210. cp++;
  211. d->rpc.r_number = atoi(cp);
  212. q = d->rpc.r_aliases = d->rpc_aliases;
  213. #ifdef __linux__
  214. if ((cp = firstwhite(cp)))
  215. *cp++ = '\0';
  216. #else
  217. cp = strchr(p, ' ');
  218. if (cp != NULL)
  219. *cp++ = '\0';
  220. else {
  221. cp = strchr(p, '\t');
  222. if (cp != NULL)
  223. *cp++ = '\0';
  224. }
  225. #endif
  226. while (cp && *cp) {
  227. if (*cp == ' ' || *cp == '\t') {
  228. cp++;
  229. continue;
  230. }
  231. if (q < &(d->rpc_aliases[MAXALIASES - 1]))
  232. *q++ = cp;
  233. #ifdef __linux__
  234. if ((cp = firstwhite(cp)))
  235. *cp++ = '\0';
  236. #else
  237. cp = strchr(p, ' ');
  238. if (cp != NULL)
  239. *cp++ = '\0';
  240. else {
  241. cp = strchr(p, '\t');
  242. if (cp != NULL)
  243. *cp++ = '\0';
  244. }
  245. #endif
  246. }
  247. *q = NULL;
  248. return &d->rpc;
  249. }
  250. #if defined(__UCLIBC_HAS_REENTRANT_RPC__)
  251. #if defined(__UCLIBC_HAS_THREADS__)
  252. # include <pthread.h>
  253. static pthread_mutex_t rpcdata_lock = PTHREAD_MUTEX_INITIALIZER;
  254. #endif
  255. #define LOCK __pthread_mutex_lock(&rpcdata_lock)
  256. #define UNLOCK __pthread_mutex_unlock(&rpcdata_lock)
  257. static int __copy_rpcent(struct rpcent *r, struct rpcent *result_buf, char *buffer,
  258. size_t buflen, struct rpcent **result)
  259. {
  260. size_t i, s;
  261. *result = NULL;
  262. if (!r)
  263. return ENOENT;
  264. /* copy the struct from the shared mem */
  265. memset(result_buf, 0x00, sizeof(*result_buf));
  266. memset(buffer, 0x00, buflen);
  267. result_buf->r_number = r->r_number;
  268. /* copy the aliases ... need to not only copy the alias strings,
  269. * but the array of pointers to the alias strings */
  270. i = 0;
  271. while (r->r_aliases[i++]) ;
  272. s = i-- * sizeof(char*);
  273. if (buflen < s)
  274. goto err_out;
  275. result_buf->r_aliases = (char**)buffer;
  276. buffer += s;
  277. buflen -= s;
  278. while (i-- > 0) {
  279. s = strlen(r->r_aliases[i]) + 1;
  280. if (buflen < s)
  281. goto err_out;
  282. result_buf->r_aliases[i] = buffer;
  283. buffer += s;
  284. buflen -= s;
  285. memcpy(result_buf->r_aliases[i], r->r_aliases[i], s);
  286. }
  287. /* copy the name */
  288. i = strlen(r->r_name);
  289. if (buflen <= i)
  290. goto err_out;
  291. result_buf->r_name = buffer;
  292. memcpy(result_buf->r_name, r->r_name, i);
  293. /* that was a hoot eh ? */
  294. *result = result_buf;
  295. return 0;
  296. err_out:
  297. return ERANGE;
  298. }
  299. int getrpcbynumber_r(int number, struct rpcent *result_buf, char *buffer,
  300. size_t buflen, struct rpcent **result)
  301. {
  302. int ret;
  303. LOCK;
  304. ret = __copy_rpcent(getrpcbynumber(number), result_buf, buffer, buflen, result);
  305. UNLOCK;
  306. return ret;
  307. }
  308. int getrpcbyname_r(const char *name, struct rpcent *result_buf, char *buffer,
  309. size_t buflen, struct rpcent **result)
  310. {
  311. int ret;
  312. LOCK;
  313. ret = __copy_rpcent(getrpcbyname(name), result_buf, buffer, buflen, result);
  314. UNLOCK;
  315. return ret;
  316. }
  317. int getrpcent_r(struct rpcent *result_buf, char *buffer,
  318. size_t buflen, struct rpcent **result)
  319. {
  320. int ret;
  321. LOCK;
  322. ret = __copy_rpcent(getrpcent(), result_buf, buffer, buflen, result);
  323. UNLOCK;
  324. return ret;
  325. }
  326. #endif /* __UCLIBC_HAS_REENTRANT_RPC__ */