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