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 sanitize_paths
= 0;
30 int start_socket_client(char *host
, char *path
, int argc
, char *argv
[])
33 char *sargs
[MAX_ARGS
];
35 char line
[MAXPATHLEN
];
37 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");
55 fd
= open_socket_out(host
, rsync_port
);
57 exit_cleanup(RERR_SOCKETIO
);
60 server_options(sargs
,&sargc
);
65 sargs
[sargc
++] = path
;
69 io_printf(fd
,"@RSYNCD: %d\n", PROTOCOL_VERSION
);
71 if (!read_line(fd
, line
, sizeof(line
)-1)) {
75 if (sscanf(line
,"@RSYNCD: %d", &remote_version
) != 1) {
81 io_printf(fd
,"%s\n",path
);
85 if (!read_line(fd
, line
, sizeof(line
)-1)) {
89 if (strncmp(line
,"@RSYNCD: AUTHREQD ",18) == 0) {
90 auth_client(fd
, user
, line
+18);
94 if (strcmp(line
,"@RSYNCD: OK") == 0) break;
95 rprintf(FINFO
,"%s\n", line
);
98 for (i
=0;i
<sargc
;i
++) {
99 io_printf(fd
,"%s\n", sargs
[i
]);
103 if (remote_version
> 17 && !am_sender
)
104 io_start_multiplex_in(fd
);
106 return client_run(fd
, fd
, -1, argc
, argv
);
111 static int rsync_module(int fd
, int i
)
114 char *argv
[MAX_ARGS
];
116 char line
[MAXPATHLEN
];
117 uid_t uid
= (uid_t
)-2;
118 gid_t gid
= (gid_t
)-2;
120 char *addr
= client_addr(fd
);
121 char *host
= client_name(fd
);
122 char *name
= lp_name(i
);
123 int use_chroot
= lp_use_chroot(i
);
127 extern int am_sender
;
128 extern int remote_version
;
131 if (!allow_access(addr
, host
, lp_hosts_allow(i
), lp_hosts_deny(i
))) {
132 rprintf(FERROR
,"rsync denied on module %s from %s (%s)\n",
133 name
, client_name(fd
), client_addr(fd
));
134 io_printf(fd
,"@ERROR: access denied to %s from %s (%s)\n",
135 name
, client_name(fd
), client_addr(fd
));
139 if (!claim_connection(lp_lock_file(i
), lp_max_connections(i
))) {
141 rprintf(FERROR
,"failed to open lock file %s : %s\n",
142 lp_lock_file(i
), strerror(errno
));
143 io_printf(fd
,"@ERROR: failed to open lock file %s : %s\n",
144 lp_lock_file(i
), strerror(errno
));
146 rprintf(FERROR
,"max connections (%d) reached\n",
147 lp_max_connections(i
));
148 io_printf(fd
,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i
));
154 auth_user
= auth_server(fd
, i
, addr
, "@RSYNCD: AUTHREQD ");
157 rprintf(FERROR
,"auth failed on module %s from %s (%s)\n",
158 name
, client_name(fd
), client_addr(fd
));
159 io_printf(fd
,"@ERROR: auth failed on module %s\n",name
);
168 am_root
= (getuid() == 0);
172 if (!name_to_uid(p
, &uid
)) {
174 rprintf(FERROR
,"Invalid uid %s\n", p
);
175 io_printf(fd
,"@ERROR: invalid uid\n");
182 if (!name_to_gid(p
, &gid
)) {
184 rprintf(FERROR
,"Invalid gid %s\n", p
);
185 io_printf(fd
,"@ERROR: invalid gid\n");
192 p
= lp_include_from(i
);
193 add_exclude_file(p
, 1, 1);
198 p
= lp_exclude_from(i
);
199 add_exclude_file(p
, 1, 0);
207 if (chroot(lp_path(i
))) {
208 rprintf(FERROR
,"chroot %s failed\n", lp_path(i
));
209 io_printf(fd
,"@ERROR: chroot failed\n");
213 if (!push_dir("/", 0)) {
214 rprintf(FERROR
,"chdir %s failed\n", lp_path(i
));
215 io_printf(fd
,"@ERROR: chdir failed\n");
220 if (!push_dir(lp_path(i
), 0)) {
221 rprintf(FERROR
,"chdir %s failed\n", lp_path(i
));
222 io_printf(fd
,"@ERROR: chdir failed\n");
230 rprintf(FERROR
,"setgid %d failed\n", gid
);
231 io_printf(fd
,"@ERROR: setgid failed\n");
236 rprintf(FERROR
,"setuid %d failed\n", uid
);
237 io_printf(fd
,"@ERROR: setuid failed\n");
241 am_root
= (getuid() == 0);
244 io_printf(fd
,"@RSYNCD: OK\n");
246 argv
[argc
++] = "rsyncd";
249 if (!read_line(fd
, line
, sizeof(line
)-1)) {
257 argv
[argc
] = strdup(p
);
263 if (start_glob
== 1) {
267 glob_expand(name
, argv
, &argc
, MAX_ARGS
);
272 if (strcmp(line
,".") == 0) {
276 if (argc
== MAX_ARGS
) {
281 if (sanitize_paths
) {
283 * Note that this is applied to all parameters, whether or not
284 * they are filenames, but no other legal parameters contain
285 * the forms that need to be sanitized so it doesn't hurt;
286 * it is not known at this point which parameters are files
289 for (i
= 1; i
< argc
; i
++) {
290 sanitize_path(argv
[i
], NULL
);
294 ret
= parse_arguments(argc
, argv
, 0);
298 rprintf(FINFO
,"rsync %s %s from %s@%s (%s)\n",
300 request
, auth_user
, host
, addr
);
302 rprintf(FINFO
,"rsync %s %s from %s (%s)\n",
304 request
, host
, addr
);
310 /* don't allow the logs to be flooded too fast */
311 if (verbose
> 1) verbose
= 1;
315 argp
= argv
+ optind
;
318 if (remote_version
> 17 && am_sender
)
319 io_start_multiplex_out(fd
);
326 extern int io_timeout
;
327 io_timeout
= lp_timeout(i
);
330 start_server(fd
, fd
, argc
, argp
);
335 /* send a list of available modules to the client. Don't list those
336 with "list = False". */
337 static void send_listing(int fd
)
339 int n
= lp_numservices();
344 io_printf(fd
, "%-15s\t%s\n", lp_name(i
), lp_comment(i
));
347 /* this is called when a socket connection is established to a client
348 and we want to start talking. The setup of the system is done from
350 static int start_daemon(int fd
)
355 extern char *config_file
;
356 extern int remote_version
;
358 if (!lp_load(config_file
, 0)) {
359 exit_cleanup(RERR_SYNTAX
);
362 set_socket_options(fd
,"SO_KEEPALIVE");
363 set_socket_options(fd
,lp_socket_options());
366 io_printf(fd
,"@RSYNCD: %d\n", PROTOCOL_VERSION
);
368 motd
= lp_motd_file();
370 FILE *f
= fopen(motd
,"r");
371 while (f
&& !feof(f
)) {
372 int len
= fread(line
, 1, sizeof(line
)-1, f
);
375 io_printf(fd
,"%s", line
);
382 if (!read_line(fd
, line
, sizeof(line
)-1)) {
386 if (sscanf(line
,"@RSYNCD: %d", &remote_version
) != 1) {
387 io_printf(fd
,"@ERROR: protocol startup error\n");
393 if (!read_line(fd
, line
, sizeof(line
)-1)) {
397 if (!*line
|| strcmp(line
,"#list")==0) {
403 /* it's some sort of command that I don't understand */
404 io_printf(fd
,"@ERROR: Unknown command '%s'\n", line
);
410 io_printf(fd
,"@ERROR: Unknown module '%s'\n", line
);
415 return rsync_module(fd
, i
);
419 int daemon_main(void)
421 extern char *config_file
;
422 extern int orig_umask
;
425 if (is_a_socket(STDIN_FILENO
)) {
428 /* we are running via inetd - close off stdout and
429 stderr so that library functions (and getopt) don't
430 try to use them. Redirect them to /dev/null */
433 open("/dev/null", O_RDWR
);
436 set_nonblocking(STDIN_FILENO
);
438 return start_daemon(STDIN_FILENO
);
443 if (!lp_load(config_file
, 1)) {
444 fprintf(stderr
,"failed to load config file %s\n", config_file
);
445 exit_cleanup(RERR_SYNTAX
);
450 rprintf(FINFO
,"rsyncd version %s starting\n",VERSION
);
452 if (((pid_file
= lp_pid_file()) != NULL
) && (*pid_file
!= '\0')) {
455 int pid
= (int) getpid();
456 cleanup_set_pid(pid
);
457 if ((fd
= do_open(lp_pid_file(), O_WRONLY
|O_CREAT
|O_TRUNC
,
458 0666 & ~orig_umask
)) == -1) {
460 fprintf(stderr
,"failed to create pid file %s\n", pid_file
);
461 exit_cleanup(RERR_FILEIO
);
463 slprintf(pidbuf
, sizeof(pidbuf
), "%d\n", pid
);
464 write(fd
, pidbuf
, strlen(pidbuf
));
468 start_accept_loop(rsync_port
, start_daemon
);