home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / rsync221.zip / clientserver.c < prev    next >
C/C++ Source or Header  |  1999-03-04  |  10KB  |  469 lines

  1. /* 
  2.    Copyright (C) Andrew Tridgell 1998
  3.    
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2 of the License, or
  7.    (at your option) any later version.
  8.    
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.    
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. /* the socket based protocol for setting up a connection wit rsyncd */
  20.  
  21. #include "rsync.h"
  22.  
  23. extern int module_id;
  24. extern int read_only;
  25. extern int verbose;
  26. extern int rsync_port;
  27. char *auth_user;
  28.  
  29. int start_socket_client(char *host, char *path, int argc, char *argv[])
  30. {
  31.     int fd, i;
  32.     char *sargs[MAX_ARGS];
  33.     int sargc=0;
  34.     char line[MAXPATHLEN];
  35.     char *p, *user=NULL;
  36.     extern int remote_version;
  37.     extern int am_client;
  38.     extern int am_sender;
  39.  
  40.     if (*path == '/') {
  41.         rprintf(FERROR,"ERROR: The remote path must start with a module name\n");
  42.         return -1;
  43.     }
  44.  
  45.     p = strchr(host, '@');
  46.     if (p) {
  47.         user = host;
  48.         host = p+1;
  49.         *p = 0;
  50.     }
  51.  
  52.     if (!user) user = getenv("USER");
  53.     if (!user) user = getenv("LOGNAME");
  54.  
  55.     am_client = 1;
  56.  
  57.     fd = open_socket_out(host, rsync_port);
  58.     if (fd == -1) {
  59.         exit_cleanup(RERR_SOCKETIO);
  60.     }
  61.     
  62.     server_options(sargs,&sargc);
  63.  
  64.     sargs[sargc++] = ".";
  65.  
  66.     if (path && *path) 
  67.         sargs[sargc++] = path;
  68.  
  69.     sargs[sargc] = NULL;
  70.  
  71.     io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
  72.  
  73.     if (!read_line(fd, line, sizeof(line)-1)) {
  74.         return -1;
  75.     }
  76.  
  77.     if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
  78.         return -1;
  79.     }
  80.  
  81.     p = strchr(path,'/');
  82.     if (p) *p = 0;
  83.     io_printf(fd,"%s\n",path);
  84.     if (p) *p = '/';
  85.  
  86.     while (1) {
  87.         if (!read_line(fd, line, sizeof(line)-1)) {
  88.             return -1;
  89.         }
  90.  
  91.         if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
  92.             auth_client(fd, user, line+18);
  93.             continue;
  94.         }
  95.  
  96.         if (strcmp(line,"@RSYNCD: OK") == 0) break;
  97.         rprintf(FINFO,"%s\n", line);
  98.     }
  99.  
  100.     for (i=0;i<sargc;i++) {
  101.         io_printf(fd,"%s\n", sargs[i]);
  102.     }
  103.     io_printf(fd,"\n");
  104.  
  105.     if (remote_version > 17 && !am_sender)
  106.         io_start_multiplex_in(fd);
  107.  
  108.     return client_run(fd, fd, -1, argc, argv);
  109. }
  110.  
  111.  
  112.  
  113. static int rsync_module(int fd, int i)
  114. {
  115.     int argc=0;
  116.     char *argv[MAX_ARGS];
  117.     char **argp;
  118.     char line[MAXPATHLEN];
  119.     uid_t uid = (uid_t)-2;
  120.     gid_t gid = (gid_t)-2;
  121.     char *p;
  122.     char *addr = client_addr(fd);
  123.     char *host = client_name(fd);
  124.     char *name = lp_name(i);
  125.     int use_chroot = lp_use_chroot(i);
  126.     int start_glob=0;
  127.     int ret;
  128.     char *request=NULL;
  129.     extern int am_sender;
  130.     extern int remote_version;
  131.     extern int am_root;
  132.  
  133.     if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
  134.         rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
  135.             name, client_name(fd), client_addr(fd));
  136.         io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
  137.               name, client_name(fd), client_addr(fd));
  138.         return -1;
  139.     }
  140.  
  141.     if (!claim_connection(lp_lock_file(), lp_max_connections())) {
  142.         if (errno) {
  143.             rprintf(FERROR,"failed to open lock file %s : %s\n",
  144.                 lp_lock_file(), strerror(errno));
  145.             io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
  146.                   lp_lock_file(), strerror(errno));
  147.         } else {
  148.             rprintf(FERROR,"max connections (%d) reached\n",
  149.                 lp_max_connections());
  150.             io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections());
  151.         }
  152.         return -1;
  153.     }
  154.  
  155.     
  156.     auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
  157.  
  158.     if (!auth_user) {
  159.         rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
  160.             name, client_name(fd), client_addr(fd));
  161.         io_printf(fd,"@ERROR: auth failed on module %s\n",name);
  162.         return -1;        
  163.     }
  164.  
  165.     module_id = i;
  166.  
  167.     if (lp_read_only(i))
  168.         read_only = 1;
  169.  
  170.     p = lp_uid(i);
  171.     if (!name_to_uid(p, &uid)) {
  172.         if (!isdigit(*p)) {
  173.             rprintf(FERROR,"Invalid uid %s\n", p);
  174.             io_printf(fd,"@ERROR: invalid uid\n");
  175.             return -1;
  176.         } 
  177.         uid = atoi(p);
  178.     }
  179.  
  180.     p = lp_gid(i);
  181.     if (!name_to_gid(p, &gid)) {
  182.         if (!isdigit(*p)) {
  183.             rprintf(FERROR,"Invalid gid %s\n", p);
  184.             io_printf(fd,"@ERROR: invalid gid\n");
  185.             return -1;
  186.         } 
  187.         gid = atoi(p);
  188.     }
  189.  
  190.     p = lp_include_from(i);
  191.     add_exclude_file(p, 1, 1);
  192.  
  193.     p = lp_include(i);
  194.     add_include_line(p);
  195.  
  196.     p = lp_exclude_from(i);
  197.     add_exclude_file(p, 1, 0);
  198.  
  199.     p = lp_exclude(i);
  200.     add_exclude_line(p);
  201.  
  202.     log_open();
  203.  
  204.     if (use_chroot) {
  205. #ifndef __EMX__
  206.         if (chroot(lp_path(i))) {
  207.             rprintf(FERROR,"chroot %s failed\n", lp_path(i));
  208.             io_printf(fd,"@ERROR: chroot failed\n");
  209.             return -1;
  210.         }
  211. #endif
  212.         if (chdir("/")) {
  213.             rprintf(FERROR,"chdir %s failed\n", lp_path(i));
  214.             io_printf(fd,"@ERROR: chdir failed\n");
  215.             return -1;
  216.         }
  217.  
  218.         if (setgid(gid) || getgid() != gid) {
  219.             rprintf(FERROR,"setgid %d failed\n", gid);
  220.             io_printf(fd,"@ERROR: setgid failed\n");
  221.             return -1;
  222.         }
  223.  
  224.         if (setuid(uid) || getuid() != uid) {
  225.             rprintf(FERROR,"setuid %d failed\n", uid);
  226.             io_printf(fd,"@ERROR: setuid failed\n");
  227.             return -1;
  228.         }
  229.  
  230.     } else {
  231.         if (!push_dir(lp_path(i), 0)) {
  232.             rprintf(FERROR,"chdir %s failed\n", lp_path(i));
  233.             io_printf(fd,"@ERROR: chdir failed\n");
  234.             return -1;
  235.         }
  236.     }
  237.  
  238.     am_root = (getuid() == 0);
  239.  
  240.     io_printf(fd,"@RSYNCD: OK\n");
  241.  
  242.     argv[argc++] = "rsyncd";
  243.  
  244.     while (1) {
  245.         if (!read_line(fd, line, sizeof(line)-1)) {
  246.             return -1;
  247.         }
  248.  
  249.         if (!*line) break;
  250.  
  251.         p = line;
  252.  
  253.         argv[argc] = strdup(p);
  254.         if (!argv[argc]) {
  255.             return -1;
  256.         }
  257.  
  258.         if (start_glob) {
  259.             if (start_glob == 1) {
  260.                 request = strdup(p);
  261.                 start_glob++;
  262.             }
  263.             glob_expand(name, argv, &argc, MAX_ARGS, !use_chroot);
  264.         } else {
  265.             argc++;
  266.         }
  267.  
  268.         if (strcmp(line,".") == 0) {
  269.             start_glob = 1;
  270.         }
  271.  
  272.         if (argc == MAX_ARGS) {
  273.             return -1;
  274.         }
  275.     }
  276.  
  277.     if (!use_chroot) {
  278.         /*
  279.          * Note that this is applied to all parameters, whether or not
  280.          *    they are filenames, but no other legal parameters contain
  281.          *    the forms that need to be sanitized so it doesn't hurt;
  282.          *    it is not known at this point which parameters are files
  283.          *    and which aren't.
  284.          */
  285.         for (i = 1; i < argc; i++) {
  286.             sanitize_path(argv[i]);
  287.         }
  288.     }
  289.  
  290.     ret = parse_arguments(argc, argv);
  291.  
  292.     if (request) {
  293.         if (*auth_user) {
  294.             rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
  295.                 am_sender?"on":"to",
  296.                 request, auth_user, host, addr);
  297.         } else {
  298.             rprintf(FINFO,"rsync %s %s from %s (%s)\n",
  299.                 am_sender?"on":"to",
  300.                 request, host, addr);
  301.         }
  302.         free(request);
  303.     }
  304.  
  305. #if !TRIDGE
  306.     /* don't allow the logs to be flooded too fast */
  307.     if (verbose > 1) verbose = 1;
  308. #endif
  309.  
  310.     argc -= optind;
  311.     argp = argv + optind;
  312.     optind = 0;
  313.  
  314.     if (remote_version > 17 && am_sender)
  315.         io_start_multiplex_out(fd);
  316.  
  317.     if (!ret) {
  318.         option_error();
  319.     }
  320.  
  321.     if (lp_timeout(i)) {
  322.         extern int io_timeout;
  323.         io_timeout = lp_timeout(i);
  324.     }
  325.  
  326.     start_server(fd, fd, argc, argp);
  327.  
  328.     return 0;
  329. }
  330.  
  331. /* send a list of available modules to the client. Don't list those
  332.    with "list = False". */
  333. static void send_listing(int fd)
  334. {
  335.     int n = lp_numservices();
  336.     int i;
  337.     
  338.     for (i=0;i<n;i++)
  339.         if (lp_list(i))
  340.             io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
  341. }
  342.  
  343. /* this is called when a socket connection is established to a client
  344.    and we want to start talking. The setup of the system is done from
  345.    here */
  346. static int start_daemon(int fd)
  347. {
  348.     char line[200];
  349.     char *motd;
  350.     int i = -1;
  351.     extern char *config_file;
  352.     extern int remote_version;
  353.  
  354.     if (!lp_load(config_file, 0)) {
  355.         exit_cleanup(RERR_SYNTAX);
  356.     }
  357.  
  358.     set_socket_options(fd,"SO_KEEPALIVE");
  359.     set_socket_options(fd,lp_socket_options());
  360.     
  361.  
  362.     io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
  363.  
  364.     motd = lp_motd_file();
  365.     if (motd && *motd) {
  366.         FILE *f = fopen(motd,"r");
  367.         while (f && !feof(f)) {
  368.             int len = fread(line, 1, sizeof(line)-1, f);
  369.             if (len > 0) {
  370.                 line[len] = 0;
  371.                 io_printf(fd,"%s", line);
  372.             }
  373.         }
  374.         if (f) fclose(f);
  375.         io_printf(fd,"\n");
  376.     }
  377.  
  378.     if (!read_line(fd, line, sizeof(line)-1)) {
  379.         return -1;
  380.     }
  381.  
  382.     if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
  383.         io_printf(fd,"@ERROR: protocol startup error\n");
  384.         return -1;
  385.     }    
  386.  
  387.     while (i == -1) {
  388.         line[0] = 0;
  389.         if (!read_line(fd, line, sizeof(line)-1)) {
  390.             return -1;
  391.         }
  392.  
  393.         if (!*line || strcmp(line,"#list")==0) {
  394.             send_listing(fd);
  395.             return -1;
  396.         } 
  397.  
  398.         if (*line == '#') {
  399.             /* it's some sort of command that I don't understand */
  400.             io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
  401.             return -1;
  402.         }
  403.  
  404.         i = lp_number(line);
  405.         if (i == -1) {
  406.             io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
  407.             return -1;
  408.         }
  409.     }
  410.  
  411.     return rsync_module(fd, i);
  412. }
  413.  
  414.  
  415. int daemon_main(void)
  416. {
  417.     extern char *config_file;
  418.     char *pid_file;
  419.  
  420.     /* this ensures that we don't call getcwd after the chroot,
  421.            which doesn't work on platforms that use popen("pwd","r")
  422.            for getcwd */
  423.     push_dir("/", 0);
  424.  
  425.     if (is_a_socket(STDIN_FILENO)) {
  426.         int i;
  427.  
  428.         /* we are running via inetd - close off stdout and
  429.            stderr so that library functions (and getopt) don't
  430.            try to use them. Redirect them to /dev/null */
  431.         for (i=1;i<3;i++) {
  432.             close(i); 
  433.             open("/dev/null", O_RDWR);
  434.         }
  435.  
  436.         set_nonblocking(STDIN_FILENO);
  437.  
  438.         return start_daemon(STDIN_FILENO);
  439.     }
  440.  
  441.     become_daemon();
  442.  
  443.     if (!lp_load(config_file, 1)) {
  444.         fprintf(stderr,"failed to load config file %s\n", config_file);
  445.         exit_cleanup(RERR_SYNTAX);
  446.     }
  447.  
  448.     log_open();
  449.  
  450.     rprintf(FINFO,"rsyncd version %s starting\n",VERSION);
  451.  
  452.     if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
  453.         FILE *f;
  454.         int pid = (int) getpid();
  455.         cleanup_set_pid(pid);
  456.         if ((f = fopen(lp_pid_file(), "w")) == NULL) {
  457.             cleanup_set_pid(0);
  458.             fprintf(stderr,"failed to create pid file %s\n", pid_file);
  459.             exit_cleanup(RERR_FILEIO);
  460.         }
  461.         fprintf(f, "%d\n", pid);
  462.         fclose(f);
  463.     }
  464.  
  465.     start_accept_loop(rsync_port, start_daemon);
  466.     return -1;
  467. }
  468.  
  469.