/source4/torture/libnet/libnet_rpc.c

https://bitbucket.org/knarf/samba · C · 230 lines · 155 code · 45 blank · 30 comment · 17 complexity · 69f08c383804c06d3fd4926db12928cb MD5 · raw file

  1. /*
  2. Unix SMB/CIFS implementation.
  3. Test suite for libnet calls.
  4. Copyright (C) Rafal Szczesniak 2005
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "includes.h"
  17. #include "lib/cmdline/popt_common.h"
  18. #include "libnet/libnet.h"
  19. #include "libcli/security/security.h"
  20. #include "librpc/gen_ndr/ndr_lsa.h"
  21. #include "librpc/gen_ndr/ndr_samr.h"
  22. #include "librpc/gen_ndr/ndr_srvsvc.h"
  23. #include "torture/rpc/torture_rpc.h"
  24. #include "torture/libnet/proto.h"
  25. #include "param/param.h"
  26. static bool test_connect_service(struct torture_context *tctx,
  27. struct libnet_context *ctx,
  28. const struct ndr_interface_table *iface,
  29. const char *binding_string,
  30. const char *hostname,
  31. const enum libnet_RpcConnect_level level,
  32. bool badcreds, NTSTATUS expected_status)
  33. {
  34. NTSTATUS status;
  35. struct libnet_RpcConnect connect_r;
  36. ZERO_STRUCT(connect_r);
  37. connect_r.level = level;
  38. connect_r.in.binding = binding_string;
  39. connect_r.in.name = hostname;
  40. connect_r.in.dcerpc_iface = iface;
  41. /* if bad credentials are needed, set baduser%badpassword instead
  42. of default commandline-passed credentials */
  43. if (badcreds) {
  44. cli_credentials_set_username(ctx->cred, "baduser", CRED_SPECIFIED);
  45. cli_credentials_set_password(ctx->cred, "badpassword", CRED_SPECIFIED);
  46. }
  47. status = libnet_RpcConnect(ctx, ctx, &connect_r);
  48. if (!NT_STATUS_EQUAL(status, expected_status)) {
  49. torture_comment(tctx, "Connecting to rpc service %s on %s.\n\tFAILED. Expected: %s."
  50. "Received: %s\n",
  51. connect_r.in.dcerpc_iface->name, connect_r.in.binding, nt_errstr(expected_status),
  52. nt_errstr(status));
  53. return false;
  54. }
  55. torture_comment(tctx, "PASSED. Expected: %s, received: %s\n", nt_errstr(expected_status),
  56. nt_errstr(status));
  57. if (connect_r.level == LIBNET_RPC_CONNECT_DC_INFO && NT_STATUS_IS_OK(status)) {
  58. torture_comment(tctx, "Domain Controller Info:\n");
  59. torture_comment(tctx, "\tDomain Name:\t %s\n", connect_r.out.domain_name);
  60. torture_comment(tctx, "\tDomain SID:\t %s\n", dom_sid_string(ctx, connect_r.out.domain_sid));
  61. torture_comment(tctx, "\tRealm:\t\t %s\n", connect_r.out.realm);
  62. torture_comment(tctx, "\tGUID:\t\t %s\n", GUID_string(ctx, connect_r.out.guid));
  63. } else if (!NT_STATUS_IS_OK(status)) {
  64. torture_comment(tctx, "Error string: %s\n", connect_r.out.error_string);
  65. }
  66. return true;
  67. }
  68. static bool torture_rpc_connect(struct torture_context *torture,
  69. const enum libnet_RpcConnect_level level,
  70. const char *bindstr, const char *hostname)
  71. {
  72. struct libnet_context *ctx;
  73. ctx = libnet_context_init(torture->ev, torture->lp_ctx);
  74. ctx->cred = cmdline_credentials;
  75. torture_comment(torture, "Testing connection to LSA interface\n");
  76. if (!test_connect_service(torture, ctx, &ndr_table_lsarpc, bindstr,
  77. hostname, level, false, NT_STATUS_OK)) {
  78. torture_comment(torture, "failed to connect LSA interface\n");
  79. return false;
  80. }
  81. torture_comment(torture, "Testing connection to SAMR interface\n");
  82. if (!test_connect_service(torture, ctx, &ndr_table_samr, bindstr,
  83. hostname, level, false, NT_STATUS_OK)) {
  84. torture_comment(torture, "failed to connect SAMR interface\n");
  85. return false;
  86. }
  87. torture_comment(torture, "Testing connection to SRVSVC interface\n");
  88. if (!test_connect_service(torture, ctx, &ndr_table_srvsvc, bindstr,
  89. hostname, level, false, NT_STATUS_OK)) {
  90. torture_comment(torture, "failed to connect SRVSVC interface\n");
  91. return false;
  92. }
  93. torture_comment(torture, "Testing connection to LSA interface with wrong credentials\n");
  94. if (!test_connect_service(torture, ctx, &ndr_table_lsarpc, bindstr,
  95. hostname, level, true, NT_STATUS_LOGON_FAILURE)) {
  96. torture_comment(torture, "failed to test wrong credentials on LSA interface\n");
  97. return false;
  98. }
  99. torture_comment(torture, "Testing connection to SAMR interface with wrong credentials\n");
  100. if (!test_connect_service(torture, ctx, &ndr_table_samr, bindstr,
  101. hostname, level, true, NT_STATUS_LOGON_FAILURE)) {
  102. torture_comment(torture, "failed to test wrong credentials on SAMR interface\n");
  103. return false;
  104. }
  105. talloc_free(ctx);
  106. return true;
  107. }
  108. bool torture_rpc_connect_srv(struct torture_context *torture)
  109. {
  110. const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_SERVER;
  111. NTSTATUS status;
  112. struct dcerpc_binding *binding;
  113. const char *host;
  114. status = torture_rpc_binding(torture, &binding);
  115. if (!NT_STATUS_IS_OK(status)) {
  116. return false;
  117. }
  118. host = dcerpc_binding_get_string_option(binding, "host");
  119. return torture_rpc_connect(torture, level, NULL, host);
  120. }
  121. bool torture_rpc_connect_pdc(struct torture_context *torture)
  122. {
  123. const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_PDC;
  124. NTSTATUS status;
  125. struct dcerpc_binding *binding;
  126. const char *domain_name;
  127. status = torture_rpc_binding(torture, &binding);
  128. if (!NT_STATUS_IS_OK(status)) {
  129. return false;
  130. }
  131. /* we're accessing domain controller so the domain name should be
  132. passed (it's going to be resolved to dc name and address) instead
  133. of specific server name. */
  134. domain_name = lpcfg_workgroup(torture->lp_ctx);
  135. return torture_rpc_connect(torture, level, NULL, domain_name);
  136. }
  137. bool torture_rpc_connect_dc(struct torture_context *torture)
  138. {
  139. const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_DC;
  140. NTSTATUS status;
  141. struct dcerpc_binding *binding;
  142. const char *domain_name;
  143. status = torture_rpc_binding(torture, &binding);
  144. if (!NT_STATUS_IS_OK(status)) {
  145. return false;
  146. }
  147. /* we're accessing domain controller so the domain name should be
  148. passed (it's going to be resolved to dc name and address) instead
  149. of specific server name. */
  150. domain_name = lpcfg_workgroup(torture->lp_ctx);
  151. return torture_rpc_connect(torture, level, NULL, domain_name);
  152. }
  153. bool torture_rpc_connect_dc_info(struct torture_context *torture)
  154. {
  155. const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_DC_INFO;
  156. NTSTATUS status;
  157. struct dcerpc_binding *binding;
  158. const char *domain_name;
  159. status = torture_rpc_binding(torture, &binding);
  160. if (!NT_STATUS_IS_OK(status)) {
  161. return false;
  162. }
  163. /* we're accessing domain controller so the domain name should be
  164. passed (it's going to be resolved to dc name and address) instead
  165. of specific server name. */
  166. domain_name = lpcfg_workgroup(torture->lp_ctx);
  167. return torture_rpc_connect(torture, level, NULL, domain_name);
  168. }
  169. bool torture_rpc_connect_binding(struct torture_context *torture)
  170. {
  171. const enum libnet_RpcConnect_level level = LIBNET_RPC_CONNECT_BINDING;
  172. NTSTATUS status;
  173. struct dcerpc_binding *binding;
  174. const char *bindstr;
  175. status = torture_rpc_binding(torture, &binding);
  176. if (!NT_STATUS_IS_OK(status)) {
  177. return false;
  178. }
  179. bindstr = dcerpc_binding_string(torture, binding);
  180. return torture_rpc_connect(torture, level, bindstr, NULL);
  181. }