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];
35 extern int remote_version;
37 fd = open_socket_out(host, rsync_port);
42 server_options(sargs,&sargc);
47 sargs[sargc++] = path;
51 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
53 if (!read_line(fd, line, sizeof(line)-1)) {
57 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
63 io_printf(fd,"%s\n",path);
67 if (!read_line(fd, line, sizeof(line)-1)) {
72 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
73 auth_client(fd, line+18);
78 if (strcmp(line,"@RSYNCD: OK") == 0) break;
79 rprintf(FINFO,"%s\n", line);
82 for (i=0;i<sargc;i++) {
83 io_printf(fd,"%s\n", sargs[i]);
89 if (!read_line(fd, line, sizeof(line)-1)) {
92 rprintf(FINFO,"%s\n", line);
96 return client_run(fd, fd, -1, argc, argv);
101 static int rsync_module(int fd, int i)
104 char *argv[MAX_ARGS];
110 char *addr = client_addr(fd);
111 char *host = client_name(fd);
114 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
115 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
116 lp_name(i), client_name(fd), client_addr(fd));
121 if (!auth_server(fd, "@RSYNCD: AUTHREQD ")) {
122 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
123 lp_name(i), client_name(fd), client_addr(fd));
128 if (!claim_connection(lp_lock_file(), lp_max_connections())) {
129 rprintf(FERROR,"ERROR: max connections reached\n");
133 rprintf(FINFO,"rsync on module %s from %s (%s)\n",
134 lp_name(i), host, addr);
142 if (!name_to_uid(p, &uid)) {
144 rprintf(FERROR,"Invalid uid %s\n", p);
151 if (!name_to_gid(p, &gid)) {
153 rprintf(FERROR,"Invalid gid %s\n", p);
159 if (chroot(lp_path(i))) {
160 io_printf(fd,"@ERROR: chroot failed\n");
165 io_printf(fd,"@ERROR: chdir failed\n");
170 io_printf(fd,"@ERROR: setgid failed\n");
175 io_printf(fd,"@ERROR: setuid failed\n");
179 io_printf(fd,"@RSYNCD: OK\n");
181 argv[argc++] = "rsyncd";
184 if (!read_line(fd, line, sizeof(line)-1)) {
190 argv[argc] = strdup(line);
196 if (argc == MAX_ARGS) {
201 parse_arguments(argc, argv);
203 /* don't allow the logs to be flooded too fast */
204 if (verbose > 1) verbose = 1;
207 argp = argv + optind;
210 start_server(fd, fd, argc, argp);
215 /* send a list of available modules to the client. Don't list those
216 with "list = False". */
217 static void send_listing(int fd)
219 int n = lp_numservices();
224 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
227 /* this is called when a socket connection is established to a client
228 and we want to start talking. The setup of the system is done from
230 static int start_daemon(int fd)
235 extern char *config_file;
236 extern int remote_version;
238 if (!lp_load(config_file)) {
242 set_socket_options(fd,"SO_KEEPALIVE");
244 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
246 motd = lp_motd_file();
248 FILE *f = fopen(motd,"r");
249 while (f && !feof(f)) {
250 int len = fread(line, 1, sizeof(line)-1, f);
253 io_printf(fd,"%s", line);
260 if (!read_line(fd, line, sizeof(line)-1)) {
264 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
271 if (!read_line(fd, line, sizeof(line)-1)) {
275 if (!*line || strcmp(line,"#list")==0) {
281 /* it's some sort of command that I don't understand */
282 io_printf(fd,"ERROR: Unknown command '%s'\n", line);
288 io_printf(fd,"ERROR: Unknown module '%s'\n", line);
293 return rsync_module(fd, i);
297 int daemon_main(void)
299 if (is_a_socket(STDIN_FILENO)) {
300 /* we are running via inetd */
301 return start_daemon(STDIN_FILENO);
306 start_accept_loop(rsync_port, start_daemon);