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.h"
23 #include "common/util/util.h"
24 #include "serviceadapter/protocolbinder/syncml_def.h"
25 #include "serviceadapter/sa_elements.h"
26 #include "serviceadapter/sa_elements_internal.h"
27 #include "serviceadapter/sa_session_internal.h"
28 #include "serviceadapter/sa_command.h"
29 #include "mo-handler/dm_mo_common.h"
31 #ifndef OMADM_AGENT_LOG
33 #define LOG_TAG "OMA_DM_SA"
36 DM_ERROR create_hmac(char *auth_name, char *auth_type, char *mac, Session ** session)
42 retvm_if((auth_name) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "auth_name is NULL!!");
43 retvm_if((auth_type) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "auth_type is NULL!!");
44 retvm_if((mac) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "mac is NULL!!");
46 if ((*session)->reqhmacinfo == NULL) {
47 (*session)->reqhmacinfo = (Hmac *) calloc(1, sizeof(Hmac));
48 if ((*session)->reqhmacinfo == NULL) {
49 ret = COMMON_ERR_ALLOC;
54 if ((*session)->reqhmacinfo->username != NULL) {
55 str_free(&(*session)->reqhmacinfo->username);
57 if ((*session)->reqhmacinfo->authtype != NULL) {
58 str_free(&(*session)->reqhmacinfo->authtype);
60 if ((*session)->reqhmacinfo->mac != NULL) {
61 str_free(&(*session)->reqhmacinfo->mac);
64 (*session)->reqhmacinfo->username = strdup(auth_name);
65 (*session)->reqhmacinfo->authtype = strdup(auth_type);
66 (*session)->reqhmacinfo->mac = strdup(mac);
68 _DEBUG_INFO("user name : %s", auth_name);
69 _DEBUG_INFO("auth type : %s", auth_type);
70 _DEBUG_INFO("mac : %s", mac);
72 _DEBUG_INFO(" end : %d", ret);
76 _DEBUG_INFO(" end error : %d", ret);
81 DM_ERROR create_location(const char *locURI, const char *locName, Location ** pLocation)
87 retvm_if((locURI) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "locURI is NULL!!");
88 _DEBUG_INFO("start with locURI = %s, locName = %s\n", locURI, locName);
90 *pLocation = (Location *) calloc(1, sizeof(Location) + 1);
91 if (*pLocation == NULL) {
92 ret = COMMON_ERR_ALLOC;
95 (*pLocation)->locURI = g_strdup(locURI);
97 if (locName != NULL) {
98 (*pLocation)->locName = g_strdup(locName);
106 _DEBUG_INFO(" error : %d\n", ret);
111 Location *dup_location(Location * pLocation)
115 DM_ERROR ret = DM_OK;
117 retvm_if((pLocation) == NULL, NULL, "pLocation is NULL!!");
119 Location *location = NULL;
120 ret = create_location(pLocation->locURI, pLocation->locName, &location);
128 _DEBUG_INFO(" error : %d\n", ret);
134 char *get_location_locname(Location * location)
138 retvm_if((location) == NULL, NULL, "location is NULL!!");
142 return location->locName;
145 char *get_location_locuri(Location * location)
149 retvm_if((location) == NULL, NULL, "location is NULL!!");
153 return location->locURI;
156 void free_location(Location * loc)
160 retm_if((loc) == NULL, "loc is NULL!!");
162 if (loc->locURI != NULL) {
163 _DEBUG_INFO(" loc->locURI = %s\n", loc->locURI);
168 if (loc->locName != NULL) {
169 _DEBUG_INFO(" loc->locName = %s\n", loc->locName);
180 void free_cred(Cred * cred)
187 if (cred->data != NULL) {
192 if (cred->username != NULL) {
193 free(cred->username);
194 cred->username = NULL;
197 if (cred->password != NULL) {
198 free(cred->password);
199 cred->password = NULL;
209 DM_ERROR create_auth_cred(char *userName, char *pwd, AuthType authType, FormatType formatType, char *data, Cred ** cred)
214 DM_ERROR ret = DM_OK;
216 retvm_if((userName) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "userName is NULL!!");
217 retvm_if((pwd) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "pwd is NULL!!");
219 _DEBUG_INFO(" user : %s , pwd : %s, authtype : %d,. formattype : %d, cred : %s\n", userName, pwd, authType, formatType, data);
221 if (strlen(userName) == 0 || strlen(pwd) == 0) {
222 ret = COMMON_ERR_INTERNAL_NOT_DEFINED;
227 *cred = (Cred *) calloc(1, sizeof(Cred));
229 ret = COMMON_ERR_ALLOC;
233 (*cred)->type = authType;
234 (*cred)->format = formatType;
235 (*cred)->username = strdup(userName);
236 (*cred)->password = strdup(pwd);
238 (*cred)->data = strdup(data);
239 } else if (data == NULL && authType == AUTH_TYPE_HMAC) {
243 ret = COMMON_ERR_IS_NULL;
249 _DEBUG_INFO(" error : %d\n", ret);
255 Cred *create_credwithdata(AuthType authType, char *data)
259 DM_ERROR ret = DM_OK;
261 retvm_if((data) == NULL, NULL, "data is NULL!!");
263 Cred *cred = (Cred *) calloc(1, sizeof(Cred));
265 ret = COMMON_ERR_ALLOC;
269 cred->type = authType;
270 cred->data = strdup(data);
275 _DEBUG_INFO(" error : %d\n", ret);
281 void set_credformattype(Cred * cred, FormatType formatType)
285 retm_if((cred) == NULL, "cred is NULL!!");
287 cred->format = formatType;
292 DM_ERROR check_server_cred(Cred * hdrCred, Session * session) //Cred *sessionCred)
296 DM_ERROR ret = DM_OK;
298 retvm_if((hdrCred) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "hdrCred is NULL!!");
299 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "session is NULL!!");
309 ret = get_server_dmacc(session->pServer_id, &server_id, &server_pwd, &sourceUrl, &targetUrl, &isBase64, &nextNonce, &authType);
312 _DEBUG_INFO("packet server pw data : %s", hdrCred->data);
313 _DEBUG_INFO("server pw : %s", server_pwd);
314 if (hdrCred->data != NULL) {
315 if (strcmp(hdrCred->data, server_pwd) == 0) {
318 ret = DM_ERR_UNAUTHORIZED;
323 str_free(&server_id);
324 str_free(&server_pwd);
325 str_free(&sourceUrl);
326 str_free(&targetUrl);
327 str_free(&nextNonce);
333 _DEBUG_INFO(" end error : %d", ret);
338 DM_ERROR create_chal(FormatType format, AuthType type, char *nextnonce, Chal ** pChal)
342 retvm_if((nextnonce) == NULL, COMMON_ERR_INTERNAL_NO_MEMORY, "nextnonce is NULL!!");
343 _DEBUG_INFO("start with format = %d, type = %d Nextnonce = %s\n", format, type, nextnonce);
345 DM_ERROR ret = DM_OK;
347 *pChal = (Chal *) calloc(1, sizeof(Chal));
348 if (*pChal == NULL) {
349 ret = COMMON_ERR_ALLOC;
353 (*pChal)->format = format;
354 (*pChal)->type = type;
355 if (type == AUTH_TYPE_MD5 || type == AUTH_TYPE_HMAC) {
356 (*pChal)->nonce_plain = g_strdup(nextnonce);
363 _DEBUG_INFO(" error : %d\n", ret);
368 void free_chal(Chal * pChal)
378 pChal->nonce_length = 0;
380 if (pChal->nonce_plain != NULL) {
381 free(pChal->nonce_plain);
382 pChal->nonce_plain = NULL;
385 if (pChal->nonce_b64 != NULL) {
386 free(pChal->nonce_b64);
387 pChal->nonce_b64 = NULL;
398 Chal *dup_chal(Chal * pChal)
402 retvm_if((pChal) == NULL, NULL, "pChal is NULL!!");
404 Chal *temp = (Chal *) calloc(1, sizeof(Chal));
410 temp->type = pChal->type;
411 temp->format = pChal->format;
413 if (pChal->nonce_b64 != NULL)
414 temp->nonce_b64 = strdup(pChal->nonce_b64);
416 if (pChal->nonce_length != 0)
417 temp->nonce_length = pChal->nonce_length;
419 if (pChal->nonce_plain != NULL)
420 temp->nonce_plain = strdup(pChal->nonce_plain);
426 void free_hmac(Hmac * hmac)
431 str_free(&(hmac->authtype));
432 str_free(&(hmac->username));
433 str_free(&(hmac->mac));
442 AuthType converttoauthtype(char *authType)
446 retvm_if((authType) == NULL, AUTH_TYPE_UNKNOWN, "authType is NULL!!");
448 if (strcmp(authType, ELEMENT_AUTH_BASIC) == 0) {
450 return AUTH_TYPE_BASIC;
451 } else if (strcmp(authType, ELEMENT_AUTH_MD5) == 0) {
453 return AUTH_TYPE_MD5;
454 } else if (strcmp(authType, ELEMENT_AUTH_HMAC) == 0) {
456 return AUTH_TYPE_HMAC;
460 return AUTH_TYPE_UNKNOWN;
463 FormatType converttoformattype(char *formatType)
467 retvm_if((formatType) == NULL, FORMAT_TYPE_UNKNOWN, "formatType is NULL!!");
469 if (strcmp(formatType, ELEMENT_FORMAT_BASE64) == 0) {
471 return FORMAT_TYPE_BASE64;
475 return FORMAT_TYPE_UNKNOWN;
478 DM_ERROR construct_Item(char *sourceUrl, const char *format, const char *contenttype, const char *data, unsigned int size, int moreData, Item ** pItem)
482 DM_ERROR ret = DM_OK;
484 retvm_if((sourceUrl) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "sourceUrl is NULL!!");
486 Location *pLocation = NULL;
488 ret = create_item_data((char *)data, size, pItem);
492 if (contenttype != NULL)
493 (*pItem)->contenttype = g_strdup(contenttype);
496 (*pItem)->format = g_strdup(format);
498 (*pItem)->moreData = moreData;
500 ret = create_location(sourceUrl, NULL, &pLocation);
504 set_itemsource((*pItem), pLocation);
510 _DEBUG_INFO(" error : %d\n", ret);
519 DM_ERROR ret = DM_OK;
521 Item *item = (Item *) calloc(1, sizeof(Item));
523 ret = COMMON_ERR_ALLOC;
527 item->dataType = ITEM_UNKNOWN;
533 _DEBUG_INFO(" error : %d\n", ret);
538 DM_ERROR create_item_data(char *data, unsigned int size, Item ** pItem)
542 DM_ERROR ret = DM_OK;
544 (*pItem) = create_Item();
545 if ((*pItem) == NULL) {
546 ret = COMMON_ERR_INTERNAL_NO_MEMORY;
551 (*pItem)->private.data = strdup(data);
554 (*pItem)->dataType = ITEM_DATA;
555 (*pItem)->size = size;
560 _DEBUG_INFO(" error : %d\n", ret);
565 void set_itemtarget(Item * item, Location * target)
569 retm_if((item) == NULL, "item is NULL!!");
571 item->target = target;
576 void set_itemsource(Item * item, Location * source)
580 retm_if((item) == NULL, "item is NULL!!");
582 item->source = source;
587 void free_Item(Item * item)
591 retm_if((item) == NULL, "item is NULL!!");
593 if (item->source != NULL) {
594 free_location(item->source);
598 if (item->target != NULL) {
599 free_location(item->target);
603 switch (item->dataType) {
605 if (item->private.data != NULL) {
606 free(item->private.data);
607 item->private.data = NULL;
615 if (item->contenttype != NULL) {
616 free(item->contenttype);
617 item->contenttype = NULL;
620 if (item->format != NULL) {
631 DM_ERROR create_syncml(SyncHdr * syncHdr, GList * status, GList * commands, int isFinal, SyncML ** pSyncML)
635 DM_ERROR ret = DM_OK;
637 retvm_if((syncHdr) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "syncHdr is NULL!!");
639 *pSyncML = (SyncML *) calloc(1, sizeof(SyncML));
640 if (*pSyncML == NULL) {
641 ret = COMMON_ERR_ALLOC;
645 (*pSyncML)->hdr = syncHdr;
646 (*pSyncML)->status = status;
647 (*pSyncML)->commands = commands;
648 (*pSyncML)->final = isFinal;
653 _DEBUG_INFO(" error : %d", ret);
659 void free_syncml(SyncML * syncML)
663 retm_if((syncML) == NULL, "syncML is NULL!!");
665 free_synchdr(syncML->hdr);
668 free_statuses(syncML->status);
669 syncML->status = NULL;
671 free_commands(syncML->commands);
672 syncML->commands = NULL;
674 if (syncML != NULL) {
682 DM_ERROR create_syncml_hdr(Session * session, SyncHdr ** pSyncHdr)
686 DM_ERROR ret = DM_OK;
688 retvm_if((session) == NULL, COMMON_ERR_INTERNAL_NOT_DEFINED, "session is NULL!!");
690 if (session->protocolVersion == VERSION_UNKNOWN) {
691 ret = COMMON_ERR_INTERNAL_NOT_DEFINED;
695 if (session->protocolType != PROTOCOL_TYPE_DM) {
696 ret = COMMON_ERR_INTERNAL_NOT_DEFINED;
700 if (session->source == NULL) {
701 ret = COMMON_ERR_INTERNAL_NOT_DEFINED;
705 if (session->target == NULL) {
706 ret = COMMON_ERR_INTERNAL_NOT_DEFINED;
710 *pSyncHdr = (SyncHdr *) calloc(1, sizeof(SyncHdr));
712 if (*pSyncHdr == NULL) {
713 ret = COMMON_ERR_ALLOC;
717 (*pSyncHdr)->version = session->protocolVersion;
718 (*pSyncHdr)->protocol = session->protocolType;
719 (*pSyncHdr)->target = session->target; //don't free free in session free section
720 (*pSyncHdr)->source = session->source; //don't free free in session free section
722 if (session->cred != NULL)
723 (*pSyncHdr)->cred = session->cred; //don't free free in session free section
725 (*pSyncHdr)->sessionID = strdup(session->sessionID); //free
726 (*pSyncHdr)->messageID = ++session->msgID;
728 (*pSyncHdr)->maxmsgsize = session->sourceMaxMsgSize;
729 (*pSyncHdr)->maxobjsize = session->sourceMaxObjSize;
735 _DEBUG_INFO(" error : %d\n", ret);
740 void free_synchdr(SyncHdr * syncHdr)
744 retm_if((syncHdr) == NULL, "syncHdr is NULL!!");
746 if (syncHdr->sessionID != NULL) {
747 free(syncHdr->sessionID);
748 syncHdr->sessionID = NULL;
751 if (syncHdr->responseURI != NULL) {
752 free(syncHdr->responseURI);
753 syncHdr->responseURI = NULL;
756 syncHdr->source = NULL;
757 syncHdr->target = NULL;
758 syncHdr->cred = NULL;
760 if (syncHdr != NULL) {