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... */
83 setup_logging("libnetapi", DEBUG_STDERR);
85 AllowDebugChange = false;
89 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
91 fprintf(stderr, "error loading %s\n", get_dyn_CONFIGFILE() );
92 return W_ERROR_V(WERR_GENERAL_FAILURE);
95 AllowDebugChange = true;
101 BlockSignals(True, SIGPIPE);
103 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
104 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
105 ctx->krb5_cc_env = talloc_strdup(frame, "MEMORY:libnetapi");
106 setenv(KRB5_ENV_CCNAME, ctx->krb5_cc_env, 1);
109 if (getenv("USER")) {
110 ctx->username = talloc_strdup(frame, getenv("USER"));
112 ctx->username = talloc_strdup(frame, "");
114 if (!ctx->username) {
116 fprintf(stderr, "libnetapi_init: out of memory\n");
117 return W_ERROR_V(WERR_NOMEM);
120 status = libnetapi_init_private_context(ctx);
126 libnetapi_initialized = true;
128 *context = stat_ctx = ctx;
130 return NET_API_STATUS_SUCCESS;
133 /****************************************************************
134 ****************************************************************/
136 NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
140 return NET_API_STATUS_SUCCESS;
143 return libnetapi_init(ctx);
146 /****************************************************************
147 ****************************************************************/
149 NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
152 return NET_API_STATUS_SUCCESS;
155 libnetapi_samr_free(ctx);
157 libnetapi_shutdown_cm(ctx);
159 if (ctx->krb5_cc_env) {
160 char *env = getenv(KRB5_ENV_CCNAME);
161 if (env && (strequal(ctx->krb5_cc_env, env))) {
162 unsetenv(KRB5_ENV_CCNAME);
179 return NET_API_STATUS_SUCCESS;
182 /****************************************************************
183 ****************************************************************/
185 NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
186 const char *debuglevel)
188 AllowDebugChange = true;
189 ctx->debuglevel = talloc_strdup(ctx, debuglevel);
190 if (!debug_parse_levels(debuglevel)) {
191 return W_ERROR_V(WERR_GENERAL_FAILURE);
193 return NET_API_STATUS_SUCCESS;
196 /****************************************************************
197 ****************************************************************/
199 NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
202 *debuglevel = ctx->debuglevel;
203 return NET_API_STATUS_SUCCESS;
206 /****************************************************************
207 ****************************************************************/
209 NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
210 const char *username)
212 TALLOC_FREE(ctx->username);
213 ctx->username = talloc_strdup(ctx, username ? username : "");
215 if (!ctx->username) {
216 return W_ERROR_V(WERR_NOMEM);
218 return NET_API_STATUS_SUCCESS;
221 NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
222 const char *password)
224 TALLOC_FREE(ctx->password);
225 ctx->password = talloc_strdup(ctx, password);
226 if (!ctx->password) {
227 return W_ERROR_V(WERR_NOMEM);
229 return NET_API_STATUS_SUCCESS;
232 NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
233 const char *workgroup)
235 TALLOC_FREE(ctx->workgroup);
236 ctx->workgroup = talloc_strdup(ctx, workgroup);
237 if (!ctx->workgroup) {
238 return W_ERROR_V(WERR_NOMEM);
240 return NET_API_STATUS_SUCCESS;
243 /****************************************************************
244 ****************************************************************/
246 NET_API_STATUS libnetapi_set_use_kerberos(struct libnetapi_ctx *ctx)
248 ctx->use_kerberos = true;
249 return NET_API_STATUS_SUCCESS;
252 NET_API_STATUS libnetapi_set_use_ccache(struct libnetapi_ctx *ctx)
254 ctx->use_ccache = true;
255 return NET_API_STATUS_SUCCESS;
258 /****************************************************************
259 ****************************************************************/
261 const char *libnetapi_errstr(NET_API_STATUS status)
263 if (status & 0xc0000000) {
264 return get_friendly_nt_error_msg(NT_STATUS(status));
267 return get_friendly_werror_msg(W_ERROR(status));
270 /****************************************************************
271 ****************************************************************/
273 NET_API_STATUS libnetapi_set_error_string(struct libnetapi_ctx *ctx,
274 const char *format, ...)
278 TALLOC_FREE(ctx->error_string);
280 va_start(args, format);
281 ctx->error_string = talloc_vasprintf(ctx, format, args);
284 if (!ctx->error_string) {
285 return W_ERROR_V(WERR_NOMEM);
287 return NET_API_STATUS_SUCCESS;
290 /****************************************************************
291 ****************************************************************/
293 const char *libnetapi_get_error_string(struct libnetapi_ctx *ctx,
294 NET_API_STATUS status_in)
296 NET_API_STATUS status;
297 struct libnetapi_ctx *tmp_ctx = ctx;
300 status = libnetapi_getctx(&tmp_ctx);
306 if (tmp_ctx->error_string) {
307 return tmp_ctx->error_string;
310 return libnetapi_errstr(status_in);
313 /****************************************************************
314 ****************************************************************/
316 NET_API_STATUS NetApiBufferAllocate(uint32_t byte_count,
322 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
325 if (byte_count == 0) {
329 buf = talloc_size(NULL, byte_count);
331 return W_ERROR_V(WERR_NOMEM);
337 return NET_API_STATUS_SUCCESS;
340 /****************************************************************
341 ****************************************************************/
343 NET_API_STATUS NetApiBufferFree(void *buffer)
346 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
351 return NET_API_STATUS_SUCCESS;