rcmd.c 17 KB

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