Tweaked the O_BINARY code to the latest idiom.
[rsync.git] / clientserver.c
blob8a26ed5a0264ea94f5dce6b9821891cf82085a2b
1 /* -*- c-file-style: "linux"; -*-
2 *
3 * Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4 * Copyright (C) 2001-2002 by Martin Pool <mbp@samba.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /**
22 * @file
24 * The socket based protocol for setting up a connection with
25 * rsyncd.
26 **/
28 #include "rsync.h"
30 extern int module_id;
31 extern int read_only;
32 extern int verbose;
33 extern int rsync_port;
34 char *auth_user;
35 extern int sanitize_paths;
36 extern int filesfrom_fd;
38 /**
39 * Run a client connected to an rsyncd. The alternative to this
40 * function for remote-shell connections is do_cmd().
42 * After negotiating which module to use and reading the server's
43 * motd, this hands over to client_run(). Telling the server the
44 * module will cause it to chroot/setuid/etc.
46 * Instead of doing a transfer, the client may at this stage instead
47 * get a listing of remote modules and exit.
49 * @return -1 for error in startup, or the result of client_run().
50 * Either way, it eventually gets passed to exit_cleanup().
51 **/
52 int start_socket_client(char *host, char *path, int argc, char *argv[])
54 int fd, ret;
55 char *p, *user=NULL;
56 extern char *bind_address;
57 extern int default_af_hint;
59 /* this is redundant with code in start_inband_exchange(), but
60 this short-circuits a problem before we open a socket, and
61 the extra check won't hurt */
62 if (*path == '/') {
63 rprintf(FERROR,"ERROR: The remote path must start with a module name not a /\n");
64 return -1;
67 p = strchr(host, '@');
68 if (p) {
69 user = host;
70 host = p+1;
71 *p = 0;
74 if (verbose >= 2) {
75 /* FIXME: If we're going to use a socket program for
76 * testing, then this message is wrong. We need to
77 * say something like "(except really using %s)" */
78 rprintf(FINFO, "opening tcp connection to %s port %d\n",
79 host, rsync_port);
81 fd = open_socket_out_wrapped (host, rsync_port, bind_address,
82 default_af_hint);
83 if (fd == -1) {
84 exit_cleanup(RERR_SOCKETIO);
87 ret = start_inband_exchange(user, path, fd, fd, argc);
89 return ret < 0? ret : client_run(fd, fd, -1, argc, argv);
92 int start_inband_exchange(char *user, char *path, int f_in, int f_out, int argc)
94 int i;
95 char *sargs[MAX_ARGS];
96 int sargc = 0;
97 char line[MAXPATHLEN];
98 char *p;
99 extern int remote_version;
100 extern int kludge_around_eof;
101 extern int am_sender;
102 extern int daemon_over_rsh;
103 extern int list_only;
105 if (argc == 0 && !am_sender)
106 list_only = 1;
108 if (*path == '/') {
109 rprintf(FERROR, "ERROR: The remote path must start with a module name\n");
110 return -1;
113 if (!user) user = getenv("USER");
114 if (!user) user = getenv("LOGNAME");
116 /* set daemon_over_rsh to false since we need to build the
117 true set of args passed through the rsh/ssh connection;
118 this is a no-op for direct-socket-connection mode */
119 daemon_over_rsh = 0;
120 server_options(sargs, &sargc);
122 sargs[sargc++] = ".";
124 if (path && *path)
125 sargs[sargc++] = path;
127 sargs[sargc] = NULL;
129 io_printf(f_out, "@RSYNCD: %d\n", PROTOCOL_VERSION);
131 if (!read_line(f_in, line, sizeof(line)-1)) {
132 rprintf(FERROR, "rsync: did not see server greeting\n");
133 return -1;
136 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
137 /* note that read_line strips of \n or \r */
138 rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n",
139 line);
140 return -1;
143 p = strchr(path,'/');
144 if (p) *p = 0;
145 io_printf(f_out, "%s\n", path);
146 if (p) *p = '/';
148 /* Old servers may just drop the connection here,
149 rather than sending a proper EXIT command. Yuck. */
150 kludge_around_eof = list_only && (remote_version < 25);
152 while (1) {
153 if (!read_line(f_in, line, sizeof(line)-1)) {
154 rprintf(FERROR, "rsync: didn't get server startup line\n");
155 return -1;
158 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
159 auth_client(f_out, user, line+18);
160 continue;
163 if (strcmp(line,"@RSYNCD: OK") == 0) break;
165 if (strcmp(line,"@RSYNCD: EXIT") == 0) {
166 /* This is sent by recent versions of the
167 * server to terminate the listing of modules.
168 * We don't want to go on and transfer
169 * anything; just exit. */
170 exit(0);
173 if (strncmp(line, "@ERROR", 6) == 0) {
174 rprintf(FERROR,"%s\n", line);
175 /* This is always fatal; the server will now
176 * close the socket. */
177 return RERR_STARTCLIENT;
178 } else {
179 rprintf(FINFO,"%s\n", line);
182 kludge_around_eof = False;
184 for (i = 0; i < sargc; i++) {
185 io_printf(f_out, "%s\n", sargs[i]);
187 io_printf(f_out, "\n");
189 if (remote_version < 23) {
190 if (remote_version == 22 || (remote_version > 17 && !am_sender))
191 io_start_multiplex_in(f_in);
194 return 0;
199 static int rsync_module(int f_in, int f_out, int i)
201 int argc=0;
202 char *argv[MAX_ARGS];
203 char **argp;
204 char line[MAXPATHLEN];
205 uid_t uid = (uid_t)-2; /* canonically "nobody" */
206 gid_t gid = (gid_t)-2;
207 char *p;
208 char *addr = client_addr(f_in);
209 char *host = client_name(f_in);
210 char *name = lp_name(i);
211 int use_chroot = lp_use_chroot(i);
212 int start_glob=0;
213 int ret;
214 char *request=NULL;
215 extern int am_sender;
216 extern int am_server;
217 extern int am_daemon;
218 extern int remote_version;
219 extern int am_root;
221 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
222 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
223 name, host, addr);
224 io_printf(f_out, "@ERROR: access denied to %s from %s (%s)\n",
225 name, host, addr);
226 return -1;
229 if (am_daemon && am_server) {
230 rprintf(FINFO, "rsync allowed access on module %s from %s (%s)\n",
231 name, host, addr);
234 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
235 if (errno) {
236 rprintf(FERROR,"failed to open lock file %s : %s\n",
237 lp_lock_file(i), strerror(errno));
238 io_printf(f_out, "@ERROR: failed to open lock file %s : %s\n",
239 lp_lock_file(i), strerror(errno));
240 } else {
241 rprintf(FERROR,"max connections (%d) reached\n",
242 lp_max_connections(i));
243 io_printf(f_out, "@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
245 return -1;
249 auth_user = auth_server(f_in, f_out, i, addr, "@RSYNCD: AUTHREQD ");
251 if (!auth_user) {
252 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
253 name, host, addr);
254 io_printf(f_out, "@ERROR: auth failed on module %s\n", name);
255 return -1;
258 module_id = i;
260 am_root = (getuid() == 0);
262 if (am_root) {
263 p = lp_uid(i);
264 if (!name_to_uid(p, &uid)) {
265 if (!isdigit(* (unsigned char *) p)) {
266 rprintf(FERROR,"Invalid uid %s\n", p);
267 io_printf(f_out, "@ERROR: invalid uid %s\n", p);
268 return -1;
270 uid = atoi(p);
273 p = lp_gid(i);
274 if (!name_to_gid(p, &gid)) {
275 if (!isdigit(* (unsigned char *) p)) {
276 rprintf(FERROR,"Invalid gid %s\n", p);
277 io_printf(f_out, "@ERROR: invalid gid %s\n", p);
278 return -1;
280 gid = atoi(p);
284 /* TODO: If we're not root, but the configuration requests
285 * that we change to some uid other than the current one, then
286 * log a warning. */
288 /* TODO: Perhaps take a list of gids, and make them into the
289 * supplementary groups. */
291 p = lp_include_from(i);
292 add_exclude_file(p, 1, 1);
294 p = lp_include(i);
295 add_include_line(p);
297 p = lp_exclude_from(i);
298 add_exclude_file(p, 1, 0);
300 p = lp_exclude(i);
301 add_exclude_line(p);
303 log_init();
305 if (use_chroot) {
307 * XXX: The 'use chroot' flag is a fairly reliable
308 * source of confusion, because it fails under two
309 * important circumstances: running as non-root,
310 * running on Win32 (or possibly others). On the
311 * other hand, if you are running as root, then it
312 * might be better to always use chroot.
314 * So, perhaps if we can't chroot we should just issue
315 * a warning, unless a "require chroot" flag is set,
316 * in which case we fail.
318 if (chroot(lp_path(i))) {
319 rsyserr(FERROR, errno, "chroot %s failed", lp_path(i));
320 io_printf(f_out, "@ERROR: chroot failed\n");
321 return -1;
324 if (!push_dir("/", 0)) {
325 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
326 io_printf(f_out, "@ERROR: chdir failed\n");
327 return -1;
330 } else {
331 if (!push_dir(lp_path(i), 0)) {
332 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
333 io_printf(f_out, "@ERROR: chdir failed\n");
334 return -1;
336 sanitize_paths = 1;
339 if (am_root) {
340 #ifdef HAVE_SETGROUPS
341 /* Get rid of any supplementary groups this process
342 * might have inheristed. */
343 if (setgroups(0, NULL)) {
344 rsyserr(FERROR, errno, "setgroups failed");
345 io_printf(f_out, "@ERROR: setgroups failed\n");
346 return -1;
348 #endif
350 /* XXXX: You could argue that if the daemon is started
351 * by a non-root user and they explicitly specify a
352 * gid, then we should try to change to that gid --
353 * this could be possible if it's already in their
354 * supplementary groups. */
356 /* TODO: Perhaps we need to document that if rsyncd is
357 * started by somebody other than root it will inherit
358 * all their supplementary groups. */
360 if (setgid(gid)) {
361 rsyserr(FERROR, errno, "setgid %d failed", (int) gid);
362 io_printf(f_out, "@ERROR: setgid failed\n");
363 return -1;
366 if (setuid(uid)) {
367 rsyserr(FERROR, errno, "setuid %d failed", (int) uid);
368 io_printf(f_out, "@ERROR: setuid failed\n");
369 return -1;
372 am_root = (getuid() == 0);
375 io_printf(f_out, "@RSYNCD: OK\n");
377 argv[argc++] = "rsyncd";
379 while (1) {
380 if (!read_line(f_in, line, sizeof(line)-1)) {
381 return -1;
384 if (!*line) break;
386 p = line;
388 argv[argc] = strdup(p);
389 if (!argv[argc]) {
390 return -1;
393 if (start_glob) {
394 if (start_glob == 1) {
395 request = strdup(p);
396 start_glob++;
398 glob_expand(name, argv, &argc, MAX_ARGS);
399 } else {
400 argc++;
403 if (strcmp(line,".") == 0) {
404 start_glob = 1;
407 if (argc == MAX_ARGS) {
408 return -1;
412 if (sanitize_paths) {
414 * Note that this is applied to all parameters, whether or not
415 * they are filenames, but no other legal parameters contain
416 * the forms that need to be sanitized so it doesn't hurt;
417 * it is not known at this point which parameters are files
418 * and which aren't.
420 for (i = 1; i < argc; i++) {
421 sanitize_path(argv[i], NULL);
425 argp = argv;
426 ret = parse_arguments(&argc, (const char ***) &argp, 0);
428 if (filesfrom_fd == 0)
429 filesfrom_fd = f_in;
431 if (request) {
432 if (*auth_user) {
433 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
434 am_sender?"on":"to",
435 request, auth_user, host, addr);
436 } else {
437 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
438 am_sender?"on":"to",
439 request, host, addr);
441 free(request);
444 #ifndef DEBUG
445 /* don't allow the logs to be flooded too fast */
446 if (verbose > 1) verbose = 1;
447 #endif
449 if (remote_version < 23) {
450 if (remote_version == 22 || (remote_version > 17 && am_sender))
451 io_start_multiplex_out(f_out);
454 /* For later protocol versions, we don't start multiplexing
455 * until we've configured nonblocking in start_server. That
456 * means we're in a sticky situation now: there's no way to
457 * convey errors to the client. */
459 /* FIXME: Hold off on reporting option processing errors until
460 * we've set up nonblocking and multiplexed IO and can get the
461 * message back to them. */
462 if (!ret) {
463 option_error();
464 exit_cleanup(RERR_UNSUPPORTED);
467 if (lp_timeout(i)) {
468 extern int io_timeout;
469 io_timeout = lp_timeout(i);
472 start_server(f_in, f_out, argc, argp);
474 return 0;
477 /* send a list of available modules to the client. Don't list those
478 with "list = False". */
479 static void send_listing(int fd)
481 int n = lp_numservices();
482 int i;
483 extern int remote_version;
485 for (i=0;i<n;i++)
486 if (lp_list(i))
487 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
489 if (remote_version >= 25)
490 io_printf(fd,"@RSYNCD: EXIT\n");
493 /* this is called when a connection is established to a client
494 and we want to start talking. The setup of the system is done from
495 here */
496 int start_daemon(int f_in, int f_out)
498 char line[200];
499 char *motd;
500 int i = -1;
501 extern char *config_file;
502 extern int remote_version;
503 extern int am_server;
505 if (!lp_load(config_file, 0)) {
506 exit_cleanup(RERR_SYNTAX);
509 log_init();
511 if (!am_server) {
512 set_socket_options(f_in, "SO_KEEPALIVE");
513 set_socket_options(f_in, lp_socket_options());
514 set_nonblocking(f_in);
517 io_printf(f_out, "@RSYNCD: %d\n", PROTOCOL_VERSION);
519 motd = lp_motd_file();
520 if (motd && *motd) {
521 FILE *f = fopen(motd,"r");
522 while (f && !feof(f)) {
523 int len = fread(line, 1, sizeof(line)-1, f);
524 if (len > 0) {
525 line[len] = 0;
526 io_printf(f_out, "%s", line);
529 if (f) fclose(f);
530 io_printf(f_out, "\n");
533 if (!read_line(f_in, line, sizeof(line)-1)) {
534 return -1;
537 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
538 io_printf(f_out, "@ERROR: protocol startup error\n");
539 return -1;
542 while (i == -1) {
543 line[0] = 0;
544 if (!read_line(f_in, line, sizeof(line)-1)) {
545 return -1;
548 if (!*line || strcmp(line,"#list")==0) {
549 send_listing(f_out);
550 return -1;
553 if (*line == '#') {
554 /* it's some sort of command that I don't understand */
555 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
556 return -1;
559 i = lp_number(line);
560 if (i == -1) {
561 io_printf(f_out, "@ERROR: Unknown module '%s'\n", line);
562 return -1;
566 return rsync_module(f_in, f_out, i);
570 int daemon_main(void)
572 extern char *config_file;
573 extern int orig_umask;
574 char *pid_file;
575 extern int no_detach;
577 if (is_a_socket(STDIN_FILENO)) {
578 int i;
580 /* we are running via inetd - close off stdout and
581 stderr so that library functions (and getopt) don't
582 try to use them. Redirect them to /dev/null */
583 for (i=1;i<3;i++) {
584 close(i);
585 open("/dev/null", O_RDWR);
588 return start_daemon(STDIN_FILENO, STDIN_FILENO);
591 if (!no_detach)
592 become_daemon();
594 if (!lp_load(config_file, 1)) {
595 exit_cleanup(RERR_SYNTAX);
598 log_init();
600 rprintf(FINFO, "rsyncd version %s starting, listening on port %d\n",
601 RSYNC_VERSION,
602 rsync_port);
603 /* TODO: If listening on a particular address, then show that
604 * address too. In fact, why not just do inet_ntop on the
605 * local address??? */
607 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
608 char pidbuf[16];
609 int fd;
610 int pid = (int) getpid();
611 cleanup_set_pid(pid);
612 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
613 0666 & ~orig_umask)) == -1) {
614 cleanup_set_pid(0);
615 rsyserr(FLOG, errno, "failed to create pid file %s", pid_file);
616 exit_cleanup(RERR_FILEIO);
618 snprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
619 write(fd, pidbuf, strlen(pidbuf));
620 close(fd);
623 start_accept_loop(rsync_port, start_daemon);
624 return -1;