getrpcent.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 const 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. free(d->current);
  86. d->current = NULL;
  87. if (d->rpcf) {
  88. fclose(d->rpcf);
  89. d->rpcf = NULL;
  90. }
  91. }
  92. libc_hidden_def(endrpcent)
  93. /* libc_hidden_proto(setrpcent) */
  94. void setrpcent(int f)
  95. {
  96. register struct rpcdata *d = _rpcdata();
  97. if (d == NULL)
  98. return;
  99. if (d->rpcf == NULL)
  100. d->rpcf = fopen(RPCDB, "r");
  101. else
  102. rewind(d->rpcf);
  103. free(d->current);
  104. d->current = NULL;
  105. d->stayopen |= f;
  106. }
  107. libc_hidden_def(setrpcent)
  108. static struct rpcent *interpret(struct rpcdata *);
  109. static struct rpcent *__get_next_rpcent(struct rpcdata *d)
  110. {
  111. if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
  112. return NULL;
  113. return interpret(d);
  114. }
  115. /* libc_hidden_proto(getrpcent) */
  116. struct rpcent *getrpcent(void)
  117. {
  118. register struct rpcdata *d = _rpcdata();
  119. if (d == NULL)
  120. return NULL;
  121. if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
  122. return NULL;
  123. return __get_next_rpcent(d);
  124. }
  125. libc_hidden_def(getrpcent)
  126. /* libc_hidden_proto(getrpcbynumber) */
  127. struct rpcent *getrpcbynumber(register int number)
  128. {
  129. register struct rpcdata *d = _rpcdata();
  130. register struct rpcent *rpc;
  131. if (d == NULL)
  132. return NULL;
  133. setrpcent(0);
  134. while ((rpc = getrpcent())) {
  135. if (rpc->r_number == number)
  136. break;
  137. }
  138. endrpcent();
  139. return rpc;
  140. }
  141. libc_hidden_def(getrpcbynumber)
  142. /* libc_hidden_proto(getrpcbyname) */
  143. struct rpcent *getrpcbyname(const char *name)
  144. {
  145. struct rpcent *rpc;
  146. char **rp;
  147. setrpcent(0);
  148. while ((rpc = getrpcent())) {
  149. if (strcmp(rpc->r_name, name) == 0)
  150. return rpc;
  151. for (rp = rpc->r_aliases; *rp != NULL; rp++) {
  152. if (strcmp(*rp, name) == 0)
  153. return rpc;
  154. }
  155. }
  156. endrpcent();
  157. return NULL;
  158. }
  159. libc_hidden_def(getrpcbyname)
  160. #ifdef __linux__
  161. static char *firstwhite(char *s)
  162. {
  163. char *s1, *s2;
  164. s1 = strchr(s, ' ');
  165. s2 = strchr(s, '\t');
  166. if (s1) {
  167. if (s2)
  168. return (s1 < s2) ? s1 : s2;
  169. else
  170. return s1;
  171. } else
  172. return s2;
  173. }
  174. #endif
  175. static struct rpcent *interpret(register struct rpcdata *d)
  176. {
  177. char *p;
  178. register char *cp, **q;
  179. p = d->line;
  180. d->line[strlen(p)-1] = '\n';
  181. if (*p == '#')
  182. return __get_next_rpcent(d);
  183. cp = strchr(p, '#');
  184. if (cp == NULL) {
  185. cp = strchr(p, '\n');
  186. if (cp == NULL)
  187. return __get_next_rpcent(d);
  188. }
  189. *cp = '\0';
  190. #ifdef __linux__
  191. if ((cp = firstwhite(p)))
  192. *cp++ = 0;
  193. else
  194. return __get_next_rpcent(d);
  195. #else
  196. cp = strchr(p, ' ');
  197. if (cp == NULL) {
  198. cp = strchr(p, '\t');
  199. if (cp == NULL)
  200. return __get_next_rpcent(d);
  201. }
  202. *cp++ = '\0';
  203. #endif
  204. /* THIS STUFF IS INTERNET SPECIFIC */
  205. d->rpc.r_name = d->line;
  206. while (*cp == ' ' || *cp == '\t')
  207. cp++;
  208. d->rpc.r_number = atoi(cp);
  209. q = d->rpc.r_aliases = d->rpc_aliases;
  210. #ifdef __linux__
  211. if ((cp = firstwhite(cp)))
  212. *cp++ = '\0';
  213. #else
  214. cp = strchr(p, ' ');
  215. if (cp != NULL)
  216. *cp++ = '\0';
  217. else {
  218. cp = strchr(p, '\t');
  219. if (cp != NULL)
  220. *cp++ = '\0';
  221. }
  222. #endif
  223. while (cp && *cp) {
  224. if (*cp == ' ' || *cp == '\t') {
  225. cp++;
  226. continue;
  227. }
  228. if (q < &(d->rpc_aliases[MAXALIASES - 1]))
  229. *q++ = cp;
  230. #ifdef __linux__
  231. if ((cp = firstwhite(cp)))
  232. *cp++ = '\0';
  233. #else
  234. cp = strchr(p, ' ');
  235. if (cp != NULL)
  236. *cp++ = '\0';
  237. else {
  238. cp = strchr(p, '\t');
  239. if (cp != NULL)
  240. *cp++ = '\0';
  241. }
  242. #endif
  243. }
  244. *q = NULL;
  245. return &d->rpc;
  246. }
  247. #if defined(__UCLIBC_HAS_REENTRANT_RPC__)
  248. #include <bits/uClibc_mutex.h>
  249. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  250. static int __copy_rpcent(struct rpcent *r, struct rpcent *result_buf, char *buffer,
  251. size_t buflen, struct rpcent **result)
  252. {
  253. size_t i, s;
  254. *result = NULL;
  255. if (!r)
  256. return ENOENT;
  257. /* copy the struct from the shared mem */
  258. memset(result_buf, 0x00, sizeof(*result_buf));
  259. memset(buffer, 0x00, buflen);
  260. result_buf->r_number = r->r_number;
  261. /* copy the aliases ... need to not only copy the alias strings,
  262. * but the array of pointers to the alias strings */
  263. i = 0;
  264. while (r->r_aliases[i++]) ;
  265. s = i-- * sizeof(char*);
  266. if (buflen < s)
  267. goto err_out;
  268. result_buf->r_aliases = (char**)buffer;
  269. buffer += s;
  270. buflen -= s;
  271. while (i-- > 0) {
  272. s = strlen(r->r_aliases[i]) + 1;
  273. if (buflen < s)
  274. goto err_out;
  275. result_buf->r_aliases[i] = buffer;
  276. buffer += s;
  277. buflen -= s;
  278. memcpy(result_buf->r_aliases[i], r->r_aliases[i], s);
  279. }
  280. /* copy the name */
  281. i = strlen(r->r_name);
  282. if (buflen <= i)
  283. goto err_out;
  284. result_buf->r_name = buffer;
  285. memcpy(result_buf->r_name, r->r_name, i);
  286. /* that was a hoot eh ? */
  287. *result = result_buf;
  288. return 0;
  289. err_out:
  290. return ERANGE;
  291. }
  292. int getrpcbynumber_r(int number, struct rpcent *result_buf, char *buffer,
  293. size_t buflen, struct rpcent **result)
  294. {
  295. int ret;
  296. __UCLIBC_MUTEX_LOCK(mylock);
  297. ret = __copy_rpcent(getrpcbynumber(number), result_buf, buffer, buflen, result);
  298. __UCLIBC_MUTEX_UNLOCK(mylock);
  299. return ret;
  300. }
  301. int getrpcbyname_r(const char *name, struct rpcent *result_buf, char *buffer,
  302. size_t buflen, struct rpcent **result)
  303. {
  304. int ret;
  305. __UCLIBC_MUTEX_LOCK(mylock);
  306. ret = __copy_rpcent(getrpcbyname(name), result_buf, buffer, buflen, result);
  307. __UCLIBC_MUTEX_UNLOCK(mylock);
  308. return ret;
  309. }
  310. int getrpcent_r(struct rpcent *result_buf, char *buffer,
  311. size_t buflen, struct rpcent **result)
  312. {
  313. int ret;
  314. __UCLIBC_MUTEX_LOCK(mylock);
  315. ret = __copy_rpcent(getrpcent(), result_buf, buffer, buflen, result);
  316. __UCLIBC_MUTEX_UNLOCK(mylock);
  317. return ret;
  318. }
  319. #endif /* __UCLIBC_HAS_REENTRANT_RPC__ */