rcmd.c 17 KB

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