]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Add command to list metadata owners for a given tenant
authorEric Bollengier <eric@baculasystems.com>
Fri, 28 Jan 2022 15:56:49 +0000 (16:56 +0100)
committerEric Bollengier <eric@baculasystems.com>
Thu, 14 Sep 2023 11:56:57 +0000 (13:56 +0200)
bacula/src/cats/bdb.h
bacula/src/cats/cats.c
bacula/src/cats/sql_list.c
bacula/src/dird/ua_cmds.c
bacula/src/dird/ua_output.c

index 65043aef8ca54e67633e8b9cc516a3bed6abccca..8357f42a3cbc9869d2c1c572880b63c8f20f6dbe 100644 (file)
@@ -289,6 +289,7 @@ public:
    void bdb_list_snapshot_records(JCR *jcr, SNAPSHOT_DBR *sdbr,
               DB_LIST_HANDLER *sendit, void *ctx, e_list_type type);
    void bdb_list_files(JCR *jcr, FILE_DBR *fr, DB_RESULT_HANDLER *sendit, void *ctx);
+   void bdb_list_metadata_owner_records(JCR *jcr, META_DBR *meta_r, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type);
    void bdb_list_metadata_records(JCR *jcr, META_DBR *meta_r, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type);
    /* sql_update.c */
    bool bdb_update_job_start_record(JCR *jcr, JOB_DBR *jr);
index fd8dcf57cc2daa439e42bee36fa16a86d4f10285..178262ce0eb3d4f970349f4967cf406246da7ff2 100644 (file)
@@ -687,12 +687,6 @@ bool META_DBR::check()
       bsnprintf(errmsg, sizeof(errmsg), _("Type is not set"));
       return false;
    }
-
-   if (!Owner[0]) {
-      bsnprintf(errmsg, sizeof(errmsg), _("Owner is not set"));
-      return false;
-   }
-
    if (!Tenant[0]) {
       bsnprintf(errmsg, sizeof(errmsg), _("Tenant not set"));
       return false;
@@ -762,6 +756,12 @@ void META_DBR::create_db_filter(JCR *jcr, BDB *db, POOLMEM **where)
          pm_strcat(where, ") ");
       }
 
+      if (ClientName[0] != 0) {
+         db_escape_string(jcr, jcr->db, esc.c_str(), ClientName, strlen(ClientName));
+         Mmsg(tmp, " Client.Name='%s'", esc.c_str());
+         append_filter(where, tmp.c_str());
+      }
+
       if (ConversationId[0] != 0) {
          db_escape_string(jcr, jcr->db, esc.c_str(), ConversationId, strlen(ConversationId));
          Mmsg(tmp, " MetaEmail.EmailConversationId = '%s'", esc.c_str());
@@ -808,7 +808,11 @@ void META_DBR::create_db_filter(JCR *jcr, BDB *db, POOLMEM **where)
 
    if (Owner[0]) {
       db_escape_string(jcr, jcr->db, esc.c_str(), Owner, strlen(Owner));
-      Mmsg(tmp, " Meta%s.%sOwner = '%s'", Type, Type, esc.c_str());
+      if (strchr(Owner, '%')) {
+         Mmsg(tmp, " Meta%s.%sOwner ILIKE '%s'", Type, Type, esc.c_str());
+      } else {
+         Mmsg(tmp, " Meta%s.%sOwner = '%s'", Type, Type, esc.c_str());
+      }
       append_filter(where, tmp.c_str());
    }
 
index 2c217378167ddca6ffa4661e6722c556fe9a1a99..d0454b87a14a9933ca2d59231d454711201aa541 100644 (file)
@@ -1295,11 +1295,85 @@ void BDB::bdb_list_tag_records(JCR *jcr, TAG_DBR *tag, DB_LIST_HANDLER *result_h
    bdb_unlock();
 }
 
+/* List Owner in the metadata table */
+void BDB::bdb_list_metadata_owner_records(JCR *jcr, META_DBR *meta_r, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type)
+{
+   POOL_MEM esc(PM_MESSAGE), tmp(PM_MESSAGE), where(PM_MESSAGE), join(PM_MESSAGE);
+
+   bdb_lock();
+   meta_r->create_db_filter(jcr, this, where.handle());
+   const char *where_filter = get_acls(DB_ACL_BIT(DB_ACL_JOB)     |
+                                       DB_ACL_BIT(DB_ACL_CLIENT), strcmp(where.c_str(), "") == 0);
+
+   const char *join_filter = (*where_filter && meta_r->ClientName[0] == 0) ?
+      get_acl_join_filter(DB_ACL_BIT(DB_ACL_CLIENT)) : "";
+
+   if (meta_r->ClientName[0] != 0) {
+      Mmsg(join, " JOIN Job ON (Job.JobId = Meta%s.JobId) JOIN Client USING (ClientId) ", meta_r->Type);
+
+   } else if (*where_filter) {  // We add manually the Job join filter part
+      Mmsg(join, " JOIN Job ON (Job.JobId = Meta%s.JobId) ", meta_r->Type);
+   }
+
+   if (where_filter && *where_filter) {
+      pm_strcat(where, where_filter);
+   }
+
+   if (join_filter && *join_filter) {
+      pm_strcat(join, join_filter);
+   }
+   if (meta_r->limit > 0) {
+      Mmsg(tmp, " LIMIT %d ", meta_r->limit);
+      pm_strcat(where, tmp.c_str());
+   }
+
+   if (meta_r->offset > 0) {
+      Mmsg(tmp, " OFFSET %ld ", meta_r->offset);
+      pm_strcat(where, tmp.c_str());
+   }
+
+   switch (type) {
+   case JSON_LIST:
+   case VERT_LIST:
+   case HORZ_LIST:
+      Mmsg(cmd,
+           "SELECT DISTINCT %sOwner "
+           "FROM Meta%s %s %s",
+           meta_r->Type, meta_r->Type, join.c_str(), where.c_str());
+         break;
+   default:
+         break;
+   }
+   Dmsg1(DT_SQL|50, "%s\n", cmd);
+   if (!QueryDB(jcr, cmd)) {
+      Jmsg(jcr, M_ERROR, 0, _("Query %s failed!\n"), cmd);
+      bdb_unlock();
+      return;
+   }
+   if (strcmp(meta_r->Type, "Email") == 0) {
+      Mmsg(esc, "metadataemail");
+   } else {
+      Mmsg(esc, "metaattachment");
+   }
+   
+   list_result(jcr, this, esc.c_str(), sendit, ctx, type);
+
+   sql_free_result();
+   bdb_unlock();
+}
+
 /*
  * List plugin objects (search is based on object provided)
  */
 void BDB::bdb_list_metadata_records(JCR *jcr, META_DBR *meta_r, DB_LIST_HANDLER *sendit, void *ctx, e_list_type type)
 {
+   if (!meta_r->Owner[0] || strchr(meta_r->Owner, '%')) {
+      /* List Owners */
+      bdb_list_metadata_owner_records(jcr, meta_r, sendit, ctx, type);
+      return;
+   }
+
    POOL_MEM esc(PM_MESSAGE), tmp(PM_MESSAGE), where(PM_MESSAGE), join(PM_MESSAGE);
    bdb_lock();
    //TODO add ACL part
@@ -1313,9 +1387,6 @@ void BDB::bdb_list_metadata_records(JCR *jcr, META_DBR *meta_r, DB_LIST_HANDLER
       get_acl_join_filter(DB_ACL_BIT(DB_ACL_CLIENT)) : "";
 
    if (meta_r->ClientName[0] != 0) {
-      bdb_escape_string(jcr, esc.c_str(), meta_r->ClientName, strlen(meta_r->ClientName));
-      Mmsg(tmp, " Client.Name='%s'", esc.c_str());
-      append_filter(where.handle(), tmp.c_str());
       Mmsg(join, " JOIN Job ON (Job.JobId = Meta%s.JobId) JOIN Client USING (ClientId) ", meta_r->Type);
 
    } else if (*where_filter) {  // We add manually the Job join filter part
index 67e22d5356c8663929f7dff57e4c28ae7fd9b1fe..20c4a3ffb874cec2bf22740cce14b050bba61596 100644 (file)
@@ -138,7 +138,7 @@ static struct cmdstruct commands[] = {                                      /* C
        "\tevents [type=<str> | limit=<int> | order=<asc|desc> | days=<int> | start=<time-specification> | end=<time-specification> |\n"
        "\t\t source=<str> | code=<str> | type=<str> ]\n"
        "\tobjects [jobid=<jobid> client=<cli> type=<str> | category=<str> | status=<S> | limit=<int> | order=<asc|desc> ]\n"
-       "\tmetadata type=<email|attachment> tenant=<str> owner=<str>\n"
+       "\tmetadata type=<email|attachment> tenant=<str> [owner=<str>]\n"
        "\t    [jobid=<jobids> client=<cc> order=<Asc|desc> limit=<nn> orderby=<time> offset=<nn>]\n"
        "\t    [from=<str> to=<str> cc=<str> tags=<str> subject=<str> bodypreview=<str> all=<str>\n"
        "\t    minsize=<int> maxsize=<int> importance=<str> isread=<0|1> isdraft=<0|1> categories=<str>\n"
index c071d4704c2f95fb0e7e49b2c652c29326d0b7d2..e9a238d9fd360beb1741e3e8fa75fe32250d1605 100644 (file)
@@ -336,7 +336,8 @@ bail_out:
  *  list objects [type=objecttype job_id=id clientname=n,status=S] - list plugin objects
  *  list pluginrestoreconf jobid=x,y,z [id=k]
  *  list filemedia jobid=x fileindex=z
- *  list metadata type=[email|attachment] owner=xxx tenant=xxx jobid=<x,w,z> from=<str>
+ *  list metadata type=[email|attachment] tenant=xxx owner=xxx jobid=<x,w,z> client=<cli>
+ *             from=<str>
  *             to=<str> cc=<str> tags=<str> 
  *             subject=<str> bodypreview=<str> all=<str> minsize=<int> maxsize=<int> 
  *             importance=<str> isread=<0|1> isdraft=<0|1>
@@ -930,7 +931,10 @@ static int do_list_cmd(UAContext *ua, const char *cmd, e_list_type llist)
          META_DBR meta_r;
 
          for (j=i+1; j<ua->argc; j++) {
-            if (strcasecmp(ua->argk[j], NT_("jobid")) == 0 && ua->argv[j]) {
+            if (!ua->argv[j]) {
+               ua->error_msg(_("Invalid %s argument. Expecting value\n"), ua->argk[j]);
+               return 1;
+            } else if (strcasecmp(ua->argk[j], NT_("jobid")) == 0) {
                if (is_a_number_list(ua->argv[j]) && acl_access_jobid_ok(ua, ua->argv[j])) {
                   meta_r.JobIds = ua->argv[j];
                 } else {
@@ -1021,16 +1025,16 @@ static int do_list_cmd(UAContext *ua, const char *cmd, e_list_type llist)
             } else if (strcasecmp(ua->argk[j], NT_("hasattachment")) == 0) {
                meta_r.HasAttachment = str_to_uint64(ua->argv[j]);
 
-            } else if (strcasecmp(ua->argk[j], NT_("offset")) == 0 && ua->argv[j]) {
+            } else if (strcasecmp(ua->argk[j], NT_("offset")) == 0) {
                meta_r.offset = atoi(ua->argv[j]);
 
-            } else if (strcasecmp(ua->argk[j], NT_("limit")) == 0 && ua->argv[j]) {
+            } else if (strcasecmp(ua->argk[j], NT_("limit")) == 0) {
                meta_r.limit = atoi(ua->argv[j]);
 
-            } else if (strcasecmp(ua->argk[j], NT_("orderby")) == 0 && ua->argv[j]) {
+            } else if (strcasecmp(ua->argk[j], NT_("orderby")) == 0) {
                meta_r.orderby = bstrcasecmp(ua->argv[j], "Time") == 1;
 
-            } else if (strcasecmp(ua->argk[j], NT_("order")) == 0 && ua->argv[j]) {
+            } else if (strcasecmp(ua->argk[j], NT_("order")) == 0) {
                /* Other order are tested before */
                meta_r.order = bstrcasecmp(ua->argv[j], "DESC");
             }