rcmd.c 18 KB

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