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