+/* Start the remote shell. cmd may be NULL to use the default. */
+static pid_t do_cmd(char *cmd, char *machine, char *user, char **remote_argv, int remote_argc,
+ int *f_in_p, int *f_out_p)
+{
+ int i, argc = 0;
+ char *args[MAX_ARGS];
+ pid_t pid;
+ int dash_l_set = 0;
+
+ if (!read_batch && !local_server) {
+ char *t, *f, in_quote = '\0';
+ char *rsh_env = getenv(RSYNC_RSH_ENV);
+ if (!cmd)
+ cmd = rsh_env;
+ if (!cmd)
+ cmd = RSYNC_RSH;
+ cmd = strdup(cmd); /* MEMORY LEAK */
+ if (!cmd)
+ goto oom;
+
+ for (t = f = cmd; *f; f++) {
+ if (*f == ' ')
+ continue;
+ /* Comparison leaves rooms for server_options(). */
+ if (argc >= MAX_ARGS - MAX_SERVER_ARGS)
+ goto arg_overflow;
+ args[argc++] = t;
+ while (*f != ' ' || in_quote) {
+ if (!*f) {
+ if (in_quote) {
+ rprintf(FERROR,
+ "Missing trailing-%c in remote-shell command.\n",
+ in_quote);
+ exit_cleanup(RERR_SYNTAX);
+ }
+ f--;
+ break;
+ }
+ if (*f == '\'' || *f == '"') {
+ if (!in_quote) {
+ in_quote = *f++;
+ continue;
+ }
+ if (*f == in_quote && *++f != in_quote) {
+ in_quote = '\0';
+ continue;
+ }
+ }
+ *t++ = *f++;
+ }
+ *t++ = '\0';
+ }
+
+ /* check to see if we've already been given '-l user' in
+ * the remote-shell command */
+ for (i = 0; i < argc-1; i++) {
+ if (!strcmp(args[i], "-l") && args[i+1][0] != '-')
+ dash_l_set = 1;
+ }
+
+#ifdef HAVE_REMSH
+ /* remsh (on HPUX) takes the arguments the other way around */
+ args[argc++] = machine;
+ if (user && !(daemon_over_rsh && dash_l_set)) {
+ args[argc++] = "-l";
+ args[argc++] = user;
+ }
+#else
+ if (user && !(daemon_over_rsh && dash_l_set)) {
+ args[argc++] = "-l";
+ args[argc++] = user;
+ }
+ args[argc++] = machine;
+#endif
+
+ args[argc++] = rsync_path;
+
+ if (blocking_io < 0) {
+ char *cp;
+ if ((cp = strrchr(cmd, '/')) != NULL)
+ cp++;
+ else
+ cp = cmd;
+ if (strcmp(cp, "rsh") == 0 || strcmp(cp, "remsh") == 0)
+ blocking_io = 1;
+ }
+
+ server_options(args,&argc);
+
+ if (argc >= MAX_ARGS - 2)
+ goto arg_overflow;
+ }
+
+ args[argc++] = ".";
+
+ if (!daemon_over_rsh) {
+ while (remote_argc > 0) {
+ if (argc >= MAX_ARGS - 1) {
+ arg_overflow:
+ rprintf(FERROR, "internal: args[] overflowed in do_cmd()\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ args[argc++] = *remote_argv++;
+ remote_argc--;
+ }
+ }
+
+ args[argc] = NULL;
+
+ if (verbose > 3) {
+ for (i = 0; i < argc; i++)
+ rprintf(FCLIENT, "cmd[%d]=%s ", i, args[i]);
+ rprintf(FCLIENT, "\n");
+ }
+
+ if (read_batch) {
+ int from_gen_pipe[2];
+ set_allow_inc_recurse();
+ if (fd_pair(from_gen_pipe) < 0) {
+ rsyserr(FERROR, errno, "pipe");
+ exit_cleanup(RERR_IPC);
+ }
+ batch_gen_fd = from_gen_pipe[0];
+ *f_out_p = from_gen_pipe[1];
+ *f_in_p = batch_fd;
+ pid = (pid_t)-1; /* no child pid */
+#ifdef ICONV_CONST
+ setup_iconv();
+#endif
+ } else if (local_server) {
+ /* If the user didn't request --[no-]whole-file, force
+ * it on, but only if we're not batch processing. */
+ if (whole_file < 0 && !write_batch)
+ whole_file = 1;
+ set_allow_inc_recurse();
+ pid = local_child(argc, args, f_in_p, f_out_p, child_main);
+#ifdef ICONV_CONST
+ setup_iconv();
+#endif
+ } else {
+ pid = piped_child(args, f_in_p, f_out_p);
+#ifdef ICONV_CONST
+ setup_iconv();
+#endif
+ if (protect_args)
+ send_protected_args(*f_out_p, args);
+ }
+
+ return pid;
+
+ oom:
+ out_of_memory("do_cmd");
+ return 0; /* not reached */
+}
+
+/* The receiving side operates in one of two modes:
+ *
+ * 1. it receives any number of files into a destination directory,
+ * placing them according to their names in the file-list.
+ *
+ * 2. it receives a single file and saves it using the name in the
+ * destination path instead of its file-list name. This requires a
+ * "local name" for writing out the destination file.
+ *
+ * So, our task is to figure out what mode/local-name we need.
+ * For mode 1, we change into the destination directory and return NULL.
+ * For mode 2, we change into the directory containing the destination
+ * file (if we aren't already there) and return the local-name. */
+static char *get_local_name(struct file_list *flist, char *dest_path)
+{
+ STRUCT_STAT st;
+ int statret;
+ char *cp;
+
+ if (verbose > 2) {
+ rprintf(FINFO, "get_local_name count=%d %s\n",
+ file_total, NS(dest_path));
+ }
+
+ if (!dest_path || list_only)
+ return NULL;
+
+ if (server_filter_list.head
+ && (check_filter(&server_filter_list, dest_path, 0 != 0) < 0
+ || check_filter(&server_filter_list, dest_path, 1 != 0) < 0)) {
+ rprintf(FERROR, "skipping daemon-excluded destination \"%s\"\n",
+ dest_path);
+ exit_cleanup(RERR_FILESELECT);
+ }
+
+ /* See what currently exists at the destination. */
+ if ((statret = do_stat(dest_path, &st)) == 0) {
+ /* If the destination is a dir, enter it and use mode 1. */
+ if (S_ISDIR(st.st_mode)) {
+ if (!push_dir(dest_path, 0)) {
+ rsyserr(FERROR, errno, "push_dir#1 %s failed",
+ full_fname(dest_path));
+ exit_cleanup(RERR_FILESELECT);
+ }
+ return NULL;
+ }
+ if (file_total > 1) {
+ rprintf(FERROR,
+ "ERROR: destination must be a directory when"
+ " copying more than 1 file\n");
+ exit_cleanup(RERR_FILESELECT);
+ }
+ if (file_total == 1 && S_ISDIR(flist->files[0]->mode)) {
+ rprintf(FERROR,
+ "ERROR: cannot overwrite non-directory"
+ " with a directory\n");
+ exit_cleanup(RERR_FILESELECT);
+ }
+ } else if (errno != ENOENT) {
+ /* If we don't know what's at the destination, fail. */
+ rsyserr(FERROR, errno, "ERROR: cannot stat destination %s",
+ full_fname(dest_path));
+ exit_cleanup(RERR_FILESELECT);
+ }
+
+ cp = strrchr(dest_path, '/');
+
+ /* If we need a destination directory because the transfer is not
+ * of a single non-directory or the user has requested one via a
+ * destination path ending in a slash, create one and use mode 1. */
+ if (file_total > 1 || (cp && !cp[1])) {
+ /* Lop off the final slash (if any). */
+ if (cp && !cp[1])
+ *cp = '\0';
+
+ if (statret == 0) {
+ rprintf(FERROR,
+ "ERROR: destination path is not a directory\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+
+ if (mkdir_defmode(dest_path) != 0) {
+ rsyserr(FERROR, errno, "mkdir %s failed",
+ full_fname(dest_path));
+ exit_cleanup(RERR_FILEIO);
+ }
+
+ if (flist->high >= flist->low
+ && strcmp(flist->files[flist->low]->basename, ".") == 0)
+ flist->files[0]->flags |= FLAG_DIR_CREATED;
+
+ if (verbose)
+ rprintf(FINFO, "created directory %s\n", dest_path);
+
+ if (dry_run) {
+ /* Indicate that dest dir doesn't really exist. */
+ dry_run++;
+ }
+
+ if (!push_dir(dest_path, dry_run > 1)) {
+ rsyserr(FERROR, errno, "push_dir#2 %s failed",
+ full_fname(dest_path));
+ exit_cleanup(RERR_FILESELECT);
+ }
+
+ return NULL;
+ }
+
+ /* Otherwise, we are writing a single file, possibly on top of an
+ * existing non-directory. Change to the item's parent directory
+ * (if it has a path component), return the basename of the
+ * destination file as the local name, and use mode 2. */
+ if (!cp)
+ return dest_path;
+
+ if (cp == dest_path)
+ dest_path = "/";
+
+ *cp = '\0';
+ if (!push_dir(dest_path, 0)) {
+ rsyserr(FERROR, errno, "push_dir#3 %s failed",
+ full_fname(dest_path));
+ exit_cleanup(RERR_FILESELECT);
+ }
+ *cp = '/';
+
+ return cp + 1;
+}
+
+/* This function checks on our alternate-basis directories. If we're in
+ * dry-run mode and the destination dir does not yet exist, we'll try to
+ * tweak any dest-relative paths to make them work for a dry-run (the
+ * destination dir must be in curr_dir[] when this function is called).
+ * We also warn about any arg that is non-existent or not a directory. */
+static void check_alt_basis_dirs(void)
+{
+ STRUCT_STAT st;
+ char **dir_p, *slash = strrchr(curr_dir, '/');
+
+ for (dir_p = basis_dir; *dir_p; dir_p++) {
+ if (dry_run > 1 && **dir_p != '/') {
+ int len = curr_dir_len + 1 + strlen(*dir_p) + 1;
+ char *new = new_array(char, len);
+ if (!new)
+ out_of_memory("check_alt_basis_dirs");
+ if (slash && strncmp(*dir_p, "../", 3) == 0) {
+ /* We want to remove only one leading "../" prefix for
+ * the directory we couldn't create in dry-run mode:
+ * this ensures that any other ".." references get
+ * evaluated the same as they would for a live copy. */
+ *slash = '\0';
+ pathjoin(new, len, curr_dir, *dir_p + 3);
+ *slash = '/';
+ } else
+ pathjoin(new, len, curr_dir, *dir_p);
+ *dir_p = new;
+ }
+ if (do_stat(*dir_p, &st) < 0) {
+ rprintf(FWARNING, "%s arg does not exist: %s\n",
+ dest_option, *dir_p);
+ } else if (!S_ISDIR(st.st_mode)) {
+ rprintf(FWARNING, "%s arg is not a dir: %s\n",
+ dest_option, *dir_p);
+ }
+ }
+}
+
+/* This is only called by the sender. */
+static void read_final_goodbye(int f_in)
+{
+ int i, iflags, xlen;
+ uchar fnamecmp_type;
+ char xname[MAXPATHLEN];
+
+ if (protocol_version < 29)
+ i = read_int(f_in);
+ else {
+ i = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
+ xname, &xlen);
+ }
+
+ if (i != NDX_DONE) {
+ rprintf(FERROR, "Invalid packet at end of run (%d) [%s]\n",
+ i, who_am_i());
+ exit_cleanup(RERR_PROTOCOL);
+ }
+}
+
+static void do_server_sender(int f_in, int f_out, int argc, char *argv[])
+{
+ struct file_list *flist;
+ char *dir = argv[0];
+
+ if (verbose > 2) {
+ rprintf(FINFO, "server_sender starting pid=%ld\n",
+ (long)getpid());
+ }
+
+ if (am_daemon && lp_write_only(module_id)) {
+ rprintf(FERROR, "ERROR: module is write only\n");
+ exit_cleanup(RERR_SYNTAX);
+ return;
+ }
+ if (am_daemon && lp_read_only(module_id) && remove_source_files) {
+ rprintf(FERROR,
+ "ERROR: --remove-%s-files cannot be used with a read-only module\n",
+ remove_source_files == 1 ? "source" : "sent");
+ exit_cleanup(RERR_SYNTAX);
+ return;
+ }
+
+ if (!relative_paths) {
+ if (!push_dir(dir, 0)) {
+ rsyserr(FERROR, errno, "push_dir#3 %s failed",
+ full_fname(dir));
+ exit_cleanup(RERR_FILESELECT);
+ }
+ }
+ argc--;
+ argv++;
+
+ if (argc == 0 && (recurse || xfer_dirs || list_only)) {
+ argc = 1;
+ argv--;
+ argv[0] = ".";
+ }
+
+ flist = send_file_list(f_out,argc,argv);
+ if (!flist || flist->used == 0)
+ exit_cleanup(0);
+
+ io_start_buffering_in(f_in);
+
+ send_files(f_in, f_out);
+ io_flush(FULL_FLUSH);
+ handle_stats(f_out);
+ if (protocol_version >= 24)
+ read_final_goodbye(f_in);
+ io_flush(FULL_FLUSH);
+ exit_cleanup(0);
+}
+
+
+static int do_recv(int f_in, int f_out, char *local_name)
+{
+ int pid;
+ int exit_code = 0;
+ int error_pipe[2];
+
+ /* The receiving side mustn't obey this, or an existing symlink that
+ * points to an identical file won't be replaced by the referent. */
+ copy_links = copy_dirlinks = copy_unsafe_links = 0;
+
+#ifdef SUPPORT_HARD_LINKS
+ if (preserve_hard_links && !inc_recurse)
+ match_hard_links(first_flist);
+#endif
+
+ if (fd_pair(error_pipe) < 0) {
+ rsyserr(FERROR, errno, "pipe failed in do_recv");
+ exit_cleanup(RERR_IPC);
+ }
+
+ io_flush(NORMAL_FLUSH);
+
+ if ((pid = do_fork()) == -1) {
+ rsyserr(FERROR, errno, "fork failed in do_recv");
+ exit_cleanup(RERR_IPC);
+ }
+
+ if (pid == 0) {
+ close(error_pipe[0]);
+ if (f_in != f_out)
+ close(f_out);
+
+ /* we can't let two processes write to the socket at one time */
+ io_end_multiplex_out();
+
+ /* set place to send errors */
+ set_msg_fd_out(error_pipe[1]);
+ io_start_buffering_out(error_pipe[1]);
+
+ recv_files(f_in, local_name);
+ io_flush(FULL_FLUSH);
+ handle_stats(f_in);
+
+ send_msg(MSG_DONE, "", 1, 0);
+ write_varlong(error_pipe[1], stats.total_read, 3);
+ io_flush(FULL_FLUSH);
+
+ /* Handle any keep-alive packets from the post-processing work
+ * that the generator does. */
+ if (protocol_version >= 29) {
+ int iflags, xlen;
+ uchar fnamecmp_type;
+ char xname[MAXPATHLEN];
+
+ kluge_around_eof = -1;
+
+ /* This should only get stopped via a USR2 signal. */
+ read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
+ xname, &xlen);
+
+ rprintf(FERROR, "Invalid packet at end of run [%s]\n",
+ who_am_i());
+ exit_cleanup(RERR_PROTOCOL);
+ }
+
+ /* Finally, we go to sleep until our parent kills us with a
+ * USR2 signal. We sleep for a short time, as on some OSes
+ * a signal won't interrupt a sleep! */
+ while (1)
+ msleep(20);
+ }
+
+ am_generator = 1;
+
+ io_end_multiplex_in();
+ if (write_batch && !am_server)
+ stop_write_batch();
+
+ close(error_pipe[1]);
+ if (f_in != f_out)
+ close(f_in);
+
+ io_start_buffering_out(f_out);
+
+ set_msg_fd_in(error_pipe[0]);
+ io_start_buffering_in(error_pipe[0]);
+
+#ifdef SUPPORT_HARD_LINKS
+ if (preserve_hard_links && inc_recurse) {
+ struct file_list *flist;
+ for (flist = first_flist; flist; flist = flist->next)
+ match_hard_links(flist);
+ }
+#endif
+
+ generate_files(f_out, local_name);
+
+ handle_stats(-1);
+ io_flush(FULL_FLUSH);
+ if (protocol_version >= 24) {
+ /* send a final goodbye message */
+ write_ndx(f_out, NDX_DONE);
+ }
+ io_flush(FULL_FLUSH);
+
+ set_msg_fd_in(-1);
+ kill(pid, SIGUSR2);
+ wait_process_with_flush(pid, &exit_code);
+ return exit_code;
+}
+
+static void do_server_recv(int f_in, int f_out, int argc, char *argv[])