Restructure inner workings of libnetapi a bit.
[samba.git] / source / lib / netapi / libnetapi.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  NetApi Support
4  *  Copyright (C) Guenther Deschner 2007-2008
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 #include "librpc/gen_ndr/libnetapi.h"
22 #include "lib/netapi/netapi.h"
23 #include "libnetapi.h"
24 #include "librpc/gen_ndr/ndr_libnetapi.h"
25
26 /****************************************************************
27  NetJoinDomain
28 ****************************************************************/
29
30 NET_API_STATUS NetJoinDomain(const char * server /* [in] [unique] */,
31                              const char * domain /* [in] [ref] */,
32                              const char * account_ou /* [in] [unique] */,
33                              const char * account /* [in] [unique] */,
34                              const char * password /* [in] [unique] */,
35                              uint32_t join_flags /* [in] */)
36 {
37         struct NetJoinDomain r;
38         struct libnetapi_ctx *ctx = NULL;
39         NET_API_STATUS status;
40         WERROR werr;
41
42         status = libnetapi_getctx(&ctx);
43         if (status != 0) {
44                 return status;
45         }
46
47         /* In parameters */
48         r.in.server = server;
49         r.in.domain = domain;
50         r.in.account_ou = account_ou;
51         r.in.account = account;
52         r.in.password = password;
53         r.in.join_flags = join_flags;
54
55         /* Out parameters */
56
57         if (DEBUGLEVEL >= 10) {
58                 NDR_PRINT_IN_DEBUG(NetJoinDomain, &r);
59         }
60
61         if (LIBNETAPI_LOCAL_SERVER(server)) {
62                 werr = NetJoinDomain_l(ctx, &r);
63         } else {
64                 werr = NetJoinDomain_r(ctx, &r);
65         }
66
67         r.out.result = W_ERROR_V(werr);
68
69         if (DEBUGLEVEL >= 10) {
70                 NDR_PRINT_OUT_DEBUG(NetJoinDomain, &r);
71         }
72
73         return r.out.result;
74 }
75
76 /****************************************************************
77  NetUnjoinDomain
78 ****************************************************************/
79
80 NET_API_STATUS NetUnjoinDomain(const char * server_name /* [in] [unique] */,
81                                const char * account /* [in] [unique] */,
82                                const char * password /* [in] [unique] */,
83                                uint32_t unjoin_flags /* [in] */)
84 {
85         struct NetUnjoinDomain r;
86         struct libnetapi_ctx *ctx = NULL;
87         NET_API_STATUS status;
88         WERROR werr;
89
90         status = libnetapi_getctx(&ctx);
91         if (status != 0) {
92                 return status;
93         }
94
95         /* In parameters */
96         r.in.server_name = server_name;
97         r.in.account = account;
98         r.in.password = password;
99         r.in.unjoin_flags = unjoin_flags;
100
101         /* Out parameters */
102
103         if (DEBUGLEVEL >= 10) {
104                 NDR_PRINT_IN_DEBUG(NetUnjoinDomain, &r);
105         }
106
107         if (LIBNETAPI_LOCAL_SERVER(server_name)) {
108                 werr = NetUnjoinDomain_l(ctx, &r);
109         } else {
110                 werr = NetUnjoinDomain_r(ctx, &r);
111         }
112
113         r.out.result = W_ERROR_V(werr);
114
115         if (DEBUGLEVEL >= 10) {
116                 NDR_PRINT_OUT_DEBUG(NetUnjoinDomain, &r);
117         }
118
119         return r.out.result;
120 }
121
122 /****************************************************************
123  NetGetJoinInformation
124 ****************************************************************/
125
126 NET_API_STATUS NetGetJoinInformation(const char * server_name /* [in] [unique] */,
127                                      const char * *name_buffer /* [out] [ref] */,
128                                      uint16_t *name_type /* [out] [ref] */)
129 {
130         struct NetGetJoinInformation r;
131         struct libnetapi_ctx *ctx = NULL;
132         NET_API_STATUS status;
133         WERROR werr;
134
135         status = libnetapi_getctx(&ctx);
136         if (status != 0) {
137                 return status;
138         }
139
140         /* In parameters */
141         r.in.server_name = server_name;
142
143         /* Out parameters */
144         r.out.name_buffer = name_buffer;
145         r.out.name_type = name_type;
146
147         if (DEBUGLEVEL >= 10) {
148                 NDR_PRINT_IN_DEBUG(NetGetJoinInformation, &r);
149         }
150
151         if (LIBNETAPI_LOCAL_SERVER(server_name)) {
152                 werr = NetGetJoinInformation_l(ctx, &r);
153         } else {
154                 werr = NetGetJoinInformation_r(ctx, &r);
155         }
156
157         r.out.result = W_ERROR_V(werr);
158
159         if (DEBUGLEVEL >= 10) {
160                 NDR_PRINT_OUT_DEBUG(NetGetJoinInformation, &r);
161         }
162
163         return r.out.result;
164 }
165
166 /****************************************************************
167  NetGetJoinableOUs
168 ****************************************************************/
169
170 NET_API_STATUS NetGetJoinableOUs(const char * server_name /* [in] [unique] */,
171                                  const char * domain /* [in] [ref] */,
172                                  const char * account /* [in] [unique] */,
173                                  const char * password /* [in] [unique] */,
174                                  uint32_t *ou_count /* [out] [ref] */,
175                                  const char * **ous /* [out] [ref] */)
176 {
177         struct NetGetJoinableOUs r;
178         struct libnetapi_ctx *ctx = NULL;
179         NET_API_STATUS status;
180         WERROR werr;
181
182         status = libnetapi_getctx(&ctx);
183         if (status != 0) {
184                 return status;
185         }
186
187         /* In parameters */
188         r.in.server_name = server_name;
189         r.in.domain = domain;
190         r.in.account = account;
191         r.in.password = password;
192
193         /* Out parameters */
194         r.out.ou_count = ou_count;
195         r.out.ous = ous;
196
197         if (DEBUGLEVEL >= 10) {
198                 NDR_PRINT_IN_DEBUG(NetGetJoinableOUs, &r);
199         }
200
201         if (LIBNETAPI_LOCAL_SERVER(server_name)) {
202                 werr = NetGetJoinableOUs_l(ctx, &r);
203         } else {
204                 werr = NetGetJoinableOUs_r(ctx, &r);
205         }
206
207         r.out.result = W_ERROR_V(werr);
208
209         if (DEBUGLEVEL >= 10) {
210                 NDR_PRINT_OUT_DEBUG(NetGetJoinableOUs, &r);
211         }
212
213         return r.out.result;
214 }
215
216 /****************************************************************
217  NetServerGetInfo
218 ****************************************************************/
219
220 NET_API_STATUS NetServerGetInfo(const char * server_name /* [in] [unique] */,
221                                 uint32_t level /* [in] */,
222                                 uint8_t **buffer /* [out] [ref] */)
223 {
224         struct NetServerGetInfo r;
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         /* In parameters */
235         r.in.server_name = server_name;
236         r.in.level = level;
237
238         /* Out parameters */
239         r.out.buffer = buffer;
240
241         if (DEBUGLEVEL >= 10) {
242                 NDR_PRINT_IN_DEBUG(NetServerGetInfo, &r);
243         }
244
245         if (LIBNETAPI_LOCAL_SERVER(server_name)) {
246                 werr = NetServerGetInfo_l(ctx, &r);
247         } else {
248                 werr = NetServerGetInfo_r(ctx, &r);
249         }
250
251         r.out.result = W_ERROR_V(werr);
252
253         if (DEBUGLEVEL >= 10) {
254                 NDR_PRINT_OUT_DEBUG(NetServerGetInfo, &r);
255         }
256
257         return r.out.result;
258 }
259
260 /****************************************************************
261  NetServerSetInfo
262 ****************************************************************/
263
264 NET_API_STATUS NetServerSetInfo(const char * server_name /* [in] [unique] */,
265                                 uint32_t level /* [in] */,
266                                 uint8_t *buffer /* [in] [ref] */,
267                                 uint32_t *parm_error /* [out] [ref] */)
268 {
269         struct NetServerSetInfo r;
270         struct libnetapi_ctx *ctx = NULL;
271         NET_API_STATUS status;
272         WERROR werr;
273
274         status = libnetapi_getctx(&ctx);
275         if (status != 0) {
276                 return status;
277         }
278
279         /* In parameters */
280         r.in.server_name = server_name;
281         r.in.level = level;
282         r.in.buffer = buffer;
283
284         /* Out parameters */
285         r.out.parm_error = parm_error;
286
287         if (DEBUGLEVEL >= 10) {
288                 NDR_PRINT_IN_DEBUG(NetServerSetInfo, &r);
289         }
290
291         if (LIBNETAPI_LOCAL_SERVER(server_name)) {
292                 werr = NetServerSetInfo_l(ctx, &r);
293         } else {
294                 werr = NetServerSetInfo_r(ctx, &r);
295         }
296
297         r.out.result = W_ERROR_V(werr);
298
299         if (DEBUGLEVEL >= 10) {
300                 NDR_PRINT_OUT_DEBUG(NetServerSetInfo, &r);
301         }
302
303         return r.out.result;
304 }
305
306 /****************************************************************
307  NetGetDCName
308 ****************************************************************/
309
310 NET_API_STATUS NetGetDCName(const char * server_name /* [in] [unique] */,
311                             const char * domain_name /* [in] [unique] */,
312                             uint8_t **buffer /* [out] [ref] */)
313 {
314         struct NetGetDCName r;
315         struct libnetapi_ctx *ctx = NULL;
316         NET_API_STATUS status;
317         WERROR werr;
318
319         status = libnetapi_getctx(&ctx);
320         if (status != 0) {
321                 return status;
322         }
323
324         /* In parameters */
325         r.in.server_name = server_name;
326         r.in.domain_name = domain_name;
327
328         /* Out parameters */
329         r.out.buffer = buffer;
330
331         if (DEBUGLEVEL >= 10) {
332                 NDR_PRINT_IN_DEBUG(NetGetDCName, &r);
333         }
334
335         if (LIBNETAPI_LOCAL_SERVER(server_name)) {
336                 werr = NetGetDCName_l(ctx, &r);
337         } else {
338                 werr = NetGetDCName_r(ctx, &r);
339         }
340
341         r.out.result = W_ERROR_V(werr);
342
343         if (DEBUGLEVEL >= 10) {
344                 NDR_PRINT_OUT_DEBUG(NetGetDCName, &r);
345         }
346
347         return r.out.result;
348 }
349
350 /****************************************************************
351  NetGetAnyDCName
352 ****************************************************************/
353
354 NET_API_STATUS NetGetAnyDCName(const char * server_name /* [in] [unique] */,
355                                const char * domain_name /* [in] [unique] */,
356                                uint8_t **buffer /* [out] [ref] */)
357 {
358         struct NetGetAnyDCName r;
359         struct libnetapi_ctx *ctx = NULL;
360         NET_API_STATUS status;
361         WERROR werr;
362
363         status = libnetapi_getctx(&ctx);
364         if (status != 0) {
365                 return status;
366         }
367
368         /* In parameters */
369         r.in.server_name = server_name;
370         r.in.domain_name = domain_name;
371
372         /* Out parameters */
373         r.out.buffer = buffer;
374
375         if (DEBUGLEVEL >= 10) {
376                 NDR_PRINT_IN_DEBUG(NetGetAnyDCName, &r);
377         }
378
379         if (LIBNETAPI_LOCAL_SERVER(server_name)) {
380                 werr = NetGetAnyDCName_l(ctx, &r);
381         } else {
382                 werr = NetGetAnyDCName_r(ctx, &r);
383         }
384
385         r.out.result = W_ERROR_V(werr);
386
387         if (DEBUGLEVEL >= 10) {
388                 NDR_PRINT_OUT_DEBUG(NetGetAnyDCName, &r);
389         }
390
391         return r.out.result;
392 }
393