82fa35d4a1b5a3a7cc81a9c171871977a17ab47c
[kai/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 "librpc/gen_ndr/libnetapi.h"
23 #include "lib/netapi/netapi.h"
24 #include "lib/netapi/netapi_private.h"
25 #include "lib/netapi/libnetapi.h"
26 #include "libnet/libnet.h"
27
28 /********************************************************************
29 ********************************************************************/
30
31 WERROR NetGetDCName_l(struct libnetapi_ctx *ctx,
32                       struct NetGetDCName *r)
33 {
34         LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetGetDCName);
35 }
36
37 /********************************************************************
38 ********************************************************************/
39
40 WERROR NetGetDCName_r(struct libnetapi_ctx *ctx,
41                       struct NetGetDCName *r)
42 {
43         struct cli_state *cli = NULL;
44         struct rpc_pipe_client *pipe_cli = NULL;
45         NTSTATUS status;
46         WERROR werr;
47
48         werr = libnetapi_open_ipc_connection(ctx, r->in.server_name, &cli);
49         if (!W_ERROR_IS_OK(werr)) {
50                 goto done;
51         }
52
53         werr = libnetapi_open_pipe(ctx, cli, &ndr_table_netlogon.syntax_id,
54                                    &pipe_cli);
55         if (!W_ERROR_IS_OK(werr)) {
56                 goto done;
57         }
58
59         status = rpccli_netr_GetDcName(pipe_cli, ctx,
60                                        r->in.server_name,
61                                        r->in.domain_name,
62                                        (const char **)r->out.buffer,
63                                        &werr);
64  done:
65
66         return werr;
67 }
68
69 /********************************************************************
70 ********************************************************************/
71
72 WERROR NetGetAnyDCName_l(struct libnetapi_ctx *ctx,
73                          struct NetGetAnyDCName *r)
74 {
75         LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetGetAnyDCName);
76 }
77
78 /********************************************************************
79 ********************************************************************/
80
81 WERROR NetGetAnyDCName_r(struct libnetapi_ctx *ctx,
82                          struct NetGetAnyDCName *r)
83 {
84         struct cli_state *cli = NULL;
85         struct rpc_pipe_client *pipe_cli = NULL;
86         NTSTATUS status;
87         WERROR werr;
88
89         werr = libnetapi_open_ipc_connection(ctx, r->in.server_name, &cli);
90         if (!W_ERROR_IS_OK(werr)) {
91                 goto done;
92         }
93
94         werr = libnetapi_open_pipe(ctx, cli, &ndr_table_netlogon.syntax_id,
95                                    &pipe_cli);
96         if (!W_ERROR_IS_OK(werr)) {
97                 goto done;
98         }
99
100         status = rpccli_netr_GetAnyDCName(pipe_cli, ctx,
101                                           r->in.server_name,
102                                           r->in.domain_name,
103                                           (const char **)r->out.buffer,
104                                           &werr);
105         if (!NT_STATUS_IS_OK(status)) {
106                 goto done;
107         }
108  done:
109
110         return werr;
111
112 }
113
114 /********************************************************************
115 ********************************************************************/
116
117 WERROR DsGetDcName_l(struct libnetapi_ctx *ctx,
118                      struct DsGetDcName *r)
119 {
120         NTSTATUS status;
121
122         status = dsgetdcname(ctx,
123                              NULL,
124                              r->in.domain_name,
125                              r->in.domain_guid,
126                              r->in.site_name,
127                              r->in.flags,
128                              (struct netr_DsRGetDCNameInfo **)r->out.dc_info);
129         if (!NT_STATUS_IS_OK(status)) {
130                 libnetapi_set_error_string(ctx,
131                         "failed to find DC: %s",
132                         get_friendly_nt_error_msg(status));
133         }
134
135         return ntstatus_to_werror(status);
136 }
137
138 /********************************************************************
139 ********************************************************************/
140
141 WERROR DsGetDcName_r(struct libnetapi_ctx *ctx,
142                      struct DsGetDcName *r)
143 {
144         WERROR werr;
145         NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
146         struct cli_state *cli = NULL;
147         struct rpc_pipe_client *pipe_cli = NULL;
148
149         werr = libnetapi_open_ipc_connection(ctx, r->in.server_name, &cli);
150         if (!W_ERROR_IS_OK(werr)) {
151                 goto done;
152         }
153
154         werr = libnetapi_open_pipe(ctx, cli, &ndr_table_netlogon.syntax_id,
155                                    &pipe_cli);
156         if (!W_ERROR_IS_OK(werr)) {
157                 goto done;
158         }
159
160         status = rpccli_netr_DsRGetDCName(pipe_cli,
161                                           ctx,
162                                           r->in.server_name,
163                                           r->in.domain_name,
164                                           r->in.domain_guid,
165                                           NULL,
166                                           r->in.flags,
167                                           (struct netr_DsRGetDCNameInfo **)r->out.dc_info,
168                                           &werr);
169         if (!NT_STATUS_IS_OK(status)) {
170                 werr = ntstatus_to_werror(status);
171                 goto done;
172         }
173
174  done:
175         return werr;
176 }