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