rcmd.c 16 KB

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