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;
37 p = strchr(host, '@');
44 if (!user) user = getenv("USER");
45 if (!user) user = getenv("LOGNAME");
47 fd = open_socket_out(host, rsync_port);
52 server_options(sargs,&sargc);
57 sargs[sargc++] = path;
61 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
63 if (!read_line(fd, line, sizeof(line)-1)) {
67 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
73 io_printf(fd,"%s\n",path);
77 if (!read_line(fd, line, sizeof(line)-1)) {
81 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
82 auth_client(fd, user, line+18);
86 if (strcmp(line,"@RSYNCD: OK") == 0) break;
87 rprintf(FINFO,"%s\n", line);
90 for (i=0;i<sargc;i++) {
91 io_printf(fd,"%s\n", sargs[i]);
95 return client_run(fd, fd, -1, argc, argv);
100 static int rsync_module(int fd, int i)
103 char *argv[MAX_ARGS];
105 char line[MAXPATHLEN];
106 uid_t uid = (uid_t)-2;
107 gid_t gid = (gid_t)-2;
109 char *addr = client_addr(fd);
110 char *host = client_name(fd);
111 char *name = lp_name(i);
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 name, client_name(fd), client_addr(fd));
117 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
118 name, client_name(fd), client_addr(fd));
122 if (!auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ")) {
123 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
124 name, client_name(fd), client_addr(fd));
125 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
129 if (!claim_connection(lp_lock_file(), lp_max_connections())) {
130 rprintf(FERROR,"max connections (%d) reached\n",
131 lp_max_connections());
132 io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections());
143 if (!name_to_uid(p, &uid)) {
145 rprintf(FERROR,"Invalid uid %s\n", p);
146 io_printf(fd,"@ERROR: invalid uid\n");
153 if (!name_to_gid(p, &gid)) {
155 rprintf(FERROR,"Invalid gid %s\n", p);
156 io_printf(fd,"@ERROR: invalid gid\n");
162 p = lp_exclude_from(i);
163 add_exclude_file(p, 1);
170 if (chroot(lp_path(i))) {
171 rprintf(FERROR,"chroot %s failed\n", lp_path(i));
172 io_printf(fd,"@ERROR: chroot failed\n");
177 rprintf(FERROR,"chdir %s failed\n", lp_path(i));
178 io_printf(fd,"@ERROR: chdir failed\n");
182 if (setgid(gid) || getgid() != gid) {
183 rprintf(FERROR,"setgid %d failed\n", gid);
184 io_printf(fd,"@ERROR: setgid failed\n");
188 if (setuid(uid) || getuid() != uid) {
189 rprintf(FERROR,"setuid %d failed\n", uid);
190 io_printf(fd,"@ERROR: setuid failed\n");
194 io_printf(fd,"@RSYNCD: OK\n");
196 argv[argc++] = "rsyncd";
199 if (!read_line(fd, line, sizeof(line)-1)) {
207 argv[argc] = strdup(p);
213 if (start_glob == 1) {
214 rprintf(FINFO,"rsync on %s from %s (%s)\n",
218 glob_expand(name, argv, &argc, MAX_ARGS);
223 if (strcmp(line,".") == 0) {
227 if (argc == MAX_ARGS) {
232 parse_arguments(argc, argv);
234 /* don't allow the logs to be flooded too fast */
235 if (verbose > 1) verbose = 1;
238 argp = argv + optind;
241 start_server(fd, fd, argc, argp);
246 /* send a list of available modules to the client. Don't list those
247 with "list = False". */
248 static void send_listing(int fd)
250 int n = lp_numservices();
255 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
258 /* this is called when a socket connection is established to a client
259 and we want to start talking. The setup of the system is done from
261 static int start_daemon(int fd)
266 extern char *config_file;
267 extern int remote_version;
269 if (!lp_load(config_file, 0)) {
273 set_socket_options(fd,"SO_KEEPALIVE");
275 io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
277 motd = lp_motd_file();
279 FILE *f = fopen(motd,"r");
280 while (f && !feof(f)) {
281 int len = fread(line, 1, sizeof(line)-1, f);
284 io_printf(fd,"%s", line);
291 if (!read_line(fd, line, sizeof(line)-1)) {
295 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
296 io_printf(fd,"@ERROR: protocol startup error\n");
302 if (!read_line(fd, line, sizeof(line)-1)) {
306 if (!*line || strcmp(line,"#list")==0) {
312 /* it's some sort of command that I don't understand */
313 io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
319 io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
324 return rsync_module(fd, i);
328 int daemon_main(void)
330 extern char *config_file;
332 if (is_a_socket(STDIN_FILENO)) {
333 /* we are running via inetd */
334 return start_daemon(STDIN_FILENO);
339 if (!lp_load(config_file, 1)) {
340 fprintf(stderr,"failed to load config file %s\n", config_file);
346 rprintf(FINFO,"rsyncd version %s starting\n",VERSION);
348 start_accept_loop(rsync_port, start_daemon);