s3-secrets: only include secrets.h when needed.
[kai/samba.git] / source3 / lib / netapi / netapi.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 "lib/netapi/netapi.h"
22 #include "lib/netapi/netapi_private.h"
23 #include "secrets.h"
24
25 extern bool AllowDebugChange;
26
27 struct libnetapi_ctx *stat_ctx = NULL;
28 TALLOC_CTX *frame = NULL;
29 static bool libnetapi_initialized = false;
30
31 /****************************************************************
32 ****************************************************************/
33
34 static NET_API_STATUS libnetapi_init_private_context(struct libnetapi_ctx *ctx)
35 {
36         struct libnetapi_private_ctx *priv;
37
38         if (!ctx) {
39                 return W_ERROR_V(WERR_INVALID_PARAM);
40         }
41
42         priv = TALLOC_ZERO_P(ctx, struct libnetapi_private_ctx);
43         if (!priv) {
44                 return W_ERROR_V(WERR_NOMEM);
45         }
46
47         ctx->private_data = priv;
48
49         return NET_API_STATUS_SUCCESS;
50 }
51
52 /****************************************************************
53 ****************************************************************/
54
55 NET_API_STATUS libnetapi_init(struct libnetapi_ctx **context)
56 {
57         NET_API_STATUS status;
58         struct libnetapi_ctx *ctx = NULL;
59         char *krb5_cc_env = NULL;
60
61         if (stat_ctx && libnetapi_initialized) {
62                 *context = stat_ctx;
63                 return NET_API_STATUS_SUCCESS;
64         }
65
66 #if 0
67         talloc_enable_leak_report();
68 #endif
69         frame = talloc_stackframe();
70
71         ctx = talloc_zero(frame, struct libnetapi_ctx);
72         if (!ctx) {
73                 TALLOC_FREE(frame);
74                 return W_ERROR_V(WERR_NOMEM);
75         }
76
77         if (!DEBUGLEVEL) {
78                 DEBUGLEVEL = 0;
79         }
80
81         /* prevent setup_logging() from closing x_stderr... */
82         dbf = 0;
83         setup_logging("libnetapi", true);
84
85         dbf = x_stderr;
86         x_setbuf(x_stderr, NULL);
87         AllowDebugChange = false;
88
89         load_case_tables();
90
91         if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
92                 TALLOC_FREE(frame);
93                 fprintf(stderr, "error loading %s\n", get_dyn_CONFIGFILE() );
94                 return W_ERROR_V(WERR_GENERAL_FAILURE);
95         }
96
97         AllowDebugChange = true;
98
99         init_names();
100         load_interfaces();
101         reopen_logs();
102
103         BlockSignals(True, SIGPIPE);
104
105         krb5_cc_env = getenv(KRB5_ENV_CCNAME);
106         if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
107                 ctx->krb5_cc_env = talloc_strdup(frame, "MEMORY:libnetapi");
108                 setenv(KRB5_ENV_CCNAME, ctx->krb5_cc_env, 1);
109         }
110
111         if (getenv("USER")) {
112                 ctx->username = talloc_strdup(frame, getenv("USER"));
113         } else {
114                 ctx->username = talloc_strdup(frame, "");
115         }
116         if (!ctx->username) {
117                 TALLOC_FREE(frame);
118                 fprintf(stderr, "libnetapi_init: out of memory\n");
119                 return W_ERROR_V(WERR_NOMEM);
120         }
121
122         status = libnetapi_init_private_context(ctx);
123         if (status != 0) {
124                 TALLOC_FREE(frame);
125                 return status;
126         }
127
128         libnetapi_initialized = true;
129
130         *context = stat_ctx = ctx;
131
132         return NET_API_STATUS_SUCCESS;
133 }
134
135 /****************************************************************
136 ****************************************************************/
137
138 NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
139 {
140         if (stat_ctx) {
141                 *ctx = stat_ctx;
142                 return NET_API_STATUS_SUCCESS;
143         }
144
145         return libnetapi_init(ctx);
146 }
147
148 /****************************************************************
149 ****************************************************************/
150
151 NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
152 {
153         if (!ctx) {
154                 return NET_API_STATUS_SUCCESS;
155         }
156
157         libnetapi_samr_free(ctx);
158
159         libnetapi_shutdown_cm(ctx);
160
161         if (ctx->krb5_cc_env) {
162                 char *env = getenv(KRB5_ENV_CCNAME);
163                 if (env && (strequal(ctx->krb5_cc_env, env))) {
164                         unsetenv(KRB5_ENV_CCNAME);
165                 }
166         }
167
168         gfree_names();
169         gfree_loadparm();
170         gfree_case_tables();
171         gfree_charcnv();
172         gfree_interfaces();
173
174         secrets_shutdown();
175
176         TALLOC_FREE(ctx);
177         TALLOC_FREE(frame);
178
179         gfree_debugsyms();
180
181         return NET_API_STATUS_SUCCESS;
182 }
183
184 /****************************************************************
185 ****************************************************************/
186
187 NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
188                                         const char *debuglevel)
189 {
190         AllowDebugChange = true;
191         ctx->debuglevel = talloc_strdup(ctx, debuglevel);
192         if (!debug_parse_levels(debuglevel)) {
193                 return W_ERROR_V(WERR_GENERAL_FAILURE);
194         }
195         return NET_API_STATUS_SUCCESS;
196 }
197
198 /****************************************************************
199 ****************************************************************/
200
201 NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
202                                         char **debuglevel)
203 {
204         *debuglevel = ctx->debuglevel;
205         return NET_API_STATUS_SUCCESS;
206 }
207
208 /****************************************************************
209 ****************************************************************/
210
211 NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
212                                       const char *username)
213 {
214         TALLOC_FREE(ctx->username);
215         ctx->username = talloc_strdup(ctx, username ? username : "");
216
217         if (!ctx->username) {
218                 return W_ERROR_V(WERR_NOMEM);
219         }
220         return NET_API_STATUS_SUCCESS;
221 }
222
223 NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
224                                       const char *password)
225 {
226         TALLOC_FREE(ctx->password);
227         ctx->password = talloc_strdup(ctx, password);
228         if (!ctx->password) {
229                 return W_ERROR_V(WERR_NOMEM);
230         }
231         return NET_API_STATUS_SUCCESS;
232 }
233
234 NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
235                                        const char *workgroup)
236 {
237         TALLOC_FREE(ctx->workgroup);
238         ctx->workgroup = talloc_strdup(ctx, workgroup);
239         if (!ctx->workgroup) {
240                 return W_ERROR_V(WERR_NOMEM);
241         }
242         return NET_API_STATUS_SUCCESS;
243 }
244
245 /****************************************************************
246 ****************************************************************/
247
248 NET_API_STATUS libnetapi_set_use_kerberos(struct libnetapi_ctx *ctx)
249 {
250         ctx->use_kerberos = true;
251         return NET_API_STATUS_SUCCESS;
252 }
253
254 NET_API_STATUS libnetapi_set_use_ccache(struct libnetapi_ctx *ctx)
255 {
256         ctx->use_ccache = true;
257         return NET_API_STATUS_SUCCESS;
258 }
259
260 /****************************************************************
261 ****************************************************************/
262
263 const char *libnetapi_errstr(NET_API_STATUS status)
264 {
265         if (status & 0xc0000000) {
266                 return get_friendly_nt_error_msg(NT_STATUS(status));
267         }
268
269         return get_friendly_werror_msg(W_ERROR(status));
270 }
271
272 /****************************************************************
273 ****************************************************************/
274
275 NET_API_STATUS libnetapi_set_error_string(struct libnetapi_ctx *ctx,
276                                           const char *format, ...)
277 {
278         va_list args;
279
280         TALLOC_FREE(ctx->error_string);
281
282         va_start(args, format);
283         ctx->error_string = talloc_vasprintf(ctx, format, args);
284         va_end(args);
285
286         if (!ctx->error_string) {
287                 return W_ERROR_V(WERR_NOMEM);
288         }
289         return NET_API_STATUS_SUCCESS;
290 }
291
292 /****************************************************************
293 ****************************************************************/
294
295 const char *libnetapi_get_error_string(struct libnetapi_ctx *ctx,
296                                        NET_API_STATUS status_in)
297 {
298         NET_API_STATUS status;
299         struct libnetapi_ctx *tmp_ctx = ctx;
300
301         if (!tmp_ctx) {
302                 status = libnetapi_getctx(&tmp_ctx);
303                 if (status != 0) {
304                         return NULL;
305                 }
306         }
307
308         if (tmp_ctx->error_string) {
309                 return tmp_ctx->error_string;
310         }
311
312         return libnetapi_errstr(status_in);
313 }
314
315 /****************************************************************
316 ****************************************************************/
317
318 NET_API_STATUS NetApiBufferAllocate(uint32_t byte_count,
319                                     void **buffer)
320 {
321         void *buf = NULL;
322
323         if (!buffer) {
324                 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
325         }
326
327         if (byte_count == 0) {
328                 goto done;
329         }
330
331         buf = talloc_size(NULL, byte_count);
332         if (!buf) {
333                 return W_ERROR_V(WERR_NOMEM);
334         }
335
336  done:
337         *buffer = buf;
338
339         return NET_API_STATUS_SUCCESS;
340 }
341
342 /****************************************************************
343 ****************************************************************/
344
345 NET_API_STATUS NetApiBufferFree(void *buffer)
346 {
347         if (!buffer) {
348                 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
349         }
350
351         talloc_free(buffer);
352
353         return NET_API_STATUS_SUCCESS;
354 }