2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: client_util.c,v 1.34 2006/05/25 01:47:11 johnfranks Exp $
33 #include "client_util.h"
36 #include "timestamp.h"
37 #include "pipespawn.h"
39 #define MAXMAXDUMPS 16
41 static int add_exclude(FILE *file_exclude, char *aexc, int verbose);
42 static int add_include(char *disk, char *device, FILE *file_include, char *ainc, int verbose);
43 static char *build_name(char *disk, char *exin, int verbose);
44 static char *get_name(char *diskname, char *exin, time_t t, int n);
54 char *dirname = amname_to_dirname(device);
55 newname = vstralloc(dirname, "/", name , NULL);
59 newname = stralloc(name);
72 char number[NUM_STR_SIZE];
76 ts = get_timestamp_from_time(t);
80 g_snprintf(number, SIZEOF(number), "%03d", n - 1);
82 filename = vstralloc(get_pname(), ".", diskname, ".", ts, number, ".",
97 char *filename = NULL;
98 char *afilename = NULL;
104 struct dirent *entry;
106 size_t match_len, d_name_len;
110 diskname = sanitise_filename(disk);
112 dbgdir = stralloc2(AMANDA_TMPDIR, "/");
113 if((d = opendir(AMANDA_TMPDIR)) == NULL) {
114 error(_("open debug directory \"%s\": %s"),
115 AMANDA_TMPDIR, strerror(errno));
118 test_name = get_name(diskname, exin,
119 curtime - (AMANDA_DEBUG_DAYS * 24 * 60 * 60), 0);
120 match_len = strlen(get_pname()) + strlen(diskname) + 2;
121 while((entry = readdir(d)) != NULL) {
122 if(is_dot_or_dotdot(entry->d_name)) {
125 d_name_len = strlen(entry->d_name);
126 if(strncmp(test_name, entry->d_name, match_len) != 0
127 || d_name_len < match_len + 14 + 8
128 || strcmp(entry->d_name+ d_name_len - 7, exin) != 0) {
129 continue; /* not one of our files */
131 if(strcmp(entry->d_name, test_name) < 0) {
132 e = newvstralloc(e, dbgdir, entry->d_name, NULL);
133 (void) unlink(e); /* get rid of old file */
142 filename = get_name(diskname, exin, curtime, n);
143 afilename = newvstralloc(afilename, dbgdir, filename, NULL);
144 if((fd=open(afilename, O_WRONLY|O_CREAT|O_APPEND, 0600)) < 0){
152 } while(!afilename && n < 1000);
154 if(afilename == NULL) {
155 filename = get_name(diskname, exin, curtime, 0);
156 afilename = newvstralloc(afilename, dbgdir, filename, NULL);
157 quoted = quote_string(afilename);
158 dbprintf(_("Cannot create %s (%s)\n"), quoted, strerror(errno));
160 g_printf(_("ERROR [cannot create %s (%s)]\n"),
161 quoted, strerror(errno));
184 (void)verbose; /* Quiet unused parameter warning */
187 if(aexc[l-1] == '\n') {
191 file = quoted = quote_string(aexc);
193 file[strlen(file) - 1] = '\0';
196 g_fprintf(file_exclude, "%s\n", file);
213 (void)disk; /* Quiet unused parameter warning */
216 if(ainc[l-1] == '\n') {
220 if (strncmp(ainc, "./", 2) != 0) {
221 quoted = quote_string(ainc);
222 dbprintf(_("include must start with './' (%s)\n"), quoted);
224 g_printf(_("ERROR [include must start with './' (%s)]\n"), quoted);
229 char *incname = ainc+2;
231 if(strchr(incname, '/')) {
232 file = quoted = quote_string(ainc);
234 file[strlen(file) - 1] = '\0';
237 g_fprintf(file_include, "%s\n", file);
244 struct dirent *entry;
246 regex = glob_to_regex(incname);
247 if((d = opendir(device)) == NULL) {
248 quoted = quote_string(device);
249 dbprintf(_("Can't open disk %s\n"), quoted);
251 g_printf(_("ERROR [Can't open disk %s]\n"), quoted);
256 while((entry = readdir(d)) != NULL) {
257 if(is_dot_or_dotdot(entry->d_name)) {
260 if(match(regex, entry->d_name)) {
261 incname = vstralloc("./", entry->d_name, NULL);
262 file = quoted = quote_string(incname);
264 file[strlen(file) - 1] = '\0';
267 g_fprintf(file_include, "%s\n", file);
296 if(options->exclude_file) nb_exclude += options->exclude_file->nb_element;
297 if(options->exclude_list) nb_exclude += options->exclude_list->nb_element;
299 if(nb_exclude == 0) return NULL;
301 if((filename = build_name(disk, "exclude", verbose)) != NULL) {
302 if((file_exclude = fopen(filename,"w")) != NULL) {
304 if(options->exclude_file) {
305 for(excl = options->exclude_file->first; excl != NULL;
307 add_exclude(file_exclude, excl->name,
308 verbose && options->exclude_optional == 0);
312 if(options->exclude_list) {
313 for(excl = options->exclude_list->first; excl != NULL;
315 char *exclname = fixup_relative(excl->name, device);
316 if((exclude = fopen(exclname, "r")) != NULL) {
317 while ((aexc = agets(exclude)) != NULL) {
318 if (aexc[0] == '\0') {
322 add_exclude(file_exclude, aexc,
323 verbose && options->exclude_optional == 0);
329 quoted = quote_string(exclname);
330 dbprintf(_("Can't open exclude file %s (%s)\n"),
331 quoted, strerror(errno));
332 if(verbose && (options->exclude_optional == 0 ||
334 g_printf(_("ERROR [Can't open exclude file %s (%s)]\n"),
335 quoted, strerror(errno));
342 fclose(file_exclude);
345 quoted = quote_string(filename);
346 dbprintf(_("Can't create exclude file %s (%s)\n"),
347 quoted, strerror(errno));
349 g_printf(_("ERROR [Can't create exclude file %s (%s)]\n"),
350 quoted, strerror(errno));
375 if(options->include_file) nb_include += options->include_file->nb_element;
376 if(options->include_list) nb_include += options->include_list->nb_element;
378 if(nb_include == 0) return NULL;
380 if((filename = build_name(disk, "include", verbose)) != NULL) {
381 if((file_include = fopen(filename,"w")) != NULL) {
383 if(options->include_file) {
384 for(incl = options->include_file->first; incl != NULL;
386 nb_exp += add_include(disk, device, file_include,
388 verbose && options->include_optional == 0);
392 if(options->include_list) {
393 for(incl = options->include_list->first; incl != NULL;
395 char *inclname = fixup_relative(incl->name, device);
396 if((include = fopen(inclname, "r")) != NULL) {
397 while ((ainc = agets(include)) != NULL) {
398 if (ainc[0] == '\0') {
402 nb_exp += add_include(disk, device,
404 verbose && options->include_optional == 0);
410 quoted = quote_string(inclname);
411 dbprintf(_("Can't open include file %s (%s)\n"),
412 quoted, strerror(errno));
413 if(verbose && (options->include_optional == 0 ||
415 g_printf(_("ERROR [Can't open include file %s (%s)]\n"),
416 quoted, strerror(errno));
423 fclose(file_include);
426 quoted = quote_string(filename);
427 dbprintf(_("Can't create include file %s (%s)\n"),
428 quoted, strerror(errno));
430 g_printf(_("ERROR [Can't create include file %s (%s)]\n"),
431 quoted, strerror(errno));
438 quoted = quote_string(disk);
439 dbprintf(_("No include for %s\n"), quoted);
440 if(verbose && options->include_optional == 0) {
441 g_printf(_("ERROR [No include for %s]\n"), quoted);
455 options->compress = COMP_NONE;
456 options->srvcompprog = NULL;
457 options->clntcompprog = NULL;
458 options->encrypt = ENCRYPT_NONE;
459 options->kencrypt = 0;
460 options->srv_encrypt = NULL;
461 options->clnt_encrypt = NULL;
462 options->srv_decrypt_opt = NULL;
463 options->clnt_decrypt_opt = NULL;
464 options->no_record = 0;
465 options->createindex = 0;
466 options->auth = NULL;
467 options->exclude_file = NULL;
468 options->exclude_list = NULL;
469 options->include_file = NULL;
470 options->include_list = NULL;
471 options->exclude_optional = 0;
472 options->include_optional = 0;
490 (void)disk; /* Quiet unused parameter warning */
491 (void)device; /* Quiet unused parameter warning */
493 options = alloc(SIZEOF(option_t));
494 init_options(options);
495 options->str = stralloc(str);
500 while (tok != NULL) {
501 if(am_has_feature(fs, fe_options_auth)
502 && BSTRNCMP(tok,"auth=") == 0) {
503 if(options->auth != NULL) {
504 quoted = quote_string(tok + 5);
505 dbprintf(_("multiple auth option %s\n"), quoted);
507 g_printf(_("ERROR [multiple auth option %s]\n"), quoted);
511 options->auth = stralloc(&tok[5]);
513 else if(am_has_feature(fs, fe_options_bsd_auth)
514 && BSTRNCMP(tok, "bsd-auth") == 0) {
515 if(options->auth != NULL) {
516 dbprintf(_("multiple auth option\n"));
518 g_printf(_("ERROR [multiple auth option]\n"));
521 options->auth = stralloc("bsd");
523 else if(am_has_feature(fs, fe_options_krb4_auth)
524 && BSTRNCMP(tok, "krb4-auth") == 0) {
525 if(options->auth != NULL) {
526 dbprintf(_("multiple auth option\n"));
528 g_printf(_("ERROR [multiple auth option]\n"));
531 options->auth = stralloc("krb4");
533 else if(BSTRNCMP(tok, "compress-fast") == 0) {
534 if(options->compress != COMP_NONE) {
535 dbprintf(_("multiple compress option\n"));
537 g_printf(_("ERROR [multiple compress option]\n"));
540 options->compress = COMP_FAST;
542 else if(BSTRNCMP(tok, "compress-best") == 0) {
543 if(options->compress != COMP_NONE) {
544 dbprintf(_("multiple compress option\n"));
546 g_printf(_("ERROR [multiple compress option]\n"));
549 options->compress = COMP_BEST;
551 else if(BSTRNCMP(tok, "srvcomp-fast") == 0) {
552 if(options->compress != COMP_NONE) {
553 dbprintf(_("multiple compress option\n"));
555 g_printf(_("ERROR [multiple compress option]\n"));
558 options->compress = COMP_SERVER_FAST;
560 else if(BSTRNCMP(tok, "srvcomp-best") == 0) {
561 if(options->compress != COMP_NONE) {
562 dbprintf(_("multiple compress option\n"));
564 g_printf(_("ERROR [multiple compress option]\n"));
567 options->compress = COMP_SERVER_BEST;
569 else if(BSTRNCMP(tok, "srvcomp-cust=") == 0) {
570 if(options->compress != COMP_NONE) {
571 dbprintf(_("multiple compress option\n"));
573 g_printf(_("ERROR [multiple compress option]\n"));
576 options->srvcompprog = stralloc(tok + SIZEOF("srvcomp-cust=") -1);
577 options->compress = COMP_SERVER_CUST;
579 else if(BSTRNCMP(tok, "comp-cust=") == 0) {
580 if(options->compress != COMP_NONE) {
581 dbprintf(_("multiple compress option\n"));
583 g_printf(_("ERROR [multiple compress option]\n"));
586 options->clntcompprog = stralloc(tok + SIZEOF("comp-cust=") -1);
587 options->compress = COMP_CUST;
588 /* parse encryption options */
590 else if(BSTRNCMP(tok, "encrypt-serv-cust=") == 0) {
591 if(options->encrypt != ENCRYPT_NONE) {
592 dbprintf(_("multiple encrypt option\n"));
594 g_printf(_("ERROR [multiple encrypt option]\n"));
597 options->srv_encrypt = stralloc(tok + SIZEOF("encrypt-serv-cust=") -1);
598 options->encrypt = ENCRYPT_SERV_CUST;
600 else if(BSTRNCMP(tok, "encrypt-cust=") == 0) {
601 if(options->encrypt != ENCRYPT_NONE) {
602 dbprintf(_("multiple encrypt option\n"));
604 g_printf(_("ERROR [multiple encrypt option]\n"));
607 options->clnt_encrypt= stralloc(tok + SIZEOF("encrypt-cust=") -1);
608 options->encrypt = ENCRYPT_CUST;
610 else if(BSTRNCMP(tok, "server-decrypt-option=") == 0) {
611 options->srv_decrypt_opt = stralloc(tok + SIZEOF("server-decrypt-option=") -1);
613 else if(BSTRNCMP(tok, "client-decrypt-option=") == 0) {
614 options->clnt_decrypt_opt = stralloc(tok + SIZEOF("client-decrypt-option=") -1);
616 else if(BSTRNCMP(tok, "no-record") == 0) {
617 if(options->no_record != 0) {
618 dbprintf(_("multiple no-record option\n"));
620 g_printf(_("ERROR [multiple no-record option]\n"));
623 options->no_record = 1;
625 else if(BSTRNCMP(tok, "index") == 0) {
626 if(options->createindex != 0) {
627 dbprintf(_("multiple index option\n"));
629 g_printf(_("ERROR [multiple index option]\n"));
632 options->createindex = 1;
634 else if(BSTRNCMP(tok, "exclude-optional") == 0) {
635 if(options->exclude_optional != 0) {
636 dbprintf(_("multiple exclude-optional option\n"));
638 g_printf(_("ERROR [multiple exclude-optional option]\n"));
641 options->exclude_optional = 1;
643 else if(strcmp(tok, "include-optional") == 0) {
644 if(options->include_optional != 0) {
645 dbprintf(_("multiple include-optional option\n"));
647 g_printf(_("ERROR [multiple include-optional option]\n"));
650 options->include_optional = 1;
652 else if(BSTRNCMP(tok,"exclude-file=") == 0) {
653 exc = unquote_string(&tok[13]);
654 options->exclude_file = append_sl(options->exclude_file, exc);
657 else if(BSTRNCMP(tok,"exclude-list=") == 0) {
658 exc = unquote_string(&tok[13]);
659 options->exclude_list = append_sl(options->exclude_list, exc);
662 else if(BSTRNCMP(tok,"include-file=") == 0) {
663 inc = unquote_string(&tok[13]);
664 options->include_file = append_sl(options->include_file, inc);
667 else if(BSTRNCMP(tok,"include-list=") == 0) {
668 inc = unquote_string(&tok[13]);
669 options->include_list = append_sl(options->include_list, inc);
672 else if(BSTRNCMP(tok,"kencrypt") == 0) {
673 options->kencrypt = 1;
675 else if(strcmp(tok,"|") != 0) {
676 quoted = quote_string(tok);
677 dbprintf(_("unknown option %s\n"), quoted);
679 g_printf(_("ERROR [unknown option: %s]\n"), quoted);
683 tok = strtok(NULL, ";");
690 output_tool_property(
697 if (!is_empty_sl(options->exclude_file)) {
698 for(sle = options->exclude_file->first ; sle != NULL; sle=sle->next) {
699 q = quote_string(sle->name);
700 g_fprintf(tool, "EXCLUDE-FILE %s\n", q);
705 if (!is_empty_sl(options->exclude_list)) {
706 for(sle = options->exclude_list->first ; sle != NULL; sle=sle->next) {
707 q = quote_string(sle->name);
708 g_fprintf(tool, "EXCLUDE-LIST %s\n", q);
713 if (!is_empty_sl(options->include_file)) {
714 for(sle = options->include_file->first ; sle != NULL; sle=sle->next) {
715 q = quote_string(sle->name);
716 g_fprintf(tool, "INCLUDE-FILE %s\n", q);
721 if (!is_empty_sl(options->include_list)) {
722 for(sle = options->include_list->first ; sle != NULL; sle=sle->next) {
723 q = quote_string(sle->name);
724 g_fprintf(tool, "INCLUDE-LIST %s\n", q);
729 if (!is_empty_sl(options->exclude_file) ||
730 !is_empty_sl(options->exclude_list)) {
731 if (options->exclude_optional)
732 g_fprintf(tool, "EXCLUDE-OPTIONAL YES\n");
734 g_fprintf(tool, "EXCLUDE-OPTIONAL NO\n");
737 if (!is_empty_sl(options->include_file) ||
738 !is_empty_sl(options->include_list)) {
739 if (options->include_optional)
740 g_fprintf(tool, "INCLUDE-OPTIONAL YES\n");
742 g_fprintf(tool, "INCLUDE-OPTIONAL NO\n");
746 backup_support_option_t *
747 backup_support_option(
749 g_option_t *g_options,
754 int supportin, supportout, supporterr;
760 backup_support_option_t *bsu;
762 cmd = vstralloc(DUMPER_DIR, "/", program, NULL);
763 argvchild = malloc(5 * SIZEOF(char *));
765 argvchild[i++] = program;
766 argvchild[i++] = "support";
767 if (g_options->config) {
768 argvchild[i++] = "--config";
769 argvchild[i++] = g_options->config;
771 if (g_options->hostname) {
772 argvchild[i++] = "--host";
773 argvchild[i++] = g_options->hostname;
776 argvchild[i++] = "--disk";
777 argvchild[i++] = disk;
780 argvchild[i++] = "--device";
781 argvchild[i++] = amdevice;
783 argvchild[i++] = NULL;
785 supporterr = fileno(stderr);
786 supportpid = pipespawnv(cmd, STDIN_PIPE|STDOUT_PIPE, &supportin,
787 &supportout, &supporterr, argvchild);
791 bsu = malloc(SIZEOF(*bsu));
792 memset(bsu, '\0', SIZEOF(*bsu));
793 streamout = fdopen(supportout, "r");
794 while((line = agets(streamout)) != NULL) {
795 dbprintf(_("support line: %s\n"), line);
796 if (strncmp(line,"CONFIG ", 7) == 0) {
797 if (strcmp(line+7, "YES") == 0)
799 } else if (strncmp(line,"HOST ", 5) == 0) {
800 if (strcmp(line+5, "YES") == 0)
802 } else if (strncmp(line,"DISK ", 5) == 0) {
803 if (strcmp(line+5, "YES") == 0)
805 } else if (strncmp(line,"INDEX-LINE ", 11) == 0) {
806 if (strcmp(line+11, "YES") == 0)
808 } else if (strncmp(line,"INDEX-XML ", 10) == 0) {
809 if (strcmp(line+10, "YES") == 0)
811 } else if (strncmp(line,"MESSAGE-LINE ", 13) == 0) {
812 if (strcmp(line+13, "YES") == 0)
813 bsu->message_line = 1;
814 } else if (strncmp(line,"MESSAGE-XML ", 12) == 0) {
815 if (strcmp(line+12, "YES") == 0)
816 bsu->message_xml = 1;
817 } else if (strncmp(line,"RECORD ", 7) == 0) {
818 if (strcmp(line+7, "YES") == 0)
820 } else if (strncmp(line,"INCLUDE-FILE ", 13) == 0) {
821 if (strcmp(line+13, "YES") == 0)
822 bsu->include_file = 1;
823 } else if (strncmp(line,"INCLUDE-LIST ", 13) == 0) {
824 if (strcmp(line+13, "YES") == 0)
825 bsu->include_list = 1;
826 } else if (strncmp(line,"EXCLUDE-FILE ", 13) == 0) {
827 if (strcmp(line+13, "YES") == 0)
828 bsu->exclude_file = 1;
829 } else if (strncmp(line,"EXCLUDE-LIST ", 13) == 0) {
830 if (strcmp(line+13, "YES") == 0)
831 bsu->exclude_list = 1;
832 } else if (strncmp(line,"COLLECTION ", 11) == 0) {
833 if (strcmp(line+11, "YES") == 0)
835 } else if (strncmp(line,"MAX-LEVEL ", 10) == 0) {
836 bsu->max_level = atoi(line+10);
838 dbprintf(_("Invalid support line: %s\n"), line);