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;
38 * Run a client connected to an rsyncd. The alternative to this
39 * function for remote-shell connections is do_cmd().
41 * After initial server startup, hands over to client_run().
43 * @return -1 for error in startup, or the result of client_run().
45 int start_socket_client(char *host, char *path, int argc, char *argv[])
48 char *sargs[MAX_ARGS];
50 char line[MAXPATHLEN];
52 extern int remote_version;
54 extern char *shell_cmd;
56 extern int kludge_around_eof;
57 extern char *bind_address;
58 extern int default_af_hint;
60 if (argc == 0 && !am_sender) {
65 /* This is just a friendliness enhancement: if the connection
66 * is to an rsyncd then there is no point specifying the -e option.
67 * Note that this is only set if the -e was explicitly specified,
68 * not if the environment variable just happens to be set.
69 * See http://lists.samba.org/pipermail/rsync/2000-September/002744.html
72 rprintf(FERROR, "WARNING: --rsh or -e option ignored when "
73 "connecting to rsync daemon\n");
78 rprintf(FERROR,"ERROR: The remote path must start with a module name not a /\n");
82 p = strchr(host, '@');
89 if (!user) user = getenv("USER");
90 if (!user) user = getenv("LOGNAME");
93 /* FIXME: If we're going to use a socket program for
94 * testing, then this message is wrong. We need to
95 * say something like "(except really using %s)" */
96 rprintf(FINFO, "opening tcp connection to %s port %d\n",
99 fd = open_socket_out_wrapped (host, rsync_port, bind_address,
102 exit_cleanup(RERR_SOCKETIO);
105 server_options(sargs,&sargc);
107 sargs[sargc++] = ".";
110 sargs[sargc++] = path;
114 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
116 if (!read_line(fd, line, sizeof(line)-1)) {
117 rprintf(FERROR, "rsync: did not see server greeting\n");
121 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
122 /* note that read_line strips of \n or \r */
123 rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n",
128 p = strchr(path,'/');
130 io_printf(fd,"%s\n",path);
133 /* Old servers may just drop the connection here,
134 rather than sending a proper EXIT command. Yuck. */
135 kludge_around_eof = list_only && (remote_version < 25);
138 if (!read_line(fd, line, sizeof(line)-1)) {
139 rprintf(FERROR, "rsync: didn't get server startup line\n");
143 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
144 auth_client(fd, user, line+18);
148 if (strcmp(line,"@RSYNCD: OK") == 0) break;
150 if (strcmp(line,"@RSYNCD: EXIT") == 0) {
151 /* This is sent by recent versions of the
152 * server to terminate the listing of modules.
153 * We don't want to go on and transfer
154 * anything; just exit. */
158 if (strncmp(line, "@ERROR", 6) == 0)
159 rprintf(FERROR,"%s\n", line);
161 rprintf(FINFO,"%s\n", line);
163 kludge_around_eof = False;
165 for (i=0;i<sargc;i++) {
166 io_printf(fd,"%s\n", sargs[i]);
170 if (remote_version < 23) {
171 if (remote_version == 22 || (remote_version > 17 && !am_sender))
172 io_start_multiplex_in(fd);
175 return client_run(fd, fd, -1, argc, argv);
180 static int rsync_module(int fd, int i)
183 char *argv[MAX_ARGS];
185 char line[MAXPATHLEN];
186 uid_t uid = (uid_t)-2; /* canonically "nobody" */
187 gid_t gid = (gid_t)-2;
189 char *addr = client_addr(fd);
190 char *host = client_name(fd);
191 char *name = lp_name(i);
192 int use_chroot = lp_use_chroot(i);
196 extern int am_sender;
197 extern int remote_version;
200 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
201 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
203 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
208 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
210 rprintf(FERROR,"failed to open lock file %s : %s\n",
211 lp_lock_file(i), strerror(errno));
212 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
213 lp_lock_file(i), strerror(errno));
215 rprintf(FERROR,"max connections (%d) reached\n",
216 lp_max_connections(i));
217 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
223 auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
226 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
227 name, client_name(fd), client_addr(fd));
228 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
234 am_root = (getuid() == 0);
238 if (!name_to_uid(p, &uid)) {
240 rprintf(FERROR,"Invalid uid %s\n", p);
241 io_printf(fd,"@ERROR: invalid uid %s\n", p);
248 if (!name_to_gid(p, &gid)) {
250 rprintf(FERROR,"Invalid gid %s\n", p);
251 io_printf(fd,"@ERROR: invalid gid %s\n", p);
258 /* TODO: If we're not root, but the configuration requests
259 * that we change to some uid other than the current one, then
262 /* TODO: Perhaps take a list of gids, and make them into the
263 * supplementary groups. */
265 p = lp_include_from(i);
266 add_exclude_file(p, 1, 1);
271 p = lp_exclude_from(i);
272 add_exclude_file(p, 1, 0);
281 * XXX: The 'use chroot' flag is a fairly reliable
282 * source of confusion, because it fails under two
283 * important circumstances: running as non-root,
284 * running on Win32 (or possibly others). On the
285 * other hand, if you are running as root, then it
286 * might be better to always use chroot.
288 * So, perhaps if we can't chroot we should just issue
289 * a warning, unless a "require chroot" flag is set,
290 * in which case we fail.
292 if (chroot(lp_path(i))) {
293 rsyserr(FERROR, errno, "chroot %s failed", lp_path(i));
294 io_printf(fd,"@ERROR: chroot failed\n");
298 if (!push_dir("/", 0)) {
299 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
300 io_printf(fd,"@ERROR: chdir failed\n");
305 if (!push_dir(lp_path(i), 0)) {
306 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
307 io_printf(fd,"@ERROR: chdir failed\n");
314 #ifdef HAVE_SETGROUPS
315 /* Get rid of any supplementary groups this process
316 * might have inheristed. */
317 if (setgroups(0, NULL)) {
318 rsyserr(FERROR, errno, "setgroups failed");
319 io_printf(fd, "@ERROR: setgroups failed\n");
324 /* XXXX: You could argue that if the daemon is started
325 * by a non-root user and they explicitly specify a
326 * gid, then we should try to change to that gid --
327 * this could be possible if it's already in their
328 * supplementary groups. */
330 /* TODO: Perhaps we need to document that if rsyncd is
331 * started by somebody other than root it will inherit
332 * all their supplementary groups. */
335 rsyserr(FERROR, errno, "setgid %d failed", (int) gid);
336 io_printf(fd,"@ERROR: setgid failed\n");
341 rsyserr(FERROR, errno, "setuid %d failed", (int) uid);
342 io_printf(fd,"@ERROR: setuid failed\n");
346 am_root = (getuid() == 0);
349 io_printf(fd,"@RSYNCD: OK\n");
351 argv[argc++] = "rsyncd";
354 if (!read_line(fd, line, sizeof(line)-1)) {
362 argv[argc] = strdup(p);
368 if (start_glob == 1) {
372 glob_expand(name, argv, &argc, MAX_ARGS);
377 if (strcmp(line,".") == 0) {
381 if (argc == MAX_ARGS) {
386 if (sanitize_paths) {
388 * Note that this is applied to all parameters, whether or not
389 * they are filenames, but no other legal parameters contain
390 * the forms that need to be sanitized so it doesn't hurt;
391 * it is not known at this point which parameters are files
394 for (i = 1; i < argc; i++) {
395 sanitize_path(argv[i], NULL);
400 ret = parse_arguments(&argc, (const char ***) &argp, 0);
404 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
406 request, auth_user, host, addr);
408 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
410 request, host, addr);
416 /* don't allow the logs to be flooded too fast */
417 if (verbose > 1) verbose = 1;
420 if (remote_version < 23) {
421 if (remote_version == 22 || (remote_version > 17 && am_sender))
422 io_start_multiplex_out(fd);
425 /* For later protocol versions, we don't start multiplexing
426 * until we've configured nonblocking in start_server. That
427 * means we're in a sticky situation now: there's no way to
428 * convey errors to the client. */
430 /* FIXME: Hold off on reporting option processing errors until
431 * we've set up nonblocking and multiplexed IO and can get the
432 * message back to them. */
435 exit_cleanup(RERR_UNSUPPORTED);
439 extern int io_timeout;
440 io_timeout = lp_timeout(i);
443 start_server(fd, fd, argc, argp);
448 /* send a list of available modules to the client. Don't list those
449 with "list = False". */
450 static void send_listing(int fd)
452 int n = lp_numservices();
454 extern int remote_version;
458 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
460 if (remote_version >= 25)
461 io_printf(fd,"@RSYNCD: EXIT\n");
464 /* this is called when a socket connection is established to a client
465 and we want to start talking. The setup of the system is done from
467 static int start_daemon(int fd)
472 extern char *config_file;
473 extern int remote_version;
475 if (!lp_load(config_file, 0)) {
476 exit_cleanup(RERR_SYNTAX);
479 set_socket_options(fd,"SO_KEEPALIVE");
480 set_socket_options(fd,lp_socket_options());
483 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
485 motd = lp_motd_file();
487 FILE *f = fopen(motd,"r");
488 while (f && !feof(f)) {
489 int len = fread(line, 1, sizeof(line)-1, f);
492 io_printf(fd,"%s", line);
499 if (!read_line(fd, line, sizeof(line)-1)) {
503 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
504 io_printf(fd,"@ERROR: protocol startup error\n");
510 if (!read_line(fd, line, sizeof(line)-1)) {
514 if (!*line || strcmp(line,"#list")==0) {
520 /* it's some sort of command that I don't understand */
521 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
527 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
532 return rsync_module(fd, i);
536 int daemon_main(void)
538 extern char *config_file;
539 extern int orig_umask;
541 extern int no_detach;
543 if (is_a_socket(STDIN_FILENO)) {
546 /* we are running via inetd - close off stdout and
547 stderr so that library functions (and getopt) don't
548 try to use them. Redirect them to /dev/null */
551 open("/dev/null", O_RDWR);
554 return start_daemon(STDIN_FILENO);
560 if (!lp_load(config_file, 1)) {
561 exit_cleanup(RERR_SYNTAX);
566 rprintf(FINFO, "rsyncd version %s starting, listening on port %d\n",
569 /* TODO: If listening on a particular address, then show that
570 * address too. In fact, why not just do inet_ntop on the
571 * local address??? */
573 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
576 int pid = (int) getpid();
577 cleanup_set_pid(pid);
578 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
579 0666 & ~orig_umask)) == -1) {
581 rsyserr(FLOG, errno, "failed to create pid file %s", pid_file);
582 exit_cleanup(RERR_FILEIO);
584 snprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
585 write(fd, pidbuf, strlen(pidbuf));
589 start_accept_loop(rsync_port, start_daemon);