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;
29 int start_socket_client(char *host, char *path, int argc, char *argv[])
32 char *sargs[MAX_ARGS];
34 char line[MAXPATHLEN];
36 extern int remote_version;
41 rprintf(FERROR,"ERROR: The remote path must start with a module name\n");
45 p = strchr(host, '@');
52 if (!user) user = getenv("USER");
53 if (!user) user = getenv("LOGNAME");
57 fd = open_socket_out(host, rsync_port);
59 exit_cleanup(RERR_SOCKETIO);
62 server_options(sargs,&sargc);
67 sargs[sargc++] = path;
71 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
73 if (!read_line(fd, line, sizeof(line)-1)) {
77 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
83 io_printf(fd,"%s\n",path);
87 if (!read_line(fd, line, sizeof(line)-1)) {
91 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
92 auth_client(fd, user, line+18);
96 if (strcmp(line,"@RSYNCD: OK") == 0) break;
97 rprintf(FINFO,"%s\n", line);
100 for (i=0;i<sargc;i++) {
101 io_printf(fd,"%s\n", sargs[i]);
105 if (remote_version > 17 && !am_sender)
106 io_start_multiplex_in(fd);
108 return client_run(fd, fd, -1, argc, argv);
113 static int rsync_module(int fd, int i)
116 char *argv[MAX_ARGS];
118 char line[MAXPATHLEN];
119 uid_t uid = (uid_t)-2;
120 gid_t gid = (gid_t)-2;
122 char *addr = client_addr(fd);
123 char *host = client_name(fd);
124 char *name = lp_name(i);
125 int use_chroot = lp_use_chroot(i);
129 extern int am_sender;
130 extern int remote_version;
133 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
134 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
135 name, client_name(fd), client_addr(fd));
136 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
137 name, client_name(fd), client_addr(fd));
141 if (!claim_connection(lp_lock_file(), lp_max_connections())) {
143 rprintf(FERROR,"failed to open lock file %s : %s\n",
144 lp_lock_file(), strerror(errno));
145 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
146 lp_lock_file(), strerror(errno));
148 rprintf(FERROR,"max connections (%d) reached\n",
149 lp_max_connections());
150 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections());
156 auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
159 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
160 name, client_name(fd), client_addr(fd));
161 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
171 if (!name_to_uid(p, &uid)) {
173 rprintf(FERROR,"Invalid uid %s\n", p);
174 io_printf(fd,"@ERROR: invalid uid\n");
181 if (!name_to_gid(p, &gid)) {
183 rprintf(FERROR,"Invalid gid %s\n", p);
184 io_printf(fd,"@ERROR: invalid gid\n");
190 p = lp_exclude_from(i);
191 add_exclude_file(p, 1, 0);
199 if (chroot(lp_path(i))) {
200 rprintf(FERROR,"chroot %s failed\n", lp_path(i));
201 io_printf(fd,"@ERROR: chroot failed\n");
206 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
207 io_printf(fd,"@ERROR: chdir failed\n");
211 if (setgid(gid) || getgid() != gid) {
212 rprintf(FERROR,"setgid %d failed\n", gid);
213 io_printf(fd,"@ERROR: setgid failed\n");
217 if (setuid(uid) || getuid() != uid) {
218 rprintf(FERROR,"setuid %d failed\n", uid);
219 io_printf(fd,"@ERROR: setuid failed\n");
224 if (!push_dir(lp_path(i), 0)) {
225 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
226 io_printf(fd,"@ERROR: chdir failed\n");
231 am_root = (getuid() == 0);
233 io_printf(fd,"@RSYNCD: OK\n");
235 argv[argc++] = "rsyncd";
238 if (!read_line(fd, line, sizeof(line)-1)) {
246 argv[argc] = strdup(p);
252 if (start_glob == 1) {
256 glob_expand(name, argv, &argc, MAX_ARGS, !use_chroot);
261 if (strcmp(line,".") == 0) {
265 if (argc == MAX_ARGS) {
272 * Note that this is applied to all parameters, whether or not
273 * they are filenames, but no other legal parameters contain
274 * the forms that need to be sanitized so it doesn't hurt;
275 * it is not known at this point which parameters are files
278 for (i = 1; i < argc; i++) {
279 char *copy = sanitize_path(argv[i]);
280 free((void *)argv[i]);
285 ret = parse_arguments(argc, argv);
289 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
291 request, auth_user, host, addr);
293 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
295 request, host, addr);
301 /* don't allow the logs to be flooded too fast */
302 if (verbose > 1) verbose = 1;
306 argp = argv + optind;
309 if (remote_version > 17 && am_sender)
310 io_start_multiplex_out(fd);
313 rprintf(FERROR,"Error parsing options (unsupported option?) - aborting\n");
314 exit_cleanup(RERR_SYNTAX);
318 extern int io_timeout;
319 io_timeout = lp_timeout(i);
322 start_server(fd, fd, argc, argp);
327 /* send a list of available modules to the client. Don't list those
328 with "list = False". */
329 static void send_listing(int fd)
331 int n = lp_numservices();
336 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
339 /* this is called when a socket connection is established to a client
340 and we want to start talking. The setup of the system is done from
342 static int start_daemon(int fd)
347 extern char *config_file;
348 extern int remote_version;
350 if (!lp_load(config_file, 0)) {
351 exit_cleanup(RERR_SYNTAX);
354 set_socket_options(fd,"SO_KEEPALIVE");
355 set_socket_options(fd,lp_socket_options());
358 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
360 motd = lp_motd_file();
362 FILE *f = fopen(motd,"r");
363 while (f && !feof(f)) {
364 int len = fread(line, 1, sizeof(line)-1, f);
367 io_printf(fd,"%s", line);
374 if (!read_line(fd, line, sizeof(line)-1)) {
378 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
379 io_printf(fd,"@ERROR: protocol startup error\n");
385 if (!read_line(fd, line, sizeof(line)-1)) {
389 if (!*line || strcmp(line,"#list")==0) {
395 /* it's some sort of command that I don't understand */
396 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
402 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
407 return rsync_module(fd, i);
411 int daemon_main(void)
413 extern char *config_file;
416 /* this ensures that we don't call getcwd after the chroot,
417 which doesn't work on platforms that use popen("pwd","r")
421 if (is_a_socket(STDIN_FILENO)) {
424 /* we are running via inetd - close off stdout and
425 stderr so that library functions (and getopt) don't
426 try to use them. Redirect them to /dev/null */
429 open("/dev/null", O_RDWR);
432 set_nonblocking(STDIN_FILENO);
434 return start_daemon(STDIN_FILENO);
439 if (!lp_load(config_file, 1)) {
440 fprintf(stderr,"failed to load config file %s\n", config_file);
441 exit_cleanup(RERR_SYNTAX);
446 rprintf(FINFO,"rsyncd version %s starting\n",VERSION);
448 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
450 int pid = (int) getpid();
451 cleanup_set_pid(pid);
452 if ((f = fopen(lp_pid_file(), "w")) == NULL) {
454 fprintf(stderr,"failed to create pid file %s\n", pid_file);
455 exit_cleanup(RERR_FILEIO);
457 fprintf(f, "%d\n", pid);
461 start_accept_loop(rsync_port, start_daemon);