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 start_socket_client(char *host, char *path, int argc, char *argv[])
31 char *sargs[MAX_ARGS];
33 char line[MAXPATHLEN];
35 extern int remote_version;
39 p = strchr(host, '@');
46 if (!user) user = getenv("USER");
47 if (!user) user = getenv("LOGNAME");
51 fd = open_socket_out(host, rsync_port);
56 server_options(sargs,&sargc);
61 sargs[sargc++] = path;
65 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
67 if (!read_line(fd, line, sizeof(line)-1)) {
71 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
77 io_printf(fd,"%s\n",path);
81 if (!read_line(fd, line, sizeof(line)-1)) {
85 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
86 auth_client(fd, user, line+18);
90 if (strcmp(line,"@RSYNCD: OK") == 0) break;
91 rprintf(FINFO,"%s\n", line);
94 for (i=0;i<sargc;i++) {
95 io_printf(fd,"%s\n", sargs[i]);
99 if (remote_version > 17 && !am_sender)
100 io_start_multiplex_in(fd);
102 return client_run(fd, fd, -1, argc, argv);
107 static int rsync_module(int fd, int i)
110 char *argv[MAX_ARGS];
112 char line[MAXPATHLEN];
113 uid_t uid = (uid_t)-2;
114 gid_t gid = (gid_t)-2;
116 char *addr = client_addr(fd);
117 char *host = client_name(fd);
118 char *name = lp_name(i);
123 extern int am_sender;
124 extern int remote_version;
127 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
128 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
129 name, client_name(fd), client_addr(fd));
130 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
131 name, client_name(fd), client_addr(fd));
135 if (!claim_connection(lp_lock_file(), lp_max_connections())) {
137 rprintf(FERROR,"failed to open lock file %s : %s\n",
138 lp_lock_file(), strerror(errno));
139 io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
140 lp_lock_file(), strerror(errno));
142 rprintf(FERROR,"max connections (%d) reached\n",
143 lp_max_connections());
144 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections());
150 user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
153 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
154 name, client_name(fd), client_addr(fd));
155 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
165 if (!name_to_uid(p, &uid)) {
167 rprintf(FERROR,"Invalid uid %s\n", p);
168 io_printf(fd,"@ERROR: invalid uid\n");
175 if (!name_to_gid(p, &gid)) {
177 rprintf(FERROR,"Invalid gid %s\n", p);
178 io_printf(fd,"@ERROR: invalid gid\n");
184 p = lp_exclude_from(i);
185 add_exclude_file(p, 1, 0);
192 if (chroot(lp_path(i))) {
193 rprintf(FERROR,"chroot %s failed\n", lp_path(i));
194 io_printf(fd,"@ERROR: chroot failed\n");
199 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
200 io_printf(fd,"@ERROR: chdir failed\n");
204 if (setgid(gid) || getgid() != gid) {
205 rprintf(FERROR,"setgid %d failed\n", gid);
206 io_printf(fd,"@ERROR: setgid failed\n");
210 if (setuid(uid) || getuid() != uid) {
211 rprintf(FERROR,"setuid %d failed\n", uid);
212 io_printf(fd,"@ERROR: setuid failed\n");
216 am_root = (getuid() == 0);
218 io_printf(fd,"@RSYNCD: OK\n");
220 argv[argc++] = "rsyncd";
223 if (!read_line(fd, line, sizeof(line)-1)) {
231 argv[argc] = strdup(p);
237 if (start_glob == 1) {
241 glob_expand(name, argv, &argc, MAX_ARGS);
246 if (strcmp(line,".") == 0) {
250 if (argc == MAX_ARGS) {
255 ret = parse_arguments(argc, argv);
259 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
261 request, user, host, addr);
263 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
265 request, host, addr);
271 /* don't allow the logs to be flooded too fast */
272 if (verbose > 1) verbose = 1;
276 argp = argv + optind;
279 if (remote_version > 17 && am_sender)
280 io_start_multiplex_out(fd);
283 rprintf(FERROR,"Error parsing options (unsupported option?) - aborting\n");
287 start_server(fd, fd, argc, argp);
292 /* send a list of available modules to the client. Don't list those
293 with "list = False". */
294 static void send_listing(int fd)
296 int n = lp_numservices();
301 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
304 /* this is called when a socket connection is established to a client
305 and we want to start talking. The setup of the system is done from
307 static int start_daemon(int fd)
312 extern char *config_file;
313 extern int remote_version;
315 if (!lp_load(config_file, 0)) {
319 set_socket_options(fd,"SO_KEEPALIVE");
320 set_socket_options(fd,lp_socket_options());
323 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
325 motd = lp_motd_file();
327 FILE *f = fopen(motd,"r");
328 while (f && !feof(f)) {
329 int len = fread(line, 1, sizeof(line)-1, f);
332 io_printf(fd,"%s", line);
339 if (!read_line(fd, line, sizeof(line)-1)) {
343 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
344 io_printf(fd,"@ERROR: protocol startup error\n");
350 if (!read_line(fd, line, sizeof(line)-1)) {
354 if (!*line || strcmp(line,"#list")==0) {
360 /* it's some sort of command that I don't understand */
361 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
367 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
372 return rsync_module(fd, i);
376 int daemon_main(void)
378 extern char *config_file;
380 /* this ensures that we don't call getcwd after the chroot,
381 which doesn't work on platforms that use popen("pwd","r")
385 if (is_a_socket(STDIN_FILENO)) {
388 /* we are running via inetd - close off stdout and
389 stderr so that library functions (and getopt) don't
390 try to use them. Redirect them to /dev/null */
393 open("/dev/null", O_RDWR);
396 set_nonblocking(STDIN_FILENO);
398 return start_daemon(STDIN_FILENO);
403 if (!lp_load(config_file, 1)) {
404 fprintf(stderr,"failed to load config file %s\n", config_file);
410 rprintf(FINFO,"rsyncd version %s starting\n",VERSION);
412 start_accept_loop(rsync_port, start_daemon);