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