getrpcent.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. /* Experimentally off - libc_hidden_proto(memcpy) */
  44. /* Experimentally off - libc_hidden_proto(memset) */
  45. /* Experimentally off - libc_hidden_proto(strchr) */
  46. /* Experimentally off - libc_hidden_proto(strcmp) */
  47. /* Experimentally off - 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. #include <bits/uClibc_mutex.h>
  252. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  253. static int __copy_rpcent(struct rpcent *r, struct rpcent *result_buf, char *buffer,
  254. size_t buflen, struct rpcent **result)
  255. {
  256. size_t i, s;
  257. *result = NULL;
  258. if (!r)
  259. return ENOENT;
  260. /* copy the struct from the shared mem */
  261. memset(result_buf, 0x00, sizeof(*result_buf));
  262. memset(buffer, 0x00, buflen);
  263. result_buf->r_number = r->r_number;
  264. /* copy the aliases ... need to not only copy the alias strings,
  265. * but the array of pointers to the alias strings */
  266. i = 0;
  267. while (r->r_aliases[i++]) ;
  268. s = i-- * sizeof(char*);
  269. if (buflen < s)
  270. goto err_out;
  271. result_buf->r_aliases = (char**)buffer;
  272. buffer += s;
  273. buflen -= s;
  274. while (i-- > 0) {
  275. s = strlen(r->r_aliases[i]) + 1;
  276. if (buflen < s)
  277. goto err_out;
  278. result_buf->r_aliases[i] = buffer;
  279. buffer += s;
  280. buflen -= s;
  281. memcpy(result_buf->r_aliases[i], r->r_aliases[i], s);
  282. }
  283. /* copy the name */
  284. i = strlen(r->r_name);
  285. if (buflen <= i)
  286. goto err_out;
  287. result_buf->r_name = buffer;
  288. memcpy(result_buf->r_name, r->r_name, i);
  289. /* that was a hoot eh ? */
  290. *result = result_buf;
  291. return 0;
  292. err_out:
  293. return ERANGE;
  294. }
  295. int getrpcbynumber_r(int number, struct rpcent *result_buf, char *buffer,
  296. size_t buflen, struct rpcent **result)
  297. {
  298. int ret;
  299. __UCLIBC_MUTEX_LOCK(mylock);
  300. ret = __copy_rpcent(getrpcbynumber(number), result_buf, buffer, buflen, result);
  301. __UCLIBC_MUTEX_UNLOCK(mylock);
  302. return ret;
  303. }
  304. int getrpcbyname_r(const char *name, struct rpcent *result_buf, char *buffer,
  305. size_t buflen, struct rpcent **result)
  306. {
  307. int ret;
  308. __UCLIBC_MUTEX_LOCK(mylock);
  309. ret = __copy_rpcent(getrpcbyname(name), result_buf, buffer, buflen, result);
  310. __UCLIBC_MUTEX_UNLOCK(mylock);
  311. return ret;
  312. }
  313. int getrpcent_r(struct rpcent *result_buf, char *buffer,
  314. size_t buflen, struct rpcent **result)
  315. {
  316. int ret;
  317. __UCLIBC_MUTEX_LOCK(mylock);
  318. ret = __copy_rpcent(getrpcent(), result_buf, buffer, buflen, result);
  319. __UCLIBC_MUTEX_UNLOCK(mylock);
  320. return ret;
  321. }
  322. #endif /* __UCLIBC_HAS_REENTRANT_RPC__ */