Commit | Line | Data |
---|---|---|
a039749b | 1 | /* -*- c-file-style: "linux"; -*- |
4a7144ee | 2 | * |
a254fd97 MP |
3 | * Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org> |
4 | * Copyright (C) 2001-2002 by Martin Pool <mbp@samba.org> | |
4a7144ee | 5 | * |
a254fd97 MP |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
4a7144ee | 10 | * |
a254fd97 MP |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
4a7144ee | 15 | * |
a254fd97 MP |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
3591c066 | 20 | |
a254fd97 MP |
21 | /** |
22 | * @file | |
23 | * | |
24 | * The socket based protocol for setting up a connection with | |
25 | * rsyncd. | |
26 | **/ | |
3591c066 AT |
27 | |
28 | #include "rsync.h" | |
29 | ||
c0814b81 WD |
30 | extern int verbose; |
31 | extern int list_only; | |
6e195fe9 WD |
32 | extern int am_sender; |
33 | extern int am_server; | |
34 | extern int am_daemon; | |
35 | extern int am_root; | |
3591c066 | 36 | extern int rsync_port; |
8dad7fc6 | 37 | extern int kluge_around_eof; |
6e195fe9 | 38 | extern int daemon_over_rsh; |
b35d0d8e | 39 | extern int sanitize_paths; |
7c2a9e76 | 40 | extern int filesfrom_fd; |
daa598df WD |
41 | extern int remote_protocol; |
42 | extern int protocol_version; | |
6e195fe9 WD |
43 | extern int io_timeout; |
44 | extern int orig_umask; | |
45 | extern int no_detach; | |
46 | extern int default_af_hint; | |
47 | extern char *bind_address; | |
7842418b | 48 | extern struct filter_list_struct server_filter_list; |
6e195fe9 | 49 | extern char *config_file; |
bf4679e8 | 50 | extern char *files_from; |
fed1f3f4 | 51 | extern char *tmpdir; |
6e195fe9 WD |
52 | |
53 | char *auth_user; | |
18638730 | 54 | int read_only = 0; |
f39b2060 WD |
55 | int daemon_log_format_has_i = 0; |
56 | int daemon_log_format_has_o_or_i = 0; | |
211bc43b | 57 | int module_id = -1; |
3591c066 | 58 | |
e7bf7c01 WD |
59 | /* Length of lp_path() string when in daemon mode & not chrooted, else 0. */ |
60 | unsigned int module_dirlen = 0; | |
61 | ||
c95ca2a2 WD |
62 | #define MAX_REQ_LEN (MAXPATHLEN + 255) |
63 | ||
be2961da | 64 | /** |
bc363ea9 | 65 | * Run a client connected to an rsyncd. The alternative to this |
be2961da MP |
66 | * function for remote-shell connections is do_cmd(). |
67 | * | |
fdf88d75 MP |
68 | * After negotiating which module to use and reading the server's |
69 | * motd, this hands over to client_run(). Telling the server the | |
70 | * module will cause it to chroot/setuid/etc. | |
71 | * | |
72 | * Instead of doing a transfer, the client may at this stage instead | |
73 | * get a listing of remote modules and exit. | |
be2961da MP |
74 | * |
75 | * @return -1 for error in startup, or the result of client_run(). | |
d0829892 | 76 | * Either way, it eventually gets passed to exit_cleanup(). |
be2961da | 77 | **/ |
3591c066 AT |
78 | int start_socket_client(char *host, char *path, int argc, char *argv[]) |
79 | { | |
68f40ebb | 80 | int fd, ret; |
2e94e70e | 81 | char *p, *user = NULL; |
4a7144ee | 82 | |
1732b6c0 WD |
83 | /* This is redundant with code in start_inband_exchange(), but this |
84 | * short-circuits a problem in the client before we open a socket, | |
85 | * and the extra check won't hurt. */ | |
f98df1d9 | 86 | if (*path == '/') { |
1732b6c0 WD |
87 | rprintf(FERROR, |
88 | "ERROR: The remote path must start with a module name not a /\n"); | |
f98df1d9 AT |
89 | return -1; |
90 | } | |
91 | ||
b31c92ed | 92 | if ((p = strrchr(host, '@')) != NULL) { |
bcb7e502 AT |
93 | user = host; |
94 | host = p+1; | |
1732b6c0 | 95 | *p = '\0'; |
bcb7e502 AT |
96 | } |
97 | ||
837cbad9 WD |
98 | fd = open_socket_out_wrapped(host, rsync_port, bind_address, |
99 | default_af_hint); | |
df5cd107 | 100 | if (fd == -1) |
65417579 | 101 | exit_cleanup(RERR_SOCKETIO); |
68f40ebb WD |
102 | |
103 | ret = start_inband_exchange(user, path, fd, fd, argc); | |
104 | ||
180443af | 105 | return ret ? ret : client_run(fd, fd, -1, argc, argv); |
68f40ebb WD |
106 | } |
107 | ||
b9f592fb WD |
108 | int start_inband_exchange(char *user, char *path, int f_in, int f_out, |
109 | int argc) | |
68f40ebb WD |
110 | { |
111 | int i; | |
112 | char *sargs[MAX_ARGS]; | |
113 | int sargc = 0; | |
20accf4d | 114 | char line[BIGPATHBUFLEN]; |
68f40ebb | 115 | char *p; |
68f40ebb WD |
116 | |
117 | if (argc == 0 && !am_sender) | |
9bcb2595 | 118 | list_only |= 1; |
68f40ebb WD |
119 | |
120 | if (*path == '/') { | |
1732b6c0 WD |
121 | rprintf(FERROR, |
122 | "ERROR: The remote path must start with a module name\n"); | |
68f40ebb WD |
123 | return -1; |
124 | } | |
125 | ||
2e94e70e WD |
126 | if (!user) |
127 | user = getenv("USER"); | |
128 | if (!user) | |
129 | user = getenv("LOGNAME"); | |
68f40ebb | 130 | |
5a8543b8 | 131 | io_printf(f_out, "@RSYNCD: %d\n", protocol_version); |
91eee594 | 132 | |
2e94e70e | 133 | if (!read_line(f_in, line, sizeof line - 1)) { |
be2961da | 134 | rprintf(FERROR, "rsync: did not see server greeting\n"); |
3591c066 AT |
135 | return -1; |
136 | } | |
137 | ||
daa598df | 138 | if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) { |
be2961da MP |
139 | /* note that read_line strips of \n or \r */ |
140 | rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n", | |
141 | line); | |
3591c066 AT |
142 | return -1; |
143 | } | |
daa598df WD |
144 | if (protocol_version > remote_protocol) |
145 | protocol_version = remote_protocol; | |
3591c066 | 146 | |
9bcb2595 WD |
147 | if (list_only && protocol_version >= 29) |
148 | list_only |= 2; | |
149 | ||
150 | /* set daemon_over_rsh to false since we need to build the | |
151 | * true set of args passed through the rsh/ssh connection; | |
152 | * this is a no-op for direct-socket-connection mode */ | |
153 | daemon_over_rsh = 0; | |
154 | server_options(sargs, &sargc); | |
155 | ||
156 | sargs[sargc++] = "."; | |
157 | ||
158 | if (path && *path) | |
159 | sargs[sargc++] = path; | |
160 | ||
161 | sargs[sargc] = NULL; | |
162 | ||
163 | if (verbose > 1) | |
164 | print_child_argv(sargs); | |
165 | ||
2c91d3d3 AT |
166 | p = strchr(path,'/'); |
167 | if (p) *p = 0; | |
68f40ebb | 168 | io_printf(f_out, "%s\n", path); |
2c91d3d3 AT |
169 | if (p) *p = '/'; |
170 | ||
063393d6 MP |
171 | /* Old servers may just drop the connection here, |
172 | rather than sending a proper EXIT command. Yuck. */ | |
8dad7fc6 | 173 | kluge_around_eof = list_only && protocol_version < 25 ? 1 : 0; |
7a55d06e | 174 | |
063393d6 | 175 | while (1) { |
2e94e70e | 176 | if (!read_line(f_in, line, sizeof line - 1)) { |
be2961da | 177 | rprintf(FERROR, "rsync: didn't get server startup line\n"); |
3591c066 AT |
178 | return -1; |
179 | } | |
31593dd6 | 180 | |
31593dd6 | 181 | if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) { |
68f40ebb | 182 | auth_client(f_out, user, line+18); |
31593dd6 AT |
183 | continue; |
184 | } | |
31593dd6 | 185 | |
2e94e70e WD |
186 | if (strcmp(line,"@RSYNCD: OK") == 0) |
187 | break; | |
8f04bb36 | 188 | |
cae95647 MP |
189 | if (strcmp(line,"@RSYNCD: EXIT") == 0) { |
190 | /* This is sent by recent versions of the | |
191 | * server to terminate the listing of modules. | |
192 | * We don't want to go on and transfer | |
193 | * anything; just exit. */ | |
194 | exit(0); | |
195 | } | |
8f04bb36 | 196 | |
136ac7ec | 197 | if (strncmp(line, "@ERROR", 6) == 0) { |
1732b6c0 | 198 | rprintf(FERROR, "%s\n", line); |
136ac7ec MP |
199 | /* This is always fatal; the server will now |
200 | * close the socket. */ | |
180443af | 201 | return -1; |
136ac7ec | 202 | } |
180443af WD |
203 | |
204 | rprintf(FINFO, "%s\n", line); | |
3591c066 | 205 | } |
8dad7fc6 | 206 | kluge_around_eof = 0; |
3591c066 | 207 | |
68f40ebb WD |
208 | for (i = 0; i < sargc; i++) { |
209 | io_printf(f_out, "%s\n", sargs[i]); | |
3591c066 | 210 | } |
68f40ebb | 211 | io_printf(f_out, "\n"); |
3591c066 | 212 | |
daa598df | 213 | if (protocol_version < 23) { |
3f55bd5d | 214 | if (protocol_version == 22 || !am_sender) |
da3478b2 | 215 | io_start_multiplex_in(); |
09b7f5db | 216 | } |
8d9dc9f9 | 217 | |
68f40ebb | 218 | return 0; |
3591c066 AT |
219 | } |
220 | ||
c95ca2a2 WD |
221 | static char *finish_pre_exec(pid_t pid, int fd, char *request) |
222 | { | |
223 | int status = -1; | |
3591c066 | 224 | |
c95ca2a2 WD |
225 | if (request) { |
226 | int len = strlen(request); | |
227 | if (len > MAX_REQ_LEN) | |
228 | len = MAX_REQ_LEN; | |
229 | write(fd, request, len); | |
230 | } | |
231 | close(fd); | |
232 | ||
233 | if (wait_process(pid, &status, 0) < 0 | |
234 | || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { | |
235 | char *e; | |
236 | if (asprintf(&e, "pre-xfer exec returned failure (%d)\n", status) < 0) | |
237 | out_of_memory("finish_pre_exec"); | |
238 | return e; | |
239 | } | |
240 | return NULL; | |
241 | } | |
3591c066 | 242 | |
68f40ebb | 243 | static int rsync_module(int f_in, int f_out, int i) |
3591c066 | 244 | { |
2e94e70e | 245 | int argc = 0; |
b7061c82 WD |
246 | int maxargs; |
247 | char **argv; | |
20accf4d | 248 | char line[BIGPATHBUFLEN]; |
2af27ad9 | 249 | uid_t uid = (uid_t)-2; /* canonically "nobody" */ |
1a016bfd | 250 | gid_t gid = (gid_t)-2; |
c95ca2a2 | 251 | char *p, *err_msg = NULL; |
09021eab DD |
252 | char *addr = client_addr(f_in); |
253 | char *host = client_name(f_in); | |
874895d5 | 254 | char *name = lp_name(i); |
8638dd48 | 255 | int use_chroot = lp_use_chroot(i); |
2e94e70e | 256 | int start_glob = 0; |
c95ca2a2 WD |
257 | int ret, pre_exec_fd = -1; |
258 | pid_t pre_exec_pid = 0; | |
2e94e70e | 259 | char *request = NULL; |
56c473b7 AT |
260 | |
261 | if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) { | |
1732b6c0 | 262 | rprintf(FLOG, "rsync denied on module %s from %s (%s)\n", |
31ec50d7 | 263 | name, host, addr); |
c0422cea WD |
264 | if (!lp_list(i)) |
265 | io_printf(f_out, "@ERROR: Unknown module '%s'\n", name); | |
266 | else { | |
267 | io_printf(f_out, | |
268 | "@ERROR: access denied to %s from %s (%s)\n", | |
269 | name, host, addr); | |
270 | } | |
56c473b7 AT |
271 | return -1; |
272 | } | |
3591c066 | 273 | |
68f40ebb | 274 | if (am_daemon && am_server) { |
9e5a5ddb | 275 | rprintf(FLOG, "rsync allowed access on module %s from %s (%s)\n", |
68f40ebb WD |
276 | name, host, addr); |
277 | } | |
278 | ||
5e71c444 | 279 | if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) { |
8d72ef6e | 280 | if (errno) { |
1732b6c0 | 281 | rsyserr(FLOG, errno, "failed to open lock file %s", |
4875d6b6 WD |
282 | safe_fname(lp_lock_file(i))); |
283 | io_printf(f_out, "@ERROR: failed to open lock file\n"); | |
8d72ef6e | 284 | } else { |
1732b6c0 | 285 | rprintf(FLOG, "max connections (%d) reached\n", |
5e71c444 | 286 | lp_max_connections(i)); |
4ccfd96c | 287 | io_printf(f_out, "@ERROR: max connections (%d) reached -- try again later\n", |
837cbad9 | 288 | lp_max_connections(i)); |
8d72ef6e | 289 | } |
0c515f17 AT |
290 | return -1; |
291 | } | |
292 | ||
45c5b903 | 293 | auth_user = auth_server(f_in, f_out, i, host, addr, "@RSYNCD: AUTHREQD "); |
d0d56395 | 294 | |
97cb8dc2 | 295 | if (!auth_user) { |
68f40ebb | 296 | io_printf(f_out, "@ERROR: auth failed on module %s\n", name); |
4a7144ee | 297 | return -1; |
d0d56395 AT |
298 | } |
299 | ||
3591c066 AT |
300 | module_id = i; |
301 | ||
211bc43b | 302 | if (lp_read_only(i)) |
18638730 WD |
303 | read_only = 1; |
304 | ||
f39b2060 | 305 | if (lp_transfer_logging(i)) { |
624d6be2 | 306 | if (log_format_has(lp_log_format(i), 'i')) |
f39b2060 WD |
307 | daemon_log_format_has_i = 1; |
308 | if (daemon_log_format_has_i | |
624d6be2 | 309 | || log_format_has(lp_log_format(i), 'o')) |
f39b2060 WD |
310 | daemon_log_format_has_o_or_i = 1; |
311 | } | |
c0814b81 | 312 | |
6fe05820 | 313 | am_root = (MY_UID() == 0); |
8ef4ffd6 | 314 | |
716baed7 DD |
315 | if (am_root) { |
316 | p = lp_uid(i); | |
317 | if (!name_to_uid(p, &uid)) { | |
2e94e70e | 318 | if (!isdigit(*(unsigned char *)p)) { |
1732b6c0 | 319 | rprintf(FLOG, "Invalid uid %s\n", p); |
68f40ebb | 320 | io_printf(f_out, "@ERROR: invalid uid %s\n", p); |
716baed7 | 321 | return -1; |
4a7144ee | 322 | } |
716baed7 DD |
323 | uid = atoi(p); |
324 | } | |
325 | ||
326 | p = lp_gid(i); | |
327 | if (!name_to_gid(p, &gid)) { | |
2e94e70e | 328 | if (!isdigit(*(unsigned char *)p)) { |
1732b6c0 | 329 | rprintf(FLOG, "Invalid gid %s\n", p); |
68f40ebb | 330 | io_printf(f_out, "@ERROR: invalid gid %s\n", p); |
716baed7 | 331 | return -1; |
4a7144ee | 332 | } |
716baed7 DD |
333 | gid = atoi(p); |
334 | } | |
8ef4ffd6 | 335 | } |
3b2b5345 | 336 | |
4a7144ee WD |
337 | /* TODO: If we're not root, but the configuration requests |
338 | * that we change to some uid other than the current one, then | |
339 | * log a warning. */ | |
340 | ||
341 | /* TODO: Perhaps take a list of gids, and make them into the | |
342 | * supplementary groups. */ | |
8ef4ffd6 | 343 | |
46bffd98 | 344 | if (use_chroot || (module_dirlen = strlen(lp_path(i))) == 1) { |
e7bf7c01 | 345 | module_dirlen = 0; |
7842418b | 346 | set_filter_dir("/", 1); |
46bffd98 | 347 | } else |
7842418b | 348 | set_filter_dir(lp_path(i), module_dirlen); |
e7bf7c01 WD |
349 | |
350 | p = lp_filter(i); | |
ebfd1a1c | 351 | parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT, |
2c8c8bba | 352 | XFLG_ABS_IF_SLASH); |
837cbad9 | 353 | |
cd64343a | 354 | p = lp_include_from(i); |
ebfd1a1c | 355 | parse_filter_file(&server_filter_list, p, MATCHFLG_INCLUDE, |
2c8c8bba | 356 | XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS); |
cd64343a DD |
357 | |
358 | p = lp_include(i); | |
ebfd1a1c | 359 | parse_rule(&server_filter_list, p, |
0a68f869 | 360 | MATCHFLG_INCLUDE | MATCHFLG_WORD_SPLIT, |
2c8c8bba | 361 | XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES); |
cd64343a | 362 | |
8f3a2d54 | 363 | p = lp_exclude_from(i); |
ebfd1a1c | 364 | parse_filter_file(&server_filter_list, p, 0, |
2c8c8bba | 365 | XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS); |
8f3a2d54 | 366 | |
5805327b | 367 | p = lp_exclude(i); |
ebfd1a1c | 368 | parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT, |
2c8c8bba | 369 | XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES); |
8f3a2d54 | 370 | |
45a83540 | 371 | log_init(); |
1a016bfd | 372 | |
9d0d18b5 | 373 | #ifdef HAVE_PUTENV |
c95ca2a2 | 374 | if (*lp_prexfer_exec(i) || *lp_postxfer_exec(i)) { |
9d0d18b5 WD |
375 | char *modname, *modpath, *hostaddr, *hostname, *username; |
376 | int status; | |
377 | if (asprintf(&modname, "RSYNC_MODULE_NAME=%s", name) < 0 | |
378 | || asprintf(&modpath, "RSYNC_MODULE_PATH=%s", lp_path(i)) < 0 | |
379 | || asprintf(&hostaddr, "RSYNC_HOST_ADDR=%s", addr) < 0 | |
380 | || asprintf(&hostname, "RSYNC_HOST_NAME=%s", host) < 0 | |
381 | || asprintf(&username, "RSYNC_USER_NAME=%s", auth_user) < 0) | |
382 | out_of_memory("rsync_module"); | |
383 | putenv(modname); | |
384 | putenv(modpath); | |
385 | putenv(hostaddr); | |
386 | putenv(hostname); | |
387 | putenv(username); | |
388 | umask(orig_umask); | |
c95ca2a2 WD |
389 | /* For post-xfer exec, fork a new process to run the rsync |
390 | * daemon while this process waits for the exit status and | |
391 | * runs the indicated command at that point. */ | |
392 | if (*lp_postxfer_exec(i)) { | |
9d0d18b5 WD |
393 | pid_t pid = fork(); |
394 | if (pid < 0) { | |
395 | rsyserr(FLOG, errno, "fork failed"); | |
396 | io_printf(f_out, "@ERROR: fork failed\n"); | |
397 | return -1; | |
398 | } | |
399 | if (pid) { | |
400 | char *ret1, *ret2; | |
c95ca2a2 WD |
401 | if (wait_process(pid, &status, 0) < 0) |
402 | status = -1; | |
9d0d18b5 WD |
403 | if (asprintf(&ret1, "RSYNC_RAW_STATUS=%d", status) > 0) |
404 | putenv(ret1); | |
405 | if (WIFEXITED(status)) | |
406 | status = WEXITSTATUS(status); | |
407 | else | |
408 | status = -1; | |
409 | if (asprintf(&ret2, "RSYNC_EXIT_STATUS=%d", status) > 0) | |
410 | putenv(ret2); | |
c95ca2a2 | 411 | system(lp_postxfer_exec(i)); |
9d0d18b5 WD |
412 | _exit(status); |
413 | } | |
414 | } | |
c95ca2a2 WD |
415 | /* For pre-xfer exec, fork a child process to run the indicated |
416 | * command, though it first waits for the parent process to | |
417 | * send us the user's request via a pipe. */ | |
418 | if (*lp_prexfer_exec(i)) { | |
419 | int fds[2]; | |
420 | if (pipe(fds) < 0 || (pre_exec_pid = fork()) < 0) { | |
421 | rsyserr(FLOG, errno, "pre-xfer exec preparation failed"); | |
422 | io_printf(f_out, "@ERROR: pre-xfer exec preparation failed\n"); | |
423 | return -1; | |
424 | } | |
425 | if (pre_exec_pid == 0) { | |
426 | char buf[MAX_REQ_LEN+1]; | |
427 | int len; | |
428 | close(fds[1]); | |
429 | set_blocking(fds[0]); | |
430 | len = read(fds[0], buf, MAX_REQ_LEN); | |
431 | close(fds[0]); | |
432 | if (len <= 0) | |
433 | _exit(1); | |
434 | buf[len] = '\0'; | |
435 | if (asprintf(&p, "RSYNC_REQUEST=%s", buf) < 0) | |
436 | out_of_memory("rsync_module"); | |
437 | putenv(p); | |
438 | close(STDIN_FILENO); | |
439 | close(STDOUT_FILENO); | |
440 | status = system(lp_prexfer_exec(i)); | |
441 | if (!WIFEXITED(status)) | |
442 | _exit(1); | |
443 | _exit(WEXITSTATUS(status)); | |
444 | } | |
445 | close(fds[0]); | |
446 | set_blocking(fds[1]); | |
447 | pre_exec_fd = fds[1]; | |
448 | } | |
9d0d18b5 WD |
449 | umask(0); |
450 | } | |
451 | #endif | |
452 | ||
8638dd48 | 453 | if (use_chroot) { |
b52c1d9d MP |
454 | /* |
455 | * XXX: The 'use chroot' flag is a fairly reliable | |
456 | * source of confusion, because it fails under two | |
457 | * important circumstances: running as non-root, | |
458 | * running on Win32 (or possibly others). On the | |
459 | * other hand, if you are running as root, then it | |
460 | * might be better to always use chroot. | |
461 | * | |
462 | * So, perhaps if we can't chroot we should just issue | |
463 | * a warning, unless a "require chroot" flag is set, | |
464 | * in which case we fail. | |
465 | */ | |
8638dd48 | 466 | if (chroot(lp_path(i))) { |
4875d6b6 WD |
467 | rsyserr(FLOG, errno, "chroot %s failed", |
468 | safe_fname(lp_path(i))); | |
68f40ebb | 469 | io_printf(f_out, "@ERROR: chroot failed\n"); |
8638dd48 DD |
470 | return -1; |
471 | } | |
3591c066 | 472 | |
59187666 | 473 | if (!push_dir("/")) { |
4875d6b6 WD |
474 | rsyserr(FLOG, errno, "chdir %s failed\n", |
475 | safe_fname(lp_path(i))); | |
68f40ebb | 476 | io_printf(f_out, "@ERROR: chdir failed\n"); |
8638dd48 DD |
477 | return -1; |
478 | } | |
3591c066 | 479 | |
716baed7 | 480 | } else { |
59187666 | 481 | if (!push_dir(lp_path(i))) { |
4875d6b6 WD |
482 | rsyserr(FLOG, errno, "chdir %s failed\n", |
483 | safe_fname(lp_path(i))); | |
68f40ebb | 484 | io_printf(f_out, "@ERROR: chdir failed\n"); |
716baed7 DD |
485 | return -1; |
486 | } | |
cb13abfe | 487 | sanitize_paths = 1; |
716baed7 DD |
488 | } |
489 | ||
490 | if (am_root) { | |
715d1f45 MP |
491 | /* XXXX: You could argue that if the daemon is started |
492 | * by a non-root user and they explicitly specify a | |
493 | * gid, then we should try to change to that gid -- | |
494 | * this could be possible if it's already in their | |
495 | * supplementary groups. */ | |
496 | ||
497 | /* TODO: Perhaps we need to document that if rsyncd is | |
498 | * started by somebody other than root it will inherit | |
499 | * all their supplementary groups. */ | |
500 | ||
716baed7 | 501 | if (setgid(gid)) { |
1732b6c0 | 502 | rsyserr(FLOG, errno, "setgid %d failed", (int)gid); |
68f40ebb | 503 | io_printf(f_out, "@ERROR: setgid failed\n"); |
8638dd48 DD |
504 | return -1; |
505 | } | |
4f5b0756 | 506 | #ifdef HAVE_SETGROUPS |
6969ebcf WD |
507 | /* Get rid of any supplementary groups this process |
508 | * might have inheristed. */ | |
509 | if (setgroups(1, &gid)) { | |
1732b6c0 | 510 | rsyserr(FLOG, errno, "setgroups failed"); |
6969ebcf WD |
511 | io_printf(f_out, "@ERROR: setgroups failed\n"); |
512 | return -1; | |
513 | } | |
514 | #endif | |
3591c066 | 515 | |
716baed7 | 516 | if (setuid(uid)) { |
1732b6c0 | 517 | rsyserr(FLOG, errno, "setuid %d failed", (int)uid); |
68f40ebb | 518 | io_printf(f_out, "@ERROR: setuid failed\n"); |
8638dd48 DD |
519 | return -1; |
520 | } | |
521 | ||
6fe05820 | 522 | am_root = (MY_UID() == 0); |
3591c066 AT |
523 | } |
524 | ||
fed1f3f4 WD |
525 | if (lp_temp_dir(i) && *lp_temp_dir(i)) { |
526 | tmpdir = lp_temp_dir(i); | |
527 | if (strlen(tmpdir) >= MAXPATHLEN - 10) { | |
528 | rprintf(FLOG, | |
529 | "the 'temp dir' value for %s is WAY too long -- ignoring.\n", | |
530 | name); | |
531 | tmpdir = NULL; | |
532 | } | |
533 | } | |
534 | ||
68f40ebb | 535 | io_printf(f_out, "@RSYNCD: OK\n"); |
3591c066 | 536 | |
b7061c82 WD |
537 | maxargs = MAX_ARGS; |
538 | if (!(argv = new_array(char *, maxargs))) | |
539 | out_of_memory("rsync_module"); | |
3591c066 AT |
540 | argv[argc++] = "rsyncd"; |
541 | ||
542 | while (1) { | |
2e94e70e | 543 | if (!read_line(f_in, line, sizeof line - 1)) |
3591c066 | 544 | return -1; |
3591c066 | 545 | |
2e94e70e WD |
546 | if (!*line) |
547 | break; | |
3591c066 | 548 | |
874895d5 AT |
549 | p = line; |
550 | ||
b7061c82 WD |
551 | if (argc == maxargs) { |
552 | maxargs += MAX_ARGS; | |
553 | if (!(argv = realloc_array(argv, char *, maxargs))) | |
554 | out_of_memory("rsync_module"); | |
555 | } | |
2e94e70e | 556 | if (!(argv[argc] = strdup(p))) |
b7061c82 | 557 | out_of_memory("rsync_module"); |
3591c066 | 558 | |
20accf4d WD |
559 | switch (start_glob) { |
560 | case 0: | |
874895d5 | 561 | argc++; |
20accf4d WD |
562 | if (strcmp(line, ".") == 0) |
563 | start_glob = 1; | |
564 | break; | |
565 | case 1: | |
c95ca2a2 WD |
566 | if (pre_exec_pid) { |
567 | err_msg = finish_pre_exec(pre_exec_pid, | |
568 | pre_exec_fd, p); | |
569 | pre_exec_pid = 0; | |
570 | } | |
20accf4d WD |
571 | request = strdup(p); |
572 | start_glob = 2; | |
573 | /* FALL THROUGH */ | |
574 | default: | |
c95ca2a2 WD |
575 | if (!err_msg) |
576 | glob_expand(name, &argv, &argc, &maxargs); | |
20accf4d WD |
577 | break; |
578 | } | |
3591c066 AT |
579 | } |
580 | ||
c95ca2a2 WD |
581 | if (pre_exec_pid) |
582 | err_msg = finish_pre_exec(pre_exec_pid, pre_exec_fd, request); | |
583 | ||
18638730 | 584 | verbose = 0; /* future verbosity is controlled by client options */ |
c9dc1300 | 585 | ret = parse_arguments(&argc, (const char ***) &argv, 0); |
3591c066 | 586 | |
7c2a9e76 WD |
587 | if (filesfrom_fd == 0) |
588 | filesfrom_fd = f_in; | |
589 | ||
7b372642 | 590 | if (request) { |
97cb8dc2 | 591 | if (*auth_user) { |
9e5a5ddb WD |
592 | rprintf(FLOG, "rsync %s %s from %s@%s (%s)\n", |
593 | am_sender ? "on" : "to", | |
97cb8dc2 | 594 | request, auth_user, host, addr); |
d0d56395 | 595 | } else { |
9e5a5ddb WD |
596 | rprintf(FLOG, "rsync %s %s from %s (%s)\n", |
597 | am_sender ? "on" : "to", | |
d0d56395 AT |
598 | request, host, addr); |
599 | } | |
7b372642 AT |
600 | free(request); |
601 | } | |
602 | ||
15b7b73d | 603 | #ifndef DEBUG |
3591c066 | 604 | /* don't allow the logs to be flooded too fast */ |
59b0e7a8 WD |
605 | if (verbose > lp_max_verbosity(i)) |
606 | verbose = lp_max_verbosity(i); | |
0199b05f | 607 | #endif |
3591c066 | 608 | |
1732b6c0 WD |
609 | if (protocol_version < 23 |
610 | && (protocol_version == 22 || am_sender)) | |
da3478b2 | 611 | io_start_multiplex_out(); |
c95ca2a2 | 612 | else if (!ret || err_msg) { |
1732b6c0 WD |
613 | /* We have to get I/O multiplexing started so that we can |
614 | * get the error back to the client. This means getting | |
615 | * the protocol setup finished first in later versions. */ | |
616 | setup_protocol(f_out, f_in); | |
b03c719f WD |
617 | if (!am_sender) { |
618 | /* Since we failed in our option parsing, we may not | |
619 | * have finished parsing that the client sent us a | |
620 | * --files-from option, so look for it manually. | |
621 | * Without this, the socket would be in the wrong | |
622 | * state for the upcoming error message. */ | |
623 | if (!files_from) { | |
624 | int i; | |
625 | for (i = 0; i < argc; i++) { | |
626 | if (strncmp(argv[i], "--files-from", 12) == 0) { | |
627 | files_from = ""; | |
628 | break; | |
629 | } | |
630 | } | |
631 | } | |
632 | if (files_from) | |
633 | write_byte(f_out, 0); | |
634 | } | |
da3478b2 | 635 | io_start_multiplex_out(); |
554e0a8d | 636 | } |
15b7b73d | 637 | |
c95ca2a2 WD |
638 | if (!ret || err_msg) { |
639 | if (err_msg) | |
640 | rprintf(FERROR, err_msg); | |
641 | else | |
642 | option_error(); | |
7e561438 | 643 | msleep(400); |
4a7144ee | 644 | exit_cleanup(RERR_UNSUPPORTED); |
41979ff8 AT |
645 | } |
646 | ||
3e6ddb37 WD |
647 | if (lp_timeout(i) && lp_timeout(i) > io_timeout) |
648 | set_io_timeout(lp_timeout(i)); | |
81791cfc | 649 | |
c9dc1300 | 650 | start_server(f_in, f_out, argc, argv); |
3591c066 AT |
651 | |
652 | return 0; | |
653 | } | |
654 | ||
7a6421fa AT |
655 | /* send a list of available modules to the client. Don't list those |
656 | with "list = False". */ | |
3591c066 AT |
657 | static void send_listing(int fd) |
658 | { | |
659 | int n = lp_numservices(); | |
660 | int i; | |
063393d6 | 661 | |
c0422cea | 662 | for (i = 0; i < n; i++) { |
3591c066 | 663 | if (lp_list(i)) |
4a7144ee | 664 | io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i)); |
c0422cea | 665 | } |
8f04bb36 | 666 | |
daa598df | 667 | if (protocol_version >= 25) |
063393d6 | 668 | io_printf(fd,"@RSYNCD: EXIT\n"); |
3591c066 AT |
669 | } |
670 | ||
68f40ebb | 671 | /* this is called when a connection is established to a client |
3591c066 AT |
672 | and we want to start talking. The setup of the system is done from |
673 | here */ | |
68f40ebb | 674 | int start_daemon(int f_in, int f_out) |
3591c066 | 675 | { |
20accf4d | 676 | char line[1024]; |
3591c066 | 677 | char *motd; |
c0422cea | 678 | int i; |
4cdf25e4 | 679 | |
da3478b2 WD |
680 | io_set_sock_fds(f_in, f_out); |
681 | ||
2e94e70e | 682 | if (!lp_load(config_file, 0)) |
65417579 | 683 | exit_cleanup(RERR_SYNTAX); |
3591c066 | 684 | |
30e8c8e1 DD |
685 | log_init(); |
686 | ||
68f40ebb WD |
687 | if (!am_server) { |
688 | set_socket_options(f_in, "SO_KEEPALIVE"); | |
689 | set_socket_options(f_in, lp_socket_options()); | |
690 | set_nonblocking(f_in); | |
691 | } | |
3591c066 | 692 | |
5a8543b8 | 693 | io_printf(f_out, "@RSYNCD: %d\n", protocol_version); |
3591c066 AT |
694 | |
695 | motd = lp_motd_file(); | |
27d3cdbc | 696 | if (motd && *motd) { |
0090cbdb | 697 | FILE *f = fopen(motd,"r"); |
3591c066 | 698 | while (f && !feof(f)) { |
2e94e70e | 699 | int len = fread(line, 1, sizeof line - 1, f); |
3591c066 AT |
700 | if (len > 0) { |
701 | line[len] = 0; | |
68f40ebb | 702 | io_printf(f_out, "%s", line); |
3591c066 AT |
703 | } |
704 | } | |
2e94e70e WD |
705 | if (f) |
706 | fclose(f); | |
68f40ebb | 707 | io_printf(f_out, "\n"); |
3591c066 AT |
708 | } |
709 | ||
2e94e70e | 710 | if (!read_line(f_in, line, sizeof line - 1)) |
91eee594 | 711 | return -1; |
91eee594 | 712 | |
daa598df | 713 | if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) { |
68f40ebb | 714 | io_printf(f_out, "@ERROR: protocol startup error\n"); |
91eee594 | 715 | return -1; |
4a7144ee | 716 | } |
daa598df WD |
717 | if (protocol_version > remote_protocol) |
718 | protocol_version = remote_protocol; | |
91eee594 | 719 | |
c0422cea WD |
720 | line[0] = 0; |
721 | if (!read_line(f_in, line, sizeof line - 1)) | |
722 | return -1; | |
3591c066 | 723 | |
c0422cea WD |
724 | if (!*line || strcmp(line, "#list") == 0) { |
725 | send_listing(f_out); | |
726 | return -1; | |
727 | } | |
3591c066 | 728 | |
c0422cea WD |
729 | if (*line == '#') { |
730 | /* it's some sort of command that I don't understand */ | |
731 | io_printf(f_out, "@ERROR: Unknown command '%s'\n", line); | |
732 | return -1; | |
733 | } | |
3591c066 | 734 | |
c0422cea WD |
735 | if ((i = lp_number(line)) < 0) { |
736 | char *addr = client_addr(f_in); | |
737 | char *host = client_name(f_in); | |
738 | rprintf(FLOG, "unknown module '%s' tried from %s (%s)\n", | |
739 | line, host, addr); | |
740 | io_printf(f_out, "@ERROR: Unknown module '%s'\n", line); | |
741 | return -1; | |
3591c066 AT |
742 | } |
743 | ||
68f40ebb | 744 | return rsync_module(f_in, f_out, i); |
3591c066 AT |
745 | } |
746 | ||
3591c066 AT |
747 | int daemon_main(void) |
748 | { | |
8638dd48 | 749 | char *pid_file; |
1a016bfd | 750 | |
3591c066 | 751 | if (is_a_socket(STDIN_FILENO)) { |
41979ff8 AT |
752 | int i; |
753 | ||
754 | /* we are running via inetd - close off stdout and | |
4a7144ee WD |
755 | * stderr so that library functions (and getopt) don't |
756 | * try to use them. Redirect them to /dev/null */ | |
2e94e70e | 757 | for (i = 1; i < 3; i++) { |
4a7144ee | 758 | close(i); |
41979ff8 AT |
759 | open("/dev/null", O_RDWR); |
760 | } | |
3eb38818 | 761 | |
68f40ebb | 762 | return start_daemon(STDIN_FILENO, STDIN_FILENO); |
3591c066 AT |
763 | } |
764 | ||
13e29995 | 765 | if (!no_detach) |
4a7144ee | 766 | become_daemon(); |
3591c066 | 767 | |
2e94e70e | 768 | if (!lp_load(config_file, 1)) |
65417579 | 769 | exit_cleanup(RERR_SYNTAX); |
f9e940ef | 770 | |
0c56b1ad WD |
771 | if (rsync_port == 0 && (rsync_port = lp_rsync_port()) == 0) |
772 | rsync_port = RSYNC_PORT; | |
3dfe6e97 | 773 | if (bind_address == NULL && *lp_bind_address()) |
986aaaaa | 774 | bind_address = lp_bind_address(); |
0c56b1ad | 775 | |
45a83540 | 776 | log_init(); |
f9e940ef | 777 | |
9e5a5ddb | 778 | rprintf(FLOG, "rsyncd version %s starting, listening on port %d\n", |
4a7144ee WD |
779 | RSYNC_VERSION, rsync_port); |
780 | /* TODO: If listening on a particular address, then show that | |
781 | * address too. In fact, why not just do inet_ntop on the | |
782 | * local address??? */ | |
e42c9458 | 783 | |
8638dd48 | 784 | if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) { |
ad517ce5 DD |
785 | char pidbuf[16]; |
786 | int fd; | |
05b7bab8 | 787 | pid_t pid = getpid(); |
8638dd48 | 788 | cleanup_set_pid(pid); |
ad517ce5 DD |
789 | if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC, |
790 | 0666 & ~orig_umask)) == -1) { | |
4a7144ee | 791 | cleanup_set_pid(0); |
4875d6b6 WD |
792 | rsyserr(FLOG, errno, "failed to create pid file %s", |
793 | safe_fname(pid_file)); | |
4a7144ee | 794 | exit_cleanup(RERR_FILEIO); |
8638dd48 | 795 | } |
05b7bab8 | 796 | snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid); |
ad517ce5 DD |
797 | write(fd, pidbuf, strlen(pidbuf)); |
798 | close(fd); | |
8638dd48 DD |
799 | } |
800 | ||
8ef4ffd6 AT |
801 | start_accept_loop(rsync_port, start_daemon); |
802 | return -1; | |
3591c066 | 803 | } |