rcmd.c 17 KB

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