+ module_dir = lp_path(i);
+ if (*module_dir == '\0') {
+ rprintf(FLOG, "No path specified for module %s\n", name);
+ io_printf(f_out, "@ERROR: no path setting.\n");
+ return -1;
+ }
+ if (use_chroot) {
+ if ((p = strstr(module_dir, "/./")) != NULL) {
+ *p = '\0'; /* Temporary... */
+ if (!(module_chdir = normalize_path(module_dir, True, NULL)))
+ return path_failure(f_out, module_dir, False);
+ *p = '/';
+ if (!(p = normalize_path(p + 2, True, &module_dirlen)))
+ return path_failure(f_out, strstr(module_dir, "/./"), False);
+ if (!(full_module_path = normalize_path(module_dir, False, NULL)))
+ full_module_path = module_dir;
+ module_dir = p;
+ } else {
+ if (!(module_chdir = normalize_path(module_dir, False, NULL)))
+ return path_failure(f_out, module_dir, False);
+ full_module_path = module_chdir;
+ module_dir = "/";
+ module_dirlen = 1;
+ }
+ } else {
+ if (!(module_chdir = normalize_path(module_dir, False, &module_dirlen)))
+ return path_failure(f_out, module_dir, False);
+ full_module_path = module_dir = module_chdir;
+ }
+ set_env_str("RSYNC_MODULE_PATH", full_module_path);
+
+ if (module_dirlen == 1) {
+ module_dirlen = 0;
+ set_filter_dir("/", 1);
+ } else
+ set_filter_dir(module_dir, module_dirlen);
+
+ p = lp_filter(i);
+ parse_filter_str(&daemon_filter_list, p, rule_template(FILTRULE_WORD_SPLIT),
+ XFLG_ABS_IF_SLASH | XFLG_DIR2WILD3);
+
+ p = lp_include_from(i);
+ parse_filter_file(&daemon_filter_list, p, rule_template(FILTRULE_INCLUDE),
+ XFLG_ABS_IF_SLASH | XFLG_DIR2WILD3 | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
+
+ p = lp_include(i);
+ parse_filter_str(&daemon_filter_list, p,
+ rule_template(FILTRULE_INCLUDE | FILTRULE_WORD_SPLIT),
+ XFLG_ABS_IF_SLASH | XFLG_DIR2WILD3 | XFLG_OLD_PREFIXES);
+
+ p = lp_exclude_from(i);
+ parse_filter_file(&daemon_filter_list, p, rule_template(0),
+ XFLG_ABS_IF_SLASH | XFLG_DIR2WILD3 | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
+
+ p = lp_exclude(i);
+ parse_filter_str(&daemon_filter_list, p, rule_template(FILTRULE_WORD_SPLIT),
+ XFLG_ABS_IF_SLASH | XFLG_DIR2WILD3 | XFLG_OLD_PREFIXES);
+
+ log_init(1);
+
+#ifdef HAVE_PUTENV
+ if (*lp_prexfer_exec(i) || *lp_postxfer_exec(i)) {
+ int status;
+
+ /* For post-xfer exec, fork a new process to run the rsync
+ * daemon while this process waits for the exit status and
+ * runs the indicated command at that point. */
+ if (*lp_postxfer_exec(i)) {
+ pid_t pid = fork();
+ if (pid < 0) {
+ rsyserr(FLOG, errno, "fork failed");
+ io_printf(f_out, "@ERROR: fork failed\n");
+ return -1;
+ }
+ if (pid) {
+ close(f_in);
+ if (f_out != f_in)
+ close(f_out);
+ set_env_num("RSYNC_PID", (long)pid);
+ if (wait_process(pid, &status, 0) < 0)
+ status = -1;
+ set_env_num("RSYNC_RAW_STATUS", status);
+ if (WIFEXITED(status))
+ status = WEXITSTATUS(status);
+ else
+ status = -1;
+ set_env_num("RSYNC_EXIT_STATUS", status);
+ if (system(lp_postxfer_exec(i)) < 0)
+ status = -1;
+ _exit(status);
+ }
+ }
+ /* For pre-xfer exec, fork a child process to run the indicated
+ * command, though it first waits for the parent process to
+ * send us the user's request via a pipe. */
+ if (*lp_prexfer_exec(i)) {
+ int fds[2];
+ set_env_num("RSYNC_PID", (long)getpid());
+ if (pipe(fds) < 0 || (pre_exec_pid = fork()) < 0) {
+ rsyserr(FLOG, errno, "pre-xfer exec preparation failed");
+ io_printf(f_out, "@ERROR: pre-xfer exec preparation failed\n");
+ return -1;
+ }
+ if (pre_exec_pid == 0) {
+ char buf[BIGPATHBUFLEN];
+ int j, len;
+ close(fds[1]);
+ set_blocking(fds[0]);
+ len = read_arg_from_pipe(fds[0], buf, BIGPATHBUFLEN);
+ if (len <= 0)
+ _exit(1);
+ set_env_str("RSYNC_REQUEST", buf);
+ for (j = 0; ; j++) {
+ len = read_arg_from_pipe(fds[0], buf,
+ BIGPATHBUFLEN);
+ if (len <= 0) {
+ if (!len)
+ break;
+ _exit(1);
+ }
+ if (asprintf(&p, "RSYNC_ARG%d=%s", j, buf) > 0)
+ putenv(p);
+ }
+ close(fds[0]);
+ close(STDIN_FILENO);
+ close(STDOUT_FILENO);
+ status = system(lp_prexfer_exec(i));
+ if (!WIFEXITED(status))
+ _exit(1);
+ _exit(WEXITSTATUS(status));
+ }
+ close(fds[0]);
+ set_blocking(fds[1]);
+ pre_exec_fd = fds[1];
+ }
+ }
+#endif
+
+ if (use_chroot) {
+ /*
+ * XXX: The 'use chroot' flag is a fairly reliable
+ * source of confusion, because it fails under two
+ * important circumstances: running as non-root,
+ * running on Win32 (or possibly others). On the
+ * other hand, if you are running as root, then it
+ * might be better to always use chroot.
+ *
+ * So, perhaps if we can't chroot we should just issue
+ * a warning, unless a "require chroot" flag is set,
+ * in which case we fail.
+ */
+ if (chroot(module_chdir)) {
+ rsyserr(FLOG, errno, "chroot %s failed", module_chdir);
+ io_printf(f_out, "@ERROR: chroot failed\n");