If an error occurs, print an explanatory string rather
[rsync/rsync.git] / clientserver.c
1 /* -*- c-file-style: "linux"; -*-
2    
3    Copyright (C) 1998-2000 by Andrew Tridgell
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /* the socket based protocol for setting up a connection with rsyncd */
21
22 #include "rsync.h"
23
24 extern int module_id;
25 extern int read_only;
26 extern int verbose;
27 extern int rsync_port;
28 char *auth_user;
29 int sanitize_paths = 0;
30
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 struct in_addr socket_address;
46         extern char *shell_cmd;
47
48         if (argc == 0 && !am_sender) {
49                 extern int list_only;
50                 list_only = 1;
51         }
52
53         /* This is just a friendliness enhancement: if the connection
54          * is to an rsyncd then there is no point specifying the -e option.
55          * Note that this is only set if the -e was explicitly specified,
56          * not if the environment variable just happens to be set.
57          * See http://lists.samba.org/pipermail/rsync/2000-September/002744.html
58          */
59         if (shell_cmd) {
60                 rprintf(FERROR, "WARNING: --rsh or -e option ignored when "
61                         "connecting to rsync daemon\n");
62                 /* continue */
63         }
64         
65         if (*path == '/') {
66                 rprintf(FERROR,"ERROR: The remote path must start with a module name\n");
67                 return -1;
68         }
69
70         p = strchr(host, '@');
71         if (p) {
72                 user = host;
73                 host = p+1;
74                 *p = 0;
75         }
76
77         if (!user) user = getenv("USER");
78         if (!user) user = getenv("LOGNAME");
79
80         fd = open_socket_out(host, rsync_port, &socket_address);
81         if (fd == -1) {
82                 exit_cleanup(RERR_SOCKETIO);
83         }
84         
85         server_options(sargs,&sargc);
86
87         sargs[sargc++] = ".";
88
89         if (path && *path) 
90                 sargs[sargc++] = path;
91
92         sargs[sargc] = NULL;
93
94         io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
95
96         if (!read_line(fd, line, sizeof(line)-1)) {
97                 return -1;
98         }
99
100         if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
101                 return -1;
102         }
103
104         p = strchr(path,'/');
105         if (p) *p = 0;
106         io_printf(fd,"%s\n",path);
107         if (p) *p = '/';
108
109         while (1) {
110                 if (!read_line(fd, line, sizeof(line)-1)) {
111                         return -1;
112                 }
113
114                 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
115                         auth_client(fd, user, line+18);
116                         continue;
117                 }
118
119                 if (strcmp(line,"@RSYNCD: OK") == 0) break;
120                 rprintf(FINFO,"%s\n", line);
121         }
122
123         for (i=0;i<sargc;i++) {
124                 io_printf(fd,"%s\n", sargs[i]);
125         }
126         io_printf(fd,"\n");
127
128         if (remote_version < 23) {
129                 if (remote_version == 22 || (remote_version > 17 && !am_sender))
130                         io_start_multiplex_in(fd);
131         }
132
133         return client_run(fd, fd, -1, argc, argv);
134 }
135
136
137
138 static int rsync_module(int fd, int i)
139 {
140         int argc=0;
141         char *argv[MAX_ARGS];
142         char **argp;
143         char line[MAXPATHLEN];
144         uid_t uid = (uid_t)-2;
145         gid_t gid = (gid_t)-2;
146         char *p;
147         char *addr = client_addr(fd);
148         char *host = client_name(fd);
149         char *name = lp_name(i);
150         int use_chroot = lp_use_chroot(i);
151         int start_glob=0;
152         int ret;
153         char *request=NULL;
154         extern int am_sender;
155         extern int remote_version;
156         extern int am_root;
157
158         if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
159                 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
160                         name, client_name(fd), client_addr(fd));
161                 io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n",
162                           name, client_name(fd), client_addr(fd));
163                 return -1;
164         }
165
166         if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
167                 if (errno) {
168                         rprintf(FERROR,"failed to open lock file %s : %s\n",
169                                 lp_lock_file(i), strerror(errno));
170                         io_printf(fd,"@ERROR: failed to open lock file %s : %s\n",
171                                   lp_lock_file(i), strerror(errno));
172                 } else {
173                         rprintf(FERROR,"max connections (%d) reached\n",
174                                 lp_max_connections(i));
175                         io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i));
176                 }
177                 return -1;
178         }
179
180         
181         auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD ");
182
183         if (!auth_user) {
184                 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
185                         name, client_name(fd), client_addr(fd));
186                 io_printf(fd,"@ERROR: auth failed on module %s\n",name);
187                 return -1;              
188         }
189
190         module_id = i;
191
192         am_root = (getuid() == 0);
193
194         if (am_root) {
195                 p = lp_uid(i);
196                 if (!name_to_uid(p, &uid)) {
197                         if (!isdigit(*p)) {
198                                 rprintf(FERROR,"Invalid uid %s\n", p);
199                                 io_printf(fd,"@ERROR: invalid uid\n");
200                                 return -1;
201                         } 
202                         uid = atoi(p);
203                 }
204
205                 p = lp_gid(i);
206                 if (!name_to_gid(p, &gid)) {
207                         if (!isdigit(*p)) {
208                                 rprintf(FERROR,"Invalid gid %s\n", p);
209                                 io_printf(fd,"@ERROR: invalid gid\n");
210                                 return -1;
211                         } 
212                         gid = atoi(p);
213                 }
214         }
215
216         p = lp_include_from(i);
217         add_exclude_file(p, 1, 1);
218
219         p = lp_include(i);
220         add_include_line(p);
221
222         p = lp_exclude_from(i);
223         add_exclude_file(p, 1, 0);
224
225         p = lp_exclude(i);
226         add_exclude_line(p);
227
228         log_init();
229
230         if (use_chroot) {
231                 if (chroot(lp_path(i))) {
232                         rsyserr(FERROR, errno, "chroot %s failed", lp_path(i));
233                         io_printf(fd,"@ERROR: chroot failed\n");
234                         return -1;
235                 }
236
237                 if (!push_dir("/", 0)) {
238                         rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
239                         io_printf(fd,"@ERROR: chdir failed\n");
240                         return -1;
241                 }
242
243         } else {
244                 if (!push_dir(lp_path(i), 0)) {
245                         rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
246                         io_printf(fd,"@ERROR: chdir failed\n");
247                         return -1;
248                 }
249                 sanitize_paths = 1;
250         }
251
252         if (am_root) {
253                 if (setgid(gid)) {
254                         rsyserr(FERROR, errno, "setgid %d failed", gid);
255                         io_printf(fd,"@ERROR: setgid failed\n");
256                         return -1;
257                 }
258
259                 if (setuid(uid)) {
260                         rsyserr(FERROR, errno, "setuid %d failed", uid);
261                         io_printf(fd,"@ERROR: setuid failed\n");
262                         return -1;
263                 }
264
265                 am_root = (getuid() == 0);
266         }
267
268         io_printf(fd,"@RSYNCD: OK\n");
269
270         argv[argc++] = "rsyncd";
271
272         while (1) {
273                 if (!read_line(fd, line, sizeof(line)-1)) {
274                         return -1;
275                 }
276
277                 if (!*line) break;
278
279                 p = line;
280
281                 argv[argc] = strdup(p);
282                 if (!argv[argc]) {
283                         return -1;
284                 }
285
286                 if (start_glob) {
287                         if (start_glob == 1) {
288                                 request = strdup(p);
289                                 start_glob++;
290                         }
291                         glob_expand(name, argv, &argc, MAX_ARGS);
292                 } else {
293                         argc++;
294                 }
295
296                 if (strcmp(line,".") == 0) {
297                         start_glob = 1;
298                 }
299
300                 if (argc == MAX_ARGS) {
301                         return -1;
302                 }
303         }
304
305         if (sanitize_paths) {
306                 /*
307                  * Note that this is applied to all parameters, whether or not
308                  *    they are filenames, but no other legal parameters contain
309                  *    the forms that need to be sanitized so it doesn't hurt;
310                  *    it is not known at this point which parameters are files
311                  *    and which aren't.
312                  */
313                 for (i = 1; i < argc; i++) {
314                         sanitize_path(argv[i], NULL);
315                 }
316         }
317
318         ret = parse_arguments(argc, argv, 0);
319
320         if (request) {
321                 if (*auth_user) {
322                         rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
323                                 am_sender?"on":"to",
324                                 request, auth_user, host, addr);
325                 } else {
326                         rprintf(FINFO,"rsync %s %s from %s (%s)\n",
327                                 am_sender?"on":"to",
328                                 request, host, addr);
329                 }
330                 free(request);
331         }
332
333 #if !TRIDGE
334         /* don't allow the logs to be flooded too fast */
335         if (verbose > 1) verbose = 1;
336 #endif
337
338         argc -= optind;
339         argp = argv + optind;
340         optind = 0;
341
342         if (remote_version < 23) {
343                 if (remote_version == 22 || (remote_version > 17 && am_sender))
344                         io_start_multiplex_out(fd);
345         }
346
347         if (!ret) {
348                 option_error();
349         }
350
351         if (lp_timeout(i)) {
352                 extern int io_timeout;
353                 io_timeout = lp_timeout(i);
354         }
355
356         start_server(fd, fd, argc, argp);
357
358         return 0;
359 }
360
361 /* send a list of available modules to the client. Don't list those
362    with "list = False". */
363 static void send_listing(int fd)
364 {
365         int n = lp_numservices();
366         int i;
367         
368         for (i=0;i<n;i++)
369                 if (lp_list(i))
370                     io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
371 }
372
373 /* this is called when a socket connection is established to a client
374    and we want to start talking. The setup of the system is done from
375    here */
376 static int start_daemon(int fd)
377 {
378         char line[200];
379         char *motd;
380         int i = -1;
381         extern char *config_file;
382         extern int remote_version;
383
384         if (!lp_load(config_file, 0)) {
385                 exit_cleanup(RERR_SYNTAX);
386         }
387
388         set_socket_options(fd,"SO_KEEPALIVE");
389         set_socket_options(fd,lp_socket_options());
390         set_nonblocking(fd);
391
392         io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION);
393
394         motd = lp_motd_file();
395         if (motd && *motd) {
396                 FILE *f = fopen(motd,"r");
397                 while (f && !feof(f)) {
398                         int len = fread(line, 1, sizeof(line)-1, f);
399                         if (len > 0) {
400                                 line[len] = 0;
401                                 io_printf(fd,"%s", line);
402                         }
403                 }
404                 if (f) fclose(f);
405                 io_printf(fd,"\n");
406         }
407
408         if (!read_line(fd, line, sizeof(line)-1)) {
409                 return -1;
410         }
411
412         if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
413                 io_printf(fd,"@ERROR: protocol startup error\n");
414                 return -1;
415         }       
416
417         while (i == -1) {
418                 line[0] = 0;
419                 if (!read_line(fd, line, sizeof(line)-1)) {
420                         return -1;
421                 }
422
423                 if (!*line || strcmp(line,"#list")==0) {
424                         send_listing(fd);
425                         return -1;
426                 } 
427
428                 if (*line == '#') {
429                         /* it's some sort of command that I don't understand */
430                         io_printf(fd,"@ERROR: Unknown command '%s'\n", line);
431                         return -1;
432                 }
433
434                 i = lp_number(line);
435                 if (i == -1) {
436                         io_printf(fd,"@ERROR: Unknown module '%s'\n", line);
437                         return -1;
438                 }
439         }
440
441         return rsync_module(fd, i);
442 }
443
444
445 int daemon_main(void)
446 {
447         extern char *config_file;
448         extern int orig_umask;
449         char *pid_file;
450
451         if (is_a_socket(STDIN_FILENO)) {
452                 int i;
453
454                 /* we are running via inetd - close off stdout and
455                    stderr so that library functions (and getopt) don't
456                    try to use them. Redirect them to /dev/null */
457                 for (i=1;i<3;i++) {
458                         close(i); 
459                         open("/dev/null", O_RDWR);
460                 }
461
462                 return start_daemon(STDIN_FILENO);
463         }
464
465         become_daemon();
466
467         if (!lp_load(config_file, 1)) {
468                 exit_cleanup(RERR_SYNTAX);
469         }
470
471         log_init();
472
473         rprintf(FINFO,"rsyncd version %s starting\n",VERSION);
474
475         if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
476                 char pidbuf[16];
477                 int fd;
478                 int pid = (int) getpid();
479                 cleanup_set_pid(pid);
480                 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
481                                         0666 & ~orig_umask)) == -1) {
482                     cleanup_set_pid(0);
483                     rsyserr(FLOG, errno, "failed to create pid file %s", pid_file);
484                     exit_cleanup(RERR_FILEIO);
485                 }
486                 slprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
487                 write(fd, pidbuf, strlen(pidbuf));
488                 close(fd);
489         }
490
491         start_accept_loop(rsync_port, start_daemon);
492         return -1;
493 }
494