rcmd.c 16 KB

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