home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / rexecd / rexecd.c < prev   
Encoding:
C/C++ Source or Header  |  1994-07-05  |  7.9 KB  |  325 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[] = "from: @(#)rexecd.c    5.12 (Berkeley) 2/25/91";*/
  42. static char rcsid[] = "$Id: rexecd.c,v 1.1 1994/05/23 09:07:53 rzsfl Exp rzsfl $";
  43. #endif /* not lint */
  44.  
  45. #include <sys/param.h>
  46. #include <sys/ioctl.h>
  47. #include <sys/socket.h>
  48. #include <sys/time.h>
  49. #include <netinet/in.h>
  50. #include <signal.h>
  51. #include <netdb.h>
  52. #include <pwd.h>
  53. #include <errno.h>
  54. #include <unistd.h>
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <string.h>
  58. #include <paths.h>
  59.  
  60. /*VARARGS1*/
  61. int error();
  62.  
  63. #ifdef TCP_WRAPPER
  64. #include <syslog.h>
  65. #include "log_tcp.h"
  66.  
  67. struct from_host from_host;
  68. int allow_severity = LOG_INFO;
  69. int deny_severity = LOG_WARNING;
  70. #endif
  71.  
  72. #ifdef RESTRICT_FTP
  73. #define FTPUSERS        "/etc/ftpusers"
  74. #endif
  75.  
  76. /*
  77.  * remote execute server:
  78.  *    username\0
  79.  *    password\0
  80.  *    command\0
  81.  *    data
  82.  */
  83. /*ARGSUSED*/
  84. main(argc, argv)
  85.     int argc;
  86.     char **argv;
  87. {
  88.     struct sockaddr_in from;
  89.     int fromlen;
  90.  
  91.     fromlen = sizeof (from);
  92.     if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
  93.         (void)fprintf(stderr,
  94.             "rexecd: getpeername: %s\n", strerror(errno));
  95.         exit(1);
  96.     }
  97. #ifdef    TCP_WRAPPER
  98.     (void) openlog(argv[0], LOG_PID, LOG_DAEMON);
  99.     /* Find out and report the remote host name. */
  100.     if (fromhost(&from_host) < 0 || !hosts_access(argv[0], &from_host))
  101.         refuse(&from_host);
  102.     syslog(allow_severity, "connect from %s", hosts_info(&from_host));
  103. #endif
  104.     doit(0, &from);
  105. }
  106.  
  107. char    username[20] = "USER=";
  108. char    homedir[64] = "HOME=";
  109. char    shell[64] = "SHELL=";
  110. char    path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH=";
  111. char    *envinit[] =
  112.         {homedir, shell, path, username, 0};
  113. char    **environ;
  114.  
  115. struct    sockaddr_in asin = { AF_INET };
  116.  
  117. doit(f, fromp)
  118.     int f;
  119.     struct sockaddr_in *fromp;
  120. {
  121.     char cmdbuf[NCARGS+1], *cp, *namep;
  122.     char user[16], pass[16];
  123.     struct passwd *pwd;
  124.     int s;
  125.     u_short port;
  126.     int pv[2], pid, ready, readfrom, cc;
  127.     char buf[BUFSIZ], sig;
  128.     int one = 1;
  129. #ifdef RESTRICT_FTP
  130.     FILE *fp;
  131. #endif
  132.  
  133.     (void) signal(SIGINT, SIG_DFL);
  134.     (void) signal(SIGQUIT, SIG_DFL);
  135.     (void) signal(SIGTERM, SIG_DFL);
  136. #ifdef DEBUG
  137.     { int t = open(_PATH_TTY, 2);
  138.       if (t >= 0) {
  139.         ioctl(t, TIOCNOTTY, (char *)0);
  140.         (void) close(t);
  141.       }
  142.     }
  143. #endif
  144.     dup2(f, 0);
  145.     dup2(f, 1);
  146.     dup2(f, 2);
  147.     (void) alarm(60);
  148.     port = 0;
  149.     for (;;) {
  150.         char c;
  151.         if (read(f, &c, 1) != 1)
  152.             exit(1);
  153.         if (c == 0)
  154.             break;
  155.         port = port * 10 + c - '0';
  156.     }
  157.     (void) alarm(0);
  158.     if (port != 0) {
  159.         s = socket(AF_INET, SOCK_STREAM, 0);
  160.         if (s < 0)
  161.             exit(1);
  162.         if (bind(s, (struct sockaddr *)&asin, sizeof (asin)) < 0)
  163.             exit(1);
  164.         (void) alarm(60);
  165.         fromp->sin_port = htons(port);
  166.         if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0)
  167.             exit(1);
  168.         (void) alarm(0);
  169.     }
  170.     getstr(user, sizeof(user), "username");
  171.     getstr(pass, sizeof(pass), "password");
  172.     getstr(cmdbuf, sizeof(cmdbuf), "command");
  173.     setpwent();
  174.     pwd = getpwnam(user);
  175.     if (pwd == NULL) {
  176. #ifdef    TCP_WRAPPER
  177.         /* Log failed attempts. */
  178.         syslog(LOG_ERR, "LOGIN FAILURE from %s, %s",
  179.             hosts_info(&from_host), user);
  180. #endif
  181.         error("Login incorrect.\n");
  182.         exit(1);
  183.     }
  184.     endpwent();
  185.     if (*pwd->pw_passwd != '\0') {
  186.         namep = crypt(pass, pwd->pw_passwd);
  187.         if (strcmp(namep, pwd->pw_passwd)) {
  188. #ifdef    TCP_WRAPPER
  189.             /* Log failed attempts. */
  190.             syslog(LOG_ERR, "LOGIN FAILURE from %s, %s",
  191.                 hosts_info(&from_host), user);
  192. #endif
  193.             error("Login incorrect.\n");
  194.             exit(1);
  195.         }
  196.     }
  197.     /* Disallow access to root account. */
  198.     if (pwd->pw_uid == 0) {
  199. #ifdef    TCP_WRAPPER
  200.         syslog(LOG_ERR, "%s LOGIN REFUSED from %s",
  201.             user, hosts_info(&from_host));
  202. #endif
  203.         error("Login incorrect.\n");
  204.         exit(1);
  205.     }
  206. #ifdef RESTRICT_FTP
  207.     /* Disallow access to accounts in /etc/ftpusers. */
  208.     if ((fp = fopen(FTPUSERS, "r")) != NULL) {
  209.         while (fgets(buf, sizeof (buf), fp) != NULL) {
  210.         if ((cp = index(buf, '\n')) != NULL)
  211.             *cp = '\0';
  212.         if (strcmp(buf, pwd->pw_name) == 0) {
  213. #ifdef    TCP_WRAPPER
  214.             syslog(LOG_ERR, "%s LOGIN REFUSED from %s",
  215.                 user, hosts_info(&from_host));
  216. #endif
  217.             error("Login incorrect.\n");
  218.             exit(1);
  219.         }
  220.         }
  221.     }
  222.     (void) fclose(fp);
  223. #endif
  224.  
  225. #ifdef    TCP_WRAPPER
  226.     /* Log successfull attempts. */
  227.     syslog(LOG_INFO, "login from %s as %s", hosts_info(&from_host), user);
  228. #endif
  229.  
  230.     if (chdir(pwd->pw_dir) < 0) {
  231.         error("No remote directory.\n");
  232.         exit(1);
  233.     }
  234.     (void) write(2, "\0", 1);
  235.     if (port) {
  236.         (void) pipe(pv);
  237.         pid = fork();
  238.         if (pid == -1)  {
  239.             error("Try again.\n");
  240.             exit(1);
  241.         }
  242.         if (pid) {
  243.             (void) close(0); (void) close(1); (void) close(2);
  244.             (void) close(f); (void) close(pv[1]);
  245.             readfrom = (1<<s) | (1<<pv[0]);
  246.             ioctl(pv[1], FIONBIO, (char *)&one);
  247.             /* should set s nbio! */
  248.             do {
  249.                 ready = readfrom;
  250.                 (void) select(16, (fd_set *)&ready,
  251.                     (fd_set *)NULL, (fd_set *)NULL,
  252.                     (struct timeval *)NULL);
  253.                 if (ready & (1<<s)) {
  254.                     if (read(s, &sig, 1) <= 0)
  255.                         readfrom &= ~(1<<s);
  256.                     else
  257.                         killpg(pid, sig);
  258.                 }
  259.                 if (ready & (1<<pv[0])) {
  260.                     cc = read(pv[0], buf, sizeof (buf));
  261.                     if (cc <= 0) {
  262.                         shutdown(s, 1+1);
  263.                         readfrom &= ~(1<<pv[0]);
  264.                     } else
  265.                         (void) write(s, buf, cc);
  266.                 }
  267.             } while (readfrom);
  268.             exit(0);
  269.         }
  270.         setpgrp();
  271.         (void) close(s); (void)close(pv[0]);
  272.         dup2(pv[1], 2);
  273.     }
  274.     if (*pwd->pw_shell == '\0')
  275.         pwd->pw_shell = _PATH_BSHELL;
  276.     if (f > 2)
  277.         (void) close(f);
  278.     (void) setgid((gid_t)pwd->pw_gid);
  279.     initgroups(pwd->pw_name, pwd->pw_gid);
  280.     (void) setuid((uid_t)pwd->pw_uid);
  281.     (void)strcat(path, _PATH_DEFPATH);
  282.     environ = envinit;
  283.     strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
  284.     strncat(shell, pwd->pw_shell, sizeof(shell)-7);
  285.     strncat(username, pwd->pw_name, sizeof(username)-6);
  286.     cp = rindex(pwd->pw_shell, '/');
  287.     if (cp)
  288.         cp++;
  289.     else
  290.         cp = pwd->pw_shell;
  291.     execle(pwd->pw_shell, cp, "-c", cmdbuf, 0, environ);
  292.     perror(pwd->pw_shell);
  293.     exit(1);
  294. }
  295.  
  296. /*VARARGS1*/
  297. error(fmt, a1, a2, a3)
  298.     char *fmt;
  299.     int a1, a2, a3;
  300. {
  301.     char buf[BUFSIZ];
  302.  
  303.     buf[0] = 1;
  304.     (void) sprintf(buf+1, fmt, a1, a2, a3);
  305.     (void) write(2, buf, strlen(buf));
  306. }
  307.  
  308. getstr(buf, cnt, err)
  309.     char *buf;
  310.     int cnt;
  311.     char *err;
  312. {
  313.     char c;
  314.  
  315.     do {
  316.         if (read(0, &c, 1) != 1)
  317.             exit(1);
  318.         *buf++ = c;
  319.         if (--cnt == 0) {
  320.             error("%s too long\n", err);
  321.             exit(1);
  322.         }
  323.     } while (c != 0);
  324. }
  325.