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 struct in_addr socket_address;
46 extern char *shell_cmd;
48 if (argc == 0 && !am_sender) {
53 /* This is just a friendliness enhancement: if the connection
54 * is to an rsyncd then there is no point specifying the -e option.
55 * Note that this is only set if the -e was explicitly specified,
56 * not if the environment variable just happens to be set.
57 * See http://lists.samba.org/pipermail/rsync/2000-September/002744.html
60 rprintf(FERROR, "WARNING: --rsh or -e option ignored when "
61 "connecting to rsync daemon\n");
66 rprintf(FERROR,"ERROR: The remote path must start with a module name\n");
70 p = strchr(host, '@');
77 if (!user) user = getenv("USER");
78 if (!user) user = getenv("LOGNAME");
80 fd = open_socket_out(host, rsync_port, &socket_address);
82 exit_cleanup(RERR_SOCKETIO);
85 server_options(sargs,&sargc);
90 sargs[sargc++] = path;
94 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
96 if (!read_line(fd, line, sizeof(line)-1)) {
100 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
104 p = strchr(path,'/');
106 io_printf(fd,"%s\n",path);
110 if (!read_line(fd, line, sizeof(line)-1)) {
114 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
115 auth_client(fd, user, line+18);
119 if (strcmp(line,"@RSYNCD: OK") == 0) break;
120 rprintf(FINFO,"%s\n", line);
123 for (i=0;i<sargc;i++) {
124 io_printf(fd,"%s\n", sargs[i]);
128 if (remote_version < 23) {
129 if (remote_version == 22 || (remote_version > 17 && !am_sender))
130 io_start_multiplex_in(fd);
133 return client_run(fd, fd, -1, argc, argv);
138 static int rsync_module(int fd, int i)
141 char *argv[MAX_ARGS];
143 char line[MAXPATHLEN];
144 uid_t uid = (uid_t)-2;
145 gid_t gid = (gid_t)-2;
147 char *addr = client_addr(fd);
148 char *host = client_name(fd);
149 char *name = lp_name(i);
150 int use_chroot = lp_use_chroot(i);
154 extern int am_sender;
155 extern int remote_version;
158 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
159 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
160 name, client_name(fd), client_addr(fd));
161 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
162 name, client_name(fd), client_addr(fd));
166 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
168 rprintf(FERROR,"failed to open lock file %s : %s\n",
169 lp_lock_file(i), strerror(errno));
170 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
171 lp_lock_file(i), strerror(errno));
173 rprintf(FERROR,"max connections (%d) reached\n",
174 lp_max_connections(i));
175 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
181 auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
184 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
185 name, client_name(fd), client_addr(fd));
186 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
192 am_root = (getuid() == 0);
196 if (!name_to_uid(p, &uid)) {
198 rprintf(FERROR,"Invalid uid %s\n", p);
199 io_printf(fd,"@ERROR: invalid uid\n");
206 if (!name_to_gid(p, &gid)) {
208 rprintf(FERROR,"Invalid gid %s\n", p);
209 io_printf(fd,"@ERROR: invalid gid\n");
216 p = lp_include_from(i);
217 add_exclude_file(p, 1, 1);
222 p = lp_exclude_from(i);
223 add_exclude_file(p, 1, 0);
231 if (chroot(lp_path(i))) {
232 rsyserr(FERROR, errno, "chroot %s failed", lp_path(i));
233 io_printf(fd,"@ERROR: chroot failed\n");
237 if (!push_dir("/", 0)) {
238 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
239 io_printf(fd,"@ERROR: chdir failed\n");
244 if (!push_dir(lp_path(i), 0)) {
245 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
246 io_printf(fd,"@ERROR: chdir failed\n");
254 rsyserr(FERROR, errno, "setgid %d failed", gid);
255 io_printf(fd,"@ERROR: setgid failed\n");
260 rsyserr(FERROR, errno, "setuid %d failed", uid);
261 io_printf(fd,"@ERROR: setuid failed\n");
265 am_root = (getuid() == 0);
268 io_printf(fd,"@RSYNCD: OK\n");
270 argv[argc++] = "rsyncd";
273 if (!read_line(fd, line, sizeof(line)-1)) {
281 argv[argc] = strdup(p);
287 if (start_glob == 1) {
291 glob_expand(name, argv, &argc, MAX_ARGS);
296 if (strcmp(line,".") == 0) {
300 if (argc == MAX_ARGS) {
305 if (sanitize_paths) {
307 * Note that this is applied to all parameters, whether or not
308 * they are filenames, but no other legal parameters contain
309 * the forms that need to be sanitized so it doesn't hurt;
310 * it is not known at this point which parameters are files
313 for (i = 1; i < argc; i++) {
314 sanitize_path(argv[i], NULL);
319 ret = parse_arguments(&argc, (const char ***) &argp, 0);
323 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
325 request, auth_user, host, addr);
327 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
329 request, host, addr);
335 /* don't allow the logs to be flooded too fast */
336 if (verbose > 1) verbose = 1;
339 if (remote_version < 23) {
340 if (remote_version == 22 || (remote_version > 17 && am_sender))
341 io_start_multiplex_out(fd);
344 /* For later protocol versions, we don't start multiplexing
345 * until we've configured nonblocking in start_server. That
346 * means we're in a sticky situation now: there's no way to
347 * convey errors to the client. */
349 /* FIXME: Hold off on reporting option processing errors until
350 * we've set up nonblocking and multiplexed IO and can get the
351 * message back to them. */
354 exit_cleanup(RERR_UNSUPPORTED);
358 extern int io_timeout;
359 io_timeout = lp_timeout(i);
362 start_server(fd, fd, argc, argp);
367 /* send a list of available modules to the client. Don't list those
368 with "list = False". */
369 static void send_listing(int fd)
371 int n = lp_numservices();
376 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
379 /* this is called when a socket connection is established to a client
380 and we want to start talking. The setup of the system is done from
382 static int start_daemon(int fd)
387 extern char *config_file;
388 extern int remote_version;
390 if (!lp_load(config_file, 0)) {
391 exit_cleanup(RERR_SYNTAX);
394 set_socket_options(fd,"SO_KEEPALIVE");
395 set_socket_options(fd,lp_socket_options());
398 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
400 motd = lp_motd_file();
402 FILE *f = fopen(motd,"r");
403 while (f && !feof(f)) {
404 int len = fread(line, 1, sizeof(line)-1, f);
407 io_printf(fd,"%s", line);
414 if (!read_line(fd, line, sizeof(line)-1)) {
418 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
419 io_printf(fd,"@ERROR: protocol startup error\n");
425 if (!read_line(fd, line, sizeof(line)-1)) {
429 if (!*line || strcmp(line,"#list")==0) {
435 /* it's some sort of command that I don't understand */
436 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
442 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
447 return rsync_module(fd, i);
451 int daemon_main(void)
453 extern char *config_file;
454 extern int orig_umask;
457 if (is_a_socket(STDIN_FILENO)) {
460 /* we are running via inetd - close off stdout and
461 stderr so that library functions (and getopt) don't
462 try to use them. Redirect them to /dev/null */
465 open("/dev/null", O_RDWR);
468 return start_daemon(STDIN_FILENO);
473 if (!lp_load(config_file, 1)) {
474 exit_cleanup(RERR_SYNTAX);
479 rprintf(FINFO, "rsyncd version %s starting, listening on port %d\n", VERSION,
481 /* TODO: If listening on a particular address, then show that
484 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
487 int pid = (int) getpid();
488 cleanup_set_pid(pid);
489 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
490 0666 & ~orig_umask)) == -1) {
492 rsyserr(FLOG, errno, "failed to create pid file %s", pid_file);
493 exit_cleanup(RERR_FILEIO);
495 slprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
496 write(fd, pidbuf, strlen(pidbuf));
500 start_accept_loop(rsync_port, start_daemon);