rcmd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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 __UCLIBC_HAS_THREADS__
  39. #undef __UCLIBC_HAS_THREADS__
  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 __UCLIBC_HAS_THREADS__
  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 __UCLIBC_HAS_THREADS__
  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 __UCLIBC_HAS_THREADS__
  286. size_t buflen;
  287. char *buffer;
  288. int herr;
  289. struct hostent hostbuf;
  290. #endif
  291. #ifdef __UCLIBC_HAS_THREADS__
  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 __UCLIBC_HAS_THREADS__
  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 (u_int32_t raddr, int superuser, const char * ruser, const char * luser)
  449. {
  450. return iruserok2 (raddr, superuser, ruser, luser, "-");
  451. }
  452. /*
  453. * XXX
  454. * Don't make static, used by lpd(8).
  455. *
  456. * This function is not used anymore. It is only present because lpd(8)
  457. * calls it (!?!). We simply call __invaliduser2() with an illegal rhost
  458. * argument. This means that netgroups won't work in .rhost/hosts.equiv
  459. * files. If you want lpd to work with netgroups, fix lpd to use ruserok()
  460. * or PAM.
  461. * Returns 0 if ok, -1 if not ok.
  462. */
  463. int
  464. __ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, const char *ruser)
  465. {
  466. return __ivaliduser2(hostf, raddr, luser, ruser, "-");
  467. }
  468. /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
  469. static int
  470. __icheckhost (u_int32_t raddr, char *lhost, const char *rhost)
  471. {
  472. struct hostent *hp;
  473. u_int32_t laddr;
  474. int negate=1; /* Multiply return with this to get -1 instead of 1 */
  475. char **pp;
  476. #ifdef __UCLIBC_HAS_THREADS__
  477. int save_errno;
  478. size_t buflen;
  479. char *buffer;
  480. struct hostent hostbuf;
  481. int herr;
  482. #endif
  483. #ifdef HAVE_NETGROUP
  484. /* Check nis netgroup. */
  485. if (strncmp ("+@", lhost, 2) == 0)
  486. return innetgr (&lhost[2], rhost, NULL, NULL);
  487. if (strncmp ("-@", lhost, 2) == 0)
  488. return -innetgr (&lhost[2], rhost, NULL, NULL);
  489. #endif /* HAVE_NETGROUP */
  490. /* -host */
  491. if (strncmp ("-", lhost,1) == 0) {
  492. negate = -1;
  493. lhost++;
  494. } else if (strcmp ("+",lhost) == 0) {
  495. return 1; /* asking for trouble, but ok.. */
  496. }
  497. /* Try for raw ip address first. */
  498. if (isdigit (*lhost) && (long) (laddr = inet_addr (lhost)) != -1)
  499. return negate * (! (raddr ^ laddr));
  500. /* Better be a hostname. */
  501. #ifdef __UCLIBC_HAS_THREADS__
  502. buflen = 1024;
  503. buffer = malloc(buflen);
  504. save_errno = errno;
  505. while (gethostbyname_r (lhost, &hostbuf, buffer, buflen, &hp, &herr)
  506. != 0) {
  507. free(buffer);
  508. return (0);
  509. }
  510. free(buffer);
  511. __set_errno (save_errno);
  512. #else
  513. hp = gethostbyname(lhost);
  514. #endif /* __UCLIBC_HAS_THREADS__ */
  515. if (hp == NULL)
  516. return 0;
  517. /* Spin through ip addresses. */
  518. for (pp = hp->h_addr_list; *pp; ++pp)
  519. if (!memcmp (&raddr, *pp, sizeof (u_int32_t)))
  520. return negate;
  521. /* No match. */
  522. return (0);
  523. }
  524. /* Returns 1 on positive match, 0 on no match, -1 on negative match. */
  525. static int
  526. __icheckuser (const char *luser, const char *ruser)
  527. {
  528. /*
  529. luser is user entry from .rhosts/hosts.equiv file
  530. ruser is user id on remote host
  531. */
  532. #ifdef HAVE_NETGROUP
  533. /* [-+]@netgroup */
  534. if (strncmp ("+@", luser, 2) == 0)
  535. return innetgr (&luser[2], NULL, ruser, NULL);
  536. if (strncmp ("-@", luser,2) == 0)
  537. return -innetgr (&luser[2], NULL, ruser, NULL);
  538. #endif /* HAVE_NETGROUP */
  539. /* -user */
  540. if (strncmp ("-", luser, 1) == 0)
  541. return -(strcmp (&luser[1], ruser) == 0);
  542. /* + */
  543. if (strcmp ("+", luser) == 0)
  544. return 1;
  545. /* simple string match */
  546. return strcmp (ruser, luser) == 0;
  547. }
  548. /*
  549. * Returns 1 for blank lines (or only comment lines) and 0 otherwise
  550. */
  551. static int
  552. __isempty(char *p)
  553. {
  554. while (*p && isspace (*p)) {
  555. ++p;
  556. }
  557. return (*p == '\0' || *p == '#') ? 1 : 0 ;
  558. }
  559. /*
  560. * Returns 0 if positive match, -1 if _not_ ok.
  561. */
  562. static int
  563. __ivaliduser2(hostf, raddr, luser, ruser, rhost)
  564. FILE *hostf;
  565. u_int32_t raddr;
  566. const char *luser, *ruser, *rhost;
  567. {
  568. register const char *user;
  569. register char *p;
  570. int hcheck, ucheck;
  571. char *buf = NULL;
  572. size_t bufsize = 0;
  573. int retval = -1;
  574. while (getline (&buf, &bufsize, hostf) > 0) {
  575. buf[bufsize - 1] = '\0'; /* Make sure it's terminated. */
  576. p = buf;
  577. /* Skip empty or comment lines */
  578. if (__isempty (p)) {
  579. continue;
  580. }
  581. /* Skip lines that are too long. */
  582. if (strchr (p, '\n') == NULL) {
  583. int ch = getc_unlocked (hostf);
  584. while (ch != '\n' && ch != EOF)
  585. ch = getc_unlocked (hostf);
  586. continue;
  587. }
  588. for (;*p && !isspace(*p); ++p) {
  589. *p = tolower (*p);
  590. }
  591. /* Next we want to find the permitted name for the remote user. */
  592. if (*p == ' ' || *p == '\t') {
  593. /* <nul> terminate hostname and skip spaces */
  594. for (*p++='\0'; *p && isspace (*p); ++p);
  595. user = p; /* this is the user's name */
  596. while (*p && !isspace (*p))
  597. ++p; /* find end of user's name */
  598. } else
  599. user = p;
  600. *p = '\0'; /* <nul> terminate username (+host?) */
  601. /* buf -> host(?) ; user -> username(?) */
  602. /* First check host part */
  603. hcheck = __icheckhost (raddr, buf, rhost);
  604. if (hcheck < 0)
  605. break;
  606. if (hcheck) {
  607. /* Then check user part */
  608. if (! (*user))
  609. user = luser;
  610. ucheck = __icheckuser (user, ruser);
  611. /* Positive 'host user' match? */
  612. if (ucheck > 0) {
  613. retval = 0;
  614. break;
  615. }
  616. /* Negative 'host -user' match? */
  617. if (ucheck < 0)
  618. break;
  619. /* Neither, go on looking for match */
  620. }
  621. }
  622. if (buf != NULL)
  623. free (buf);
  624. return retval;
  625. }