Explicitly set kluge_around_eof to a 1 when it is needed.
[rsync/rsync.git] / clientserver.c
CommitLineData
a039749b 1/* -*- c-file-style: "linux"; -*-
4a7144ee 2 *
a254fd97
MP
3 * Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4 * Copyright (C) 2001-2002 by Martin Pool <mbp@samba.org>
4a7144ee 5 *
a254fd97
MP
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.
4a7144ee 10 *
a254fd97
MP
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.
4a7144ee 15 *
a254fd97
MP
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
c0814b81
WD
30extern int verbose;
31extern int list_only;
6e195fe9
WD
32extern int am_sender;
33extern int am_server;
34extern int am_daemon;
35extern int am_root;
3591c066 36extern int rsync_port;
8dad7fc6 37extern int kluge_around_eof;
6e195fe9 38extern int daemon_over_rsh;
b35d0d8e 39extern int sanitize_paths;
7c2a9e76 40extern int filesfrom_fd;
daa598df
WD
41extern int remote_protocol;
42extern int protocol_version;
6e195fe9 43extern int io_timeout;
ad54dcc8 44extern int select_timeout;
6e195fe9
WD
45extern int orig_umask;
46extern int no_detach;
47extern int default_af_hint;
48extern char *bind_address;
7842418b 49extern struct filter_list_struct server_filter_list;
6e195fe9 50extern char *config_file;
bf4679e8 51extern char *files_from;
6e195fe9
WD
52
53char *auth_user;
18638730 54int read_only = 0;
f39b2060
WD
55int daemon_log_format_has_i = 0;
56int daemon_log_format_has_o_or_i = 0;
211bc43b 57int module_id = -1;
3591c066 58
e7bf7c01
WD
59/* Length of lp_path() string when in daemon mode & not chrooted, else 0. */
60unsigned int module_dirlen = 0;
61
be2961da 62/**
bc363ea9 63 * Run a client connected to an rsyncd. The alternative to this
be2961da
MP
64 * function for remote-shell connections is do_cmd().
65 *
fdf88d75
MP
66 * After negotiating which module to use and reading the server's
67 * motd, this hands over to client_run(). Telling the server the
68 * module will cause it to chroot/setuid/etc.
69 *
70 * Instead of doing a transfer, the client may at this stage instead
71 * get a listing of remote modules and exit.
be2961da
MP
72 *
73 * @return -1 for error in startup, or the result of client_run().
d0829892 74 * Either way, it eventually gets passed to exit_cleanup().
be2961da 75 **/
3591c066
AT
76int start_socket_client(char *host, char *path, int argc, char *argv[])
77{
68f40ebb 78 int fd, ret;
2e94e70e 79 char *p, *user = NULL;
4a7144ee 80
1732b6c0
WD
81 /* This is redundant with code in start_inband_exchange(), but this
82 * short-circuits a problem in the client before we open a socket,
83 * and the extra check won't hurt. */
f98df1d9 84 if (*path == '/') {
1732b6c0
WD
85 rprintf(FERROR,
86 "ERROR: The remote path must start with a module name not a /\n");
f98df1d9
AT
87 return -1;
88 }
89
1732b6c0 90 if ((p = strchr(host, '@')) != NULL) {
bcb7e502
AT
91 user = host;
92 host = p+1;
1732b6c0 93 *p = '\0';
bcb7e502
AT
94 }
95
3add5835
WD
96 if (rsync_port == 0)
97 rsync_port = RSYNC_PORT;
98
837cbad9
WD
99 fd = open_socket_out_wrapped(host, rsync_port, bind_address,
100 default_af_hint);
df5cd107 101 if (fd == -1)
65417579 102 exit_cleanup(RERR_SOCKETIO);
68f40ebb
WD
103
104 ret = start_inband_exchange(user, path, fd, fd, argc);
105
106 return ret < 0? ret : client_run(fd, fd, -1, argc, argv);
107}
108
b9f592fb
WD
109int start_inband_exchange(char *user, char *path, int f_in, int f_out,
110 int argc)
68f40ebb
WD
111{
112 int i;
113 char *sargs[MAX_ARGS];
114 int sargc = 0;
115 char line[MAXPATHLEN];
116 char *p;
68f40ebb
WD
117
118 if (argc == 0 && !am_sender)
9bcb2595 119 list_only |= 1;
68f40ebb
WD
120
121 if (*path == '/') {
1732b6c0
WD
122 rprintf(FERROR,
123 "ERROR: The remote path must start with a module name\n");
68f40ebb
WD
124 return -1;
125 }
126
2e94e70e
WD
127 if (!user)
128 user = getenv("USER");
129 if (!user)
130 user = getenv("LOGNAME");
68f40ebb 131
5a8543b8 132 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
91eee594 133
2e94e70e 134 if (!read_line(f_in, line, sizeof line - 1)) {
be2961da 135 rprintf(FERROR, "rsync: did not see server greeting\n");
3591c066
AT
136 return -1;
137 }
138
daa598df 139 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
be2961da
MP
140 /* note that read_line strips of \n or \r */
141 rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n",
142 line);
3591c066
AT
143 return -1;
144 }
daa598df
WD
145 if (protocol_version > remote_protocol)
146 protocol_version = remote_protocol;
3591c066 147
9bcb2595
WD
148 if (list_only && protocol_version >= 29)
149 list_only |= 2;
150
151 /* set daemon_over_rsh to false since we need to build the
152 * true set of args passed through the rsh/ssh connection;
153 * this is a no-op for direct-socket-connection mode */
154 daemon_over_rsh = 0;
155 server_options(sargs, &sargc);
156
157 sargs[sargc++] = ".";
158
159 if (path && *path)
160 sargs[sargc++] = path;
161
162 sargs[sargc] = NULL;
163
164 if (verbose > 1)
165 print_child_argv(sargs);
166
2c91d3d3
AT
167 p = strchr(path,'/');
168 if (p) *p = 0;
68f40ebb 169 io_printf(f_out, "%s\n", path);
2c91d3d3
AT
170 if (p) *p = '/';
171
063393d6
MP
172 /* Old servers may just drop the connection here,
173 rather than sending a proper EXIT command. Yuck. */
8dad7fc6 174 kluge_around_eof = list_only && protocol_version < 25 ? 1 : 0;
7a55d06e 175
063393d6 176 while (1) {
2e94e70e 177 if (!read_line(f_in, line, sizeof line - 1)) {
be2961da 178 rprintf(FERROR, "rsync: didn't get server startup line\n");
3591c066
AT
179 return -1;
180 }
31593dd6 181
31593dd6 182 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
68f40ebb 183 auth_client(f_out, user, line+18);
31593dd6
AT
184 continue;
185 }
31593dd6 186
2e94e70e
WD
187 if (strcmp(line,"@RSYNCD: OK") == 0)
188 break;
8f04bb36 189
cae95647
MP
190 if (strcmp(line,"@RSYNCD: EXIT") == 0) {
191 /* This is sent by recent versions of the
192 * server to terminate the listing of modules.
193 * We don't want to go on and transfer
194 * anything; just exit. */
195 exit(0);
196 }
8f04bb36 197
136ac7ec 198 if (strncmp(line, "@ERROR", 6) == 0) {
1732b6c0 199 rprintf(FERROR, "%s\n", line);
136ac7ec
MP
200 /* This is always fatal; the server will now
201 * close the socket. */
202 return RERR_STARTCLIENT;
203 } else {
c613d370 204 rprintf(FINFO,"%s\n", line);
136ac7ec 205 }
3591c066 206 }
8dad7fc6 207 kluge_around_eof = 0;
3591c066 208
68f40ebb
WD
209 for (i = 0; i < sargc; i++) {
210 io_printf(f_out, "%s\n", sargs[i]);
3591c066 211 }
68f40ebb 212 io_printf(f_out, "\n");
3591c066 213
daa598df 214 if (protocol_version < 23) {
3f55bd5d 215 if (protocol_version == 22 || !am_sender)
da3478b2 216 io_start_multiplex_in();
09b7f5db 217 }
8d9dc9f9 218
68f40ebb 219 return 0;
3591c066
AT
220}
221
222
223
68f40ebb 224static int rsync_module(int f_in, int f_out, int i)
3591c066 225{
2e94e70e 226 int argc = 0;
b7061c82
WD
227 int maxargs;
228 char **argv;
3591c066 229 char **argp;
e42c9458 230 char line[MAXPATHLEN];
2af27ad9 231 uid_t uid = (uid_t)-2; /* canonically "nobody" */
1a016bfd 232 gid_t gid = (gid_t)-2;
8ef4ffd6 233 char *p;
09021eab
DD
234 char *addr = client_addr(f_in);
235 char *host = client_name(f_in);
874895d5 236 char *name = lp_name(i);
8638dd48 237 int use_chroot = lp_use_chroot(i);
2e94e70e 238 int start_glob = 0;
41979ff8 239 int ret;
2e94e70e 240 char *request = NULL;
56c473b7
AT
241
242 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
1732b6c0 243 rprintf(FLOG, "rsync denied on module %s from %s (%s)\n",
31ec50d7 244 name, host, addr);
c0422cea
WD
245 if (!lp_list(i))
246 io_printf(f_out, "@ERROR: Unknown module '%s'\n", name);
247 else {
248 io_printf(f_out,
249 "@ERROR: access denied to %s from %s (%s)\n",
250 name, host, addr);
251 }
56c473b7
AT
252 return -1;
253 }
3591c066 254
68f40ebb 255 if (am_daemon && am_server) {
9e5a5ddb 256 rprintf(FLOG, "rsync allowed access on module %s from %s (%s)\n",
68f40ebb
WD
257 name, host, addr);
258 }
259
5e71c444 260 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
8d72ef6e 261 if (errno) {
1732b6c0 262 rsyserr(FLOG, errno, "failed to open lock file %s",
4875d6b6
WD
263 safe_fname(lp_lock_file(i)));
264 io_printf(f_out, "@ERROR: failed to open lock file\n");
8d72ef6e 265 } else {
1732b6c0 266 rprintf(FLOG, "max connections (%d) reached\n",
5e71c444 267 lp_max_connections(i));
4ccfd96c 268 io_printf(f_out, "@ERROR: max connections (%d) reached -- try again later\n",
837cbad9 269 lp_max_connections(i));
8d72ef6e 270 }
0c515f17
AT
271 return -1;
272 }
273
68f40ebb 274 auth_user = auth_server(f_in, f_out, i, addr, "@RSYNCD: AUTHREQD ");
d0d56395 275
97cb8dc2 276 if (!auth_user) {
1732b6c0 277 rprintf(FLOG, "auth failed on module %s from %s (%s)\n",
68f40ebb
WD
278 name, host, addr);
279 io_printf(f_out, "@ERROR: auth failed on module %s\n", name);
4a7144ee 280 return -1;
d0d56395
AT
281 }
282
3591c066
AT
283 module_id = i;
284
211bc43b 285 if (lp_read_only(i))
18638730
WD
286 read_only = 1;
287
f39b2060
WD
288 if (lp_transfer_logging(i)) {
289 if (strstr(lp_log_format(i), "%i") != NULL)
290 daemon_log_format_has_i = 1;
291 if (daemon_log_format_has_i
292 || strstr(lp_log_format(i), "%o") != NULL)
293 daemon_log_format_has_o_or_i = 1;
294 }
c0814b81 295
6fe05820 296 am_root = (MY_UID() == 0);
8ef4ffd6 297
716baed7
DD
298 if (am_root) {
299 p = lp_uid(i);
300 if (!name_to_uid(p, &uid)) {
2e94e70e 301 if (!isdigit(*(unsigned char *)p)) {
1732b6c0 302 rprintf(FLOG, "Invalid uid %s\n", p);
68f40ebb 303 io_printf(f_out, "@ERROR: invalid uid %s\n", p);
716baed7 304 return -1;
4a7144ee 305 }
716baed7
DD
306 uid = atoi(p);
307 }
308
309 p = lp_gid(i);
310 if (!name_to_gid(p, &gid)) {
2e94e70e 311 if (!isdigit(*(unsigned char *)p)) {
1732b6c0 312 rprintf(FLOG, "Invalid gid %s\n", p);
68f40ebb 313 io_printf(f_out, "@ERROR: invalid gid %s\n", p);
716baed7 314 return -1;
4a7144ee 315 }
716baed7
DD
316 gid = atoi(p);
317 }
8ef4ffd6 318 }
3b2b5345 319
4a7144ee
WD
320 /* TODO: If we're not root, but the configuration requests
321 * that we change to some uid other than the current one, then
322 * log a warning. */
323
324 /* TODO: Perhaps take a list of gids, and make them into the
325 * supplementary groups. */
8ef4ffd6 326
e7bf7c01
WD
327 if (use_chroot) {
328 module_dirlen = 0;
7842418b 329 set_filter_dir("/", 1);
e7bf7c01
WD
330 } else {
331 module_dirlen = strlen(lp_path(i));
7842418b 332 set_filter_dir(lp_path(i), module_dirlen);
e7bf7c01
WD
333 }
334
335 p = lp_filter(i);
ebfd1a1c 336 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
0a68f869 337 XFLG_ANCHORED2ABS);
837cbad9 338
cd64343a 339 p = lp_include_from(i);
ebfd1a1c 340 parse_filter_file(&server_filter_list, p, MATCHFLG_INCLUDE,
0a68f869 341 XFLG_ANCHORED2ABS | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
cd64343a
DD
342
343 p = lp_include(i);
ebfd1a1c 344 parse_rule(&server_filter_list, p,
0a68f869
WD
345 MATCHFLG_INCLUDE | MATCHFLG_WORD_SPLIT,
346 XFLG_ANCHORED2ABS | XFLG_OLD_PREFIXES);
cd64343a 347
8f3a2d54 348 p = lp_exclude_from(i);
ebfd1a1c 349 parse_filter_file(&server_filter_list, p, 0,
0a68f869 350 XFLG_ANCHORED2ABS | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
8f3a2d54 351
5805327b 352 p = lp_exclude(i);
ebfd1a1c 353 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
0a68f869 354 XFLG_ANCHORED2ABS | XFLG_OLD_PREFIXES);
8f3a2d54 355
45a83540 356 log_init();
1a016bfd 357
8638dd48 358 if (use_chroot) {
b52c1d9d
MP
359 /*
360 * XXX: The 'use chroot' flag is a fairly reliable
361 * source of confusion, because it fails under two
362 * important circumstances: running as non-root,
363 * running on Win32 (or possibly others). On the
364 * other hand, if you are running as root, then it
365 * might be better to always use chroot.
366 *
367 * So, perhaps if we can't chroot we should just issue
368 * a warning, unless a "require chroot" flag is set,
369 * in which case we fail.
370 */
8638dd48 371 if (chroot(lp_path(i))) {
4875d6b6
WD
372 rsyserr(FLOG, errno, "chroot %s failed",
373 safe_fname(lp_path(i)));
68f40ebb 374 io_printf(f_out, "@ERROR: chroot failed\n");
8638dd48
DD
375 return -1;
376 }
3591c066 377
59187666 378 if (!push_dir("/")) {
4875d6b6
WD
379 rsyserr(FLOG, errno, "chdir %s failed\n",
380 safe_fname(lp_path(i)));
68f40ebb 381 io_printf(f_out, "@ERROR: chdir failed\n");
8638dd48
DD
382 return -1;
383 }
3591c066 384
716baed7 385 } else {
59187666 386 if (!push_dir(lp_path(i))) {
4875d6b6
WD
387 rsyserr(FLOG, errno, "chdir %s failed\n",
388 safe_fname(lp_path(i)));
68f40ebb 389 io_printf(f_out, "@ERROR: chdir failed\n");
716baed7
DD
390 return -1;
391 }
cb13abfe 392 sanitize_paths = 1;
716baed7
DD
393 }
394
395 if (am_root) {
715d1f45
MP
396 /* XXXX: You could argue that if the daemon is started
397 * by a non-root user and they explicitly specify a
398 * gid, then we should try to change to that gid --
399 * this could be possible if it's already in their
400 * supplementary groups. */
401
402 /* TODO: Perhaps we need to document that if rsyncd is
403 * started by somebody other than root it will inherit
404 * all their supplementary groups. */
405
716baed7 406 if (setgid(gid)) {
1732b6c0 407 rsyserr(FLOG, errno, "setgid %d failed", (int)gid);
68f40ebb 408 io_printf(f_out, "@ERROR: setgid failed\n");
8638dd48
DD
409 return -1;
410 }
4f5b0756 411#ifdef HAVE_SETGROUPS
6969ebcf
WD
412 /* Get rid of any supplementary groups this process
413 * might have inheristed. */
414 if (setgroups(1, &gid)) {
1732b6c0 415 rsyserr(FLOG, errno, "setgroups failed");
6969ebcf
WD
416 io_printf(f_out, "@ERROR: setgroups failed\n");
417 return -1;
418 }
419#endif
3591c066 420
716baed7 421 if (setuid(uid)) {
1732b6c0 422 rsyserr(FLOG, errno, "setuid %d failed", (int)uid);
68f40ebb 423 io_printf(f_out, "@ERROR: setuid failed\n");
8638dd48
DD
424 return -1;
425 }
426
6fe05820 427 am_root = (MY_UID() == 0);
3591c066
AT
428 }
429
68f40ebb 430 io_printf(f_out, "@RSYNCD: OK\n");
3591c066 431
b7061c82
WD
432 maxargs = MAX_ARGS;
433 if (!(argv = new_array(char *, maxargs)))
434 out_of_memory("rsync_module");
3591c066
AT
435 argv[argc++] = "rsyncd";
436
437 while (1) {
2e94e70e 438 if (!read_line(f_in, line, sizeof line - 1))
3591c066 439 return -1;
3591c066 440
2e94e70e
WD
441 if (!*line)
442 break;
3591c066 443
874895d5
AT
444 p = line;
445
b7061c82
WD
446 if (argc == maxargs) {
447 maxargs += MAX_ARGS;
448 if (!(argv = realloc_array(argv, char *, maxargs)))
449 out_of_memory("rsync_module");
450 }
2e94e70e 451 if (!(argv[argc] = strdup(p)))
b7061c82 452 out_of_memory("rsync_module");
3591c066 453
874895d5 454 if (start_glob) {
1a016bfd 455 if (start_glob == 1) {
7b372642 456 request = strdup(p);
1a016bfd
AT
457 start_glob++;
458 }
b7061c82 459 glob_expand(name, &argv, &argc, &maxargs);
2e94e70e 460 } else
874895d5 461 argc++;
874895d5 462
2e94e70e 463 if (strcmp(line, ".") == 0)
874895d5 464 start_glob = 1;
3591c066
AT
465 }
466
18638730 467 verbose = 0; /* future verbosity is controlled by client options */
4a7144ee 468 argp = argv;
15b7b73d 469 ret = parse_arguments(&argc, (const char ***) &argp, 0);
3591c066 470
7c2a9e76
WD
471 if (filesfrom_fd == 0)
472 filesfrom_fd = f_in;
473
7b372642 474 if (request) {
97cb8dc2 475 if (*auth_user) {
9e5a5ddb
WD
476 rprintf(FLOG, "rsync %s %s from %s@%s (%s)\n",
477 am_sender ? "on" : "to",
97cb8dc2 478 request, auth_user, host, addr);
d0d56395 479 } else {
9e5a5ddb
WD
480 rprintf(FLOG, "rsync %s %s from %s (%s)\n",
481 am_sender ? "on" : "to",
d0d56395
AT
482 request, host, addr);
483 }
7b372642
AT
484 free(request);
485 }
486
15b7b73d 487#ifndef DEBUG
3591c066 488 /* don't allow the logs to be flooded too fast */
33e9d10d
WD
489 if (verbose > lp_max_verbosity())
490 verbose = lp_max_verbosity();
0199b05f 491#endif
3591c066 492
1732b6c0
WD
493 if (protocol_version < 23
494 && (protocol_version == 22 || am_sender))
da3478b2 495 io_start_multiplex_out();
1732b6c0
WD
496 else if (!ret) {
497 /* We have to get I/O multiplexing started so that we can
498 * get the error back to the client. This means getting
499 * the protocol setup finished first in later versions. */
500 setup_protocol(f_out, f_in);
bf4679e8
WD
501 if (files_from && !am_sender && strcmp(files_from, "-") != 0)
502 write_byte(f_out, 0);
da3478b2 503 io_start_multiplex_out();
554e0a8d 504 }
15b7b73d 505
41979ff8 506 if (!ret) {
4a7144ee 507 option_error();
7e561438 508 msleep(400);
4a7144ee 509 exit_cleanup(RERR_UNSUPPORTED);
41979ff8
AT
510 }
511
ad54dcc8 512 if (lp_timeout(i)) {
81791cfc 513 io_timeout = lp_timeout(i);
43bab403 514 if (io_timeout < select_timeout)
ad54dcc8
WD
515 select_timeout = io_timeout;
516 }
81791cfc 517
68f40ebb 518 start_server(f_in, f_out, argc, argp);
3591c066
AT
519
520 return 0;
521}
522
7a6421fa
AT
523/* send a list of available modules to the client. Don't list those
524 with "list = False". */
3591c066
AT
525static void send_listing(int fd)
526{
527 int n = lp_numservices();
528 int i;
063393d6 529
c0422cea 530 for (i = 0; i < n; i++) {
3591c066 531 if (lp_list(i))
4a7144ee 532 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
c0422cea 533 }
8f04bb36 534
daa598df 535 if (protocol_version >= 25)
063393d6 536 io_printf(fd,"@RSYNCD: EXIT\n");
3591c066
AT
537}
538
68f40ebb 539/* this is called when a connection is established to a client
3591c066
AT
540 and we want to start talking. The setup of the system is done from
541 here */
68f40ebb 542int start_daemon(int f_in, int f_out)
3591c066 543{
7a6421fa 544 char line[200];
3591c066 545 char *motd;
c0422cea 546 int i;
4cdf25e4 547
da3478b2
WD
548 io_set_sock_fds(f_in, f_out);
549
2e94e70e 550 if (!lp_load(config_file, 0))
65417579 551 exit_cleanup(RERR_SYNTAX);
3591c066 552
30e8c8e1
DD
553 log_init();
554
68f40ebb
WD
555 if (!am_server) {
556 set_socket_options(f_in, "SO_KEEPALIVE");
557 set_socket_options(f_in, lp_socket_options());
558 set_nonblocking(f_in);
559 }
3591c066 560
5a8543b8 561 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
3591c066
AT
562
563 motd = lp_motd_file();
27d3cdbc 564 if (motd && *motd) {
0090cbdb 565 FILE *f = fopen(motd,"r");
3591c066 566 while (f && !feof(f)) {
2e94e70e 567 int len = fread(line, 1, sizeof line - 1, f);
3591c066
AT
568 if (len > 0) {
569 line[len] = 0;
68f40ebb 570 io_printf(f_out, "%s", line);
3591c066
AT
571 }
572 }
2e94e70e
WD
573 if (f)
574 fclose(f);
68f40ebb 575 io_printf(f_out, "\n");
3591c066
AT
576 }
577
2e94e70e 578 if (!read_line(f_in, line, sizeof line - 1))
91eee594 579 return -1;
91eee594 580
daa598df 581 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
68f40ebb 582 io_printf(f_out, "@ERROR: protocol startup error\n");
91eee594 583 return -1;
4a7144ee 584 }
daa598df
WD
585 if (protocol_version > remote_protocol)
586 protocol_version = remote_protocol;
91eee594 587
c0422cea
WD
588 line[0] = 0;
589 if (!read_line(f_in, line, sizeof line - 1))
590 return -1;
3591c066 591
c0422cea
WD
592 if (!*line || strcmp(line, "#list") == 0) {
593 send_listing(f_out);
594 return -1;
595 }
3591c066 596
c0422cea
WD
597 if (*line == '#') {
598 /* it's some sort of command that I don't understand */
599 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
600 return -1;
601 }
3591c066 602
c0422cea
WD
603 if ((i = lp_number(line)) < 0) {
604 char *addr = client_addr(f_in);
605 char *host = client_name(f_in);
606 rprintf(FLOG, "unknown module '%s' tried from %s (%s)\n",
607 line, host, addr);
608 io_printf(f_out, "@ERROR: Unknown module '%s'\n", line);
609 return -1;
3591c066
AT
610 }
611
68f40ebb 612 return rsync_module(f_in, f_out, i);
3591c066
AT
613}
614
615
616int daemon_main(void)
617{
8638dd48 618 char *pid_file;
1a016bfd 619
3591c066 620 if (is_a_socket(STDIN_FILENO)) {
41979ff8
AT
621 int i;
622
623 /* we are running via inetd - close off stdout and
4a7144ee
WD
624 * stderr so that library functions (and getopt) don't
625 * try to use them. Redirect them to /dev/null */
2e94e70e 626 for (i = 1; i < 3; i++) {
4a7144ee 627 close(i);
41979ff8
AT
628 open("/dev/null", O_RDWR);
629 }
3eb38818 630
68f40ebb 631 return start_daemon(STDIN_FILENO, STDIN_FILENO);
3591c066
AT
632 }
633
13e29995 634 if (!no_detach)
4a7144ee 635 become_daemon();
3591c066 636
2e94e70e 637 if (!lp_load(config_file, 1))
65417579 638 exit_cleanup(RERR_SYNTAX);
f9e940ef 639
0c56b1ad
WD
640 if (rsync_port == 0 && (rsync_port = lp_rsync_port()) == 0)
641 rsync_port = RSYNC_PORT;
3dfe6e97 642 if (bind_address == NULL && *lp_bind_address())
986aaaaa 643 bind_address = lp_bind_address();
0c56b1ad 644
45a83540 645 log_init();
f9e940ef 646
9e5a5ddb 647 rprintf(FLOG, "rsyncd version %s starting, listening on port %d\n",
4a7144ee
WD
648 RSYNC_VERSION, rsync_port);
649 /* TODO: If listening on a particular address, then show that
650 * address too. In fact, why not just do inet_ntop on the
651 * local address??? */
e42c9458 652
8638dd48 653 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
ad517ce5
DD
654 char pidbuf[16];
655 int fd;
05b7bab8 656 pid_t pid = getpid();
8638dd48 657 cleanup_set_pid(pid);
ad517ce5
DD
658 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
659 0666 & ~orig_umask)) == -1) {
4a7144ee 660 cleanup_set_pid(0);
4875d6b6
WD
661 rsyserr(FLOG, errno, "failed to create pid file %s",
662 safe_fname(pid_file));
4a7144ee 663 exit_cleanup(RERR_FILEIO);
8638dd48 664 }
05b7bab8 665 snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
ad517ce5
DD
666 write(fd, pidbuf, strlen(pidbuf));
667 close(fd);
8638dd48
DD
668 }
669
8ef4ffd6
AT
670 start_accept_loop(rsync_port, start_daemon);
671 return -1;
3591c066 672}