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