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