dmServerSession.cc revision 3d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51
1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/*
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * Copyright (C) 2014 The Android Open Source Project
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License");
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * you may not use this file except in compliance with the License.
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * You may obtain a copy of the License at
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *      http://www.apache.org/licenses/LICENSE-2.0
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) *
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * Unless required by applicable law or agreed to in writing, software
11010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS,
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * See the License for the specific language governing permissions and
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) * limitations under the License.
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) */
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/*==================================================================================================
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Source Name: DMServerSession.cc
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    General Description: Implementation of DMServerSession class.
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)==================================================================================================*/
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "dmServerSession.h"
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "dm_ua_handlecommand.h"
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "dm_tree_util.h"
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "xpl_dm_Manager.h"
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "xpl_dm_Notifications.h"
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "dmLockingHelper.h"
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef DM_NO_LOCKING
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "dmThreadQueue.h"
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)extern "C" {
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "xpt-b64.h"
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "stdio.h"
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "xpl_Logger.h"
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)extern int g_cancelSession;
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/*==================================================================================================
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                 SOURCE FUNCTIONS
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)==================================================================================================*/
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/*==================================================================================================
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)FUNCTION        : DMServerSession::DMServerSession
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)DESCRIPTION     : The class constructor.
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ARGUMENT PASSED : sml_ContentType
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  sml_EncodingType
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)OUTPUT PARAMETER:
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)RETURN VALUE    :
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)IMPORTANT NOTES :
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)==================================================================================================*/
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)DMServerSession::DMServerSession()
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles){
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    m_nSecState = DM_CLIENT_NO_SERVER_NO_AUTH;
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/*==================================================================================================
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)FUNCTION        : DMServerSession::ConnectServer
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)DESCRIPTION     : The SessionStart() function calls this function to connect to the server.
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  The function will perform the following operations:
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  1) Get the DM account URI from the DM tree.
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  2) Call SYNCML_DM_TransportController::Connect() function to establish the DM
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                     session clent/server connection.
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ARGUMENT PASSED : server_Id
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)OUTPUT PARAMETER:
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)RETURN VALUE    :
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)IMPORTANT NOTES :
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)==================================================================================================*/
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)SYNCML_DM_RET_STATUS_T
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)DMServerSession::ConnectServer(CPCHAR pServerId)
86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles){
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    SYNCML_DM_RET_STATUS_T  ret_stat = SYNCML_DM_SUCCESS;
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    XPL_ADDR_TYPE_T         addressType = XPL_ADDR_DEFAULT;
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    CPCHAR                  szDMAccRootPath = ::XPL_DM_GetEnv( SYNCML_DM_DMACC_ROOT_PATH );
90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    CPCHAR                  szServerIdNodeName = ::XPL_DM_GetEnv( SYNCML_DM_NODENAME_SERVERID );
91a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     XPL_LOG_DM_SESS_Debug(("Entered DMServerSession::ConnectServer\n"));
94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
95a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    /* Get the current server ID's DM account URI */
96a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if ( !dmTreeObj.GetParentOfKeyValue (pServerId,
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                         szServerIdNodeName,
98a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                         szDMAccRootPath,
99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                         clientServerCreds.pDMAccNodeName) )
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    {
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      XPL_LOG_DM_SESS_Error(("Cannot Get Parent for %s\n",szServerIdNodeName ));
102a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return SYNCML_DM_FAIL;
103a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
104a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
105a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    DMGetData oDataAddr, oDataAddrType, oDataPort, oData;
106a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
107a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ret_stat = dmTreeObj.GetDefAccountAddrInfo((CPCHAR)clientServerCreds.pDMAccNodeName,
108a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                               oDataAddr,
109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                               oDataAddrType,
110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                               oDataPort );
111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
112a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if ( ret_stat != SYNCML_DM_SUCCESS )
113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    {
114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      XPL_LOG_DM_SESS_Error(("Cannot Get Account Address Info for %s\n",(CPCHAR)clientServerCreds.pDMAccNodeName ));
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return ret_stat;
116a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if ( dmTreeObj.IsVersion_12()  )
119a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    {
120a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)	    // Get the value of the pbDMAccUri/PrefConRef node
121a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)	    addressType = XPL_ADDR_HTTP;
122a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)	    ret_stat = dmTreeObj.GetAccNodeValue((CPCHAR)clientServerCreds.pDMAccNodeName,
123a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)	                                          DM_PREFCONREF,
124a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)	                                          oData);
125a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
126a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    else
127a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    {
128a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)           if ( oDataAddrType.getCharData() )
129a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)   		addressType = DmAtoi(oDataAddrType.getCharData());
130a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)	    else
131a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)	       addressType = XPL_ADDR_HTTP;
132a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
133a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)	    if (  addressType != XPL_ADDR_HTTP && addressType != XPL_ADDR_WSP )
134a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)			return SYNCML_DM_FAIL;
135a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
136a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)   	    ret_stat = dmTreeObj.GetAccNodeValue((CPCHAR)clientServerCreds.pDMAccNodeName,
137a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)	                                          DM_CONREF,
138a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)	                                          oData);
139a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
140a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
141a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if ( ret_stat == SYNCML_DM_SUCCESS )
142a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     { dmTreeObj.SetConRef(oData.getCharData());
143a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      	ret_stat = m_oConnObject.Init(g_iDMWorkspaceSize,addressType,oData.getCharData());
144a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
145a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    else
146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        if ( ret_stat == SYNCML_DM_NOT_FOUND )
147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)         { 	dmTreeObj.SetConRef(NULL);
148a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)         	ret_stat = m_oConnObject.Init(g_iDMWorkspaceSize,addressType,NULL);
149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)         }
150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    XPL_LOG_DM_SESS_Debug (("Leaving DMServerSession::ConnectServer ret_stat=%d\n",ret_stat));
152a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return (ret_stat);
153a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
154a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
155a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
156a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/*==================================================================================================
157a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)FUNCTION        : DMServerSession::BuildSendPackageOne
158a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
159a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)DESCRIPTION     : SessionStart function calls this function to build the DM package one and send it
160a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  to the server.
161a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  The function will perform the following operations:
162a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  1) Create the SYNCML_DM_BuildPackage object.
163010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                  2) Build the package one document.
164a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  3) Send the package one to the Transport Binding.
165a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ARGUMENT PASSED : session_Direction
166a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  p_ParsedPk0
167a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)OUTPUT PARAMETER:
168a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)RETURN VALUE    : STYNCML_DM_SUCCES for success or SYNCML_DM_FAIL for failure
169a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)IMPORTANT NOTES :
170a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
171a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
172a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)==================================================================================================*/
173a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)SYNCML_DM_RET_STATUS_T
174a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)DMServerSession::BuildSendPackageOne(CPCHAR pServerID, DmtSessionProp * pSessionProp)
175a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles){
176a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    XPL_LOG_DM_SESS_Debug (("Entering DMServerSession::BuildSendPackageOne serverId = %s\n", pServerID));
177a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    SYNCML_DM_RET_STATUS_T  ret_stat = SYNCML_DM_SUCCESS;
178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    m_oPkgBuilder.Init(this);
179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
180a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef DM_NO_LOCKING
181a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    INT32 nLockID = 0;
182a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    {
183a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        DMLockingHelper oLock( 0, ".", pServerID,SYNCML_DM_LOCK_TYPE_EXCLUSIVE, FALSE);
184a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        nLockID = oLock.GetID();
185a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
186a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        if ( !oLock.IsLockedSuccessfully() )
187a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        {
188a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            return SYNCML_DM_FAIL;
189a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        }
190a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
191a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        ret_stat = m_oPkgBuilder.BuildPackageOne(pServerID, pSessionProp);
192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
194a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    dmTreeObj.ReleaseLock( nLockID );
195a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#else
196a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ret_stat = m_oPkgBuilder.BuildPackageOne(pServerID, pSessionProp);
197a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif
198a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if ( ret_stat == SYNCML_DM_SUCCESS )
199a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    {
200a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        ret_stat = SendPackage();
201a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
202a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
203010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return (ret_stat);
204a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
205a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
206a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
207a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/*==================================================================================================
208a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)FUNCTION        : DMServerSession::SessionStart
209a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
210a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)DESCRIPTION     : The UserAgen::SessionStart calls this function after it creates MgmtSession object.
211a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  The function will perform the following operations:
212a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  1) Call SessionStart() to setup the DM tree.
213a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  2) Register the DM engine with the SYNCML toolkit.
214a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  3) Connect the client with the server.
215a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  4) Build and send the package one.
216a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)ARGUMENT PASSED : p_SessionStart
217a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)OUTPUT PARAMETER:
218a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)RETURN VALUE    :
219a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)IMPORTANT NOTES :
220a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
221a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
222a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)==================================================================================================*/
223a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)SYNCML_DM_RET_STATUS_T
224a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)DMServerSession::Start(CPCHAR pServerID,
225a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                       DmtSessionProp * pSessionProp)
226a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles){
227a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    SYNCML_DM_RET_STATUS_T ret_stat;
228a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    BOOLEAN bMoreMessage = TRUE;
229a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
230a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ret_stat = Init(pSessionProp->isWBXML());
231a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if ( ret_stat != SYNCML_DM_SUCCESS )
232a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        return ret_stat;
233
234    /* Register the DM engine with the SYNCML toolkit. */
235    userData.pSessionMng = this;
236    userData.pPkgBuilder = &m_oPkgBuilder;
237    ret_stat = RegisterDmEngineWithSyncmlToolkit(&userData);
238    if ( ret_stat != SYNCML_DM_SUCCESS )
239    {
240        XPL_LOG_DM_SESS_Debug(("Exiting: RegisterDmEngineWithSyncmlToolkit failed\n"));
241        return (ret_stat);
242    }
243
244    /* Create connection between server and client */
245    ret_stat = ConnectServer(pServerID);
246    if (ret_stat != SYNCML_DM_SUCCESS)
247    {
248        XPL_LOG_DM_SESS_Error(("Cannot connect server\n"));
249        return (ret_stat);
250    }
251
252    /* Remember the sessionDirection in case we have to resend the Alert.*/
253    pSessionProp->generateSessionID();
254    serverSessionId = pSessionProp->getSessionID();
255
256
257    /* Build and send the package one to the Server. */
258    ret_stat = BuildSendPackageOne(pServerID, pSessionProp);
259    if (ret_stat != SYNCML_DM_SUCCESS)
260        return (ret_stat);
261
262    XPL_DM_NotifySessionProgress(TRUE);
263
264    while (bMoreMessage == TRUE && g_cancelSession == 0)
265    {
266        /* Call the method to receive the package from the transport.*/
267        ret_stat = RecvPackage();
268        if (ret_stat != SYNCML_DM_SUCCESS)
269        {
270             XPL_DM_NotifySessionProgress(FALSE);
271            return ret_stat;
272        }
273
274        /* Call the method to parse and handle the commands in the package.*/
275        commandCount = 0;
276
277 #ifndef DM_NO_LOCKING
278        int nLockID = 0;
279
280        {
281           DMLockingHelper oLock( 0, ".", pServerID, SYNCML_DM_LOCK_TYPE_EXCLUSIVE, FALSE );
282           nLockID = oLock.GetID();
283
284           if ( !oLock.IsLockedSuccessfully() )
285           {
286               XPL_DM_NotifySessionProgress(FALSE);
287               return SYNCML_DM_FAIL;
288           }
289
290           ret_stat = ParseMessage();
291        }
292
293        dmTreeObj.ReleaseLock( nLockID );
294#else
295        ret_stat = ParseMessage();
296#endif
297
298        if (ret_stat != SYNCML_DM_SUCCESS)
299        {
300            XPL_DM_NotifySessionProgress(FALSE);
301            return ret_stat;
302        }
303
304        /* Check to see if we are passed the max retries */
305        if (serverRetryCount > MAX_AUTH_RETRY || clientRetryCount > MAX_AUTH_RETRY)
306        {
307             XPL_DM_NotifySessionProgress(FALSE);
308             return SYNCML_DM_SESSION_AUTH_FAIL;
309        }
310
311        /* Check to see if any operational commands were parsed.*/
312        if ( commandCount != 0 && m_bSessionAborted == FALSE && g_cancelSession == 0)
313        {
314            ret_stat = SendPackage();
315            if (ret_stat != SYNCML_DM_SUCCESS)
316            {
317                XPL_DM_NotifySessionProgress(FALSE);
318                return ret_stat;
319            }
320        }
321        else
322        {
323            /* End the DM Session.*/
324            bMoreMessage = FALSE;
325
326        }
327    }
328
329    XPL_DM_NotifySessionProgress(FALSE);
330    return SYNCML_DM_SUCCESS;
331}
332
333
334/*==================================================================================================
335FUNCTION        : DMServerSession::SetURI
336
337DESCRIPTION     : The UserAgent::SetURI will call this function if receiving package set the
338                  response URI to a new value.
339                  The function will call Connection object to set the URI of the SyncML server
340                  for HTTP/WSP.
341ARGUMENT PASSED : p_RespURI
342OUTPUT PARAMETER:
343RETURN VALUE    : SYNCML_DM_SUCCESS if success,
344                  SYNCML_DM_FAIL if failed.
345IMPORTANT NOTES :
346
347
348==================================================================================================*/
349SYNCML_DM_RET_STATUS_T
350DMServerSession::SetURI (const char *p_RespURI)
351{
352    return m_oConnObject.SetURI(p_RespURI);
353}
354
355
356
357/*==================================================================================================
358FUNCTION        : DMServerSession::RecvPackage
359
360DESCRIPTION     : The UserAgent::TransportMsg will call this function when data is received
361                  This function calls SYNCML_DM_Connection::Recv() to get the DM docuemnt from the
362                  Transport.
363ARGUMENT PASSED :
364OUTPUT PARAMETER:
365RETURN VALUE    :
366IMPORTANT NOTES :
367
368
369==================================================================================================*/
370SYNCML_DM_RET_STATUS_T
371DMServerSession::RecvPackage()
372{
373    SYNCML_DM_RET_STATUS_T      serverAuthStatus = SYNCML_DM_SUCCESS;
374    SYNCMLDM_SEC_CREDENTIALS_T  *pGenHmac;
375    UINT8                       decodedNonce[MAX_BIN_NONCE_LEN];
376    UINT32                      encodedNonceLen =0;
377    UINT32                      decodedNonceLen =0;
378    SYNCML_DM_RET_STATUS_T      retStat = SYNCML_DM_SUCCESS;
379
380
381    pWritePos = recvSmlDoc.pData;
382
383    /* Unlock the workspace so the Toolkit controls it again. */
384    smlUnlockReadBuffer(sendInstanceId, workspaceUsedSize);
385
386    /* Check if the HMAC-MD5 headers were included.*/
387    if( m_oRecvCredHeaders.empty() == FALSE )
388    {
389        //XPL_LOG_DM_SESS_Debug(("m_oRecvCredHeaders is not empty.\n"));
390        //XPL_LOG_DM_SESS_Debug(("m_oRecvCredHeaders: username:%s\n", (CPCHAR)(m_oRecvCredHeaders.m_oUserName.getBuffer())));
391        //XPL_LOG_DM_SESS_Debug(("m_oRecvCredHeaders: mac:%s\n", (CPCHAR)(m_oRecvCredHeaders.m_oMac.getBuffer())));
392
393        SYNCMLDM_HMAC_SEC_INFO_T    hmacSecInfo;
394        // since server sends hmac, use hmac after that
395        clientServerCreds.SetPrefServerAuth(SYNCML_DM_CHAL_HMAC);
396
397         /* Remember that the Cred Headers are here.*/
398        SetServCredsMissing(FALSE);
399
400        /* Copy the received DM document into the SyncML workspace */
401        /* Create the Server Credentials.*/
402        hmacSecInfo.pb_user_name_or_server_id = (UINT8*)clientServerCreds.pServerId.c_str();
403        hmacSecInfo.pb_password = (UINT8*)clientServerCreds.pServerPW.c_str();
404        hmacSecInfo.o_encode_base64 = TRUE;         /* Always true for HMAC */
405        hmacSecInfo.pb_syncml_document = pWritePos; /* Pointer for SyncML Doc */
406        hmacSecInfo.dw_syncml_document_length = recvSmlDoc.dataSize; /* Size of Doc*/
407
408         /* Validate the HMAC-MD5 header from the HTTP header.
409          * We will check every message regardless of the Security state.
410          * The ServerNonce string is b64 encoded and must be decoded now.*/
411        if(clientServerCreds.pServerNonce != NULL)
412        {
413            encodedNonceLen = DmStrlen((const char *)clientServerCreds.pServerNonce);
414            decodedNonceLen = base64Decode((unsigned char *)decodedNonce,
415                                            MAX_BIN_NONCE_LEN,
416                                            (unsigned char*)clientServerCreds.pServerNonce.c_str(),
417                                            (unsigned long*)&encodedNonceLen);
418        }
419
420         /* Generate the Server Credentials.*/
421        hmacSecInfo.pb_nonce = decodedNonce;
422        hmacSecInfo.w_nonce_length = decodedNonceLen;
423
424        pGenHmac = syncmldm_sec_build_hmac_cred((const SYNCMLDM_HMAC_SEC_INFO_T *)&hmacSecInfo);
425
426         /* Compare the created creds to the received creds.*/
427        if ( pGenHmac == NULL || !m_oRecvCredHeaders.m_oMac.compare((CPCHAR)pGenHmac->ab_credential_string,
428                                                                     pGenHmac->w_credential_string_length) )
429
430        {
431             serverAuthStatus = SYNCML_DM_UNAUTHORIZED;
432             serverRetryCount++;
433        }
434        else
435        {
436             /* Reset count since server is now authenticated.*/
437             serverRetryCount = 0;
438        }
439        DmFreeMem(pGenHmac);
440
441         /* Update the security state with the server authentication status. Note that
442          * we only check for cases that cause a change in the SecurityState.
443          */
444         switch (m_nSecState)
445         {
446            case DM_CLIENT_NO_SERVER_NO_AUTH:
447                if (serverAuthStatus == SYNCML_DM_SUCCESS)
448                    m_nSecState = DM_CLIENT_NO_SERVER_Y_AUTH;
449                break;
450
451            case DM_CLIENT_Y_SERVER_NO_AUTH:
452                if (serverAuthStatus == SYNCML_DM_SUCCESS)
453                    m_nSecState = DM_BOTH_CLIENT_SERVER_AUTH;
454                break;
455
456            case DM_CLIENT_NO_SERVER_Y_AUTH:
457                if (serverAuthStatus != SYNCML_DM_SUCCESS)
458                     m_nSecState = DM_CLIENT_NO_SERVER_NO_AUTH;
459                break;
460
461            case DM_BOTH_CLIENT_SERVER_AUTH:
462                if (serverAuthStatus != SYNCML_DM_SUCCESS)
463                    m_nSecState = DM_CLIENT_Y_SERVER_NO_AUTH;
464                break;
465
466            default:
467                /* The Security State is messed up, so reset it.*/
468                m_nSecState = DM_CLIENT_NO_SERVER_NO_AUTH;
469                break;
470         }
471         /* Update the User Agent's security state.*/
472
473    } /* p_cred_headers != NULL */
474    else
475    {
476        /* The HMAC-MD5 creditials are missing.*/
477        if ( !IsServerAuthorized() )
478        {
479            SetServCredsMissing(TRUE);
480            serverRetryCount++;
481        }
482        else
483            serverRetryCount = 0;
484
485    }
486
487    return(retStat);
488
489}
490
491
492/*==================================================================================================
493FUNCTION        : DMServerSession::SendPackage
494
495DESCRIPTION     : This function will be called when a DM package is built up and ready to send.
496                  The function will call SYNCML_DM_Connection::Send() to send the DM document to
497                  the remote server.
498ARGUMENT PASSED :
499OUTPUT PARAMETER:
500RETURN VALUE    :
501IMPORTANT NOTES : Both sendSmlDoc and pRecvSmlDoc must pass to pConnObject->Send. pConnObject will
502                  write receiving DM document to the pRecvSmlDoc after it sends sendSmlDoc.
503
504
505==================================================================================================*/
506SYNCML_DM_RET_STATUS_T
507DMServerSession::SendPackage()
508{
509    SYNCMLDM_HMAC_SEC_INFO_T       hmacSecInfo;
510    SYNCMLDM_SEC_CREDENTIALS_T    *pHmacCreds = NULL;
511    SYNCML_DM_RET_STATUS_T         ret_stat;
512    UINT8                          decodedNonce[MAX_BIN_NONCE_LEN];
513    UINT32                         encodedNonceLen = 0;
514    UINT32                         decodedNonceLen = 0;
515    Ret_t                          sml_ret_stat;
516
517     /* Lock the workspace for reading and writing SyncML document.
518     * These buffers will be unlocked after the UA receives SyncML document */
519    smlLockReadBuffer(sendInstanceId, &pReadPos, &workspaceUsedSize);
520
521    /* Set sendSmlDoc point to workspace */
522    sendSmlDoc.dataSize = workspaceUsedSize;
523    sendSmlDoc.pData = pReadPos;
524
525    /* The ClientNonce string is b64 encoded and must be decoded now.*/
526    if(clientServerCreds.pClientNonce != NULL)
527    {
528        const char *clientNonce = clientServerCreds.pClientNonce.c_str();
529        encodedNonceLen = DmStrlen(clientNonce);
530        if (encodedNonceLen == 0) {
531            clientNonce = SERVER_RESYNC_NONCE;
532            encodedNonceLen = DmStrlen(clientNonce);
533        }
534        decodedNonceLen = base64Decode((UINT8*)decodedNonce,
535                                       MAX_BIN_NONCE_LEN,
536                                       (unsigned char*)clientNonce,
537                                       (unsigned long*)&encodedNonceLen);
538    }
539    /* Let's make up our credentials before we send the package. */
540    hmacSecInfo.pb_user_name_or_server_id = (UINT8*)clientServerCreds.pClientUserName.c_str();
541    hmacSecInfo.pb_password = (UINT8*)clientServerCreds.pClientPW.c_str();
542    hmacSecInfo.pb_nonce = decodedNonce;
543    hmacSecInfo.pb_syncml_document = pReadPos; /* Used as the pointer to the SyncML Doc */
544    hmacSecInfo.o_encode_base64 = TRUE;        /* Always true for HMAC credentials */
545    hmacSecInfo.w_nonce_length = decodedNonceLen;
546    hmacSecInfo.dw_syncml_document_length = workspaceUsedSize; /* Size of the SyncML Doc */
547
548    if( clientServerCreds.AuthPrefCredType == SYNCML_DM_CHAL_HMAC )
549	    pHmacCreds = syncmldm_sec_build_hmac_cred((const SYNCMLDM_HMAC_SEC_INFO_T *)&hmacSecInfo);
550
551    m_oRecvCredHeaders.clear();
552    if ( pHmacCreds != NULL )
553    {
554        if ( pHmacCreds->w_credential_string_length )
555        {
556            m_oRecvCredHeaders.m_oMac.assign(pHmacCreds->ab_credential_string,pHmacCreds->w_credential_string_length);
557            if ( m_oRecvCredHeaders.m_oMac.getBuffer() == NULL )
558            {
559                FreeAndSetNull(pHmacCreds);
560                return SYNCML_DM_DEVICE_FULL;
561            }
562        }
563
564        m_oRecvCredHeaders.m_oAlgorithm.assign(SYNCML_MAC_ALG);
565        if ( m_oRecvCredHeaders.m_oAlgorithm.getBuffer() == NULL )
566        {
567            FreeAndSetNull(pHmacCreds);
568            return SYNCML_DM_DEVICE_FULL;
569        }
570
571        if ( clientServerCreds.pClientUserName )
572        {
573            m_oRecvCredHeaders.m_oUserName.assign((CPCHAR)clientServerCreds.pClientUserName);
574            if ( m_oRecvCredHeaders.m_oUserName.getBuffer() == NULL )
575            {
576                FreeAndSetNull(pHmacCreds);
577                return SYNCML_DM_DEVICE_FULL;
578            }
579        }
580    }
581
582    sml_ret_stat = smlLockWriteBuffer(recvInstanceId, &pWritePos, &workspaceFreeSize);
583
584    if ( sml_ret_stat == SML_ERR_OK )
585    {
586        recvSmlDoc.pData = pWritePos;
587        ret_stat = m_oConnObject.Send(&sendSmlDoc, &recvSmlDoc, smlContentType, &m_oRecvCredHeaders);
588    }
589    else
590        ret_stat = SYNCML_DM_FAIL;
591
592    FreeAndSetNull(pHmacCreds);
593    return (ret_stat);
594}
595
596
597/*==================================================================================================
598FUNCTION        : DMProcessScriptSession::SetServCredsMissing
599
600DESCRIPTION     : This function reset class data members value to defaults after one DM session is
601                  ended.
602ARGUMENT PASSED :
603OUTPUT PARAMETER:
604RETURN VALUE    :
605IMPORTANT NOTES :
606
607==================================================================================================*/
608void
609DMServerSession::SetServCredsMissing( BOOLEAN newIsServCredsMissing )
610{
611   isServCredsMissing = newIsServCredsMissing;
612}
613