dsgetdcname: use existing messaging_context if possible.
[jra/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         return WERR_NOT_SUPPORTED;
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, PI_NETLOGON, &pipe_cli);
54         if (!W_ERROR_IS_OK(werr)) {
55                 goto done;
56         }
57
58         status = rpccli_netr_GetDcName(pipe_cli, ctx,
59                                        r->in.server_name,
60                                        r->in.domain_name,
61                                        (const char **)r->out.buffer,
62                                        &werr);
63  done:
64
65         return werr;
66 }
67
68 /********************************************************************
69 ********************************************************************/
70
71 WERROR NetGetAnyDCName_l(struct libnetapi_ctx *ctx,
72                          struct NetGetAnyDCName *r)
73 {
74         return WERR_NOT_SUPPORTED;
75 }
76
77 /********************************************************************
78 ********************************************************************/
79
80 WERROR NetGetAnyDCName_r(struct libnetapi_ctx *ctx,
81                          struct NetGetAnyDCName *r)
82 {
83         struct cli_state *cli = NULL;
84         struct rpc_pipe_client *pipe_cli = NULL;
85         NTSTATUS status;
86         WERROR werr;
87
88         werr = libnetapi_open_ipc_connection(ctx, r->in.server_name, &cli);
89         if (!W_ERROR_IS_OK(werr)) {
90                 goto done;
91         }
92
93         werr = libnetapi_open_pipe(ctx, cli, PI_NETLOGON, &pipe_cli);
94         if (!W_ERROR_IS_OK(werr)) {
95                 goto done;
96         }
97
98         status = rpccli_netr_GetAnyDCName(pipe_cli, ctx,
99                                           r->in.server_name,
100                                           r->in.domain_name,
101                                           (const char **)r->out.buffer,
102                                           &werr);
103         if (!NT_STATUS_IS_OK(status)) {
104                 goto done;
105         }
106  done:
107
108         return werr;
109
110 }
111
112 /********************************************************************
113 ********************************************************************/
114
115 WERROR DsGetDcName_l(struct libnetapi_ctx *ctx,
116                      struct DsGetDcName *r)
117 {
118         NTSTATUS status;
119
120         status = dsgetdcname(ctx,
121                              NULL,
122                              r->in.domain_name,
123                              r->in.domain_guid,
124                              r->in.site_name,
125                              r->in.flags,
126                              (struct netr_DsRGetDCNameInfo **)r->out.dc_info);
127         if (!NT_STATUS_IS_OK(status)) {
128                 libnetapi_set_error_string(ctx,
129                         "failed to find DC: %s",
130                         get_friendly_nt_error_msg(status));
131         }
132
133         return ntstatus_to_werror(status);
134 }
135
136 /********************************************************************
137 ********************************************************************/
138
139 WERROR DsGetDcName_r(struct libnetapi_ctx *ctx,
140                      struct DsGetDcName *r)
141 {
142         WERROR werr;
143         NTSTATUS status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
144         struct cli_state *cli = NULL;
145         struct rpc_pipe_client *pipe_cli = NULL;
146
147         werr = libnetapi_open_ipc_connection(ctx, r->in.server_name, &cli);
148         if (!W_ERROR_IS_OK(werr)) {
149                 goto done;
150         }
151
152         werr = libnetapi_open_pipe(ctx, cli, PI_NETLOGON, &pipe_cli);
153         if (!W_ERROR_IS_OK(werr)) {
154                 goto done;
155         }
156
157         status = rpccli_netr_DsRGetDCName(pipe_cli,
158                                           ctx,
159                                           r->in.server_name,
160                                           r->in.domain_name,
161                                           r->in.domain_guid,
162                                           NULL,
163                                           r->in.flags,
164                                           (struct netr_DsRGetDCNameInfo **)r->out.dc_info,
165                                           &werr);
166         if (!NT_STATUS_IS_OK(status)) {
167                 werr = ntstatus_to_werror(status);
168                 goto done;
169         }
170
171  done:
172         return werr;
173 }