getrpcent.c 7.1 KB

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