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