]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/ua_restore.c
Fix setip crash + missing unlocks()+cleanups
[bacula/bacula] / bacula / src / dird / ua_restore.c
index 9b4d29a262520eb0a0f17de8a32fb5e7aeafcd77..14764bbbfaca8093500cc41ec4275a3139e33da1 100644 (file)
@@ -45,9 +45,9 @@ extern void print_bsr(UAContext *ua, RBSR *bsr);
 extern char *uar_list_jobs,     *uar_file,        *uar_sel_files;
 extern char *uar_del_temp,      *uar_del_temp1,   *uar_create_temp;
 extern char *uar_create_temp1,  *uar_last_full,   *uar_full;
-extern char *uar_inc_dec,       *uar_list_temp,   *uar_sel_jobid_temp;
+extern char *uar_inc,           *uar_list_temp,   *uar_sel_jobid_temp;
 extern char *uar_sel_all_temp1,  *uar_sel_fileset, *uar_mediatype;
-extern char *uar_jobid_fileindex;
+extern char *uar_jobid_fileindex, *uar_dif,       *uar_sel_all_temp;
 
 
 struct NAME_LIST {
@@ -74,11 +74,11 @@ struct RESTORE_CTX {
    uint32_t selected_files;
    char *where;
    RBSR *bsr;
-   POOLMEM *fname;
-   POOLMEM *path;
+   POOLMEM *fname;                   /* filename only */
+   POOLMEM *path;                    /* path only */
    POOLMEM *query;
-   int fnl;
-   int pnl;
+   int fnl;                          /* filename length */
+   int pnl;                          /* path length */
    bool found;
    NAME_LIST name_list;
 };
@@ -90,7 +90,7 @@ struct RESTORE_CTX {
 /* Forward referenced functions */
 static int last_full_handler(void *ctx, int num_fields, char **row);
 static int jobid_handler(void *ctx, int num_fields, char **row);
-static int next_jobid_from_list(char **p, uint32_t *JobId);
+static int get_next_jobid_from_list(char **p, uint32_t *JobId);
 static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx);
 static int fileset_handler(void *ctx, int num_fields, char **row);
 static void print_name_list(UAContext *ua, NAME_LIST *name_list);
@@ -98,7 +98,7 @@ static int unique_name_list_handler(void *ctx, int num_fields, char **row);
 static void free_name_list(NAME_LIST *name_list);
 static void get_storage_from_mediatype(UAContext *ua, NAME_LIST *name_list, RESTORE_CTX *rx);
 static int select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *date);
-static void build_directory_tree(UAContext *ua, RESTORE_CTX *rx);
+static bool build_directory_tree(UAContext *ua, RESTORE_CTX *rx);
 static void free_rx(RESTORE_CTX *rx);
 static void split_path_and_filename(RESTORE_CTX *rx, char *fname);
 static int jobid_fileindex_handler(void *ctx, int num_fields, char **row);
@@ -115,11 +115,10 @@ static int get_date(UAContext *ua, char *date, int date_len);
 int restore_cmd(UAContext *ua, char *cmd)
 {
    RESTORE_CTX rx;                   /* restore context */
-   JOB *job = NULL;
+   JOB *job;
    int i;
 
    memset(&rx, 0, sizeof(rx));
-
    rx.path = get_pool_memory(PM_FNAME);
    rx.fname = get_pool_memory(PM_FNAME);
    rx.JobIds = get_pool_memory(PM_FNAME);
@@ -137,7 +136,7 @@ int restore_cmd(UAContext *ua, char *cmd)
 
    /* Ensure there is at least one Restore Job */
    LockRes();
-   while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
+   foreach_res(job, R_JOB) {
       if (job->JobType == JT_RESTORE) {
         if (!rx.restore_job) {
            rx.restore_job = job;
@@ -163,7 +162,10 @@ int restore_cmd(UAContext *ua, char *cmd)
    case 0:
       goto bail_out;
    case 1:                           /* select by jobid */
-      build_directory_tree(ua, &rx);
+      if (!build_directory_tree(ua, &rx)) {
+         bsendmsg(ua, _("Restore not done.\n"));
+        goto bail_out;
+      }
       break;
    case 2:                           /* select by filename, no tree needed */
       break;
@@ -201,17 +203,17 @@ int restore_cmd(UAContext *ua, char *cmd)
    if (rx.where) {
       Mmsg(&ua->cmd, 
           "run job=\"%s\" client=\"%s\" storage=\"%s\" bootstrap=\"%s/restore.bsr\""
-          " where=\"%s\"",
+          " where=\"%s\" files=%d",
           job->hdr.name, rx.ClientName, rx.store?rx.store->hdr.name:"",
-         working_directory, rx.where);
+         working_directory, rx.where, rx.selected_files);
    } else {
       Mmsg(&ua->cmd, 
           "run job=\"%s\" client=\"%s\" storage=\"%s\" bootstrap=\"%s/restore.bsr\"",
           job->hdr.name, rx.ClientName, rx.store?rx.store->hdr.name:"",
          working_directory);
    }
-   if (find_arg(ua, _("run")) >= 0) {
-      pm_strcat(&ua->cmd, " run");    /* pass it on to the run command */
+   if (find_arg(ua, _("yes")) > 0) {
+      pm_strcat(&ua->cmd, " yes");    /* pass it on to the run command */
    }
    Dmsg1(400, "Submitting: %s\n", ua->cmd);
    parse_ua_args(ua);
@@ -283,11 +285,11 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx)
    JobId_t JobId;
    JOB_DBR jr;
    bool done = false;
-   int i;
+   int i, j;
    char *list[] = { 
       "List last 20 Jobs run",
       "List Jobs where a given File is saved",
-      "Enter list of JobIds to select",
+      "Enter list of comma separated JobIds to select",
       "Enter SQL list command", 
       "Select the most recent backup for a client",
       "Select backup for a client before a specified time",
@@ -303,76 +305,94 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx)
       "file",      /* 3 */
       "select",    /* 4 */
       "pool",      /* 5 */
+      "client",    /* 6 */
+      "storage",   /* 7 */
+      "where",     /* 8 */
+      "all",       /* 9 */
+      "yes",       /* 10 */
       NULL
    };
 
    *rx->JobIds = 0;
-   switch (find_arg_keyword(ua, kw)) {
-   case 0:                           /* jobid */
-      for ( ;; ) {
-         i = find_arg_with_value(ua, _("jobid"));
-        if (i < 0) {
+
+   for (i=1; i<ua->argc; i++) {       /* loop through arguments */
+      bool found_kw = false;
+      for (j=0; kw[j]; j++) {        /* loop through keywords */
+        if (strcasecmp(kw[j], ua->argk[i]) == 0) {
+           found_kw = true;
            break;
         }
-        pm_strcpy(&rx->JobIds, ua->argv[i]);
-         ua->argk[i][0] = 0;          /* "consume" jobid= */
-      }
-      done = true;
-      break;
-   case 1:                           /* current */
-      bstrutime(date, sizeof(date), time(NULL));
-      have_date = true;
-      break;
-   case 2:                           /* before */
-      i = find_arg_with_value(ua, _("before"));
-      if (i < 0) {
-        return 0;
       }
-      if (str_to_utime(ua->argv[i]) == 0) {
-         bsendmsg(ua, _("Improper date format: %s\n"), ua->argv[i]);
+      if (!found_kw) {
+         bsendmsg(ua, _("Unknown keyword: %s\n"), ua->argk[i]);
         return 0;
       }
-      bstrncpy(date, ua->argv[i], sizeof(date));
-      have_date = true;
-      break;
-   case 3:                           /* file */
-      if (!have_date) {
+      /* Found keyword in kw[] list, process it */
+      switch (j) {
+      case 0:                           /* jobid */
+        if (*rx->JobIds != 0) {
+            pm_strcat(&rx->JobIds, ",");
+        }
+        pm_strcat(&rx->JobIds, ua->argv[i]);
+        done = true;
+        break;
+      case 1:                           /* current */
         bstrutime(date, sizeof(date), time(NULL));
-      }
-      if (!get_client_name(ua, rx)) {
-        return 0;
-      }
-      for ( ;; ) {
-         i = find_arg_with_value(ua, _("file"));
-        if (i < 0) {
-           break;
+        have_date = true;
+        break;
+      case 2:                           /* before */
+        if (str_to_utime(ua->argv[i]) == 0) {
+            bsendmsg(ua, _("Improper date format: %s\n"), ua->argv[i]);
+           return 0;
+        }
+        bstrncpy(date, ua->argv[i], sizeof(date));
+        have_date = true;
+        break;
+      case 3:                           /* file */
+        if (!have_date) {
+           bstrutime(date, sizeof(date), time(NULL));
+        }
+        if (!get_client_name(ua, rx)) {
+           return 0;
         }
         pm_strcpy(&ua->cmd, ua->argv[i]);
         insert_one_file(ua, rx, date);
-         ua->argk[i][0] = 0;          /* "consume" the file= */
-      }
-      /* Check MediaType and select storage that corresponds */
-      get_storage_from_mediatype(ua, &rx->name_list, rx);
-      return 2;
-   case 4:                           /* select */
-      if (!have_date) {
-        bstrutime(date, sizeof(date), time(NULL));
-      }
-      if (!select_backups_before_date(ua, rx, date)) {
-        return 0;
-      }
-      done = true;
-      break;
-   case 5:                           /* pool specified */
-      i = find_arg_with_value(ua, "pool");
-      if (i >= 0) {
+        if (rx->name_list.num_ids) {
+           /* Check MediaType and select storage that corresponds */
+           get_storage_from_mediatype(ua, &rx->name_list, rx);
+           done = true;
+        }
+        break;
+      case 4:                           /* select */
+        if (!have_date) {
+           bstrutime(date, sizeof(date), time(NULL));
+        }
+        if (!select_backups_before_date(ua, rx, date)) {
+           return 0;
+        }
+        done = true;
+        break;
+      case 5:                           /* pool specified */
         rx->pool = (POOL *)GetResWithName(R_POOL, ua->argv[i]);
-      } else {
-         bsendmsg(ua, _("Error: Pool resource \"%s\" does not exist.\n"), ua->argv[i]);
+        if (!rx->pool) {
+            bsendmsg(ua, _("Error: Pool resource \"%s\" does not exist.\n"), ua->argv[i]);
+           return 0;
+        }
+        if (!acl_access_ok(ua, Pool_ACL, ua->argv[i])) {
+           rx->pool = NULL;
+            bsendmsg(ua, _("Error: Pool resource \"%s\" access not allowed.\n"), ua->argv[i]);
+           return 0;
+        }
+        break;
+      /*     
+       * All keywords 6 or greater are ignored or handled by a select prompt
+       */
+      default:
+        break;
       }
-      break;
-   default:
-      break;
+   }
+   if (rx->name_list.num_ids) {
+      return 2;                      /* filename list made */
    }
        
    if (!done) {
@@ -457,7 +477,9 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx)
            insert_one_file(ua, rx, date);
         }
         /* Check MediaType and select storage that corresponds */
-        get_storage_from_mediatype(ua, &rx->name_list, rx);
+        if (rx->name_list.num_ids) {
+           get_storage_from_mediatype(ua, &rx->name_list, rx);
+        }
         return 2;
        case 7:                       /* enter files backed up before specified time */
         if (!get_date(ua, date, sizeof(date))) {
@@ -480,7 +502,9 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx)
            insert_one_file(ua, rx, date);
         }
         /* Check MediaType and select storage that corresponds */
-        get_storage_from_mediatype(ua, &rx->name_list, rx);
+        if (rx->name_list.num_ids) {
+           get_storage_from_mediatype(ua, &rx->name_list, rx);
+        }
         return 2;
 
       
@@ -500,7 +524,7 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx)
 
    rx->TotalFiles = 0;
    for (p=rx->JobIds; ; ) {
-      int stat = next_jobid_from_list(&p, &JobId);
+      int stat = get_next_jobid_from_list(&p, &JobId);
       if (stat < 0) {
          bsendmsg(ua, _("Invalid JobId in list.\n"));
         return 0;
@@ -517,6 +541,11 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx)
            JobId, db_strerror(ua->db));
         return 0;
       }
+      if (!acl_access_ok(ua, Job_ACL, jr.Name)) {
+         bsendmsg(ua, _("No authorization. Job \"%s\" not selected.\n"), 
+           jr.Name);
+        continue; 
+      }
       rx->TotalFiles += jr.JobFiles;
    }
    return 1;
@@ -658,12 +687,13 @@ static void split_path_and_filename(RESTORE_CTX *rx, char *name)
    Dmsg2(100, "sllit path=%s file=%s\n", rx->path, rx->fname);
 }
 
-static void build_directory_tree(UAContext *ua, RESTORE_CTX *rx)
+static bool build_directory_tree(UAContext *ua, RESTORE_CTX *rx)
 {
    TREE_CTX tree;
    JobId_t JobId, last_JobId;
    char *p;
    char *nofname = "";
+   bool OK = true;
 
    memset(&tree, 0, sizeof(TREE_CTX));
    /* 
@@ -678,7 +708,7 @@ static void build_directory_tree(UAContext *ua, RESTORE_CTX *rx)
     * appear more than once, however, we only insert it once.
     */
    int items = 0;
-   for (p=rx->JobIds; next_jobid_from_list(&p, &JobId) > 0; ) {
+   for (p=rx->JobIds; get_next_jobid_from_list(&p, &JobId) > 0; ) {
 
       if (JobId == last_JobId) {            
         continue;                    /* eliminate duplicate JobIds */
@@ -708,24 +738,27 @@ static void build_directory_tree(UAContext *ua, RESTORE_CTX *rx)
    get_storage_from_mediatype(ua, &rx->name_list, rx);
 
    if (find_arg(ua, _("all")) < 0) {
-      /* Let the user select which files to restore */
-      user_select_files_from_tree(&tree);
+      /* Let the user interact in selecting which files to restore */
+      OK = user_select_files_from_tree(&tree);
    }
 
    /*
     * Walk down through the tree finding all files marked to be 
     *  extracted making a bootstrap file.
     */
-   for (TREE_NODE *node=first_tree_node(tree.root); node; node=next_tree_node(node)) {
-      Dmsg2(400, "FI=%d node=0x%x\n", node->FileIndex, node);
-      if (node->extract) {
-         Dmsg2(400, "type=%d FI=%d\n", node->type, node->FileIndex);
-        add_findex(rx->bsr, node->JobId, node->FileIndex);
-        rx->selected_files++;
+   if (OK) {
+      for (TREE_NODE *node=first_tree_node(tree.root); node; node=next_tree_node(node)) {
+         Dmsg2(400, "FI=%d node=0x%x\n", node->FileIndex, node);
+        if (node->extract || node->extract_dir) {
+            Dmsg2(400, "type=%d FI=%d\n", node->type, node->FileIndex);
+           add_findex(rx->bsr, node->JobId, node->FileIndex);
+           rx->selected_files++;
+        }
       }
    }
 
    free_tree(tree.root);             /* free the directory tree */
+   return OK;
 }
 
 
@@ -832,8 +865,24 @@ static int select_backups_before_date(UAContext *ua, RESTORE_CTX *rx, char *date
       goto bail_out;
    }
 
-   /* Now find all Incremental/Decremental Jobs after Full save */
-   Mmsg(&rx->query, uar_inc_dec, edit_uint64(rx->JobTDate, ed1), date,
+   /* Now find most recent Differental Job after Full save, if any */
+   Mmsg(&rx->query, uar_dif, edit_uint64(rx->JobTDate, ed1), date,
+       cr.ClientId, fsr.FileSet, pool_select);
+   if (!db_sql_query(ua->db, rx->query, NULL, NULL)) {
+      bsendmsg(ua, "%s\n", db_strerror(ua->db));
+   }
+   /* Now update JobTDate to lock onto Differental, if any */
+   rx->JobTDate = 0;
+   if (!db_sql_query(ua->db, uar_sel_all_temp, last_full_handler, (void *)rx)) {
+      bsendmsg(ua, "%s\n", db_strerror(ua->db));
+   }
+   if (rx->JobTDate == 0) {
+      bsendmsg(ua, _("No Full backup before %s found.\n"), date);
+      goto bail_out;
+   }
+
+   /* Now find all Incremental Jobs after Full/dif save */
+   Mmsg(&rx->query, uar_inc, edit_uint64(rx->JobTDate, ed1), date,
        cr.ClientId, fsr.FileSet, pool_select);
    if (!db_sql_query(ua->db, rx->query, NULL, NULL)) {
       bsendmsg(ua, "%s\n", db_strerror(ua->db));
@@ -863,7 +912,7 @@ bail_out:
 
 
 /* Return next JobId from comma separated list */
-static int next_jobid_from_list(char **p, uint32_t *JobId)
+static int get_next_jobid_from_list(char **p, uint32_t *JobId)
 {
    char jobid[30];
    char *q = *p;
@@ -1001,6 +1050,8 @@ static void free_name_list(NAME_LIST *name_list)
 
 static void get_storage_from_mediatype(UAContext *ua, NAME_LIST *name_list, RESTORE_CTX *rx)
 {
+   STORE *store;
+
    if (name_list->num_ids > 1) {
       bsendmsg(ua, _("Warning, the JobIds that you selected refer to more than one MediaType.\n"
          "Restore is not possible. The MediaTypes used are:\n"));
@@ -1014,12 +1065,28 @@ static void get_storage_from_mediatype(UAContext *ua, NAME_LIST *name_list, REST
       rx->store = select_storage_resource(ua);
       return;
    }
+   if (rx->store) {
+      return;
+   }
+   /*
+    * We have a single MediaType, look it up in our Storage resource 
+    */
+   LockRes();
+   foreach_res(store, R_STORAGE) {
+      if (strcmp(name_list->name[0], store->media_type) == 0) {
+        rx->store = store;
+        UnlockRes();
+        return;
+      }
+   }
+   UnlockRes();
 
+   /* Try asking user */
    rx->store = get_storage_resource(ua, false /* don't use default */);
 
    if (!rx->store) {
       bsendmsg(ua, _("\nWarning. Unable to find Storage resource for\n"
-         "MediaType %s, needed by the Jobs you selected.\n"
+         "MediaType \"%s\", needed by the Jobs you selected.\n"
          "You will be allowed to select a Storage device later.\n"),
         name_list->name[0]); 
    }