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;
42 frame = talloc_stackframe();
44 ctx = talloc_zero(frame, struct libnetapi_ctx);
47 return W_ERROR_V(WERR_NOMEM);
51 setup_logging("libnetapi", true);
54 x_setbuf(x_stderr, NULL);
55 AllowDebugChange = false;
59 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
61 return W_ERROR_V(WERR_GENERAL_FAILURE);
64 AllowDebugChange = true;
70 BlockSignals(True, SIGPIPE);
72 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
73 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
74 ctx->krb5_cc_env = talloc_strdup(frame, "MEMORY:libnetapi");
75 setenv(KRB5_ENV_CCNAME, ctx->krb5_cc_env, 1);
78 libnetapi_initialized = true;
80 *context = stat_ctx = ctx;
82 return NET_API_STATUS_SUCCESS;
85 /****************************************************************
86 ****************************************************************/
88 NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
92 return NET_API_STATUS_SUCCESS;
95 return libnetapi_init(ctx);
98 /****************************************************************
99 ****************************************************************/
101 NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
113 if (ctx->krb5_cc_env &&
114 (strequal(ctx->krb5_cc_env, getenv(KRB5_ENV_CCNAME)))) {
115 unsetenv(KRB5_ENV_CCNAME);
123 return NET_API_STATUS_SUCCESS;
126 /****************************************************************
127 ****************************************************************/
129 NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
130 const char *debuglevel)
132 AllowDebugChange = true;
133 ctx->debuglevel = talloc_strdup(ctx, debuglevel);
134 if (!debug_parse_levels(debuglevel)) {
135 return W_ERROR_V(WERR_GENERAL_FAILURE);
137 return NET_API_STATUS_SUCCESS;
140 /****************************************************************
141 ****************************************************************/
143 NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
146 *debuglevel = ctx->debuglevel;
147 return NET_API_STATUS_SUCCESS;
150 /****************************************************************
151 ****************************************************************/
153 NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
154 const char *username)
156 TALLOC_FREE(ctx->username);
157 ctx->username = talloc_strdup(ctx, username);
158 if (!ctx->username) {
159 return W_ERROR_V(WERR_NOMEM);
161 return NET_API_STATUS_SUCCESS;
164 NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
165 const char *password)
167 TALLOC_FREE(ctx->password);
168 ctx->password = talloc_strdup(ctx, password);
169 if (!ctx->password) {
170 return W_ERROR_V(WERR_NOMEM);
172 return NET_API_STATUS_SUCCESS;
175 NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
176 const char *workgroup)
178 TALLOC_FREE(ctx->workgroup);
179 ctx->workgroup = talloc_strdup(ctx, workgroup);
180 if (!ctx->workgroup) {
181 return W_ERROR_V(WERR_NOMEM);
183 return NET_API_STATUS_SUCCESS;
186 /****************************************************************
187 ****************************************************************/
189 const char *libnetapi_errstr(struct libnetapi_ctx *ctx,
190 NET_API_STATUS status)
192 if (status & 0xc0000000) {
193 return get_friendly_nt_error_msg(NT_STATUS(status));
196 return get_friendly_werror_msg(W_ERROR(status));
199 /****************************************************************
200 ****************************************************************/
202 NET_API_STATUS libnetapi_set_error_string(struct libnetapi_ctx *ctx,
203 const char *error_string)
205 TALLOC_FREE(ctx->error_string);
206 ctx->error_string = talloc_strdup(ctx, error_string);
207 if (!ctx->error_string) {
208 return W_ERROR_V(WERR_NOMEM);
210 return NET_API_STATUS_SUCCESS;
214 /****************************************************************
215 ****************************************************************/
217 const char *libnetapi_get_error_string(struct libnetapi_ctx *ctx)
219 return ctx->error_string;