rcmd.c 18 KB

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