]> granicus.if.org Git - p11-kit/commitdiff
trust: Add index callback for when an object is removed
authorStef Walter <stef@thewalter.net>
Wed, 28 Aug 2013 07:45:21 +0000 (09:45 +0200)
committerStef Walter <stef@thewalter.net>
Thu, 29 Aug 2013 08:29:53 +0000 (10:29 +0200)
This allows a token to remove the file if desired

trust/index.c
trust/index.h
trust/session.c
trust/tests/test-builder.c
trust/tests/test-index.c
trust/token.c

index 83a4503439c2f2db0952c4996b8a16cef46d0ec2..9a193c5307f60a67f57efddf49ef6154a0afa83e 100644 (file)
@@ -80,6 +80,9 @@ struct _p11_index {
        /* Called after each object ready to be stored */
        p11_index_store_cb store;
 
+       /* Called after an object has been removed */
+       p11_index_remove_cb remove;
+
        /* Called after objects change */
        p11_index_notify_cb notify;
 
@@ -129,9 +132,18 @@ default_notify (void *data,
 
 }
 
+static CK_RV
+default_remove (void *data,
+                p11_index *index,
+                CK_ATTRIBUTE *attrs)
+{
+       return CKR_OK;
+}
+
 p11_index *
 p11_index_new (p11_index_build_cb build,
                p11_index_store_cb store,
+               p11_index_remove_cb remove,
                p11_index_notify_cb notify,
                void *data)
 {
@@ -146,10 +158,13 @@ p11_index_new (p11_index_build_cb build,
                store = default_store;
        if (notify == NULL)
                notify = default_notify;
+       if (remove == NULL)
+               remove = default_remove;
 
        index->build = build;
        index->store = store;
        index->notify = notify;
+       index->remove = remove;
        index->data = data;
 
        index->objects = p11_dict_new (p11_dict_ulongptr_hash,
@@ -578,12 +593,22 @@ p11_index_remove (p11_index *index,
                   CK_OBJECT_HANDLE handle)
 {
        index_object *obj;
+       CK_RV rv;
 
        return_val_if_fail (index != NULL, CKR_GENERAL_ERROR);
 
        if (!p11_dict_steal (index->objects, &handle, NULL, (void **)&obj))
                return CKR_OBJECT_HANDLE_INVALID;
 
+       rv = (index->remove) (index->data, index, obj->attrs);
+
+       /* If the writer failed the remove, then add it back */
+       if (rv != CKR_OK) {
+               if (!p11_dict_set (index->objects, &obj->handle, obj))
+                       return_val_if_reached (CKR_HOST_MEMORY);
+               return rv;
+       }
+
        /* This takes ownership of the attributes */
        index_notify (index, handle, obj->attrs);
        obj->attrs = NULL;
index 192bfcdb9f3438f1a2e7873cecd3127cc1583ee0..3ae24a174c42287db63192e0fc38f9f2b3024ed6 100644 (file)
@@ -53,6 +53,10 @@ typedef CK_RV   (* p11_index_store_cb)   (void *data,
                                           CK_OBJECT_HANDLE handle,
                                           CK_ATTRIBUTE **attrs);
 
+typedef CK_RV   (* p11_index_remove_cb)  (void *data,
+                                          p11_index *index,
+                                          CK_ATTRIBUTE *attrs);
+
 typedef void    (* p11_index_notify_cb)  (void *data,
                                           p11_index *index,
                                           CK_OBJECT_HANDLE handle,
@@ -60,6 +64,7 @@ typedef void    (* p11_index_notify_cb)  (void *data,
 
 p11_index *        p11_index_new         (p11_index_build_cb build,
                                           p11_index_store_cb store,
+                                          p11_index_remove_cb remove,
                                           p11_index_notify_cb notify,
                                           void *data);
 
index 76a9acf0052206075135577016c5ed45f2b63844..b93a5c35c678c93874fbcf65f40818d3efcafe68 100644 (file)
@@ -61,7 +61,7 @@ p11_session_new (p11_token *token)
        session->builder = p11_builder_new (P11_BUILDER_FLAG_NONE);
        return_val_if_fail (session->builder, NULL);
 
-       session->index = p11_index_new (p11_builder_build, NULL,
+       session->index = p11_index_new (p11_builder_build, NULL, NULL,
                                        p11_builder_changed,
                                        session->builder);
        return_val_if_fail (session->index != NULL, NULL);
index 6e061aa0a91b4af6d2a753cf727be0114ae2b4ec..5c3c15b5d9e922fc47979af7712b784b616b592e 100644 (file)
@@ -77,7 +77,7 @@ setup (void *unused)
        test.builder = p11_builder_new (P11_BUILDER_FLAG_TOKEN);
        assert_ptr_not_null (test.builder);
 
-       test.index = p11_index_new (p11_builder_build, NULL, p11_builder_changed, test.builder);
+       test.index = p11_index_new (p11_builder_build, NULL, NULL, p11_builder_changed, test.builder);
        assert_ptr_not_null (test.index);
 }
 
index 074ab2dcf43ca382fdc69ac35c7e71ae84a564b7..fc861b28f669ba7199ea63ed51234792290af224 100644 (file)
@@ -53,7 +53,7 @@ struct {
 static void
 setup (void *unused)
 {
-       test.index = p11_index_new (NULL, NULL, NULL, NULL);
+       test.index = p11_index_new (NULL, NULL, NULL, NULL, NULL);
        assert_ptr_not_null (test.index);
 }
 
@@ -688,7 +688,7 @@ test_replace_all_build_fails (void)
        p11_index *index;
        CK_RV rv;
 
-       index = p11_index_new (on_index_build_fail, NULL, NULL, &match);
+       index = p11_index_new (on_index_build_fail, NULL, NULL, NULL, &match);
        assert_ptr_not_null (index);
 
        array = p11_array_new (p11_attrs_free);
@@ -745,7 +745,7 @@ test_build_populate (void)
        p11_index *index;
        CK_RV rv;
 
-       index = p11_index_new (on_build_populate, NULL, NULL, "blah");
+       index = p11_index_new (on_build_populate, NULL, NULL, NULL, "blah");
        assert_ptr_not_null (index);
 
        rv = p11_index_add (index, original, 2, &handle);
@@ -808,7 +808,7 @@ test_build_fail (void)
        p11_index *index;
        CK_RV rv;
 
-       index = p11_index_new (on_build_fail, NULL, NULL, "testo");
+       index = p11_index_new (on_build_fail, NULL, NULL, NULL, "testo");
        assert_ptr_not_null (index);
 
        rv = p11_index_add (index, okay, 2, &handle);
@@ -872,7 +872,7 @@ test_change_called (void)
        p11_index *index;
        CK_RV rv;
 
-       index = p11_index_new (NULL, NULL, on_change_check, "change-check");
+       index = p11_index_new (NULL, NULL, NULL, on_change_check, "change-check");
        assert_ptr_not_null (index);
 
        on_change_removing = false;
@@ -917,7 +917,7 @@ test_change_batch (void)
        p11_index *index;
        CK_RV rv;
 
-       index = p11_index_new (NULL, NULL, on_change_check, "change-check");
+       index = p11_index_new (NULL, NULL, NULL, on_change_check, "change-check");
        assert_ptr_not_null (index);
 
        on_change_batching = true;
@@ -1008,7 +1008,7 @@ test_change_nested (void)
        p11_index *index;
        CK_RV rv;
 
-       index = p11_index_new (NULL, NULL, on_change_nested, "change-nested");
+       index = p11_index_new (NULL, NULL, NULL, on_change_nested, "change-nested");
        assert_ptr_not_null (index);
 
        on_change_called = 0;
@@ -1027,6 +1027,89 @@ test_change_nested (void)
        p11_index_free (index);
 }
 
+static CK_RV
+on_remove_callback (void *data,
+                    p11_index *index,
+                    CK_ATTRIBUTE *attrs)
+{
+       int *removed = data;
+       assert_ptr_not_null (removed);
+       assert_num_eq (*removed, 0);
+       *removed = 1;
+       return CKR_OK;
+}
+
+static void
+test_remove_callback (void)
+{
+       CK_ATTRIBUTE original[] = {
+               { CKA_LABEL, "yay", 3 },
+               { CKA_VALUE, "eight", 5 },
+               { CKA_INVALID }
+
+       };
+
+       CK_OBJECT_HANDLE handle;
+       p11_index *index;
+       int removed = 0;
+       CK_RV rv;
+
+       index = p11_index_new (NULL, NULL, on_remove_callback, NULL, &removed);
+       assert_ptr_not_null (index);
+
+       rv = p11_index_add (index, original, 2, &handle);
+       assert_num_eq (rv, CKR_OK);
+
+       assert_ptr_not_null (p11_index_lookup (index, handle));
+
+       rv = p11_index_remove (index, handle);
+       assert_num_eq (rv, CKR_OK);
+
+       assert_num_eq (removed, 1);
+       assert_ptr_eq (p11_index_lookup (index, handle), NULL);
+
+       p11_index_free (index);
+}
+
+static CK_RV
+on_remove_fail (void *data,
+                p11_index *index,
+                CK_ATTRIBUTE *attrs)
+{
+       assert_str_eq (data, "remove-fail");
+       return CKR_DEVICE_REMOVED;
+}
+
+static void
+test_remove_fail (void)
+{
+       CK_ATTRIBUTE original[] = {
+               { CKA_LABEL, "yay", 3 },
+               { CKA_VALUE, "eight", 5 },
+               { CKA_INVALID }
+
+       };
+
+       CK_OBJECT_HANDLE handle;
+       p11_index *index;
+       CK_RV rv;
+
+       index = p11_index_new (NULL, NULL, on_remove_fail, NULL, "remove-fail");
+       assert_ptr_not_null (index);
+
+       rv = p11_index_add (index, original, 2, &handle);
+       assert (rv == CKR_OK);
+
+       assert_ptr_not_null (p11_index_lookup (index, handle));
+
+       rv = p11_index_remove (index, handle);
+       assert_num_eq (rv, CKR_DEVICE_REMOVED);
+
+       assert_ptr_not_null (p11_index_lookup (index, handle));
+
+       p11_index_free (index);
+}
+
 int
 main (int argc,
       char *argv[])
@@ -1054,6 +1137,8 @@ main (int argc,
        p11_test (test_change_batch, "/index/change_batch");
        p11_test (test_change_nested, "/index/change_nested");
        p11_test (test_replace_all_build_fails, "/index/replace-all-build-fails");
+       p11_test (test_remove_callback, "/index/remove-callback");
+       p11_test (test_remove_fail, "/index/remove-fail");
 
        return p11_test_run (argc, argv);
 }
index cfcdba69322c31efab80befe333e5c24de7713b0..427c1d5c12e905d35b9194428bc5f6f7b7347098 100644 (file)
@@ -742,6 +742,7 @@ p11_token_new (CK_SLOT_ID slot,
 
        token->index = p11_index_new (on_index_build,
                                      on_index_store,
+                                     NULL,
                                      on_index_notify,
                                      token);
        return_val_if_fail (token->index != NULL, NULL);