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"
61 typedef struct REMOVE_ITEM
64 struct REMOVE_ITEM *next;
68 static int from_amandad;
69 static char local_hostname[MAX_HOSTNAME_LENGTH+1]; /* me! */
70 static char *remote_hostname = NULL; /* the client */
71 static char *dump_hostname = NULL; /* machine we are restoring */
72 static char *disk_name; /* disk we are restoring */
73 char *qdisk_name = NULL; /* disk we are restoring */
74 static char *target_date = NULL;
75 static disklist_t disk_list; /* all disks in cur config */
76 static find_result_t *output_find = NULL;
77 static g_option_t *g_options = NULL;
78 static int cmdfdin, cmdfdout;
80 static int amindexd_debug = 0;
82 static REMOVE_ITEM *uncompress_remove = NULL;
83 /* uncompressed files to remove */
85 static am_feature_t *our_features = NULL;
86 static am_feature_t *their_features = NULL;
88 static REMOVE_ITEM *remove_files(REMOVE_ITEM *);
89 static char *uncompress_file(char *, char **);
90 static int process_ls_dump(char *, DUMP_ITEM *, int, char **);
92 static size_t reply_buffer_size = 1;
93 static char *reply_buffer = NULL;
94 static char *amandad_auth = NULL;
96 static void reply(int, char *, ...)
97 __attribute__ ((format (printf, 2, 3)));
98 static void lreply(int, char *, ...)
99 __attribute__ ((format (printf, 2, 3)));
100 static void fast_lreply(int, char *, ...)
101 __attribute__ ((format (printf, 2, 3)));
102 static int is_dump_host_valid(char *);
103 static int is_disk_valid(char *);
104 static int is_config_valid(char *);
105 static int build_disk_table(void);
106 static int disk_history_list(void);
107 static int is_dir_valid_opaque(char *);
108 static int opaque_ls(char *, int);
109 static void opaque_ls_one (DIR_ITEM *dir_item, am_feature_e marshall_feature,
111 static int tapedev_is(void);
112 static int are_dumps_compressed(void);
113 static char *amindexd_nicedate (char *datestamp);
114 static int cmp_date (const char *date1, const char *date2);
115 int main(int, char **);
124 dbprintf(("%s: removing index file: %s\n",
125 debug_prefix_time(NULL), remove->filename));
126 unlink(remove->filename);
127 amfree(remove->filename);
129 remove = remove->next;
141 char *filename = NULL;
142 struct stat stat_filename;
146 filename = stralloc(filename_gz);
147 len = strlen(filename);
148 if(len > 3 && strcmp(&(filename[len-3]),".gz")==0) {
149 filename[len-3]='\0';
150 } else if(len > 2 && strcmp(&(filename[len-2]),".Z")==0) {
151 filename[len-2]='\0';
154 /* uncompress the file */
155 result=stat(filename,&stat_filename);
156 if(result==-1 && errno==ENOENT) { /* file does not exist */
158 REMOVE_ITEM *remove_file;
161 * Check that compressed file exists manually.
163 if (stat(filename_gz, &statbuf) < 0) {
164 *emsg = newvstralloc(*emsg, "Compressed file '",
166 "' is inaccessable: ",
169 dbprintf(("%s\n",*emsg));
174 cmd = vstralloc(UNCOMPRESS_PATH,
175 #ifdef UNCOMPRESS_OPT
178 " \'", filename_gz, "\'",
180 " | (LC_ALL=C; export LC_ALL ; sort) ",
181 " > ", "\'", filename, "\'",
183 dbprintf(("%s: uncompress command: %s\n",
184 debug_prefix_time(NULL), cmd));
185 if (system(cmd) != 0) {
186 *emsg = newvstralloc(*emsg, "\"", cmd, "\" failed", NULL);
194 /* add at beginning */
195 remove_file = (REMOVE_ITEM *)alloc(SIZEOF(REMOVE_ITEM));
196 remove_file->filename = stralloc(filename);
197 remove_file->next = uncompress_remove;
198 uncompress_remove = remove_file;
199 } else if(!S_ISREG((stat_filename.st_mode))) {
201 *emsg = vstralloc("\"", filename, "\" is not a regular file", NULL);
211 /* find all matching entries in a dump listing */
212 /* return -1 if error */
216 DUMP_ITEM * dump_item,
221 char *old_line = NULL;
222 char *filename = NULL;
224 char *dir_slash = NULL;
228 size_t len_dir_slash;
230 if (strcmp(dir, "/") == 0) {
231 dir_slash = stralloc(dir);
233 dir_slash = stralloc2(dir, "/");
236 filename_gz = getindexfname(dump_hostname, disk_name, dump_item->date,
238 if((filename = uncompress_file(filename_gz, emsg)) == NULL) {
245 if((fp = fopen(filename,"r"))==0) {
247 *emsg = stralloc(strerror(errno));
252 len_dir_slash=strlen(dir_slash);
254 while ((line = agets(fp)) != NULL) {
255 if (line[0] != '\0') {
256 if(strncmp(dir_slash, line, len_dir_slash) == 0) {
258 s = line + len_dir_slash;
260 while(ch && ch != '/')
261 ch = *s++;/* find end of the file name */
267 if(old_line == NULL || strcmp(line, old_line) != 0) {
268 add_dir_list_item(dump_item, line);
275 /*@i@*/ amfree(line);
278 /*@i@*/ amfree(old_line);
284 /* send a 1 line reply to the client */
285 printf_arglist_function1(static void reply, int, n, char *, fmt)
291 reply_buffer = alloc(reply_buffer_size);
294 arglist_start(args, fmt);
295 len = vsnprintf(reply_buffer, reply_buffer_size, fmt, args);
298 if (len > -1 && (size_t)len < reply_buffer_size)
301 reply_buffer_size *= 2;
302 amfree(reply_buffer);
303 reply_buffer = alloc(reply_buffer_size);
306 if (printf("%03d %s\r\n", n, reply_buffer) < 0)
308 dbprintf(("%s: ! error %d (%s) in printf\n",
309 debug_prefix_time(NULL), errno, strerror(errno)));
310 uncompress_remove = remove_files(uncompress_remove);
313 if (fflush(stdout) != 0)
315 dbprintf(("%s: ! error %d (%s) in fflush\n",
316 debug_prefix_time(NULL), errno, strerror(errno)));
317 uncompress_remove = remove_files(uncompress_remove);
320 dbprintf(("%s: < %03d %s\n", debug_prefix_time(NULL), n, reply_buffer));
323 /* send one line of a multi-line response */
324 printf_arglist_function1(static void lreply, int, n, char *, fmt)
330 reply_buffer = alloc(reply_buffer_size);
333 arglist_start(args, fmt);
334 len = vsnprintf(reply_buffer, reply_buffer_size, fmt, args);
337 if (len > -1 && (size_t)len < reply_buffer_size)
340 reply_buffer_size *= 2;
341 amfree(reply_buffer);
342 reply_buffer = alloc(reply_buffer_size);
345 if (printf("%03d-%s\r\n", n, reply_buffer) < 0)
347 dbprintf(("%s: ! error %d (%s) in printf\n",
348 debug_prefix_time(NULL), errno, strerror(errno)));
349 uncompress_remove = remove_files(uncompress_remove);
352 if (fflush(stdout) != 0)
354 dbprintf(("%s: ! error %d (%s) in fflush\n",
355 debug_prefix_time(NULL), errno, strerror(errno)));
356 uncompress_remove = remove_files(uncompress_remove);
360 dbprintf(("%s: < %03d-%s\n", debug_prefix_time(NULL), n, reply_buffer));
364 /* send one line of a multi-line response */
365 printf_arglist_function1(static void fast_lreply, int, n, char *, fmt)
371 reply_buffer = alloc(reply_buffer_size);
374 arglist_start(args, fmt);
375 len = vsnprintf(reply_buffer, reply_buffer_size, fmt, args);
378 if (len > -1 && (size_t)len < reply_buffer_size)
381 reply_buffer_size *= 2;
382 amfree(reply_buffer);
383 reply_buffer = alloc(reply_buffer_size);
386 if (printf("%03d-%s\r\n", n, reply_buffer) < 0)
388 dbprintf(("%s: ! error %d (%s) in printf\n",
389 debug_prefix_time(NULL), errno, strerror(errno)));
390 uncompress_remove = remove_files(uncompress_remove);
394 dbprintf(("%s: < %03d-%s\n", debug_prefix_time(NULL), n, reply_buffer));
397 /* see if hostname is valid */
398 /* valid is defined to be that there is an index directory for it */
399 /* also do a security check on the requested dump hostname */
400 /* to restrict access to index records if required */
401 /* return -1 if not okay */
406 struct stat dir_stat;
410 if (config_name == NULL) {
411 reply(501, "Must set config before setting host.");
416 /* only let a client restore itself for now unless it is the server */
417 if (strcasecmp(remote_hostname, local_hostname) == 0)
419 if (strcasecmp(remote_hostname, host) != 0)
422 "You don't have the necessary permissions to set dump host to %s.",
428 /* check that the config actually handles that host */
429 ihost = lookup_host(host);
431 reply(501, "Host %s is not in your disklist.", host);
435 /* assume an index dir already */
436 fn = getindexfname(host, NULL, NULL, 0);
437 if (stat (fn, &dir_stat) != 0 || !S_ISDIR(dir_stat.st_mode)) {
438 reply(501, "No index records for host: %s. Have you enabled indexing?", host);
453 struct stat dir_stat;
457 if (config_name == NULL) {
458 reply(501, "Must set config,host before setting disk.");
461 else if (dump_hostname == NULL) {
462 reply(501, "Must set host before setting disk.");
466 /* check that the config actually handles that disk */
467 idisk = lookup_disk(dump_hostname, disk);
469 qdisk = quote_string(disk);
470 reply(501, "Disk %s:%s is not in your disklist.", dump_hostname, qdisk);
475 /* assume an index dir already */
476 fn = getindexfname(dump_hostname, disk, NULL, 0);
477 if (stat (fn, &dir_stat) != 0 || !S_ISDIR(dir_stat.st_mode)) {
478 qdisk = quote_string(disk);
479 reply(501, "No index records for disk: %s. Invalid?", qdisk);
498 struct stat dir_stat;
500 /* check that the config actually exists */
501 if (config == NULL) {
502 reply(501, "Must set config first.");
507 conffile = stralloc2(config_dir, CONFFILE_NAME);
508 if (read_conffile(conffile)) {
509 reply(501, "Could not read config file %s!", conffile);
515 conf_diskfile = getconf_str(CNF_DISKFILE);
516 if (*conf_diskfile == '/') {
517 conf_diskfile = stralloc(conf_diskfile);
519 conf_diskfile = stralloc2(config_dir, conf_diskfile);
521 if (read_diskfile(conf_diskfile, &disk_list) < 0) {
522 reply(501, "Could not read disk file %s!", conf_diskfile);
523 amfree(conf_diskfile);
526 amfree(conf_diskfile);
528 conf_tapelist = getconf_str(CNF_TAPELIST);
529 if (*conf_tapelist == '/') {
530 conf_tapelist = stralloc(conf_tapelist);
532 conf_tapelist = stralloc2(config_dir, conf_tapelist);
534 if(read_tapelist(conf_tapelist)) {
535 reply(501, "Could not read tapelist file %s!", conf_tapelist);
536 amfree(conf_tapelist);
539 amfree(conf_tapelist);
541 dbrename(config, DBG_SUBDIR_SERVER);
543 output_find = find_dump(1, &disk_list);
544 sort_find_result("DLKHpB", &output_find);
546 conf_indexdir = getconf_str(CNF_INDEXDIR);
547 if(*conf_indexdir == '/') {
548 conf_indexdir = stralloc(conf_indexdir);
550 conf_indexdir = stralloc2(config_dir, conf_indexdir);
552 if (stat (conf_indexdir, &dir_stat) != 0 || !S_ISDIR(dir_stat.st_mode)) {
553 reply(501, "Index directory %s does not exist", conf_indexdir);
554 amfree(conf_indexdir);
557 amfree(conf_indexdir);
564 build_disk_table(void)
567 char *last_timestamp;
571 find_result_t *find_output;
573 if (config_name == NULL) {
574 reply(590, "Must set config,host,disk before building disk table");
577 else if (dump_hostname == NULL) {
578 reply(590, "Must set host,disk before building disk table");
581 else if (disk_name == NULL) {
582 reply(590, "Must set disk before building disk table");
587 last_timestamp = NULL;
588 last_filenum = (off_t)-1;
591 for(find_output = output_find;
593 find_output = find_output->next) {
594 if(strcasecmp(dump_hostname, find_output->hostname) == 0 &&
595 strcmp(disk_name , find_output->diskname) == 0 &&
596 strcmp("OK" , find_output->status) == 0) {
598 if(strcmp("--", find_output->partnum)){
599 partnum = atoi(find_output->partnum);
602 * The sort order puts holding disk entries first. We want to
603 * use them if at all possible, so ignore any other entries
604 * for the same datestamp after we see a holding disk entry
605 * (as indicated by a filenum of zero).
608 strcmp(find_output->timestamp, last_timestamp) == 0 &&
609 find_output->level == last_level &&
610 partnum == last_partnum && last_filenum == 0) {
613 last_timestamp = find_output->timestamp;
614 last_filenum = find_output->filenum;
615 last_level = find_output->level;
616 last_partnum = partnum;
617 date = amindexd_nicedate(find_output->timestamp);
618 add_dump(date, find_output->level, find_output->label,
619 find_output->filenum, partnum);
620 dbprintf(("%s: - %s %d %s " OFF_T_FMT " %d\n",
621 debug_prefix_time(NULL), date, find_output->level,
623 (OFF_T_FMT_TYPE)find_output->filenum,
632 disk_history_list(void)
637 if (config_name == NULL) {
638 reply(502, "Must set config,host,disk before listing history");
641 else if (dump_hostname == NULL) {
642 reply(502, "Must set host,disk before listing history");
645 else if (disk_name == NULL) {
646 reply(502, "Must set disk before listing history");
650 lreply(200, " Dump history for config \"%s\" host \"%s\" disk %s",
651 config_name, dump_hostname, qdisk_name);
653 for (item=first_dump(); item!=NULL; item=next_dump(item)){
654 char *tapelist_str = marshal_tapelist(item->tapes, 1);
656 strncpy(date, item->date, 20);
658 if(!am_has_feature(their_features,fe_amrecover_timestamp))
661 if(am_has_feature(their_features, fe_amindexd_marshall_in_DHST)){
662 lreply(201, " %s %d %s", date, item->level, tapelist_str);
665 lreply(201, " %s %d %s " OFF_T_FMT, date, item->level,
666 tapelist_str, (OFF_T_FMT_TYPE)item->file);
668 amfree(tapelist_str);
671 reply(200, "Dump history for config \"%s\" host \"%s\" disk %s",
672 config_name, dump_hostname, qdisk_name);
679 * is the directory dir backed up - dir assumed complete relative to
682 /* opaque version of command */
692 char *filename_gz = NULL;
693 char *filename = NULL;
695 static char *emsg = NULL;
697 if (config_name == NULL || dump_hostname == NULL || disk_name == NULL) {
698 reply(502, "Must set config,host,disk before asking about directories");
701 else if (dump_hostname == NULL) {
702 reply(502, "Must set host,disk before asking about directories");
705 else if (disk_name == NULL) {
706 reply(502, "Must set disk before asking about directories");
709 else if (target_date == NULL) {
710 reply(502, "Must set date before asking about directories");
714 /* scan through till we find first dump on or before date */
715 for (item=first_dump(); item!=NULL; item=next_dump(item))
716 if (cmp_date(item->date, target_date) <= 0)
721 /* no dump for given date */
722 reply(500, "No dumps available on or before date \"%s\"", target_date);
726 if(strcmp(dir, "/") == 0) {
727 ldir = stralloc(dir);
729 ldir = stralloc2(dir, "/");
731 ldir_len = strlen(ldir);
733 /* go back till we hit a level 0 dump */
737 filename_gz = getindexfname(dump_hostname, disk_name,
738 item->date, item->level);
739 if((filename = uncompress_file(filename_gz, &emsg)) == NULL) {
740 reply(599, "System error %s", emsg);
747 dbprintf(("%s: f %s\n", debug_prefix_time(NULL), filename));
748 if ((fp = fopen(filename, "r")) == NULL) {
749 reply(599, "System error %s", strerror(errno));
754 for(; (line = agets(fp)) != NULL; free(line)) {
757 if (strncmp(line, ldir, ldir_len) != 0) {
758 continue; /* not found yet */
768 last_level = item->level;
771 item=next_dump(item);
772 } while ((item != NULL) && (item->level >= last_level));
773 } while (item != NULL);
777 reply(500, "\"%s\" is an invalid directory", dir);
786 DUMP_ITEM *dump_item;
788 int level, last_level;
789 static char *emsg = NULL;
790 am_feature_e marshall_feature;
793 marshall_feature = fe_amindexd_marshall_in_ORLD;
795 marshall_feature = fe_amindexd_marshall_in_OLSD;
800 if (config_name == NULL) {
801 reply(502, "Must set config,host,disk before listing a directory");
804 else if (dump_hostname == NULL) {
805 reply(502, "Must set host,disk before listing a directory");
808 else if (disk_name == NULL) {
809 reply(502, "Must set disk before listing a directory");
812 else if (target_date == NULL) {
813 reply(502, "Must set date before listing a directory");
817 /* scan through till we find first dump on or before date */
818 for (dump_item=first_dump(); dump_item!=NULL; dump_item=next_dump(dump_item))
819 if (cmp_date(dump_item->date, target_date) <= 0)
822 if (dump_item == NULL)
824 /* no dump for given date */
825 reply(500, "No dumps available on or before date \"%s\"", target_date);
829 /* get data from that dump */
830 if (process_ls_dump(dir, dump_item, recursive, &emsg) == -1) {
831 reply(599, "System error %s", emsg);
836 /* go back processing higher level dumps till we hit a level 0 dump */
837 last_level = dump_item->level;
838 while ((last_level != 0) && ((dump_item=next_dump(dump_item)) != NULL))
840 if (dump_item->level < last_level)
842 last_level = dump_item->level;
843 if (process_ls_dump(dir, dump_item, recursive, &emsg) == -1) {
844 reply(599, "System error %s", emsg);
851 /* return the information to the caller */
852 lreply(200, " Opaque list of %s", dir);
853 for(level=0; level<=9; level++) {
854 for (dir_item = get_dir_list(); dir_item != NULL;
855 dir_item = dir_item->next) {
857 if(dir_item->dump->level == level) {
858 if (!am_has_feature(their_features, marshall_feature) &&
859 (num_entries(dir_item->dump->tapes) > 1 ||
860 dir_item->dump->tapes->numfiles > 1)) {
861 fast_lreply(501, " ERROR: Split dumps not supported"
862 " with old version of amrecover.");
866 opaque_ls_one(dir_item, marshall_feature, recursive);
871 reply(200, " Opaque list of %s", dir);
879 am_feature_e marshall_feature,
886 if (am_has_feature(their_features, marshall_feature)) {
887 tapelist_str = marshal_tapelist(dir_item->dump->tapes, 1);
889 tapelist_str = dir_item->dump->tapes->label;
892 strncpy(date, dir_item->dump->date, 20);
894 if(!am_has_feature(their_features,fe_amrecover_timestamp))
897 qpath = quote_string(dir_item->path);
898 if((!recursive && am_has_feature(their_features,
899 fe_amindexd_fileno_in_OLSD)) ||
900 (recursive && am_has_feature(their_features,
901 fe_amindexd_fileno_in_ORLD))) {
902 fast_lreply(201, " %s %d %s " OFF_T_FMT " %s",
904 dir_item->dump->level,
906 (OFF_T_FMT_TYPE)dir_item->dump->file,
911 fast_lreply(201, " %s %d %s %s",
912 date, dir_item->dump->level,
913 tapelist_str, qpath);
916 if(am_has_feature(their_features, marshall_feature)) {
917 amfree(tapelist_str);
922 * returns the value of changer or tapedev from the amanda.conf file if set,
923 * otherwise reports an error
931 /* check state okay to do this */
932 if (config_name == NULL) {
933 reply(501, "Must set config before asking about tapedev.");
937 /* use amrecover_changer if possible */
938 if ((result = getconf_str(CNF_AMRECOVER_CHANGER)) != NULL &&
940 dbprintf(("%s: tapedev_is amrecover_changer: %s\n",
941 debug_prefix_time(NULL), result));
946 /* use changer if possible */
947 if ((result = getconf_str(CNF_TPCHANGER)) != NULL && *result != '\0') {
948 dbprintf(("%s: tapedev_is tpchanger: %s\n",
949 debug_prefix_time(NULL), result));
954 /* get tapedev value */
955 if ((result = getconf_str(CNF_TAPEDEV)) != NULL && *result != '\0') {
956 dbprintf(("%s: tapedev_is tapedev: %s\n",
957 debug_prefix_time(NULL), result));
962 dbprintf(("%s: No tapedev or changer in config site.\n",
963 debug_prefix_time(NULL)));
964 reply(501, "Tapedev or changer not set in config file.");
969 /* returns YES if dumps for disk are compressed, NO if not */
971 are_dumps_compressed(void)
975 /* check state okay to do this */
976 if (config_name == NULL) {
977 reply(501, "Must set config,host,disk name before asking about dumps.");
980 else if (dump_hostname == NULL) {
981 reply(501, "Must set host,disk name before asking about dumps.");
984 else if (disk_name == NULL) {
985 reply(501, "Must set disk name before asking about dumps.");
989 /* now go through the list of disks and find which have indexes */
990 for (diskp = disk_list.head; diskp != NULL; diskp = diskp->next) {
991 if ((strcasecmp(diskp->host->hostname, dump_hostname) == 0)
992 && (strcmp(diskp->name, disk_name) == 0)) {
998 reply(501, "Couldn't find host/disk in disk file.");
1002 /* send data to caller */
1003 if (diskp->compress == COMP_NONE)
1016 char *line = NULL, *part = NULL;
1019 char *cmd_undo, cmd_undo_ch;
1021 struct sockaddr_in his_addr;
1022 struct hostent *his_name;
1026 int user_validated = 0;
1027 char *errstr = NULL;
1028 char *pgm = "amindexd"; /* in case argv[0] is not set */
1030 safe_fd(DATA_FD_OFFSET, 2);
1034 * When called via inetd, it is not uncommon to forget to put the
1035 * argv[0] value on the config line. On some systems (e.g. Solaris)
1036 * this causes argv and/or argv[0] to be NULL, so we have to be
1037 * careful getting our name.
1039 if (argc >= 1 && argv != NULL && argv[0] != NULL) {
1040 if((pgm = strrchr(argv[0], '/')) != NULL) {
1049 /* Don't die when child closes pipe */
1050 signal(SIGPIPE, SIG_IGN);
1054 /* we'd rather not run as root */
1056 if(geteuid() == 0) {
1057 if(client_uid == (uid_t) -1) {
1058 error("error [cannot find user %s in passwd file]\n", CLIENT_LOGIN);
1063 initgroups(CLIENT_LOGIN, client_gid);
1069 #endif /* FORCE_USERID */
1071 dbopen(DBG_SUBDIR_SERVER);
1072 dbprintf(("%s: version %s\n", get_pname(), version()));
1075 error("argv == NULL\n");
1078 if (! (argc >= 1 && argv[0] != NULL)) {
1079 dbprintf(("%s: WARNING: argv[0] not defined: check inetd.conf\n",
1080 debug_prefix_time(NULL)));
1095 if(argc > 0 && strcmp(*argv, "-t") == 0) {
1101 if(argc > 0 && strcmp(*argv, "amandad") == 0) {
1106 amandad_auth = *argv;
1117 config_name = stralloc(*argv);
1118 config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
1123 if(gethostname(local_hostname, SIZEOF(local_hostname)-1) == -1) {
1124 error("gethostname: %s", strerror(errno));
1127 local_hostname[SIZEOF(local_hostname)-1] = '\0';
1129 /* now trim domain off name */
1132 while(ch && ch != '.') ch = *s++;
1136 if(from_amandad == 0) {
1137 if(amindexd_debug) {
1139 * Fake the remote address as the local address enough to get
1140 * through the security check.
1142 his_name = gethostbyname(local_hostname);
1143 if(his_name == NULL) {
1144 error("gethostbyname(%s) failed\n", local_hostname);
1147 assert((sa_family_t)his_name->h_addrtype == (sa_family_t)AF_INET);
1148 his_addr.sin_family = (sa_family_t)his_name->h_addrtype;
1149 his_addr.sin_port = (in_port_t)htons(0);
1150 memcpy((void *)&his_addr.sin_addr.s_addr,
1151 (void *)his_name->h_addr_list[0],
1152 (size_t)his_name->h_length);
1154 /* who are we talking to? */
1155 socklen = sizeof (his_addr);
1156 if (getpeername(0, (struct sockaddr *)&his_addr, &socklen) == -1)
1157 error("getpeername: %s", strerror(errno));
1159 if ((his_addr.sin_family != (sa_family_t)AF_INET)
1160 || (ntohs(his_addr.sin_port) == 20)) {
1161 error("connection rejected from %s family %d port %d",
1162 inet_ntoa(his_addr.sin_addr), his_addr.sin_family,
1163 htons(his_addr.sin_port));
1166 if ((his_name = gethostbyaddr((char *)&(his_addr.sin_addr),
1167 sizeof(his_addr.sin_addr),
1168 AF_INET)) == NULL) {
1169 error("gethostbyaddr(%s): hostname lookup failed",
1170 inet_ntoa(his_addr.sin_addr));
1173 fp = s = stralloc(his_name->h_name);
1175 while(ch && ch != '.') ch = *s++;
1177 remote_hostname = newstralloc(remote_hostname, fp);
1182 cmdfdout = DATA_FD_OFFSET + 0;
1183 cmdfdin = DATA_FD_OFFSET + 1;
1185 /* read the REQ packet */
1186 for(; (line = agets(stdin)) != NULL; free(line)) {
1187 #define sc "OPTIONS "
1188 if(strncmp(line, sc, sizeof(sc)-1) == 0) {
1190 g_options = parse_g_options(line+8, 1);
1191 if(!g_options->hostname) {
1192 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
1193 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
1194 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
1200 if(amandad_auth && g_options->auth) {
1201 if(strcasecmp(amandad_auth, g_options->auth) != 0) {
1202 printf("ERROR recover program ask for auth=%s while amindexd is configured for '%s'\n",
1203 g_options->auth, amandad_auth);
1204 error("amindexd: ERROR recover program ask for auth=%s while amindexd is configured for '%s'",
1205 g_options->auth, amandad_auth);
1209 /* send the REP packet */
1210 printf("CONNECT MESG %d\n", DATA_FD_OFFSET);
1214 if ((dup2(cmdfdout, fileno(stdout)) < 0)
1215 || (dup2(cmdfdin, fileno(stdin)) < 0)) {
1216 error("amandad: Failed to setup stdin or stdout");
1221 /* clear these so we can detect when the have not been set by the client */
1222 amfree(dump_hostname);
1225 amfree(target_date);
1227 our_features = am_init_feature_set();
1228 their_features = am_set_default_feature_set();
1230 if (config_name != NULL && is_config_valid(config_name) != -1) {
1234 reply(220, "%s AMANDA index server (%s) ready.", local_hostname,
1237 user_validated = from_amandad;
1239 /* a real simple parser since there are only a few commands */
1242 /* get a line from the client */
1244 if((part = agets(stdin)) == NULL) {
1246 dbprintf(("%s: ? read error: %s\n",
1247 debug_prefix_time(NULL), strerror(errno)));
1249 dbprintf(("%s: ? unexpected EOF\n",
1250 debug_prefix_time(NULL)));
1253 dbprintf(("%s: ? unprocessed input:\n",
1254 debug_prefix_time(NULL)));
1255 dbprintf(("-----\n"));
1256 dbprintf(("? %s\n", line));
1257 dbprintf(("-----\n"));
1261 uncompress_remove = remove_files(uncompress_remove);
1263 return 1; /* they hung up? */
1265 strappend(line, part); /* Macro: line can be null */
1268 if(amindexd_debug) {
1269 break; /* we have a whole line */
1271 if((len = strlen(line)) > 0 && line[len-1] == '\r') {
1272 line[len-1] = '\0'; /* zap the '\r' */
1276 * Hmmm. We got a "line" from agets(), which means it saw
1277 * a '\n' (or EOF, etc), but there was not a '\r' before it.
1278 * Put a '\n' back in the buffer and loop for more.
1280 strappend(line, "\n");
1283 dbprintf(("%s: > %s\n", debug_prefix_time(NULL), line));
1290 skip_whitespace(s, ch);
1292 reply(500, "Command not recognised/incorrect: %s", line);
1298 skip_non_whitespace(s, ch);
1299 cmd_undo = s-1; /* for error message */
1300 cmd_undo_ch = *cmd_undo;
1303 skip_whitespace(s, ch); /* find the argument */
1306 skip_quoted_string(s, ch);
1307 arg = unquote_string(arg);
1312 if (!user_validated && strcmp(cmd, "SECURITY") == 0 && arg) {
1313 user_validated = check_security(&his_addr, arg, 0, &errstr);
1314 if(user_validated) {
1315 reply(200, "Access OK");
1320 if (!user_validated) { /* don't tell client the reason, just log it to debug log */
1321 reply(500, "Access not allowed");
1323 dbprintf(("%s: %s\n", debug_prefix_time(NULL), errstr));
1328 if (strcmp(cmd, "QUIT") == 0) {
1331 } else if (strcmp(cmd, "HOST") == 0 && arg) {
1332 /* set host we are restoring */
1334 if (is_dump_host_valid(arg) != -1)
1336 dump_hostname = newstralloc(dump_hostname, arg);
1337 reply(200, "Dump host set to %s.", dump_hostname);
1338 amfree(qdisk_name); /* invalidate any value */
1339 amfree(disk_name); /* invalidate any value */
1342 } else if (strcmp(cmd, "LISTHOST") == 0) {
1348 if (config_name == NULL) {
1349 reply(501, "Must set config before listhost");
1352 lreply(200, " List hosts for config %s", config_name);
1353 for (disk = disk_list.head; disk!=NULL; disk = disk->next) {
1355 for (diskdup = disk_list.head; diskdup!=disk; diskdup = diskdup->next) {
1356 if(strcmp(diskdup->host->hostname, disk->host->hostname) == 0) {
1362 fast_lreply(201, " %s", disk->host->hostname);
1367 reply(200, " List hosts for config %s", config_name);
1370 reply(200, "No hosts for config %s", config_name);
1374 } else if (strcmp(cmd, "DISK") == 0 && arg) {
1376 if (is_disk_valid(arg) != -1) {
1377 disk_name = newstralloc(disk_name, arg);
1378 qdisk_name = quote_string(disk_name);
1379 if (build_disk_table() != -1) {
1380 reply(200, "Disk set to %s.", qdisk_name);
1384 } else if (strcmp(cmd, "LISTDISK") == 0) {
1389 if (config_name == NULL) {
1390 reply(501, "Must set config, host before listdisk");
1392 else if (dump_hostname == NULL) {
1393 reply(501, "Must set host before listdisk");
1396 lreply(200, " List of disk for device %s on host %s", arg,
1398 for (disk = disk_list.head; disk!=NULL; disk = disk->next) {
1400 if (strcmp(disk->host->hostname, dump_hostname) == 0 &&
1401 ((disk->device && strcmp(disk->device, arg) == 0) ||
1402 (!disk->device && strcmp(disk->name, arg) == 0))) {
1403 qname = quote_string(disk->name);
1404 fast_lreply(201, " %s", qname);
1410 reply(200, "List of disk for device %s on host %s", arg,
1414 reply(200, "No disk for device %s on host %s", arg,
1419 lreply(200, " List of disk for host %s", dump_hostname);
1420 for (disk = disk_list.head; disk!=NULL; disk = disk->next) {
1421 if(strcmp(disk->host->hostname, dump_hostname) == 0) {
1422 qname = quote_string(disk->name);
1423 fast_lreply(201, " %s", qname);
1429 reply(200, "List of disk for host %s", dump_hostname);
1432 reply(200, "No disk for host %s", dump_hostname);
1436 } else if (strcmp(cmd, "SCNF") == 0 && arg) {
1438 amfree(config_name);
1440 config_name = newstralloc(config_name, arg);
1441 config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
1442 if (is_config_valid(arg) != -1) {
1443 amfree(dump_hostname); /* invalidate any value */
1444 amfree(qdisk_name); /* invalidate any value */
1445 amfree(disk_name); /* invalidate any value */
1446 reply(200, "Config set to %s.", config_name);
1448 amfree(config_name);
1452 } else if (strcmp(cmd, "FEATURES") == 0 && arg) {
1453 char *our_feature_string = NULL;
1454 char *their_feature_string = NULL;
1456 am_release_feature_set(our_features);
1457 am_release_feature_set(their_features);
1458 our_features = am_init_feature_set();
1459 our_feature_string = am_feature_to_string(our_features);
1460 their_feature_string = newstralloc(their_feature_string, arg);
1461 their_features = am_string_to_feature(their_feature_string);
1462 reply(200, "FEATURES %s", our_feature_string);
1463 amfree(our_feature_string);
1464 amfree(their_feature_string);
1466 } else if (strcmp(cmd, "DATE") == 0 && arg) {
1468 target_date = newstralloc(target_date, arg);
1469 reply(200, "Working date set to %s.", target_date);
1471 } else if (strcmp(cmd, "DHST") == 0) {
1472 (void)disk_history_list();
1473 } else if (strcmp(cmd, "OISD") == 0 && arg) {
1474 if (is_dir_valid_opaque(arg) != -1) {
1475 reply(200, "\"%s\" is a valid directory", arg);
1477 } else if (strcmp(cmd, "OLSD") == 0 && arg) {
1478 (void)opaque_ls(arg,0);
1479 } else if (strcmp(cmd, "ORLD") == 0 && arg) {
1480 (void)opaque_ls(arg, 1);
1481 } else if (strcmp(cmd, "TAPE") == 0) {
1483 } else if (strcmp(cmd, "DCMP") == 0) {
1484 (void)are_dumps_compressed();
1486 *cmd_undo = cmd_undo_ch; /* restore the command line */
1487 reply(500, "Command not recognised/incorrect: %s", cmd);
1493 uncompress_remove = remove_files(uncompress_remove);
1494 free_find_result(&output_find);
1495 reply(200, "Good bye.");
1504 static char nice[20];
1505 int year, month, day;
1506 int hours, minutes, seconds;
1507 char date[9], atime[7];
1508 int numdate, numtime;
1510 strncpy(date, datestamp, 8);
1512 numdate = atoi(date);
1513 year = numdate / 10000;
1514 month = (numdate / 100) % 100;
1515 day = numdate % 100;
1517 if(strlen(datestamp) <= 8) {
1518 snprintf(nice, SIZEOF(nice), "%4d-%02d-%02d",
1522 strncpy(atime, &(datestamp[8]), 6);
1524 numtime = atoi(atime);
1525 hours = numtime / 10000;
1526 minutes = (numtime / 100) % 100;
1527 seconds = numtime % 100;
1529 snprintf(nice, SIZEOF(nice), "%4d-%02d-%02d-%02d-%02d-%02d",
1530 year, month, day, hours, minutes, seconds);
1541 return strncmp(date1, date2, strlen(date2));