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);
56 setup_logging("libnetapi", true);
59 x_setbuf(x_stderr, NULL);
60 AllowDebugChange = false;
64 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
66 return W_ERROR_V(WERR_GENERAL_FAILURE);
69 AllowDebugChange = true;
75 BlockSignals(True, SIGPIPE);
77 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
78 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
79 ctx->krb5_cc_env = talloc_strdup(frame, "MEMORY:libnetapi");
80 setenv(KRB5_ENV_CCNAME, ctx->krb5_cc_env, 1);
83 libnetapi_initialized = true;
85 *context = stat_ctx = ctx;
87 return NET_API_STATUS_SUCCESS;
90 /****************************************************************
91 ****************************************************************/
93 NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
97 return NET_API_STATUS_SUCCESS;
100 return libnetapi_init(ctx);
103 /****************************************************************
104 ****************************************************************/
106 NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
109 if (ctx->krb5_cc_env) {
110 char *env = getenv(KRB5_ENV_CCNAME);
111 if (env && (strequal(ctx->krb5_cc_env, env))) {
112 unsetenv(KRB5_ENV_CCNAME);
130 return NET_API_STATUS_SUCCESS;
133 /****************************************************************
134 ****************************************************************/
136 NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
137 const char *debuglevel)
139 AllowDebugChange = true;
140 ctx->debuglevel = talloc_strdup(ctx, debuglevel);
141 if (!debug_parse_levels(debuglevel)) {
142 return W_ERROR_V(WERR_GENERAL_FAILURE);
144 return NET_API_STATUS_SUCCESS;
147 /****************************************************************
148 ****************************************************************/
150 NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
153 *debuglevel = ctx->debuglevel;
154 return NET_API_STATUS_SUCCESS;
157 /****************************************************************
158 ****************************************************************/
160 NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
161 const char *username)
163 TALLOC_FREE(ctx->username);
164 ctx->username = talloc_strdup(ctx, username);
165 if (!ctx->username) {
166 return W_ERROR_V(WERR_NOMEM);
168 return NET_API_STATUS_SUCCESS;
171 NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
172 const char *password)
174 TALLOC_FREE(ctx->password);
175 ctx->password = talloc_strdup(ctx, password);
176 if (!ctx->password) {
177 return W_ERROR_V(WERR_NOMEM);
179 return NET_API_STATUS_SUCCESS;
182 NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
183 const char *workgroup)
185 TALLOC_FREE(ctx->workgroup);
186 ctx->workgroup = talloc_strdup(ctx, workgroup);
187 if (!ctx->workgroup) {
188 return W_ERROR_V(WERR_NOMEM);
190 return NET_API_STATUS_SUCCESS;
193 /****************************************************************
194 ****************************************************************/
196 const char *libnetapi_errstr(NET_API_STATUS status)
198 if (status & 0xc0000000) {
199 return get_friendly_nt_error_msg(NT_STATUS(status));
202 return get_friendly_werror_msg(W_ERROR(status));
205 /****************************************************************
206 ****************************************************************/
208 NET_API_STATUS libnetapi_set_error_string(struct libnetapi_ctx *ctx,
209 const char *format, ...)
213 TALLOC_FREE(ctx->error_string);
215 va_start(args, format);
216 ctx->error_string = talloc_vasprintf(ctx, format, args);
219 if (!ctx->error_string) {
220 return W_ERROR_V(WERR_NOMEM);
222 return NET_API_STATUS_SUCCESS;
225 /****************************************************************
226 ****************************************************************/
228 const char *libnetapi_get_error_string(struct libnetapi_ctx *ctx,
229 NET_API_STATUS status)
231 struct libnetapi_ctx *tmp_ctx = ctx;
234 status = libnetapi_getctx(&tmp_ctx);
240 if (tmp_ctx->error_string) {
241 return tmp_ctx->error_string;
244 return libnetapi_errstr(status);
247 /****************************************************************
248 ****************************************************************/
250 NET_API_STATUS NetApiBufferFree(void *buffer)
253 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
258 return NET_API_STATUS_SUCCESS;