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.

408 lines
9.9 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. params.cxx
  5. Abstract:
  6. Header and parameter parser for digest sspi package.
  7. Author:
  8. Adriaan Canter (adriaanc) 01-Aug-1998
  9. --*/
  10. #include "include.hxx"
  11. // Static parameter map - this must
  12. // agree with PARAM_INDEX.
  13. // BUGBUG - declare const.
  14. LPSTR CParams::szParamMap[] =
  15. {
  16. HOST_SZ,
  17. USER_SZ,
  18. PASS_SZ,
  19. URL_SZ,
  20. METHOD_SZ,
  21. NONCE_SZ,
  22. OPAQUE_SZ,
  23. REALM_SZ,
  24. DOMAIN_SZ,
  25. STALE_SZ,
  26. ALGORITHM_SZ,
  27. QOP_SZ,
  28. MS_LOGOFF_SZ
  29. };
  30. //--------------------------------------------------------------------
  31. // CParams::TrimQuotes
  32. // Inplace trim of one leading and one trailing quote.
  33. // BUGBUG -ppsz
  34. //--------------------------------------------------------------------
  35. VOID CParams::TrimQuotes(LPSTR *psz, LPDWORD pcb)
  36. {
  37. if (*pcb && (**psz == '"'))
  38. {
  39. (*psz)++;
  40. (*pcb)--;
  41. }
  42. if (*pcb && (*(*psz + *pcb - 1) == '"'))
  43. (*pcb)--;
  44. }
  45. //--------------------------------------------------------------------
  46. // CParams::TrimWhiteSpace
  47. // Inplace trim of leading and trailing whitespace.
  48. //--------------------------------------------------------------------
  49. VOID CParams::TrimWhiteSpace(LPSTR *psz, LPDWORD pcb)
  50. {
  51. DWORD cb = *pcb;
  52. CHAR* beg = *psz;
  53. CHAR* end = beg + cb - 1;
  54. while ((cb != 0) && ((*beg == ' ') || (*beg == '\t')))
  55. {
  56. beg++;
  57. cb--;
  58. }
  59. while ((cb != 0) && ((*end == ' ') || (*end == '\t')))
  60. {
  61. end--;
  62. cb--;
  63. }
  64. *psz = beg;
  65. *pcb = cb;
  66. }
  67. //--------------------------------------------------------------------
  68. // CParams::GetDelimitedToken
  69. // Inplace strtok based on one delimiter. Ignores delimiter scoped by quotes.
  70. // bugbug - IN/OUT
  71. //--------------------------------------------------------------------
  72. BOOL CParams::GetDelimitedToken(LPSTR* pszBuf, LPDWORD pcbBuf,
  73. LPSTR* pszTok, LPDWORD pcbTok,
  74. CHAR cDelim)
  75. {
  76. CHAR *pEnd;
  77. BOOL fQuote = FALSE,
  78. fRet = FALSE;
  79. *pcbTok = 0;
  80. *pszTok = *pszBuf;
  81. pEnd = *pszBuf + *pcbBuf - 1;
  82. while (*pcbBuf)
  83. {
  84. if ( ((**pszBuf == cDelim) && !fQuote)
  85. || (**pszBuf =='\r')
  86. || (**pszBuf =='\n')
  87. || (**pszBuf =='\0'))
  88. {
  89. fRet = TRUE;
  90. break;
  91. }
  92. if (**pszBuf == '"')
  93. fQuote = !fQuote;
  94. (*pszBuf)++;
  95. (*pcbBuf)--;
  96. }
  97. // bugbug - OFF BY ONE WHEN NOT TERMINATING WITH A COMMA.
  98. if (fRet)
  99. {
  100. *pcbBuf = (DWORD)(pEnd - *pszBuf);
  101. *pcbTok = (DWORD)(*pszBuf - *pszTok);
  102. if (**pszBuf == cDelim)
  103. (*pszBuf)++;
  104. }
  105. return fRet;
  106. }
  107. //--------------------------------------------------------------------
  108. // CParams::GetKeyValuePair
  109. // Inplace retrieval of key and value from a buffer of form key = <">value<">
  110. //--------------------------------------------------------------------
  111. BOOL CParams::GetKeyValuePair(LPSTR szB, DWORD cbB,
  112. LPSTR* pszK, LPDWORD pcbK,
  113. LPSTR* pszV, LPDWORD pcbV)
  114. {
  115. if (GetDelimitedToken(&szB, &cbB, pszK, pcbK, '='))
  116. {
  117. TrimWhiteSpace(pszK, pcbK);
  118. if (cbB)
  119. {
  120. *pszV = szB;
  121. *pcbV = cbB;
  122. TrimWhiteSpace(pszV, pcbV);
  123. }
  124. else
  125. {
  126. *pszV = NULL;
  127. *pcbV = 0;
  128. }
  129. return TRUE;
  130. }
  131. else
  132. {
  133. *pszK = *pszV = NULL;
  134. *pcbK = *pcbV = 0;
  135. }
  136. return FALSE;
  137. }
  138. //--------------------------------------------------------------------
  139. // CParams::CParams
  140. // BUGBUG - SET A dwstatus variable.
  141. //--------------------------------------------------------------------
  142. CParams::CParams(LPSTR szBuffer)
  143. {
  144. LPSTR szData, szTok, szKey, szValue;
  145. DWORD cbData, cbTok, cbKey, cbValue;
  146. // Zero set the entry array.
  147. memset(_Entry, 0, sizeof(_Entry));
  148. _hWnd = 0;
  149. _cNC = 0;
  150. _fPreAuth = FALSE;
  151. _fCredsSupplied = FALSE;
  152. // May be created with NULL buffer.
  153. if (!szBuffer)
  154. {
  155. _szBuffer = 0;
  156. _cbBuffer = 0;
  157. return;
  158. }
  159. _cbBuffer = strlen(szBuffer) + 1;
  160. _szBuffer = new CHAR[_cbBuffer];
  161. if (_szBuffer)
  162. {
  163. memcpy(_szBuffer, szBuffer, _cbBuffer);
  164. }
  165. else
  166. {
  167. DIGEST_ASSERT(FALSE);
  168. return;
  169. }
  170. szData = _szBuffer;
  171. cbData = _cbBuffer;
  172. DWORD Idx;
  173. while (GetDelimitedToken(&szData, &cbData, &szTok, &cbTok, ','))
  174. {
  175. if (GetKeyValuePair(szTok, cbTok, &szKey, &cbKey, &szValue, &cbValue))
  176. {
  177. TrimQuotes(&szValue, &cbValue);
  178. for (Idx = METHOD; Idx < MAX_PARAMS; Idx++)
  179. {
  180. if (!_strnicmp(szKey, szParamMap[Idx], cbKey)
  181. && !szParamMap[Idx][cbKey])
  182. {
  183. if (szValue)
  184. {
  185. *(szValue + cbValue) = '\0';
  186. }
  187. _Entry[Idx].szParam = szParamMap[Idx];
  188. _Entry[Idx].szValue = szValue;
  189. _Entry[Idx].cbValue = cbValue;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. //--------------------------------------------------------------------
  196. // CParams::~CParams
  197. // BUGBUG [] for deletes
  198. //--------------------------------------------------------------------
  199. CParams::~CParams()
  200. {
  201. for (DWORD Idx = 0; Idx < MAX_PARAMS; Idx++)
  202. {
  203. if (_Entry[Idx].fAllocated)
  204. delete _Entry[Idx].szValue;
  205. }
  206. if (_szBuffer)
  207. delete _szBuffer;
  208. }
  209. //--------------------------------------------------------------------
  210. // CParams::GetParam
  211. //--------------------------------------------------------------------
  212. LPSTR CParams::GetParam(PARAM_INDEX Idx)
  213. {
  214. return _Entry[Idx].szValue;
  215. }
  216. //--------------------------------------------------------------------
  217. // CParams::GetParam
  218. //--------------------------------------------------------------------
  219. BOOL CParams::GetParam(PARAM_INDEX Idx, LPSTR *pszValue, LPDWORD pcbValue)
  220. {
  221. *pszValue = _Entry[Idx].szValue;
  222. if (!*pszValue)
  223. return FALSE;
  224. *pcbValue = _Entry[Idx].cbValue;
  225. return TRUE;
  226. }
  227. //--------------------------------------------------------------------
  228. // CParams::SetParam
  229. // BUGBUG - []
  230. // AND FIGURE OUT IF EVER CALLED WITH null, 0
  231. //--------------------------------------------------------------------
  232. BOOL CParams::SetParam(PARAM_INDEX Idx, LPSTR szValue, DWORD cbValue)
  233. {
  234. if (_Entry[Idx].fAllocated && _Entry[Idx].szValue)
  235. delete _Entry[Idx].szValue;
  236. if (szValue && cbValue)
  237. {
  238. _Entry[Idx].szValue = new CHAR[cbValue + 1];
  239. if (!_Entry[Idx].szValue)
  240. {
  241. DIGEST_ASSERT(FALSE);
  242. return FALSE;
  243. }
  244. memcpy(_Entry[Idx].szValue, szValue, cbValue);
  245. *(_Entry[Idx].szValue + cbValue) = '\0';
  246. }
  247. _Entry[Idx].cbValue = cbValue;
  248. _Entry[Idx].szParam = szParamMap[Idx];
  249. _Entry[Idx].fAllocated = TRUE;
  250. return TRUE;
  251. }
  252. //--------------------------------------------------------------------
  253. // CParams::GetHwnd
  254. //--------------------------------------------------------------------
  255. HWND CParams::GetHwnd()
  256. {
  257. return _hWnd;
  258. }
  259. //--------------------------------------------------------------------
  260. // CParams::SetHwnd
  261. //--------------------------------------------------------------------
  262. BOOL CParams::SetHwnd(HWND* phWnd)
  263. {
  264. _hWnd = phWnd ? *phWnd : GetActiveWindow();
  265. return TRUE;
  266. }
  267. //--------------------------------------------------------------------
  268. // CParams::SetNC
  269. //--------------------------------------------------------------------
  270. VOID CParams::SetNC(DWORD* pcNC)
  271. {
  272. _cNC = *pcNC;
  273. }
  274. //--------------------------------------------------------------------
  275. // CParams::GetNC
  276. //--------------------------------------------------------------------
  277. DWORD CParams::GetNC()
  278. {
  279. return _cNC;
  280. }
  281. //--------------------------------------------------------------------
  282. // CParams::SetPreAuth
  283. //--------------------------------------------------------------------
  284. VOID CParams::SetPreAuth(BOOL fPreAuth)
  285. {
  286. _fPreAuth = fPreAuth;
  287. }
  288. //--------------------------------------------------------------------
  289. // CParams::IsPreAuth
  290. //--------------------------------------------------------------------
  291. BOOL CParams::IsPreAuth()
  292. {
  293. return _fPreAuth;
  294. }
  295. //--------------------------------------------------------------------
  296. // CParams::SetMd5Sess
  297. //--------------------------------------------------------------------
  298. VOID CParams::SetMd5Sess(BOOL fMd5Sess)
  299. {
  300. _fMd5Sess = fMd5Sess;
  301. }
  302. //--------------------------------------------------------------------
  303. // CParams::IsMd5Sess
  304. //--------------------------------------------------------------------
  305. BOOL CParams::IsMd5Sess()
  306. {
  307. return _fMd5Sess;
  308. }
  309. //--------------------------------------------------------------------
  310. // CParams::SetCredsSupplied
  311. //--------------------------------------------------------------------
  312. VOID CParams::SetCredsSupplied(BOOL fCredsSupplied)
  313. {
  314. _fCredsSupplied = fCredsSupplied;
  315. }
  316. //--------------------------------------------------------------------
  317. // CParams::AreCredsSupplied
  318. //--------------------------------------------------------------------
  319. BOOL CParams::AreCredsSupplied()
  320. {
  321. return _fCredsSupplied;
  322. }
  323. //--------------------------------------------------------------------
  324. // CParams::FindToken
  325. // Returns TRUE if non-ws token is found in comma delmited string.
  326. //--------------------------------------------------------------------
  327. BOOL CParams::FindToken(LPSTR szBuf, DWORD cbBuf, LPSTR szMatch, DWORD cbMatch, LPSTR *pszPtr)
  328. {
  329. LPSTR ptr = szBuf, szTok;
  330. DWORD cb = cbBuf, cbTok;
  331. while (GetDelimitedToken(&ptr, &cb, &szTok, &cbTok, ','))
  332. {
  333. TrimWhiteSpace(&szTok, &cbTok);
  334. if (!_strnicmp(szTok, szMatch, cbMatch) && (cbTok == cbMatch))
  335. {
  336. if (pszPtr)
  337. *pszPtr = szTok;
  338. return TRUE;
  339. }
  340. }
  341. return FALSE;
  342. }
  343.