Initialize Tizen 2.3
[framework/system/oma-dm-agent.git] / src / agent / serviceadapter / sa_session.c
1 /*
2  * oma-dm-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /*lib*/
19 #include <string.h>
20
21 /*sync-agent*/
22 #include <sync_agent.h>
23
24 /*dm-agent*/
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"
39
40 #ifndef OMADM_AGENT_LOG
41 #undef LOG_TAG
42 #define LOG_TAG "OMA_DM_SA"
43 #endif
44
45 //#define DEFAULT_SERVER_MAXMSGSIZE 61440
46 #define DEFAULT_SERVER_MAXMSGSIZE 5120
47
48 static DM_ERROR _receive_alert_status(Session * session, Status * status);
49 static DM_ERROR _receive_results_status(Session * session, Status * status);
50
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);
58
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);
62
63 static void _free_pending_statuses(GList * pendingStatus);
64 static void __free_pending_status(PendingStatus * pendingStatus);
65
66 static void _set_hmac_value(char **key, char *value, int size);
67
68 static DM_ERROR _receive_get_cmd(Session * session, Command * command)
69 {
70         _INNER_FUNC_ENTER;
71
72         DM_ERROR ret = DM_OK;
73
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!!");
77
78         int resultStatus = DM_OK;
79
80         Status *temp = NULL;
81         Command *pCommand = NULL;
82
83         char *full_path = NULL;
84         int error_acl = 0;
85
86         ret = create_results_cmd(session, &pCommand);
87         if (ret != DM_OK)
88                 goto error;
89
90         set_resultscommand_msgref(pCommand, command->msgID);
91         set_resultscommand_cmdref(pCommand, command->cmdID);
92
93         GList *iter = NULL;
94         Item *item = NULL;
95         char *format = NULL;
96         char *contenttype = NULL;
97         char *data = NULL;
98         unsigned int size = 0;
99         int moreData = 0;
100         Item *pItem;
101         int is_mo = 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)) {
105                 item = (iter->data);
106
107                 if (item == NULL) {
108                         _DEBUG_TRACE("item null");
109                         resultStatus = DM_ERR_NOT_FOUND;
110                         goto error;
111                 }
112
113                 char *target = get_location_locuri(item->target);
114                 if (target != NULL) {
115                         full_path = g_strdup(target);
116                 }
117
118                 GList *pItems = NULL;
119                 GetType gettype = GET_TYPE_NOMAL;
120                 char *real_full_path = NULL;
121
122                 ret = get_type_mo_tree(full_path, &gettype, &real_full_path);
123                 if (ret != DM_OK)
124                         goto error;
125
126                 switch (gettype) {
127                 case GET_TYPE_NOMAL:
128                         {
129                                 _DEBUG_TRACE(" get_type_nomal \n");
130
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);
134                                 if (is_mo == 0) {
135                                         _DEBUG_TRACE("NOT EXIST MO");
136                                         resultStatus = DM_ERR_NOT_FOUND;
137                                         goto error;
138                                 }
139                                 _DEBUG_TRACE("EXIST MO");
140
141                                 //error_acl = 1;
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;
146                                         goto error;
147                                 }
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;
152                                         goto error;
153                                 } else if (error_acl == 1) {
154                                         _DEBUG_TRACE("accesstype permission ERROR");
155                                         resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
156                                         goto error;
157                                 } else {
158                                         _DEBUG_TRACE("ACL PERMISSION");
159                                         ret = get_mo_node_format_contenttype(full_path, &format, &contenttype);
160                                         if (ret != DM_OK)
161                                                 goto error;
162
163                                         ret = get_mo_data(full_path, &data);
164                                         if (ret != DM_OK)
165                                                 goto error;
166
167                                         if (data != NULL) {
168                                                 size = strlen(data);
169                                                 if (session->targetMaxMsgSize - DEFAULT_MORE_REST_SIZE < size) {
170                                                         _DEBUG_TRACE("set more data ");
171                                                         moreData = 1;
172                                                 }
173                                         }
174
175                                         ret = construct_Item(full_path, format, contenttype, data, size, moreData, &pItem);
176                                         if (ret != DM_OK)
177                                                 goto error;
178
179                                         pCommand->private.results.items = g_list_append(pCommand->private.results.items, pItem);
180                                 }
181                         }
182                         break;
183                 case GET_TYPE_STRUCT:
184                         {
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);
189                                 if (is_mo == 0) {
190                                         str_free(&real_full_path);
191                                         _DEBUG_TRACE("NOT EXIST MO");
192                                         resultStatus = DM_ERR_NOT_FOUND;
193                                         goto error;
194                                 }
195                                 _DEBUG_TRACE("EXIST MO");
196
197                                 //error_acl = 1;
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;
204                                         goto error;
205                                 }
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);
211                                         goto error;
212                                 } else if (error_acl == 1) {
213                                         _DEBUG_TRACE("accesstype PERMISSION ERROR");
214                                         resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
215                                         goto error;
216                                 } else {
217                                         _DEBUG_TRACE("ACL PERMISSION");
218                                         ret = get_struct(real_full_path, &pItems);
219                                         if (ret != DM_OK) {
220                                                 str_free(&real_full_path);
221                                                 goto error;
222                                         }
223
224                                         str_free(&real_full_path);
225                                         pCommand->private.results.items = pItems;
226                                 }
227                         }
228                         break;
229                 case GET_TYPE_STRUCTDATA:
230                         {
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);
234                                 if (is_mo == 0) {
235                                         str_free(&real_full_path);
236                                         _DEBUG_TRACE("NOT EXIST MO");
237                                         resultStatus = DM_ERR_NOT_FOUND;
238                                         goto error;
239                                 }
240                                 _DEBUG_TRACE("EXIST MO");
241
242                                 _DEBUG_TRACE(" get_type_structdata \n");
243                                 //error_acl = 1;
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;
250                                         goto error;
251                                 }
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);
257                                         goto error;
258                                 } else if (error_acl == 1) {
259                                         _DEBUG_TRACE("accesstype PERMISSION ERROR");
260                                         resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
261                                         goto error;
262                                 } else {
263                                         _DEBUG_TRACE("ACL PERMISSION");
264                                         ret = get_structdata(real_full_path, &pItems);
265                                         if (ret != DM_OK) {
266                                                 str_free(&real_full_path);
267                                                 goto error;
268                                         }
269
270                                         str_free(&real_full_path);
271                                         pCommand->private.results.items = pItems;
272                                 }
273                         }
274                         break;
275                 case GET_TYPE_TNDS:
276                         {
277                                 _DEBUG_TRACE(" get_type_tnds \n");
278                                 /*xml, wbxml */
279                                 /*currnt XML */
280                                 err_code = sync_agent_is_exist_mo(real_full_path, &is_mo);
281                                 _DEBUG_TRACE("is existed mo : %d", err_code);
282                                 if (is_mo == 0) {
283                                         str_free(&real_full_path);
284                                         _DEBUG_TRACE("NOT EXIST MO");
285                                         resultStatus = DM_ERR_NOT_FOUND;
286                                         goto error;
287                                 }
288                                 _DEBUG_TRACE("EXIST MO");
289
290                                 //error_acl = 1;
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;
297                                         goto error;
298                                 }
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);
304                                         goto error;
305                                 } else if (error_acl == 1) {
306                                         _DEBUG_TRACE("accesstype PERMISSION ERROR");
307                                         resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
308                                         goto error;
309                                 } else {
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;
315                                         int moreData = 0;
316                                         Item *pItem;
317
318                                         ret = get_tnds(ELEMENT_DMTNDS_XML, full_path, &output_stream, &size);
319                                         if (ret != DM_OK)
320                                                 goto error;
321
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 ");
326                                                 moreData = 1;
327                                         }
328
329                                         ret = construct_Item(real_full_path, format, contenttype, output_stream, size, moreData, &pItem);
330                                         if (ret != DM_OK) {
331                                                 str_free(&real_full_path);
332                                                 goto error;
333                                         }
334
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);
338                                 }
339                         }
340                         break;
341                 case GET_TYPE_ACL_PROP:
342                         {
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);
346                                         goto error;
347                                 }
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);
351                         }
352                         break;
353                 case GET_TYPE_SIZE_PROP:
354                         {
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);
358                                         goto error;
359                                 }
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);
363                         }
364                         break;
365                 case GET_TYPE_TYPE_PROP:
366                         {
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);
370                                         goto error;
371                                 }
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);
375                         }
376                         break;
377                 case GET_TYPE_FORMAT_PROP:
378                         {
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);
382                                         goto error;
383                                 }
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);
387                         }
388                         break;
389                 case GET_TYPE_NAME_PROP:
390                         {
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);
394                                         goto error;
395                                 }
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);
399                         }
400                         break;
401
402                 default:
403                         _DEBUG_TRACE(" get_type_default \n");
404                         break;
405                 }
406
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));
409
410                 ret = create_newstatus(session, resultStatus, command, item->source, item->target, COMMAND_TYPE_GET, &temp);
411                 if (ret != DM_OK)
412                         goto error;
413
414                 session->status = g_list_append(session->status, temp);
415
416                 str_free(&full_path);
417
418         }
419
420         _EXTERN_FUNC_EXIT;
421         return ret;
422
423  error:
424
425         str_free(&full_path);
426         free_command(pCommand);
427         _DEBUG_TRACE(" error : %d\n", ret);
428
429         if (item != NULL) {
430                 ret = create_newstatus(session, resultStatus, command, item->source, item->target, COMMAND_TYPE_GET, &temp);
431                 if (ret != DM_OK)
432                         goto ex_error;
433         } else {
434                 ret = create_newstatus(session, resultStatus, command, command->source, command->target, COMMAND_TYPE_GET, &temp);
435                 if (ret != DM_OK)
436                         goto ex_error;
437         }
438
439         session->status = g_list_append(session->status, temp);
440         _EXTERN_FUNC_EXIT;
441         return ret;
442
443  ex_error:
444
445         free_status(temp);
446         _EXTERN_FUNC_EXIT;
447         return ret;
448
449 }
450
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)
452 {
453         _INNER_FUNC_ENTER;
454
455         DM_ERROR ret = DM_OK;
456
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!!");
460
461         sync_agent_dm_mo_error_e err_code = SYNC_AGENT_DM_MO_SUCCESS;
462         int is_mo = 0;
463         int error_acl = 0;
464         char *format = NULL;
465         char *contenttype = NULL;
466         int moreData = 0;
467         unsigned int size = 0;
468
469         _DEBUG_TRACE(" get property ");
470
471         err_code = sync_agent_is_exist_mo(real_full_path, &is_mo);
472         _DEBUG_TRACE("is existed mo : %d", err_code);
473         if (is_mo == 0) {
474                 _DEBUG_TRACE("NOT EXIST MO");
475                 *resultStatus = DM_ERR_NOT_FOUND;
476                 goto error;
477         }
478         _DEBUG_TRACE("EXIST MO");
479
480         //error_acl = 1;
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;
486                 goto error;
487         }
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);
493                 goto error;
494         } else if (error_acl == 1) {
495                 _DEBUG_TRACE("accesstype PERMISSION ERROR");
496                 *resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
497                 goto error;
498         } else {
499                 _DEBUG_TRACE("ACL PERMISSION");
500
501                 ret = get_mo_node_format_contenttype(real_full_path, &format, &contenttype);
502                 if (ret != DM_OK)
503                         goto error;
504
505                 char *property = NULL;
506                 ret = get_mo_node_property(real_full_path, pr_type, &property);
507                 if (ret != DM_OK) {
508                         *resultStatus = DM_ERR_PERMISSION_DENIED;
509                         //str_free(&real_full_path);
510                         goto error;
511                 }
512                 _DEBUG_TRACE("property value : %s", property);
513                 ret = construct_Item(full_path, format, contenttype, property, size, moreData, pItem);
514                 if (ret != DM_OK)
515                         goto error;
516         }
517
518         _DEBUG_TRACE(" end %d", ret);
519         _INNER_FUNC_EXIT;
520         return ret;
521  error:
522
523         _DEBUG_TRACE(" end eror : %d", ret);
524         _INNER_FUNC_EXIT;
525         return ret;
526 }
527
528 static DM_ERROR _receive_alert_cmd(Session * session, Command * command)
529 {
530         _INNER_FUNC_ENTER;
531
532         DM_ERROR ret = DM_OK;
533
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!!");
536
537         Status *temp = NULL;
538         int result_status = DM_OK;
539         GList *response = NULL;
540
541         int alert_data = command->private.alert.type;   //atoi(item->private.data);
542
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);
547
548         } else if (DM_ALERT_NEXT_MESSAGE <= alert_data && DM_ALERT_MULTIPLE_CHOICE > alert_data) {
549                 _DEBUG_TRACE("result status : %d", result_status);
550                 goto error;
551         }
552
553         if (ret != DM_OK) {
554                 ret = DM_OK;
555                 result_status = DM_ERR_NOT_EXECUTED;
556                 goto error;
557         }
558
559         ret = create_newstatus(session, result_status, command, command->source, command->target, COMMAND_TYPE_ALERT, &temp);
560         if (ret != DM_OK) {
561                 goto ex_error;
562         }
563
564         switch (alert_data) {
565         case DM_ALERT_DISPLAY:
566         case DM_ALERT_CONFIRM_OR_REJECT:
567                 break;
568         case DM_ALERT_TEXT_INPUT:
569         case DM_ALERT_SINGLE_CHOICE:
570         case DM_ALERT_MULTIPLE_CHOICE:
571                 {
572                         Item *pItem = NULL;
573                         GList *iter = NULL;
574
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) {
579                                                 char *value = NULL;
580                                                 value = strdup((char *)iter->data);
581                                                 ret = create_item_data(value, strlen(value), &pItem);
582                                                 if (ret != DM_OK) {
583                                                         if (temp->data != NULL) {
584                                                                 str_free(&(temp->data));
585                                                         }
586                                                         temp->data = g_strdup_printf("%d", DM_ERR_NOT_EXECUTED);
587                                                         goto return_part;
588                                                 }
589
590                                                 temp->items = g_list_append(temp->items, pItem);
591                                         }
592                                 }
593                         } else {
594                                 _DEBUG_TRACE("--------------------response data not existed--------------------");
595                                 //do nothing
596                         }
597                 }
598                 break;
599
600         }
601
602  return_part:
603
604         session->status = g_list_append(session->status, temp);
605         _INNER_FUNC_EXIT;
606         return ret;
607
608  error:
609
610         ret = create_newstatus(session, result_status, command, command->source, command->target, COMMAND_TYPE_ALERT, &temp);
611         if (ret != DM_OK) {
612                 goto ex_error;
613         }
614
615         session->status = g_list_append(session->status, temp);
616         _DEBUG_TRACE(" error : %d\n", ret);
617         _INNER_FUNC_EXIT;
618         return ret;
619
620  ex_error:
621         free_status(temp);
622         _DEBUG_TRACE(" error : %d\n", ret);
623         _INNER_FUNC_EXIT;
624         return ret;
625 }
626
627 static DM_ERROR _receive_results_cmd(Session * session, Command * command)
628 {
629         _INNER_FUNC_ENTER;
630
631         DM_ERROR ret = DM_OK;
632
633         _INNER_FUNC_EXIT;
634         return ret;
635
636 }
637
638 static DM_ERROR _receive_changes_cmd(Session * session, Command * command)
639 {
640         _INNER_FUNC_ENTER;
641
642         DM_ERROR ret = DM_OK;
643
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!!");
646
647         ChangeType type = command->private.change.type;
648         _DEBUG_TRACE(" start command type :  %d\n", type);
649
650         int resultStatus = DM_OK;
651         int error_acl = 0;
652         int is_mo = 0;
653         GList *item_iter = NULL;
654         Item *item = NULL;
655         char *targetUrl = NULL;
656         char *sourceUrl = NULL;
657         Status *temp = NULL;
658         sync_agent_dm_mo_error_e err_code = SYNC_AGENT_DM_MO_SUCCESS;
659
660         char *real_full_path = NULL;
661         GetType gettype = GET_TYPE_NOMAL;
662
663         for (item_iter = command->private.change.items; item_iter != NULL; item_iter = g_list_next(item_iter)) {
664                 item = item_iter->data;
665                 targetUrl = NULL;
666                 targetUrl = get_location_locuri(item->target);
667
668                 switch (type) {
669                 case CHANGE_ADD:
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);
674                         if (is_mo == 1) {
675                                 resultStatus = DM_ERR_ALEADY_EXISTS;
676                                 goto error;
677                         } else {
678                                 //error_acl = 1;
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) {
683                                                 error_acl = 2;
684                                         } else {
685                                                 resultStatus = DM_ERR_PERMISSION_DENIED;
686                                                 goto error;
687                                         }
688                                 }
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;
693                                         goto error;
694                                 } else if (error_acl == 1) {
695                                         _DEBUG_TRACE("accesstype PERMISSION ERROR");
696                                         resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
697                                         goto error;
698                                 } else {
699                                         _DEBUG_TRACE("ACL PERMISSION");
700                                         ret = add_itemtomo(item, session->pServer_id);
701                                         if (ret != DM_OK)
702                                                 goto error;
703                                 }
704                         }
705
706                         break;
707                 case CHANGE_REPLACE:
708                         _DEBUG_TRACE(" replace command \n");
709
710                         ret = get_type_mo_tree(targetUrl, &gettype, &real_full_path);
711                         if (ret != DM_OK)
712                                 goto error;
713
714                         switch (gettype) {
715                         case GET_TYPE_NOMAL:
716                                 {
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);
720                                         if (is_mo == 0) {
721                                                 _DEBUG_TRACE("NOT EXIST MO");
722                                                 resultStatus = DM_ERR_NOT_FOUND;
723                                                 goto error;
724                                         }
725                                         //error_acl = 1;
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;
731                                                 goto error;
732                                         }
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;
737                                                 goto error;
738                                         } else if (error_acl == 1) {
739                                                 _DEBUG_TRACE("accesstype PERMISSION ERROR");
740                                                 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
741                                                 goto error;
742                                         } else {
743                                                 _DEBUG_TRACE("ACL PERMISSION");
744                                                 ret = check_mo_scope(targetUrl);
745                                                 if (ret != DM_OK) {
746                                                         //resultStatus = ret;
747                                                         //goto error;
748                                                         ret = replace_itemtomo(item); // Permanent node. But Accesstype is allow.
749                                                         if (ret != DM_OK) {
750                                                                 goto error;
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;
758                                                         }
759                                                 } else {
760                                                         ret = replace_itemtomo(item);
761                                                         if (ret != DM_OK)
762                                                                 goto error;
763                                                 }
764                                         }
765                                 }
766                                 break;
767                         case GET_TYPE_ACL_PROP:
768                                 {
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);
772                                         if (is_mo == 0) {
773                                                 _DEBUG_TRACE("NOT EXIST MO");
774                                                 resultStatus = DM_ERR_NOT_FOUND;
775                                                 goto error;
776                                         }
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;
782                                            goto error;
783                                            }
784                                          */
785                                         /*       error_acl = 2;
786                                            _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
787                                            if(error_acl == 0) {
788                                            _DEBUG_TRACE("ACL PERMISSION ERROR");
789                                            resultStatus = DM_ERR_PERMISSION_DENIED;
790                                            goto error;
791                                            } else if(error_acl == 1) {
792                                            _DEBUG_TRACE("accesstype PERMISSION ERROR");
793                                            resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
794                                            goto error;
795                                            } else { */
796                                         _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", 2);
797                                         ret = check_mo_scope(real_full_path);
798                                         if (ret != DM_OK) {
799                                                 resultStatus = ret;
800                                                 goto error;
801                                         } else {
802                                                 //check_scope == 1 (dynamic)
803                                                 ret = replace_itemtomo(item);
804                                                 if (ret != DM_OK)
805                                                         goto error;
806                                         }
807                                         //}
808                                 }
809                                 break;
810                         case GET_TYPE_NAME_PROP:
811                         case GET_TYPE_TYPE_PROP:
812                         case GET_TYPE_FORMAT_PROP:
813                         case GET_TYPE_SIZE_PROP:
814                                 {
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);
818                                         if (is_mo == 0) {
819                                                 _DEBUG_TRACE("NOT EXIST MO");
820                                                 resultStatus = DM_ERR_NOT_FOUND;
821                                                 goto error;
822                                         }
823                                         resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
824                                 }
825                                 break;
826                         default:
827                                 _DEBUG_TRACE("replace type error");
828                                 break;
829                         }
830                         break;
831                 case CHANGE_DELETE:
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);
835                         if (is_mo == 0) {
836                                 _DEBUG_TRACE("NOT EXIST MO");
837                                 resultStatus = DM_ERR_NOT_FOUND;
838                                 goto error;
839                         } else {
840
841                                 //error_acl = 1;
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;
847                                         goto error;
848                                 }
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;
853                                         goto error;
854                                 } else if (error_acl == 1) {
855                                         _DEBUG_TRACE("accesstype PERMISSION ERROR");
856                                         resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
857                                         goto error;
858                                 } else {
859                                         _DEBUG_TRACE("ACL PERMISSION");
860                                         ret = check_mo_scope(targetUrl);
861                                         if (ret != DM_OK) {
862                                                 resultStatus = ret;
863                                         } else {
864                                                 ret = delete_itemtomo(item);
865                                                 if (ret != DM_OK)
866                                                         goto error;
867                                         }
868                                 }
869                         }
870                         break;
871                 case CHANGE_COPY:
872                         _DEBUG_TRACE(" copy command \n");
873                         sourceUrl = NULL;
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);
878                                 if (is_mo == 0) {
879                                         _DEBUG_TRACE("NOT EXIT MO");
880                                         resultStatus = DM_ERR_NOT_FOUND;
881                                         goto error;
882                                 } else {
883                                         //error_acl = 1;
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;
889                                                 goto error;
890                                         }
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;
895                                                 goto error;
896                                         } else if (error_acl == 1) {
897                                                 _DEBUG_TRACE("accesstype PERMISSION ERROR");
898                                                 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
899                                                 goto error;
900                                         }
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;
906                                                 goto error;
907                                         }
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;
912                                                 goto error;
913                                         } else if (error_acl == 1) {
914                                                 _DEBUG_TRACE("accesstype PERMISSION ERROR");
915                                                 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
916                                                 goto error;
917                                         } else {
918                                                 _DEBUG_TRACE("ACL PERMISSION");
919                                                 ret = copy_itemtomo(item);
920                                                 if (ret != DM_OK)
921                                                         goto error;
922                                         }
923                                 }
924                         } else {
925                                 //do nothing
926                         }
927                         break;
928                 default:
929                         break;
930                 }
931
932                 ret = create_newstatus(session, resultStatus, command, item->source, item->target, command->type, &temp);
933                 if (ret != DM_OK)
934                         goto error;
935                 session->status = g_list_append(session->status, temp);
936         }
937
938         str_free(&(real_full_path));
939         _INNER_FUNC_ENTER;
940         return ret;
941
942  error:
943         ret = create_newstatus(session, resultStatus, command, command->source, command->target, command->type, &temp);
944         if (ret != DM_OK)
945                 goto ex_error;
946         session->status = g_list_append(session->status, temp);
947
948         str_free(&(real_full_path));
949         _DEBUG_TRACE(" error : %d\n", ret);
950         _INNER_FUNC_ENTER;
951         return ret;
952  ex_error:
953
954         free_status(temp);
955         str_free(&(real_full_path));
956         _DEBUG_TRACE(" error : %d\n", ret);
957         _INNER_FUNC_ENTER;
958         return ret;
959 }
960
961 static DM_ERROR _receive_exec_cmd(Session * session, Command * command)
962 {
963         _INNER_FUNC_ENTER;
964
965         DM_ERROR ret = DM_OK;
966
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!!");
969
970         int resultStatus = DM_ERR_ACCEPTED_FOR_PROCESSING;
971         int error_acl = 0;
972         char *targetUrl = NULL;
973         Item *item = NULL;
974         item = command->private.exec.item;
975         Status *temp = NULL;
976         char *pCorrelator = NULL;
977         char *server_id = NULL;
978         int is_mo = 0;
979         sync_agent_dm_mo_error_e err_code = SYNC_AGENT_DM_MO_SUCCESS;
980
981         _DEBUG_TRACE("----------------------------------------------------------------------------exec cmd --------------------------------------------------------------------------- ");
982         targetUrl = get_location_locuri(command->private.exec.item->target);
983         /*todo: server_id */
984         //error_acl = 1;
985         err_code = sync_agent_is_exist_mo(targetUrl, &is_mo);
986         _DEBUG_TRACE("is existed mo : %d", err_code);
987         if (is_mo == 0) {
988                 _DEBUG_TRACE("NOT EXIT MO");
989                 resultStatus = DM_ERR_NOT_FOUND;
990                 goto error;
991         } else {
992
993                 //error_acl = 1;
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;
999                         goto error;
1000                 }
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;
1005                         goto error;
1006                 } else if (error_acl == 1) {
1007                         _DEBUG_TRACE("accesstype PERMISSION ERROR");
1008                         resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
1009                         goto error;
1010                 } else {
1011                         _DEBUG_TRACE("ACL PERMISSION");
1012                         targetUrl = get_location_locuri(item->target);
1013                         pCorrelator = command->private.exec.correlator;
1014                         server_id = session->pServer_id;
1015
1016                         ret = engine_start(server_id, targetUrl, pCorrelator, &(session->service_engine_id), &resultStatus);
1017                         _DEBUG_TRACE("--------------------------------------------------------engine_start off %d-----------------------------------------------------------------\n", ret);
1018                         if (ret != DM_OK) {
1019                                 //ret = DM_ERR_NOT_FOUND;
1020                                 goto error;
1021                         }
1022                 }
1023         }
1024
1025         ret = create_newstatus(session, resultStatus, command, item->source, item->target, COMMAND_TYPE_EXEC, &temp);
1026         if (ret != DM_OK)
1027                 goto ex_error;
1028         session->status = g_list_append(session->status, temp);
1029
1030         _INNER_FUNC_ENTER;
1031         return ret;
1032
1033  error:
1034         ret = create_newstatus(session, resultStatus, command, item->source, item->target, COMMAND_TYPE_EXEC, &temp);
1035         if (ret != DM_OK)
1036                 goto ex_error;
1037         session->status = g_list_append(session->status, temp);
1038
1039         _DEBUG_TRACE(" error : %d\n", ret);
1040         _INNER_FUNC_ENTER;
1041         return ret;
1042  ex_error:
1043         _DEBUG_TRACE(" error : %d\n", ret);
1044         _INNER_FUNC_ENTER;
1045         return ret;
1046
1047 }
1048
1049 static DM_ERROR _receice_sequence_cmd(Session * session, Command * command)
1050 {
1051         _INNER_FUNC_ENTER;
1052
1053         DM_ERROR ret = DM_OK;
1054
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!!");
1057
1058         int resultStatus = DM_OK;
1059         //sync_agent_dm_mo_error_e mo_ret  = sync_agent_begin_transaction_mo();
1060
1061         Status *temp = NULL;
1062         ret = create_newstatus(session, resultStatus, command, command->source, command->target, COMMAND_TYPE_SEQUENCE, &temp);
1063         if (ret != DM_OK)
1064                 goto error;
1065
1066         session->status = g_list_append(session->status, temp);
1067
1068         ret = receive_cmd(session, command->private.sequence_atomic.commands);
1069         if (ret != DM_OK) {
1070                 resultStatus = DM_ERR_COMMAND_FAILED;
1071         }
1072         //mo_ret = sync_agent_end_transaction_mo(SYNC_AGENT_DM_MO_TRANSACTION_COMMIT);
1073
1074         _INNER_FUNC_ENTER;
1075         return ret;
1076
1077  error:
1078         _DEBUG_TRACE(" error : %d\n", ret);
1079         _INNER_FUNC_ENTER;
1080         return ret;
1081
1082 }
1083
1084 static DM_ERROR _receive_atomic_cmd(Session * session, Command * command)
1085 {
1086         _INNER_FUNC_ENTER;
1087
1088         DM_ERROR ret = DM_OK;
1089
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!!");
1092
1093         int resultStatus = DM_ERR_ACCEPTED_FOR_PROCESSING;
1094
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();
1099
1100         for (atomic_iter = command->private.sequence_atomic.commands; atomic_iter != NULL;) {
1101                 pCommand = atomic_iter->data;
1102
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);
1105                         if (ret != DM_OK)
1106                                 goto error;
1107
1108                         session->status = g_list_append(session->status, temp);
1109
1110                         atomic_iter = g_list_next(atomic_iter);
1111                         command->private.sequence_atomic.commands = g_list_remove(command->private.sequence_atomic.commands, pCommand);
1112                 }
1113         }
1114
1115         ret = receive_cmd(session, command->private.sequence_atomic.commands);
1116         if (ret != DM_OK) {
1117                 resultStatus = DM_ERR_ATOMIC_FAILED;
1118         }
1119
1120         if (resultStatus == DM_ERR_ACCEPTED_FOR_PROCESSING) {
1121                 mo_ret = sync_agent_end_transaction_mo(SYNC_AGENT_DM_MO_TRANSACTION_COMMIT);
1122         } else {
1123                 mo_ret = sync_agent_end_transaction_mo(SYNC_AGENT_DM_MO_TRANSACTION_ROLLBACK);
1124                 __rollback_atomic(session, command);
1125         }
1126
1127         Status *temp_status = NULL;
1128         ret = create_newstatus(session, resultStatus, command, pCommand->source, pCommand->target, COMMAND_TYPE_ATOMIC, &temp_status);
1129         if (ret != DM_OK)
1130                 goto error;
1131
1132         session->status = g_list_append(session->status, temp_status);
1133
1134         _INNER_FUNC_ENTER;
1135         return ret;
1136
1137  error:
1138         _DEBUG_TRACE(" error : %d\n", ret);
1139         _INNER_FUNC_ENTER;
1140         return ret;
1141
1142 }
1143
1144 static DM_ERROR _receive_alert_status(Session * session, Status * status)
1145 {
1146         _INNER_FUNC_ENTER;
1147
1148         DM_ERROR ret = DM_OK;
1149
1150         retvm_if((status) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "status is NULL!!");
1151
1152         unsigned int code = get_statuscode(status);
1153         if (code >= 400 && code != DM_ERR_REQUIRE_REFRESH) {
1154                 //401   unauthorized
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
1162
1163                 //TODO error handling it's error~!!!!!
1164                 ret = code;
1165         }
1166         _DEBUG_TRACE(" end, Error Type : %d\n", ret);
1167         _INNER_FUNC_EXIT;
1168         return ret;
1169 }
1170
1171 static DM_ERROR _receive_results_status(Session * session, Status * status)
1172 {
1173         _INNER_FUNC_ENTER;
1174
1175         DM_ERROR ret = DM_OK;
1176
1177         retvm_if((status) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "status is NULL!!");
1178
1179         unsigned int code = get_statuscode(status);
1180         if (code >= 400) {
1181                 //TODO error handling it's error~!!!!!
1182                 //401 unauthorized
1183                 //407 authentication required
1184                 //404 not found
1185                 //413 request entity too large
1186                 //415 unspported media type or format
1187                 //500 command failed
1188                 ret = code;
1189         }
1190
1191         _DEBUG_TRACE(" end, Error Type : %d\n", ret);
1192         _INNER_FUNC_EXIT;
1193         return ret;
1194 }
1195
1196 static void _free_pending_statuses(GList * pendingStatus)
1197 {
1198         _INNER_FUNC_ENTER;
1199
1200         retm_if((pendingStatus) == NULL, "pendingStatus is NULL!!");
1201
1202         GList *iter = NULL;
1203         for (iter = pendingStatus; iter != NULL; iter = g_list_next(iter))
1204                 __free_pending_status(iter->data);
1205
1206         g_list_free(pendingStatus);
1207
1208         _INNER_FUNC_EXIT;
1209 }
1210
1211 static void __free_pending_status(PendingStatus * pendingStatus)
1212 {
1213         _INNER_FUNC_ENTER;
1214
1215         retm_if((pendingStatus) == NULL, "pendingStatus is NULL!!");
1216
1217         free(pendingStatus);
1218         pendingStatus = NULL;
1219
1220         _INNER_FUNC_EXIT;
1221 }
1222
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)
1224 {
1225         _EXTERN_FUNC_ENTER;
1226
1227         DM_ERROR ret = DM_OK;
1228
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!!");
1231
1232         *pSession = (Session *) calloc(1, sizeof(Session));
1233         if (*pSession == NULL) {
1234                 ret = COMMON_ERR_ALLOC;
1235                 goto error;
1236         }
1237
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;
1246
1247         ret = create_location(pSourceUrl, client_id, &((*pSession)->source));
1248         if (ret != DM_OK)
1249                 goto error;
1250
1251         ret = create_location(pTargetUrl, NULL, &((*pSession)->target));
1252         if (ret != DM_OK)
1253                 goto error;
1254
1255         (*pSession)->msgID = 0;
1256         (*pSession)->cmdID = 1;
1257
1258         (*pSession)->res_chal = NULL;
1259         (*pSession)->req_chal = NULL;
1260
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;
1265                 goto error;
1266         }
1267         (*pSession)->resphmacinfo = (Hmac *) calloc(1, sizeof(Hmac) + 1);
1268         if ((*pSession)->resphmacinfo == NULL) {
1269                 ret = COMMON_ERR_ALLOC;
1270                 goto error;
1271         }
1272
1273         (*pSession)->sourceMaxMsgSize = DEFAULT_MAX_MSG_SIZE;
1274         (*pSession)->sourceMaxObjSize = DEFAULT_MAX_OBJ_SIZE;
1275
1276         (*pSession)->targetMaxMsgSize = 0;
1277         (*pSession)->targetMaxObjSize = 0;
1278
1279         _EXTERN_FUNC_EXIT;
1280         return ret;
1281
1282  error:
1283
1284         if (*pSession != NULL) {
1285                 _DEBUG_TRACE("free session");
1286                 free_session(*pSession);
1287         }
1288
1289         _DEBUG_TRACE(" error : %d\n", ret);
1290         _EXTERN_FUNC_EXIT;
1291         return ret;
1292
1293 }
1294
1295 void free_session(Session * session)
1296 {
1297         _EXTERN_FUNC_ENTER;
1298
1299         retm_if((session) == NULL, "session is NULL!!");
1300
1301         if (session->pServer_id != NULL) {
1302                 free(session->pServer_id);
1303                 session->pServer_id = NULL;
1304         }
1305
1306         if (session->sessionID != NULL) {
1307                 free(session->sessionID);
1308                 session->sessionID = NULL;
1309         }
1310
1311         free_statuses(session->status);
1312         session->status = NULL;
1313
1314         free_statuses(session->tempStatus);
1315         session->status = NULL;
1316
1317         if (session->target != NULL) {
1318                 free_location(session->target);
1319                 session->target = NULL;
1320         }
1321
1322         if (session->source != NULL) {
1323                 free_location(session->source);
1324                 session->source = NULL;
1325         }
1326
1327         if (session->orgTarget != NULL) {
1328                 free_location(session->orgTarget);
1329                 session->orgTarget = NULL;
1330         }
1331
1332         if (session->cred != NULL) {
1333                 free_cred(session->cred);
1334                 session->cred = NULL;
1335         }
1336
1337         if (session->res_chal != NULL) {
1338                 free_chal(session->res_chal);
1339                 session->res_chal = NULL;
1340         }
1341
1342         if (session->req_chal != NULL) {
1343                 free_chal(session->req_chal);
1344                 session->req_chal = NULL;
1345         }
1346
1347         if (session->reqhmacinfo != NULL) {
1348                 free_hmac(session->reqhmacinfo);
1349                 session->reqhmacinfo = NULL;
1350         }
1351         if (session->resphmacinfo != NULL) {
1352                 free_hmac(session->resphmacinfo);
1353                 session->resphmacinfo = NULL;
1354         }
1355
1356         _free_pending_statuses(session->pendingStatus);
1357         session->pendingStatus = NULL;
1358
1359         if (session->pLargeObjCmd != NULL) {
1360                 free_command(session->pLargeObjCmd);
1361         }
1362
1363         session = NULL;
1364
1365         _EXTERN_FUNC_EXIT;
1366 }
1367
1368 DM_ERROR receive_header(Session * session, SyncHdr * header)
1369 {
1370         _EXTERN_FUNC_ENTER;
1371
1372         DM_ERROR ret = DM_OK;
1373         Location *pLocation = NULL;
1374
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!!");
1377
1378         if (session->sessionID != NULL && header->sessionID != NULL) {
1379                 if (strcmp(session->sessionID, header->sessionID) != 0) {
1380                         ret = COMMON_ERR_GENERIC;
1381                         goto error;
1382                 }
1383         }
1384
1385         session->lastRecievedMsgID = header->messageID;
1386
1387         if (0 < header->maxmsgsize)
1388                 session->targetMaxMsgSize = header->maxmsgsize;
1389         else
1390                 session->targetMaxMsgSize = DEFAULT_SERVER_MAXMSGSIZE;
1391
1392         if (0 < header->maxobjsize)
1393                 session->targetMaxObjSize = header->maxobjsize;
1394
1395         if (header->responseURI != NULL) {
1396                 if (session->orgTarget == NULL) {
1397                         session->orgTarget = session->target;
1398                         session->target = NULL;
1399                 }
1400
1401                 if (session->target != NULL)
1402                         free_location(session->target);
1403
1404                 ret = create_location(header->responseURI, get_location_locname(session->orgTarget), &pLocation);
1405                 if (ret != DM_OK)
1406                         goto error;
1407                 session->target = pLocation;
1408
1409                 if (session->target == NULL) {
1410                         ret = COMMON_ERR_INTERNAL_NO_MEMORY;
1411                         goto error;
1412                 }
1413
1414                 _DEBUG_INFO("  target %s\n", session->target->locURI);
1415         }
1416
1417         Status *temp = NULL;
1418         DM_ERROR statusData = DM_ERR_UNKNOWN;
1419
1420         if (header->cred != NULL) {
1421                 _DEBUG_INFO("header cred ");
1422                 //ret = check_server_cred(header->cred, session);
1423                 statusData = DM_ERR_AUTH_ACCEPTED;
1424                 /*
1425                  * check server cred
1426                  */
1427         } else {
1428                 _DEBUG_INFO("header not cred ");
1429                 statusData = DM_OK;
1430         }
1431
1432         ret = create_status(statusData, session->cmdID++, session->lastRecievedMsgID, 0, header->source, header->target, NULL, COMMAND_TYPE_HEADER, &temp);
1433         if (ret != DM_OK)
1434                 goto error;
1435
1436         session->status = g_list_append(session->status, temp);
1437
1438         //free_chal(pChal);
1439
1440         _EXTERN_FUNC_EXIT;
1441         return ret;
1442
1443  error:
1444         //free_chal(pChal);
1445         _DEBUG_INFO(" error : %d\n", ret);
1446         _EXTERN_FUNC_EXIT;
1447         return ret;
1448 }
1449
1450 DM_ERROR receive_statuses(Session * session, GList * receiveStatus)
1451 {
1452         _EXTERN_FUNC_ENTER;
1453
1454         DM_ERROR ret = DM_OK;
1455
1456         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
1457
1458         GList *statusItem = receiveStatus;
1459         Status *status = NULL;
1460         Chal *pChal = NULL;
1461         Status *req_chal_status = NULL;
1462         GList *header = NULL;
1463
1464         while (statusItem) {
1465                 status = statusItem->data;
1466
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);
1472
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.
1477
1478                                 Chal *chal = status->chal;
1479                                 if (chal != NULL) {
1480                                         _DEBUG_INFO("exist chal");
1481
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);
1484
1485                                         /*next session nonce data */
1486                                         if (chal->nonce_plain != NULL) {
1487                                                 set_client_nextnonce(session->pServer_id, chal->nonce_plain);
1488                                         }
1489
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);
1495                                                 if (ret != DM_OK)
1496                                                         goto error;
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);
1502                                                 if (ret != DM_OK)
1503                                                         goto error;
1504                                         }
1505
1506                                         if (pChal != NULL) {
1507
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");
1515                                                                         break;
1516                                                                 }
1517                                                         }
1518                                                 }
1519                                         }
1520
1521                                 } else {
1522                                         _DEBUG_INFO("chal null");
1523                                 }
1524
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;
1529                                         } else {
1530                                                 //do nothing
1531                                         }
1532                                 }
1533                                 // 401 ||  407
1534                                 if (errorCode == DM_ERR_UNAUTHORIZED) {
1535                                         ret = DM_ERR_UNAUTHORIZED;
1536                                         goto error;
1537                                 } else if (errorCode == DM_ERR_AUTHENTICATION_REQUIRED) {
1538                                         ret = DM_ERR_AUTHENTICATION_REQUIRED;
1539                                         goto error;
1540                                 }
1541
1542                         } else if (errorCode == DM_ERR_SERVER_FAILURE) {
1543                                 ret = DM_ERR_SERVER_FAILURE;
1544                                 goto error;
1545                         }
1546                 } else {
1547                         //status except status of SyncHdr
1548                         GList *pendingStatusItem = session->pendingStatus;
1549                         PendingStatus *pending = NULL;
1550                         while (pendingStatusItem) {
1551                                 pending = pendingStatusItem->data;
1552
1553                                 if (pending->cmdID == status->cmdRef && pending->msgID == status->msgRef) {
1554                                         //if status's cmdID and msgID exists in pendigStatus
1555
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);
1560                                         } else {
1561                                                 /*
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
1564                                                  */
1565                                         }
1566                                         if (ret != DM_OK)
1567                                                 goto error;
1568
1569                                         session->pendingStatus = g_list_remove(session->pendingStatus, pending);
1570                                         free(pending);
1571                                 } else {
1572                                         //if status's cmdID and msgID doesn't exists in pendigStatus
1573                                         //is it error?? does this can happen??
1574                                 }
1575                                 pendingStatusItem = g_list_next(pendingStatusItem);
1576                         }
1577                 }
1578                 statusItem = g_list_next(statusItem);
1579         }
1580
1581         free_chal(pChal);
1582         _EXTERN_FUNC_EXIT;
1583         return ret;
1584
1585  error:
1586         free_chal(pChal);
1587         _DEBUG_INFO(" error, Error Type %d \n", ret);
1588         _EXTERN_FUNC_EXIT;
1589         return ret;
1590 }
1591
1592 DM_ERROR receive_cmd(Session * session, GList * receiveCommand)
1593 {
1594         _EXTERN_FUNC_ENTER;
1595
1596         DM_ERROR ret = DM_OK;
1597
1598         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
1599
1600         GList *commandItem = receiveCommand;
1601         Command *command = NULL;
1602
1603         while (commandItem) {
1604                 command = commandItem->data;
1605
1606                 switch (command->type) {
1607                 case COMMAND_TYPE_ALERT:
1608                         ret = _receive_alert_cmd(session, command);
1609                         break;
1610                 case COMMAND_TYPE_GET:
1611                         ret = _receive_get_cmd(session, command);
1612                         break;
1613                 case COMMAND_TYPE_RESULTS:
1614                         ret = _receive_results_cmd(session, command);
1615                         break;
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);
1621                         break;
1622                 case COMMAND_TYPE_EXEC:
1623                         ret = _receive_exec_cmd(session, command);
1624                         break;
1625                 case COMMAND_TYPE_SEQUENCE:
1626                         ret = _receice_sequence_cmd(session, command);
1627                         break;
1628                 case COMMAND_TYPE_ATOMIC:
1629                         ret = _receive_atomic_cmd(session, command);
1630                         break;
1631                 case COMMAND_TYPE_UNKNOWN:
1632                 case COMMAND_TYPE_HEADER:
1633                         _DEBUG_INFO("not support command");
1634                         break;
1635                 default:
1636                         break;
1637                 }
1638
1639                 _DEBUG_INFO("receivce cmd ret : %d", ret);
1640                 commandItem = g_list_next(commandItem);
1641                 if (ret != DM_OK)
1642                         goto error;
1643         }
1644
1645         _EXTERN_FUNC_EXIT;
1646         return ret;
1647
1648  error:
1649         _DEBUG_INFO(" error : %d\n", ret);
1650         _EXTERN_FUNC_EXIT;
1651         return ret;
1652 }
1653
1654 DM_ERROR create_prependingstatus(unsigned int msgID, unsigned int cmdID, PendingStatus ** pPendingStatus)
1655 {
1656         _EXTERN_FUNC_EXIT;
1657
1658         DM_ERROR ret = DM_OK;
1659
1660         *pPendingStatus = (PendingStatus *) calloc(1, sizeof(PendingStatus));
1661
1662         if (*pPendingStatus == NULL) {
1663                 ret = COMMON_ERR_ALLOC;
1664                 goto error;
1665         }
1666
1667         (*pPendingStatus)->msgID = msgID;
1668         (*pPendingStatus)->cmdID = cmdID;
1669
1670         ret = DM_OK;
1671
1672         _EXTERN_FUNC_EXIT;
1673         return ret;
1674
1675  error:
1676         _DEBUG_INFO(" error : %d\n", ret);
1677         _EXTERN_FUNC_EXIT;
1678         return ret;
1679 }
1680
1681 DM_ERROR set_session_authcred(Session * session, Cred * cred)
1682 {
1683         _EXTERN_FUNC_ENTER;
1684
1685         DM_ERROR ret = DM_OK;
1686
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!!");
1689         if (cred != NULL) {
1690                 session->cred = cred;
1691         }
1692
1693         _EXTERN_FUNC_EXIT;
1694         return ret;
1695 }
1696
1697 void reset_cmdid_session(Session * session)
1698 {
1699         _EXTERN_FUNC_ENTER;
1700
1701         retm_if((session) == NULL, "session is NULL!!");
1702
1703         session->cmdID = 1;
1704
1705         _EXTERN_FUNC_EXIT;
1706 }
1707
1708 AuthType __get_Session_AuthType_enum(char *data)
1709 {
1710         _EXTERN_FUNC_ENTER;
1711
1712         AuthType type = AUTH_TYPE_UNKNOWN;
1713
1714         retvm_if((data) == NULL, AUTH_TYPE_MD5, "data is NULL!!");
1715
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;
1722         } else {
1723                 type = AUTH_TYPE_UNKNOWN;
1724         }
1725         _DEBUG_VERBOSE(" end : [%d] \n", type);
1726         _EXTERN_FUNC_EXIT;
1727         return type;
1728 }
1729
1730 void __set_Session_AuthType_name(AuthType authType, char **authname)
1731 {
1732         _EXTERN_FUNC_ENTER;
1733
1734         switch (authType) {
1735         case AUTH_TYPE_BASIC:
1736                 (*authname) = strdup(ELEMENT_BASIC);
1737                 break;
1738         case AUTH_TYPE_MD5:
1739                 (*authname) = strdup(ELEMENT_DIGEST);
1740                 break;
1741         case AUTH_TYPE_HMAC:
1742                 (*authname) = strdup(ELEMENT_HMAC);
1743                 break;
1744         default:
1745                 (*authname) = strdup(ELEMENT_BASIC);
1746                 break;
1747         }
1748
1749         _EXTERN_FUNC_EXIT;
1750
1751 }
1752
1753 //temp code
1754 /*
1755 DM_ERROR get_session_clientauthtype(Session * session, AuthType * authType){
1756         _DEBUG_INFO(" start \n");
1757
1758         DM_ERROR ret = DM_OK;
1759         char* data  = NULL;
1760
1761          ret = get_dmacc_authType( session->pServer_id, authType);
1762         if(ret != DM_OK) {
1763                 (*authType) = AUTH_TYPE_BASIC;
1764                 ret = DM_OK;
1765         }
1766
1767         if(data != NULL) {
1768                 (*authType) = __get_Session_AuthType(data);
1769                 free(data);
1770                 data = NULL;
1771         }
1772         _DEBUG_INFO(" end \n");
1773         return ret;
1774 error :
1775         _DEBUG_INFO(" error end [%d]\n", ret);
1776         return  ret;
1777 }
1778
1779 DM_ERROR get_session_serverauthtype(Session * session, AuthType* authType){
1780         _DEBUG_INFO(" start \n");
1781
1782         DM_ERROR ret = DM_OK;
1783         char* data  = NULL;
1784
1785         ret = get_dmacc_authType( session->pServer_id, authType);
1786         if(ret != DM_OK) {
1787                 (*authType) = AUTH_TYPE_BASIC;
1788                 ret = DM_OK;
1789         }
1790
1791         if(data != NULL) {
1792                 (*authType) = __get_Session_AuthType(data);
1793
1794                 free(data);
1795                 data = NULL;
1796         }
1797
1798         _DEBUG_INFO(" end \n");
1799         return ret;
1800
1801 error:
1802         _DEBUG_INFO(" error end [%d]\n", ret);
1803         return ret;
1804 }
1805 */
1806
1807 static DM_ERROR __userInteration_operation(Command * command, GList ** response, int *result_status)
1808 {
1809         _INNER_FUNC_ENTER;
1810
1811         DM_ERROR ret = DM_OK;
1812
1813         retvm_if((command) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "command is NULL!!");
1814
1815         CommandAlertPrivate pAlert = command->private.alert;
1816         AlertType type = pAlert.type;
1817         GList *alertItems = pAlert.items;
1818         GList *iter = NULL;
1819         Item *pItem = NULL;
1820         int size = g_list_length(alertItems);
1821         int i = 0;
1822         //GList * responseItems = NULL;
1823         char *result_data;
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");
1829                 _INNER_FUNC_EXIT;
1830                 return COMMON_ERR_ALLOC;
1831         }
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);
1836                 _INNER_FUNC_EXIT;
1837                 return COMMON_ERR_ALLOC;
1838         }
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);
1844                 _INNER_FUNC_EXIT;
1845                 return COMMON_ERR_ALLOC;
1846         }
1847         switch (type) {
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) {
1851                         pItem = iter->data;
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));
1856                         } else {
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;
1863                                 }
1864                         }
1865                 }
1866                 break;
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) {
1871                         pItem = iter->data;
1872                         if (i == 0 && size != 1) {
1873                                 /*user interaction option */
1874                                 ret = exec_userinteration_option(pItem->private.data, &(pUic_data->pUic_option));
1875                         } else {
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;
1881                         }
1882                 }
1883                 break;
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) {
1887                         pItem = iter->data;
1888                         if (i == 0 && size != 1) {
1889                                 /*user interaction option */
1890                                 /*IT ( A , N, D, T , P , I )
1891                                  *ET (T , P )
1892                                  */
1893                                 ret = exec_userinteration_option(pItem->private.data, &(pUic_data->pUic_option));
1894                         } else {
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);
1902                                 } else {
1903                                         _DEBUG_VERBOSE("input text null");
1904                                 }
1905                         }
1906                 }
1907
1908                 break;
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) {
1912                                 pItem = iter->data;
1913                                 if (i == 0 && size != 1) {
1914                                         /*user interaction option */
1915                                         ret = exec_userinteration_option(pItem->private.data, &(pUic_data->pUic_option));
1916                                 } else {
1917                                         /*user interaction execute */
1918                                         if (i == 1) {
1919                                                 _DEBUG_VERBOSE("uic display : %s", pItem->private.data);
1920                                                 pUic_data->display = strdup(pItem->private.data);
1921                                         } else {
1922                                                 GList *iter_end = NULL;
1923                                                 iter_end = g_list_next(iter);
1924                                                 char *data = NULL;
1925                                                 data = strdup(pItem->private.data);
1926                                                 pUic_data->multi_data = g_list_append(pUic_data->multi_data, data);
1927
1928                                                 if (iter_end == NULL) {
1929                                                         ret = ui_single_choice(pUic_data, response);
1930                                                         *result_status = pUic_data->res_data->status;
1931                                                         break;
1932                                                 }
1933                                         }
1934                                 }
1935                         }
1936                 }
1937                 break;
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) {
1941                                 pItem = iter->data;
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));
1946                                 } else {
1947                                         /*user interaction execute */
1948                                         if (i == 1) {
1949                                                 _DEBUG_VERBOSE("uic display : %s", pItem->private.data);
1950                                                 pUic_data->display = strdup(pItem->private.data);
1951                                         } else {
1952                                                 GList *iter_end = NULL;
1953                                                 iter_end = g_list_next(iter);
1954                                                 char *data = NULL;
1955                                                 data = strdup(pItem->private.data);
1956                                                 pUic_data->multi_data = g_list_append(pUic_data->multi_data, data);
1957
1958                                                 if (iter_end == NULL) {
1959                                                         ret = ui_multiple_choice(pUic_data, response);
1960                                                         *result_status = pUic_data->res_data->status;
1961                                                         break;
1962                                                 }
1963                                         }
1964                                 }
1965                         }
1966                 }
1967                 break;
1968         case ALERT_UNKNOWN:
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");
1977                 break;
1978         default:
1979                 break;
1980         }
1981
1982         /*
1983            session->status
1984            for(iter = responseItems; iter != NULL; iter = g_list_next(iter)){
1985            pItem = iter->data;
1986            temp->items = g_list_append(temp->items, pItem);
1987            }
1988          */
1989         free_uic_value(&pUic_data);
1990
1991         _DEBUG_VERBOSE(" end\n");
1992         _INNER_FUNC_EXIT;
1993         return ret;
1994
1995 }
1996
1997 DM_ERROR add_response_hmacinfo(Session ** session, char *temp_hmac)
1998 {
1999         _EXTERN_FUNC_ENTER;
2000         DM_ERROR ret = DM_OK;
2001         /*x-syncml-hmac: algorithm=MD5, username="156354238", mac=hvMeISwxSVUEofF2jHhQjA== */
2002
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!!");
2005
2006         char *start = NULL;
2007         char *end = NULL;
2008
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;
2014                 }
2015         }
2016
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;
2022         }
2023         _DEBUG_INFO("hmac : %s", start);
2024
2025         end = strstr(temp_hmac, ",");
2026         if (end == NULL) {
2027                 _DEBUG_INFO("auth type null");
2028                 return COMMON_ERR_INTERNAL_NOT_DEFINED;
2029         }
2030         _DEBUG_INFO("hmac : %s", end);
2031
2032         _set_hmac_value(&((*session)->resphmacinfo->authtype), start + 1, (end - start - 1));
2033         if ((*session)->resphmacinfo->authtype == NULL) {
2034                 _DEBUG_INFO("alloc fail");
2035                 _EXTERN_FUNC_EXIT;
2036                 return COMMON_ERR_ALLOC;
2037         }
2038         _DEBUG_INFO("auty type : %s", (*session)->resphmacinfo->authtype);
2039
2040         start = strstr(end, "=");
2041         if (start == NULL) {
2042                 _DEBUG_INFO("username null");
2043                 return COMMON_ERR_INTERNAL_NOT_DEFINED;
2044         }
2045         _DEBUG_INFO("hmac : %s", start);
2046
2047         end = strstr(start, ",");
2048         if (end == NULL) {
2049                 _DEBUG_INFO("username null");
2050                 return COMMON_ERR_INTERNAL_NOT_DEFINED;
2051         }
2052         _DEBUG_INFO("hmac : %s", end);
2053
2054         _set_hmac_value(&((*session)->resphmacinfo->username), start + 2, (end - start - 3));
2055         if ((*session)->resphmacinfo->username == NULL) {
2056                 _DEBUG_INFO("alloc fail");
2057                 _EXTERN_FUNC_EXIT;
2058                 return COMMON_ERR_ALLOC;
2059         }
2060         _DEBUG_INFO("username : %s", (*session)->resphmacinfo->username);
2061
2062         start = strstr(end, "=");
2063         if (start == NULL) {
2064                 _DEBUG_INFO("mac null");
2065                 return COMMON_ERR_INTERNAL_NOT_DEFINED;
2066         }
2067         _DEBUG_INFO("hmac : %s", start);
2068         (*session)->resphmacinfo->mac = strdup(start + 1);
2069         _DEBUG_INFO("mac : %s", (*session)->resphmacinfo->mac);
2070
2071         _EXTERN_FUNC_EXIT;
2072         return ret;
2073 }
2074
2075 static void _set_hmac_value(char **key, char *value, int size)
2076 {
2077         _INNER_FUNC_ENTER;
2078
2079         retm_if((value) == NULL, "value is NULL!!");
2080         retm_if((size) == 0, "size is NULL!!");
2081
2082         if (*key != NULL) {
2083                 free(*key);
2084                 *key = NULL;
2085         } else {
2086                 _DEBUG_INFO("key null");
2087         }
2088
2089         (*key) = (char *)calloc(1, size + 1);
2090         if ((*key) == NULL) {
2091                 _INNER_FUNC_EXIT;
2092                 return;
2093         }
2094
2095         strncpy((*key), value, size);
2096
2097         _INNER_FUNC_EXIT;
2098 }
2099
2100 /*DM_ERROR process_commands(Command* pCommand,  int * resultStatus) {
2101
2102         _DEBUG_TRACE( " start\n");
2103
2104         DM_ERROR ret = DM_OK;
2105         switch (pCommand->type) {
2106         case COMMAND_TYPE_UNKNOWN:
2107                 break;
2108         case COMMAND_TYPE_ALERT:
2109                 ret = __process_alert(pCommand, resultStatus);
2110                 break;
2111         case COMMAND_TYPE_ADD:
2112                 break;
2113                 ret = __process_add(pCommand, resultStatus);
2114         case COMMAND_TYPE_REPLACE:
2115                 ret = __process_replace(pCommand, resultStatus);
2116                 break;
2117         case COMMAND_TYPE_DELETE:
2118                 ret = __process_delete(pCommand, resultStatus);
2119                 break;
2120         case COMMAND_TYPE_EXEC:
2121                 ret = __process_exec(pCommand, resultStatus);
2122                 break;
2123         case COMMAND_TYPE_SEQUENCE:
2124                 ret = __process_sequence(pCommand, resultStatus);
2125                 break;
2126         case COMMAND_TYPE_ATOMIC:
2127                 ret = __process_atomic(pCommand, resultStatus);
2128                 break;
2129         case COMMAND_TYPE_COPY:
2130                 break;
2131         default:
2132                 break;
2133         }
2134
2135 return_part:
2136         if (ret != DM_OK)
2137                 goto error;
2138
2139         _DEBUG_TRACE( " end\n");
2140         return ret;
2141 error:
2142         _DEBUG_TRACE( " error end : ret %d\n", ret);
2143         return ret;
2144
2145 }*/
2146
2147 static DM_ERROR __rollback_atomic(Session * session, Command * pCommand)
2148 {
2149         _INNER_FUNC_ENTER;
2150
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!!");
2154
2155         Command *current_Command = NULL;
2156         /*Status * temp_status = NULL; */
2157         GList *atomic_iter = NULL;
2158         GList *items = NULL;
2159         GList *temp_items = NULL;
2160         Item *item = NULL;
2161         sync_agent_dm_mo_error_e mo_ret = sync_agent_begin_transaction_mo();
2162         _DEBUG_VERBOSE("transaction start : %d", mo_ret);
2163
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;
2166
2167                 switch (current_Command->type) {
2168                 case COMMAND_TYPE_ALERT:
2169                         items = current_Command->private.alert.items;
2170                         break;
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;
2176                         break;
2177                 case COMMAND_TYPE_GET:
2178                         items = current_Command->private.access.items;
2179                         // delete result status
2180                         break;
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");
2188                         break;
2189                 default:
2190                         break;
2191                 }
2192
2193                 for (temp_items = items; temp_items != NULL; temp_items = g_list_next(temp_items)) {
2194                         item = temp_items->data;
2195
2196                         ret = set_status_data(session, current_Command, item, DM_ERR_ATOMIC_ROLL_BACK_OK);
2197                 }
2198         }
2199
2200         _DEBUG_VERBOSE(" end\n");
2201         _INNER_FUNC_EXIT;
2202         return ret;
2203
2204 }