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 negotiating which module to use and reading the server's
42 * motd, this hands over to client_run(). Telling the server the
43 * module will cause it to chroot/setuid/etc.
45 * Instead of doing a transfer, the client may at this stage instead
46 * get a listing of remote modules and exit.
48 * @return -1 for error in startup, or the result of client_run().
49 * Either way, it eventually gets passed to exit_cleanup().
51 int start_socket_client(char *host, char *path, int argc, char *argv[])
54 char *sargs[MAX_ARGS];
56 char line[MAXPATHLEN];
58 extern int remote_version;
60 extern char *shell_cmd;
62 extern int kludge_around_eof;
63 extern char *bind_address;
64 extern int default_af_hint;
66 if (argc == 0 && !am_sender) {
71 /* This is just a friendliness enhancement: if the connection
72 * is to an rsyncd then there is no point specifying the -e option.
73 * Note that this is only set if the -e was explicitly specified,
74 * not if the environment variable just happens to be set.
75 * See http://lists.samba.org/pipermail/rsync/2000-September/002744.html
78 rprintf(FERROR, "WARNING: --rsh or -e option ignored when "
79 "connecting to rsync daemon\n");
84 rprintf(FERROR,"ERROR: The remote path must start with a module name not a /\n");
88 p = strchr(host, '@');
95 if (!user) user = getenv("USER");
96 if (!user) user = getenv("LOGNAME");
99 /* FIXME: If we're going to use a socket program for
100 * testing, then this message is wrong. We need to
101 * say something like "(except really using %s)" */
102 rprintf(FINFO, "opening tcp connection to %s port %d\n",
105 fd = open_socket_out_wrapped (host, rsync_port, bind_address,
108 exit_cleanup(RERR_SOCKETIO);
111 server_options(sargs,&sargc);
113 sargs[sargc++] = ".";
116 sargs[sargc++] = path;
120 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
122 if (!read_line(fd, line, sizeof(line)-1)) {
123 rprintf(FERROR, "rsync: did not see server greeting\n");
127 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
128 /* note that read_line strips of \n or \r */
129 rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n",
134 p = strchr(path,'/');
136 io_printf(fd,"%s\n",path);
139 /* Old servers may just drop the connection here,
140 rather than sending a proper EXIT command. Yuck. */
141 kludge_around_eof = list_only && (remote_version < 25);
144 if (!read_line(fd, line, sizeof(line)-1)) {
145 rprintf(FERROR, "rsync: didn't get server startup line\n");
149 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
150 auth_client(fd, user, line+18);
154 if (strcmp(line,"@RSYNCD: OK") == 0) break;
156 if (strcmp(line,"@RSYNCD: EXIT") == 0) {
157 /* This is sent by recent versions of the
158 * server to terminate the listing of modules.
159 * We don't want to go on and transfer
160 * anything; just exit. */
164 if (strncmp(line, "@ERROR", 6) == 0)
165 rprintf(FERROR,"%s\n", line);
167 rprintf(FINFO,"%s\n", line);
169 kludge_around_eof = False;
171 for (i=0;i<sargc;i++) {
172 io_printf(fd,"%s\n", sargs[i]);
176 if (remote_version < 23) {
177 if (remote_version == 22 || (remote_version > 17 && !am_sender))
178 io_start_multiplex_in(fd);
181 return client_run(fd, fd, -1, argc, argv);
186 static int rsync_module(int fd, int i)
189 char *argv[MAX_ARGS];
191 char line[MAXPATHLEN];
192 uid_t uid = (uid_t)-2; /* canonically "nobody" */
193 gid_t gid = (gid_t)-2;
195 char *addr = client_addr(fd);
196 char *host = client_name(fd);
197 char *name = lp_name(i);
198 int use_chroot = lp_use_chroot(i);
202 extern int am_sender;
203 extern int remote_version;
206 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
207 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
209 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
214 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
216 rprintf(FERROR,"failed to open lock file %s : %s\n",
217 lp_lock_file(i), strerror(errno));
218 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
219 lp_lock_file(i), strerror(errno));
221 rprintf(FERROR,"max connections (%d) reached\n",
222 lp_max_connections(i));
223 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
229 auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
232 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
233 name, client_name(fd), client_addr(fd));
234 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
240 am_root = (getuid() == 0);
244 if (!name_to_uid(p, &uid)) {
246 rprintf(FERROR,"Invalid uid %s\n", p);
247 io_printf(fd,"@ERROR: invalid uid %s\n", p);
254 if (!name_to_gid(p, &gid)) {
256 rprintf(FERROR,"Invalid gid %s\n", p);
257 io_printf(fd,"@ERROR: invalid gid %s\n", p);
264 /* TODO: If we're not root, but the configuration requests
265 * that we change to some uid other than the current one, then
268 /* TODO: Perhaps take a list of gids, and make them into the
269 * supplementary groups. */
271 p = lp_include_from(i);
272 add_exclude_file(p, 1, 1);
277 p = lp_exclude_from(i);
278 add_exclude_file(p, 1, 0);
287 * XXX: The 'use chroot' flag is a fairly reliable
288 * source of confusion, because it fails under two
289 * important circumstances: running as non-root,
290 * running on Win32 (or possibly others). On the
291 * other hand, if you are running as root, then it
292 * might be better to always use chroot.
294 * So, perhaps if we can't chroot we should just issue
295 * a warning, unless a "require chroot" flag is set,
296 * in which case we fail.
298 if (chroot(lp_path(i))) {
299 rsyserr(FERROR, errno, "chroot %s failed", lp_path(i));
300 io_printf(fd,"@ERROR: chroot failed\n");
304 if (!push_dir("/", 0)) {
305 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
306 io_printf(fd,"@ERROR: chdir failed\n");
311 if (!push_dir(lp_path(i), 0)) {
312 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
313 io_printf(fd,"@ERROR: chdir failed\n");
320 #ifdef HAVE_SETGROUPS
321 /* Get rid of any supplementary groups this process
322 * might have inheristed. */
323 if (setgroups(0, NULL)) {
324 rsyserr(FERROR, errno, "setgroups failed");
325 io_printf(fd, "@ERROR: setgroups failed\n");
330 /* XXXX: You could argue that if the daemon is started
331 * by a non-root user and they explicitly specify a
332 * gid, then we should try to change to that gid --
333 * this could be possible if it's already in their
334 * supplementary groups. */
336 /* TODO: Perhaps we need to document that if rsyncd is
337 * started by somebody other than root it will inherit
338 * all their supplementary groups. */
341 rsyserr(FERROR, errno, "setgid %d failed", (int) gid);
342 io_printf(fd,"@ERROR: setgid failed\n");
347 rsyserr(FERROR, errno, "setuid %d failed", (int) uid);
348 io_printf(fd,"@ERROR: setuid failed\n");
352 am_root = (getuid() == 0);
355 io_printf(fd,"@RSYNCD: OK\n");
357 argv[argc++] = "rsyncd";
360 if (!read_line(fd, line, sizeof(line)-1)) {
368 argv[argc] = strdup(p);
374 if (start_glob == 1) {
378 glob_expand(name, argv, &argc, MAX_ARGS);
383 if (strcmp(line,".") == 0) {
387 if (argc == MAX_ARGS) {
392 if (sanitize_paths) {
394 * Note that this is applied to all parameters, whether or not
395 * they are filenames, but no other legal parameters contain
396 * the forms that need to be sanitized so it doesn't hurt;
397 * it is not known at this point which parameters are files
400 for (i = 1; i < argc; i++) {
401 sanitize_path(argv[i], NULL);
406 ret = parse_arguments(&argc, (const char ***) &argp, 0);
410 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
412 request, auth_user, host, addr);
414 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
416 request, host, addr);
422 /* don't allow the logs to be flooded too fast */
423 if (verbose > 1) verbose = 1;
426 if (remote_version < 23) {
427 if (remote_version == 22 || (remote_version > 17 && am_sender))
428 io_start_multiplex_out(fd);
431 /* For later protocol versions, we don't start multiplexing
432 * until we've configured nonblocking in start_server. That
433 * means we're in a sticky situation now: there's no way to
434 * convey errors to the client. */
436 /* FIXME: Hold off on reporting option processing errors until
437 * we've set up nonblocking and multiplexed IO and can get the
438 * message back to them. */
441 exit_cleanup(RERR_UNSUPPORTED);
445 extern int io_timeout;
446 io_timeout = lp_timeout(i);
449 start_server(fd, fd, argc, argp);
454 /* send a list of available modules to the client. Don't list those
455 with "list = False". */
456 static void send_listing(int fd)
458 int n = lp_numservices();
460 extern int remote_version;
464 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
466 if (remote_version >= 25)
467 io_printf(fd,"@RSYNCD: EXIT\n");
470 /* this is called when a socket connection is established to a client
471 and we want to start talking. The setup of the system is done from
473 static int start_daemon(int fd)
478 extern char *config_file;
479 extern int remote_version;
481 if (!lp_load(config_file, 0)) {
482 exit_cleanup(RERR_SYNTAX);
485 set_socket_options(fd,"SO_KEEPALIVE");
486 set_socket_options(fd,lp_socket_options());
489 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
491 motd = lp_motd_file();
493 FILE *f = fopen(motd,"r");
494 while (f && !feof(f)) {
495 int len = fread(line, 1, sizeof(line)-1, f);
498 io_printf(fd,"%s", line);
505 if (!read_line(fd, line, sizeof(line)-1)) {
509 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
510 io_printf(fd,"@ERROR: protocol startup error\n");
516 if (!read_line(fd, line, sizeof(line)-1)) {
520 if (!*line || strcmp(line,"#list")==0) {
526 /* it's some sort of command that I don't understand */
527 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
533 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
538 return rsync_module(fd, i);
542 int daemon_main(void)
544 extern char *config_file;
545 extern int orig_umask;
547 extern int no_detach;
549 if (is_a_socket(STDIN_FILENO)) {
552 /* we are running via inetd - close off stdout and
553 stderr so that library functions (and getopt) don't
554 try to use them. Redirect them to /dev/null */
557 open("/dev/null", O_RDWR);
560 return start_daemon(STDIN_FILENO);
566 if (!lp_load(config_file, 1)) {
567 exit_cleanup(RERR_SYNTAX);
572 rprintf(FINFO, "rsyncd version %s starting, listening on port %d\n",
575 /* TODO: If listening on a particular address, then show that
576 * address too. In fact, why not just do inet_ntop on the
577 * local address??? */
579 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
582 int pid = (int) getpid();
583 cleanup_set_pid(pid);
584 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
585 0666 & ~orig_umask)) == -1) {
587 rsyserr(FLOG, errno, "failed to create pid file %s", pid_file);
588 exit_cleanup(RERR_FILEIO);
590 snprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
591 write(fd, pidbuf, strlen(pidbuf));
595 start_accept_loop(rsync_port, start_daemon);