Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
7.4 KiB

  1. #include "precomp.h"
  2. #include "confpolicies.h"
  3. #include "MapiMyInfo.h"
  4. #define USES_IID_IMailUser
  5. #define USES_IID_IAddrBook
  6. #define INITGUID
  7. #include <initguid.h>
  8. #include <mapiguid.h>
  9. #include <mapix.h>
  10. #include <mapiutil.h>
  11. #include <mapitags.h>
  12. HRESULT MAPIGetMyInfo()
  13. {
  14. HRESULT hr = E_FAIL;
  15. enum eProps
  16. {
  17. NAME,
  18. SUR_NAME,
  19. EMAIL,
  20. LOCATION,
  21. PHONENUM,
  22. COMMENT,
  23. PROP_COUNT
  24. };
  25. typedef struct tagData
  26. {
  27. DWORD dwPropVal;
  28. int iIndex;
  29. } PROPDATA;
  30. HMODULE hMapi = ::LoadLibrary( MAPIDLL );
  31. if( NULL == hMapi )
  32. {
  33. return hr;
  34. }
  35. LPMAPIINITIALIZE pfnMAPIInitialize = (LPMAPIINITIALIZE)GetProcAddress( hMapi, MAPIINITname );
  36. LPMAPILOGONEX pfnMAPILogonEx = (LPMAPILOGONEX)GetProcAddress( hMapi, MAPILOGONEXname );
  37. LPMAPIFREEBUFFER pfnMAPIFreeBuffer = (LPMAPIFREEBUFFER)GetProcAddress( hMapi, MAPIFREEBUFFERname );
  38. LPMAPIUNINITIALIZE pfnMAPIUninitialize = (LPMAPIUNINITIALIZE)GetProcAddress( hMapi, MAPIUNINITIALIZEname );
  39. if( !(pfnMAPIInitialize && pfnMAPILogonEx && pfnMAPIFreeBuffer && pfnMAPIUninitialize) )
  40. {
  41. return hr;
  42. }
  43. PROPDATA PropData[ PROP_COUNT ];
  44. ZeroMemory( PropData, sizeof( PropData ) );
  45. PropData[NAME].dwPropVal = ConfPolicies::GetGALName();
  46. PropData[SUR_NAME].dwPropVal = ConfPolicies::GetGALSurName();
  47. PropData[EMAIL].dwPropVal = ConfPolicies::GetGALEmail();
  48. PropData[LOCATION].dwPropVal = ConfPolicies::GetGALLocation();
  49. PropData[PHONENUM].dwPropVal = ConfPolicies::GetGALPhoneNum();
  50. PropData[COMMENT].dwPropVal = ConfPolicies::GetGALComment();
  51. // First four are required, rest are optional
  52. if( !( PropData[NAME].dwPropVal && PropData[SUR_NAME].dwPropVal && PropData[EMAIL].dwPropVal && PropData[PHONENUM].dwPropVal) )
  53. {
  54. ERROR_OUT(("One or more required MAPI property fields are not set"));
  55. return hr;
  56. }
  57. SizedSPropTagArray( PROP_COUNT, SizedPropTagArray );
  58. LPSPropTagArray lpSPropTagArray = (LPSPropTagArray) &SizedPropTagArray;
  59. ZeroMemory( lpSPropTagArray, sizeof( lpSPropTagArray ) );
  60. // We can not retrieve the same property from this array twice. Therefore never insert
  61. // until we are sure that the value is not there already.
  62. int insertAt;
  63. BOOL bPointAtNew = TRUE;
  64. int iCurPropTagArrayIndex = 0;
  65. for( int i = 0; i < PROP_COUNT; i++ )
  66. {
  67. if( PropData[i].dwPropVal )
  68. {
  69. bPointAtNew = TRUE;
  70. for( insertAt = 0; insertAt < iCurPropTagArrayIndex; insertAt++ )
  71. {
  72. if( PropData[insertAt].dwPropVal == PropData[i].dwPropVal )
  73. {
  74. bPointAtNew = FALSE;
  75. break;
  76. }
  77. }
  78. PropData[i].iIndex = insertAt;
  79. ++iCurPropTagArrayIndex;
  80. lpSPropTagArray->aulPropTag[PropData[i].iIndex] = PROP_TAG( PT_TSTRING, PropData[i].dwPropVal );
  81. if( bPointAtNew )
  82. {
  83. lpSPropTagArray->cValues++;
  84. }
  85. }
  86. }
  87. hr = pfnMAPIInitialize( NULL );
  88. if( SUCCEEDED( hr ) )
  89. {
  90. LPMAILUSER pMailUser = NULL;
  91. LPADRBOOK pAddrBook = NULL;
  92. LPMAPISESSION pMapiSession = NULL;
  93. LPSPropValue pPropValues = NULL;
  94. LPENTRYID pPrimaryIdentityEntryID = NULL;
  95. hr = pfnMAPILogonEx( NULL, // parent window
  96. NULL, // profile name
  97. NULL, // password
  98. MAPI_USE_DEFAULT | MAPI_NO_MAIL,
  99. &pMapiSession );
  100. if( SUCCEEDED( hr ) )
  101. {
  102. ULONG cbPrimaryIdentitySize = 0;
  103. hr = pMapiSession->QueryIdentity( &cbPrimaryIdentitySize,
  104. &pPrimaryIdentityEntryID );
  105. if( SUCCEEDED( hr ) )
  106. {
  107. hr = pMapiSession->OpenAddressBook( NULL, // parent window
  108. NULL, // Get an IAddrBook pointer
  109. AB_NO_DIALOG, // Supress UI interaction
  110. &pAddrBook );
  111. if( SUCCEEDED( hr ) )
  112. {
  113. ULONG uEntryType = 0;
  114. hr = pAddrBook->OpenEntry( cbPrimaryIdentitySize,
  115. pPrimaryIdentityEntryID,
  116. &IID_IMailUser,
  117. 0, // Flags
  118. &uEntryType,
  119. (LPUNKNOWN *)&pMailUser );
  120. if( SUCCEEDED( hr ) )
  121. {
  122. if( MAPI_MAILUSER == uEntryType )
  123. {
  124. ULONG cValues;
  125. hr = pMailUser->GetProps( lpSPropTagArray,
  126. fMapiUnicode,
  127. &cValues,
  128. &pPropValues );
  129. if( SUCCEEDED( hr ) )
  130. {
  131. // Check for full success
  132. for( i = 0; i < (int)lpSPropTagArray->cValues; i++ )
  133. {
  134. // We failed if a prop was specified, but none returned....
  135. if( ( PT_ERROR == LOWORD( pPropValues[i].ulPropTag ) ) && ( 0 != PropData[i].dwPropVal ) )
  136. {
  137. hr = E_FAIL;
  138. goto cleanup;
  139. }
  140. }
  141. // TODO - are there limitations on the length of this?
  142. RegEntry reIsapi( ISAPI_CLIENT_KEY, HKEY_CURRENT_USER );
  143. LPCTSTR pszName = pPropValues[ PropData[NAME].iIndex ].Value.LPSZ;
  144. if(pszName)
  145. {
  146. reIsapi.SetValue( REGVAL_ULS_FIRST_NAME, pszName );
  147. }
  148. LPCTSTR pszSurName = pPropValues[ PropData[SUR_NAME].iIndex ].Value.LPSZ;
  149. if(pszSurName)
  150. {
  151. reIsapi.SetValue( REGVAL_ULS_LAST_NAME, pszSurName );
  152. }
  153. LPCTSTR pszEmail = pPropValues[ PropData[EMAIL].iIndex ].Value.LPSZ;
  154. if(pszEmail)
  155. {
  156. reIsapi.SetValue( REGVAL_ULS_EMAIL_NAME, pszEmail );
  157. }
  158. LPCTSTR pszPhoneNum = pPropValues[ PropData[PHONENUM].iIndex ].Value.LPSZ;
  159. if(pszPhoneNum)
  160. {
  161. reIsapi.SetValue( REGVAL_ULS_PHONENUM_NAME, pszPhoneNum );
  162. }
  163. if(pszName)
  164. {
  165. TCHAR szULSName[ MAX_DCL_NAME_LEN + 1];
  166. wsprintf( szULSName,
  167. TEXT("%s %s"),
  168. pszName,
  169. pszSurName
  170. );
  171. szULSName[ MAX_DCL_NAME_LEN ] = 0;
  172. reIsapi.SetValue( REGVAL_ULS_NAME, szULSName );
  173. }
  174. // Set Resolve Name
  175. LPCTSTR pszServerName = reIsapi.GetString( REGVAL_SERVERNAME );
  176. if( pszServerName && pszEmail)
  177. {
  178. TCHAR szBuffer[ MAX_PATH ];
  179. wsprintf( szBuffer,
  180. TEXT("%s/%s"),
  181. pszServerName,
  182. pszEmail );
  183. szBuffer[ MAX_PATH - 1 ] = 0;
  184. reIsapi.SetValue( REGVAL_ULS_RES_NAME, szBuffer );
  185. }
  186. // Optional properties...
  187. if( PropData[ COMMENT ].dwPropVal )
  188. {
  189. reIsapi.SetValue( REGVAL_ULS_COMMENTS_NAME, pPropValues[ PropData[ COMMENT ].iIndex ].Value.LPSZ );
  190. }
  191. else
  192. {
  193. reIsapi.DeleteValue( REGVAL_ULS_COMMENTS_NAME );
  194. }
  195. if( PropData[LOCATION].dwPropVal )
  196. {
  197. reIsapi.SetValue( REGVAL_ULS_LOCATION_NAME, pPropValues[ PropData[LOCATION].iIndex ].Value.LPSZ );
  198. }
  199. else
  200. {
  201. reIsapi.DeleteValue( REGVAL_ULS_LOCATION_NAME );
  202. }
  203. // Generate a cert based on the entered information for secure calls
  204. TCHAR szName[ MAX_PATH ];
  205. TCHAR szSurName[ MAX_PATH ];
  206. TCHAR szEmail[ MAX_PATH ];
  207. lstrcpy( szName, reIsapi.GetString( REGVAL_ULS_FIRST_NAME ) );
  208. lstrcpy( szSurName, reIsapi.GetString( REGVAL_ULS_LAST_NAME ) );
  209. lstrcpy( szEmail, reIsapi.GetString( REGVAL_ULS_EMAIL_NAME ) );
  210. MakeCertWrap(szName, szSurName, szEmail, 0);
  211. hr = S_OK;
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. cleanup:
  219. if( NULL != pPropValues )
  220. {
  221. pfnMAPIFreeBuffer( pPropValues );
  222. }
  223. if( NULL != pPrimaryIdentityEntryID )
  224. {
  225. pfnMAPIFreeBuffer( pPrimaryIdentityEntryID );
  226. }
  227. if( NULL != pMailUser)
  228. {
  229. pMailUser->Release();
  230. }
  231. if( NULL != pAddrBook)
  232. {
  233. pAddrBook->Release();
  234. }
  235. if( NULL != pMapiSession )
  236. {
  237. pMapiSession->Release();
  238. }
  239. pfnMAPIUninitialize();
  240. FreeLibrary( hMapi );
  241. }
  242. return hr;
  243. }