Made a couple sentences about rsync:// syntax clearer.
[rsync/rsync.git] / clientserver.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux"; -*-
2 *
3 * Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4 * Copyright (C) 2001-2002 by Martin Pool <mbp@samba.org>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21/**
22 * @file
23 *
24 * The socket based protocol for setting up a connection with
25 * rsyncd.
26 **/
27
28#include "rsync.h"
29
30extern int verbose;
31extern int list_only;
32extern int am_sender;
33extern int am_server;
34extern int am_daemon;
35extern int am_root;
36extern int rsync_port;
37extern int kluge_around_eof;
38extern int daemon_over_rsh;
39extern int sanitize_paths;
40extern int filesfrom_fd;
41extern int remote_protocol;
42extern int protocol_version;
43extern int io_timeout;
44extern int select_timeout;
45extern int orig_umask;
46extern int no_detach;
47extern int default_af_hint;
48extern char *bind_address;
49extern struct filter_list_struct server_filter_list;
50extern char *config_file;
51extern char *files_from;
52
53char *auth_user;
54int read_only = 0;
55int daemon_log_format_has_i = 0;
56int daemon_log_format_has_o_or_i = 0;
57int module_id = -1;
58
59/* Length of lp_path() string when in daemon mode & not chrooted, else 0. */
60unsigned int module_dirlen = 0;
61
62/**
63 * Run a client connected to an rsyncd. The alternative to this
64 * function for remote-shell connections is do_cmd().
65 *
66 * After negotiating which module to use and reading the server's
67 * motd, this hands over to client_run(). Telling the server the
68 * module will cause it to chroot/setuid/etc.
69 *
70 * Instead of doing a transfer, the client may at this stage instead
71 * get a listing of remote modules and exit.
72 *
73 * @return -1 for error in startup, or the result of client_run().
74 * Either way, it eventually gets passed to exit_cleanup().
75 **/
76int start_socket_client(char *host, char *path, int argc, char *argv[])
77{
78 int fd, ret;
79 char *p, *user = NULL;
80
81 /* This is redundant with code in start_inband_exchange(), but this
82 * short-circuits a problem in the client before we open a socket,
83 * and the extra check won't hurt. */
84 if (*path == '/') {
85 rprintf(FERROR,
86 "ERROR: The remote path must start with a module name not a /\n");
87 return -1;
88 }
89
90 if ((p = strchr(host, '@')) != NULL) {
91 user = host;
92 host = p+1;
93 *p = '\0';
94 }
95
96 if (rsync_port == 0)
97 rsync_port = RSYNC_PORT;
98
99 fd = open_socket_out_wrapped(host, rsync_port, bind_address,
100 default_af_hint);
101 if (fd == -1)
102 exit_cleanup(RERR_SOCKETIO);
103
104 ret = start_inband_exchange(user, path, fd, fd, argc);
105
106 return ret < 0? ret : client_run(fd, fd, -1, argc, argv);
107}
108
109int start_inband_exchange(char *user, char *path, int f_in, int f_out,
110 int argc)
111{
112 int i;
113 char *sargs[MAX_ARGS];
114 int sargc = 0;
115 char line[MAXPATHLEN];
116 char *p;
117
118 if (argc == 0 && !am_sender)
119 list_only |= 1;
120
121 if (*path == '/') {
122 rprintf(FERROR,
123 "ERROR: The remote path must start with a module name\n");
124 return -1;
125 }
126
127 if (!user)
128 user = getenv("USER");
129 if (!user)
130 user = getenv("LOGNAME");
131
132 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
133
134 if (!read_line(f_in, line, sizeof line - 1)) {
135 rprintf(FERROR, "rsync: did not see server greeting\n");
136 return -1;
137 }
138
139 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
140 /* note that read_line strips of \n or \r */
141 rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n",
142 line);
143 return -1;
144 }
145 if (protocol_version > remote_protocol)
146 protocol_version = remote_protocol;
147
148 if (list_only && protocol_version >= 29)
149 list_only |= 2;
150
151 /* set daemon_over_rsh to false since we need to build the
152 * true set of args passed through the rsh/ssh connection;
153 * this is a no-op for direct-socket-connection mode */
154 daemon_over_rsh = 0;
155 server_options(sargs, &sargc);
156
157 sargs[sargc++] = ".";
158
159 if (path && *path)
160 sargs[sargc++] = path;
161
162 sargs[sargc] = NULL;
163
164 if (verbose > 1)
165 print_child_argv(sargs);
166
167 p = strchr(path,'/');
168 if (p) *p = 0;
169 io_printf(f_out, "%s\n", path);
170 if (p) *p = '/';
171
172 /* Old servers may just drop the connection here,
173 rather than sending a proper EXIT command. Yuck. */
174 kluge_around_eof = list_only && protocol_version < 25 ? 1 : 0;
175
176 while (1) {
177 if (!read_line(f_in, line, sizeof line - 1)) {
178 rprintf(FERROR, "rsync: didn't get server startup line\n");
179 return -1;
180 }
181
182 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
183 auth_client(f_out, user, line+18);
184 continue;
185 }
186
187 if (strcmp(line,"@RSYNCD: OK") == 0)
188 break;
189
190 if (strcmp(line,"@RSYNCD: EXIT") == 0) {
191 /* This is sent by recent versions of the
192 * server to terminate the listing of modules.
193 * We don't want to go on and transfer
194 * anything; just exit. */
195 exit(0);
196 }
197
198 if (strncmp(line, "@ERROR", 6) == 0) {
199 rprintf(FERROR, "%s\n", line);
200 /* This is always fatal; the server will now
201 * close the socket. */
202 return RERR_STARTCLIENT;
203 } else {
204 rprintf(FINFO,"%s\n", line);
205 }
206 }
207 kluge_around_eof = 0;
208
209 for (i = 0; i < sargc; i++) {
210 io_printf(f_out, "%s\n", sargs[i]);
211 }
212 io_printf(f_out, "\n");
213
214 if (protocol_version < 23) {
215 if (protocol_version == 22 || !am_sender)
216 io_start_multiplex_in();
217 }
218
219 return 0;
220}
221
222
223
224static int rsync_module(int f_in, int f_out, int i)
225{
226 int argc = 0;
227 int maxargs;
228 char **argv;
229 char **argp;
230 char line[MAXPATHLEN];
231 uid_t uid = (uid_t)-2; /* canonically "nobody" */
232 gid_t gid = (gid_t)-2;
233 char *p;
234 char *addr = client_addr(f_in);
235 char *host = client_name(f_in);
236 char *name = lp_name(i);
237 int use_chroot = lp_use_chroot(i);
238 int start_glob = 0;
239 int ret;
240 char *request = NULL;
241
242 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
243 rprintf(FLOG, "rsync denied on module %s from %s (%s)\n",
244 name, host, addr);
245 if (!lp_list(i))
246 io_printf(f_out, "@ERROR: Unknown module '%s'\n", name);
247 else {
248 io_printf(f_out,
249 "@ERROR: access denied to %s from %s (%s)\n",
250 name, host, addr);
251 }
252 return -1;
253 }
254
255 if (am_daemon && am_server) {
256 rprintf(FLOG, "rsync allowed access on module %s from %s (%s)\n",
257 name, host, addr);
258 }
259
260 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
261 if (errno) {
262 rsyserr(FLOG, errno, "failed to open lock file %s",
263 safe_fname(lp_lock_file(i)));
264 io_printf(f_out, "@ERROR: failed to open lock file\n");
265 } else {
266 rprintf(FLOG, "max connections (%d) reached\n",
267 lp_max_connections(i));
268 io_printf(f_out, "@ERROR: max connections (%d) reached -- try again later\n",
269 lp_max_connections(i));
270 }
271 return -1;
272 }
273
274 auth_user = auth_server(f_in, f_out, i, addr, "@RSYNCD: AUTHREQD ");
275
276 if (!auth_user) {
277 rprintf(FLOG, "auth failed on module %s from %s (%s)\n",
278 name, host, addr);
279 io_printf(f_out, "@ERROR: auth failed on module %s\n", name);
280 return -1;
281 }
282
283 module_id = i;
284
285 if (lp_read_only(i))
286 read_only = 1;
287
288 if (lp_transfer_logging(i)) {
289 if (strstr(lp_log_format(i), "%i") != NULL)
290 daemon_log_format_has_i = 1;
291 if (daemon_log_format_has_i
292 || strstr(lp_log_format(i), "%o") != NULL)
293 daemon_log_format_has_o_or_i = 1;
294 }
295
296 am_root = (MY_UID() == 0);
297
298 if (am_root) {
299 p = lp_uid(i);
300 if (!name_to_uid(p, &uid)) {
301 if (!isdigit(*(unsigned char *)p)) {
302 rprintf(FLOG, "Invalid uid %s\n", p);
303 io_printf(f_out, "@ERROR: invalid uid %s\n", p);
304 return -1;
305 }
306 uid = atoi(p);
307 }
308
309 p = lp_gid(i);
310 if (!name_to_gid(p, &gid)) {
311 if (!isdigit(*(unsigned char *)p)) {
312 rprintf(FLOG, "Invalid gid %s\n", p);
313 io_printf(f_out, "@ERROR: invalid gid %s\n", p);
314 return -1;
315 }
316 gid = atoi(p);
317 }
318 }
319
320 /* TODO: If we're not root, but the configuration requests
321 * that we change to some uid other than the current one, then
322 * log a warning. */
323
324 /* TODO: Perhaps take a list of gids, and make them into the
325 * supplementary groups. */
326
327 if (use_chroot) {
328 module_dirlen = 0;
329 set_filter_dir("/", 1);
330 } else {
331 module_dirlen = strlen(lp_path(i));
332 set_filter_dir(lp_path(i), module_dirlen);
333 }
334
335 p = lp_filter(i);
336 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
337 XFLG_ANCHORED2ABS);
338
339 p = lp_include_from(i);
340 parse_filter_file(&server_filter_list, p, MATCHFLG_INCLUDE,
341 XFLG_ANCHORED2ABS | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
342
343 p = lp_include(i);
344 parse_rule(&server_filter_list, p,
345 MATCHFLG_INCLUDE | MATCHFLG_WORD_SPLIT,
346 XFLG_ANCHORED2ABS | XFLG_OLD_PREFIXES);
347
348 p = lp_exclude_from(i);
349 parse_filter_file(&server_filter_list, p, 0,
350 XFLG_ANCHORED2ABS | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
351
352 p = lp_exclude(i);
353 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
354 XFLG_ANCHORED2ABS | XFLG_OLD_PREFIXES);
355
356 log_init();
357
358 if (use_chroot) {
359 /*
360 * XXX: The 'use chroot' flag is a fairly reliable
361 * source of confusion, because it fails under two
362 * important circumstances: running as non-root,
363 * running on Win32 (or possibly others). On the
364 * other hand, if you are running as root, then it
365 * might be better to always use chroot.
366 *
367 * So, perhaps if we can't chroot we should just issue
368 * a warning, unless a "require chroot" flag is set,
369 * in which case we fail.
370 */
371 if (chroot(lp_path(i))) {
372 rsyserr(FLOG, errno, "chroot %s failed",
373 safe_fname(lp_path(i)));
374 io_printf(f_out, "@ERROR: chroot failed\n");
375 return -1;
376 }
377
378 if (!push_dir("/")) {
379 rsyserr(FLOG, errno, "chdir %s failed\n",
380 safe_fname(lp_path(i)));
381 io_printf(f_out, "@ERROR: chdir failed\n");
382 return -1;
383 }
384
385 } else {
386 if (!push_dir(lp_path(i))) {
387 rsyserr(FLOG, errno, "chdir %s failed\n",
388 safe_fname(lp_path(i)));
389 io_printf(f_out, "@ERROR: chdir failed\n");
390 return -1;
391 }
392 sanitize_paths = 1;
393 }
394
395 if (am_root) {
396 /* XXXX: You could argue that if the daemon is started
397 * by a non-root user and they explicitly specify a
398 * gid, then we should try to change to that gid --
399 * this could be possible if it's already in their
400 * supplementary groups. */
401
402 /* TODO: Perhaps we need to document that if rsyncd is
403 * started by somebody other than root it will inherit
404 * all their supplementary groups. */
405
406 if (setgid(gid)) {
407 rsyserr(FLOG, errno, "setgid %d failed", (int)gid);
408 io_printf(f_out, "@ERROR: setgid failed\n");
409 return -1;
410 }
411#ifdef HAVE_SETGROUPS
412 /* Get rid of any supplementary groups this process
413 * might have inheristed. */
414 if (setgroups(1, &gid)) {
415 rsyserr(FLOG, errno, "setgroups failed");
416 io_printf(f_out, "@ERROR: setgroups failed\n");
417 return -1;
418 }
419#endif
420
421 if (setuid(uid)) {
422 rsyserr(FLOG, errno, "setuid %d failed", (int)uid);
423 io_printf(f_out, "@ERROR: setuid failed\n");
424 return -1;
425 }
426
427 am_root = (MY_UID() == 0);
428 }
429
430 io_printf(f_out, "@RSYNCD: OK\n");
431
432 maxargs = MAX_ARGS;
433 if (!(argv = new_array(char *, maxargs)))
434 out_of_memory("rsync_module");
435 argv[argc++] = "rsyncd";
436
437 while (1) {
438 if (!read_line(f_in, line, sizeof line - 1))
439 return -1;
440
441 if (!*line)
442 break;
443
444 p = line;
445
446 if (argc == maxargs) {
447 maxargs += MAX_ARGS;
448 if (!(argv = realloc_array(argv, char *, maxargs)))
449 out_of_memory("rsync_module");
450 }
451 if (!(argv[argc] = strdup(p)))
452 out_of_memory("rsync_module");
453
454 if (start_glob) {
455 if (start_glob == 1) {
456 request = strdup(p);
457 start_glob++;
458 }
459 glob_expand(name, &argv, &argc, &maxargs);
460 } else
461 argc++;
462
463 if (strcmp(line, ".") == 0)
464 start_glob = 1;
465 }
466
467 verbose = 0; /* future verbosity is controlled by client options */
468 argp = argv;
469 ret = parse_arguments(&argc, (const char ***) &argp, 0);
470
471 if (filesfrom_fd == 0)
472 filesfrom_fd = f_in;
473
474 if (request) {
475 if (*auth_user) {
476 rprintf(FLOG, "rsync %s %s from %s@%s (%s)\n",
477 am_sender ? "on" : "to",
478 request, auth_user, host, addr);
479 } else {
480 rprintf(FLOG, "rsync %s %s from %s (%s)\n",
481 am_sender ? "on" : "to",
482 request, host, addr);
483 }
484 free(request);
485 }
486
487#ifndef DEBUG
488 /* don't allow the logs to be flooded too fast */
489 if (verbose > lp_max_verbosity())
490 verbose = lp_max_verbosity();
491#endif
492
493 if (protocol_version < 23
494 && (protocol_version == 22 || am_sender))
495 io_start_multiplex_out();
496 else if (!ret) {
497 /* We have to get I/O multiplexing started so that we can
498 * get the error back to the client. This means getting
499 * the protocol setup finished first in later versions. */
500 setup_protocol(f_out, f_in);
501 if (files_from && !am_sender && strcmp(files_from, "-") != 0)
502 write_byte(f_out, 0);
503 io_start_multiplex_out();
504 }
505
506 if (!ret) {
507 option_error();
508 msleep(400);
509 exit_cleanup(RERR_UNSUPPORTED);
510 }
511
512 if (lp_timeout(i)) {
513 io_timeout = lp_timeout(i);
514 if (io_timeout < select_timeout)
515 select_timeout = io_timeout;
516 }
517
518 start_server(f_in, f_out, argc, argp);
519
520 return 0;
521}
522
523/* send a list of available modules to the client. Don't list those
524 with "list = False". */
525static void send_listing(int fd)
526{
527 int n = lp_numservices();
528 int i;
529
530 for (i = 0; i < n; i++) {
531 if (lp_list(i))
532 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
533 }
534
535 if (protocol_version >= 25)
536 io_printf(fd,"@RSYNCD: EXIT\n");
537}
538
539/* this is called when a connection is established to a client
540 and we want to start talking. The setup of the system is done from
541 here */
542int start_daemon(int f_in, int f_out)
543{
544 char line[200];
545 char *motd;
546 int i;
547
548 io_set_sock_fds(f_in, f_out);
549
550 if (!lp_load(config_file, 0))
551 exit_cleanup(RERR_SYNTAX);
552
553 log_init();
554
555 if (!am_server) {
556 set_socket_options(f_in, "SO_KEEPALIVE");
557 set_socket_options(f_in, lp_socket_options());
558 set_nonblocking(f_in);
559 }
560
561 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
562
563 motd = lp_motd_file();
564 if (motd && *motd) {
565 FILE *f = fopen(motd,"r");
566 while (f && !feof(f)) {
567 int len = fread(line, 1, sizeof line - 1, f);
568 if (len > 0) {
569 line[len] = 0;
570 io_printf(f_out, "%s", line);
571 }
572 }
573 if (f)
574 fclose(f);
575 io_printf(f_out, "\n");
576 }
577
578 if (!read_line(f_in, line, sizeof line - 1))
579 return -1;
580
581 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
582 io_printf(f_out, "@ERROR: protocol startup error\n");
583 return -1;
584 }
585 if (protocol_version > remote_protocol)
586 protocol_version = remote_protocol;
587
588 line[0] = 0;
589 if (!read_line(f_in, line, sizeof line - 1))
590 return -1;
591
592 if (!*line || strcmp(line, "#list") == 0) {
593 send_listing(f_out);
594 return -1;
595 }
596
597 if (*line == '#') {
598 /* it's some sort of command that I don't understand */
599 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
600 return -1;
601 }
602
603 if ((i = lp_number(line)) < 0) {
604 char *addr = client_addr(f_in);
605 char *host = client_name(f_in);
606 rprintf(FLOG, "unknown module '%s' tried from %s (%s)\n",
607 line, host, addr);
608 io_printf(f_out, "@ERROR: Unknown module '%s'\n", line);
609 return -1;
610 }
611
612 return rsync_module(f_in, f_out, i);
613}
614
615
616int daemon_main(void)
617{
618 char *pid_file;
619
620 if (is_a_socket(STDIN_FILENO)) {
621 int i;
622
623 /* we are running via inetd - close off stdout and
624 * stderr so that library functions (and getopt) don't
625 * try to use them. Redirect them to /dev/null */
626 for (i = 1; i < 3; i++) {
627 close(i);
628 open("/dev/null", O_RDWR);
629 }
630
631 return start_daemon(STDIN_FILENO, STDIN_FILENO);
632 }
633
634 if (!no_detach)
635 become_daemon();
636
637 if (!lp_load(config_file, 1))
638 exit_cleanup(RERR_SYNTAX);
639
640 if (rsync_port == 0 && (rsync_port = lp_rsync_port()) == 0)
641 rsync_port = RSYNC_PORT;
642 if (bind_address == NULL && *lp_bind_address())
643 bind_address = lp_bind_address();
644
645 log_init();
646
647 rprintf(FLOG, "rsyncd version %s starting, listening on port %d\n",
648 RSYNC_VERSION, rsync_port);
649 /* TODO: If listening on a particular address, then show that
650 * address too. In fact, why not just do inet_ntop on the
651 * local address??? */
652
653 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
654 char pidbuf[16];
655 int fd;
656 pid_t pid = getpid();
657 cleanup_set_pid(pid);
658 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
659 0666 & ~orig_umask)) == -1) {
660 cleanup_set_pid(0);
661 rsyserr(FLOG, errno, "failed to create pid file %s",
662 safe_fname(pid_file));
663 exit_cleanup(RERR_FILEIO);
664 }
665 snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
666 write(fd, pidbuf, strlen(pidbuf));
667 close(fd);
668 }
669
670 start_accept_loop(rsync_port, start_daemon);
671 return -1;
672}