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