rcmd.c 18 KB

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