rcmd.c 17 KB

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