/lsass/client/rpc/samr/samr_binding.c

https://github.com/BeyondTrust/pbis-open · C · 396 lines · 285 code · 68 blank · 43 comment · 34 complexity · f55fda804df19de1617329e1a7f87893 MD5 · raw file

  1. /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*-
  2. * ex: set softtabstop=4 tabstop=8 expandtab shiftwidth=4: *
  3. * Editor Settings: expandtabs and use 4 spaces for indentation */
  4. /*
  5. * Copyright © BeyondTrust Software 2004 - 2019
  6. * All rights reserved.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * BEYONDTRUST MAKES THIS SOFTWARE AVAILABLE UNDER OTHER LICENSING TERMS AS
  21. * WELL. IF YOU HAVE ENTERED INTO A SEPARATE LICENSE AGREEMENT WITH
  22. * BEYONDTRUST, THEN YOU MAY ELECT TO USE THE SOFTWARE UNDER THE TERMS OF THAT
  23. * SOFTWARE LICENSE AGREEMENT INSTEAD OF THE TERMS OF THE APACHE LICENSE,
  24. * NOTWITHSTANDING THE ABOVE NOTICE. IF YOU HAVE QUESTIONS, OR WISH TO REQUEST
  25. * A COPY OF THE ALTERNATE LICENSING TERMS OFFERED BY BEYONDTRUST, PLEASE CONTACT
  26. * BEYONDTRUST AT beyondtrust.com/contact
  27. */
  28. /*
  29. * Copyright (C) BeyondTrust Software. All rights reserved.
  30. *
  31. * Module Name:
  32. *
  33. * samr_binding.c
  34. *
  35. * Abstract:
  36. *
  37. * Remote Procedure Call (RPC) Client Interface
  38. *
  39. * DCE/RPC binding functions
  40. *
  41. * Authors: Rafal Szczesniak (rafal@likewise.com)
  42. */
  43. #include "includes.h"
  44. static
  45. NTSTATUS
  46. SamrInitBindingDefaultA(
  47. OUT PSAMR_BINDING phBinding,
  48. IN PCSTR pszHostname,
  49. IN PIO_CREDS pCreds
  50. );
  51. static
  52. NTSTATUS
  53. SamrInitBindingFullA(
  54. OUT PSAMR_BINDING phBinding,
  55. IN PCSTR pszProtSeq,
  56. IN PCSTR pszHostname,
  57. IN PCSTR pszEndpoint,
  58. IN PCSTR pszUuid,
  59. IN PCSTR pszOptions,
  60. IN PIO_CREDS pCreds
  61. );
  62. NTSTATUS
  63. SamrInitBindingDefault(
  64. OUT PSAMR_BINDING phBinding,
  65. IN PCWSTR pwszHostname,
  66. IN PIO_CREDS pCreds
  67. )
  68. {
  69. NTSTATUS ntStatus = STATUS_SUCCESS;
  70. DWORD dwError = ERROR_SUCCESS;
  71. PSTR pszHostname = NULL;
  72. if (pwszHostname)
  73. {
  74. dwError = LwWc16sToMbs(pwszHostname, &pszHostname);
  75. BAIL_ON_WIN_ERROR(dwError);
  76. }
  77. ntStatus = SamrInitBindingDefaultA(phBinding,
  78. pszHostname,
  79. pCreds);
  80. BAIL_ON_NT_STATUS(ntStatus);
  81. cleanup:
  82. LW_SAFE_FREE_MEMORY(pszHostname);
  83. if (ntStatus == STATUS_SUCCESS &&
  84. dwError != ERROR_SUCCESS)
  85. {
  86. ntStatus = LwWin32ErrorToNtStatus(dwError);
  87. }
  88. return ntStatus;
  89. error:
  90. goto cleanup;
  91. }
  92. static
  93. NTSTATUS
  94. SamrInitBindingDefaultA(
  95. OUT PSAMR_BINDING phBinding,
  96. IN PCSTR pszHostname,
  97. IN PIO_CREDS pCreds
  98. )
  99. {
  100. NTSTATUS ntStatus = STATUS_SUCCESS;
  101. PSTR pszProtSeq = (PSTR)SAMR_DEFAULT_PROT_SEQ;
  102. PSTR pszLpcProtSeq = (PSTR)"ncalrpc";
  103. PSTR pszEndpoint = (PSTR)SAMR_DEFAULT_ENDPOINT;
  104. PSTR pszLpcEndpoint = (PSTR)SAMR_LOCAL_ENDPOINT;
  105. PSTR pszUuid = NULL;
  106. PSTR pszOptions = NULL;
  107. SAMR_BINDING hBinding = NULL;
  108. BAIL_ON_INVALID_PTR(phBinding, ntStatus);
  109. ntStatus = SamrInitBindingFullA(
  110. &hBinding,
  111. (pszHostname) ? pszProtSeq : pszLpcProtSeq,
  112. pszHostname,
  113. (pszHostname) ? pszEndpoint : pszLpcEndpoint,
  114. pszUuid,
  115. pszOptions,
  116. pCreds);
  117. BAIL_ON_NT_STATUS(ntStatus);
  118. *phBinding = hBinding;
  119. cleanup:
  120. return ntStatus;
  121. error:
  122. if (phBinding)
  123. {
  124. *phBinding = NULL;
  125. }
  126. goto cleanup;
  127. }
  128. NTSTATUS
  129. SamrInitBindingFull(
  130. OUT PSAMR_BINDING phBinding,
  131. IN PCWSTR pwszProtSeq,
  132. IN PCWSTR pwszHostname,
  133. IN PCWSTR pwszEndpoint,
  134. IN PCWSTR pwszUuid,
  135. IN PCWSTR pwszOptions,
  136. IN PIO_CREDS pCreds
  137. )
  138. {
  139. NTSTATUS ntStatus = STATUS_SUCCESS;
  140. DWORD dwError = ERROR_SUCCESS;
  141. PSTR pszProtSeq = NULL;
  142. PSTR pszHostname = NULL;
  143. PSTR pszEndpoint = NULL;
  144. PSTR pszUuid = NULL;
  145. PSTR pszOptions = NULL;
  146. SAMR_BINDING hBinding = NULL;
  147. BAIL_ON_INVALID_PTR(phBinding, ntStatus);
  148. BAIL_ON_INVALID_PTR(pwszProtSeq, ntStatus);
  149. dwError = LwWc16sToMbs(pwszProtSeq, &pszProtSeq);
  150. BAIL_ON_WIN_ERROR(dwError);
  151. if (pwszHostname)
  152. {
  153. dwError = LwWc16sToMbs(pwszHostname, &pszHostname);
  154. BAIL_ON_WIN_ERROR(dwError);
  155. }
  156. dwError = LwWc16sToMbs(pwszEndpoint, &pszEndpoint);
  157. BAIL_ON_WIN_ERROR(dwError);
  158. if (pwszUuid)
  159. {
  160. dwError = LwWc16sToMbs(pwszUuid, &pszUuid);
  161. BAIL_ON_WIN_ERROR(dwError);
  162. }
  163. if (pwszOptions)
  164. {
  165. dwError = LwWc16sToMbs(pwszOptions, &pszOptions);
  166. BAIL_ON_WIN_ERROR(dwError);
  167. }
  168. ntStatus = SamrInitBindingFullA(&hBinding,
  169. pszProtSeq,
  170. pszHostname,
  171. pszEndpoint,
  172. pszUuid,
  173. pszOptions,
  174. pCreds);
  175. BAIL_ON_NT_STATUS(ntStatus);
  176. *phBinding = hBinding;
  177. cleanup:
  178. LW_SAFE_FREE_MEMORY(pszProtSeq);
  179. LW_SAFE_FREE_MEMORY(pszHostname);
  180. LW_SAFE_FREE_MEMORY(pszEndpoint);
  181. LW_SAFE_FREE_MEMORY(pszUuid);
  182. LW_SAFE_FREE_MEMORY(pszOptions);
  183. if (ntStatus == STATUS_SUCCESS &&
  184. dwError != ERROR_SUCCESS)
  185. {
  186. ntStatus = LwWin32ErrorToNtStatus(dwError);
  187. }
  188. return ntStatus;
  189. error:
  190. if (phBinding)
  191. {
  192. *phBinding = NULL;
  193. }
  194. goto cleanup;
  195. }
  196. static
  197. NTSTATUS
  198. SamrInitBindingFullA(
  199. OUT PSAMR_BINDING phBinding,
  200. IN PCSTR pszProtSeq,
  201. IN PCSTR pszHostname,
  202. IN PCSTR pszEndpoint,
  203. IN PCSTR pszUuid,
  204. IN PCSTR pszOptions,
  205. IN PIO_CREDS pCreds
  206. )
  207. {
  208. NTSTATUS ntStatus = STATUS_SUCCESS;
  209. unsigned32 rpcStatus = RPC_S_OK;
  210. unsigned32 st = RPC_S_OK;
  211. unsigned char *binding_string = NULL;
  212. unsigned char *prot_seq = NULL;
  213. unsigned char *endpoint = NULL;
  214. unsigned char *uuid = NULL;
  215. unsigned char *options = NULL;
  216. unsigned char *address = NULL;
  217. handle_t hBinding = NULL;
  218. rpc_transport_info_handle_t hInfo = NULL;
  219. BAIL_ON_INVALID_PTR(phBinding, ntStatus);
  220. BAIL_ON_INVALID_PTR(pszProtSeq, ntStatus);
  221. prot_seq = (unsigned char*) strdup(pszProtSeq);
  222. BAIL_ON_NULL_PTR(prot_seq, ntStatus);
  223. if (pszEndpoint != NULL)
  224. {
  225. endpoint = (unsigned char*) strdup(pszEndpoint);
  226. BAIL_ON_NULL_PTR(endpoint, ntStatus);
  227. }
  228. if (pszUuid != NULL)
  229. {
  230. uuid = (unsigned char*) strdup(pszUuid);
  231. BAIL_ON_NULL_PTR(uuid, ntStatus);
  232. }
  233. if (pszOptions != NULL)
  234. {
  235. options = (unsigned char*) strdup(pszOptions);
  236. BAIL_ON_NULL_PTR(options, ntStatus);
  237. }
  238. if (pszHostname != NULL)
  239. {
  240. address = (unsigned char*) strdup(pszHostname);
  241. BAIL_ON_NULL_PTR(address, ntStatus);
  242. }
  243. rpc_string_binding_compose(
  244. uuid,
  245. prot_seq,
  246. address,
  247. endpoint,
  248. options,
  249. &binding_string,
  250. &rpcStatus);
  251. BAIL_ON_RPC_STATUS(rpcStatus);
  252. rpc_binding_from_string_binding(
  253. binding_string,
  254. &hBinding,
  255. &rpcStatus);
  256. BAIL_ON_RPC_STATUS(rpcStatus);
  257. if (strcmp(pszProtSeq, "ncacn_np") == 0)
  258. {
  259. rpc_smb_transport_info_from_lwio_creds(
  260. pCreds,
  261. &hInfo,
  262. &rpcStatus);
  263. BAIL_ON_RPC_STATUS(rpcStatus);
  264. rpc_binding_set_transport_info(
  265. hBinding,
  266. hInfo,
  267. &rpcStatus);
  268. BAIL_ON_RPC_STATUS(rpcStatus);
  269. hInfo = NULL;
  270. }
  271. rpc_mgmt_set_com_timeout(hBinding,
  272. 6,
  273. &rpcStatus);
  274. BAIL_ON_RPC_STATUS(rpcStatus);
  275. *phBinding = (SAMR_BINDING)hBinding;
  276. cleanup:
  277. LW_SAFE_FREE_MEMORY(prot_seq);
  278. LW_SAFE_FREE_MEMORY(endpoint);
  279. LW_SAFE_FREE_MEMORY(uuid);
  280. LW_SAFE_FREE_MEMORY(options);
  281. LW_SAFE_FREE_MEMORY(address);
  282. if (hInfo)
  283. {
  284. rpc_smb_transport_info_free(hInfo);
  285. }
  286. if (binding_string)
  287. {
  288. rpc_string_free(&binding_string, &st);
  289. }
  290. if (rpcStatus == RPC_S_OK &&
  291. st != RPC_S_OK) {
  292. rpcStatus = st;
  293. }
  294. if (ntStatus == STATUS_SUCCESS &&
  295. rpcStatus != RPC_S_OK)
  296. {
  297. ntStatus = LwRpcStatusToNtStatus(rpcStatus);
  298. }
  299. return ntStatus;
  300. error:
  301. if (hBinding)
  302. {
  303. rpc_binding_free(&hBinding, &rpcStatus);
  304. }
  305. if (phBinding)
  306. {
  307. *phBinding = NULL;
  308. }
  309. goto cleanup;
  310. }
  311. VOID
  312. SamrFreeBinding(
  313. IN PSAMR_BINDING phBinding
  314. )
  315. {
  316. unsigned32 rpcStatus = RPC_S_OK;
  317. /* Free the binding itself */
  318. if (phBinding && *phBinding)
  319. {
  320. rpc_binding_free((handle_t*)phBinding, &rpcStatus);
  321. BAIL_ON_RPC_STATUS(rpcStatus);
  322. *phBinding = NULL;
  323. }
  324. cleanup:
  325. return;
  326. error:
  327. goto cleanup;
  328. }