rcmd.c 17 KB

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