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