3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
22 #include <sync_agent.h>
25 #include "common/dm_common.h"
26 #include "common/dm_common_def.h"
27 #include "common/util/util.h"
28 #include "framework/ui-event-handler/user-interaction/user_interaction.h"
29 #include "serviceadapter/sa_session.h"
30 #include "serviceadapter/sa_session_internal.h"
31 #include "serviceadapter/sa_elements.h"
32 #include "serviceadapter/sa_elements_internal.h"
33 #include "serviceadapter/sa_command.h"
34 #include "serviceadapter/sa_command_internal.h"
35 #include "serviceadapter/dm-phase-handler/dm_phase_handler.h"
36 #include "mo-handler/dm_mo_common.h"
37 #include "mo-handler/dm_mo_handler.h"
38 #include "dm-engine/dm_common_engine.h"
40 #ifndef OMADM_AGENT_LOG
42 #define LOG_TAG "OMA_DM_SA"
45 //#define DEFAULT_SERVER_MAXMSGSIZE 61440
46 #define DEFAULT_SERVER_MAXMSGSIZE 5120
48 static DM_ERROR _receive_alert_status(Session * session, Status * status);
49 static DM_ERROR _receive_results_status(Session * session, Status * status);
51 static DM_ERROR _receive_alert_cmd(Session * session, Command * command);
52 static DM_ERROR _receive_get_cmd(Session * session, Command * command);
53 static DM_ERROR _receive_results_cmd(Session * session, Command * command);
54 static DM_ERROR _receive_changes_cmd(Session * session, Command * command);
55 static DM_ERROR _receive_exec_cmd(Session * session, Command * command);
56 static DM_ERROR _receive_atomic_cmd(Session * session, Command * command);
57 static DM_ERROR _receice_sequence_cmd(Session * session, Command * command);
59 static DM_ERROR __receive_get_property_cmd(char *server_id, char *full_path, char *real_full_path, propoerty_type pr_type, Item ** pItem, int *resultStatus);
60 static DM_ERROR __rollback_atomic(Session * session, Command * pCommand);
61 static DM_ERROR __userInteration_operation(Command * command, GList ** response, int *result_status);
63 static void _free_pending_statuses(GList * pendingStatus);
64 static void __free_pending_status(PendingStatus * pendingStatus);
66 static void _set_hmac_value(char **key, char *value, int size);
68 static DM_ERROR _receive_get_cmd(Session * session, Command * command)
74 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
75 retvm_if((command) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "command is NULL!!");
76 retvm_if((command->private.access.items) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "command->private.access.items is NULL!!");
78 int resultStatus = DM_OK;
81 Command *pCommand = NULL;
83 char *full_path = NULL;
86 ret = create_results_cmd(session, &pCommand);
90 set_resultscommand_msgref(pCommand, command->msgID);
91 set_resultscommand_cmdref(pCommand, command->cmdID);
96 char *contenttype = NULL;
98 unsigned int size = 0;
102 sync_agent_dm_mo_error_e err_code = SYNC_AGENT_DM_MO_SUCCESS;
103 /* URL length over size : 414 error */
104 for (iter = command->private.access.items; iter != NULL; iter = g_list_next(iter)) {
108 _DEBUG_TRACE("item null");
109 resultStatus = DM_ERR_NOT_FOUND;
113 char *target = get_location_locuri(item->target);
114 if (target != NULL) {
115 full_path = g_strdup(target);
118 GList *pItems = NULL;
119 GetType gettype = GET_TYPE_NOMAL;
120 char *real_full_path = NULL;
122 ret = get_type_mo_tree(full_path, &gettype, &real_full_path);
129 _DEBUG_TRACE(" get_type_nomal \n");
131 err_code = sync_agent_is_exist_mo(full_path, &is_mo);
132 _DEBUG_TRACE("is existed mo : %d", err_code);
133 _DEBUG_TRACE("is mo : %d", is_mo);
135 _DEBUG_TRACE("NOT EXIST MO");
136 resultStatus = DM_ERR_NOT_FOUND;
139 _DEBUG_TRACE("EXIST MO");
142 err_code = sync_agent_check_acl_value(full_path, SYNC_AGENT_DM_MO_ACCESSTYPE_GET, session->pServer_id, &error_acl);
143 _DEBUG_TRACE("sync agent acl result : %d", err_code);
144 if (err_code != SYNC_AGENT_DM_MO_SUCCESS) {
145 resultStatus = DM_ERR_NOT_FOUND;
148 _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
149 if (error_acl == 0) {
150 _DEBUG_TRACE("ACL PERMISSION ERROR");
151 resultStatus = DM_ERR_PERMISSION_DENIED;
153 } else if (error_acl == 1) {
154 _DEBUG_TRACE("accesstype permission ERROR");
155 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
158 _DEBUG_TRACE("ACL PERMISSION");
159 ret = get_mo_node_format_contenttype(full_path, &format, &contenttype);
163 ret = get_mo_data(full_path, &data);
169 if (session->targetMaxMsgSize - DEFAULT_MORE_REST_SIZE < size) {
170 _DEBUG_TRACE("set more data ");
175 ret = construct_Item(full_path, format, contenttype, data, size, moreData, &pItem);
179 pCommand->private.results.items = g_list_append(pCommand->private.results.items, pItem);
183 case GET_TYPE_STRUCT:
185 _DEBUG_TRACE(" get_type_struct \n");
186 err_code = sync_agent_is_exist_mo(real_full_path, &is_mo);
187 _DEBUG_TRACE("is existed mo : %d", err_code);
188 _DEBUG_TRACE("is mo : %d", is_mo);
190 str_free(&real_full_path);
191 _DEBUG_TRACE("NOT EXIST MO");
192 resultStatus = DM_ERR_NOT_FOUND;
195 _DEBUG_TRACE("EXIST MO");
198 err_code = sync_agent_check_acl_value(real_full_path, SYNC_AGENT_DM_MO_ACCESSTYPE_GET, session->pServer_id, &error_acl);
199 _DEBUG_TRACE("sync agent acl result : %d", err_code);
200 if (err_code != SYNC_AGENT_DM_MO_SUCCESS) {
201 str_free(&real_full_path);
202 _DEBUG_TRACE("acl get fail");
203 resultStatus = DM_ERR_NOT_FOUND;
206 _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
207 if (error_acl == 0) {
208 _DEBUG_TRACE("ACL PERMISSION ERROR");
209 resultStatus = DM_ERR_PERMISSION_DENIED;
210 //free_command(pCommand);
212 } else if (error_acl == 1) {
213 _DEBUG_TRACE("accesstype PERMISSION ERROR");
214 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
217 _DEBUG_TRACE("ACL PERMISSION");
218 ret = get_struct(real_full_path, &pItems);
220 str_free(&real_full_path);
224 str_free(&real_full_path);
225 pCommand->private.results.items = pItems;
229 case GET_TYPE_STRUCTDATA:
231 err_code = sync_agent_is_exist_mo(real_full_path, &is_mo);
232 _DEBUG_TRACE("is existed mo : %d", err_code);
233 _DEBUG_TRACE("is mo : %d", is_mo);
235 str_free(&real_full_path);
236 _DEBUG_TRACE("NOT EXIST MO");
237 resultStatus = DM_ERR_NOT_FOUND;
240 _DEBUG_TRACE("EXIST MO");
242 _DEBUG_TRACE(" get_type_structdata \n");
244 err_code = sync_agent_check_acl_value(real_full_path, SYNC_AGENT_DM_MO_ACCESSTYPE_GET, session->pServer_id, &error_acl);
245 _DEBUG_TRACE("sync agent acl result : %d", err_code);
246 if (err_code != SYNC_AGENT_DM_MO_SUCCESS) {
247 str_free(&real_full_path);
248 _DEBUG_TRACE("acl get fail");
249 resultStatus = DM_ERR_NOT_FOUND;
252 _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
253 if (error_acl == 0) {
254 _DEBUG_TRACE("ACL PERMISSION ERROR");
255 resultStatus = DM_ERR_PERMISSION_DENIED;
256 //free_command(pCommand);
258 } else if (error_acl == 1) {
259 _DEBUG_TRACE("accesstype PERMISSION ERROR");
260 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
263 _DEBUG_TRACE("ACL PERMISSION");
264 ret = get_structdata(real_full_path, &pItems);
266 str_free(&real_full_path);
270 str_free(&real_full_path);
271 pCommand->private.results.items = pItems;
277 _DEBUG_TRACE(" get_type_tnds \n");
280 err_code = sync_agent_is_exist_mo(real_full_path, &is_mo);
281 _DEBUG_TRACE("is existed mo : %d", err_code);
283 str_free(&real_full_path);
284 _DEBUG_TRACE("NOT EXIST MO");
285 resultStatus = DM_ERR_NOT_FOUND;
288 _DEBUG_TRACE("EXIST MO");
291 err_code = sync_agent_check_acl_value(real_full_path, SYNC_AGENT_DM_MO_ACCESSTYPE_GET, session->pServer_id, &error_acl);
292 _DEBUG_TRACE("sync agent acl result : %d", err_code);
293 if (err_code != SYNC_AGENT_DM_MO_SUCCESS) {
294 str_free(&real_full_path);
295 _DEBUG_TRACE("acl get fail");
296 resultStatus = DM_ERR_NOT_FOUND;
299 _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
300 if (error_acl == 0) {
301 _DEBUG_TRACE("ACL PERMISSION ERROR");
302 resultStatus = DM_ERR_PERMISSION_DENIED;
303 //free_command(pCommand);
305 } else if (error_acl == 1) {
306 _DEBUG_TRACE("accesstype PERMISSION ERROR");
307 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
310 _DEBUG_TRACE("ACL PERMISSION");
311 const char *format = ELEMENT_XML;
312 const char *contenttype = ELEMENT_DMTNDS_XML;
313 char *output_stream = NULL;
314 unsigned int size = 0;
318 ret = get_tnds(ELEMENT_DMTNDS_XML, full_path, &output_stream, &size);
322 _DEBUG_TRACE("max size : %d", session->targetMaxMsgSize);
323 _DEBUG_TRACE("data size : %d", size);
324 if (output_stream != NULL && session->targetMaxMsgSize - DEFAULT_MORE_REST_SIZE < size) {
325 _DEBUG_TRACE("set more data ");
329 ret = construct_Item(real_full_path, format, contenttype, output_stream, size, moreData, &pItem);
331 str_free(&real_full_path);
335 str_free(&real_full_path);
336 _DEBUG_TRACE("add cmd item");
337 pCommand->private.results.items = g_list_append(pCommand->private.results.items, pItem);
341 case GET_TYPE_ACL_PROP:
343 ret = __receive_get_property_cmd(session->pServer_id, full_path, real_full_path, PROP_TYPE_ACL, &pItem, &resultStatus);
344 if (ret != DM_OK || resultStatus != DM_OK) {
345 str_free(&real_full_path);
348 str_free(&real_full_path);
349 _DEBUG_TRACE("add cmd item");
350 pCommand->private.results.items = g_list_append(pCommand->private.results.items, pItem);
353 case GET_TYPE_SIZE_PROP:
355 ret = __receive_get_property_cmd(session->pServer_id, full_path, real_full_path, PROP_TYPE_SIZE, &pItem, &resultStatus);
356 if (ret != DM_OK || resultStatus != DM_OK) {
357 str_free(&real_full_path);
360 str_free(&real_full_path);
361 _DEBUG_TRACE("add cmd item");
362 pCommand->private.results.items = g_list_append(pCommand->private.results.items, pItem);
365 case GET_TYPE_TYPE_PROP:
367 ret = __receive_get_property_cmd(session->pServer_id, full_path, real_full_path, PROP_TYPE_TYPE, &pItem, &resultStatus);
368 if (ret != DM_OK || resultStatus != DM_OK) {
369 str_free(&real_full_path);
372 str_free(&real_full_path);
373 _DEBUG_TRACE("add cmd item");
374 pCommand->private.results.items = g_list_append(pCommand->private.results.items, pItem);
377 case GET_TYPE_FORMAT_PROP:
379 ret = __receive_get_property_cmd(session->pServer_id, full_path, real_full_path, PROP_TYPE_FORMAT, &pItem, &resultStatus);
380 if (ret != DM_OK || resultStatus != DM_OK) {
381 str_free(&real_full_path);
384 str_free(&real_full_path);
385 _DEBUG_TRACE("add cmd item");
386 pCommand->private.results.items = g_list_append(pCommand->private.results.items, pItem);
389 case GET_TYPE_NAME_PROP:
391 ret = __receive_get_property_cmd(session->pServer_id, full_path, real_full_path, PROP_TYPE_NAME, &pItem, &resultStatus);
392 if (ret != DM_OK || resultStatus != DM_OK) {
393 str_free(&real_full_path);
396 str_free(&real_full_path);
397 _DEBUG_TRACE("add cmd item");
398 pCommand->private.results.items = g_list_append(pCommand->private.results.items, pItem);
403 _DEBUG_TRACE(" get_type_default \n");
407 session->resultsCommand = g_list_append(session->resultsCommand, pCommand);
408 _DEBUG_TRACE("add session result command result command list length : %d", g_list_length(session->resultsCommand));
410 ret = create_newstatus(session, resultStatus, command, item->source, item->target, COMMAND_TYPE_GET, &temp);
414 session->status = g_list_append(session->status, temp);
416 str_free(&full_path);
425 str_free(&full_path);
426 free_command(pCommand);
427 _DEBUG_TRACE(" error : %d\n", ret);
430 ret = create_newstatus(session, resultStatus, command, item->source, item->target, COMMAND_TYPE_GET, &temp);
434 ret = create_newstatus(session, resultStatus, command, command->source, command->target, COMMAND_TYPE_GET, &temp);
439 session->status = g_list_append(session->status, temp);
451 static DM_ERROR __receive_get_property_cmd(char *server_id, char *full_path, char *real_full_path, propoerty_type pr_type, Item ** pItem, int *resultStatus)
455 DM_ERROR ret = DM_OK;
457 retvm_if((server_id) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "server_id is NULL!!");
458 retvm_if((full_path) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "full_path is NULL!!");
459 retvm_if((real_full_path) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "real_full_path is NULL!!");
461 sync_agent_dm_mo_error_e err_code = SYNC_AGENT_DM_MO_SUCCESS;
465 char *contenttype = NULL;
467 unsigned int size = 0;
469 _DEBUG_TRACE(" get property ");
471 err_code = sync_agent_is_exist_mo(real_full_path, &is_mo);
472 _DEBUG_TRACE("is existed mo : %d", err_code);
474 _DEBUG_TRACE("NOT EXIST MO");
475 *resultStatus = DM_ERR_NOT_FOUND;
478 _DEBUG_TRACE("EXIST MO");
481 err_code = sync_agent_check_acl_value(real_full_path, SYNC_AGENT_DM_MO_ACCESSTYPE_GET, server_id, &error_acl);
482 _DEBUG_TRACE("sync agent acl result : %d", err_code);
483 if (err_code != SYNC_AGENT_DM_MO_SUCCESS) {
484 _DEBUG_TRACE("acl get fail");
485 *resultStatus = DM_ERR_NOT_FOUND;
488 _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
489 if (error_acl == 0) {
490 _DEBUG_TRACE("ACL PERMISSION ERROR");
491 *resultStatus = DM_ERR_PERMISSION_DENIED;
492 //free_command(pCommand);
494 } else if (error_acl == 1) {
495 _DEBUG_TRACE("accesstype PERMISSION ERROR");
496 *resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
499 _DEBUG_TRACE("ACL PERMISSION");
501 ret = get_mo_node_format_contenttype(real_full_path, &format, &contenttype);
505 char *property = NULL;
506 ret = get_mo_node_property(real_full_path, pr_type, &property);
508 *resultStatus = DM_ERR_PERMISSION_DENIED;
509 //str_free(&real_full_path);
512 _DEBUG_TRACE("property value : %s", property);
513 ret = construct_Item(full_path, format, contenttype, property, size, moreData, pItem);
518 _DEBUG_TRACE(" end %d", ret);
523 _DEBUG_TRACE(" end eror : %d", ret);
528 static DM_ERROR _receive_alert_cmd(Session * session, Command * command)
532 DM_ERROR ret = DM_OK;
534 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
535 retvm_if((command) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "command is NULL!!");
538 int result_status = DM_OK;
539 GList *response = NULL;
541 int alert_data = command->private.alert.type; //atoi(item->private.data);
543 if (DM_ALERT_DISPLAY <= alert_data && DM_ALERT_MULTIPLE_CHOICE >= alert_data) {
544 _DEBUG_TRACE("alert ui");
545 ret = __userInteration_operation(command, &response, &result_status);
546 _DEBUG_TRACE("status : %d", result_status);
548 } else if (DM_ALERT_NEXT_MESSAGE <= alert_data && DM_ALERT_MULTIPLE_CHOICE > alert_data) {
549 _DEBUG_TRACE("result status : %d", result_status);
555 result_status = DM_ERR_NOT_EXECUTED;
559 ret = create_newstatus(session, result_status, command, command->source, command->target, COMMAND_TYPE_ALERT, &temp);
564 switch (alert_data) {
565 case DM_ALERT_DISPLAY:
566 case DM_ALERT_CONFIRM_OR_REJECT:
568 case DM_ALERT_TEXT_INPUT:
569 case DM_ALERT_SINGLE_CHOICE:
570 case DM_ALERT_MULTIPLE_CHOICE:
575 if (response != NULL) {
576 _DEBUG_TRACE("--------------------response data existed--------------------");
577 for (iter = response; iter != NULL; iter = g_list_next(iter)) {
578 if (iter->data != NULL) {
580 value = strdup((char *)iter->data);
581 ret = create_item_data(value, strlen(value), &pItem);
583 if (temp->data != NULL) {
584 str_free(&(temp->data));
586 temp->data = g_strdup_printf("%d", DM_ERR_NOT_EXECUTED);
590 temp->items = g_list_append(temp->items, pItem);
594 _DEBUG_TRACE("--------------------response data not existed--------------------");
604 session->status = g_list_append(session->status, temp);
610 ret = create_newstatus(session, result_status, command, command->source, command->target, COMMAND_TYPE_ALERT, &temp);
615 session->status = g_list_append(session->status, temp);
616 _DEBUG_TRACE(" error : %d\n", ret);
622 _DEBUG_TRACE(" error : %d\n", ret);
627 static DM_ERROR _receive_results_cmd(Session * session, Command * command)
631 DM_ERROR ret = DM_OK;
638 static DM_ERROR _receive_changes_cmd(Session * session, Command * command)
642 DM_ERROR ret = DM_OK;
644 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
645 retvm_if((command) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "command is NULL!!");
647 ChangeType type = command->private.change.type;
648 _DEBUG_TRACE(" start command type : %d\n", type);
650 int resultStatus = DM_OK;
653 GList *item_iter = NULL;
655 char *targetUrl = NULL;
656 char *sourceUrl = NULL;
658 sync_agent_dm_mo_error_e err_code = SYNC_AGENT_DM_MO_SUCCESS;
660 char *real_full_path = NULL;
661 GetType gettype = GET_TYPE_NOMAL;
663 for (item_iter = command->private.change.items; item_iter != NULL; item_iter = g_list_next(item_iter)) {
664 item = item_iter->data;
666 targetUrl = get_location_locuri(item->target);
670 _DEBUG_TRACE(" add command \n");
671 err_code = sync_agent_is_exist_mo(targetUrl, &is_mo);
672 _DEBUG_TRACE("is exist result : %d", err_code);
673 _DEBUG_TRACE("is mo : %d", is_mo);
675 resultStatus = DM_ERR_ALEADY_EXISTS;
679 err_code = sync_agent_check_acl_value(targetUrl, SYNC_AGENT_DM_MO_ACCESSTYPE_ADD, session->pServer_id, &error_acl);
680 _DEBUG_TRACE("sync agent acl result : %d", err_code);
681 if (err_code != SYNC_AGENT_DM_MO_SUCCESS) {
682 if (err_code == SYNC_AGENT_DM_MO_NOT_EXIST_NODE) {
685 resultStatus = DM_ERR_PERMISSION_DENIED;
689 _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
690 if (error_acl == 0) {
691 _DEBUG_TRACE("ACL PERMISSION ERROR");
692 resultStatus = DM_ERR_PERMISSION_DENIED;
694 } else if (error_acl == 1) {
695 _DEBUG_TRACE("accesstype PERMISSION ERROR");
696 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
699 _DEBUG_TRACE("ACL PERMISSION");
700 ret = add_itemtomo(item, session->pServer_id);
708 _DEBUG_TRACE(" replace command \n");
710 ret = get_type_mo_tree(targetUrl, &gettype, &real_full_path);
717 _DEBUG_TRACE("nomal type : %d", gettype);
718 err_code = sync_agent_is_exist_mo(targetUrl, &is_mo);
719 _DEBUG_TRACE("is existed mo: %d, result: %d", is_mo, err_code);
721 _DEBUG_TRACE("NOT EXIST MO");
722 resultStatus = DM_ERR_NOT_FOUND;
726 err_code = sync_agent_check_acl_value(targetUrl, SYNC_AGENT_DM_MO_ACCESSTYPE_REPLACE, session->pServer_id, &error_acl);
727 _DEBUG_TRACE("sync agent acl result : %d", err_code);
728 if (err_code != SYNC_AGENT_DM_MO_SUCCESS) {
729 _DEBUG_TRACE("acl get fail");
730 resultStatus = DM_ERR_NOT_FOUND;
733 _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
734 if (error_acl == 0) {
735 _DEBUG_TRACE("ACL PERMISSION ERROR");
736 resultStatus = DM_ERR_PERMISSION_DENIED;
738 } else if (error_acl == 1) {
739 _DEBUG_TRACE("accesstype PERMISSION ERROR");
740 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
743 _DEBUG_TRACE("ACL PERMISSION");
744 ret = check_mo_scope(targetUrl);
746 //resultStatus = ret;
748 ret = replace_itemtomo(item); // Permanent node. But Accesstype is allow.
751 } else { // Permanent node. But device need execution about replace cmd.
752 char *server_id = NULL;
753 //targetUrl = get_location_locuri(item->target);
754 server_id = session->pServer_id;
755 _DEBUG_TRACE("server_id [%s]", server_id);
756 //ret = engine_replace_start(server_id, targetUrl, &(session->service_engine_id), &resultStatus, item);
757 //resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
760 ret = replace_itemtomo(item);
767 case GET_TYPE_ACL_PROP:
769 _DEBUG_TRACE(" property type : %d", gettype);
770 err_code = sync_agent_is_exist_mo(real_full_path, &is_mo);
771 _DEBUG_TRACE("is existed mo : %d", err_code);
773 _DEBUG_TRACE("NOT EXIST MO");
774 resultStatus = DM_ERR_NOT_FOUND;
777 /*do not check acl for replace property */
778 /*err_code = sync_agent_check_acl_value(real_full_path, SYNC_AGENT_DM_MO_ACCESSTYPE_REPLACE, session->pServer_id, &error_acl);
779 _DEBUG_TRACE("sync agent acl result : %d", err_code);
780 if(err_code != SYNC_AGENT_DM_MO_SUCCESS) {
781 resultStatus = DM_ERR_NOT_FOUND;
786 _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
788 _DEBUG_TRACE("ACL PERMISSION ERROR");
789 resultStatus = DM_ERR_PERMISSION_DENIED;
791 } else if(error_acl == 1) {
792 _DEBUG_TRACE("accesstype PERMISSION ERROR");
793 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
796 _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", 2);
797 ret = check_mo_scope(real_full_path);
802 //check_scope == 1 (dynamic)
803 ret = replace_itemtomo(item);
810 case GET_TYPE_NAME_PROP:
811 case GET_TYPE_TYPE_PROP:
812 case GET_TYPE_FORMAT_PROP:
813 case GET_TYPE_SIZE_PROP:
815 _DEBUG_TRACE(" property type : %d", gettype);
816 err_code = sync_agent_is_exist_mo(real_full_path, &is_mo);
817 _DEBUG_TRACE("is existed mo : %d", err_code);
819 _DEBUG_TRACE("NOT EXIST MO");
820 resultStatus = DM_ERR_NOT_FOUND;
823 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
827 _DEBUG_TRACE("replace type error");
832 _DEBUG_TRACE(" delete command \n");
833 err_code = sync_agent_is_exist_mo(targetUrl, &is_mo);
834 _DEBUG_TRACE("is existed mo : %d", err_code);
836 _DEBUG_TRACE("NOT EXIST MO");
837 resultStatus = DM_ERR_NOT_FOUND;
842 err_code = sync_agent_check_acl_value(targetUrl, SYNC_AGENT_DM_MO_ACCESSTYPE_DELETE, session->pServer_id, &error_acl);
843 _DEBUG_TRACE("sync agent acl result : %d", err_code);
844 if (err_code != SYNC_AGENT_DM_MO_SUCCESS) {
845 _DEBUG_TRACE("acl get fail");
846 resultStatus = DM_ERR_NOT_FOUND;
849 _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
850 if (error_acl == 0) {
851 _DEBUG_TRACE("ACL PERMISSION ERROR");
852 resultStatus = DM_ERR_PERMISSION_DENIED;
854 } else if (error_acl == 1) {
855 _DEBUG_TRACE("accesstype PERMISSION ERROR");
856 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
859 _DEBUG_TRACE("ACL PERMISSION");
860 ret = check_mo_scope(targetUrl);
864 ret = delete_itemtomo(item);
872 _DEBUG_TRACE(" copy command \n");
874 sourceUrl = get_location_locuri(item->source);
875 if (sourceUrl != NULL) {
876 err_code = sync_agent_is_exist_mo(sourceUrl, &is_mo);
877 _DEBUG_TRACE("is existed mo : %d", err_code);
879 _DEBUG_TRACE("NOT EXIT MO");
880 resultStatus = DM_ERR_NOT_FOUND;
884 err_code = sync_agent_check_acl_value(sourceUrl, SYNC_AGENT_DM_MO_ACCESSTYPE_COPY, session->pServer_id, &error_acl);
885 _DEBUG_TRACE("sync agent acl result : %d", err_code);
886 if (err_code != SYNC_AGENT_DM_MO_SUCCESS) {
887 _DEBUG_TRACE("acl get fail");
888 resultStatus = DM_ERR_NOT_FOUND;
891 _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
892 if (error_acl == 0) {
893 _DEBUG_TRACE("ACL PERMISSION ERROR");
894 resultStatus = DM_ERR_PERMISSION_DENIED;
896 } else if (error_acl == 1) {
897 _DEBUG_TRACE("accesstype PERMISSION ERROR");
898 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
901 err_code = sync_agent_check_acl_value(targetUrl, SYNC_AGENT_DM_MO_ACCESSTYPE_COPY, session->pServer_id, &error_acl);
902 _DEBUG_TRACE("sync agent acl result : %d", err_code);
903 if (err_code != SYNC_AGENT_DM_MO_SUCCESS) {
904 _DEBUG_TRACE("acl get fail");
905 resultStatus = DM_ERR_NOT_FOUND;
908 _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
909 if (error_acl == 0) {
910 _DEBUG_TRACE("ACL PERMISSION ERROR");
911 resultStatus = DM_ERR_PERMISSION_DENIED;
913 } else if (error_acl == 1) {
914 _DEBUG_TRACE("accesstype PERMISSION ERROR");
915 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
918 _DEBUG_TRACE("ACL PERMISSION");
919 ret = copy_itemtomo(item);
932 ret = create_newstatus(session, resultStatus, command, item->source, item->target, command->type, &temp);
935 session->status = g_list_append(session->status, temp);
938 str_free(&(real_full_path));
943 ret = create_newstatus(session, resultStatus, command, command->source, command->target, command->type, &temp);
946 session->status = g_list_append(session->status, temp);
948 str_free(&(real_full_path));
949 _DEBUG_TRACE(" error : %d\n", ret);
955 str_free(&(real_full_path));
956 _DEBUG_TRACE(" error : %d\n", ret);
961 static DM_ERROR _receive_exec_cmd(Session * session, Command * command)
965 DM_ERROR ret = DM_OK;
967 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
968 retvm_if((command) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "command is NULL!!");
970 int resultStatus = DM_ERR_ACCEPTED_FOR_PROCESSING;
972 char *targetUrl = NULL;
974 item = command->private.exec.item;
976 char *pCorrelator = NULL;
977 char *server_id = NULL;
979 sync_agent_dm_mo_error_e err_code = SYNC_AGENT_DM_MO_SUCCESS;
981 _DEBUG_TRACE("----------------------------------------------------------------------------exec cmd --------------------------------------------------------------------------- ");
982 targetUrl = get_location_locuri(command->private.exec.item->target);
985 err_code = sync_agent_is_exist_mo(targetUrl, &is_mo);
986 _DEBUG_TRACE("is existed mo : %d", err_code);
988 _DEBUG_TRACE("NOT EXIT MO");
989 resultStatus = DM_ERR_NOT_FOUND;
994 err_code = sync_agent_check_acl_value(targetUrl, SYNC_AGENT_DM_MO_ACCESSTYPE_EXEC, session->pServer_id, &error_acl);
995 _DEBUG_TRACE("sync agent acl result : %d", err_code);
996 if (err_code != SYNC_AGENT_DM_MO_SUCCESS) {
997 _DEBUG_TRACE("acl get fail");
998 resultStatus = DM_ERR_NOT_FOUND;
1001 _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
1002 if (error_acl == 0) {
1003 _DEBUG_TRACE("ACL PERMISSION ERROR");
1004 resultStatus = DM_ERR_PERMISSION_DENIED;
1006 } else if (error_acl == 1) {
1007 _DEBUG_TRACE("accesstype PERMISSION ERROR");
1008 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
1011 _DEBUG_TRACE("ACL PERMISSION");
1012 targetUrl = get_location_locuri(item->target);
1013 pCorrelator = command->private.exec.correlator;
1014 server_id = session->pServer_id;
1016 ret = engine_start(server_id, targetUrl, pCorrelator, &(session->service_engine_id), &resultStatus);
1017 _DEBUG_TRACE("--------------------------------------------------------engine_start off %d-----------------------------------------------------------------\n", ret);
1019 //ret = DM_ERR_NOT_FOUND;
1025 ret = create_newstatus(session, resultStatus, command, item->source, item->target, COMMAND_TYPE_EXEC, &temp);
1028 session->status = g_list_append(session->status, temp);
1034 ret = create_newstatus(session, resultStatus, command, item->source, item->target, COMMAND_TYPE_EXEC, &temp);
1037 session->status = g_list_append(session->status, temp);
1039 _DEBUG_TRACE(" error : %d\n", ret);
1043 _DEBUG_TRACE(" error : %d\n", ret);
1049 static DM_ERROR _receice_sequence_cmd(Session * session, Command * command)
1053 DM_ERROR ret = DM_OK;
1055 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
1056 retvm_if((command) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "command is NULL!!");
1058 int resultStatus = DM_OK;
1059 //sync_agent_dm_mo_error_e mo_ret = sync_agent_begin_transaction_mo();
1061 Status *temp = NULL;
1062 ret = create_newstatus(session, resultStatus, command, command->source, command->target, COMMAND_TYPE_SEQUENCE, &temp);
1066 session->status = g_list_append(session->status, temp);
1068 ret = receive_cmd(session, command->private.sequence_atomic.commands);
1070 resultStatus = DM_ERR_COMMAND_FAILED;
1072 //mo_ret = sync_agent_end_transaction_mo(SYNC_AGENT_DM_MO_TRANSACTION_COMMIT);
1078 _DEBUG_TRACE(" error : %d\n", ret);
1084 static DM_ERROR _receive_atomic_cmd(Session * session, Command * command)
1088 DM_ERROR ret = DM_OK;
1090 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
1091 retvm_if((command) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "command is NULL!!");
1093 int resultStatus = DM_ERR_ACCEPTED_FOR_PROCESSING;
1095 GList *atomic_iter = NULL;
1096 Command *pCommand = NULL;
1097 Status *temp = NULL;
1098 sync_agent_dm_mo_error_e mo_ret = sync_agent_begin_transaction_mo();
1100 for (atomic_iter = command->private.sequence_atomic.commands; atomic_iter != NULL;) {
1101 pCommand = atomic_iter->data;
1103 if (pCommand->type == COMMAND_TYPE_GET) {
1104 ret = create_newstatus(session, DM_ERR_COMMAND_FAILED, pCommand, pCommand->source, pCommand->target, COMMAND_TYPE_GET, &temp);
1108 session->status = g_list_append(session->status, temp);
1110 atomic_iter = g_list_next(atomic_iter);
1111 command->private.sequence_atomic.commands = g_list_remove(command->private.sequence_atomic.commands, pCommand);
1115 ret = receive_cmd(session, command->private.sequence_atomic.commands);
1117 resultStatus = DM_ERR_ATOMIC_FAILED;
1120 if (resultStatus == DM_ERR_ACCEPTED_FOR_PROCESSING) {
1121 mo_ret = sync_agent_end_transaction_mo(SYNC_AGENT_DM_MO_TRANSACTION_COMMIT);
1123 mo_ret = sync_agent_end_transaction_mo(SYNC_AGENT_DM_MO_TRANSACTION_ROLLBACK);
1124 __rollback_atomic(session, command);
1127 Status *temp_status = NULL;
1128 ret = create_newstatus(session, resultStatus, command, pCommand->source, pCommand->target, COMMAND_TYPE_ATOMIC, &temp_status);
1132 session->status = g_list_append(session->status, temp_status);
1138 _DEBUG_TRACE(" error : %d\n", ret);
1144 static DM_ERROR _receive_alert_status(Session * session, Status * status)
1148 DM_ERROR ret = DM_OK;
1150 retvm_if((status) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "status is NULL!!");
1152 unsigned int code = get_statuscode(status);
1153 if (code >= 400 && code != DM_ERR_REQUIRE_REFRESH) {
1155 //407 Authentication required
1156 //405 command not allowed
1157 //406 optional feature not supported
1158 //500 command failed
1159 //412 Incomplete command
1160 //415 unsupported media type or format
1161 //404 not found ->it have to be considered
1163 //TODO error handling it's error~!!!!!
1166 _DEBUG_TRACE(" end, Error Type : %d\n", ret);
1171 static DM_ERROR _receive_results_status(Session * session, Status * status)
1175 DM_ERROR ret = DM_OK;
1177 retvm_if((status) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "status is NULL!!");
1179 unsigned int code = get_statuscode(status);
1181 //TODO error handling it's error~!!!!!
1183 //407 authentication required
1185 //413 request entity too large
1186 //415 unspported media type or format
1187 //500 command failed
1191 _DEBUG_TRACE(" end, Error Type : %d\n", ret);
1196 static void _free_pending_statuses(GList * pendingStatus)
1200 retm_if((pendingStatus) == NULL, "pendingStatus is NULL!!");
1203 for (iter = pendingStatus; iter != NULL; iter = g_list_next(iter))
1204 __free_pending_status(iter->data);
1206 g_list_free(pendingStatus);
1211 static void __free_pending_status(PendingStatus * pendingStatus)
1215 retm_if((pendingStatus) == NULL, "pendingStatus is NULL!!");
1217 free(pendingStatus);
1218 pendingStatus = NULL;
1223 DM_ERROR create_syncml_session(ProtocolVersion protocolVersion, ProtocolType protocolType, char *server_id, char *client_id, char *pSession_id, char *pSourceUrl, char *pTargetUrl, Session ** pSession)
1227 DM_ERROR ret = DM_OK;
1229 retvm_if((pSourceUrl) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "pSourceUrl is NULL!!");
1230 retvm_if((pTargetUrl) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "pTargetUrl is NULL!!");
1232 *pSession = (Session *) calloc(1, sizeof(Session));
1233 if (*pSession == NULL) {
1234 ret = COMMON_ERR_ALLOC;
1238 (*pSession)->protocolType = protocolType;
1239 (*pSession)->protocolVersion = protocolVersion;
1240 /* (*pSession)->accountId = accountId;*/
1241 if (server_id != NULL)
1242 (*pSession)->pServer_id = strdup(server_id);
1243 (*pSession)->service_engine_id = NO_SERVICE_ENGINE;
1244 (*pSession)->sessionID = strdup(pSession_id);
1245 (*pSession)->jsessionid = NULL;
1247 ret = create_location(pSourceUrl, client_id, &((*pSession)->source));
1251 ret = create_location(pTargetUrl, NULL, &((*pSession)->target));
1255 (*pSession)->msgID = 0;
1256 (*pSession)->cmdID = 1;
1258 (*pSession)->res_chal = NULL;
1259 (*pSession)->req_chal = NULL;
1261 (*pSession)->isHMAC = 0; /*not using hmac */
1262 (*pSession)->reqhmacinfo = (Hmac *) calloc(1, sizeof(Hmac) + 1);
1263 if ((*pSession)->reqhmacinfo == NULL) {
1264 ret = COMMON_ERR_ALLOC;
1267 (*pSession)->resphmacinfo = (Hmac *) calloc(1, sizeof(Hmac) + 1);
1268 if ((*pSession)->resphmacinfo == NULL) {
1269 ret = COMMON_ERR_ALLOC;
1273 (*pSession)->sourceMaxMsgSize = DEFAULT_MAX_MSG_SIZE;
1274 (*pSession)->sourceMaxObjSize = DEFAULT_MAX_OBJ_SIZE;
1276 (*pSession)->targetMaxMsgSize = 0;
1277 (*pSession)->targetMaxObjSize = 0;
1284 if (*pSession != NULL) {
1285 _DEBUG_TRACE("free session");
1286 free_session(*pSession);
1289 _DEBUG_TRACE(" error : %d\n", ret);
1295 void free_session(Session * session)
1299 retm_if((session) == NULL, "session is NULL!!");
1301 if (session->pServer_id != NULL) {
1302 free(session->pServer_id);
1303 session->pServer_id = NULL;
1306 if (session->sessionID != NULL) {
1307 free(session->sessionID);
1308 session->sessionID = NULL;
1311 free_statuses(session->status);
1312 session->status = NULL;
1314 free_statuses(session->tempStatus);
1315 session->status = NULL;
1317 if (session->target != NULL) {
1318 free_location(session->target);
1319 session->target = NULL;
1322 if (session->source != NULL) {
1323 free_location(session->source);
1324 session->source = NULL;
1327 if (session->orgTarget != NULL) {
1328 free_location(session->orgTarget);
1329 session->orgTarget = NULL;
1332 if (session->cred != NULL) {
1333 free_cred(session->cred);
1334 session->cred = NULL;
1337 if (session->res_chal != NULL) {
1338 free_chal(session->res_chal);
1339 session->res_chal = NULL;
1342 if (session->req_chal != NULL) {
1343 free_chal(session->req_chal);
1344 session->req_chal = NULL;
1347 if (session->reqhmacinfo != NULL) {
1348 free_hmac(session->reqhmacinfo);
1349 session->reqhmacinfo = NULL;
1351 if (session->resphmacinfo != NULL) {
1352 free_hmac(session->resphmacinfo);
1353 session->resphmacinfo = NULL;
1356 _free_pending_statuses(session->pendingStatus);
1357 session->pendingStatus = NULL;
1359 if (session->pLargeObjCmd != NULL) {
1360 free_command(session->pLargeObjCmd);
1368 DM_ERROR receive_header(Session * session, SyncHdr * header)
1372 DM_ERROR ret = DM_OK;
1373 Location *pLocation = NULL;
1375 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
1376 retvm_if((header) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "header is NULL!!");
1378 if (session->sessionID != NULL && header->sessionID != NULL) {
1379 if (strcmp(session->sessionID, header->sessionID) != 0) {
1380 ret = COMMON_ERR_GENERIC;
1385 session->lastRecievedMsgID = header->messageID;
1387 if (0 < header->maxmsgsize)
1388 session->targetMaxMsgSize = header->maxmsgsize;
1390 session->targetMaxMsgSize = DEFAULT_SERVER_MAXMSGSIZE;
1392 if (0 < header->maxobjsize)
1393 session->targetMaxObjSize = header->maxobjsize;
1395 if (header->responseURI != NULL) {
1396 if (session->orgTarget == NULL) {
1397 session->orgTarget = session->target;
1398 session->target = NULL;
1401 if (session->target != NULL)
1402 free_location(session->target);
1404 ret = create_location(header->responseURI, get_location_locname(session->orgTarget), &pLocation);
1407 session->target = pLocation;
1409 if (session->target == NULL) {
1410 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
1414 _DEBUG_INFO(" target %s\n", session->target->locURI);
1417 Status *temp = NULL;
1418 DM_ERROR statusData = DM_ERR_UNKNOWN;
1420 if (header->cred != NULL) {
1421 _DEBUG_INFO("header cred ");
1422 //ret = check_server_cred(header->cred, session);
1423 statusData = DM_ERR_AUTH_ACCEPTED;
1428 _DEBUG_INFO("header not cred ");
1432 ret = create_status(statusData, session->cmdID++, session->lastRecievedMsgID, 0, header->source, header->target, NULL, COMMAND_TYPE_HEADER, &temp);
1436 session->status = g_list_append(session->status, temp);
1445 _DEBUG_INFO(" error : %d\n", ret);
1450 DM_ERROR receive_statuses(Session * session, GList * receiveStatus)
1454 DM_ERROR ret = DM_OK;
1456 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
1458 GList *statusItem = receiveStatus;
1459 Status *status = NULL;
1461 Status *req_chal_status = NULL;
1462 GList *header = NULL;
1464 while (statusItem) {
1465 status = statusItem->data;
1467 if (status->cmdRef == 0) {
1468 /*status of SyncHdr */
1469 assert(status->type == COMMAND_TYPE_HEADER);
1470 assert(status->data);
1471 DM_ERROR errorCode = atoi(status->data);
1473 if (errorCode == DM_ERR_AUTH_ACCEPTED || errorCode == DM_OK || errorCode == DM_ERR_UNAUTHORIZED || errorCode == DM_ERR_AUTHENTICATION_REQUIRED) {
1474 //212 || 200 || 401 || 407
1475 //when auth type is AUTH_TYPE_BASIC does not need to send cred in syncHdr in same session
1476 //when auth type is AUTH_TYPE_MD5 the next nonce in Chal MUST used for the digest when the next sync session is started.
1478 Chal *chal = status->chal;
1480 _DEBUG_INFO("exist chal");
1482 //res_chal in status have to be stored in config_tbl because it have to be used next sync session
1483 _DEBUG_INFO("format type :%d\n", chal->format);
1485 /*next session nonce data */
1486 if (chal->nonce_plain != NULL) {
1487 set_client_nextnonce(session->pServer_id, chal->nonce_plain);
1490 /*create next req_chal */
1491 if (chal->type == AUTH_TYPE_MD5) {
1492 _DEBUG_INFO("session have cred md5");
1493 set_client_authtype(session->pServer_id, chal->type);
1494 ret = create_chal(FORMAT_TYPE_BASE64, AUTH_TYPE_MD5, ELEMENT_DEFAULT_CLIENT_NONCE, &pChal);
1497 } else if (chal->type == AUTH_TYPE_HMAC) {
1498 session->isHMAC = 1;
1499 _DEBUG_INFO("session have not cred , using hmac");
1500 set_client_authtype(session->pServer_id, chal->type);
1501 ret = create_chal(FORMAT_TYPE_BASE64, AUTH_TYPE_HMAC, ELEMENT_DEFAULT_CLIENT_NONCE, &pChal);
1506 if (pChal != NULL) {
1508 for (header = session->status; header != NULL; header = g_list_next(header)) {
1509 req_chal_status = (Status *) (header->data);
1510 if (req_chal_status != NULL) {
1511 _DEBUG_TRACE("req_chal_status status type : %d", req_chal_status->type);
1512 if (req_chal_status->type == COMMAND_TYPE_HEADER) {
1513 ((Status *) (req_chal_status))->chal = dup_chal(pChal);
1514 _DEBUG_TRACE("add header chal");
1522 _DEBUG_INFO("chal null");
1525 if (errorCode == DM_ERR_AUTH_ACCEPTED || errorCode == DM_ERR_UNAUTHORIZED || errorCode == DM_ERR_AUTHENTICATION_REQUIRED) {
1526 if (session->cred != NULL) {
1527 free_cred(session->cred);
1528 session->cred = NULL;
1534 if (errorCode == DM_ERR_UNAUTHORIZED) {
1535 ret = DM_ERR_UNAUTHORIZED;
1537 } else if (errorCode == DM_ERR_AUTHENTICATION_REQUIRED) {
1538 ret = DM_ERR_AUTHENTICATION_REQUIRED;
1542 } else if (errorCode == DM_ERR_SERVER_FAILURE) {
1543 ret = DM_ERR_SERVER_FAILURE;
1547 //status except status of SyncHdr
1548 GList *pendingStatusItem = session->pendingStatus;
1549 PendingStatus *pending = NULL;
1550 while (pendingStatusItem) {
1551 pending = pendingStatusItem->data;
1553 if (pending->cmdID == status->cmdRef && pending->msgID == status->msgRef) {
1554 //if status's cmdID and msgID exists in pendigStatus
1556 if (status->type == COMMAND_TYPE_ALERT) {
1557 ret = _receive_alert_status(session, status);
1558 } else if (status->type == COMMAND_TYPE_RESULTS) {
1559 ret = _receive_results_status(session, status);
1562 COMMAND_TYPE_GET, COMMAND_TYPE_ADD, COMMAND_TYPE_REPLACE, COMMAND_TYPE_DELETE
1563 COMMAND_TYPE_COPY, COMMAND_TYPE_EXEC, COMMAND_TYPE_ATOMIC, COMMAND_TYPE_SEQUENCE
1569 session->pendingStatus = g_list_remove(session->pendingStatus, pending);
1572 //if status's cmdID and msgID doesn't exists in pendigStatus
1573 //is it error?? does this can happen??
1575 pendingStatusItem = g_list_next(pendingStatusItem);
1578 statusItem = g_list_next(statusItem);
1587 _DEBUG_INFO(" error, Error Type %d \n", ret);
1592 DM_ERROR receive_cmd(Session * session, GList * receiveCommand)
1596 DM_ERROR ret = DM_OK;
1598 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
1600 GList *commandItem = receiveCommand;
1601 Command *command = NULL;
1603 while (commandItem) {
1604 command = commandItem->data;
1606 switch (command->type) {
1607 case COMMAND_TYPE_ALERT:
1608 ret = _receive_alert_cmd(session, command);
1610 case COMMAND_TYPE_GET:
1611 ret = _receive_get_cmd(session, command);
1613 case COMMAND_TYPE_RESULTS:
1614 ret = _receive_results_cmd(session, command);
1616 case COMMAND_TYPE_ADD:
1617 case COMMAND_TYPE_REPLACE:
1618 case COMMAND_TYPE_DELETE:
1619 case COMMAND_TYPE_COPY:
1620 ret = _receive_changes_cmd(session, command);
1622 case COMMAND_TYPE_EXEC:
1623 ret = _receive_exec_cmd(session, command);
1625 case COMMAND_TYPE_SEQUENCE:
1626 ret = _receice_sequence_cmd(session, command);
1628 case COMMAND_TYPE_ATOMIC:
1629 ret = _receive_atomic_cmd(session, command);
1631 case COMMAND_TYPE_UNKNOWN:
1632 case COMMAND_TYPE_HEADER:
1633 _DEBUG_INFO("not support command");
1639 _DEBUG_INFO("receivce cmd ret : %d", ret);
1640 commandItem = g_list_next(commandItem);
1649 _DEBUG_INFO(" error : %d\n", ret);
1654 DM_ERROR create_prependingstatus(unsigned int msgID, unsigned int cmdID, PendingStatus ** pPendingStatus)
1658 DM_ERROR ret = DM_OK;
1660 *pPendingStatus = (PendingStatus *) calloc(1, sizeof(PendingStatus));
1662 if (*pPendingStatus == NULL) {
1663 ret = COMMON_ERR_ALLOC;
1667 (*pPendingStatus)->msgID = msgID;
1668 (*pPendingStatus)->cmdID = cmdID;
1676 _DEBUG_INFO(" error : %d\n", ret);
1681 DM_ERROR set_session_authcred(Session * session, Cred * cred)
1685 DM_ERROR ret = DM_OK;
1687 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
1688 // retvm_if((cred) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED , "cred is NULL!!");
1690 session->cred = cred;
1697 void reset_cmdid_session(Session * session)
1701 retm_if((session) == NULL, "session is NULL!!");
1708 AuthType __get_Session_AuthType_enum(char *data)
1712 AuthType type = AUTH_TYPE_UNKNOWN;
1714 retvm_if((data) == NULL, AUTH_TYPE_MD5, "data is NULL!!");
1716 if (strcmp(data, ELEMENT_BASIC) == 0) {
1717 type = AUTH_TYPE_BASIC;
1718 } else if (strcmp(data, ELEMENT_DIGEST) == 0) {
1719 type = AUTH_TYPE_MD5;
1720 } else if (strcmp(data, ELEMENT_HMAC) == 0) {
1721 type = AUTH_TYPE_HMAC;
1723 type = AUTH_TYPE_UNKNOWN;
1725 _DEBUG_VERBOSE(" end : [%d] \n", type);
1730 void __set_Session_AuthType_name(AuthType authType, char **authname)
1735 case AUTH_TYPE_BASIC:
1736 (*authname) = strdup(ELEMENT_BASIC);
1739 (*authname) = strdup(ELEMENT_DIGEST);
1741 case AUTH_TYPE_HMAC:
1742 (*authname) = strdup(ELEMENT_HMAC);
1745 (*authname) = strdup(ELEMENT_BASIC);
1755 DM_ERROR get_session_clientauthtype(Session * session, AuthType * authType){
1756 _DEBUG_INFO(" start \n");
1758 DM_ERROR ret = DM_OK;
1761 ret = get_dmacc_authType( session->pServer_id, authType);
1763 (*authType) = AUTH_TYPE_BASIC;
1768 (*authType) = __get_Session_AuthType(data);
1772 _DEBUG_INFO(" end \n");
1775 _DEBUG_INFO(" error end [%d]\n", ret);
1779 DM_ERROR get_session_serverauthtype(Session * session, AuthType* authType){
1780 _DEBUG_INFO(" start \n");
1782 DM_ERROR ret = DM_OK;
1785 ret = get_dmacc_authType( session->pServer_id, authType);
1787 (*authType) = AUTH_TYPE_BASIC;
1792 (*authType) = __get_Session_AuthType(data);
1798 _DEBUG_INFO(" end \n");
1802 _DEBUG_INFO(" error end [%d]\n", ret);
1807 static DM_ERROR __userInteration_operation(Command * command, GList ** response, int *result_status)
1811 DM_ERROR ret = DM_OK;
1813 retvm_if((command) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "command is NULL!!");
1815 CommandAlertPrivate pAlert = command->private.alert;
1816 AlertType type = pAlert.type;
1817 GList *alertItems = pAlert.items;
1820 int size = g_list_length(alertItems);
1822 //GList * responseItems = NULL;
1824 /*char* findValue = NULL; */
1825 /*int isCancel = 0; */
1826 uic_data *pUic_data = (uic_data *) calloc(1, sizeof(uic_data) + 1);
1827 if (pUic_data == NULL) {
1828 _DEBUG_VERBOSE("alloc fail");
1830 return COMMON_ERR_ALLOC;
1832 pUic_data->pUic_option = (uic_option *) calloc(1, sizeof(uic_option) + 1);
1833 if ((pUic_data->pUic_option) == NULL) {
1834 _DEBUG_VERBOSE("alloc fail");
1835 free_uic_value(&pUic_data);
1837 return COMMON_ERR_ALLOC;
1839 pUic_data->res_data = (uic_res_data *) calloc(1, sizeof(uic_res_data) + 1);
1840 //uic_option *uic_value = (uic_option *)calloc(1,sizeof(uic_option));
1841 if ((pUic_data->res_data) == NULL) {
1842 _DEBUG_VERBOSE("alloc fail");
1843 free_uic_value(&pUic_data);
1845 return COMMON_ERR_ALLOC;
1848 case DM_ALERT_DISPLAY:
1849 _DEBUG_VERBOSE("alert display ui");
1850 for (iter = alertItems, i = 0; iter != NULL; iter = g_list_next(iter), ++i) {
1852 if (i == 0 && size != 1) {
1853 /*user interaction option */
1854 _DEBUG_VERBOSE("uic opation : %s", pItem->private.data);
1855 ret = exec_userinteration_option(pItem->private.data, &(pUic_data->pUic_option));
1857 if (pItem->private.data != NULL) {
1858 /*user interaction execute */
1859 _DEBUG_VERBOSE("uic display : %s", pItem->private.data);
1860 pUic_data->display = strdup(pItem->private.data);
1861 ret = ui_display(pUic_data);
1862 *result_status = pUic_data->res_data->status;
1867 case DM_ALERT_CONFIRM_OR_REJECT:
1868 _DEBUG_VERBOSE("alert confirm ui");
1869 _DEBUG_VERBOSE("alert confirm alert item size : %d", size);
1870 for (iter = alertItems, i = 0; iter != NULL; iter = g_list_next(iter), ++i) {
1872 if (i == 0 && size != 1) {
1873 /*user interaction option */
1874 ret = exec_userinteration_option(pItem->private.data, &(pUic_data->pUic_option));
1876 /*user interaction execute */
1877 _DEBUG_VERBOSE("uic display : %s", pItem->private.data);
1878 pUic_data->display = strdup(pItem->private.data);
1879 ret = ui_confirm_or_reject(pUic_data);
1880 *result_status = pUic_data->res_data->status;
1884 case DM_ALERT_TEXT_INPUT:
1885 _DEBUG_VERBOSE("alert input ui");
1886 for (iter = alertItems, i = 0; iter != NULL; iter = g_list_next(iter), ++i) {
1888 if (i == 0 && size != 1) {
1889 /*user interaction option */
1890 /*IT ( A , N, D, T , P , I )
1893 ret = exec_userinteration_option(pItem->private.data, &(pUic_data->pUic_option));
1895 /*user interaction execute */
1896 _DEBUG_VERBOSE("uic display : %s", pItem->private.data);
1897 pUic_data->display = strdup(pItem->private.data);
1898 ret = ui_text_input(pUic_data, &result_data);
1899 *result_status = pUic_data->res_data->status;
1900 if (pUic_data->res_data->status == DM_OK) {
1901 (*response) = g_list_append((*response), result_data);
1903 _DEBUG_VERBOSE("input text null");
1909 case DM_ALERT_SINGLE_CHOICE:{
1910 _DEBUG_VERBOSE("alert single choice ui");
1911 for (iter = alertItems, i = 0; iter != NULL; iter = g_list_next(iter), ++i) {
1913 if (i == 0 && size != 1) {
1914 /*user interaction option */
1915 ret = exec_userinteration_option(pItem->private.data, &(pUic_data->pUic_option));
1917 /*user interaction execute */
1919 _DEBUG_VERBOSE("uic display : %s", pItem->private.data);
1920 pUic_data->display = strdup(pItem->private.data);
1922 GList *iter_end = NULL;
1923 iter_end = g_list_next(iter);
1925 data = strdup(pItem->private.data);
1926 pUic_data->multi_data = g_list_append(pUic_data->multi_data, data);
1928 if (iter_end == NULL) {
1929 ret = ui_single_choice(pUic_data, response);
1930 *result_status = pUic_data->res_data->status;
1938 case DM_ALERT_MULTIPLE_CHOICE:{
1939 _DEBUG_VERBOSE("alert single/multiple choice ui");
1940 for (iter = alertItems, i = 0; iter != NULL; iter = g_list_next(iter), ++i) {
1942 if (i == 0 && size != 1) {
1943 /*user interaction option */
1944 if (pItem->private.data != NULL)
1945 ret = exec_userinteration_option(pItem->private.data, &(pUic_data->pUic_option));
1947 /*user interaction execute */
1949 _DEBUG_VERBOSE("uic display : %s", pItem->private.data);
1950 pUic_data->display = strdup(pItem->private.data);
1952 GList *iter_end = NULL;
1953 iter_end = g_list_next(iter);
1955 data = strdup(pItem->private.data);
1956 pUic_data->multi_data = g_list_append(pUic_data->multi_data, data);
1958 if (iter_end == NULL) {
1959 ret = ui_multiple_choice(pUic_data, response);
1960 *result_status = pUic_data->res_data->status;
1969 case DM_ALERT_SERVER_INITIATED_MGMT:
1970 case DM_ALERT_CLIENT_INITIATED_MGMT:
1971 case DM_ALERT_NEXT_MESSAGE:
1972 case DM_ALERT_SESSION_ABORT:
1973 case DM_ALERT_CLIENT_EVENT:
1974 case DM_ALERT_NO_END_OF_DATA:
1975 case DM_ALERT_GENERIC_ALERT:
1976 _DEBUG_VERBOSE("not user interaction type");
1984 for(iter = responseItems; iter != NULL; iter = g_list_next(iter)){
1986 temp->items = g_list_append(temp->items, pItem);
1989 free_uic_value(&pUic_data);
1991 _DEBUG_VERBOSE(" end\n");
1997 DM_ERROR add_response_hmacinfo(Session ** session, char *temp_hmac)
2000 DM_ERROR ret = DM_OK;
2001 /*x-syncml-hmac: algorithm=MD5, username="156354238", mac=hvMeISwxSVUEofF2jHhQjA== */
2003 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
2004 retvm_if((temp_hmac) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "hmac is NULL!!");
2009 if ((*session)->resphmacinfo == NULL) {
2010 _DEBUG_INFO("hmac response calloc");
2011 (*session)->resphmacinfo = (Hmac *) calloc(1, sizeof(Hmac) + 1);
2012 if ((*session)->resphmacinfo == NULL) {
2013 return COMMON_ERR_ALLOC;
2017 _DEBUG_INFO("temp_hmac : %s", temp_hmac);
2018 start = strstr(temp_hmac, "=");
2019 if (start == NULL) {
2020 _DEBUG_INFO("auth type null");
2021 return COMMON_ERR_INTERNAL_NOT_DEFINED;
2023 _DEBUG_INFO("hmac : %s", start);
2025 end = strstr(temp_hmac, ",");
2027 _DEBUG_INFO("auth type null");
2028 return COMMON_ERR_INTERNAL_NOT_DEFINED;
2030 _DEBUG_INFO("hmac : %s", end);
2032 _set_hmac_value(&((*session)->resphmacinfo->authtype), start + 1, (end - start - 1));
2033 if ((*session)->resphmacinfo->authtype == NULL) {
2034 _DEBUG_INFO("alloc fail");
2036 return COMMON_ERR_ALLOC;
2038 _DEBUG_INFO("auty type : %s", (*session)->resphmacinfo->authtype);
2040 start = strstr(end, "=");
2041 if (start == NULL) {
2042 _DEBUG_INFO("username null");
2043 return COMMON_ERR_INTERNAL_NOT_DEFINED;
2045 _DEBUG_INFO("hmac : %s", start);
2047 end = strstr(start, ",");
2049 _DEBUG_INFO("username null");
2050 return COMMON_ERR_INTERNAL_NOT_DEFINED;
2052 _DEBUG_INFO("hmac : %s", end);
2054 _set_hmac_value(&((*session)->resphmacinfo->username), start + 2, (end - start - 3));
2055 if ((*session)->resphmacinfo->username == NULL) {
2056 _DEBUG_INFO("alloc fail");
2058 return COMMON_ERR_ALLOC;
2060 _DEBUG_INFO("username : %s", (*session)->resphmacinfo->username);
2062 start = strstr(end, "=");
2063 if (start == NULL) {
2064 _DEBUG_INFO("mac null");
2065 return COMMON_ERR_INTERNAL_NOT_DEFINED;
2067 _DEBUG_INFO("hmac : %s", start);
2068 (*session)->resphmacinfo->mac = strdup(start + 1);
2069 _DEBUG_INFO("mac : %s", (*session)->resphmacinfo->mac);
2075 static void _set_hmac_value(char **key, char *value, int size)
2079 retm_if((value) == NULL, "value is NULL!!");
2080 retm_if((size) == 0, "size is NULL!!");
2086 _DEBUG_INFO("key null");
2089 (*key) = (char *)calloc(1, size + 1);
2090 if ((*key) == NULL) {
2095 strncpy((*key), value, size);
2100 /*DM_ERROR process_commands(Command* pCommand, int * resultStatus) {
2102 _DEBUG_TRACE( " start\n");
2104 DM_ERROR ret = DM_OK;
2105 switch (pCommand->type) {
2106 case COMMAND_TYPE_UNKNOWN:
2108 case COMMAND_TYPE_ALERT:
2109 ret = __process_alert(pCommand, resultStatus);
2111 case COMMAND_TYPE_ADD:
2113 ret = __process_add(pCommand, resultStatus);
2114 case COMMAND_TYPE_REPLACE:
2115 ret = __process_replace(pCommand, resultStatus);
2117 case COMMAND_TYPE_DELETE:
2118 ret = __process_delete(pCommand, resultStatus);
2120 case COMMAND_TYPE_EXEC:
2121 ret = __process_exec(pCommand, resultStatus);
2123 case COMMAND_TYPE_SEQUENCE:
2124 ret = __process_sequence(pCommand, resultStatus);
2126 case COMMAND_TYPE_ATOMIC:
2127 ret = __process_atomic(pCommand, resultStatus);
2129 case COMMAND_TYPE_COPY:
2139 _DEBUG_TRACE( " end\n");
2142 _DEBUG_TRACE( " error end : ret %d\n", ret);
2147 static DM_ERROR __rollback_atomic(Session * session, Command * pCommand)
2151 DM_ERROR ret = DM_OK;
2152 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
2153 retvm_if((pCommand) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "pCommand is NULL!!");
2155 Command *current_Command = NULL;
2156 /*Status * temp_status = NULL; */
2157 GList *atomic_iter = NULL;
2158 GList *items = NULL;
2159 GList *temp_items = NULL;
2161 sync_agent_dm_mo_error_e mo_ret = sync_agent_begin_transaction_mo();
2162 _DEBUG_VERBOSE("transaction start : %d", mo_ret);
2164 for (atomic_iter = pCommand->private.sequence_atomic.commands; atomic_iter != NULL; atomic_iter = g_list_next(atomic_iter)) {
2165 current_Command = (Command *) atomic_iter->data;
2167 switch (current_Command->type) {
2168 case COMMAND_TYPE_ALERT:
2169 items = current_Command->private.alert.items;
2171 case COMMAND_TYPE_ADD:
2172 case COMMAND_TYPE_REPLACE:
2173 case COMMAND_TYPE_DELETE:
2174 case COMMAND_TYPE_COPY:
2175 items = current_Command->private.change.items;
2177 case COMMAND_TYPE_GET:
2178 items = current_Command->private.access.items;
2179 // delete result status
2181 case COMMAND_TYPE_UNKNOWN:
2182 case COMMAND_TYPE_HEADER:
2183 case COMMAND_TYPE_RESULTS:
2184 case COMMAND_TYPE_EXEC:
2185 case COMMAND_TYPE_SEQUENCE:
2186 case COMMAND_TYPE_ATOMIC:
2187 _DEBUG_VERBOSE("not support command in atomic");
2193 for (temp_items = items; temp_items != NULL; temp_items = g_list_next(temp_items)) {
2194 item = temp_items->data;
2196 ret = set_status_data(session, current_Command, item, DM_ERR_ATOMIC_ROLL_BACK_OK);
2200 _DEBUG_VERBOSE(" end\n");