/* Copyright (C) Andrew Tridgell 1998 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* the socket based protocol for setting up a connection wit rsyncd */ #include "rsync.h" extern int module_id; extern int read_only; extern int verbose; extern int rsync_port; int start_socket_client(char *host, char *path, int argc, char *argv[]) { int fd, i; char *sargs[MAX_ARGS]; int sargc=0; char line[MAXPATHLEN]; char *p, *user=NULL; extern int remote_version; p = strchr(host, '@'); if (p) { user = host; host = p+1; *p = 0; } if (!user) user = getenv("USER"); if (!user) user = getenv("LOGNAME"); fd = open_socket_out(host, rsync_port); if (fd == -1) { exit_cleanup(1); } server_options(sargs,&sargc); sargs[sargc++] = "."; if (path && *path) sargs[sargc++] = path; sargs[sargc] = NULL; io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION); if (!read_line(fd, line, sizeof(line)-1)) { return -1; } if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) { return -1; } p = strchr(path,'/'); if (p) *p = 0; io_printf(fd,"%s\n",path); if (p) *p = '/'; while (1) { if (!read_line(fd, line, sizeof(line)-1)) { return -1; } if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) { auth_client(fd, user, line+18); continue; } if (strcmp(line,"@RSYNCD: OK") == 0) break; rprintf(FINFO,"%s\n", line); } for (i=0;i 1) verbose = 1; argc -= optind; argp = argv + optind; optind = 0; start_server(fd, fd, argc, argp); return 0; } /* send a list of available modules to the client. Don't list those with "list = False". */ static void send_listing(int fd) { int n = lp_numservices(); int i; for (i=0;i 0) { line[len] = 0; io_printf(fd,"%s", line); } } if (f) fclose(f); io_printf(fd,"\n"); } if (!read_line(fd, line, sizeof(line)-1)) { return -1; } if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) { io_printf(fd,"@ERROR: protocol startup error\n"); return -1; } while (i == -1) { line[0] = 0; if (!read_line(fd, line, sizeof(line)-1)) { return -1; } if (!*line || strcmp(line,"#list")==0) { send_listing(fd); return -1; } if (*line == '#') { /* it's some sort of command that I don't understand */ io_printf(fd,"@ERROR: Unknown command '%s'\n", line); return -1; } i = lp_number(line); if (i == -1) { io_printf(fd,"@ERROR: Unknown module '%s'\n", line); return -1; } } return rsync_module(fd, i); } int daemon_main(void) { extern char *config_file; if (is_a_socket(STDIN_FILENO)) { /* we are running via inetd */ return start_daemon(STDIN_FILENO); } become_daemon(); if (!lp_load(config_file, 1)) { fprintf(stderr,"failed to load config file %s\n", config_file); exit_cleanup(1); } log_open(); rprintf(FINFO,"rsyncd version %s starting\n",VERSION); start_accept_loop(rsync_port, start_daemon); return -1; }