diff options
-rw-r--r-- | include/local-def.h | 14 | ||||
-rw-r--r-- | include/proto-def.h | 13 | ||||
-rw-r--r-- | nbproject/configurations.xml | 14 | ||||
-rw-r--r-- | nbproject/private/configurations.xml | 5 | ||||
-rw-r--r-- | src/afbs-api.c | 78 | ||||
-rw-r--r-- | src/alsa-api.c | 14 | ||||
-rw-r--r-- | src/config.c | 7 | ||||
-rw-r--r-- | src/dbus-api.c | 7 | ||||
-rw-r--r-- | src/main.c | 20 | ||||
-rw-r--r-- | src/radio-api.c | 8 | ||||
-rw-r--r-- | src/rest-api.c | 34 | ||||
-rw-r--r-- | src/session.c | 49 |
12 files changed, 149 insertions, 114 deletions
diff --git a/include/local-def.h b/include/local-def.h index df66a09e..791f4c19 100644 --- a/include/local-def.h +++ b/include/local-def.h @@ -59,6 +59,7 @@ #define DEFLT_API_TIMEOUT 0 // default Plugin API Timeout [0=NoLimit for Debug Only] #define DEFLT_API_TIMEOUT 0 // default Plugin API Timeout #define DEFLT_CACHE_TIMEOUT 100000 // default Static File Chache [Client Side Cache 100000~=1day] +#define DEFLT_AUTH_TOKEN NULL // expect for debug should == NULL typedef int BOOL; #ifndef FALSE @@ -74,6 +75,10 @@ typedef int BOOL; extern int verbose; // this is the only global variable + +// Plugin Type +typedef enum {AFB_PLUGIN_JSON=123456789, AFB_PLUGIN_JSCRIPT=987654321, AFB_PLUGIN_RAW=987123546} AFB_pluginT; + // prebuild json error are constructed in config.c typedef enum { AFB_FALSE, AFB_TRUE, AFB_FATAL, AFB_FAIL, AFB_WARNING, AFB_EMPTY, AFB_SUCCESS, AFB_DONE} AFB_error; @@ -85,8 +90,7 @@ extern char *ERROR_LABEL[]; #define MAX_POST_SIZE 4096 // maximum size for POST data #define CTX_NBCLIENTS 10 // allow a default of 10 authenticated clients -// use to check anonymous data when using dynamic loadable lib -typedef enum {AFB_PLUGIN=1234, AFB_REQUEST=5678} AFB_type; + typedef json_object* (*AFB_apiCB)(); // Error code are requested through function to manage json usage count @@ -136,7 +140,8 @@ typedef struct { char *pidfile; // where to store pid when running background char *sessiondir; // where to store mixer session files char *configfile; // where to store configuration on gateway exit - char *setuid; + char *setuid; // downgrade uid to username + char *token; // initial authentication token [default NULL no session] int cacheTimeout; int apiTimeout; int cntxTimeout; // Client Session Context timeout @@ -196,7 +201,7 @@ typedef struct { // Plugin definition typedef struct { - AFB_type type; + AFB_pluginT type; char *info; char *prefix; size_t prefixlen; @@ -204,7 +209,6 @@ typedef struct { AFB_restapi *apis; void *handle; int ctxCount; - AFB_clientCtx *ctxGlobal; } AFB_plugin; diff --git a/include/proto-def.h b/include/proto-def.h index 25834524..b6cdb94e 100644 --- a/include/proto-def.h +++ b/include/proto-def.h @@ -22,7 +22,7 @@ // Rest-api -PUBLIC json_object* apiPingTest(AFB_request *request); +PUBLIC json_object* apiPingTest(AFB_request *request, void *pluginHandle); PUBLIC const char* getQueryValue (AFB_request * request, char *name); PUBLIC int getQueryAll(AFB_request * request, char *query, size_t len); @@ -38,18 +38,17 @@ PUBLIC AFB_plugin* dbusRegister (); PUBLIC AFB_plugin* alsaRegister (); PUBLIC AFB_plugin* radioRegister (AFB_session *session); - - // Session handling PUBLIC AFB_error sessionCheckdir (AFB_session *session); PUBLIC json_object *sessionList (AFB_session *session, AFB_request *request); PUBLIC json_object *sessionToDisk (AFB_session *session, AFB_request *request, char *name,json_object *jsonSession); PUBLIC json_object *sessionFromDisk (AFB_session *session, AFB_request *request, char *name); -PUBLIC char* ctxTokenRefresh (AFB_request *request); -PUBLIC char* ctxTokenCreate (AFB_request *request); + +PUBLIC AFB_error ctxTokenRefresh (AFB_request *request); +PUBLIC AFB_error ctxTokenCreate (AFB_request *request); PUBLIC AFB_error ctxTokenCheck (AFB_request *request); -PUBLIC int ctxTokenReset (AFB_request *request); -PUBLIC int ctxClientGet (AFB_request *request); +PUBLIC AFB_error ctxTokenReset (AFB_request *request); +PUBLIC AFB_error ctxClientGet (AFB_request *request); diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 4b821789..5fcbb4ea 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -74,12 +74,22 @@ </cTool> </item> <item path="src/alsa-api.c" ex="false" tool="0" flavor2="2"> - <cTool flags="0"> + <cTool flags="1"> <incDir> - <pElem>include</pElem> + <pElem>src</pElem> <pElem>/usr/include/json-c</pElem> + <pElem>include</pElem> + <pElem>/usr/include/uuid</pElem> <pElem>build/src</pElem> </incDir> + <preprocessorList> + <Elem>__PIC__=2</Elem> + <Elem>__PIE__=2</Elem> + <Elem>__REGISTER_PREFIX__=</Elem> + <Elem>__USER_LABEL_PREFIX__=</Elem> + <Elem>__pic__=2</Elem> + <Elem>__pie__=2</Elem> + </preprocessorList> </cTool> </item> <item path="src/config.c" ex="false" tool="0" flavor2="2"> diff --git a/nbproject/private/configurations.xml b/nbproject/private/configurations.xml index 4cc0c9f9..0c74ed30 100644 --- a/nbproject/private/configurations.xml +++ b/nbproject/private/configurations.xml @@ -99,8 +99,11 @@ <runcommandpicklist> <runcommandpicklistitem>"${OUTPUT_PATH}"</runcommandpicklistitem> <runcommandpicklistitem>"${OUTPUT_PATH}" --verbose --alias=icons:/usr/share/icons</runcommandpicklistitem> + <runcommandpicklistitem>"${OUTPUT_PATH}" --verbose --alias=icons:/usr/share/icons +--token=123456789</runcommandpicklistitem> + <runcommandpicklistitem>"${OUTPUT_PATH}" --verbose --alias=icons:/usr/share/icons --token=123456789</runcommandpicklistitem> </runcommandpicklist> - <runcommand>"${OUTPUT_PATH}" --verbose --alias=icons:/usr/share/icons</runcommand> + <runcommand>"${OUTPUT_PATH}" --verbose --alias=icons:/usr/share/icons --token=123456789</runcommand> <rundir>build</rundir> <buildfirst>true</buildfirst> <terminal-type>0</terminal-type> diff --git a/src/afbs-api.c b/src/afbs-api.c index f5a55a43..42ea7597 100644 --- a/src/afbs-api.c +++ b/src/afbs-api.c @@ -27,20 +27,24 @@ typedef struct { // Request Creation of new context if it does not exist -PUBLIC json_object* clientContextCreate (AFB_request *request) { +STATIC json_object* clientContextCreate (AFB_request *request) { json_object *jresp; int res; char *token; AFB_clientCtx *client=request->client; // get client context from request - + // check we do not already have a session - if (client->handle != NULL) { + if ((client != NULL) && (client->handle != NULL)) { request->errcode=MHD_HTTP_FORBIDDEN; return (jsonNewMessage(AFB_FAIL, "Token exist use refresh")); } - // request a new client context token and check result - ctxTokenCreate (request); + // request a new client context token and check result + if (AFB_SUCCESS != ctxTokenCreate (request)) { + request->errcode=MHD_HTTP_UNAUTHORIZED; + jresp= jsonNewMessage(AFB_FAIL, "Token Session Not Activated [restart with --token=xxxx]"); + return (jresp); + } // add a client handle to session client->handle = malloc (sizeof (MyClientApplicationHandle)); @@ -53,19 +57,16 @@ PUBLIC json_object* clientContextCreate (AFB_request *request) { } // Renew an existing context -PUBLIC json_object* clientContextRefresh (AFB_request *request) { +STATIC json_object* clientContextRefresh (AFB_request *request) { json_object *jresp; - // check we do not already have a session - if (request->client == NULL) return (jsonNewMessage(AFB_FAIL, "No Previous Token use Create")); - // note: we do not need to parse the old token as clientContextRefresh doit for us - if (ctxTokenRefresh (request)) { - jresp = json_object_new_object(); - json_object_object_add(jresp, "token", json_object_new_string (request->client->token)); - } else { + if (AFB_SUCCESS != ctxTokenRefresh (request)) { request->errcode=MHD_HTTP_UNAUTHORIZED; jresp= jsonNewMessage(AFB_FAIL, "Token Exchange Broken Refresh Refused"); + } else { + jresp = json_object_new_object(); + json_object_object_add(jresp, "token", json_object_new_string (request->client->token)); } return (jresp); @@ -73,49 +74,54 @@ PUBLIC json_object* clientContextRefresh (AFB_request *request) { // Verify a context is still valid -PUBLIC json_object* clientContextCheck (AFB_request *request) { - json_object *jresp; - int isvalid; - - // check is token is valid - isvalid= ctxTokenCheck (request); +STATIC json_object* clientContextCheck (AFB_request *request) { - // add an error code to respond - if (!isvalid) request->errcode=MHD_HTTP_UNAUTHORIZED; - - // prepare response for client side application - jresp = json_object_new_object(); - json_object_object_add(jresp, "isvalid", json_object_new_boolean (isvalid)); + json_object *jresp = json_object_new_object(); + // add an error code to respond + if (AFB_SUCCESS != ctxTokenCheck (request)) { + request->errcode=MHD_HTTP_UNAUTHORIZED; + json_object_object_add(jresp, "isvalid", json_object_new_boolean (FALSE)); + } else { + json_object_object_add(jresp, "isvalid", json_object_new_boolean (TRUE)); + } + return (jresp); } // Close and Free context -PUBLIC json_object* clientContextReset (AFB_request *request) { +STATIC json_object* clientContextReset (AFB_request *request) { json_object *jresp; - - jresp = json_object_new_object(); - json_object_object_add(jresp, "done", json_object_new_boolean (ctxTokenReset (request))); + + // note: we do not need to parse the old token as clientContextRefresh doit for us + if (AFB_SUCCESS != ctxTokenReset (request)) { + request->errcode=MHD_HTTP_UNAUTHORIZED; + jresp= jsonNewMessage(AFB_FAIL, "No Token Client Context [use --token=xxx]"); + } else { + jresp = json_object_new_object(); + json_object_object_add(jresp, "uuid", json_object_new_string (request->client->uuid)); + } return (jresp); } STATIC AFB_restapi pluginApis[]= { - {"ping" , (AFB_apiCB)apiPingTest ,"Ping Rest Test Service", NULL}, - {"token-create" , (AFB_apiCB)clientContextCreate ,"Request Client Context Creation",NULL}, - {"token-refresh" , (AFB_apiCB)clientContextRefresh,"Refresh Client Context Token",NULL}, - {"token-check" , (AFB_apiCB)clientContextCheck ,"Check Client Context Token",NULL}, - {"token-reset" , (AFB_apiCB)clientContextReset ,"Close Client Context and Free resources",NULL}, - {0,0,0,0} + {"ping" , (AFB_apiCB)apiPingTest ,"Ping Rest Test Service"}, + {"token-create" , (AFB_apiCB)clientContextCreate ,"Request Client Context Creation"}, + {"token-refresh" , (AFB_apiCB)clientContextRefresh,"Refresh Client Context Token"}, + {"token-check" , (AFB_apiCB)clientContextCheck ,"Check Client Context Token"}, + {"token-reset" , (AFB_apiCB)clientContextReset ,"Close Client Context and Free resources"}, + {NULL} }; PUBLIC AFB_plugin *afsvRegister () { AFB_plugin *plugin = malloc (sizeof (AFB_plugin)); - plugin->type = AFB_PLUGIN; + plugin->type = AFB_PLUGIN_JSON; plugin->info = "Application Framework Binder Service"; plugin->prefix= "afbs"; // url base plugin->apis = pluginApis; + plugin->handle= (void*) "What ever you want"; return (plugin); };
\ No newline at end of file diff --git a/src/alsa-api.c b/src/alsa-api.c index 18529d93..70311462 100644 --- a/src/alsa-api.c +++ b/src/alsa-api.c @@ -52,19 +52,19 @@ STATIC struct { STATIC AFB_restapi pluginApis[]= { - {"ping" , (AFB_apiCB)pingSample , "Ping Application Framework",NULL}, - {"error" , (AFB_apiCB)wrongApi , "Ping Application Framework",NULL}, - {"ctx-store", (AFB_apiCB)pingSample , "Verbose Mode",NULL}, - {"ctx-load" , (AFB_apiCB)pingSample , "Verbose Mode",NULL}, - {0,0,0,0} + {"ping" , (AFB_apiCB)pingSample , "Ping Application Framework"}, + {"error" , (AFB_apiCB)wrongApi , "Ping Application Framework"}, + {"ctx-store", (AFB_apiCB)pingSample , "Verbose Mode"}, + {"ctx-load" , (AFB_apiCB)pingSample , "Verbose Mode"}, + {NULL} }; PUBLIC AFB_plugin *alsaRegister () { AFB_plugin *plugin = malloc (sizeof (AFB_plugin)); - plugin->type = AFB_PLUGIN; + plugin->type = AFB_PLUGIN_JSON; plugin->info = "Application Framework Binder Service"; plugin->prefix= "alsa"; plugin->apis = pluginApis; - + return (plugin); };
\ No newline at end of file diff --git a/src/config.c b/src/config.c index 29d7241d..2ec90595 100644 --- a/src/config.c +++ b/src/config.c @@ -71,6 +71,10 @@ PUBLIC AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig) { // default Plugin API timeout if (cliconfig->apiTimeout == 0) session->config->apiTimeout=DEFLT_API_TIMEOUT; else session->config->apiTimeout=cliconfig->apiTimeout; + + // default AUTH_TOKEN + if (cliconfig->token == NULL) session->config->token= DEFLT_AUTH_TOKEN; + else session->config->token=cliconfig->token; // cache timeout default one hour if (cliconfig->cacheTimeout == 0) session->config->cacheTimeout=DEFLT_CACHE_TIMEOUT; @@ -303,9 +307,6 @@ PUBLIC AFB_session *configInit () { } verbose = verbosesav; - // Load Plugins - initPlugins (session); - return (session); } diff --git a/src/dbus-api.c b/src/dbus-api.c index f7f64fab..d7097fc7 100644 --- a/src/dbus-api.c +++ b/src/dbus-api.c @@ -81,17 +81,16 @@ STATIC AFB_restapi pluginApis[]= { {"pingJson" , (AFB_apiCB)pingJson , "Return a JSON object"}, {"ctx-store", (AFB_apiCB)pingSample , "Verbose Mode"}, {"ctx-load" , (AFB_apiCB)pingSample , "Verbose Mode"}, - {0,0,0} + {NULL} }; PUBLIC AFB_plugin *dbusRegister () { AFB_plugin *plugin = malloc (sizeof (AFB_plugin)); - plugin->type = AFB_PLUGIN; + plugin->type = AFB_PLUGIN_JSON; plugin->info = "Application Framework Binder Service"; plugin->prefix= "dbus"; plugin->apis = pluginApis; - plugin->handle= (void*) "Any you Want"; - + plugin->handle= (void*) "Any you Want"; return (plugin); };
\ No newline at end of file @@ -72,9 +72,10 @@ static sigjmp_buf exitPoint; // context save for set/longjmp #define SET_CONFIG_EXIT 138 #define SET_SMACK 140 - #define SET_PLUGINS 141 - #define SET_APITIMEOUT 142 - #define SET_CNTXTIMEOUT 143 + #define SET_AUTH_TOKEN 141 + #define SET_PLUGINS 142 + #define SET_APITIMEOUT 143 + #define SET_CNTXTIMEOUT 144 #define DISPLAY_VERSION 150 #define DISPLAY_HELP 151 @@ -108,6 +109,7 @@ static AFB_options cliOptions [] = { {SET_SMACK ,1,"smack" , "Set Smack Label [default demo]"}, {SET_PLUGINS ,1,"mods" , "Enable module [default all]"}, + {SET_AUTH_TOKEN ,1,"token" , "Initial Secret [default=non]"}, {DISPLAY_VERSION ,0,"version" , "Display version and copyright"}, {DISPLAY_HELP ,0,"help" , "Display this help"}, @@ -157,7 +159,7 @@ void signalQuit (int signum) { fprintf (stderr," --%-15s %s\n", command, cliOptions[ind].help); } } - fprintf (stderr,"Example:\n %s\\\n --verbose --port=1234 --smack=xxxx --mods=alsa:dbus\n", name); + fprintf (stderr,"Example:\n %s\\\n --verbose --port=1234 --smack=xxxx --token='azerty' --mods=alsa:dbus\n", name); } // end printHelp /*---------------------------------------------------------- @@ -348,6 +350,11 @@ int main(int argc, char *argv[]) { cliconfig.smack = optarg; break; + case SET_AUTH_TOKEN: + if (optarg == 0) goto needValueForOption; + cliconfig.token = optarg; + break; + case SET_PLUGINS: if (optarg == 0) goto needValueForOption; fprintf (stderr, "Not Implemented yet\n"); @@ -427,11 +434,10 @@ int main(int argc, char *argv[]) { } } - // Create session config - configInit (/* session & config are initialized globally */); - + // if exist merge config file with CLI arguments configLoadFile (session, &cliconfig); + initPlugins(session); // ------------------ sanity check ---------------------------------------- if ((session->background) && (session->foreground)) { diff --git a/src/radio-api.c b/src/radio-api.c index 37da4ebc..18562c32 100644 --- a/src/radio-api.c +++ b/src/radio-api.c @@ -493,14 +493,14 @@ STATIC struct { STATIC AFB_restapi pluginApis[]= { - {"start" , (AFB_apiCB)start , "Ping Application Framework", NULL}, - {"stop" , (AFB_apiCB)stop , "Ping Application Framework", NULL}, - {0,0,0} + {"start" , (AFB_apiCB)start , "Ping Application Framework"}, + {"stop" , (AFB_apiCB)stop , "Ping Application Framework"}, + {NULL} }; PUBLIC AFB_plugin *radioRegister (AFB_session *session) { AFB_plugin *plugin = malloc (sizeof (AFB_plugin)); - plugin->type = AFB_PLUGIN; + plugin->type = AFB_PLUGIN_JSON; plugin->info = "Application Framework Binder - Radio plugin"; plugin->prefix = "radio"; plugin->apis = pluginApis; diff --git a/src/rest-api.c b/src/rest-api.c index 4f06066e..83bb2d22 100644 --- a/src/rest-api.c +++ b/src/rest-api.c @@ -37,7 +37,7 @@ static json_object *afbJsonType; // Sample Generic Ping Debug API -PUBLIC json_object* apiPingTest(AFB_request *request) { +PUBLIC json_object* apiPingTest(AFB_request *request, void *pluginHandle) { static pingcount = 0; json_object *response; char query [512]; @@ -51,8 +51,8 @@ PUBLIC json_object* apiPingTest(AFB_request *request) { if (request->post == NULL) request->post="NoData"; // return response to caller - response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon count=%d CtxtId=%d Loa=%d query={%s} PostData: \'%s\' " - , pingcount++, request->client->cid, request->loa, query, request->post); + response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon count=%d CtxtId=%d Loa=%d query={%s} Handle=0x%x PostData: \'%s\' " + , pingcount++, request->client->cid, request->loa, query, request->post, pluginHandle); return (response); } @@ -154,7 +154,7 @@ STATIC AFB_error callPluginApi(AFB_plugin *plugin, AFB_request *request) { ctxClientGet(request); // Effectively call the API with a subset of the context - jresp = plugin->apis[idx].callback(request); + jresp = plugin->apis[idx].callback(request, plugin->handle); // API should return NULL of a valid Json Object if (jresp == NULL) { @@ -278,11 +278,11 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co // build request structure memset(&request, 0, sizeof (request)); request.connection = connection; - request.config = session->config; - request.url = url; + request.config = session->config; + request.url = url; request.plugin = baseurl; - request.api = baseapi; - request.jresp = json_object_new_object(); + request.api = baseapi; + request.jresp = json_object_new_object(); // increase reference count and add jtype to response json_object_get (afbJsonType); @@ -312,7 +312,7 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co free(urlcpy1); // client did not pass token on URI let's use cookies - if (!request.restfull) { + if ((!request.restfull) && (request.client != NULL)) { char cookie[64]; snprintf (cookie, sizeof (cookie), "%s=%s", COOKIE_NAME, request.client->uuid); MHD_add_response_header (webResponse, MHD_HTTP_HEADER_SET_COOKIE, cookie); @@ -339,12 +339,12 @@ ExitOnError: // Loop on plugins. Check that they have the right type, prepare a JSON object with prefix -STATIC AFB_plugin ** RegisterPlugins(AFB_plugin **plugins) { +STATIC AFB_plugin ** RegisterJsonPlugins(AFB_plugin **plugins) { int idx, jdx; for (idx = 0; plugins[idx] != NULL; idx++) { - if (plugins[idx]->type != AFB_PLUGIN) { - fprintf(stderr, "ERROR: AFSV plugin[%d] invalid type=%d != %d\n", idx, AFB_PLUGIN, plugins[idx]->type); + if (plugins[idx]->type != AFB_PLUGIN_JSON) { + fprintf(stderr, "ERROR: AFSV plugin[%d] invalid type=%d != %d\n", idx, AFB_PLUGIN_JSON, plugins[idx]->type); } else { // some sanity controls if ((plugins[idx]->prefix == NULL) || (plugins[idx]->info == NULL) || (plugins[idx]->apis == NULL)) { @@ -356,10 +356,6 @@ STATIC AFB_plugin ** RegisterPlugins(AFB_plugin **plugins) { if (verbose) fprintf(stderr, "Loading plugin[%d] prefix=[%s] info=%s\n", idx, plugins[idx]->prefix, plugins[idx]->info); - // register private to plugin global context [cid=0] - plugins[idx]->ctxGlobal= malloc (sizeof (AFB_clientCtx)); - plugins[idx]->ctxGlobal->cid=0; - // Prebuild plugin jtype to boost API response plugins[idx]->jtype = json_object_new_string(plugins[idx]->prefix); json_object_get(plugins[idx]->jtype); // increase reference count to make it permanent @@ -370,8 +366,8 @@ STATIC AFB_plugin ** RegisterPlugins(AFB_plugin **plugins) { for (jdx = 0; plugins[idx]->apis[jdx].name != NULL; jdx++) { AFB_privateApi *private = malloc (sizeof (AFB_privateApi)); if (plugins[idx]->apis[jdx].private != NULL) { - fprintf (stderr, "WARNING: plugin=%s api=%s private handle should be NULL\n" - ,plugins[idx]->prefix,plugins[idx]->apis[jdx].name); + fprintf (stderr, "WARNING: plugin=%s api=%s private handle should be NULL=0x%x\n" + ,plugins[idx]->prefix,plugins[idx]->apis[jdx].name, plugins[idx]->apis[jdx].private); } private->len = strlen (plugins[idx]->apis[jdx].name); private->jtype=json_object_new_string(plugins[idx]->apis[jdx].name); @@ -397,5 +393,5 @@ void initPlugins(AFB_session *session) { plugins[i++] = NULL; // complete plugins and save them within current sessions - session->plugins = RegisterPlugins(plugins); + session->plugins = RegisterJsonPlugins(plugins); } diff --git a/src/session.c b/src/session.c index cd5b09f7..a5a00406 100644 --- a/src/session.c +++ b/src/session.c @@ -270,7 +270,7 @@ PUBLIC json_object * sessionToDisk (AFB_session *session, AFB_request *request, // info is a valid AFB_info type if (!json_object_object_get_ex (info, "jtype", &jtype)) { - response = jsonNewMessage (AFB_EMPTY,"sndcard=%s session=%s No 'AFB_type' args=%s", request->plugin, name, request->post); + response = jsonNewMessage (AFB_EMPTY,"sndcard=%s session=%s No 'AFB_pluginT' args=%s", request->plugin, name, request->post); goto OnErrorExit; } @@ -379,17 +379,19 @@ PUBLIC int ctxStoreGarbage (struct lh_table *lht, const int timeout) { } // This function will return exiting client context or newly created client context -PUBLIC int ctxClientGet (AFB_request *request) { +PUBLIC AFB_error ctxClientGet (AFB_request *request) { static int cid=0; AFB_clientCtx *clientCtx=NULL; const char *uuid; uuid_t newuuid; int ret; - // if client session store is null create it - if (clientCtxs == NULL) { + if (request->config->token == NULL) return AFB_EMPTY; + + // if client session store is null create it + if (clientCtxs == NULL) { clientCtxs= ctxStoreCreate(CTX_NBCLIENTS); - } + } // Check if client as a context or not inside the URL uuid = MHD_lookup_connection_value(request->connection, MHD_GET_ARGUMENT_KIND, "uuid"); @@ -424,18 +426,21 @@ PUBLIC int ctxClientGet (AFB_request *request) { if(clientCtxs->count > (clientCtxs->size*0.5)) ctxStoreGarbage(clientCtxs, request->config->cntxTimeout); // finally add uuid into hashtable - ret= lh_table_insert (clientCtxs, (void*)clientCtx->uuid, clientCtx); + ret=lh_table_insert (clientCtxs, (void*)clientCtx->uuid, clientCtx); + if (ret < 0) return (AFB_FAIL); - if (verbose) fprintf (stderr, "ctxClientGet New uuid=[%s] token=[%s] timestamp=%d\n", clientCtx->uuid, clientCtx->token, clientCtx->timeStamp); - + if (verbose) fprintf (stderr, "ctxClientGet New uuid=[%s] token=[%s] timestamp=%d\n", clientCtx->uuid, clientCtx->token, clientCtx->timeStamp); request->client = clientCtx; - return (ret); + + return (AFB_SUCCESS); } // Sample Generic Ping Debug API PUBLIC AFB_error ctxTokenCheck (AFB_request *request) { const char *token; + if (request->client == NULL) return AFB_EMPTY; + // this time have to extract token from query list token = MHD_lookup_connection_value(request->connection, MHD_GET_ARGUMENT_KIND, "token"); @@ -444,32 +449,36 @@ PUBLIC AFB_error ctxTokenCheck (AFB_request *request) { // compare current token with previous one if ((0 == strcmp (token, request->client->token)) && (!ctxStoreToOld (request->client, request->config->cntxTimeout))) { - return (AFB_TRUE); + return (AFB_SUCCESS); } // Token is not valid let move level of assurance to zero and free attached client handle - return (AFB_FALSE); + return (AFB_FAIL); } // Free Client Session Context -PUBLIC int ctxTokenReset (AFB_request *request) { +PUBLIC AFB_error ctxTokenReset (AFB_request *request) { struct lh_entry* entry; int ret; - + + if (request->client == NULL) return AFB_EMPTY; + entry = lh_table_lookup_entry (clientCtxs, request->client->uuid); - if (entry == NULL) return FALSE; + if (entry == NULL) return AFB_FALSE; lh_table_delete_entry (clientCtxs, entry); - return (TRUE); + return (AFB_SUCCESS); } // generate a new token -PUBLIC char* ctxTokenCreate (AFB_request *request) { +PUBLIC AFB_error ctxTokenCreate (AFB_request *request) { int oldTnkValid; const char *ornew; uuid_t newuuid; + if (request->client == NULL) return AFB_EMPTY; + // create a UUID as token value uuid_generate(newuuid); uuid_unparse_lower(newuuid, request->client->token); @@ -478,15 +487,17 @@ PUBLIC char* ctxTokenCreate (AFB_request *request) { request->client->timeStamp=time(NULL); // Token is also store in context but it might be convenient for plugin to access it directly - return (request->client->token); + return (AFB_SUCCESS); } // generate a new token and update client context -PUBLIC char* ctxTokenRefresh (AFB_request *request) { +PUBLIC AFB_error ctxTokenRefresh (AFB_request *request) { int oldTnkValid; const char *oldornew; uuid_t newuuid; + + if (request->client == NULL) return AFB_EMPTY; // Check if the old token is valid oldTnkValid= ctxTokenCheck (request); @@ -498,7 +509,7 @@ PUBLIC char* ctxTokenRefresh (AFB_request *request) { } // No existing token and no request to create one - if (oldTnkValid != TRUE) return NULL; + if (oldTnkValid != TRUE) return AFB_WARNING; return (ctxTokenCreate (request)); } |