RSA sync with private
[platform/core/messaging/msg-service.git] / framework / transaction-manager / MsgTransManager.cpp
1 /*
2 * Copyright 2012  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *    http://www.tizenopensource.org/license
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <errno.h>
18 #include <stdlib.h>
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21
22 #include <security-server.h>
23
24 #include "MsgDebug.h"
25 #include "MsgMemory.h"
26 #include "MsgException.h"
27 #include "MsgCppTypes.h"
28 #include "MsgContact.h"
29 #include "MsgIpcSocket.h"
30 #include "MsgGconfWrapper.h"
31 #include "MsgUtilFunction.h"
32 #include "MsgCmdHandler.h"
33 #include "MsgSettingHandler.h"
34 #include "MsgStorageHandler.h"
35 #include "MsgPluginManager.h"
36 #include "MsgTransManager.h"
37
38 /*==================================================================================================
39                                      FUNCTION IMPLEMENTATION
40 ==================================================================================================*/
41 void MsgContactChangedCallback()
42 {
43         msg_id_list_s msgIdList;
44         memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
45
46         MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_CONTACT, &msgIdList);
47 }
48
49 /*==================================================================================================
50                                      IMPLEMENTATION OF MsgTransactionManager - Member Functions
51 ==================================================================================================*/
52 MsgTransactionManager* MsgTransactionManager::pInstance = NULL;
53 MsgIpcServerSocket MsgTransactionManager::servSock;
54
55
56 MsgTransactionManager::MsgTransactionManager() : running(false), mx(), cv()
57 {
58         sentMsgMap.clear();
59
60         statusCBFdMap.clear();
61         newMsgCBList.clear();
62         newMMSConfMsgCBList.clear();
63         newSyncMLMsgCBList.clear();
64         newLBSMsgCBList.clear();
65         javaMMSList.clear();
66         operationSyncMLMsgCBList.clear();
67         storageChangeFdMap.clear();
68
69         handlerMap.clear();
70
71 //      Fill in mMsgHandlers, as given in the below.
72         handlerMap[MSG_CMD_ADD_MSG]                             = &MsgAddMessageHandler;
73         handlerMap[MSG_CMD_ADD_SYNCML_MSG]              = &MsgAddSyncMLMessageHandler;
74         handlerMap[MSG_CMD_UPDATE_MSG]                  = &MsgUpdateMessageHandler;
75         handlerMap[MSG_CMD_UPDATE_READ]                         = &MsgUpdateReadStatusHandler;
76         handlerMap[MSG_CMD_UPDATE_PROTECTED]            = &MsgUpdateProtectedStatusHandler;
77         handlerMap[MSG_CMD_DELETE_MSG]                  = &MsgDeleteMessageHandler;
78         handlerMap[MSG_CMD_DELALL_MSGINFOLDER]  = &MsgDeleteAllMessageInFolderHandler;
79         handlerMap[MSG_CMD_MOVE_MSGTOFOLDER]    = &MsgMoveMessageToFolderHandler;
80         handlerMap[MSG_CMD_MOVE_MSGTOSTORAGE]   = &MsgMoveMessageToStorageHandler;
81         handlerMap[MSG_CMD_COUNT_MSG]                   = &MsgCountMessageHandler;
82         handlerMap[MSG_CMD_GET_MSG]                             = &MsgGetMessageHandler;
83         handlerMap[MSG_CMD_GET_FOLDERVIEWLIST]  = &MsgGetFolderViewListHandler;
84
85         handlerMap[MSG_CMD_ADD_FOLDER]                  = &MsgAddFolderHandler;
86         handlerMap[MSG_CMD_UPDATE_FOLDER]               = &MsgUpdateFolderHandler;
87         handlerMap[MSG_CMD_DELETE_FOLDER]               = &MsgDeleteFolderHandler;
88         handlerMap[MSG_CMD_GET_FOLDERLIST]              = &MsgGetFolderListHandler;
89
90         handlerMap[MSG_CMD_ADD_FILTER]                  = &MsgAddFilterHandler;
91         handlerMap[MSG_CMD_UPDATE_FILTER]                       = &MsgUpdateFilterHandler;
92         handlerMap[MSG_CMD_DELETE_FILTER]               = &MsgDeleteFilterHandler;
93         handlerMap[MSG_CMD_GET_FILTERLIST]              = &MsgGetFilterListHandler;
94         handlerMap[MSG_CMD_SET_FILTER_OPERATION]        = &MsgSetFilterOperationHandler;
95         handlerMap[MSG_CMD_GET_FILTER_OPERATION]        = &MsgGetFilterOperationHandler;
96         handlerMap[MSG_CMD_SET_FILTER_ACTIVATION] = &MsgSetFilterActivationHandler;
97
98         handlerMap[MSG_CMD_GET_MSG_TYPE]                        = &MsgGetMsgTypeHandler;
99
100         handlerMap[MSG_CMD_SUBMIT_REQ]                  = &MsgSubmitReqHandler;
101         handlerMap[MSG_CMD_CANCEL_REQ]                  = &MsgCancelReqHandler;
102
103         handlerMap[MSG_CMD_REG_SENT_STATUS_CB]  = &MsgRegSentStatusCallbackHandler;
104         handlerMap[MSG_CMD_REG_STORAGE_CHANGE_CB] = &MsgRegStorageChangeCallbackHandler;
105         handlerMap[MSG_CMD_REG_INCOMING_MSG_CB] = &MsgRegIncomingMsgCallbackHandler;
106         handlerMap[MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB]        = &MsgRegIncomingMMSConfMsgCallbackHandler;
107         handlerMap[MSG_CMD_REG_INCOMING_SYNCML_MSG_CB]  = &MsgRegIncomingSyncMLMsgCallbackHandler;
108         handlerMap[MSG_CMD_REG_INCOMING_PUSH_MSG_CB]    = &MsgRegIncomingPushMsgCallbackHandler;
109         handlerMap[MSG_CMD_REG_INCOMING_CB_MSG_CB]      = &MsgRegIncomingCBMsgCallbackHandler;
110         handlerMap[MSG_CMD_REG_INCOMING_LBS_MSG_CB]     = &MsgRegIncomingLBSMsgCallbackHandler;
111         handlerMap[MSG_CMD_REG_SYNCML_MSG_OPERATION_CB] = &MsgRegSyncMLMsgOperationCallbackHandler;
112
113         handlerMap[MSG_CMD_PLG_SENT_STATUS_CNF] = &MsgSentStatusHandler;
114         handlerMap[MSG_CMD_PLG_STORAGE_CHANGE_IND]      = &MsgStorageChangeHandler;
115         handlerMap[MSG_CMD_PLG_INCOMING_MSG_IND]        = &MsgIncomingMsgHandler;
116         handlerMap[MSG_CMD_PLG_INCOMING_MMS_CONF]       = &MsgIncomingMMSConfMsgHandler;
117         handlerMap[MSG_CMD_PLG_INCOMING_PUSH_IND]       = &MsgIncomingPushMsgHandler;
118         handlerMap[MSG_CMD_PLG_INCOMING_CB_IND] = &MsgIncomingCBMsgHandler;
119
120         handlerMap[MSG_CMD_PLG_INCOMING_SYNCML_IND] = &MsgIncomingSyncMLMsgHandler;
121         handlerMap[MSG_CMD_PLG_INCOMING_LBS_IND] = &MsgIncomingLBSMsgHandler;
122         handlerMap[MSG_CMD_PLG_INIT_SIM_BY_SAT] = &MsgInitSimBySatHandler;
123
124         handlerMap[MSG_CMD_GET_THREADVIEWLIST]  = &MsgGetThreadViewListHandler;
125         handlerMap[MSG_CMD_GET_CONVERSATIONVIEWLIST]    = &MsgGetConversationViewListHandler;
126         handlerMap[MSG_CMD_DELETE_THREADMESSAGELIST]    = &MsgDeleteThreadMessageListHandler;
127
128         handlerMap[MSG_CMD_GET_CONTACT_COUNT]   = &MsgCountMsgByContactHandler;
129         handlerMap[MSG_CMD_GET_QUICKPANEL_DATA]         = &MsgGetQuickPanelDataHandler;
130         handlerMap[MSG_CMD_COUNT_BY_MSGTYPE]    = &MsgCountMsgByTypeHandler;
131         handlerMap[MSG_CMD_RESET_DB]    = &MsgResetDatabaseHandler;
132         handlerMap[MSG_CMD_GET_MEMSIZE]         = &MsgGetMemSizeHandler;
133
134         handlerMap[MSG_CMD_BACKUP_MESSAGE]      = &MsgBackupMessageHandler;
135         handlerMap[MSG_CMD_RESTORE_MESSAGE] = &MsgRestoreMessageHandler;
136
137         handlerMap[MSG_CMD_UPDATE_THREAD_READ] = &MsgUpdateThreadReadStatusHandler;
138
139         handlerMap[MSG_CMD_SYNCML_OPERATION] = &MsgSyncMLMsgOperationHandler;
140         handlerMap[MSG_CMD_GET_REPORT_STATUS]                   = &MsgGetReportStatusHandler;
141
142         handlerMap[MSG_CMD_GET_THREAD_ID_BY_ADDRESS] = &MsgGetThreadIdByAddressHandler;
143         handlerMap[MSG_CMD_GET_THREAD_INFO] = &MsgGetThreadInfoHandler;
144
145         handlerMap[MSG_CMD_GET_SMSC_OPT] = &MsgGetConfigHandler;
146         handlerMap[MSG_CMD_GET_CB_OPT] = &MsgGetConfigHandler;
147         handlerMap[MSG_CMD_GET_SMS_SEND_OPT] = &MsgGetConfigHandler;
148         handlerMap[MSG_CMD_GET_MMS_SEND_OPT] = &MsgGetConfigHandler;
149         handlerMap[MSG_CMD_GET_MMS_RECV_OPT] = &MsgGetConfigHandler;
150         handlerMap[MSG_CMD_GET_PUSH_MSG_OPT] = &MsgGetConfigHandler;
151         handlerMap[MSG_CMD_GET_VOICE_MSG_OPT] = &MsgGetConfigHandler;
152         handlerMap[MSG_CMD_GET_GENERAL_MSG_OPT] = &MsgGetConfigHandler;
153         handlerMap[MSG_CMD_GET_MSG_SIZE_OPT] = &MsgGetConfigHandler;
154
155         handlerMap[MSG_CMD_SET_SMSC_OPT] = &MsgSetConfigHandler;
156         handlerMap[MSG_CMD_SET_CB_OPT] = &MsgSetConfigHandler;
157         handlerMap[MSG_CMD_SET_SMS_SEND_OPT] = &MsgSetConfigHandler;
158         handlerMap[MSG_CMD_SET_MMS_SEND_OPT] = &MsgSetConfigHandler;
159         handlerMap[MSG_CMD_SET_MMS_RECV_OPT] = &MsgSetConfigHandler;
160         handlerMap[MSG_CMD_SET_PUSH_MSG_OPT] = &MsgSetConfigHandler;
161         handlerMap[MSG_CMD_SET_VOICE_MSG_OPT] = &MsgSetConfigHandler;
162         handlerMap[MSG_CMD_SET_GENERAL_MSG_OPT] = &MsgSetConfigHandler;
163         handlerMap[MSG_CMD_SET_MSG_SIZE_OPT] = &MsgSetConfigHandler;
164
165         handlerMap[MSG_CMD_ADD_PUSH_EVENT] = &MsgAddPushEventHandler;
166         handlerMap[MSG_CMD_DELETE_PUSH_EVENT] = &MsgDeletePushEventHandler;
167         handlerMap[MSG_CMD_UPDATE_PUSH_EVENT] = &MsgUpdatePushEventHandler;
168         handlerMap[MSG_CMD_DELETE_MESSAGE_BY_LIST] = &MsgDeleteMessageByListHandler;
169 }
170
171
172 MsgTransactionManager::~MsgTransactionManager()
173 {
174 //      pthread_cond_init(&retCV, NULL); // = PTHREAD_COND_INITIALIZER;
175
176 }
177
178
179 MsgTransactionManager* MsgTransactionManager::instance()
180 {
181         if (!pInstance)
182                 pInstance = new MsgTransactionManager();
183
184         return pInstance;
185 }
186
187
188 void MsgTransactionManager::run()
189 {
190         servSock.open(MSG_SOCKET_PATH);
191
192         fd_set readfds = servSock.fdSet();
193         int nfds = 0;
194
195         MSG_DEBUG("Start Transaction Manager");
196
197         while(1)
198         {
199                 readfds = servSock.fdSet();
200                 nfds = servSock.maxFd();
201
202                 MSG_DEBUG("Wait For Select() : nfds %d", nfds);
203
204                 // set Status;
205                 setTMStatus();
206
207                 if(select(nfds, &readfds, NULL, NULL, NULL) == -1) {
208                         THROW(MsgException::SELECT_ERROR, "select error : %s", strerror(errno));
209                 }
210
211                 try
212                 {
213                         for (int i=0 ; i < nfds; i++)
214                         {
215                                 if (FD_ISSET(i, &readfds))
216                                 {
217                                         if (i == servSock.fd()) // if it is socket connection request
218                                                 servSock.accept();
219                                         else
220                                                 handleRequest(i);
221                                 }
222                         }
223                 }
224                 catch (MsgException& e)
225                 {
226                         MSG_FATAL("%s", e.what());
227                 }
228                 catch (exception& e)
229                 {
230                         MSG_FATAL("%s", e.what());
231                 }
232
233                 // Release Memory
234                 MsgReleaseMemory();
235         }
236 }
237
238
239 void MsgTransactionManager::write(int fd, const char* buf, int len)
240 {
241         servSock.write(fd, buf, len);
242 }
243
244
245 void MsgTransactionManager::insertSentMsg(int reqId, MSG_PROXY_INFO_S* pPrxInfo)
246 {
247         if (pPrxInfo == NULL)
248                 THROW(MsgException::SENT_STATUS_ERROR, "Input Parameter is NULL");
249
250         MSG_DEBUG("msg for submit: reqId %d listenerFd %d handleAddr %x", reqId, pPrxInfo->listenerFd, pPrxInfo->handleAddr);
251
252         fd_map::iterator it = statusCBFdMap.find(pPrxInfo->listenerFd);
253
254         if (it == statusCBFdMap.end()) { // if the status CB is not registered
255                 MSG_DEBUG("No sent_status registered for fd %d", pPrxInfo->listenerFd);
256         } else {
257                 sentMsgMap.insert(make_pair(reqId, *pPrxInfo));
258         }
259 }
260
261
262 MSG_PROXY_INFO_S* MsgTransactionManager::getProxyInfo(int reqId)
263 {
264         sentmsg_map::iterator it = sentMsgMap.find(reqId);
265
266         if (it == sentMsgMap.end())
267         {
268                 //THROW(MsgException::SENT_STATUS_ERROR, "No submit request for %d", reqId);
269                 MSG_DEBUG("No sent status cb found (exception: mobile tracker)");
270                 return NULL;
271         }
272
273         return &(it->second);
274 }
275
276
277 void MsgTransactionManager::delProxyInfo(int reqId)
278 {
279         sentmsg_map::iterator it = sentMsgMap.find(reqId);
280
281         if (it == sentMsgMap.end())
282         {
283                 THROW(MsgException::SENT_STATUS_ERROR, "channel info does not exist");
284         }
285
286         sentMsgMap.erase(it);
287 }
288
289
290 void MsgTransactionManager::handleRequest(int fd)
291 {
292         MSG_BEGIN();
293
294         MSG_DEBUG("Event from fd %d", fd);
295
296         char* buf = NULL;
297         AutoPtr<char> wrap(&buf);
298         int len;
299         int ret = servSock.read(fd, &buf, &len);
300
301         if( ret == CLOSE_CONNECTION_BY_SIGNAL || ret == CLOSE_CONNECTION_BY_USER || ret < 0)
302         {
303                 MSG_DEBUG("Read value [%d]", ret);
304                 cleanup(fd);
305                 return;
306         }
307
308         if (len <= 0)
309                 THROW(MsgException::INVALID_RESULT, "read buffer size <= 0");
310
311         char* pEventData = NULL;
312         AutoPtr<char> eventBuf(&pEventData);
313
314         int eventSize = 0;
315
316         // decoding cmd from APP
317         MSG_CMD_S* pCmd = (MSG_CMD_S*) buf;
318         MSG_DEBUG("Command Type [%d : %s]", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType));
319
320         if (pCmd->cmdType > MSG_CMD_NUM)
321                 THROW(MsgException::OUT_OF_RANGE, "request CMD is not defined");
322
323         if (pCmd->cmdType < MSG_CMD_GET_REPORT_STATUS)
324         {
325                 // check privilege
326                 if (checkPrivilege(pCmd->cmdType, pCmd->cmdCookie) == false)
327                 {
328 #ifdef MSG_CHECK_PRIVILEGE
329                         eventSize = sizeof(MSG_EVENT_S);
330
331                         pEventData = new char[eventSize];
332
333                         MSG_EVENT_S* pMsgEvent = (MSG_EVENT_S*)pEventData;
334
335                         pMsgEvent->eventType = pCmd->cmdType;
336                         pMsgEvent->result = MSG_ERR_SECURITY_ERROR;
337
338                         MSG_DEBUG("Replying to fd [%d], size [%d]", fd, eventSize);
339                         servSock.write(fd, pEventData, eventSize);
340
341                         return;
342 #endif
343                 }
344         }
345
346         // determine the handler based on pCmd->cmdType
347         int (*pfHandler)(const MSG_CMD_S*, char**) = NULL;
348
349         pfHandler = handlerMap[pCmd->cmdType];
350
351         if (!pfHandler)
352                 THROW(MsgException::INVALID_PARAM, "No handler for %d", pCmd->cmdType);
353
354         // run handler function
355         eventSize = pfHandler(pCmd, &pEventData);
356
357         if (eventSize == 0 || pEventData == NULL)
358                 THROW(MsgException::INVALID_RESULT, "event size = 0 or event data = NULL");
359
360         MSG_DEBUG("Replying to fd [%d], size [%d]", fd, eventSize);
361
362         servSock.write(fd, pEventData, eventSize);
363
364         MSG_END();
365 }
366
367
368 // terminating the socket connection between ipc server and ipc client
369 void MsgTransactionManager::cleanup(int fd)
370 {
371         MSG_BEGIN();
372
373         servSock.close(fd);
374
375         MSG_DEBUG("fd %d disonnected", fd);
376
377         // remove sent msg info for fd
378         sentmsg_map::iterator sentmsg_it = sentMsgMap.begin();
379
380         for (; sentmsg_it != sentMsgMap.end(); sentmsg_it++)
381         {
382                 if (sentmsg_it->second.listenerFd == fd)
383                 {
384                         sentmsg_it->second.listenerFd = 0;
385                         sentmsg_it->second.handleAddr = 0;
386                 }
387         }
388
389         // remove sent status callback for fd
390         statusCBFdMap.erase(fd);
391
392         // remove all newMsgCBs for fd
393         newmsg_list::iterator newmsg_it = newMsgCBList.begin();
394
395         while (newmsg_it != newMsgCBList.end())
396         {
397                 if (newmsg_it->listenerFd == fd)
398                 {
399                         newmsg_it = newMsgCBList.erase(newmsg_it);
400                 }
401                 else
402                 {
403                         ++newmsg_it;
404                 }
405         }
406
407         // remove all newMMSConfMsgCBs for fd
408         mmsconf_list::iterator mmsconf_it = newMMSConfMsgCBList.begin();
409
410         while (mmsconf_it != newMMSConfMsgCBList.end())
411         {
412                 if (mmsconf_it->listenerFd == fd)
413                 {
414                         mmsconf_it = newMMSConfMsgCBList.erase(mmsconf_it);
415                 }
416                 else
417                 {
418                         ++mmsconf_it;
419                 }
420         }
421
422         // remove all newSyncMLMsgCBs for fd
423         syncmlmsg_list::iterator syncmlmsg_it = newSyncMLMsgCBList.begin();
424
425         while (syncmlmsg_it != newSyncMLMsgCBList.end())
426         {
427                 if (syncmlmsg_it->listenerFd == fd)
428                 {
429                         syncmlmsg_it = newSyncMLMsgCBList.erase(syncmlmsg_it);
430                 }
431                 else
432                 {
433                         ++syncmlmsg_it;
434                 }
435         }
436
437         // remove all newLBSMsgCBs for fd
438         lbsmsg_list::iterator lbsmsg_it = newLBSMsgCBList.begin();
439
440         while (lbsmsg_it != newLBSMsgCBList.end())
441         {
442                 if (lbsmsg_it->listenerFd == fd)
443                 {
444                         lbsmsg_it = newLBSMsgCBList.erase(lbsmsg_it);
445                 }
446                 else
447                 {
448                         ++lbsmsg_it;
449                 }
450         }
451
452         // remove all newPushMsgCBs for fd
453         pushmsg_list::iterator pushmsg_it = newPushMsgCBList.begin();
454
455         while (pushmsg_it != newPushMsgCBList.end())
456         {
457                 if (pushmsg_it->listenerFd == fd)
458                 {
459                         pushmsg_it = newPushMsgCBList.erase(pushmsg_it);
460                 }
461                 else
462                 {
463                         ++pushmsg_it;
464                 }
465         }
466
467         // remove all newCBMsgCBs for fd
468         cbmsg_list::iterator cbmsg_it = newCBMsgCBList.begin();
469         bool bSave = false;
470
471         while (cbmsg_it != newCBMsgCBList.end())
472         {
473                 if (cbmsg_it->listenerFd == fd)
474                 {
475                         cbmsg_it = newCBMsgCBList.erase(cbmsg_it);
476                 }
477                 else
478                 {
479                         if(cbmsg_it->bsave == true)
480                                 bSave = true;
481                         ++cbmsg_it;
482                 }
483         }
484
485         // remove all operationSyncMLMsgCBs for fd
486         syncmlop_list::iterator syncmlop_it = operationSyncMLMsgCBList.begin();
487
488         while (syncmlop_it != operationSyncMLMsgCBList.end())
489         {
490                 if (syncmlop_it->listenerFd == fd)
491                 {
492                         syncmlop_it = operationSyncMLMsgCBList.erase(syncmlop_it);
493                 }
494                 else
495                 {
496                         ++syncmlop_it;
497                 }
498         }
499
500         // remove storage change callback for fd
501         storageChangeFdMap.erase(fd);
502
503         MSG_END();
504 }
505
506
507 bool MsgTransactionManager::checkPrivilege(MSG_CMD_TYPE_T CmdType, const char *pCookie)
508 {
509         if (CmdType >= MSG_CMD_PLG_SENT_STATUS_CNF && CmdType <= MSG_CMD_PLG_INIT_SIM_BY_SAT)
510         {
511                 MSG_DEBUG("Request from Plug-in");
512                 return true;
513         }
514
515         // Get Cookie from APP
516         if (pCookie == NULL)
517         {
518                 MSG_DEBUG("Cookie is NULL");
519                 return false;
520         }
521
522 #ifdef MSG_FOR_DEBUG
523         for (int i = 0; i < MAX_COOKIE_LEN; i++)
524         {
525                 MSG_DEBUG("cookie : [%02x]", pCookie[i]);
526         }
527 #endif
528
529         // Check Cookie
530         size_t cookieSize;
531         gid_t gid;
532
533         cookieSize = security_server_get_cookie_size();
534
535         MSG_DEBUG("cookie size : [%d]", cookieSize);
536
537 //      char cookie[MAX_COOKIE_LEN];
538
539         // Get GID
540         if (CmdType == MSG_CMD_REG_INCOMING_SYNCML_MSG_CB)
541         {
542                 MSG_DEBUG("get GID for message_sync");
543                 gid = security_server_get_gid("message_sync");
544         }
545         else if (CmdType == MSG_CMD_REG_INCOMING_LBS_MSG_CB)
546         {
547                 MSG_DEBUG("get GID for message_lbs");
548                 gid = security_server_get_gid("message_lbs");
549         }
550         else
551         {
552                 MSG_DEBUG("get GID for message");
553                 gid = security_server_get_gid("message");
554         }
555
556         MSG_DEBUG("gid [%d]", gid);
557
558         int retVal = 0;
559
560         retVal = security_server_check_privilege(pCookie, gid);
561
562         if (retVal < 0)
563         {
564                 if (retVal == SECURITY_SERVER_API_ERROR_ACCESS_DENIED)
565                 {
566                         MSG_DEBUG("access denied !! [%d]", retVal);
567                 }
568                 else
569                 {
570                         MSG_DEBUG("fail to check privilege [%d]", retVal);
571                 }
572
573                 return false;
574         }
575
576         MSG_DEBUG("privilege check success !!");
577
578         return true;
579 }
580
581
582 void MsgTransactionManager::setSentStatusCB(int listenerFd)
583 {
584         if (listenerFd <= 0)
585                 THROW(MsgException::INVALID_PARAM,"InParam Error: listenerFd %d",listenerFd);
586
587         statusCBFdMap[listenerFd] = true;
588 }
589
590
591 void MsgTransactionManager::setIncomingMsgCB(MSG_CMD_REG_INCOMING_MSG_CB_S *pCbInfo)
592 {
593         if (!pCbInfo)
594         {
595                 MSG_FATAL("cbinfo NULL");
596                 return;
597         }
598
599         newmsg_list::iterator it = newMsgCBList.begin();
600
601         for (; it != newMsgCBList.end(); it++)
602         {
603                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType) && (it->port == pCbInfo->port))
604                 {
605                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d, port %d", it->listenerFd, it->msgType, it->port);
606                         return;
607                 }
608         }
609
610         newMsgCBList.push_back(*pCbInfo);
611 }
612
613
614 void MsgTransactionManager::setMMSConfMsgCB(MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S *pCbInfo)
615 {
616         if (!pCbInfo)
617         {
618                 MSG_FATAL("cbinfo NULL");
619                 return;
620         }
621
622         mmsconf_list::iterator it = newMMSConfMsgCBList.begin();
623
624         for (; it != newMMSConfMsgCBList.end(); it++)
625         {
626                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType) && (!strncmp(it->appId, pCbInfo->appId, MAX_MMS_JAVA_APPID_LEN)))
627                 {
628                         MSG_DEBUG("Duplicated MMSConfMessageCB info fd:%d, mType:%d, appId:%s", it->listenerFd, it->msgType, it->appId);
629                         return;
630                 }
631         }
632
633         newMMSConfMsgCBList.push_back(*pCbInfo);
634 }
635
636
637 void MsgTransactionManager::setPushMsgCB(MSG_CMD_REG_INCOMING_PUSH_MSG_CB_S *pCbInfo)
638 {
639         if (!pCbInfo)
640         {
641                 MSG_FATAL("cbinfo NULL");
642                 return;
643         }
644
645         pushmsg_list::iterator it = newPushMsgCBList.begin();
646
647         for (; it != newPushMsgCBList.end(); it++)
648         {
649                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
650                 {
651                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
652                         return;
653                 }
654         }
655
656         newPushMsgCBList.push_back(*pCbInfo);
657 }
658
659 void MsgTransactionManager::setCBMsgCB(MSG_CMD_REG_INCOMING_CB_MSG_CB_S *pCbInfo)
660 {
661         if (!pCbInfo)
662         {
663                 MSG_FATAL("cbinfo NULL");
664                 return;
665         }
666
667         cbmsg_list::iterator it = newCBMsgCBList.begin();
668
669         for (; it != newCBMsgCBList.end(); it++)
670         {
671                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
672                 {
673                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
674                         return;
675                 }
676         }
677
678         if(pCbInfo->bsave)
679                 MsgSettingSetBool(CB_SAVE, pCbInfo->bsave);
680
681
682         newCBMsgCBList.push_back(*pCbInfo);
683 }
684
685 void MsgTransactionManager::setSyncMLMsgCB(MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S *pCbInfo)
686 {
687         if (!pCbInfo)
688         {
689                 MSG_FATAL("cbinfo NULL");
690                 return;
691         }
692
693         syncmlmsg_list::iterator it = newSyncMLMsgCBList.begin();
694
695         for (; it != newSyncMLMsgCBList.end(); it++)
696         {
697                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
698                 {
699                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
700                         return;
701                 }
702         }
703
704         newSyncMLMsgCBList.push_back(*pCbInfo);
705 }
706
707
708 void MsgTransactionManager::setLBSMsgCB(MSG_CMD_REG_INCOMING_LBS_MSG_CB_S *pCbInfo)
709 {
710         if (!pCbInfo)
711         {
712                 MSG_FATAL("cbinfo NULL");
713                 return;
714         }
715
716         lbsmsg_list::iterator it = newLBSMsgCBList.begin();
717
718         for (; it != newLBSMsgCBList.end(); it++)
719         {
720                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
721                 {
722                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
723                         return;
724                 }
725         }
726
727         newLBSMsgCBList.push_back(*pCbInfo);
728 }
729
730
731 void MsgTransactionManager::setJavaMMSList(MSG_CMD_REG_INCOMING_JAVAMMS_TRID_S *pTrId)
732 {
733         if (!pTrId)
734         {
735                 MSG_FATAL("trId NULL");
736                 return;
737         }
738
739         javamms_list::iterator it;
740
741         for (it = javaMMSList.begin(); it != javaMMSList.end(); it++)
742         {
743                 if (!strcmp(it->id, pTrId->id))
744                 {
745                         MSG_DEBUG("Duplicated javaMMS transaction Id:%s", it->id);
746                         return;
747                 }
748         }
749
750         javaMMSList.push_back(*pTrId);
751 }
752
753
754 void MsgTransactionManager::setSyncMLMsgOperationCB(MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S *pCbInfo)
755 {
756         if (!pCbInfo)
757         {
758                 MSG_FATAL("cbinfo NULL");
759                 return;
760         }
761
762         syncmlop_list::iterator it = operationSyncMLMsgCBList.begin();
763
764         for (; it != operationSyncMLMsgCBList.end(); it++)
765         {
766                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
767                 {
768                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
769                         return;
770                 }
771         }
772
773         operationSyncMLMsgCBList.push_back(*pCbInfo);
774 }
775
776
777 void MsgTransactionManager::setStorageChangeCB(int listenerFd)
778 {
779         if (listenerFd <= 0)
780                 THROW(MsgException::INVALID_PARAM,"InParam Error: listenerFd %d", listenerFd);
781
782         storageChangeFdMap[listenerFd] = true;
783 }
784
785
786 javamms_list& MsgTransactionManager::getJavaMMSList()
787 {
788         return javaMMSList;
789 }
790
791
792 void MsgTransactionManager::broadcastIncomingMsgCB(const msg_error_t err, const MSG_MESSAGE_INFO_S *msgInfo)
793 {
794         MSG_BEGIN();
795
796         char* pEventData = NULL;
797         AutoPtr<char> eventBuf(&pEventData);
798
799         int eventSize = MsgMakeEvent(msgInfo, sizeof(MSG_MESSAGE_INFO_S), MSG_EVENT_PLG_INCOMING_MSG_IND, err, (void**)(&pEventData));
800
801         MSG_DEBUG("valid %d dstport %d", msgInfo->msgPort.valid, msgInfo->msgPort.dstPort);
802
803         newmsg_list::iterator it = newMsgCBList.begin();
804
805         for (; it != newMsgCBList.end(); it++)
806         {
807                 MSG_DEBUG("fd %d dstport %d",it->listenerFd, it->port);
808
809                 if ((msgInfo->msgPort.valid == false) && (it->port == 0))
810                 {
811                         MSG_DEBUG("Send incoming normal msg to listener %d", it->listenerFd);
812                         write(it->listenerFd, pEventData, eventSize);
813                 }
814                 else if ((msgInfo->msgPort.valid == true) && (it->port == msgInfo->msgPort.dstPort))
815                 {
816                         MSG_DEBUG("Send incoming port msg to listener %d", it->listenerFd);
817                         write(it->listenerFd, pEventData, eventSize);
818                 }
819         }
820
821         MSG_END();
822 }
823
824
825 void MsgTransactionManager::broadcastMMSConfCB(const msg_error_t err, const MSG_MESSAGE_INFO_S *msgInfo, const MMS_RECV_DATA_S *mmsRecvData)
826 {
827         MSG_BEGIN();
828
829         char* pEventData = NULL;
830         AutoPtr<char> eventBuf(&pEventData);
831
832         int eventSize = MsgMakeEvent(msgInfo, sizeof(MSG_MESSAGE_INFO_S), MSG_EVENT_PLG_INCOMING_MMS_CONF, err, (void**)(&pEventData));
833
834         mmsconf_list::iterator it = newMMSConfMsgCBList.begin();
835
836         for (; it != newMMSConfMsgCBList.end(); it++)
837         {
838                 MSG_DEBUG("fd:%d appId:%s",it->listenerFd, it->appId);
839
840                 if (mmsRecvData->msgAppId.valid == true)
841                 {
842                         if (!strcmp(it->appId, mmsRecvData->msgAppId.appId))
843                         {
844                                 MSG_DEBUG("Send incoming java msg to listener %d", it->listenerFd);
845                                 write(it->listenerFd, pEventData, eventSize);
846                         }
847                 }
848                 else
849                 {
850                         if (strlen(it->appId) <= 0)
851                         {
852                                 MSG_DEBUG("Send incoming normal msg to listener %d", it->listenerFd);
853                                 write(it->listenerFd, pEventData, eventSize);
854                         }
855                 }
856         }
857
858
859         MSG_END();
860 }
861
862 void MsgTransactionManager::broadcastPushMsgCB(const msg_error_t err, const MSG_PUSH_MESSAGE_DATA_S *pushData)
863 {
864         MSG_BEGIN();
865
866         char* pEventData = NULL;
867         AutoPtr<char> eventBuf(&pEventData);
868
869         int eventSize = MsgMakeEvent(pushData, sizeof(MSG_PUSH_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_PUSH_MSG_IND, err, (void**)(&pEventData));
870
871         pushmsg_list::iterator it = newPushMsgCBList.begin();
872
873         for (; it != newPushMsgCBList.end(); it++)
874         {
875                 if (!strcmp(it->appId, pushData->pushAppId))
876                 {
877                         MSG_DEBUG("Send incoming Push information to listener %d", it->listenerFd);
878                         write(it->listenerFd, pEventData, eventSize);
879                 }
880         }
881
882         MSG_END();
883 }
884
885 void MsgTransactionManager::broadcastCBMsgCB(const msg_error_t err, const MSG_CB_MSG_S *cbMsg)
886 {
887         MSG_BEGIN();
888
889         char* pEventData = NULL;
890         AutoPtr<char> eventBuf(&pEventData);
891
892         int eventSize = MsgMakeEvent(cbMsg, sizeof(MSG_CB_MSG_S), MSG_EVENT_PLG_INCOMING_CB_MSG_IND, err, (void**)(&pEventData));
893
894         cbmsg_list::iterator it = newCBMsgCBList.begin();
895
896         for (; it != newCBMsgCBList.end(); it++)
897         {
898                 MSG_DEBUG("Send incoming CB information to listener %d", it->listenerFd);
899                 write(it->listenerFd, pEventData, eventSize);
900         }
901
902         MSG_END();
903 }
904
905 void MsgTransactionManager::broadcastSyncMLMsgCB(const msg_error_t err, const MSG_SYNCML_MESSAGE_DATA_S *syncMLData)
906 {
907         MSG_BEGIN();
908
909         char* pEventData = NULL;
910         AutoPtr<char> eventBuf(&pEventData);
911
912         int eventSize = MsgMakeEvent(syncMLData, sizeof(MSG_SYNCML_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_SYNCML_MSG_IND, err, (void**)(&pEventData));
913
914         syncmlmsg_list::iterator it = newSyncMLMsgCBList.begin();
915
916         for (; it != newSyncMLMsgCBList.end(); it++)
917         {
918                 MSG_DEBUG("Send incoming SyncML information to listener %d", it->listenerFd);
919                 write(it->listenerFd, pEventData, eventSize);
920         }
921
922         MSG_END();
923 }
924
925
926 void MsgTransactionManager::broadcastLBSMsgCB(const msg_error_t err, const MSG_LBS_MESSAGE_DATA_S *lbsData)
927 {
928         MSG_BEGIN();
929
930         char* pEventData = NULL;
931         AutoPtr<char> eventBuf(&pEventData);
932
933         int eventSize = MsgMakeEvent(lbsData, sizeof(MSG_LBS_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_LBS_MSG_IND, err, (void**)(&pEventData));
934
935         lbsmsg_list::iterator it = newLBSMsgCBList.begin();
936
937         for (; it != newLBSMsgCBList.end(); it++)
938         {
939                 MSG_DEBUG("Send incoming LBS msg to listener %d", it->listenerFd);
940                 write(it->listenerFd, pEventData, eventSize);
941         }
942
943         MSG_END();
944 }
945
946
947 void MsgTransactionManager::broadcastSyncMLMsgOperationCB(const msg_error_t err, const int msgId, const int extId)
948 {
949         MSG_BEGIN();
950
951         char* pEventData = NULL;
952         AutoPtr<char> eventBuf(&pEventData);
953
954         char* encodedData = NULL;
955         AutoPtr<char> buf(&encodedData);
956
957         // Encoding Storage Change Data
958         int dataSize = MsgEncodeSyncMLOperationData(msgId, extId, &encodedData);
959
960         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_SYNCML_OPERATION, err, (void**)(&pEventData));
961
962         syncmlop_list::iterator it = operationSyncMLMsgCBList.begin();
963
964         for( ; it != operationSyncMLMsgCBList.end() ; it++ )
965         {
966                 MSG_DEBUG("Send SyncML operation to listener %d", it->listenerFd);
967                 write(it->listenerFd, pEventData, eventSize);
968         }
969
970         MSG_END();
971 }
972
973
974 void MsgTransactionManager::broadcastStorageChangeCB(const msg_error_t err, const msg_storage_change_type_t storageChangeType, const msg_id_list_s *pMsgIdList)
975 {
976         MSG_BEGIN();
977
978         if(pMsgIdList == NULL) {
979                 MSG_DEBUG("pMsgIdList is NULL.");
980                 return;
981         }
982
983         MSG_DEBUG("storageChangeType [%d]", storageChangeType);
984
985         int dataSize = 0;
986
987         char* pEventData = NULL;
988         AutoPtr<char> eventBuf(&pEventData);
989
990         char* encodedData = NULL;
991         AutoPtr<char> buf(&encodedData);
992
993         // Encoding Storage Change Data
994         dataSize = MsgEncodeStorageChangeData(storageChangeType, pMsgIdList, &encodedData);
995
996         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_PLG_STORAGE_CHANGE_IND, err, (void**)(&pEventData));
997
998         fd_map::iterator it = storageChangeFdMap.begin();
999
1000         for (; it != storageChangeFdMap.end(); it++)
1001         {
1002                 MSG_DEBUG("Send Storage Change Callback to listener %d", it->first);
1003                 write(it->first, pEventData, eventSize);
1004         }
1005
1006         MSG_END();
1007 }
1008
1009
1010 void MsgTransactionManager::setTMStatus()
1011 {
1012         MSG_BEGIN();
1013         mx.lock();
1014         cv.signal();
1015         mx.unlock();
1016         MSG_END();
1017 }
1018
1019
1020 void MsgTransactionManager::getTMStatus()
1021 {
1022         MSG_BEGIN();
1023         mx.lock();
1024
1025         int ret = 0;
1026
1027         ret = cv.timedwait(mx.pMutex(), 3);
1028
1029         mx.unlock();
1030
1031         if (ret == ETIMEDOUT)
1032         {
1033                 MSG_DEBUG("MsgTransactionManager::getTMStatus TIME-OUT");
1034         }
1035         MSG_END();
1036 }