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.52 2006/01/14 04:37:19 paddy_s Exp $
29 * an interactive program for recovering backed-up files
34 #ifdef HAVE_NETINET_IN_SYSTM_H
35 #include <netinet/in_systm.h>
37 #include <netinet/in.h>
38 #ifdef HAVE_NETINET_IP_H
39 #include <netinet/ip.h>
42 #include "amfeatures.h"
43 #include "amrecover.h"
48 #ifdef HAVE_LIBREADLINE
49 # ifdef HAVE_READLINE_READLINE_H
50 # include <readline/readline.h>
51 # ifdef HAVE_READLINE_HISTORY_H
52 # include <readline/history.h>
55 # ifdef HAVE_READLINE_H
56 # include <readline.h>
57 # ifdef HAVE_HISTORY_H
61 # undef HAVE_LIBREADLINE
66 extern int process_line P((char *line));
67 int guess_disk P((char *cwd, size_t cwd_len, char **dn_guess, char **mpt_guess));
69 #define USAGE "Usage: amrecover [[-C] <config>] [-s <index-server>] [-t <tape-server>] [-d <tape-device>]\n"
72 char *server_name = NULL;
74 char *server_line = NULL;
75 char *dump_datestamp = NULL; /* date we are restoring */
76 char *dump_hostname; /* which machine we are restoring */
77 char *disk_name = NULL; /* disk we are restoring */
78 char *mount_point = NULL; /* where disk was mounted */
79 char *disk_path = NULL; /* path relative to mount point */
80 char dump_date[STR_SIZE]; /* date on which we are restoring */
81 int quit_prog; /* set when time to exit parser */
82 char *tape_server_name = NULL;
83 int tape_server_socket;
84 char *tape_device_name = NULL;
85 am_feature_t *our_features = NULL;
86 am_feature_t *indexsrv_features = NULL;
87 am_feature_t *tapesrv_features = NULL;
90 #ifndef HAVE_LIBREADLINE
92 * simple readline() replacements
100 fflush(stdout); fflush(stderr);
104 #define add_history(x) /* add_history((x)) */
108 /* gets a "line" from server and put in server_line */
109 /* server_line is terminated with \0, \r\n is striped */
110 /* returns -1 if error */
119 if((part = areads(server_socket)) == NULL) {
120 int save_errno = errno;
123 fputs(server_line, stderr); /* show the last line read */
126 if(save_errno != 0) {
127 fprintf(stderr, "%s: Error reading line from server: %s\n",
129 strerror(save_errno));
131 fprintf(stderr, "%s: Unexpected end of file, check amindexd*debug on server %s\n",
141 strappend(line, part);
147 if((len = strlen(line)) > 0 && line[len-1] == '\r') {
149 server_line = newstralloc(server_line, line);
154 * Hmmm. We got a "line" from areads(), which means it saw
155 * a '\n' (or EOF, etc), but there was not a '\r' before it.
156 * Put a '\n' back in the buffer and loop for more.
158 strappend(line, "\n");
163 /* get reply from server and print to screen */
164 /* handle multi-line reply */
165 /* return -1 if error */
166 /* return code returned by server always occupies first 3 bytes of global
167 variable server_line */
168 int grab_reply (show)
172 if (get_line() == -1) {
175 if(show) puts(server_line);
176 } while (server_line[3] == '-');
177 if(show) fflush(stdout);
183 /* get 1 line of reply */
184 /* returns -1 if error, 0 if last (or only) line, 1 if more to follow */
185 int get_reply_line ()
187 if (get_line() == -1)
189 return server_line[3] == '-';
193 /* returns pointer to returned line */
201 /* returns 0 if server returned an error code (ie code starting with 5)
202 and non-zero otherwise */
205 return server_line[0] != '5';
209 int send_command(cmd)
213 * NOTE: this routine is called from sigint_handler, so we must be
214 * **very** careful about what we do since there is no way to know
215 * our state at the time the interrupt happened. For instance,
216 * do not use any stdio or malloc routines here.
221 msg[0].iov_base = cmd;
222 msg[0].iov_len = strlen(msg[0].iov_base);
223 msg[1].iov_base = "\r\n";
224 msg[1].iov_len = strlen(msg[1].iov_base);
225 bytes = msg[0].iov_len + msg[1].iov_len;
227 if (writev(server_socket, msg, 2) < bytes) {
234 /* send a command to the server, get reply and print to screen */
238 if (send_command(cmd) == -1) return -1;
239 if (grab_reply(1) == -1) return -1;
244 /* same as converse() but reply not echoed to stdout */
248 if (send_command(cmd) == -1) return -1;
249 if (grab_reply(0) == -1) return -1;
254 /* basic interrupt handler for when user presses ^C */
255 /* Bale out, letting server know before doing so */
256 void sigint_handler(signum)
260 * NOTE: we must be **very** careful about what we do here since there
261 * is no way to know our state at the time the interrupt happened.
262 * For instance, do not use any stdio routines here or in any called
263 * routines. Also, use _exit() instead of exit() to make sure stdio
264 * buffer flushing is not attempted.
266 if (extract_restore_child_pid != -1)
267 (void)kill(extract_restore_child_pid, SIGKILL);
268 extract_restore_child_pid = -1;
270 (void)send_command("QUIT");
275 void clean_pathname(s)
281 /* remove "/" at end of path */
282 if(length>1 && s[length-1]=='/')
285 /* change "/." to "/" */
286 if(strcmp(s,"/.")==0)
289 /* remove "/." at end of path */
290 if(strcmp(&(s[length-2]),"/.")==0)
295 /* try and guess the disk the user is currently on.
296 Return -1 if error, 0 if disk not local, 1 if disk local,
297 2 if disk local but can't guess name */
298 /* do this by looking for the longest mount point which matches the
300 int guess_disk (cwd, cwd_len, dn_guess, mpt_guess)
301 char *cwd, **dn_guess, **mpt_guess;
304 size_t longest_match = 0;
305 size_t current_length;
308 generic_fsent_t fsent;
310 char *disk_try = NULL;
312 *dn_guess = *mpt_guess = NULL;
314 if (getcwd(cwd, cwd_len) == NULL)
316 cwd_length = strlen(cwd);
317 dbprintf(("guess_disk: %d: \"%s\"\n", cwd_length, cwd));
319 if (open_fstab() == 0)
322 while (get_fstab_nextentry(&fsent))
324 current_length = fsent.mntdir ? strlen(fsent.mntdir) : (size_t)0;
325 dbprintf(("guess_disk: %d: %d: \"%s\": \"%s\"\n",
328 fsent.mntdir ? fsent.mntdir : "(mntdir null)",
329 fsent.fsname ? fsent.fsname : "(fsname null)"));
330 if ((current_length > longest_match)
331 && (current_length <= cwd_length)
332 && (strncmp(fsent.mntdir, cwd, current_length) == 0))
334 longest_match = current_length;
336 *mpt_guess = stralloc(fsent.mntdir);
337 if(strncmp(fsent.fsname,DEV_PREFIX,(strlen(DEV_PREFIX))))
339 fsname = newstralloc(fsname, fsent.fsname);
343 fsname = newstralloc(fsname,fsent.fsname+strlen(DEV_PREFIX));
345 local_disk = is_local_fstype(&fsent);
346 dbprintf(("guess_disk: local_disk = %d, fsname = \"%s\"\n",
353 if (longest_match == 0) {
356 return -1; /* ? at least / should match */
365 /* have mount point now */
366 /* disk name may be specified by mount point (logical name) or
367 device name, have to determine */
368 printf("Trying disk %s ...\n", *mpt_guess);
369 disk_try = stralloc2("DISK ", *mpt_guess); /* try logical name */
370 if (exchange(disk_try) == -1)
375 *dn_guess = stralloc(*mpt_guess); /* logical is okay */
379 printf("Trying disk %s ...\n", fsname);
380 disk_try = stralloc2("DISK ", fsname); /* try device name */
381 if (exchange(disk_try) == -1)
386 *dn_guess = stralloc(fsname); /* dev name is okay */
391 /* neither is okay */
401 (void)converse("QUIT");
404 char *localhost = NULL;
414 char *lineread = NULL;
415 struct sigaction act, oact;
418 char cwd[STR_SIZE], *dn_guess = NULL, *mpt_guess = NULL;
424 set_pname("amrecover");
426 /* Don't die when child closes pipe */
427 signal(SIGPIPE, SIG_IGN);
431 #ifndef IGNORE_UID_CHECK
432 if (geteuid() != 0) {
433 erroutput_type |= ERR_SYSLOG;
434 error("amrecover must be run by root");
438 localhost = alloc(MAX_HOSTNAME_LENGTH+1);
439 if (gethostname(localhost, MAX_HOSTNAME_LENGTH) != 0) {
440 error("cannot determine local host name\n");
442 localhost[MAX_HOSTNAME_LENGTH] = '\0';
444 config = newstralloc(config, DEFAULT_CONFIG);
445 server_name = newstralloc(server_name, DEFAULT_SERVER);
446 #ifdef DEFAULT_TAPE_SERVER
447 tape_server_name = newstralloc(tape_server_name, DEFAULT_TAPE_SERVER);
449 amfree(tape_server_name);
451 if (argc > 1 && argv[1][0] != '-')
454 * If the first argument is not an option flag, then we assume
455 * it is a configuration name to match the syntax of the other
460 new_argv = (char **) alloc ((argc + 1 + 1) * sizeof (*new_argv));
461 new_argv[0] = argv[0];
463 for (i = 1; i < argc; i++)
465 new_argv[i + 1] = argv[i];
467 new_argv[i + 1] = NULL;
471 while ((i = getopt(argc, argv, "C:s:t:d:U")) != EOF)
476 config = newstralloc(config, optarg);
480 server_name = newstralloc(server_name, optarg);
484 tape_server_name = newstralloc(tape_server_name, optarg);
488 tape_device_name = newstralloc(tape_device_name, optarg);
499 (void)fprintf(stderr, USAGE);
508 /* Don't die when child closes pipe */
509 signal(SIGPIPE, SIG_IGN);
511 /* set up signal handler */
512 act.sa_handler = sigint_handler;
513 sigemptyset(&act.sa_mask);
515 if (sigaction(SIGINT, &act, &oact) != 0)
517 error("error setting signal handler: %s", strerror(errno));
520 service_name = stralloc2("amandaidx", SERVICE_SUFFIX);
522 printf("AMRECOVER Version %s. Contacting server on %s ...\n",
523 version(), server_name);
524 if ((sp = getservbyname(service_name, "tcp")) == NULL)
526 error("%s/tcp unknown protocol", service_name);
528 amfree(service_name);
529 server_socket = stream_client_privileged(server_name,
535 if (server_socket < 0)
537 error("cannot connect to %s: %s", server_name, strerror(errno));
539 if (my_port >= IPPORT_RESERVED)
541 error("did not get a reserved port: %d", my_port);
546 * We may need root privilege again later for a reserved port to
547 * the tape server, so we will drop down now but might have to
554 /* get server's banner */
555 if (grab_reply(1) == -1)
560 aclose(server_socket);
564 /* do the security thing */
565 line = get_security();
566 if (converse(line) == -1)
570 memset(line, '\0', strlen(line));
573 /* try to get the features from the server */
575 char *our_feature_string = NULL;
576 char *their_feature_string = NULL;
578 our_features = am_init_feature_set();
579 our_feature_string = am_feature_to_string(our_features);
580 line = stralloc2("FEATURES ", our_feature_string);
581 if(exchange(line) == 0) {
582 their_feature_string = stralloc(server_line+13);
583 indexsrv_features = am_string_to_feature(their_feature_string);
586 indexsrv_features = am_set_default_feature_set();
588 amfree(our_feature_string);
589 amfree(their_feature_string);
593 /* set the date of extraction to be today */
595 strftime(dump_date, sizeof(dump_date), "%Y-%m-%d", localtime(&timer));
596 printf("Setting restore date to today (%s)\n", dump_date);
597 line = stralloc2("DATE ", dump_date);
598 if (converse(line) == -1)
602 line = stralloc2("SCNF ", config);
603 if (converse(line) == -1)
609 /* set host we are restoring to this host by default */
610 amfree(dump_hostname);
614 /* get a starting disk and directory based on where
616 switch (guess_disk(cwd, sizeof(cwd), &dn_guess, &mpt_guess))
619 /* okay, got a guess. Set disk accordingly */
620 printf("$CWD '%s' is on disk '%s' mounted at '%s'.\n",
621 cwd, dn_guess, mpt_guess);
622 set_disk(dn_guess, mpt_guess);
624 if (server_happy() && strcmp(cwd, mpt_guess) != 0)
625 printf("WARNING: not on root of selected filesystem, check man-page!\n");
631 printf("$CWD '%s' is on a network mounted disk\n",
633 printf("so you must 'sethost' to the server\n");
634 /* fake an unhappy server */
635 server_line[0] = '5';
641 printf("Can't determine disk and mount point from $CWD '%s'\n", cwd);
642 /* fake an unhappy server */
643 server_line[0] = '5';
652 if ((lineread = readline("amrecover> ")) == NULL) {
657 if (lineread[0] != '\0')
659 add_history(lineread);
660 process_line(lineread); /* act on line's content */
663 } while (!quit_prog);
667 aclose(server_socket);
674 struct passwd *pwptr;
676 if((pwptr = getpwuid(getuid())) == NULL)
677 error("can't get login name for my uid %ld", (long)getuid());
678 return stralloc2("SECURITY USER ", pwptr->pw_name);