Restructure inner workings of libnetapi a bit.
[samba.git] / source / lib / netapi / getdc.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  NetApi GetDC Support
4  *  Copyright (C) Guenther Deschner 2007
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21
22 #include "librpc/gen_ndr/libnetapi.h"
23 #include "lib/netapi/netapi.h"
24 #include "lib/netapi/libnetapi.h"
25 #include "libnet/libnet.h"
26
27 /********************************************************************
28 ********************************************************************/
29
30 WERROR NetGetDCName_l(struct libnetapi_ctx *ctx,
31                       struct NetGetDCName *r)
32 {
33         return WERR_NOT_SUPPORTED;
34 }
35
36 /********************************************************************
37 ********************************************************************/
38
39 WERROR NetGetDCName_r(struct libnetapi_ctx *ctx,
40                       struct NetGetDCName *r)
41 {
42         struct cli_state *cli = NULL;
43         struct rpc_pipe_client *pipe_cli = NULL;
44         NTSTATUS status;
45         WERROR werr;
46
47         status = cli_full_connection(&cli, NULL, r->in.server_name,
48                                      NULL, 0,
49                                      "IPC$", "IPC",
50                                      ctx->username,
51                                      ctx->workgroup,
52                                      ctx->password,
53                                      0, Undefined, NULL);
54
55         if (!NT_STATUS_IS_OK(status)) {
56                 werr = ntstatus_to_werror(status);
57                 goto done;
58         }
59
60         pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON,
61                                             &status);
62         if (!pipe_cli) {
63                 werr = ntstatus_to_werror(status);
64                 goto done;
65         }
66
67         status = rpccli_netr_GetDcName(pipe_cli, ctx,
68                                        r->in.server_name,
69                                        r->in.domain_name,
70                                        (const char **)r->out.buffer,
71                                        &werr);
72  done:
73         if (cli) {
74                 cli_shutdown(cli);
75         }
76
77         return werr;
78 }
79
80 /********************************************************************
81 ********************************************************************/
82
83 WERROR NetGetAnyDCName_l(struct libnetapi_ctx *ctx,
84                          struct NetGetAnyDCName *r)
85 {
86         return WERR_NOT_SUPPORTED;
87 }
88
89 /********************************************************************
90 ********************************************************************/
91
92 WERROR NetGetAnyDCName_r(struct libnetapi_ctx *ctx,
93                          struct NetGetAnyDCName *r)
94 {
95         struct cli_state *cli = NULL;
96         struct rpc_pipe_client *pipe_cli = NULL;
97         NTSTATUS status;
98         WERROR werr;
99
100         status = cli_full_connection(&cli, NULL, r->in.server_name,
101                                      NULL, 0,
102                                      "IPC$", "IPC",
103                                      ctx->username,
104                                      ctx->workgroup,
105                                      ctx->password,
106                                      0, Undefined, NULL);
107
108         if (!NT_STATUS_IS_OK(status)) {
109                 werr = ntstatus_to_werror(status);
110                 goto done;
111         }
112
113         pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON,
114                                             &status);
115         if (!pipe_cli) {
116                 werr = ntstatus_to_werror(status);
117                 goto done;
118         };
119
120         status = rpccli_netr_GetAnyDCName(pipe_cli, ctx,
121                                           r->in.server_name,
122                                           r->in.domain_name,
123                                           (const char **)r->out.buffer,
124                                           &werr);
125         if (!NT_STATUS_IS_OK(status)) {
126                 goto done;
127         }
128  done:
129         if (cli) {
130                 cli_shutdown(cli);
131         }
132
133         return werr;
134
135 }