rcmd.c 18 KB

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