Implemented targeted SWID request
authorAndreas Steffen <andreas.steffen@strongswan.org>
Fri, 6 Sep 2013 20:06:39 +0000 (22:06 +0200)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Fri, 6 Sep 2013 20:06:39 +0000 (22:06 +0200)
src/libpts/plugins/imc_swid/imc_swid.c
src/libpts/swid/swid_inventory.c
src/libpts/swid/swid_inventory.h
src/libpts/tcg/swid/tcg_swid_attr_req.c
src/libpts/tcg/swid/tcg_swid_attr_req.h

index 8e7c49b..e130580 100644 (file)
@@ -154,7 +154,7 @@ static TNC_Result receive_message(imc_state_t *state, imc_msg_t *in_msg)
                tcg_swid_attr_req_t *attr_req;
                u_int8_t flags;
                u_int32_t request_id, eid_epoch;
-               swid_inventory_t *swid_inventory;
+               swid_inventory_t *swid_inventory, *targets;
                char *swid_directory;
                bool full_tags;
 
@@ -168,6 +168,7 @@ static TNC_Result receive_message(imc_state_t *state, imc_msg_t *in_msg)
                attr_req = (tcg_swid_attr_req_t*)attr;
                flags = attr_req->get_flags(attr_req);
                request_id = attr_req->get_request_id(attr_req);
+               targets = attr_req->get_targets(attr_req);
                eid_epoch = swid_state->get_eid_epoch(swid_state);
 
                if (flags & (TCG_SWID_ATTR_REQ_FLAG_S | TCG_SWID_ATTR_REQ_FLAG_C))
@@ -183,7 +184,7 @@ static TNC_Result receive_message(imc_state_t *state, imc_msg_t *in_msg)
                                                                "libimcv.plugins.imc-swid.swid_directory",
                                                                 SWID_DIRECTORY);
                swid_inventory = swid_inventory_create(full_tags);
-               if (!swid_inventory->collect(swid_inventory, swid_directory))
+               if (!swid_inventory->collect(swid_inventory, swid_directory, targets))
                {
                        swid_inventory->destroy(swid_inventory);
                        attr = swid_error_create(TCG_SWID_ERROR, request_id,
index 9d5c309..f03a4cd 100644 (file)
@@ -52,7 +52,8 @@ struct private_swid_inventory_t {
        linked_list_t *list;
 };
 
-static bool collect_tags(private_swid_inventory_t *this, char *pathname)
+static bool collect_tags(private_swid_inventory_t *this, char *pathname,
+                                                swid_inventory_t *targets)
 {
        char *rel_name, *abs_name;
        struct stat st;
@@ -73,26 +74,50 @@ static bool collect_tags(private_swid_inventory_t *this, char *pathname)
                char * start, *stop;
                chunk_t tag_creator;
                chunk_t unique_sw_id = chunk_empty, unique_seq_id = chunk_empty;
-
                if (!strstr(rel_name, "regid."))
                {
                        continue;
                }
                if (S_ISDIR(st.st_mode))
                {
-                       if (!collect_tags(this, abs_name))
+                       /* In case of a targeted request */
+                       if (targets->get_count(targets))
+                       {
+                               enumerator_t *target_enumerator;
+                               swid_tag_id_t *tag_id;
+                               bool match = FALSE;
+
+                               target_enumerator = targets->create_enumerator(targets);
+                               while (target_enumerator->enumerate(target_enumerator, &tag_id))
+                               {
+                                       if (chunk_equals(tag_id->get_tag_creator(tag_id),
+                                               chunk_from_str(rel_name)))
+                                       {
+                                               match = TRUE;
+                                               break;
+                                       }
+                               }
+                               target_enumerator->destroy(target_enumerator);
+
+                               if (!match)
+                               {
+                                       continue;
+                               }
+                       }
+
+                       if (!collect_tags(this, abs_name, targets))
                        {
                                goto end;
                        }
                        continue;
                }
-               DBG2(DBG_IMC, "  %s", rel_name);
 
                /* parse the regid filename into its components */
                start = rel_name;
                stop = strchr(start, '_');
                if (!stop)
                {
+                       DBG1(DBG_IMC, "  %s", rel_name);
                        DBG1(DBG_IMC, "  '_' separator not found");
                        goto end;
                }
@@ -109,6 +134,7 @@ static bool collect_tags(private_swid_inventory_t *this, char *pathname)
                stop = strstr(start, ".swidtag");
                if (!stop)
                {
+                       DBG1(DBG_IMC, "  %s", rel_name);
                        DBG1(DBG_IMC, "  swidtag postfix not found");
                        goto end;
                }
@@ -121,6 +147,34 @@ static bool collect_tags(private_swid_inventory_t *this, char *pathname)
                        unique_sw_id = chunk_create(start, stop-start);
                }
 
+               /* In case of a targeted request */
+               if (targets->get_count(targets))
+               {
+                       enumerator_t *target_enumerator;
+                       swid_tag_id_t *tag_id;
+                       bool match = FALSE;
+
+                       target_enumerator = targets->create_enumerator(targets);
+                       while (target_enumerator->enumerate(target_enumerator, &tag_id))
+                       {
+                               if (chunk_equals(tag_id->get_tag_creator(tag_id),
+                                                                tag_creator) &&
+                                       chunk_equals(tag_id->get_unique_sw_id(tag_id, NULL),
+                                                                unique_sw_id))
+                               {
+                                       match = TRUE;
+                                       break;
+                               }
+                       }
+                       target_enumerator->destroy(target_enumerator);
+
+                       if (!match)
+                       {
+                               continue;
+                       }
+               }
+               DBG2(DBG_IMC, "  %s", rel_name);
+
                if (this->full_tags)
                {
                        swid_tag_t *tag;
@@ -177,9 +231,9 @@ end:
 }
 
 METHOD(swid_inventory_t, collect, bool,
-       private_swid_inventory_t *this, char *directory)
+       private_swid_inventory_t *this, char *directory, swid_inventory_t *targets)
 {
-       return collect_tags(this, directory);
+       return collect_tags(this, directory, targets);
 }
 
 METHOD(swid_inventory_t, add, void,
index ae3a0c2..68d3047 100644 (file)
@@ -34,9 +34,11 @@ struct swid_inventory_t {
         * Collect the SWID tags stored on the endpoint
         *
         * @param directory             SWID directory path
+        * @param targets               List of target tag IDs
         * @return                              TRUE if successful
         */
-       bool (*collect)(swid_inventory_t *this, char *directory);
+       bool (*collect)(swid_inventory_t *this, char *directory,
+                                       swid_inventory_t *targets);
 
        /**
         * Collect the SWID tags stored on the endpoint
index 50cca8d..b8e7962 100644 (file)
@@ -88,9 +88,9 @@ struct private_tcg_swid_attr_req_t {
        u_int32_t earliest_eid;
 
        /**
-        * List of Tag Identifiers
+        * List of Target Tag Identifiers
         */
-       linked_list_t *tag_id_list;
+       swid_inventory_t *targets;
 
        /**
         * Reference count
@@ -137,11 +137,11 @@ METHOD(pa_tnc_attr_t, build, void,
 
        writer = bio_writer_create(SWID_REQ_SIZE);
        writer->write_uint8 (writer, this->flags);
-       writer->write_uint24(writer, this->tag_id_list->get_count(this->tag_id_list));
+       writer->write_uint24(writer, this->targets->get_count(this->targets));
        writer->write_uint32(writer, this->request_id);
        writer->write_uint32(writer, this->earliest_eid);
 
-       enumerator = this->tag_id_list->create_enumerator(this->tag_id_list);
+       enumerator = this->targets->create_enumerator(this->targets);
        while (enumerator->enumerate(enumerator, &tag_id))
        {
                tag_creator = tag_id->get_tag_creator(tag_id);
@@ -202,7 +202,7 @@ METHOD(pa_tnc_attr_t, process, status_t,
                *offset += 2 + unique_sw_id.len;
                
                tag_id = swid_tag_id_create(tag_creator, unique_sw_id, chunk_empty);
-               this->tag_id_list->insert_last(this->tag_id_list, tag_id);
+               this->targets->add(this->targets, tag_id);
        }
        reader->destroy(reader);
 
@@ -221,8 +221,7 @@ METHOD(pa_tnc_attr_t, destroy, void,
 {
        if (ref_put(&this->ref))
        {
-               this->tag_id_list->destroy_offset(this->tag_id_list,
-                                                                                 offsetof(swid_tag_id_t, destroy));
+               this->targets->destroy(this->targets);
                free(this->value.ptr);
                free(this);
        }
@@ -246,16 +245,16 @@ METHOD(tcg_swid_attr_req_t, get_earliest_eid, u_int32_t,
        return this->earliest_eid;
 }
 
-METHOD(tcg_swid_attr_req_t, add_tag_id, void,
+METHOD(tcg_swid_attr_req_t, add_target, void,
        private_tcg_swid_attr_req_t *this, swid_tag_id_t *tag_id)
 {
-       this->tag_id_list->insert_last(this->tag_id_list, tag_id);
+       this->targets->add(this->targets, tag_id);
 }
 
-METHOD(tcg_swid_attr_req_t, create_tag_id_enumerator, enumerator_t*,
+METHOD(tcg_swid_attr_req_t, get_targets, swid_inventory_t*,
        private_tcg_swid_attr_req_t *this)
 {
-       return this->tag_id_list->create_enumerator(this->tag_id_list);
+       return this->targets;
 }
 
 /**
@@ -281,14 +280,14 @@ pa_tnc_attr_t *tcg_swid_attr_req_create(u_int8_t flags, u_int32_t request_id,
                        .get_flags = _get_flags,
                        .get_request_id = _get_request_id,
                        .get_earliest_eid = _get_earliest_eid,
-                       .add_tag_id = _add_tag_id,
-                       .create_tag_id_enumerator = _create_tag_id_enumerator,
+                       .add_target = _add_target,
+                       .get_targets = _get_targets,
                },
                .type = { PEN_TCG, TCG_SWID_REQUEST },
                .flags = flags & SWID_REQ_RESERVED_MASK,
                .request_id = request_id,
                .earliest_eid = eid,
-               .tag_id_list = linked_list_create(),
+               .targets = swid_inventory_create(FALSE),
                .ref = 1,
        );
 
@@ -317,12 +316,12 @@ pa_tnc_attr_t *tcg_swid_attr_req_create_from_data(chunk_t data)
                        .get_flags = _get_flags,
                        .get_request_id = _get_request_id,
                        .get_earliest_eid = _get_earliest_eid,
-                       .add_tag_id = _add_tag_id,
-                       .create_tag_id_enumerator = _create_tag_id_enumerator,
+                       .add_target = _add_target,
+                       .get_targets = _get_targets,
                },
                .type = { PEN_TCG, TCG_SWID_REQUEST },
                .value = chunk_clone(data),
-               .tag_id_list = linked_list_create(),
+               .targets = swid_inventory_create(FALSE),
                .ref = 1,
        );
 
index 40503df..851b68d 100644 (file)
@@ -33,6 +33,7 @@ enum tcg_swid_attr_req_flag_t {
 
 #include "tcg/tcg_attr.h"
 #include "swid/swid_tag_id.h"
+#include "swid/swid_inventory.h"
 #include "pa_tnc/pa_tnc_attr.h"
 
 /**
@@ -71,14 +72,14 @@ struct tcg_swid_attr_req_t {
         *
         * @param tag_id                        SWID Tag ID (is not cloned by constructor!)
         */
-       void (*add_tag_id)(tcg_swid_attr_req_t *this, swid_tag_id_t *tag_id);
+       void (*add_target)(tcg_swid_attr_req_t *this, swid_tag_id_t *tag_id);
 
        /**
         * Create Tag ID enumerator
         *
-        * @return                                      Tag ID enumerator
+        * @return                                      Get a list of target tag IDs
         */
-       enumerator_t* (*create_tag_id_enumerator)(tcg_swid_attr_req_t *this);
+       swid_inventory_t* (*get_targets)(tcg_swid_attr_req_t *this);
 
 };