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;
42 rprintf(FERROR,"ERROR: The remote path must start with a module name\n");
46 p = strchr(host, '@');
53 if (!user) user = getenv("USER");
54 if (!user) user = getenv("LOGNAME");
56 fd = open_socket_out(host, rsync_port, &socket_address);
58 exit_cleanup(RERR_SOCKETIO);
61 server_options(sargs,&sargc);
66 sargs[sargc++] = path;
70 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
72 if (!read_line(fd, line, sizeof(line)-1)) {
76 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
82 io_printf(fd,"%s\n",path);
86 if (!read_line(fd, line, sizeof(line)-1)) {
90 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
91 auth_client(fd, user, line+18);
95 if (strcmp(line,"@RSYNCD: OK") == 0) break;
96 rprintf(FINFO,"%s\n", line);
99 for (i=0;i<sargc;i++) {
100 io_printf(fd,"%s\n", sargs[i]);
104 if (remote_version >= 22 || (remote_version > 17 && !am_sender))
105 io_start_multiplex_in(fd);
107 return client_run(fd, fd, -1, argc, argv);
112 static int rsync_module(int fd, int i)
115 char *argv[MAX_ARGS];
117 char line[MAXPATHLEN];
118 uid_t uid = (uid_t)-2;
119 gid_t gid = (gid_t)-2;
121 char *addr = client_addr(fd);
122 char *host = client_name(fd);
123 char *name = lp_name(i);
124 int use_chroot = lp_use_chroot(i);
128 extern int am_sender;
129 extern int remote_version;
132 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
133 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
134 name, client_name(fd), client_addr(fd));
135 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
136 name, client_name(fd), client_addr(fd));
140 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
142 rprintf(FERROR,"failed to open lock file %s : %s\n",
143 lp_lock_file(i), strerror(errno));
144 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
145 lp_lock_file(i), strerror(errno));
147 rprintf(FERROR,"max connections (%d) reached\n",
148 lp_max_connections(i));
149 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
155 auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
158 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
159 name, client_name(fd), client_addr(fd));
160 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
169 am_root = (getuid() == 0);
173 if (!name_to_uid(p, &uid)) {
175 rprintf(FERROR,"Invalid uid %s\n", p);
176 io_printf(fd,"@ERROR: invalid uid\n");
183 if (!name_to_gid(p, &gid)) {
185 rprintf(FERROR,"Invalid gid %s\n", p);
186 io_printf(fd,"@ERROR: invalid gid\n");
193 p = lp_include_from(i);
194 add_exclude_file(p, 1, 1);
199 p = lp_exclude_from(i);
200 add_exclude_file(p, 1, 0);
208 if (chroot(lp_path(i))) {
209 rprintf(FERROR,"chroot %s failed\n", lp_path(i));
210 io_printf(fd,"@ERROR: chroot failed\n");
214 if (!push_dir("/", 0)) {
215 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
216 io_printf(fd,"@ERROR: chdir failed\n");
221 if (!push_dir(lp_path(i), 0)) {
222 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
223 io_printf(fd,"@ERROR: chdir failed\n");
231 rprintf(FERROR,"setgid %d failed\n", gid);
232 io_printf(fd,"@ERROR: setgid failed\n");
237 rprintf(FERROR,"setuid %d failed\n", uid);
238 io_printf(fd,"@ERROR: setuid failed\n");
242 am_root = (getuid() == 0);
245 io_printf(fd,"@RSYNCD: OK\n");
247 argv[argc++] = "rsyncd";
250 if (!read_line(fd, line, sizeof(line)-1)) {
258 argv[argc] = strdup(p);
264 if (start_glob == 1) {
268 glob_expand(name, argv, &argc, MAX_ARGS);
273 if (strcmp(line,".") == 0) {
277 if (argc == MAX_ARGS) {
282 if (sanitize_paths) {
284 * Note that this is applied to all parameters, whether or not
285 * they are filenames, but no other legal parameters contain
286 * the forms that need to be sanitized so it doesn't hurt;
287 * it is not known at this point which parameters are files
290 for (i = 1; i < argc; i++) {
291 sanitize_path(argv[i], NULL);
295 ret = parse_arguments(argc, argv, 0);
299 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
301 request, auth_user, host, addr);
303 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
305 request, host, addr);
311 /* don't allow the logs to be flooded too fast */
312 if (verbose > 1) verbose = 1;
316 argp = argv + optind;
319 if (remote_version >= 22 || (remote_version > 17 && am_sender))
320 io_start_multiplex_out(fd);
323 extern int am_sender;
325 rprintf(FERROR,"ERROR: module is read only\n");
335 extern int io_timeout;
336 io_timeout = lp_timeout(i);
339 start_server(fd, fd, argc, argp);
344 /* send a list of available modules to the client. Don't list those
345 with "list = False". */
346 static void send_listing(int fd)
348 int n = lp_numservices();
353 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
356 /* this is called when a socket connection is established to a client
357 and we want to start talking. The setup of the system is done from
359 static int start_daemon(int fd)
364 extern char *config_file;
365 extern int remote_version;
367 if (!lp_load(config_file, 0)) {
368 exit_cleanup(RERR_SYNTAX);
371 set_socket_options(fd,"SO_KEEPALIVE");
372 set_socket_options(fd,lp_socket_options());
375 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
377 motd = lp_motd_file();
379 FILE *f = fopen(motd,"r");
380 while (f && !feof(f)) {
381 int len = fread(line, 1, sizeof(line)-1, f);
384 io_printf(fd,"%s", line);
391 if (!read_line(fd, line, sizeof(line)-1)) {
395 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
396 io_printf(fd,"@ERROR: protocol startup error\n");
402 if (!read_line(fd, line, sizeof(line)-1)) {
406 if (!*line || strcmp(line,"#list")==0) {
412 /* it's some sort of command that I don't understand */
413 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
419 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
424 return rsync_module(fd, i);
428 int daemon_main(void)
430 extern char *config_file;
431 extern int orig_umask;
434 if (is_a_socket(STDIN_FILENO)) {
437 /* we are running via inetd - close off stdout and
438 stderr so that library functions (and getopt) don't
439 try to use them. Redirect them to /dev/null */
442 open("/dev/null", O_RDWR);
445 return start_daemon(STDIN_FILENO);
450 if (!lp_load(config_file, 1)) {
451 fprintf(stderr,"failed to load config file %s\n", config_file);
452 exit_cleanup(RERR_SYNTAX);
457 rprintf(FINFO,"rsyncd version %s starting\n",VERSION);
459 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
462 int pid = (int) getpid();
463 cleanup_set_pid(pid);
464 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
465 0666 & ~orig_umask)) == -1) {
467 fprintf(stderr,"failed to create pid file %s\n", pid_file);
468 exit_cleanup(RERR_FILEIO);
470 slprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
471 write(fd, pidbuf, strlen(pidbuf));
475 start_accept_loop(rsync_port, start_daemon);