rcmd.c 17 KB

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