s3-rpc: Avoid including every pipe's client and server stubs everywhere in samba.
[kamenim/samba.git] / source3 / lib / netapi / getdc.c
index 85a0ae52efbd4c8db2c6f51c17161e33aaa2d75e..9af01cecfdd47c14e40eea183d453436ce8b2ff9 100644 (file)
 
 #include "includes.h"
 
+#include "librpc/gen_ndr/libnetapi.h"
 #include "lib/netapi/netapi.h"
+#include "lib/netapi/netapi_private.h"
+#include "lib/netapi/libnetapi.h"
 #include "libnet/libnet.h"
+#include "../librpc/gen_ndr/cli_netlogon.h"
 
-#if 0
-#include "librpc/gen_ndr/cli_netlogon.h"
-#endif
-
-NTSTATUS rpccli_netr_GetDcName(struct rpc_pipe_client *cli,
-                              TALLOC_CTX *mem_ctx,
-                              const char *logon_server,
-                              const char *domainname,
-                              const char **dcname);
-NTSTATUS rpccli_netr_GetAnyDCName(struct rpc_pipe_client *cli,
-                                 TALLOC_CTX *mem_ctx,
-                                 const char *logon_server,
-                                 const char *domainname,
-                                 const char **dcname,
-                                 WERROR *werror);
-
-static WERROR NetGetDCNameLocal(struct libnetapi_ctx *ctx,
-                               const char *server_name,
-                               const char *domain_name,
-                               uint8_t **buffer)
+/********************************************************************
+********************************************************************/
+
+WERROR NetGetDCName_l(struct libnetapi_ctx *ctx,
+                     struct NetGetDCName *r)
 {
-       return WERR_NOT_SUPPORTED;
+       LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetGetDCName);
 }
 
-static WERROR NetGetDCNameRemote(struct libnetapi_ctx *ctx,
-                                const char *server_name,
-                                const char *domain_name,
-                                uint8_t **buffer)
+/********************************************************************
+********************************************************************/
+
+WERROR NetGetDCName_r(struct libnetapi_ctx *ctx,
+                     struct NetGetDCName *r)
 {
-       struct cli_state *cli = NULL;
        struct rpc_pipe_client *pipe_cli = NULL;
        NTSTATUS status;
        WERROR werr;
 
-       status = cli_full_connection(&cli, NULL, server_name,
-                                    NULL, 0,
-                                    "IPC$", "IPC",
-                                    ctx->username,
-                                    ctx->workgroup,
-                                    ctx->password,
-                                    0, Undefined, NULL);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               werr = ntstatus_to_werror(status);
+       werr = libnetapi_open_pipe(ctx, r->in.server_name,
+                                  &ndr_table_netlogon.syntax_id,
+                                  &pipe_cli);
+       if (!W_ERROR_IS_OK(werr)) {
                goto done;
        }
 
-       pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON,
-                                           &status);
-       if (!pipe_cli) {
+       status = rpccli_netr_GetDcName(pipe_cli, ctx,
+                                      r->in.server_name,
+                                      r->in.domain_name,
+                                      (const char **)r->out.buffer,
+                                      &werr);
+
+       if (!NT_STATUS_IS_OK(status)) {
                werr = ntstatus_to_werror(status);
-               goto done;
-       };
-
-#if 0
-       werr = rpccli_netr_GetDcName(pipe_cli, ctx,
-                                    server_name,
-                                    domain_name,
-                                    (const char **)&buffer);
-#else
-       werr = rpccli_netlogon_getdcname(pipe_cli, ctx,
-                                        server_name,
-                                        domain_name,
-                                        (char **)buffer);
-#endif
- done:
-       if (cli) {
-               cli_shutdown(cli);
        }
+ done:
 
        return werr;
 }
 
-static WERROR libnetapi_NetGetDCName(struct libnetapi_ctx *ctx,
-                                    const char *server_name,
-                                    const char *domain_name,
-                                    uint8_t **buffer)
-{
-       if (!server_name || is_myname_or_ipaddr(server_name)) {
-               return NetGetDCNameLocal(ctx,
-                                        server_name,
-                                        domain_name,
-                                        buffer);
-       }
-
-       return NetGetDCNameRemote(ctx,
-                                 server_name,
-                                 domain_name,
-                                 buffer);
-}
+/********************************************************************
+********************************************************************/
 
-NET_API_STATUS NetGetDCName(const char *server_name,
-                           const char *domain_name,
-                           uint8_t **buffer)
+WERROR NetGetAnyDCName_l(struct libnetapi_ctx *ctx,
+                        struct NetGetAnyDCName *r)
 {
-       struct libnetapi_ctx *ctx = NULL;
-       NET_API_STATUS status;
-       WERROR werr;
-
-       status = libnetapi_getctx(&ctx);
-       if (status != 0) {
-               return status;
-       }
-
-       werr = libnetapi_NetGetDCName(ctx,
-                                     server_name,
-                                     domain_name,
-                                     buffer);
-       if (!W_ERROR_IS_OK(werr)) {
-               return W_ERROR_V(werr);
-       }
-
-       return 0;
+       LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetGetAnyDCName);
 }
 
-static WERROR NetGetAnyDCNameLocal(struct libnetapi_ctx *ctx,
-                                  const char *server_name,
-                                  const char *domain_name,
-                                  uint8_t **buffer)
-{
-       return WERR_NOT_SUPPORTED;
-}
+/********************************************************************
+********************************************************************/
 
-static WERROR NetGetAnyDCNameRemote(struct libnetapi_ctx *ctx,
-                                   const char *server_name,
-                                   const char *domain_name,
-                                   uint8_t **buffer)
+WERROR NetGetAnyDCName_r(struct libnetapi_ctx *ctx,
+                        struct NetGetAnyDCName *r)
 {
-       struct cli_state *cli = NULL;
        struct rpc_pipe_client *pipe_cli = NULL;
        NTSTATUS status;
        WERROR werr;
 
-       status = cli_full_connection(&cli, NULL, server_name,
-                                    NULL, 0,
-                                    "IPC$", "IPC",
-                                    ctx->username,
-                                    ctx->workgroup,
-                                    ctx->password,
-                                    0, Undefined, NULL);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               werr = ntstatus_to_werror(status);
+       werr = libnetapi_open_pipe(ctx, r->in.server_name,
+                                  &ndr_table_netlogon.syntax_id,
+                                  &pipe_cli);
+       if (!W_ERROR_IS_OK(werr)) {
                goto done;
        }
 
-       pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON,
-                                           &status);
-       if (!pipe_cli) {
-               werr = ntstatus_to_werror(status);
-               goto done;
-       };
-
-#if 0
        status = rpccli_netr_GetAnyDCName(pipe_cli, ctx,
-                                         server_name,
-                                         domain_name,
-                                         (const char **)&buffer,
+                                         r->in.server_name,
+                                         r->in.domain_name,
+                                         (const char **)r->out.buffer,
                                          &werr);
        if (!NT_STATUS_IS_OK(status)) {
-               werr = ntstatus_to_werror(status);
                goto done;
        }
-#else
-       werr = rpccli_netlogon_getanydcname(pipe_cli, ctx,
-                                           server_name,
-                                           domain_name,
-                                           (char **)buffer);
-#endif
  done:
-       if (cli) {
-               cli_shutdown(cli);
-       }
 
        return werr;
 
 }
 
-static WERROR libnetapi_NetGetAnyDCName(struct libnetapi_ctx *ctx,
-                                       const char *server_name,
-                                       const char *domain_name,
-                                       uint8_t **buffer)
+/********************************************************************
+********************************************************************/
+
+WERROR DsGetDcName_l(struct libnetapi_ctx *ctx,
+                    struct DsGetDcName *r)
 {
-       if (!server_name || is_myname_or_ipaddr(server_name)) {
-               return NetGetAnyDCNameLocal(ctx,
-                                           server_name,
-                                           domain_name,
-                                           buffer);
+       NTSTATUS status;
+
+       status = dsgetdcname(ctx,
+                            NULL,
+                            r->in.domain_name,
+                            r->in.domain_guid,
+                            r->in.site_name,
+                            r->in.flags,
+                            (struct netr_DsRGetDCNameInfo **)r->out.dc_info);
+       if (!NT_STATUS_IS_OK(status)) {
+               libnetapi_set_error_string(ctx,
+                       "failed to find DC: %s",
+                       get_friendly_nt_error_msg(status));
        }
 
-       return NetGetAnyDCNameRemote(ctx,
-                                    server_name,
-                                    domain_name,
-                                    buffer);
+       return ntstatus_to_werror(status);
 }
 
-NET_API_STATUS NetGetAnyDCName(const char *server_name,
-                              const char *domain_name,
-                              uint8_t **buffer)
+/********************************************************************
+********************************************************************/
+
+WERROR DsGetDcName_r(struct libnetapi_ctx *ctx,
+                    struct DsGetDcName *r)
 {
-       struct libnetapi_ctx *ctx = NULL;
-       NET_API_STATUS status;
        WERROR werr;
+       NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
+       struct rpc_pipe_client *pipe_cli = NULL;
 
-       status = libnetapi_getctx(&ctx);
-       if (status != 0) {
-               return status;
+       werr = libnetapi_open_pipe(ctx, r->in.server_name,
+                                  &ndr_table_netlogon.syntax_id,
+                                  &pipe_cli);
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
        }
 
-       werr = libnetapi_NetGetAnyDCName(ctx,
-                                        server_name,
-                                        domain_name,
-                                        buffer);
-       if (!W_ERROR_IS_OK(werr)) {
-               return W_ERROR_V(werr);
+       status = rpccli_netr_DsRGetDCName(pipe_cli,
+                                         ctx,
+                                         r->in.server_name,
+                                         r->in.domain_name,
+                                         r->in.domain_guid,
+                                         NULL,
+                                         r->in.flags,
+                                         (struct netr_DsRGetDCNameInfo **)r->out.dc_info,
+                                         &werr);
+       if (!NT_STATUS_IS_OK(status)) {
+               werr = ntstatus_to_werror(status);
+               goto done;
        }
 
-       return 0;
+ done:
+       return werr;
 }