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