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 | |
bcb7e502 AT |
39 | p = strchr(host, '@'); |
40 | if (p) { | |
41 | user = host; | |
42 | host = p+1; | |
43 | *p = 0; | |
44 | } | |
45 | ||
46 | if (!user) user = getenv("USER"); | |
47 | if (!user) user = getenv("LOGNAME"); | |
48 | ||
8d9dc9f9 AT |
49 | am_client = 1; |
50 | ||
3591c066 AT |
51 | fd = open_socket_out(host, rsync_port); |
52 | if (fd == -1) { | |
53 | exit_cleanup(1); | |
54 | } | |
55 | ||
56 | server_options(sargs,&sargc); | |
57 | ||
58 | sargs[sargc++] = "."; | |
59 | ||
60 | if (path && *path) | |
61 | sargs[sargc++] = path; | |
62 | ||
63 | sargs[sargc] = NULL; | |
64 | ||
91eee594 AT |
65 | io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION); |
66 | ||
3591c066 AT |
67 | if (!read_line(fd, line, sizeof(line)-1)) { |
68 | return -1; | |
69 | } | |
70 | ||
13c5fc0e | 71 | if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) { |
3591c066 AT |
72 | return -1; |
73 | } | |
74 | ||
2c91d3d3 AT |
75 | p = strchr(path,'/'); |
76 | if (p) *p = 0; | |
77 | io_printf(fd,"%s\n",path); | |
78 | if (p) *p = '/'; | |
79 | ||
3591c066 AT |
80 | while (1) { |
81 | if (!read_line(fd, line, sizeof(line)-1)) { | |
82 | return -1; | |
83 | } | |
31593dd6 | 84 | |
31593dd6 | 85 | if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) { |
bcb7e502 | 86 | auth_client(fd, user, line+18); |
31593dd6 AT |
87 | continue; |
88 | } | |
31593dd6 | 89 | |
3591c066 AT |
90 | if (strcmp(line,"@RSYNCD: OK") == 0) break; |
91 | rprintf(FINFO,"%s\n", line); | |
92 | } | |
93 | ||
94 | for (i=0;i<sargc;i++) { | |
95 | io_printf(fd,"%s\n", sargs[i]); | |
96 | } | |
97 | io_printf(fd,"\n"); | |
98 | ||
8d9dc9f9 AT |
99 | if (remote_version > 17 && !am_sender) |
100 | io_start_multiplex_in(fd); | |
101 | ||
3591c066 AT |
102 | return client_run(fd, fd, -1, argc, argv); |
103 | } | |
104 | ||
105 | ||
106 | ||
107 | static int rsync_module(int fd, int i) | |
108 | { | |
109 | int argc=0; | |
110 | char *argv[MAX_ARGS]; | |
111 | char **argp; | |
e42c9458 | 112 | char line[MAXPATHLEN]; |
1a016bfd AT |
113 | uid_t uid = (uid_t)-2; |
114 | gid_t gid = (gid_t)-2; | |
8ef4ffd6 | 115 | char *p; |
56c473b7 AT |
116 | char *addr = client_addr(fd); |
117 | char *host = client_name(fd); | |
874895d5 | 118 | char *name = lp_name(i); |
d0d56395 | 119 | char *user; |
874895d5 | 120 | int start_glob=0; |
7b372642 AT |
121 | char *request=NULL; |
122 | extern int am_sender; | |
8d9dc9f9 | 123 | extern int remote_version; |
943882a2 | 124 | extern int am_root; |
56c473b7 AT |
125 | |
126 | if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) { | |
127 | rprintf(FERROR,"rsync denied on module %s from %s (%s)\n", | |
874895d5 | 128 | name, client_name(fd), client_addr(fd)); |
c8e78d87 | 129 | io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n", |
874895d5 | 130 | name, client_name(fd), client_addr(fd)); |
56c473b7 AT |
131 | return -1; |
132 | } | |
3591c066 | 133 | |
0c515f17 | 134 | if (!claim_connection(lp_lock_file(), lp_max_connections())) { |
8d72ef6e AT |
135 | if (errno) { |
136 | rprintf(FERROR,"failed to open lock file %s : %s\n", | |
137 | lp_lock_file(), strerror(errno)); | |
138 | io_printf(fd,"@ERROR: failed to open lock file %s : %s\n", | |
139 | lp_lock_file(), strerror(errno)); | |
140 | } else { | |
141 | rprintf(FERROR,"max connections (%d) reached\n", | |
142 | lp_max_connections()); | |
143 | io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections()); | |
144 | } | |
0c515f17 AT |
145 | return -1; |
146 | } | |
147 | ||
31593dd6 | 148 | |
d0d56395 AT |
149 | user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD "); |
150 | ||
151 | if (!user) { | |
152 | rprintf(FERROR,"auth failed on module %s from %s (%s)\n", | |
153 | name, client_name(fd), client_addr(fd)); | |
154 | io_printf(fd,"@ERROR: auth failed on module %s\n",name); | |
155 | return -1; | |
156 | } | |
157 | ||
3591c066 AT |
158 | module_id = i; |
159 | ||
160 | if (lp_read_only(i)) | |
161 | read_only = 1; | |
162 | ||
8ef4ffd6 AT |
163 | p = lp_uid(i); |
164 | if (!name_to_uid(p, &uid)) { | |
165 | if (!isdigit(*p)) { | |
166 | rprintf(FERROR,"Invalid uid %s\n", p); | |
c8e78d87 | 167 | io_printf(fd,"@ERROR: invalid uid\n"); |
8ef4ffd6 AT |
168 | return -1; |
169 | } | |
170 | uid = atoi(p); | |
171 | } | |
172 | ||
173 | p = lp_gid(i); | |
174 | if (!name_to_gid(p, &gid)) { | |
175 | if (!isdigit(*p)) { | |
176 | rprintf(FERROR,"Invalid gid %s\n", p); | |
c8e78d87 | 177 | io_printf(fd,"@ERROR: invalid gid\n"); |
8ef4ffd6 AT |
178 | return -1; |
179 | } | |
180 | gid = atoi(p); | |
181 | } | |
182 | ||
8f3a2d54 | 183 | p = lp_exclude_from(i); |
2b6b4d53 | 184 | add_exclude_file(p, 1, 0); |
8f3a2d54 | 185 | |
5805327b | 186 | p = lp_exclude(i); |
8f3a2d54 AT |
187 | add_exclude_line(p); |
188 | ||
1a016bfd AT |
189 | log_open(); |
190 | ||
3591c066 | 191 | if (chroot(lp_path(i))) { |
1a016bfd | 192 | rprintf(FERROR,"chroot %s failed\n", lp_path(i)); |
3591c066 AT |
193 | io_printf(fd,"@ERROR: chroot failed\n"); |
194 | return -1; | |
195 | } | |
196 | ||
197 | if (chdir("/")) { | |
1a016bfd | 198 | rprintf(FERROR,"chdir %s failed\n", lp_path(i)); |
3591c066 AT |
199 | io_printf(fd,"@ERROR: chdir failed\n"); |
200 | return -1; | |
201 | } | |
202 | ||
1a016bfd AT |
203 | if (setgid(gid) || getgid() != gid) { |
204 | rprintf(FERROR,"setgid %d failed\n", gid); | |
3591c066 AT |
205 | io_printf(fd,"@ERROR: setgid failed\n"); |
206 | return -1; | |
207 | } | |
208 | ||
1a016bfd AT |
209 | if (setuid(uid) || getuid() != uid) { |
210 | rprintf(FERROR,"setuid %d failed\n", uid); | |
3591c066 AT |
211 | io_printf(fd,"@ERROR: setuid failed\n"); |
212 | return -1; | |
213 | } | |
214 | ||
943882a2 AT |
215 | am_root = (getuid() == 0); |
216 | ||
3591c066 AT |
217 | io_printf(fd,"@RSYNCD: OK\n"); |
218 | ||
219 | argv[argc++] = "rsyncd"; | |
220 | ||
221 | while (1) { | |
222 | if (!read_line(fd, line, sizeof(line)-1)) { | |
223 | return -1; | |
224 | } | |
225 | ||
226 | if (!*line) break; | |
227 | ||
874895d5 AT |
228 | p = line; |
229 | ||
874895d5 | 230 | argv[argc] = strdup(p); |
3591c066 AT |
231 | if (!argv[argc]) { |
232 | return -1; | |
233 | } | |
234 | ||
874895d5 | 235 | if (start_glob) { |
1a016bfd | 236 | if (start_glob == 1) { |
7b372642 | 237 | request = strdup(p); |
1a016bfd AT |
238 | start_glob++; |
239 | } | |
087bf010 | 240 | glob_expand(name, argv, &argc, MAX_ARGS); |
874895d5 AT |
241 | } else { |
242 | argc++; | |
243 | } | |
244 | ||
245 | if (strcmp(line,".") == 0) { | |
246 | start_glob = 1; | |
247 | } | |
248 | ||
3591c066 AT |
249 | if (argc == MAX_ARGS) { |
250 | return -1; | |
251 | } | |
252 | } | |
253 | ||
254 | parse_arguments(argc, argv); | |
255 | ||
7b372642 | 256 | if (request) { |
d0d56395 AT |
257 | if (*user) { |
258 | rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n", | |
259 | am_sender?"on":"to", | |
260 | request, user, host, addr); | |
261 | } else { | |
262 | rprintf(FINFO,"rsync %s %s from %s (%s)\n", | |
263 | am_sender?"on":"to", | |
264 | request, host, addr); | |
265 | } | |
7b372642 AT |
266 | free(request); |
267 | } | |
268 | ||
3591c066 AT |
269 | /* don't allow the logs to be flooded too fast */ |
270 | if (verbose > 1) verbose = 1; | |
271 | ||
272 | argc -= optind; | |
273 | argp = argv + optind; | |
274 | optind = 0; | |
275 | ||
8d9dc9f9 AT |
276 | if (remote_version > 17 && am_sender) |
277 | io_start_multiplex_out(fd); | |
278 | ||
3591c066 AT |
279 | start_server(fd, fd, argc, argp); |
280 | ||
281 | return 0; | |
282 | } | |
283 | ||
7a6421fa AT |
284 | /* send a list of available modules to the client. Don't list those |
285 | with "list = False". */ | |
3591c066 AT |
286 | static void send_listing(int fd) |
287 | { | |
288 | int n = lp_numservices(); | |
289 | int i; | |
290 | ||
291 | for (i=0;i<n;i++) | |
292 | if (lp_list(i)) | |
293 | io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i)); | |
294 | } | |
295 | ||
296 | /* this is called when a socket connection is established to a client | |
297 | and we want to start talking. The setup of the system is done from | |
298 | here */ | |
299 | static int start_daemon(int fd) | |
300 | { | |
7a6421fa | 301 | char line[200]; |
3591c066 | 302 | char *motd; |
8ef4ffd6 | 303 | int i = -1; |
4cdf25e4 | 304 | extern char *config_file; |
13c5fc0e | 305 | extern int remote_version; |
4cdf25e4 | 306 | |
f9e940ef | 307 | if (!lp_load(config_file, 0)) { |
4cdf25e4 AT |
308 | exit_cleanup(1); |
309 | } | |
3591c066 AT |
310 | |
311 | set_socket_options(fd,"SO_KEEPALIVE"); | |
a6801c39 AT |
312 | set_socket_options(fd,lp_socket_options()); |
313 | ||
3591c066 AT |
314 | |
315 | io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION); | |
316 | ||
317 | motd = lp_motd_file(); | |
318 | if (*motd) { | |
319 | FILE *f = fopen(motd,"r"); | |
320 | while (f && !feof(f)) { | |
321 | int len = fread(line, 1, sizeof(line)-1, f); | |
322 | if (len > 0) { | |
323 | line[len] = 0; | |
324 | io_printf(fd,"%s", line); | |
325 | } | |
326 | } | |
327 | if (f) fclose(f); | |
328 | io_printf(fd,"\n"); | |
329 | } | |
330 | ||
91eee594 AT |
331 | if (!read_line(fd, line, sizeof(line)-1)) { |
332 | return -1; | |
333 | } | |
334 | ||
335 | if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) { | |
c8e78d87 | 336 | io_printf(fd,"@ERROR: protocol startup error\n"); |
91eee594 AT |
337 | return -1; |
338 | } | |
339 | ||
8ef4ffd6 | 340 | while (i == -1) { |
3591c066 AT |
341 | line[0] = 0; |
342 | if (!read_line(fd, line, sizeof(line)-1)) { | |
343 | return -1; | |
344 | } | |
345 | ||
346 | if (!*line || strcmp(line,"#list")==0) { | |
347 | send_listing(fd); | |
348 | return -1; | |
349 | } | |
350 | ||
351 | if (*line == '#') { | |
352 | /* it's some sort of command that I don't understand */ | |
c8e78d87 | 353 | io_printf(fd,"@ERROR: Unknown command '%s'\n", line); |
3591c066 AT |
354 | return -1; |
355 | } | |
356 | ||
357 | i = lp_number(line); | |
358 | if (i == -1) { | |
c8e78d87 | 359 | io_printf(fd,"@ERROR: Unknown module '%s'\n", line); |
3591c066 AT |
360 | return -1; |
361 | } | |
3591c066 AT |
362 | } |
363 | ||
8ef4ffd6 | 364 | return rsync_module(fd, i); |
3591c066 AT |
365 | } |
366 | ||
367 | ||
368 | int daemon_main(void) | |
369 | { | |
f9e940ef | 370 | extern char *config_file; |
1a016bfd | 371 | |
5243c216 AT |
372 | /* this ensures that we don't call getcwd after the chroot, |
373 | which doesn't work on platforms that use popen("pwd","r") | |
374 | for getcwd */ | |
375 | push_dir("/", 0); | |
376 | ||
3591c066 AT |
377 | if (is_a_socket(STDIN_FILENO)) { |
378 | /* we are running via inetd */ | |
379 | return start_daemon(STDIN_FILENO); | |
380 | } | |
381 | ||
382 | become_daemon(); | |
383 | ||
f9e940ef AT |
384 | if (!lp_load(config_file, 1)) { |
385 | fprintf(stderr,"failed to load config file %s\n", config_file); | |
386 | exit_cleanup(1); | |
387 | } | |
388 | ||
389 | log_open(); | |
390 | ||
e42c9458 AT |
391 | rprintf(FINFO,"rsyncd version %s starting\n",VERSION); |
392 | ||
8ef4ffd6 AT |
393 | start_accept_loop(rsync_port, start_daemon); |
394 | return -1; | |
3591c066 AT |
395 | } |
396 |