Commit | Line | Data |
---|---|---|
a039749b MP |
1 | /* -*- c-file-style: "linux"; -*- |
2 | ||
3 | Copyright (C) 1998-2000 by Andrew Tridgell | |
3591c066 AT |
4 | |
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | ||
20 | /* the socket based protocol for setting up a connection wit rsyncd */ | |
21 | ||
22 | #include "rsync.h" | |
23 | ||
24 | extern int module_id; | |
25 | extern int read_only; | |
26 | extern int verbose; | |
27 | extern int rsync_port; | |
97cb8dc2 | 28 | char *auth_user; |
cb13abfe | 29 | int sanitize_paths = 0; |
3591c066 AT |
30 | |
31 | int start_socket_client(char *host, char *path, int argc, char *argv[]) | |
32 | { | |
33 | int fd, i; | |
34 | char *sargs[MAX_ARGS]; | |
35 | int sargc=0; | |
e42c9458 | 36 | char line[MAXPATHLEN]; |
bcb7e502 | 37 | char *p, *user=NULL; |
13c5fc0e | 38 | extern int remote_version; |
8d9dc9f9 | 39 | extern int am_sender; |
e30f0657 | 40 | extern struct in_addr socket_address; |
3591c066 | 41 | |
27e3e9c9 AT |
42 | if (argc == 0 && !am_sender) { |
43 | extern int list_only; | |
44 | list_only = 1; | |
45 | } | |
46 | ||
f98df1d9 AT |
47 | if (*path == '/') { |
48 | rprintf(FERROR,"ERROR: The remote path must start with a module name\n"); | |
49 | return -1; | |
50 | } | |
51 | ||
bcb7e502 AT |
52 | p = strchr(host, '@'); |
53 | if (p) { | |
54 | user = host; | |
55 | host = p+1; | |
56 | *p = 0; | |
57 | } | |
58 | ||
59 | if (!user) user = getenv("USER"); | |
60 | if (!user) user = getenv("LOGNAME"); | |
61 | ||
e30f0657 | 62 | fd = open_socket_out(host, rsync_port, &socket_address); |
3591c066 | 63 | if (fd == -1) { |
65417579 | 64 | exit_cleanup(RERR_SOCKETIO); |
3591c066 AT |
65 | } |
66 | ||
67 | server_options(sargs,&sargc); | |
68 | ||
69 | sargs[sargc++] = "."; | |
70 | ||
71 | if (path && *path) | |
72 | sargs[sargc++] = path; | |
73 | ||
74 | sargs[sargc] = NULL; | |
75 | ||
91eee594 AT |
76 | io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION); |
77 | ||
3591c066 AT |
78 | if (!read_line(fd, line, sizeof(line)-1)) { |
79 | return -1; | |
80 | } | |
81 | ||
13c5fc0e | 82 | if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) { |
3591c066 AT |
83 | return -1; |
84 | } | |
85 | ||
2c91d3d3 AT |
86 | p = strchr(path,'/'); |
87 | if (p) *p = 0; | |
88 | io_printf(fd,"%s\n",path); | |
89 | if (p) *p = '/'; | |
90 | ||
3591c066 AT |
91 | while (1) { |
92 | if (!read_line(fd, line, sizeof(line)-1)) { | |
93 | return -1; | |
94 | } | |
31593dd6 | 95 | |
31593dd6 | 96 | if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) { |
bcb7e502 | 97 | auth_client(fd, user, line+18); |
31593dd6 AT |
98 | continue; |
99 | } | |
31593dd6 | 100 | |
3591c066 AT |
101 | if (strcmp(line,"@RSYNCD: OK") == 0) break; |
102 | rprintf(FINFO,"%s\n", line); | |
103 | } | |
104 | ||
105 | for (i=0;i<sargc;i++) { | |
106 | io_printf(fd,"%s\n", sargs[i]); | |
107 | } | |
108 | io_printf(fd,"\n"); | |
109 | ||
09b7f5db AT |
110 | if (remote_version < 23) { |
111 | if (remote_version == 22 || (remote_version > 17 && !am_sender)) | |
112 | io_start_multiplex_in(fd); | |
113 | } | |
8d9dc9f9 | 114 | |
3591c066 AT |
115 | return client_run(fd, fd, -1, argc, argv); |
116 | } | |
117 | ||
118 | ||
119 | ||
120 | static int rsync_module(int fd, int i) | |
121 | { | |
122 | int argc=0; | |
123 | char *argv[MAX_ARGS]; | |
124 | char **argp; | |
e42c9458 | 125 | char line[MAXPATHLEN]; |
1a016bfd AT |
126 | uid_t uid = (uid_t)-2; |
127 | gid_t gid = (gid_t)-2; | |
8ef4ffd6 | 128 | char *p; |
56c473b7 AT |
129 | char *addr = client_addr(fd); |
130 | char *host = client_name(fd); | |
874895d5 | 131 | char *name = lp_name(i); |
8638dd48 | 132 | int use_chroot = lp_use_chroot(i); |
874895d5 | 133 | int start_glob=0; |
41979ff8 | 134 | int ret; |
7b372642 AT |
135 | char *request=NULL; |
136 | extern int am_sender; | |
8d9dc9f9 | 137 | extern int remote_version; |
943882a2 | 138 | extern int am_root; |
56c473b7 AT |
139 | |
140 | if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) { | |
141 | rprintf(FERROR,"rsync denied on module %s from %s (%s)\n", | |
874895d5 | 142 | name, client_name(fd), client_addr(fd)); |
c8e78d87 | 143 | io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n", |
874895d5 | 144 | name, client_name(fd), client_addr(fd)); |
56c473b7 AT |
145 | return -1; |
146 | } | |
3591c066 | 147 | |
5e71c444 | 148 | if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) { |
8d72ef6e AT |
149 | if (errno) { |
150 | rprintf(FERROR,"failed to open lock file %s : %s\n", | |
5e71c444 | 151 | lp_lock_file(i), strerror(errno)); |
8d72ef6e | 152 | io_printf(fd,"@ERROR: failed to open lock file %s : %s\n", |
5e71c444 | 153 | lp_lock_file(i), strerror(errno)); |
8d72ef6e AT |
154 | } else { |
155 | rprintf(FERROR,"max connections (%d) reached\n", | |
5e71c444 AT |
156 | lp_max_connections(i)); |
157 | io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i)); | |
8d72ef6e | 158 | } |
0c515f17 AT |
159 | return -1; |
160 | } | |
161 | ||
31593dd6 | 162 | |
97cb8dc2 | 163 | auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD "); |
d0d56395 | 164 | |
97cb8dc2 | 165 | if (!auth_user) { |
d0d56395 AT |
166 | rprintf(FERROR,"auth failed on module %s from %s (%s)\n", |
167 | name, client_name(fd), client_addr(fd)); | |
168 | io_printf(fd,"@ERROR: auth failed on module %s\n",name); | |
169 | return -1; | |
170 | } | |
171 | ||
3591c066 AT |
172 | module_id = i; |
173 | ||
716baed7 | 174 | am_root = (getuid() == 0); |
8ef4ffd6 | 175 | |
716baed7 DD |
176 | if (am_root) { |
177 | p = lp_uid(i); | |
178 | if (!name_to_uid(p, &uid)) { | |
179 | if (!isdigit(*p)) { | |
180 | rprintf(FERROR,"Invalid uid %s\n", p); | |
181 | io_printf(fd,"@ERROR: invalid uid\n"); | |
182 | return -1; | |
183 | } | |
184 | uid = atoi(p); | |
185 | } | |
186 | ||
187 | p = lp_gid(i); | |
188 | if (!name_to_gid(p, &gid)) { | |
189 | if (!isdigit(*p)) { | |
190 | rprintf(FERROR,"Invalid gid %s\n", p); | |
191 | io_printf(fd,"@ERROR: invalid gid\n"); | |
192 | return -1; | |
193 | } | |
194 | gid = atoi(p); | |
195 | } | |
8ef4ffd6 AT |
196 | } |
197 | ||
cd64343a DD |
198 | p = lp_include_from(i); |
199 | add_exclude_file(p, 1, 1); | |
200 | ||
201 | p = lp_include(i); | |
202 | add_include_line(p); | |
203 | ||
8f3a2d54 | 204 | p = lp_exclude_from(i); |
2b6b4d53 | 205 | add_exclude_file(p, 1, 0); |
8f3a2d54 | 206 | |
5805327b | 207 | p = lp_exclude(i); |
8f3a2d54 AT |
208 | add_exclude_line(p); |
209 | ||
45a83540 | 210 | log_init(); |
1a016bfd | 211 | |
8638dd48 DD |
212 | if (use_chroot) { |
213 | if (chroot(lp_path(i))) { | |
a039749b | 214 | rsyserr(FERROR, errno, "chroot %s failed", lp_path(i)); |
8638dd48 DD |
215 | io_printf(fd,"@ERROR: chroot failed\n"); |
216 | return -1; | |
217 | } | |
3591c066 | 218 | |
c226b7c2 | 219 | if (!push_dir("/", 0)) { |
a039749b | 220 | rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i)); |
8638dd48 DD |
221 | io_printf(fd,"@ERROR: chdir failed\n"); |
222 | return -1; | |
223 | } | |
3591c066 | 224 | |
716baed7 DD |
225 | } else { |
226 | if (!push_dir(lp_path(i), 0)) { | |
a039749b | 227 | rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i)); |
716baed7 DD |
228 | io_printf(fd,"@ERROR: chdir failed\n"); |
229 | return -1; | |
230 | } | |
cb13abfe | 231 | sanitize_paths = 1; |
716baed7 DD |
232 | } |
233 | ||
234 | if (am_root) { | |
235 | if (setgid(gid)) { | |
a039749b | 236 | rsyserr(FERROR, errno, "setgid %d failed", gid); |
8638dd48 DD |
237 | io_printf(fd,"@ERROR: setgid failed\n"); |
238 | return -1; | |
239 | } | |
3591c066 | 240 | |
716baed7 | 241 | if (setuid(uid)) { |
a039749b | 242 | rsyserr(FERROR, errno, "setuid %d failed", uid); |
8638dd48 DD |
243 | io_printf(fd,"@ERROR: setuid failed\n"); |
244 | return -1; | |
245 | } | |
246 | ||
716baed7 | 247 | am_root = (getuid() == 0); |
3591c066 AT |
248 | } |
249 | ||
250 | io_printf(fd,"@RSYNCD: OK\n"); | |
251 | ||
252 | argv[argc++] = "rsyncd"; | |
253 | ||
254 | while (1) { | |
255 | if (!read_line(fd, line, sizeof(line)-1)) { | |
256 | return -1; | |
257 | } | |
258 | ||
259 | if (!*line) break; | |
260 | ||
874895d5 AT |
261 | p = line; |
262 | ||
874895d5 | 263 | argv[argc] = strdup(p); |
3591c066 AT |
264 | if (!argv[argc]) { |
265 | return -1; | |
266 | } | |
267 | ||
874895d5 | 268 | if (start_glob) { |
1a016bfd | 269 | if (start_glob == 1) { |
7b372642 | 270 | request = strdup(p); |
1a016bfd AT |
271 | start_glob++; |
272 | } | |
cb13abfe | 273 | glob_expand(name, argv, &argc, MAX_ARGS); |
874895d5 AT |
274 | } else { |
275 | argc++; | |
276 | } | |
277 | ||
278 | if (strcmp(line,".") == 0) { | |
279 | start_glob = 1; | |
280 | } | |
281 | ||
3591c066 AT |
282 | if (argc == MAX_ARGS) { |
283 | return -1; | |
284 | } | |
285 | } | |
286 | ||
cb13abfe | 287 | if (sanitize_paths) { |
8638dd48 DD |
288 | /* |
289 | * Note that this is applied to all parameters, whether or not | |
290 | * they are filenames, but no other legal parameters contain | |
291 | * the forms that need to be sanitized so it doesn't hurt; | |
292 | * it is not known at this point which parameters are files | |
293 | * and which aren't. | |
294 | */ | |
295 | for (i = 1; i < argc; i++) { | |
cb13abfe | 296 | sanitize_path(argv[i], NULL); |
8638dd48 DD |
297 | } |
298 | } | |
299 | ||
b86f0cef | 300 | ret = parse_arguments(argc, argv, 0); |
3591c066 | 301 | |
7b372642 | 302 | if (request) { |
97cb8dc2 | 303 | if (*auth_user) { |
d0d56395 AT |
304 | rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n", |
305 | am_sender?"on":"to", | |
97cb8dc2 | 306 | request, auth_user, host, addr); |
d0d56395 AT |
307 | } else { |
308 | rprintf(FINFO,"rsync %s %s from %s (%s)\n", | |
309 | am_sender?"on":"to", | |
310 | request, host, addr); | |
311 | } | |
7b372642 AT |
312 | free(request); |
313 | } | |
314 | ||
0199b05f | 315 | #if !TRIDGE |
3591c066 AT |
316 | /* don't allow the logs to be flooded too fast */ |
317 | if (verbose > 1) verbose = 1; | |
0199b05f | 318 | #endif |
3591c066 AT |
319 | |
320 | argc -= optind; | |
321 | argp = argv + optind; | |
322 | optind = 0; | |
323 | ||
09b7f5db AT |
324 | if (remote_version < 23) { |
325 | if (remote_version == 22 || (remote_version > 17 && am_sender)) | |
326 | io_start_multiplex_out(fd); | |
554e0a8d AT |
327 | } |
328 | ||
41979ff8 | 329 | if (!ret) { |
cd8185f2 | 330 | option_error(); |
41979ff8 AT |
331 | } |
332 | ||
81791cfc AT |
333 | if (lp_timeout(i)) { |
334 | extern int io_timeout; | |
335 | io_timeout = lp_timeout(i); | |
336 | } | |
337 | ||
3591c066 AT |
338 | start_server(fd, fd, argc, argp); |
339 | ||
340 | return 0; | |
341 | } | |
342 | ||
7a6421fa AT |
343 | /* send a list of available modules to the client. Don't list those |
344 | with "list = False". */ | |
3591c066 AT |
345 | static void send_listing(int fd) |
346 | { | |
347 | int n = lp_numservices(); | |
348 | int i; | |
349 | ||
350 | for (i=0;i<n;i++) | |
351 | if (lp_list(i)) | |
352 | io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i)); | |
353 | } | |
354 | ||
355 | /* this is called when a socket connection is established to a client | |
356 | and we want to start talking. The setup of the system is done from | |
357 | here */ | |
358 | static int start_daemon(int fd) | |
359 | { | |
7a6421fa | 360 | char line[200]; |
3591c066 | 361 | char *motd; |
8ef4ffd6 | 362 | int i = -1; |
4cdf25e4 | 363 | extern char *config_file; |
13c5fc0e | 364 | extern int remote_version; |
4cdf25e4 | 365 | |
f9e940ef | 366 | if (!lp_load(config_file, 0)) { |
65417579 | 367 | exit_cleanup(RERR_SYNTAX); |
4cdf25e4 | 368 | } |
3591c066 AT |
369 | |
370 | set_socket_options(fd,"SO_KEEPALIVE"); | |
a6801c39 | 371 | set_socket_options(fd,lp_socket_options()); |
f0359dd0 | 372 | set_nonblocking(fd); |
3591c066 AT |
373 | |
374 | io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION); | |
375 | ||
376 | motd = lp_motd_file(); | |
27d3cdbc | 377 | if (motd && *motd) { |
3591c066 AT |
378 | FILE *f = fopen(motd,"r"); |
379 | while (f && !feof(f)) { | |
380 | int len = fread(line, 1, sizeof(line)-1, f); | |
381 | if (len > 0) { | |
382 | line[len] = 0; | |
383 | io_printf(fd,"%s", line); | |
384 | } | |
385 | } | |
386 | if (f) fclose(f); | |
387 | io_printf(fd,"\n"); | |
388 | } | |
389 | ||
91eee594 AT |
390 | if (!read_line(fd, line, sizeof(line)-1)) { |
391 | return -1; | |
392 | } | |
393 | ||
394 | if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) { | |
c8e78d87 | 395 | io_printf(fd,"@ERROR: protocol startup error\n"); |
91eee594 AT |
396 | return -1; |
397 | } | |
398 | ||
8ef4ffd6 | 399 | while (i == -1) { |
3591c066 AT |
400 | line[0] = 0; |
401 | if (!read_line(fd, line, sizeof(line)-1)) { | |
402 | return -1; | |
403 | } | |
404 | ||
405 | if (!*line || strcmp(line,"#list")==0) { | |
406 | send_listing(fd); | |
407 | return -1; | |
408 | } | |
409 | ||
410 | if (*line == '#') { | |
411 | /* it's some sort of command that I don't understand */ | |
c8e78d87 | 412 | io_printf(fd,"@ERROR: Unknown command '%s'\n", line); |
3591c066 AT |
413 | return -1; |
414 | } | |
415 | ||
416 | i = lp_number(line); | |
417 | if (i == -1) { | |
c8e78d87 | 418 | io_printf(fd,"@ERROR: Unknown module '%s'\n", line); |
3591c066 AT |
419 | return -1; |
420 | } | |
3591c066 AT |
421 | } |
422 | ||
8ef4ffd6 | 423 | return rsync_module(fd, i); |
3591c066 AT |
424 | } |
425 | ||
426 | ||
427 | int daemon_main(void) | |
428 | { | |
f9e940ef | 429 | extern char *config_file; |
ad517ce5 | 430 | extern int orig_umask; |
8638dd48 | 431 | char *pid_file; |
1a016bfd | 432 | |
3591c066 | 433 | if (is_a_socket(STDIN_FILENO)) { |
41979ff8 AT |
434 | int i; |
435 | ||
436 | /* we are running via inetd - close off stdout and | |
437 | stderr so that library functions (and getopt) don't | |
438 | try to use them. Redirect them to /dev/null */ | |
439 | for (i=1;i<3;i++) { | |
440 | close(i); | |
441 | open("/dev/null", O_RDWR); | |
442 | } | |
3eb38818 | 443 | |
3591c066 AT |
444 | return start_daemon(STDIN_FILENO); |
445 | } | |
446 | ||
447 | become_daemon(); | |
448 | ||
f9e940ef | 449 | if (!lp_load(config_file, 1)) { |
65417579 | 450 | exit_cleanup(RERR_SYNTAX); |
f9e940ef AT |
451 | } |
452 | ||
45a83540 | 453 | log_init(); |
f9e940ef | 454 | |
e42c9458 AT |
455 | rprintf(FINFO,"rsyncd version %s starting\n",VERSION); |
456 | ||
8638dd48 | 457 | if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) { |
ad517ce5 DD |
458 | char pidbuf[16]; |
459 | int fd; | |
8638dd48 DD |
460 | int pid = (int) getpid(); |
461 | cleanup_set_pid(pid); | |
ad517ce5 DD |
462 | if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC, |
463 | 0666 & ~orig_umask)) == -1) { | |
8638dd48 | 464 | cleanup_set_pid(0); |
a039749b | 465 | rsyserr(FLOG, errno, "failed to create pid file %s", pid_file); |
65417579 | 466 | exit_cleanup(RERR_FILEIO); |
8638dd48 | 467 | } |
ad517ce5 DD |
468 | slprintf(pidbuf, sizeof(pidbuf), "%d\n", pid); |
469 | write(fd, pidbuf, strlen(pidbuf)); | |
470 | close(fd); | |
8638dd48 DD |
471 | } |
472 | ||
8ef4ffd6 AT |
473 | start_accept_loop(rsync_port, start_daemon); |
474 | return -1; | |
3591c066 AT |
475 | } |
476 |