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; | |
27 | ||
28 | int start_socket_client(char *host, char *path, int argc, char *argv[]) | |
29 | { | |
30 | int fd, i; | |
31 | char *sargs[MAX_ARGS]; | |
32 | int sargc=0; | |
e42c9458 | 33 | char line[MAXPATHLEN]; |
bcb7e502 | 34 | char *p, *user=NULL; |
13c5fc0e | 35 | extern int remote_version; |
8d9dc9f9 AT |
36 | extern int am_client; |
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 | ||
8d9dc9f9 AT |
54 | am_client = 1; |
55 | ||
3591c066 AT |
56 | fd = open_socket_out(host, rsync_port); |
57 | if (fd == -1) { | |
58 | exit_cleanup(1); | |
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 | ||
8d9dc9f9 AT |
104 | if (remote_version > 17 && !am_sender) |
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); |
d0d56395 | 124 | char *user; |
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 | |
0c515f17 | 140 | if (!claim_connection(lp_lock_file(), lp_max_connections())) { |
8d72ef6e AT |
141 | if (errno) { |
142 | rprintf(FERROR,"failed to open lock file %s : %s\n", | |
143 | lp_lock_file(), strerror(errno)); | |
144 | io_printf(fd,"@ERROR: failed to open lock file %s : %s\n", | |
145 | lp_lock_file(), strerror(errno)); | |
146 | } else { | |
147 | rprintf(FERROR,"max connections (%d) reached\n", | |
148 | lp_max_connections()); | |
149 | io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections()); | |
150 | } | |
0c515f17 AT |
151 | return -1; |
152 | } | |
153 | ||
31593dd6 | 154 | |
d0d56395 AT |
155 | user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD "); |
156 | ||
157 | if (!user) { | |
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 | ||
8ef4ffd6 AT |
169 | p = lp_uid(i); |
170 | if (!name_to_uid(p, &uid)) { | |
171 | if (!isdigit(*p)) { | |
172 | rprintf(FERROR,"Invalid uid %s\n", p); | |
c8e78d87 | 173 | io_printf(fd,"@ERROR: invalid uid\n"); |
8ef4ffd6 AT |
174 | return -1; |
175 | } | |
176 | uid = atoi(p); | |
177 | } | |
178 | ||
179 | p = lp_gid(i); | |
180 | if (!name_to_gid(p, &gid)) { | |
181 | if (!isdigit(*p)) { | |
182 | rprintf(FERROR,"Invalid gid %s\n", p); | |
c8e78d87 | 183 | io_printf(fd,"@ERROR: invalid gid\n"); |
8ef4ffd6 AT |
184 | return -1; |
185 | } | |
186 | gid = atoi(p); | |
187 | } | |
188 | ||
8f3a2d54 | 189 | p = lp_exclude_from(i); |
2b6b4d53 | 190 | add_exclude_file(p, 1, 0); |
8f3a2d54 | 191 | |
5805327b | 192 | p = lp_exclude(i); |
8f3a2d54 AT |
193 | add_exclude_line(p); |
194 | ||
1a016bfd AT |
195 | log_open(); |
196 | ||
3591c066 | 197 | if (chroot(lp_path(i))) { |
1a016bfd | 198 | rprintf(FERROR,"chroot %s failed\n", lp_path(i)); |
3591c066 AT |
199 | io_printf(fd,"@ERROR: chroot failed\n"); |
200 | return -1; | |
201 | } | |
202 | ||
203 | if (chdir("/")) { | |
1a016bfd | 204 | rprintf(FERROR,"chdir %s failed\n", lp_path(i)); |
3591c066 AT |
205 | io_printf(fd,"@ERROR: chdir failed\n"); |
206 | return -1; | |
207 | } | |
208 | ||
1a016bfd AT |
209 | if (setgid(gid) || getgid() != gid) { |
210 | rprintf(FERROR,"setgid %d failed\n", gid); | |
3591c066 AT |
211 | io_printf(fd,"@ERROR: setgid failed\n"); |
212 | return -1; | |
213 | } | |
214 | ||
1a016bfd AT |
215 | if (setuid(uid) || getuid() != uid) { |
216 | rprintf(FERROR,"setuid %d failed\n", uid); | |
3591c066 AT |
217 | io_printf(fd,"@ERROR: setuid failed\n"); |
218 | return -1; | |
219 | } | |
220 | ||
943882a2 AT |
221 | am_root = (getuid() == 0); |
222 | ||
3591c066 AT |
223 | io_printf(fd,"@RSYNCD: OK\n"); |
224 | ||
225 | argv[argc++] = "rsyncd"; | |
226 | ||
227 | while (1) { | |
228 | if (!read_line(fd, line, sizeof(line)-1)) { | |
229 | return -1; | |
230 | } | |
231 | ||
232 | if (!*line) break; | |
233 | ||
874895d5 AT |
234 | p = line; |
235 | ||
874895d5 | 236 | argv[argc] = strdup(p); |
3591c066 AT |
237 | if (!argv[argc]) { |
238 | return -1; | |
239 | } | |
240 | ||
874895d5 | 241 | if (start_glob) { |
1a016bfd | 242 | if (start_glob == 1) { |
7b372642 | 243 | request = strdup(p); |
1a016bfd AT |
244 | start_glob++; |
245 | } | |
087bf010 | 246 | glob_expand(name, argv, &argc, MAX_ARGS); |
874895d5 AT |
247 | } else { |
248 | argc++; | |
249 | } | |
250 | ||
251 | if (strcmp(line,".") == 0) { | |
252 | start_glob = 1; | |
253 | } | |
254 | ||
3591c066 AT |
255 | if (argc == MAX_ARGS) { |
256 | return -1; | |
257 | } | |
258 | } | |
259 | ||
41979ff8 | 260 | ret = parse_arguments(argc, argv); |
3591c066 | 261 | |
7b372642 | 262 | if (request) { |
d0d56395 AT |
263 | if (*user) { |
264 | rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n", | |
265 | am_sender?"on":"to", | |
266 | request, user, host, addr); | |
267 | } else { | |
268 | rprintf(FINFO,"rsync %s %s from %s (%s)\n", | |
269 | am_sender?"on":"to", | |
270 | request, host, addr); | |
271 | } | |
7b372642 AT |
272 | free(request); |
273 | } | |
274 | ||
0199b05f | 275 | #if !TRIDGE |
3591c066 AT |
276 | /* don't allow the logs to be flooded too fast */ |
277 | if (verbose > 1) verbose = 1; | |
0199b05f | 278 | #endif |
3591c066 AT |
279 | |
280 | argc -= optind; | |
281 | argp = argv + optind; | |
282 | optind = 0; | |
283 | ||
8d9dc9f9 AT |
284 | if (remote_version > 17 && am_sender) |
285 | io_start_multiplex_out(fd); | |
286 | ||
41979ff8 AT |
287 | if (!ret) { |
288 | rprintf(FERROR,"Error parsing options (unsupported option?) - aborting\n"); | |
289 | exit_cleanup(1); | |
290 | } | |
291 | ||
3591c066 AT |
292 | start_server(fd, fd, argc, argp); |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
7a6421fa AT |
297 | /* send a list of available modules to the client. Don't list those |
298 | with "list = False". */ | |
3591c066 AT |
299 | static void send_listing(int fd) |
300 | { | |
301 | int n = lp_numservices(); | |
302 | int i; | |
303 | ||
304 | for (i=0;i<n;i++) | |
305 | if (lp_list(i)) | |
306 | io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i)); | |
307 | } | |
308 | ||
309 | /* this is called when a socket connection is established to a client | |
310 | and we want to start talking. The setup of the system is done from | |
311 | here */ | |
312 | static int start_daemon(int fd) | |
313 | { | |
7a6421fa | 314 | char line[200]; |
3591c066 | 315 | char *motd; |
8ef4ffd6 | 316 | int i = -1; |
4cdf25e4 | 317 | extern char *config_file; |
13c5fc0e | 318 | extern int remote_version; |
4cdf25e4 | 319 | |
f9e940ef | 320 | if (!lp_load(config_file, 0)) { |
4cdf25e4 AT |
321 | exit_cleanup(1); |
322 | } | |
3591c066 AT |
323 | |
324 | set_socket_options(fd,"SO_KEEPALIVE"); | |
a6801c39 AT |
325 | set_socket_options(fd,lp_socket_options()); |
326 | ||
3591c066 AT |
327 | |
328 | io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION); | |
329 | ||
330 | motd = lp_motd_file(); | |
331 | if (*motd) { | |
332 | FILE *f = fopen(motd,"r"); | |
333 | while (f && !feof(f)) { | |
334 | int len = fread(line, 1, sizeof(line)-1, f); | |
335 | if (len > 0) { | |
336 | line[len] = 0; | |
337 | io_printf(fd,"%s", line); | |
338 | } | |
339 | } | |
340 | if (f) fclose(f); | |
341 | io_printf(fd,"\n"); | |
342 | } | |
343 | ||
91eee594 AT |
344 | if (!read_line(fd, line, sizeof(line)-1)) { |
345 | return -1; | |
346 | } | |
347 | ||
348 | if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) { | |
c8e78d87 | 349 | io_printf(fd,"@ERROR: protocol startup error\n"); |
91eee594 AT |
350 | return -1; |
351 | } | |
352 | ||
8ef4ffd6 | 353 | while (i == -1) { |
3591c066 AT |
354 | line[0] = 0; |
355 | if (!read_line(fd, line, sizeof(line)-1)) { | |
356 | return -1; | |
357 | } | |
358 | ||
359 | if (!*line || strcmp(line,"#list")==0) { | |
360 | send_listing(fd); | |
361 | return -1; | |
362 | } | |
363 | ||
364 | if (*line == '#') { | |
365 | /* it's some sort of command that I don't understand */ | |
c8e78d87 | 366 | io_printf(fd,"@ERROR: Unknown command '%s'\n", line); |
3591c066 AT |
367 | return -1; |
368 | } | |
369 | ||
370 | i = lp_number(line); | |
371 | if (i == -1) { | |
c8e78d87 | 372 | io_printf(fd,"@ERROR: Unknown module '%s'\n", line); |
3591c066 AT |
373 | return -1; |
374 | } | |
3591c066 AT |
375 | } |
376 | ||
8ef4ffd6 | 377 | return rsync_module(fd, i); |
3591c066 AT |
378 | } |
379 | ||
380 | ||
381 | int daemon_main(void) | |
382 | { | |
f9e940ef | 383 | extern char *config_file; |
1a016bfd | 384 | |
5243c216 AT |
385 | /* this ensures that we don't call getcwd after the chroot, |
386 | which doesn't work on platforms that use popen("pwd","r") | |
387 | for getcwd */ | |
388 | push_dir("/", 0); | |
389 | ||
3591c066 | 390 | if (is_a_socket(STDIN_FILENO)) { |
41979ff8 AT |
391 | int i; |
392 | ||
393 | /* we are running via inetd - close off stdout and | |
394 | stderr so that library functions (and getopt) don't | |
395 | try to use them. Redirect them to /dev/null */ | |
396 | for (i=1;i<3;i++) { | |
397 | close(i); | |
398 | open("/dev/null", O_RDWR); | |
399 | } | |
3eb38818 AT |
400 | |
401 | set_nonblocking(STDIN_FILENO); | |
402 | ||
3591c066 AT |
403 | return start_daemon(STDIN_FILENO); |
404 | } | |
405 | ||
406 | become_daemon(); | |
407 | ||
f9e940ef AT |
408 | if (!lp_load(config_file, 1)) { |
409 | fprintf(stderr,"failed to load config file %s\n", config_file); | |
410 | exit_cleanup(1); | |
411 | } | |
412 | ||
413 | log_open(); | |
414 | ||
e42c9458 AT |
415 | rprintf(FINFO,"rsyncd version %s starting\n",VERSION); |
416 | ||
8ef4ffd6 AT |
417 | start_accept_loop(rsync_port, start_daemon); |
418 | return -1; | |
3591c066 AT |
419 | } |
420 |