home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / libexec / rexecd / rexecd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-12  |  6.3 KB  |  258 lines

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