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