home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / inetutil.1 / inetutil / inetutils-1.1 / rexecd / rexecd.c < prev   
Encoding:
C/C++ Source or Header  |  1996-07-31  |  6.8 KB  |  291 lines

  1. /*
  2.  * Copyright (c) 1983, 1993
  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.  
  34. #ifndef lint
  35. static char copyright[] =
  36. "@(#) Copyright (c) 1983, 1993\n\
  37.     The Regents of the University of California.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)rexecd.c    8.1 (Berkeley) 6/4/93";
  42. #endif /* not lint */
  43.  
  44. #ifdef HAVE_CONFIG_H
  45. #include <config.h>
  46. #endif
  47.  
  48. #include <sys/param.h>
  49. #include <sys/ioctl.h>
  50. #include <sys/socket.h>
  51. #include <sys/time.h>
  52.  
  53. #include <netinet/in.h>
  54.  
  55. #include <errno.h>
  56. #include <netdb.h>
  57. #include <paths.h>
  58. #include <pwd.h>
  59. #include <signal.h>
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <string.h>
  63. #include <unistd.h>
  64. #include <crypt.h>
  65.  
  66. /*VARARGS1*/
  67. int error();
  68.  
  69. /*
  70.  * remote execute server:
  71.  *    username\0
  72.  *    password\0
  73.  *    command\0
  74.  *    data
  75.  */
  76. /*ARGSUSED*/
  77. main(argc, argv)
  78.     int argc;
  79.     char **argv;
  80. {
  81.     struct sockaddr_in from;
  82.     int fromlen;
  83.  
  84.     fromlen = sizeof (from);
  85.     if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
  86.         (void)fprintf(stderr,
  87.             "rexecd: getpeername: %s\n", strerror(errno));
  88.         exit(1);
  89.     }
  90.     doit(0, &from);
  91. }
  92.  
  93. char    username[20] = "USER=";
  94. char    homedir[64] = "HOME=";
  95. char    shell[64] = "SHELL=";
  96. char    path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH=";
  97. char    *envinit[] =
  98.         {homedir, shell, path, username, 0};
  99. char    **environ;
  100.  
  101. struct    sockaddr_in asin = { AF_INET };
  102.  
  103. char *getstr ();
  104.  
  105. doit(f, fromp)
  106.     int f;
  107.     struct sockaddr_in *fromp;
  108. {
  109.     char *cmdbuf, *cp, *namep;
  110.     char *user, *pass;
  111.     struct passwd *pwd;
  112.     int s;
  113.     u_short port;
  114.     int pv[2], pid, ready, readfrom, cc;
  115.     char buf[BUFSIZ], sig;
  116.     int one = 1;
  117.  
  118.     (void) signal(SIGINT, SIG_DFL);
  119.     (void) signal(SIGQUIT, SIG_DFL);
  120.     (void) signal(SIGTERM, SIG_DFL);
  121. #ifdef DEBUG
  122.     { int t = open(_PATH_TTY, 2);
  123.       if (t >= 0) {
  124.         ioctl(t, TIOCNOTTY, (char *)0);
  125.         (void) close(t);
  126.       }
  127.     }
  128. #endif
  129.     dup2(f, 0);
  130.     dup2(f, 1);
  131.     dup2(f, 2);
  132.     (void) alarm(60);
  133.     port = 0;
  134.     for (;;) {
  135.         char c;
  136.         if (read(f, &c, 1) != 1)
  137.             exit(1);
  138.         if (c == 0)
  139.             break;
  140.         port = port * 10 + c - '0';
  141.     }
  142.     (void) alarm(0);
  143.     if (port != 0) {
  144.         s = socket(AF_INET, SOCK_STREAM, 0);
  145.         if (s < 0)
  146.             exit(1);
  147.         if (bind(s, (struct sockaddr *)&asin, sizeof (asin)) < 0)
  148.             exit(1);
  149.         (void) alarm(60);
  150.         fromp->sin_port = htons(port);
  151.         if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0)
  152.             exit(1);
  153.         (void) alarm(0);
  154.     }
  155.  
  156.     user = getstr ("username");
  157.     pass = getstr ("password");
  158.     cmdbuf = getstr ("command");
  159.  
  160.     setpwent();
  161.     pwd = getpwnam(user);
  162.     if (pwd == NULL) {
  163.         error("Login incorrect.\n");
  164.         exit(1);
  165.     }
  166.     endpwent();
  167.     if (*pwd->pw_passwd != '\0') {
  168.         namep = CRYPT (pass, pwd->pw_passwd);
  169.         if (strcmp(namep, pwd->pw_passwd)) {
  170.             error("Password incorrect.\n");
  171.             exit(1);
  172.         }
  173.     }
  174.     if (chdir(pwd->pw_dir) < 0) {
  175.         error("No remote directory.\n");
  176.         exit(1);
  177.     }
  178.     (void) write(2, "\0", 1);
  179.     if (port) {
  180.         (void) pipe(pv);
  181.         pid = fork();
  182.         if (pid == -1)  {
  183.             error("Try again.\n");
  184.             exit(1);
  185.         }
  186.         if (pid) {
  187.             (void) close(0); (void) close(1); (void) close(2);
  188.             (void) close(f); (void) close(pv[1]);
  189.             readfrom = (1<<s) | (1<<pv[0]);
  190.             ioctl(pv[1], FIONBIO, (char *)&one);
  191.             /* should set s nbio! */
  192.             do {
  193.                 ready = readfrom;
  194.                 (void) select(16, (fd_set *)&ready,
  195.                     (fd_set *)NULL, (fd_set *)NULL,
  196.                     (struct timeval *)NULL);
  197.                 if (ready & (1<<s)) {
  198.                     if (read(s, &sig, 1) <= 0)
  199.                         readfrom &= ~(1<<s);
  200.                     else
  201.                         killpg(pid, sig);
  202.                 }
  203.                 if (ready & (1<<pv[0])) {
  204.                     cc = read(pv[0], buf, sizeof (buf));
  205.                     if (cc <= 0) {
  206.                         shutdown(s, 1+1);
  207.                         readfrom &= ~(1<<pv[0]);
  208.                     } else
  209.                         (void) write(s, buf, cc);
  210.                 }
  211.             } while (readfrom);
  212.             exit(0);
  213.         }
  214.         setpgid (0, getpid());
  215.         (void) close(s); (void)close(pv[0]);
  216.         dup2(pv[1], 2);
  217.     }
  218.     if (*pwd->pw_shell == '\0')
  219.         pwd->pw_shell = _PATH_BSHELL;
  220.     if (f > 2)
  221.         (void) close(f);
  222.     (void) setgid((gid_t)pwd->pw_gid);
  223.     initgroups(pwd->pw_name, pwd->pw_gid);
  224.     (void) setuid((uid_t)pwd->pw_uid);
  225.     (void)strcat(path, _PATH_DEFPATH);
  226.     environ = envinit;
  227.     strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
  228.     strncat(shell, pwd->pw_shell, sizeof(shell)-7);
  229.     strncat(username, pwd->pw_name, sizeof(username)-6);
  230.     cp = strrchr(pwd->pw_shell, '/');
  231.     if (cp)
  232.         cp++;
  233.     else
  234.         cp = pwd->pw_shell;
  235.     execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
  236.     perror(pwd->pw_shell);
  237.     exit(1);
  238. }
  239.  
  240. /*VARARGS1*/
  241. error(fmt, a1, a2, a3)
  242.     char *fmt;
  243.     int a1, a2, a3;
  244. {
  245.     char buf[BUFSIZ];
  246.  
  247.     buf[0] = 1;
  248.     (void) sprintf(buf+1, fmt, a1, a2, a3);
  249.     (void) write(2, buf, strlen(buf));
  250. }
  251.  
  252. char *
  253. getstr(err)
  254.     char *err;
  255. {
  256.     size_t buf_len = 100;
  257.     char *buf = malloc (buf_len), *end = buf;
  258.  
  259.     if (! buf) {
  260.         error ("Out of space reading %s\n", err);
  261.         exit (1);
  262.     }
  263.  
  264.     do {
  265.         /* Oh this is efficient, oh yes.  [But what can be done?] */
  266.         int rd = read(STDIN_FILENO, end, 1);
  267.         if (rd <= 0) {
  268.             if (rd == 0)
  269.                 error ("EOF reading %s\n", err);
  270.             else
  271.                 perror (err);
  272.             exit(1);
  273.         }
  274.  
  275.         end += rd;
  276.         if ((buf + buf_len - end) < (buf_len >> 3)) {
  277.             /* Not very much room left in our buffer, grow it. */
  278.             size_t end_offs = end - buf;
  279.             buf_len += buf_len;
  280.             buf = realloc (buf, buf_len);
  281.             if (! buf) {
  282.                 error ("Out of space reading %s\n", err);
  283.                 exit (1);
  284.             }
  285.             end = buf + end_offs;
  286.         }
  287.     } while (*(end - 1));
  288.  
  289.     return buf;
  290. }
  291.