rcmd.c 16 KB

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