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