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