rcmd.c 16 KB

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