rcmd.c 16 KB

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