0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 #include <ndrx_config.h>
0035 #include <string.h>
0036 #include <stdio.h>
0037 #include <stdlib.h>
0038 #include <dlfcn.h>
0039
0040 #include <ndrstandard.h>
0041 #include <expluginbase.h>
0042 #include <excrypto.h>
0043 #include <inttypes.h>
0044
0045 #include "ndebug.h"
0046 #include "userlog.h"
0047
0048
0049
0050 #define OFSZ(e) EXOFFSET(ndrx_pluginbase_t,p_ndrx_##e), EXOFFSET(ndrx_pluginbase_t,ndrx_##e##_provider)
0051
0052
0053
0054
0055
0056
0057 struct plugin_loader_map
0058 {
0059 char *symb;
0060 int func_off;
0061 int provider_off;
0062 long flags;
0063 };
0064 typedef struct plugin_loader_map plugin_loader_map_t;
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076 expublic ndrx_pluginbase_t ndrx_G_plugins = {
0077
0078
0079 .plugins_loaded = EXFALSE
0080
0081
0082 ,.p_ndrx_crypto_getkey = ndrx_crypto_getkey_std
0083 ,.ndrx_crypto_getkey_provider = "built in"
0084
0085
0086 ,.p_ndrx_tplogprintubf_hook = NULL
0087 ,.ndrx_tplogprintubf_hook_provider = "none"
0088 };
0089
0090
0091
0092
0093
0094
0095 exprivate plugin_loader_map_t M_map_driver[] =
0096 {
0097 {NDRX_PLUGIN_CRYPTO_GETKEY_SYMB, OFSZ(crypto_getkey), NDRX_PLUGIN_FUNC_ENCKEY}
0098 ,{NDRX_PLUGIN_TPLOGPRINTUBF_HOOK_SYMB, OFSZ(tplogprintubf_hook), NDRX_PLUGIN_FUNC_TPLOGPRINTUBF_HOOK}
0099 ,{NULL}
0100 };
0101
0102
0103
0104
0105
0106
0107
0108
0109 expublic int ndrx_plugins_loadone(char *fname)
0110 {
0111 int ret = EXSUCCEED;
0112 void *handle;
0113 ndrx_plugin_init_t init;
0114 ndrx_plugin_crypto_getkey_t crypto;
0115 long flags;
0116 char provider[NDRX_PLUGIN_PROVIDERSTR_BUFSZ];
0117
0118 plugin_loader_map_t *p = M_map_driver;
0119
0120
0121 handle = dlopen(fname, RTLD_LOCAL | RTLD_LAZY);
0122
0123 if (NULL==handle)
0124 {
0125 NDRX_LOG_EARLY(log_error, "Failed to load [%s]: %s", fname, dlerror());
0126 EXFAIL_OUT(ret);
0127 }
0128
0129
0130 init = (ndrx_plugin_init_t)dlsym(handle, NDRX_PLUGIN_INIT_SYMB);
0131
0132 if (NULL==init)
0133 {
0134 NDRX_LOG_EARLY(log_error, "Invalid plugin [%s] - symbol [%s] not found: %s",
0135 fname, NDRX_PLUGIN_INIT_SYMB, dlerror());
0136 userlog("Invalid plugin [%s] - symbol [%s] not found: %s",
0137 fname, NDRX_PLUGIN_INIT_SYMB, dlerror());
0138 EXFAIL_OUT(ret);
0139 }
0140
0141 NDRX_LOG_EARLY(log_debug, "About to call init: %p", init);
0142
0143 flags = init(provider, sizeof(provider));
0144
0145 if (EXFAIL==flags)
0146 {
0147 NDRX_LOG_EARLY(log_error, "Invalid plugin [%s] init failed!", fname);
0148 userlog("Invalid plugin [%s] init failed!", fname);
0149 EXFAIL_OUT(ret);
0150 }
0151
0152
0153
0154 NDRX_LOG_EARLY(log_info, "[%s] flags %lx", fname, flags);
0155
0156 while (NULL!=p->symb)
0157 {
0158 if (flags & p->flags)
0159 {
0160 void *fptr = dlsym(handle, p->symb);
0161 void **func_ptr = (void *)(((char *)&ndrx_G_plugins) + p->func_off);
0162 char *prov_ptr = ((char *)&ndrx_G_plugins) + p->provider_off;
0163
0164 if (NULL==fptr)
0165 {
0166 NDRX_LOG_EARLY(log_error, "Invalid plugin [%s] - symbol [%s] not "
0167 "found (flags " PRIx64 "): %s",
0168 fname, p->flags, flags, dlerror());
0169 userlog("Invalid plugin [%s] - symbol [%s] not "
0170 "found (flags " PRIx64 "): %s",
0171 fname, p->flags, flags, dlerror());
0172 EXFAIL_OUT(ret);
0173 }
0174
0175 NDRX_LOG_EARLY(log_info, "Plugin [%s] provides [%s] function",
0176 provider, p->symb);
0177
0178 *func_ptr = fptr;
0179 NDRX_STRCPY_SAFE_DST(prov_ptr, provider, NDRX_PLUGIN_PROVIDERSTR_BUFSZ);
0180
0181 }
0182
0183 p++;
0184 }
0185
0186 out:
0187 if (EXSUCCEED!=ret && NULL!=handle)
0188 {
0189 dlclose(handle);
0190 }
0191 return ret;
0192 }
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206 expublic int ndrx_plugins_load(void)
0207 {
0208 int ret = EXSUCCEED;
0209 char *plugins_env = getenv(CONF_NDRX_PLUGINS);
0210 char *plugins = NULL;
0211 char *p;
0212 char *fname;
0213 char *save_ptr;
0214
0215 if (NULL==plugins_env)
0216 {
0217 NDRX_LOG_EARLY(log_info, "No plugins defined by %s env variable",
0218 CONF_NDRX_PLUGINS);
0219
0220 goto out;
0221 }
0222
0223
0224
0225 plugins = NDRX_STRDUP(plugins_env);
0226
0227 NDRX_LOG_EARLY(log_debug, "%s: loading plugins.... [%s]", __func__, plugins);
0228
0229 p = strtok_r (plugins, ";", &save_ptr);
0230
0231 while (NULL!=p)
0232 {
0233
0234
0235 fname = ndrx_str_lstrip_ptr(p, " \t");
0236 ndrx_str_rstrip(fname, " \t");
0237
0238 NDRX_LOG_EARLY(log_info, "About to load: [%s]", fname);
0239
0240
0241 if (EXSUCCEED!=ndrx_plugins_loadone(fname))
0242 {
0243 userlog("Failed to load [%s] plugin...", fname);
0244 }
0245
0246 p = strtok_r (NULL, ";", &save_ptr);
0247 }
0248
0249 out:
0250 if (NULL!=plugins)
0251 {
0252 NDRX_FREE(plugins);
0253 }
0254 return ret;
0255 }
0256
0257