pull last change pending from pre-git CVS tree
[debian/amanda] / recover-src / set_commands.c
index 21603abb51058423705355f121e6ba579e0fa03a..194e88d9243f9e7dc640d5cfbe48f562c8d7e903 100644 (file)
  * file named AUTHORS, in the root directory of this distribution.
  */
 /*
- * $Id: set_commands.c,v 1.11.2.3.4.2.2.7 2004/02/11 13:15:29 martinea Exp $
+ * $Id: set_commands.c,v 1.26 2006/07/05 13:14:58 martinea Exp $
  *
  * implements the "set" commands in amrecover
  */
 
 #include "amanda.h"
+#include "util.h"
 #include "amrecover.h"
 
 #ifdef SAMBA_CLIENT
@@ -37,10 +38,12 @@ extern unsigned short samba_extract_method;
 #endif /* SAMBA_CLIENT */
 
 /* sets a date, mapping given date into standard form if needed */
-int set_date(date)
-char *date;
+int
+set_date(
+    char *     date)
 {
     char *cmd = NULL;
+    char *qdisk_path;
 
     clear_dir_list();
 
@@ -52,7 +55,9 @@ char *date;
        is still valid at the new date, and if not set directory to
        mount_point */
     if (disk_path != NULL) {
-       cmd = newstralloc2(cmd, "OISD ", disk_path);
+       qdisk_path = quote_string(disk_path);
+       cmd = newstralloc2(cmd, "OISD ", qdisk_path);
+       amfree(qdisk_path);
        if (exchange(cmd) == -1)
            exit(1);
        if (server_happy())
@@ -68,18 +73,19 @@ char *date;
        }
     }
     amfree(cmd);
-
     return 0;
 }
 
 
-void set_host(host)
-char *host;
+void
+set_host(
+    const char *host)
 {
     char *cmd = NULL;
-    struct hostent *hp;
+    struct hostent *hp = NULL;
     char **hostp;
     int found_host = 0;
+    char *uqhost = unquote_string(host);
 
     if (is_extract_list_nonempty())
     {
@@ -87,48 +93,89 @@ char *host;
        return;
     }
 
-    cmd = stralloc2("HOST ", host);
+    /*
+     * The idea here is to try as many permutations of the hostname
+     * as we can imagine.  The server will reject anything it doesn't
+     * recognize.
+     */
+
+    cmd = stralloc2("HOST ", uqhost);
     if (converse(cmd) == -1)
        exit(1);
     if (server_happy())
-    {
        found_host = 1;
-    }
-    else
-    {
-       /*
-        * Try converting the given host to a fully qualified name
-        * and then try each of the aliases.
-        */
-       if ((hp = gethostbyname(host)) != NULL) {
+
+    /*
+     * Try converting the given host to a fully qualified, canonical
+     * name.
+     */
+    if (!found_host) {
+       if ((hp = gethostbyname(uqhost)) != NULL) {
            host = hp->h_name;
            printf("Trying host %s ...\n", host);
            cmd = newstralloc2(cmd, "HOST ", host);
            if (converse(cmd) == -1)
                exit(1);
            if(server_happy())
-           {
                found_host = 1;
-           }
-           else
+       }
+    }
+
+    /*
+     * Since we have them, try any CNAMEs that were traversed from uqhost
+     * to the canonical name (this assumes gethostbyname was called above)
+     */
+    if (!found_host) {
+       if (hp) {
+           for (hostp = hp->h_aliases; (host = *hostp) != NULL; hostp++)
            {
-               for (hostp = hp->h_aliases; (host = *hostp) != NULL; hostp++)
-               {
-                   printf("Trying host %s ...\n", host);
-                   cmd = newstralloc2(cmd, "HOST ", host);
-                   if (converse(cmd) == -1)
-                       exit(1);
-                   if(server_happy())
-                   {
-                       found_host = 1;
-                       break;
-                   }
+               printf(_("Trying host %s ...\n"), host);
+               cmd = newstralloc2(cmd, "HOST ", host);
+               if (converse(cmd) == -1)
+                   exit(1);
+               if(server_happy())
+               {
+                   found_host = 1;
+                   break;
                }
            }
        }
     }
-    if(found_host)
-    {
+
+    /*
+     * gethostbyname() will not return a canonical name for a host with no
+     * IPv4 addresses, so use getaddrinfo() (if supported)
+     */
+#ifdef WORKING_IPV6
+    if (!found_host) {
+       struct addrinfo hints;
+       struct addrinfo *gaires = NULL;
+       int res;
+
+       hints.ai_flags = AI_CANONNAME;
+       hints.ai_family = AF_UNSPEC;
+       hints.ai_socktype = 0;
+       hints.ai_protocol = 0;
+       hints.ai_addrlen = 0;
+       hints.ai_addr = NULL;
+       hints.ai_canonname = NULL;
+       hints.ai_next = NULL;
+       if ((res = getaddrinfo(uqhost, NULL, &hints, &gaires)) == 0) {
+           if (gaires && (host = gaires->ai_canonname)) {
+               printf(_("Trying host %s ...\n"), host);
+               cmd = newstralloc2(cmd, "HOST ", host);
+               if (converse(cmd) == -1)
+                   exit(1);
+               if(server_happy())
+                   found_host = 1;
+           }
+       }
+
+       if (gaires) freeaddrinfo(gaires);
+    }
+#endif
+
+    if(found_host) {
        dump_hostname = newstralloc(dump_hostname, host);
        amfree(disk_name);
        amfree(mount_point);
@@ -136,14 +183,29 @@ char *host;
        clear_dir_list();
     }
     amfree(cmd);
+    amfree(uqhost);
 }
 
+void
+list_host(void)
+{
+    char *cmd = NULL;
+
+    cmd = stralloc("LISTHOST");
+    if (converse(cmd) == -1)
+        exit(1);
+    amfree(cmd);
+}
 
-void set_disk(dsk, mtpt)
-char *dsk;
-char *mtpt;
+void
+set_disk(
+    char *     dsk,
+    char *     mtpt)
 {
     char *cmd = NULL;
+    char *qdsk;
+    char *uqdsk;
+    char *uqmtpt = NULL;
 
     if (is_extract_list_nonempty())
     {
@@ -152,14 +214,20 @@ char *mtpt;
     }
 
     /* if mount point specified, check it is valid */
-    if ((mtpt != NULL) && (*mtpt != '/'))
-    {
-       printf("Mount point \"%s\" invalid - must start with /\n", mtpt);
-       return;
+    if (mtpt != NULL) {
+       uqmtpt = unquote_string(mtpt);
+       if (*mtpt != '/') {
+           printf("Mount point \"%s\" invalid - must start with /\n", uqmtpt);
+           amfree(uqmtpt);
+           return;
+       }
     }
 
     clear_dir_list();
-    cmd = stralloc2("DISK ", dsk);
+    uqdsk = unquote_string(dsk);
+    qdsk = quote_string(uqdsk);
+    cmd = stralloc2("DISK ", qdsk);
+    amfree(qdsk);
     if (converse(cmd) == -1)
        exit(1);
     amfree(cmd);
@@ -167,14 +235,14 @@ char *mtpt;
     if (!server_happy())
        return;
 
-    disk_name = newstralloc(disk_name, dsk);
+    disk_name = newstralloc(disk_name, uqdsk);
     if (mtpt == NULL)
     {
        /* mount point not specified */
-       if (*dsk == '/')
+       if (*uqdsk == '/')
        {
            /* disk specified by mount point, hence use it */
-           mount_point = newstralloc(mount_point, dsk);
+           mount_point = newstralloc(mount_point, uqdsk);
        }
        else
        {
@@ -185,7 +253,7 @@ char *mtpt;
     else
     {
        /* mount point specified */
-       mount_point = newstralloc(mount_point, mtpt);
+       mount_point = newstralloc(mount_point, uqmtpt);
     }
 
     /* set the working directory to the mount point */
@@ -207,15 +275,23 @@ char *mtpt;
        disk_path = newstralloc(disk_path, "/");        /* fake it */
        clear_dir_list();
     }
+    amfree(uqmtpt);
+    amfree(uqdsk);
 }
 
-void list_disk(amdevice)
-char *amdevice;
+void
+list_disk(
+    char *     amdevice)
 {
     char *cmd = NULL;
+    char *qamdevice, *uqamdevice;
 
     if(amdevice) {
-       cmd = stralloc2("LISTDISK ", amdevice);
+       uqamdevice = unquote_string(amdevice);
+       qamdevice = quote_string(uqamdevice);
+       cmd = stralloc2("LISTDISK ", qamdevice);
+       amfree(uqamdevice);
+       amfree(qamdevice);
        if (converse(cmd) == -1)
            exit(1);
        amfree(cmd);
@@ -228,12 +304,25 @@ char *amdevice;
     }
 }
 
-void cd_glob(glob)
-char *glob;
+void
+local_cd(
+    char *dir)
+{
+    char *uqdir = unquote_string(dir);
+    if (chdir(uqdir) == -1) {
+       perror(uqdir);
+    }
+    amfree(uqdir);
+}
+
+void
+cd_glob(
+    char *     glob)
 {
     char *regex;
     char *regex_path;
     char *s;
+    char *uqglob;
 
     char *path_on_disk = NULL;
 
@@ -242,8 +331,9 @@ char *glob;
        return;
     }
 
-    regex = glob_to_regex(glob);
-    dbprintf(("cd_glob (%s) -> %s\n", glob, regex));
+    uqglob = unquote_string(glob);
+    regex = glob_to_regex(uqglob);
+    dbprintf(("cd_glob (%s) -> %s\n", uqglob, regex));
     if ((s = validate_regexp(regex)) != NULL) {
         printf("\"%s\" is not a valid shell wildcard pattern: ", glob);
         puts(s);
@@ -272,16 +362,19 @@ char *glob;
         amfree(clean_disk_path);
     }
 
-    cd_dir(path_on_disk, glob);
+    cd_dir(path_on_disk, uqglob);
 
     amfree(regex_path);
     amfree(path_on_disk);
+    amfree(uqglob);
 }
 
-void cd_regex(regex)
-char *regex;
+void
+cd_regex(
+    char *     regex)
 {
     char *s;
+    char *uqregex;
 
     char *path_on_disk = NULL;
 
@@ -290,8 +383,10 @@ char *regex;
        return;
     }
 
-    if ((s = validate_regexp(regex)) != NULL) {
-       printf("\"%s\" is not a valid regular expression: ", regex);
+    uqregex = unquote_string(regex);
+    if ((s = validate_regexp(uqregex)) != NULL) {
+       printf("\"%s\" is not a valid regular expression: ", uqregex);
+       amfree(uqregex);
        puts(s);
        return;
     }
@@ -305,17 +400,20 @@ char *regex;
         amfree(clean_disk_path);
     }
 
-    cd_dir(path_on_disk, regex);
+    cd_dir(path_on_disk, uqregex);
 
     amfree(path_on_disk);
+    amfree(uqregex);
 }
 
-void cd_dir(path_on_disk, default_dir)
-char *path_on_disk;
-char *default_dir;
+void
+cd_dir(
+    char *     path_on_disk,
+    char *     default_dir)
 {
     char *path_on_disk_slash = NULL;
     char *dir = NULL;
+    char *s;
 
     int nb_found;
     size_t i;
@@ -323,14 +421,21 @@ char *default_dir;
     DIR_ITEM *ditem;
 
     path_on_disk_slash = stralloc2(path_on_disk, "/");
+    if ((s = validate_regexp(path_on_disk_slash)) != NULL) {
+       amfree(path_on_disk_slash);
+    }
+
+    if ((s = validate_regexp(path_on_disk)) != NULL) {
+       path_on_disk = NULL;
+    }
 
     nb_found = 0;
 
     for (ditem=get_dir_list(); ditem!=NULL && nb_found <= 1; 
                               ditem=get_next_dir_item(ditem))
     {
-       if (match(path_on_disk, ditem->path)
-           || match(path_on_disk_slash, ditem->path))
+       if ((path_on_disk && match(path_on_disk, ditem->path))
+           || (path_on_disk_slash && match(path_on_disk_slash, ditem->path)))
        {
            i = strlen(ditem->path);
            if((i > 0 && ditem->path[i-1] == '/')
@@ -343,10 +448,12 @@ char *default_dir;
                    dir[strlen(dir)-1] = '\0'; /* remove last / */
                /* remove everything before the last / */
                dir1 = rindex(dir,'/');
-               dir1++;
-               dir2 = stralloc(dir1);
-               amfree(dir);
-               dir = dir2;
+               if (dir1) {
+                   dir1++;
+                   dir2 = stralloc(dir1);
+                   amfree(dir);
+                   dir = dir2;
+               }
            }
        }
     }
@@ -364,11 +471,13 @@ char *default_dir;
     amfree(dir);
 }
 
-void set_directory(dir)
-char *dir;
+void
+set_directory(
+    char *     dir)
 {
     char *cmd = NULL;
     char *new_dir = NULL;
+    char *qnew_dir;
     char *dp, *de;
     char *ldir = NULL;
 
@@ -376,11 +485,13 @@ char *dir;
     if(strcmp(dir,".")==0) {
        show_directory();               /* say where we are */
        return;
+       /*NOTREACHED*/
     }
 
     if (disk_name == NULL) {
        printf("Must select disk before setting directory\n");
        return;
+       /*NOTREACHED*/
     }
 
     ldir = stralloc(dir);
@@ -402,6 +513,7 @@ char *dir;
                       mount_point);
                amfree(ldir);
                return;
+               /*NOTREACHED*/
            }
            new_dir = stralloc(ldir+strlen(mount_point));
            if (strlen(new_dir) == 0) {
@@ -435,9 +547,12 @@ char *dir;
                /* at top of disk */
                printf("Invalid directory - Can't cd outside mount point \"%s\"\n",
                       mount_point);
+               /*@ignore@*/
                amfree(new_dir);
+               /*@end@*/
                amfree(ldir);
                return;
+               /*NOTREACHED*/
            }
            de = strrchr(new_dir, '/'); /* always at least 1 */
            if (de == new_dir)
@@ -450,17 +565,24 @@ char *dir;
                *de = '\0';
            }
        } else {
+           /*@ignore@*/
            if (strcmp(new_dir, "/") != 0) {
                strappend(new_dir, "/");
            }
            strappend(new_dir, ldir);
+           /*@end@*/
        }
     }
 
-    cmd = stralloc2("OISD ", new_dir);
-    if (exchange(cmd) == -1)
+    qnew_dir = quote_string(new_dir);
+    cmd = stralloc2("OISD ", qnew_dir);
+    amfree(qnew_dir);
+    if (exchange(cmd) == -1) {
        exit(1);
+       /*NOTREACHED*/
+    }
     amfree(cmd);
+
     if (server_happy())
     {
        disk_path = newstralloc(disk_path, new_dir);
@@ -472,13 +594,16 @@ char *dir;
        printf("Invalid directory - %s\n", dir);
     }
 
+    /*@ignore@*/
     amfree(new_dir);
     amfree(ldir);
+    /*@end@*/
 }
 
 
 /* prints the current working directory */
-void show_directory P((void))
+void
+show_directory(void)
 {
     if (mount_point == NULL || disk_path == NULL)
         printf("Must select disk first\n");
@@ -492,24 +617,26 @@ void show_directory P((void))
 
 
 /* set the tape server and device */
-void set_tape (tape)
-    char *tape;
+void
+set_tape(
+    char *     tape)
 {
-    char *tapedev = strchr(tape, ':');
+    char *uqtape = unquote_string(tape);
+    char *tapedev = strchr(uqtape, ':');
 
     if (tapedev)
     {
-       if (tapedev != tape) {
+       if (tapedev != uqtape) {
            if((strchr(tapedev+1, ':') == NULL) &&
-              (strncmp(tape, "null:", 5) == 0 ||
-               strncmp(tape, "rait:", 5) == 0 ||
-               strncmp(tape, "file:", 5) == 0 ||
-               strncmp(tape, "tape:", 5) == 0)) {
-               tapedev = tape;
+              (strncmp(uqtape, "null:", 5) == 0 ||
+               strncmp(uqtape, "rait:", 5) == 0 ||
+               strncmp(uqtape, "file:", 5) == 0 ||
+               strncmp(uqtape, "tape:", 5) == 0)) {
+               tapedev = uqtape;
            }
            else {
                *tapedev = '\0';
-               tape_server_name = newstralloc(tape_server_name, tape);
+               tape_server_name = newstralloc(tape_server_name, uqtape);
                ++tapedev;
            }
        } else { /* reset server_name if start with : */
@@ -517,7 +644,7 @@ void set_tape (tape)
            ++tapedev;
        }
     } else
-       tapedev = tape;
+       tapedev = uqtape;
     
     if (tapedev[0])
     {
@@ -539,8 +666,9 @@ void set_tape (tape)
                server_name);
 }
 
-void set_mode (mode)
-int mode;
+void
+set_mode(
+    int                mode)
 {
 #ifdef SAMBA_CLIENT
   if (mode == SAMBA_SMBCLIENT) {
@@ -552,10 +680,13 @@ int mode;
       samba_extract_method = SAMBA_TAR;
     }
   }
+#else
+  (void)mode;  /* Quiet unused parameter warning */
 #endif /* SAMBA_CLIENT */
 }
 
-void show_mode (void) 
+void
+show_mode(void) 
 {
 #ifdef SAMBA_CLIENT
   printf ("SAMBA dumps are extracted ");