rcmd.c 16 KB

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