2 Copyright (C) Andrew Tridgell 1998
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.
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.
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.
19 /* the socket based protocol for setting up a connection wit rsyncd */
26 extern int rsync_port;
28 int sanitize_paths = 0;
30 int start_socket_client(char *host, char *path, int argc, char *argv[])
33 char *sargs[MAX_ARGS];
35 char line[MAXPATHLEN];
37 extern int remote_version;
39 extern struct in_addr socket_address;
40 extern int blocking_io;
43 rprintf(FERROR,"ERROR: The remote path must start with a module name\n");
47 p = strchr(host, '@');
54 if (!user) user = getenv("USER");
55 if (!user) user = getenv("LOGNAME");
59 fd = open_socket_out(host, rsync_port, &socket_address);
61 exit_cleanup(RERR_SOCKETIO);
64 server_options(sargs,&sargc);
69 sargs[sargc++] = path;
73 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
75 if (!read_line(fd, line, sizeof(line)-1)) {
79 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
85 io_printf(fd,"%s\n",path);
89 if (!read_line(fd, line, sizeof(line)-1)) {
93 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
94 auth_client(fd, user, line+18);
98 if (strcmp(line,"@RSYNCD: OK") == 0) break;
99 rprintf(FINFO,"%s\n", line);
102 for (i=0;i<sargc;i++) {
103 io_printf(fd,"%s\n", sargs[i]);
107 if (remote_version < 23) {
108 if (remote_version == 22 || (remote_version > 17 && !am_sender))
109 io_start_multiplex_in(fd);
112 return client_run(fd, fd, -1, argc, argv);
117 static int rsync_module(int fd, int i)
120 char *argv[MAX_ARGS];
122 char line[MAXPATHLEN];
123 uid_t uid = (uid_t)-2;
124 gid_t gid = (gid_t)-2;
126 char *addr = client_addr(fd);
127 char *host = client_name(fd);
128 char *name = lp_name(i);
129 int use_chroot = lp_use_chroot(i);
133 extern int am_sender;
134 extern int remote_version;
137 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
138 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
139 name, client_name(fd), client_addr(fd));
140 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
141 name, client_name(fd), client_addr(fd));
145 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
147 rprintf(FERROR,"failed to open lock file %s : %s\n",
148 lp_lock_file(i), strerror(errno));
149 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
150 lp_lock_file(i), strerror(errno));
152 rprintf(FERROR,"max connections (%d) reached\n",
153 lp_max_connections(i));
154 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
160 auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
163 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
164 name, client_name(fd), client_addr(fd));
165 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
171 am_root = (getuid() == 0);
175 if (!name_to_uid(p, &uid)) {
177 rprintf(FERROR,"Invalid uid %s\n", p);
178 io_printf(fd,"@ERROR: invalid uid\n");
185 if (!name_to_gid(p, &gid)) {
187 rprintf(FERROR,"Invalid gid %s\n", p);
188 io_printf(fd,"@ERROR: invalid gid\n");
195 p = lp_include_from(i);
196 add_exclude_file(p, 1, 1);
201 p = lp_exclude_from(i);
202 add_exclude_file(p, 1, 0);
210 if (chroot(lp_path(i))) {
211 rprintf(FERROR,"chroot %s failed\n", lp_path(i));
212 io_printf(fd,"@ERROR: chroot failed\n");
216 if (!push_dir("/", 0)) {
217 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
218 io_printf(fd,"@ERROR: chdir failed\n");
223 if (!push_dir(lp_path(i), 0)) {
224 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
225 io_printf(fd,"@ERROR: chdir failed\n");
233 rprintf(FERROR,"setgid %d failed\n", gid);
234 io_printf(fd,"@ERROR: setgid failed\n");
239 rprintf(FERROR,"setuid %d failed\n", uid);
240 io_printf(fd,"@ERROR: setuid failed\n");
244 am_root = (getuid() == 0);
247 io_printf(fd,"@RSYNCD: OK\n");
249 argv[argc++] = "rsyncd";
252 if (!read_line(fd, line, sizeof(line)-1)) {
260 argv[argc] = strdup(p);
266 if (start_glob == 1) {
270 glob_expand(name, argv, &argc, MAX_ARGS);
275 if (strcmp(line,".") == 0) {
279 if (argc == MAX_ARGS) {
284 if (sanitize_paths) {
286 * Note that this is applied to all parameters, whether or not
287 * they are filenames, but no other legal parameters contain
288 * the forms that need to be sanitized so it doesn't hurt;
289 * it is not known at this point which parameters are files
292 for (i = 1; i < argc; i++) {
293 sanitize_path(argv[i], NULL);
297 ret = parse_arguments(argc, argv, 0);
301 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
303 request, auth_user, host, addr);
305 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
307 request, host, addr);
313 /* don't allow the logs to be flooded too fast */
314 if (verbose > 1) verbose = 1;
318 argp = argv + optind;
321 if (remote_version < 23) {
322 if (remote_version == 22 || (remote_version > 17 && am_sender))
323 io_start_multiplex_out(fd);
331 extern int io_timeout;
332 io_timeout = lp_timeout(i);
335 start_server(fd, fd, argc, argp);
340 /* send a list of available modules to the client. Don't list those
341 with "list = False". */
342 static void send_listing(int fd)
344 int n = lp_numservices();
349 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
352 /* this is called when a socket connection is established to a client
353 and we want to start talking. The setup of the system is done from
355 static int start_daemon(int fd)
360 extern char *config_file;
361 extern int remote_version;
363 if (!lp_load(config_file, 0)) {
364 exit_cleanup(RERR_SYNTAX);
367 set_socket_options(fd,"SO_KEEPALIVE");
368 set_socket_options(fd,lp_socket_options());
371 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
373 motd = lp_motd_file();
375 FILE *f = fopen(motd,"r");
376 while (f && !feof(f)) {
377 int len = fread(line, 1, sizeof(line)-1, f);
380 io_printf(fd,"%s", line);
387 if (!read_line(fd, line, sizeof(line)-1)) {
391 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
392 io_printf(fd,"@ERROR: protocol startup error\n");
398 if (!read_line(fd, line, sizeof(line)-1)) {
402 if (!*line || strcmp(line,"#list")==0) {
408 /* it's some sort of command that I don't understand */
409 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
415 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
420 return rsync_module(fd, i);
424 int daemon_main(void)
426 extern char *config_file;
427 extern int orig_umask;
430 if (is_a_socket(STDIN_FILENO)) {
433 /* we are running via inetd - close off stdout and
434 stderr so that library functions (and getopt) don't
435 try to use them. Redirect them to /dev/null */
438 open("/dev/null", O_RDWR);
441 return start_daemon(STDIN_FILENO);
446 if (!lp_load(config_file, 1)) {
447 exit_cleanup(RERR_SYNTAX);
452 rprintf(FINFO,"rsyncd version %s starting\n",VERSION);
454 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
457 int pid = (int) getpid();
458 cleanup_set_pid(pid);
459 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
460 0666 & ~orig_umask)) == -1) {
462 rprintf(FLOG,"failed to create pid file %s\n", pid_file);
463 exit_cleanup(RERR_FILEIO);
465 slprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
466 write(fd, pidbuf, strlen(pidbuf));
470 start_accept_loop(rsync_port, start_daemon);