configuration parsing and loading code for rsyncd. This is based
[rsync/rsync.git] / main.c
CommitLineData
c627d613
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
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#include "rsync.h"
21
22int verbose = 0;
23int always_checksum = 0;
f0fca04e 24time_t starttime = 0;
71c46176 25int64 total_size = 0;
c627d613
AT
26int block_size=BLOCK_SIZE;
27
28char *backup_suffix = BACKUP_SUFFIX;
950ab32d 29char *tmpdir = NULL;
c627d613
AT
30
31static char *rsync_path = RSYNC_NAME;
32
33int make_backups = 0;
82306bf6
AT
34int whole_file = 0;
35int copy_links = 0;
c627d613 36int preserve_links = 0;
dc5ddbcc 37int preserve_hard_links = 0;
c627d613
AT
38int preserve_perms = 0;
39int preserve_devices = 0;
40int preserve_uid = 0;
41int preserve_gid = 0;
42int preserve_times = 0;
43int update_only = 0;
44int cvs_exclude = 0;
45int dry_run=0;
46int local_server=0;
47int ignore_times=0;
48int delete_mode=0;
49int one_file_system=0;
4fe159a8 50int remote_version=0;
dc5ddbcc 51int sparse_files=0;
70d794dc 52int do_compression=0;
7b8356d0
AT
53int am_root=0;
54int orig_umask=0;
6574b4f7 55int relative_paths=0;
f6c34742 56int numeric_ids = 0;
3cb6f5d6 57int force_delete = 0;
6ba9279f
AT
58int io_timeout = 0;
59int io_error = 0;
f0fca04e
AT
60int read_only = 0;
61static int module_id;
62
9486289c 63static int port = RSYNC_PORT;
70d794dc 64
5d6bcd44
AT
65static char *shell_cmd;
66
34ccb63e 67extern int csum_length;
c627d613
AT
68
69int am_server = 0;
f0fca04e 70int am_sender=0;
c627d613 71int recurse = 0;
f0fca04e 72int am_daemon=0;
c627d613 73
9486289c 74static void usage(int fd);
c627d613
AT
75
76static void report(int f)
77{
71c46176 78 int64 in,out,tsize;
c627d613
AT
79 time_t t = time(NULL);
80
81 if (!verbose) return;
82
366345fe 83 if (am_server && am_sender) {
3a6a366f
AT
84 write_longint(f,read_total());
85 write_longint(f,write_total());
86 write_longint(f,total_size);
c627d613
AT
87 write_flush(f);
88 return;
89 }
90
366345fe 91 if (am_sender) {
c627d613
AT
92 in = read_total();
93 out = write_total();
3a6a366f 94 tsize = total_size;
c627d613 95 } else {
3a6a366f
AT
96 in = read_longint(f);
97 out = read_longint(f);
98 tsize = read_longint(f);
c627d613
AT
99 }
100
efb2f6bf
AT
101 printf("wrote %.0f bytes read %.0f bytes %.2f bytes/sec\n",
102 (double)out,(double)in,(in+out)/(0.5 + (t-starttime)));
103 printf("total size is %.0f speedup is %.2f\n",
104 (double)tsize,(1.0*tsize)/(in+out));
c627d613
AT
105}
106
107
108static void server_options(char **args,int *argc)
109{
110 int ac = *argc;
111 static char argstr[50];
112 static char bsize[30];
6ba9279f 113 static char iotime[30];
c627d613
AT
114 int i, x;
115
116 args[ac++] = "--server";
117
366345fe 118 if (!am_sender)
c627d613
AT
119 args[ac++] = "--sender";
120
121 x = 1;
122 argstr[0] = '-';
123 for (i=0;i<verbose;i++)
124 argstr[x++] = 'v';
125 if (make_backups)
126 argstr[x++] = 'b';
127 if (update_only)
128 argstr[x++] = 'u';
129 if (dry_run)
130 argstr[x++] = 'n';
131 if (preserve_links)
132 argstr[x++] = 'l';
82306bf6
AT
133 if (copy_links)
134 argstr[x++] = 'L';
135 if (whole_file)
136 argstr[x++] = 'W';
dc5ddbcc
AT
137 if (preserve_hard_links)
138 argstr[x++] = 'H';
c627d613
AT
139 if (preserve_uid)
140 argstr[x++] = 'o';
141 if (preserve_gid)
142 argstr[x++] = 'g';
143 if (preserve_devices)
144 argstr[x++] = 'D';
145 if (preserve_times)
146 argstr[x++] = 't';
147 if (preserve_perms)
148 argstr[x++] = 'p';
149 if (recurse)
150 argstr[x++] = 'r';
151 if (always_checksum)
152 argstr[x++] = 'c';
153 if (cvs_exclude)
154 argstr[x++] = 'C';
155 if (ignore_times)
156 argstr[x++] = 'I';
6574b4f7
AT
157 if (relative_paths)
158 argstr[x++] = 'R';
c627d613
AT
159 if (one_file_system)
160 argstr[x++] = 'x';
dc5ddbcc
AT
161 if (sparse_files)
162 argstr[x++] = 'S';
861c20b4
PM
163 if (do_compression)
164 argstr[x++] = 'z';
c627d613
AT
165 argstr[x] = 0;
166
167 if (x != 1) args[ac++] = argstr;
168
169 if (block_size != BLOCK_SIZE) {
170 sprintf(bsize,"-B%d",block_size);
171 args[ac++] = bsize;
172 }
43a481dc 173
6ba9279f
AT
174 if (io_timeout) {
175 sprintf(iotime,"--timeout=%d",io_timeout);
176 args[ac++] = iotime;
177 }
178
dc7a9478
AT
179 if (strcmp(backup_suffix, BACKUP_SUFFIX)) {
180 args[ac++] = "--suffix";
181 args[ac++] = backup_suffix;
182 }
183
c627d613
AT
184 if (delete_mode)
185 args[ac++] = "--delete";
186
3cb6f5d6
AT
187 if (force_delete)
188 args[ac++] = "--force";
189
f6c34742
AT
190 if (numeric_ids)
191 args[ac++] = "--numeric-ids";
192
950ab32d
AT
193 if (tmpdir) {
194 args[ac++] = "--temp-dir";
195 args[ac++] = tmpdir;
196 }
197
c627d613
AT
198 *argc = ac;
199}
200
201
202
e3cd198f 203static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
c627d613 204{
366345fe
AT
205 char *args[100];
206 int i,argc=0, ret;
207 char *tok,*dir=NULL;
208
209 if (!local_server) {
210 if (!cmd)
211 cmd = getenv(RSYNC_RSH_ENV);
212 if (!cmd)
213 cmd = RSYNC_RSH;
214 cmd = strdup(cmd);
215 if (!cmd)
216 goto oom;
217
218 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
219 args[argc++] = tok;
220 }
c627d613 221
7b8356d0 222#if HAVE_REMSH
366345fe
AT
223 /* remsh (on HPUX) takes the arguments the other way around */
224 args[argc++] = machine;
225 if (user) {
226 args[argc++] = "-l";
227 args[argc++] = user;
228 }
7b8356d0 229#else
366345fe
AT
230 if (user) {
231 args[argc++] = "-l";
232 args[argc++] = user;
233 }
234 args[argc++] = machine;
7b8356d0 235#endif
c627d613 236
366345fe 237 args[argc++] = rsync_path;
c627d613 238
366345fe
AT
239 server_options(args,&argc);
240 }
c627d613 241
366345fe 242 args[argc++] = ".";
76076c4b 243
366345fe
AT
244 if (path && *path)
245 args[argc++] = path;
c627d613 246
366345fe 247 args[argc] = NULL;
c627d613 248
366345fe 249 if (verbose > 3) {
9486289c 250 rprintf(FINFO,"cmd=");
366345fe 251 for (i=0;i<argc;i++)
9486289c
AT
252 rprintf(FINFO,"%s ",args[i]);
253 rprintf(FINFO,"\n");
366345fe
AT
254 }
255
256 if (local_server) {
257 ret = local_child(argc, args, f_in, f_out);
258 } else {
259 ret = piped_child(args,f_in,f_out);
260 }
c627d613 261
366345fe 262 if (dir) free(dir);
82306bf6 263
366345fe 264 return ret;
c627d613
AT
265
266oom:
366345fe
AT
267 out_of_memory("do_cmd");
268 return 0; /* not reached */
c627d613
AT
269}
270
271
272
273
274static char *get_local_name(struct file_list *flist,char *name)
275{
bcacc18b 276 STRUCT_STAT st;
c627d613 277
bcacc18b 278 if (do_stat(name,&st) == 0) {
c627d613
AT
279 if (S_ISDIR(st.st_mode)) {
280 if (chdir(name) != 0) {
9486289c 281 rprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
34ccb63e 282 exit_cleanup(1);
c627d613
AT
283 }
284 return NULL;
285 }
286 if (flist->count > 1) {
9486289c 287 rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
34ccb63e 288 exit_cleanup(1);
c627d613
AT
289 }
290 return name;
291 }
292
293 if (flist->count == 1)
294 return name;
295
296 if (!name)
297 return NULL;
298
1b2d733a 299 if (do_mkdir(name,0777 & ~orig_umask) != 0) {
9486289c 300 rprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
34ccb63e 301 exit_cleanup(1);
94481d91 302 } else {
9486289c 303 rprintf(FINFO,"created directory %s\n",name);
c627d613
AT
304 }
305
306 if (chdir(name) != 0) {
9486289c 307 rprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
34ccb63e 308 exit_cleanup(1);
c627d613
AT
309 }
310
311 return NULL;
312}
313
314
315
316
9486289c 317static void do_server_sender(int f_in, int f_out, int argc,char *argv[])
c627d613
AT
318{
319 int i;
c627d613 320 struct file_list *flist;
76076c4b 321 char *dir = argv[0];
c627d613
AT
322
323 if (verbose > 2)
9486289c 324 rprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid());
c627d613 325
6574b4f7 326 if (!relative_paths && chdir(dir) != 0) {
9486289c 327 rprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
76076c4b 328 exit_cleanup(1);
c627d613
AT
329 }
330 argc--;
331 argv++;
332
333 if (strcmp(dir,".")) {
76076c4b
AT
334 int l = strlen(dir);
335 if (strcmp(dir,"/") == 0)
336 l = 0;
337 for (i=0;i<argc;i++)
338 argv[i] += l+1;
c627d613
AT
339 }
340
f0fca04e
AT
341 if (am_daemon) {
342 char *name = lp_name(module_id);
343 int l = strlen(name);
344 for (i=0;i<argc;i++) {
345 if (strncmp(argv[i], name, l) == 0) {
346 argv[i] += l;
347 if (!*argv[i]) argv[i] = ".";
348 }
349 }
350 }
351
c627d613 352 if (argc == 0 && recurse) {
76076c4b
AT
353 argc=1;
354 argv--;
355 argv[0] = ".";
c627d613 356 }
f0fca04e
AT
357
358 rprintf(FINFO,"sending file list\n");
c627d613 359
9486289c
AT
360 flist = send_file_list(f_out,argc,argv);
361 send_files(flist,f_out,f_in);
362 report(f_out);
34ccb63e 363 exit_cleanup(0);
c627d613
AT
364}
365
366
dc5ddbcc
AT
367static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
368{
369 int pid;
370 int status=0;
c6e7fcb4 371 int recv_pipe[2];
dc5ddbcc
AT
372
373 if (preserve_hard_links)
374 init_hard_links(flist);
375
c6e7fcb4 376 if (pipe(recv_pipe) < 0) {
9486289c 377 rprintf(FERROR,"pipe failed in do_recv\n");
c6e7fcb4
AT
378 exit(1);
379 }
380
381
3ba62a83 382 if ((pid=do_fork()) == 0) {
c6e7fcb4 383 recv_files(f_in,flist,local_name,recv_pipe[1]);
dc5ddbcc 384 if (verbose > 2)
9486289c 385 rprintf(FINFO,"receiver read %ld\n",(long)read_total());
dc5ddbcc
AT
386 exit_cleanup(0);
387 }
388
c6e7fcb4 389 generate_files(f_out,flist,local_name,recv_pipe[0]);
dc5ddbcc
AT
390
391 waitpid(pid, &status, 0);
392
393 return status;
394}
395
c627d613 396
9486289c 397static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
c627d613 398{
dc5ddbcc 399 int status;
c627d613
AT
400 struct file_list *flist;
401 char *local_name=NULL;
76076c4b 402 char *dir = NULL;
c627d613
AT
403
404 if (verbose > 2)
9486289c 405 rprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
c627d613 406
f0fca04e
AT
407 if (am_daemon) {
408 char *name = lp_name(module_id);
409 int i, l = strlen(name);
410 for (i=0;i<argc;i++) {
411 if (strncmp(argv[i], name, l) == 0) {
412 argv[i] += l;
413 if (!*argv[i]) argv[i] = ".";
414 }
415 rprintf(FINFO,"argv[%d]=%s\n", i, argv[i]);
416 }
417 }
418
c627d613 419 if (argc > 0) {
76076c4b
AT
420 dir = argv[0];
421 argc--;
422 argv++;
f0fca04e 423 if (!am_daemon && chdir(dir) != 0) {
9486289c 424 rprintf(FERROR,"chdir %s : %s (4)\n",
76076c4b
AT
425 dir,strerror(errno));
426 exit_cleanup(1);
427 }
c627d613
AT
428 }
429
430 if (delete_mode)
9486289c 431 recv_exclude_list(f_in);
c627d613 432
9486289c 433 flist = recv_file_list(f_in);
c627d613 434 if (!flist || flist->count == 0) {
9486289c 435 rprintf(FERROR,"nothing to do\n");
34ccb63e 436 exit_cleanup(1);
c627d613
AT
437 }
438
439 if (argc > 0) {
76076c4b
AT
440 if (strcmp(dir,".")) {
441 argv[0] += strlen(dir);
442 if (argv[0][0] == '/') argv[0]++;
443 }
444 local_name = get_local_name(flist,argv[0]);
c627d613
AT
445 }
446
9486289c 447 status = do_recv(f_in,f_out,flist,local_name);
34ccb63e 448 exit_cleanup(status);
c627d613
AT
449}
450
451
9486289c 452void start_server(int f_in, int f_out, int argc, char *argv[])
366345fe 453{
9486289c 454 setup_protocol(f_out, f_in);
366345fe
AT
455
456 if (am_sender) {
9486289c 457 recv_exclude_list(f_in);
366345fe
AT
458 if (cvs_exclude)
459 add_cvs_excludes();
9486289c 460 do_server_sender(f_in, f_out, argc, argv);
366345fe 461 } else {
9486289c 462 do_server_recv(f_in, f_out, argc, argv);
366345fe
AT
463 }
464 exit_cleanup(0);
465}
466
9486289c
AT
467static int client_run(int f_in, int f_out, int pid, int argc, char *argv[])
468{
469 struct file_list *flist;
470 int status = 0, status2 = 0;
471 char *local_name = NULL;
472
473 setup_protocol(f_out,f_in);
474
475 if (am_sender) {
476 if (cvs_exclude)
477 add_cvs_excludes();
478 if (delete_mode)
479 send_exclude_list(f_out);
480 flist = send_file_list(f_out,argc,argv);
481 if (verbose > 3)
482 rprintf(FINFO,"file list sent\n");
483 send_files(flist,f_out,f_in);
484 if (pid != -1) {
485 if (verbose > 3)
486 rprintf(FINFO,"waiting on %d\n",pid);
487 waitpid(pid, &status, 0);
488 }
489 report(-1);
490 exit_cleanup(status);
491 }
492
493 send_exclude_list(f_out);
494
495 flist = recv_file_list(f_in);
496 if (!flist || flist->count == 0) {
497 rprintf(FINFO,"nothing to do\n");
498 exit_cleanup(0);
499 }
500
501 local_name = get_local_name(flist,argv[0]);
502
503 status2 = do_recv(f_in,f_out,flist,local_name);
504
505 report(f_in);
506
507 if (pid != -1) {
508 waitpid(pid, &status, 0);
509 }
510
511 return status | status2;
512}
513
514
515int start_socket_client(char *host, char *path, int argc, char *argv[])
516{
f0fca04e
AT
517 int fd, i;
518 char *sargs[MAX_ARGS];
9486289c 519 int sargc=0;
f0fca04e
AT
520 char line[1024];
521 char *p;
522 int version;
523
9486289c
AT
524 fd = open_socket_out(host, port);
525 if (fd == -1) {
9486289c
AT
526 exit_cleanup(1);
527 }
528
529 server_options(sargs,&sargc);
530
531 sargs[sargc++] = ".";
532
533 if (path && *path)
534 sargs[sargc++] = path;
535
536 sargs[sargc] = NULL;
537
f0fca04e
AT
538 p = strchr(path,'/');
539 if (p) *p = 0;
540 io_printf(fd,"%s\n",path);
541 if (p) *p = '/';
542
543 if (!read_line(fd, line, sizeof(line)-1)) {
544 return -1;
545 }
546
547 if (sscanf(line,"RSYNCD %d", &version) != 1) {
548 return -1;
549 }
550
551 while (1) {
552 if (!read_line(fd, line, sizeof(line)-1)) {
553 return -1;
554 }
555 if (strcmp(line,"RSYNCD: OK") == 0) break;
556 rprintf(FINFO,"%s\n", line);
557 }
558
559 for (i=0;i<sargc;i++) {
560 io_printf(fd,"%s\n", sargs[i]);
561 }
562 io_printf(fd,"\n");
563
564#if 0
565 while (1) {
566 if (!read_line(fd, line, sizeof(line)-1)) {
567 return -1;
568 }
569 rprintf(FINFO,"%s\n", line);
570 }
571#endif
572
9486289c
AT
573 return client_run(fd, fd, -1, argc, argv);
574}
575
5d6bcd44
AT
576int start_client(int argc, char *argv[])
577{
578 char *p;
579 char *shell_machine = NULL;
580 char *shell_path = NULL;
581 char *shell_user = NULL;
9486289c 582 int pid;
5d6bcd44 583 int f_in,f_out;
5d6bcd44
AT
584
585 p = strchr(argv[0],':');
586
587 if (p) {
9486289c
AT
588 if (p[1] == ':') {
589 *p = 0;
590 return start_socket_client(argv[0], p+2, argc-1, argv+1);
591 }
5d6bcd44
AT
592 am_sender = 0;
593 *p = 0;
594 shell_machine = argv[0];
595 shell_path = p+1;
596 argc--;
597 argv++;
598 } else {
599 am_sender = 1;
9486289c 600
5d6bcd44
AT
601 p = strchr(argv[argc-1],':');
602 if (!p) {
603 local_server = 1;
9486289c
AT
604 } else if (p[1] == ':') {
605 *p = 0;
606 return start_socket_client(argv[argc-1], p+2, argc-1, argv);
5d6bcd44 607 }
9486289c 608
5d6bcd44
AT
609 if (local_server) {
610 shell_machine = NULL;
611 shell_path = argv[argc-1];
612 } else {
613 *p = 0;
614 shell_machine = argv[argc-1];
615 shell_path = p+1;
616 }
617 argc--;
618 }
619
620 if (shell_machine) {
621 p = strchr(shell_machine,'@');
622 if (p) {
623 *p = 0;
624 shell_user = shell_machine;
625 shell_machine = p+1;
626 }
627 }
628
629 if (verbose > 3) {
9486289c 630 rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
5d6bcd44
AT
631 shell_cmd?shell_cmd:"",
632 shell_machine?shell_machine:"",
633 shell_user?shell_user:"",
634 shell_path?shell_path:"");
635 }
636
637 if (!am_sender && argc != 1) {
638 usage(FERROR);
639 exit_cleanup(1);
640 }
641
642 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
643
5d6bcd44 644#if HAVE_SETLINEBUF
9486289c
AT
645 setlinebuf(stdout);
646 setlinebuf(stderr);
5d6bcd44 647#endif
9486289c
AT
648
649 return client_run(f_in, f_out, pid, argc, argv);
5d6bcd44
AT
650}
651
366345fe 652
9486289c 653static void usage(int F)
c627d613 654{
9486289c 655 rprintf(F,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
c627d613 656 VERSION);
9486289c
AT
657 rprintf(F,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
658 rprintf(F,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
659 rprintf(F,"Options:\n");
660 rprintf(F,"-v, --verbose increase verbosity\n");
661 rprintf(F,"-c, --checksum always checksum\n");
662 rprintf(F,"-a, --archive archive mode (same as -rlptDog)\n");
663 rprintf(F,"-r, --recursive recurse into directories\n");
664 rprintf(F,"-R, --relative use relative path names\n");
665 rprintf(F,"-b, --backup make backups (default ~ extension)\n");
666 rprintf(F,"-u, --update update only (don't overwrite newer files)\n");
667 rprintf(F,"-l, --links preserve soft links\n");
668 rprintf(F,"-L, --copy-links treat soft links like regular files\n");
669 rprintf(F,"-H, --hard-links preserve hard links\n");
670 rprintf(F,"-p, --perms preserve permissions\n");
671 rprintf(F,"-o, --owner preserve owner (root only)\n");
672 rprintf(F,"-g, --group preserve group\n");
673 rprintf(F,"-D, --devices preserve devices (root only)\n");
674 rprintf(F,"-t, --times preserve times\n");
675 rprintf(F,"-S, --sparse handle sparse files efficiently\n");
676 rprintf(F,"-n, --dry-run show what would have been transferred\n");
677 rprintf(F,"-W, --whole-file copy whole files, no incremental checks\n");
678 rprintf(F,"-x, --one-file-system don't cross filesystem boundaries\n");
679 rprintf(F,"-B, --block-size SIZE checksum blocking size\n");
680 rprintf(F,"-e, --rsh COMMAND specify rsh replacement\n");
681 rprintf(F," --rsync-path PATH specify path to rsync on the remote machine\n");
682 rprintf(F,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
683 rprintf(F," --delete delete files that don't exist on the sending side\n");
684 rprintf(F," --force force deletion of directories even if not empty\n");
685 rprintf(F," --numeric-ids don't map uid/gid values by user/group name\n");
686 rprintf(F," --timeout TIME set IO timeout in seconds\n");
687 rprintf(F,"-I, --ignore-times don't exclude files that match length and time\n");
688 rprintf(F,"-T --temp-dir DIR create temporary files in directory DIR\n");
689 rprintf(F,"-z, --compress compress file data\n");
690 rprintf(F," --exclude FILE exclude file FILE\n");
691 rprintf(F," --exclude-from FILE exclude files listed in FILE\n");
692 rprintf(F," --suffix SUFFIX override backup suffix\n");
693 rprintf(F," --version print version number\n");
694
695 rprintf(F,"\n");
696 rprintf(F,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
697 rprintf(F,"the block size defaults to %d\n",BLOCK_SIZE);
c627d613
AT
698}
699
700enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
6ba9279f 701 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH,
9486289c 702 OPT_FORCE,OPT_TIMEOUT,OPT_DAEMON};
c627d613 703
950ab32d 704static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:T:z";
c627d613
AT
705
706static struct option long_options[] = {
707 {"version", 0, 0, OPT_VERSION},
708 {"server", 0, 0, OPT_SERVER},
709 {"sender", 0, 0, OPT_SENDER},
710 {"delete", 0, 0, OPT_DELETE},
3cb6f5d6 711 {"force", 0, 0, OPT_FORCE},
f6c34742 712 {"numeric-ids", 0, 0, OPT_NUMERIC_IDS},
c627d613
AT
713 {"exclude", 1, 0, OPT_EXCLUDE},
714 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
715 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
716 {"one-file-system",0, 0, 'x'},
717 {"ignore-times",0, 0, 'I'},
718 {"help", 0, 0, 'h'},
719 {"dry-run", 0, 0, 'n'},
dc5ddbcc 720 {"sparse", 0, 0, 'S'},
c627d613
AT
721 {"cvs-exclude", 0, 0, 'C'},
722 {"archive", 0, 0, 'a'},
723 {"checksum", 0, 0, 'c'},
724 {"backup", 0, 0, 'b'},
725 {"update", 0, 0, 'u'},
726 {"verbose", 0, 0, 'v'},
727 {"recursive", 0, 0, 'r'},
6574b4f7 728 {"relative", 0, 0, 'R'},
c627d613
AT
729 {"devices", 0, 0, 'D'},
730 {"perms", 0, 0, 'p'},
731 {"links", 0, 0, 'l'},
82306bf6
AT
732 {"copy-links", 0, 0, 'L'},
733 {"whole-file", 0, 0, 'W'},
dc5ddbcc 734 {"hard-links", 0, 0, 'H'},
c627d613
AT
735 {"owner", 0, 0, 'o'},
736 {"group", 0, 0, 'g'},
737 {"times", 0, 0, 't'},
738 {"rsh", 1, 0, 'e'},
739 {"suffix", 1, 0, OPT_SUFFIX},
740 {"block-size", 1, 0, 'B'},
6ba9279f 741 {"timeout", 1, 0, OPT_TIMEOUT},
950ab32d 742 {"temp-dir", 1, 0, 'T'},
861c20b4 743 {"compress", 0, 0, 'z'},
9486289c 744 {"daemon", 0, 0, OPT_DAEMON},
c627d613
AT
745 {0,0,0,0}};
746
82306bf6
AT
747RETSIGTYPE sigusr1_handler(int val) {
748 exit_cleanup(1);
749}
750
5d6bcd44
AT
751
752static void parse_arguments(int argc, char *argv[])
c627d613 753{
c627d613
AT
754 int opt;
755 int option_index;
c627d613
AT
756
757 while ((opt = getopt_long(argc, argv,
758 short_options, long_options, &option_index))
759 != -1) {
760 switch (opt)
761 {
762 case OPT_VERSION:
763 printf("rsync version %s protocol version %d\n",
764 VERSION,PROTOCOL_VERSION);
34ccb63e 765 exit_cleanup(0);
c627d613
AT
766
767 case OPT_SUFFIX:
768 backup_suffix = optarg;
769 break;
770
771 case OPT_RSYNC_PATH:
772 rsync_path = optarg;
773 break;
774
775 case 'I':
776 ignore_times = 1;
777 break;
778
779 case 'x':
780 one_file_system=1;
781 break;
782
783 case OPT_DELETE:
784 delete_mode = 1;
785 break;
786
3cb6f5d6
AT
787 case OPT_FORCE:
788 force_delete = 1;
789 break;
790
f6c34742
AT
791 case OPT_NUMERIC_IDS:
792 numeric_ids = 1;
793 break;
794
c627d613
AT
795 case OPT_EXCLUDE:
796 add_exclude(optarg);
797 break;
798
799 case OPT_EXCLUDE_FROM:
800 add_exclude_file(optarg,1);
801 break;
802
803 case 'h':
dc5ddbcc 804 usage(FINFO);
34ccb63e 805 exit_cleanup(0);
c627d613
AT
806
807 case 'b':
808 make_backups=1;
809 break;
810
811 case 'n':
812 dry_run=1;
813 break;
814
dc5ddbcc
AT
815 case 'S':
816 sparse_files=1;
817 break;
818
c627d613
AT
819 case 'C':
820 cvs_exclude=1;
821 break;
822
823 case 'u':
824 update_only=1;
825 break;
826
c627d613
AT
827 case 'l':
828 preserve_links=1;
829 break;
dc5ddbcc 830
82306bf6
AT
831 case 'L':
832 copy_links=1;
833 break;
834
835 case 'W':
836 whole_file=1;
837 break;
838
dc5ddbcc
AT
839 case 'H':
840#if SUPPORT_HARD_LINKS
841 preserve_hard_links=1;
cbbe4892 842#else
9486289c 843 rprintf(FERROR,"ERROR: hard links not supported on this platform\n");
cbbe4892 844 exit_cleanup(1);
c627d613 845#endif
dc5ddbcc 846 break;
c627d613
AT
847
848 case 'p':
849 preserve_perms=1;
850 break;
851
852 case 'o':
7b8356d0 853 preserve_uid=1;
c627d613
AT
854 break;
855
856 case 'g':
857 preserve_gid=1;
858 break;
859
860 case 'D':
7b8356d0 861 preserve_devices=1;
c627d613
AT
862 break;
863
864 case 't':
865 preserve_times=1;
866 break;
867
868 case 'c':
869 always_checksum=1;
870 break;
871
872 case 'v':
873 verbose++;
874 break;
875
876 case 'a':
877 recurse=1;
878#if SUPPORT_LINKS
879 preserve_links=1;
880#endif
881 preserve_perms=1;
882 preserve_times=1;
883 preserve_gid=1;
7b8356d0 884 if (am_root) {
c627d613
AT
885 preserve_devices=1;
886 preserve_uid=1;
7b8356d0 887 }
c627d613
AT
888 break;
889
890 case OPT_SERVER:
891 am_server = 1;
892 break;
893
894 case OPT_SENDER:
895 if (!am_server) {
dc5ddbcc 896 usage(FERROR);
34ccb63e 897 exit_cleanup(1);
c627d613 898 }
366345fe 899 am_sender = 1;
c627d613
AT
900 break;
901
902 case 'r':
903 recurse = 1;
904 break;
905
6574b4f7
AT
906 case 'R':
907 relative_paths = 1;
908 break;
909
c627d613
AT
910 case 'e':
911 shell_cmd = optarg;
912 break;
913
914 case 'B':
915 block_size = atoi(optarg);
916 break;
917
6ba9279f
AT
918 case OPT_TIMEOUT:
919 io_timeout = atoi(optarg);
920 break;
921
950ab32d
AT
922 case 'T':
923 tmpdir = optarg;
924 break;
925
861c20b4
PM
926 case 'z':
927 do_compression = 1;
928 break;
929
9486289c
AT
930 case OPT_DAEMON:
931 am_daemon = 1;
932 break;
933
c627d613 934 default:
9486289c 935 /* rprintf(FERROR,"bad option -%c\n",opt); */
34ccb63e 936 exit_cleanup(1);
c627d613
AT
937 }
938 }
5d6bcd44
AT
939}
940
f0fca04e
AT
941static int rsync_module(int fd, int i)
942{
943 int argc=0;
944 char *argv[MAX_ARGS];
945 char **argp;
946 char line[1024];
947
948 module_id = i;
949
950 if (lp_read_only(i))
951 read_only = 1;
952
953 rprintf(FERROR,"rsyncd starting\n");
954
955 if (chroot(lp_path(i))) {
956 io_printf(fd,"ERROR: chroot failed\n");
957 return -1;
958 }
959
960 if (chdir("/")) {
961 io_printf(fd,"ERROR: chdir failed\n");
962 return -1;
963 }
964
965 if (setgid(lp_gid(i))) {
966 io_printf(fd,"ERROR: setgid failed\n");
967 return -1;
968 }
969
970 if (setuid(lp_uid(i))) {
971 io_printf(fd,"ERROR: setuid failed\n");
972 return -1;
973 }
974
975 io_printf(fd,"RSYNCD: OK\n");
976
977 argv[argc++] = "rsyncd";
978
979 while (1) {
980 if (!read_line(fd, line, sizeof(line)-1)) {
981 return -1;
982 }
983
984 if (!*line) break;
985
986 argv[argc] = strdup(line);
987 if (!argv[argc]) {
988 return -1;
989 }
990
991 argc++;
992 if (argc == MAX_ARGS) {
993 return -1;
994 }
995 }
996
997 parse_arguments(argc, argv);
998
999 /* don't allow the logs to be flooded too fast */
1000 if (verbose > 1) verbose = 1;
1001
1002 argc -= optind;
1003 argp = argv + optind;
1004 optind = 0;
1005
1006 start_server(fd, fd, argc, argp);
1007
1008 return 0;
1009}
1010
1011static void send_listing(int fd)
1012{
1013 int n = lp_numservices();
1014 int i;
1015
1016 for (i=0;i<n;i++)
1017 if (lp_list(i))
1018 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
1019}
1020
1021/* this is called when a socket connection is established to a client
1022 and we want to start talking. The setup of the system is done from
1023 here */
1024static int start_daemon(int fd)
1025{
1026 char line[1024];
1027 char *motd;
1028
1029 set_socket_options(fd,"SO_KEEPALIVE");
1030
1031 io_printf(fd,"RSYNCD %d\n", PROTOCOL_VERSION);
1032
1033 motd = lp_motd_file();
1034 if (*motd) {
1035 FILE *f = fopen(motd,"r");
1036 while (f && !feof(f)) {
1037 int len = fread(line, 1, sizeof(line)-1, f);
1038 if (len > 0) {
1039 line[len] = 0;
1040 io_printf(fd,"%s", line);
1041 }
1042 }
1043 if (f) fclose(f);
1044 io_printf(fd,"\n");
1045 }
1046
1047 /* read a single line indicating the resource that is wanted */
1048 while (1) {
1049 int i;
1050
1051 line[0] = 0;
1052 if (!read_line(fd, line, sizeof(line)-1)) {
1053 return -1;
1054 }
1055
1056 if (!*line || strcmp(line,"#list")==0) {
1057 send_listing(fd);
1058 return -1;
1059 }
1060
1061 if (*line == '#') {
1062 /* it's some sort of command that I don't understand */
1063 io_printf(fd,"ERROR: Unknown command '%s'\n", line);
1064 return -1;
1065 }
1066
1067 i = lp_number(line);
1068 if (i == -1) {
1069 io_printf(fd,"ERROR: Unknown module '%s'\n", line);
1070 return -1;
1071 }
1072
1073 return rsync_module(fd, i);
1074 }
1075
1076 return 0;
1077}
1078
1079
1080static int daemon_main(void)
1081{
1082 if (!lp_load(RSYNCD_CONF)) {
1083 exit_cleanup(1);
1084 }
1085
1086 if (is_a_socket(STDIN_FILENO)) {
1087 /* we are running via inetd */
1088 return start_daemon(STDIN_FILENO);
1089 }
1090
1091 become_daemon();
1092
1093 return start_accept_loop(port, start_daemon);
1094}
1095
5d6bcd44
AT
1096int main(int argc,char *argv[])
1097{
1098
1099 signal(SIGUSR1, sigusr1_handler);
1100
1101 starttime = time(NULL);
1102 am_root = (getuid() == 0);
c627d613 1103
5d6bcd44
AT
1104 /* we set a 0 umask so that correct file permissions can be
1105 carried across */
1106 orig_umask = (int)umask(0);
1107
1108 parse_arguments(argc, argv);
1109
f0fca04e
AT
1110 argc -= optind;
1111 argv += optind;
1112 optind = 0;
c627d613 1113
6b83141d
AT
1114 signal(SIGCHLD,SIG_IGN);
1115 signal(SIGINT,SIGNAL_CAST sig_int);
1116 signal(SIGPIPE,SIGNAL_CAST sig_int);
1117 signal(SIGHUP,SIGNAL_CAST sig_int);
1118
f0fca04e
AT
1119 if (am_daemon) {
1120 return daemon_main();
1121 }
1122
c627d613
AT
1123 if (dry_run)
1124 verbose = MAX(verbose,1);
1125
cbbe4892
AT
1126#ifndef SUPPORT_LINKS
1127 if (!am_server && preserve_links) {
9486289c 1128 rprintf(FERROR,"ERROR: symbolic links not supported\n");
cbbe4892
AT
1129 exit_cleanup(1);
1130 }
1131#endif
1132
c627d613 1133 if (am_server) {
9486289c 1134 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
c627d613
AT
1135 }
1136
1137 if (argc < 2) {
dc5ddbcc 1138 usage(FERROR);
34ccb63e 1139 exit_cleanup(1);
c627d613
AT
1140 }
1141
5d6bcd44 1142 return start_client(argc, argv);
c627d613 1143}
82306bf6 1144