3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <sync_agent.h>
22 #include "common/dm_common_def.h"
23 #include "common/dm_common.h"
24 #include "common/util/util.h"
25 #include "serviceadapter/dm-phase-handler/dm_phase_handler.h"
26 #include "serviceadapter/protocolbinder/syncml_protocol_binder.h"
27 #include "serviceadapter/sa_define.h"
28 #include "serviceadapter/sa_syncml_binders.h"
29 #include "serviceadapter/sa_elements.h"
30 #include "serviceadapter/sa_elements_internal.h"
31 #include "serviceadapter/sa_command.h"
32 #include "serviceadapter/sa_command_internal.h"
33 #include "serviceadapter/sa_session.h"
34 #include "serviceadapter/sa_session_internal.h"
35 #include "serviceadapter/sa_util.h"
36 #include "serviceadapter/networkbinder/na_binder.h"
37 #include "mo-handler/dm_mo_common.h"
38 #include "mo-handler/dm_mo_handler.h"
39 #include "dm-engine/dm_common_engine.h"
41 #ifndef OMADM_AGENT_LOG
43 #define LOG_TAG "OMA_DM_CONTEXT"
46 #define MAX_SESSION_COUNT 5
49 #define LARGE_OBJECT_SIZE 10000
51 static int file_count = 0;
53 /*static ErrorType get_AccountInfo(int accountId, char **id, char **pwd, char **serverUrl, int *isBase64, char **nextNonce);*/
54 static DM_ERROR __get_dmaccInfo(char *pServer_id, char **client_id, char **client_pwd, char **sourceUrl, char **targetUrl, int *isBase64, char **nextNonce);
55 static DM_ERROR __check_hmac(Session ** session, char *msg, unsigned int msg_size);
56 static DM_ERROR __create_authentication(Session ** session, char *client_id, char *client_pwd, int isBase64, char *nextNonce);
57 static DM_ERROR ___generater_deviceInfo_replacecmd(Command ** pReplaceCommand);
58 static DM_ERROR __create_devinfo_replace_cmd(Session * session, Command ** pCommand);
59 static DM_ERROR _generate_setupphase_msg(Session ** session, char *pServer_id, char *pSession_id, int session_type, char **msg, unsigned int *msg_size);
60 static DM_ERROR _generate_management_msg(Session ** session, char **msg, unsigned int *msg_size);
61 static DM_ERROR _generate_generic_alert(Session ** session, char *pServer_id, char *pSession_id, char **msg, int *msg_size, int service_engine_id, int result);
62 static DM_ERROR _send_msg(char *accType, int transportType, Session ** session, char *server_url, GList * header_info, char *sendMsg, unsigned int sendMsg_length, GList ** recv_header, char **recvMsg, unsigned int *recvMsg_length);
63 static DM_ERROR _recv_msg(Session ** session, char *recvMsg, unsigned int recvMsg_length, ENGINE_ID * service_engine_id, int *isFinish, int isgeneticAlert);
65 static DM_ERROR __get_dmaccInfo(char *pServer_id, char **client_id, char **client_pwd, char **sourceUrl, char **targetUrl, int *isBase64, char **nextNonce)
71 retvm_if((pServer_id) == NULL, COMMON_ERR_IS_NULL, "pServer_id is NULL!!");
73 char *authType = NULL;
74 ret = get_client_dmacc(pServer_id, client_id, client_pwd, sourceUrl, targetUrl, isBase64, nextNonce, &authType);
78 _DEBUG_VERBOSE(" end\n");
82 _DEBUG_VERBOSE(" error end [%d]\n", ret);
87 static DM_ERROR ___generater_deviceInfo_replacecmd(Command ** pReplaceCommand)
95 ret = get_child_mo_list(DM_DEVINFO, &(*pReplaceCommand)->private.change.items);
100 for (iter = (*pReplaceCommand)->private.change.items; iter != NULL; iter = g_list_next(iter)) {
102 pItem->contenttype = g_strdup(ELEMENT_TEXTPLAIN);
103 pItem->format = g_strdup(ELEMENT_CHR);
110 _DEBUG_VERBOSE(" error end\n");
116 static DM_ERROR __create_devinfo_replace_cmd(Session * session, Command ** pCommand)
120 DM_ERROR ret = DM_OK;
121 retvm_if((session) == NULL, COMMON_ERR_IS_NULL, "session is NULL!!");
123 ret = create_replace_cmd(session, pCommand);
127 ret = ___generater_deviceInfo_replacecmd(pCommand);
134 _DEBUG_VERBOSE(" error : %d\n", ret);
140 static DM_ERROR _generate_setupphase_msg(Session ** session, char *pServer_id, char *pSession_id, int session_type, char **msg, unsigned int *msg_size)
144 DM_ERROR ret = DM_OK;
146 retvm_if((pServer_id) == NULL, COMMON_ERR_IS_NULL, "pServer_id is NULL!!");
151 if (pSession_id == NULL)
152 pSession_id = g_strdup_printf("%ld", time(NULL));
154 char *sourceUrl = NULL;
155 char *client_id = NULL;
156 char *client_pwd = NULL;
157 char *targetUrl = NULL;
159 char *nextNonce = NULL;
161 ret = __get_dmaccInfo(pServer_id, &client_id, &client_pwd, &sourceUrl, &targetUrl, &isBase64, &nextNonce);
165 if (nextNonce == NULL) {
166 (nextNonce) = strdup(ELEMENT_DEFAULT_CLIENT_NONCE);
169 if ((*session) == NULL) {
170 ret = create_syncml_session(VERSION_12, PROTOCOL_TYPE_DM, pServer_id, client_id, pSession_id, sourceUrl, targetUrl, session);
174 (*session)->msgID = 0;
176 #ifdef _DM_BUNDANG_TEST
177 /*redirection code */
178 if ((*session)->target->locURI != NULL) {
179 char *replace = NULL;
180 replace = get_new_uri((*session)->target->locURI);
181 str_free(&((*session)->target->locURI));
182 (*session)->target->locURI = g_strdup(replace);
184 _DEBUG_TRACE(" targer url : %s \n", (*session)->target->locURI);
190 _DEBUG_TRACE(" targer url : %s \n", (*session)->target->locURI);
191 ret = __create_authentication(session, client_id, client_pwd, isBase64, nextNonce);
195 _DEBUG_TRACE("targer url : %s \n", (*session)->target->locURI);
197 GList *commands = NULL;
198 GList *commands_last = NULL;
201 SyncHdr *pSyncHdr = NULL;
202 ret = create_syncml_hdr((*session), &pSyncHdr);
209 switch (session_type) {
210 case 0: //IP_PUSH_EVENT
211 case 1: //WAP_PUSH_EVENT
212 syncType = DM_ALERT_SERVER_INITIATED_MGMT;
215 case 3: //SERVER_BOOTSTRAP_EVENT
216 syncType = DM_ALERT_CLIENT_INITIATED_MGMT;
219 syncType = DM_ALERT_CLIENT_INITIATED_MGMT;
223 Command *pAlertCommand = NULL;
224 ret = create_alert_cmd((*session), syncType, &pAlertCommand);
227 putCmdIntoList(&commands, &commands_last, pAlertCommand);
230 Command *pReplaceCommand = NULL;
231 ret = __create_devinfo_replace_cmd((*session), &pReplaceCommand);
234 putCmdIntoList(&commands, &commands_last, pReplaceCommand);
237 SyncML *pSyncML = NULL;
238 ret = create_syncml(pSyncHdr, NULL, commands, isFinal, &pSyncML);
243 ret = syncml_objectbinder(pSyncML, msg, msg_size);
248 /* xml_len = remove_xml_header(xml, xml_len);
249 _DEBUG_TRACE("msg : %s", msg);
250 ret = check_hmac( session, xml, xml_len);*/
253 _DEBUG_TRACE("msg : %s", msg);
254 ret = __check_hmac(session, *msg, *msg_size);
255 _DEBUG_TRACE(" check hmac:%d\n", ret);
257 //free generate structure
258 free_syncml(pSyncML);
261 //session->status, session->alertCommand are already freed in free_SyncML function
262 (*session)->status = NULL;
264 if ((*session)->alertCommand != NULL) {
265 g_list_free((*session)->alertCommand);
266 (*session)->alertCommand = NULL;
269 (*session)->isSendingfinal = isFinal;
276 _DEBUG_TRACE(" error : %d\n", ret);
281 static DM_ERROR _generate_management_msg(Session ** session, char **msg, unsigned int *msg_size)
285 DM_ERROR ret = DM_OK;
287 retvm_if((*session) == NULL, COMMON_ERR_IS_NULL, "session is NULL!!");
289 sync_agent_pb_protocol_binder_function_set_s *pBinder_function_set = NULL;
290 sync_agent_pb_error_e err = init_syncml_binder_function_set(&pBinder_function_set);
292 SyncML *pSyncML = NULL;
293 SyncHdr *pSyncHdr = NULL;
295 GList *commands = NULL;
296 GList *commands_last = NULL;
298 bool cancel_flag = false;
300 GList *status_iter = NULL;
301 Status *pStatus = NULL;
303 Command *pAlertCommand = NULL;
304 GList *alert_iter = NULL;
306 GList *results_iter = NULL;
307 Command *pResultsCommand = NULL;
308 Command *restResultsCommand = NULL;
310 unsigned int estimate_size = 0;
313 GList *results_data_iter = NULL;
314 GList *rest_result_data_iter = NULL;
316 Item *result_item = NULL;
317 Item *rest_result_item = NULL;
318 char *result_data = NULL;
321 sync_agent_pb_protocol_binder_info_s *pBinder = NULL;
322 pBinder = syncml_binder_init(pBinder, SYNC_AGENT_PB_ENCODING_WBXML_12, false, true, pBinder_function_set);
324 #ifdef _DM_BUNDANG_TEST
325 /*redirection code */
326 if ((*session)->target->locURI != NULL) {
327 char *replace = NULL;
328 replace = get_new_uri((*session)->target->locURI);
329 str_free(&((*session)->target->locURI));
330 (*session)->target->locURI = g_strdup(replace);
333 _DEBUG_TRACE(" targer url : %s \n", (*session)->target->locURI);
338 ret = create_syncml_hdr((*session), &pSyncHdr);
341 err = syncml_binder_append(pBinder, PE_SYNCML_START, NULL);
342 err = syncml_binder_append(pBinder, PE_HEADER, pSyncHdr);
343 err = syncml_binder_append(pBinder, PE_BODY_START, NULL);
345 /* ExecuteObject ** responseObj = (ExecuteObject*) pResponseObj;*/
346 for (status_iter = (*session)->status; status_iter != NULL; status_iter = g_list_next(status_iter)) {
347 _DEBUG_TRACE("======================================status command================================\n");
348 pStatus = status_iter->data;
349 err = syncml_binder_append(pBinder, PE_STATUS, pStatus);
352 //if we have to send alert command(1222,1223 etc)
353 cancel_flag = sync_agent_check_cancel_flag();
354 _DEBUG_TRACE("======================================alert command================================\n");
355 _DEBUG_TRACE("======================================%d================================\n", cancel_flag);
357 if (cancel_flag != 0) {
358 ret = create_alert_cmd((*session), DM_ALERT_SESSION_ABORT, &pAlertCommand);
361 putCmdIntoList(&commands, &commands_last, pAlertCommand);
362 err = syncml_binder_append(pBinder, PE_ALERT, pAlertCommand);
365 for (alert_iter = (*session)->alertCommand; alert_iter != NULL; alert_iter = g_list_next(alert_iter)) {
366 _DEBUG_TRACE("======================================alert command================================\n");
367 pAlertCommand = alert_iter->data;
368 putCmdIntoList(&commands, &commands_last, pAlertCommand);
369 err = syncml_binder_append(pBinder, PE_ALERT, pAlertCommand);
372 _DEBUG_TRACE(" server maxsize : %d ", (*session)->targetMaxMsgSize);
373 if ((*session)->resultsCommand != NULL) {
374 _DEBUG_TRACE("result command length : %d ", g_list_length((*session)->resultsCommand));
376 _DEBUG_TRACE("======================================result command================================\n");
377 for (results_iter = (*session)->resultsCommand; results_iter != NULL;) {
378 _DEBUG_TRACE("resultcommand iter");
379 pResultsCommand = results_iter->data;
381 sync_agent_get_stream_size_from_protocol_binder(pBinder, &estimate_size);
382 _DEBUG_TRACE("msg extimate size : %d ", estimate_size);
384 ret = duplicate_command(pResultsCommand, &restResultsCommand);
385 _DEBUG_TRACE("dupicate command : %d", ret);
390 results_iter = g_list_next(results_iter);
392 for (results_data_iter = pResultsCommand->private.results.items, rest_result_data_iter = restResultsCommand->private.results.items; results_data_iter != NULL && rest_result_data_iter != NULL;) {
393 _DEBUG_TRACE("resultcommand item iter");
395 result_item = (Item *) (results_data_iter->data);
396 rest_result_item = (Item *) (rest_result_data_iter->data);
398 result_data = result_item->private.data;
400 results_data_iter = g_list_next(results_data_iter);
401 rest_result_data_iter = g_list_next(rest_result_data_iter);
404 _DEBUG_TRACE("remove item because msg size over");
405 pResultsCommand->private.results.items = g_list_remove(pResultsCommand->private.results.items, result_item);
407 _DEBUG_TRACE("result item add packet");
408 if (result_item->moreData == 1) {
409 _DEBUG_TRACE("item msg cut by largeObject");
414 data = strdup(result_data);
416 str_free(&(result_item->private.data));
417 str_free(&(rest_result_item->private.data));
419 maxsize = (*session)->targetMaxMsgSize - estimate_size - DEFAULT_REST_SIZE;
420 _DEBUG_TRACE("maxsize : %d", maxsize);
422 result_item->private.data = (char *)calloc(1, maxsize + 1);
423 if (result_item->private.data == NULL) {
424 _DEBUG_TRACE("alloc fail");
425 ret = COMMON_ERR_ALLOC;
428 memcpy(result_item->private.data, data, maxsize);
430 _DEBUG_TRACE("rest data size : %d", result_item->size - maxsize);
431 rest_result_item->private.data = (char *)calloc(1, result_item->size - maxsize + 1);
432 if (rest_result_item->private.data == NULL) {
433 _DEBUG_TRACE("alloc fail");
434 ret = COMMON_ERR_ALLOC;
437 memcpy(rest_result_item->private.data, data + maxsize, result_item->size - maxsize);
438 rest_result_item->size = result_item->size - maxsize;
440 if (maxsize > result_item->size - maxsize) {
441 _DEBUG_TRACE("set msg non more data");
442 rest_result_item->moreData = 0;
445 data_size += maxsize;
446 _DEBUG_TRACE("msg(moredata) data size : %d ", data_size);
450 _DEBUG_TRACE("remove item temp list");
451 data_size += result_item->size;
453 _DEBUG_TRACE("msg data size : %d ", data_size);
454 restResultsCommand->private.results.items = g_list_remove(restResultsCommand->private.results.items, rest_result_item);
460 _DEBUG_TRACE(" ESTIMATE SIZE IN RESULTS= %d", estimate_size + data_size + DEFAULT_REST_SIZE);
461 if ((*session)->targetMaxMsgSize >= estimate_size + data_size + DEFAULT_REST_SIZE) {
462 _DEBUG_TRACE("insert result in msg");
463 putCmdIntoList(&commands, &commands_last, pResultsCommand);
464 (*session)->resultsCommand = g_list_remove((*session)->resultsCommand, pResultsCommand);
466 err = syncml_binder_append(pBinder, PE_RESULTS_START, pResultsCommand);
467 err = syncml_binder_append(pBinder, PE_RESULTS_END, NULL);
472 _DEBUG_TRACE("cut result in msg");
475 _DEBUG_TRACE("msg msg over large object");
477 _DEBUG_TRACE("msg cut by MultiMsg");
479 _DEBUG_TRACE("free rest result command");
485 /*large object rest part preapend in resultsCommands list for next msg */
486 if (g_list_length(restResultsCommand->private.results.items) > 0) {
487 (*session)->resultsCommand = g_list_prepend((*session)->resultsCommand, restResultsCommand);
489 _DEBUG_TRACE("msg cut by MultiMsg");
492 free_command(restResultsCommand);
493 restResultsCommand = NULL;
494 _DEBUG_TRACE("free rest result command");
497 _DEBUG_TRACE("added result command");
501 ret = create_syncml(pSyncHdr, (*session)->status, commands, isFinal, &pSyncML);
505 _DEBUG_TRACE("final element : %d", isFinal);
507 err = syncml_binder_append(pBinder, PE_FINAL, NULL);
510 err = syncml_binder_append(pBinder, PE_BODY_END, NULL);
511 err = syncml_binder_append(pBinder, PE_SYNCML_END, NULL);
513 syncml_binder_get_stream(pBinder, msg, msg_size);
516 unsigned int xml_size = 0;
517 err = sync_agent_get_xml_from_protocol_binder(pBinder, &xml, &xml_size);
518 if (err == SYNC_AGENT_PB_RETURN_OK) {
519 fprintf(stderr, " msg from client \n");
520 if (fputs(xml, stderr) == EOF) {
521 _DEBUG_TRACE("debuging test error");
523 set_xml_to_file(xml, OMA_DM_MSG_PATH);
527 syncml_binder_terminate(pBinder);
530 /* xml_len = remove_xml_header(xml, xml_len);
531 _DEBUG_TRACE("msg : %s", msg);
532 ret = check_hmac( session, xml, xml_len);*/
535 ret = __check_hmac(session, *msg, *msg_size);
536 _DEBUG_TRACE(" check hmac:%d\n", ret);
538 free_syncml(pSyncML);
541 //session->status, session->alertCommand are already freed in free_SyncML function
542 (*session)->status = NULL;
543 if ((*session)->alertCommand != NULL) {
544 g_list_free((*session)->alertCommand);
545 (*session)->alertCommand = NULL;
548 g_list_free((*session)->resultsCommand);
549 (*session)->resultsCommand = NULL;
552 (*session)->isSendingfinal = isFinal;
558 free_command(restResultsCommand);
559 restResultsCommand = NULL;
561 free_synchdr(pSyncHdr);
564 free_syncml(pSyncML);
567 _DEBUG_TRACE(" error :%d\n", ret);
572 //1500 -> ERROR_INTERNAL_OK : generatePreSyncMsg has been completed without any error
573 //1512 -> ERROR_INTERNAL_NO_MEMORY : failed to allocate memory
574 //1513 -> ERROR_INTERNAL_NOT_DEFINED : something is not defined that must have
575 //1503 -> ERROR_INTERNAL_BINDER_ERROR : binder error
578 DM_ERROR __check_hmac(Session ** session, char *xml, unsigned int xml_len)
582 DM_ERROR ret = DM_OK;
584 retvm_if((xml) == NULL, COMMON_ERR_IS_NULL, "xml is NULL!!");
585 retvm_if((*session) == NULL, COMMON_ERR_IS_NULL, "session is NULL!!");
587 AuthType auth_type = AUTH_TYPE_UNKNOWN;
588 sync_agent_dm_mo_error_e err_code = SYNC_AGENT_DM_MO_SUCCESS;
589 sync_agent_dm_acc_info_s *acc_info = NULL;
590 sync_agent_dm_mo_acc_item_info_s sync_agent_mo_acc_info_item;
592 sync_agent_mo_acc_info_item.acc_type = DEVICE_MANAGE_ACC_TYPE_CLIENT;
593 sync_agent_mo_acc_info_item.serverid = (*session)->pServer_id;
595 err_code = sync_agent_get_mo_acc_item(&sync_agent_mo_acc_info_item, &acc_info);
596 //acc_info = sync_agent_get_acc_info(pServer_id, DEVICE_MANAGE_ACC_TYPE_CLIENT);
597 if (err_code != SYNC_AGENT_DM_MO_SUCCESS || acc_info == NULL) {
598 _DEBUG_VERBOSE(" acc null!!\n");
599 ret = COMMON_ERR_IS_NULL;
603 _DEBUG_VERBOSE(" id = %s\n", acc_info->auth_name);
604 _DEBUG_VERBOSE(" pwd = %s\n", acc_info->auth_secret);
605 _DEBUG_VERBOSE(" serverUrl = %s\n", acc_info->addr);
606 _DEBUG_VERBOSE(" nextNonce = %s\n", acc_info->auth_data);
608 if ((acc_info->auth_name) == NULL || (acc_info->auth_secret) == NULL || (acc_info->addr) == NULL) {
609 ret = COMMON_ERR_IS_NULL;
613 auth_type = __get_Session_AuthType_enum(acc_info->auth_type);
614 if (auth_type == AUTH_TYPE_HMAC) {
616 _DEBUG_VERBOSE("auth name : %s", acc_info->auth_name);
617 _DEBUG_VERBOSE("auth secret : %s", acc_info->auth_secret);
618 _DEBUG_VERBOSE("auth data : %s", acc_info->auth_data);
619 /* _DEBUG_VERBOSE("xml : %s", xml);
620 _DEBUG_VERBOSE("xml_len : %d", xml_len);*/
622 unsigned char *nonce = NULL;
623 unsigned int nonce_size = 0;
624 ret = nonce_decode(acc_info->auth_data, &nonce, &nonce_size);
625 _DEBUG_VERBOSE("nonce result : %d", ret);
626 _DEBUG_VERBOSE("nonce : %s, noncesize : %d", nonce, nonce_size);
629 ret = gemerate_hmac(acc_info->auth_name, acc_info->auth_secret, nonce, nonce_size, xml, xml_len, &mac);
632 _DEBUG_VERBOSE("mac : %s", mac);
634 ret = create_hmac(acc_info->auth_name, "MD5", mac, session);
639 _DEBUG_VERBOSE(" auth type : [%d] \n", auth_type);
642 sync_agent_free_mo_acc_item(&acc_info);
648 sync_agent_free_mo_acc_item(&acc_info);
651 _DEBUG_VERBOSE(" end : %d", ret);
655 static DM_ERROR __create_authentication(Session ** session, char *client_id, char *client_pwd, int isBase64, char *nextNonce)
659 DM_ERROR ret = DM_OK;
661 retvm_if((*session) == NULL, COMMON_ERR_IS_NULL, "session is NULL!!");
662 retvm_if((client_id) == NULL, COMMON_ERR_IS_NULL, "client_id is NULL!!");
665 Chal *chal = (*session)->res_chal;
666 FormatType formatType = FORMAT_TYPE_UNKNOWN;
667 AuthType pAuthType = AUTH_TYPE_UNKNOWN;
668 char *credData = NULL;
670 ret = get_dmacc_authType((*session)->pServer_id, &pAuthType);
673 _DEBUG_VERBOSE(" pAuthType : [%d] \n", pAuthType);
676 _DEBUG_VERBOSE("CHAL DOES NOT EXIST\n");
678 unsigned int out_len = 0;
679 unsigned char *nextNonceDecode = NULL;
682 case AUTH_TYPE_BASIC:{
683 formatType = FORMAT_TYPE_BASE64;
690 formatType = FORMAT_TYPE_BASE64;
692 formatType = FORMAT_TYPE_UNKNOWN;
694 if (nextNonce != NULL) {
695 _DEBUG_VERBOSE("nextnonce : %s", nextNonce);
696 ret = nonce_decode(nextNonce, &nextNonceDecode, &out_len);
697 _DEBUG_VERBOSE("nonce result : %d", ret);
698 _DEBUG_VERBOSE("nextnoncedecode : %s out len : %d", nextNonceDecode, out_len);
700 pAuthType = AUTH_TYPE_BASIC;
701 formatType = FORMAT_TYPE_BASE64;
703 if (pAuthType == AUTH_TYPE_HMAC) {
704 (*session)->isHMAC = 1;
712 _DEBUG_VERBOSE(" client id : %s , client pwd : %s\n", client_id, client_pwd);
714 ret = create_auth_credString(pAuthType, client_id, client_pwd, nextNonceDecode, out_len, &credData);
718 ret = create_auth_cred(client_id, client_pwd, pAuthType, formatType, credData, &pCred);
723 _DEBUG_VERBOSE("CHAL DOES EXIST\n");
725 unsigned int out_len = 0;
726 unsigned char *nextNonceDecode = NULL;
728 if ((*session)->res_chal->type == AUTH_TYPE_HMAC) {
729 (*session)->isHMAC = 1;
732 (*session)->isHMAC = 0;
735 if (chal->nonce_plain != NULL) {
736 _DEBUG_VERBOSE("chal->nonce_plain : %s", chal->nonce_plain);
737 ret = nonce_decode(chal->nonce_plain, &nextNonceDecode, &out_len);
738 _DEBUG_VERBOSE("nonce result : %d", ret);
739 _DEBUG_VERBOSE("nextnonceDecode : %s, out len : %d", nextNonceDecode, out_len);
743 ret = create_auth_credString(chal->type, client_id, client_pwd, chal->type == AUTH_TYPE_BASIC ? (const unsigned char *)(chal->nonce_b64) : (const unsigned char *)(nextNonceDecode), out_len, &credData);
747 ret = create_auth_cred(client_id, client_pwd, chal->type, chal->format, credData, &pCred);
753 ret = set_session_authcred((*session), pCred);
760 _DEBUG_VERBOSE(" end error %d \n", ret);
765 static DM_ERROR _generate_generic_alert(Session ** session, char *pServer_id, char *pSession_id, char **msg, int *msg_size, int service_engine_id, int result)
769 DM_ERROR ret = DM_OK;
770 engine_status *service_status = NULL;
773 retvm_if((pServer_id) == NULL, COMMON_ERR_IS_NULL, "pServer_id is NULL!!");
775 char *sourceUrl = NULL;
776 char *client_id = NULL;
777 char *client_pwd = NULL;
778 char *targetUrl = NULL;
780 char *nextNonce = NULL;
781 SyncHdr *pSyncHdr = NULL;
782 Command *pAlertCommand = NULL;
784 char *contenttype = NULL;
785 Command *pReplaceCommand = NULL;
787 char *pCorrelator = NULL;
788 char *rSourceUrl = NULL;
792 if ((*session) != NULL) {
793 _DEBUG_TRACE("session is Exist");
794 if ((*session)->sessionID != NULL) {
795 _DEBUG_TRACE("session id : %s", (*session)->sessionID);
796 pSession_id = (*session)->sessionID;
798 } else if (pSession_id == NULL) {
799 pSession_id = g_strdup_printf("%ld", time(NULL));
802 ret = __get_dmaccInfo(pServer_id, &client_id, &client_pwd, &sourceUrl, &targetUrl, &isBase64, &nextNonce);
807 if (nextNonce == NULL) {
808 (nextNonce) = strdup(ELEMENT_DEFAULT_CLIENT_NONCE);
811 _DEBUG_TRACE(" id = %s\n", client_id);
812 _DEBUG_TRACE(" pwd = %s\n", client_pwd);
813 _DEBUG_TRACE(" Session_id = %s\n", pSession_id);
814 _DEBUG_TRACE(" sourceUrl = %s\n", sourceUrl);
815 _DEBUG_TRACE(" serverUrl = %s\n", targetUrl);
816 _DEBUG_TRACE(" nextNonce = %s\n", nextNonce);
818 if ((*session) == NULL) {
819 ret = create_syncml_session(VERSION_12, PROTOCOL_TYPE_DM, pServer_id, client_id, pSession_id, sourceUrl, targetUrl, session);
823 (*session)->msgID = 0;
825 #ifdef _DM_BUNDANG_TEST
826 /*redirection code */
827 if ((*session)->target->locURI != NULL) {
828 char *replace = NULL;
829 replace = get_new_uri((*session)->target->locURI);
830 str_free(&((*session)->target->locURI));
831 (*session)->target->locURI = g_strdup(replace);
833 _DEBUG_TRACE(" targer url : %s \n", (*session)->target->locURI);
837 //nextNonce = g_nextnonce;
838 //nextNonce = strdup( (*session)->res_chal->nonce_plain);
839 _DEBUG_TRACE(" NextNonce = %s\n", nextNonce);
843 ret = __create_authentication(session, client_id, client_pwd, isBase64, nextNonce);
849 ret = create_syncml_hdr((*session), &pSyncHdr);
853 GList *commands = NULL;
854 GList *commands_last = NULL;
857 ret = create_alert_cmd((*session), DM_ALERT_CLIENT_INITIATED_MGMT, &pAlertCommand);
860 putCmdIntoList(&commands, &commands_last, pAlertCommand);
863 pAlertCommand = NULL;
864 ret = create_alert_cmd((*session), DM_ALERT_GENERIC_ALERT, &pAlertCommand);
868 _DEBUG_TRACE(" service engine status = [%d]\n", service_engine_id);
870 ret = Get_Engine_Status(service_engine_id, &service_status);
875 _DEBUG_TRACE("STATUS VALUE : %d ", service_status->engine_status);
876 _DEBUG_TRACE("generic alert status data : %d ", result);
878 if (service_status->correlator != NULL) {
879 pCorrelator = g_strdup(service_status->correlator);
881 if (service_status->mo_path != NULL) {
882 rSourceUrl = g_strdup(service_status->mo_path);
884 if (rSourceUrl == NULL) {
885 ret = COMMON_ERR_IS_NULL;
889 SERVICE_SERVER_TYPE engine_type = get_service_type(rSourceUrl);
890 switch (engine_type) {
891 case SAMSUNG_FUMO_TYPE:
893 //format = strdup("chr");//NULL;
894 //contenttype = strdup("text/plain");//NULL;
895 contenttype = strdup("org.openmobilealliance.dm.firmwareupdate.downloadandupdate");
897 case SAMSUNG_FMM_TYPE:
898 contenttype = strdup("urn:oma:at:lawmo:1.0:OperationComplete");
903 //ret = get_mo_node_format_contenttype(rSourceUrl ,&format, &contenttype );
905 data = g_strdup_printf("%d", result);
906 unsigned int size = 0;
907 unsigned int moredata = 0;
908 _DEBUG_TRACE("generic alert status data : %s ", data);
911 ret = set_correlator(&pAlertCommand, pCorrelator);
913 ret = construct_Item(rSourceUrl, format, contenttype, data, size, moredata, &pItem);
914 pAlertCommand->private.alert.items = g_list_append(pAlertCommand->private.alert.items, pItem);
915 putCmdIntoList(&commands, &commands_last, pAlertCommand);
918 ret = __create_devinfo_replace_cmd((*session), &pReplaceCommand);
921 putCmdIntoList(&commands, &commands_last, pReplaceCommand);
924 SyncML *pSyncML = NULL;
925 ret = create_syncml(pSyncHdr, NULL, commands, isFinal, &pSyncML);
929 (*session)->isSendingfinal = isFinal;
932 ret = syncml_objectbinder(pSyncML, msg, (unsigned int *)msg_size);
937 /* xml_len = remove_xml_header(xml, xml_len);
938 _DEBUG_TRACE("msg : %s", msg);
939 ret = check_hmac( session, xml, xml_len);*/
942 _DEBUG_TRACE("msg : %s", msg);
943 ret = __check_hmac(session, *msg, *msg_size);
945 //free generate structure
946 free_syncml(pSyncML);
949 //session->status, session->alertCommand are already freed in free_SyncML function
950 if ((*session)->resultsCommand != NULL) {
951 g_list_free((*session)->resultsCommand);
952 (*session)->resultsCommand = NULL;
955 if ((*session)->alertCommand != NULL) {
956 g_list_free((*session)->alertCommand);
957 (*session)->alertCommand = NULL;
960 (*session)->status = NULL;
961 (*session)->isSendingfinal = isFinal;
963 Free_Memory_Engine_Status(&service_status, 1);
965 str_free(&pCorrelator);
966 str_free(&rSourceUrl);
973 if (service_status != NULL)
974 Free_Memory_Engine_Status(&service_status, 1);
976 str_free(&pCorrelator);
977 str_free(&rSourceUrl);
980 _DEBUG_TRACE(" error : %d\n", ret);
985 DM_ERROR send_msg(char *accType, int transportType, Session ** session, GList * header_info, char *sendMsg, unsigned int sendMsg_length, GList ** recv_header, char **recvMsg, unsigned int *recvMsg_length)
988 DM_ERROR ret = DM_OK;
990 retvm_if((accType) == NULL, COMMON_ERR_IS_NULL, "accType is NULL!!");
991 retvm_if((*session) == NULL, COMMON_ERR_IS_NULL, "session is NULL!!");
992 retvm_if((sendMsg) == NULL, COMMON_ERR_IS_NULL, "sendMsg is NULL!!");
994 ret = _send_msg(accType, transportType, session, (*session)->target->locURI, header_info, sendMsg, sendMsg_length, recv_header, recvMsg, recvMsg_length);
1002 _DEBUG_INFO(" error : %d\n", ret);
1008 DM_ERROR recv_msg(Session ** session, char *recvMsg, unsigned int recvMsg_length, ENGINE_ID * service_engine_id, int *isFinish, int isgeneticAlert)
1012 DM_ERROR ret = DM_OK;
1014 retvm_if((*session) == NULL, COMMON_ERR_IS_NULL, "session is NULL!!");
1015 retvm_if((recvMsg) == NULL, COMMON_ERR_IS_NULL, "recvMsg is NULL!!");
1017 ret = _recv_msg(session, recvMsg, recvMsg_length, service_engine_id, isFinish, isgeneticAlert);
1025 _DEBUG_INFO(" error : %d\n", ret);
1030 DM_ERROR _send_msg(char *accType, int transportType, Session ** session, char *server_url, GList * header_info, char *sendMsg, unsigned int sendMsg_length, GList ** recv_header, char **recvMsg, unsigned int *recvMsg_length)
1034 sync_agent_na_result_e res = SYNC_AGENT_NA_SUCCESS;
1035 DM_ERROR ret = DM_OK;
1037 retvm_if((*session) == NULL, COMMON_ERR_IS_NULL, "session is NULL!!");
1038 retvm_if((accType) == NULL, COMMON_ERR_IS_NULL, "accType is NULL!!");
1039 retvm_if((server_url) == NULL, COMMON_ERR_IS_NULL, "server_url is NULL!!");
1040 retvm_if((sendMsg) == NULL, COMMON_ERR_IS_NULL, "sendMsg is NULL!!");
1042 int net_session_id = 0;
1045 _DEBUG_TRACE("server url : %s", server_url);
1046 //timeout : 30 seconds
1047 res = sync_agent_open_connection(transportType, 30, (unsigned int *)(&(net_session_id)));
1049 _DEBUG_TRACE("res =%d\n", res);
1051 if (res != SYNC_AGENT_NA_SUCCESS) {
1052 ret = COMMON_ERR_INTERNAL_CONNECTION_ERROR;
1056 sync_agent_register_cancel_callback(network_cancel_callback, &(net_session_id));
1058 //check cancel flag (note that this can be called every where inside processing logic)
1059 cancel_flag = sync_agent_check_cancel_flag();
1061 ret = DM_ERR_USER_CANDELLED;
1062 sync_agent_unregister_cancel_callback(network_cancel_callback, &(net_session_id));
1067 if ((*session)->reqhmacinfo != NULL) {
1068 err = NA_http_header_binder(accType, server_url, (*session)->jsessionid, (*session)->isHMAC, (*session)->reqhmacinfo->username, (*session)->reqhmacinfo->mac, &header_info);
1070 err = NA_http_header_binder(accType, server_url, (*session)->jsessionid, (*session)->isHMAC, NULL, NULL, &header_info);
1074 _DEBUG_TRACE("header binding error : %d", err);
1075 ret = DM_ERR_USER_CANDELLED;
1076 sync_agent_unregister_cancel_callback(network_cancel_callback, &(net_session_id));
1080 res = sync_agent_send_msg( /*acc_info (id, pw, uri, ...), */
1081 header_info, transportType, sendMsg, sendMsg_length, recv_header, (unsigned char **)recvMsg, recvMsg_length, SYNC_AGENT_NA_SEND_TYPE_SEND_N_RECEIVE, net_session_id);
1082 _DEBUG_TRACE(" res =%d\n", res);
1084 sync_agent_unregister_cancel_callback(network_cancel_callback, &(net_session_id));
1085 if (res != SYNC_AGENT_NA_SUCCESS) {
1086 ret = COMMON_ERR_INTERNAL_CONNECTION_ERROR;
1089 cancel_flag = sync_agent_check_cancel_flag();
1091 ret = DM_ERR_USER_CANDELLED;
1096 //_DEBUG_TRACE("recieve http header : %s ", (*recv_header));
1098 _DEBUG_TRACE(" CKECK JSESSION\n");
1099 char *temp_jsessionid = NULL;
1100 char *real_jsessionid = NULL;
1101 char *real_other_cookie = NULL;
1102 res = sync_agent_get_header_info(1, (*recv_header), "Set-Cookie", &temp_jsessionid);
1103 if (res == SYNC_AGENT_NA_SUCCESS) {
1104 ret = add_jsession_in_httpheader(*recv_header, temp_jsessionid, &real_jsessionid, &real_other_cookie);
1106 if (real_jsessionid != NULL && (*session)->jsessionid != NULL) {
1107 if (strcmp((*session)->jsessionid, real_jsessionid) != 0) {
1108 str_free(&((*session)->jsessionid));
1109 (*session)->jsessionid = strdup(real_jsessionid);
1111 str_free(&real_jsessionid);
1114 (*session)->jsessionid = strdup(real_jsessionid);
1117 _DEBUG_TRACE("-------------------------------------------------------------------------------------------------------------------------------------------------");
1118 _DEBUG_TRACE("jsession id = %s\n", (*session)->jsessionid);
1119 _DEBUG_TRACE("-------------------------------------------------------------------------------------------------------------------------------------------------");
1124 char *temp_hmac = NULL;
1125 res = sync_agent_get_header_info(1, (*recv_header), HTTP_X_SYNCML_HMAC, &temp_hmac);
1126 if (res == SYNC_AGENT_NA_SUCCESS) {
1127 if (temp_hmac != NULL) {
1128 ret = add_response_hmacinfo(session, temp_hmac);
1130 res = sync_agent_close_connection(transportType, net_session_id);
1131 _DEBUG_TRACE(" %d res =%d\n", __LINE__, res);
1137 res = sync_agent_close_connection(transportType, net_session_id);
1139 _DEBUG_TRACE("res =%d\n", res);
1140 if (res != SYNC_AGENT_NA_SUCCESS) {
1141 ret = COMMON_ERR_INTERNAL_CONNECTION_ERROR;
1145 if (sendMsg != NULL)
1152 _DEBUG_TRACE(" returnerror = %d\n", ret);
1154 res = sync_agent_close_connection(transportType, net_session_id);
1155 _DEBUG_TRACE(" %d res =%d\n", __LINE__, res);
1156 if (res != SYNC_AGENT_NA_SUCCESS) {
1157 ret = COMMON_ERR_INTERNAL_CONNECTION_ERROR;
1161 if (sendMsg != NULL)
1167 _DEBUG_TRACE(" error = %d\n", ret);
1169 if (sendMsg != NULL)
1176 static DM_ERROR _recv_msg(Session ** session, char *recvMsg, unsigned int recvMsg_length, ENGINE_ID * service_engine_id, int *isFinish, int isgeneticAlert)
1180 DM_ERROR ret = DM_OK;
1182 retvm_if((*session) == NULL, COMMON_ERR_IS_NULL, "session is NULL!!");
1183 retvm_if((recvMsg) == NULL, COMMON_ERR_IS_NULL, "recvMsg is NULL!!");
1185 reset_cmdid_session((*session));
1187 SyncML *pSyncML = (SyncML *) calloc(1, sizeof(SyncML));
1188 if (pSyncML == NULL) {
1189 _DEBUG_TRACE("alloc fail");
1190 ret = COMMON_ERR_ALLOC;
1194 unsigned int xml_len = 0;
1195 ret = reverse_syncml_objectbinder(&pSyncML, recvMsg, recvMsg_length, &xml, &xml_len);
1199 /* check receive msg */
1200 ret = receive_header((*session), pSyncML->hdr);
1204 ret = receive_statuses((*session), pSyncML->status);
1209 /* check that pending list is empty */
1211 for (o = (*session)->pendingStatus; o != NULL; o = g_list_next(o)) {
1212 PendingStatus *pending = o->data;
1213 _DEBUG_TRACE(" in pendingStatus List msgID : %d, cmdID : %d\n", pending->msgID, pending->cmdID);
1216 ret = receive_cmd((*session), pSyncML->commands);
1221 if ((*session)->service_engine_id != NO_SERVICE_ENGINE) {
1222 *service_engine_id = (*session)->service_engine_id;
1225 if (pSyncML->final != 0) {
1226 (*session)->isSendingfinal = 0;
1227 (*session)->isReceivingFinal = 1;
1229 (*session)->isReceivingFinal = 0;
1232 _DEBUG_TRACE("receive final : %d , isgeneticAlert : %d ", (*session)->isReceivingFinal, isgeneticAlert);
1234 if (*session != NULL && (*session)->isReceivingFinal != 0 && ((*session)->status != NULL && g_list_length((*session)->status) == 1 && (*session)->resultsCommand == NULL && isgeneticAlert == 0)) {
1236 _DEBUG_TRACE("---------------------------------------------------------------------------recv msg end------------------------------------------------------------------------\n");
1237 _DEBUG_TRACE("---------------------------------------------------------------------------free session------------------------------------------------------------------------\n");
1238 free_session((*session));
1241 _DEBUG_TRACE("not final");
1244 if (recvMsg != NULL)
1246 if (pSyncML != NULL)
1247 free_syncml(pSyncML);
1253 _DEBUG_TRACE(" error : %d\n", ret);
1254 if (recvMsg != NULL)
1256 if (pSyncML != NULL)
1257 free_syncml(pSyncML);
1264 DM_ERROR setup_phase(int transportType, Session ** session, char *pServer_id, char *pSession_id, int session_type, ENGINE_ID * service_engine_id, int *isFinish, int isgenericAlert)
1268 DM_ERROR ret = DM_OK;
1269 retvm_if((pServer_id) == NULL, COMMON_ERR_IS_NULL, "pServer_id is NULL!!");
1271 /*generate setupPhase Msg */
1273 unsigned int msg_size = 0;
1276 char *recv_msg = NULL;
1277 int recv_msg_size = 0;
1279 GList *recv_header = 0;
1283 while (count++ != MAX_SESSION_COUNT) {
1284 //when errortype is ERROR_AUTH_REQUIRED try request using res_chal just one more time
1285 //it can be only happend when server required MD5 authentication(we send basic authentication every time by default)
1288 ret = _generate_setupphase_msg(session, pServer_id, pSession_id, session_type, &msg, &msg_size);
1293 char file_name[1000];
1295 snprintf(file_name, (int)sizeof(file_name), "/opt/data/oma-dm-cfg/packet/wbxml_send_msg_%d.wbxml", file_count);
1296 _DEBUG_INFO("file name : %s", file_name);
1297 sync_agent_write_whole_file(file_name, msg, msg_size, true);
1301 ret = _send_msg("OMA_DM", 1, session, (*session)->target->locURI, list, msg, msg_size, &recv_header, &recv_msg, (unsigned int *)(&recv_msg_size));
1305 snprintf(file_name, (int)sizeof(file_name), "/opt/data/oma-dm-cfg/packet/wbxml_receivce_msg_%d.wbxml", file_count);
1306 _DEBUG_INFO("file name : %s", file_name);
1307 sync_agent_write_whole_file(file_name, recv_msg, recv_msg_size, true);
1309 ret = _recv_msg(session, recv_msg, recv_msg_size, service_engine_id, isFinish, isgenericAlert);
1310 if (ret != DM_ERR_UNAUTHORIZED && ret != DM_ERR_AUTHENTICATION_REQUIRED) {
1311 _DEBUG_INFO(" authentication : %d\n ", ret);
1315 _DEBUG_INFO("revceive end ======================");
1319 if (ret != DM_OK || (*session != NULL && (*session)->status != NULL && (g_list_length((*session)->status) == 1 && (*session)->resultsCommand == NULL && (*session)->alertCommand == NULL && isgenericAlert == 0))) {
1321 _DEBUG_INFO("---------------------------------------------------------------------------recv msg end------------------------------------------------------------------------\n");
1322 _DEBUG_INFO("---------------------------------------------------------------------------free session------------------------------------------------------------------------\n");
1323 free_session((*session));
1333 _DEBUG_INFO(" end error %d\n", ret);
1338 DM_ERROR management_phase(int transportType, Session ** session, char *pSession_id, ENGINE_ID * service_engine_id, int *isFinish)
1342 DM_ERROR ret = DM_OK;
1344 retvm_if((*session) == NULL, COMMON_ERR_IS_NULL, "session is NULL!!");
1346 while ((*isFinish) == 0) {
1348 /*generate management phase Msg */
1350 unsigned int msg_size = 0;
1353 char *recvMsg = NULL;
1354 unsigned int recvMsg_size = 0;
1355 GList *recv_header = 0;
1358 ret = _generate_management_msg(session, &msg, &msg_size);
1363 char file_name[1000];
1364 snprintf(file_name, (int)sizeof(file_name), "/opt/data/oma-dm-cfg/packet/wbxml_management_send_msg_%d.wbxml", file_count);
1365 _DEBUG_INFO("file name : %s", file_name);
1366 sync_agent_write_whole_file(file_name, msg, msg_size, true);
1368 ret = send_msg("OMA_DM", 1, session, list, msg, msg_size, &recv_header, &recvMsg, &recvMsg_size);
1373 snprintf(file_name, (int)sizeof(file_name), "/opt/data/oma-dm-cfg/packet/wbxml_management_receivce_msg_%d.wbxml", file_count);
1374 _DEBUG_INFO("file name : %s", file_name);
1375 sync_agent_write_whole_file(file_name, recvMsg, recvMsg_size, true);
1377 ret = recv_msg(session, recvMsg, recvMsg_size, service_engine_id, isFinish, 0);
1381 _DEBUG_INFO(" end : %d \n", ret);
1388 _DEBUG_INFO(" error : %d end \n", ret);
1393 DM_ERROR generic_alert(int transportType, Session ** session, char *pServer_id, char *pSession_id, int *isFinish, int isgeneticAlert, ENGINE_ID service_engine_id, int result)
1397 DM_ERROR ret = DM_OK;
1399 /*generate setupPhase Msg */
1401 unsigned int msg_size = 0;
1405 char *recvMsg = NULL;
1406 unsigned int recvMsg_size = 0;
1407 GList *recv_header = 0;
1410 while (count++ != MAX_SESSION_COUNT) {
1414 ret = _generate_generic_alert(session, pServer_id, pSession_id, &msg, (int *)(&msg_size), service_engine_id, result);
1419 char file_name[1000];
1421 snprintf(file_name, (int)sizeof(file_name), "/opt/data/oma-dm-cfg/packet/wbxml_generic_send_msg_%d.wbxml", file_count);
1422 _DEBUG_INFO("file name : %s", file_name);
1423 sync_agent_write_whole_file(file_name, msg, msg_size, true);
1427 ret = send_msg("OMA_DM", 1, session, list, msg, msg_size, &recv_header, &recvMsg, &recvMsg_size);
1431 snprintf(file_name, (int)sizeof(file_name), "/opt/data/oma-dm-cfg/packet/wbxml_generic_receivce_msg_%d.wbxml", file_count);
1432 _DEBUG_INFO("file name : %s", file_name);
1433 sync_agent_write_whole_file(file_name, recvMsg, recvMsg_size, true);
1435 ret = recv_msg(session, recvMsg, recvMsg_size, &service_engine_id, isFinish, isgeneticAlert);
1436 _DEBUG_INFO(" Recv_Msg : %d count : %d \n ", ret, count);
1438 if (ret != DM_ERR_UNAUTHORIZED && ret != DM_ERR_AUTHENTICATION_REQUIRED) {
1439 _DEBUG_INFO(" generic alert authentication : %d\n ", ret);
1444 _DEBUG_INFO(" end : %d \n", ret);
1448 _DEBUG_INFO(" error : %d end \n", ret);