rcmd.c 17 KB

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