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.

417 lines
9.8 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // formbuf.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // Defines the class FormattedBuffer.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 08/04/1998 Original version.
  16. // 12/17/1998 Add append overload for IASATTRIBUTE&.
  17. // 01/22/1999 UTF-8 support.
  18. // 01/25/1999 Date and time are separate fields.
  19. // 03/23/1999 Add support for text qualifiers.
  20. // 04/19/1999 Strip null-terminators from OctetStrings.
  21. // 05/17/1999 Handle ANSI strings correctly.
  22. // 06/01/1999 Make sure Class 'string' is printable.
  23. //
  24. ///////////////////////////////////////////////////////////////////////////////
  25. #include <ias.h>
  26. #include <iasattr.h>
  27. #include <iasutil.h>
  28. #include <iasutf8.h>
  29. #include <sdoias.h>
  30. #include <classattr.h>
  31. #include <formbuf.h>
  32. //////////
  33. // Helper function that returns the number of printable characters if an
  34. // OctetString consists solely of printable UTF-8 characters.
  35. //////////
  36. DWORD
  37. WINAPI
  38. IsOctetStringPrintable(
  39. PBYTE buf,
  40. DWORD buflen
  41. ) throw ()
  42. {
  43. // Is the last character just a null terminator?
  44. if (buflen && !buf[buflen - 1]) { --buflen; }
  45. PBYTE p = buf;
  46. PBYTE end = buf + buflen;
  47. // Scan for control characters and delimiters.
  48. while (p < end)
  49. {
  50. if (!(*p & 0x60)) { return 0; }
  51. ++p;
  52. }
  53. // Ensure it's a valid UTF-8 string.
  54. return (IASUtf8ToUnicodeLength((PCSTR)buf, buflen) >= 0) ? buflen : 0;
  55. }
  56. void FormattedBuffer::append(DWORD value)
  57. {
  58. CHAR buffer[11], *p = buffer + 11;
  59. do
  60. {
  61. *--p = '0' + (CHAR)(value % 10);
  62. } while (value /= 10);
  63. append((const BYTE*)p, (DWORD)(buffer + 11 - p));
  64. }
  65. void FormattedBuffer::append(DWORDLONG value)
  66. {
  67. CHAR buffer[21], *p = buffer + 21;
  68. do
  69. {
  70. *--p = '0' + (CHAR)(value % 10);
  71. } while (value /= 10);
  72. append((const BYTE*)p, (DWORD)(buffer + 21 - p));
  73. }
  74. void FormattedBuffer::append(const IASVALUE& value)
  75. {
  76. switch (value.itType)
  77. {
  78. case IASTYPE_BOOLEAN:
  79. case IASTYPE_INTEGER:
  80. case IASTYPE_ENUM:
  81. {
  82. append(value.Integer);
  83. break;
  84. }
  85. case IASTYPE_OCTET_STRING:
  86. case IASTYPE_PROV_SPECIFIC:
  87. {
  88. DWORD len = IsOctetStringPrintable(
  89. value.OctetString.lpValue,
  90. value.OctetString.dwLength
  91. );
  92. if (len)
  93. {
  94. appendText((PCSTR)value.OctetString.lpValue, len);
  95. }
  96. else
  97. {
  98. appendFormattedOctets(value.OctetString.lpValue,
  99. value.OctetString.dwLength);
  100. }
  101. break;
  102. }
  103. case IASTYPE_INET_ADDR:
  104. {
  105. CHAR buffer[16];
  106. append(ias_inet_htoa(value.InetAddr, buffer));
  107. break;
  108. }
  109. case IASTYPE_STRING:
  110. {
  111. // Make sure we have a Unicode string available.
  112. if (!value.String.pszWide)
  113. {
  114. // If there's no ANSI string either, then there's nothing to log.
  115. if (!value.String.pszAnsi) { break; }
  116. // Convert the value into an attribute ...
  117. PIASATTRIBUTE p = (PIASATTRIBUTE)
  118. ((ULONG_PTR)&value - FIELD_OFFSET(IASATTRIBUTE, Value));
  119. // ... and allocate a Unicode string.
  120. if (IASAttributeUnicodeAlloc(p) != NO_ERROR)
  121. {
  122. throw std::bad_alloc();
  123. }
  124. }
  125. // Compute the length of the source Unicode string.
  126. DWORD srclen = wcslen(value.String.pszWide);
  127. // Compute the length of the converted UTF-8 string.
  128. LONG dstlen = IASUnicodeToUtf8Length(value.String.pszWide, srclen);
  129. // Allocate space for the converted string.
  130. PSTR dst = (PSTR)_alloca(dstlen);
  131. // Convert the string.
  132. IASUnicodeToUtf8(value.String.pszWide, srclen, dst);
  133. // Write the buffer.
  134. appendText(dst, dstlen);
  135. break;
  136. }
  137. case IASTYPE_UTC_TIME:
  138. {
  139. SYSTEMTIME st;
  140. FileTimeToSystemTime(&value.UTCTime, &st);
  141. appendDate(st);
  142. append(' ');
  143. appendTime(st);
  144. break;
  145. }
  146. }
  147. }
  148. //////////
  149. // Appends a single attribute value.
  150. //////////
  151. void FormattedBuffer::append(const IASATTRIBUTE& attr)
  152. {
  153. // Class attributes are special-cased.
  154. if (attr.dwId == RADIUS_ATTRIBUTE_CLASS)
  155. {
  156. appendClassAttribute(attr.Value.OctetString);
  157. }
  158. else
  159. {
  160. append(attr.Value);
  161. }
  162. }
  163. //////////
  164. // Appends an array of attributes. The array is terminated either by a null
  165. // attribute pointer or an attribute with a different ID.
  166. //////////
  167. void FormattedBuffer::append(const ATTRIBUTEPOSITION* pos)
  168. {
  169. DWORD id = pos->pAttribute->dwId;
  170. // Will this attribute be written as text?
  171. BOOL isText = FALSE;
  172. switch (pos->pAttribute->Value.itType)
  173. {
  174. case IASTYPE_INET_ADDR:
  175. case IASTYPE_STRING:
  176. case IASTYPE_OCTET_STRING:
  177. case IASTYPE_PROV_SPECIFIC:
  178. isText = TRUE;
  179. }
  180. // If so, then we surround it with the text qualifier.
  181. if (isText) { appendQualifier(); }
  182. // Write the first value.
  183. append(*(pos->pAttribute));
  184. ++pos;
  185. // Then add any additional values delimited by a pipe.
  186. while (pos->pAttribute && pos->pAttribute->dwId == id)
  187. {
  188. append('|');
  189. append(*(pos->pAttribute));
  190. ++pos;
  191. }
  192. // Write the closing text qualifier if necessary.
  193. if (isText) { appendQualifier(); }
  194. }
  195. void FormattedBuffer::appendClassAttribute(const IAS_OCTET_STRING& value)
  196. {
  197. // Extract a class attribute from the blob.
  198. IASClass* cl = (IASClass*)value.lpValue;
  199. if (!cl->isMicrosoft(value.dwLength))
  200. {
  201. // If it's not one of ours, just write it as an octet string.
  202. appendFormattedOctets(value.lpValue, value.dwLength);
  203. }
  204. else
  205. {
  206. // Vendor ID.
  207. append(cl->getVendorID());
  208. // Version.
  209. append(' ');
  210. append((DWORD)cl->getVersion());
  211. // Server IP address.
  212. CHAR buffer[16];
  213. append(' ');
  214. append(ias_inet_htoa(cl->getServerAddress(), buffer));
  215. // Server reboot time.
  216. FILETIME ft = cl->getLastReboot();
  217. SYSTEMTIME st;
  218. FileTimeToSystemTime(&ft, &st);
  219. append(' ');
  220. appendDate(st);
  221. append(' ');
  222. appendTime(st);
  223. // Session serial number.
  224. DWORDLONG serialNumber = cl->getSerialNumber();
  225. append(' ');
  226. append(serialNumber);
  227. // Class string.
  228. if (value.dwLength > sizeof(IASClass))
  229. {
  230. append(' ');
  231. // Convert the remainder to an OctetString ...
  232. IASVALUE tmp;
  233. tmp.itType = IASTYPE_OCTET_STRING;
  234. tmp.OctetString.lpValue = const_cast<PBYTE>(cl->getString());
  235. tmp.OctetString.dwLength = value.dwLength - sizeof(IASClass);
  236. // ... and append.
  237. append(tmp);
  238. }
  239. }
  240. }
  241. // Convert a hex number to an ascii digit. Does not check for overflow.
  242. #define HEX_TO_ASCII(h) ((h) < 10 ? '0' + (CHAR)(h) : ('A' - 10) + (CHAR)(h))
  243. //////////
  244. // Appends an octet string as stringized hex.
  245. //////////
  246. void FormattedBuffer::appendFormattedOctets(
  247. const BYTE* buf,
  248. DWORD buflen
  249. )
  250. {
  251. PCHAR dst = (PCHAR)reserve(buflen * 2 + 2);
  252. //////////
  253. // Add a leading 0x.
  254. //////////
  255. *dst = '0';
  256. ++dst;
  257. *dst = 'x';
  258. ++dst;
  259. //////////
  260. // Add each octet.
  261. //////////
  262. while (buflen)
  263. {
  264. CHAR digit;
  265. // High-order digit.
  266. digit = (CHAR)(*buf >> 4);
  267. *dst = HEX_TO_ASCII(digit);
  268. ++dst;
  269. // Low-order digit.
  270. digit = (CHAR)(*buf & 0xf);
  271. *dst = HEX_TO_ASCII(digit);
  272. ++dst;
  273. // Advance to the next octet.
  274. ++buf;
  275. --buflen;
  276. }
  277. }
  278. // insert a 4 character integer.
  279. #define INSERT_4u(p, v) \
  280. { *p = '0' + (v) / 1000; ++p; *p = '0' + (v) / 100 % 10; ++p; \
  281. *p = '0' + (v) / 10 % 10; ++p; *p = '0' + (v) % 10; ++p; }
  282. // insert a 2 character integer.
  283. #define INSERT_2u(p, v) \
  284. { *p = '0' + (v) / 10; ++p; *p = '0' + (v) % 10; ++p; }
  285. // insert a single character.
  286. #define INSERT_1c(p, v) \
  287. { *p = v; ++p; }
  288. void FormattedBuffer::appendDate(const SYSTEMTIME& value)
  289. {
  290. PCHAR p = (PCHAR)reserve(10);
  291. INSERT_2u(p, value.wMonth);
  292. INSERT_1c(p, '/');
  293. INSERT_2u(p, value.wDay);
  294. INSERT_1c(p, '/');
  295. INSERT_4u(p, value.wYear);
  296. }
  297. void FormattedBuffer::appendText(PCSTR sz, DWORD szlen)
  298. {
  299. if (textQualifier)
  300. {
  301. /////////
  302. // We have a text qualifier, so we don't have to worry about embedded
  303. // delimiters, but we do have to worry about embedded qualifiers. We
  304. // replace each embedded qualifier with a double qualifier.
  305. /////////
  306. PCSTR p;
  307. while ((p = (PCSTR)memchr(sz, textQualifier, szlen)) != NULL)
  308. {
  309. // Skip past the qualifier.
  310. ++p;
  311. // How many bytes do we have ?
  312. DWORD nbyte = p - sz;
  313. // Write the bytes.
  314. append((const BYTE*)sz, nbyte);
  315. // Add an extra qualifer.
  316. append(textQualifier);
  317. // Update our state to point to the remainder of the text.
  318. sz = p;
  319. szlen -= nbyte;
  320. }
  321. // Write the piece after the last embedded qualifer.
  322. append((PBYTE)sz, szlen);
  323. }
  324. else
  325. {
  326. ////////
  327. // No text qualifer, so we can't handle embedded delimiter.
  328. ////////
  329. if (!memchr(sz, ',', szlen))
  330. {
  331. // No delimiters, so write 'as is'.
  332. append((PBYTE)sz, szlen);
  333. }
  334. else
  335. {
  336. // It contains a delimiter so write as formatted octets.
  337. appendFormattedOctets((PBYTE)sz, szlen);
  338. }
  339. }
  340. }
  341. void FormattedBuffer::appendTime(const SYSTEMTIME& value)
  342. {
  343. PCHAR p = (PCHAR)reserve(8);
  344. INSERT_2u(p, value.wHour);
  345. INSERT_1c(p, ':');
  346. INSERT_2u(p, value.wMinute);
  347. INSERT_1c(p, ':');
  348. INSERT_2u(p, value.wSecond);
  349. }