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