/lwraft/server/kdckrb5/kdcreq.c

https://github.com/vmware/lightwave · C · 263 lines · 202 code · 27 blank · 34 comment · 27 complexity · f19fb1f487b346d589763756e43bfb61 MD5 · raw file

  1. /*
  2. * Copyright © 2012-2015 VMware, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the “License”); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an “AS IS” BASIS, without
  10. * warranties or conditions of any kind, EITHER EXPRESS OR IMPLIED. See the
  11. * License for the specific language governing permissions and limitations
  12. * under the License.
  13. */
  14. #include "includes.h"
  15. VOID
  16. VmKdcFreeKdcReq(
  17. PVMKDC_KDCREQ pKdcReq)
  18. {
  19. if (pKdcReq) {
  20. VMKDC_SAFE_FREE_METHOD_DATA(pKdcReq->padata);
  21. VMKDC_SAFE_FREE_DATA(pKdcReq->req_body.realm);
  22. VMKDC_SAFE_FREE_PRINCIPAL(pKdcReq->req_body.cname);
  23. VMKDC_SAFE_FREE_PRINCIPAL(pKdcReq->req_body.sname);
  24. VMKDC_SAFE_FREE_MEMORY(pKdcReq->req_body.etype.type);
  25. VMKDC_SAFE_FREE_MEMORY(pKdcReq->req_body.from);
  26. VMKDC_SAFE_FREE_MEMORY(pKdcReq->req_body.till);
  27. VMKDC_SAFE_FREE_MEMORY(pKdcReq->req_body.rtime);
  28. VMKDC_SAFE_FREE_MEMORY(pKdcReq);
  29. }
  30. }
  31. static
  32. VOID
  33. _VmKdcOptionsGet(
  34. KDCOptions *heimOptions,
  35. DWORD *kdcOptions)
  36. {
  37. if (heimOptions->forwardable)
  38. {
  39. VMKDC_FLAG_SET(*kdcOptions, VMKDC_KO_FORWARDABLE);
  40. }
  41. if (heimOptions->forwarded)
  42. {
  43. VMKDC_FLAG_SET(*kdcOptions, VMKDC_KO_FORWARDED);
  44. }
  45. if (heimOptions->proxiable)
  46. {
  47. VMKDC_FLAG_SET(*kdcOptions, VMKDC_KO_PROXIABLE);
  48. }
  49. if (heimOptions->proxy)
  50. {
  51. VMKDC_FLAG_SET(*kdcOptions, VMKDC_KO_PROXY);
  52. }
  53. if (heimOptions->allow_postdate)
  54. {
  55. VMKDC_FLAG_SET(*kdcOptions, VMKDC_KO_ALLOW_POSTDATE);
  56. }
  57. if (heimOptions->postdated)
  58. {
  59. VMKDC_FLAG_SET(*kdcOptions, VMKDC_KO_POSTDATED);
  60. }
  61. if (heimOptions->renewable)
  62. {
  63. VMKDC_FLAG_SET(*kdcOptions, VMKDC_KO_RENEWABLE);
  64. }
  65. if (heimOptions->disable_transited_check)
  66. {
  67. VMKDC_FLAG_SET(*kdcOptions, VMKDC_KO_DISABLE_TRANSITED_CHECK);
  68. }
  69. if (heimOptions->renewable_ok)
  70. {
  71. VMKDC_FLAG_SET(*kdcOptions, VMKDC_KO_RENEWABLE_OK);
  72. }
  73. if (heimOptions->enc_tkt_in_skey)
  74. {
  75. VMKDC_FLAG_SET(*kdcOptions, VMKDC_KO_ENC_TKT_IN_SKEY);
  76. }
  77. if (heimOptions->renew)
  78. {
  79. VMKDC_FLAG_SET(*kdcOptions, VMKDC_KO_RENEW);
  80. }
  81. if (heimOptions->validate)
  82. {
  83. VMKDC_FLAG_SET(*kdcOptions, VMKDC_KO_VALIDATE);
  84. }
  85. }
  86. DWORD
  87. VmKdcDecodeKdcReq(
  88. VMKDC_MESSAGE_TYPE messageType,
  89. PVMKDC_DATA pData,
  90. PVMKDC_KDCREQ *ppRetKdcReq)
  91. {
  92. PVMKDC_KDCREQ pKdcReq = NULL;
  93. AS_REQ heimReq = {0};
  94. size_t heimReqLen = 0;
  95. DWORD dwError = 0;
  96. PUCHAR reqBufPtr = NULL;
  97. DWORD reqBufLen = 0;
  98. unsigned int i = 0;
  99. reqBufPtr = VMKDC_GET_PTR_DATA(pData);
  100. reqBufLen = VMKDC_GET_LEN_DATA(pData);
  101. /*
  102. * Decode the request into Heimdal AS_REQ structure.
  103. */
  104. switch (messageType)
  105. {
  106. case VMKDC_MESSAGE_TYPE_KRB_AS_REQ:
  107. decode_AS_REQ(reqBufPtr, reqBufLen, &heimReq, &heimReqLen);
  108. break;
  109. case VMKDC_MESSAGE_TYPE_KRB_TGS_REQ:
  110. decode_TGS_REQ(reqBufPtr, reqBufLen, &heimReq, &heimReqLen);
  111. break;
  112. default:
  113. dwError = ERROR_PROTOCOL;
  114. BAIL_ON_VMKDC_ERROR(dwError);
  115. break;
  116. }
  117. if (heimReqLen <= 0)
  118. {
  119. dwError = ERROR_PROTOCOL;
  120. BAIL_ON_VMKDC_ERROR(dwError);
  121. }
  122. /*
  123. * Translate the decoded request to a VMKDC_KDCREQ structure.
  124. */
  125. dwError = VmKdcAllocateMemory(sizeof(VMKDC_KDCREQ), (PVOID*)&pKdcReq);
  126. BAIL_ON_VMKDC_ERROR(dwError);
  127. memset(pKdcReq, 0, sizeof(VMKDC_KDCREQ));
  128. /* pvno */
  129. pKdcReq->pvno = heimReq.pvno;
  130. /* message-type */
  131. pKdcReq->msg_type = heimReq.msg_type;
  132. /* method-data (optional) */
  133. if (heimReq.padata)
  134. {
  135. dwError = VmKdcAllocateMethodData(heimReq.padata->len,
  136. &pKdcReq->padata);
  137. BAIL_ON_VMKDC_ERROR(dwError);
  138. for (i=0; i<heimReq.padata->len; i++)
  139. {
  140. dwError = VmKdcMakePaData(heimReq.padata->val[i].padata_type,
  141. (DWORD) heimReq.padata->val[i].padata_value.length,
  142. heimReq.padata->val[i].padata_value.data,
  143. &pKdcReq->padata->padata[i]);
  144. BAIL_ON_VMKDC_ERROR(dwError);
  145. }
  146. }
  147. /* kdc-options */
  148. _VmKdcOptionsGet(&heimReq.req_body.kdc_options, &pKdcReq->req_body.kdc_options);
  149. /* cname (optional) */
  150. if (heimReq.req_body.cname)
  151. {
  152. dwError = VmKdcMakePrincipal(heimReq.req_body.realm,
  153. heimReq.req_body.cname->name_string.len,
  154. (PCSTR *)heimReq.req_body.cname->name_string.val,
  155. &pKdcReq->req_body.cname);
  156. BAIL_ON_VMKDC_ERROR(dwError);
  157. }
  158. /* realm */
  159. dwError = VmKdcAllocateDataString(heimReq.req_body.realm,
  160. &pKdcReq->req_body.realm);
  161. BAIL_ON_VMKDC_ERROR(dwError);
  162. /* sname (optional) */
  163. if (heimReq.req_body.sname)
  164. {
  165. dwError = VmKdcMakePrincipal(heimReq.req_body.realm,
  166. heimReq.req_body.sname->name_string.len,
  167. (PCSTR *)heimReq.req_body.sname->name_string.val,
  168. &pKdcReq->req_body.sname);
  169. BAIL_ON_VMKDC_ERROR(dwError);
  170. }
  171. /* from (optional) */
  172. if (heimReq.req_body.from)
  173. {
  174. dwError = VmKdcAllocateMemory(sizeof(time_t),
  175. (PVOID*)&pKdcReq->req_body.from);
  176. BAIL_ON_VMKDC_ERROR(dwError);
  177. *pKdcReq->req_body.from = *((time_t *)heimReq.req_body.from);
  178. }
  179. /* till (optional) */
  180. if (heimReq.req_body.till)
  181. {
  182. dwError = VmKdcAllocateMemory(sizeof(time_t),
  183. (PVOID*)&pKdcReq->req_body.till);
  184. BAIL_ON_VMKDC_ERROR(dwError);
  185. *pKdcReq->req_body.till = *((time_t *)heimReq.req_body.till);
  186. }
  187. /* rtime (optional) */
  188. if (heimReq.req_body.rtime)
  189. {
  190. dwError = VmKdcAllocateMemory(sizeof(time_t),
  191. (PVOID*)&pKdcReq->req_body.rtime);
  192. BAIL_ON_VMKDC_ERROR(dwError);
  193. *pKdcReq->req_body.rtime = *((time_t *)heimReq.req_body.rtime);
  194. }
  195. /* nonce */
  196. pKdcReq->req_body.nonce = heimReq.req_body.nonce;
  197. /* etype */
  198. dwError = VmKdcAllocateMemory(sizeof(VMKDC_ENCTYPE) * heimReq.req_body.etype.len,
  199. (PVOID*)&pKdcReq->req_body.etype.type);
  200. BAIL_ON_VMKDC_ERROR(dwError);
  201. pKdcReq->req_body.etype.count = heimReq.req_body.etype.len;
  202. for (i=0; i<heimReq.req_body.etype.len; i++)
  203. {
  204. pKdcReq->req_body.etype.type[i] = heimReq.req_body.etype.val[i];
  205. }
  206. /* addresses */
  207. #if 0
  208. if (pHeimReq->req_body.addresses)
  209. {
  210. pKdcReq->req_body.addresses = pHeimReq->req_body.addresses;
  211. }
  212. #endif
  213. /* enc-authorization-data */
  214. #if 0
  215. if (pHeimReq->req_body.enc_authorization_data)
  216. {
  217. pKdcReq->req_body.enc_authorization_data = pHeimReq->req_body.authorization_data;
  218. }
  219. #endif
  220. /* additional-tickets */
  221. #if 0
  222. if (pHeimReq->req_body.additional_tickets)
  223. {
  224. pKdcReq->req_body.additional_tickets = pHeimReq->req_body.additional_tickets;
  225. }
  226. #endif
  227. error:
  228. if (dwError)
  229. {
  230. VmKdcFreeKdcReq(pKdcReq);
  231. pKdcReq = NULL;
  232. }
  233. free_AS_REQ(&heimReq);
  234. *ppRetKdcReq = pKdcReq;
  235. return dwError;
  236. }