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