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