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;
41 if (argc == 0 && !am_sender) {
47 rprintf(FERROR,"ERROR: The remote path must start with a module name\n");
51 p = strchr(host, '@');
58 if (!user) user = getenv("USER");
59 if (!user) user = getenv("LOGNAME");
61 fd = open_socket_out(host, rsync_port, &socket_address);
63 exit_cleanup(RERR_SOCKETIO);
66 server_options(sargs,&sargc);
71 sargs[sargc++] = path;
75 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
77 if (!read_line(fd, line, sizeof(line)-1)) {
81 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
87 io_printf(fd,"%s\n",path);
91 if (!read_line(fd, line, sizeof(line)-1)) {
95 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
96 auth_client(fd, user, line+18);
100 if (strcmp(line,"@RSYNCD: OK") == 0) break;
101 rprintf(FINFO,"%s\n", line);
104 for (i=0;i<sargc;i++) {
105 io_printf(fd,"%s\n", sargs[i]);
109 if (remote_version < 23) {
110 if (remote_version == 22 || (remote_version > 17 && !am_sender))
111 io_start_multiplex_in(fd);
114 return client_run(fd, fd, -1, argc, argv);
119 static int rsync_module(int fd, int i)
122 char *argv[MAX_ARGS];
124 char line[MAXPATHLEN];
125 uid_t uid = (uid_t)-2;
126 gid_t gid = (gid_t)-2;
128 char *addr = client_addr(fd);
129 char *host = client_name(fd);
130 char *name = lp_name(i);
131 int use_chroot = lp_use_chroot(i);
135 extern int am_sender;
136 extern int remote_version;
139 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
140 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
141 name, client_name(fd), client_addr(fd));
142 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
143 name, client_name(fd), client_addr(fd));
147 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
149 rprintf(FERROR,"failed to open lock file %s : %s\n",
150 lp_lock_file(i), strerror(errno));
151 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
152 lp_lock_file(i), strerror(errno));
154 rprintf(FERROR,"max connections (%d) reached\n",
155 lp_max_connections(i));
156 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
162 auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
165 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
166 name, client_name(fd), client_addr(fd));
167 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
173 am_root = (getuid() == 0);
177 if (!name_to_uid(p, &uid)) {
179 rprintf(FERROR,"Invalid uid %s\n", p);
180 io_printf(fd,"@ERROR: invalid uid\n");
187 if (!name_to_gid(p, &gid)) {
189 rprintf(FERROR,"Invalid gid %s\n", p);
190 io_printf(fd,"@ERROR: invalid gid\n");
197 p = lp_include_from(i);
198 add_exclude_file(p, 1, 1);
203 p = lp_exclude_from(i);
204 add_exclude_file(p, 1, 0);
212 if (chroot(lp_path(i))) {
213 rprintf(FERROR,"chroot %s failed\n", lp_path(i));
214 io_printf(fd,"@ERROR: chroot failed\n");
218 if (!push_dir("/", 0)) {
219 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
220 io_printf(fd,"@ERROR: chdir failed\n");
225 if (!push_dir(lp_path(i), 0)) {
226 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
227 io_printf(fd,"@ERROR: chdir failed\n");
235 rprintf(FERROR,"setgid %d failed\n", gid);
236 io_printf(fd,"@ERROR: setgid failed\n");
241 rprintf(FERROR,"setuid %d failed\n", uid);
242 io_printf(fd,"@ERROR: setuid failed\n");
246 am_root = (getuid() == 0);
249 io_printf(fd,"@RSYNCD: OK\n");
251 argv[argc++] = "rsyncd";
254 if (!read_line(fd, line, sizeof(line)-1)) {
262 argv[argc] = strdup(p);
268 if (start_glob == 1) {
272 glob_expand(name, argv, &argc, MAX_ARGS);
277 if (strcmp(line,".") == 0) {
281 if (argc == MAX_ARGS) {
286 if (sanitize_paths) {
288 * Note that this is applied to all parameters, whether or not
289 * they are filenames, but no other legal parameters contain
290 * the forms that need to be sanitized so it doesn't hurt;
291 * it is not known at this point which parameters are files
294 for (i = 1; i < argc; i++) {
295 sanitize_path(argv[i], NULL);
299 ret = parse_arguments(argc, argv, 0);
303 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
305 request, auth_user, host, addr);
307 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
309 request, host, addr);
315 /* don't allow the logs to be flooded too fast */
316 if (verbose > 1) verbose = 1;
320 argp = argv + optind;
323 if (remote_version < 23) {
324 if (remote_version == 22 || (remote_version > 17 && am_sender))
325 io_start_multiplex_out(fd);
333 extern int io_timeout;
334 io_timeout = lp_timeout(i);
337 start_server(fd, fd, argc, argp);
342 /* send a list of available modules to the client. Don't list those
343 with "list = False". */
344 static void send_listing(int fd)
346 int n = lp_numservices();
351 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
354 /* this is called when a socket connection is established to a client
355 and we want to start talking. The setup of the system is done from
357 static int start_daemon(int fd)
362 extern char *config_file;
363 extern int remote_version;
365 if (!lp_load(config_file, 0)) {
366 exit_cleanup(RERR_SYNTAX);
369 set_socket_options(fd,"SO_KEEPALIVE");
370 set_socket_options(fd,lp_socket_options());
373 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
375 motd = lp_motd_file();
377 FILE *f = fopen(motd,"r");
378 while (f && !feof(f)) {
379 int len = fread(line, 1, sizeof(line)-1, f);
382 io_printf(fd,"%s", line);
389 if (!read_line(fd, line, sizeof(line)-1)) {
393 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
394 io_printf(fd,"@ERROR: protocol startup error\n");
400 if (!read_line(fd, line, sizeof(line)-1)) {
404 if (!*line || strcmp(line,"#list")==0) {
410 /* it's some sort of command that I don't understand */
411 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
417 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
422 return rsync_module(fd, i);
426 int daemon_main(void)
428 extern char *config_file;
429 extern int orig_umask;
432 if (is_a_socket(STDIN_FILENO)) {
435 /* we are running via inetd - close off stdout and
436 stderr so that library functions (and getopt) don't
437 try to use them. Redirect them to /dev/null */
440 open("/dev/null", O_RDWR);
443 return start_daemon(STDIN_FILENO);
448 if (!lp_load(config_file, 1)) {
449 exit_cleanup(RERR_SYNTAX);
454 rprintf(FINFO,"rsyncd version %s starting\n",VERSION);
456 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
459 int pid = (int) getpid();
460 cleanup_set_pid(pid);
461 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
462 0666 & ~orig_umask)) == -1) {
464 rprintf(FLOG,"failed to create pid file %s\n", pid_file);
465 exit_cleanup(RERR_FILEIO);
467 slprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
468 write(fd, pidbuf, strlen(pidbuf));
472 start_accept_loop(rsync_port, start_daemon);