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
33 extern int rsync_port;
35 extern int sanitize_paths;
36 extern int filesfrom_fd;
37 extern struct exclude_struct **server_exclude_list;
38 extern char *exclude_path_prefix;
41 * Run a client connected to an rsyncd. The alternative to this
42 * function for remote-shell connections is do_cmd().
44 * After negotiating which module to use and reading the server's
45 * motd, this hands over to client_run(). Telling the server the
46 * module will cause it to chroot/setuid/etc.
48 * Instead of doing a transfer, the client may at this stage instead
49 * get a listing of remote modules and exit.
51 * @return -1 for error in startup, or the result of client_run().
52 * Either way, it eventually gets passed to exit_cleanup().
54 int start_socket_client(char *host, char *path, int argc, char *argv[])
58 extern char *bind_address;
59 extern int default_af_hint;
61 /* this is redundant with code in start_inband_exchange(), but
62 * this short-circuits a problem before we open a socket, and
63 * the extra check won't hurt */
65 rprintf(FERROR,"ERROR: The remote path must start with a module name not a /\n");
69 p = strchr(host, '@');
77 /* FIXME: If we're going to use a socket program for
78 * testing, then this message is wrong. We need to
79 * say something like "(except really using %s)" */
80 rprintf(FINFO, "opening tcp connection to %s port %d\n",
83 fd = open_socket_out_wrapped(host, rsync_port, bind_address,
86 exit_cleanup(RERR_SOCKETIO);
89 ret = start_inband_exchange(user, path, fd, fd, argc);
91 return ret < 0? ret : client_run(fd, fd, -1, argc, argv);
94 int start_inband_exchange(char *user, char *path, int f_in, int f_out, int argc)
97 char *sargs[MAX_ARGS];
99 char line[MAXPATHLEN];
101 extern int remote_version;
102 extern int kludge_around_eof;
103 extern int am_sender;
104 extern int daemon_over_rsh;
105 extern int list_only;
107 if (argc == 0 && !am_sender)
111 rprintf(FERROR, "ERROR: The remote path must start with a module name\n");
115 if (!user) user = getenv("USER");
116 if (!user) user = getenv("LOGNAME");
118 /* set daemon_over_rsh to false since we need to build the
119 * true set of args passed through the rsh/ssh connection;
120 * this is a no-op for direct-socket-connection mode */
122 server_options(sargs, &sargc);
124 sargs[sargc++] = ".";
127 sargs[sargc++] = path;
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_version) != 1) {
139 /* note that read_line strips of \n or \r */
140 rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n",
145 p = strchr(path,'/');
147 io_printf(f_out, "%s\n", path);
150 /* Old servers may just drop the connection here,
151 rather than sending a proper EXIT command. Yuck. */
152 kludge_around_eof = list_only && (remote_version < 25);
155 if (!read_line(f_in, line, sizeof(line)-1)) {
156 rprintf(FERROR, "rsync: didn't get server startup line\n");
160 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
161 auth_client(f_out, user, line+18);
165 if (strcmp(line,"@RSYNCD: OK") == 0) break;
167 if (strcmp(line,"@RSYNCD: EXIT") == 0) {
168 /* This is sent by recent versions of the
169 * server to terminate the listing of modules.
170 * We don't want to go on and transfer
171 * anything; just exit. */
175 if (strncmp(line, "@ERROR", 6) == 0) {
176 rprintf(FERROR,"%s\n", line);
177 /* This is always fatal; the server will now
178 * close the socket. */
179 return RERR_STARTCLIENT;
181 rprintf(FINFO,"%s\n", line);
184 kludge_around_eof = False;
186 for (i = 0; i < sargc; i++) {
187 io_printf(f_out, "%s\n", sargs[i]);
189 io_printf(f_out, "\n");
191 if (remote_version < 23) {
192 if (remote_version == 22 || (remote_version > 17 && !am_sender))
193 io_start_multiplex_in(f_in);
201 static int rsync_module(int f_in, int f_out, int i)
204 char *argv[MAX_ARGS];
206 char line[MAXPATHLEN];
207 uid_t uid = (uid_t)-2; /* canonically "nobody" */
208 gid_t gid = (gid_t)-2;
210 char *addr = client_addr(f_in);
211 char *host = client_name(f_in);
212 char *name = lp_name(i);
213 int use_chroot = lp_use_chroot(i);
217 extern int am_sender;
218 extern int am_server;
219 extern int am_daemon;
220 extern int remote_version;
223 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
224 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
226 io_printf(f_out, "@ERROR: access denied to %s from %s (%s)\n",
231 if (am_daemon && am_server) {
232 rprintf(FINFO, "rsync allowed access on module %s from %s (%s)\n",
236 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
238 rprintf(FERROR,"failed to open lock file %s : %s\n",
239 lp_lock_file(i), strerror(errno));
240 io_printf(f_out, "@ERROR: failed to open lock file %s : %s\n",
241 lp_lock_file(i), strerror(errno));
243 rprintf(FERROR,"max connections (%d) reached\n",
244 lp_max_connections(i));
245 io_printf(f_out, "@ERROR: max connections (%d) reached - try again later\n",
246 lp_max_connections(i));
252 auth_user = auth_server(f_in, f_out, i, addr, "@RSYNCD: AUTHREQD ");
255 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
257 io_printf(f_out, "@ERROR: auth failed on module %s\n", name);
263 am_root = (getuid() == 0);
267 if (!name_to_uid(p, &uid)) {
268 if (!isdigit(* (unsigned char *) p)) {
269 rprintf(FERROR,"Invalid uid %s\n", p);
270 io_printf(f_out, "@ERROR: invalid uid %s\n", p);
277 if (!name_to_gid(p, &gid)) {
278 if (!isdigit(* (unsigned char *) p)) {
279 rprintf(FERROR,"Invalid gid %s\n", p);
280 io_printf(f_out, "@ERROR: invalid gid %s\n", p);
287 /* TODO: If we're not root, but the configuration requests
288 * that we change to some uid other than the current one, then
291 /* TODO: Perhaps take a list of gids, and make them into the
292 * supplementary groups. */
294 exclude_path_prefix = use_chroot? "" : lp_path(i);
296 p = lp_include_from(i);
297 add_exclude_file(&server_exclude_list, p, MISSING_FATAL, ADD_INCLUDE);
300 add_exclude_line(&server_exclude_list, p, ADD_INCLUDE);
302 p = lp_exclude_from(i);
303 add_exclude_file(&server_exclude_list, p, MISSING_FATAL, ADD_EXCLUDE);
306 add_exclude_line(&server_exclude_list, p, ADD_EXCLUDE);
308 exclude_path_prefix = NULL;
314 * XXX: The 'use chroot' flag is a fairly reliable
315 * source of confusion, because it fails under two
316 * important circumstances: running as non-root,
317 * running on Win32 (or possibly others). On the
318 * other hand, if you are running as root, then it
319 * might be better to always use chroot.
321 * So, perhaps if we can't chroot we should just issue
322 * a warning, unless a "require chroot" flag is set,
323 * in which case we fail.
325 if (chroot(lp_path(i))) {
326 rsyserr(FERROR, errno, "chroot %s failed", lp_path(i));
327 io_printf(f_out, "@ERROR: chroot failed\n");
331 if (!push_dir("/", 0)) {
332 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
333 io_printf(f_out, "@ERROR: chdir failed\n");
338 if (!push_dir(lp_path(i), 0)) {
339 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
340 io_printf(f_out, "@ERROR: chdir failed\n");
347 /* XXXX: You could argue that if the daemon is started
348 * by a non-root user and they explicitly specify a
349 * gid, then we should try to change to that gid --
350 * this could be possible if it's already in their
351 * supplementary groups. */
353 /* TODO: Perhaps we need to document that if rsyncd is
354 * started by somebody other than root it will inherit
355 * all their supplementary groups. */
358 rsyserr(FERROR, errno, "setgid %d failed", (int) gid);
359 io_printf(f_out, "@ERROR: setgid failed\n");
362 #ifdef HAVE_SETGROUPS
363 /* Get rid of any supplementary groups this process
364 * might have inheristed. */
365 if (setgroups(1, &gid)) {
366 rsyserr(FERROR, errno, "setgroups failed");
367 io_printf(f_out, "@ERROR: setgroups failed\n");
373 rsyserr(FERROR, errno, "setuid %d failed", (int) uid);
374 io_printf(f_out, "@ERROR: setuid failed\n");
378 am_root = (getuid() == 0);
381 io_printf(f_out, "@RSYNCD: OK\n");
383 argv[argc++] = "rsyncd";
386 if (!read_line(f_in, line, sizeof(line)-1)) {
394 argv[argc] = strdup(p);
400 if (start_glob == 1) {
404 glob_expand(name, argv, &argc, MAX_ARGS);
409 if (strcmp(line,".") == 0) {
413 if (argc == MAX_ARGS) {
418 if (sanitize_paths) {
420 * Note that this is applied to all parameters, whether or not
421 * they are filenames, but no other legal parameters contain
422 * the forms that need to be sanitized so it doesn't hurt;
423 * it is not known at this point which parameters are files
426 for (i = 1; i < argc; i++) {
427 sanitize_path(argv[i], NULL);
432 ret = parse_arguments(&argc, (const char ***) &argp, 0);
434 if (filesfrom_fd == 0)
439 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
441 request, auth_user, host, addr);
443 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
445 request, host, addr);
451 /* don't allow the logs to be flooded too fast */
452 if (verbose > 1) verbose = 1;
455 if (remote_version < 23) {
456 if (remote_version == 22 || (remote_version > 17 && am_sender))
457 io_start_multiplex_out(f_out);
460 /* For later protocol versions, we don't start multiplexing
461 * until we've configured nonblocking in start_server. That
462 * means we're in a sticky situation now: there's no way to
463 * convey errors to the client. */
465 /* FIXME: Hold off on reporting option processing errors until
466 * we've set up nonblocking and multiplexed IO and can get the
467 * message back to them. */
470 exit_cleanup(RERR_UNSUPPORTED);
474 extern int io_timeout;
475 io_timeout = lp_timeout(i);
478 start_server(f_in, f_out, argc, argp);
483 /* send a list of available modules to the client. Don't list those
484 with "list = False". */
485 static void send_listing(int fd)
487 int n = lp_numservices();
489 extern int remote_version;
493 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
495 if (remote_version >= 25)
496 io_printf(fd,"@RSYNCD: EXIT\n");
499 /* this is called when a connection is established to a client
500 and we want to start talking. The setup of the system is done from
502 int start_daemon(int f_in, int f_out)
507 extern char *config_file;
508 extern int remote_version;
509 extern int am_server;
511 if (!lp_load(config_file, 0)) {
512 exit_cleanup(RERR_SYNTAX);
518 set_socket_options(f_in, "SO_KEEPALIVE");
519 set_socket_options(f_in, lp_socket_options());
520 set_nonblocking(f_in);
523 io_printf(f_out, "@RSYNCD: %d\n", PROTOCOL_VERSION);
525 motd = lp_motd_file();
527 FILE *f = fopen(motd,"r");
528 while (f && !feof(f)) {
529 int len = fread(line, 1, sizeof(line)-1, f);
532 io_printf(f_out, "%s", line);
536 io_printf(f_out, "\n");
539 if (!read_line(f_in, line, sizeof(line)-1)) {
543 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
544 io_printf(f_out, "@ERROR: protocol startup error\n");
550 if (!read_line(f_in, line, sizeof(line)-1)) {
554 if (!*line || strcmp(line,"#list")==0) {
560 /* it's some sort of command that I don't understand */
561 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
567 io_printf(f_out, "@ERROR: Unknown module '%s'\n", line);
572 return rsync_module(f_in, f_out, i);
576 int daemon_main(void)
578 extern char *config_file;
579 extern int orig_umask;
581 extern int no_detach;
583 if (is_a_socket(STDIN_FILENO)) {
586 /* we are running via inetd - close off stdout and
587 * stderr so that library functions (and getopt) don't
588 * try to use them. Redirect them to /dev/null */
591 open("/dev/null", O_RDWR);
594 return start_daemon(STDIN_FILENO, STDIN_FILENO);
600 if (!lp_load(config_file, 1)) {
601 exit_cleanup(RERR_SYNTAX);
606 rprintf(FINFO, "rsyncd version %s starting, listening on port %d\n",
607 RSYNC_VERSION, rsync_port);
608 /* TODO: If listening on a particular address, then show that
609 * address too. In fact, why not just do inet_ntop on the
610 * local address??? */
612 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
615 int pid = (int) getpid();
616 cleanup_set_pid(pid);
617 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
618 0666 & ~orig_umask)) == -1) {
620 rsyserr(FLOG, errno, "failed to create pid file %s", pid_file);
621 exit_cleanup(RERR_FILEIO);
623 snprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
624 write(fd, pidbuf, strlen(pidbuf));
628 start_accept_loop(rsync_port, start_daemon);