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"
25 extern bool AllowDebugChange;
27 struct libnetapi_ctx *stat_ctx = NULL;
28 TALLOC_CTX *frame = NULL;
29 static bool libnetapi_initialized = false;
31 /****************************************************************
32 ****************************************************************/
34 static NET_API_STATUS libnetapi_init_private_context(struct libnetapi_ctx *ctx)
36 struct libnetapi_private_ctx *priv;
39 return W_ERROR_V(WERR_INVALID_PARAM);
42 priv = TALLOC_ZERO_P(ctx, struct libnetapi_private_ctx);
44 return W_ERROR_V(WERR_NOMEM);
47 ctx->private_data = priv;
49 return NET_API_STATUS_SUCCESS;
52 /****************************************************************
53 ****************************************************************/
55 NET_API_STATUS libnetapi_init(struct libnetapi_ctx **context)
57 NET_API_STATUS status;
58 struct libnetapi_ctx *ctx = NULL;
59 char *krb5_cc_env = NULL;
61 if (stat_ctx && libnetapi_initialized) {
63 return NET_API_STATUS_SUCCESS;
67 talloc_enable_leak_report();
69 frame = talloc_stackframe();
71 ctx = talloc_zero(frame, struct libnetapi_ctx);
74 return W_ERROR_V(WERR_NOMEM);
81 /* prevent setup_logging() from closing x_stderr... */
83 setup_logging("libnetapi", true);
86 x_setbuf(x_stderr, NULL);
87 AllowDebugChange = false;
91 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
93 fprintf(stderr, "error loading %s\n", get_dyn_CONFIGFILE() );
94 return W_ERROR_V(WERR_GENERAL_FAILURE);
97 AllowDebugChange = true;
103 BlockSignals(True, SIGPIPE);
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);
111 if (getenv("USER")) {
112 ctx->username = talloc_strdup(frame, getenv("USER"));
114 ctx->username = talloc_strdup(frame, "");
116 if (!ctx->username) {
118 fprintf(stderr, "libnetapi_init: out of memory\n");
119 return W_ERROR_V(WERR_NOMEM);
122 status = libnetapi_init_private_context(ctx);
128 libnetapi_initialized = true;
130 *context = stat_ctx = ctx;
132 return NET_API_STATUS_SUCCESS;
135 /****************************************************************
136 ****************************************************************/
138 NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
142 return NET_API_STATUS_SUCCESS;
145 return libnetapi_init(ctx);
148 /****************************************************************
149 ****************************************************************/
151 NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
154 return NET_API_STATUS_SUCCESS;
157 libnetapi_samr_free(ctx);
159 libnetapi_shutdown_cm(ctx);
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);
181 return NET_API_STATUS_SUCCESS;
184 /****************************************************************
185 ****************************************************************/
187 NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
188 const char *debuglevel)
190 AllowDebugChange = true;
191 ctx->debuglevel = talloc_strdup(ctx, debuglevel);
192 if (!debug_parse_levels(debuglevel)) {
193 return W_ERROR_V(WERR_GENERAL_FAILURE);
195 return NET_API_STATUS_SUCCESS;
198 /****************************************************************
199 ****************************************************************/
201 NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
204 *debuglevel = ctx->debuglevel;
205 return NET_API_STATUS_SUCCESS;
208 /****************************************************************
209 ****************************************************************/
211 NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
212 const char *username)
214 TALLOC_FREE(ctx->username);
215 ctx->username = talloc_strdup(ctx, username ? username : "");
217 if (!ctx->username) {
218 return W_ERROR_V(WERR_NOMEM);
220 return NET_API_STATUS_SUCCESS;
223 NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
224 const char *password)
226 TALLOC_FREE(ctx->password);
227 ctx->password = talloc_strdup(ctx, password);
228 if (!ctx->password) {
229 return W_ERROR_V(WERR_NOMEM);
231 return NET_API_STATUS_SUCCESS;
234 NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
235 const char *workgroup)
237 TALLOC_FREE(ctx->workgroup);
238 ctx->workgroup = talloc_strdup(ctx, workgroup);
239 if (!ctx->workgroup) {
240 return W_ERROR_V(WERR_NOMEM);
242 return NET_API_STATUS_SUCCESS;
245 /****************************************************************
246 ****************************************************************/
248 NET_API_STATUS libnetapi_set_use_kerberos(struct libnetapi_ctx *ctx)
250 ctx->use_kerberos = true;
251 return NET_API_STATUS_SUCCESS;
254 NET_API_STATUS libnetapi_set_use_ccache(struct libnetapi_ctx *ctx)
256 ctx->use_ccache = true;
257 return NET_API_STATUS_SUCCESS;
260 /****************************************************************
261 ****************************************************************/
263 const char *libnetapi_errstr(NET_API_STATUS status)
265 if (status & 0xc0000000) {
266 return get_friendly_nt_error_msg(NT_STATUS(status));
269 return get_friendly_werror_msg(W_ERROR(status));
272 /****************************************************************
273 ****************************************************************/
275 NET_API_STATUS libnetapi_set_error_string(struct libnetapi_ctx *ctx,
276 const char *format, ...)
280 TALLOC_FREE(ctx->error_string);
282 va_start(args, format);
283 ctx->error_string = talloc_vasprintf(ctx, format, args);
286 if (!ctx->error_string) {
287 return W_ERROR_V(WERR_NOMEM);
289 return NET_API_STATUS_SUCCESS;
292 /****************************************************************
293 ****************************************************************/
295 const char *libnetapi_get_error_string(struct libnetapi_ctx *ctx,
296 NET_API_STATUS status_in)
298 NET_API_STATUS status;
299 struct libnetapi_ctx *tmp_ctx = ctx;
302 status = libnetapi_getctx(&tmp_ctx);
308 if (tmp_ctx->error_string) {
309 return tmp_ctx->error_string;
312 return libnetapi_errstr(status_in);
315 /****************************************************************
316 ****************************************************************/
318 NET_API_STATUS NetApiBufferAllocate(uint32_t byte_count,
324 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
327 if (byte_count == 0) {
331 buf = talloc_size(NULL, byte_count);
333 return W_ERROR_V(WERR_NOMEM);
339 return NET_API_STATUS_SUCCESS;
342 /****************************************************************
343 ****************************************************************/
345 NET_API_STATUS NetApiBufferFree(void *buffer)
348 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
353 return NET_API_STATUS_SUCCESS;