rcmd.c 18 KB

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