rcmd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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. #if 0
  58. static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
  59. #endif /* LIBC_SCCS and not lint */
  60. #define __UCLIBC_HIDE_DEPRECATED__
  61. #include <features.h>
  62. #include <sys/param.h>
  63. #include <sys/poll.h>
  64. #include <sys/socket.h>
  65. #include <sys/stat.h>
  66. #include <netinet/in.h>
  67. #include <arpa/inet.h>
  68. #include <alloca.h>
  69. #include <signal.h>
  70. #include <fcntl.h>
  71. #include <netdb.h>
  72. #include <unistd.h>
  73. #include <pwd.h>
  74. #include <errno.h>
  75. #include <stdio.h>
  76. #include <stdio_ext.h>
  77. #include <ctype.h>
  78. #include <string.h>
  79. #include <libintl.h>
  80. #include <stdlib.h>
  81. #ifdef __UCLIBC_HAS_WCHAR__
  82. #include <wchar.h>
  83. #endif
  84. #include <sys/uio.h>
  85. libc_hidden_proto(memcmp)
  86. libc_hidden_proto(strcat)
  87. libc_hidden_proto(strchr)
  88. libc_hidden_proto(strcmp)
  89. libc_hidden_proto(strcpy)
  90. libc_hidden_proto(strlen)
  91. libc_hidden_proto(strncmp)
  92. libc_hidden_proto(strnlen)
  93. libc_hidden_proto(memmove)
  94. libc_hidden_proto(getpid)
  95. libc_hidden_proto(socket)
  96. libc_hidden_proto(close)
  97. libc_hidden_proto(fcntl)
  98. libc_hidden_proto(read)
  99. libc_hidden_proto(write)
  100. libc_hidden_proto(perror)
  101. libc_hidden_proto(lstat)
  102. libc_hidden_proto(fstat)
  103. libc_hidden_proto(tolower)
  104. libc_hidden_proto(sysconf)
  105. libc_hidden_proto(getline)
  106. libc_hidden_proto(geteuid)
  107. libc_hidden_proto(seteuid)
  108. libc_hidden_proto(getpwnam_r)
  109. libc_hidden_proto(gethostbyname)
  110. libc_hidden_proto(gethostbyname_r)
  111. libc_hidden_proto(fileno)
  112. libc_hidden_proto(sleep)
  113. libc_hidden_proto(inet_addr)
  114. libc_hidden_proto(inet_ntoa)
  115. libc_hidden_proto(herror)
  116. libc_hidden_proto(bind)
  117. libc_hidden_proto(connect)
  118. libc_hidden_proto(sigblock)
  119. libc_hidden_proto(snprintf)
  120. libc_hidden_proto(poll)
  121. libc_hidden_proto(accept)
  122. libc_hidden_proto(listen)
  123. libc_hidden_proto(sigsetmask)
  124. libc_hidden_proto(getc_unlocked)
  125. libc_hidden_proto(__fgetc_unlocked)
  126. libc_hidden_proto(fopen)
  127. libc_hidden_proto(fclose)
  128. libc_hidden_proto(fprintf)
  129. libc_hidden_proto(__h_errno_location)
  130. #ifdef __UCLIBC_HAS_XLOCALE__
  131. libc_hidden_proto(__ctype_b_loc)
  132. libc_hidden_proto(__ctype_tolower_loc)
  133. #else
  134. libc_hidden_proto(__ctype_b)
  135. libc_hidden_proto(__ctype_tolower)
  136. #endif
  137. libc_hidden_proto(rresvport)
  138. /* some forward declarations */
  139. static int __ivaliduser2(FILE *hostf, u_int32_t raddr,
  140. const char *luser, const char *ruser, const char *rhost);
  141. static int iruserok2 (u_int32_t raddr, int superuser, const char *ruser,
  142. const char *luser, const char *rhost);
  143. int rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
  144. char **ahost;
  145. u_short rport;
  146. const char *locuser, *remuser, *cmd;
  147. int *fd2p;
  148. {
  149. #ifdef __UCLIBC_HAS_REENTRANT_RPC__
  150. int herr;
  151. struct hostent hostbuf;
  152. size_t hstbuflen;
  153. char *tmphstbuf;
  154. #endif
  155. struct hostent *hp;
  156. struct sockaddr_in sin, from;
  157. struct pollfd pfd[2];
  158. int32_t oldmask;
  159. pid_t pid;
  160. int s, lport, timo;
  161. char c;
  162. pid = getpid();
  163. #ifdef __UCLIBC_HAS_REENTRANT_RPC__
  164. hstbuflen = 1024;
  165. #ifdef __ARCH_USE_MMU__
  166. tmphstbuf = alloca (hstbuflen);
  167. #else
  168. tmphstbuf = malloc (hstbuflen);
  169. #endif
  170. while (gethostbyname_r (*ahost, &hostbuf, tmphstbuf,
  171. hstbuflen, &hp, &herr) != 0 || hp == NULL)
  172. {
  173. if (herr != NETDB_INTERNAL || errno != ERANGE)
  174. {
  175. __set_h_errno (herr);
  176. #ifndef __ARCH_USE_MMU__
  177. free(tmphstbuf);
  178. #endif
  179. herror(*ahost);
  180. return -1;
  181. }
  182. else
  183. {
  184. /* Enlarge the buffer. */
  185. hstbuflen *= 2;
  186. #ifdef __ARCH_USE_MMU__
  187. tmphstbuf = alloca (hstbuflen);
  188. #else
  189. if (tmphstbuf) {
  190. free(tmphstbuf);
  191. }
  192. tmphstbuf = malloc (hstbuflen);
  193. #endif
  194. }
  195. }
  196. #ifndef __ARCH_USE_MMU__
  197. free(tmphstbuf);
  198. #endif
  199. #else /* call the non-reentrant version */
  200. if ((hp = gethostbyname(*ahost)) == NULL) {
  201. return -1;
  202. }
  203. #endif
  204. pfd[0].events = POLLIN;
  205. pfd[1].events = POLLIN;
  206. *ahost = hp->h_name;
  207. oldmask = sigblock(sigmask(SIGURG)); /* __sigblock */
  208. for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
  209. s = rresvport(&lport);
  210. if (s < 0) {
  211. if (errno == EAGAIN)
  212. (void)fprintf(stderr,
  213. "rcmd: socket: All ports in use\n");
  214. else
  215. (void)fprintf(stderr, "rcmd: socket: %m\n");
  216. sigsetmask(oldmask); /* sigsetmask */
  217. return -1;
  218. }
  219. fcntl(s, F_SETOWN, pid);
  220. sin.sin_family = hp->h_addrtype;
  221. memmove(&sin.sin_addr, hp->h_addr_list[0],
  222. MIN (sizeof (sin.sin_addr), hp->h_length));
  223. sin.sin_port = rport;
  224. if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) /* __connect */
  225. break;
  226. (void)close(s);
  227. if (errno == EADDRINUSE) {
  228. lport--;
  229. continue;
  230. }
  231. if (errno == ECONNREFUSED && timo <= 16) {
  232. (void)sleep(timo); /* __sleep */
  233. timo *= 2;
  234. continue;
  235. }
  236. if (hp->h_addr_list[1] != NULL) {
  237. int oerrno = errno;
  238. (void)fprintf(stderr, "connect to address %s: ",
  239. inet_ntoa(sin.sin_addr));
  240. __set_errno (oerrno);
  241. perror(0);
  242. hp->h_addr_list++;
  243. memmove(&sin.sin_addr, hp->h_addr_list[0],
  244. MIN (sizeof (sin.sin_addr), hp->h_length));
  245. (void)fprintf(stderr, "Trying %s...\n",
  246. inet_ntoa(sin.sin_addr));
  247. continue;
  248. }
  249. (void)fprintf(stderr, "%s: %m\n", hp->h_name);
  250. sigsetmask(oldmask); /* __sigsetmask */
  251. return -1;
  252. }
  253. lport--;
  254. if (fd2p == 0) {
  255. write(s, "", 1);
  256. lport = 0;
  257. } else {
  258. char num[8];
  259. int s2 = rresvport(&lport), s3;
  260. socklen_t len = sizeof(from);
  261. if (s2 < 0)
  262. goto bad;
  263. listen(s2, 1);
  264. (void)snprintf(num, sizeof(num), "%d", lport); /* __snprintf */
  265. if (write(s, num, strlen(num)+1) != strlen(num)+1) {
  266. (void)fprintf(stderr,
  267. "rcmd: write (setting up stderr): %m\n");
  268. (void)close(s2);
  269. goto bad;
  270. }
  271. pfd[0].fd = s;
  272. pfd[1].fd = s2;
  273. __set_errno (0);
  274. if (poll (pfd, 2, -1) < 1 || (pfd[1].revents & POLLIN) == 0){
  275. if (errno != 0)
  276. (void)fprintf(stderr, "rcmd: poll (setting up stderr): %m\n");
  277. else
  278. (void)fprintf(stderr, "poll: protocol failure in circuit setup\n");
  279. (void)close(s2);
  280. goto bad;
  281. }
  282. s3 = accept(s2, (struct sockaddr *)&from, &len);
  283. (void)close(s2);
  284. if (s3 < 0) {
  285. (void)fprintf(stderr,
  286. "rcmd: accept: %m\n");
  287. lport = 0;
  288. goto bad;
  289. }
  290. *fd2p = s3;
  291. from.sin_port = ntohs((u_short)from.sin_port);
  292. if (from.sin_family != AF_INET ||
  293. from.sin_port >= IPPORT_RESERVED ||
  294. from.sin_port < IPPORT_RESERVED / 2) {
  295. (void)fprintf(stderr,
  296. "socket: protocol failure in circuit setup\n");
  297. goto bad2;
  298. }
  299. }
  300. (void)write(s, locuser, strlen(locuser)+1);
  301. (void)write(s, remuser, strlen(remuser)+1);
  302. (void)write(s, cmd, strlen(cmd)+1);
  303. if (read(s, &c, 1) != 1) {
  304. (void)fprintf(stderr,
  305. "rcmd: %s: %m\n", *ahost);
  306. goto bad2;
  307. }
  308. if (c != 0) {
  309. while (read(s, &c, 1) == 1) {
  310. (void)write(STDERR_FILENO, &c, 1);
  311. if (c == '\n')
  312. break;
  313. }
  314. goto bad2;
  315. }
  316. sigsetmask(oldmask);
  317. return s;
  318. bad2:
  319. if (lport)
  320. (void)close(*fd2p);
  321. bad:
  322. (void)close(s);
  323. sigsetmask(oldmask);
  324. return -1;
  325. }
  326. int rresvport(int *alport)
  327. {
  328. struct sockaddr_in sin;
  329. int s;
  330. sin.sin_family = AF_INET;
  331. sin.sin_addr.s_addr = INADDR_ANY;
  332. s = socket(AF_INET, SOCK_STREAM, 0);
  333. if (s < 0)
  334. return -1;
  335. for (;;) {
  336. sin.sin_port = htons((u_short)*alport);
  337. if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
  338. return s;
  339. if (errno != EADDRINUSE) {
  340. (void)close(s);
  341. return -1;
  342. }
  343. (*alport)--;
  344. if (*alport == IPPORT_RESERVED/2) {
  345. (void)close(s);
  346. __set_errno (EAGAIN); /* close */
  347. return -1;
  348. }
  349. }
  350. return -1;
  351. }
  352. libc_hidden_def(rresvport)
  353. static int __check_rhosts_file = 1;
  354. int ruserok(rhost, superuser, ruser, luser)
  355. const char *rhost, *ruser, *luser;
  356. int superuser;
  357. {
  358. struct hostent *hp;
  359. u_int32_t addr;
  360. char **ap;
  361. #ifdef __UCLIBC_HAS_REENTRANT_RPC__
  362. size_t buflen;
  363. char *buffer;
  364. int herr;
  365. struct hostent hostbuf;
  366. #endif
  367. #ifdef __UCLIBC_HAS_REENTRANT_RPC__
  368. buflen = 1024;
  369. #ifdef __ARCH_USE_MMU__
  370. buffer = alloca (buflen);
  371. #else
  372. buffer = malloc (buflen);
  373. #endif
  374. while (gethostbyname_r (rhost, &hostbuf, buffer,
  375. buflen, &hp, &herr) != 0 || hp == NULL)
  376. {
  377. if (herr != NETDB_INTERNAL || errno != ERANGE) {
  378. #ifndef __ARCH_USE_MMU__
  379. free(buffer);
  380. #endif
  381. return -1;
  382. } else
  383. {
  384. /* Enlarge the buffer. */
  385. buflen *= 2;
  386. #ifdef __ARCH_USE_MMU__
  387. buffer = alloca (buflen);
  388. #else
  389. if (buffer) {
  390. free(buffer);
  391. }
  392. buffer = malloc (buflen);
  393. #endif
  394. }
  395. }
  396. #ifndef __ARCH_USE_MMU__
  397. free(buffer);
  398. #endif
  399. #else
  400. if ((hp = gethostbyname(rhost)) == NULL) {
  401. return -1;
  402. }
  403. #endif
  404. for (ap = hp->h_addr_list; *ap; ++ap) {
  405. memmove(&addr, *ap, sizeof(addr));
  406. if (iruserok2(addr, superuser, ruser, luser, rhost) == 0)
  407. return 0;
  408. }
  409. return -1;
  410. }
  411. /* Extremely paranoid file open function. */
  412. static FILE *
  413. iruserfopen (const char *file, uid_t okuser)
  414. {
  415. struct stat st;
  416. char *cp = NULL;
  417. FILE *res = NULL;
  418. /* If not a regular file, if owned by someone other than user or
  419. root, if writeable by anyone but the owner, or if hardlinked
  420. anywhere, quit. */
  421. if (lstat (file, &st))
  422. cp = "lstat failed";
  423. else if (!S_ISREG (st.st_mode))
  424. cp = "not regular file";
  425. else
  426. {
  427. res = fopen (file, "r");
  428. if (!res)
  429. cp = "cannot open";
  430. else if (fstat (fileno (res), &st) < 0)
  431. cp = "fstat failed";
  432. else if (st.st_uid && st.st_uid != okuser)
  433. cp = "bad owner";
  434. else if (st.st_mode & (S_IWGRP|S_IWOTH))
  435. cp = "writeable by other than owner";
  436. else if (st.st_nlink > 1)
  437. cp = "hard linked somewhere";
  438. }
  439. /* If there were any problems, quit. */
  440. if (cp != NULL)
  441. {
  442. if (res)
  443. fclose (res);
  444. return NULL;
  445. }
  446. return res;
  447. }
  448. /*
  449. * New .rhosts strategy: We are passed an ip address. We spin through
  450. * hosts.equiv and .rhosts looking for a match. When the .rhosts only
  451. * has ip addresses, we don't have to trust a nameserver. When it
  452. * contains hostnames, we spin through the list of addresses the nameserver
  453. * gives us and look for a match.
  454. *
  455. * Returns 0 if ok, -1 if not ok.
  456. */
  457. static int
  458. iruserok2 (raddr, superuser, ruser, luser, rhost)
  459. u_int32_t raddr;
  460. int superuser;
  461. const char *ruser, *luser, *rhost;
  462. {
  463. FILE *hostf = NULL;
  464. int isbad = -1;
  465. if (!superuser)
  466. hostf = iruserfopen (_PATH_HEQUIV, 0);
  467. if (hostf) {
  468. isbad = __ivaliduser2 (hostf, raddr, luser, ruser, rhost);
  469. fclose (hostf);
  470. if (!isbad)
  471. return 0;
  472. }
  473. if (__check_rhosts_file || superuser) {
  474. char *pbuf;
  475. struct passwd *pwd;
  476. size_t dirlen;
  477. uid_t uid;
  478. #ifdef __UCLIBC_HAS_REENTRANT_RPC__
  479. size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
  480. struct passwd pwdbuf;
  481. #ifdef __ARCH_USE_MMU__
  482. char *buffer = alloca (buflen);
  483. #else
  484. char *buffer = malloc (buflen);
  485. #endif
  486. if (getpwnam_r (luser, &pwdbuf, buffer,
  487. buflen, &pwd) != 0 || pwd == NULL)
  488. {
  489. #ifndef __ARCH_USE_MMU__
  490. free(buffer);
  491. #endif
  492. return -1;
  493. }
  494. #ifndef __ARCH_USE_MMU__
  495. free(buffer);
  496. #endif
  497. #else
  498. if ((pwd = getpwnam(luser)) == NULL)
  499. return -1;
  500. #endif
  501. dirlen = strlen (pwd->pw_dir);
  502. pbuf = malloc (dirlen + sizeof "/.rhosts");
  503. strcpy (pbuf, pwd->pw_dir);
  504. strcat (pbuf, "/.rhosts");
  505. /* Change effective uid while reading .rhosts. If root and
  506. reading an NFS mounted file system, can't read files that
  507. are protected read/write owner only. */
  508. uid = geteuid ();
  509. seteuid (pwd->pw_uid);
  510. hostf = iruserfopen (pbuf, pwd->pw_uid);
  511. free(pbuf);
  512. if (hostf != NULL) {
  513. isbad = __ivaliduser2 (hostf, raddr, luser, ruser, rhost);
  514. fclose (hostf);
  515. }
  516. seteuid (uid);
  517. return isbad;
  518. }
  519. return -1;
  520. }
  521. /* This is the exported version. */
  522. int iruserok (u_int32_t raddr, int superuser, const char * ruser, const char * luser);
  523. int iruserok (u_int32_t raddr, int superuser, const char * ruser, const char * luser)
  524. {
  525. return iruserok2 (raddr, superuser, ruser, luser, "-");
  526. }
  527. /*
  528. * XXX
  529. * Don't make static, used by lpd(8).
  530. *
  531. * This function is not used anymore. It is only present because lpd(8)
  532. * calls it (!?!). We simply call __invaliduser2() with an illegal rhost
  533. * argument. This means that netgroups won't work in .rhost/hosts.equiv
  534. * files. If you want lpd to work with netgroups, fix lpd to use ruserok()
  535. * or PAM.
  536. * Returns 0 if ok, -1 if not ok.
  537. */
  538. int
  539. __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser);
  540. int
  541. __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser)
  542. {
  543. return __ivaliduser2(hostf, raddr, luser, ruser, "-");
  544. }
  545. /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
  546. static int
  547. __icheckhost (u_int32_t raddr, char *lhost, const char *rhost)
  548. {
  549. struct hostent *hp;
  550. u_int32_t laddr;
  551. int negate=1; /* Multiply return with this to get -1 instead of 1 */
  552. char **pp;
  553. #ifdef __UCLIBC_HAS_REENTRANT_RPC__
  554. int save_errno;
  555. size_t buflen;
  556. char *buffer;
  557. struct hostent hostbuf;
  558. int herr;
  559. #endif
  560. #ifdef HAVE_NETGROUP
  561. /* Check nis netgroup. */
  562. if (strncmp ("+@", lhost, 2) == 0)
  563. return innetgr (&lhost[2], rhost, NULL, NULL);
  564. if (strncmp ("-@", lhost, 2) == 0)
  565. return -innetgr (&lhost[2], rhost, NULL, NULL);
  566. #endif /* HAVE_NETGROUP */
  567. /* -host */
  568. if (strncmp ("-", lhost,1) == 0) {
  569. negate = -1;
  570. lhost++;
  571. } else if (strcmp ("+",lhost) == 0) {
  572. return 1; /* asking for trouble, but ok.. */
  573. }
  574. /* Try for raw ip address first. */
  575. if (isdigit (*lhost) && (laddr = inet_addr (lhost)) != INADDR_NONE)
  576. return negate * (! (raddr ^ laddr));
  577. /* Better be a hostname. */
  578. #ifdef __UCLIBC_HAS_REENTRANT_RPC__
  579. buflen = 1024;
  580. buffer = malloc(buflen);
  581. save_errno = errno;
  582. while (gethostbyname_r (lhost, &hostbuf, buffer, buflen, &hp, &herr)
  583. != 0) {
  584. free(buffer);
  585. return (0);
  586. }
  587. free(buffer);
  588. __set_errno (save_errno);
  589. #else
  590. hp = gethostbyname(lhost);
  591. #endif /* __UCLIBC_HAS_REENTRANT_RPC__ */
  592. if (hp == NULL)
  593. return 0;
  594. /* Spin through ip addresses. */
  595. for (pp = hp->h_addr_list; *pp; ++pp)
  596. if (!memcmp (&raddr, *pp, sizeof (u_int32_t)))
  597. return negate;
  598. /* No match. */
  599. return (0);
  600. }
  601. /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
  602. static int
  603. __icheckuser (const char *luser, const char *ruser)
  604. {
  605. /*
  606. luser is user entry from .rhosts/hosts.equiv file
  607. ruser is user id on remote host
  608. */
  609. #ifdef HAVE_NETGROUP
  610. /* [-+]@netgroup */
  611. if (strncmp ("+@", luser, 2) == 0)
  612. return innetgr (&luser[2], NULL, ruser, NULL);
  613. if (strncmp ("-@", luser,2) == 0)
  614. return -innetgr (&luser[2], NULL, ruser, NULL);
  615. #endif /* HAVE_NETGROUP */
  616. /* -user */
  617. if (strncmp ("-", luser, 1) == 0)
  618. return -(strcmp (&luser[1], ruser) == 0);
  619. /* + */
  620. if (strcmp ("+", luser) == 0)
  621. return 1;
  622. /* simple string match */
  623. return strcmp (ruser, luser) == 0;
  624. }
  625. /*
  626. * Returns 1 for blank lines (or only comment lines) and 0 otherwise
  627. */
  628. static int
  629. __isempty(char *p)
  630. {
  631. while (*p && isspace (*p)) {
  632. ++p;
  633. }
  634. return (*p == '\0' || *p == '#') ? 1 : 0 ;
  635. }
  636. /*
  637. * Returns 0 if positive match, -1 if _not_ ok.
  638. */
  639. static int
  640. __ivaliduser2(hostf, raddr, luser, ruser, rhost)
  641. FILE *hostf;
  642. u_int32_t raddr;
  643. const char *luser, *ruser, *rhost;
  644. {
  645. register const char *user;
  646. register char *p;
  647. int hcheck, ucheck;
  648. char *buf = NULL;
  649. size_t bufsize = 0;
  650. int retval = -1;
  651. while (getline (&buf, &bufsize, hostf) > 0) {
  652. buf[bufsize - 1] = '\0'; /* Make sure it's terminated. */
  653. p = buf;
  654. /* Skip empty or comment lines */
  655. if (__isempty (p)) {
  656. continue;
  657. }
  658. /* Skip lines that are too long. */
  659. if (strchr (p, '\n') == NULL) {
  660. int ch = getc_unlocked (hostf);
  661. while (ch != '\n' && ch != EOF)
  662. ch = getc_unlocked (hostf);
  663. continue;
  664. }
  665. for (;*p && !isspace(*p); ++p) {
  666. *p = tolower (*p);
  667. }
  668. /* Next we want to find the permitted name for the remote user. */
  669. if (*p == ' ' || *p == '\t') {
  670. /* <nul> terminate hostname and skip spaces */
  671. for (*p++='\0'; *p && isspace (*p); ++p);
  672. user = p; /* this is the user's name */
  673. while (*p && !isspace (*p))
  674. ++p; /* find end of user's name */
  675. } else
  676. user = p;
  677. *p = '\0'; /* <nul> terminate username (+host?) */
  678. /* buf -> host(?) ; user -> username(?) */
  679. /* First check host part */
  680. hcheck = __icheckhost (raddr, buf, rhost);
  681. if (hcheck < 0)
  682. break;
  683. if (hcheck) {
  684. /* Then check user part */
  685. if (! (*user))
  686. user = luser;
  687. ucheck = __icheckuser (user, ruser);
  688. /* Positive 'host user' match? */
  689. if (ucheck > 0) {
  690. retval = 0;
  691. break;
  692. }
  693. /* Negative 'host -user' match? */
  694. if (ucheck < 0)
  695. break;
  696. /* Neither, go on looking for match */
  697. }
  698. }
  699. if (buf != NULL)
  700. free (buf);
  701. return retval;
  702. }