rcmd.c 17 KB

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