Discussion:
[Libstoragemgmt-devel] [PATCH 3/3] Example plug-in from wiki, stored here so I can find it and keep it updated.
Tony Asleson
2014-04-02 21:25:07 UTC
Permalink
Signed-off-by: Tony Asleson <***@redhat.com>
---
examples/plugin_example.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 81 insertions(+)
create mode 100644 examples/plugin_example.c

diff --git a/examples/plugin_example.c b/examples/plugin_example.c
new file mode 100644
index 0000000..f516901
--- /dev/null
+++ b/examples/plugin_example.c
@@ -0,0 +1,81 @@
+/*
+ * Web example.
+ * https://sourceforge.net/p/libstoragemgmt/wiki/WritingPlugins/
+ *
+ *
+ gcc -Wall -g -O0 plugin_example.c -I../include/ -L../src/.libs \
+ -lstoragemgmt -o c_example_lsmplugin
+ */
+
+ #include <libstoragemgmt/libstoragemgmt_plug_interface.h>
+ #include <stdlib.h>
+ #include <stdint.h>
+
+ static char name[] = "Simple limited plug-in example";
+ static char version [] = "0.01";
+
+ struct plugin_data {
+ uint32_t tmo;
+ /* All your other variables as needed */
+ };
+
+ /* Create the functions you plan on implementing that
+ match the callback signatures */
+ static int tmoSet(lsm_plugin_ptr c, uint32_t timeout, lsm_flag flags )
+ {
+ int rc = LSM_ERR_OK;
+ struct plugin_data *pd = (struct plugin_data*)lsm_private_data_get(c);
+ /* Do something with state to set timeout */
+ pd->tmo = timeout;
+ return rc;
+ }
+
+ static int tmoGet(lsm_plugin_ptr c, uint32_t *timeout, lsm_flag flags )
+ {
+ int rc = LSM_ERR_OK;
+ struct plugin_data *pd = (struct plugin_data*)lsm_private_data_get(c);
+ /* Do something with state to get timeout */
+ *timeout = pd->tmo;
+ return rc;
+ }
+
+ /* Setup the function addresses in the appropriate
+ required callback structure */
+ static struct lsm_mgmt_ops_v1 mgmOps = {
+ tmoSet,
+ tmoGet,
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+ };
+
+ int load( lsm_plugin_ptr c, const char *uri, const char *password,
+ uint32_t timeout, lsm_flag flags )
+ {
+ /* Do plug-in specific init. and setup callback structures */
+ struct plugin_data *data = (struct plugin_data *)
+ malloc(sizeof(struct plugin_data));
+
+ if (!data) {
+ return LSM_ERR_NO_MEMORY;
+ }
+
+ /* Call back into the framework */
+ int rc = lsm_register_plugin_v1( c, data, &mgmOps, NULL, NULL, NULL);
+ return rc;
+ }
+
+ int unload( lsm_plugin_ptr c, lsm_flag flags)
+ {
+ /* Get a handle to your private data and do clean-up */
+ struct plugin_data *pd = (struct plugin_data*)lsm_private_data_get(c);
+ free(pd);
+ return LSM_ERR_OK;
+ }
+
+ int main(int argc, char *argv[] )
+ {
+ return lsm_plugin_init_v1(argc, argv, load, unload, name, version);
+ }
--
1.8.2.1
Tony Asleson
2014-04-02 21:25:06 UTC
Permalink
This matches the expectation of a md5 instead of the
password md5 from crypt.

Signed-off-by: Tony Asleson <***@redhat.com>
---
configure.ac | 3 +++
plugin/Makefile.am | 2 +-
plugin/simc_lsmplugin.c | 19 ++++++++++++++++---
3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/configure.ac b/configure.ac
index 100bfdf..1211794 100644
--- a/configure.ac
+++ b/configure.ac
@@ -72,6 +72,9 @@ AC_CHECK_FUNCS([getpass memset socket strchr strdup strtol strtoul])
dnl Availability of various common headers (non-fatal if missing).
AC_CHECK_HEADERS([dlfcn.h])

+#Check for openssl development libs, we are using in simc_lsmplugin
+AC_CHECK_LIB([ssl], [SSL_library_init], [], AC_MSG_ERROR([Missing openssl libraries]))
+
#Check for json parser yajl
AC_CHECK_HEADERS([yajl/yajl_gen.h yajl/yajl_parse.h], [] , AC_MSG_ERROR([Missing yajl development headers]) )
AC_CHECK_HEADERS([yajl/yajl_version.h])
diff --git a/plugin/Makefile.am b/plugin/Makefile.am
index 557e15a..a0b200d 100644
--- a/plugin/Makefile.am
+++ b/plugin/Makefile.am
@@ -3,6 +3,6 @@ AM_CPPFLAGS= -I$(top_srcdir)/include -***@srcdir@/include \

bin_PROGRAMS = simc_lsmplugin

-simc_lsmplugin_LDADD= ../src/libstoragemgmt.la -lcrypt $(LIBGLIB_LIBS)
+simc_lsmplugin_LDADD= ../src/libstoragemgmt.la $(LIBGLIB_LIBS) $(LIBSSL_LIBS)
simc_lsmplugin_SOURCES= \
simc_lsmplugin.c
diff --git a/plugin/simc_lsmplugin.c b/plugin/simc_lsmplugin.c
index 194a085..f4f422a 100644
--- a/plugin/simc_lsmplugin.c
+++ b/plugin/simc_lsmplugin.c
@@ -28,6 +28,7 @@
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
+#include <openssl/md5.h>

#ifdef __cplusplus
extern "C" {
@@ -43,13 +44,25 @@ static char sys_id[] = "sim-01";
#define MAX_EXPORT 32

/**
- * Creates a pseudo md5 (DO NOT FREE RETURN VALUE!)
+ * Creates a md5 string (DO NOT FREE RETURN VALUE as the string is static)
* @param data Data to generate md5
- * @return Pointer to character array.
+ * @return Pointer to string which contains the string digest
*/
char* md5(const char *data)
{
- return crypt(data, "$1$LSM$");
+ int i = 0;
+ MD5_CTX c;
+ unsigned char digest[16];
+ static char digest_str[33];
+
+ MD5_Init(&c);
+ MD5_Update(&c, data, strlen(data));
+ MD5_Final(digest, &c);
+
+ for( i = 0; i < sizeof(digest); ++i ) {
+ sprintf(&digest_str[i*2], "%02x", (unsigned int)digest[i]);
+ }
+ return digest_str;
}

/**
--
1.8.2.1
Loading...