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