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(i), lp_max_connections(i))) {
143 rprintf(FERROR,"failed to open lock file %s : %s\n",
144 lp_lock_file(i), strerror(errno));
145 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
146 lp_lock_file(i), strerror(errno));
148 rprintf(FERROR,"max connections (%d) reached\n",
149 lp_max_connections(i));
150 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
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_include_from(i);
191 add_exclude_file(p, 1, 1);
196 p = lp_exclude_from(i);
197 add_exclude_file(p, 1, 0);
205 if (chroot(lp_path(i))) {
206 rprintf(FERROR,"chroot %s failed\n", lp_path(i));
207 io_printf(fd,"@ERROR: chroot failed\n");
212 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
213 io_printf(fd,"@ERROR: chdir failed\n");
217 if (setgid(gid) || getgid() != gid) {
218 rprintf(FERROR,"setgid %d failed\n", gid);
219 io_printf(fd,"@ERROR: setgid failed\n");
223 if (setuid(uid) || getuid() != uid) {
224 rprintf(FERROR,"setuid %d failed\n", uid);
225 io_printf(fd,"@ERROR: setuid failed\n");
230 if (!push_dir(lp_path(i), 0)) {
231 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
232 io_printf(fd,"@ERROR: chdir failed\n");
237 am_root = (getuid() == 0);
239 io_printf(fd,"@RSYNCD: OK\n");
241 argv[argc++] = "rsyncd";
244 if (!read_line(fd, line, sizeof(line)-1)) {
252 argv[argc] = strdup(p);
258 if (start_glob == 1) {
262 glob_expand(name, argv, &argc, MAX_ARGS, !use_chroot);
267 if (strcmp(line,".") == 0) {
271 if (argc == MAX_ARGS) {
278 * Note that this is applied to all parameters, whether or not
279 * they are filenames, but no other legal parameters contain
280 * the forms that need to be sanitized so it doesn't hurt;
281 * it is not known at this point which parameters are files
284 for (i = 1; i < argc; i++) {
285 sanitize_path(argv[i]);
289 ret = parse_arguments(argc, argv);
293 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
295 request, auth_user, host, addr);
297 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
299 request, host, addr);
305 /* don't allow the logs to be flooded too fast */
306 if (verbose > 1) verbose = 1;
310 argp = argv + optind;
313 if (remote_version > 17 && am_sender)
314 io_start_multiplex_out(fd);
321 extern int io_timeout;
322 io_timeout = lp_timeout(i);
325 start_server(fd, fd, argc, argp);
330 /* send a list of available modules to the client. Don't list those
331 with "list = False". */
332 static void send_listing(int fd)
334 int n = lp_numservices();
339 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
342 /* this is called when a socket connection is established to a client
343 and we want to start talking. The setup of the system is done from
345 static int start_daemon(int fd)
350 extern char *config_file;
351 extern int remote_version;
353 if (!lp_load(config_file, 0)) {
354 exit_cleanup(RERR_SYNTAX);
357 set_socket_options(fd,"SO_KEEPALIVE");
358 set_socket_options(fd,lp_socket_options());
361 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
363 motd = lp_motd_file();
365 FILE *f = fopen(motd,"r");
366 while (f && !feof(f)) {
367 int len = fread(line, 1, sizeof(line)-1, f);
370 io_printf(fd,"%s", line);
377 if (!read_line(fd, line, sizeof(line)-1)) {
381 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
382 io_printf(fd,"@ERROR: protocol startup error\n");
388 if (!read_line(fd, line, sizeof(line)-1)) {
392 if (!*line || strcmp(line,"#list")==0) {
398 /* it's some sort of command that I don't understand */
399 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
405 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
410 return rsync_module(fd, i);
414 int daemon_main(void)
416 extern char *config_file;
419 /* this ensures that we don't call getcwd after the chroot,
420 which doesn't work on platforms that use popen("pwd","r")
424 if (is_a_socket(STDIN_FILENO)) {
427 /* we are running via inetd - close off stdout and
428 stderr so that library functions (and getopt) don't
429 try to use them. Redirect them to /dev/null */
432 open("/dev/null", O_RDWR);
435 set_nonblocking(STDIN_FILENO);
437 return start_daemon(STDIN_FILENO);
442 if (!lp_load(config_file, 1)) {
443 fprintf(stderr,"failed to load config file %s\n", config_file);
444 exit_cleanup(RERR_SYNTAX);
449 rprintf(FINFO,"rsyncd version %s starting\n",VERSION);
451 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
453 int pid = (int) getpid();
454 cleanup_set_pid(pid);
455 if ((f = fopen(lp_pid_file(), "w")) == NULL) {
457 fprintf(stderr,"failed to create pid file %s\n", pid_file);
458 exit_cleanup(RERR_FILEIO);
460 fprintf(f, "%d\n", pid);
464 start_accept_loop(rsync_port, start_daemon);