1 /* -*- c-file-style: "linux"; -*-
3 * Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4 * Copyright (C) 2001-2002 by Martin Pool <mbp@samba.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * The socket based protocol for setting up a connection with
36 extern int rsync_port;
37 extern int kluge_around_eof;
38 extern int daemon_over_rsh;
39 extern int sanitize_paths;
40 extern int filesfrom_fd;
41 extern int remote_protocol;
42 extern int protocol_version;
43 extern int io_timeout;
44 extern int orig_umask;
46 extern int default_af_hint;
47 extern char *bind_address;
48 extern struct filter_list_struct server_filter_list;
49 extern char *config_file;
50 extern char *files_from;
55 int daemon_log_format_has_i = 0;
56 int daemon_log_format_has_o_or_i = 0;
59 /* Length of lp_path() string when in daemon mode & not chrooted, else 0. */
60 unsigned int module_dirlen = 0;
62 #define MAX_REQ_LEN (MAXPATHLEN + 255)
65 * Run a client connected to an rsyncd. The alternative to this
66 * function for remote-shell connections is do_cmd().
68 * After negotiating which module to use and reading the server's
69 * motd, this hands over to client_run(). Telling the server the
70 * module will cause it to chroot/setuid/etc.
72 * Instead of doing a transfer, the client may at this stage instead
73 * get a listing of remote modules and exit.
75 * @return -1 for error in startup, or the result of client_run().
76 * Either way, it eventually gets passed to exit_cleanup().
78 int start_socket_client(char *host, char *path, int argc, char *argv[])
81 char *p, *user = NULL;
83 /* This is redundant with code in start_inband_exchange(), but this
84 * short-circuits a problem in the client before we open a socket,
85 * and the extra check won't hurt. */
88 "ERROR: The remote path must start with a module name not a /\n");
92 if ((p = strrchr(host, '@')) != NULL) {
98 fd = open_socket_out_wrapped(host, rsync_port, bind_address,
101 exit_cleanup(RERR_SOCKETIO);
103 ret = start_inband_exchange(user, path, fd, fd, argc);
105 return ret ? ret : client_run(fd, fd, -1, argc, argv);
108 int start_inband_exchange(char *user, char *path, int f_in, int f_out,
112 char *sargs[MAX_ARGS];
114 char line[BIGPATHBUFLEN];
117 if (argc == 0 && !am_sender)
122 "ERROR: The remote path must start with a module name\n");
127 user = getenv("USER");
129 user = getenv("LOGNAME");
131 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
133 if (!read_line(f_in, line, sizeof line - 1)) {
134 rprintf(FERROR, "rsync: did not see server greeting\n");
138 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
139 /* note that read_line strips of \n or \r */
140 rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n",
144 if (protocol_version > remote_protocol)
145 protocol_version = remote_protocol;
147 if (list_only && protocol_version >= 29)
150 /* set daemon_over_rsh to false since we need to build the
151 * true set of args passed through the rsh/ssh connection;
152 * this is a no-op for direct-socket-connection mode */
154 server_options(sargs, &sargc);
156 sargs[sargc++] = ".";
159 sargs[sargc++] = path;
164 print_child_argv(sargs);
166 p = strchr(path,'/');
168 io_printf(f_out, "%s\n", path);
171 /* Old servers may just drop the connection here,
172 rather than sending a proper EXIT command. Yuck. */
173 kluge_around_eof = list_only && protocol_version < 25 ? 1 : 0;
176 if (!read_line(f_in, line, sizeof line - 1)) {
177 rprintf(FERROR, "rsync: didn't get server startup line\n");
181 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
182 auth_client(f_out, user, line+18);
186 if (strcmp(line,"@RSYNCD: OK") == 0)
189 if (strcmp(line,"@RSYNCD: EXIT") == 0) {
190 /* This is sent by recent versions of the
191 * server to terminate the listing of modules.
192 * We don't want to go on and transfer
193 * anything; just exit. */
197 if (strncmp(line, "@ERROR", 6) == 0) {
198 rprintf(FERROR, "%s\n", line);
199 /* This is always fatal; the server will now
200 * close the socket. */
204 rprintf(FINFO, "%s\n", line);
206 kluge_around_eof = 0;
208 for (i = 0; i < sargc; i++) {
209 io_printf(f_out, "%s\n", sargs[i]);
211 io_printf(f_out, "\n");
213 if (protocol_version < 23) {
214 if (protocol_version == 22 || !am_sender)
215 io_start_multiplex_in();
221 static char *finish_pre_exec(pid_t pid, int fd, char *request)
226 int len = strlen(request);
227 if (len > MAX_REQ_LEN)
229 write(fd, request, len);
233 if (wait_process(pid, &status, 0) < 0
234 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
236 if (asprintf(&e, "pre-xfer exec returned failure (%d)\n", status) < 0)
237 out_of_memory("finish_pre_exec");
243 static int rsync_module(int f_in, int f_out, int i)
248 char line[BIGPATHBUFLEN];
249 uid_t uid = (uid_t)-2; /* canonically "nobody" */
250 gid_t gid = (gid_t)-2;
251 char *p, *err_msg = NULL;
252 char *addr = client_addr(f_in);
253 char *host = client_name(f_in);
254 char *name = lp_name(i);
255 int use_chroot = lp_use_chroot(i);
257 int ret, pre_exec_fd = -1;
258 pid_t pre_exec_pid = 0;
259 char *request = NULL;
261 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
262 rprintf(FLOG, "rsync denied on module %s from %s (%s)\n",
265 io_printf(f_out, "@ERROR: Unknown module '%s'\n", name);
268 "@ERROR: access denied to %s from %s (%s)\n",
274 if (am_daemon && am_server) {
275 rprintf(FLOG, "rsync allowed access on module %s from %s (%s)\n",
279 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
281 rsyserr(FLOG, errno, "failed to open lock file %s",
282 safe_fname(lp_lock_file(i)));
283 io_printf(f_out, "@ERROR: failed to open lock file\n");
285 rprintf(FLOG, "max connections (%d) reached\n",
286 lp_max_connections(i));
287 io_printf(f_out, "@ERROR: max connections (%d) reached -- try again later\n",
288 lp_max_connections(i));
293 auth_user = auth_server(f_in, f_out, i, host, addr, "@RSYNCD: AUTHREQD ");
296 io_printf(f_out, "@ERROR: auth failed on module %s\n", name);
305 if (lp_transfer_logging(i)) {
306 if (log_format_has(lp_log_format(i), 'i'))
307 daemon_log_format_has_i = 1;
308 if (daemon_log_format_has_i
309 || log_format_has(lp_log_format(i), 'o'))
310 daemon_log_format_has_o_or_i = 1;
313 am_root = (MY_UID() == 0);
317 if (!name_to_uid(p, &uid)) {
318 if (!isdigit(*(unsigned char *)p)) {
319 rprintf(FLOG, "Invalid uid %s\n", p);
320 io_printf(f_out, "@ERROR: invalid uid %s\n", p);
327 if (!name_to_gid(p, &gid)) {
328 if (!isdigit(*(unsigned char *)p)) {
329 rprintf(FLOG, "Invalid gid %s\n", p);
330 io_printf(f_out, "@ERROR: invalid gid %s\n", p);
337 /* TODO: If we're not root, but the configuration requests
338 * that we change to some uid other than the current one, then
341 /* TODO: Perhaps take a list of gids, and make them into the
342 * supplementary groups. */
344 if (use_chroot || (module_dirlen = strlen(lp_path(i))) == 1) {
346 set_filter_dir("/", 1);
348 set_filter_dir(lp_path(i), module_dirlen);
351 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
354 p = lp_include_from(i);
355 parse_filter_file(&server_filter_list, p, MATCHFLG_INCLUDE,
356 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
359 parse_rule(&server_filter_list, p,
360 MATCHFLG_INCLUDE | MATCHFLG_WORD_SPLIT,
361 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES);
363 p = lp_exclude_from(i);
364 parse_filter_file(&server_filter_list, p, 0,
365 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
368 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
369 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES);
374 if (*lp_prexfer_exec(i) || *lp_postxfer_exec(i)) {
375 char *modname, *modpath, *hostaddr, *hostname, *username;
377 if (asprintf(&modname, "RSYNC_MODULE_NAME=%s", name) < 0
378 || asprintf(&modpath, "RSYNC_MODULE_PATH=%s", lp_path(i)) < 0
379 || asprintf(&hostaddr, "RSYNC_HOST_ADDR=%s", addr) < 0
380 || asprintf(&hostname, "RSYNC_HOST_NAME=%s", host) < 0
381 || asprintf(&username, "RSYNC_USER_NAME=%s", auth_user) < 0)
382 out_of_memory("rsync_module");
389 /* For post-xfer exec, fork a new process to run the rsync
390 * daemon while this process waits for the exit status and
391 * runs the indicated command at that point. */
392 if (*lp_postxfer_exec(i)) {
395 rsyserr(FLOG, errno, "fork failed");
396 io_printf(f_out, "@ERROR: fork failed\n");
401 if (wait_process(pid, &status, 0) < 0)
403 if (asprintf(&ret1, "RSYNC_RAW_STATUS=%d", status) > 0)
405 if (WIFEXITED(status))
406 status = WEXITSTATUS(status);
409 if (asprintf(&ret2, "RSYNC_EXIT_STATUS=%d", status) > 0)
411 system(lp_postxfer_exec(i));
415 /* For pre-xfer exec, fork a child process to run the indicated
416 * command, though it first waits for the parent process to
417 * send us the user's request via a pipe. */
418 if (*lp_prexfer_exec(i)) {
420 if (pipe(fds) < 0 || (pre_exec_pid = fork()) < 0) {
421 rsyserr(FLOG, errno, "pre-xfer exec preparation failed");
422 io_printf(f_out, "@ERROR: pre-xfer exec preparation failed\n");
425 if (pre_exec_pid == 0) {
426 char buf[MAX_REQ_LEN+1];
429 set_blocking(fds[0]);
430 len = read(fds[0], buf, MAX_REQ_LEN);
435 if (asprintf(&p, "RSYNC_REQUEST=%s", buf) < 0)
436 out_of_memory("rsync_module");
439 close(STDOUT_FILENO);
440 status = system(lp_prexfer_exec(i));
441 if (!WIFEXITED(status))
443 _exit(WEXITSTATUS(status));
446 set_blocking(fds[1]);
447 pre_exec_fd = fds[1];
455 * XXX: The 'use chroot' flag is a fairly reliable
456 * source of confusion, because it fails under two
457 * important circumstances: running as non-root,
458 * running on Win32 (or possibly others). On the
459 * other hand, if you are running as root, then it
460 * might be better to always use chroot.
462 * So, perhaps if we can't chroot we should just issue
463 * a warning, unless a "require chroot" flag is set,
464 * in which case we fail.
466 if (chroot(lp_path(i))) {
467 rsyserr(FLOG, errno, "chroot %s failed",
468 safe_fname(lp_path(i)));
469 io_printf(f_out, "@ERROR: chroot failed\n");
473 if (!push_dir("/")) {
474 rsyserr(FLOG, errno, "chdir %s failed\n",
475 safe_fname(lp_path(i)));
476 io_printf(f_out, "@ERROR: chdir failed\n");
481 if (!push_dir(lp_path(i))) {
482 rsyserr(FLOG, errno, "chdir %s failed\n",
483 safe_fname(lp_path(i)));
484 io_printf(f_out, "@ERROR: chdir failed\n");
491 /* XXXX: You could argue that if the daemon is started
492 * by a non-root user and they explicitly specify a
493 * gid, then we should try to change to that gid --
494 * this could be possible if it's already in their
495 * supplementary groups. */
497 /* TODO: Perhaps we need to document that if rsyncd is
498 * started by somebody other than root it will inherit
499 * all their supplementary groups. */
502 rsyserr(FLOG, errno, "setgid %d failed", (int)gid);
503 io_printf(f_out, "@ERROR: setgid failed\n");
506 #ifdef HAVE_SETGROUPS
507 /* Get rid of any supplementary groups this process
508 * might have inheristed. */
509 if (setgroups(1, &gid)) {
510 rsyserr(FLOG, errno, "setgroups failed");
511 io_printf(f_out, "@ERROR: setgroups failed\n");
517 rsyserr(FLOG, errno, "setuid %d failed", (int)uid);
518 io_printf(f_out, "@ERROR: setuid failed\n");
522 am_root = (MY_UID() == 0);
525 if (lp_temp_dir(i) && *lp_temp_dir(i)) {
526 tmpdir = lp_temp_dir(i);
527 if (strlen(tmpdir) >= MAXPATHLEN - 10) {
529 "the 'temp dir' value for %s is WAY too long -- ignoring.\n",
535 io_printf(f_out, "@RSYNCD: OK\n");
538 if (!(argv = new_array(char *, maxargs)))
539 out_of_memory("rsync_module");
540 argv[argc++] = "rsyncd";
543 if (!read_line(f_in, line, sizeof line - 1))
551 if (argc == maxargs) {
553 if (!(argv = realloc_array(argv, char *, maxargs)))
554 out_of_memory("rsync_module");
556 if (!(argv[argc] = strdup(p)))
557 out_of_memory("rsync_module");
559 switch (start_glob) {
562 if (strcmp(line, ".") == 0)
567 err_msg = finish_pre_exec(pre_exec_pid,
576 glob_expand(name, &argv, &argc, &maxargs);
582 err_msg = finish_pre_exec(pre_exec_pid, pre_exec_fd, request);
584 verbose = 0; /* future verbosity is controlled by client options */
585 ret = parse_arguments(&argc, (const char ***) &argv, 0);
587 if (filesfrom_fd == 0)
592 rprintf(FLOG, "rsync %s %s from %s@%s (%s)\n",
593 am_sender ? "on" : "to",
594 request, auth_user, host, addr);
596 rprintf(FLOG, "rsync %s %s from %s (%s)\n",
597 am_sender ? "on" : "to",
598 request, host, addr);
604 /* don't allow the logs to be flooded too fast */
605 if (verbose > lp_max_verbosity(i))
606 verbose = lp_max_verbosity(i);
609 if (protocol_version < 23
610 && (protocol_version == 22 || am_sender))
611 io_start_multiplex_out();
612 else if (!ret || err_msg) {
613 /* We have to get I/O multiplexing started so that we can
614 * get the error back to the client. This means getting
615 * the protocol setup finished first in later versions. */
616 setup_protocol(f_out, f_in);
618 /* Since we failed in our option parsing, we may not
619 * have finished parsing that the client sent us a
620 * --files-from option, so look for it manually.
621 * Without this, the socket would be in the wrong
622 * state for the upcoming error message. */
625 for (i = 0; i < argc; i++) {
626 if (strncmp(argv[i], "--files-from", 12) == 0) {
633 write_byte(f_out, 0);
635 io_start_multiplex_out();
638 if (!ret || err_msg) {
640 rprintf(FERROR, err_msg);
644 exit_cleanup(RERR_UNSUPPORTED);
647 if (lp_timeout(i) && lp_timeout(i) > io_timeout)
648 set_io_timeout(lp_timeout(i));
650 start_server(f_in, f_out, argc, argv);
655 /* send a list of available modules to the client. Don't list those
656 with "list = False". */
657 static void send_listing(int fd)
659 int n = lp_numservices();
662 for (i = 0; i < n; i++) {
664 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
667 if (protocol_version >= 25)
668 io_printf(fd,"@RSYNCD: EXIT\n");
671 /* this is called when a connection is established to a client
672 and we want to start talking. The setup of the system is done from
674 int start_daemon(int f_in, int f_out)
680 io_set_sock_fds(f_in, f_out);
682 if (!lp_load(config_file, 0))
683 exit_cleanup(RERR_SYNTAX);
688 set_socket_options(f_in, "SO_KEEPALIVE");
689 set_socket_options(f_in, lp_socket_options());
690 set_nonblocking(f_in);
693 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
695 motd = lp_motd_file();
697 FILE *f = fopen(motd,"r");
698 while (f && !feof(f)) {
699 int len = fread(line, 1, sizeof line - 1, f);
702 io_printf(f_out, "%s", line);
707 io_printf(f_out, "\n");
710 if (!read_line(f_in, line, sizeof line - 1))
713 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
714 io_printf(f_out, "@ERROR: protocol startup error\n");
717 if (protocol_version > remote_protocol)
718 protocol_version = remote_protocol;
721 if (!read_line(f_in, line, sizeof line - 1))
724 if (!*line || strcmp(line, "#list") == 0) {
730 /* it's some sort of command that I don't understand */
731 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
735 if ((i = lp_number(line)) < 0) {
736 char *addr = client_addr(f_in);
737 char *host = client_name(f_in);
738 rprintf(FLOG, "unknown module '%s' tried from %s (%s)\n",
740 io_printf(f_out, "@ERROR: Unknown module '%s'\n", line);
744 return rsync_module(f_in, f_out, i);
747 int daemon_main(void)
751 if (is_a_socket(STDIN_FILENO)) {
754 /* we are running via inetd - close off stdout and
755 * stderr so that library functions (and getopt) don't
756 * try to use them. Redirect them to /dev/null */
757 for (i = 1; i < 3; i++) {
759 open("/dev/null", O_RDWR);
762 return start_daemon(STDIN_FILENO, STDIN_FILENO);
768 if (!lp_load(config_file, 1))
769 exit_cleanup(RERR_SYNTAX);
771 if (rsync_port == 0 && (rsync_port = lp_rsync_port()) == 0)
772 rsync_port = RSYNC_PORT;
773 if (bind_address == NULL && *lp_bind_address())
774 bind_address = lp_bind_address();
778 rprintf(FLOG, "rsyncd version %s starting, listening on port %d\n",
779 RSYNC_VERSION, rsync_port);
780 /* TODO: If listening on a particular address, then show that
781 * address too. In fact, why not just do inet_ntop on the
782 * local address??? */
784 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
787 pid_t pid = getpid();
788 cleanup_set_pid(pid);
789 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
790 0666 & ~orig_umask)) == -1) {
792 rsyserr(FLOG, errno, "failed to create pid file %s",
793 safe_fname(pid_file));
794 exit_cleanup(RERR_FILEIO);
796 snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
797 write(fd, pidbuf, strlen(pidbuf));
801 start_accept_loop(rsync_port, start_daemon);