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"
23 extern bool AllowDebugChange;
25 struct libnetapi_ctx *stat_ctx = NULL;
26 TALLOC_CTX *frame = NULL;
27 static bool libnetapi_initialized = false;
29 /****************************************************************
30 ****************************************************************/
32 NET_API_STATUS libnetapi_init(struct libnetapi_ctx **context)
34 struct libnetapi_ctx *ctx = NULL;
35 char *krb5_cc_env = NULL;
37 if (stat_ctx && libnetapi_initialized) {
39 return NET_API_STATUS_SUCCESS;
43 talloc_enable_leak_report();
45 frame = talloc_stackframe();
47 ctx = talloc_zero(frame, struct libnetapi_ctx);
50 return W_ERROR_V(WERR_NOMEM);
54 setup_logging("libnetapi", true);
57 x_setbuf(x_stderr, NULL);
58 AllowDebugChange = false;
62 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
64 return W_ERROR_V(WERR_GENERAL_FAILURE);
67 AllowDebugChange = true;
73 BlockSignals(True, SIGPIPE);
75 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
76 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
77 ctx->krb5_cc_env = talloc_strdup(frame, "MEMORY:libnetapi");
78 setenv(KRB5_ENV_CCNAME, ctx->krb5_cc_env, 1);
81 libnetapi_initialized = true;
83 *context = stat_ctx = ctx;
85 return NET_API_STATUS_SUCCESS;
88 /****************************************************************
89 ****************************************************************/
91 NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
95 return NET_API_STATUS_SUCCESS;
98 return libnetapi_init(ctx);
101 /****************************************************************
102 ****************************************************************/
104 NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
116 if (ctx->krb5_cc_env &&
117 (strequal(ctx->krb5_cc_env, getenv(KRB5_ENV_CCNAME)))) {
118 unsetenv(KRB5_ENV_CCNAME);
126 return NET_API_STATUS_SUCCESS;
129 /****************************************************************
130 ****************************************************************/
132 NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
133 const char *debuglevel)
135 AllowDebugChange = true;
136 ctx->debuglevel = talloc_strdup(ctx, debuglevel);
137 if (!debug_parse_levels(debuglevel)) {
138 return W_ERROR_V(WERR_GENERAL_FAILURE);
140 return NET_API_STATUS_SUCCESS;
143 /****************************************************************
144 ****************************************************************/
146 NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
149 *debuglevel = ctx->debuglevel;
150 return NET_API_STATUS_SUCCESS;
153 /****************************************************************
154 ****************************************************************/
156 NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
157 const char *username)
159 TALLOC_FREE(ctx->username);
160 ctx->username = talloc_strdup(ctx, username);
161 if (!ctx->username) {
162 return W_ERROR_V(WERR_NOMEM);
164 return NET_API_STATUS_SUCCESS;
167 NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
168 const char *password)
170 TALLOC_FREE(ctx->password);
171 ctx->password = talloc_strdup(ctx, password);
172 if (!ctx->password) {
173 return W_ERROR_V(WERR_NOMEM);
175 return NET_API_STATUS_SUCCESS;
178 NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
179 const char *workgroup)
181 TALLOC_FREE(ctx->workgroup);
182 ctx->workgroup = talloc_strdup(ctx, workgroup);
183 if (!ctx->workgroup) {
184 return W_ERROR_V(WERR_NOMEM);
186 return NET_API_STATUS_SUCCESS;
189 /****************************************************************
190 ****************************************************************/
192 const char *libnetapi_errstr(struct libnetapi_ctx *ctx,
193 NET_API_STATUS status)
195 if (status & 0xc0000000) {
196 return get_friendly_nt_error_msg(NT_STATUS(status));
199 return get_friendly_werror_msg(W_ERROR(status));
202 /****************************************************************
203 ****************************************************************/
205 NET_API_STATUS libnetapi_set_error_string(struct libnetapi_ctx *ctx,
206 const char *error_string)
208 TALLOC_FREE(ctx->error_string);
209 ctx->error_string = talloc_strdup(ctx, error_string);
210 if (!ctx->error_string) {
211 return W_ERROR_V(WERR_NOMEM);
213 return NET_API_STATUS_SUCCESS;
217 /****************************************************************
218 ****************************************************************/
220 const char *libnetapi_get_error_string(struct libnetapi_ctx *ctx)
222 return ctx->error_string;