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"
58 #include "pipespawn.h"
59 #include "sockaddr-util.h"
63 #define amindexd_debug(i,x) do { \
64 if ((i) <= debug_amindexd) { \
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 REMOVE_ITEM *remove_files(REMOVE_ITEM *);
95 static char *uncompress_file(char *, char **);
96 static int process_ls_dump(char *, DUMP_ITEM *, int, char **);
98 static size_t reply_buffer_size = 1;
99 static char *reply_buffer = NULL;
100 static char *amandad_auth = NULL;
104 static void reply(int, char *, ...) G_GNUC_PRINTF(2, 3);
105 static void lreply(int, char *, ...) G_GNUC_PRINTF(2, 3);
106 static void fast_lreply(int, char *, ...) G_GNUC_PRINTF(2, 3);
107 static am_host_t *is_dump_host_valid(char *);
108 static int is_disk_valid(char *);
109 static int check_and_load_config(char *);
110 static int build_disk_table(void);
111 static int disk_history_list(void);
112 static int is_dir_valid_opaque(char *);
113 static int opaque_ls(char *, int);
114 static void opaque_ls_one (DIR_ITEM *dir_item, am_feature_e marshall_feature,
116 static int tapedev_is(void);
117 static int are_dumps_compressed(void);
118 static char *amindexd_nicedate (char *datestamp);
119 static int cmp_date (const char *date1, const char *date2);
120 static char *clean_backslash(char *line);
121 static char *get_index_name(char *dump_hostname, char *hostname,
122 char *diskname, char *timestamps, int level);
123 static int get_index_dir(char *dump_hostname, char *hostname, char *diskname);
125 int main(int, char **);
134 dbprintf(_("removing index file: %s\n"), remove->filename);
135 unlink(remove->filename);
136 amfree(remove->filename);
138 remove = remove->next;
150 char *filename = NULL;
151 struct stat stat_filename;
163 amwait_t wait_status;
166 filename = stralloc(filename_gz);
167 len = strlen(filename);
168 if(len > 3 && strcmp(&(filename[len-3]),".gz")==0) {
169 filename[len-3]='\0';
170 } else if(len > 2 && strcmp(&(filename[len-2]),".Z")==0) {
171 filename[len-2]='\0';
174 /* uncompress the file */
175 result=stat(filename,&stat_filename);
176 if(result==-1 && errno==ENOENT) { /* file does not exist */
178 REMOVE_ITEM *remove_file;
181 * Check that compressed file exists manually.
183 if (stat(filename_gz, &statbuf) < 0) {
184 *emsg = newvstrallocf(*emsg,
185 _("Compressed file '%s' is inaccessable: %s"),
186 filename_gz, strerror(errno));
187 dbprintf("%s\n",*emsg);
192 #ifdef UNCOMPRESS_OPT
193 # define PARAM_UNCOMPRESS_OPT UNCOMPRESS_OPT
195 # define PARAM_UNCOMPRESS_OPT skip_argument
198 nullfd = open("/dev/null", O_RDONLY);
200 indexfd = open(filename,O_WRONLY|O_CREAT, 0600);
202 *emsg = newvstrallocf(*emsg, _("Can't open '%s' for writting: %s"),
203 filename, strerror(errno));
204 dbprintf("%s\n",*emsg);
209 /* just use our stderr directly for the pipe's stderr; in
210 * main() we send stderr to the debug file, or /dev/null
211 * if debugging is disabled */
212 debugfd = STDERR_FILENO;
214 /* start the uncompress process */
215 putenv(stralloc("LC_ALL=C"));
216 pid_gzip = pipespawn(UNCOMPRESS_PATH, STDOUT_PIPE,
217 &nullfd, &pipe_from_gzip, &debugfd,
218 UNCOMPRESS_PATH, PARAM_UNCOMPRESS_OPT,
222 pipe_stream = fdopen(pipe_from_gzip,"r");
223 if(pipe_stream == NULL) {
224 *emsg = newvstrallocf(*emsg, _("Can't fdopen pipe from gzip: %s"),
226 dbprintf("%s\n",*emsg);
231 /* start the sort process */
232 putenv(stralloc("LC_ALL=C"));
233 pid_sort = pipespawn(SORT_PATH, STDIN_PIPE,
234 &pipe_to_sort, &indexfd, &debugfd,
238 /* send all ouput from uncompress process to sort process */
239 /* clean the data with clean_backslash */
240 while (fgets(line, STR_SIZE, pipe_stream) != NULL) {
241 if (line[0] != '\0') {
242 if (index(line,'/')) {
243 clean_backslash(line);
244 fullwrite(pipe_to_sort,line,strlen(line));
250 aclose(pipe_to_sort);
251 if (waitpid(pid_gzip, &wait_status, 0) < 0) {
252 if (!WIFEXITED(wait_status)) {
253 dbprintf(_("Uncompress exited with signal %d"),
254 WTERMSIG(wait_status));
255 } else if (WEXITSTATUS(wait_status) != 0) {
256 dbprintf(_("Uncompress exited with status %d"),
257 WEXITSTATUS(wait_status));
259 dbprintf(_("Uncompres returned negative value: %s"),
263 if (waitpid(pid_sort, &wait_status, 0) < 0) {
264 if (!WIFEXITED(wait_status)) {
265 dbprintf(_("Sort exited with signal %d"),
266 WTERMSIG(wait_status));
267 } else if (WEXITSTATUS(wait_status) != 0) {
268 dbprintf(_("Sort exited with status %d"),
269 WEXITSTATUS(wait_status));
271 dbprintf(_("Sort returned negative value: %s"),
276 /* add at beginning */
277 remove_file = (REMOVE_ITEM *)alloc(SIZEOF(REMOVE_ITEM));
278 remove_file->filename = stralloc(filename);
279 remove_file->next = uncompress_remove;
280 uncompress_remove = remove_file;
281 } else if(!S_ISREG((stat_filename.st_mode))) {
283 *emsg = vstrallocf(_("\"%s\" is not a regular file"), filename);
293 /* find all matching entries in a dump listing */
294 /* return -1 if error */
298 DUMP_ITEM * dump_item,
303 char *old_line = NULL;
304 char *filename = NULL;
306 char *dir_slash = NULL;
310 size_t len_dir_slash;
312 if (strcmp(dir, "/") == 0) {
313 dir_slash = stralloc(dir);
315 dir_slash = stralloc2(dir, "/");
318 filename_gz = get_index_name(dump_hostname, dump_item->hostname, disk_name,
319 dump_item->date, dump_item->level);
320 if (filename_gz == NULL) {
321 *emsg = stralloc(_("index file not found"));
325 if((filename = uncompress_file(filename_gz, emsg)) == NULL) {
332 if((fp = fopen(filename,"r"))==0) {
334 *emsg = vstrallocf("%s", strerror(errno));
339 len_dir_slash=strlen(dir_slash);
341 while (fgets(line, STR_SIZE, fp) != NULL) {
342 if (line[0] != '\0') {
343 if(line[strlen(line)-1] == '\n')
344 line[strlen(line)-1] = '\0';
345 if(strncmp(dir_slash, line, len_dir_slash) == 0) {
347 s = line + len_dir_slash;
349 while(ch && ch != '/')
350 ch = *s++;/* find end of the file name */
356 if(old_line == NULL || strcmp(line, old_line) != 0) {
357 add_dir_list_item(dump_item, line);
359 old_line = stralloc(line);
365 /*@i@*/ amfree(old_line);
371 /* send a 1 line reply to the client */
372 printf_arglist_function1(static void reply, int, n, char *, fmt)
378 reply_buffer = alloc(reply_buffer_size);
381 arglist_start(args, fmt);
382 len = g_vsnprintf(reply_buffer, reply_buffer_size, fmt, args);
385 if (len > -1 && (size_t)len < reply_buffer_size-1)
388 reply_buffer_size *= 2;
389 amfree(reply_buffer);
390 reply_buffer = alloc(reply_buffer_size);
393 if (g_fprintf(cmdout,"%03d %s\r\n", n, reply_buffer) < 0)
395 dbprintf(_("! error %d (%s) in printf\n"), errno, strerror(errno));
396 uncompress_remove = remove_files(uncompress_remove);
399 if (fflush(cmdout) != 0)
401 dbprintf(_("! error %d (%s) in fflush\n"), errno, strerror(errno));
402 uncompress_remove = remove_files(uncompress_remove);
405 dbprintf(_("< %03d %s\n"), n, reply_buffer);
408 /* send one line of a multi-line response */
409 printf_arglist_function1(static void lreply, int, n, char *, fmt)
415 reply_buffer = alloc(reply_buffer_size);
418 arglist_start(args, fmt);
419 len = g_vsnprintf(reply_buffer, reply_buffer_size, fmt, args);
422 if (len > -1 && (size_t)len < reply_buffer_size-1)
425 reply_buffer_size *= 2;
426 amfree(reply_buffer);
427 reply_buffer = alloc(reply_buffer_size);
430 if (g_fprintf(cmdout,"%03d-%s\r\n", n, reply_buffer) < 0)
432 dbprintf(_("! error %d (%s) in printf\n"),
433 errno, strerror(errno));
434 uncompress_remove = remove_files(uncompress_remove);
437 if (fflush(cmdout) != 0)
439 dbprintf(_("! error %d (%s) in fflush\n"),
440 errno, strerror(errno));
441 uncompress_remove = remove_files(uncompress_remove);
445 dbprintf("< %03d-%s\n", n, reply_buffer);
449 /* send one line of a multi-line response */
450 printf_arglist_function1(static void fast_lreply, int, n, char *, fmt)
456 reply_buffer = alloc(reply_buffer_size);
459 arglist_start(args, fmt);
460 len = g_vsnprintf(reply_buffer, reply_buffer_size, fmt, args);
463 if (len > -1 && (size_t)len < reply_buffer_size-1)
466 reply_buffer_size *= 2;
467 amfree(reply_buffer);
468 reply_buffer = alloc(reply_buffer_size);
471 if (g_fprintf(cmdout,"%03d-%s\r\n", n, reply_buffer) < 0)
473 dbprintf(_("! error %d (%s) in printf\n"),
474 errno, strerror(errno));
475 uncompress_remove = remove_files(uncompress_remove);
479 dbprintf("< %03d-%s\n", n, reply_buffer);
482 /* see if hostname is valid */
483 /* valid is defined to be that there is an index directory for it */
484 /* also do a security check on the requested dump hostname */
485 /* to restrict access to index records if required */
486 /* return -1 if not okay */
494 if (config_name == NULL) {
495 reply(501, _("Must set config before setting host."));
499 /* check that the config actually handles that host */
500 ihost = lookup_host(host);
502 reply(501, _("Host %s is not in your disklist."), host);
506 /* check if an index dir exist */
507 if(get_index_dir(host, ihost->hostname, NULL)) {
511 /* check if an index dir exist for at least one DLE */
512 for(diskp = ihost->disks; diskp != NULL; diskp = diskp->hostnext) {
513 if (get_index_dir(diskp->hostname, NULL, NULL)) {
518 reply(501, _("No index records for host: %s. Have you enabled indexing?"),
531 if (config_name == NULL) {
532 reply(501, _("Must set config,host before setting disk."));
535 else if (dump_hostname == NULL) {
536 reply(501, _("Must set host before setting disk."));
540 /* check that the config actually handles that disk */
541 idisk = lookup_disk(dump_hostname, disk);
543 qdisk = quote_string(disk);
544 reply(501, _("Disk %s:%s is not in your disklist."), dump_hostname, qdisk);
549 /* assume an index dir already */
550 if (get_index_dir(dump_hostname, idisk->hostname, disk) == 0) {
551 qdisk = quote_string(disk);
552 reply(501, _("No index records for disk: %s. Invalid?"), qdisk);
562 check_and_load_config(
568 struct stat dir_stat;
570 /* check that the config actually exists */
571 if (config == NULL) {
572 reply(501, _("Must set config first."));
576 /* (re-)initialize configuration with the new config name */
577 if (!config_init(CONFIG_INIT_EXPLICIT_NAME, config)) {
578 reply(501, _("Could not read config file for %s!"), config);
582 check_running_as(RUNNING_AS_DUMPUSER_PREFERRED);
584 conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
585 if (read_diskfile(conf_diskfile, &disk_list) < 0) {
586 reply(501, _("Could not read disk file %s!"), conf_diskfile);
587 amfree(conf_diskfile);
590 amfree(conf_diskfile);
592 conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
593 if(read_tapelist(conf_tapelist)) {
594 reply(501, _("Could not read tapelist file %s!"), conf_tapelist);
595 amfree(conf_tapelist);
598 amfree(conf_tapelist);
600 dbrename(config_name, DBG_SUBDIR_SERVER);
602 output_find = find_dump(&disk_list);
603 sort_find_result("DLKHpB", &output_find);
605 conf_indexdir = config_dir_relative(getconf_str(CNF_INDEXDIR));
606 if (stat (conf_indexdir, &dir_stat) != 0 || !S_ISDIR(dir_stat.st_mode)) {
607 reply(501, _("Index directory %s does not exist"), conf_indexdir);
608 amfree(conf_indexdir);
611 amfree(conf_indexdir);
618 build_disk_table(void)
621 char *last_timestamp;
625 find_result_t *find_output;
627 if (config_name == NULL) {
628 reply(590, _("Must set config,host,disk before building disk table"));
631 else if (dump_hostname == NULL) {
632 reply(590, _("Must set host,disk before building disk table"));
635 else if (disk_name == NULL) {
636 reply(590, _("Must set disk before building disk table"));
641 last_timestamp = NULL;
642 last_filenum = (off_t)-1;
645 for(find_output = output_find;
647 find_output = find_output->next) {
648 if(strcasecmp(dump_hostname, find_output->hostname) == 0 &&
649 strcmp(disk_name , find_output->diskname) == 0 &&
650 strcmp("OK" , find_output->status) == 0) {
653 if (strcmp("1/1", find_output->partnum) == 0) {
655 } else if (strcmp("1/-1", find_output->partnum) == 0) {
656 if (find_output->next &&
657 strcmp(dump_hostname, find_output->next->hostname) == 0 &&
658 strcmp(disk_name, find_output->next->diskname) == 0 &&
659 strcmp(find_output->timestamp,
660 find_output->next->timestamp) == 0 &&
661 strcmp("OK", find_output->next->status) == 0 &&
662 strcmp("2/-1", find_output->next->partnum) == 0) {
668 } else if (strcmp("--", find_output->partnum)) {
670 partnum = atoi(find_output->partnum);
671 c = strchr(find_output->partnum,'/');
678 * The sort order puts holding disk entries first. We want to
679 * use them if at all possible, so ignore any other entries
680 * for the same datestamp after we see a holding disk entry
681 * (as indicated by a filenum of zero).
684 strcmp(find_output->timestamp, last_timestamp) == 0 &&
685 find_output->level == last_level &&
686 partnum == last_partnum && last_filenum == 0) {
689 /* ignore duplicate partnum */
691 strcmp(find_output->timestamp, last_timestamp) == 0 &&
692 find_output->level == last_level &&
693 partnum == last_partnum) {
696 last_timestamp = find_output->timestamp;
697 last_filenum = find_output->filenum;
698 last_level = find_output->level;
699 last_partnum = partnum;
700 date = amindexd_nicedate(find_output->timestamp);
701 add_dump(find_output->hostname, date, find_output->level,
702 find_output->label, find_output->filenum, partnum,
704 dbprintf("- %s %d %s %lld %d %d\n",
705 date, find_output->level,
707 (long long)find_output->filenum,
719 disk_history_list(void)
724 if (config_name == NULL) {
725 reply(502, _("Must set config,host,disk before listing history"));
728 else if (dump_hostname == NULL) {
729 reply(502, _("Must set host,disk before listing history"));
732 else if (disk_name == NULL) {
733 reply(502, _("Must set disk before listing history"));
737 lreply(200, _(" Dump history for config \"%s\" host \"%s\" disk %s"),
738 config_name, dump_hostname, qdisk_name);
740 for (item=first_dump(); item!=NULL; item=next_dump(item)){
741 char *tapelist_str = marshal_tapelist(item->tapes, 1);
743 strncpy(date, item->date, 20);
745 if(!am_has_feature(their_features,fe_amrecover_timestamp))
748 if(am_has_feature(their_features, fe_amindexd_marshall_in_DHST)){
749 lreply(201, " %s %d %s", date, item->level, tapelist_str);
752 lreply(201, " %s %d %s %lld", date, item->level,
753 tapelist_str, (long long)item->file);
755 amfree(tapelist_str);
758 reply(200, _("Dump history for config \"%s\" host \"%s\" disk %s"),
759 config_name, dump_hostname, qdisk_name);
766 * is the directory dir backed up - dir assumed complete relative to
769 /* opaque version of command */
779 char *filename_gz = NULL;
780 char *filename = NULL;
782 static char *emsg = NULL;
784 if (config_name == NULL || dump_hostname == NULL || disk_name == NULL) {
785 reply(502, _("Must set config,host,disk before asking about directories"));
788 else if (dump_hostname == NULL) {
789 reply(502, _("Must set host,disk before asking about directories"));
792 else if (disk_name == NULL) {
793 reply(502, _("Must set disk before asking about directories"));
796 else if (target_date == NULL) {
797 reply(502, _("Must set date before asking about directories"));
800 /* scan through till we find first dump on or before date */
801 for (item=first_dump(); item!=NULL; item=next_dump(item))
802 if (cmp_date(item->date, target_date) <= 0)
807 /* no dump for given date */
808 reply(500, _("No dumps available on or before date \"%s\""), target_date);
812 if(strcmp(dir, "/") == 0) {
813 ldir = stralloc(dir);
815 ldir = stralloc2(dir, "/");
817 ldir_len = strlen(ldir);
819 /* go back till we hit a level 0 dump */
823 filename_gz = get_index_name(dump_hostname, item->hostname, disk_name,
824 item->date, item->level);
825 if (filename_gz == NULL) {
826 reply(599, "index not found");
830 if((filename = uncompress_file(filename_gz, &emsg)) == NULL) {
831 reply(599, _("System error %s"), emsg);
838 dbprintf("f %s\n", filename);
839 if ((fp = fopen(filename, "r")) == NULL) {
840 reply(599, _("System error %s"), strerror(errno));
845 while (fgets(line, STR_SIZE, fp) != NULL) {
848 if(line[strlen(line)-1] == '\n')
849 line[strlen(line)-1] = '\0';
850 if (strncmp(line, ldir, ldir_len) != 0) {
851 continue; /* not found yet */
860 last_level = item->level;
863 item=next_dump(item);
864 } while ((item != NULL) && (item->level >= last_level));
865 } while (item != NULL);
869 reply(500, _("\"%s\" is an invalid directory"), dir);
878 DUMP_ITEM *dump_item;
880 int level, last_level;
881 static char *emsg = NULL;
882 am_feature_e marshall_feature;
885 marshall_feature = fe_amindexd_marshall_in_ORLD;
887 marshall_feature = fe_amindexd_marshall_in_OLSD;
892 if (config_name == NULL) {
893 reply(502, _("Must set config,host,disk before listing a directory"));
896 else if (dump_hostname == NULL) {
897 reply(502, _("Must set host,disk before listing a directory"));
900 else if (disk_name == NULL) {
901 reply(502, _("Must set disk before listing a directory"));
904 else if (target_date == NULL) {
905 reply(502, _("Must set date before listing a directory"));
909 /* scan through till we find first dump on or before date */
910 for (dump_item=first_dump(); dump_item!=NULL; dump_item=next_dump(dump_item))
911 if (cmp_date(dump_item->date, target_date) <= 0)
914 if (dump_item == NULL)
916 /* no dump for given date */
917 reply(500, _("No dumps available on or before date \"%s\""), target_date);
921 /* get data from that dump */
922 if (process_ls_dump(dir, dump_item, recursive, &emsg) == -1) {
923 reply(599, _("System error %s"), emsg);
928 /* go back processing higher level dumps till we hit a level 0 dump */
929 last_level = dump_item->level;
930 while ((last_level != 0) && ((dump_item=next_dump(dump_item)) != NULL))
932 if (dump_item->level < last_level)
934 last_level = dump_item->level;
935 if (process_ls_dump(dir, dump_item, recursive, &emsg) == -1) {
936 reply(599, _("System error %s"), emsg);
943 /* return the information to the caller */
944 lreply(200, _(" Opaque list of %s"), dir);
945 for(level=0; level<=9; level++) {
946 for (dir_item = get_dir_list(); dir_item != NULL;
947 dir_item = dir_item->next) {
949 if(dir_item->dump->level == level) {
950 if (!am_has_feature(their_features, marshall_feature) &&
951 (num_entries(dir_item->dump->tapes) > 1 ||
952 dir_item->dump->tapes->numfiles > 1)) {
953 fast_lreply(501, _(" ERROR: Split dumps not supported"
954 " with old version of amrecover."));
958 opaque_ls_one(dir_item, marshall_feature, recursive);
963 reply(200, _(" Opaque list of %s"), dir);
971 am_feature_e marshall_feature,
978 if (am_has_feature(their_features, marshall_feature)) {
979 tapelist_str = marshal_tapelist(dir_item->dump->tapes, 1);
981 tapelist_str = dir_item->dump->tapes->label;
984 strncpy(date, dir_item->dump->date, 20);
986 if(!am_has_feature(their_features,fe_amrecover_timestamp))
989 qpath = quote_string(dir_item->path);
990 if((!recursive && am_has_feature(their_features,
991 fe_amindexd_fileno_in_OLSD)) ||
992 (recursive && am_has_feature(their_features,
993 fe_amindexd_fileno_in_ORLD))) {
994 fast_lreply(201, " %s %d %s %lld %s",
996 dir_item->dump->level,
998 (long long)dir_item->dump->file,
1003 fast_lreply(201, " %s %d %s %s",
1004 date, dir_item->dump->level,
1005 tapelist_str, qpath);
1008 if(am_has_feature(their_features, marshall_feature)) {
1009 amfree(tapelist_str);
1014 * returns the value of changer or tapedev from the amanda.conf file if set,
1015 * otherwise reports an error
1023 /* check state okay to do this */
1024 if (config_name == NULL) {
1025 reply(501, _("Must set config before asking about tapedev."));
1029 /* use amrecover_changer if possible */
1030 if ((result = getconf_str(CNF_AMRECOVER_CHANGER)) != NULL &&
1032 dbprintf(_("tapedev_is amrecover_changer: %s\n"), result);
1037 /* use changer if possible */
1038 if ((result = getconf_str(CNF_TPCHANGER)) != NULL && *result != '\0') {
1039 dbprintf(_("tapedev_is tpchanger: %s\n"), result);
1044 /* get tapedev value */
1045 if ((result = getconf_str(CNF_TAPEDEV)) != NULL && *result != '\0') {
1046 dbprintf(_("tapedev_is tapedev: %s\n"), result);
1051 dbprintf(_("No tapedev or tpchanger in config site.\n"));
1053 reply(501, _("Tapedev or tpchanger not set in config file."));
1058 /* returns YES if dumps for disk are compressed, NO if not */
1060 are_dumps_compressed(void)
1064 /* check state okay to do this */
1065 if (config_name == NULL) {
1066 reply(501, _("Must set config,host,disk name before asking about dumps."));
1069 else if (dump_hostname == NULL) {
1070 reply(501, _("Must set host,disk name before asking about dumps."));
1073 else if (disk_name == NULL) {
1074 reply(501, _("Must set disk name before asking about dumps."));
1078 /* now go through the list of disks and find which have indexes */
1079 for (diskp = disk_list.head; diskp != NULL; diskp = diskp->next) {
1080 if ((strcasecmp(diskp->host->hostname, dump_hostname) == 0)
1081 && (strcmp(diskp->name, disk_name) == 0)) {
1086 if (diskp == NULL) {
1087 reply(501, _("Couldn't find host/disk in disk file."));
1091 /* send data to caller */
1092 if (diskp->compress == COMP_NONE)
1105 char *line = NULL, *part = NULL;
1108 char *cmd_undo, cmd_undo_ch;
1110 struct sockaddr_storage his_addr;
1114 int user_validated = 0;
1115 char *errstr = NULL;
1116 char *pgm = "amindexd"; /* in case argv[0] is not set */
1117 char his_hostname[MAX_HOSTNAME_LENGTH];
1118 char *cfg_opt = NULL;
1121 * Configure program for internationalization:
1122 * 1) Only set the message locale for now.
1123 * 2) Set textdomain for all amanda related programs to "amanda"
1124 * We don't want to be forced to support dozens of message catalogs.
1126 setlocale(LC_MESSAGES, "C");
1127 textdomain("amanda");
1129 safe_fd(DATA_FD_OFFSET, 2);
1133 * When called via inetd, it is not uncommon to forget to put the
1134 * argv[0] value on the config line. On some systems (e.g. Solaris)
1135 * this causes argv and/or argv[0] to be NULL, so we have to be
1136 * careful getting our name.
1138 if (argc >= 1 && argv != NULL && argv[0] != NULL) {
1139 if((pgm = strrchr(argv[0], '/')) != NULL) {
1148 /* Don't die when child closes pipe */
1149 signal(SIGPIPE, SIG_IGN);
1151 dbopen(DBG_SUBDIR_SERVER);
1152 dbprintf(_("version %s\n"), version());
1155 error("argv == NULL\n");
1158 if (! (argc >= 1 && argv[0] != NULL)) {
1159 dbprintf(_("WARNING: argv[0] not defined: check inetd.conf\n"));
1162 debug_dup_stderr_to_debug();
1169 if(argc > 0 && strcmp(*argv, "-t") == 0) {
1175 if(argc > 0 && strcmp(*argv, "amandad") == 0) {
1180 amandad_auth = *argv;
1196 if(gethostname(local_hostname, SIZEOF(local_hostname)-1) == -1) {
1197 error(_("gethostname: %s"), strerror(errno));
1200 local_hostname[SIZEOF(local_hostname)-1] = '\0';
1202 /* now trim domain off name */
1205 while(ch && ch != '.') ch = *s++;
1209 if(from_amandad == 0) {
1210 if(!amindexd_debug) {
1211 /* who are we talking to? */
1212 socklen = sizeof (his_addr);
1213 if (getpeername(0, (struct sockaddr *)&his_addr, &socklen) == -1)
1214 error(_("getpeername: %s"), strerror(errno));
1216 /* Try a reverse (IP->hostname) resolution, and fail if it does
1217 * not work -- this is a basic security check */
1218 if (getnameinfo((struct sockaddr *)&his_addr, SS_LEN(&his_addr),
1219 his_hostname, sizeof(his_hostname),
1222 error(_("getnameinfo(%s): hostname lookup failed"),
1223 str_sockaddr(&his_addr));
1228 /* Set up the input and output FILEs */
1233 /* read the REQ packet */
1234 for(; (line = agets(stdin)) != NULL; free(line)) {
1235 if(strncmp_const(line, "OPTIONS ") == 0) {
1236 g_options = parse_g_options(line+8, 1);
1237 if(!g_options->hostname) {
1238 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
1239 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
1240 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
1246 if(amandad_auth && g_options->auth) {
1247 if(strcasecmp(amandad_auth, g_options->auth) != 0) {
1248 g_printf(_("ERROR recover program ask for auth=%s while amindexd is configured for '%s'\n"),
1249 g_options->auth, amandad_auth);
1250 error(_("amindexd: ERROR recover program ask for auth=%s while amindexd is configured for '%s'"),
1251 g_options->auth, amandad_auth);
1255 /* send the REP packet */
1256 g_printf("CONNECT MESG %d\n", DATA_FD_OFFSET);
1263 cmdout = fdopen(DATA_FD_OFFSET + 0, "a");
1265 error(_("amindexd: Can't fdopen(%d): %s"), DATA_FD_OFFSET + 0, strerror(errno));
1269 cmdin = fdopen(DATA_FD_OFFSET + 1, "r");
1271 error(_("amindexd: Can't fdopen(%d): %s"), DATA_FD_OFFSET + 1, strerror(errno));
1276 /* clear these so we can detect when the have not been set by the client */
1277 amfree(dump_hostname);
1280 amfree(target_date);
1282 our_features = am_init_feature_set();
1283 their_features = am_set_default_feature_set();
1285 if (cfg_opt != NULL && check_and_load_config(cfg_opt) != -1) { /* load the config */
1289 reply(220, _("%s AMANDA index server (%s) ready."), local_hostname,
1292 user_validated = from_amandad;
1294 /* a real simple parser since there are only a few commands */
1297 /* get a line from the client */
1299 if((part = agets(cmdin)) == NULL) {
1301 dbprintf(_("? read error: %s\n"), strerror(errno));
1303 dbprintf(_("? unexpected EOF\n"));
1306 dbprintf(_("? unprocessed input:\n"));
1307 dbprintf("-----\n");
1308 dbprintf("? %s\n", line);
1309 dbprintf("-----\n");
1313 uncompress_remove = remove_files(uncompress_remove);
1315 return 1; /* they hung up? */
1317 strappend(line, part); /* Macro: line can be null */
1320 if(amindexd_debug) {
1321 break; /* we have a whole line */
1323 if((len = strlen(line)) > 0 && line[len-1] == '\r') {
1324 line[len-1] = '\0'; /* zap the '\r' */
1328 * Hmmm. We got a "line" from agets(), which means it saw
1329 * a '\n' (or EOF, etc), but there was not a '\r' before it.
1330 * Put a '\n' back in the buffer and loop for more.
1332 strappend(line, "\n");
1335 dbprintf("> %s\n", line);
1342 skip_whitespace(s, ch);
1344 reply(500, _("Command not recognised/incorrect: %s"), line);
1350 skip_non_whitespace(s, ch);
1351 cmd_undo = s-1; /* for error message */
1352 cmd_undo_ch = *cmd_undo;
1355 skip_whitespace(s, ch); /* find the argument */
1358 skip_quoted_string(s, ch);
1359 arg = unquote_string(arg);
1364 if (!user_validated && strcmp(cmd, "SECURITY") == 0 && arg) {
1365 user_validated = amindexd_debug ||
1367 (struct sockaddr_storage *)&his_addr,
1369 if(user_validated) {
1370 reply(200, _("Access OK"));
1375 if (!user_validated) { /* don't tell client the reason, just log it to debug log */
1376 reply(500, _("Access not allowed"));
1378 dbprintf("%s\n", errstr);
1383 if (strcmp(cmd, "QUIT") == 0) {
1386 } else if (strcmp(cmd, "HOST") == 0 && arg) {
1388 /* set host we are restoring */
1390 if ((lhost = is_dump_host_valid(arg)) != NULL)
1392 dump_hostname = newstralloc(dump_hostname, lhost->hostname);
1393 reply(200, _("Dump host set to %s."), dump_hostname);
1394 amfree(qdisk_name); /* invalidate any value */
1395 amfree(disk_name); /* invalidate any value */
1398 } else if (strcmp(cmd, "LISTHOST") == 0) {
1404 if (config_name == NULL) {
1405 reply(501, _("Must set config before listhost"));
1408 lreply(200, _(" List hosts for config %s"), config_name);
1409 for (disk = disk_list.head; disk!=NULL; disk = disk->next) {
1411 for (diskdup = disk_list.head; diskdup!=disk; diskdup = diskdup->next) {
1412 if(strcmp(diskdup->host->hostname, disk->host->hostname) == 0) {
1418 fast_lreply(201, " %s", disk->host->hostname);
1423 reply(200, _(" List hosts for config %s"), config_name);
1426 reply(200, _("No hosts for config %s"), config_name);
1430 } else if (strcmp(cmd, "DISK") == 0 && arg) {
1432 if (is_disk_valid(arg) != -1) {
1433 disk_name = newstralloc(disk_name, arg);
1434 qdisk_name = quote_string(disk_name);
1435 if (build_disk_table() != -1) {
1436 reply(200, _("Disk set to %s."), qdisk_name);
1440 } else if (strcmp(cmd, "LISTDISK") == 0) {
1445 if (config_name == NULL) {
1446 reply(501, _("Must set config, host before listdisk"));
1448 else if (dump_hostname == NULL) {
1449 reply(501, _("Must set host before listdisk"));
1452 lreply(200, _(" List of disk for device %s on host %s"), arg,
1454 for (disk = disk_list.head; disk!=NULL; disk = disk->next) {
1456 if (strcasecmp(disk->host->hostname, dump_hostname) == 0 &&
1457 ((disk->device && strcmp(disk->device, arg) == 0) ||
1458 (!disk->device && strcmp(disk->name, arg) == 0))) {
1459 qname = quote_string(disk->name);
1460 fast_lreply(201, " %s", qname);
1466 reply(200, _("List of disk for device %s on host %s"), arg,
1470 reply(200, _("No disk for device %s on host %s"), arg,
1475 lreply(200, _(" List of disk for host %s"), dump_hostname);
1476 for (disk = disk_list.head; disk!=NULL; disk = disk->next) {
1477 if(strcasecmp(disk->host->hostname, dump_hostname) == 0) {
1478 qname = quote_string(disk->name);
1479 fast_lreply(201, " %s", qname);
1485 reply(200, _("List of disk for host %s"), dump_hostname);
1488 reply(200, _("No disk for host %s"), dump_hostname);
1492 } else if (strcmp(cmd, "SCNF") == 0 && arg) {
1494 if (check_and_load_config(arg) != -1) { /* try to load the new config */
1495 amfree(dump_hostname); /* invalidate any value */
1496 amfree(qdisk_name); /* invalidate any value */
1497 amfree(disk_name); /* invalidate any value */
1498 reply(200, _("Config set to %s."), config_name);
1499 } /* check_and_load_config replies with any failure messages */
1501 } else if (strcmp(cmd, "FEATURES") == 0 && arg) {
1502 char *our_feature_string = NULL;
1503 char *their_feature_string = NULL;
1505 am_release_feature_set(our_features);
1506 am_release_feature_set(their_features);
1507 our_features = am_init_feature_set();
1508 our_feature_string = am_feature_to_string(our_features);
1509 their_feature_string = newstralloc(their_feature_string, arg);
1510 their_features = am_string_to_feature(their_feature_string);
1511 reply(200, "FEATURES %s", our_feature_string);
1512 amfree(our_feature_string);
1513 amfree(their_feature_string);
1515 } else if (strcmp(cmd, "DATE") == 0 && arg) {
1517 target_date = newstralloc(target_date, arg);
1518 reply(200, _("Working date set to %s."), target_date);
1520 } else if (strcmp(cmd, "DHST") == 0) {
1521 (void)disk_history_list();
1522 } else if (strcmp(cmd, "OISD") == 0 && arg) {
1523 if (is_dir_valid_opaque(arg) != -1) {
1524 reply(200, _("\"%s\" is a valid directory"), arg);
1526 } else if (strcmp(cmd, "OLSD") == 0 && arg) {
1527 (void)opaque_ls(arg,0);
1528 } else if (strcmp(cmd, "ORLD") == 0 && arg) {
1529 (void)opaque_ls(arg, 1);
1530 } else if (strcmp(cmd, "TAPE") == 0) {
1532 } else if (strcmp(cmd, "DCMP") == 0) {
1533 (void)are_dumps_compressed();
1535 *cmd_undo = cmd_undo_ch; /* restore the command line */
1536 reply(500, _("Command not recognised/incorrect: %s"), cmd);
1542 uncompress_remove = remove_files(uncompress_remove);
1543 free_find_result(&output_find);
1544 reply(200, _("Good bye."));
1553 static char nice[20];
1554 int year, month, day;
1555 int hours, minutes, seconds;
1556 char date[9], atime[7];
1557 int numdate, numtime;
1559 strncpy(date, datestamp, 8);
1561 numdate = atoi(date);
1562 year = numdate / 10000;
1563 month = (numdate / 100) % 100;
1564 day = numdate % 100;
1566 if(strlen(datestamp) <= 8) {
1567 g_snprintf(nice, SIZEOF(nice), "%4d-%02d-%02d",
1571 strncpy(atime, &(datestamp[8]), 6);
1573 numtime = atoi(atime);
1574 hours = numtime / 10000;
1575 minutes = (numtime / 100) % 100;
1576 seconds = numtime % 100;
1578 g_snprintf(nice, SIZEOF(nice), "%4d-%02d-%02d-%02d-%02d-%02d",
1579 year, month, day, hours, minutes, seconds);
1590 return strncmp(date1, date2, strlen(date2));
1597 char *s = line, *s1, *s2;
1606 if (*s != '\0' && isdigit((int)*s) &&
1607 *s1 != '\0' && isdigit((int)*s1) &&
1608 *s2 != '\0' && isdigit((int)*s2)) {
1609 /* this is \000, an octal value */
1610 i = ((*s)-'0')*64 + ((*s1)-'0')*8 + ((*s2)-'0');
1613 } else if (*s == '\\') { /* we remove one / */
1615 } else { /* we keep the / */
1631 char *dump_hostname,
1635 struct stat dir_stat;
1638 char *lower_hostname;
1640 lower_hostname = stralloc(dump_hostname);
1641 for(s=lower_hostname; *s != '\0'; s++)
1644 fn = getindexfname(dump_hostname, diskname, NULL, 0);
1645 if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
1646 amfree(lower_hostname);
1651 if (hostname != NULL) {
1652 fn = getindexfname(hostname, diskname, NULL, 0);
1653 if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
1654 amfree(lower_hostname);
1660 fn = getindexfname(lower_hostname, diskname, NULL, 0);
1661 if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
1662 amfree(lower_hostname);
1667 if(diskname != NULL) {
1668 fn = getoldindexfname(dump_hostname, diskname, NULL, 0);
1669 if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
1670 amfree(lower_hostname);
1675 if (hostname != NULL) {
1676 fn = getoldindexfname(hostname, diskname, NULL, 0);
1677 if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
1678 amfree(lower_hostname);
1684 fn = getoldindexfname(lower_hostname, diskname, NULL, 0);
1685 if (stat(fn, &dir_stat) == 0 && S_ISDIR(dir_stat.st_mode)) {
1686 amfree(lower_hostname);
1692 amfree(lower_hostname);
1698 char *dump_hostname,
1704 struct stat dir_stat;
1707 char *lower_hostname;
1709 lower_hostname = stralloc(dump_hostname);
1710 for(s=lower_hostname; *s != '\0'; s++)
1713 fn = getindexfname(dump_hostname, diskname, timestamps, level);
1714 if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
1715 amfree(lower_hostname);
1719 if(hostname != NULL) {
1720 fn = getindexfname(hostname, diskname, timestamps, level);
1721 if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
1722 amfree(lower_hostname);
1727 fn = getindexfname(lower_hostname, diskname, timestamps, level);
1728 if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
1729 amfree(lower_hostname);
1733 if(diskname != NULL) {
1734 fn = getoldindexfname(dump_hostname, diskname, timestamps, level);
1735 if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
1736 amfree(lower_hostname);
1740 if(hostname != NULL) {
1741 fn = getoldindexfname(hostname, diskname, timestamps, level);
1742 if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
1743 amfree(lower_hostname);
1748 fn = getoldindexfname(lower_hostname, diskname, timestamps, level);
1749 if (stat(fn, &dir_stat) == 0 && S_ISREG(dir_stat.st_mode)) {
1750 amfree(lower_hostname);
1754 amfree(lower_hostname);