Merge branch 'v3-2-test' of git://git.samba.org/samba into v3-2-test
[kamenim/samba.git] / source3 / 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 "lib/netapi/netapi.h"
23 #include "libnet/libnet.h"
24
25 #if 0
26 #include "librpc/gen_ndr/cli_netlogon.h"
27 #endif
28
29 NTSTATUS rpccli_netr_GetDcName(struct rpc_pipe_client *cli,
30                                TALLOC_CTX *mem_ctx,
31                                const char *logon_server,
32                                const char *domainname,
33                                const char **dcname);
34 NTSTATUS rpccli_netr_GetAnyDCName(struct rpc_pipe_client *cli,
35                                   TALLOC_CTX *mem_ctx,
36                                   const char *logon_server,
37                                   const char *domainname,
38                                   const char **dcname,
39                                   WERROR *werror);
40
41 static WERROR NetGetDCNameLocal(struct libnetapi_ctx *ctx,
42                                 const char *server_name,
43                                 const char *domain_name,
44                                 uint8_t **buffer)
45 {
46         return WERR_NOT_SUPPORTED;
47 }
48
49 static WERROR NetGetDCNameRemote(struct libnetapi_ctx *ctx,
50                                  const char *server_name,
51                                  const char *domain_name,
52                                  uint8_t **buffer)
53 {
54         struct cli_state *cli = NULL;
55         struct rpc_pipe_client *pipe_cli = NULL;
56         NTSTATUS status;
57         WERROR werr;
58
59         status = cli_full_connection(&cli, NULL, server_name,
60                                      NULL, 0,
61                                      "IPC$", "IPC",
62                                      ctx->username,
63                                      ctx->workgroup,
64                                      ctx->password,
65                                      0, Undefined, NULL);
66
67         if (!NT_STATUS_IS_OK(status)) {
68                 werr = ntstatus_to_werror(status);
69                 goto done;
70         }
71
72         pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON,
73                                             &status);
74         if (!pipe_cli) {
75                 werr = ntstatus_to_werror(status);
76                 goto done;
77         };
78
79 #if 0
80         werr = rpccli_netr_GetDcName(pipe_cli, ctx,
81                                      server_name,
82                                      domain_name,
83                                      (const char **)&buffer);
84 #else
85         werr = rpccli_netlogon_getdcname(pipe_cli, ctx,
86                                          server_name,
87                                          domain_name,
88                                          (char **)buffer);
89 #endif
90  done:
91         if (cli) {
92                 cli_shutdown(cli);
93         }
94
95         return werr;
96 }
97
98 static WERROR libnetapi_NetGetDCName(struct libnetapi_ctx *ctx,
99                                      const char *server_name,
100                                      const char *domain_name,
101                                      uint8_t **buffer)
102 {
103         if (!server_name || is_myname_or_ipaddr(server_name)) {
104                 return NetGetDCNameLocal(ctx,
105                                          server_name,
106                                          domain_name,
107                                          buffer);
108         }
109
110         return NetGetDCNameRemote(ctx,
111                                   server_name,
112                                   domain_name,
113                                   buffer);
114 }
115
116 NET_API_STATUS NetGetDCName(const char *server_name,
117                             const char *domain_name,
118                             uint8_t **buffer)
119 {
120         struct libnetapi_ctx *ctx = NULL;
121         NET_API_STATUS status;
122         WERROR werr;
123
124         status = libnetapi_getctx(&ctx);
125         if (status != 0) {
126                 return status;
127         }
128
129         werr = libnetapi_NetGetDCName(ctx,
130                                       server_name,
131                                       domain_name,
132                                       buffer);
133         if (!W_ERROR_IS_OK(werr)) {
134                 return W_ERROR_V(werr);
135         }
136
137         return 0;
138 }
139
140 static WERROR NetGetAnyDCNameLocal(struct libnetapi_ctx *ctx,
141                                    const char *server_name,
142                                    const char *domain_name,
143                                    uint8_t **buffer)
144 {
145         return WERR_NOT_SUPPORTED;
146 }
147
148 static WERROR NetGetAnyDCNameRemote(struct libnetapi_ctx *ctx,
149                                     const char *server_name,
150                                     const char *domain_name,
151                                     uint8_t **buffer)
152 {
153         struct cli_state *cli = NULL;
154         struct rpc_pipe_client *pipe_cli = NULL;
155         NTSTATUS status;
156         WERROR werr;
157
158         status = cli_full_connection(&cli, NULL, server_name,
159                                      NULL, 0,
160                                      "IPC$", "IPC",
161                                      ctx->username,
162                                      ctx->workgroup,
163                                      ctx->password,
164                                      0, Undefined, NULL);
165
166         if (!NT_STATUS_IS_OK(status)) {
167                 werr = ntstatus_to_werror(status);
168                 goto done;
169         }
170
171         pipe_cli = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON,
172                                             &status);
173         if (!pipe_cli) {
174                 werr = ntstatus_to_werror(status);
175                 goto done;
176         };
177
178 #if 0
179         status = rpccli_netr_GetAnyDCName(pipe_cli, ctx,
180                                           server_name,
181                                           domain_name,
182                                           (const char **)&buffer,
183                                           &werr);
184         if (!NT_STATUS_IS_OK(status)) {
185                 werr = ntstatus_to_werror(status);
186                 goto done;
187         }
188 #else
189         werr = rpccli_netlogon_getanydcname(pipe_cli, ctx,
190                                             server_name,
191                                             domain_name,
192                                             (char **)buffer);
193 #endif
194  done:
195         if (cli) {
196                 cli_shutdown(cli);
197         }
198
199         return werr;
200
201 }
202
203 static WERROR libnetapi_NetGetAnyDCName(struct libnetapi_ctx *ctx,
204                                         const char *server_name,
205                                         const char *domain_name,
206                                         uint8_t **buffer)
207 {
208         if (!server_name || is_myname_or_ipaddr(server_name)) {
209                 return NetGetAnyDCNameLocal(ctx,
210                                             server_name,
211                                             domain_name,
212                                             buffer);
213         }
214
215         return NetGetAnyDCNameRemote(ctx,
216                                      server_name,
217                                      domain_name,
218                                      buffer);
219 }
220
221 NET_API_STATUS NetGetAnyDCName(const char *server_name,
222                                const char *domain_name,
223                                uint8_t **buffer)
224 {
225         struct libnetapi_ctx *ctx = NULL;
226         NET_API_STATUS status;
227         WERROR werr;
228
229         status = libnetapi_getctx(&ctx);
230         if (status != 0) {
231                 return status;
232         }
233
234         werr = libnetapi_NetGetAnyDCName(ctx,
235                                          server_name,
236                                          domain_name,
237                                          buffer);
238         if (!W_ERROR_IS_OK(werr)) {
239                 return W_ERROR_V(werr);
240         }
241
242         return 0;
243 }