Doc.
[rsync/rsync.git] / clientserver.c
CommitLineData
a039749b
MP
1/* -*- c-file-style: "linux"; -*-
2
15b7b73d
MP
3 Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4 Copyright (C) 2001 by Martin Pool <mbp@samba.org>
3591c066
AT
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
bc363ea9 21/* the socket based protocol for setting up a connection with rsyncd */
3591c066
AT
22
23#include "rsync.h"
24
25extern int module_id;
26extern int read_only;
27extern int verbose;
28extern int rsync_port;
97cb8dc2 29char *auth_user;
cb13abfe 30int sanitize_paths = 0;
3591c066 31
bc363ea9
MP
32/*
33 * Run a client connected to an rsyncd. The alternative to this
34 * function for remote-shell connections is do_cmd.
35 */
3591c066
AT
36int start_socket_client(char *host, char *path, int argc, char *argv[])
37{
38 int fd, i;
39 char *sargs[MAX_ARGS];
40 int sargc=0;
e42c9458 41 char line[MAXPATHLEN];
bcb7e502 42 char *p, *user=NULL;
13c5fc0e 43 extern int remote_version;
8d9dc9f9 44 extern int am_sender;
bc363ea9 45 extern char *shell_cmd;
7a55d06e 46 extern int kludge_around_eof;
06963d0f 47 extern char *bind_address;
13e29995 48 extern int default_af_hint;
06963d0f 49
27e3e9c9
AT
50 if (argc == 0 && !am_sender) {
51 extern int list_only;
52 list_only = 1;
53 }
54
bc363ea9
MP
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
60 */
61 if (shell_cmd) {
62 rprintf(FERROR, "WARNING: --rsh or -e option ignored when "
63 "connecting to rsync daemon\n");
64 /* continue */
65 }
66
f98df1d9 67 if (*path == '/') {
ff81e809 68 rprintf(FERROR,"ERROR: The remote path must start with a module name not a /\n");
f98df1d9
AT
69 return -1;
70 }
71
bcb7e502
AT
72 p = strchr(host, '@');
73 if (p) {
74 user = host;
75 host = p+1;
76 *p = 0;
77 }
78
79 if (!user) user = getenv("USER");
80 if (!user) user = getenv("LOGNAME");
81
1bbd10fe 82 if (verbose >= 2) {
604f343c
MP
83 /* FIXME: If we're going to use a socket program for
84 * testing, then this message is wrong. We need to
85 * say something like "(except really using %s)" */
1264288c
MP
86 rprintf(FINFO, "opening tcp connection to %s port %d\n",
87 host, rsync_port);
1bbd10fe 88 }
d5d4b282 89 fd = open_socket_out_wrapped (host, rsync_port, bind_address,
13e29995 90 default_af_hint);
3591c066 91 if (fd == -1) {
65417579 92 exit_cleanup(RERR_SOCKETIO);
3591c066
AT
93 }
94
95 server_options(sargs,&sargc);
96
97 sargs[sargc++] = ".";
98
99 if (path && *path)
100 sargs[sargc++] = path;
101
102 sargs[sargc] = NULL;
103
91eee594
AT
104 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
105
3591c066
AT
106 if (!read_line(fd, line, sizeof(line)-1)) {
107 return -1;
108 }
109
13c5fc0e 110 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
3591c066
AT
111 return -1;
112 }
113
2c91d3d3
AT
114 p = strchr(path,'/');
115 if (p) *p = 0;
116 io_printf(fd,"%s\n",path);
117 if (p) *p = '/';
118
063393d6
MP
119 /* Old servers may just drop the connection here,
120 rather than sending a proper EXIT command. Yuck. */
121 kludge_around_eof = remote_version < 25;
7a55d06e 122
063393d6 123 while (1) {
3591c066
AT
124 if (!read_line(fd, line, sizeof(line)-1)) {
125 return -1;
126 }
31593dd6 127
31593dd6 128 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
bcb7e502 129 auth_client(fd, user, line+18);
31593dd6
AT
130 continue;
131 }
31593dd6 132
3591c066 133 if (strcmp(line,"@RSYNCD: OK") == 0) break;
8f04bb36
AT
134
135 if (strcmp(line,"@RSYNCD: EXIT") == 0) exit(0);
136
c613d370
DD
137 if (strncmp(line, "@ERROR", 6) == 0)
138 rprintf(FERROR,"%s\n", line);
139 else
140 rprintf(FINFO,"%s\n", line);
3591c066 141 }
7a55d06e 142 kludge_around_eof = False;
3591c066
AT
143
144 for (i=0;i<sargc;i++) {
145 io_printf(fd,"%s\n", sargs[i]);
146 }
147 io_printf(fd,"\n");
148
09b7f5db
AT
149 if (remote_version < 23) {
150 if (remote_version == 22 || (remote_version > 17 && !am_sender))
151 io_start_multiplex_in(fd);
152 }
8d9dc9f9 153
3591c066
AT
154 return client_run(fd, fd, -1, argc, argv);
155}
156
157
158
159static int rsync_module(int fd, int i)
160{
161 int argc=0;
162 char *argv[MAX_ARGS];
163 char **argp;
e42c9458 164 char line[MAXPATHLEN];
2af27ad9 165 uid_t uid = (uid_t)-2; /* canonically "nobody" */
1a016bfd 166 gid_t gid = (gid_t)-2;
8ef4ffd6 167 char *p;
56c473b7
AT
168 char *addr = client_addr(fd);
169 char *host = client_name(fd);
874895d5 170 char *name = lp_name(i);
8638dd48 171 int use_chroot = lp_use_chroot(i);
874895d5 172 int start_glob=0;
41979ff8 173 int ret;
7b372642
AT
174 char *request=NULL;
175 extern int am_sender;
8d9dc9f9 176 extern int remote_version;
943882a2 177 extern int am_root;
56c473b7
AT
178
179 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
180 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
31ec50d7 181 name, host, addr);
c8e78d87 182 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
31ec50d7 183 name, host, addr);
56c473b7
AT
184 return -1;
185 }
3591c066 186
5e71c444 187 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
8d72ef6e
AT
188 if (errno) {
189 rprintf(FERROR,"failed to open lock file %s : %s\n",
5e71c444 190 lp_lock_file(i), strerror(errno));
8d72ef6e 191 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
5e71c444 192 lp_lock_file(i), strerror(errno));
8d72ef6e
AT
193 } else {
194 rprintf(FERROR,"max connections (%d) reached\n",
5e71c444
AT
195 lp_max_connections(i));
196 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
8d72ef6e 197 }
0c515f17
AT
198 return -1;
199 }
200
31593dd6 201
97cb8dc2 202 auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
d0d56395 203
97cb8dc2 204 if (!auth_user) {
d0d56395
AT
205 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
206 name, client_name(fd), client_addr(fd));
207 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
208 return -1;
209 }
210
3591c066
AT
211 module_id = i;
212
716baed7 213 am_root = (getuid() == 0);
8ef4ffd6 214
716baed7
DD
215 if (am_root) {
216 p = lp_uid(i);
217 if (!name_to_uid(p, &uid)) {
218 if (!isdigit(*p)) {
219 rprintf(FERROR,"Invalid uid %s\n", p);
fd2dd2aa 220 io_printf(fd,"@ERROR: invalid uid %s\n", p);
716baed7
DD
221 return -1;
222 }
223 uid = atoi(p);
224 }
225
226 p = lp_gid(i);
227 if (!name_to_gid(p, &gid)) {
228 if (!isdigit(*p)) {
229 rprintf(FERROR,"Invalid gid %s\n", p);
fd2dd2aa 230 io_printf(fd,"@ERROR: invalid gid %s\n", p);
716baed7
DD
231 return -1;
232 }
233 gid = atoi(p);
234 }
8ef4ffd6 235 }
3b2b5345
MP
236
237 /* TODO: If we're not root, but the configuration requests
238 * that we change to some uid other than the current one, then
239 * log a warning. */
240
241 /* TODO: Perhaps take a list of gids, and make them into the
242 * supplementary groups. */
8ef4ffd6 243
cd64343a
DD
244 p = lp_include_from(i);
245 add_exclude_file(p, 1, 1);
246
247 p = lp_include(i);
248 add_include_line(p);
249
8f3a2d54 250 p = lp_exclude_from(i);
2b6b4d53 251 add_exclude_file(p, 1, 0);
8f3a2d54 252
5805327b 253 p = lp_exclude(i);
8f3a2d54
AT
254 add_exclude_line(p);
255
45a83540 256 log_init();
1a016bfd 257
8638dd48 258 if (use_chroot) {
b52c1d9d
MP
259 /*
260 * XXX: The 'use chroot' flag is a fairly reliable
261 * source of confusion, because it fails under two
262 * important circumstances: running as non-root,
263 * running on Win32 (or possibly others). On the
264 * other hand, if you are running as root, then it
265 * might be better to always use chroot.
266 *
267 * So, perhaps if we can't chroot we should just issue
268 * a warning, unless a "require chroot" flag is set,
269 * in which case we fail.
270 */
8638dd48 271 if (chroot(lp_path(i))) {
a039749b 272 rsyserr(FERROR, errno, "chroot %s failed", lp_path(i));
8638dd48
DD
273 io_printf(fd,"@ERROR: chroot failed\n");
274 return -1;
275 }
3591c066 276
c226b7c2 277 if (!push_dir("/", 0)) {
a039749b 278 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
8638dd48
DD
279 io_printf(fd,"@ERROR: chdir failed\n");
280 return -1;
281 }
3591c066 282
716baed7
DD
283 } else {
284 if (!push_dir(lp_path(i), 0)) {
a039749b 285 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
716baed7
DD
286 io_printf(fd,"@ERROR: chdir failed\n");
287 return -1;
288 }
cb13abfe 289 sanitize_paths = 1;
716baed7
DD
290 }
291
292 if (am_root) {
e3bdb763 293#ifdef HAVE_SETGROUPS
715d1f45
MP
294 /* Get rid of any supplementary groups this process
295 * might have inheristed. */
296 if (setgroups(0, NULL)) {
297 rsyserr(FERROR, errno, "setgroups failed");
298 io_printf(fd, "@ERROR: setgroups failed\n");
299 return -1;
300 }
e3bdb763 301#endif
715d1f45
MP
302
303 /* XXXX: You could argue that if the daemon is started
304 * by a non-root user and they explicitly specify a
305 * gid, then we should try to change to that gid --
306 * this could be possible if it's already in their
307 * supplementary groups. */
308
309 /* TODO: Perhaps we need to document that if rsyncd is
310 * started by somebody other than root it will inherit
311 * all their supplementary groups. */
312
716baed7 313 if (setgid(gid)) {
08a740ff 314 rsyserr(FERROR, errno, "setgid %d failed", (int) gid);
8638dd48
DD
315 io_printf(fd,"@ERROR: setgid failed\n");
316 return -1;
317 }
3591c066 318
716baed7 319 if (setuid(uid)) {
08a740ff 320 rsyserr(FERROR, errno, "setuid %d failed", (int) uid);
8638dd48
DD
321 io_printf(fd,"@ERROR: setuid failed\n");
322 return -1;
323 }
324
716baed7 325 am_root = (getuid() == 0);
3591c066
AT
326 }
327
328 io_printf(fd,"@RSYNCD: OK\n");
329
330 argv[argc++] = "rsyncd";
331
332 while (1) {
333 if (!read_line(fd, line, sizeof(line)-1)) {
334 return -1;
335 }
336
337 if (!*line) break;
338
874895d5
AT
339 p = line;
340
874895d5 341 argv[argc] = strdup(p);
3591c066
AT
342 if (!argv[argc]) {
343 return -1;
344 }
345
874895d5 346 if (start_glob) {
1a016bfd 347 if (start_glob == 1) {
7b372642 348 request = strdup(p);
1a016bfd
AT
349 start_glob++;
350 }
cb13abfe 351 glob_expand(name, argv, &argc, MAX_ARGS);
874895d5
AT
352 } else {
353 argc++;
354 }
355
356 if (strcmp(line,".") == 0) {
357 start_glob = 1;
358 }
359
3591c066
AT
360 if (argc == MAX_ARGS) {
361 return -1;
362 }
363 }
364
cb13abfe 365 if (sanitize_paths) {
8638dd48
DD
366 /*
367 * Note that this is applied to all parameters, whether or not
368 * they are filenames, but no other legal parameters contain
369 * the forms that need to be sanitized so it doesn't hurt;
370 * it is not known at this point which parameters are files
371 * and which aren't.
372 */
373 for (i = 1; i < argc; i++) {
cb13abfe 374 sanitize_path(argv[i], NULL);
8638dd48
DD
375 }
376 }
377
15b7b73d
MP
378 argp = argv;
379 ret = parse_arguments(&argc, (const char ***) &argp, 0);
3591c066 380
7b372642 381 if (request) {
97cb8dc2 382 if (*auth_user) {
d0d56395
AT
383 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
384 am_sender?"on":"to",
97cb8dc2 385 request, auth_user, host, addr);
d0d56395
AT
386 } else {
387 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
388 am_sender?"on":"to",
389 request, host, addr);
390 }
7b372642
AT
391 free(request);
392 }
393
15b7b73d 394#ifndef DEBUG
3591c066
AT
395 /* don't allow the logs to be flooded too fast */
396 if (verbose > 1) verbose = 1;
0199b05f 397#endif
3591c066 398
09b7f5db
AT
399 if (remote_version < 23) {
400 if (remote_version == 22 || (remote_version > 17 && am_sender))
401 io_start_multiplex_out(fd);
554e0a8d 402 }
15b7b73d
MP
403
404 /* For later protocol versions, we don't start multiplexing
405 * until we've configured nonblocking in start_server. That
406 * means we're in a sticky situation now: there's no way to
407 * convey errors to the client. */
408
409 /* FIXME: Hold off on reporting option processing errors until
410 * we've set up nonblocking and multiplexed IO and can get the
411 * message back to them. */
41979ff8 412 if (!ret) {
15b7b73d
MP
413 option_error();
414 exit_cleanup(RERR_UNSUPPORTED);
41979ff8
AT
415 }
416
81791cfc
AT
417 if (lp_timeout(i)) {
418 extern int io_timeout;
419 io_timeout = lp_timeout(i);
420 }
421
3591c066
AT
422 start_server(fd, fd, argc, argp);
423
424 return 0;
425}
426
7a6421fa
AT
427/* send a list of available modules to the client. Don't list those
428 with "list = False". */
3591c066
AT
429static void send_listing(int fd)
430{
431 int n = lp_numservices();
432 int i;
063393d6
MP
433 extern int remote_version;
434
3591c066
AT
435 for (i=0;i<n;i++)
436 if (lp_list(i))
437 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
8f04bb36 438
063393d6
MP
439 if (remote_version >= 25)
440 io_printf(fd,"@RSYNCD: EXIT\n");
3591c066
AT
441}
442
443/* this is called when a socket connection is established to a client
444 and we want to start talking. The setup of the system is done from
445 here */
446static int start_daemon(int fd)
447{
7a6421fa 448 char line[200];
3591c066 449 char *motd;
8ef4ffd6 450 int i = -1;
4cdf25e4 451 extern char *config_file;
13c5fc0e 452 extern int remote_version;
4cdf25e4 453
f9e940ef 454 if (!lp_load(config_file, 0)) {
65417579 455 exit_cleanup(RERR_SYNTAX);
4cdf25e4 456 }
3591c066
AT
457
458 set_socket_options(fd,"SO_KEEPALIVE");
a6801c39 459 set_socket_options(fd,lp_socket_options());
f0359dd0 460 set_nonblocking(fd);
3591c066
AT
461
462 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
463
464 motd = lp_motd_file();
27d3cdbc 465 if (motd && *motd) {
3591c066
AT
466 FILE *f = fopen(motd,"r");
467 while (f && !feof(f)) {
468 int len = fread(line, 1, sizeof(line)-1, f);
469 if (len > 0) {
470 line[len] = 0;
471 io_printf(fd,"%s", line);
472 }
473 }
474 if (f) fclose(f);
475 io_printf(fd,"\n");
476 }
477
91eee594
AT
478 if (!read_line(fd, line, sizeof(line)-1)) {
479 return -1;
480 }
481
482 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
c8e78d87 483 io_printf(fd,"@ERROR: protocol startup error\n");
91eee594
AT
484 return -1;
485 }
486
8ef4ffd6 487 while (i == -1) {
3591c066
AT
488 line[0] = 0;
489 if (!read_line(fd, line, sizeof(line)-1)) {
490 return -1;
491 }
492
493 if (!*line || strcmp(line,"#list")==0) {
494 send_listing(fd);
495 return -1;
496 }
497
498 if (*line == '#') {
499 /* it's some sort of command that I don't understand */
c8e78d87 500 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
3591c066
AT
501 return -1;
502 }
503
504 i = lp_number(line);
505 if (i == -1) {
c8e78d87 506 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
3591c066
AT
507 return -1;
508 }
3591c066
AT
509 }
510
8ef4ffd6 511 return rsync_module(fd, i);
3591c066
AT
512}
513
514
515int daemon_main(void)
516{
f9e940ef 517 extern char *config_file;
ad517ce5 518 extern int orig_umask;
8638dd48 519 char *pid_file;
13e29995 520 extern int no_detach;
1a016bfd 521
3591c066 522 if (is_a_socket(STDIN_FILENO)) {
41979ff8
AT
523 int i;
524
525 /* we are running via inetd - close off stdout and
526 stderr so that library functions (and getopt) don't
527 try to use them. Redirect them to /dev/null */
528 for (i=1;i<3;i++) {
529 close(i);
530 open("/dev/null", O_RDWR);
531 }
3eb38818 532
3591c066
AT
533 return start_daemon(STDIN_FILENO);
534 }
535
13e29995 536 if (!no_detach)
a538066d 537 become_daemon();
3591c066 538
f9e940ef 539 if (!lp_load(config_file, 1)) {
65417579 540 exit_cleanup(RERR_SYNTAX);
f9e940ef
AT
541 }
542
45a83540 543 log_init();
f9e940ef 544
a358449a
MP
545 rprintf(FINFO, "rsyncd version %s starting, listening on port %d\n",
546 RSYNC_VERSION,
15b7b73d
MP
547 rsync_port);
548 /* TODO: If listening on a particular address, then show that
431efc89
MP
549 * address too. In fact, why not just do inet_ntop on the
550 * local address??? */
e42c9458 551
8638dd48 552 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
ad517ce5
DD
553 char pidbuf[16];
554 int fd;
8638dd48
DD
555 int pid = (int) getpid();
556 cleanup_set_pid(pid);
ad517ce5
DD
557 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
558 0666 & ~orig_umask)) == -1) {
8638dd48 559 cleanup_set_pid(0);
a039749b 560 rsyserr(FLOG, errno, "failed to create pid file %s", pid_file);
65417579 561 exit_cleanup(RERR_FILEIO);
8638dd48 562 }
8950ac03 563 snprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
ad517ce5
DD
564 write(fd, pidbuf, strlen(pidbuf));
565 close(fd);
8638dd48
DD
566 }
567
8ef4ffd6
AT
568 start_accept_loop(rsync_port, start_daemon);
569 return -1;
3591c066
AT
570}
571