ypclnt.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /* Copyright (C) 1996-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <fcntl.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <libintl.h>
  19. #include <rpc/rpc.h>
  20. #include <rpcsvc/nis.h>
  21. #include <rpcsvc/yp.h>
  22. #include <rpcsvc/ypclnt.h>
  23. #include <rpcsvc/ypupd.h>
  24. #include <sys/socket.h>
  25. #include <sys/uio.h>
  26. #include <libc-lock.h>
  27. #include <shlib-compat.h>
  28. #include <libc-diag.h>
  29. /* This should only be defined on systems with a BSD compatible ypbind */
  30. #ifndef BINDINGDIR
  31. # define BINDINGDIR "/var/yp/binding"
  32. #endif
  33. struct dom_binding
  34. {
  35. struct dom_binding *dom_pnext;
  36. char dom_domain[YPMAXDOMAIN + 1];
  37. struct sockaddr_in dom_server_addr;
  38. int dom_socket;
  39. CLIENT *dom_client;
  40. };
  41. typedef struct dom_binding dom_binding;
  42. static const struct timeval RPCTIMEOUT = {25, 0};
  43. static const struct timeval UDPTIMEOUT = {5, 0};
  44. static int const MAXTRIES = 2;
  45. static char ypdomainname[NIS_MAXNAMELEN + 1];
  46. __libc_lock_define_initialized (static, ypbindlist_lock)
  47. static dom_binding *ypbindlist = NULL;
  48. static void
  49. yp_bind_client_create (const char *domain, dom_binding *ysd,
  50. struct ypbind_resp *ypbr)
  51. {
  52. ysd->dom_server_addr.sin_family = AF_INET;
  53. memcpy (&ysd->dom_server_addr.sin_port,
  54. ypbr->ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port,
  55. sizeof (ysd->dom_server_addr.sin_port));
  56. memcpy (&ysd->dom_server_addr.sin_addr.s_addr,
  57. ypbr->ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
  58. sizeof (ysd->dom_server_addr.sin_addr.s_addr));
  59. strncpy (ysd->dom_domain, domain, YPMAXDOMAIN);
  60. ysd->dom_domain[YPMAXDOMAIN] = '\0';
  61. ysd->dom_socket = RPC_ANYSOCK;
  62. ysd->dom_client = __libc_clntudp_bufcreate (&ysd->dom_server_addr, YPPROG,
  63. YPVERS, UDPTIMEOUT,
  64. &ysd->dom_socket,
  65. UDPMSGSIZE, UDPMSGSIZE,
  66. SOCK_CLOEXEC);
  67. }
  68. #if USE_BINDINGDIR
  69. static void
  70. yp_bind_file (const char *domain, dom_binding *ysd)
  71. {
  72. char path[sizeof (BINDINGDIR) + strlen (domain) + 3 * sizeof (unsigned) + 3];
  73. snprintf (path, sizeof (path), "%s/%s.%u", BINDINGDIR, domain, YPBINDVERS);
  74. int fd = open (path, O_RDONLY);
  75. if (fd >= 0)
  76. {
  77. /* We have a binding file and could save a RPC call. The file
  78. contains a port number and the YPBIND_RESP record. The port
  79. number (16 bits) can be ignored. */
  80. struct ypbind_resp ypbr;
  81. if (pread (fd, &ypbr, sizeof (ypbr), 2) == sizeof (ypbr))
  82. yp_bind_client_create (domain, ysd, &ypbr);
  83. close (fd);
  84. }
  85. }
  86. #endif
  87. static int
  88. yp_bind_ypbindprog (const char *domain, dom_binding *ysd)
  89. {
  90. struct sockaddr_in clnt_saddr;
  91. struct ypbind_resp ypbr;
  92. int clnt_sock;
  93. CLIENT *client;
  94. clnt_saddr.sin_family = AF_INET;
  95. clnt_saddr.sin_port = 0;
  96. clnt_saddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  97. clnt_sock = RPC_ANYSOCK;
  98. client = clnttcp_create (&clnt_saddr, YPBINDPROG, YPBINDVERS,
  99. &clnt_sock, 0, 0);
  100. if (client == NULL)
  101. return YPERR_YPBIND;
  102. /* Check the port number -- should be < IPPORT_RESERVED.
  103. If not, it's possible someone has registered a bogus
  104. ypbind with the portmapper and is trying to trick us. */
  105. if (ntohs (clnt_saddr.sin_port) >= IPPORT_RESERVED)
  106. {
  107. clnt_destroy (client);
  108. return YPERR_YPBIND;
  109. }
  110. if (clnt_call (client, YPBINDPROC_DOMAIN,
  111. (xdrproc_t) xdr_domainname, (caddr_t) &domain,
  112. (xdrproc_t) xdr_ypbind_resp,
  113. (caddr_t) &ypbr, RPCTIMEOUT) != RPC_SUCCESS)
  114. {
  115. clnt_destroy (client);
  116. return YPERR_YPBIND;
  117. }
  118. clnt_destroy (client);
  119. if (ypbr.ypbind_status != YPBIND_SUCC_VAL)
  120. {
  121. fprintf (stderr, "YPBINDPROC_DOMAIN: %s\n",
  122. ypbinderr_string (ypbr.ypbind_resp_u.ypbind_error));
  123. return YPERR_DOMAIN;
  124. }
  125. memset (&ysd->dom_server_addr, '\0', sizeof ysd->dom_server_addr);
  126. yp_bind_client_create (domain, ysd, &ypbr);
  127. return YPERR_SUCCESS;
  128. }
  129. static int
  130. __yp_bind (const char *domain, dom_binding **ypdb)
  131. {
  132. dom_binding *ysd = NULL;
  133. int is_new = 0;
  134. if (domain == NULL || domain[0] == '\0')
  135. return YPERR_BADARGS;
  136. ysd = *ypdb;
  137. while (ysd != NULL)
  138. {
  139. if (strcmp (domain, ysd->dom_domain) == 0)
  140. break;
  141. ysd = ysd->dom_pnext;
  142. }
  143. if (ysd == NULL)
  144. {
  145. is_new = 1;
  146. ysd = (dom_binding *) calloc (1, sizeof *ysd);
  147. if (__glibc_unlikely (ysd == NULL))
  148. return YPERR_RESRC;
  149. }
  150. #if USE_BINDINGDIR
  151. /* Try binding dir at first if we have no binding */
  152. if (ysd->dom_client == NULL)
  153. yp_bind_file (domain, ysd);
  154. #endif /* USE_BINDINGDIR */
  155. if (ysd->dom_client == NULL)
  156. {
  157. int retval = yp_bind_ypbindprog (domain, ysd);
  158. if (retval != YPERR_SUCCESS)
  159. {
  160. if (is_new)
  161. free (ysd);
  162. return retval;
  163. }
  164. }
  165. if (ysd->dom_client == NULL)
  166. {
  167. if (is_new)
  168. free (ysd);
  169. return YPERR_YPSERV;
  170. }
  171. if (is_new)
  172. {
  173. ysd->dom_pnext = *ypdb;
  174. *ypdb = ysd;
  175. }
  176. return YPERR_SUCCESS;
  177. }
  178. static void
  179. __yp_unbind (dom_binding *ydb)
  180. {
  181. clnt_destroy (ydb->dom_client);
  182. free (ydb);
  183. }
  184. int
  185. yp_bind (const char *indomain)
  186. {
  187. int status;
  188. __libc_lock_lock (ypbindlist_lock);
  189. status = __yp_bind (indomain, &ypbindlist);
  190. __libc_lock_unlock (ypbindlist_lock);
  191. return status;
  192. }
  193. libnsl_hidden_nolink_def (yp_bind, GLIBC_2_0)
  194. static void
  195. yp_unbind_locked (const char *indomain)
  196. {
  197. dom_binding *ydbptr, *ydbptr2;
  198. ydbptr2 = NULL;
  199. ydbptr = ypbindlist;
  200. while (ydbptr != NULL)
  201. {
  202. if (strcmp (ydbptr->dom_domain, indomain) == 0)
  203. {
  204. dom_binding *work;
  205. work = ydbptr;
  206. if (ydbptr2 == NULL)
  207. ypbindlist = ypbindlist->dom_pnext;
  208. else
  209. ydbptr2 = ydbptr->dom_pnext;
  210. __yp_unbind (work);
  211. break;
  212. }
  213. ydbptr2 = ydbptr;
  214. ydbptr = ydbptr->dom_pnext;
  215. }
  216. }
  217. void
  218. yp_unbind (const char *indomain)
  219. {
  220. __libc_lock_lock (ypbindlist_lock);
  221. yp_unbind_locked (indomain);
  222. __libc_lock_unlock (ypbindlist_lock);
  223. return;
  224. }
  225. libnsl_hidden_nolink_def(yp_unbind, GLIBC_2_0)
  226. static int
  227. __ypclnt_call (const char *domain, u_long prog, xdrproc_t xargs,
  228. caddr_t req, xdrproc_t xres, caddr_t resp, dom_binding **ydb,
  229. int print_error)
  230. {
  231. enum clnt_stat result;
  232. result = clnt_call ((*ydb)->dom_client, prog,
  233. xargs, req, xres, resp, RPCTIMEOUT);
  234. if (result != RPC_SUCCESS)
  235. {
  236. /* We don't print an error message, if we try our old,
  237. cached data. Only print this for data, which should work. */
  238. if (print_error)
  239. clnt_perror ((*ydb)->dom_client, "do_ypcall: clnt_call");
  240. return YPERR_RPC;
  241. }
  242. return YPERR_SUCCESS;
  243. }
  244. static int
  245. do_ypcall (const char *domain, u_long prog, xdrproc_t xargs,
  246. caddr_t req, xdrproc_t xres, caddr_t resp)
  247. {
  248. dom_binding *ydb;
  249. int status;
  250. int saved_errno = errno;
  251. status = YPERR_YPERR;
  252. __libc_lock_lock (ypbindlist_lock);
  253. ydb = ypbindlist;
  254. while (ydb != NULL)
  255. {
  256. if (strcmp (domain, ydb->dom_domain) == 0)
  257. {
  258. if (__yp_bind (domain, &ydb) == 0)
  259. {
  260. /* Call server, print no error message, do not unbind. */
  261. status = __ypclnt_call (domain, prog, xargs, req, xres,
  262. resp, &ydb, 0);
  263. if (status == YPERR_SUCCESS)
  264. {
  265. __libc_lock_unlock (ypbindlist_lock);
  266. __set_errno (saved_errno);
  267. return status;
  268. }
  269. }
  270. /* We use ypbindlist, and the old cached data is
  271. invalid. unbind now and create a new binding */
  272. yp_unbind_locked (domain);
  273. break;
  274. }
  275. ydb = ydb->dom_pnext;
  276. }
  277. __libc_lock_unlock (ypbindlist_lock);
  278. /* First try with cached data failed. Now try to get
  279. current data from the system. */
  280. ydb = NULL;
  281. if (__yp_bind (domain, &ydb) == 0)
  282. {
  283. status = __ypclnt_call (domain, prog, xargs, req, xres,
  284. resp, &ydb, 1);
  285. __yp_unbind (ydb);
  286. }
  287. #if USE_BINDINGDIR
  288. /* If we support binding dir data, we have a third chance:
  289. Ask ypbind. */
  290. if (status != YPERR_SUCCESS)
  291. {
  292. ydb = calloc (1, sizeof (dom_binding));
  293. if (ydb != NULL && yp_bind_ypbindprog (domain, ydb) == YPERR_SUCCESS)
  294. {
  295. status = __ypclnt_call (domain, prog, xargs, req, xres,
  296. resp, &ydb, 1);
  297. __yp_unbind (ydb);
  298. }
  299. else
  300. free (ydb);
  301. }
  302. #endif
  303. __set_errno (saved_errno);
  304. return status;
  305. }
  306. /* Like do_ypcall, but translate the status value if necessary. */
  307. static int
  308. do_ypcall_tr (const char *domain, u_long prog, xdrproc_t xargs,
  309. caddr_t req, xdrproc_t xres, caddr_t resp)
  310. {
  311. int status = do_ypcall (domain, prog, xargs, req, xres, resp);
  312. DIAG_PUSH_NEEDS_COMMENT;
  313. /* This cast results in a warning that a ypresp_val is partly
  314. outside the bounds of the actual object referenced, but as
  315. explained below only the stat element (in a common prefix) is
  316. accessed. */
  317. DIAG_IGNORE_NEEDS_COMMENT (11, "-Warray-bounds");
  318. if (status == YPERR_SUCCESS)
  319. /* We cast to ypresp_val although the pointer could also be of
  320. type ypresp_key_val or ypresp_master or ypresp_order or
  321. ypresp_maplist. But the stat element is in a common prefix so
  322. this does not matter. */
  323. status = ypprot_err (((struct ypresp_val *) resp)->stat);
  324. DIAG_POP_NEEDS_COMMENT;
  325. return status;
  326. }
  327. __libc_lock_define_initialized (static, domainname_lock)
  328. int
  329. yp_get_default_domain (char **outdomain)
  330. {
  331. int result = YPERR_SUCCESS;;
  332. *outdomain = NULL;
  333. __libc_lock_lock (domainname_lock);
  334. if (ypdomainname[0] == '\0')
  335. {
  336. if (getdomainname (ypdomainname, NIS_MAXNAMELEN))
  337. result = YPERR_NODOM;
  338. else if (strcmp (ypdomainname, "(none)") == 0)
  339. {
  340. /* If domainname is not set, some systems will return "(none)" */
  341. ypdomainname[0] = '\0';
  342. result = YPERR_NODOM;
  343. }
  344. else
  345. *outdomain = ypdomainname;
  346. }
  347. else
  348. *outdomain = ypdomainname;
  349. __libc_lock_unlock (domainname_lock);
  350. return result;
  351. }
  352. libnsl_hidden_nolink_def (yp_get_default_domain, GLIBC_2_0)
  353. int
  354. __yp_check (char **domain)
  355. {
  356. char *unused;
  357. if (ypdomainname[0] == '\0')
  358. if (yp_get_default_domain (&unused))
  359. return 0;
  360. if (domain)
  361. *domain = ypdomainname;
  362. if (yp_bind (ypdomainname) == 0)
  363. return 1;
  364. return 0;
  365. }
  366. libnsl_hidden_nolink_def(__yp_check, GLIBC_2_0)
  367. int
  368. yp_match (const char *indomain, const char *inmap, const char *inkey,
  369. const int inkeylen, char **outval, int *outvallen)
  370. {
  371. ypreq_key req;
  372. ypresp_val resp;
  373. enum clnt_stat result;
  374. if (indomain == NULL || indomain[0] == '\0'
  375. || inmap == NULL || inmap[0] == '\0'
  376. || inkey == NULL || inkey[0] == '\0' || inkeylen <= 0)
  377. return YPERR_BADARGS;
  378. req.domain = (char *) indomain;
  379. req.map = (char *) inmap;
  380. req.key.keydat_val = (char *) inkey;
  381. req.key.keydat_len = inkeylen;
  382. *outval = NULL;
  383. *outvallen = 0;
  384. memset (&resp, '\0', sizeof (resp));
  385. result = do_ypcall_tr (indomain, YPPROC_MATCH, (xdrproc_t) xdr_ypreq_key,
  386. (caddr_t) &req, (xdrproc_t) xdr_ypresp_val,
  387. (caddr_t) &resp);
  388. if (result != YPERR_SUCCESS)
  389. return result;
  390. *outvallen = resp.val.valdat_len;
  391. *outval = malloc (*outvallen + 1);
  392. int status = YPERR_RESRC;
  393. if (__glibc_likely (*outval != NULL))
  394. {
  395. memcpy (*outval, resp.val.valdat_val, *outvallen);
  396. (*outval)[*outvallen] = '\0';
  397. status = YPERR_SUCCESS;
  398. }
  399. xdr_free ((xdrproc_t) xdr_ypresp_val, (char *) &resp);
  400. return status;
  401. }
  402. libnsl_hidden_nolink_def(yp_match, GLIBC_2_0)
  403. int
  404. yp_first (const char *indomain, const char *inmap, char **outkey,
  405. int *outkeylen, char **outval, int *outvallen)
  406. {
  407. ypreq_nokey req;
  408. ypresp_key_val resp;
  409. enum clnt_stat result;
  410. if (indomain == NULL || indomain[0] == '\0'
  411. || inmap == NULL || inmap[0] == '\0')
  412. return YPERR_BADARGS;
  413. req.domain = (char *) indomain;
  414. req.map = (char *) inmap;
  415. *outkey = *outval = NULL;
  416. *outkeylen = *outvallen = 0;
  417. memset (&resp, '\0', sizeof (resp));
  418. result = do_ypcall (indomain, YPPROC_FIRST, (xdrproc_t) xdr_ypreq_nokey,
  419. (caddr_t) &req, (xdrproc_t) xdr_ypresp_key_val,
  420. (caddr_t) &resp);
  421. if (result != RPC_SUCCESS)
  422. return YPERR_RPC;
  423. if (resp.stat != YP_TRUE)
  424. return ypprot_err (resp.stat);
  425. int status;
  426. if (__builtin_expect ((*outkey = malloc (resp.key.keydat_len + 1)) != NULL
  427. && (*outval = malloc (resp.val.valdat_len
  428. + 1)) != NULL, 1))
  429. {
  430. *outkeylen = resp.key.keydat_len;
  431. memcpy (*outkey, resp.key.keydat_val, *outkeylen);
  432. (*outkey)[*outkeylen] = '\0';
  433. *outvallen = resp.val.valdat_len;
  434. memcpy (*outval, resp.val.valdat_val, *outvallen);
  435. (*outval)[*outvallen] = '\0';
  436. status = YPERR_SUCCESS;
  437. }
  438. else
  439. {
  440. free (*outkey);
  441. status = YPERR_RESRC;
  442. }
  443. xdr_free ((xdrproc_t) xdr_ypresp_key_val, (char *) &resp);
  444. return status;
  445. }
  446. libnsl_hidden_nolink_def(yp_first, GLIBC_2_0)
  447. int
  448. yp_next (const char *indomain, const char *inmap, const char *inkey,
  449. const int inkeylen, char **outkey, int *outkeylen, char **outval,
  450. int *outvallen)
  451. {
  452. ypreq_key req;
  453. ypresp_key_val resp;
  454. enum clnt_stat result;
  455. if (indomain == NULL || indomain[0] == '\0'
  456. || inmap == NULL || inmap[0] == '\0'
  457. || inkeylen <= 0 || inkey == NULL || inkey[0] == '\0')
  458. return YPERR_BADARGS;
  459. req.domain = (char *) indomain;
  460. req.map = (char *) inmap;
  461. req.key.keydat_val = (char *) inkey;
  462. req.key.keydat_len = inkeylen;
  463. *outkey = *outval = NULL;
  464. *outkeylen = *outvallen = 0;
  465. memset (&resp, '\0', sizeof (resp));
  466. result = do_ypcall_tr (indomain, YPPROC_NEXT, (xdrproc_t) xdr_ypreq_key,
  467. (caddr_t) &req, (xdrproc_t) xdr_ypresp_key_val,
  468. (caddr_t) &resp);
  469. if (result != YPERR_SUCCESS)
  470. return result;
  471. int status;
  472. if (__builtin_expect ((*outkey = malloc (resp.key.keydat_len + 1)) != NULL
  473. && (*outval = malloc (resp.val.valdat_len
  474. + 1)) != NULL, 1))
  475. {
  476. *outkeylen = resp.key.keydat_len;
  477. memcpy (*outkey, resp.key.keydat_val, *outkeylen);
  478. (*outkey)[*outkeylen] = '\0';
  479. *outvallen = resp.val.valdat_len;
  480. memcpy (*outval, resp.val.valdat_val, *outvallen);
  481. (*outval)[*outvallen] = '\0';
  482. status = YPERR_SUCCESS;
  483. }
  484. else
  485. {
  486. free (*outkey);
  487. status = YPERR_RESRC;
  488. }
  489. xdr_free ((xdrproc_t) xdr_ypresp_key_val, (char *) &resp);
  490. return status;
  491. }
  492. libnsl_hidden_nolink_def(yp_next, GLIBC_2_0)
  493. int
  494. yp_master (const char *indomain, const char *inmap, char **outname)
  495. {
  496. ypreq_nokey req;
  497. ypresp_master resp;
  498. enum clnt_stat result;
  499. if (indomain == NULL || indomain[0] == '\0'
  500. || inmap == NULL || inmap[0] == '\0')
  501. return YPERR_BADARGS;
  502. req.domain = (char *) indomain;
  503. req.map = (char *) inmap;
  504. memset (&resp, '\0', sizeof (ypresp_master));
  505. result = do_ypcall_tr (indomain, YPPROC_MASTER, (xdrproc_t) xdr_ypreq_nokey,
  506. (caddr_t) &req, (xdrproc_t) xdr_ypresp_master,
  507. (caddr_t) &resp);
  508. if (result != YPERR_SUCCESS)
  509. return result;
  510. *outname = strdup (resp.peer);
  511. xdr_free ((xdrproc_t) xdr_ypresp_master, (char *) &resp);
  512. return *outname == NULL ? YPERR_YPERR : YPERR_SUCCESS;
  513. }
  514. libnsl_hidden_nolink_def (yp_master, GLIBC_2_0)
  515. int
  516. yp_order (const char *indomain, const char *inmap, unsigned int *outorder)
  517. {
  518. struct ypreq_nokey req;
  519. struct ypresp_order resp;
  520. enum clnt_stat result;
  521. if (indomain == NULL || indomain[0] == '\0'
  522. || inmap == NULL || inmap[0] == '\0')
  523. return YPERR_BADARGS;
  524. req.domain = (char *) indomain;
  525. req.map = (char *) inmap;
  526. memset (&resp, '\0', sizeof (resp));
  527. result = do_ypcall_tr (indomain, YPPROC_ORDER, (xdrproc_t) xdr_ypreq_nokey,
  528. (caddr_t) &req, (xdrproc_t) xdr_ypresp_order,
  529. (caddr_t) &resp);
  530. if (result != YPERR_SUCCESS)
  531. return result;
  532. *outorder = resp.ordernum;
  533. xdr_free ((xdrproc_t) xdr_ypresp_order, (char *) &resp);
  534. return result;
  535. }
  536. libnsl_hidden_nolink_def(yp_order, GLIBC_2_0)
  537. struct ypresp_all_data
  538. {
  539. unsigned long status;
  540. void *data;
  541. int (*foreach) (int status, char *key, int keylen,
  542. char *val, int vallen, char *data);
  543. };
  544. static bool_t
  545. __xdr_ypresp_all (XDR *xdrs, struct ypresp_all_data *objp)
  546. {
  547. while (1)
  548. {
  549. struct ypresp_all resp;
  550. memset (&resp, '\0', sizeof (struct ypresp_all));
  551. if (!xdr_ypresp_all (xdrs, &resp))
  552. {
  553. xdr_free ((xdrproc_t) xdr_ypresp_all, (char *) &resp);
  554. objp->status = YP_YPERR;
  555. return FALSE;
  556. }
  557. if (resp.more == 0)
  558. {
  559. xdr_free ((xdrproc_t) xdr_ypresp_all, (char *) &resp);
  560. objp->status = YP_NOMORE;
  561. return TRUE;
  562. }
  563. switch (resp.ypresp_all_u.val.stat)
  564. {
  565. case YP_TRUE:
  566. {
  567. char key[resp.ypresp_all_u.val.key.keydat_len + 1];
  568. char val[resp.ypresp_all_u.val.val.valdat_len + 1];
  569. int keylen = resp.ypresp_all_u.val.key.keydat_len;
  570. int vallen = resp.ypresp_all_u.val.val.valdat_len;
  571. /* We are not allowed to modify the key and val data.
  572. But we are allowed to add data behind the buffer,
  573. if we don't modify the length. So add an extra NUL
  574. character to avoid trouble with broken code. */
  575. objp->status = YP_TRUE;
  576. *((char *) __mempcpy (key, resp.ypresp_all_u.val.key.keydat_val,
  577. keylen)) = '\0';
  578. *((char *) __mempcpy (val, resp.ypresp_all_u.val.val.valdat_val,
  579. vallen)) = '\0';
  580. xdr_free ((xdrproc_t) xdr_ypresp_all, (char *) &resp);
  581. if ((*objp->foreach) (objp->status, key, keylen,
  582. val, vallen, objp->data))
  583. return TRUE;
  584. }
  585. break;
  586. default:
  587. objp->status = resp.ypresp_all_u.val.stat;
  588. xdr_free ((xdrproc_t) xdr_ypresp_all, (char *) &resp);
  589. /* Sun says we don't need to make this call, but must return
  590. immediately. Since Solaris makes this call, we will call
  591. the callback function, too. */
  592. (*objp->foreach) (objp->status, NULL, 0, NULL, 0, objp->data);
  593. return TRUE;
  594. }
  595. }
  596. }
  597. int
  598. yp_all (const char *indomain, const char *inmap,
  599. const struct ypall_callback *incallback)
  600. {
  601. struct ypreq_nokey req;
  602. dom_binding *ydb = NULL;
  603. int try, res;
  604. enum clnt_stat result;
  605. struct sockaddr_in clnt_sin;
  606. CLIENT *clnt;
  607. struct ypresp_all_data data;
  608. int clnt_sock;
  609. int saved_errno = errno;
  610. if (indomain == NULL || indomain[0] == '\0'
  611. || inmap == NULL || inmap[0] == '\0')
  612. return YPERR_BADARGS;
  613. try = 0;
  614. res = YPERR_YPERR;
  615. while (try < MAXTRIES && res != YPERR_SUCCESS)
  616. {
  617. if (__yp_bind (indomain, &ydb) != 0)
  618. {
  619. __set_errno (saved_errno);
  620. return YPERR_DOMAIN;
  621. }
  622. clnt_sock = RPC_ANYSOCK;
  623. clnt_sin = ydb->dom_server_addr;
  624. clnt_sin.sin_port = 0;
  625. /* We don't need the UDP connection anymore. */
  626. __yp_unbind (ydb);
  627. ydb = NULL;
  628. clnt = clnttcp_create (&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0);
  629. if (clnt == NULL)
  630. {
  631. __set_errno (saved_errno);
  632. return YPERR_PMAP;
  633. }
  634. req.domain = (char *) indomain;
  635. req.map = (char *) inmap;
  636. data.foreach = incallback->foreach;
  637. data.data = (void *) incallback->data;
  638. result = clnt_call (clnt, YPPROC_ALL, (xdrproc_t) xdr_ypreq_nokey,
  639. (caddr_t) &req, (xdrproc_t) __xdr_ypresp_all,
  640. (caddr_t) &data, RPCTIMEOUT);
  641. if (__glibc_unlikely (result != RPC_SUCCESS))
  642. {
  643. /* Print the error message only on the last try. */
  644. if (try == MAXTRIES - 1)
  645. clnt_perror (clnt, "yp_all: clnt_call");
  646. res = YPERR_RPC;
  647. }
  648. else
  649. res = YPERR_SUCCESS;
  650. clnt_destroy (clnt);
  651. if (res == YPERR_SUCCESS && data.status != YP_NOMORE)
  652. {
  653. __set_errno (saved_errno);
  654. return ypprot_err (data.status);
  655. }
  656. ++try;
  657. }
  658. __set_errno (saved_errno);
  659. return res;
  660. }
  661. libnsl_hidden_nolink_def (yp_all, GLIBC_2_0)
  662. int
  663. yp_maplist (const char *indomain, struct ypmaplist **outmaplist)
  664. {
  665. struct ypresp_maplist resp;
  666. enum clnt_stat result;
  667. if (indomain == NULL || indomain[0] == '\0')
  668. return YPERR_BADARGS;
  669. memset (&resp, '\0', sizeof (resp));
  670. result = do_ypcall_tr (indomain, YPPROC_MAPLIST, (xdrproc_t) xdr_domainname,
  671. (caddr_t) &indomain, (xdrproc_t) xdr_ypresp_maplist,
  672. (caddr_t) &resp);
  673. if (__glibc_likely (result == YPERR_SUCCESS))
  674. {
  675. *outmaplist = resp.maps;
  676. /* We don't free the list, this will be done by ypserv
  677. xdr_free((xdrproc_t)xdr_ypresp_maplist, (char *)&resp); */
  678. }
  679. return result;
  680. }
  681. libnsl_hidden_nolink_def (yp_maplist, GLIBC_2_0)
  682. const char *
  683. yperr_string (const int error)
  684. {
  685. const char *str;
  686. switch (error)
  687. {
  688. case YPERR_SUCCESS:
  689. str = N_("Success");
  690. break;
  691. case YPERR_BADARGS:
  692. str = N_("Request arguments bad");
  693. break;
  694. case YPERR_RPC:
  695. str = N_("RPC failure on NIS operation");
  696. break;
  697. case YPERR_DOMAIN:
  698. str = N_("Can't bind to server which serves this domain");
  699. break;
  700. case YPERR_MAP:
  701. str = N_("No such map in server's domain");
  702. break;
  703. case YPERR_KEY:
  704. str = N_("No such key in map");
  705. break;
  706. case YPERR_YPERR:
  707. str = N_("Internal NIS error");
  708. break;
  709. case YPERR_RESRC:
  710. str = N_("Local resource allocation failure");
  711. break;
  712. case YPERR_NOMORE:
  713. str = N_("No more records in map database");
  714. break;
  715. case YPERR_PMAP:
  716. str = N_("Can't communicate with portmapper");
  717. break;
  718. case YPERR_YPBIND:
  719. str = N_("Can't communicate with ypbind");
  720. break;
  721. case YPERR_YPSERV:
  722. str = N_("Can't communicate with ypserv");
  723. break;
  724. case YPERR_NODOM:
  725. str = N_("Local domain name not set");
  726. break;
  727. case YPERR_BADDB:
  728. str = N_("NIS map database is bad");
  729. break;
  730. case YPERR_VERS:
  731. str = N_("NIS client/server version mismatch - can't supply service");
  732. break;
  733. case YPERR_ACCESS:
  734. str = N_("Permission denied");
  735. break;
  736. case YPERR_BUSY:
  737. str = N_("Database is busy");
  738. break;
  739. default:
  740. str = N_("Unknown NIS error code");
  741. break;
  742. }
  743. return _(str);
  744. }
  745. libnsl_hidden_nolink_def(yperr_string, GLIBC_2_0)
  746. static const int8_t yp_2_yperr[] =
  747. {
  748. #define YP2YPERR(yp, yperr) [YP_##yp - YP_VERS] = YPERR_##yperr
  749. YP2YPERR (TRUE, SUCCESS),
  750. YP2YPERR (NOMORE, NOMORE),
  751. YP2YPERR (FALSE, YPERR),
  752. YP2YPERR (NOMAP, MAP),
  753. YP2YPERR (NODOM, DOMAIN),
  754. YP2YPERR (NOKEY, KEY),
  755. YP2YPERR (BADOP, YPERR),
  756. YP2YPERR (BADDB, BADDB),
  757. YP2YPERR (YPERR, YPERR),
  758. YP2YPERR (BADARGS, BADARGS),
  759. YP2YPERR (VERS, VERS)
  760. };
  761. int
  762. ypprot_err (const int code)
  763. {
  764. if (code < YP_VERS || code > YP_NOMORE)
  765. return YPERR_YPERR;
  766. return yp_2_yperr[code - YP_VERS];
  767. }
  768. libnsl_hidden_nolink_def (ypprot_err, GLIBC_2_0)
  769. const char *
  770. ypbinderr_string (const int error)
  771. {
  772. const char *str;
  773. switch (error)
  774. {
  775. case 0:
  776. str = N_("Success");
  777. break;
  778. case YPBIND_ERR_ERR:
  779. str = N_("Internal ypbind error");
  780. break;
  781. case YPBIND_ERR_NOSERV:
  782. str = N_("Domain not bound");
  783. break;
  784. case YPBIND_ERR_RESC:
  785. str = N_("System resource allocation failure");
  786. break;
  787. default:
  788. str = N_("Unknown ypbind error");
  789. break;
  790. }
  791. return _(str);
  792. }
  793. libnsl_hidden_nolink_def (ypbinderr_string, GLIBC_2_0)
  794. #define WINDOW 60
  795. int
  796. yp_update (char *domain, char *map, unsigned ypop,
  797. char *key, int keylen, char *data, int datalen)
  798. {
  799. union
  800. {
  801. ypupdate_args update_args;
  802. ypdelete_args delete_args;
  803. }
  804. args;
  805. xdrproc_t xdr_argument;
  806. unsigned res = 0;
  807. CLIENT *clnt;
  808. char *master;
  809. struct sockaddr saddr;
  810. char servername[MAXNETNAMELEN + 1];
  811. int r;
  812. if (!domain || !map || !key || (ypop != YPOP_DELETE && !data))
  813. return YPERR_BADARGS;
  814. args.update_args.mapname = map;
  815. args.update_args.key.yp_buf_len = keylen;
  816. args.update_args.key.yp_buf_val = key;
  817. args.update_args.datum.yp_buf_len = datalen;
  818. args.update_args.datum.yp_buf_val = data;
  819. if ((r = yp_master (domain, map, &master)) != YPERR_SUCCESS)
  820. return r;
  821. if (!host2netname (servername, master, domain))
  822. {
  823. fputs (_("yp_update: cannot convert host to netname\n"), stderr);
  824. free (master);
  825. return YPERR_YPERR;
  826. }
  827. clnt = clnt_create (master, YPU_PROG, YPU_VERS, "tcp");
  828. /* We do not need the string anymore. */
  829. free (master);
  830. if (clnt == NULL)
  831. {
  832. clnt_pcreateerror ("yp_update: clnt_create");
  833. return YPERR_RPC;
  834. }
  835. if (!clnt_control (clnt, CLGET_SERVER_ADDR, (char *) &saddr))
  836. {
  837. fputs (_("yp_update: cannot get server address\n"), stderr);
  838. return YPERR_RPC;
  839. }
  840. switch (ypop)
  841. {
  842. case YPOP_CHANGE:
  843. case YPOP_INSERT:
  844. case YPOP_STORE:
  845. xdr_argument = (xdrproc_t) xdr_ypupdate_args;
  846. break;
  847. case YPOP_DELETE:
  848. xdr_argument = (xdrproc_t) xdr_ypdelete_args;
  849. break;
  850. default:
  851. return YPERR_BADARGS;
  852. break;
  853. }
  854. clnt->cl_auth = authdes_create (servername, WINDOW, &saddr, NULL);
  855. if (clnt->cl_auth == NULL)
  856. clnt->cl_auth = authunix_create_default ();
  857. again:
  858. r = clnt_call (clnt, ypop, xdr_argument, (caddr_t) &args,
  859. (xdrproc_t) xdr_u_int, (caddr_t) &res, RPCTIMEOUT);
  860. if (r == RPC_AUTHERROR)
  861. {
  862. if (clnt->cl_auth->ah_cred.oa_flavor == AUTH_DES)
  863. {
  864. auth_destroy (clnt->cl_auth);
  865. clnt->cl_auth = authunix_create_default ();
  866. goto again;
  867. }
  868. else
  869. return YPERR_ACCESS;
  870. }
  871. if (r != RPC_SUCCESS)
  872. {
  873. clnt_perror (clnt, "yp_update: clnt_call");
  874. return YPERR_RPC;
  875. }
  876. return res;
  877. }
  878. libnsl_hidden_nolink_def(yp_update, GLIBC_2_0)