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;
39 * Run a client connected to an rsyncd. The alternative to this
40 * function for remote-shell connections is do_cmd().
42 * After negotiating which module to use and reading the server's
43 * motd, this hands over to client_run(). Telling the server the
44 * module will cause it to chroot/setuid/etc.
46 * Instead of doing a transfer, the client may at this stage instead
47 * get a listing of remote modules and exit.
49 * @return -1 for error in startup, or the result of client_run().
50 * Either way, it eventually gets passed to exit_cleanup().
52 int start_socket_client(char *host, char *path, int argc, char *argv[])
56 extern char *bind_address;
57 extern int default_af_hint;
59 /* this is redundant with code in start_inband_exchange(), but
60 this short-circuits a problem before we open a socket, and
61 the extra check won't hurt */
63 rprintf(FERROR,"ERROR: The remote path must start with a module name not a /\n");
67 p = strchr(host, '@');
75 /* FIXME: If we're going to use a socket program for
76 * testing, then this message is wrong. We need to
77 * say something like "(except really using %s)" */
78 rprintf(FINFO, "opening tcp connection to %s port %d\n",
81 fd = open_socket_out_wrapped (host, rsync_port, bind_address,
84 exit_cleanup(RERR_SOCKETIO);
87 ret = start_inband_exchange(user, path, fd, fd, argc);
89 return ret < 0? ret : client_run(fd, fd, -1, argc, argv);
92 int start_inband_exchange(char *user, char *path, int f_in, int f_out, int argc)
95 char *sargs[MAX_ARGS];
97 char line[MAXPATHLEN];
99 extern int remote_version;
100 extern int kludge_around_eof;
101 extern int am_sender;
102 extern int daemon_over_rsh;
103 extern int list_only;
105 if (argc == 0 && !am_sender)
109 rprintf(FERROR, "ERROR: The remote path must start with a module name\n");
113 if (!user) user = getenv("USER");
114 if (!user) user = getenv("LOGNAME");
116 /* set daemon_over_rsh to false since we need to build the
117 true set of args passed through the rsh/ssh connection;
118 this is a no-op for direct-socket-connection mode */
120 server_options(sargs, &sargc);
122 sargs[sargc++] = ".";
125 sargs[sargc++] = path;
129 io_printf(f_out, "@RSYNCD: %d\n", PROTOCOL_VERSION);
131 if (!read_line(f_in, line, sizeof(line)-1)) {
132 rprintf(FERROR, "rsync: did not see server greeting\n");
136 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
137 /* note that read_line strips of \n or \r */
138 rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n",
143 p = strchr(path,'/');
145 io_printf(f_out, "%s\n", path);
148 /* Old servers may just drop the connection here,
149 rather than sending a proper EXIT command. Yuck. */
150 kludge_around_eof = list_only && (remote_version < 25);
153 if (!read_line(f_in, line, sizeof(line)-1)) {
154 rprintf(FERROR, "rsync: didn't get server startup line\n");
158 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
159 auth_client(f_out, user, line+18);
163 if (strcmp(line,"@RSYNCD: OK") == 0) break;
165 if (strcmp(line,"@RSYNCD: EXIT") == 0) {
166 /* This is sent by recent versions of the
167 * server to terminate the listing of modules.
168 * We don't want to go on and transfer
169 * anything; just exit. */
173 if (strncmp(line, "@ERROR", 6) == 0) {
174 rprintf(FERROR,"%s\n", line);
175 /* This is always fatal; the server will now
176 * close the socket. */
177 return RERR_STARTCLIENT;
179 rprintf(FINFO,"%s\n", line);
182 kludge_around_eof = False;
184 for (i = 0; i < sargc; i++) {
185 io_printf(f_out, "%s\n", sargs[i]);
187 io_printf(f_out, "\n");
189 if (remote_version < 23) {
190 if (remote_version == 22 || (remote_version > 17 && !am_sender))
191 io_start_multiplex_in(f_in);
199 static int rsync_module(int f_in, int f_out, int i)
202 char *argv[MAX_ARGS];
204 char line[MAXPATHLEN];
205 uid_t uid = (uid_t)-2; /* canonically "nobody" */
206 gid_t gid = (gid_t)-2;
208 char *addr = client_addr(f_in);
209 char *host = client_name(f_in);
210 char *name = lp_name(i);
211 int use_chroot = lp_use_chroot(i);
215 extern int am_sender;
216 extern int am_server;
217 extern int am_daemon;
218 extern int remote_version;
221 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
222 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
224 io_printf(f_out, "@ERROR: access denied to %s from %s (%s)\n",
229 if (am_daemon && am_server) {
230 rprintf(FINFO, "rsync allowed access on module %s from %s (%s)\n",
234 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
236 rprintf(FERROR,"failed to open lock file %s : %s\n",
237 lp_lock_file(i), strerror(errno));
238 io_printf(f_out, "@ERROR: failed to open lock file %s : %s\n",
239 lp_lock_file(i), strerror(errno));
241 rprintf(FERROR,"max connections (%d) reached\n",
242 lp_max_connections(i));
243 io_printf(f_out, "@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
249 auth_user = auth_server(f_in, f_out, i, addr, "@RSYNCD: AUTHREQD ");
252 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
254 io_printf(f_out, "@ERROR: auth failed on module %s\n", name);
260 am_root = (getuid() == 0);
264 if (!name_to_uid(p, &uid)) {
265 if (!isdigit(* (unsigned char *) p)) {
266 rprintf(FERROR,"Invalid uid %s\n", p);
267 io_printf(f_out, "@ERROR: invalid uid %s\n", p);
274 if (!name_to_gid(p, &gid)) {
275 if (!isdigit(* (unsigned char *) p)) {
276 rprintf(FERROR,"Invalid gid %s\n", p);
277 io_printf(f_out, "@ERROR: invalid gid %s\n", p);
284 /* TODO: If we're not root, but the configuration requests
285 * that we change to some uid other than the current one, then
288 /* TODO: Perhaps take a list of gids, and make them into the
289 * supplementary groups. */
291 p = lp_include_from(i);
292 add_exclude_file(p, 1, 1);
297 p = lp_exclude_from(i);
298 add_exclude_file(p, 1, 0);
307 * XXX: The 'use chroot' flag is a fairly reliable
308 * source of confusion, because it fails under two
309 * important circumstances: running as non-root,
310 * running on Win32 (or possibly others). On the
311 * other hand, if you are running as root, then it
312 * might be better to always use chroot.
314 * So, perhaps if we can't chroot we should just issue
315 * a warning, unless a "require chroot" flag is set,
316 * in which case we fail.
318 if (chroot(lp_path(i))) {
319 rsyserr(FERROR, errno, "chroot %s failed", lp_path(i));
320 io_printf(f_out, "@ERROR: chroot failed\n");
324 if (!push_dir("/", 0)) {
325 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
326 io_printf(f_out, "@ERROR: chdir failed\n");
331 if (!push_dir(lp_path(i), 0)) {
332 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
333 io_printf(f_out, "@ERROR: chdir failed\n");
340 #ifdef HAVE_SETGROUPS
341 /* Get rid of any supplementary groups this process
342 * might have inheristed. */
343 if (setgroups(0, NULL)) {
344 rsyserr(FERROR, errno, "setgroups failed");
345 io_printf(f_out, "@ERROR: setgroups failed\n");
350 /* XXXX: You could argue that if the daemon is started
351 * by a non-root user and they explicitly specify a
352 * gid, then we should try to change to that gid --
353 * this could be possible if it's already in their
354 * supplementary groups. */
356 /* TODO: Perhaps we need to document that if rsyncd is
357 * started by somebody other than root it will inherit
358 * all their supplementary groups. */
361 rsyserr(FERROR, errno, "setgid %d failed", (int) gid);
362 io_printf(f_out, "@ERROR: setgid failed\n");
367 rsyserr(FERROR, errno, "setuid %d failed", (int) uid);
368 io_printf(f_out, "@ERROR: setuid failed\n");
372 am_root = (getuid() == 0);
375 io_printf(f_out, "@RSYNCD: OK\n");
377 argv[argc++] = "rsyncd";
380 if (!read_line(f_in, line, sizeof(line)-1)) {
388 argv[argc] = strdup(p);
394 if (start_glob == 1) {
398 glob_expand(name, argv, &argc, MAX_ARGS);
403 if (strcmp(line,".") == 0) {
407 if (argc == MAX_ARGS) {
412 if (sanitize_paths) {
414 * Note that this is applied to all parameters, whether or not
415 * they are filenames, but no other legal parameters contain
416 * the forms that need to be sanitized so it doesn't hurt;
417 * it is not known at this point which parameters are files
420 for (i = 1; i < argc; i++) {
421 sanitize_path(argv[i], NULL);
426 ret = parse_arguments(&argc, (const char ***) &argp, 0);
428 if (filesfrom_fd == 0)
433 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
435 request, auth_user, host, addr);
437 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
439 request, host, addr);
445 /* don't allow the logs to be flooded too fast */
446 if (verbose > 1) verbose = 1;
449 if (remote_version < 23) {
450 if (remote_version == 22 || (remote_version > 17 && am_sender))
451 io_start_multiplex_out(f_out);
454 /* For later protocol versions, we don't start multiplexing
455 * until we've configured nonblocking in start_server. That
456 * means we're in a sticky situation now: there's no way to
457 * convey errors to the client. */
459 /* FIXME: Hold off on reporting option processing errors until
460 * we've set up nonblocking and multiplexed IO and can get the
461 * message back to them. */
464 exit_cleanup(RERR_UNSUPPORTED);
468 extern int io_timeout;
469 io_timeout = lp_timeout(i);
472 start_server(f_in, f_out, argc, argp);
477 /* send a list of available modules to the client. Don't list those
478 with "list = False". */
479 static void send_listing(int fd)
481 int n = lp_numservices();
483 extern int remote_version;
487 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
489 if (remote_version >= 25)
490 io_printf(fd,"@RSYNCD: EXIT\n");
493 /* this is called when a connection is established to a client
494 and we want to start talking. The setup of the system is done from
496 int start_daemon(int f_in, int f_out)
501 extern char *config_file;
502 extern int remote_version;
503 extern int am_server;
505 if (!lp_load(config_file, 0)) {
506 exit_cleanup(RERR_SYNTAX);
512 set_socket_options(f_in, "SO_KEEPALIVE");
513 set_socket_options(f_in, lp_socket_options());
514 set_nonblocking(f_in);
517 io_printf(f_out, "@RSYNCD: %d\n", PROTOCOL_VERSION);
519 motd = lp_motd_file();
521 FILE *f = fopen(motd,"r");
522 while (f && !feof(f)) {
523 int len = fread(line, 1, sizeof(line)-1, f);
526 io_printf(f_out, "%s", line);
530 io_printf(f_out, "\n");
533 if (!read_line(f_in, line, sizeof(line)-1)) {
537 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
538 io_printf(f_out, "@ERROR: protocol startup error\n");
544 if (!read_line(f_in, line, sizeof(line)-1)) {
548 if (!*line || strcmp(line,"#list")==0) {
554 /* it's some sort of command that I don't understand */
555 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
561 io_printf(f_out, "@ERROR: Unknown module '%s'\n", line);
566 return rsync_module(f_in, f_out, i);
570 int daemon_main(void)
572 extern char *config_file;
573 extern int orig_umask;
575 extern int no_detach;
577 if (is_a_socket(STDIN_FILENO)) {
580 /* we are running via inetd - close off stdout and
581 stderr so that library functions (and getopt) don't
582 try to use them. Redirect them to /dev/null */
585 open("/dev/null", O_RDWR);
588 return start_daemon(STDIN_FILENO, STDIN_FILENO);
594 if (!lp_load(config_file, 1)) {
595 exit_cleanup(RERR_SYNTAX);
600 rprintf(FINFO, "rsyncd version %s starting, listening on port %d\n",
603 /* TODO: If listening on a particular address, then show that
604 * address too. In fact, why not just do inet_ntop on the
605 * local address??? */
607 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
610 int pid = (int) getpid();
611 cleanup_set_pid(pid);
612 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
613 0666 & ~orig_umask)) == -1) {
615 rsyserr(FLOG, errno, "failed to create pid file %s", pid_file);
616 exit_cleanup(RERR_FILEIO);
618 snprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
619 write(fd, pidbuf, strlen(pidbuf));
623 start_accept_loop(rsync_port, start_daemon);