2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Guenther Deschner 2007-2008
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.
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.
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/>.
21 #include "lib/netapi/netapi.h"
22 #include "lib/netapi/netapi_private.h"
26 extern bool AllowDebugChange;
28 struct libnetapi_ctx *stat_ctx = NULL;
29 TALLOC_CTX *frame = NULL;
30 static bool libnetapi_initialized = false;
32 /****************************************************************
33 ****************************************************************/
35 static NET_API_STATUS libnetapi_init_private_context(struct libnetapi_ctx *ctx)
37 struct libnetapi_private_ctx *priv;
40 return W_ERROR_V(WERR_INVALID_PARAM);
43 priv = TALLOC_ZERO_P(ctx, struct libnetapi_private_ctx);
45 return W_ERROR_V(WERR_NOMEM);
48 ctx->private_data = priv;
50 return NET_API_STATUS_SUCCESS;
53 /****************************************************************
54 ****************************************************************/
56 NET_API_STATUS libnetapi_init(struct libnetapi_ctx **context)
58 NET_API_STATUS status;
59 struct libnetapi_ctx *ctx = NULL;
60 char *krb5_cc_env = NULL;
62 if (stat_ctx && libnetapi_initialized) {
64 return NET_API_STATUS_SUCCESS;
68 talloc_enable_leak_report();
70 frame = talloc_stackframe();
72 ctx = talloc_zero(frame, struct libnetapi_ctx);
75 return W_ERROR_V(WERR_NOMEM);
82 /* prevent setup_logging() from closing x_stderr... */
84 setup_logging("libnetapi", true);
87 x_setbuf(x_stderr, NULL);
88 AllowDebugChange = false;
92 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
94 fprintf(stderr, "error loading %s\n", get_dyn_CONFIGFILE() );
95 return W_ERROR_V(WERR_GENERAL_FAILURE);
98 AllowDebugChange = true;
104 BlockSignals(True, SIGPIPE);
106 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
107 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
108 ctx->krb5_cc_env = talloc_strdup(frame, "MEMORY:libnetapi");
109 setenv(KRB5_ENV_CCNAME, ctx->krb5_cc_env, 1);
112 if (getenv("USER")) {
113 ctx->username = talloc_strdup(frame, getenv("USER"));
115 ctx->username = talloc_strdup(frame, "");
117 if (!ctx->username) {
119 fprintf(stderr, "libnetapi_init: out of memory\n");
120 return W_ERROR_V(WERR_NOMEM);
123 status = libnetapi_init_private_context(ctx);
129 libnetapi_initialized = true;
131 *context = stat_ctx = ctx;
133 return NET_API_STATUS_SUCCESS;
136 /****************************************************************
137 ****************************************************************/
139 NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
143 return NET_API_STATUS_SUCCESS;
146 return libnetapi_init(ctx);
149 /****************************************************************
150 ****************************************************************/
152 NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
155 return NET_API_STATUS_SUCCESS;
158 libnetapi_samr_free(ctx);
160 libnetapi_shutdown_cm(ctx);
162 if (ctx->krb5_cc_env) {
163 char *env = getenv(KRB5_ENV_CCNAME);
164 if (env && (strequal(ctx->krb5_cc_env, env))) {
165 unsetenv(KRB5_ENV_CCNAME);
182 return NET_API_STATUS_SUCCESS;
185 /****************************************************************
186 ****************************************************************/
188 NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
189 const char *debuglevel)
191 AllowDebugChange = true;
192 ctx->debuglevel = talloc_strdup(ctx, debuglevel);
193 if (!debug_parse_levels(debuglevel)) {
194 return W_ERROR_V(WERR_GENERAL_FAILURE);
196 return NET_API_STATUS_SUCCESS;
199 /****************************************************************
200 ****************************************************************/
202 NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
205 *debuglevel = ctx->debuglevel;
206 return NET_API_STATUS_SUCCESS;
209 /****************************************************************
210 ****************************************************************/
212 NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
213 const char *username)
215 TALLOC_FREE(ctx->username);
216 ctx->username = talloc_strdup(ctx, username ? username : "");
218 if (!ctx->username) {
219 return W_ERROR_V(WERR_NOMEM);
221 return NET_API_STATUS_SUCCESS;
224 NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
225 const char *password)
227 TALLOC_FREE(ctx->password);
228 ctx->password = talloc_strdup(ctx, password);
229 if (!ctx->password) {
230 return W_ERROR_V(WERR_NOMEM);
232 return NET_API_STATUS_SUCCESS;
235 NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
236 const char *workgroup)
238 TALLOC_FREE(ctx->workgroup);
239 ctx->workgroup = talloc_strdup(ctx, workgroup);
240 if (!ctx->workgroup) {
241 return W_ERROR_V(WERR_NOMEM);
243 return NET_API_STATUS_SUCCESS;
246 /****************************************************************
247 ****************************************************************/
249 NET_API_STATUS libnetapi_set_use_kerberos(struct libnetapi_ctx *ctx)
251 ctx->use_kerberos = true;
252 return NET_API_STATUS_SUCCESS;
255 NET_API_STATUS libnetapi_set_use_ccache(struct libnetapi_ctx *ctx)
257 ctx->use_ccache = true;
258 return NET_API_STATUS_SUCCESS;
261 /****************************************************************
262 ****************************************************************/
264 const char *libnetapi_errstr(NET_API_STATUS status)
266 if (status & 0xc0000000) {
267 return get_friendly_nt_error_msg(NT_STATUS(status));
270 return get_friendly_werror_msg(W_ERROR(status));
273 /****************************************************************
274 ****************************************************************/
276 NET_API_STATUS libnetapi_set_error_string(struct libnetapi_ctx *ctx,
277 const char *format, ...)
281 TALLOC_FREE(ctx->error_string);
283 va_start(args, format);
284 ctx->error_string = talloc_vasprintf(ctx, format, args);
287 if (!ctx->error_string) {
288 return W_ERROR_V(WERR_NOMEM);
290 return NET_API_STATUS_SUCCESS;
293 /****************************************************************
294 ****************************************************************/
296 const char *libnetapi_get_error_string(struct libnetapi_ctx *ctx,
297 NET_API_STATUS status_in)
299 NET_API_STATUS status;
300 struct libnetapi_ctx *tmp_ctx = ctx;
303 status = libnetapi_getctx(&tmp_ctx);
309 if (tmp_ctx->error_string) {
310 return tmp_ctx->error_string;
313 return libnetapi_errstr(status_in);
316 /****************************************************************
317 ****************************************************************/
319 NET_API_STATUS NetApiBufferAllocate(uint32_t byte_count,
325 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
328 if (byte_count == 0) {
332 buf = talloc_size(NULL, byte_count);
334 return W_ERROR_V(WERR_NOMEM);
340 return NET_API_STATUS_SUCCESS;
343 /****************************************************************
344 ****************************************************************/
346 NET_API_STATUS NetApiBufferFree(void *buffer)
349 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
354 return NET_API_STATUS_SUCCESS;