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: amindexd.c,v 1.106 2006/07/25 18:27:57 martinea Exp $
29 * This is the server daemon part of the index client/server system.
30 * It is assumed that this is launched from inetd instead of being
31 * started as a daemon because it is not often used
36 ** - this server will do very little until it knows what Amanda config it
37 ** is to use. Setting the config has the side effect of changing to the
39 ** - XXX - I'm pretty sure the config directory name should have '/'s stripped
40 ** from it. It is given to us by an unknown person via the network.
50 #include "disk_history.h"
57 #include "pipespawn.h"
58 #include "sockaddr-util.h"
63 #define DBG(i, ...) do { \
64 if ((i) <= debug_amindexd) { \
65 g_debug(__VA_ARGS__); \
69 typedef struct REMOVE_ITEM
72 struct REMOVE_ITEM *next;
76 static int from_amandad;
77 static char local_hostname[MAX_HOSTNAME_LENGTH+1]; /* me! */
78 static char *dump_hostname = NULL; /* machine we are restoring */
79 static char *disk_name; /* disk we are restoring */
80 char *qdisk_name = NULL; /* disk we are restoring */
81 static char *target_date = NULL;
82 static disklist_t disk_list; /* all disks in cur config */
83 static find_result_t *output_find = NULL;
84 static g_option_t *g_options = NULL;
86 static int amindexd_debug = 0;
88 static REMOVE_ITEM *uncompress_remove = NULL;
89 /* uncompressed files to remove */
91 static am_feature_t *our_features = NULL;
92 static am_feature_t *their_features = NULL;
94 static int get_pid_status(int pid, char *program, GPtrArray **emsg);
95 static REMOVE_ITEM *remove_files(REMOVE_ITEM *);
96 static char *uncompress_file(char *, GPtrArray **);
97 static int process_ls_dump(char *, DUMP_ITEM *, int, GPtrArray **);
99 static size_t reply_buffer_size = 1;
100 static char *reply_buffer = NULL;
101 static char *amandad_auth = NULL;
105 static void reply_ptr_array(int, GPtrArray *);
106 static void reply(int, char *, ...) G_GNUC_PRINTF(2, 3);
107 static void lreply(int, char *, ...) G_GNUC_PRINTF(2, 3);
108 static void fast_lreply(int, char *, ...) G_GNUC_PRINTF(2, 3);
109 static am_host_t *is_dump_host_valid(char *);
110 static int is_disk_valid(char *);
111 static int check_and_load_config(char *);
112 static int build_disk_table(void);
113 static int disk_history_list(void);
114 static int is_dir_valid_opaque(char *);
115 static int opaque_ls(char *, int);
116 static void opaque_ls_one (DIR_ITEM *dir_item, am_feature_e marshall_feature,
118 static int tapedev_is(void);
119 static int are_dumps_compressed(void);
120 static char *amindexd_nicedate (char *datestamp);
121 static int cmp_date (const char *date1, const char *date2);
122 static char *get_index_name(char *dump_hostname, char *hostname,
123 char *diskname, char *timestamps, int level);
124 static int get_index_dir(char *dump_hostname, char *hostname, char *diskname);
126 int main(int, char **);
136 amwait_t wait_status;
140 status = waitpid(pid, &wait_status, 0);
143 _("%s (%d) returned negative value: %s"),
144 program, pid, strerror(errno));
145 dbprintf("%s\n", msg);
146 g_ptr_array_add(*emsg, msg);
149 if (!WIFEXITED(wait_status)) {
151 _("%s exited with signal %d"),
152 program, WTERMSIG(wait_status));
153 dbprintf("%s\n", msg);
154 g_ptr_array_add(*emsg, msg);
156 } else if (WEXITSTATUS(wait_status) != 0) {
158 _("%s exited with status %d"),
159 program, WEXITSTATUS(wait_status));
160 dbprintf("%s\n", msg);
161 g_ptr_array_add(*emsg, msg);
175 dbprintf(_("removing index file: %s\n"), remove->filename);
176 unlink(remove->filename);
177 amfree(remove->filename);
179 remove = remove->next;
191 char *filename = NULL;
192 struct stat stat_filename;
199 int uncompress_errfd;
210 GPtrArray *uncompress_err;
212 FILE *uncompress_err_stream;
213 FILE *sort_err_stream;
215 filename = stralloc(filename_gz);
216 len = strlen(filename);
217 if(len > 3 && strcmp(&(filename[len-3]),".gz")==0) {
218 filename[len-3]='\0';
219 } else if(len > 2 && strcmp(&(filename[len-2]),".Z")==0) {
220 filename[len-2]='\0';
223 /* uncompress the file */
224 result=stat(filename,&stat_filename);
225 if(result==-1 && errno==ENOENT) { /* file does not exist */
227 REMOVE_ITEM *remove_file;
230 * Check that compressed file exists manually.
232 if (stat(filename_gz, &statbuf) < 0) {
233 msg = vstrallocf(_("Compressed file '%s' is inaccessable: %s"),
234 filename_gz, strerror(errno));
235 dbprintf("%s\n", msg);
236 g_ptr_array_add(*emsg, msg);
241 #ifdef UNCOMPRESS_OPT
242 # define PARAM_UNCOMPRESS_OPT UNCOMPRESS_OPT
244 # define PARAM_UNCOMPRESS_OPT skip_argument
247 nullfd = open("/dev/null", O_RDONLY);
249 indexfd = open(filename,O_WRONLY|O_CREAT, 0600);
251 msg = vstrallocf(_("Can't open '%s' for writting: %s"),
252 filename, strerror(errno));
253 dbprintf("%s\n", msg);
254 g_ptr_array_add(*emsg, msg);
260 /* just use our stderr directly for the pipe's stderr; in
261 * main() we send stderr to the debug file, or /dev/null
262 * if debugging is disabled */
264 /* start the uncompress process */
265 putenv(stralloc("LC_ALL=C"));
266 pid_gzip = pipespawn(UNCOMPRESS_PATH, STDOUT_PIPE|STDERR_PIPE, 0,
267 &nullfd, &pipe_from_gzip, &uncompress_errfd,
268 UNCOMPRESS_PATH, PARAM_UNCOMPRESS_OPT,
272 pipe_stream = fdopen(pipe_from_gzip,"r");
273 if(pipe_stream == NULL) {
274 msg = vstrallocf(_("Can't fdopen pipe from gzip: %s"),
276 dbprintf("%s\n", msg);
277 g_ptr_array_add(*emsg, msg);
283 /* start the sort process */
284 putenv(stralloc("LC_ALL=C"));
285 pid_sort = pipespawn(SORT_PATH, STDIN_PIPE|STDERR_PIPE, 0,
286 &pipe_to_sort, &indexfd, &sort_errfd,
290 /* start a subprocess */
291 /* send all ouput from uncompress process to sort process */
298 dbprintf("%s\n", msg);
299 g_ptr_array_add(*emsg, msg);
304 while (fgets(line, STR_SIZE, pipe_stream) != NULL) {
305 if (line[0] != '\0') {
306 if (strchr(line,'/')) {
307 full_write(pipe_to_sort,line,strlen(line));
315 aclose(pipe_to_sort);
317 uncompress_err_stream = fdopen(uncompress_errfd, "r");
318 uncompress_err = g_ptr_array_new();
319 while (fgets(line, sizeof(line), uncompress_err_stream) != NULL) {
320 if (line[strlen(line)-1] == '\n')
321 line[strlen(line)-1] = '\0';
322 g_ptr_array_add(uncompress_err, vstrallocf(" %s", line));
323 dbprintf("Uncompress: %s\n", line);
325 fclose(uncompress_err_stream);
327 sort_err_stream = fdopen(sort_errfd, "r");
328 sort_err = g_ptr_array_new();
329 while (fgets(line, sizeof(line), sort_err_stream) != NULL) {
330 if (line[strlen(line)-1] == '\n')
331 line[strlen(line)-1] = '\0';
332 g_ptr_array_add(sort_err, vstrallocf(" %s", line));
333 dbprintf("Sort: %s\n", line);
335 fclose(sort_err_stream);
337 status = get_pid_status(pid_gzip, UNCOMPRESS_PATH, emsg);
338 if (status == 0 && filename) {
342 if (uncompress_err->len > 0) {
343 p_last = uncompress_err->pdata + uncompress_err->len;
344 for (p = uncompress_err->pdata; p < p_last ;p++) {
345 g_ptr_array_add(*emsg, (char *)*p);
348 g_ptr_array_free(uncompress_err, TRUE);
350 status = get_pid_status(pid_index, "index", emsg);
351 if (status == 0 && filename) {
356 status = get_pid_status(pid_sort, SORT_PATH, emsg);
357 if (status == 0 && filename) {
361 if (sort_err->len > 0) {
362 p_last = sort_err->pdata + sort_err->len;
363 for (p = sort_err->pdata; p < p_last ;p++) {
364 g_ptr_array_add(*emsg, (char *)*p);
367 g_ptr_array_free(sort_err, TRUE);
369 /* add at beginning */
371 remove_file = (REMOVE_ITEM *)alloc(SIZEOF(REMOVE_ITEM));
372 remove_file->filename = stralloc(filename);
373 remove_file->next = uncompress_remove;
374 uncompress_remove = remove_file;
377 } else if(!S_ISREG((stat_filename.st_mode))) {
378 msg = vstrallocf(_("\"%s\" is not a regular file"), filename);
379 dbprintf("%s\n", msg);
380 g_ptr_array_add(*emsg, msg);
390 /* find all matching entries in a dump listing */
391 /* return -1 if error */
395 DUMP_ITEM * dump_item,
399 char line[STR_SIZE], old_line[STR_SIZE];
400 char *filename = NULL;
402 char *dir_slash = NULL;
406 size_t len_dir_slash;
409 if (strcmp(dir, "/") == 0) {
410 dir_slash = stralloc(dir);
412 dir_slash = stralloc2(dir, "/");
415 filename_gz = get_index_name(dump_hostname, dump_item->hostname, disk_name,
416 dump_item->date, dump_item->level);
417 if (filename_gz == NULL) {
418 g_ptr_array_add(*emsg, stralloc(_("index file not found")));
422 filename = uncompress_file(filename_gz, emsg);
423 if(filename == NULL) {
430 if((fp = fopen(filename,"r"))==0) {
431 g_ptr_array_add(*emsg, vstrallocf("%s", strerror(errno)));
437 len_dir_slash=strlen(dir_slash);
439 while (fgets(line, STR_SIZE, fp) != NULL) {
440 if (line[0] != '\0') {
441 if(line[strlen(line)-1] == '\n')
442 line[strlen(line)-1] = '\0';
443 if(strncmp(dir_slash, line, len_dir_slash) == 0) {
445 s = line + len_dir_slash;
447 while(ch && ch != '/')
448 ch = *s++;/* find end of the file name */
454 if(strcmp(line, old_line) != 0) {
455 add_dir_list_item(dump_item, line);
456 strcpy(old_line, line);
478 while (p != emsg->pdata + emsg->len -1) {
479 fast_lreply(n, "%s", (char *)*p);
482 reply(n, "%s", (char *)*p);
485 /* send a 1 line reply to the client */
486 printf_arglist_function1(static void reply, int, n, char *, fmt)
492 reply_buffer = alloc(reply_buffer_size);
495 arglist_start(args, fmt);
496 len = g_vsnprintf(reply_buffer, reply_buffer_size, fmt, args);
499 if (len > -1 && (size_t)len < reply_buffer_size-1)
502 reply_buffer_size *= 2;
503 amfree(reply_buffer);
504 reply_buffer = alloc(reply_buffer_size);
507 if (g_fprintf(cmdout,"%03d %s\r\n", n, reply_buffer) < 0)
509 dbprintf(_("! error %d (%s) in printf\n"), errno, strerror(errno));
510 uncompress_remove = remove_files(uncompress_remove);
513 if (fflush(cmdout) != 0)
515 dbprintf(_("! error %d (%s) in fflush\n"), errno, strerror(errno));
516 uncompress_remove = remove_files(uncompress_remove);
519 dbprintf(_("< %03d %s\n"), n, reply_buffer);
522 /* send one line of a multi-line response */
523 printf_arglist_function1(static void lreply, int, n, char *, fmt)
529 reply_buffer = alloc(reply_buffer_size);
532 arglist_start(args, fmt);
533 len = g_vsnprintf(reply_buffer, reply_buffer_size, fmt, args);
536 if (len > -1 && (size_t)len < reply_buffer_size-1)
539 reply_buffer_size *= 2;
540 amfree(reply_buffer);
541 reply_buffer = alloc(reply_buffer_size);
544 if (g_fprintf(cmdout,"%03d-%s\r\n", n, reply_buffer) < 0)
546 dbprintf(_("! error %d (%s) in printf\n"),
547 errno, strerror(errno));
548 uncompress_remove = remove_files(uncompress_remove);
551 if (fflush(cmdout) != 0)
553 dbprintf(_("! error %d (%s) in fflush\n"),
554 errno, strerror(errno));
555 uncompress_remove = remove_files(uncompress_remove);
559 dbprintf("< %03d-%s\n", n, reply_buffer);
563 /* send one line of a multi-line response */
564 printf_arglist_function1(static void fast_lreply, int, n, char *, fmt)
570 reply_buffer = alloc(reply_buffer_size);
573 arglist_start(args, fmt);
574 len = g_vsnprintf(reply_buffer, reply_buffer_size, fmt, args);
577 if (len > -1 && (size_t)len < reply_buffer_size-1)
580 reply_buffer_size *= 2;
581 amfree(reply_buffer);
582 reply_buffer = alloc(reply_buffer_size);
585 if (g_fprintf(cmdout,"%03d-%s\r\n", n, reply_buffer) < 0)
587 dbprintf(_("! error %d (%s) in printf\n"),
588 errno, strerror(errno));
589 uncompress_remove = remove_files(uncompress_remove);
593 DBG(2, "< %03d-%s", n, reply_buffer);
596 /* see if hostname is valid */
597 /* valid is defined to be that there is an index directory for it */
598 /* also do a security check on the requested dump hostname */
599 /* to restrict access to index records if required */
600 /* return -1 if not okay */
608 if (get_config_name() == NULL) {
609 reply(501, _("Must set config before setting host."));
613 /* check that the config actually handles that host */
614 ihost = lookup_host(host);
616 reply(501, _("Host %s is not in your disklist."), host);
620 /* check if an index dir exist */
621 if(get_index_dir(host, ihost->hostname, NULL)) {
625 /* check if an index dir exist for at least one DLE */
626 for(diskp = ihost->disks; diskp != NULL; diskp = diskp->hostnext) {
627 if (get_index_dir(diskp->hostname, NULL, NULL)) {
632 reply(501, _("No index records for host: %s. Have you enabled indexing?"),
642 dumptype_t *dt = disk->config;
643 host_limit_t *rl = NULL;
648 /* get the config: either for the DLE or the global config */
650 if (dumptype_seen(dt, DUMPTYPE_RECOVERY_LIMIT)) {
651 g_debug("using recovery limit from DLE");
652 rl = dumptype_get_recovery_limit(dt);
656 if (getconf_seen(CNF_RECOVERY_LIMIT)) {
657 g_debug("using global recovery limit");
658 rl = getconf_recovery_limit(CNF_RECOVERY_LIMIT);
662 g_debug("no recovery limit found; allowing access");
666 peer = getenv("AMANDA_AUTHENTICATED_PEER");
667 if (!peer || !*peer) {
668 g_warning("DLE has a recovery-limit, but no authenticated peer name is "
669 "available; rejecting");
673 /* check same-host */
674 dle_hostname = disk->host? disk->host->hostname : NULL;
675 if (rl->same_host && dle_hostname) {
676 if (0 == g_ascii_strcasecmp(peer, dle_hostname)) {
677 g_debug("peer matched same-host ('%s')", dle_hostname);
684 char myhostname[MAX_HOSTNAME_LENGTH+1];
685 if (gethostname(myhostname, MAX_HOSTNAME_LENGTH) == 0) {
686 myhostname[MAX_HOSTNAME_LENGTH] = '\0';
687 g_debug("server hostname: %s", myhostname);
688 if (0 == g_ascii_strcasecmp(peer, myhostname)) {
689 g_debug("peer matched server ('%s')", myhostname);
695 /* check the match list */
696 for (iter = rl->match_pats; iter; iter = iter->next) {
697 char *pat = iter->data;
698 if (match_host(pat, peer))
702 g_warning("peer '%s' does not match any of the recovery-limit restrictions; rejecting",
715 if (get_config_name() == NULL) {
716 reply(501, _("Must set config,host before setting disk."));
719 else if (dump_hostname == NULL) {
720 reply(501, _("Must set host before setting disk."));
724 /* check that the config actually handles that disk, and that recovery-limit allows it */
725 idisk = lookup_disk(dump_hostname, disk);
726 if(idisk == NULL || !is_disk_allowed(idisk)) {
727 qdisk = quote_string(disk);
728 reply(501, _("Disk %s:%s is not in the server's disklist."), dump_hostname, qdisk);
733 /* assume an index dir already */
734 if (get_index_dir(dump_hostname, idisk->hostname, disk) == 0) {
735 qdisk = quote_string(disk);
736 reply(501, _("No index records for disk: %s. Invalid?"), qdisk);
746 check_and_load_config(
752 struct stat dir_stat;
754 /* check that the config actually exists */
755 if (config == NULL) {
756 reply(501, _("Must set config first."));
760 /* (re-)initialize configuration with the new config name */
761 config_init(CONFIG_INIT_EXPLICIT_NAME, config);
762 if (config_errors(NULL) >= CFGERR_ERRORS) {
763 reply(501, _("Could not read config file for %s!"), config);
767 check_running_as(RUNNING_AS_DUMPUSER_PREFERRED);
769 conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
770 read_diskfile(conf_diskfile, &disk_list);
771 amfree(conf_diskfile);
772 if (config_errors(NULL) >= CFGERR_ERRORS) {
773 reply(501, _("Could not read disk file %s!"), conf_diskfile);
777 conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
778 if(read_tapelist(conf_tapelist)) {
779 reply(501, _("Could not read tapelist file %s!"), conf_tapelist);
780 amfree(conf_tapelist);
783 amfree(conf_tapelist);
785 dbrename(get_config_name(), DBG_SUBDIR_SERVER);
787 output_find = find_dump(&disk_list);
788 /* the 'w' here sorts by write timestamp, so that the first instance of
789 * any particular datestamp/host/disk/level/part that we see is the one
791 sort_find_result("DLKHpwB", &output_find);
793 conf_indexdir = config_dir_relative(getconf_str(CNF_INDEXDIR));
794 if (stat (conf_indexdir, &dir_stat) != 0 || !S_ISDIR(dir_stat.st_mode)) {
795 reply(501, _("Index directory %s does not exist"), conf_indexdir);
796 amfree(conf_indexdir);
799 amfree(conf_indexdir);
806 build_disk_table(void)
809 char *last_timestamp;
813 find_result_t *find_output;
815 if (get_config_name() == NULL) {
816 reply(590, _("Must set config,host,disk before building disk table"));
819 else if (dump_hostname == NULL) {
820 reply(590, _("Must set host,disk before building disk table"));
823 else if (disk_name == NULL) {
824 reply(590, _("Must set disk before building disk table"));
829 last_timestamp = NULL;
830 last_filenum = (off_t)-1;
833 for(find_output = output_find;
835 find_output = find_output->next) {
836 if(strcasecmp(dump_hostname, find_output->hostname) == 0 &&
837 strcmp(disk_name , find_output->diskname) == 0 &&
838 strcmp("OK" , find_output->status) == 0 &&
839 strcmp("OK" , find_output->dump_status) == 0) {
841 * The sort order puts holding disk entries first. We want to
842 * use them if at all possible, so ignore any other entries
843 * for the same datestamp after we see a holding disk entry
844 * (as indicated by a filenum of zero).
847 strcmp(find_output->timestamp, last_timestamp) == 0 &&
848 find_output->level == last_level &&
852 /* ignore duplicate partnum */
854 strcmp(find_output->timestamp, last_timestamp) == 0 &&
855 find_output->level == last_level &&
856 find_output->partnum == last_partnum) {
859 last_timestamp = find_output->timestamp;
860 last_filenum = find_output->filenum;
861 last_level = find_output->level;
862 last_partnum = find_output->partnum;
863 date = amindexd_nicedate(find_output->timestamp);
864 add_dump(find_output->hostname, date, find_output->level,
865 find_output->label, find_output->filenum,
866 find_output->partnum, find_output->totalparts);
867 dbprintf("- %s %d %s %lld %d %d\n",
868 date, find_output->level,
870 (long long)find_output->filenum,
871 find_output->partnum, find_output->totalparts);
882 disk_history_list(void)
887 if (get_config_name() == NULL) {
888 reply(502, _("Must set config,host,disk before listing history"));
891 else if (dump_hostname == NULL) {
892 reply(502, _("Must set host,disk before listing history"));
895 else if (disk_name == NULL) {
896 reply(502, _("Must set disk before listing history"));
900 lreply(200, _(" Dump history for config \"%s\" host \"%s\" disk %s"),
901 get_config_name(), dump_hostname, qdisk_name);
903 for (item=first_dump(); item!=NULL; item=next_dump(item)){
904 char *tapelist_str = marshal_tapelist(item->tapes, 1);
906 strncpy(date, item->date, 20);
908 if(!am_has_feature(their_features,fe_amrecover_timestamp))
911 if(am_has_feature(their_features, fe_amindexd_marshall_in_DHST)){
912 lreply(201, " %s %d %s", date, item->level, tapelist_str);
915 lreply(201, " %s %d %s %lld", date, item->level,
916 tapelist_str, (long long)item->file);
918 amfree(tapelist_str);
921 reply(200, _("Dump history for config \"%s\" host \"%s\" disk %s"),
922 get_config_name(), dump_hostname, qdisk_name);
929 * is the directory dir backed up - dir assumed complete relative to
932 /* opaque version of command */
942 char *filename_gz = NULL;
943 char *filename = NULL;
945 GPtrArray *emsg = NULL;
947 if (get_config_name() == NULL || dump_hostname == NULL || disk_name == NULL) {
948 reply(502, _("Must set config,host,disk before asking about directories"));
951 else if (dump_hostname == NULL) {
952 reply(502, _("Must set host,disk before asking about directories"));
955 else if (disk_name == NULL) {
956 reply(502, _("Must set disk before asking about directories"));
959 else if (target_date == NULL) {
960 reply(502, _("Must set date before asking about directories"));
963 /* scan through till we find first dump on or before date */
964 for (item=first_dump(); item!=NULL; item=next_dump(item))
965 if (cmp_date(item->date, target_date) <= 0)
970 /* no dump for given date */
971 reply(500, _("No dumps available on or before date \"%s\""), target_date);
975 if(strcmp(dir, "/") == 0) {
976 ldir = stralloc(dir);
978 ldir = stralloc2(dir, "/");
980 ldir_len = strlen(ldir);
982 /* go back till we hit a level 0 dump */
986 filename_gz = get_index_name(dump_hostname, item->hostname, disk_name,
987 item->date, item->level);
988 if (filename_gz == NULL) {
989 reply(599, "index not found");
993 emsg = g_ptr_array_new();
994 if((filename = uncompress_file(filename_gz, &emsg)) == NULL) {
995 reply_ptr_array(599, emsg);
997 g_ptr_array_free_full(emsg);
1001 g_ptr_array_free_full(emsg);
1002 amfree(filename_gz);
1003 dbprintf("f %s\n", filename);
1004 if ((fp = fopen(filename, "r")) == NULL) {
1005 reply(599, _("System error: %s"), strerror(errno));
1010 while (fgets(line, STR_SIZE, fp) != NULL) {
1011 if (line[0] == '\0')
1013 if(line[strlen(line)-1] == '\n')
1014 line[strlen(line)-1] = '\0';
1015 if (strncmp(line, ldir, ldir_len) != 0) {
1016 continue; /* not found yet */
1025 last_level = item->level;
1028 item=next_dump(item);
1029 } while ((item != NULL) && (item->level >= last_level));
1030 } while (item != NULL);
1034 reply(500, _("\"%s\" is an invalid directory"), dir);
1043 DUMP_ITEM *dump_item;
1045 int level, last_level;
1046 GPtrArray *emsg = NULL;
1047 am_feature_e marshall_feature;
1050 marshall_feature = fe_amindexd_marshall_in_ORLD;
1052 marshall_feature = fe_amindexd_marshall_in_OLSD;
1057 if (get_config_name() == NULL) {
1058 reply(502, _("Must set config,host,disk before listing a directory"));
1061 else if (dump_hostname == NULL) {
1062 reply(502, _("Must set host,disk before listing a directory"));
1065 else if (disk_name == NULL) {
1066 reply(502, _("Must set disk before listing a directory"));
1069 else if (target_date == NULL) {
1070 reply(502, _("Must set date before listing a directory"));
1074 /* scan through till we find first dump on or before date */
1075 for (dump_item=first_dump(); dump_item!=NULL; dump_item=next_dump(dump_item))
1076 if (cmp_date(dump_item->date, target_date) <= 0)
1079 if (dump_item == NULL)
1081 /* no dump for given date */
1082 reply(500, _("No dumps available on or before date \"%s\""), target_date);
1086 /* get data from that dump */
1087 emsg = g_ptr_array_new();
1088 if (process_ls_dump(dir, dump_item, recursive, &emsg) == -1) {
1089 reply_ptr_array(599, emsg);
1090 g_ptr_array_free_full(emsg);
1094 /* go back processing higher level dumps till we hit a level 0 dump */
1095 last_level = dump_item->level;
1096 while ((last_level != 0) && ((dump_item=next_dump(dump_item)) != NULL))
1098 if (dump_item->level < last_level)
1100 last_level = dump_item->level;
1101 if (process_ls_dump(dir, dump_item, recursive, &emsg) == -1) {
1102 reply_ptr_array(599, emsg);
1103 g_ptr_array_free_full(emsg);
1108 g_ptr_array_free_full(emsg);
1110 /* return the information to the caller */
1111 lreply(200, _(" Opaque list of %s"), dir);
1112 for(level=0; level < DUMP_LEVELS; level++) {
1113 for (dir_item = get_dir_list(); dir_item != NULL;
1114 dir_item = dir_item->next) {
1116 if(dir_item->dump->level == level) {
1117 if (!am_has_feature(their_features, marshall_feature) &&
1118 (num_entries(dir_item->dump->tapes) > 1 ||
1119 dir_item->dump->tapes->numfiles > 1)) {
1120 fast_lreply(501, _(" ERROR: Split dumps not supported"
1121 " with old version of amrecover."));
1125 opaque_ls_one(dir_item, marshall_feature, recursive);
1130 reply(200, _(" Opaque list of %s"), dir);
1137 DIR_ITEM * dir_item,
1138 am_feature_e marshall_feature,
1143 char *qtapelist_str;
1146 if (am_has_feature(their_features, marshall_feature)) {
1147 tapelist_str = marshal_tapelist(dir_item->dump->tapes, 1);
1149 tapelist_str = dir_item->dump->tapes->label;
1152 if (am_has_feature(their_features, fe_amindexd_quote_label)) {
1153 qtapelist_str = quote_string(tapelist_str);
1155 qtapelist_str = stralloc(tapelist_str);
1157 strncpy(date, dir_item->dump->date, 20);
1159 if(!am_has_feature(their_features,fe_amrecover_timestamp))
1162 qpath = quote_string(dir_item->path);
1163 if((!recursive && am_has_feature(their_features,
1164 fe_amindexd_fileno_in_OLSD)) ||
1165 (recursive && am_has_feature(their_features,
1166 fe_amindexd_fileno_in_ORLD))) {
1167 fast_lreply(201, " %s %d %s %lld %s",
1169 dir_item->dump->level,
1171 (long long)dir_item->dump->file,
1176 fast_lreply(201, " %s %d %s %s",
1177 date, dir_item->dump->level,
1178 qtapelist_str, qpath);
1181 if(am_has_feature(their_features, marshall_feature)) {
1182 amfree(tapelist_str);
1184 amfree(qtapelist_str);
1188 * returns the value of changer or tapedev from the amanda.conf file if set,
1189 * otherwise reports an error
1197 /* check state okay to do this */
1198 if (get_config_name() == NULL) {
1199 reply(501, _("Must set config before asking about tapedev."));
1203 /* use amrecover_changer if possible */
1204 if ((result = getconf_str(CNF_AMRECOVER_CHANGER)) != NULL &&
1206 dbprintf(_("tapedev_is amrecover_changer: %s\n"), result);
1207 reply(200, "%s", result);
1211 /* use changer if possible */
1212 if ((result = getconf_str(CNF_TPCHANGER)) != NULL && *result != '\0') {
1213 dbprintf(_("tapedev_is tpchanger: %s\n"), result);
1214 reply(200, "%s", result);
1218 /* get tapedev value */
1219 if ((result = getconf_str(CNF_TAPEDEV)) != NULL && *result != '\0') {
1220 dbprintf(_("tapedev_is tapedev: %s\n"), result);
1221 reply(200, "%s", result);
1225 dbprintf(_("No tapedev or tpchanger in config site.\n"));
1227 reply(501, _("Tapedev or tpchanger not set in config file."));
1232 /* returns YES if dumps for disk are compressed, NO if not */
1234 are_dumps_compressed(void)
1238 /* check state okay to do this */
1239 if (get_config_name() == NULL) {
1240 reply(501, _("Must set config,host,disk name before asking about dumps."));
1243 else if (dump_hostname == NULL) {
1244 reply(501, _("Must set host,disk name before asking about dumps."));
1247 else if (disk_name == NULL) {
1248 reply(501, _("Must set disk name before asking about dumps."));
1252 /* now go through the list of disks and find which have indexes */
1253 for (diskp = disk_list.head; diskp != NULL; diskp = diskp->next) {
1254 if ((strcasecmp(diskp->host->hostname, dump_hostname) == 0)
1255 && (strcmp(diskp->name, disk_name) == 0)) {
1260 if (diskp == NULL) {
1261 reply(501, _("Couldn't find host/disk in disk file."));
1265 /* send data to caller */
1266 if (diskp->compress == COMP_NONE)
1279 char *line = NULL, *part = NULL;
1282 char *cmd_undo, cmd_undo_ch;
1283 socklen_t_equiv socklen;
1284 sockaddr_union his_addr;
1288 int user_validated = 0;
1289 char *errstr = NULL;
1290 char *pgm = "amindexd"; /* in case argv[0] is not set */
1291 char his_hostname[MAX_HOSTNAME_LENGTH];
1292 char *cfg_opt = NULL;
1295 * Configure program for internationalization:
1296 * 1) Only set the message locale for now.
1297 * 2) Set textdomain for all amanda related programs to "amanda"
1298 * We don't want to be forced to support dozens of message catalogs.
1300 setlocale(LC_MESSAGES, "C");
1301 textdomain("amanda");
1303 safe_fd(DATA_FD_OFFSET, 2);
1304 openbsd_fd_inform();
1308 * When called via inetd, it is not uncommon to forget to put the
1309 * argv[0] value on the config line. On some systems (e.g. Solaris)
1310 * this causes argv and/or argv[0] to be NULL, so we have to be
1311 * careful getting our name.
1313 if (argc >= 1 && argv != NULL && argv[0] != NULL) {
1314 if((pgm = strrchr(argv[0], '/')) != NULL) {
1323 /* Don't die when child closes pipe */
1324 signal(SIGPIPE, SIG_IGN);
1326 dbopen(DBG_SUBDIR_SERVER);
1327 dbprintf(_("version %s\n"), VERSION);
1330 error("argv == NULL\n");
1333 if (! (argc >= 1 && argv[0] != NULL)) {
1334 dbprintf(_("WARNING: argv[0] not defined: check inetd.conf\n"));
1337 debug_dup_stderr_to_debug();
1344 if(argc > 0 && strcmp(*argv, "-t") == 0) {
1350 if(argc > 0 && strcmp(*argv, "amandad") == 0) {
1355 amandad_auth = *argv;
1371 if(gethostname(local_hostname, SIZEOF(local_hostname)-1) == -1) {
1372 error(_("gethostname: %s"), strerror(errno));
1375 local_hostname[SIZEOF(local_hostname)-1] = '\0';
1377 /* now trim domain off name */
1380 while(ch && ch != '.') ch = *s++;
1384 if(from_amandad == 0) {
1385 if(!amindexd_debug) {
1386 /* who are we talking to? */
1387 socklen = sizeof (his_addr);
1388 if (getpeername(0, (struct sockaddr *)&his_addr, &socklen) == -1)
1389 error(_("getpeername: %s"), strerror(errno));
1391 /* Try a reverse (IP->hostname) resolution, and fail if it does
1392 * not work -- this is a basic security check */
1393 if (getnameinfo((struct sockaddr *)&his_addr, SS_LEN(&his_addr),
1394 his_hostname, sizeof(his_hostname),
1397 error(_("getnameinfo(%s): hostname lookup failed"),
1398 str_sockaddr(&his_addr));
1403 /* Set up the input and output FILEs */
1408 /* read the REQ packet */
1409 for(; (line = agets(stdin)) != NULL; free(line)) {
1410 if(strncmp_const(line, "OPTIONS ") == 0) {
1411 if (g_options != NULL) {
1412 dbprintf(_("REQ packet specified multiple OPTIONS.\n"));
1413 free_g_options(g_options);
1415 g_options = parse_g_options(line+8, 1);
1416 if(!g_options->hostname) {
1417 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
1418 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
1419 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
1425 if(amandad_auth && g_options->auth) {
1426 if(strcasecmp(amandad_auth, g_options->auth) != 0) {
1427 g_printf(_("ERROR recover program ask for auth=%s while amindexd is configured for '%s'\n"),
1428 g_options->auth, amandad_auth);
1429 error(_("amindexd: ERROR recover program ask for auth=%s while amindexd is configured for '%s'"),
1430 g_options->auth, amandad_auth);
1434 /* send the REP packet */
1435 g_printf("CONNECT MESG %d\n", DATA_FD_OFFSET);
1442 cmdout = fdopen(DATA_FD_OFFSET + 0, "a");
1444 error(_("amindexd: Can't fdopen(%d): %s"), DATA_FD_OFFSET + 0, strerror(errno));
1448 cmdin = fdopen(DATA_FD_OFFSET + 1, "r");
1450 error(_("amindexd: Can't fdopen(%d): %s"), DATA_FD_OFFSET + 1, strerror(errno));
1455 /* clear these so we can detect when the have not been set by the client */
1456 amfree(dump_hostname);
1459 amfree(target_date);
1461 our_features = am_init_feature_set();
1462 their_features = am_set_default_feature_set();
1464 if (cfg_opt != NULL && check_and_load_config(cfg_opt) != -1) { /* load the config */
1468 reply(220, _("%s AMANDA index server (%s) ready."), local_hostname,
1471 user_validated = from_amandad;
1473 /* a real simple parser since there are only a few commands */
1476 /* get a line from the client */
1478 if((part = agets(cmdin)) == NULL) {
1480 dbprintf(_("? read error: %s\n"), strerror(errno));
1482 dbprintf(_("? unexpected EOF\n"));
1485 dbprintf(_("? unprocessed input:\n"));
1486 dbprintf("-----\n");
1487 dbprintf("? %s\n", line);
1488 dbprintf("-----\n");
1492 uncompress_remove = remove_files(uncompress_remove);
1494 return 1; /* they hung up? */
1496 strappend(line, part); /* Macro: line can be null */
1499 if(amindexd_debug) {
1500 break; /* we have a whole line */
1502 if((len = strlen(line)) > 0 && line[len-1] == '\r') {
1503 line[len-1] = '\0'; /* zap the '\r' */
1507 * Hmmm. We got a "line" from agets(), which means it saw
1508 * a '\n' (or EOF, etc), but there was not a '\r' before it.
1509 * Put a '\n' back in the buffer and loop for more.
1511 strappend(line, "\n");
1514 dbprintf("> %s\n", line);
1521 skip_whitespace(s, ch);
1523 reply(500, _("Command not recognised/incorrect: %s"), line);
1529 skip_non_whitespace(s, ch);
1530 cmd_undo = s-1; /* for error message */
1531 cmd_undo_ch = *cmd_undo;
1534 skip_whitespace(s, ch); /* find the argument */
1537 skip_quoted_string(s, ch);
1538 arg = unquote_string(arg);
1543 if (!user_validated && strcmp(cmd, "SECURITY") == 0 && arg) {
1544 user_validated = amindexd_debug ||
1546 (sockaddr_union *)&his_addr,
1548 if(user_validated) {
1549 reply(200, _("Access OK"));
1554 if (!user_validated) { /* don't tell client the reason, just log it to debug log */
1555 reply(500, _("Access not allowed"));
1557 dbprintf("%s\n", errstr);
1562 if (strcmp(cmd, "QUIT") == 0) {
1565 } else if (strcmp(cmd, "HOST") == 0 && arg) {
1567 /* set host we are restoring */
1569 if ((lhost = is_dump_host_valid(arg)) != NULL)
1571 dump_hostname = newstralloc(dump_hostname, lhost->hostname);
1572 reply(200, _("Dump host set to %s."), dump_hostname);
1573 amfree(qdisk_name); /* invalidate any value */
1574 amfree(disk_name); /* invalidate any value */
1577 } else if (strcmp(cmd, "LISTHOST") == 0) {
1583 if (get_config_name() == NULL) {
1584 reply(501, _("Must set config before listhost"));
1587 lreply(200, _(" List hosts for config %s"), get_config_name());
1588 for (disk = disk_list.head; disk!=NULL; disk = disk->next) {
1590 for (diskdup = disk_list.head; diskdup!=disk; diskdup = diskdup->next) {
1591 if(strcmp(diskdup->host->hostname, disk->host->hostname) == 0) {
1597 fast_lreply(201, " %s", disk->host->hostname);
1602 reply(200, _(" List hosts for config %s"), get_config_name());
1605 reply(200, _("No hosts for config %s"), get_config_name());
1609 } else if (strcmp(cmd, "DISK") == 0 && arg) {
1611 if (is_disk_valid(arg) != -1) {
1612 disk_name = newstralloc(disk_name, arg);
1613 qdisk_name = quote_string(disk_name);
1614 if (build_disk_table() != -1) {
1615 reply(200, _("Disk set to %s."), qdisk_name);
1619 } else if (strcmp(cmd, "DLE") == 0) {
1625 dp = lookup_disk(dump_hostname, disk_name);
1626 if (dp->line == 0) {
1627 reply(200, "NODLE");
1629 GPtrArray *errarray;
1632 b64disk = amxml_format_tag("disk", dp->name);
1633 dp->host->features = their_features;
1634 errarray = validate_optionstr(dp);
1635 if (errarray->len > 0) {
1636 for (i=0; i < errarray->len; i++) {
1637 g_debug(_("ERROR: %s:%s %s"),
1638 dump_hostname, disk_name,
1639 (char *)g_ptr_array_index(errarray, i));
1641 g_ptr_array_free(errarray, TRUE);
1642 reply(200, "NODLE");
1644 optionstr = xml_optionstr(dp, 0);
1645 l = vstralloc("<dle>\n",
1646 " <program>", dp->program, "</program>\n", NULL);
1647 if (dp->application) {
1648 application_t *application;
1651 application = lookup_application(dp->application);
1652 g_assert(application != NULL);
1653 xml_app = xml_application(dp, application,
1655 vstrextend(&l, xml_app, NULL);
1658 vstrextend(&l, " ", b64disk, "\n", NULL);
1660 char *b64device = amxml_format_tag("diskdevice",
1662 vstrextend(&l, " ", b64device, "\n", NULL);
1665 vstrextend(&l, optionstr, "</dle>\n", NULL);
1666 ql = quote_string(l);
1667 reply(200, "%s", ql);
1674 } else if (strcmp(cmd, "LISTDISK") == 0) {
1679 if (get_config_name() == NULL) {
1680 reply(501, _("Must set config, host before listdisk"));
1682 else if (dump_hostname == NULL) {
1683 reply(501, _("Must set host before listdisk"));
1686 lreply(200, _(" List of disk for device %s on host %s"), arg,
1688 for (disk = disk_list.head; disk!=NULL; disk = disk->next) {
1690 if (strcasecmp(disk->host->hostname, dump_hostname) == 0 &&
1691 ((disk->device && strcmp(disk->device, arg) == 0) ||
1692 (!disk->device && strcmp(disk->name, arg) == 0))) {
1693 qname = quote_string(disk->name);
1694 fast_lreply(201, " %s", qname);
1700 reply(200, _("List of disk for device %s on host %s"), arg,
1704 reply(200, _("No disk for device %s on host %s"), arg,
1709 lreply(200, _(" List of disk for host %s"), dump_hostname);
1710 for (disk = disk_list.head; disk!=NULL; disk = disk->next) {
1711 if(strcasecmp(disk->host->hostname, dump_hostname) == 0) {
1712 qname = quote_string(disk->name);
1713 fast_lreply(201, " %s", qname);
1719 reply(200, _("List of disk for host %s"), dump_hostname);
1722 reply(200, _("No disk for host %s"), dump_hostname);
1726 } else if (strcmp(cmd, "SCNF") == 0 && arg) {
1728 if (check_and_load_config(arg) != -1) { /* try to load the new config */
1729 amfree(dump_hostname); /* invalidate any value */
1730 amfree(qdisk_name); /* invalidate any value */
1731 amfree(disk_name); /* invalidate any value */
1732 reply(200, _("Config set to %s."), get_config_name());
1733 } /* check_and_load_config replies with any failure messages */
1735 } else if (strcmp(cmd, "FEATURES") == 0 && arg) {
1736 char *our_feature_string = NULL;
1737 char *their_feature_string = NULL;
1739 am_release_feature_set(our_features);
1740 am_release_feature_set(their_features);
1741 our_features = am_init_feature_set();
1742 our_feature_string = am_feature_to_string(our_features);
1743 their_feature_string = newstralloc(their_feature_string, arg);
1744 their_features = am_string_to_feature(their_feature_string);
1745 if (!their_features) {
1746 g_warning("Invalid client feature set '%s'", their_feature_string);
1747 their_features = am_set_default_feature_set();
1749 reply(200, "FEATURES %s", our_feature_string);
1750 amfree(our_feature_string);
1751 amfree(their_feature_string);
1753 } else if (strcmp(cmd, "DATE") == 0 && arg) {
1755 target_date = newstralloc(target_date, arg);
1756 reply(200, _("Working date set to %s."), target_date);
1758 } else if (strcmp(cmd, "DHST") == 0) {
1759 (void)disk_history_list();
1760 } else if (strcmp(cmd, "OISD") == 0 && arg) {
1761 if (is_dir_valid_opaque(arg) != -1) {
1762 reply(200, _("\"%s\" is a valid directory"), arg);
1764 } else if (strcmp(cmd, "OLSD") == 0 && arg) {
1765 (void)opaque_ls(arg,0);
1766 } else if (strcmp(cmd, "ORLD") == 0 && arg) {
1767 (void)opaque_ls(arg, 1);
1768 } else if (strcmp(cmd, "TAPE") == 0) {
1770 } else if (strcmp(cmd, "DCMP") == 0) {
1771 (void)are_dumps_compressed();
1773 *cmd_undo = cmd_undo_ch; /* restore the command line */
1774 reply(500, _("Command not recognised/incorrect: %s"), cmd);
1780 uncompress_remove = remove_files(uncompress_remove);
1781 free_find_result(&output_find);
1782 reply(200, _("Good bye."));
1791 static char nice[20];
1792 int year, month, day;
1793 int hours, minutes, seconds;
1794 char date[9], atime[7];
1795 int numdate, numtime;
1797 strncpy(date, datestamp, 8);
1799 numdate = atoi(date);
1800 year = numdate / 10000;
1801 month = (numdate / 100) % 100;
1802 day = numdate % 100;
1804 if(strlen(datestamp) <= 8) {
1805 g_snprintf(nice, SIZEOF(nice), "%4d-%02d-%02d",
1809 strncpy(atime, &(datestamp[8]), 6);
1811 numtime = atoi(atime);
1812 hours = numtime / 10000;
1813 minutes = (numtime / 100) % 100;
1814 seconds = numtime % 100;
1816 g_snprintf(nice, SIZEOF(nice), "%4d-%02d-%02d-%02d-%02d-%02d",
1817 year, month, day, hours, minutes, seconds);
1828 return strncmp(date1, date2, strlen(date2));
1833 char *dump_hostname,
1837 struct stat dir_stat;
1840 char *lower_hostname;
1842 lower_hostname = stralloc(dump_hostname);
1843 for(s=lower_hostname; *s != '\0'; s++)
1846 fn = getindexfname(dump_hostname, diskname, NULL, 0);
1847 if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
1848 amfree(lower_hostname);
1853 if (hostname != NULL) {
1854 fn = getindexfname(hostname, diskname, NULL, 0);
1855 if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
1856 amfree(lower_hostname);
1862 fn = getindexfname(lower_hostname, diskname, NULL, 0);
1863 if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
1864 amfree(lower_hostname);
1869 if(diskname != NULL) {
1870 fn = getoldindexfname(dump_hostname, diskname, NULL, 0);
1871 if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
1872 amfree(lower_hostname);
1877 if (hostname != NULL) {
1878 fn = getoldindexfname(hostname, diskname, NULL, 0);
1879 if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
1880 amfree(lower_hostname);
1886 fn = getoldindexfname(lower_hostname, diskname, NULL, 0);
1887 if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
1888 amfree(lower_hostname);
1894 amfree(lower_hostname);
1900 char *dump_hostname,
1906 struct stat dir_stat;
1909 char *lower_hostname;
1911 lower_hostname = stralloc(dump_hostname);
1912 for(s=lower_hostname; *s != '\0'; s++)
1915 fn = getindexfname(dump_hostname, diskname, timestamps, level);
1916 if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
1917 amfree(lower_hostname);
1921 if(hostname != NULL) {
1922 fn = getindexfname(hostname, diskname, timestamps, level);
1923 if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
1924 amfree(lower_hostname);
1929 fn = getindexfname(lower_hostname, diskname, timestamps, level);
1930 if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
1931 amfree(lower_hostname);
1935 if(diskname != NULL) {
1936 fn = getoldindexfname(dump_hostname, diskname, timestamps, level);
1937 if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
1938 amfree(lower_hostname);
1942 if(hostname != NULL) {
1943 fn = getoldindexfname(hostname, diskname, timestamps, level);
1944 if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
1945 amfree(lower_hostname);
1950 fn = getoldindexfname(lower_hostname, diskname, timestamps, level);
1951 if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
1952 amfree(lower_hostname);
1956 amfree(lower_hostname);