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