If a daemon prints an error message of @ERROR, have the client treat the
[rsync/rsync.git] / clientserver.c
1 /* -*- c-file-style: "linux"; -*-
2    
3    Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4    Copyright (C) 2001 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.
10    
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.
15    
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.
19 */
20
21 /* the socket based protocol for setting up a connection with rsyncd */
22
23 #include "rsync.h"
24
25 extern int module_id;
26 extern int read_only;
27 extern int verbose;
28 extern int rsync_port;
29 char *auth_user;
30 int sanitize_paths = 0;
31
32 /*
33  * Run a client connected to an rsyncd.  The alternative to this
34  * function for remote-shell connections is do_cmd.
35  */
36 int start_socket_client(char *host, char *path, int argc, char *argv[])
37 {
38         int fd, i;
39         char *sargs[MAX_ARGS];
40         int sargc=0;
41         char line[MAXPATHLEN];
42         char *p, *user=NULL;
43         extern int remote_version;
44         extern int am_sender;
45         extern char *shell_cmd;
46         extern int kludge_around_eof;
47         extern char *bind_address;
48         extern int default_af_hint;
49        
50         if (argc == 0 && !am_sender) {
51                 extern int list_only;
52                 list_only = 1;
53         }
54
55         /* This is just a friendliness enhancement: if the connection
56          * is to an rsyncd then there is no point specifying the -e option.
57          * Note that this is only set if the -e was explicitly specified,
58          * not if the environment variable just happens to be set.
59          * See http://lists.samba.org/pipermail/rsync/2000-September/002744.html
60          */
61         if (shell_cmd) {
62                 rprintf(FERROR, "WARNING: --rsh or -e option ignored when "
63                         "connecting to rsync daemon\n");
64                 /* continue */
65         }
66         
67         if (*path == '/') {
68                 rprintf(FERROR,"ERROR: The remote path must start with a module name not a /\n");
69                 return -1;
70         }
71
72         p = strchr(host, '@');
73         if (p) {
74                 user = host;
75                 host = p+1;
76                 *p = 0;
77         }
78
79         if (!user) user = getenv("USER");
80         if (!user) user = getenv("LOGNAME");
81
82         if (verbose >= 2) {
83         }
84         fd = open_socket_out_wrapped (host, rsync_port, bind_address,
85                                       default_af_hint);
86         if (fd == -1) {
87                 exit_cleanup(RERR_SOCKETIO);
88         }
89         
90         server_options(sargs,&sargc);
91
92         sargs[sargc++] = ".";
93
94         if (path && *path) 
95                 sargs[sargc++] = path;
96
97         sargs[sargc] = NULL;
98
99         io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
100
101         if (!read_line(fd, line, sizeof(line)-1)) {
102                 return -1;
103         }
104
105         if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
106                 return -1;
107         }
108
109         p = strchr(path,'/');
110         if (p) *p = 0;
111         io_printf(fd,"%s\n",path);
112         if (p) *p = '/';
113
114         /* Old servers may just drop the connection here,
115          rather than sending a proper EXIT command.  Yuck. */
116         kludge_around_eof = remote_version < 25;
117
118         while (1) {
119                 if (!read_line(fd, line, sizeof(line)-1)) {
120                         return -1;
121                 }
122
123                 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
124                         auth_client(fd, user, line+18);
125                         continue;
126                 }
127
128                 if (strcmp(line,"@RSYNCD: OK") == 0) break;
129
130                 if (strcmp(line,"@RSYNCD: EXIT") == 0) exit(0);
131
132                 if (strncmp(line, "@ERROR", 6) == 0)
133                         rprintf(FERROR,"%s\n", line);
134                 else
135                         rprintf(FINFO,"%s\n", line);
136         }
137         kludge_around_eof = False;
138
139         for (i=0;i<sargc;i++) {
140                 io_printf(fd,"%s\n", sargs[i]);
141         }
142         io_printf(fd,"\n");
143
144         if (remote_version < 23) {
145                 if (remote_version == 22 || (remote_version > 17 && !am_sender))
146                         io_start_multiplex_in(fd);
147         }
148
149         return client_run(fd, fd, -1, argc, argv);
150 }
151
152
153
154 static int rsync_module(int fd, int i)
155 {
156         int argc=0;
157         char *argv[MAX_ARGS];
158         char **argp;
159         char line[MAXPATHLEN];
160         uid_t uid = (uid_t)-2;  /* canonically "nobody" */
161         gid_t gid = (gid_t)-2;
162         char *p;
163         char *addr = client_addr(fd);
164         char *host = client_name(fd);
165         char *name = lp_name(i);
166         int use_chroot = lp_use_chroot(i);
167         int start_glob=0;
168         int ret;
169         char *request=NULL;
170         extern int am_sender;
171         extern int remote_version;
172         extern int am_root;
173
174         if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
175                 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
176                         name, host, addr);
177                 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
178                           name, host, addr);
179                 return -1;
180         }
181
182         if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
183                 if (errno) {
184                         rprintf(FERROR,"failed to open lock file %s : %s\n",
185                                 lp_lock_file(i), strerror(errno));
186                         io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
187                                   lp_lock_file(i), strerror(errno));
188                 } else {
189                         rprintf(FERROR,"max connections (%d) reached\n",
190                                 lp_max_connections(i));
191                         io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
192                 }
193                 return -1;
194         }
195
196         
197         auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
198
199         if (!auth_user) {
200                 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
201                         name, client_name(fd), client_addr(fd));
202                 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
203                 return -1;              
204         }
205
206         module_id = i;
207
208         am_root = (getuid() == 0);
209
210         if (am_root) {
211                 p = lp_uid(i);
212                 if (!name_to_uid(p, &uid)) {
213                         if (!isdigit(*p)) {
214                                 rprintf(FERROR,"Invalid uid %s\n", p);
215                                 io_printf(fd,"@ERROR: invalid uid %s\n", p);
216                                 return -1;
217                         } 
218                         uid = atoi(p);
219                 }
220
221                 p = lp_gid(i);
222                 if (!name_to_gid(p, &gid)) {
223                         if (!isdigit(*p)) {
224                                 rprintf(FERROR,"Invalid gid %s\n", p);
225                                 io_printf(fd,"@ERROR: invalid gid %s\n", p);
226                                 return -1;
227                         } 
228                         gid = atoi(p);
229                 }
230         }
231         
232         /* TODO: If we're not root, but the configuration requests
233          * that we change to some uid other than the current one, then
234          * log a warning. */
235
236         /* TODO: Perhaps take a list of gids, and make them into the
237          * supplementary groups. */
238
239         p = lp_include_from(i);
240         add_exclude_file(p, 1, 1);
241
242         p = lp_include(i);
243         add_include_line(p);
244
245         p = lp_exclude_from(i);
246         add_exclude_file(p, 1, 0);
247
248         p = lp_exclude(i);
249         add_exclude_line(p);
250
251         log_init();
252
253         if (use_chroot) {
254                 /*
255                  * XXX: The 'use chroot' flag is a fairly reliable
256                  * source of confusion, because it fails under two
257                  * important circumstances: running as non-root,
258                  * running on Win32 (or possibly others).  On the
259                  * other hand, if you are running as root, then it
260                  * might be better to always use chroot.
261                  *
262                  * So, perhaps if we can't chroot we should just issue
263                  * a warning, unless a "require chroot" flag is set,
264                  * in which case we fail.
265                  */
266                 if (chroot(lp_path(i))) {
267                         rsyserr(FERROR, errno, "chroot %s failed", lp_path(i));
268                         io_printf(fd,"@ERROR: chroot failed\n");
269                         return -1;
270                 }
271
272                 if (!push_dir("/", 0)) {
273                         rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
274                         io_printf(fd,"@ERROR: chdir failed\n");
275                         return -1;
276                 }
277
278         } else {
279                 if (!push_dir(lp_path(i), 0)) {
280                         rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
281                         io_printf(fd,"@ERROR: chdir failed\n");
282                         return -1;
283                 }
284                 sanitize_paths = 1;
285         }
286
287         if (am_root) {
288 #ifdef HAVE_SETGROUPS
289                 /* Get rid of any supplementary groups this process
290                  * might have inheristed. */
291                 if (setgroups(0, NULL)) {
292                         rsyserr(FERROR, errno, "setgroups failed");
293                         io_printf(fd, "@ERROR: setgroups failed\n");
294                         return -1;
295                 }
296 #endif
297
298                 /* XXXX: You could argue that if the daemon is started
299                  * by a non-root user and they explicitly specify a
300                  * gid, then we should try to change to that gid --
301                  * this could be possible if it's already in their
302                  * supplementary groups. */
303
304                 /* TODO: Perhaps we need to document that if rsyncd is
305                  * started by somebody other than root it will inherit
306                  * all their supplementary groups. */
307
308                 if (setgid(gid)) {
309                         rsyserr(FERROR, errno, "setgid %d failed", (int) gid);
310                         io_printf(fd,"@ERROR: setgid failed\n");
311                         return -1;
312                 }
313
314                 if (setuid(uid)) {
315                         rsyserr(FERROR, errno, "setuid %d failed", (int) uid);
316                         io_printf(fd,"@ERROR: setuid failed\n");
317                         return -1;
318                 }
319
320                 am_root = (getuid() == 0);
321         }
322
323         io_printf(fd,"@RSYNCD: OK\n");
324
325         argv[argc++] = "rsyncd";
326
327         while (1) {
328                 if (!read_line(fd, line, sizeof(line)-1)) {
329                         return -1;
330                 }
331
332                 if (!*line) break;
333
334                 p = line;
335
336                 argv[argc] = strdup(p);
337                 if (!argv[argc]) {
338                         return -1;
339                 }
340
341                 if (start_glob) {
342                         if (start_glob == 1) {
343                                 request = strdup(p);
344                                 start_glob++;
345                         }
346                         glob_expand(name, argv, &argc, MAX_ARGS);
347                 } else {
348                         argc++;
349                 }
350
351                 if (strcmp(line,".") == 0) {
352                         start_glob = 1;
353                 }
354
355                 if (argc == MAX_ARGS) {
356                         return -1;
357                 }
358         }
359
360         if (sanitize_paths) {
361                 /*
362                  * Note that this is applied to all parameters, whether or not
363                  *    they are filenames, but no other legal parameters contain
364                  *    the forms that need to be sanitized so it doesn't hurt;
365                  *    it is not known at this point which parameters are files
366                  *    and which aren't.
367                  */
368                 for (i = 1; i < argc; i++) {
369                         sanitize_path(argv[i], NULL);
370                 }
371         }
372
373         argp = argv;
374         ret = parse_arguments(&argc, (const char ***) &argp, 0);
375
376         if (request) {
377                 if (*auth_user) {
378                         rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
379                                 am_sender?"on":"to",
380                                 request, auth_user, host, addr);
381                 } else {
382                         rprintf(FINFO,"rsync %s %s from %s (%s)\n",
383                                 am_sender?"on":"to",
384                                 request, host, addr);
385                 }
386                 free(request);
387         }
388
389 #ifndef DEBUG
390         /* don't allow the logs to be flooded too fast */
391         if (verbose > 1) verbose = 1;
392 #endif
393
394         if (remote_version < 23) {
395                 if (remote_version == 22 || (remote_version > 17 && am_sender))
396                         io_start_multiplex_out(fd);
397         }
398         
399         /* For later protocol versions, we don't start multiplexing
400          * until we've configured nonblocking in start_server.  That
401          * means we're in a sticky situation now: there's no way to
402          * convey errors to the client. */
403
404         /* FIXME: Hold off on reporting option processing errors until
405          * we've set up nonblocking and multiplexed IO and can get the
406          * message back to them. */
407         if (!ret) {
408                 option_error();
409                 exit_cleanup(RERR_UNSUPPORTED);
410         }
411
412         if (lp_timeout(i)) {
413                 extern int io_timeout;
414                 io_timeout = lp_timeout(i);
415         }
416
417         start_server(fd, fd, argc, argp);
418
419         return 0;
420 }
421
422 /* send a list of available modules to the client. Don't list those
423    with "list = False". */
424 static void send_listing(int fd)
425 {
426         int n = lp_numservices();
427         int i;
428         extern int remote_version;
429
430         for (i=0;i<n;i++)
431                 if (lp_list(i))
432                     io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
433
434         if (remote_version >= 25)
435                 io_printf(fd,"@RSYNCD: EXIT\n");
436 }
437
438 /* this is called when a socket connection is established to a client
439    and we want to start talking. The setup of the system is done from
440    here */
441 static int start_daemon(int fd)
442 {
443         char line[200];
444         char *motd;
445         int i = -1;
446         extern char *config_file;
447         extern int remote_version;
448
449         if (!lp_load(config_file, 0)) {
450                 exit_cleanup(RERR_SYNTAX);
451         }
452
453         set_socket_options(fd,"SO_KEEPALIVE");
454         set_socket_options(fd,lp_socket_options());
455         set_nonblocking(fd);
456
457         io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
458
459         motd = lp_motd_file();
460         if (motd && *motd) {
461                 FILE *f = fopen(motd,"r");
462                 while (f && !feof(f)) {
463                         int len = fread(line, 1, sizeof(line)-1, f);
464                         if (len > 0) {
465                                 line[len] = 0;
466                                 io_printf(fd,"%s", line);
467                         }
468                 }
469                 if (f) fclose(f);
470                 io_printf(fd,"\n");
471         }
472
473         if (!read_line(fd, line, sizeof(line)-1)) {
474                 return -1;
475         }
476
477         if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
478                 io_printf(fd,"@ERROR: protocol startup error\n");
479                 return -1;
480         }       
481
482         while (i == -1) {
483                 line[0] = 0;
484                 if (!read_line(fd, line, sizeof(line)-1)) {
485                         return -1;
486                 }
487
488                 if (!*line || strcmp(line,"#list")==0) {
489                         send_listing(fd);
490                         return -1;
491                 } 
492
493                 if (*line == '#') {
494                         /* it's some sort of command that I don't understand */
495                         io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
496                         return -1;
497                 }
498
499                 i = lp_number(line);
500                 if (i == -1) {
501                         io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
502                         return -1;
503                 }
504         }
505
506         return rsync_module(fd, i);
507 }
508
509
510 int daemon_main(void)
511 {
512         extern char *config_file;
513         extern int orig_umask;
514         char *pid_file;
515         extern int no_detach;
516
517         if (is_a_socket(STDIN_FILENO)) {
518                 int i;
519
520                 /* we are running via inetd - close off stdout and
521                    stderr so that library functions (and getopt) don't
522                    try to use them. Redirect them to /dev/null */
523                 for (i=1;i<3;i++) {
524                         close(i); 
525                         open("/dev/null", O_RDWR);
526                 }
527
528                 return start_daemon(STDIN_FILENO);
529         }
530
531         if (!no_detach)
532             become_daemon();
533
534         if (!lp_load(config_file, 1)) {
535                 exit_cleanup(RERR_SYNTAX);
536         }
537
538         log_init();
539
540         rprintf(FINFO, "rsyncd version %s starting, listening on port %d\n",
541                 RSYNC_VERSION,
542                 rsync_port);
543         /* TODO: If listening on a particular address, then show that
544          * address too.  In fact, why not just do inet_ntop on the
545          * local address??? */
546
547         if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
548                 char pidbuf[16];
549                 int fd;
550                 int pid = (int) getpid();
551                 cleanup_set_pid(pid);
552                 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
553                                         0666 & ~orig_umask)) == -1) {
554                     cleanup_set_pid(0);
555                     rsyserr(FLOG, errno, "failed to create pid file %s", pid_file);
556                     exit_cleanup(RERR_FILEIO);
557                 }
558                 snprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
559                 write(fd, pidbuf, strlen(pidbuf));
560                 close(fd);
561         }
562
563         start_accept_loop(rsync_port, start_daemon);
564         return -1;
565 }
566