Tizen 2.0 Release
[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                                                 } else {
749                                                         ret = replace_itemtomo(item);
750                                                         if (ret != DM_OK)
751                                                                 goto error;
752                                                 }
753                                         }
754                                 }
755                                 break;
756                         case GET_TYPE_ACL_PROP:
757                                 {
758                                         _DEBUG_TRACE(" property type : %d", gettype);
759                                         err_code = sync_agent_is_exist_mo(real_full_path, &is_mo);
760                                         _DEBUG_TRACE("is existed mo : %d", err_code);
761                                         if (is_mo == 0) {
762                                                 _DEBUG_TRACE("NOT EXIST MO");
763                                                 resultStatus = DM_ERR_NOT_FOUND;
764                                                 goto error;
765                                         }
766                                         /*do not check acl for replace property */
767                                         /*err_code = sync_agent_check_acl_value(real_full_path, SYNC_AGENT_DM_MO_ACCESSTYPE_REPLACE, session->pServer_id, &error_acl);
768                                            _DEBUG_TRACE("sync agent acl result : %d", err_code);
769                                            if(err_code != SYNC_AGENT_DM_MO_SUCCESS) {
770                                            resultStatus = DM_ERR_NOT_FOUND;
771                                            goto error;
772                                            }
773                                          */
774                                         /*       error_acl = 2;
775                                            _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
776                                            if(error_acl == 0) {
777                                            _DEBUG_TRACE("ACL PERMISSION ERROR");
778                                            resultStatus = DM_ERR_PERMISSION_DENIED;
779                                            goto error;
780                                            } else if(error_acl == 1) {
781                                            _DEBUG_TRACE("accesstype PERMISSION ERROR");
782                                            resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
783                                            goto error;
784                                            } else { */
785                                         _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", 2);
786                                         ret = check_mo_scope(real_full_path);
787                                         if (ret != DM_OK) {
788                                                 resultStatus = ret;
789                                                 goto error;
790                                         } else {
791                                                 //check_scope == 1 (dynamic)
792                                                 ret = replace_itemtomo(item);
793                                                 if (ret != DM_OK)
794                                                         goto error;
795                                         }
796                                         //}
797                                 }
798                                 break;
799                         case GET_TYPE_NAME_PROP:
800                         case GET_TYPE_TYPE_PROP:
801                         case GET_TYPE_FORMAT_PROP:
802                         case GET_TYPE_SIZE_PROP:
803                                 {
804                                         _DEBUG_TRACE(" property type : %d", gettype);
805                                         err_code = sync_agent_is_exist_mo(real_full_path, &is_mo);
806                                         _DEBUG_TRACE("is existed mo : %d", err_code);
807                                         if (is_mo == 0) {
808                                                 _DEBUG_TRACE("NOT EXIST MO");
809                                                 resultStatus = DM_ERR_NOT_FOUND;
810                                                 goto error;
811                                         }
812                                         resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
813                                 }
814                                 break;
815                         default:
816                                 _DEBUG_TRACE("replace type error");
817                                 break;
818                         }
819                         break;
820                 case CHANGE_DELETE:
821                         _DEBUG_TRACE(" delete command \n");
822                         err_code = sync_agent_is_exist_mo(targetUrl, &is_mo);
823                         _DEBUG_TRACE("is existed mo : %d", err_code);
824                         if (is_mo == 0) {
825                                 _DEBUG_TRACE("NOT EXIST MO");
826                                 resultStatus = DM_ERR_NOT_FOUND;
827                                 goto error;
828                         } else {
829
830                                 //error_acl = 1;
831                                 err_code = sync_agent_check_acl_value(targetUrl, SYNC_AGENT_DM_MO_ACCESSTYPE_DELETE, session->pServer_id, &error_acl);
832                                 _DEBUG_TRACE("sync agent acl result : %d", err_code);
833                                 if (err_code != SYNC_AGENT_DM_MO_SUCCESS) {
834                                         _DEBUG_TRACE("acl get fail");
835                                         resultStatus = DM_ERR_NOT_FOUND;
836                                         goto error;
837                                 }
838                                 _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
839                                 if (error_acl == 0) {
840                                         _DEBUG_TRACE("ACL PERMISSION ERROR");
841                                         resultStatus = DM_ERR_PERMISSION_DENIED;
842                                         goto error;
843                                 } else if (error_acl == 1) {
844                                         _DEBUG_TRACE("accesstype PERMISSION ERROR");
845                                         resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
846                                         goto error;
847                                 } else {
848                                         _DEBUG_TRACE("ACL PERMISSION");
849                                         ret = check_mo_scope(targetUrl);
850                                         if (ret != DM_OK) {
851                                                 resultStatus = ret;
852                                         } else {
853                                                 ret = delete_itemtomo(item);
854                                                 if (ret != DM_OK)
855                                                         goto error;
856                                         }
857                                 }
858                         }
859                         break;
860                 case CHANGE_COPY:
861                         _DEBUG_TRACE(" copy command \n");
862                         sourceUrl = NULL;
863                         sourceUrl = get_location_locuri(item->source);
864                         if (sourceUrl != NULL) {
865                                 err_code = sync_agent_is_exist_mo(sourceUrl, &is_mo);
866                                 _DEBUG_TRACE("is existed mo : %d", err_code);
867                                 if (is_mo == 0) {
868                                         _DEBUG_TRACE("NOT EXIT MO");
869                                         resultStatus = DM_ERR_NOT_FOUND;
870                                         goto error;
871                                 } else {
872                                         //error_acl = 1;
873                                         err_code = sync_agent_check_acl_value(sourceUrl, SYNC_AGENT_DM_MO_ACCESSTYPE_COPY, session->pServer_id, &error_acl);
874                                         _DEBUG_TRACE("sync agent acl result : %d", err_code);
875                                         if (err_code != SYNC_AGENT_DM_MO_SUCCESS) {
876                                                 _DEBUG_TRACE("acl get fail");
877                                                 resultStatus = DM_ERR_NOT_FOUND;
878                                                 goto error;
879                                         }
880                                         _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
881                                         if (error_acl == 0) {
882                                                 _DEBUG_TRACE("ACL PERMISSION ERROR");
883                                                 resultStatus = DM_ERR_PERMISSION_DENIED;
884                                                 goto error;
885                                         } else if (error_acl == 1) {
886                                                 _DEBUG_TRACE("accesstype PERMISSION ERROR");
887                                                 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
888                                                 goto error;
889                                         }
890                                         err_code = sync_agent_check_acl_value(targetUrl, SYNC_AGENT_DM_MO_ACCESSTYPE_COPY, session->pServer_id, &error_acl);
891                                         _DEBUG_TRACE("sync agent acl result : %d", err_code);
892                                         if (err_code != SYNC_AGENT_DM_MO_SUCCESS) {
893                                                 _DEBUG_TRACE("acl get fail");
894                                                 resultStatus = DM_ERR_NOT_FOUND;
895                                                 goto error;
896                                         }
897                                         _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
898                                         if (error_acl == 0) {
899                                                 _DEBUG_TRACE("ACL PERMISSION ERROR");
900                                                 resultStatus = DM_ERR_PERMISSION_DENIED;
901                                                 goto error;
902                                         } else if (error_acl == 1) {
903                                                 _DEBUG_TRACE("accesstype PERMISSION ERROR");
904                                                 resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
905                                                 goto error;
906                                         } else {
907                                                 _DEBUG_TRACE("ACL PERMISSION");
908                                                 ret = copy_itemtomo(item);
909                                                 if (ret != DM_OK)
910                                                         goto error;
911                                         }
912                                 }
913                         } else {
914                                 //do nothing
915                         }
916                         break;
917                 default:
918                         break;
919                 }
920
921                 ret = create_newstatus(session, resultStatus, command, item->source, item->target, command->type, &temp);
922                 if (ret != DM_OK)
923                         goto error;
924                 session->status = g_list_append(session->status, temp);
925         }
926
927         str_free(&(real_full_path));
928         _INNER_FUNC_ENTER;
929         return ret;
930
931  error:
932         ret = create_newstatus(session, resultStatus, command, command->source, command->target, command->type, &temp);
933         if (ret != DM_OK)
934                 goto ex_error;
935         session->status = g_list_append(session->status, temp);
936
937         str_free(&(real_full_path));
938         _DEBUG_TRACE(" error : %d\n", ret);
939         _INNER_FUNC_ENTER;
940         return ret;
941  ex_error:
942
943         free_status(temp);
944         str_free(&(real_full_path));
945         _DEBUG_TRACE(" error : %d\n", ret);
946         _INNER_FUNC_ENTER;
947         return ret;
948 }
949
950 static DM_ERROR _receive_exec_cmd(Session * session, Command * command)
951 {
952         _INNER_FUNC_ENTER;
953
954         DM_ERROR ret = DM_OK;
955
956         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
957         retvm_if((command) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "command is NULL!!");
958
959         int resultStatus = DM_ERR_ACCEPTED_FOR_PROCESSING;
960         int error_acl = 0;
961         char *targetUrl = NULL;
962         Item *item = NULL;
963         item = command->private.exec.item;
964         Status *temp = NULL;
965         char *pCorrelator = NULL;
966         char *server_id = NULL;
967         int is_mo = 0;
968         sync_agent_dm_mo_error_e err_code = SYNC_AGENT_DM_MO_SUCCESS;
969
970         _DEBUG_TRACE("----------------------------------------------------------------------------exec cmd --------------------------------------------------------------------------- ");
971         targetUrl = get_location_locuri(command->private.exec.item->target);
972         /*todo: server_id */
973         //error_acl = 1;
974         err_code = sync_agent_is_exist_mo(targetUrl, &is_mo);
975         _DEBUG_TRACE("is existed mo : %d", err_code);
976         if (is_mo == 0) {
977                 _DEBUG_TRACE("NOT EXIT MO");
978                 resultStatus = DM_ERR_NOT_FOUND;
979                 goto error;
980         } else {
981
982                 //error_acl = 1;
983                 err_code = sync_agent_check_acl_value(targetUrl, SYNC_AGENT_DM_MO_ACCESSTYPE_EXEC, session->pServer_id, &error_acl);
984                 _DEBUG_TRACE("sync agent acl result : %d", err_code);
985                 if (err_code != SYNC_AGENT_DM_MO_SUCCESS) {
986                         _DEBUG_TRACE("acl get fail");
987                         resultStatus = DM_ERR_NOT_FOUND;
988                         goto error;
989                 }
990                 _DEBUG_TRACE("----------------------------------------------------------------------------------------ACL RETURN--------------------------------------------------- : %d", error_acl);
991                 if (error_acl == 0) {
992                         _DEBUG_TRACE("ACL PERMISSION ERROR");
993                         resultStatus = DM_ERR_PERMISSION_DENIED;
994                         goto error;
995                 } else if (error_acl == 1) {
996                         _DEBUG_TRACE("accesstype PERMISSION ERROR");
997                         resultStatus = DM_ERR_COMMAND_NOT_ALLOWED;
998                         goto error;
999                 } else {
1000                         _DEBUG_TRACE("ACL PERMISSION");
1001                         targetUrl = get_location_locuri(item->target);
1002                         pCorrelator = command->private.exec.correlator;
1003                         server_id = session->pServer_id;
1004
1005                         ret = engine_start(server_id, targetUrl, pCorrelator, &(session->service_engine_id), &resultStatus);
1006                         _DEBUG_TRACE("--------------------------------------------------------engine_start off %d-----------------------------------------------------------------\n", ret);
1007                         if (ret != DM_OK) {
1008                                 //ret = DM_ERR_NOT_FOUND;
1009                                 goto error;
1010                         }
1011                 }
1012         }
1013
1014         ret = create_newstatus(session, resultStatus, command, item->source, item->target, COMMAND_TYPE_EXEC, &temp);
1015         if (ret != DM_OK)
1016                 goto ex_error;
1017         session->status = g_list_append(session->status, temp);
1018
1019         _INNER_FUNC_ENTER;
1020         return ret;
1021
1022  error:
1023         ret = create_newstatus(session, resultStatus, command, item->source, item->target, COMMAND_TYPE_EXEC, &temp);
1024         if (ret != DM_OK)
1025                 goto ex_error;
1026         session->status = g_list_append(session->status, temp);
1027
1028         _DEBUG_TRACE(" error : %d\n", ret);
1029         _INNER_FUNC_ENTER;
1030         return ret;
1031  ex_error:
1032         _DEBUG_TRACE(" error : %d\n", ret);
1033         _INNER_FUNC_ENTER;
1034         return ret;
1035
1036 }
1037
1038 static DM_ERROR _receice_sequence_cmd(Session * session, Command * command)
1039 {
1040         _INNER_FUNC_ENTER;
1041
1042         DM_ERROR ret = DM_OK;
1043
1044         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
1045         retvm_if((command) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "command is NULL!!");
1046
1047         int resultStatus = DM_OK;
1048         //sync_agent_dm_mo_error_e mo_ret  = sync_agent_begin_transaction_mo();
1049
1050         Status *temp = NULL;
1051         ret = create_newstatus(session, resultStatus, command, command->source, command->target, COMMAND_TYPE_SEQUENCE, &temp);
1052         if (ret != DM_OK)
1053                 goto error;
1054
1055         session->status = g_list_append(session->status, temp);
1056
1057         ret = receive_cmd(session, command->private.sequence_atomic.commands);
1058         if (ret != DM_OK) {
1059                 resultStatus = DM_ERR_COMMAND_FAILED;
1060         }
1061         //mo_ret = sync_agent_end_transaction_mo(SYNC_AGENT_DM_MO_TRANSACTION_COMMIT);
1062
1063         _INNER_FUNC_ENTER;
1064         return ret;
1065
1066  error:
1067         _DEBUG_TRACE(" error : %d\n", ret);
1068         _INNER_FUNC_ENTER;
1069         return ret;
1070
1071 }
1072
1073 static DM_ERROR _receive_atomic_cmd(Session * session, Command * command)
1074 {
1075         _INNER_FUNC_ENTER;
1076
1077         DM_ERROR ret = DM_OK;
1078
1079         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
1080         retvm_if((command) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "command is NULL!!");
1081
1082         int resultStatus = DM_ERR_ACCEPTED_FOR_PROCESSING;
1083
1084         GList *atomic_iter = NULL;
1085         Command *pCommand = NULL;
1086         Status *temp = NULL;
1087         sync_agent_dm_mo_error_e mo_ret = sync_agent_begin_transaction_mo();
1088
1089         for (atomic_iter = command->private.sequence_atomic.commands; atomic_iter != NULL;) {
1090                 pCommand = atomic_iter->data;
1091
1092                 if (pCommand->type == COMMAND_TYPE_GET) {
1093                         ret = create_newstatus(session, DM_ERR_COMMAND_FAILED, pCommand, pCommand->source, pCommand->target, COMMAND_TYPE_GET, &temp);
1094                         if (ret != DM_OK)
1095                                 goto error;
1096
1097                         session->status = g_list_append(session->status, temp);
1098
1099                         atomic_iter = g_list_next(atomic_iter);
1100                         command->private.sequence_atomic.commands = g_list_remove(command->private.sequence_atomic.commands, pCommand);
1101                 }
1102         }
1103
1104         ret = receive_cmd(session, command->private.sequence_atomic.commands);
1105         if (ret != DM_OK) {
1106                 resultStatus = DM_ERR_ATOMIC_FAILED;
1107         }
1108
1109         if (resultStatus == DM_ERR_ACCEPTED_FOR_PROCESSING) {
1110                 mo_ret = sync_agent_end_transaction_mo(SYNC_AGENT_DM_MO_TRANSACTION_COMMIT);
1111         } else {
1112                 mo_ret = sync_agent_end_transaction_mo(SYNC_AGENT_DM_MO_TRANSACTION_ROLLBACK);
1113                 __rollback_atomic(session, command);
1114         }
1115
1116         Status *temp_status = NULL;
1117         ret = create_newstatus(session, resultStatus, command, pCommand->source, pCommand->target, COMMAND_TYPE_ATOMIC, &temp_status);
1118         if (ret != DM_OK)
1119                 goto error;
1120
1121         session->status = g_list_append(session->status, temp_status);
1122
1123         _INNER_FUNC_ENTER;
1124         return ret;
1125
1126  error:
1127         _DEBUG_TRACE(" error : %d\n", ret);
1128         _INNER_FUNC_ENTER;
1129         return ret;
1130
1131 }
1132
1133 static DM_ERROR _receive_alert_status(Session * session, Status * status)
1134 {
1135         _INNER_FUNC_ENTER;
1136
1137         DM_ERROR ret = DM_OK;
1138
1139         retvm_if((status) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "status is NULL!!");
1140
1141         unsigned int code = get_statuscode(status);
1142         if (code >= 400 && code != DM_ERR_REQUIRE_REFRESH) {
1143                 //401   unauthorized
1144                 //407   Authentication required
1145                 //405   command not allowed
1146                 //406   optional feature not supported
1147                 //500   command failed
1148                 //412   Incomplete command
1149                 //415 unsupported media type or format
1150                 //404 not found ->it have to be considered
1151
1152                 //TODO error handling it's error~!!!!!
1153                 ret = code;
1154         }
1155         _DEBUG_TRACE(" end, Error Type : %d\n", ret);
1156         _INNER_FUNC_EXIT;
1157         return ret;
1158 }
1159
1160 static DM_ERROR _receive_results_status(Session * session, Status * status)
1161 {
1162         _INNER_FUNC_ENTER;
1163
1164         DM_ERROR ret = DM_OK;
1165
1166         retvm_if((status) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "status is NULL!!");
1167
1168         unsigned int code = get_statuscode(status);
1169         if (code >= 400) {
1170                 //TODO error handling it's error~!!!!!
1171                 //401 unauthorized
1172                 //407 authentication required
1173                 //404 not found
1174                 //413 request entity too large
1175                 //415 unspported media type or format
1176                 //500 command failed
1177                 ret = code;
1178         }
1179
1180         _DEBUG_TRACE(" end, Error Type : %d\n", ret);
1181         _INNER_FUNC_EXIT;
1182         return ret;
1183 }
1184
1185 static void _free_pending_statuses(GList * pendingStatus)
1186 {
1187         _INNER_FUNC_ENTER;
1188
1189         retm_if((pendingStatus) == NULL, "pendingStatus is NULL!!");
1190
1191         GList *iter = NULL;
1192         for (iter = pendingStatus; iter != NULL; iter = g_list_next(iter))
1193                 __free_pending_status(iter->data);
1194
1195         g_list_free(pendingStatus);
1196
1197         _INNER_FUNC_EXIT;
1198 }
1199
1200 static void __free_pending_status(PendingStatus * pendingStatus)
1201 {
1202         _INNER_FUNC_ENTER;
1203
1204         retm_if((pendingStatus) == NULL, "pendingStatus is NULL!!");
1205
1206         free(pendingStatus);
1207         pendingStatus = NULL;
1208
1209         _INNER_FUNC_EXIT;
1210 }
1211
1212 DM_ERROR create_syncml_session(ProtocolVersion protocolVersion, ProtocolType protocolType, char *server_id, char *client_id, char *pSession_id, char *pSourceUrl, char *pTargetUrl, Session ** pSession)
1213 {
1214         _EXTERN_FUNC_ENTER;
1215
1216         DM_ERROR ret = DM_OK;
1217
1218         retvm_if((pSourceUrl) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "pSourceUrl is NULL!!");
1219         retvm_if((pTargetUrl) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "pTargetUrl is NULL!!");
1220
1221         *pSession = (Session *) calloc(1, sizeof(Session));
1222         if (*pSession == NULL) {
1223                 ret = COMMON_ERR_ALLOC;
1224                 goto error;
1225         }
1226
1227         (*pSession)->protocolType = protocolType;
1228         (*pSession)->protocolVersion = protocolVersion;
1229 /*              (*pSession)->accountId = accountId;*/
1230         if (server_id != NULL)
1231                 (*pSession)->pServer_id = strdup(server_id);
1232         (*pSession)->service_engine_id = NO_SERVICE_ENGINE;
1233         (*pSession)->sessionID = strdup(pSession_id);
1234         (*pSession)->jsessionid = NULL;
1235
1236         ret = create_location(pSourceUrl, client_id, &((*pSession)->source));
1237         if (ret != DM_OK)
1238                 goto error;
1239
1240         ret = create_location(pTargetUrl, NULL, &((*pSession)->target));
1241         if (ret != DM_OK)
1242                 goto error;
1243
1244         (*pSession)->msgID = 0;
1245         (*pSession)->cmdID = 1;
1246
1247         (*pSession)->res_chal = NULL;
1248         (*pSession)->req_chal = NULL;
1249
1250         (*pSession)->isHMAC = 0;        /*not using hmac */
1251         (*pSession)->reqhmacinfo = (Hmac *) calloc(1, sizeof(Hmac) + 1);
1252         if ((*pSession)->reqhmacinfo == NULL) {
1253                 ret = COMMON_ERR_ALLOC;
1254                 goto error;
1255         }
1256         (*pSession)->resphmacinfo = (Hmac *) calloc(1, sizeof(Hmac) + 1);
1257         if ((*pSession)->resphmacinfo == NULL) {
1258                 ret = COMMON_ERR_ALLOC;
1259                 goto error;
1260         }
1261
1262         (*pSession)->sourceMaxMsgSize = DEFAULT_MAX_MSG_SIZE;
1263         (*pSession)->sourceMaxObjSize = DEFAULT_MAX_OBJ_SIZE;
1264
1265         (*pSession)->targetMaxMsgSize = 0;
1266         (*pSession)->targetMaxObjSize = 0;
1267
1268         _EXTERN_FUNC_EXIT;
1269         return ret;
1270
1271  error:
1272
1273         if (*pSession != NULL) {
1274                 _DEBUG_TRACE("free session");
1275                 free_session(*pSession);
1276         }
1277
1278         _DEBUG_TRACE(" error : %d\n", ret);
1279         _EXTERN_FUNC_EXIT;
1280         return ret;
1281
1282 }
1283
1284 void free_session(Session * session)
1285 {
1286         _EXTERN_FUNC_ENTER;
1287
1288         retm_if((session) == NULL, "session is NULL!!");
1289
1290         if (session->pServer_id != NULL) {
1291                 free(session->pServer_id);
1292                 session->pServer_id = NULL;
1293         }
1294
1295         if (session->sessionID != NULL) {
1296                 free(session->sessionID);
1297                 session->sessionID = NULL;
1298         }
1299
1300         free_statuses(session->status);
1301         session->status = NULL;
1302
1303         free_statuses(session->tempStatus);
1304         session->status = NULL;
1305
1306         if (session->target != NULL) {
1307                 free_location(session->target);
1308                 session->target = NULL;
1309         }
1310
1311         if (session->source != NULL) {
1312                 free_location(session->source);
1313                 session->source = NULL;
1314         }
1315
1316         if (session->orgTarget != NULL) {
1317                 free_location(session->orgTarget);
1318                 session->orgTarget = NULL;
1319         }
1320
1321         if (session->cred != NULL) {
1322                 free_cred(session->cred);
1323                 session->cred = NULL;
1324         }
1325
1326         if (session->res_chal != NULL) {
1327                 free_chal(session->res_chal);
1328                 session->res_chal = NULL;
1329         }
1330
1331         if (session->req_chal != NULL) {
1332                 free_chal(session->req_chal);
1333                 session->req_chal = NULL;
1334         }
1335
1336         if (session->reqhmacinfo != NULL) {
1337                 free_hmac(session->reqhmacinfo);
1338                 session->reqhmacinfo = NULL;
1339         }
1340         if (session->resphmacinfo != NULL) {
1341                 free_hmac(session->resphmacinfo);
1342                 session->resphmacinfo = NULL;
1343         }
1344
1345         _free_pending_statuses(session->pendingStatus);
1346         session->pendingStatus = NULL;
1347
1348         if (session->pLargeObjCmd != NULL) {
1349                 free_command(session->pLargeObjCmd);
1350         }
1351
1352         session = NULL;
1353
1354         _EXTERN_FUNC_EXIT;
1355 }
1356
1357 DM_ERROR receive_header(Session * session, SyncHdr * header)
1358 {
1359         _EXTERN_FUNC_ENTER;
1360
1361         DM_ERROR ret = DM_OK;
1362         Location *pLocation = NULL;
1363
1364         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
1365         retvm_if((header) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "header is NULL!!");
1366
1367         if (session->sessionID != NULL && header->sessionID != NULL) {
1368                 if (strcmp(session->sessionID, header->sessionID) != 0) {
1369                         ret = COMMON_ERR_GENERIC;
1370                         goto error;
1371                 }
1372         }
1373
1374         session->lastRecievedMsgID = header->messageID;
1375
1376         if (0 < header->maxmsgsize)
1377                 session->targetMaxMsgSize = header->maxmsgsize;
1378         else
1379                 session->targetMaxMsgSize = DEFAULT_SERVER_MAXMSGSIZE;
1380
1381         if (0 < header->maxobjsize)
1382                 session->targetMaxObjSize = header->maxobjsize;
1383
1384         if (header->responseURI != NULL) {
1385                 if (session->orgTarget == NULL) {
1386                         session->orgTarget = session->target;
1387                         session->target = NULL;
1388                 }
1389
1390                 if (session->target != NULL)
1391                         free_location(session->target);
1392
1393                 ret = create_location(header->responseURI, get_location_locname(session->orgTarget), &pLocation);
1394                 if (ret != DM_OK)
1395                         goto error;
1396                 session->target = pLocation;
1397
1398                 if (session->target == NULL) {
1399                         ret = COMMON_ERR_INTERNAL_NO_MEMORY;
1400                         goto error;
1401                 }
1402
1403                 _DEBUG_INFO("  target %s\n", session->target->locURI);
1404         }
1405
1406         Status *temp = NULL;
1407         DM_ERROR statusData = DM_ERR_UNKNOWN;
1408
1409         if (header->cred != NULL) {
1410                 _DEBUG_INFO("header cred ");
1411                 //ret = check_server_cred(header->cred, session);
1412                 statusData = DM_ERR_AUTH_ACCEPTED;
1413                 /*
1414                  * check server cred
1415                  */
1416         } else {
1417                 _DEBUG_INFO("header not cred ");
1418                 statusData = DM_OK;
1419         }
1420
1421         ret = create_status(statusData, session->cmdID++, session->lastRecievedMsgID, 0, header->source, header->target, NULL, COMMAND_TYPE_HEADER, &temp);
1422         if (ret != DM_OK)
1423                 goto error;
1424
1425         session->status = g_list_append(session->status, temp);
1426
1427         //free_chal(pChal);
1428
1429         _EXTERN_FUNC_EXIT;
1430         return ret;
1431
1432  error:
1433         //free_chal(pChal);
1434         _DEBUG_INFO(" error : %d\n", ret);
1435         _EXTERN_FUNC_EXIT;
1436         return ret;
1437 }
1438
1439 DM_ERROR receive_statuses(Session * session, GList * receiveStatus)
1440 {
1441         _EXTERN_FUNC_ENTER;
1442
1443         DM_ERROR ret = DM_OK;
1444
1445         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
1446
1447         GList *statusItem = receiveStatus;
1448         Status *status = NULL;
1449         Chal *pChal = NULL;
1450         Status *req_chal_status = NULL;
1451         GList *header = NULL;
1452
1453         while (statusItem) {
1454                 status = statusItem->data;
1455
1456                 if (status->cmdRef == 0) {
1457                         /*status of SyncHdr */
1458                         assert(status->type == COMMAND_TYPE_HEADER);
1459                         assert(status->data);
1460                         DM_ERROR errorCode = atoi(status->data);
1461
1462                         if (errorCode == DM_ERR_AUTH_ACCEPTED || errorCode == DM_OK || errorCode == DM_ERR_UNAUTHORIZED || errorCode == DM_ERR_AUTHENTICATION_REQUIRED) {
1463                                 //212 || 200 || 401 || 407
1464                                 //when auth type is AUTH_TYPE_BASIC does not need to send cred in syncHdr in same session
1465                                 //when auth type is AUTH_TYPE_MD5 the next nonce in Chal MUST used for the digest when the next sync session is started.
1466
1467                                 Chal *chal = status->chal;
1468                                 if (chal != NULL) {
1469                                         _DEBUG_INFO("exist chal");
1470
1471                                         //res_chal in status have to be stored in config_tbl because it have to be used next sync session
1472                                         _DEBUG_INFO("format type :%d\n", chal->format);
1473
1474                                         /*next session nonce data */
1475                                         if (chal->nonce_plain != NULL) {
1476                                                 set_client_nextnonce(session->pServer_id, chal->nonce_plain);
1477                                         }
1478
1479                                         /*create next req_chal */
1480                                         if (chal->type == AUTH_TYPE_MD5) {
1481                                                 _DEBUG_INFO("session have cred md5");
1482                                                 set_client_authtype(session->pServer_id, chal->type);
1483                                                 ret = create_chal(FORMAT_TYPE_BASE64, AUTH_TYPE_MD5, ELEMENT_DEFAULT_CLIENT_NONCE, &pChal);
1484                                                 if (ret != DM_OK)
1485                                                         goto error;
1486                                         } else if (chal->type == AUTH_TYPE_HMAC) {
1487                                                 session->isHMAC = 1;
1488                                                 _DEBUG_INFO("session have not cred , using hmac");
1489                                                 set_client_authtype(session->pServer_id, chal->type);
1490                                                 ret = create_chal(FORMAT_TYPE_BASE64, AUTH_TYPE_HMAC, ELEMENT_DEFAULT_CLIENT_NONCE, &pChal);
1491                                                 if (ret != DM_OK)
1492                                                         goto error;
1493                                         }
1494
1495                                         if (pChal != NULL) {
1496
1497                                                 for (header = session->status; header != NULL; header = g_list_next(header)) {
1498                                                         req_chal_status = (Status *) (header->data);
1499                                                         if (req_chal_status != NULL) {
1500                                                                 _DEBUG_TRACE("req_chal_status status type : %d", req_chal_status->type);
1501                                                                 if (req_chal_status->type == COMMAND_TYPE_HEADER) {
1502                                                                         ((Status *) (req_chal_status))->chal = dup_chal(pChal);
1503                                                                         _DEBUG_TRACE("add header chal");
1504                                                                         break;
1505                                                                 }
1506                                                         }
1507                                                 }
1508                                         }
1509
1510                                 } else {
1511                                         _DEBUG_INFO("chal null");
1512                                 }
1513
1514                                 if (errorCode == DM_ERR_AUTH_ACCEPTED || errorCode == DM_ERR_UNAUTHORIZED || errorCode == DM_ERR_AUTHENTICATION_REQUIRED) {
1515                                         if (session->cred != NULL) {
1516                                                 free_cred(session->cred);
1517                                                 session->cred = NULL;
1518                                         } else {
1519                                                 //do nothing
1520                                         }
1521                                 }
1522                                 // 401 ||  407
1523                                 if (errorCode == DM_ERR_UNAUTHORIZED) {
1524                                         ret = DM_ERR_UNAUTHORIZED;
1525                                         goto error;
1526                                 } else if (errorCode == DM_ERR_AUTHENTICATION_REQUIRED) {
1527                                         ret = DM_ERR_AUTHENTICATION_REQUIRED;
1528                                         goto error;
1529                                 }
1530
1531                         } else if (errorCode == DM_ERR_SERVER_FAILURE) {
1532                                 ret = DM_ERR_SERVER_FAILURE;
1533                                 goto error;
1534                         }
1535                 } else {
1536                         //status except status of SyncHdr
1537                         GList *pendingStatusItem = session->pendingStatus;
1538                         PendingStatus *pending = NULL;
1539                         while (pendingStatusItem) {
1540                                 pending = pendingStatusItem->data;
1541
1542                                 if (pending->cmdID == status->cmdRef && pending->msgID == status->msgRef) {
1543                                         //if status's cmdID and msgID exists in pendigStatus
1544
1545                                         if (status->type == COMMAND_TYPE_ALERT) {
1546                                                 ret = _receive_alert_status(session, status);
1547                                         } else if (status->type == COMMAND_TYPE_RESULTS) {
1548                                                 ret = _receive_results_status(session, status);
1549                                         } else {
1550                                                 /*
1551                                                    COMMAND_TYPE_GET, COMMAND_TYPE_ADD, COMMAND_TYPE_REPLACE, COMMAND_TYPE_DELETE
1552                                                    COMMAND_TYPE_COPY, COMMAND_TYPE_EXEC, COMMAND_TYPE_ATOMIC, COMMAND_TYPE_SEQUENCE
1553                                                  */
1554                                         }
1555                                         if (ret != DM_OK)
1556                                                 goto error;
1557
1558                                         session->pendingStatus = g_list_remove(session->pendingStatus, pending);
1559                                         free(pending);
1560                                 } else {
1561                                         //if status's cmdID and msgID doesn't exists in pendigStatus
1562                                         //is it error?? does this can happen??
1563                                 }
1564                                 pendingStatusItem = g_list_next(pendingStatusItem);
1565                         }
1566                 }
1567                 statusItem = g_list_next(statusItem);
1568         }
1569
1570         free_chal(pChal);
1571         _EXTERN_FUNC_EXIT;
1572         return ret;
1573
1574  error:
1575         free_chal(pChal);
1576         _DEBUG_INFO(" error, Error Type %d \n", ret);
1577         _EXTERN_FUNC_EXIT;
1578         return ret;
1579 }
1580
1581 DM_ERROR receive_cmd(Session * session, GList * receiveCommand)
1582 {
1583         _EXTERN_FUNC_ENTER;
1584
1585         DM_ERROR ret = DM_OK;
1586
1587         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
1588
1589         GList *commandItem = receiveCommand;
1590         Command *command = NULL;
1591
1592         while (commandItem) {
1593                 command = commandItem->data;
1594
1595                 switch (command->type) {
1596                 case COMMAND_TYPE_ALERT:
1597                         ret = _receive_alert_cmd(session, command);
1598                         break;
1599                 case COMMAND_TYPE_GET:
1600                         ret = _receive_get_cmd(session, command);
1601                         break;
1602                 case COMMAND_TYPE_RESULTS:
1603                         ret = _receive_results_cmd(session, command);
1604                         break;
1605                 case COMMAND_TYPE_ADD:
1606                 case COMMAND_TYPE_REPLACE:
1607                 case COMMAND_TYPE_DELETE:
1608                 case COMMAND_TYPE_COPY:
1609                         ret = _receive_changes_cmd(session, command);
1610                         break;
1611                 case COMMAND_TYPE_EXEC:
1612                         ret = _receive_exec_cmd(session, command);
1613                         break;
1614                 case COMMAND_TYPE_SEQUENCE:
1615                         ret = _receice_sequence_cmd(session, command);
1616                         break;
1617                 case COMMAND_TYPE_ATOMIC:
1618                         ret = _receive_atomic_cmd(session, command);
1619                         break;
1620                 case COMMAND_TYPE_UNKNOWN:
1621                 case COMMAND_TYPE_HEADER:
1622                         _DEBUG_INFO("not support command");
1623                         break;
1624                 default:
1625                         break;
1626                 }
1627
1628                 _DEBUG_INFO("receivce cmd ret : %d", ret);
1629                 commandItem = g_list_next(commandItem);
1630                 if (ret != DM_OK)
1631                         goto error;
1632         }
1633
1634         _EXTERN_FUNC_EXIT;
1635         return ret;
1636
1637  error:
1638         _DEBUG_INFO(" error : %d\n", ret);
1639         _EXTERN_FUNC_EXIT;
1640         return ret;
1641 }
1642
1643 DM_ERROR create_prependingstatus(unsigned int msgID, unsigned int cmdID, PendingStatus ** pPendingStatus)
1644 {
1645         _EXTERN_FUNC_EXIT;
1646
1647         DM_ERROR ret = DM_OK;
1648
1649         *pPendingStatus = (PendingStatus *) calloc(1, sizeof(PendingStatus));
1650
1651         if (*pPendingStatus == NULL) {
1652                 ret = COMMON_ERR_ALLOC;
1653                 goto error;
1654         }
1655
1656         (*pPendingStatus)->msgID = msgID;
1657         (*pPendingStatus)->cmdID = cmdID;
1658
1659         ret = DM_OK;
1660
1661         _EXTERN_FUNC_EXIT;
1662         return ret;
1663
1664  error:
1665         _DEBUG_INFO(" error : %d\n", ret);
1666         _EXTERN_FUNC_EXIT;
1667         return ret;
1668 }
1669
1670 DM_ERROR set_session_authcred(Session * session, Cred * cred)
1671 {
1672         _EXTERN_FUNC_ENTER;
1673
1674         DM_ERROR ret = DM_OK;
1675
1676         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
1677 //      retvm_if((cred) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED , "cred is NULL!!");
1678         if (cred != NULL) {
1679                 session->cred = cred;
1680         }
1681
1682         _EXTERN_FUNC_EXIT;
1683         return ret;
1684 }
1685
1686 void reset_cmdid_session(Session * session)
1687 {
1688         _EXTERN_FUNC_ENTER;
1689
1690         retm_if((session) == NULL, "session is NULL!!");
1691
1692         session->cmdID = 1;
1693
1694         _EXTERN_FUNC_EXIT;
1695 }
1696
1697 AuthType __get_Session_AuthType_enum(char *data)
1698 {
1699         _EXTERN_FUNC_ENTER;
1700
1701         AuthType type = AUTH_TYPE_UNKNOWN;
1702
1703         retvm_if((data) == NULL, AUTH_TYPE_MD5, "data is NULL!!");
1704
1705         if (strcmp(data, ELEMENT_BASIC) == 0) {
1706                 type = AUTH_TYPE_BASIC;
1707         } else if (strcmp(data, ELEMENT_DIGEST) == 0) {
1708                 type = AUTH_TYPE_MD5;
1709         } else if (strcmp(data, ELEMENT_HMAC) == 0) {
1710                 type = AUTH_TYPE_HMAC;
1711         } else {
1712                 type = AUTH_TYPE_UNKNOWN;
1713         }
1714         _DEBUG_VERBOSE(" end : [%d] \n", type);
1715         _EXTERN_FUNC_EXIT;
1716         return type;
1717 }
1718
1719 void __set_Session_AuthType_name(AuthType authType, char **authname)
1720 {
1721         _EXTERN_FUNC_ENTER;
1722
1723         switch (authType) {
1724         case AUTH_TYPE_BASIC:
1725                 (*authname) = strdup(ELEMENT_BASIC);
1726                 break;
1727         case AUTH_TYPE_MD5:
1728                 (*authname) = strdup(ELEMENT_DIGEST);
1729                 break;
1730         case AUTH_TYPE_HMAC:
1731                 (*authname) = strdup(ELEMENT_HMAC);
1732                 break;
1733         default:
1734                 (*authname) = strdup(ELEMENT_BASIC);
1735                 break;
1736         }
1737
1738         _EXTERN_FUNC_EXIT;
1739
1740 }
1741
1742 //temp code
1743 /*
1744 DM_ERROR get_session_clientauthtype(Session * session, AuthType * authType){
1745         _DEBUG_INFO(" start \n");
1746
1747         DM_ERROR ret = DM_OK;
1748         char* data  = NULL;
1749
1750          ret = get_dmacc_authType( session->pServer_id, authType);
1751         if(ret != DM_OK) {
1752                 (*authType) = AUTH_TYPE_BASIC;
1753                 ret = DM_OK;
1754         }
1755
1756         if(data != NULL) {
1757                 (*authType) = __get_Session_AuthType(data);
1758                 free(data);
1759                 data = NULL;
1760         }
1761         _DEBUG_INFO(" end \n");
1762         return ret;
1763 error :
1764         _DEBUG_INFO(" error end [%d]\n", ret);
1765         return  ret;
1766 }
1767
1768 DM_ERROR get_session_serverauthtype(Session * session, AuthType* authType){
1769         _DEBUG_INFO(" start \n");
1770
1771         DM_ERROR ret = DM_OK;
1772         char* data  = NULL;
1773
1774         ret = get_dmacc_authType( session->pServer_id, authType);
1775         if(ret != DM_OK) {
1776                 (*authType) = AUTH_TYPE_BASIC;
1777                 ret = DM_OK;
1778         }
1779
1780         if(data != NULL) {
1781                 (*authType) = __get_Session_AuthType(data);
1782
1783                 free(data);
1784                 data = NULL;
1785         }
1786
1787         _DEBUG_INFO(" end \n");
1788         return ret;
1789
1790 error:
1791         _DEBUG_INFO(" error end [%d]\n", ret);
1792         return ret;
1793 }
1794 */
1795
1796 static DM_ERROR __userInteration_operation(Command * command, GList ** response, int *result_status)
1797 {
1798         _INNER_FUNC_ENTER;
1799
1800         DM_ERROR ret = DM_OK;
1801
1802         retvm_if((command) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "command is NULL!!");
1803
1804         CommandAlertPrivate pAlert = command->private.alert;
1805         AlertType type = pAlert.type;
1806         GList *alertItems = pAlert.items;
1807         GList *iter = NULL;
1808         Item *pItem = NULL;
1809         int size = g_list_length(alertItems);
1810         int i = 0;
1811         //GList * responseItems = NULL;
1812         char *result_data;
1813         /*char* findValue = NULL; */
1814         /*int isCancel = 0; */
1815         uic_data *pUic_data = (uic_data *) calloc(1, sizeof(uic_data) + 1);
1816         if (pUic_data == NULL) {
1817                 _DEBUG_VERBOSE("alloc fail");
1818                 _INNER_FUNC_EXIT;
1819                 return COMMON_ERR_ALLOC;
1820         }
1821         pUic_data->pUic_option = (uic_option *) calloc(1, sizeof(uic_option) + 1);
1822         if ((pUic_data->pUic_option) == NULL) {
1823                 _DEBUG_VERBOSE("alloc fail");
1824                 free_uic_value(&pUic_data);
1825                 _INNER_FUNC_EXIT;
1826                 return COMMON_ERR_ALLOC;
1827         }
1828         pUic_data->res_data = (uic_res_data *) calloc(1, sizeof(uic_res_data) + 1);
1829         //uic_option *uic_value = (uic_option *)calloc(1,sizeof(uic_option));
1830         if ((pUic_data->res_data) == NULL) {
1831                 _DEBUG_VERBOSE("alloc fail");
1832                 free_uic_value(&pUic_data);
1833                 _INNER_FUNC_EXIT;
1834                 return COMMON_ERR_ALLOC;
1835         }
1836         switch (type) {
1837         case DM_ALERT_DISPLAY:
1838                 _DEBUG_VERBOSE("alert display ui");
1839                 for (iter = alertItems, i = 0; iter != NULL; iter = g_list_next(iter), ++i) {
1840                         pItem = iter->data;
1841                         if (i == 0 && size != 1) {
1842                                 /*user interaction option */
1843                                 _DEBUG_VERBOSE("uic opation : %s", pItem->private.data);
1844                                 ret = exec_userinteration_option(pItem->private.data, &(pUic_data->pUic_option));
1845                         } else {
1846                                 if (pItem->private.data != NULL) {
1847                                         /*user interaction execute */
1848                                         _DEBUG_VERBOSE("uic display : %s", pItem->private.data);
1849                                         pUic_data->display = strdup(pItem->private.data);
1850                                         ret = ui_display(pUic_data);
1851                                         *result_status = pUic_data->res_data->status;
1852                                 }
1853                         }
1854                 }
1855                 break;
1856         case DM_ALERT_CONFIRM_OR_REJECT:
1857                 _DEBUG_VERBOSE("alert confirm ui");
1858                 _DEBUG_VERBOSE("alert confirm alert item size : %d", size);
1859                 for (iter = alertItems, i = 0; iter != NULL; iter = g_list_next(iter), ++i) {
1860                         pItem = iter->data;
1861                         if (i == 0 && size != 1) {
1862                                 /*user interaction option */
1863                                 ret = exec_userinteration_option(pItem->private.data, &(pUic_data->pUic_option));
1864                         } else {
1865                                 /*user interaction execute */
1866                                 _DEBUG_VERBOSE("uic display : %s", pItem->private.data);
1867                                 pUic_data->display = strdup(pItem->private.data);
1868                                 ret = ui_confirm_or_reject(pUic_data);
1869                                 *result_status = pUic_data->res_data->status;
1870                         }
1871                 }
1872                 break;
1873         case DM_ALERT_TEXT_INPUT:
1874                 _DEBUG_VERBOSE("alert input ui");
1875                 for (iter = alertItems, i = 0; iter != NULL; iter = g_list_next(iter), ++i) {
1876                         pItem = iter->data;
1877                         if (i == 0 && size != 1) {
1878                                 /*user interaction option */
1879                                 /*IT ( A , N, D, T , P , I )
1880                                  *ET (T , P )
1881                                  */
1882                                 ret = exec_userinteration_option(pItem->private.data, &(pUic_data->pUic_option));
1883                         } else {
1884                                 /*user interaction execute */
1885                                 _DEBUG_VERBOSE("uic display : %s", pItem->private.data);
1886                                 pUic_data->display = strdup(pItem->private.data);
1887                                 ret = ui_text_input(pUic_data, &result_data);
1888                                 *result_status = pUic_data->res_data->status;
1889                                 if (pUic_data->res_data->status == DM_OK) {
1890                                         (*response) = g_list_append((*response), result_data);
1891                                 } else {
1892                                         _DEBUG_VERBOSE("input text null");
1893                                 }
1894                         }
1895                 }
1896
1897                 break;
1898         case DM_ALERT_SINGLE_CHOICE:{
1899                         _DEBUG_VERBOSE("alert single choice ui");
1900                         for (iter = alertItems, i = 0; iter != NULL; iter = g_list_next(iter), ++i) {
1901                                 pItem = iter->data;
1902                                 if (i == 0 && size != 1) {
1903                                         /*user interaction option */
1904                                         ret = exec_userinteration_option(pItem->private.data, &(pUic_data->pUic_option));
1905                                 } else {
1906                                         /*user interaction execute */
1907                                         if (i == 1) {
1908                                                 _DEBUG_VERBOSE("uic display : %s", pItem->private.data);
1909                                                 pUic_data->display = strdup(pItem->private.data);
1910                                         } else {
1911                                                 GList *iter_end = NULL;
1912                                                 iter_end = g_list_next(iter);
1913                                                 char *data = NULL;
1914                                                 data = strdup(pItem->private.data);
1915                                                 pUic_data->multi_data = g_list_append(pUic_data->multi_data, data);
1916
1917                                                 if (iter_end == NULL) {
1918                                                         ret = ui_single_choice(pUic_data, response);
1919                                                         *result_status = pUic_data->res_data->status;
1920                                                         break;
1921                                                 }
1922                                         }
1923                                 }
1924                         }
1925                 }
1926                 break;
1927         case DM_ALERT_MULTIPLE_CHOICE:{
1928                         _DEBUG_VERBOSE("alert single/multiple choice ui");
1929                         for (iter = alertItems, i = 0; iter != NULL; iter = g_list_next(iter), ++i) {
1930                                 pItem = iter->data;
1931                                 if (i == 0 && size != 1) {
1932                                         /*user interaction option */
1933                                         if (pItem->private.data != NULL)
1934                                                 ret = exec_userinteration_option(pItem->private.data, &(pUic_data->pUic_option));
1935                                 } else {
1936                                         /*user interaction execute */
1937                                         if (i == 1) {
1938                                                 _DEBUG_VERBOSE("uic display : %s", pItem->private.data);
1939                                                 pUic_data->display = strdup(pItem->private.data);
1940                                         } else {
1941                                                 GList *iter_end = NULL;
1942                                                 iter_end = g_list_next(iter);
1943                                                 char *data = NULL;
1944                                                 data = strdup(pItem->private.data);
1945                                                 pUic_data->multi_data = g_list_append(pUic_data->multi_data, data);
1946
1947                                                 if (iter_end == NULL) {
1948                                                         ret = ui_multiple_choice(pUic_data, response);
1949                                                         *result_status = pUic_data->res_data->status;
1950                                                         break;
1951                                                 }
1952                                         }
1953                                 }
1954                         }
1955                 }
1956                 break;
1957         case ALERT_UNKNOWN:
1958         case DM_ALERT_SERVER_INITIATED_MGMT:
1959         case DM_ALERT_CLIENT_INITIATED_MGMT:
1960         case DM_ALERT_NEXT_MESSAGE:
1961         case DM_ALERT_SESSION_ABORT:
1962         case DM_ALERT_CLIENT_EVENT:
1963         case DM_ALERT_NO_END_OF_DATA:
1964         case DM_ALERT_GENERIC_ALERT:
1965                 _DEBUG_VERBOSE("not user interaction type");
1966                 break;
1967         default:
1968                 break;
1969         }
1970
1971         /*
1972            session->status
1973            for(iter = responseItems; iter != NULL; iter = g_list_next(iter)){
1974            pItem = iter->data;
1975            temp->items = g_list_append(temp->items, pItem);
1976            }
1977          */
1978         free_uic_value(&pUic_data);
1979
1980         _DEBUG_VERBOSE(" end\n");
1981         _INNER_FUNC_EXIT;
1982         return ret;
1983
1984 }
1985
1986 DM_ERROR add_response_hmacinfo(Session ** session, char *temp_hmac)
1987 {
1988         _EXTERN_FUNC_ENTER;
1989         DM_ERROR ret = DM_OK;
1990         /*x-syncml-hmac: algorithm=MD5, username="156354238", mac=hvMeISwxSVUEofF2jHhQjA== */
1991
1992         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
1993         retvm_if((temp_hmac) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "hmac is NULL!!");
1994
1995         char *start = NULL;
1996         char *end = NULL;
1997
1998         if ((*session)->resphmacinfo == NULL) {
1999                 _DEBUG_INFO("hmac response calloc");
2000                 (*session)->resphmacinfo = (Hmac *) calloc(1, sizeof(Hmac) + 1);
2001                 if ((*session)->resphmacinfo == NULL) {
2002                         return COMMON_ERR_ALLOC;
2003                 }
2004         }
2005
2006         _DEBUG_INFO("temp_hmac : %s", temp_hmac);
2007         start = strstr(temp_hmac, "=");
2008         if (start == NULL) {
2009                 _DEBUG_INFO("auth type null");
2010                 return COMMON_ERR_INTERNAL_NOT_DEFINED;
2011         }
2012         _DEBUG_INFO("hmac : %s", start);
2013
2014         end = strstr(temp_hmac, ",");
2015         if (end == NULL) {
2016                 _DEBUG_INFO("auth type null");
2017                 return COMMON_ERR_INTERNAL_NOT_DEFINED;
2018         }
2019         _DEBUG_INFO("hmac : %s", end);
2020
2021         _set_hmac_value(&((*session)->resphmacinfo->authtype), start + 1, (end - start - 1));
2022         if ((*session)->resphmacinfo->authtype == NULL) {
2023                 _DEBUG_INFO("alloc fail");
2024                 _EXTERN_FUNC_EXIT;
2025                 return COMMON_ERR_ALLOC;
2026         }
2027         _DEBUG_INFO("auty type : %s", (*session)->resphmacinfo->authtype);
2028
2029         start = strstr(end, "=");
2030         if (start == NULL) {
2031                 _DEBUG_INFO("username null");
2032                 return COMMON_ERR_INTERNAL_NOT_DEFINED;
2033         }
2034         _DEBUG_INFO("hmac : %s", start);
2035
2036         end = strstr(start, ",");
2037         if (end == NULL) {
2038                 _DEBUG_INFO("username null");
2039                 return COMMON_ERR_INTERNAL_NOT_DEFINED;
2040         }
2041         _DEBUG_INFO("hmac : %s", end);
2042
2043         _set_hmac_value(&((*session)->resphmacinfo->username), start + 2, (end - start - 3));
2044         if ((*session)->resphmacinfo->username == NULL) {
2045                 _DEBUG_INFO("alloc fail");
2046                 _EXTERN_FUNC_EXIT;
2047                 return COMMON_ERR_ALLOC;
2048         }
2049         _DEBUG_INFO("username : %s", (*session)->resphmacinfo->username);
2050
2051         start = strstr(end, "=");
2052         if (start == NULL) {
2053                 _DEBUG_INFO("mac null");
2054                 return COMMON_ERR_INTERNAL_NOT_DEFINED;
2055         }
2056         _DEBUG_INFO("hmac : %s", start);
2057         (*session)->resphmacinfo->mac = strdup(start + 1);
2058         _DEBUG_INFO("mac : %s", (*session)->resphmacinfo->mac);
2059
2060         _EXTERN_FUNC_EXIT;
2061         return ret;
2062 }
2063
2064 static void _set_hmac_value(char **key, char *value, int size)
2065 {
2066         _INNER_FUNC_ENTER;
2067
2068         retm_if((value) == NULL, "value is NULL!!");
2069         retm_if((size) == 0, "size is NULL!!");
2070
2071         if (*key != NULL) {
2072                 free(*key);
2073                 *key = NULL;
2074         } else {
2075                 _DEBUG_INFO("key null");
2076         }
2077
2078         (*key) = (char *)calloc(1, size + 1);
2079         if ((*key) == NULL) {
2080                 _INNER_FUNC_EXIT;
2081                 return;
2082         }
2083
2084         strncpy((*key), value, size);
2085
2086         _INNER_FUNC_EXIT;
2087 }
2088
2089 /*DM_ERROR process_commands(Command* pCommand,  int * resultStatus) {
2090
2091         _DEBUG_TRACE( " start\n");
2092
2093         DM_ERROR ret = DM_OK;
2094         switch (pCommand->type) {
2095         case COMMAND_TYPE_UNKNOWN:
2096                 break;
2097         case COMMAND_TYPE_ALERT:
2098                 ret = __process_alert(pCommand, resultStatus);
2099                 break;
2100         case COMMAND_TYPE_ADD:
2101                 break;
2102                 ret = __process_add(pCommand, resultStatus);
2103         case COMMAND_TYPE_REPLACE:
2104                 ret = __process_replace(pCommand, resultStatus);
2105                 break;
2106         case COMMAND_TYPE_DELETE:
2107                 ret = __process_delete(pCommand, resultStatus);
2108                 break;
2109         case COMMAND_TYPE_EXEC:
2110                 ret = __process_exec(pCommand, resultStatus);
2111                 break;
2112         case COMMAND_TYPE_SEQUENCE:
2113                 ret = __process_sequence(pCommand, resultStatus);
2114                 break;
2115         case COMMAND_TYPE_ATOMIC:
2116                 ret = __process_atomic(pCommand, resultStatus);
2117                 break;
2118         case COMMAND_TYPE_COPY:
2119                 break;
2120         default:
2121                 break;
2122         }
2123
2124 return_part:
2125         if (ret != DM_OK)
2126                 goto error;
2127
2128         _DEBUG_TRACE( " end\n");
2129         return ret;
2130 error:
2131         _DEBUG_TRACE( " error end : ret %d\n", ret);
2132         return ret;
2133
2134 }*/
2135
2136 static DM_ERROR __rollback_atomic(Session * session, Command * pCommand)
2137 {
2138         _INNER_FUNC_ENTER;
2139
2140         DM_ERROR ret = DM_OK;
2141         retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
2142         retvm_if((pCommand) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "pCommand is NULL!!");
2143
2144         Command *current_Command = NULL;
2145         /*Status * temp_status = NULL; */
2146         GList *atomic_iter = NULL;
2147         GList *items = NULL;
2148         GList *temp_items = NULL;
2149         Item *item = NULL;
2150         sync_agent_dm_mo_error_e mo_ret = sync_agent_begin_transaction_mo();
2151         _DEBUG_VERBOSE("transaction start : %d", mo_ret);
2152
2153         for (atomic_iter = pCommand->private.sequence_atomic.commands; atomic_iter != NULL; atomic_iter = g_list_next(atomic_iter)) {
2154                 current_Command = (Command *) atomic_iter->data;
2155
2156                 switch (current_Command->type) {
2157                 case COMMAND_TYPE_ALERT:
2158                         items = current_Command->private.alert.items;
2159                         break;
2160                 case COMMAND_TYPE_ADD:
2161                 case COMMAND_TYPE_REPLACE:
2162                 case COMMAND_TYPE_DELETE:
2163                 case COMMAND_TYPE_COPY:
2164                         items = current_Command->private.change.items;
2165                         break;
2166                 case COMMAND_TYPE_GET:
2167                         items = current_Command->private.access.items;
2168                         // delete result status
2169                         break;
2170                 case COMMAND_TYPE_UNKNOWN:
2171                 case COMMAND_TYPE_HEADER:
2172                 case COMMAND_TYPE_RESULTS:
2173                 case COMMAND_TYPE_EXEC:
2174                 case COMMAND_TYPE_SEQUENCE:
2175                 case COMMAND_TYPE_ATOMIC:
2176                         _DEBUG_VERBOSE("not support command in atomic");
2177                         break;
2178                 default:
2179                         break;
2180                 }
2181
2182                 for (temp_items = items; temp_items != NULL; temp_items = g_list_next(temp_items)) {
2183                         item = temp_items->data;
2184
2185                         ret = set_status_data(session, current_Command, item, DM_ERR_ATOMIC_ROLL_BACK_OK);
2186                 }
2187         }
2188
2189         _DEBUG_VERBOSE(" end\n");
2190         _INNER_FUNC_EXIT;
2191         return ret;
2192
2193 }