rcmd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*
  2. * Copyright (C) 1998 WIDE Project.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the project nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Copyright (c) 1983, 1993, 1994
  31. * The Regents of the University of California. All rights reserved.
  32. *
  33. * Redistribution and use in source and binary forms, with or without
  34. * modification, are permitted provided that the following conditions
  35. * are met:
  36. * 1. Redistributions of source code must retain the above copyright
  37. * notice, this list of conditions and the following disclaimer.
  38. * 2. Redistributions in binary form must reproduce the above copyright
  39. * notice, this list of conditions and the following disclaimer in the
  40. * documentation and/or other materials provided with the distribution.
  41. * 4. Neither the name of the University nor the names of its contributors
  42. * may be used to endorse or promote products derived from this software
  43. * without specific prior written permission.
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  46. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  48. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  49. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  50. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  51. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  52. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  53. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  54. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  55. * SUCH DAMAGE.
  56. */
  57. #include <sys/param.h>
  58. #include <sys/poll.h>
  59. #include <sys/socket.h>
  60. #include <sys/stat.h>
  61. #include <netinet/in.h>
  62. #include <arpa/inet.h>
  63. #include <alloca.h>
  64. #include <signal.h>
  65. #include <fcntl.h>
  66. #include <netdb.h>
  67. #include <unistd.h>
  68. #include <pwd.h>
  69. #include <errno.h>
  70. #include <stdio.h>
  71. #include <stdio_ext.h>
  72. #include <ctype.h>
  73. #include <string.h>
  74. #include <libintl.h>
  75. #include <stdlib.h>
  76. #include <wchar.h>
  77. #include <sys/uio.h>
  78. #include <sigsetops.h>
  79. int __ivaliduser (FILE *, uint32_t, const char *, const char *);
  80. static int __validuser2_sa (FILE *, struct sockaddr *, size_t,
  81. const char *, const char *, const char *);
  82. static int ruserok2_sa (struct sockaddr *ra, size_t ralen,
  83. int superuser, const char *ruser,
  84. const char *luser, const char *rhost);
  85. static int ruserok_sa (struct sockaddr *ra, size_t ralen,
  86. int superuser, const char *ruser,
  87. const char *luser);
  88. int iruserok_af (const void *raddr, int superuser, const char *ruser,
  89. const char *luser, sa_family_t af);
  90. int iruserok (uint32_t raddr, int superuser, const char *ruser,
  91. const char *luser);
  92. libc_hidden_proto (iruserok_af)
  93. libc_freeres_ptr(static char *ahostbuf);
  94. int
  95. rcmd_af (char **ahost, u_short rport, const char *locuser, const char *remuser,
  96. const char *cmd, int *fd2p, sa_family_t af)
  97. {
  98. char paddr[INET6_ADDRSTRLEN];
  99. struct addrinfo hints, *res, *ai;
  100. union
  101. {
  102. struct sockaddr sa;
  103. struct sockaddr_storage ss;
  104. struct sockaddr_in sin;
  105. struct sockaddr_in6 sin6;
  106. } from;
  107. struct pollfd pfd[2];
  108. sigset_t mask, omask;
  109. pid_t pid;
  110. int s, lport, timo, error;
  111. char c;
  112. int refused;
  113. char num[8];
  114. ssize_t n;
  115. if (af != AF_INET && af != AF_INET6 && af != AF_UNSPEC)
  116. {
  117. __set_errno (EAFNOSUPPORT);
  118. return -1;
  119. }
  120. pid = __getpid();
  121. memset(&hints, '\0', sizeof(hints));
  122. hints.ai_flags = AI_CANONNAME;
  123. hints.ai_family = af;
  124. hints.ai_socktype = SOCK_STREAM;
  125. (void)__snprintf(num, sizeof(num), "%d", ntohs(rport));
  126. error = getaddrinfo(*ahost, num, &hints, &res);
  127. if (error) {
  128. if (error == EAI_NONAME && *ahost != NULL)
  129. __fxprintf(NULL, "%s: Unknown host\n", *ahost);
  130. else
  131. __fxprintf(NULL, "rcmd: getaddrinfo: %s\n",
  132. gai_strerror(error));
  133. return -1;
  134. }
  135. pfd[0].events = POLLIN;
  136. pfd[1].events = POLLIN;
  137. if (res->ai_canonname){
  138. free (ahostbuf);
  139. ahostbuf = __strdup (res->ai_canonname);
  140. if (ahostbuf == NULL) {
  141. __fxprintf(NULL, "%s",
  142. _("rcmd: Cannot allocate memory\n"));
  143. return -1;
  144. }
  145. *ahost = ahostbuf;
  146. } else
  147. *ahost = NULL;
  148. ai = res;
  149. refused = 0;
  150. __sigemptyset(&mask);
  151. __sigaddset(&mask, SIGURG);
  152. __sigprocmask (SIG_BLOCK, &mask, &omask);
  153. for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
  154. char errbuf[200];
  155. s = rresvport_af(&lport, ai->ai_family);
  156. if (s < 0) {
  157. if (errno == EAGAIN)
  158. __fxprintf(NULL, "%s", _("\
  159. rcmd: socket: All ports in use\n"));
  160. else
  161. __fxprintf(NULL, "rcmd: socket: %m\n");
  162. __sigprocmask (SIG_SETMASK, &omask, 0);
  163. freeaddrinfo(res);
  164. return -1;
  165. }
  166. __fcntl(s, F_SETOWN, pid);
  167. if (__connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
  168. break;
  169. (void)__close(s);
  170. if (errno == EADDRINUSE) {
  171. lport--;
  172. continue;
  173. }
  174. if (errno == ECONNREFUSED)
  175. refused = 1;
  176. if (ai->ai_next != NULL) {
  177. int oerrno = errno;
  178. char *buf = NULL;
  179. getnameinfo(ai->ai_addr, ai->ai_addrlen,
  180. paddr, sizeof(paddr),
  181. NULL, 0,
  182. NI_NUMERICHOST);
  183. if (__asprintf (&buf, _("connect to address %s: "),
  184. paddr) >= 0)
  185. {
  186. __fxprintf(NULL, "%s", buf);
  187. free (buf);
  188. }
  189. __set_errno (oerrno);
  190. perror(0);
  191. ai = ai->ai_next;
  192. getnameinfo(ai->ai_addr, ai->ai_addrlen,
  193. paddr, sizeof(paddr),
  194. NULL, 0,
  195. NI_NUMERICHOST);
  196. if (__asprintf (&buf, _("Trying %s...\n"), paddr) >= 0)
  197. {
  198. __fxprintf (NULL, "%s", buf);
  199. free (buf);
  200. }
  201. continue;
  202. }
  203. if (refused && timo <= 16) {
  204. (void)__sleep(timo);
  205. timo *= 2;
  206. ai = res;
  207. refused = 0;
  208. continue;
  209. }
  210. freeaddrinfo(res);
  211. (void)__fxprintf(NULL, "%s: %s\n", *ahost,
  212. __strerror_r(errno, errbuf, sizeof (errbuf)));
  213. __sigprocmask (SIG_SETMASK, &omask, 0);
  214. return -1;
  215. }
  216. lport--;
  217. if (fd2p == 0) {
  218. __write(s, "", 1);
  219. lport = 0;
  220. } else {
  221. char num[8];
  222. int s2 = rresvport_af(&lport, ai->ai_family), s3;
  223. socklen_t len = ai->ai_addrlen;
  224. if (s2 < 0)
  225. goto bad;
  226. __listen(s2, 1);
  227. (void)__snprintf(num, sizeof(num), "%d", lport);
  228. if (__write(s, num, strlen(num)+1) != (ssize_t)strlen(num)+1) {
  229. char *buf = NULL;
  230. if (__asprintf (&buf, _("\
  231. rcmd: write (setting up stderr): %m\n")) >= 0)
  232. {
  233. __fxprintf(NULL, "%s", buf);
  234. free (buf);
  235. }
  236. (void)__close(s2);
  237. goto bad;
  238. }
  239. pfd[0].fd = s;
  240. pfd[1].fd = s2;
  241. __set_errno (0);
  242. if (__poll (pfd, 2, -1) < 1 || (pfd[1].revents & POLLIN) == 0){
  243. char *buf = NULL;
  244. if ((errno != 0
  245. && __asprintf(&buf, _("\
  246. rcmd: poll (setting up stderr): %m\n")) >= 0)
  247. || (errno == 0
  248. && __asprintf(&buf, _("\
  249. poll: protocol failure in circuit setup\n")) >= 0))
  250. {
  251. __fxprintf (NULL, "%s", buf);
  252. free (buf);
  253. }
  254. (void)__close(s2);
  255. goto bad;
  256. }
  257. s3 = TEMP_FAILURE_RETRY (accept(s2, &from.sa, &len));
  258. switch (from.sa.sa_family) {
  259. case AF_INET:
  260. rport = ntohs(from.sin.sin_port);
  261. break;
  262. case AF_INET6:
  263. rport = ntohs(from.sin6.sin6_port);
  264. break;
  265. default:
  266. rport = 0;
  267. break;
  268. }
  269. (void)__close(s2);
  270. if (s3 < 0) {
  271. (void)__fxprintf(NULL, "rcmd: accept: %m\n");
  272. lport = 0;
  273. goto bad;
  274. }
  275. *fd2p = s3;
  276. if (rport >= IPPORT_RESERVED || rport < IPPORT_RESERVED / 2){
  277. char *buf = NULL;
  278. if (__asprintf(&buf, _("\
  279. socket: protocol failure in circuit setup\n")) >= 0)
  280. {
  281. __fxprintf (NULL, "%s", buf);
  282. free (buf);
  283. }
  284. goto bad2;
  285. }
  286. }
  287. struct iovec iov[3] =
  288. {
  289. [0] = { .iov_base = (void *) locuser,
  290. .iov_len = strlen (locuser) + 1 },
  291. [1] = { .iov_base = (void *) remuser,
  292. .iov_len = strlen (remuser) + 1 },
  293. [2] = { .iov_base = (void *) cmd,
  294. .iov_len = strlen (cmd) + 1 }
  295. };
  296. (void) TEMP_FAILURE_RETRY (__writev (s, iov, 3));
  297. n = TEMP_FAILURE_RETRY (__read(s, &c, 1));
  298. if (n != 1) {
  299. char *buf = NULL;
  300. if ((n == 0
  301. && __asprintf(&buf, _("rcmd: %s: short read"),
  302. *ahost) >= 0)
  303. || (n != 0
  304. && __asprintf(&buf, "rcmd: %s: %m\n", *ahost) >= 0))
  305. {
  306. __fxprintf (NULL, "%s", buf);
  307. free (buf);
  308. }
  309. goto bad2;
  310. }
  311. if (c != 0) {
  312. while (__read(s, &c, 1) == 1) {
  313. (void)__write(STDERR_FILENO, &c, 1);
  314. if (c == '\n')
  315. break;
  316. }
  317. goto bad2;
  318. }
  319. __sigprocmask (SIG_SETMASK, &omask, 0);
  320. freeaddrinfo(res);
  321. return s;
  322. bad2:
  323. if (lport)
  324. (void)__close(*fd2p);
  325. bad:
  326. (void)__close(s);
  327. __sigprocmask (SIG_SETMASK, &omask, 0);
  328. freeaddrinfo(res);
  329. return -1;
  330. }
  331. libc_hidden_def (rcmd_af)
  332. int
  333. rcmd (char **ahost, u_short rport, const char *locuser, const char *remuser,
  334. const char *cmd, int *fd2p)
  335. {
  336. return rcmd_af (ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
  337. }
  338. int
  339. rresvport_af (int *alport, sa_family_t family)
  340. {
  341. union {
  342. struct sockaddr generic;
  343. struct sockaddr_in in;
  344. struct sockaddr_in6 in6;
  345. } ss;
  346. int s;
  347. size_t len;
  348. uint16_t *sport;
  349. switch(family){
  350. case AF_INET:
  351. len = sizeof(struct sockaddr_in);
  352. sport = &ss.in.sin_port;
  353. break;
  354. case AF_INET6:
  355. len = sizeof(struct sockaddr_in6);
  356. sport = &ss.in6.sin6_port;
  357. break;
  358. default:
  359. __set_errno (EAFNOSUPPORT);
  360. return -1;
  361. }
  362. /* NB: No SOCK_CLOEXEC for backwards compatibility. */
  363. s = __socket(family, SOCK_STREAM, 0);
  364. if (s < 0)
  365. return -1;
  366. memset (&ss, '\0', sizeof(ss));
  367. #ifdef SALEN
  368. ss.generic.__ss_len = len;
  369. #endif
  370. ss.generic.sa_family = family;
  371. /* Ignore invalid values. */
  372. if (*alport < IPPORT_RESERVED / 2)
  373. *alport = IPPORT_RESERVED / 2;
  374. else if (*alport >= IPPORT_RESERVED)
  375. *alport = IPPORT_RESERVED - 1;
  376. int start = *alport;
  377. do {
  378. *sport = htons((uint16_t) *alport);
  379. if (__bind(s, &ss.generic, len) >= 0)
  380. return s;
  381. if (errno != EADDRINUSE) {
  382. (void)__close(s);
  383. return -1;
  384. }
  385. if ((*alport)-- == IPPORT_RESERVED/2)
  386. *alport = IPPORT_RESERVED - 1;
  387. } while (*alport != start);
  388. (void)__close(s);
  389. __set_errno (EAGAIN);
  390. return -1;
  391. }
  392. libc_hidden_def (rresvport_af)
  393. int
  394. rresvport (int *alport)
  395. {
  396. return rresvport_af(alport, AF_INET);
  397. }
  398. int __check_rhosts_file = 1;
  399. char *__rcmd_errstr;
  400. int
  401. ruserok_af (const char *rhost, int superuser, const char *ruser,
  402. const char *luser, sa_family_t af)
  403. {
  404. struct addrinfo hints, *res, *res0;
  405. int gai;
  406. int ret;
  407. memset (&hints, '\0', sizeof(hints));
  408. hints.ai_family = af;
  409. gai = getaddrinfo(rhost, NULL, &hints, &res0);
  410. if (gai)
  411. return -1;
  412. ret = -1;
  413. for (res=res0; res; res=res->ai_next)
  414. if (ruserok2_sa(res->ai_addr, res->ai_addrlen,
  415. superuser, ruser, luser, rhost) == 0){
  416. ret = 0;
  417. break;
  418. }
  419. freeaddrinfo(res0);
  420. return (ret);
  421. }
  422. libc_hidden_def (ruserok_af)
  423. int
  424. ruserok (const char *rhost, int superuser, const char *ruser,
  425. const char *luser)
  426. {
  427. return ruserok_af(rhost, superuser, ruser, luser, AF_INET);
  428. }
  429. /* Extremely paranoid file open function. */
  430. static FILE *
  431. iruserfopen (const char *file, uid_t okuser)
  432. {
  433. struct stat64 st;
  434. char *cp = NULL;
  435. FILE *res = NULL;
  436. /* If not a regular file, if owned by someone other than user or
  437. root, if writeable by anyone but the owner, or if hardlinked
  438. anywhere, quit. */
  439. if (__lxstat64 (_STAT_VER, file, &st))
  440. cp = _("lstat failed");
  441. else if (!S_ISREG (st.st_mode))
  442. cp = _("not regular file");
  443. else
  444. {
  445. res = fopen (file, "rce");
  446. if (!res)
  447. cp = _("cannot open");
  448. else if (__fxstat64 (_STAT_VER, fileno (res), &st) < 0)
  449. cp = _("fstat failed");
  450. else if (st.st_uid && st.st_uid != okuser)
  451. cp = _("bad owner");
  452. else if (st.st_mode & (S_IWGRP|S_IWOTH))
  453. cp = _("writeable by other than owner");
  454. else if (st.st_nlink > 1)
  455. cp = _("hard linked somewhere");
  456. }
  457. /* If there were any problems, quit. */
  458. if (cp != NULL)
  459. {
  460. __rcmd_errstr = cp;
  461. if (res)
  462. fclose (res);
  463. return NULL;
  464. }
  465. /* No threads use this stream. */
  466. __fsetlocking (res, FSETLOCKING_BYCALLER);
  467. return res;
  468. }
  469. /*
  470. * New .rhosts strategy: We are passed an ip address. We spin through
  471. * hosts.equiv and .rhosts looking for a match. When the .rhosts only
  472. * has ip addresses, we don't have to trust a nameserver. When it
  473. * contains hostnames, we spin through the list of addresses the nameserver
  474. * gives us and look for a match.
  475. *
  476. * Returns 0 if ok, -1 if not ok.
  477. */
  478. static int
  479. ruserok2_sa (struct sockaddr *ra, size_t ralen, int superuser,
  480. const char *ruser, const char *luser, const char *rhost)
  481. {
  482. FILE *hostf = NULL;
  483. int isbad = -1;
  484. if (!superuser)
  485. hostf = iruserfopen (_PATH_HEQUIV, 0);
  486. if (hostf)
  487. {
  488. isbad = __validuser2_sa (hostf, ra, ralen, luser, ruser, rhost);
  489. fclose (hostf);
  490. if (!isbad)
  491. return 0;
  492. }
  493. if (__check_rhosts_file || superuser)
  494. {
  495. char *pbuf;
  496. struct passwd pwdbuf, *pwd;
  497. size_t dirlen;
  498. size_t buflen = __sysconf (_SC_GETPW_R_SIZE_MAX);
  499. char *buffer = __alloca (buflen);
  500. uid_t uid;
  501. if (__getpwnam_r (luser, &pwdbuf, buffer, buflen, &pwd) != 0
  502. || pwd == NULL)
  503. return -1;
  504. dirlen = strlen (pwd->pw_dir);
  505. pbuf = alloca (dirlen + sizeof "/.rhosts");
  506. __mempcpy (__mempcpy (pbuf, pwd->pw_dir, dirlen),
  507. "/.rhosts", sizeof "/.rhosts");
  508. /* Change effective uid while reading .rhosts. If root and
  509. reading an NFS mounted file system, can't read files that
  510. are protected read/write owner only. */
  511. uid = __geteuid ();
  512. seteuid (pwd->pw_uid);
  513. hostf = iruserfopen (pbuf, pwd->pw_uid);
  514. if (hostf != NULL)
  515. {
  516. isbad = __validuser2_sa (hostf, ra, ralen, luser, ruser, rhost);
  517. fclose (hostf);
  518. }
  519. seteuid (uid);
  520. return isbad;
  521. }
  522. return -1;
  523. }
  524. /*
  525. * ruserok_sa() is now discussed on ipng, so
  526. * currently disabled for external use
  527. */
  528. static int
  529. ruserok_sa (struct sockaddr *ra, size_t ralen, int superuser,
  530. const char *ruser, const char *luser)
  531. {
  532. return ruserok2_sa(ra, ralen, superuser, ruser, luser, "-");
  533. }
  534. /* This is the exported version. */
  535. int
  536. iruserok_af (const void *raddr, int superuser, const char *ruser,
  537. const char *luser, sa_family_t af)
  538. {
  539. union {
  540. struct sockaddr generic;
  541. struct sockaddr_in in;
  542. struct sockaddr_in6 in6;
  543. } ra;
  544. size_t ralen;
  545. memset (&ra, '\0', sizeof(ra));
  546. switch (af){
  547. case AF_INET:
  548. ra.in.sin_family = AF_INET;
  549. memcpy (&ra.in.sin_addr, raddr, sizeof(struct in_addr));
  550. ralen = sizeof(struct sockaddr_in);
  551. break;
  552. case AF_INET6:
  553. ra.in6.sin6_family = AF_INET6;
  554. memcpy (&ra.in6.sin6_addr, raddr, sizeof(struct in6_addr));
  555. ralen = sizeof(struct sockaddr_in6);
  556. break;
  557. default:
  558. return 0;
  559. }
  560. return ruserok_sa (&ra.generic, ralen, superuser, ruser, luser);
  561. }
  562. libc_hidden_def (iruserok_af)
  563. int
  564. iruserok (uint32_t raddr, int superuser, const char *ruser, const char *luser)
  565. {
  566. return iruserok_af (&raddr, superuser, ruser, luser, AF_INET);
  567. }
  568. /*
  569. * XXX
  570. * Don't make static, used by lpd(8).
  571. *
  572. * This function is not used anymore. It is only present because lpd(8)
  573. * calls it (!?!). We simply call __invaliduser2() with an illegal rhost
  574. * argument. This means that netgroups won't work in .rhost/hosts.equiv
  575. * files. If you want lpd to work with netgroups, fix lpd to use ruserok()
  576. * or PAM.
  577. * Returns 0 if ok, -1 if not ok.
  578. */
  579. int
  580. __ivaliduser (FILE *hostf, uint32_t raddr, const char *luser,
  581. const char *ruser)
  582. {
  583. struct sockaddr_in ra;
  584. memset(&ra, '\0', sizeof(ra));
  585. ra.sin_family = AF_INET;
  586. ra.sin_addr.s_addr = raddr;
  587. return __validuser2_sa(hostf, (struct sockaddr *)&ra, sizeof(ra),
  588. luser, ruser, "-");
  589. }
  590. /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
  591. static int
  592. __checkhost_sa (struct sockaddr *ra, size_t ralen, char *lhost,
  593. const char *rhost)
  594. {
  595. struct addrinfo hints, *res0, *res;
  596. char raddr[INET6_ADDRSTRLEN];
  597. int match;
  598. int negate=1; /* Multiply return with this to get -1 instead of 1 */
  599. /* Check nis netgroup. */
  600. if (strncmp ("+@", lhost, 2) == 0)
  601. return innetgr (&lhost[2], rhost, NULL, NULL);
  602. if (strncmp ("-@", lhost, 2) == 0)
  603. return -innetgr (&lhost[2], rhost, NULL, NULL);
  604. /* -host */
  605. if (strncmp ("-", lhost,1) == 0) {
  606. negate = -1;
  607. lhost++;
  608. } else if (strcmp ("+",lhost) == 0) {
  609. return 1; /* asking for trouble, but ok.. */
  610. }
  611. /* Try for raw ip address first. */
  612. /* XXX */
  613. if (getnameinfo(ra, ralen,
  614. raddr, sizeof(raddr), NULL, 0,
  615. NI_NUMERICHOST) == 0
  616. && strcmp(raddr, lhost) == 0)
  617. return negate;
  618. /* Better be a hostname. */
  619. match = 0;
  620. memset(&hints, '\0', sizeof(hints));
  621. hints.ai_family = ra->sa_family;
  622. if (getaddrinfo(lhost, NULL, &hints, &res0) == 0){
  623. /* Spin through ip addresses. */
  624. for (res = res0; res; res = res->ai_next)
  625. {
  626. if (res->ai_family == ra->sa_family
  627. && !memcmp(res->ai_addr, ra, res->ai_addrlen))
  628. {
  629. match = 1;
  630. break;
  631. }
  632. }
  633. freeaddrinfo (res0);
  634. }
  635. return negate * match;
  636. }
  637. /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
  638. static int
  639. __icheckuser (const char *luser, const char *ruser)
  640. {
  641. /*
  642. luser is user entry from .rhosts/hosts.equiv file
  643. ruser is user id on remote host
  644. */
  645. /* [-+]@netgroup */
  646. if (strncmp ("+@", luser, 2) == 0)
  647. return innetgr (&luser[2], NULL, ruser, NULL);
  648. if (strncmp ("-@", luser,2) == 0)
  649. return -innetgr (&luser[2], NULL, ruser, NULL);
  650. /* -user */
  651. if (strncmp ("-", luser, 1) == 0)
  652. return -(strcmp (&luser[1], ruser) == 0);
  653. /* + */
  654. if (strcmp ("+", luser) == 0)
  655. return 1;
  656. /* simple string match */
  657. return strcmp (ruser, luser) == 0;
  658. }
  659. /*
  660. * Returns 1 for blank lines (or only comment lines) and 0 otherwise
  661. */
  662. static int
  663. __isempty (char *p)
  664. {
  665. while (*p && isspace (*p)) {
  666. ++p;
  667. }
  668. return (*p == '\0' || *p == '#') ? 1 : 0 ;
  669. }
  670. /*
  671. * Returns 0 if positive match, -1 if _not_ ok.
  672. */
  673. static int
  674. __validuser2_sa (FILE *hostf, struct sockaddr *ra, size_t ralen,
  675. const char *luser, const char *ruser, const char *rhost)
  676. {
  677. const char *user;
  678. char *p;
  679. int hcheck, ucheck;
  680. char *buf = NULL;
  681. size_t bufsize = 0;
  682. int retval = -1;
  683. while (__getline (&buf, &bufsize, hostf) > 0) {
  684. buf[bufsize - 1] = '\0'; /* Make sure it's terminated. */
  685. p = buf;
  686. /* Skip empty or comment lines */
  687. if (__isempty (p)) {
  688. continue;
  689. }
  690. for (;*p && !isspace(*p); ++p) {
  691. *p = _tolower (*p);
  692. }
  693. /* Next we want to find the permitted name for the remote user. */
  694. if (*p == ' ' || *p == '\t') {
  695. /* <nul> terminate hostname and skip spaces */
  696. for (*p++='\0'; *p && isspace (*p); ++p);
  697. user = p; /* this is the user's name */
  698. while (*p && !isspace (*p))
  699. ++p; /* find end of user's name */
  700. } else
  701. user = p;
  702. *p = '\0'; /* <nul> terminate username (+host?) */
  703. /* buf -> host(?) ; user -> username(?) */
  704. if (*buf == '\0')
  705. break;
  706. if (*user == '\0')
  707. user = luser;
  708. /* First check the user part. In a naive implementation we
  709. would check the host part first, then the user. However,
  710. if we check the user first and reject the entry we will
  711. have saved doing any host lookups to normalize the comparison
  712. and that likely saves several DNS queries. Therefore we
  713. check the user first. */
  714. ucheck = __icheckuser (user, ruser);
  715. /* Either we found the user, or we didn't and this is a
  716. negative host check. We must do the negative host lookup
  717. in order to preserve the semantics of stopping on this line
  718. before processing others. */
  719. if (ucheck != 0 || *buf == '-') {
  720. /* Next check host part. */
  721. hcheck = __checkhost_sa (ra, ralen, buf, rhost);
  722. /* Negative '-host user(?)' match? */
  723. if (hcheck < 0)
  724. break;
  725. /* Positive 'host user' match? */
  726. if (hcheck > 0 && ucheck > 0) {
  727. retval = 0;
  728. break;
  729. }
  730. /* Negative 'host -user' match? */
  731. if (hcheck > 0 && ucheck < 0)
  732. break;
  733. /* Neither, go on looking for match. */
  734. }
  735. }
  736. free (buf);
  737. return retval;
  738. }