1 /* -*- c-file-style: "linux"; -*-
3 Copyright (C) 1998-2000 by Andrew Tridgell
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /* the socket based protocol for setting up a connection wit rsyncd */
27 extern int rsync_port;
29 int sanitize_paths = 0;
31 int start_socket_client(char *host, char *path, int argc, char *argv[])
34 char *sargs[MAX_ARGS];
36 char line[MAXPATHLEN];
38 extern int remote_version;
40 extern struct in_addr socket_address;
42 if (argc == 0 && !am_sender) {
48 rprintf(FERROR,"ERROR: The remote path must start with a module name\n");
52 p = strchr(host, '@');
59 if (!user) user = getenv("USER");
60 if (!user) user = getenv("LOGNAME");
62 fd = open_socket_out(host, rsync_port, &socket_address);
64 exit_cleanup(RERR_SOCKETIO);
67 server_options(sargs,&sargc);
72 sargs[sargc++] = path;
76 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
78 if (!read_line(fd, line, sizeof(line)-1)) {
82 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
88 io_printf(fd,"%s\n",path);
92 if (!read_line(fd, line, sizeof(line)-1)) {
96 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
97 auth_client(fd, user, line+18);
101 if (strcmp(line,"@RSYNCD: OK") == 0) break;
102 rprintf(FINFO,"%s\n", line);
105 for (i=0;i<sargc;i++) {
106 io_printf(fd,"%s\n", sargs[i]);
110 if (remote_version < 23) {
111 if (remote_version == 22 || (remote_version > 17 && !am_sender))
112 io_start_multiplex_in(fd);
115 return client_run(fd, fd, -1, argc, argv);
120 static int rsync_module(int fd, int i)
123 char *argv[MAX_ARGS];
125 char line[MAXPATHLEN];
126 uid_t uid = (uid_t)-2;
127 gid_t gid = (gid_t)-2;
129 char *addr = client_addr(fd);
130 char *host = client_name(fd);
131 char *name = lp_name(i);
132 int use_chroot = lp_use_chroot(i);
136 extern int am_sender;
137 extern int remote_version;
140 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
141 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
142 name, client_name(fd), client_addr(fd));
143 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
144 name, client_name(fd), client_addr(fd));
148 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
150 rprintf(FERROR,"failed to open lock file %s : %s\n",
151 lp_lock_file(i), strerror(errno));
152 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
153 lp_lock_file(i), strerror(errno));
155 rprintf(FERROR,"max connections (%d) reached\n",
156 lp_max_connections(i));
157 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
163 auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
166 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
167 name, client_name(fd), client_addr(fd));
168 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
174 am_root = (getuid() == 0);
178 if (!name_to_uid(p, &uid)) {
180 rprintf(FERROR,"Invalid uid %s\n", p);
181 io_printf(fd,"@ERROR: invalid uid\n");
188 if (!name_to_gid(p, &gid)) {
190 rprintf(FERROR,"Invalid gid %s\n", p);
191 io_printf(fd,"@ERROR: invalid gid\n");
198 p = lp_include_from(i);
199 add_exclude_file(p, 1, 1);
204 p = lp_exclude_from(i);
205 add_exclude_file(p, 1, 0);
213 if (chroot(lp_path(i))) {
214 rsyserr(FERROR, errno, "chroot %s failed", lp_path(i));
215 io_printf(fd,"@ERROR: chroot failed\n");
219 if (!push_dir("/", 0)) {
220 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
221 io_printf(fd,"@ERROR: chdir failed\n");
226 if (!push_dir(lp_path(i), 0)) {
227 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
228 io_printf(fd,"@ERROR: chdir failed\n");
236 rsyserr(FERROR, errno, "setgid %d failed", gid);
237 io_printf(fd,"@ERROR: setgid failed\n");
242 rsyserr(FERROR, errno, "setuid %d failed", uid);
243 io_printf(fd,"@ERROR: setuid failed\n");
247 am_root = (getuid() == 0);
250 io_printf(fd,"@RSYNCD: OK\n");
252 argv[argc++] = "rsyncd";
255 if (!read_line(fd, line, sizeof(line)-1)) {
263 argv[argc] = strdup(p);
269 if (start_glob == 1) {
273 glob_expand(name, argv, &argc, MAX_ARGS);
278 if (strcmp(line,".") == 0) {
282 if (argc == MAX_ARGS) {
287 if (sanitize_paths) {
289 * Note that this is applied to all parameters, whether or not
290 * they are filenames, but no other legal parameters contain
291 * the forms that need to be sanitized so it doesn't hurt;
292 * it is not known at this point which parameters are files
295 for (i = 1; i < argc; i++) {
296 sanitize_path(argv[i], NULL);
300 ret = parse_arguments(argc, argv, 0);
304 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
306 request, auth_user, host, addr);
308 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
310 request, host, addr);
316 /* don't allow the logs to be flooded too fast */
317 if (verbose > 1) verbose = 1;
321 argp = argv + optind;
324 if (remote_version < 23) {
325 if (remote_version == 22 || (remote_version > 17 && am_sender))
326 io_start_multiplex_out(fd);
334 extern int io_timeout;
335 io_timeout = lp_timeout(i);
338 start_server(fd, fd, argc, argp);
343 /* send a list of available modules to the client. Don't list those
344 with "list = False". */
345 static void send_listing(int fd)
347 int n = lp_numservices();
352 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
355 /* this is called when a socket connection is established to a client
356 and we want to start talking. The setup of the system is done from
358 static int start_daemon(int fd)
363 extern char *config_file;
364 extern int remote_version;
366 if (!lp_load(config_file, 0)) {
367 exit_cleanup(RERR_SYNTAX);
370 set_socket_options(fd,"SO_KEEPALIVE");
371 set_socket_options(fd,lp_socket_options());
374 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
376 motd = lp_motd_file();
378 FILE *f = fopen(motd,"r");
379 while (f && !feof(f)) {
380 int len = fread(line, 1, sizeof(line)-1, f);
383 io_printf(fd,"%s", line);
390 if (!read_line(fd, line, sizeof(line)-1)) {
394 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
395 io_printf(fd,"@ERROR: protocol startup error\n");
401 if (!read_line(fd, line, sizeof(line)-1)) {
405 if (!*line || strcmp(line,"#list")==0) {
411 /* it's some sort of command that I don't understand */
412 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
418 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
423 return rsync_module(fd, i);
427 int daemon_main(void)
429 extern char *config_file;
430 extern int orig_umask;
433 if (is_a_socket(STDIN_FILENO)) {
436 /* we are running via inetd - close off stdout and
437 stderr so that library functions (and getopt) don't
438 try to use them. Redirect them to /dev/null */
441 open("/dev/null", O_RDWR);
444 return start_daemon(STDIN_FILENO);
449 if (!lp_load(config_file, 1)) {
450 exit_cleanup(RERR_SYNTAX);
455 rprintf(FINFO,"rsyncd version %s starting\n",VERSION);
457 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
460 int pid = (int) getpid();
461 cleanup_set_pid(pid);
462 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
463 0666 & ~orig_umask)) == -1) {
465 rsyserr(FLOG, errno, "failed to create pid file %s", pid_file);
466 exit_cleanup(RERR_FILEIO);
468 slprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
469 write(fd, pidbuf, strlen(pidbuf));
473 start_accept_loop(rsync_port, start_daemon);