rcmd.c 16 KB

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