1 /* -*- c-file-style: "linux"; -*-
3 Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4 Copyright (C) 2001 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.
21 /* the socket based protocol for setting up a connection with rsyncd */
28 extern int rsync_port;
30 int sanitize_paths = 0;
33 * Run a client connected to an rsyncd. The alternative to this
34 * function for remote-shell connections is do_cmd.
36 int start_socket_client(char *host, char *path, int argc, char *argv[])
39 char *sargs[MAX_ARGS];
41 char line[MAXPATHLEN];
43 extern int remote_version;
45 extern char *shell_cmd;
46 extern int kludge_around_eof;
47 extern char *bind_address;
48 extern int default_af_hint;
50 if (argc == 0 && !am_sender) {
55 /* This is just a friendliness enhancement: if the connection
56 * is to an rsyncd then there is no point specifying the -e option.
57 * Note that this is only set if the -e was explicitly specified,
58 * not if the environment variable just happens to be set.
59 * See http://lists.samba.org/pipermail/rsync/2000-September/002744.html
62 rprintf(FERROR, "WARNING: --rsh or -e option ignored when "
63 "connecting to rsync daemon\n");
68 rprintf(FERROR,"ERROR: The remote path must start with a module name not a /\n");
72 p = strchr(host, '@');
79 if (!user) user = getenv("USER");
80 if (!user) user = getenv("LOGNAME");
84 fd = open_socket_out_wrapped (host, rsync_port, bind_address,
87 exit_cleanup(RERR_SOCKETIO);
90 server_options(sargs,&sargc);
95 sargs[sargc++] = path;
99 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
101 if (!read_line(fd, line, sizeof(line)-1)) {
105 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
109 p = strchr(path,'/');
111 io_printf(fd,"%s\n",path);
114 /* Old servers may just drop the connection here,
115 rather than sending a proper EXIT command. Yuck. */
116 kludge_around_eof = remote_version < 25;
119 if (!read_line(fd, line, sizeof(line)-1)) {
123 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
124 auth_client(fd, user, line+18);
128 if (strcmp(line,"@RSYNCD: OK") == 0) break;
130 if (strcmp(line,"@RSYNCD: EXIT") == 0) exit(0);
132 if (strncmp(line, "@ERROR", 6) == 0)
133 rprintf(FERROR,"%s\n", line);
135 rprintf(FINFO,"%s\n", line);
137 kludge_around_eof = False;
139 for (i=0;i<sargc;i++) {
140 io_printf(fd,"%s\n", sargs[i]);
144 if (remote_version < 23) {
145 if (remote_version == 22 || (remote_version > 17 && !am_sender))
146 io_start_multiplex_in(fd);
149 return client_run(fd, fd, -1, argc, argv);
154 static int rsync_module(int fd, int i)
157 char *argv[MAX_ARGS];
159 char line[MAXPATHLEN];
160 uid_t uid = (uid_t)-2; /* canonically "nobody" */
161 gid_t gid = (gid_t)-2;
163 char *addr = client_addr(fd);
164 char *host = client_name(fd);
165 char *name = lp_name(i);
166 int use_chroot = lp_use_chroot(i);
170 extern int am_sender;
171 extern int remote_version;
174 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
175 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
177 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
182 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
184 rprintf(FERROR,"failed to open lock file %s : %s\n",
185 lp_lock_file(i), strerror(errno));
186 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
187 lp_lock_file(i), strerror(errno));
189 rprintf(FERROR,"max connections (%d) reached\n",
190 lp_max_connections(i));
191 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
197 auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
200 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
201 name, client_name(fd), client_addr(fd));
202 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
208 am_root = (getuid() == 0);
212 if (!name_to_uid(p, &uid)) {
214 rprintf(FERROR,"Invalid uid %s\n", p);
215 io_printf(fd,"@ERROR: invalid uid %s\n", p);
222 if (!name_to_gid(p, &gid)) {
224 rprintf(FERROR,"Invalid gid %s\n", p);
225 io_printf(fd,"@ERROR: invalid gid %s\n", p);
232 /* TODO: If we're not root, but the configuration requests
233 * that we change to some uid other than the current one, then
236 /* TODO: Perhaps take a list of gids, and make them into the
237 * supplementary groups. */
239 p = lp_include_from(i);
240 add_exclude_file(p, 1, 1);
245 p = lp_exclude_from(i);
246 add_exclude_file(p, 1, 0);
255 * XXX: The 'use chroot' flag is a fairly reliable
256 * source of confusion, because it fails under two
257 * important circumstances: running as non-root,
258 * running on Win32 (or possibly others). On the
259 * other hand, if you are running as root, then it
260 * might be better to always use chroot.
262 * So, perhaps if we can't chroot we should just issue
263 * a warning, unless a "require chroot" flag is set,
264 * in which case we fail.
266 if (chroot(lp_path(i))) {
267 rsyserr(FERROR, errno, "chroot %s failed", lp_path(i));
268 io_printf(fd,"@ERROR: chroot failed\n");
272 if (!push_dir("/", 0)) {
273 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
274 io_printf(fd,"@ERROR: chdir failed\n");
279 if (!push_dir(lp_path(i), 0)) {
280 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
281 io_printf(fd,"@ERROR: chdir failed\n");
288 /* Get rid of any supplementary groups this process
289 * might have inheristed. */
290 if (setgroups(0, NULL)) {
291 rsyserr(FERROR, errno, "setgroups failed");
292 io_printf(fd, "@ERROR: setgroups failed\n");
296 /* XXXX: You could argue that if the daemon is started
297 * by a non-root user and they explicitly specify a
298 * gid, then we should try to change to that gid --
299 * this could be possible if it's already in their
300 * supplementary groups. */
302 /* TODO: Perhaps we need to document that if rsyncd is
303 * started by somebody other than root it will inherit
304 * all their supplementary groups. */
307 rsyserr(FERROR, errno, "setgid %d failed", (int) gid);
308 io_printf(fd,"@ERROR: setgid failed\n");
313 rsyserr(FERROR, errno, "setuid %d failed", (int) uid);
314 io_printf(fd,"@ERROR: setuid failed\n");
318 am_root = (getuid() == 0);
321 io_printf(fd,"@RSYNCD: OK\n");
323 argv[argc++] = "rsyncd";
326 if (!read_line(fd, line, sizeof(line)-1)) {
334 argv[argc] = strdup(p);
340 if (start_glob == 1) {
344 glob_expand(name, argv, &argc, MAX_ARGS);
349 if (strcmp(line,".") == 0) {
353 if (argc == MAX_ARGS) {
358 if (sanitize_paths) {
360 * Note that this is applied to all parameters, whether or not
361 * they are filenames, but no other legal parameters contain
362 * the forms that need to be sanitized so it doesn't hurt;
363 * it is not known at this point which parameters are files
366 for (i = 1; i < argc; i++) {
367 sanitize_path(argv[i], NULL);
372 ret = parse_arguments(&argc, (const char ***) &argp, 0);
376 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
378 request, auth_user, host, addr);
380 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
382 request, host, addr);
388 /* don't allow the logs to be flooded too fast */
389 if (verbose > 1) verbose = 1;
392 if (remote_version < 23) {
393 if (remote_version == 22 || (remote_version > 17 && am_sender))
394 io_start_multiplex_out(fd);
397 /* For later protocol versions, we don't start multiplexing
398 * until we've configured nonblocking in start_server. That
399 * means we're in a sticky situation now: there's no way to
400 * convey errors to the client. */
402 /* FIXME: Hold off on reporting option processing errors until
403 * we've set up nonblocking and multiplexed IO and can get the
404 * message back to them. */
407 exit_cleanup(RERR_UNSUPPORTED);
411 extern int io_timeout;
412 io_timeout = lp_timeout(i);
415 start_server(fd, fd, argc, argp);
420 /* send a list of available modules to the client. Don't list those
421 with "list = False". */
422 static void send_listing(int fd)
424 int n = lp_numservices();
426 extern int remote_version;
430 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
432 if (remote_version >= 25)
433 io_printf(fd,"@RSYNCD: EXIT\n");
436 /* this is called when a socket connection is established to a client
437 and we want to start talking. The setup of the system is done from
439 static int start_daemon(int fd)
444 extern char *config_file;
445 extern int remote_version;
447 if (!lp_load(config_file, 0)) {
448 exit_cleanup(RERR_SYNTAX);
451 set_socket_options(fd,"SO_KEEPALIVE");
452 set_socket_options(fd,lp_socket_options());
455 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
457 motd = lp_motd_file();
459 FILE *f = fopen(motd,"r");
460 while (f && !feof(f)) {
461 int len = fread(line, 1, sizeof(line)-1, f);
464 io_printf(fd,"%s", line);
471 if (!read_line(fd, line, sizeof(line)-1)) {
475 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
476 io_printf(fd,"@ERROR: protocol startup error\n");
482 if (!read_line(fd, line, sizeof(line)-1)) {
486 if (!*line || strcmp(line,"#list")==0) {
492 /* it's some sort of command that I don't understand */
493 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
499 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
504 return rsync_module(fd, i);
508 int daemon_main(void)
510 extern char *config_file;
511 extern int orig_umask;
513 extern int no_detach;
515 if (is_a_socket(STDIN_FILENO)) {
518 /* we are running via inetd - close off stdout and
519 stderr so that library functions (and getopt) don't
520 try to use them. Redirect them to /dev/null */
523 open("/dev/null", O_RDWR);
526 return start_daemon(STDIN_FILENO);
532 if (!lp_load(config_file, 1)) {
533 exit_cleanup(RERR_SYNTAX);
538 rprintf(FINFO, "rsyncd version %s starting, listening on port %d\n",
541 /* TODO: If listening on a particular address, then show that
542 * address too. In fact, why not just do inet_ntop on the
543 * local address??? */
545 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
548 int pid = (int) getpid();
549 cleanup_set_pid(pid);
550 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
551 0666 & ~orig_umask)) == -1) {
553 rsyserr(FLOG, errno, "failed to create pid file %s", pid_file);
554 exit_cleanup(RERR_FILEIO);
556 snprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
557 write(fd, pidbuf, strlen(pidbuf));
561 start_accept_loop(rsync_port, start_daemon);