rcmd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. * Copyright (c) 1983, 1993, 1994
  3. * The Regents of the University of California. 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. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #if 0
  34. static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94";
  35. #endif /* LIBC_SCCS and not lint */
  36. #define bcopy __bcopy
  37. #define sysconf __sysconf
  38. #define getline __getline
  39. #define geteuid __geteuid
  40. #define seteuid __seteuid
  41. #define getpwnam_r __getpwnam_r
  42. #define gethostbyname __gethostbyname
  43. #define gethostbyname_r __gethostbyname_r
  44. #define fileno __fileno
  45. #define sleep __sleep
  46. #define inet_addr __inet_addr
  47. #define inet_ntoa __inet_ntoa
  48. #define herror __herror
  49. #define bind __bind
  50. #define connect __connect
  51. #define sigblock __sigblock
  52. #define __FORCE_GLIBC
  53. #include <features.h>
  54. #define __USE_GNU
  55. #include <ctype.h>
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include <errno.h>
  60. #include <alloca.h>
  61. #include <signal.h>
  62. #include <fcntl.h>
  63. #include <unistd.h>
  64. #include <pwd.h>
  65. #include <sys/param.h>
  66. #include <sys/poll.h>
  67. #include <sys/socket.h>
  68. #include <sys/stat.h>
  69. #include <netdb.h>
  70. #include <netinet/in.h>
  71. #include <arpa/inet.h>
  72. extern int __rresvport(int *alport) attribute_hidden;
  73. /* some forward declarations */
  74. static int __ivaliduser2(FILE *hostf, u_int32_t raddr,
  75. const char *luser, const char *ruser, const char *rhost);
  76. static int iruserok2 (u_int32_t raddr, int superuser, const char *ruser,
  77. const char *luser, const char *rhost);
  78. int rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
  79. char **ahost;
  80. u_short rport;
  81. const char *locuser, *remuser, *cmd;
  82. int *fd2p;
  83. {
  84. #ifdef __UCLIBC_HAS_REENTRANT_RPC__
  85. int herr;
  86. struct hostent hostbuf;
  87. size_t hstbuflen;
  88. char *tmphstbuf;
  89. #endif
  90. struct hostent *hp;
  91. struct sockaddr_in sin, from;
  92. struct pollfd pfd[2];
  93. int32_t oldmask;
  94. pid_t pid;
  95. int s, lport, timo;
  96. char c;
  97. pid = __getpid();
  98. #ifdef __UCLIBC_HAS_REENTRANT_RPC__
  99. hstbuflen = 1024;
  100. #ifdef __ARCH_HAS_MMU__
  101. tmphstbuf = alloca (hstbuflen);
  102. #else
  103. tmphstbuf = malloc (hstbuflen);
  104. #endif
  105. while (gethostbyname_r (*ahost, &hostbuf, tmphstbuf,
  106. hstbuflen, &hp, &herr) != 0 || hp == NULL)
  107. {
  108. if (herr != NETDB_INTERNAL || errno != ERANGE)
  109. {
  110. __set_h_errno (herr);
  111. #ifndef __ARCH_HAS_MMU__
  112. free(tmphstbuf);
  113. #endif
  114. herror(*ahost);
  115. return -1;
  116. }
  117. else
  118. {
  119. /* Enlarge the buffer. */
  120. hstbuflen *= 2;
  121. #ifdef __ARCH_HAS_MMU__
  122. tmphstbuf = alloca (hstbuflen);
  123. #else
  124. if (tmphstbuf) {
  125. free(tmphstbuf);
  126. }
  127. tmphstbuf = malloc (hstbuflen);
  128. #endif
  129. }
  130. }
  131. #ifndef __ARCH_HAS_MMU__
  132. free(tmphstbuf);
  133. #endif
  134. #else /* call the non-reentrant version */
  135. if ((hp = gethostbyname(*ahost)) == NULL) {
  136. return -1;
  137. }
  138. #endif
  139. pfd[0].events = POLLIN;
  140. pfd[1].events = POLLIN;
  141. *ahost = hp->h_name;
  142. oldmask = sigblock(sigmask(SIGURG)); /* __sigblock */
  143. for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
  144. s = __rresvport(&lport);
  145. if (s < 0) {
  146. if (errno == EAGAIN)
  147. (void)fprintf(stderr,
  148. "rcmd: socket: All ports in use\n");
  149. else
  150. (void)fprintf(stderr, "rcmd: socket: %m\n");
  151. sigsetmask(oldmask); /* sigsetmask */
  152. return -1;
  153. }
  154. fcntl(s, F_SETOWN, pid); /* __fcntl */
  155. sin.sin_family = hp->h_addrtype;
  156. bcopy(hp->h_addr_list[0], &sin.sin_addr,
  157. MIN (sizeof (sin.sin_addr), hp->h_length));
  158. sin.sin_port = rport;
  159. if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) /* __connect */
  160. break;
  161. (void)__close(s);
  162. if (errno == EADDRINUSE) {
  163. lport--;
  164. continue;
  165. }
  166. if (errno == ECONNREFUSED && timo <= 16) {
  167. (void)sleep(timo); /* __sleep */
  168. timo *= 2;
  169. continue;
  170. }
  171. if (hp->h_addr_list[1] != NULL) {
  172. int oerrno = errno;
  173. (void)fprintf(stderr, "connect to address %s: ",
  174. inet_ntoa(sin.sin_addr));
  175. __set_errno (oerrno);
  176. __perror(0);
  177. hp->h_addr_list++;
  178. bcopy(hp->h_addr_list[0], &sin.sin_addr,
  179. MIN (sizeof (sin.sin_addr), hp->h_length));
  180. (void)fprintf(stderr, "Trying %s...\n",
  181. inet_ntoa(sin.sin_addr));
  182. continue;
  183. }
  184. (void)fprintf(stderr, "%s: %m\n", hp->h_name);
  185. sigsetmask(oldmask); /* __sigsetmask */
  186. return -1;
  187. }
  188. lport--;
  189. if (fd2p == 0) {
  190. __write(s, "", 1);
  191. lport = 0;
  192. } else {
  193. char num[8];
  194. int s2 = __rresvport(&lport), s3;
  195. socklen_t len = sizeof(from);
  196. if (s2 < 0)
  197. goto bad;
  198. listen(s2, 1);
  199. (void)snprintf(num, sizeof(num), "%d", lport); /* __snprintf */
  200. if (__write(s, num, __strlen(num)+1) != __strlen(num)+1) {
  201. (void)fprintf(stderr,
  202. "rcmd: write (setting up stderr): %m\n");
  203. (void)__close(s2);
  204. goto bad;
  205. }
  206. pfd[0].fd = s;
  207. pfd[1].fd = s2;
  208. __set_errno (0);
  209. if (poll (pfd, 2, -1) < 1 || (pfd[1].revents & POLLIN) == 0){
  210. if (errno != 0)
  211. (void)fprintf(stderr, "rcmd: poll (setting up stderr): %m\n");
  212. else
  213. (void)fprintf(stderr, "poll: protocol failure in circuit setup\n");
  214. (void)__close(s2);
  215. goto bad;
  216. }
  217. s3 = accept(s2, (struct sockaddr *)&from, &len);
  218. (void)__close(s2);
  219. if (s3 < 0) {
  220. (void)fprintf(stderr,
  221. "rcmd: accept: %m\n");
  222. lport = 0;
  223. goto bad;
  224. }
  225. *fd2p = s3;
  226. from.sin_port = ntohs((u_short)from.sin_port);
  227. if (from.sin_family != AF_INET ||
  228. from.sin_port >= IPPORT_RESERVED ||
  229. from.sin_port < IPPORT_RESERVED / 2) {
  230. (void)fprintf(stderr,
  231. "socket: protocol failure in circuit setup\n");
  232. goto bad2;
  233. }
  234. }
  235. (void)__write(s, locuser, __strlen(locuser)+1);
  236. (void)__write(s, remuser, __strlen(remuser)+1);
  237. (void)__write(s, cmd, __strlen(cmd)+1);
  238. if (__read(s, &c, 1) != 1) {
  239. (void)fprintf(stderr,
  240. "rcmd: %s: %m\n", *ahost);
  241. goto bad2;
  242. }
  243. if (c != 0) {
  244. while (__read(s, &c, 1) == 1) {
  245. (void)__write(STDERR_FILENO, &c, 1);
  246. if (c == '\n')
  247. break;
  248. }
  249. goto bad2;
  250. }
  251. sigsetmask(oldmask);
  252. return s;
  253. bad2:
  254. if (lport)
  255. (void)__close(*fd2p);
  256. bad:
  257. (void)__close(s);
  258. sigsetmask(oldmask);
  259. return -1;
  260. }
  261. int attribute_hidden __rresvport(int *alport)
  262. {
  263. struct sockaddr_in sin;
  264. int s;
  265. sin.sin_family = AF_INET;
  266. sin.sin_addr.s_addr = INADDR_ANY;
  267. s = __socket(AF_INET, SOCK_STREAM, 0);
  268. if (s < 0)
  269. return -1;
  270. for (;;) {
  271. sin.sin_port = htons((u_short)*alport);
  272. if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
  273. return s;
  274. if (errno != EADDRINUSE) {
  275. (void)__close(s);
  276. return -1;
  277. }
  278. (*alport)--;
  279. if (*alport == IPPORT_RESERVED/2) {
  280. (void)__close(s);
  281. __set_errno (EAGAIN); /* close */
  282. return -1;
  283. }
  284. }
  285. return -1;
  286. }
  287. strong_alias(__rresvport,rresvport)
  288. int __check_rhosts_file = 1;
  289. char *__rcmd_errstr;
  290. int ruserok(rhost, superuser, ruser, luser)
  291. const char *rhost, *ruser, *luser;
  292. int superuser;
  293. {
  294. struct hostent *hp;
  295. u_int32_t addr;
  296. char **ap;
  297. #ifdef __UCLIBC_HAS_REENTRANT_RPC__
  298. size_t buflen;
  299. char *buffer;
  300. int herr;
  301. struct hostent hostbuf;
  302. #endif
  303. #ifdef __UCLIBC_HAS_REENTRANT_RPC__
  304. buflen = 1024;
  305. #ifdef __ARCH_HAS_MMU__
  306. buffer = alloca (buflen);
  307. #else
  308. buffer = malloc (buflen);
  309. #endif
  310. while (gethostbyname_r (rhost, &hostbuf, buffer,
  311. buflen, &hp, &herr) != 0 || hp == NULL)
  312. {
  313. if (herr != NETDB_INTERNAL || errno != ERANGE) {
  314. #ifndef __ARCH_HAS_MMU__
  315. free(buffer);
  316. #endif
  317. return -1;
  318. } else
  319. {
  320. /* Enlarge the buffer. */
  321. buflen *= 2;
  322. #ifdef __ARCH_HAS_MMU__
  323. buffer = alloca (buflen);
  324. #else
  325. if (buffer) {
  326. free(buffer);
  327. }
  328. buffer = malloc (buflen);
  329. #endif
  330. }
  331. }
  332. #ifndef __ARCH_HAS_MMU__
  333. free(buffer);
  334. #endif
  335. #else
  336. if ((hp = gethostbyname(rhost)) == NULL) {
  337. return -1;
  338. }
  339. #endif
  340. for (ap = hp->h_addr_list; *ap; ++ap) {
  341. bcopy(*ap, &addr, sizeof(addr));
  342. if (iruserok2(addr, superuser, ruser, luser, rhost) == 0)
  343. return 0;
  344. }
  345. return -1;
  346. }
  347. /* Extremely paranoid file open function. */
  348. static FILE *
  349. iruserfopen (char *file, uid_t okuser)
  350. {
  351. struct stat st;
  352. char *cp = NULL;
  353. FILE *res = NULL;
  354. /* If not a regular file, if owned by someone other than user or
  355. root, if writeable by anyone but the owner, or if hardlinked
  356. anywhere, quit. */
  357. cp = NULL;
  358. if (lstat (file, &st))
  359. cp = "lstat failed";
  360. else if (!S_ISREG (st.st_mode))
  361. cp = "not regular file";
  362. else
  363. {
  364. res = fopen (file, "r");
  365. if (!res)
  366. cp = "cannot open";
  367. else if (fstat (fileno (res), &st) < 0)
  368. cp = "fstat failed";
  369. else if (st.st_uid && st.st_uid != okuser)
  370. cp = "bad owner";
  371. else if (st.st_mode & (S_IWGRP|S_IWOTH))
  372. cp = "writeable by other than owner";
  373. else if (st.st_nlink > 1)
  374. cp = "hard linked somewhere";
  375. }
  376. /* If there were any problems, quit. */
  377. if (cp != NULL)
  378. {
  379. __rcmd_errstr = cp;
  380. if (res)
  381. fclose (res);
  382. return NULL;
  383. }
  384. return res;
  385. }
  386. /*
  387. * New .rhosts strategy: We are passed an ip address. We spin through
  388. * hosts.equiv and .rhosts looking for a match. When the .rhosts only
  389. * has ip addresses, we don't have to trust a nameserver. When it
  390. * contains hostnames, we spin through the list of addresses the nameserver
  391. * gives us and look for a match.
  392. *
  393. * Returns 0 if ok, -1 if not ok.
  394. */
  395. static int
  396. iruserok2 (raddr, superuser, ruser, luser, rhost)
  397. u_int32_t raddr;
  398. int superuser;
  399. const char *ruser, *luser, *rhost;
  400. {
  401. FILE *hostf = NULL;
  402. int isbad = -1;
  403. if (!superuser)
  404. hostf = iruserfopen (_PATH_HEQUIV, 0);
  405. if (hostf) {
  406. isbad = __ivaliduser2 (hostf, raddr, luser, ruser, rhost);
  407. fclose (hostf);
  408. if (!isbad)
  409. return 0;
  410. }
  411. if (__check_rhosts_file || superuser) {
  412. char *pbuf;
  413. struct passwd *pwd;
  414. size_t dirlen;
  415. uid_t uid;
  416. #ifdef __UCLIBC_HAS_REENTRANT_RPC__
  417. size_t buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
  418. struct passwd pwdbuf;
  419. #ifdef __ARCH_HAS_MMU__
  420. char *buffer = alloca (buflen);
  421. #else
  422. char *buffer = malloc (buflen);
  423. #endif
  424. if (getpwnam_r (luser, &pwdbuf, buffer,
  425. buflen, &pwd) != 0 || pwd == NULL)
  426. {
  427. #ifndef __ARCH_HAS_MMU__
  428. free(buffer);
  429. #endif
  430. return -1;
  431. }
  432. #ifndef __ARCH_HAS_MMU__
  433. free(buffer);
  434. #endif
  435. #else
  436. if ((pwd = getpwnam(luser)) == NULL)
  437. return -1;
  438. #endif
  439. dirlen = __strlen (pwd->pw_dir);
  440. pbuf = malloc (dirlen + sizeof "/.rhosts");
  441. __strcpy (pbuf, pwd->pw_dir);
  442. __strcat (pbuf, "/.rhosts");
  443. /* Change effective uid while reading .rhosts. If root and
  444. reading an NFS mounted file system, can't read files that
  445. are protected read/write owner only. */
  446. uid = geteuid ();
  447. seteuid (pwd->pw_uid);
  448. hostf = iruserfopen (pbuf, pwd->pw_uid);
  449. free(pbuf);
  450. if (hostf != NULL) {
  451. isbad = __ivaliduser2 (hostf, raddr, luser, ruser, rhost);
  452. fclose (hostf);
  453. }
  454. seteuid (uid);
  455. return isbad;
  456. }
  457. return -1;
  458. }
  459. /* This is the exported version. */
  460. int iruserok (u_int32_t raddr, int superuser, const char * ruser, const char * luser)
  461. {
  462. return iruserok2 (raddr, superuser, ruser, luser, "-");
  463. }
  464. /*
  465. * XXX
  466. * Don't make static, used by lpd(8).
  467. *
  468. * This function is not used anymore. It is only present because lpd(8)
  469. * calls it (!?!). We simply call __invaliduser2() with an illegal rhost
  470. * argument. This means that netgroups won't work in .rhost/hosts.equiv
  471. * files. If you want lpd to work with netgroups, fix lpd to use ruserok()
  472. * or PAM.
  473. * Returns 0 if ok, -1 if not ok.
  474. */
  475. int
  476. __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser)
  477. {
  478. return __ivaliduser2(hostf, raddr, luser, ruser, "-");
  479. }
  480. /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
  481. static int
  482. __icheckhost (u_int32_t raddr, char *lhost, const char *rhost)
  483. {
  484. struct hostent *hp;
  485. u_int32_t laddr;
  486. int negate=1; /* Multiply return with this to get -1 instead of 1 */
  487. char **pp;
  488. #ifdef __UCLIBC_HAS_REENTRANT_RPC__
  489. int save_errno;
  490. size_t buflen;
  491. char *buffer;
  492. struct hostent hostbuf;
  493. int herr;
  494. #endif
  495. #ifdef HAVE_NETGROUP
  496. /* Check nis netgroup. */
  497. if (__strncmp ("+@", lhost, 2) == 0)
  498. return innetgr (&lhost[2], rhost, NULL, NULL);
  499. if (__strncmp ("-@", lhost, 2) == 0)
  500. return -innetgr (&lhost[2], rhost, NULL, NULL);
  501. #endif /* HAVE_NETGROUP */
  502. /* -host */
  503. if (__strncmp ("-", lhost,1) == 0) {
  504. negate = -1;
  505. lhost++;
  506. } else if (__strcmp ("+",lhost) == 0) {
  507. return 1; /* asking for trouble, but ok.. */
  508. }
  509. /* Try for raw ip address first. */
  510. if (isdigit (*lhost) && (laddr = inet_addr (lhost)) != INADDR_NONE)
  511. return negate * (! (raddr ^ laddr));
  512. /* Better be a hostname. */
  513. #ifdef __UCLIBC_HAS_REENTRANT_RPC__
  514. buflen = 1024;
  515. buffer = malloc(buflen);
  516. save_errno = errno;
  517. while (gethostbyname_r (lhost, &hostbuf, buffer, buflen, &hp, &herr)
  518. != 0) {
  519. free(buffer);
  520. return (0);
  521. }
  522. free(buffer);
  523. __set_errno (save_errno);
  524. #else
  525. hp = gethostbyname(lhost);
  526. #endif /* __UCLIBC_HAS_REENTRANT_RPC__ */
  527. if (hp == NULL)
  528. return 0;
  529. /* Spin through ip addresses. */
  530. for (pp = hp->h_addr_list; *pp; ++pp)
  531. if (!__memcmp (&raddr, *pp, sizeof (u_int32_t)))
  532. return negate;
  533. /* No match. */
  534. return (0);
  535. }
  536. /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
  537. static int
  538. __icheckuser (const char *luser, const char *ruser)
  539. {
  540. /*
  541. luser is user entry from .rhosts/hosts.equiv file
  542. ruser is user id on remote host
  543. */
  544. #ifdef HAVE_NETGROUP
  545. /* [-+]@netgroup */
  546. if (__strncmp ("+@", luser, 2) == 0)
  547. return innetgr (&luser[2], NULL, ruser, NULL);
  548. if (__strncmp ("-@", luser,2) == 0)
  549. return -innetgr (&luser[2], NULL, ruser, NULL);
  550. #endif /* HAVE_NETGROUP */
  551. /* -user */
  552. if (__strncmp ("-", luser, 1) == 0)
  553. return -(__strcmp (&luser[1], ruser) == 0);
  554. /* + */
  555. if (__strcmp ("+", luser) == 0)
  556. return 1;
  557. /* simple string match */
  558. return __strcmp (ruser, luser) == 0;
  559. }
  560. /*
  561. * Returns 1 for blank lines (or only comment lines) and 0 otherwise
  562. */
  563. static int
  564. __isempty(char *p)
  565. {
  566. while (*p && isspace (*p)) {
  567. ++p;
  568. }
  569. return (*p == '\0' || *p == '#') ? 1 : 0 ;
  570. }
  571. /*
  572. * Returns 0 if positive match, -1 if _not_ ok.
  573. */
  574. static int
  575. __ivaliduser2(hostf, raddr, luser, ruser, rhost)
  576. FILE *hostf;
  577. u_int32_t raddr;
  578. const char *luser, *ruser, *rhost;
  579. {
  580. register const char *user;
  581. register char *p;
  582. int hcheck, ucheck;
  583. char *buf = NULL;
  584. size_t bufsize = 0;
  585. int retval = -1;
  586. while (getline (&buf, &bufsize, hostf) > 0) {
  587. buf[bufsize - 1] = '\0'; /* Make sure it's terminated. */
  588. p = buf;
  589. /* Skip empty or comment lines */
  590. if (__isempty (p)) {
  591. continue;
  592. }
  593. /* Skip lines that are too long. */
  594. if (__strchr (p, '\n') == NULL) {
  595. int ch = getc_unlocked (hostf);
  596. while (ch != '\n' && ch != EOF)
  597. ch = getc_unlocked (hostf);
  598. continue;
  599. }
  600. for (;*p && !isspace(*p); ++p) {
  601. *p = tolower (*p);
  602. }
  603. /* Next we want to find the permitted name for the remote user. */
  604. if (*p == ' ' || *p == '\t') {
  605. /* <nul> terminate hostname and skip spaces */
  606. for (*p++='\0'; *p && isspace (*p); ++p);
  607. user = p; /* this is the user's name */
  608. while (*p && !isspace (*p))
  609. ++p; /* find end of user's name */
  610. } else
  611. user = p;
  612. *p = '\0'; /* <nul> terminate username (+host?) */
  613. /* buf -> host(?) ; user -> username(?) */
  614. /* First check host part */
  615. hcheck = __icheckhost (raddr, buf, rhost);
  616. if (hcheck < 0)
  617. break;
  618. if (hcheck) {
  619. /* Then check user part */
  620. if (! (*user))
  621. user = luser;
  622. ucheck = __icheckuser (user, ruser);
  623. /* Positive 'host user' match? */
  624. if (ucheck > 0) {
  625. retval = 0;
  626. break;
  627. }
  628. /* Negative 'host -user' match? */
  629. if (ucheck < 0)
  630. break;
  631. /* Neither, go on looking for match */
  632. }
  633. }
  634. if (buf != NULL)
  635. free (buf);
  636. return retval;
  637. }