2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include <sys/types.h>
28 #include <plugin_message.h>
30 #include "auth-adaptor.h"
31 #include "auth-adaptor-log.h"
33 #define PLUGIN_MESSAGE_LISTENER_CMD_APPEND_FD "append;"
34 #define PLUGIN_MESSAGE_LISTENER_CMD_STOP "stop;"
35 #define PLUGIN_MESSAGE_PROTOCOL_MAX_BUF_SIZE 8192
37 #define AUTH_PLUGIN_INTERFACE_CREATE_CONTEXT "create_context"
38 #define AUTH_PLUGIN_INTERFACE_DESTROY_CONTEXT "destroy_context"
39 #define AUTH_PLUGIN_INTERFACE_DESTROY_HANDLE "destroy_handle"
40 #define AUTH_PLUGIN_INTERFACE_IS_AUTH "is_auth"
41 #define AUTH_PLUGIN_INTERFACE_JOIN "join"
42 #define AUTH_PLUGIN_INTERFACE_LOGIN "login"
43 #define AUTH_PLUGIN_INTERFACE_REFRESH_ACCESS_TOKEN "refresh"
46 #define IF_IS_PLUGIN_THAN_RETURN_NULL() do {if (!g_process_identity) return NULL; } while (0)
48 #define SAFE_ADD_STRING(x) ((x) ? (x) : "")
51 PLUGIN_TYPE_INHOUSE = 0,
52 PLUGIN_TYPE_3RD_PARTY = 1,
56 * @brief Describes Auth adaptor plugin
58 typedef struct auth_adaptor_plugin_s {
59 auth_adaptor_h adaptor; /* Adaptor */
60 char *path; /* Plugin library path */
61 auth_adaptor_plugin_handle_h handle; /* Plugin handle */
62 void *dl_handle; /* Plugin library handle */
63 int ref_counter; /* Plugin reference counter */
64 GMutex ref_counter_mutex; /* Plugin reference counter mutex */
65 auth_adaptor_plugin_listener_h plugin_listener; /* Plugin callback listener */
66 GMutex plugin_listener_mutex; /* Plugin callback listener mutex */
69 auth_plugin_type_e type;
74 GMutex contexts_mutex;
76 } auth_adaptor_plugin_t;
78 typedef struct _plugin_message_context_s {
81 } plugin_message_context_t;
84 * @brief Describes Auth adaptor
86 typedef struct auth_adaptor_s {
87 GMutex auth_adaptor_mutex; /* Adaptor mutex */
88 int started; /* Started flag */
89 char *plugins_dir; /* Plugins directory path */
90 GList *plugins; /* List of loaded plugins */
91 GMutex plugins_mutex; /* Plugin list mutex */
92 GList *adaptor_listeners; /* List of vservice channel listener (for now not effective) */
93 GMutex adaptor_listeners_mutex; /* Listener list mutex */
98 pthread_t plugin_listener;
101 static int g_process_identity = -1;
104 * @brief Creates plugin
106 static auth_adaptor_plugin_h auth_adaptor_create_plugin(const char *plugin_path);
109 * @brief Destroys plugin and deletes all resources associated with it
111 static void auth_adaptor_destroy_plugin(auth_adaptor_plugin_h plugin);
114 * @brief Loads plugins from selected directory
116 static int auth_adaptor_load_plugins_from_directory(auth_adaptor_h adaptor,
117 const char *dir_path);
120 * @brief Checks if plugin is loaded by selected plugin adaptor
122 static int auth_adaptor_has_plugin(auth_adaptor_h adaptor,
123 auth_adaptor_plugin_h plugin);
125 static auth_adaptor_error_code_h auth_adaptor_create_error_code(const int64_t code,
128 * Increases adaptor's plugin references counter
130 static void auth_adaptor_plugin_ref(auth_adaptor_plugin_h);
133 * @brief Decreases adaptor's plugin references counter
135 static void auth_adaptor_plugin_unref(auth_adaptor_plugin_h);
138 void auth_adaptor_login_reply_cb(auth_adaptor_plugin_context_h context,
139 auth_adaptor_error_code_h error_code,
142 if (_service_adaptor_login_reply != NULL)
143 _service_adaptor_login_reply(imsi, plugin_uri, app_id, msisdn, response);
147 /* ////////////////////////////////////////////////////////////////////////////// */
148 /* //////////// Internal function prototype (for forked plugin) /////////////// */
149 /* ////////////////////////////////////////////////////////////////////////////// */
152 /* To be used by adaptor */
153 void *_auth_adaptor_plugin_message_collector(void *data);
154 void __auth_adaptor_transfer_message(const char *msg);
155 int __auth_adaptor_parse_message_cmd(auth_adaptor_h adaptor, char *msg);
156 void _auth_adaptor_send_cmd_add_fd(auth_adaptor_h adaptor, int fd);
157 void _auth_adaptor_send_cmd_stop_listen(auth_adaptor_h adaptor);
159 static int auth_adaptor_send_message_to_plugin_sync(auth_adaptor_plugin_h plugin,
160 plugin_message_h send_message,
161 plugin_message_h *receive_message);
163 /* To be used by adaptor (virtual plugin handle) */
164 auth_adaptor_plugin_handle_h __auth_adaptor_create_3rd_party_plugin_handle(const char *plugin_uri);
166 auth_error_code_t auth_plugin_send_create_context(auth_adaptor_plugin_context_h *context,
168 const char *user_password,
170 const char *app_secret,
171 const char *service_name);
173 auth_error_code_t auth_plugin_send_destroy_context(auth_adaptor_plugin_context_h context);
175 auth_error_code_t auth_plugin_send_is_auth(auth_adaptor_plugin_context_h context,
178 auth_adaptor_error_code_h *error,
181 auth_error_code_t auth_plugin_send_join(auth_adaptor_plugin_context_h context,
182 const char *device_id,
184 auth_adaptor_error_code_h *error,
187 auth_error_code_t auth_plugin_send_login(auth_adaptor_plugin_context_h context,
189 auth_adaptor_error_code_h *error,
192 auth_error_code_t auth_plugin_send_refresh_access_token(auth_adaptor_plugin_context_h context,
194 auth_adaptor_error_code_h *error,
198 auth_error_code_t auth_plugin_send_set_service_status(auth_adaptor_plugin_context_h context,
199 const int service_id,
202 auth_adaptor_error_code_h *error,
205 auth_error_code_t auth_plugin_send_get_msisdn(auth_adaptor_plugin_context_h context,
208 auth_adaptor_error_code_h *error,
211 auth_error_code_t auth_plugin_send_get_service_status(auth_adaptor_plugin_context_h context,
212 const int service_id,
215 auth_adaptor_error_code_h *error,
218 auth_error_code_t auth_plugin_send_get_service_policy(auth_adaptor_plugin_context_h context,
219 const int service_id,
221 char **default_status,
222 char **policy_feature,
223 char **policy_version,
224 char **policy_doc_url,
225 auth_adaptor_error_code_h *error,
228 auth_error_code_t auth_plugin_send_get_server_info(auth_adaptor_plugin_context_h context,
230 GHashTable **server_info,
231 auth_adaptor_error_code_h *error,
235 /* To be used by forked plugin */
236 void *_auth_plugin_request_collector(void *data);
237 auth_adaptor_plugin_context_h __auth_plugin_get_context_by_context_id(auth_adaptor_plugin_h plugin, int context_id);
238 void __auth_plugin_progress_command(auth_adaptor_plugin_h plugin, char *order, char **result);
241 /* ------------------------------------------------------------------------
242 // Functions implementations
243 // ------------------------------------------------------------------------ */
245 /* ////////////////////////////////////////////////////// */
246 /* // Mandatory: External adaptor management function /// */
247 /* ////////////////////////////////////////////////////// */
248 auth_adaptor_h auth_adaptor_create(const char *plugins_dir)
250 if (NULL == plugins_dir) {
251 auth_adaptor_error("Invalid argument""(plugins_dir: %s)", plugins_dir);
255 auth_adaptor_h auth_adaptor = (auth_adaptor_h) calloc(1, sizeof(auth_adaptor_t));
257 if (NULL == auth_adaptor) {
258 auth_adaptor_error("Critical : Memory allocation failed");
262 /* for forked plugin */
263 if (pipe(auth_adaptor->rd_cmd) == -1) {
268 g_mutex_init(&auth_adaptor->rd_mutex);
269 auth_adaptor->rd_list = NULL;
270 /* auth_adaptor->rd_list = g_list_append(auth_adaptor->rd_list, (gpointer)auth_adaptor->rd_cmd[0]); */
272 auth_adaptor->started = 0;
273 auth_adaptor->plugins_dir = strdup(plugins_dir);
275 g_mutex_init(&auth_adaptor->auth_adaptor_mutex);
276 g_mutex_init(&auth_adaptor->plugins_mutex);
277 g_mutex_init(&auth_adaptor->adaptor_listeners_mutex);
279 g_mutex_lock(&auth_adaptor->adaptor_listeners_mutex);
280 auth_adaptor->adaptor_listeners = NULL;
281 g_mutex_unlock(&auth_adaptor->adaptor_listeners_mutex);
283 g_mutex_lock(&auth_adaptor->plugins_mutex);
284 auth_adaptor->plugins = NULL;
285 g_mutex_unlock(&auth_adaptor->plugins_mutex);
291 void auth_adaptor_destroy(auth_adaptor_h adaptor)
293 if (NULL == adaptor) {
294 auth_adaptor_error("Invalid argument""(adaptor: %p)", adaptor);
298 g_mutex_lock(&adaptor->auth_adaptor_mutex);
299 if (adaptor->started) {
300 auth_adaptor_error("Auth adaptor is running. Forcing stop before destroy");
301 auth_adaptor_stop(adaptor);
304 g_mutex_lock(&adaptor->plugins_mutex);
305 if (NULL != adaptor->plugins) {
306 g_list_free_full(adaptor->plugins, (GDestroyNotify) auth_adaptor_plugin_unref);
307 adaptor->plugins = NULL;
309 g_mutex_unlock(&adaptor->plugins_mutex);
311 g_mutex_lock(&adaptor->adaptor_listeners_mutex);
312 if (NULL != adaptor->adaptor_listeners) {
313 g_list_free(adaptor->adaptor_listeners);
314 adaptor->adaptor_listeners = NULL;
316 g_mutex_unlock(&adaptor->adaptor_listeners_mutex);
318 _service_adaptor_login_reply = NULL;
320 free(adaptor->plugins_dir);
321 adaptor->plugins_dir = NULL;
323 /* For forked plugin */
324 g_list_free(adaptor->rd_list);
325 close(adaptor->rd_cmd[0]);
326 close(adaptor->rd_cmd[1]);
331 int auth_adaptor_start(auth_adaptor_h adaptor)
333 auth_adaptor_debug("Starting auth adaptor");
334 if (NULL == adaptor) {
335 auth_adaptor_error("Invalid argument""(adaptor: %p)", adaptor);
336 return AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT;
339 g_mutex_lock(&adaptor->auth_adaptor_mutex);
340 int result = AUTH_ADAPTOR_ERROR_NONE;
341 if (adaptor->started) {
342 auth_adaptor_error("Auth adaptor is already started");
344 adaptor->started = 1;
347 if (pthread_create(&pid, NULL, _auth_adaptor_plugin_message_collector, (void *)adaptor)) {
348 adaptor->started = 0;
349 auth_adaptor_error("Could not create 3rd party plugin listener");
350 result = AUTH_ADAPTOR_ERROR_NOT_FOUND;
351 } else if (AUTH_ADAPTOR_ERROR_NONE != (result = auth_adaptor_load_plugins_from_directory(adaptor, adaptor->plugins_dir))) {
352 _auth_adaptor_send_cmd_stop_listen(adaptor);
353 adaptor->started = 0;
354 auth_adaptor_error("Could not load plugins from directory");
355 result = AUTH_ADAPTOR_ERROR_NOT_FOUND;
357 adaptor->plugin_listener = pid;
358 auth_adaptor_info("Auth adaptor started successfully");
361 g_mutex_unlock(&adaptor->auth_adaptor_mutex);
366 int auth_adaptor_stop(auth_adaptor_h adaptor)
368 if (NULL == adaptor) {
369 auth_adaptor_error("Invalid argument""(adaptor: %p)", adaptor);
370 return AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT;
373 g_mutex_lock(&adaptor->auth_adaptor_mutex);
375 auth_adaptor_debug("stop plugin listener");
376 _auth_adaptor_send_cmd_stop_listen(adaptor);
377 pthread_join(adaptor->plugin_listener, NULL);
378 int result = AUTH_ADAPTOR_ERROR_NONE;
379 if (0 == adaptor->started) {
380 result = AUTH_ADAPTOR_ERROR_STOP;
382 if (NULL != adaptor->plugins) {
383 g_mutex_lock(&adaptor->plugins_mutex);
384 g_list_free_full(adaptor->plugins, (GDestroyNotify) auth_adaptor_plugin_unref);
385 adaptor->plugins = NULL;
386 g_mutex_unlock(&adaptor->plugins_mutex);
388 adaptor->started = 0;
389 auth_adaptor_debug("Auth adaptor stopped");
392 g_mutex_unlock(&adaptor->auth_adaptor_mutex);
397 int auth_adaptor_register_listener(auth_adaptor_h adaptor,
398 auth_adaptor_listener_h listener)
400 if ((NULL == adaptor) || (NULL == listener)) {
401 auth_adaptor_error("Invalid argument""(adaptor: %p, listener: %p)", adaptor, listener);
402 return AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT;
405 g_mutex_lock(&adaptor->adaptor_listeners_mutex);
407 adaptor->adaptor_listeners = g_list_append(adaptor->adaptor_listeners, listener);
409 g_mutex_unlock(&adaptor->adaptor_listeners_mutex);
411 _service_adaptor_login_reply =
412 (auth_adaptor_service_login_reply_cb)listener->login_reply;
414 return AUTH_ADAPTOR_ERROR_NONE;
417 int auth_adaptor_unregister_listener(auth_adaptor_h adaptor,
418 auth_adaptor_listener_h listener)
420 if (NULL == adaptor) {
421 auth_adaptor_error("Invalid argument (adaptor)");
422 if (NULL != listener) {
426 return AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT;
428 if (NULL == listener) {
429 auth_adaptor_error("Invalid argument (listener)");
430 return AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT;
433 g_mutex_lock(&adaptor->adaptor_listeners_mutex);
435 if (NULL == g_list_find(adaptor->adaptor_listeners, listener)) {
436 g_mutex_unlock(&adaptor->adaptor_listeners_mutex);
437 auth_adaptor_error("Could not find listener");
439 return AUTH_ADAPTOR_ERROR_NOT_FOUND;
442 adaptor->adaptor_listeners = g_list_remove(adaptor->adaptor_listeners, listener);
445 g_mutex_unlock(&adaptor->adaptor_listeners_mutex);
447 _service_adaptor_login_reply = NULL;
449 return AUTH_ADAPTOR_ERROR_NONE;
452 /* /////////////////////////////////////////////////////////////
453 // Plugin create / destroy / ref. count / get plugin name
454 ///////////////////////////////////////////////////////////// */
455 static auth_adaptor_plugin_h auth_adaptor_create_plugin(const char *plugin_path)
457 if (NULL == plugin_path) {
458 auth_adaptor_error("Invalid argument""(plugin_path: %p)", plugin_path);
463 void *dl_handle = dlopen(plugin_path, RTLD_LAZY);
464 if (NULL == dl_handle) {
465 auth_adaptor_error("Could not load plugin %s: %s", plugin_path, dlerror());
469 auth_adaptor_plugin_handle_h (*get_adaptee_handle)(void) = NULL;
471 get_adaptee_handle = (auth_adaptor_plugin_handle_h (*)(void))(dlsym(dl_handle, "create_plugin_handle"));
472 if (NULL == get_adaptee_handle) {
474 auth_adaptor_error("Could not get function pointer to create_plugin_handle");
479 auth_adaptor_plugin_handle_h handle = get_adaptee_handle();
480 plugin_req_exit_void();
481 if (NULL == handle) {
483 auth_adaptor_error("Could not get adaptee handle");
487 auth_adaptor_plugin_h plugin = (auth_adaptor_plugin_h) calloc(1, sizeof(auth_adaptor_plugin_t));
488 if (NULL == plugin) {
490 auth_adaptor_error("Could not create plugin object");
494 plugin->path = g_strdup(plugin_path);
495 plugin->handle = handle;
496 plugin->dl_handle = dl_handle;
497 plugin->ref_counter = 0;
499 plugin->type = PLUGIN_TYPE_INHOUSE;
501 g_mutex_init(&plugin->ref_counter_mutex);
502 g_mutex_init(&plugin->plugin_listener_mutex);
503 g_mutex_init(&plugin->contexts_mutex);
504 plugin->contexts = NULL;
506 auth_adaptor_plugin_listener_h listener =
507 (auth_adaptor_plugin_listener_h) calloc(1, sizeof(auth_adaptor_plugin_listener_t));
509 listener->auth_adaptor_login_reply = auth_adaptor_login_reply_cb;
512 plugin->handle->set_listener(listener);
513 plugin_req_exit_void();
515 g_mutex_lock(&plugin->plugin_listener_mutex);
516 plugin->plugin_listener = listener;
517 g_mutex_unlock(&plugin->plugin_listener_mutex);
522 static void auth_adaptor_destroy_plugin(auth_adaptor_plugin_h plugin)
524 if (NULL == plugin) {
525 auth_adaptor_error("Invalid argument""(plugin: %p)", plugin);
529 if (NULL != plugin->handle) {
530 plugin->handle->destroy_handle(plugin->handle);
532 g_mutex_lock(&plugin->plugin_listener_mutex);
534 plugin->handle->unset_listener();
535 plugin_req_exit_void();
536 g_mutex_unlock(&plugin->plugin_listener_mutex);
538 plugin->handle = NULL;
541 if (NULL != plugin->dl_handle) {
542 dlclose(plugin->dl_handle);
543 plugin->dl_handle = NULL;
546 g_free(plugin->path);
552 static int auth_adaptor_load_plugins_from_directory(auth_adaptor_h adaptor,
553 const char *dir_path)
555 char *plugin_path = NULL;
557 struct dirent dir_entry, *result = NULL;
559 auth_adaptor_debug("Starting load plugins from directory");
561 if ((NULL == adaptor) || (NULL == dir_path)) {
562 auth_adaptor_error("Invalid argument""(adaptor: %p, dir_path: %p)", adaptor, dir_path);
563 return AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT;
566 dir = opendir(dir_path);
568 auth_adaptor_error("Could not open dir path (%s)", dir_path);
569 return AUTH_ADAPTOR_ERROR_NOT_FOUND;
572 int ret = AUTH_ADAPTOR_ERROR_NONE;
573 while (0 == (readdir_r(dir, &dir_entry, &result))) {
575 if (NULL == result) {
576 auth_adaptor_error("Could not open directory %s", plugin_path);
580 if (dir_entry.d_type & DT_DIR) {
584 plugin_path = g_strconcat(dir_path, "/", dir_entry.d_name, NULL);
585 auth_adaptor_plugin_h plugin = auth_adaptor_create_plugin(plugin_path);
587 if (NULL != plugin) {
588 auth_adaptor_debug("Loaded plugin: %s", plugin_path);
589 plugin->adaptor = adaptor;
590 auth_adaptor_plugin_ref(plugin);
591 g_mutex_lock(&adaptor->plugins_mutex);
592 adaptor->plugins = g_list_append(adaptor->plugins, plugin);
593 g_mutex_unlock(&adaptor->plugins_mutex);
595 auth_adaptor_error("Could not load plugin %s", plugin_path);
602 auth_adaptor_debug("End load plugins from directory");
607 static int auth_adaptor_has_plugin(auth_adaptor_h adaptor,
608 auth_adaptor_plugin_h plugin)
610 if ((NULL == adaptor) || (NULL == plugin)) {
611 auth_adaptor_error("Invalid argument""(adaptor: %p, plugin: %p)", adaptor, plugin);
617 g_mutex_lock(&adaptor->plugins_mutex);
618 if (NULL != g_list_find(adaptor->plugins, plugin)) {
621 g_mutex_unlock(&adaptor->plugins_mutex);
626 static void auth_adaptor_plugin_ref(auth_adaptor_plugin_h plugin)
628 if (NULL == plugin) {
629 auth_adaptor_error("Invalid argument""(plugin: %p)", plugin);
633 g_mutex_lock(&plugin->ref_counter_mutex);
634 plugin->ref_counter = plugin->ref_counter + 1;
635 if (NULL != plugin->handle) {
636 auth_adaptor_info("plugin name: %s, ref_counter: %d",
637 plugin->handle->plugin_uri, plugin->ref_counter);
639 auth_adaptor_info("ref_counter: %d", plugin->ref_counter);
641 g_mutex_unlock(&plugin->ref_counter_mutex);
644 static void auth_adaptor_plugin_unref(auth_adaptor_plugin_h plugin)
646 if (NULL == plugin) {
647 auth_adaptor_error("Invalid argument""(plugin: %p)", plugin);
651 int should_destroy = 0;
653 g_mutex_lock(&plugin->ref_counter_mutex);
654 plugin->ref_counter = plugin->ref_counter - 1;
655 if (NULL != plugin->handle) {
656 auth_adaptor_info("plugin name: %s, ref_counter: %d",
657 plugin->handle->plugin_uri, plugin->ref_counter);
659 auth_adaptor_info("ref_counter: %d", plugin->ref_counter);
661 if (0 >= plugin->ref_counter) {
664 g_mutex_unlock(&plugin->ref_counter_mutex);
666 if (should_destroy) {
667 auth_adaptor_debug("Plugin is being destroyed");
668 auth_adaptor_destroy_plugin(plugin);
672 /* For 3rd party plugin packages */
673 int auth_adaptor_load_plugin_from_package(auth_adaptor_h adaptor,
674 const char *package_id,
675 const char *plugin_path)
680 if (pipe(adaptor_fd) == -1) {
681 auth_adaptor_debug("pipe creation error, can not load plugin package");
682 } else if (pipe(plugin_fd) == -1) {
683 close(adaptor_fd[0]);
684 close(adaptor_fd[1]);
685 auth_adaptor_debug("pipe creation error[2], can not load plugin package");
687 g_process_identity = fork();
688 if (0 == g_process_identity) { /* child */
689 auth_adaptor_debug_func("[CHILD PROCESS] forked success (PID : %d, id : %d)", (int)getpid());
691 auth_adaptor_plugin_h plugin = NULL;
692 plugin = auth_adaptor_create_plugin(plugin_path);
694 if (NULL == plugin) {
695 auth_adaptor_error("[CHILD PROCESS] Load plugin failed");
699 plugin->rd = plugin_fd[0];
701 plugin->wd = adaptor_fd[1];
702 close(adaptor_fd[0]);
703 void *temp = _auth_plugin_request_collector((void *)plugin);
704 auth_adaptor_debug_func("[CHILD PROCESS] exit %p", temp);
706 } else if (0 < g_process_identity) { /* parent */
707 auth_adaptor_debug_func("[PARENT PROCESS] forked success (PID : %d)", (int)getpid());
708 auth_adaptor_plugin_h _plugin = (auth_adaptor_plugin_h) calloc(1, sizeof(auth_adaptor_plugin_t));
709 if (NULL != _plugin) {
710 _plugin->ref_counter = 0;
711 g_mutex_init(&_plugin->ref_counter_mutex);
712 g_mutex_init(&_plugin->message_mutex);
714 _plugin->handle = __auth_adaptor_create_3rd_party_plugin_handle(package_id);
716 _plugin->type = PLUGIN_TYPE_3RD_PARTY;
717 _plugin->pid = g_process_identity;
718 _plugin->rd = adaptor_fd[0];
719 close(adaptor_fd[1]);
720 _plugin->wd = plugin_fd[1];
723 _auth_adaptor_send_cmd_add_fd(adaptor, _plugin->rd);
725 _plugin->adaptor = adaptor;
726 auth_adaptor_plugin_ref(_plugin);
727 g_mutex_lock(&adaptor->plugins_mutex);
728 adaptor->plugins = g_list_append(adaptor->plugins, _plugin);
729 g_mutex_unlock(&adaptor->plugins_mutex);
732 close(adaptor_fd[0]);
733 close(adaptor_fd[1]);
736 auth_adaptor_debug("fork error, can not load plugin package");
743 /* //////////////////////////////////////////////////////
744 // Get plugin name by plugin
745 ////////////////////////////////////////////////////// */
746 void auth_adaptor_get_plugin_uri(auth_adaptor_plugin_h plugin,
749 if ((NULL == plugin) || (NULL == plugin_uri)) {
750 auth_adaptor_error("Invalid argument""(plugin: %p, uri: %p)", plugin, plugin_uri);
753 if (NULL != plugin->handle) {
754 *plugin_uri = strdup(plugin->handle->plugin_uri);
758 /* //////////////////////////////////////////////////////
759 // Create / Destroy error code
760 ////////////////////////////////////////////////////// */
761 static auth_adaptor_error_code_h auth_adaptor_create_error_code(const int64_t code,
768 auth_adaptor_error_code_h error_code =
769 (auth_adaptor_error_code_h) malloc(sizeof(auth_adaptor_error_code_t));
771 if (NULL != error_code) {
772 error_code->code = code;
773 error_code->msg = strdup(msg);
779 void auth_adaptor_destroy_error_code(auth_adaptor_error_code_h *error_code)
781 if ((NULL != error_code) && (NULL != (*error_code))) {
782 free((*error_code)->msg);
783 (*error_code)->msg = NULL;
789 /* //////////////////////////////////////////////////////
790 // Plugin context create / destroy
791 ////////////////////////////////////////////////////// */
792 auth_adaptor_plugin_context_h auth_adaptor_create_plugin_context(auth_adaptor_plugin_h plugin,
794 const char *user_password,
796 const char *app_secret,
798 const char *service_name)
800 auth_adaptor_debug("Starting auth_adaptor_create_plugin_context");
802 if ((NULL == plugin) || (NULL == service_name)) {
803 auth_adaptor_error("Invalid argument""(plugin: %p, service_name: %s)",
804 plugin, service_name);
808 if (NULL != plugin->handle) {
809 auth_adaptor_plugin_context_h plugin_context = NULL;
810 if (plugin->type == PLUGIN_TYPE_3RD_PARTY) {
811 plugin_context = (auth_adaptor_plugin_context_h) calloc(1, sizeof(auth_adaptor_plugin_context_t));
812 if (NULL == plugin_context) {
813 auth_adaptor_error("Create context failed");
816 plugin_context->plugin_handle = plugin;
821 plugin->handle->create_context(&plugin_context, SAFE_ADD_STRING(user_id), SAFE_ADD_STRING(user_password), SAFE_ADD_STRING(app_id), SAFE_ADD_STRING(app_secret), SAFE_ADD_STRING(imsi));
822 plugin_req_exit_void();
824 if (NULL == plugin_context) {
825 auth_adaptor_error("Create context failed");
828 plugin_context->plugin_uri = strdup(plugin->handle->plugin_uri);
829 plugin_context->service_name = strdup(service_name);
832 /* For forked plugin */
833 g_mutex_lock(&plugin->contexts_mutex);
834 plugin->contexts = g_list_append(plugin->contexts, (gpointer)plugin_context);
835 g_mutex_unlock(&plugin->contexts_mutex);
837 return plugin_context;
839 auth_adaptor_error("Plugin handle is null");
842 auth_adaptor_debug("End auth_adaptor_create_plugin_context");
846 void auth_adaptor_destroy_plugin_context(auth_adaptor_plugin_h plugin,
847 auth_adaptor_plugin_context_h plugin_context)
849 if ((NULL == plugin) || (NULL == plugin_context)) {
850 auth_adaptor_error("Invalid argument""(plugin: %p, plugin_context: %p)", plugin, plugin_context);
854 if (NULL != plugin->handle) {
855 /* For forked plugin */
856 g_mutex_lock(&plugin->contexts_mutex);
857 plugin->contexts = g_list_remove(plugin->contexts, (gpointer)plugin_context);
858 g_mutex_unlock(&plugin->contexts_mutex);
861 plugin->handle->destroy_context(plugin_context);
862 plugin_req_exit_void();
864 auth_adaptor_error("Plugin handle is null");
870 /* //////////////////////////////////////////////////////
871 // Get plugin by plugin name
872 ////////////////////////////////////////////////////// */
873 auth_adaptor_plugin_h auth_adaptor_get_plugin_by_name(auth_adaptor_h adaptor,
874 const char *plugin_uri)
876 auth_adaptor_debug("Starting auth_adaptor_get_plugin_by_name");
878 if ((NULL == adaptor) || (NULL == plugin_uri)) {
879 auth_adaptor_error("Invalid argument""(adaptor: %p, plugin_uri: %p)", adaptor, plugin_uri);
883 auth_adaptor_plugin_h plugin = NULL;
884 g_mutex_lock(&adaptor->plugins_mutex);
886 int count = g_list_length(adaptor->plugins);
888 for (i = 0; i < count; i++) {
889 auth_adaptor_plugin_h temp_plugin = g_list_nth_data(adaptor->plugins, i);
890 if (NULL != temp_plugin) {
891 if (0 == strcmp(temp_plugin->handle->plugin_uri, plugin_uri)) {
892 auth_adaptor_plugin_ref(temp_plugin);
893 plugin = temp_plugin;
894 g_mutex_unlock(&adaptor->plugins_mutex);
899 g_mutex_unlock(&adaptor->plugins_mutex);
904 /* //////////////////////////////////////////////////////
905 // Plugin load / unload / get plugin list
906 ////////////////////////////////////////////////////// */
907 int auth_adaptor_load_plugin(auth_adaptor_h adaptor,
908 const char *plugin_path)
910 if ((NULL == adaptor) || (NULL == plugin_path)) {
911 auth_adaptor_error("Invalid argument""(adaptor: %p, plugin_path: %p)", adaptor, plugin_path);
912 return AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT;
915 if (0 == adaptor->started) {
916 auth_adaptor_error("Auth adaptor is not started");
917 return AUTH_ADAPTOR_ERROR_START;
920 auth_adaptor_plugin_h plugin = auth_adaptor_create_plugin(plugin_path);
921 if (NULL == plugin) {
922 auth_adaptor_error("Could not load plugin %s", plugin_path);
923 return AUTH_ADAPTOR_ERROR_CREATE;
926 plugin->adaptor = adaptor;
927 auth_adaptor_plugin_ref(plugin);
929 g_mutex_lock(&adaptor->plugins_mutex);
930 adaptor->plugins = g_list_append(adaptor->plugins, plugin);
931 g_mutex_unlock(&adaptor->plugins_mutex);
933 return AUTH_ADAPTOR_ERROR_NONE;
936 int auth_adaptor_unload_plugin(auth_adaptor_h adaptor,
937 auth_adaptor_plugin_h plugin)
939 if ((NULL == adaptor) || (NULL == plugin)) {
940 auth_adaptor_error("Invalid argument""(adaptor: %p, plugin: %p)", adaptor, plugin);
941 return AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT;
944 if (0 == adaptor->started) {
945 auth_adaptor_error("Auth adaptor is not started");
946 return AUTH_ADAPTOR_ERROR_START;
949 if (0 == auth_adaptor_has_plugin(adaptor, plugin)) {
950 auth_adaptor_error("Auth adaptor has no plugin");
951 return AUTH_ADAPTOR_ERROR_NOT_FOUND;
954 plugin->adaptor = NULL;
956 g_mutex_lock(&adaptor->plugins_mutex);
957 adaptor->plugins = g_list_remove(adaptor->plugins, plugin);
958 g_mutex_unlock(&adaptor->plugins_mutex);
960 auth_adaptor_plugin_unref(plugin);
962 return AUTH_ADAPTOR_ERROR_NONE;
965 GList *auth_adaptor_get_plugins(auth_adaptor_h adaptor)
967 if (NULL == adaptor) {
968 auth_adaptor_error("Invalid argument""(adaptor: %p)", adaptor);
972 GList *plugins = NULL;
974 g_mutex_lock(&adaptor->plugins_mutex);
975 int plugins_count = g_list_length(adaptor->plugins);
977 for (i = 0; i < plugins_count; i++) {
978 auth_adaptor_plugin_h plugin = g_list_nth_data(adaptor->plugins, i);
979 if (NULL != plugin) {
980 auth_adaptor_plugin_ref(plugin);
981 plugins = g_list_append(plugins, plugin);
984 g_mutex_unlock(&adaptor->plugins_mutex);
989 /* ////////////////////////////////////////////////////////////
990 // Adaptor get Element Functions
991 //////////////////////////////////////////////////////////// */
994 * @return : Need free
997 char *auth_adaptor_get_access_token_dup(auth_adaptor_plugin_context_h context)
999 if (NULL == context) {
1000 auth_adaptor_warning("Invalid argument""(context: %p", context);
1004 if (NULL == context->access_token) {
1005 auth_adaptor_warning("Invalid argument""(context->access_token: %p", context->access_token);
1009 if (0 >= strlen(context->access_token)) {
1010 auth_adaptor_warning("Invalid argument""(context->access_token length: %d", strlen(context->access_token));
1014 return strdup(context->access_token);
1018 * @return : Need free
1021 char *auth_adaptor_get_uid_dup(auth_adaptor_plugin_context_h context)
1023 if (NULL == context) {
1024 auth_adaptor_warning("Invalid argument""(context: %p", context);
1028 if (NULL == context->uid) {
1029 auth_adaptor_warning("Invalid argument""(context->uid: %p", context->uid);
1033 if (0 >= strlen(context->uid)) {
1034 auth_adaptor_warning("Invalid argument""(context->uid length: %d", strlen(context->uid));
1038 return strdup(context->uid);
1042 * @return : Need free
1045 char *auth_adaptor_get_msisdn_dup(auth_adaptor_plugin_context_h context)
1047 if (NULL == context) {
1048 auth_adaptor_warning("Invalid argument""(context: %p", context);
1052 if (NULL == context->msisdn) {
1053 auth_adaptor_warning("Invalid argument""(context->msisdn: %p", context->msisdn);
1057 if (0 >= strlen(context->msisdn)) {
1058 auth_adaptor_warning("Invalid argument""(context->msisdn length: %d", strlen(context->msisdn));
1062 return strdup(context->msisdn);
1065 /* ////////////////////////////////////////////////////////////
1066 // Adaptor Plugin call Functions
1067 //////////////////////////////////////////////////////////// */
1069 auth_error_code_t _auth_adaptor_mandatory_param_check(auth_adaptor_plugin_h plugin,
1070 auth_adaptor_plugin_context_h context,
1071 auth_adaptor_error_code_h *error_code)
1073 auth_error_code_t ret = AUTH_ADAPTOR_ERROR_NONE;
1075 if ((NULL == plugin) || (NULL == context)) {
1076 auth_adaptor_error("Invalid argument""(plugin: %p, context: %p)", plugin, context);
1077 if (NULL != error_code) {
1078 *error_code = auth_adaptor_create_error_code(
1079 (int64_t) AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT,
1080 "Invalid argument (plugin or context)");
1083 return AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT;
1086 if (NULL == plugin->handle) {
1087 auth_adaptor_error("Plugin handle is null");
1089 if (NULL != error_code) {
1090 *error_code = auth_adaptor_create_error_code(
1091 (int64_t) AUTH_ADAPTOR_ERROR_INVALID_HANDLE,
1092 "Plugin handle is null");
1095 return AUTH_ADAPTOR_ERROR_INVALID_HANDLE;
1102 * @brief Check Account Registration [Sync API]
1104 * @param[in] plugin specifies Auth-adaptor Plugin handle
1105 * @param[in] context specifies Auth-adaptor Plugin context
1106 * @param[in] request specifies Optional value for specific Plugin (Recommend Json Object format)
1107 * @param[out] is_auth specifies Registered Flag (Registered : 1, Not registered : 0)
1108 * @param[out] error_code specifies Error code
1109 * @param[out] response specifies Optional value from specific Plugin (Recommend Json Object format)
1110 * @return 0 on success, otherwise a positive error value
1111 * @retval error code defined in auth_error_code_e - AUTH_ADAPTOR_ERROR_NONE if Successful
1113 auth_error_code_t auth_adaptor_is_auth(auth_adaptor_plugin_h plugin,
1114 auth_adaptor_plugin_context_h context,
1117 auth_adaptor_error_code_h *error_code, void *response)
1119 auth_error_code_t ret = _auth_adaptor_mandatory_param_check(plugin, context, error_code);
1120 if (AUTH_ADAPTOR_ERROR_NONE != ret) {
1124 if ((NULL == is_auth)) {
1125 auth_adaptor_error("Invalid argument""(is_auth: %p)", is_auth);
1126 if (NULL != error_code) {
1127 *error_code = auth_adaptor_create_error_code(
1128 (int64_t) AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT,
1129 "Invalid argument (is_auth pointer is null)");
1132 return AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT;
1135 if (NULL == plugin->handle->is_auth) {
1136 auth_adaptor_error("Plugin does not support this API");
1137 if (NULL != error_code) {
1138 *error_code = auth_adaptor_create_error_code(
1139 (int64_t) AUTH_ADAPTOR_ERROR_UNSUPPORTED,
1140 "Plugin does not support this API (is_auth)");
1142 return AUTH_ADAPTOR_ERROR_UNSUPPORTED;
1146 ret = plugin->handle->is_auth(context, request, is_auth, error_code, response);
1147 plugin_req_exit(ret, plugin, error_code);
1153 * @brief Request Access Token (included auth_adaptor_context) [Sync API]
1155 * @param[in] plugin specifies Auth-adaptor Plugin handle
1156 * @param[in] context specifies Auth-adaptor Plugin context
1157 * @param[in] is_auth specifies Registration Flag (Must be issued from "auth_adaptor_is_auth" Function)
1158 * @param[in] request specifies Optional value for specific Plugin (Recommend Json Object format)
1159 * @param[out] error_code specifies Error code
1160 * @param[out] response specifies Optional value from specific Plugin (Recommend Json Object format)
1161 * @return 0 on success, otherwise a positive error value
1162 * @retval error code defined in auth_error_code_e - AUTH_ADAPTOR_ERROR_NONE if Successful
1164 auth_error_code_t auth_adaptor_login(auth_adaptor_plugin_h plugin,
1165 auth_adaptor_plugin_context_h context,
1168 auth_adaptor_error_code_h *error_code,
1171 auth_error_code_t ret = _auth_adaptor_mandatory_param_check(plugin, context, error_code);
1172 if (AUTH_ADAPTOR_ERROR_NONE != ret) {
1176 if ((0 > is_auth) || (1 < is_auth)) {
1177 auth_adaptor_error("\"is_auth\" value must be issued from \"auth_adaptor_is_auth\"");
1179 if (NULL != error_code) {
1180 *error_code = auth_adaptor_create_error_code(
1181 (int64_t) AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT,
1182 "Invalid argument (is_auth)");
1185 return AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT;
1189 auth_adaptor_error("Device is not joined. Please register to ESU");
1191 if (NULL != error_code) {
1192 *error_code = auth_adaptor_create_error_code(
1193 (int64_t) AUTH_ADAPTOR_ERROR_PLUGIN_INTERNAL,
1194 "Device is not joined");
1197 return AUTH_ADAPTOR_ERROR_PLUGIN_INTERNAL;
1201 char *old_access_token = auth_adaptor_get_access_token_dup(context);
1202 char *old_uid = auth_adaptor_get_uid_dup(context);
1204 free(context->access_token);
1205 context->access_token = NULL;
1206 context->uid = NULL;
1209 ret = plugin->handle->login(context, request, error_code, response);
1210 plugin_req_exit(ret, plugin, error_code);
1212 if (AUTH_ADAPTOR_ERROR_NONE == ret) {
1213 auth_adaptor_info("Login Successed");
1215 if (NULL == context->access_token)
1216 /* || (NULL == context->uid)) */
1218 auth_adaptor_error("Critical missmatching Error!!\n");
1219 auth_adaptor_error("Login returns success but access_token is NULL");
1220 ret = AUTH_ADAPTOR_ERROR_PLUGIN_INTERNAL;
1222 if (NULL != error_code) {
1223 *error_code = auth_adaptor_create_error_code((int64_t) AUTH_ADAPTOR_ERROR_PLUGIN_INTERNAL,
1224 "Plugin returns Login success but access_token is Empty");
1229 if (NULL != context->access_token) {
1230 free(old_access_token);
1232 auth_adaptor_error("Fill old access token by error");
1233 context->access_token = old_access_token;
1235 if (NULL != context->uid) {
1238 auth_adaptor_error("Fill old uid by error");
1239 context->uid = old_uid;
1246 * @brief Request for Refreshing Access Token (included auth_adaptor_context) [Sync API]
1248 * @param[in] plugin specifies Auth-adaptor Plugin handle
1249 * @param[in] context specifies Auth-adaptor Plugin context
1250 * @param[in] request specifies Optional value for specific Plugin (Recommend Json Object format)
1251 * @param[out] error_code specifies Error code
1252 * @param[out] response specifies Optional value from specific Plugin (Recommend Json Object format)
1253 * @return 0 on success, otherwise a positive error value
1254 * @retval error code defined in auth_error_code_e - AUTH_ADAPTOR_ERROR_NONE if Successful
1256 auth_error_code_t auth_adaptor_login_refresh(auth_adaptor_plugin_h plugin,
1257 auth_adaptor_plugin_context_h context,
1259 auth_adaptor_error_code_h *error_code,
1262 auth_error_code_t ret = _auth_adaptor_mandatory_param_check(plugin, context, error_code);
1263 if (AUTH_ADAPTOR_ERROR_NONE != ret) {
1267 if ((NULL == context->access_token) || (0 >= strlen(context->access_token))) {
1268 auth_adaptor_error("Access_token is empty. Please login first");
1270 if (NULL != error_code) {
1271 *error_code = auth_adaptor_create_error_code(
1272 (int64_t) AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT,
1273 "Invalid argument (context->access_token)");
1276 return AUTH_ADAPTOR_ERROR_NOT_AUTHORIZED;
1279 char *old_access_token = auth_adaptor_get_access_token_dup(context);
1280 char *old_uid = auth_adaptor_get_uid_dup(context);
1281 /* free(context->uid); */
1282 /* free(context->access_token); */
1283 /* context->access_token = NULL; */
1284 /* context->uid = NULL; */
1286 ret = plugin->handle->refresh_access_token(context, request, error_code, response);
1287 plugin_req_exit(ret, plugin, error_code);
1289 if (AUTH_ADAPTOR_ERROR_NONE == ret) {
1290 auth_adaptor_info("Login refresh Successed");
1292 if ((NULL == context->access_token) || (NULL == context->uid))
1294 auth_adaptor_error("Critical missmatching Error!!\n");
1295 auth_adaptor_error("Login_refresh returns success but access_token or uid is NULL");
1296 ret = AUTH_ADAPTOR_ERROR_PLUGIN_INTERNAL;
1298 if (NULL != error_code)
1300 *error_code = auth_adaptor_create_error_code((int64_t) AUTH_ADAPTOR_ERROR_PLUGIN_INTERNAL,
1301 "Plugin returns Login_refresh success but access_token or uid is Empty");
1307 if (NULL != context->access_token) {
1308 free(old_access_token);
1310 auth_adaptor_error("Fill old access token by error");
1311 context->access_token = old_access_token;
1313 if (NULL != context->uid) {
1316 auth_adaptor_error("Fill old uid by error");
1317 context->uid = old_uid;
1324 * @brief Request Account Registration [Sync API]
1326 * @param[in] plugin specifies Auth-adaptor Plugin handle
1327 * @param[in] context specifies Auth-adaptor Plugin context
1328 * @param[in] device_id specifies Device Unique ID
1329 * @param[in] request specifies Optional value for specific Plugin (Recommend Json Object format)
1330 * @param[out] error_code specifies Error code
1331 * @param[out] response specifies Optional value from specific Plugin (Recommend Json Object format)
1332 * @return 0 on success, otherwise a positive error value
1333 * @retval error code defined in auth_error_code_e - AUTH_ADAPTOR_ERROR_NONE if Successful
1335 auth_error_code_t auth_adaptor_join(auth_adaptor_plugin_h plugin,
1336 auth_adaptor_plugin_context_h context,
1337 const char *device_id,
1339 auth_adaptor_error_code_h *error_code,
1342 auth_error_code_t ret = _auth_adaptor_mandatory_param_check(plugin, context, error_code);
1343 if (AUTH_ADAPTOR_ERROR_NONE != ret) {
1347 if (NULL == plugin->handle->join) {
1348 auth_adaptor_error("Plugin does not support this API");
1349 if (NULL != error_code) {
1350 *error_code = auth_adaptor_create_error_code(
1351 (int64_t) AUTH_ADAPTOR_ERROR_UNSUPPORTED,
1352 "Plugin does not support this API (is_auth)");
1354 return AUTH_ADAPTOR_ERROR_UNSUPPORTED;
1358 ret = plugin->handle->join(context, device_id, request, error_code, response);
1359 plugin_req_exit(ret, plugin, error_code);
1365 * @brief Request Account Information (MSISDN) [Sync API]
1367 * @param[in] plugin specifies Auth-adaptor Plugin handle
1368 * @param[in] context specifies Auth-adaptor Plugin context
1369 * @param[in] request specifies Optional value for specific Plugin (Recommend Json Object format)
1370 * @param[out] msisdn specifies MSISDN
1371 * @param[out] error_code specifies Error code
1372 * @param[out] response specifies Optional value from specific Plugin (Recommend Json Object format)
1373 * @return 0 on success, otherwise a positive error value
1374 * @retval error code defined in auth_error_code_e - AUTH_ADAPTOR_ERROR_NONE if Successful
1376 auth_error_code_t auth_adaptor_get_msisdn(auth_adaptor_plugin_h plugin,
1377 auth_adaptor_plugin_context_h context,
1380 auth_adaptor_error_code_h *error_code,
1383 auth_error_code_t ret = _auth_adaptor_mandatory_param_check(plugin, context, error_code);
1384 if (AUTH_ADAPTOR_ERROR_NONE != ret) {
1389 ret = plugin->handle->get_msisdn(context, request, msisdn, error_code, response);
1390 plugin_req_exit(ret, plugin, error_code);
1396 * @brief Request Changing Service Status (Enable, Disable or etc...) [Sync API]
1398 * @param[in] plugin specifies Auth-adaptor Plugin handle
1399 * @param[in] context specifies Auth-adaptor Plugin context
1400 * @param[in] service_id specifies Service ID (The value is depended by Server)
1401 * @param[in] status specifies Service Status (The value is depended by Server)
1402 * @param[in] request specifies Optional value for specific Plugin (Recommend Json Object format)
1403 * @param[out] error_code specifies Error code
1404 * @param[out] response specifies Optional value from specific Plugin (Recommend Json Object format)
1405 * @return 0 on success, otherwise a positive error value
1406 * @retval error code defined in auth_error_code_e - AUTH_ADAPTOR_ERROR_NONE if Successful
1408 auth_error_code_t auth_adaptor_set_service_status(auth_adaptor_plugin_h plugin,
1409 auth_adaptor_plugin_context_h context,
1410 const int service_id,
1413 auth_adaptor_error_code_h *error_code,
1416 auth_error_code_t ret = _auth_adaptor_mandatory_param_check(plugin, context, error_code);
1417 if (AUTH_ADAPTOR_ERROR_NONE != ret) {
1422 ret = plugin->handle->set_service_status(context, service_id, status, request, error_code, response);
1423 plugin_req_exit(ret, plugin, error_code);
1429 * @brief Request Service Status [Sync API]
1431 * @param[in] plugin specifies Auth-adaptor Plugin handle
1432 * @param[in] context specifies Auth-adaptor Plugin context
1433 * @param[in] service_id specifies Service ID (The value is depended by Server)
1434 * @param[in] request specifies Optional value for specific Plugin (Recommend Json Object format)
1435 * @param[out] status specifies Service Status (The value is depended by Server)
1436 * @param[out] error_code specifies Error code
1437 * @param[out] response specifies Optional value from specific Plugin (Recommend Json Object format)
1438 * @return 0 on success, otherwise a positive error value
1439 * @retval error code defined in auth_error_code_e - AUTH_ADAPTOR_ERROR_NONE if Successful
1441 auth_error_code_t auth_adaptor_get_service_status(auth_adaptor_plugin_h plugin,
1442 auth_adaptor_plugin_context_h context,
1443 const int service_id,
1446 auth_adaptor_error_code_h *error_code,
1449 auth_error_code_t ret = _auth_adaptor_mandatory_param_check(plugin, context, error_code);
1450 if (AUTH_ADAPTOR_ERROR_NONE != ret) {
1455 ret = plugin->handle->get_service_status(context, service_id, request, status, error_code, response);
1456 plugin_req_exit(ret, plugin, error_code);
1462 * @brief Request Service Policy [Sync API]
1464 * @param[in] plugin specifies Auth-adaptor Plugin handle
1465 * @param[in] context specifies Auth-adaptor Plugin context
1466 * @param[in] service_id specifies Service ID (The value is depended by Server)
1467 * @param[in] request specifies Optional value for specific Plugin (Recommend Json Object format)
1468 * @param[in] default_status specifies Service default Status Policy
1469 * @param[in] policy_feature specifies Service Policy Feature
1470 * @param[in] policy_version specifies Service Policy Version
1471 * @param[in] policy_doc_url specifies Service Policy Document URL
1472 * @param[out] error_code specifies Error code
1473 * @param[out] response specifies Optional value from specific Plugin (Recommend Json Object format)
1474 * @return 0 on success, otherwise a positive error value
1475 * @retval error code defined in auth_error_code_e - AUTH_ADAPTOR_ERROR_NONE if Successful
1477 auth_error_code_t auth_adaptor_get_service_policy(auth_adaptor_plugin_h plugin,
1478 auth_adaptor_plugin_context_h context,
1479 const int service_id,
1481 char **default_status,
1482 char **policy_feature,
1483 char **policy_version,
1484 char **policy_doc_url,
1485 auth_adaptor_error_code_h *error_code,
1488 auth_error_code_t ret = _auth_adaptor_mandatory_param_check(plugin, context, error_code);
1489 if (AUTH_ADAPTOR_ERROR_NONE != ret) {
1494 ret = plugin->handle->get_service_policy(context, service_id, request, default_status,
1495 policy_feature, policy_version, policy_doc_url, error_code, response);
1496 plugin_req_exit(ret, plugin, error_code);
1502 * @brief Request Permitted Server information for Account [Sync API]
1504 * @param[in] plugin specifies Auth-adaptor Plugin handle
1505 * @param[in] context specifies Auth-adaptor Plugin context
1506 * @param[in] request specifies Optional value for specific Plugin (Recommend Json Object format)
1507 * @param[out] server_info specifies server information (URL, Scheme, Port)
1508 * @param[out] error_code specifies Error code
1509 * @param[out] response specifies Optional value from specific Plugin (Recommend Json Object format)
1510 * @return 0 on success, otherwise a positive error value
1511 * @retval error code defined in auth_error_code_e - AUTH_ADAPTOR_ERROR_NONE if Successful
1514 auth_error_code_t auth_adaptor_get_server_info(auth_adaptor_plugin_h plugin,
1515 auth_adaptor_plugin_context_h context,
1517 GHashTable **server_info,
1518 auth_adaptor_error_code_h *error_code,
1521 auth_error_code_t ret = _auth_adaptor_mandatory_param_check(plugin, context, error_code);
1522 if (AUTH_ADAPTOR_ERROR_NONE != ret) {
1527 ret = plugin->handle->get_server_info(context, request, server_info, error_code, response);
1528 plugin_req_exit(ret, plugin, error_code);
1534 auth_error_code_t auth_adaptor_external_request(auth_adaptor_plugin_h plugin,
1535 auth_adaptor_plugin_context_h context,
1536 const char *api_uri,
1537 const unsigned char *req_bundle_raw,
1539 unsigned char **res_bundle_raw,
1541 auth_adaptor_error_code_h *error_code)
1543 auth_error_code_t ret = AUTH_ADAPTOR_ERROR_NONE;
1545 if (NULL == plugin) {
1546 auth_adaptor_error("Invalid argument""(plugin: %p)", plugin);
1547 if (NULL != error_code) {
1548 *error_code = auth_adaptor_create_error_code(
1549 (int64_t) AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT,
1550 "Invalid argument (plugin)");
1553 return AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT;
1556 if (NULL == plugin->handle) {
1557 auth_adaptor_error("Plugin handle is null");
1559 if (NULL != error_code) {
1560 *error_code = auth_adaptor_create_error_code(
1561 (int64_t) AUTH_ADAPTOR_ERROR_INVALID_HANDLE,
1562 "Plugin handle is null");
1565 return AUTH_ADAPTOR_ERROR_INVALID_HANDLE;
1568 if (NULL == plugin->handle->external_request) {
1569 if (NULL != error_code) {
1570 *error_code = auth_adaptor_create_error_code(AUTH_ADAPTOR_ERROR_UNSUPPORTED,
1571 "API does not supported by plugin");
1573 return AUTH_ADAPTOR_ERROR_UNSUPPORTED;
1577 ret = plugin->handle->external_request(context, api_uri,
1578 req_bundle_raw, req_len,
1579 res_bundle_raw, res_len, error_code);
1580 plugin_req_exit(ret, plugin, error_code);
1585 /* ///////////////////////////////////////////////////////////////////////////////
1586 ///////////////////////////////////////////////////////////////////////////////
1587 //////////// Internal function description (for forked plugin) //////////////
1588 ///////////////////////////////////////////////////////////////////////////////
1589 /////////////////////////////////////////////////////////////////////////////// */
1591 void *_auth_adaptor_plugin_message_collector(void *data)
1593 auth_adaptor_h adaptor = (auth_adaptor_h) data;
1595 auth_adaptor_info("3rd party plugin listener run");
1596 int i, lagest_fd = -1;
1599 tv.tv_sec = 10L; /* TODO change to define or meaningful value */
1600 char msg_buf[PLUGIN_MESSAGE_PROTOCOL_MAX_BUF_SIZE] = {0, };
1601 int buf_size, rcv_len;
1602 GList *dead_list = NULL;
1605 /* Clears and sets fds for select */
1607 FD_SET(adaptor->rd_cmd[0], &read_set);
1608 lagest_fd = adaptor->rd_cmd[0];
1610 /* Sets plugin fds for select */
1611 for (i = 0; i < g_list_length(adaptor->rd_list); i++) {
1612 int fd = (int) g_list_nth_data(adaptor->rd_list, i);
1613 FD_SET(fd, &read_set);
1614 if (lagest_fd < fd) {
1619 /* Select with timeout (for avoid blocking issue) */
1620 int stmt = select((lagest_fd + 1), &read_set, NULL, NULL, &tv);
1621 IF_IS_PLUGIN_THAN_RETURN_NULL();
1623 /* auth_adaptor_debug("select refrech by timeout(%ld sec) [id : %d]", tv.tv_sec, g_process_identity); */
1624 if (0L >= tv.tv_sec) {
1625 /* auth_adaptor_debug("Resets selector timeout sec"); */
1628 IF_IS_PLUGIN_THAN_RETURN_NULL();
1629 } else if (stmt > 0) {
1630 /* Checking message queue with Plugin processes. */
1631 for (i = 0; i < g_list_length(adaptor->rd_list); i++) {
1632 IF_IS_PLUGIN_THAN_RETURN_NULL();
1633 int fd = (int) g_list_nth_data(adaptor->rd_list, i);
1634 if (FD_ISSET(fd, &read_set)) {
1635 IF_IS_PLUGIN_THAN_RETURN_NULL();
1636 /* pre-read buf size */
1637 rcv_len = read(fd, &buf_size, sizeof(int));
1639 auth_adaptor_debug("Child process dead (Remove from listening queue)");
1640 dead_list = g_list_append(dead_list, (gpointer)fd);
1643 /* allocates and read buf data */
1644 memset(msg_buf, 0, PLUGIN_MESSAGE_PROTOCOL_MAX_BUF_SIZE);
1645 buf_size %= PLUGIN_MESSAGE_PROTOCOL_MAX_BUF_SIZE - 1;
1646 rcv_len = read(fd, msg_buf, buf_size);
1647 auth_adaptor_debug("read message [%s][%d]", msg_buf, rcv_len);
1650 /* transfer data to adaptor */
1651 __auth_adaptor_transfer_message(msg_buf);
1653 auth_adaptor_debug("Child process dead (Remove from listening queue)");
1654 dead_list = g_list_append(dead_list, (gpointer)fd);
1659 /* Checking message queue with Adaptor internal command. */
1660 IF_IS_PLUGIN_THAN_RETURN_NULL();
1661 if (FD_ISSET(adaptor->rd_cmd[0], &read_set)) {
1662 int fd = adaptor->rd_cmd[0];
1663 IF_IS_PLUGIN_THAN_RETURN_NULL();
1664 /* pre-read buf size */
1665 rcv_len = read(fd, &buf_size, sizeof(int));
1668 auth_adaptor_debug("Parent process dead : Listener break");
1672 /* allocates and read buf data */
1673 memset(msg_buf, 0, PLUGIN_MESSAGE_PROTOCOL_MAX_BUF_SIZE);
1674 buf_size %= PLUGIN_MESSAGE_PROTOCOL_MAX_BUF_SIZE - 1;
1675 rcv_len = read(fd, msg_buf, buf_size);
1676 auth_adaptor_debug("read message [%s][%d]", msg_buf, rcv_len);
1679 auth_adaptor_debug("Parent process dead : Listener break");
1683 /* parse cmd message (e.g. append read_fd / change timeout sec / stop listener) */
1684 int cmd_ret = __auth_adaptor_parse_message_cmd(adaptor, msg_buf);
1686 auth_adaptor_info("3rd party plugin listener stopped by adaptor cmd");
1691 /* Remove fd with disconnected plugin. */
1692 for (i = 0; i < g_list_length(dead_list); i++) {
1693 adaptor->rd_list = g_list_remove(adaptor->rd_list, (gpointer) g_list_nth_data(dead_list, i));
1695 g_list_free(dead_list);
1698 auth_adaptor_error("plugin message listener error (errno : %d)", errno);
1701 auth_adaptor_info("3rd party plugin listener stopped");
1706 void __auth_adaptor_transfer_message(const char *msg)
1708 plugin_message_h t_msg = NULL;
1710 ret = plugin_message_deserialize(msg, &t_msg);
1712 pmnumber req_type, req_id;
1713 ret = plugin_message_get_value_number(t_msg, PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE, &req_type);
1715 if (!ret && (PLUGIN_MESSAGE_TYPE_FUNCTION == req_type)) {
1716 auth_adaptor_debug("MEssage function type : function");
1717 ret = plugin_message_get_value_number(t_msg, PLUGIN_MESSAGE_ELEMENT_REQUEST_ID, &req_id);
1719 auth_adaptor_debug("Send plugin data to requester");
1720 int hooked_fd = (int) req_id;
1721 int len = strlen(msg);
1722 ret = write(hooked_fd, &len, sizeof(int));
1724 ret = write(hooked_fd, msg, sizeof(char) * len);
1727 auth_adaptor_debug("Couldn't get request id");
1729 } else if (!ret && (PLUGIN_MESSAGE_TYPE_CALLBACK == req_type)) {
1730 auth_adaptor_warning("Auth adaptor unsupport callback yet");
1732 auth_adaptor_warning("Received message parsing fail.");
1734 plugin_message_destroy(t_msg);
1738 int __auth_adaptor_parse_message_cmd(auth_adaptor_h adaptor, char *msg)
1740 char *cmd_data = NULL;
1741 if (0 == strncmp(PLUGIN_MESSAGE_LISTENER_CMD_APPEND_FD, msg, strlen(PLUGIN_MESSAGE_LISTENER_CMD_APPEND_FD))) {
1742 cmd_data = msg + strlen(PLUGIN_MESSAGE_LISTENER_CMD_APPEND_FD);
1743 int fd = atoi(cmd_data);
1745 adaptor->rd_list = g_list_append(adaptor->rd_list, (gpointer)fd);
1746 } else if (0 == strncmp(PLUGIN_MESSAGE_LISTENER_CMD_STOP, msg, strlen(PLUGIN_MESSAGE_LISTENER_CMD_STOP))) {
1754 void _auth_adaptor_send_cmd_add_fd(auth_adaptor_h adaptor, int fd)
1756 char cmd_buf[256] = {0, };
1757 snprintf(cmd_buf, 255, "%s%d", PLUGIN_MESSAGE_LISTENER_CMD_APPEND_FD, fd);
1758 int len = strlen(cmd_buf);
1761 g_mutex_lock(&adaptor->rd_mutex);
1762 wr_ret = write(adaptor->rd_cmd[1], &len, sizeof(int));
1763 wr_ret = write(adaptor->rd_cmd[1], cmd_buf, sizeof(char) * len);
1764 g_mutex_unlock(&adaptor->rd_mutex);
1765 auth_adaptor_debug("writed (%d)(%s)", wr_ret, cmd_buf);
1768 void _auth_adaptor_send_cmd_stop_listen(auth_adaptor_h adaptor)
1770 char cmd_buf[256] = {0, };
1771 snprintf(cmd_buf, 255, "%s", PLUGIN_MESSAGE_LISTENER_CMD_STOP);
1772 int len = strlen(cmd_buf);
1775 g_mutex_lock(&adaptor->rd_mutex);
1776 wr_ret = write(adaptor->rd_cmd[1], &len, sizeof(int));
1777 wr_ret = write(adaptor->rd_cmd[1], cmd_buf, sizeof(char) * len);
1778 g_mutex_unlock(&adaptor->rd_mutex);
1779 auth_adaptor_debug("writed (%d)(%s)", wr_ret, cmd_buf);
1782 static int auth_adaptor_send_message_to_plugin_sync(auth_adaptor_plugin_h plugin,
1783 plugin_message_h send_message,
1784 plugin_message_h *receive_message)
1787 int wfd = plugin->wd;
1790 if (pipe(sync_hook) != -1) {
1791 char read_buf[PLUGIN_MESSAGE_PROTOCOL_MAX_BUF_SIZE] = {0, };
1793 plugin_message_set_value_number(send_message, PLUGIN_MESSAGE_ELEMENT_REQUEST_ID, (pmnumber) sync_hook[1]);
1794 char *stream = NULL;
1795 io_ret = plugin_message_serialize(send_message, &stream);
1796 int len = strlen(stream);
1798 g_mutex_lock(&plugin->message_mutex);
1799 io_ret = write(wfd, &len, sizeof(len));
1800 io_ret = write(wfd, stream, sizeof(char) * len);
1801 g_mutex_unlock(&plugin->message_mutex);
1806 io_ret = read(sync_hook[0], &len, sizeof(len));
1809 memset(read_buf, 0, PLUGIN_MESSAGE_PROTOCOL_MAX_BUF_SIZE);
1810 len %= PLUGIN_MESSAGE_PROTOCOL_MAX_BUF_SIZE - 1;
1811 io_ret = read(sync_hook[0], read_buf, len);
1813 auth_adaptor_debug("io ret : %d", io_ret);
1814 close(sync_hook[0]);
1815 close(sync_hook[1]);
1817 plugin_message_h _rcv;
1818 if (0 < strlen(read_buf)) {
1819 io_ret = plugin_message_deserialize(read_buf, &_rcv);
1821 *receive_message = _rcv;
1831 auth_adaptor_plugin_handle_h __auth_adaptor_create_3rd_party_plugin_handle(const char *plugin_uri)
1833 auth_adaptor_plugin_handle_h handle = (auth_adaptor_plugin_handle_h) calloc(1, sizeof(auth_adaptor_plugin_handle_t));
1835 if (NULL != handle) {
1836 handle->create_context = auth_plugin_send_create_context;
1837 handle->destroy_context = auth_plugin_send_destroy_context;
1838 handle->is_auth = auth_plugin_send_is_auth;
1839 handle->login = auth_plugin_send_login;
1840 handle->refresh_access_token = auth_plugin_send_refresh_access_token;
1842 handle->set_service_status = auth_plugin_send_set_service_status;
1843 handle->get_msisdn = auth_plugin_send_get_msisdn;
1844 handle->get_service_status = auth_plugin_send_get_service_status;
1845 handle->get_service_policy = auth_plugin_send_get_service_policy;
1846 handle->get_server_info = auth_plugin_send_get_server_info;
1849 handle->plugin_uri = strdup(plugin_uri);
1855 auth_error_code_t auth_plugin_send_create_context(auth_adaptor_plugin_context_h *context,
1856 const char *user_id,
1857 const char *user_password,
1859 const char *app_secret,
1862 auth_adaptor_plugin_h plugin = NULL;
1863 plugin = (*context)->plugin_handle;
1866 plugin_message_h message = NULL;
1867 ret = plugin_message_create(&message);
1870 (*context)->context_id = (int) (intptr_t)(*context);
1872 plugin_message_set_value_number(message, PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID,
1873 (pmnumber) (*context)->context_id);
1874 plugin_message_set_value_string(message, PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME,
1875 AUTH_PLUGIN_INTERFACE_CREATE_CONTEXT);
1877 plugin_message_set_value_number(message, PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE,
1878 (pmnumber) PLUGIN_MESSAGE_TYPE_FUNCTION);
1880 int param_index = 1;
1881 plugin_message_set_param_string(message, param_index++, user_id);
1882 plugin_message_set_param_string(message, param_index++, user_password);
1883 plugin_message_set_param_string(message, param_index++, app_id);
1884 plugin_message_set_param_string(message, param_index++, app_secret);
1885 plugin_message_set_param_string(message, param_index++, imsi);
1887 plugin_message_h result_message = NULL;
1888 ret = auth_adaptor_send_message_to_plugin_sync(plugin, message, &result_message);
1892 plugin_message_get_value_number(result_message, PLUGIN_MESSAGE_ELEMENT_RESULT_CODE, &ret_code);
1894 ret = (int) ret_code;
1896 char *ret_msg = NULL;
1897 if (AUTH_ADAPTOR_ERROR_NONE == ret) {
1898 auth_adaptor_debug("Create context successed");
1900 plugin_message_get_value_string(result_message, PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE, &ret_msg);
1901 auth_adaptor_debug("Create context failed (%d)(%s)", ret, ret_msg);
1908 plugin_message_destroy(result_message);
1911 ret = AUTH_ADAPTOR_ERROR_PLUGIN_INTERNAL;
1913 plugin_message_destroy(message);
1920 auth_error_code_t auth_plugin_send_destroy_context(auth_adaptor_plugin_context_h context)
1922 auth_adaptor_plugin_h plugin = NULL;
1923 plugin = context->plugin_handle;
1926 plugin_message_h message = NULL;
1927 ret = plugin_message_create(&message);
1930 plugin_message_set_value_number(message, PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID,
1931 (pmnumber) context->context_id);
1932 plugin_message_set_value_string(message, PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME,
1933 AUTH_PLUGIN_INTERFACE_DESTROY_CONTEXT);
1935 plugin_message_set_value_number(message, PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE,
1936 (pmnumber) PLUGIN_MESSAGE_TYPE_FUNCTION);
1938 plugin_message_h result_message = NULL;
1939 ret = auth_adaptor_send_message_to_plugin_sync(plugin, message, &result_message);
1943 plugin_message_get_value_number(result_message, PLUGIN_MESSAGE_ELEMENT_RESULT_CODE, &ret_code);
1945 ret = (int) ret_code;
1947 char *ret_msg = NULL;
1948 if (AUTH_ADAPTOR_ERROR_NONE == ret) {
1949 auth_adaptor_debug("Destroy context successed");
1951 plugin_message_get_value_string(result_message, PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE, &ret_msg);
1952 auth_adaptor_debug("Destroy context failed (%d)(%s)", ret, ret_msg);
1956 auth_adaptor_debug("Force release memory by adaptor process");
1957 free(context->access_token);
1961 plugin_message_destroy(result_message);
1964 ret = AUTH_ADAPTOR_ERROR_PLUGIN_INTERNAL;
1966 plugin_message_destroy(message);
1976 auth_error_code_t auth_plugin_send_is_auth(auth_adaptor_plugin_context_h context,
1979 auth_adaptor_error_code_h *error,
1982 auth_adaptor_plugin_h plugin = NULL;
1983 plugin = context->plugin_handle;
1986 plugin_message_h message = NULL;
1987 ret = plugin_message_create(&message);
1990 plugin_message_set_value_number(message, PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID, (pmnumber) context->context_id);
1991 plugin_message_set_value_string(message, PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME, AUTH_PLUGIN_INTERFACE_IS_AUTH);
1993 plugin_message_set_value_number(message, PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE, (pmnumber) PLUGIN_MESSAGE_TYPE_FUNCTION);
1995 plugin_message_h result_message = NULL;
1996 ret = auth_adaptor_send_message_to_plugin_sync(plugin, message, &result_message);
1999 pmnumber _is_auth, ret_code;
2000 plugin_message_get_value_number(result_message, PLUGIN_MESSAGE_ELEMENT_RESULT_CODE, &ret_code);
2002 ret = (int) ret_code;
2004 char *ret_msg = NULL;
2005 if (AUTH_ADAPTOR_ERROR_NONE == ret) {
2006 auth_adaptor_debug("Is_auth successed");
2007 plugin_message_get_param_number(result_message, 1, &_is_auth);
2008 *is_auth = (int)_is_auth;
2010 plugin_message_get_value_string(result_message, PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE, &ret_msg);
2011 auth_adaptor_debug("Is_auth failed (%d)(%s)", ret, ret_msg);
2012 if (NULL != error) {
2013 auth_adaptor_error_code_h error_code = auth_adaptor_create_error_code(ret, ret_msg);
2014 *error = error_code;
2020 plugin_message_destroy(result_message);
2023 ret = AUTH_ADAPTOR_ERROR_PLUGIN_INTERNAL;
2025 plugin_message_destroy(message);
2030 auth_error_code_t auth_plugin_send_join(auth_adaptor_plugin_context_h context,
2031 const char *device_id,
2033 auth_adaptor_error_code_h *error,
2036 auth_adaptor_plugin_h plugin = NULL;
2037 plugin = context->plugin_handle;
2040 plugin_message_h message = NULL;
2041 ret = plugin_message_create(&message);
2044 plugin_message_set_value_number(message, PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID, (pmnumber) context->context_id);
2045 plugin_message_set_value_string(message, PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME, AUTH_PLUGIN_INTERFACE_JOIN);
2047 plugin_message_set_value_number(message, PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE, (pmnumber) PLUGIN_MESSAGE_TYPE_FUNCTION);
2049 plugin_message_h result_message = NULL;
2050 ret = auth_adaptor_send_message_to_plugin_sync(plugin, message, &result_message);
2054 plugin_message_get_value_number(result_message, PLUGIN_MESSAGE_ELEMENT_RESULT_CODE, &ret_code);
2056 ret = (int) ret_code;
2058 char *ret_msg = NULL;
2059 if (AUTH_ADAPTOR_ERROR_NONE == ret) {
2060 auth_adaptor_debug("Join successed");
2062 plugin_message_get_value_string(result_message, PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE, &ret_msg);
2063 auth_adaptor_debug("Join failed (%d)(%s)", ret, ret_msg);
2064 if (NULL != error) {
2065 auth_adaptor_error_code_h error_code = auth_adaptor_create_error_code(ret, ret_msg);
2066 *error = error_code;
2072 plugin_message_destroy(result_message);
2075 ret = AUTH_ADAPTOR_ERROR_PLUGIN_INTERNAL;
2077 plugin_message_destroy(message);
2084 auth_error_code_t auth_plugin_send_login(auth_adaptor_plugin_context_h context,
2086 auth_adaptor_error_code_h *error,
2089 auth_adaptor_plugin_h plugin = NULL;
2090 plugin = context->plugin_handle;
2093 plugin_message_h message = NULL;
2094 ret = plugin_message_create(&message);
2097 plugin_message_set_value_number(message, PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID,
2098 (pmnumber) context->context_id);
2099 plugin_message_set_value_string(message, PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME,
2100 AUTH_PLUGIN_INTERFACE_LOGIN);
2102 plugin_message_set_value_number(message, PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE,
2103 (pmnumber) PLUGIN_MESSAGE_TYPE_FUNCTION);
2105 plugin_message_h result_message = NULL;
2106 ret = auth_adaptor_send_message_to_plugin_sync(plugin, message, &result_message);
2110 plugin_message_get_value_number(result_message, PLUGIN_MESSAGE_ELEMENT_RESULT_CODE, &ret_code);
2112 ret = (int) ret_code;
2113 char *ret_msg = NULL;
2114 if (AUTH_ADAPTOR_ERROR_NONE == ret) {
2115 auth_adaptor_debug("Login successed");
2116 char *access_token = NULL;
2119 plugin_message_get_param_string(result_message, param_idx++, &access_token);
2120 plugin_message_get_param_string(result_message, param_idx++, &uid);
2121 auth_adaptor_debug("access token : %s", access_token);
2122 auth_adaptor_debug("uid : %s", uid);
2124 context->access_token = access_token;
2127 plugin_message_get_value_string(result_message, PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE, &ret_msg);
2128 auth_adaptor_debug("Login failed (%d)(%s)", ret, ret_msg);
2129 if (NULL != error) {
2130 auth_adaptor_error_code_h error_code = auth_adaptor_create_error_code(ret, ret_msg);
2131 *error = error_code;
2136 plugin_message_destroy(result_message);
2139 ret = AUTH_ADAPTOR_ERROR_PLUGIN_INTERNAL;
2141 plugin_message_destroy(message);
2147 auth_error_code_t auth_plugin_send_refresh_access_token(auth_adaptor_plugin_context_h context,
2149 auth_adaptor_error_code_h *error,
2152 auth_adaptor_plugin_h plugin = NULL;
2153 plugin = context->plugin_handle;
2156 plugin_message_h message = NULL;
2157 ret = plugin_message_create(&message);
2160 plugin_message_set_value_number(message, PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID,
2161 (pmnumber) context->context_id);
2162 plugin_message_set_value_string(message, PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME,
2163 AUTH_PLUGIN_INTERFACE_REFRESH_ACCESS_TOKEN);
2165 plugin_message_set_value_number(message, PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE,
2166 (pmnumber) PLUGIN_MESSAGE_TYPE_FUNCTION);
2168 plugin_message_h result_message = NULL;
2169 ret = auth_adaptor_send_message_to_plugin_sync(plugin, message, &result_message);
2173 plugin_message_get_value_number(result_message, PLUGIN_MESSAGE_ELEMENT_RESULT_CODE, &ret_code);
2175 ret = (int) ret_code;
2176 char *ret_msg = NULL;
2177 if (AUTH_ADAPTOR_ERROR_NONE == ret) {
2178 auth_adaptor_debug("Login refresh successed");
2179 char *access_token = NULL;
2182 plugin_message_get_param_string(result_message, param_idx++, &access_token);
2183 plugin_message_get_param_string(result_message, param_idx++, &uid);
2184 auth_adaptor_debug("access token : %s", access_token);
2185 auth_adaptor_debug("uid : %s", uid);
2187 context->access_token = access_token;
2190 plugin_message_get_value_string(result_message, PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE, &ret_msg);
2191 auth_adaptor_debug("Login refresh failed (%d)(%s)", ret, ret_msg);
2192 if (NULL != error) {
2193 auth_adaptor_error_code_h error_code = auth_adaptor_create_error_code(ret, ret_msg);
2194 *error = error_code;
2199 plugin_message_destroy(result_message);
2202 ret = AUTH_ADAPTOR_ERROR_PLUGIN_INTERNAL;
2204 plugin_message_destroy(message);
2210 auth_error_code_t auth_plugin_send_get_server_info(auth_adaptor_plugin_context_h context,
2212 GHashTable **server_info,
2213 auth_adaptor_error_code_h *error,
2217 auth_adaptor_plugin_h plugin = NULL;
2218 plugin = context->plugin_handle;
2221 plugin_message_h message = NULL;
2222 ret = plugin_message_create(&message);
2226 plugin_message_set_value_number(message, PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID,
2227 (pmnumber) context->context_id);
2228 plugin_message_set_value_string(message, PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME,
2229 AUTH_PLUGIN_INTERFACE_LOGIN);
2231 plugin_message_set_value_number(message, PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE,
2232 (pmnumber) PLUGIN_MESSAGE_TYPE_FUNCTION);
2234 plugin_message_h result_message = NULL;
2235 ret = auth_adaptor_send_message_to_plugin_sync(plugin, message, &result_message);
2240 plugin_message_get_value_number(result_message, PLUGIN_MESSAGE_ELEMENT_RESULT_CODE, &ret_code);
2242 ret = (int) ret_code;
2243 char *ret_msg = NULL;
2244 if (AUTH_ADAPTOR_ERROR_NONE == ret)
2246 auth_adaptor_debug("Get server info successed");
2247 char *server_info_raw = NULL;
2249 plugin_message_get_param_string(result_message, param_idx++, &server_info_raw);
2251 if ((NULL != server_info) && (NULL != server_info_raw))
2253 bundle *info_data = bundle_decode((bundle_raw)server_info_raw, strlen(server_info_raw));
2254 if (NULL != info_data)
2256 GHashTable *ht = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
2257 bundle_foreach(info_data, server_info_iter, (void *) ht);
2262 free(server_info_raw);
2266 plugin_message_get_value_string(result_message, PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE, &ret_msg);
2267 auth_adaptor_debug("Login failed (%d)(%s)", ret, ret_msg);
2270 auth_adaptor_error_code_h error_code = auth_adaptor_create_error_code(ret, ret_msg);
2271 *error = error_code;
2276 plugin_message_destroy(result_message);
2281 ret = AUTH_ADAPTOR_ERROR_PLUGIN_INTERNAL;
2283 plugin_message_destroy(message);
2291 auth_error_code_t auth_plugin_send_set_service_status(auth_adaptor_plugin_context_h context,
2292 const int service_id,
2295 auth_adaptor_error_code_h *error,
2298 return AUTH_ADAPTOR_ERROR_UNSUPPORTED;
2301 auth_error_code_t auth_plugin_send_get_msisdn(auth_adaptor_plugin_context_h context,
2304 auth_adaptor_error_code_h *error,
2307 return AUTH_ADAPTOR_ERROR_UNSUPPORTED;
2310 auth_error_code_t auth_plugin_send_get_service_status(auth_adaptor_plugin_context_h context,
2311 const int service_id,
2314 auth_adaptor_error_code_h *error,
2317 return AUTH_ADAPTOR_ERROR_UNSUPPORTED;
2320 auth_error_code_t auth_plugin_send_get_service_policy(auth_adaptor_plugin_context_h context,
2321 const int service_id,
2323 char **default_status,
2324 char **policy_feature,
2325 char **policy_version,
2326 char **policy_doc_url,
2327 auth_adaptor_error_code_h *error,
2330 return AUTH_ADAPTOR_ERROR_UNSUPPORTED;
2333 auth_error_code_t auth_plugin_send_get_server_info(auth_adaptor_plugin_context_h context,
2335 GHashTable **server_info,
2336 auth_adaptor_error_code_h *error,
2339 return AUTH_ADAPTOR_ERROR_UNSUPPORTED;
2343 /* For forked plugin */
2344 void *_auth_plugin_request_collector(void *data)
2346 auth_adaptor_plugin_h plugin = (auth_adaptor_plugin_h) data;
2348 int rcv_len, buf_size;
2349 char msg_buf[PLUGIN_MESSAGE_PROTOCOL_MAX_BUF_SIZE] = {0, };
2352 /* pre-read buf size */
2353 rcv_len = read(plugin->rd, &buf_size, sizeof(int));
2356 LOGD("shutdown by adaptor disconnected");
2360 /* allocates and read buf data */
2361 memset(msg_buf, 0, PLUGIN_MESSAGE_PROTOCOL_MAX_BUF_SIZE);
2362 buf_size %= PLUGIN_MESSAGE_PROTOCOL_MAX_BUF_SIZE - 1;
2363 rcv_len = read(plugin->rd, msg_buf, buf_size);
2364 LOGD("read message [%s][len: %d]", msg_buf, rcv_len);
2367 LOGD("shutdown by adaptor disconnected");
2371 char *result = NULL;
2372 __auth_plugin_progress_command(plugin, msg_buf, &result);
2374 if (NULL != result) {
2375 int res_len = strlen(result);
2376 rcv_len = write(plugin->wd, &res_len, sizeof(int));
2377 rcv_len = write(plugin->wd, result, sizeof(char) * res_len);
2379 /* transfer data to adaptor */
2380 /* __auth_adaptor_transfer_message(adaptor, msg_buf); */
2385 auth_adaptor_plugin_context_h __auth_plugin_get_context_by_context_id(auth_adaptor_plugin_h plugin, int context_id)
2387 if (NULL == plugin) {
2391 /* For forked plugin */
2392 auth_adaptor_plugin_context_h ctx = NULL;
2394 len = g_list_length(plugin->contexts);
2396 for (i = 0; i < len; i++) {
2397 ctx = (auth_adaptor_plugin_context_h) g_list_nth_data(plugin->contexts, i);
2399 if (context_id == ctx->context_id) {
2406 void __auth_plugin_progress_command(auth_adaptor_plugin_h plugin, char *order, char **result)
2409 plugin_message_h m_order = NULL;
2410 plugin_message_h m_result = NULL;
2412 if ((NULL == order) || (plugin_message_deserialize(order, &m_order))) {
2413 LOGE("[%s/%d] Message parse error", __FUNCTION__, __LINE__);
2415 } else if (plugin_message_create(&m_result)) {
2416 plugin_message_destroy(m_order);
2419 LOGE("[%s/%d] Message parse error", __FUNCTION__, __LINE__);
2423 char *func_name = NULL;
2426 pmnumber ctx, req, type;
2428 plugin_message_get_value_number(m_order, PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID, &ctx);
2429 plugin_message_set_value_number(m_result, PLUGIN_MESSAGE_ELEMENT_CONTEXT_ID, ctx);
2430 plugin_message_get_value_number(m_order, PLUGIN_MESSAGE_ELEMENT_REQUEST_ID, &req);
2431 plugin_message_set_value_number(m_result, PLUGIN_MESSAGE_ELEMENT_REQUEST_ID, req);
2432 plugin_message_get_value_string(m_order, PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME, &func_name);
2433 plugin_message_set_value_string(m_result, PLUGIN_MESSAGE_ELEMENT_FUNCTION_NAME, func_name);
2434 plugin_message_get_value_number(m_result, PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE, &type);
2435 plugin_message_set_value_number(m_result, PLUGIN_MESSAGE_ELEMENT_MESSAGE_TYPE, type);
2436 plugin_message_set_value_number(m_result, PLUGIN_MESSAGE_ELEMENT_RESULT_CODE, (pmnumber)AUTH_ADAPTOR_ERROR_NONE);
2437 context_id = (int) ctx;
2438 auth_adaptor_plugin_context_h context = __auth_plugin_get_context_by_context_id(plugin, context_id);
2440 auth_adaptor_error_code_h error_code = NULL;
2442 if (0 == strncmp(AUTH_PLUGIN_INTERFACE_CREATE_CONTEXT,
2443 func_name, strlen(AUTH_PLUGIN_INTERFACE_CREATE_CONTEXT))) {
2444 LOGD(">>>>>> %s func start", func_name);
2445 char *user_id = NULL;
2446 char *user_password = NULL;
2447 char *app_id = NULL;
2448 char *app_secret = NULL;
2449 char *service_name = NULL;
2453 plugin_message_get_param_string(m_order, param_idx++, &user_id);
2454 plugin_message_get_param_string(m_order, param_idx++, &user_password);
2455 plugin_message_get_param_string(m_order, param_idx++, &app_id);
2456 plugin_message_get_param_string(m_order, param_idx++, &app_secret);
2457 plugin_message_get_param_string(m_order, param_idx++, &imsi);
2459 LOGD("Call library function");
2460 context = auth_adaptor_create_plugin_context(plugin,
2461 user_id, user_password, app_id, app_secret, imsi, "");
2462 if (NULL == context) {
2463 LOGE("[%s<%s>/%d] Could not create context", __FUNCTION__, func_name, __LINE__);
2464 plugin_message_set_value_number(m_result,
2465 PLUGIN_MESSAGE_ELEMENT_RESULT_CODE,
2466 (pmnumber)AUTH_ADAPTOR_ERROR_PLUGIN_INTERNAL);
2467 plugin_message_set_value_string(m_result,
2468 PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE,
2469 "Could not create context");
2471 LOGD("[%s<%s>/%d] Created context successfuly", __FUNCTION__, func_name, __LINE__);
2472 context->context_id = context_id;
2474 plugin_message_set_value_number(m_result,
2475 PLUGIN_MESSAGE_ELEMENT_RESULT_CODE,
2476 (pmnumber)AUTH_ADAPTOR_ERROR_NONE);
2480 free(user_password);
2484 LOGD("<<<<<< %s func end", func_name);
2485 } else if (0 == strncmp(AUTH_PLUGIN_INTERFACE_DESTROY_CONTEXT,
2486 func_name, strlen(AUTH_PLUGIN_INTERFACE_DESTROY_CONTEXT))) {
2487 LOGD(">>>>>> %s func start", func_name);
2488 if (NULL == context) {
2489 LOGE("[%s<%s>/%d] Could not found context", __FUNCTION__, func_name, __LINE__);
2490 plugin_message_set_value_number(m_result,
2491 PLUGIN_MESSAGE_ELEMENT_RESULT_CODE,
2492 (pmnumber)AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT);
2493 plugin_message_set_value_string(m_result,
2494 PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE,
2495 "Invalid argument (context is NULL)");
2497 LOGD("[%s<%s>/%d] function success", __FUNCTION__, func_name, __LINE__);
2498 auth_adaptor_destroy_plugin_context(plugin, context);
2500 plugin_message_set_value_number(m_result,
2501 PLUGIN_MESSAGE_ELEMENT_RESULT_CODE,
2502 (pmnumber)AUTH_ADAPTOR_ERROR_NONE);
2504 LOGD("<<<<<< %s func end", func_name);
2505 } else if (0 == strncmp(AUTH_PLUGIN_INTERFACE_IS_AUTH,
2506 func_name, strlen(AUTH_PLUGIN_INTERFACE_IS_AUTH))) {
2507 LOGD(">>>>>> %s func start", func_name);
2509 LOGD("Call library function");
2510 if (NULL == context) {
2511 LOGE("[%s<%s>/%d] Could not found context", __FUNCTION__, func_name, __LINE__);
2512 plugin_message_set_value_number(m_result,
2513 PLUGIN_MESSAGE_ELEMENT_RESULT_CODE,
2514 (pmnumber)AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT);
2515 plugin_message_set_value_string(m_result,
2516 PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE,
2517 "Invalid argument (context is NULL)");
2519 ret = plugin->handle->is_auth(context, NULL, &is_auth, &error_code, NULL);
2520 if (AUTH_ADAPTOR_ERROR_NONE == ret) {
2522 plugin_message_set_param_number(m_result, param_idx++, (pmnumber)is_auth);
2523 plugin_message_set_value_number(m_result, PLUGIN_MESSAGE_ELEMENT_RESULT_CODE, (pmnumber)ret);
2524 } else if (NULL != error_code) {
2525 plugin_message_set_value_number(m_result,
2526 PLUGIN_MESSAGE_ELEMENT_RESULT_CODE,
2527 (pmnumber)error_code->code);
2528 plugin_message_set_value_string(m_result,
2529 PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE,
2530 error_code->msg ? error_code->msg : "");
2531 free(error_code->msg);
2534 plugin_message_set_value_number(m_result, PLUGIN_MESSAGE_ELEMENT_RESULT_CODE, (pmnumber)ret);
2537 LOGD("<<<<<< %s func end", func_name);
2538 } else if (0 == strncmp(AUTH_PLUGIN_INTERFACE_LOGIN,
2539 func_name, strlen(AUTH_PLUGIN_INTERFACE_LOGIN))) {
2540 LOGD(">>>>>> %s func start", func_name);
2541 LOGD("Call library function");
2542 if (NULL == context) {
2543 LOGE("[%s<%s>/%d] Could not found context", __FUNCTION__, func_name, __LINE__);
2544 plugin_message_set_value_number(m_result,
2545 PLUGIN_MESSAGE_ELEMENT_RESULT_CODE,
2546 (pmnumber)AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT);
2547 plugin_message_set_value_string(m_result,
2548 PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE,
2549 "Invalid argument (context is NULL)");
2551 ret = plugin->handle->login(context, NULL, &error_code, NULL);
2552 if (AUTH_ADAPTOR_ERROR_NONE == ret) {
2554 plugin_message_set_param_string(m_result, param_idx++, context->access_token ? context->access_token : "");
2555 plugin_message_set_param_string(m_result, param_idx++, context->uid ? context->uid : "");
2556 plugin_message_set_value_number(m_result, PLUGIN_MESSAGE_ELEMENT_RESULT_CODE, (pmnumber)ret);
2557 } else if (NULL != error_code) {
2558 plugin_message_set_value_number(m_result,
2559 PLUGIN_MESSAGE_ELEMENT_RESULT_CODE,
2560 (pmnumber)error_code->code);
2561 plugin_message_set_value_string(m_result,
2562 PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE,
2563 error_code->msg ? error_code->msg : "");
2564 free(error_code->msg);
2567 plugin_message_set_value_number(m_result, PLUGIN_MESSAGE_ELEMENT_RESULT_CODE, (pmnumber)ret);
2570 LOGD("<<<<<< %s func end", func_name);
2571 } else if (0 == strncmp(AUTH_PLUGIN_INTERFACE_REFRESH_ACCESS_TOKEN,
2572 func_name, strlen(AUTH_PLUGIN_INTERFACE_REFRESH_ACCESS_TOKEN))) {
2573 LOGD(">>>>>> %s func start", func_name);
2574 LOGD("Call library function");
2575 if (NULL == context) {
2576 LOGE("[%s<%s>/%d] Could not found context", __FUNCTION__, func_name, __LINE__);
2577 plugin_message_set_value_number(m_result,
2578 PLUGIN_MESSAGE_ELEMENT_RESULT_CODE,
2579 (pmnumber)AUTH_ADAPTOR_ERROR_INVALID_ARGUMENT);
2580 plugin_message_set_value_string(m_result,
2581 PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE,
2582 "Invalid argument (context is NULL)");
2584 ret = plugin->handle->refresh_access_token(context, NULL, &error_code, NULL);
2585 if (AUTH_ADAPTOR_ERROR_NONE == ret) {
2587 plugin_message_set_param_string(m_result, param_idx++, context->access_token ? context->access_token : "");
2588 plugin_message_set_param_string(m_result, param_idx++, context->uid ? context->uid : "");
2589 plugin_message_set_value_number(m_result, PLUGIN_MESSAGE_ELEMENT_RESULT_CODE, (pmnumber)ret);
2590 } else if (NULL != error_code) {
2591 plugin_message_set_value_number(m_result,
2592 PLUGIN_MESSAGE_ELEMENT_RESULT_CODE,
2593 (pmnumber)error_code->code);
2594 plugin_message_set_value_string(m_result,
2595 PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE,
2596 error_code->msg ? error_code->msg : "");
2597 free(error_code->msg);
2600 plugin_message_set_value_number(m_result, PLUGIN_MESSAGE_ELEMENT_RESULT_CODE, (pmnumber)ret);
2603 LOGD("<<<<<< %s func end", func_name);
2605 plugin_message_set_value_number(m_result,
2606 PLUGIN_MESSAGE_ELEMENT_RESULT_CODE,
2607 (pmnumber)AUTH_ADAPTOR_ERROR_UNSUPPORTED);
2608 plugin_message_set_value_string(m_result,
2609 PLUGIN_MESSAGE_ELEMENT_RESULT_MESSAGE,
2610 "Unsupported operation");
2615 char *result_data = NULL;
2616 plugin_message_serialize(m_result, &result_data);
2617 plugin_message_destroy(m_result);
2618 plugin_message_destroy(m_order);
2620 *result = result_data;