Leaked source code of windows server 2003
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.

251 lines
5.6 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // EAPDnary.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // This file declares the class EAPTranslator.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 01/15/1998 Original version.
  16. // 05/08/1998 Do not restrict to attributes defined in raseapif.h.
  17. // Allow filtering of translated attributes.
  18. // 08/26/1998 Converted to a namespace.
  19. // 04/09/1999 Fix leak when converting outgoing attributes.
  20. // 04/17/2000 Port to new dictionary API.
  21. //
  22. ///////////////////////////////////////////////////////////////////////////////
  23. #include <ias.h>
  24. #include <iastlb.h>
  25. #include <iasutil.h>
  26. #include <hashmap.h>
  27. #include <eapdnary.h>
  28. namespace EAPTranslator
  29. {
  30. typedef hash_map < DWORD, IASTYPE, identity<DWORD> > TypeMap;
  31. long theRefCount; // Initialization refCount.
  32. TypeMap theTypeMap(0x200); // Maps attribute ID's to IASTYPE's.
  33. }
  34. HRESULT EAPTranslator::initialize() throw ()
  35. {
  36. IASGlobalLockSentry sentry;
  37. if (theRefCount > 0)
  38. {
  39. // We're already initialized, so just bump the ref count.
  40. ++theRefCount;
  41. return S_OK;
  42. }
  43. try
  44. {
  45. // Names of various columns in the dictionary.
  46. const PCWSTR COLUMNS[] =
  47. {
  48. L"ID",
  49. L"Syntax",
  50. NULL
  51. };
  52. // Open the attributes table.
  53. IASTL::IASDictionary dnary(COLUMNS);
  54. // Iterate through the attributes and populate our dictionary.
  55. while (dnary.next())
  56. {
  57. DWORD id = (DWORD)dnary.getLong(0);
  58. IASTYPE type = (IASTYPE)dnary.getLong(1);
  59. theTypeMap[id] = type;
  60. }
  61. }
  62. catch (std::bad_alloc)
  63. {
  64. theTypeMap.clear();
  65. return E_OUTOFMEMORY;
  66. }
  67. catch (const _com_error& ce)
  68. {
  69. theTypeMap.clear();
  70. return ce.Error();
  71. }
  72. // We made it so increase the refCount.
  73. ++theRefCount;
  74. return S_OK;
  75. }
  76. void EAPTranslator::finalize() throw ()
  77. {
  78. IASGlobalLockSentry sentry;
  79. if (--theRefCount == 0)
  80. {
  81. theTypeMap.clear();
  82. }
  83. }
  84. BOOL EAPTranslator::translate(
  85. IASAttribute& dst,
  86. const RAS_AUTH_ATTRIBUTE& src
  87. )
  88. {
  89. dst->dwId = (DWORD)src.raaType;
  90. const TypeMap::value_type* val = theTypeMap.find(dst->dwId);
  91. IASTYPE itType = val ? val->second : IASTYPE_INVALID;
  92. switch (itType)
  93. {
  94. case IASTYPE_BOOLEAN:
  95. case IASTYPE_INTEGER:
  96. case IASTYPE_ENUM:
  97. case IASTYPE_INET_ADDR:
  98. {
  99. switch (src.dwLength)
  100. {
  101. case 4:
  102. case 2:
  103. case 1:
  104. dst->Value.Integer = PtrToUlong(src.Value);
  105. break;
  106. default:
  107. _com_issue_error(E_INVALIDARG);
  108. }
  109. break;
  110. }
  111. case IASTYPE_STRING:
  112. {
  113. dst.setString(src.dwLength, (const BYTE*)src.Value);
  114. break;
  115. }
  116. case IASTYPE_OCTET_STRING:
  117. case IASTYPE_PROV_SPECIFIC:
  118. {
  119. dst.setOctetString(src.dwLength, (const BYTE*)src.Value);
  120. break;
  121. }
  122. default:
  123. return FALSE;
  124. }
  125. dst->Value.itType = itType;
  126. return TRUE;
  127. }
  128. BOOL EAPTranslator::translate(
  129. RAS_AUTH_ATTRIBUTE& dst,
  130. const IASATTRIBUTE& src
  131. )
  132. {
  133. dst.raaType = (RAS_AUTH_ATTRIBUTE_TYPE)src.dwId;
  134. switch (src.Value.itType)
  135. {
  136. case IASTYPE_BOOLEAN:
  137. case IASTYPE_INTEGER:
  138. case IASTYPE_ENUM:
  139. case IASTYPE_INET_ADDR:
  140. {
  141. dst.dwLength = sizeof(DWORD);
  142. dst.Value = UlongToPtr(src.Value.Integer);
  143. break;
  144. }
  145. case IASTYPE_STRING:
  146. {
  147. DWORD dwErr = IASAttributeAnsiAlloc(const_cast<PIASATTRIBUTE>(&src));
  148. if (dwErr != NO_ERROR)
  149. {
  150. issue_error(HRESULT_FROM_WIN32(dwErr));
  151. }
  152. if (src.Value.String.pszAnsi)
  153. {
  154. dst.dwLength = (DWORD)strlen(src.Value.String.pszAnsi) + 1;
  155. dst.Value = (PVOID)src.Value.String.pszAnsi;
  156. }
  157. else
  158. {
  159. dst.dwLength = 0;
  160. dst.Value = NULL;
  161. }
  162. break;
  163. }
  164. case IASTYPE_OCTET_STRING:
  165. case IASTYPE_PROV_SPECIFIC:
  166. {
  167. dst.dwLength = src.Value.OctetString.dwLength;
  168. dst.Value = (PVOID)src.Value.OctetString.lpValue;
  169. break;
  170. }
  171. default:
  172. return FALSE;
  173. }
  174. return TRUE;
  175. }
  176. void EAPTranslator::translate(
  177. IASAttributeVector& dst,
  178. const RAS_AUTH_ATTRIBUTE* src
  179. )
  180. {
  181. IASAttribute attr;
  182. const RAS_AUTH_ATTRIBUTE* i;
  183. for (i = src; i->raaType != raatMinimum; ++i)
  184. {
  185. attr.alloc();
  186. if (translate(attr, *i))
  187. {
  188. dst.push_back(attr, false);
  189. attr.detach();
  190. }
  191. }
  192. }
  193. void EAPTranslator::translate(
  194. PRAS_AUTH_ATTRIBUTE dst,
  195. const IASAttributeVector& src,
  196. DWORD filter
  197. )
  198. {
  199. PRAS_AUTH_ATTRIBUTE next = dst;
  200. IASAttributeVector::const_iterator i;
  201. for (i = src.begin(); i != src.end(); ++i)
  202. {
  203. if ((i->pAttribute->dwFlags & filter) != 0 &&
  204. translate(*next, *(i->pAttribute)))
  205. {
  206. ++next;
  207. }
  208. }
  209. next->raaType = raatMinimum;
  210. }