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.

221 lines
7.0 KiB

  1. /////////////////////////////////////////////////////////////
  2. // Copyright(c) 2000, Microsoft Corporation
  3. //
  4. // guidgen.cpp
  5. //
  6. // Created on 3/1/00 by DKalin (Dennis Kalinichenko)
  7. // Revisions:
  8. //
  9. // Implementation for the guid/name generation routines
  10. //
  11. /////////////////////////////////////////////////////////////
  12. #include "ipseccmd.h"
  13. /*********************************************************************
  14. FUNCTION: GenerateGuidNamePair
  15. PURPOSE: Generates GUID and name for the object using specified prefix
  16. PARAMS:
  17. pszPrefix - prefix to use, can be NULL (then default prefix will be used)
  18. gID - reference to GUID
  19. ppszName - address of name pointer, memory will be allocated inside this function
  20. RETURNS: none, will assert if memory cannot be allocated
  21. COMMENTS:
  22. caller is responsible for freeing the memory allocated
  23. (see also DeleteGuidsNames routine)
  24. *********************************************************************/
  25. void GenerateGuidNamePair (IN LPWSTR pszPrefix, OUT GUID& gID, OUT LPWSTR* ppszName)
  26. {
  27. WCHAR StringTxt[POTF_MAX_STRLEN];
  28. RPC_STATUS RpcStat;
  29. int iReturn;
  30. // cleanup first
  31. assert(ppszName != 0);
  32. if (*ppszName != 0)
  33. {
  34. delete[] *ppszName;
  35. }
  36. // set the prefix
  37. if (pszPrefix == 0 || pszPrefix[0] == 0)
  38. {
  39. wcscpy(StringTxt, L"text2pol ");
  40. }
  41. else
  42. {
  43. wcscpy(StringTxt, pszPrefix);
  44. }
  45. RpcStat = UuidCreate(&gID);
  46. assert(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY);
  47. // set the name to be equal to the prefix + GUID
  48. iReturn = StringFromGUID2(gID, StringTxt+wcslen(StringTxt), POTF_MAX_STRLEN-wcslen(StringTxt));
  49. assert(iReturn != 0);
  50. *ppszName = new WCHAR[wcslen(StringTxt)+1];
  51. assert(*ppszName != NULL);
  52. wcscpy(*ppszName, StringTxt);
  53. } /* GenerateGuidNamePair */
  54. /*********************************************************************
  55. FUNCTION: GenerateGuidsNames
  56. PURPOSE: Generates all necessary GUIDs and names for IPSEC_IKE_POLICY
  57. PARAMS:
  58. pszPrefix - prefix to use, can be NULL (then default prefix will be used)
  59. IPSecIkePol - reference to IPSEC_IKE_POLICY structure
  60. RETURNS: none, will assert if memory cannot be allocated
  61. COMMENTS:
  62. caller is responsible for freeing the memory allocated
  63. (see also DeleteGuidsNames routine)
  64. *********************************************************************/
  65. void GenerateGuidsNames (IN LPWSTR pszPrefix, IN OUT IPSEC_IKE_POLICY& IPSecIkePol)
  66. {
  67. int i;
  68. IPSEC_IKE_POLICY TmpPol; // for checks
  69. RPC_STATUS RpcStat;
  70. // set TmpPol to 0's
  71. memset(&TmpPol, 0, sizeof(TmpPol));
  72. // walk through all the substructures and call GenerateGuidNamePair
  73. for (i = 0; i < (int) IPSecIkePol.dwNumMMFilters; i++)
  74. {
  75. GenerateGuidNamePair(pszPrefix, IPSecIkePol.pMMFilters[i].gFilterID, &IPSecIkePol.pMMFilters[i].pszFilterName);
  76. }
  77. for (i = 0; i < (int) IPSecIkePol.dwNumFilters; i++)
  78. {
  79. // printf("GenerateGuidsNames i is %d", i);
  80. if (IPSecIkePol.QMFilterType == QM_TRANSPORT_FILTER)
  81. {
  82. GenerateGuidNamePair(pszPrefix, IPSecIkePol.pTransportFilters[i].gFilterID, &IPSecIkePol.pTransportFilters[i].pszFilterName);
  83. }
  84. else
  85. {
  86. // tunnel
  87. GenerateGuidNamePair(pszPrefix, IPSecIkePol.pTunnelFilters[i].gFilterID, &IPSecIkePol.pTunnelFilters[i].pszFilterName);
  88. }
  89. }
  90. if (memcmp(&IPSecIkePol.IkePol, &TmpPol.IkePol, sizeof(TmpPol.IkePol)) != 0)
  91. {
  92. // IkePol is not 0's
  93. GenerateGuidNamePair(pszPrefix, IPSecIkePol.IkePol.gPolicyID, &IPSecIkePol.IkePol.pszPolicyName);
  94. }
  95. if (memcmp(&IPSecIkePol.IpsPol, &TmpPol.IpsPol, sizeof(TmpPol.IpsPol)) != 0)
  96. {
  97. // IkePol is not 0's
  98. GenerateGuidNamePair(pszPrefix, IPSecIkePol.IpsPol.gPolicyID, &IPSecIkePol.IpsPol.pszPolicyName);
  99. }
  100. // go for auth methods
  101. if (memcmp(&IPSecIkePol.AuthInfos, &TmpPol.AuthInfos, sizeof(TmpPol.AuthInfos)) != 0)
  102. {
  103. RpcStat = UuidCreate(&IPSecIkePol.AuthInfos.gMMAuthID);
  104. assert(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY);
  105. }
  106. /* now fixup guid links */
  107. /* mainmode filters */
  108. for (i = 0; i < (int) IPSecIkePol.dwNumMMFilters; i++)
  109. {
  110. if (UuidIsNil(&IPSecIkePol.pMMFilters[i].gPolicyID, &RpcStat))
  111. {
  112. IPSecIkePol.pMMFilters[i].gPolicyID = IPSecIkePol.IkePol.gPolicyID;
  113. }
  114. if (UuidIsNil(&IPSecIkePol.pMMFilters[i].gMMAuthID, &RpcStat))
  115. {
  116. IPSecIkePol.pMMFilters[i].gMMAuthID = IPSecIkePol.AuthInfos.gMMAuthID;
  117. }
  118. assert(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY);
  119. }
  120. /* quickmode filters */
  121. for (i = 0; i < (int) IPSecIkePol.dwNumFilters; i++)
  122. {
  123. if (IPSecIkePol.QMFilterType == QM_TRANSPORT_FILTER)
  124. {
  125. if (UuidIsNil(&IPSecIkePol.pTransportFilters[i].gPolicyID, &RpcStat))
  126. {
  127. IPSecIkePol.pTransportFilters[i].gPolicyID = IPSecIkePol.IpsPol.gPolicyID;
  128. }
  129. }
  130. else
  131. {
  132. // tunnel
  133. if (UuidIsNil(&IPSecIkePol.pTunnelFilters[i].gPolicyID, &RpcStat))
  134. {
  135. IPSecIkePol.pTunnelFilters[i].gPolicyID = IPSecIkePol.IpsPol.gPolicyID;
  136. }
  137. }
  138. assert(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY);
  139. }
  140. } /* GenerateGuidsNames */
  141. /*********************************************************************
  142. FUNCTION: DeleteGuidsNames
  143. PURPOSE: Deletes all GUIDs and names from IPSEC_IKE_POLICY (used for cleanup)
  144. PARAMS:
  145. IPSecIkePol - reference to IPSEC_IKE_POLICY structure
  146. RETURNS: none
  147. COMMENTS:
  148. *********************************************************************/
  149. void DeleteGuidsNames (IN OUT IPSEC_IKE_POLICY& IPSecIkePol)
  150. {
  151. int i;
  152. // walk through all the substructures and call GenerateGuidNamePair
  153. for (i = 0; i < (int) IPSecIkePol.dwNumMMFilters; i++)
  154. {
  155. UuidCreateNil(&IPSecIkePol.pMMFilters[i].gFilterID);
  156. UuidCreateNil(&IPSecIkePol.pMMFilters[i].gPolicyID);
  157. UuidCreateNil(&IPSecIkePol.pMMFilters[i].gMMAuthID);
  158. if (IPSecIkePol.pMMFilters[i].pszFilterName != 0)
  159. {
  160. delete[] IPSecIkePol.pMMFilters[i].pszFilterName;
  161. IPSecIkePol.pMMFilters[i].pszFilterName = 0;
  162. }
  163. }
  164. for (i = 0; i < (int) IPSecIkePol.dwNumFilters; i++)
  165. {
  166. if (IPSecIkePol.QMFilterType == QM_TRANSPORT_FILTER)
  167. {
  168. UuidCreateNil(&IPSecIkePol.pTransportFilters[i].gFilterID);
  169. UuidCreateNil(&IPSecIkePol.pTransportFilters[i].gPolicyID);
  170. if (IPSecIkePol.pTransportFilters[i].pszFilterName != 0)
  171. {
  172. delete[] IPSecIkePol.pTransportFilters[i].pszFilterName;
  173. IPSecIkePol.pTransportFilters[i].pszFilterName = 0;
  174. }
  175. }
  176. else
  177. {
  178. // tunnel
  179. UuidCreateNil(&IPSecIkePol.pTunnelFilters[i].gFilterID);
  180. UuidCreateNil(&IPSecIkePol.pTunnelFilters[i].gPolicyID);
  181. if (IPSecIkePol.pTunnelFilters[i].pszFilterName != 0)
  182. {
  183. delete[] IPSecIkePol.pTunnelFilters[i].pszFilterName;
  184. IPSecIkePol.pTunnelFilters[i].pszFilterName = 0;
  185. }
  186. }
  187. }
  188. UuidCreateNil(&IPSecIkePol.IkePol.gPolicyID);
  189. if (IPSecIkePol.IkePol.pszPolicyName != 0)
  190. {
  191. delete[] IPSecIkePol.IkePol.pszPolicyName;
  192. IPSecIkePol.IkePol.pszPolicyName = 0;
  193. }
  194. UuidCreateNil(&IPSecIkePol.IpsPol.gPolicyID);
  195. if (IPSecIkePol.IpsPol.pszPolicyName != 0)
  196. {
  197. delete[] IPSecIkePol.IpsPol.pszPolicyName;
  198. IPSecIkePol.IpsPol.pszPolicyName = 0;
  199. }
  200. UuidCreateNil(&IPSecIkePol.AuthInfos.gMMAuthID);
  201. } /* DeleteGuidsNames */
  202.