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 select_timeout;
45 extern int orig_umask;
47 extern int default_af_hint;
48 extern char *bind_address;
49 extern struct filter_list_struct server_filter_list;
50 extern char *config_file;
51 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;
63 * Run a client connected to an rsyncd. The alternative to this
64 * function for remote-shell connections is do_cmd().
66 * After negotiating which module to use and reading the server's
67 * motd, this hands over to client_run(). Telling the server the
68 * module will cause it to chroot/setuid/etc.
70 * Instead of doing a transfer, the client may at this stage instead
71 * get a listing of remote modules and exit.
73 * @return -1 for error in startup, or the result of client_run().
74 * Either way, it eventually gets passed to exit_cleanup().
76 int start_socket_client(char *host, char *path, int argc, char *argv[])
79 char *p, *user = NULL;
81 /* This is redundant with code in start_inband_exchange(), but this
82 * short-circuits a problem in the client before we open a socket,
83 * and the extra check won't hurt. */
86 "ERROR: The remote path must start with a module name not a /\n");
90 if ((p = strchr(host, '@')) != NULL) {
97 rsync_port = RSYNC_PORT;
99 fd = open_socket_out_wrapped(host, rsync_port, bind_address,
102 exit_cleanup(RERR_SOCKETIO);
104 ret = start_inband_exchange(user, path, fd, fd, argc);
106 return ret < 0? ret : client_run(fd, fd, -1, argc, argv);
109 int start_inband_exchange(char *user, char *path, int f_in, int f_out,
113 char *sargs[MAX_ARGS];
115 char line[MAXPATHLEN];
118 if (argc == 0 && !am_sender)
123 "ERROR: The remote path must start with a module name\n");
128 user = getenv("USER");
130 user = getenv("LOGNAME");
132 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
134 if (!read_line(f_in, line, sizeof line - 1)) {
135 rprintf(FERROR, "rsync: did not see server greeting\n");
139 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
140 /* note that read_line strips of \n or \r */
141 rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n",
145 if (protocol_version > remote_protocol)
146 protocol_version = remote_protocol;
148 if (list_only && protocol_version >= 29)
151 /* set daemon_over_rsh to false since we need to build the
152 * true set of args passed through the rsh/ssh connection;
153 * this is a no-op for direct-socket-connection mode */
155 server_options(sargs, &sargc);
157 sargs[sargc++] = ".";
160 sargs[sargc++] = path;
165 print_child_argv(sargs);
167 p = strchr(path,'/');
169 io_printf(f_out, "%s\n", path);
172 /* Old servers may just drop the connection here,
173 rather than sending a proper EXIT command. Yuck. */
174 kluge_around_eof = list_only && protocol_version < 25 ? 1 : 0;
177 if (!read_line(f_in, line, sizeof line - 1)) {
178 rprintf(FERROR, "rsync: didn't get server startup line\n");
182 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
183 auth_client(f_out, user, line+18);
187 if (strcmp(line,"@RSYNCD: OK") == 0)
190 if (strcmp(line,"@RSYNCD: EXIT") == 0) {
191 /* This is sent by recent versions of the
192 * server to terminate the listing of modules.
193 * We don't want to go on and transfer
194 * anything; just exit. */
198 if (strncmp(line, "@ERROR", 6) == 0) {
199 rprintf(FERROR, "%s\n", line);
200 /* This is always fatal; the server will now
201 * close the socket. */
202 return RERR_STARTCLIENT;
204 rprintf(FINFO,"%s\n", line);
207 kluge_around_eof = 0;
209 for (i = 0; i < sargc; i++) {
210 io_printf(f_out, "%s\n", sargs[i]);
212 io_printf(f_out, "\n");
214 if (protocol_version < 23) {
215 if (protocol_version == 22 || !am_sender)
216 io_start_multiplex_in();
224 static int rsync_module(int f_in, int f_out, int i)
230 char line[MAXPATHLEN];
231 uid_t uid = (uid_t)-2; /* canonically "nobody" */
232 gid_t gid = (gid_t)-2;
234 char *addr = client_addr(f_in);
235 char *host = client_name(f_in);
236 char *name = lp_name(i);
237 int use_chroot = lp_use_chroot(i);
240 char *request = NULL;
242 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
243 rprintf(FLOG, "rsync denied on module %s from %s (%s)\n",
246 io_printf(f_out, "@ERROR: Unknown module '%s'\n", name);
249 "@ERROR: access denied to %s from %s (%s)\n",
255 if (am_daemon && am_server) {
256 rprintf(FLOG, "rsync allowed access on module %s from %s (%s)\n",
260 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
262 rsyserr(FLOG, errno, "failed to open lock file %s",
263 safe_fname(lp_lock_file(i)));
264 io_printf(f_out, "@ERROR: failed to open lock file\n");
266 rprintf(FLOG, "max connections (%d) reached\n",
267 lp_max_connections(i));
268 io_printf(f_out, "@ERROR: max connections (%d) reached -- try again later\n",
269 lp_max_connections(i));
274 auth_user = auth_server(f_in, f_out, i, addr, "@RSYNCD: AUTHREQD ");
277 rprintf(FLOG, "auth failed on module %s from %s (%s)\n",
279 io_printf(f_out, "@ERROR: auth failed on module %s\n", name);
288 if (lp_transfer_logging(i)) {
289 if (strstr(lp_log_format(i), "%i") != NULL)
290 daemon_log_format_has_i = 1;
291 if (daemon_log_format_has_i
292 || strstr(lp_log_format(i), "%o") != NULL)
293 daemon_log_format_has_o_or_i = 1;
296 am_root = (MY_UID() == 0);
300 if (!name_to_uid(p, &uid)) {
301 if (!isdigit(*(unsigned char *)p)) {
302 rprintf(FLOG, "Invalid uid %s\n", p);
303 io_printf(f_out, "@ERROR: invalid uid %s\n", p);
310 if (!name_to_gid(p, &gid)) {
311 if (!isdigit(*(unsigned char *)p)) {
312 rprintf(FLOG, "Invalid gid %s\n", p);
313 io_printf(f_out, "@ERROR: invalid gid %s\n", p);
320 /* TODO: If we're not root, but the configuration requests
321 * that we change to some uid other than the current one, then
324 /* TODO: Perhaps take a list of gids, and make them into the
325 * supplementary groups. */
329 set_filter_dir("/", 1);
331 module_dirlen = strlen(lp_path(i));
332 set_filter_dir(lp_path(i), module_dirlen);
336 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
339 p = lp_include_from(i);
340 parse_filter_file(&server_filter_list, p, MATCHFLG_INCLUDE,
341 XFLG_ANCHORED2ABS | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
344 parse_rule(&server_filter_list, p,
345 MATCHFLG_INCLUDE | MATCHFLG_WORD_SPLIT,
346 XFLG_ANCHORED2ABS | XFLG_OLD_PREFIXES);
348 p = lp_exclude_from(i);
349 parse_filter_file(&server_filter_list, p, 0,
350 XFLG_ANCHORED2ABS | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
353 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
354 XFLG_ANCHORED2ABS | XFLG_OLD_PREFIXES);
360 * XXX: The 'use chroot' flag is a fairly reliable
361 * source of confusion, because it fails under two
362 * important circumstances: running as non-root,
363 * running on Win32 (or possibly others). On the
364 * other hand, if you are running as root, then it
365 * might be better to always use chroot.
367 * So, perhaps if we can't chroot we should just issue
368 * a warning, unless a "require chroot" flag is set,
369 * in which case we fail.
371 if (chroot(lp_path(i))) {
372 rsyserr(FLOG, errno, "chroot %s failed",
373 safe_fname(lp_path(i)));
374 io_printf(f_out, "@ERROR: chroot failed\n");
378 if (!push_dir("/")) {
379 rsyserr(FLOG, errno, "chdir %s failed\n",
380 safe_fname(lp_path(i)));
381 io_printf(f_out, "@ERROR: chdir failed\n");
386 if (!push_dir(lp_path(i))) {
387 rsyserr(FLOG, errno, "chdir %s failed\n",
388 safe_fname(lp_path(i)));
389 io_printf(f_out, "@ERROR: chdir failed\n");
396 /* XXXX: You could argue that if the daemon is started
397 * by a non-root user and they explicitly specify a
398 * gid, then we should try to change to that gid --
399 * this could be possible if it's already in their
400 * supplementary groups. */
402 /* TODO: Perhaps we need to document that if rsyncd is
403 * started by somebody other than root it will inherit
404 * all their supplementary groups. */
407 rsyserr(FLOG, errno, "setgid %d failed", (int)gid);
408 io_printf(f_out, "@ERROR: setgid failed\n");
411 #ifdef HAVE_SETGROUPS
412 /* Get rid of any supplementary groups this process
413 * might have inheristed. */
414 if (setgroups(1, &gid)) {
415 rsyserr(FLOG, errno, "setgroups failed");
416 io_printf(f_out, "@ERROR: setgroups failed\n");
422 rsyserr(FLOG, errno, "setuid %d failed", (int)uid);
423 io_printf(f_out, "@ERROR: setuid failed\n");
427 am_root = (MY_UID() == 0);
430 io_printf(f_out, "@RSYNCD: OK\n");
433 if (!(argv = new_array(char *, maxargs)))
434 out_of_memory("rsync_module");
435 argv[argc++] = "rsyncd";
438 if (!read_line(f_in, line, sizeof line - 1))
446 if (argc == maxargs) {
448 if (!(argv = realloc_array(argv, char *, maxargs)))
449 out_of_memory("rsync_module");
451 if (!(argv[argc] = strdup(p)))
452 out_of_memory("rsync_module");
455 if (start_glob == 1) {
459 glob_expand(name, &argv, &argc, &maxargs);
463 if (strcmp(line, ".") == 0)
467 verbose = 0; /* future verbosity is controlled by client options */
469 ret = parse_arguments(&argc, (const char ***) &argp, 0);
471 if (filesfrom_fd == 0)
476 rprintf(FLOG, "rsync %s %s from %s@%s (%s)\n",
477 am_sender ? "on" : "to",
478 request, auth_user, host, addr);
480 rprintf(FLOG, "rsync %s %s from %s (%s)\n",
481 am_sender ? "on" : "to",
482 request, host, addr);
488 /* don't allow the logs to be flooded too fast */
489 if (verbose > lp_max_verbosity())
490 verbose = lp_max_verbosity();
493 if (protocol_version < 23
494 && (protocol_version == 22 || am_sender))
495 io_start_multiplex_out();
497 /* We have to get I/O multiplexing started so that we can
498 * get the error back to the client. This means getting
499 * the protocol setup finished first in later versions. */
500 setup_protocol(f_out, f_in);
501 if (files_from && !am_sender && strcmp(files_from, "-") != 0)
502 write_byte(f_out, 0);
503 io_start_multiplex_out();
509 exit_cleanup(RERR_UNSUPPORTED);
513 io_timeout = lp_timeout(i);
514 if (io_timeout < select_timeout)
515 select_timeout = io_timeout;
518 start_server(f_in, f_out, argc, argp);
523 /* send a list of available modules to the client. Don't list those
524 with "list = False". */
525 static void send_listing(int fd)
527 int n = lp_numservices();
530 for (i = 0; i < n; i++) {
532 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
535 if (protocol_version >= 25)
536 io_printf(fd,"@RSYNCD: EXIT\n");
539 /* this is called when a connection is established to a client
540 and we want to start talking. The setup of the system is done from
542 int start_daemon(int f_in, int f_out)
548 io_set_sock_fds(f_in, f_out);
550 if (!lp_load(config_file, 0))
551 exit_cleanup(RERR_SYNTAX);
556 set_socket_options(f_in, "SO_KEEPALIVE");
557 set_socket_options(f_in, lp_socket_options());
558 set_nonblocking(f_in);
561 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
563 motd = lp_motd_file();
565 FILE *f = fopen(motd,"r");
566 while (f && !feof(f)) {
567 int len = fread(line, 1, sizeof line - 1, f);
570 io_printf(f_out, "%s", line);
575 io_printf(f_out, "\n");
578 if (!read_line(f_in, line, sizeof line - 1))
581 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
582 io_printf(f_out, "@ERROR: protocol startup error\n");
585 if (protocol_version > remote_protocol)
586 protocol_version = remote_protocol;
589 if (!read_line(f_in, line, sizeof line - 1))
592 if (!*line || strcmp(line, "#list") == 0) {
598 /* it's some sort of command that I don't understand */
599 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
603 if ((i = lp_number(line)) < 0) {
604 char *addr = client_addr(f_in);
605 char *host = client_name(f_in);
606 rprintf(FLOG, "unknown module '%s' tried from %s (%s)\n",
608 io_printf(f_out, "@ERROR: Unknown module '%s'\n", line);
612 return rsync_module(f_in, f_out, i);
616 int daemon_main(void)
620 if (is_a_socket(STDIN_FILENO)) {
623 /* we are running via inetd - close off stdout and
624 * stderr so that library functions (and getopt) don't
625 * try to use them. Redirect them to /dev/null */
626 for (i = 1; i < 3; i++) {
628 open("/dev/null", O_RDWR);
631 return start_daemon(STDIN_FILENO, STDIN_FILENO);
637 if (!lp_load(config_file, 1))
638 exit_cleanup(RERR_SYNTAX);
640 if (rsync_port == 0 && (rsync_port = lp_rsync_port()) == 0)
641 rsync_port = RSYNC_PORT;
642 if (bind_address == NULL && *lp_bind_address())
643 bind_address = lp_bind_address();
647 rprintf(FLOG, "rsyncd version %s starting, listening on port %d\n",
648 RSYNC_VERSION, rsync_port);
649 /* TODO: If listening on a particular address, then show that
650 * address too. In fact, why not just do inet_ntop on the
651 * local address??? */
653 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
656 pid_t pid = getpid();
657 cleanup_set_pid(pid);
658 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
659 0666 & ~orig_umask)) == -1) {
661 rsyserr(FLOG, errno, "failed to create pid file %s",
662 safe_fname(pid_file));
663 exit_cleanup(RERR_FILEIO);
665 snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
666 write(fd, pidbuf, strlen(pidbuf));
670 start_accept_loop(rsync_port, start_daemon);