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;
40 rprintf(FERROR,"ERROR: The remote path must start with a module name\n");
44 p = strchr(host, '@');
51 if (!user) user = getenv("USER");
52 if (!user) user = getenv("LOGNAME");
54 fd = open_socket_out(host, rsync_port);
56 exit_cleanup(RERR_SOCKETIO);
59 server_options(sargs,&sargc);
64 sargs[sargc++] = path;
68 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
70 if (!read_line(fd, line, sizeof(line)-1)) {
74 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
80 io_printf(fd,"%s\n",path);
84 if (!read_line(fd, line, sizeof(line)-1)) {
88 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
89 auth_client(fd, user, line+18);
93 if (strcmp(line,"@RSYNCD: OK") == 0) break;
94 rprintf(FINFO,"%s\n", line);
97 for (i=0;i<sargc;i++) {
98 io_printf(fd,"%s\n", sargs[i]);
102 if (remote_version > 17 && !am_sender)
103 io_start_multiplex_in(fd);
105 return client_run(fd, fd, -1, argc, argv);
110 static int rsync_module(int fd, int i)
113 char *argv[MAX_ARGS];
115 char line[MAXPATHLEN];
116 uid_t uid = (uid_t)-2;
117 gid_t gid = (gid_t)-2;
119 char *addr = client_addr(fd);
120 char *host = client_name(fd);
121 char *name = lp_name(i);
122 int use_chroot = lp_use_chroot(i);
126 extern int am_sender;
127 extern int remote_version;
130 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
131 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
132 name, client_name(fd), client_addr(fd));
133 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
134 name, client_name(fd), client_addr(fd));
138 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
140 rprintf(FERROR,"failed to open lock file %s : %s\n",
141 lp_lock_file(i), strerror(errno));
142 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
143 lp_lock_file(i), strerror(errno));
145 rprintf(FERROR,"max connections (%d) reached\n",
146 lp_max_connections(i));
147 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
153 auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
156 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
157 name, client_name(fd), client_addr(fd));
158 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
167 am_root = (getuid() == 0);
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");
191 p = lp_include_from(i);
192 add_exclude_file(p, 1, 1);
197 p = lp_exclude_from(i);
198 add_exclude_file(p, 1, 0);
206 if (chroot(lp_path(i))) {
207 rprintf(FERROR,"chroot %s failed\n", lp_path(i));
208 io_printf(fd,"@ERROR: chroot failed\n");
212 if (!push_dir("/", 0)) {
213 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
214 io_printf(fd,"@ERROR: chdir failed\n");
219 if (!push_dir(lp_path(i), 0)) {
220 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
221 io_printf(fd,"@ERROR: chdir failed\n");
228 rprintf(FERROR,"setgid %d failed\n", gid);
229 io_printf(fd,"@ERROR: setgid failed\n");
234 rprintf(FERROR,"setuid %d failed\n", uid);
235 io_printf(fd,"@ERROR: setuid failed\n");
239 am_root = (getuid() == 0);
242 io_printf(fd,"@RSYNCD: OK\n");
244 argv[argc++] = "rsyncd";
247 if (!read_line(fd, line, sizeof(line)-1)) {
255 argv[argc] = strdup(p);
261 if (start_glob == 1) {
265 glob_expand(name, argv, &argc, MAX_ARGS, !use_chroot);
270 if (strcmp(line,".") == 0) {
274 if (argc == MAX_ARGS) {
281 * Note that this is applied to all parameters, whether or not
282 * they are filenames, but no other legal parameters contain
283 * the forms that need to be sanitized so it doesn't hurt;
284 * it is not known at this point which parameters are files
287 for (i = 1; i < argc; i++) {
288 sanitize_path(argv[i]);
292 ret = parse_arguments(argc, argv, 0);
296 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
298 request, auth_user, host, addr);
300 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
302 request, host, addr);
308 /* don't allow the logs to be flooded too fast */
309 if (verbose > 1) verbose = 1;
313 argp = argv + optind;
316 if (remote_version > 17 && am_sender)
317 io_start_multiplex_out(fd);
324 extern int io_timeout;
325 io_timeout = lp_timeout(i);
328 start_server(fd, fd, argc, argp);
333 /* send a list of available modules to the client. Don't list those
334 with "list = False". */
335 static void send_listing(int fd)
337 int n = lp_numservices();
342 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
345 /* this is called when a socket connection is established to a client
346 and we want to start talking. The setup of the system is done from
348 static int start_daemon(int fd)
353 extern char *config_file;
354 extern int remote_version;
356 if (!lp_load(config_file, 0)) {
357 exit_cleanup(RERR_SYNTAX);
360 set_socket_options(fd,"SO_KEEPALIVE");
361 set_socket_options(fd,lp_socket_options());
364 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
366 motd = lp_motd_file();
368 FILE *f = fopen(motd,"r");
369 while (f && !feof(f)) {
370 int len = fread(line, 1, sizeof(line)-1, f);
373 io_printf(fd,"%s", line);
380 if (!read_line(fd, line, sizeof(line)-1)) {
384 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
385 io_printf(fd,"@ERROR: protocol startup error\n");
391 if (!read_line(fd, line, sizeof(line)-1)) {
395 if (!*line || strcmp(line,"#list")==0) {
401 /* it's some sort of command that I don't understand */
402 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
408 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
413 return rsync_module(fd, i);
417 int daemon_main(void)
419 extern char *config_file;
422 if (is_a_socket(STDIN_FILENO)) {
425 /* we are running via inetd - close off stdout and
426 stderr so that library functions (and getopt) don't
427 try to use them. Redirect them to /dev/null */
430 open("/dev/null", O_RDWR);
433 set_nonblocking(STDIN_FILENO);
435 return start_daemon(STDIN_FILENO);
440 if (!lp_load(config_file, 1)) {
441 fprintf(stderr,"failed to load config file %s\n", config_file);
442 exit_cleanup(RERR_SYNTAX);
447 rprintf(FINFO,"rsyncd version %s starting\n",VERSION);
449 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
451 int pid = (int) getpid();
452 cleanup_set_pid(pid);
453 if ((f = fopen(lp_pid_file(), "w")) == NULL) {
455 fprintf(stderr,"failed to create pid file %s\n", pid_file);
456 exit_cleanup(RERR_FILEIO);
458 fprintf(f, "%d\n", pid);
462 start_accept_loop(rsync_port, start_daemon);