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"
24 extern bool AllowDebugChange
;
26 struct libnetapi_ctx
*stat_ctx
= NULL
;
27 TALLOC_CTX
*frame
= NULL
;
28 static bool libnetapi_initialized
= false;
30 /****************************************************************
31 ****************************************************************/
33 static NET_API_STATUS
libnetapi_init_private_context(struct libnetapi_ctx
*ctx
)
35 struct libnetapi_private_ctx
*priv
;
38 return W_ERROR_V(WERR_INVALID_PARAM
);
41 priv
= TALLOC_ZERO_P(ctx
, struct libnetapi_private_ctx
);
43 return W_ERROR_V(WERR_NOMEM
);
46 ctx
->private_data
= priv
;
48 return NET_API_STATUS_SUCCESS
;
51 /****************************************************************
52 ****************************************************************/
54 NET_API_STATUS
libnetapi_init(struct libnetapi_ctx
**context
)
56 NET_API_STATUS status
;
57 struct libnetapi_ctx
*ctx
= NULL
;
59 if (stat_ctx
&& libnetapi_initialized
) {
61 return NET_API_STATUS_SUCCESS
;
65 talloc_enable_leak_report();
67 frame
= talloc_stackframe();
69 ctx
= talloc_zero(frame
, struct libnetapi_ctx
);
72 return W_ERROR_V(WERR_NOMEM
);
79 /* prevent setup_logging() from closing x_stderr... */
81 setup_logging("libnetapi", true);
84 x_setbuf(x_stderr
, NULL
);
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 if (getenv("USER")) {
104 ctx
->username
= talloc_strdup(frame
, getenv("USER"));
106 ctx
->username
= talloc_strdup(frame
, "");
108 if (!ctx
->username
) {
110 fprintf(stderr
, "libnetapi_init: out of memory\n");
111 return W_ERROR_V(WERR_NOMEM
);
114 status
= libnetapi_init_private_context(ctx
);
120 libnetapi_initialized
= true;
122 *context
= stat_ctx
= ctx
;
124 return NET_API_STATUS_SUCCESS
;
127 /****************************************************************
128 ****************************************************************/
130 NET_API_STATUS
libnetapi_getctx(struct libnetapi_ctx
**ctx
)
134 return NET_API_STATUS_SUCCESS
;
137 return libnetapi_init(ctx
);
140 /****************************************************************
141 ****************************************************************/
143 NET_API_STATUS
libnetapi_free(struct libnetapi_ctx
*ctx
)
146 return NET_API_STATUS_SUCCESS
;
149 libnetapi_samr_free(ctx
);
151 libnetapi_shutdown_cm(ctx
);
153 if (ctx
->krb5_cc_env
) {
154 char *env
= getenv(KRB5_ENV_CCNAME
);
155 if (env
&& (strequal(ctx
->krb5_cc_env
, env
))) {
156 unsetenv(KRB5_ENV_CCNAME
);
173 return NET_API_STATUS_SUCCESS
;
176 /****************************************************************
177 ****************************************************************/
179 NET_API_STATUS
libnetapi_set_debuglevel(struct libnetapi_ctx
*ctx
,
180 const char *debuglevel
)
182 AllowDebugChange
= true;
183 ctx
->debuglevel
= talloc_strdup(ctx
, debuglevel
);
184 if (!debug_parse_levels(debuglevel
)) {
185 return W_ERROR_V(WERR_GENERAL_FAILURE
);
187 return NET_API_STATUS_SUCCESS
;
190 /****************************************************************
191 ****************************************************************/
193 NET_API_STATUS
libnetapi_get_debuglevel(struct libnetapi_ctx
*ctx
,
196 *debuglevel
= ctx
->debuglevel
;
197 return NET_API_STATUS_SUCCESS
;
200 /****************************************************************
201 ****************************************************************/
203 NET_API_STATUS
libnetapi_set_username(struct libnetapi_ctx
*ctx
,
204 const char *username
)
206 TALLOC_FREE(ctx
->username
);
207 ctx
->username
= talloc_strdup(ctx
, username
? username
: "");
209 if (!ctx
->username
) {
210 return W_ERROR_V(WERR_NOMEM
);
212 return NET_API_STATUS_SUCCESS
;
215 NET_API_STATUS
libnetapi_set_password(struct libnetapi_ctx
*ctx
,
216 const char *password
)
218 TALLOC_FREE(ctx
->password
);
219 ctx
->password
= talloc_strdup(ctx
, password
);
220 if (!ctx
->password
) {
221 return W_ERROR_V(WERR_NOMEM
);
223 return NET_API_STATUS_SUCCESS
;
226 NET_API_STATUS
libnetapi_set_workgroup(struct libnetapi_ctx
*ctx
,
227 const char *workgroup
)
229 TALLOC_FREE(ctx
->workgroup
);
230 ctx
->workgroup
= talloc_strdup(ctx
, workgroup
);
231 if (!ctx
->workgroup
) {
232 return W_ERROR_V(WERR_NOMEM
);
234 return NET_API_STATUS_SUCCESS
;
237 /****************************************************************
238 ****************************************************************/
240 NET_API_STATUS
libnetapi_set_use_kerberos(struct libnetapi_ctx
*ctx
)
242 ctx
->use_kerberos
= true;
243 return NET_API_STATUS_SUCCESS
;
246 /****************************************************************
247 ****************************************************************/
249 NET_API_STATUS
libnetapi_set_use_ccache(struct libnetapi_ctx
*ctx
)
251 ctx
->use_ccache
= true;
252 return NET_API_STATUS_SUCCESS
;
255 /****************************************************************
256 ****************************************************************/
258 const char *libnetapi_errstr(NET_API_STATUS status
)
260 if (status
& 0xc0000000) {
261 return get_friendly_nt_error_msg(NT_STATUS(status
));
264 return get_friendly_werror_msg(W_ERROR(status
));
267 /****************************************************************
268 ****************************************************************/
270 NET_API_STATUS
libnetapi_set_error_string(struct libnetapi_ctx
*ctx
,
271 const char *format
, ...)
275 TALLOC_FREE(ctx
->error_string
);
277 va_start(args
, format
);
278 ctx
->error_string
= talloc_vasprintf(ctx
, format
, args
);
281 if (!ctx
->error_string
) {
282 return W_ERROR_V(WERR_NOMEM
);
284 return NET_API_STATUS_SUCCESS
;
287 /****************************************************************
288 ****************************************************************/
290 const char *libnetapi_get_error_string(struct libnetapi_ctx
*ctx
,
291 NET_API_STATUS status_in
)
293 NET_API_STATUS status
;
294 struct libnetapi_ctx
*tmp_ctx
= ctx
;
297 status
= libnetapi_getctx(&tmp_ctx
);
303 if (tmp_ctx
->error_string
) {
304 return tmp_ctx
->error_string
;
307 return libnetapi_errstr(status_in
);
310 /****************************************************************
311 ****************************************************************/
313 NET_API_STATUS
NetApiBufferAllocate(uint32_t byte_count
,
319 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER
);
322 if (byte_count
== 0) {
326 buf
= talloc_size(NULL
, byte_count
);
328 return W_ERROR_V(WERR_NOMEM
);
334 return NET_API_STATUS_SUCCESS
;
337 /****************************************************************
338 ****************************************************************/
340 NET_API_STATUS
NetApiBufferFree(void *buffer
)
343 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER
);
348 return NET_API_STATUS_SUCCESS
;