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