2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998, 2000 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: amrecover.c,v 1.73 2006/07/25 18:27:57 martinea Exp $
29 * an interactive program for recovering backed-up files
35 #include "amfeatures.h"
36 #include "amrecover.h"
45 #define amrecover_debug(i,x) do { \
46 if ((i) <= debug_amrecover) { \
51 extern int process_line(char *line);
53 int grab_reply(int show);
54 void sigint_handler(int signum);
55 int main(int argc, char **argv);
57 #define USAGE "Usage: amrecover [[-C] <config>] [-s <index-server>] [-t <tape-server>] [-d <tape-device>] [-o <clientconfigoption>]*\n"
60 char *server_name = NULL;
62 char *server_line = NULL;
63 char *dump_datestamp = NULL; /* date we are restoring */
64 char *dump_hostname; /* which machine we are restoring */
65 char *disk_name = NULL; /* disk we are restoring */
66 char *mount_point = NULL; /* where disk was mounted */
67 char *disk_path = NULL; /* path relative to mount point */
68 char dump_date[STR_SIZE]; /* date on which we are restoring */
69 int quit_prog; /* set when time to exit parser */
70 char *tape_server_name = NULL;
71 int tape_server_socket;
72 char *tape_device_name = NULL;
73 am_feature_t *our_features = NULL;
74 char *our_features_string = NULL;
75 am_feature_t *indexsrv_features = NULL;
76 am_feature_t *tapesrv_features = NULL;
77 static char *errstr = NULL;
79 int amindexd_alive = 0;
83 security_stream_t *fd;
88 #define NSTREAMS (int)(sizeof(streams) / sizeof(streams[0]))
90 static void amindexd_response(void *, pkt_t *, security_handle_t *);
91 void stop_amindexd(void);
92 char *amindexd_client_get_security_conf(char *, void *);
94 static char* mesg_buffer = NULL;
95 /* gets a "line" from server and put in server_line */
96 /* server_line is terminated with \0, \r\n is striped */
97 /* returns -1 if error */
107 mesg_buffer = stralloc("");
109 while (!strstr(mesg_buffer,"\r\n")) {
111 size = security_stream_read_sync(streams[MESGFD].fd, &buf);
113 amrecover_debug(1, ("%s: amrecover: get_line size < 0 (%zd)\n", debug_prefix_time(NULL), size));
117 amrecover_debug(1, ("%s: amrecover: get_line size == 0 (%zd)\n", debug_prefix_time(NULL), size));
120 else if (buf == NULL) {
121 amrecover_debug(1, ("%s: amrecover: get_line buf == NULL\n", debug_prefix_time(NULL)));
124 amrecover_debug(1, ("%s: amrecover: get_line size = %zd\n", debug_prefix_time(NULL), size));
125 newbuf = alloc(strlen(mesg_buffer)+size+1);
126 strncpy(newbuf, mesg_buffer, (size_t)(strlen(mesg_buffer) + size));
127 memcpy(newbuf+strlen(mesg_buffer), buf, (size_t)size);
128 newbuf[strlen(mesg_buffer)+size] = '\0';
130 mesg_buffer = newbuf;
133 s = strstr(mesg_buffer,"\r\n");
135 newbuf = stralloc(s+2);
136 server_line = newstralloc(server_line, mesg_buffer);
138 mesg_buffer = newbuf;
143 /* get reply from server and print to screen */
144 /* handle multi-line reply */
145 /* return -1 if error */
146 /* return code returned by server always occupies first 3 bytes of global
147 variable server_line */
153 if (get_line() == -1) {
156 if(show) puts(server_line);
157 } while (server_line[3] == '-');
158 if(show) fflush(stdout);
164 /* get 1 line of reply */
165 /* returns -1 if error, 0 if last (or only) line, 1 if more to follow */
169 if (get_line() == -1)
171 return server_line[3] == '-';
175 /* returns pointer to returned line */
184 /* returns 0 if server returned an error code (ie code starting with 5)
185 and non-zero otherwise */
189 return server_line[0] != '5';
198 * NOTE: this routine is called from sigint_handler, so we must be
199 * **very** careful about what we do since there is no way to know
200 * our state at the time the interrupt happened. For instance,
201 * do not use any stdio or malloc routines here.
205 buffer = alloc(strlen(cmd)+3);
206 strncpy(buffer, cmd, strlen(cmd));
207 buffer[strlen(cmd)] = '\r';
208 buffer[strlen(cmd)+1] = '\n';
209 buffer[strlen(cmd)+2] = '\0';
211 if(security_stream_write(streams[MESGFD].fd, buffer, strlen(buffer)) < 0) {
219 /* send a command to the server, get reply and print to screen */
224 if (send_command(cmd) == -1) return -1;
225 if (grab_reply(1) == -1) return -1;
230 /* same as converse() but reply not echoed to stdout */
235 if (send_command(cmd) == -1) return -1;
236 if (grab_reply(0) == -1) return -1;
241 /* basic interrupt handler for when user presses ^C */
242 /* Bale out, letting server know before doing so */
248 * NOTE: we must be **very** careful about what we do here since there
249 * is no way to know our state at the time the interrupt happened.
250 * For instance, do not use any stdio routines here or in any called
251 * routines. Also, use _exit() instead of exit() to make sure stdio
252 * buffer flushing is not attempted.
254 (void)signum; /* Quiet unused parameter warning */
256 if (extract_restore_child_pid != -1)
257 (void)kill(extract_restore_child_pid, SIGKILL);
258 extract_restore_child_pid = -1;
261 (void)send_command("QUIT");
274 /* remove "/" at end of path */
275 if(length>1 && s[length-1]=='/')
278 /* change "/." to "/" */
279 if(strcmp(s,"/.")==0)
282 /* remove "/." at end of path */
283 if(strcmp(&(s[length-2]),"/.")==0)
292 (void)converse("QUIT");
296 char *localhost = NULL;
298 #ifdef DEFAULT_TAPE_SERVER
299 # define DEFAULT_TAPE_SERVER_FAILOVER (DEFAULT_TAPE_SERVER)
301 # define DEFAULT_TAPE_SERVER_FAILOVER (NULL)
311 char *lineread = NULL;
312 struct sigaction act, oact;
317 const security_driver_t *secdrv;
326 set_pname("amrecover");
328 /* Don't die when child closes pipe */
329 signal(SIGPIPE, SIG_IGN);
331 dbopen(DBG_SUBDIR_CLIENT);
333 #ifndef IGNORE_UID_CHECK
334 if (geteuid() != 0) {
335 erroutput_type |= ERR_SYSLOG;
336 error("amrecover must be run by root");
341 localhost = alloc(MAX_HOSTNAME_LENGTH+1);
342 if (gethostname(localhost, MAX_HOSTNAME_LENGTH) != 0) {
343 error("cannot determine local host name\n");
346 localhost[MAX_HOSTNAME_LENGTH] = '\0';
348 parse_conf(argc, argv, &new_argc, &new_argv);
350 if (new_argc > 1 && new_argv[1][0] != '-') {
352 * If the first argument is not an option flag, then we assume
353 * it is a configuration name to match the syntax of the other
358 new_argv1 = (char **) alloc((size_t)((new_argc + 1 + 1) * sizeof(*new_argv1)));
359 new_argv1[0] = new_argv[0];
361 for (i = 1; i < new_argc; i++) {
362 new_argv1[i + 1] = new_argv[i];
364 new_argv1[i + 1] = NULL;
367 new_argv = new_argv1;
369 while ((i = getopt(new_argc, new_argv, "C:s:t:d:U")) != EOF) {
372 add_client_conf(CNF_CONF, optarg);
376 add_client_conf(CNF_INDEX_SERVER, optarg);
380 add_client_conf(CNF_TAPE_SERVER, optarg);
384 add_client_conf(CNF_TAPEDEV, optarg);
393 if (optind != new_argc) {
394 (void)fprintf(stderr, USAGE);
398 our_features = am_init_feature_set();
399 our_features_string = am_feature_to_string(our_features);
401 conffile = vstralloc(CONFIG_DIR, "/", "amanda-client.conf", NULL);
402 if (read_clientconf(conffile) > 0) {
403 error("error reading conffile: %s", conffile);
408 config = stralloc(getconf_str(CNF_CONF));
410 conffile = vstralloc(CONFIG_DIR, "/", config, "/", "amanda-client.conf",
412 if (read_clientconf(conffile) > 0) {
413 error("error reading conffile: %s", conffile);
418 dbrename(config, DBG_SUBDIR_CLIENT);
420 report_bad_conf_arg();
423 if (getconf_seen(CNF_INDEX_SERVER) == -2) { /* command line argument */
424 server_name = getconf_str(CNF_INDEX_SERVER);
427 server_name = getenv("AMANDA_SERVER");
429 printf("Using index server from environment AMANDA_SERVER (%s)\n", server_name);
433 server_name = getconf_str(CNF_INDEX_SERVER);
436 error("No index server set");
439 server_name = stralloc(server_name);
441 tape_server_name = NULL;
442 if (getconf_seen(CNF_TAPE_SERVER) == -2) { /* command line argument */
443 tape_server_name = getconf_str(CNF_TAPE_SERVER);
445 if (!tape_server_name) {
446 tape_server_name = getenv("AMANDA_TAPE_SERVER");
447 if (!tape_server_name) {
448 tape_server_name = getenv("AMANDA_TAPESERVER");
449 if (tape_server_name) {
450 printf("Using tape server from environment AMANDA_TAPESERVER (%s)\n", tape_server_name);
453 printf("Using tape server from environment AMANDA_TAPE_SERVER (%s)\n", tape_server_name);
456 if (!tape_server_name) {
457 tape_server_name = getconf_str(CNF_TAPE_SERVER);
459 if (!tape_server_name) {
460 error("No tape server set");
463 tape_server_name = stralloc(tape_server_name);
465 amfree(tape_device_name);
466 tape_device_name = getconf_str(CNF_TAPEDEV);
467 if (tape_device_name)
468 tape_device_name = stralloc(tape_device_name);
470 authopt = stralloc(getconf_str(CNF_AUTH));
478 /* Don't die when child closes pipe */
479 signal(SIGPIPE, SIG_IGN);
481 /* set up signal handler */
482 act.sa_handler = sigint_handler;
483 sigemptyset(&act.sa_mask);
485 if (sigaction(SIGINT, &act, &oact) != 0) {
486 error("error setting signal handler: %s", strerror(errno));
492 /* We assume that amindexd support fe_amindexd_options_features */
493 /* and fe_amindexd_options_auth */
494 /* We should send a noop to really know */
495 req = vstralloc("SERVICE amindexd\n",
496 "OPTIONS ", "features=", our_features_string, ";",
497 "auth=", authopt, ";",
500 secdrv = security_getdriver(authopt);
501 if (secdrv == NULL) {
502 error("no '%s' security driver available for host '%s'",
503 authopt, server_name);
507 protocol_sendreq(server_name, secdrv, generic_client_get_security_conf,
508 req, STARTUP_TIMEOUT, amindexd_response, &response_error);
513 printf("AMRECOVER Version %s. Contacting server on %s ...\n",
514 version(), server_name);
516 if(response_error != 0) {
517 fprintf(stderr,"%s\n",errstr);
523 * We may need root privilege again later for a reserved port to
524 * the tape server, so we will drop down now but might have to
531 /* get server's banner */
532 if (grab_reply(1) == -1) {
533 aclose(server_socket);
536 if (!server_happy()) {
538 aclose(server_socket);
542 /* try to get the features from the server */
544 char *their_feature_string = NULL;
546 line = stralloc2("FEATURES ", our_features_string);
547 if(exchange(line) == 0) {
548 their_feature_string = stralloc(server_line+13);
549 indexsrv_features = am_string_to_feature(their_feature_string);
552 indexsrv_features = am_set_default_feature_set();
554 amfree(their_feature_string);
558 /* set the date of extraction to be today */
560 tm = localtime(&timer);
562 strftime(dump_date, sizeof(dump_date), "%Y-%m-%d", tm);
566 printf("Setting restore date to today (%s)\n", dump_date);
567 line = stralloc2("DATE ", dump_date);
568 if (converse(line) == -1) {
569 aclose(server_socket);
574 line = stralloc2("SCNF ", config);
575 if (converse(line) == -1) {
576 aclose(server_socket);
581 if (server_happy()) {
582 /* set host we are restoring to this host by default */
583 amfree(dump_hostname);
586 printf("Use the setdisk command to choose dump disk to recover\n");
588 printf("Use the sethost command to choose a host to recover\n");
594 if ((lineread = readline("amrecover> ")) == NULL) {
599 if (lineread[0] != '\0')
601 add_history(lineread);
602 process_line(lineread); /* act on line's content */
605 } while (!quit_prog);
609 aclose(server_socket);
617 security_handle_t *sech)
619 int ports[NSTREAMS], *response_error = datap, i;
624 assert(response_error != NULL);
625 assert(sech != NULL);
628 errstr = newvstralloc(errstr, "[request failed: ",
629 security_geterror(sech), "]", NULL);
634 if (pkt->type == P_NAK) {
635 #if defined(PACKET_DEBUG)
636 fprintf(stderr, "got nak response:\n----\n%s\n----\n\n", pkt->body);
639 tok = strtok(pkt->body, " ");
640 if (tok == NULL || strcmp(tok, "ERROR") != 0)
643 tok = strtok(NULL, "\n");
645 errstr = newvstralloc(errstr, "NAK: ", tok, NULL);
649 errstr = newstralloc(errstr, "request NAK");
655 if (pkt->type != P_REP) {
656 errstr = newvstralloc(errstr, "received strange packet type ",
657 pkt_type2str(pkt->type), ": ", pkt->body, NULL);
662 #if defined(PACKET_DEBUG)
663 fprintf(stderr, "got response:\n----\n%s\n----\n\n", pkt->body);
666 for(i = 0; i < NSTREAMS; i++) {
668 streams[i].fd = NULL;
672 while((tok = strtok(p, " \n")) != NULL) {
676 * Error response packets have "ERROR" followed by the error message
677 * followed by a newline.
679 if (strcmp(tok, "ERROR") == 0) {
680 tok = strtok(NULL, "\n");
682 tok = "[bogus error packet]";
683 errstr = newstralloc(errstr, tok);
690 * Regular packets have CONNECT followed by three streams
692 if (strcmp(tok, "CONNECT") == 0) {
695 * Parse the three stream specifiers out of the packet.
697 for (i = 0; i < NSTREAMS; i++) {
698 tok = strtok(NULL, " ");
699 if (tok == NULL || strcmp(tok, streams[i].name) != 0) {
700 extra = vstralloc("CONNECT token is \"",
701 tok ? tok : "(null)",
708 tok = strtok(NULL, " \n");
709 if (tok == NULL || sscanf(tok, "%d", &ports[i]) != 1) {
710 extra = vstralloc("CONNECT ",
713 tok ? tok : "(null)",
714 "\": expected a port number",
723 * OPTIONS [options string] '\n'
725 if (strcmp(tok, "OPTIONS") == 0) {
726 tok = strtok(NULL, "\n");
728 extra = stralloc("OPTIONS token is missing");
732 tok_end = tok + strlen(tok);
733 while((p = strchr(tok, ';')) != NULL) {
735 if(strncmp_const(tok, "features=") == 0) {
736 tok += SIZEOF("features=") - 1;
737 am_release_feature_set(their_features);
738 if((their_features = am_string_to_feature(tok)) == NULL) {
739 errstr = newvstralloc(errstr,
740 "OPTIONS: bad features value: ",
752 extra = vstralloc("next token is \"",
753 tok ? tok : "(null)",
754 "\": expected \"CONNECT\", \"ERROR\" or \"OPTIONS\"",
761 * Connect the streams to their remote ports
763 for (i = 0; i < NSTREAMS; i++) {
764 /*@i@*/ if (ports[i] == -1)
766 streams[i].fd = security_stream_client(sech, ports[i]);
767 if (streams[i].fd == NULL) {
768 errstr = newvstralloc(errstr,
769 "[could not connect ", streams[i].name, " stream: ",
770 security_geterror(sech), "]", NULL);
775 * Authenticate the streams
777 for (i = 0; i < NSTREAMS; i++) {
778 if (streams[i].fd == NULL)
780 if (security_stream_auth(streams[i].fd) < 0) {
781 errstr = newvstralloc(errstr,
782 "[could not authenticate ", streams[i].name, " stream: ",
783 security_stream_geterror(streams[i].fd), "]", NULL);
789 * The MESGFD and DATAFD streams are mandatory. If we didn't get
792 if (streams[MESGFD].fd == NULL) {
793 errstr = newstralloc(errstr, "[couldn't open MESG streams]");
797 /* everything worked */
803 errstr = newvstralloc(errstr,
804 "[parse of reply message failed: ",
805 extra ? extra : "(no additional information)",
818 * This is called when everything needs to shut down so event_loop()
827 for (i = 0; i < NSTREAMS; i++) {
828 if (streams[i].fd != NULL) {
829 security_stream_close(streams[i].fd);
830 streams[i].fd = NULL;
836 amindexd_client_get_security_conf(
840 (void)arg; /* Quiet unused parameter warning */
842 if(!string || !*string)
845 if(strcmp(string, "auth")==0) {
846 return(getconf_str(CNF_AUTH));
848 else if(strcmp(string, "ssh_keys")==0) {
849 return(getconf_str(CNF_SSH_KEYS));