1 /* -*- c-file-style: "linux"; -*-
3 Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4 Copyright (C) 2001 by Martin Pool <mbp@samba.org>
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.
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.
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.
21 /* the socket based protocol for setting up a connection with rsyncd */
28 extern int rsync_port
;
30 int sanitize_paths
= 0;
33 * Run a client connected to an rsyncd. The alternative to this
34 * function for remote-shell connections is do_cmd.
36 int start_socket_client(char *host
, char *path
, int argc
, char *argv
[])
39 char *sargs
[MAX_ARGS
];
41 char line
[MAXPATHLEN
];
43 extern int remote_version
;
45 extern struct in_addr socket_address
;
46 extern char *shell_cmd
;
48 if (argc
== 0 && !am_sender
) {
53 /* This is just a friendliness enhancement: if the connection
54 * is to an rsyncd then there is no point specifying the -e option.
55 * Note that this is only set if the -e was explicitly specified,
56 * not if the environment variable just happens to be set.
57 * See http://lists.samba.org/pipermail/rsync/2000-September/002744.html
60 rprintf(FERROR
, "WARNING: --rsh or -e option ignored when "
61 "connecting to rsync daemon\n");
66 rprintf(FERROR
,"ERROR: The remote path must start with a module name not a /\n");
70 p
= strchr(host
, '@');
77 if (!user
) user
= getenv("USER");
78 if (!user
) user
= getenv("LOGNAME");
80 fd
= open_socket_out(host
, rsync_port
, &socket_address
);
82 exit_cleanup(RERR_SOCKETIO
);
85 server_options(sargs
,&sargc
);
90 sargs
[sargc
++] = path
;
94 io_printf(fd
,"@RSYNCD: %d\n", PROTOCOL_VERSION
);
96 if (!read_line(fd
, line
, sizeof(line
)-1)) {
100 if (sscanf(line
,"@RSYNCD: %d", &remote_version
) != 1) {
104 p
= strchr(path
,'/');
106 io_printf(fd
,"%s\n",path
);
110 if (!read_line(fd
, line
, sizeof(line
)-1)) {
114 if (strncmp(line
,"@RSYNCD: AUTHREQD ",18) == 0) {
115 auth_client(fd
, user
, line
+18);
119 if (strcmp(line
,"@RSYNCD: OK") == 0) break;
121 if (strcmp(line
,"@RSYNCD: EXIT") == 0) exit(0);
123 rprintf(FINFO
,"%s\n", line
);
126 for (i
=0;i
<sargc
;i
++) {
127 io_printf(fd
,"%s\n", sargs
[i
]);
131 if (remote_version
< 23) {
132 if (remote_version
== 22 || (remote_version
> 17 && !am_sender
))
133 io_start_multiplex_in(fd
);
136 return client_run(fd
, fd
, -1, argc
, argv
);
141 static int rsync_module(int fd
, int i
)
144 char *argv
[MAX_ARGS
];
146 char line
[MAXPATHLEN
];
147 uid_t uid
= (uid_t
)-2; /* canonically "nobody" */
148 gid_t gid
= (gid_t
)-2;
150 char *addr
= client_addr(fd
);
151 char *host
= client_name(fd
);
152 char *name
= lp_name(i
);
153 int use_chroot
= lp_use_chroot(i
);
157 extern int am_sender
;
158 extern int remote_version
;
161 if (!allow_access(addr
, host
, lp_hosts_allow(i
), lp_hosts_deny(i
))) {
162 rprintf(FERROR
,"rsync denied on module %s from %s (%s)\n",
163 name
, client_name(fd
), client_addr(fd
));
164 io_printf(fd
,"@ERROR: access denied to %s from %s (%s)\n",
165 name
, client_name(fd
), client_addr(fd
));
169 if (!claim_connection(lp_lock_file(i
), lp_max_connections(i
))) {
171 rprintf(FERROR
,"failed to open lock file %s : %s\n",
172 lp_lock_file(i
), strerror(errno
));
173 io_printf(fd
,"@ERROR: failed to open lock file %s : %s\n",
174 lp_lock_file(i
), strerror(errno
));
176 rprintf(FERROR
,"max connections (%d) reached\n",
177 lp_max_connections(i
));
178 io_printf(fd
,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i
));
184 auth_user
= auth_server(fd
, i
, addr
, "@RSYNCD: AUTHREQD ");
187 rprintf(FERROR
,"auth failed on module %s from %s (%s)\n",
188 name
, client_name(fd
), client_addr(fd
));
189 io_printf(fd
,"@ERROR: auth failed on module %s\n",name
);
195 am_root
= (getuid() == 0);
199 if (!name_to_uid(p
, &uid
)) {
201 rprintf(FERROR
,"Invalid uid %s\n", p
);
202 io_printf(fd
,"@ERROR: invalid uid %s\n", p
);
209 if (!name_to_gid(p
, &gid
)) {
211 rprintf(FERROR
,"Invalid gid %s\n", p
);
212 io_printf(fd
,"@ERROR: invalid gid %s\n", p
);
219 /* TODO: If we're not root, but the configuration requests
220 * that we change to some uid other than the current one, then
223 /* TODO: Perhaps take a list of gids, and make them into the
224 * supplementary groups. */
226 p
= lp_include_from(i
);
227 add_exclude_file(p
, 1, 1);
232 p
= lp_exclude_from(i
);
233 add_exclude_file(p
, 1, 0);
241 if (chroot(lp_path(i
))) {
242 rsyserr(FERROR
, errno
, "chroot %s failed", lp_path(i
));
243 io_printf(fd
,"@ERROR: chroot failed\n");
247 if (!push_dir("/", 0)) {
248 rsyserr(FERROR
, errno
, "chdir %s failed\n", lp_path(i
));
249 io_printf(fd
,"@ERROR: chdir failed\n");
254 if (!push_dir(lp_path(i
), 0)) {
255 rsyserr(FERROR
, errno
, "chdir %s failed\n", lp_path(i
));
256 io_printf(fd
,"@ERROR: chdir failed\n");
264 rsyserr(FERROR
, errno
, "setgid %d failed", (int) gid
);
265 io_printf(fd
,"@ERROR: setgid failed\n");
270 rsyserr(FERROR
, errno
, "setuid %d failed", (int) uid
);
271 io_printf(fd
,"@ERROR: setuid failed\n");
275 am_root
= (getuid() == 0);
278 io_printf(fd
,"@RSYNCD: OK\n");
280 argv
[argc
++] = "rsyncd";
283 if (!read_line(fd
, line
, sizeof(line
)-1)) {
291 argv
[argc
] = strdup(p
);
297 if (start_glob
== 1) {
301 glob_expand(name
, argv
, &argc
, MAX_ARGS
);
306 if (strcmp(line
,".") == 0) {
310 if (argc
== MAX_ARGS
) {
315 if (sanitize_paths
) {
317 * Note that this is applied to all parameters, whether or not
318 * they are filenames, but no other legal parameters contain
319 * the forms that need to be sanitized so it doesn't hurt;
320 * it is not known at this point which parameters are files
323 for (i
= 1; i
< argc
; i
++) {
324 sanitize_path(argv
[i
], NULL
);
329 ret
= parse_arguments(&argc
, (const char ***) &argp
, 0);
333 rprintf(FINFO
,"rsync %s %s from %s@%s (%s)\n",
335 request
, auth_user
, host
, addr
);
337 rprintf(FINFO
,"rsync %s %s from %s (%s)\n",
339 request
, host
, addr
);
345 /* don't allow the logs to be flooded too fast */
346 if (verbose
> 1) verbose
= 1;
349 if (remote_version
< 23) {
350 if (remote_version
== 22 || (remote_version
> 17 && am_sender
))
351 io_start_multiplex_out(fd
);
354 /* For later protocol versions, we don't start multiplexing
355 * until we've configured nonblocking in start_server. That
356 * means we're in a sticky situation now: there's no way to
357 * convey errors to the client. */
359 /* FIXME: Hold off on reporting option processing errors until
360 * we've set up nonblocking and multiplexed IO and can get the
361 * message back to them. */
364 exit_cleanup(RERR_UNSUPPORTED
);
368 extern int io_timeout
;
369 io_timeout
= lp_timeout(i
);
372 start_server(fd
, fd
, argc
, argp
);
377 /* send a list of available modules to the client. Don't list those
378 with "list = False". */
379 static void send_listing(int fd
)
381 int n
= lp_numservices();
386 io_printf(fd
, "%-15s\t%s\n", lp_name(i
), lp_comment(i
));
388 io_printf(fd
, "@RSYNCD: EXIT\n");
391 /* this is called when a socket connection is established to a client
392 and we want to start talking. The setup of the system is done from
394 static int start_daemon(int fd
)
399 extern char *config_file
;
400 extern int remote_version
;
402 if (!lp_load(config_file
, 0)) {
403 exit_cleanup(RERR_SYNTAX
);
406 set_socket_options(fd
,"SO_KEEPALIVE");
407 set_socket_options(fd
,lp_socket_options());
410 io_printf(fd
,"@RSYNCD: %d\n", PROTOCOL_VERSION
);
412 motd
= lp_motd_file();
414 FILE *f
= fopen(motd
,"r");
415 while (f
&& !feof(f
)) {
416 int len
= fread(line
, 1, sizeof(line
)-1, f
);
419 io_printf(fd
,"%s", line
);
426 if (!read_line(fd
, line
, sizeof(line
)-1)) {
430 if (sscanf(line
,"@RSYNCD: %d", &remote_version
) != 1) {
431 io_printf(fd
,"@ERROR: protocol startup error\n");
437 if (!read_line(fd
, line
, sizeof(line
)-1)) {
441 if (!*line
|| strcmp(line
,"#list")==0) {
447 /* it's some sort of command that I don't understand */
448 io_printf(fd
,"@ERROR: Unknown command '%s'\n", line
);
454 io_printf(fd
,"@ERROR: Unknown module '%s'\n", line
);
459 return rsync_module(fd
, i
);
463 int daemon_main(void)
465 extern char *config_file
;
466 extern int orig_umask
;
469 if (is_a_socket(STDIN_FILENO
)) {
472 /* we are running via inetd - close off stdout and
473 stderr so that library functions (and getopt) don't
474 try to use them. Redirect them to /dev/null */
477 open("/dev/null", O_RDWR
);
480 return start_daemon(STDIN_FILENO
);
485 if (!lp_load(config_file
, 1)) {
486 exit_cleanup(RERR_SYNTAX
);
491 rprintf(FINFO
, "rsyncd version %s starting, listening on port %d\n", VERSION
,
493 /* TODO: If listening on a particular address, then show that
496 if (((pid_file
= lp_pid_file()) != NULL
) && (*pid_file
!= '\0')) {
499 int pid
= (int) getpid();
500 cleanup_set_pid(pid
);
501 if ((fd
= do_open(lp_pid_file(), O_WRONLY
|O_CREAT
|O_TRUNC
,
502 0666 & ~orig_umask
)) == -1) {
504 rsyserr(FLOG
, errno
, "failed to create pid file %s", pid_file
);
505 exit_cleanup(RERR_FILEIO
);
507 snprintf(pidbuf
, sizeof(pidbuf
), "%d\n", pid
);
508 write(fd
, pidbuf
, strlen(pidbuf
));
512 start_accept_loop(rsync_port
, start_daemon
);