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