rcmd.c 18 KB

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