]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
libimcv: Moved REST API from imv_swid and imv_swima to libimcv
authorAndreas Steffen <andreas.steffen@strongswan.org>
Mon, 26 Jun 2017 16:07:11 +0000 (18:07 +0200)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Sat, 8 Jul 2017 21:19:51 +0000 (23:19 +0200)
configure.ac
src/libimcv/Makefile.am
src/libimcv/plugins/imv_swid/Makefile.am
src/libimcv/plugins/imv_swid/imv_swid_agent.c
src/libimcv/plugins/imv_swid/imv_swid_rest.c [deleted file]
src/libimcv/plugins/imv_swid/imv_swid_rest.h [deleted file]
src/libimcv/plugins/imv_swima/Makefile.am
src/libimcv/plugins/imv_swima/imv_swima_agent.c
src/libimcv/rest/rest.c [moved from src/libimcv/plugins/imv_swima/imv_swima_rest.c with 50% similarity]
src/libimcv/rest/rest.h [moved from src/libimcv/plugins/imv_swima/imv_swima_rest.h with 64% similarity]

index e6e0402e90b30d7c64467029bf2f0749f8079785..4c45200c4cc595c86bf58ed67480f7ae71c95512 100644 (file)
@@ -1003,7 +1003,7 @@ if test x$tss_tss2 = xtrue; then
        AC_SUBST(tss2_LIBS)
 fi
 
-if test x$imv_swid = xtrue -o x$imv_swima = xtrue; then
+if test x$imc_swima = xtrue -o $imv_swima = xtrue -o x$imv_swid = xtrue; then
        PKG_CHECK_MODULES(json, [json-c], [],
                [PKG_CHECK_MODULES(json, [json])])
        AC_SUBST(json_CFLAGS)
@@ -1711,6 +1711,9 @@ fi
 if test x$fuzzing = xtrue; then
        AC_DEFINE([USE_FUZZING], [], [build code for fuzzing])
 fi
+if test x$imc_swima = xtrue -o x$imv_swima = xtrue -o x$imv_swid = xtrue ; then
+       AC_DEFINE([USE_JSON], [], [build code for JSON])
+fi
 
 # ====================================================
 #  options for enabled modules (see conf/Makefile.am)
index ab66bccbae0d4eb96df8abd28f1a10b9c9fa8e3e..dda0c2cd86f6a11df7075ec8a899de51c006b56b 100644 (file)
@@ -5,6 +5,9 @@ AM_CPPFLAGS = \
        -DIPSEC_SCRIPT=\"${ipsec_script}\" \
        -DSWID_DIRECTORY=\"${prefix}/share\"
 
+AM_CFLAGS = \
+       $(json_CFLAGS)
+
 ipseclib_LTLIBRARIES = libimcv.la
 
 libimcv_la_LDFLAGS = \
@@ -13,7 +16,8 @@ libimcv_la_LDFLAGS = \
 libimcv_la_LIBADD = \
        $(top_builddir)/src/libstrongswan/libstrongswan.la \
        $(top_builddir)/src/libtncif/libtncif.la \
-       $(top_builddir)/src/libtpmtss/libtpmtss.la
+       $(top_builddir)/src/libtpmtss/libtpmtss.la \
+       $(json_LIBS)
 
 if USE_WINDOWS
   libimcv_la_LIBADD += -lws2_32
@@ -88,6 +92,7 @@ libimcv_la_SOURCES = \
        pts/components/tcg/tcg_comp_func_name.h pts/components/tcg/tcg_comp_func_name.c \
        pwg/pwg_attr.h pwg/pwg_attr.c \
        pwg/pwg_attr_vendor_smi_code.h pwg/pwg_attr_vendor_smi_code.c \
+       rest/rest.h rest/rest.c \
        seg/seg_contract.h seg/seg_contract.c \
        seg/seg_contract_manager.h seg/seg_contract_manager.c \
        seg/seg_env.h seg/seg_env.c \
index 73da84b55a4752a5e99e8305d9eb6a4707109911..e573ea0d89199911f91ec4484a5e29c80c5489d2 100644 (file)
@@ -16,7 +16,6 @@ imv_swid_la_LIBADD = \
 
 imv_swid_la_SOURCES = \
        imv_swid.c imv_swid_state.h imv_swid_state.c \
-       imv_swid_agent.h imv_swid_agent.c \
-       imv_swid_rest.h imv_swid_rest.c
+       imv_swid_agent.h imv_swid_agent.c
 
 imv_swid_la_LDFLAGS = -module -avoid-version -no-undefined
index c057e7ed174414fcae4c8afbc58a8f8aa2f8a89d..09a387fd33dfed66e0560463d26cf4ff5c745c05 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013-2016 Andreas Steffen
+ * Copyright (C) 2013-2017 Andreas Steffen
  * HSR Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
 
 #include "imv_swid_agent.h"
 #include "imv_swid_state.h"
-#include "imv_swid_rest.h"
 
 #include <imcv.h>
 #include <imv/imv_agent.h>
 #include <imv/imv_msg.h>
 #include <ietf/ietf_attr_pa_tnc_error.h>
+#include "rest/rest.h"
 #include "tcg/seg/tcg_seg_attr_max_size.h"
 #include "tcg/seg/tcg_seg_attr_seg_env.h"
 #include "tcg/swid/tcg_swid_attr_req.h"
@@ -72,7 +72,7 @@ struct private_imv_swid_agent_t {
        /**
         * REST API to strongTNC manager
         */
-       imv_swid_rest_t *rest_api;
+       rest_t *rest_api;
 
 };
 
@@ -719,7 +719,7 @@ imv_agent_if_t *imv_swid_agent_create(const char *name, TNC_IMVID id,
                                                "%s.plugins.imv-swid.rest_api_timeout", 120, lib->ns);
        if (rest_api_uri)
        {
-               this->rest_api = imv_swid_rest_create(rest_api_uri, rest_api_timeout);
+               this->rest_api = rest_create(rest_api_uri, rest_api_timeout);
        }
 
        return &this->public;
diff --git a/src/libimcv/plugins/imv_swid/imv_swid_rest.c b/src/libimcv/plugins/imv_swid/imv_swid_rest.c
deleted file mode 100644 (file)
index 0fe96ed..0000000
+++ /dev/null
@@ -1,124 +0,0 @@
-/*
- * Copyright (C) 2014 Andreas Steffen
- * HSR Hochschule fuer Technik Rapperswil
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * for more details.
- */
-
-#define _GNU_SOURCE
-#include <stdio.h>
-
-#include "imv_swid_rest.h"
-
-typedef struct private_imv_swid_rest_t private_imv_swid_rest_t;
-
-/**
- * Private data of an imv_swid_rest_t object.
- */
-struct private_imv_swid_rest_t {
-
-       /**
-        * Public members of imv_swid_rest_t
-        */
-       imv_swid_rest_t public;
-
-       /**
-        * URI of REST API
-        */
-       char *uri;
-
-       /**
-        * Timeout of REST API connection
-        */
-       u_int timeout;
-
-};
-
-#define HTTP_STATUS_CODE_PRECONDITION_FAILED   412
-
-METHOD(imv_swid_rest_t, post, status_t,
-       private_imv_swid_rest_t *this, char *command, json_object *jrequest,
-       json_object **jresponse)
-{
-       struct json_tokener *tokener;
-       chunk_t data, response = chunk_empty;
-       status_t status;
-       char *uri;
-       int code;
-
-       if (asprintf(&uri, "%s%s",this->uri, command) < 0)
-       {
-               return FAILED;
-       }
-       data = chunk_from_str((char*)json_object_to_json_string(jrequest));
-
-       status = lib->fetcher->fetch(lib->fetcher, uri, &response,
-                               FETCH_TIMEOUT, this->timeout,
-                               FETCH_REQUEST_DATA, data,
-                               FETCH_REQUEST_TYPE, "application/json; charset=utf-8",
-                               FETCH_REQUEST_HEADER, "Accept: application/json",
-                               FETCH_REQUEST_HEADER, "Expect:",
-                               FETCH_RESPONSE_CODE, &code,
-                               FETCH_END);
-       free(uri);
-
-       if (status != SUCCESS)
-       {
-               if (code != HTTP_STATUS_CODE_PRECONDITION_FAILED || !response.ptr)
-               {
-                       DBG2(DBG_IMV, "REST http request failed with status code: %d", code);
-                       status = FAILED;
-               }
-               else
-               {
-                       if (jresponse)
-                       {
-                               /* Parse HTTP response into a JSON object */
-                               tokener = json_tokener_new();
-                               *jresponse = json_tokener_parse_ex(tokener, response.ptr,
-                                                                                                                       response.len);
-                               json_tokener_free(tokener);
-                       }
-                       status = NEED_MORE;
-               }
-       }
-       free(response.ptr);
-
-       return status;
-}
-
-METHOD(imv_swid_rest_t, destroy, void,
-       private_imv_swid_rest_t *this)
-{
-       free(this->uri);
-       free(this);
-}
-
-/**
- * Described in header.
- */
-imv_swid_rest_t *imv_swid_rest_create(char *uri, u_int timeout)
-{
-       private_imv_swid_rest_t *this;
-
-       INIT(this,
-               .public = {
-                       .post = _post,
-                       .destroy = _destroy,
-               },
-               .uri = strdup(uri),
-               .timeout = timeout,
-       );
-
-       return &this->public;
-}
-
-
diff --git a/src/libimcv/plugins/imv_swid/imv_swid_rest.h b/src/libimcv/plugins/imv_swid/imv_swid_rest.h
deleted file mode 100644 (file)
index 32392cb..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Copyright (C) 2013-2014 Andreas Steffen
- * HSR Hochschule fuer Technik Rapperswil
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * for more details.
- */
-
-/**
- * @defgroup imv_swid imv_swid
- * @ingroup libimcv_plugins
- *
- * @defgroup imv_swid_rest_t imv_swid_rest
- * @{ @ingroup imv_swid
- */
-
-#ifndef IMV_SWID_REST_H_
-#define IMV_SWID_REST_H_
-
-#include <library.h>
-
-#include <json.h>
-
-typedef struct imv_swid_rest_t imv_swid_rest_t;
-
-/**
- * Public REST interface
- */
-struct imv_swid_rest_t {
-
-       /**
-        * Post a HTTP request including a JSON object
-        *
-        * @param jreq          JSON object in HTTP request
-        * @param jresp         JSON object in HTTP response if NEED_MORE
-        * @return                      Status (SUCCESS, NEED_MORE or FAILED)
-        */
-       status_t (*post)(imv_swid_rest_t *this, char *command, json_object *jreq,
-                                        json_object **jresp);
-
-       /**
-        * Destroy imv_swid_rest_t object
-        */
-       void (*destroy)(imv_swid_rest_t *this);
-
-};
-
-/**
- * Create an imv_swid_rest_t instance
- *
- * @param uri                  REST URI (http://username:password@hostname[:port]/api/)
- * @param timeout              Timeout of the REST connection
- */
-imv_swid_rest_t* imv_swid_rest_create(char *uri, u_int timeout);
-
-#endif /** IMV_SWID_REST_H_ @}*/
index 0c548411ddbc6e476d7c4384221ad6efd7ab8e6d..b1726f0618a183cf260b42f82c608f07f35e22af 100644 (file)
@@ -16,7 +16,6 @@ imv_swima_la_LIBADD = \
 
 imv_swima_la_SOURCES = \
        imv_swima.c imv_swima_state.h imv_swima_state.c \
-       imv_swima_agent.h imv_swima_agent.c \
-       imv_swima_rest.h imv_swima_rest.c
+       imv_swima_agent.h imv_swima_agent.c
 
 imv_swima_la_LDFLAGS = -module -avoid-version -no-undefined
index 5d653fffc9f39df609beb8f2379b5b1a49485cab..eedc5da819ac6b702fae4a4a6c85301abc4b16c0 100644 (file)
 
 #include "imv_swima_agent.h"
 #include "imv_swima_state.h"
-#include "imv_swima_rest.h"
 
 #include <imcv.h>
 #include <imv/imv_agent.h>
 #include <imv/imv_msg.h>
+#include "rest/rest.h"
 #include "tcg/seg/tcg_seg_attr_max_size.h"
 #include "tcg/seg/tcg_seg_attr_seg_env.h"
 #include "ietf/swima/ietf_swima_attr_req.h"
@@ -73,7 +73,7 @@ struct private_imv_swima_agent_t {
        /**
         * REST API to strongTNC manager
         */
-       imv_swima_rest_t *rest_api;
+       rest_t *rest_api;
 
 };
 
@@ -770,7 +770,7 @@ imv_agent_if_t *imv_swima_agent_create(const char *name, TNC_IMVID id,
                                                "%s.plugins.imv-swima.rest_api_timeout", 120, lib->ns);
        if (rest_api_uri)
        {
-               this->rest_api = imv_swima_rest_create(rest_api_uri, rest_api_timeout);
+               this->rest_api = rest_create(rest_api_uri, rest_api_timeout);
        }
 
        return &this->public;
similarity index 50%
rename from src/libimcv/plugins/imv_swima/imv_swima_rest.c
rename to src/libimcv/rest/rest.c
index b82e38a6a5097d94105a9afb3f60647badee44f8..531da090e21c1154759b7c97d24198488e495624 100644 (file)
  * for more details.
  */
 
+#ifdef USE_JSON
+
 #define _GNU_SOURCE
 #include <stdio.h>
 
-#include "imv_swima_rest.h"
+#include "rest.h"
 
-typedef struct private_imv_swima_rest_t private_imv_swima_rest_t;
+typedef struct private_rest_t private_rest_t;
 
 /**
- * Private data of an imv_swima_rest_t object.
+ * Private data of an rest_t object.
  */
-struct private_imv_swima_rest_t {
+struct private_rest_t {
 
        /**
-        * Public members of imv_swima_rest_t
+        * Public members of rest_t
         */
-       imv_swima_rest_t public;
+       rest_t public;
 
        /**
         * URI of REST API
@@ -42,10 +44,41 @@ struct private_imv_swima_rest_t {
 
 };
 
+METHOD(rest_t, get, status_t,
+       private_rest_t *this, char *command, json_object **jresponse)
+{
+       struct json_tokener *tokener;
+       chunk_t response = chunk_empty;
+       status_t status;
+       char *uri;
+
+       if (asprintf(&uri, "%s%s",this->uri, command) < 0)
+       {
+               return FAILED;
+       }
+
+       status = lib->fetcher->fetch(lib->fetcher, uri, &response,
+                               FETCH_TIMEOUT, this->timeout,
+                               FETCH_END);
+       free(uri);
+
+       if (status == SUCCESS && jresponse)
+       {
+               /* Parse HTTP response into a JSON object */
+               tokener = json_tokener_new();
+               *jresponse = json_tokener_parse_ex(tokener, response.ptr, response.len);
+               json_tokener_free(tokener);
+       }
+       free(response.ptr);
+
+       return status;
+}
+
+#define HTTP_STATUS_CODE_NOT_FOUND                             404
 #define HTTP_STATUS_CODE_PRECONDITION_FAILED   412
 
-METHOD(imv_swima_rest_t, post, status_t,
-       private_imv_swima_rest_t *this, char *command, json_object *jrequest,
+METHOD(rest_t, post, status_t,
+       private_rest_t *this, char *command, json_object *jrequest,
        json_object **jresponse)
 {
        struct json_tokener *tokener;
@@ -72,22 +105,31 @@ METHOD(imv_swima_rest_t, post, status_t,
 
        if (status != SUCCESS)
        {
-               if (code != HTTP_STATUS_CODE_PRECONDITION_FAILED || !response.ptr)
+               switch (code)
                {
-                       DBG2(DBG_IMV, "REST http request failed with status code: %d", code);
-                       status = FAILED;
-               }
-               else
-               {
-                       if (jresponse)
-                       {
-                               /* Parse HTTP response into a JSON object */
-                               tokener = json_tokener_new();
-                               *jresponse = json_tokener_parse_ex(tokener, response.ptr,
-                                                                                                                       response.len);
-                               json_tokener_free(tokener);
-                       }
-                       status = NEED_MORE;
+                       case HTTP_STATUS_CODE_NOT_FOUND:
+                               status = NOT_FOUND;
+                               break;
+                       case HTTP_STATUS_CODE_PRECONDITION_FAILED:
+                               if (!response.ptr)
+                               {
+                                       return FAILED;
+                               }
+                               if (jresponse)
+                               {
+                                       /* Parse HTTP response into a JSON object */
+                                       tokener = json_tokener_new();
+                                       *jresponse = json_tokener_parse_ex(tokener, response.ptr,
+                                                                                                                               response.len);
+                                       json_tokener_free(tokener);
+                               }
+                               status = NEED_MORE;
+                               break;
+                       default:
+                               DBG2(DBG_IMV, "REST http request failed with status code: %d",
+                                                          code);
+                               status = FAILED;
+                               break;
                }
        }
        free(response.ptr);
@@ -95,8 +137,8 @@ METHOD(imv_swima_rest_t, post, status_t,
        return status;
 }
 
-METHOD(imv_swima_rest_t, destroy, void,
-       private_imv_swima_rest_t *this)
+METHOD(rest_t, destroy, void,
+       private_rest_t *this)
 {
        free(this->uri);
        free(this);
@@ -105,12 +147,13 @@ METHOD(imv_swima_rest_t, destroy, void,
 /**
  * Described in header.
  */
-imv_swima_rest_t *imv_swima_rest_create(char *uri, u_int timeout)
+rest_t *rest_create(char *uri, u_int timeout)
 {
-       private_imv_swima_rest_t *this;
+       private_rest_t *this;
 
        INIT(this,
                .public = {
+                       .get = _get,
                        .post = _post,
                        .destroy = _destroy,
                },
@@ -121,4 +164,4 @@ imv_swima_rest_t *imv_swima_rest_create(char *uri, u_int timeout)
        return &this->public;
 }
 
-
+#endif /* USE_JSON */
similarity index 64%
rename from src/libimcv/plugins/imv_swima/imv_swima_rest.h
rename to src/libimcv/rest/rest.h
index 55266ddbdc9832498e32c07bcce5fefee909d891..bddb881b82fb8e120bc3b602d5aa7399f2fafca9 100644 (file)
  * @defgroup imv_swima imv_swima
  * @ingroup libimcv_plugins
  *
- * @defgroup imv_swima_rest_t imv_swima_rest
+ * @defgroup rest_t rest
  * @{ @ingroup imv_swima
  */
 
-#ifndef IMV_SWIMA_REST_H_
-#define IMV_SWIMA_REST_H_
+#ifndef REST_H_
+#define REST_H_
 
-#include <library.h>
+#ifdef USE_JSON
 
+#include <library.h>
 #include <json.h>
 
-typedef struct imv_swima_rest_t imv_swima_rest_t;
+typedef struct rest_t rest_t;
 
 /**
  * Public REST interface
  */
-struct imv_swima_rest_t {
+struct rest_t {
 
        /**
-        * Post a HTTP request including a JSON object
+        * Send an HTTP GET request returning a JSON object
+        *
+        * @param jresp         JSON object in HTTP
+        * @return                      Status (SUCCESS or FAILED)
+        */
+       status_t (*get)(rest_t *this, char *command, json_object **jresp);
+
+       /**
+        * Send an HTTP POST request including a JSON object
         *
         * @param jreq          JSON object in HTTP request
         * @param jresp         JSON object in HTTP response if NEED_MORE
         * @return                      Status (SUCCESS, NEED_MORE or FAILED)
         */
-       status_t (*post)(imv_swima_rest_t *this, char *command, json_object *jreq,
+       status_t (*post)(rest_t *this, char *command, json_object *jreq,
                                         json_object **jresp);
 
        /**
-        * Destroy imv_swima_rest_t object
+        * Destroy rest_t object
         */
-       void (*destroy)(imv_swima_rest_t *this);
+       void (*destroy)(rest_t *this);
 
 };
 
 /**
- * Create an imv_swima_rest_t instance
+ * Create an rest_t instance
  *
  * @param uri                  REST URI (http://username:password@hostname[:port]/api/)
  * @param timeout              Timeout of the REST connection
  */
-imv_swima_rest_t* imv_swima_rest_create(char *uri, u_int timeout);
+rest_t* rest_create(char *uri, u_int timeout);
+
+#endif /* USE_JSON */
 
-#endif /** IMV_SWIMA_REST_H_ @}*/
+#endif /** REST_H_ @}*/