rcmd.c 19 KB

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