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 "../libcli/auth/netlogon_creds_cli.h"
22 #include "lib/netapi/netapi.h"
23 #include "lib/netapi/netapi_private.h"
27 struct libnetapi_ctx *stat_ctx = 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_PARAMETER);
41 priv = talloc_zero(ctx, struct libnetapi_private_ctx);
43 return W_ERROR_V(WERR_NOT_ENOUGH_MEMORY);
46 ctx->private_data = priv;
48 return NET_API_STATUS_SUCCESS;
51 /****************************************************************
52 Create a libnetapi context, for use in non-Samba applications. This
53 loads the smb.conf file and sets the debug level to 0, so that
54 applications are not flooded with debug logs at level 10, when they
55 were not expecting it.
56 ****************************************************************/
58 NET_API_STATUS libnetapi_init(struct libnetapi_ctx **context)
62 if (stat_ctx && libnetapi_initialized) {
64 return NET_API_STATUS_SUCCESS;
68 talloc_enable_leak_report();
70 frame = talloc_stackframe();
72 /* When libnetapi is invoked from an application, it does not
73 * want to be swamped with level 10 debug messages, even if
74 * this has been set for the server in smb.conf */
75 lp_set_cmdline("log level", "0");
76 setup_logging("libnetapi", DEBUG_STDERR);
78 if (!lp_load_global(get_dyn_CONFIGFILE())) {
80 fprintf(stderr, "error loading %s\n", get_dyn_CONFIGFILE() );
81 return W_ERROR_V(WERR_GEN_FAILURE);
87 BlockSignals(True, SIGPIPE);
89 ret = libnetapi_net_init(context);
94 /****************************************************************
95 Create a libnetapi context, for use inside the 'net' binary.
97 As we know net has already loaded the smb.conf file, and set the debug
98 level etc, this avoids doing so again (which causes trouble with -d on
100 ****************************************************************/
102 NET_API_STATUS libnetapi_net_init(struct libnetapi_ctx **context)
104 NET_API_STATUS status;
105 struct libnetapi_ctx *ctx = NULL;
106 TALLOC_CTX *frame = talloc_stackframe();
108 ctx = talloc_zero(frame, struct libnetapi_ctx);
111 return W_ERROR_V(WERR_NOT_ENOUGH_MEMORY);
114 BlockSignals(True, SIGPIPE);
116 if (getenv("USER")) {
117 ctx->username = talloc_strdup(ctx, getenv("USER"));
119 ctx->username = talloc_strdup(ctx, "");
121 if (!ctx->username) {
123 fprintf(stderr, "libnetapi_init: out of memory\n");
124 return W_ERROR_V(WERR_NOT_ENOUGH_MEMORY);
127 status = libnetapi_init_private_context(ctx);
133 libnetapi_initialized = true;
135 talloc_steal(NULL, ctx);
136 *context = stat_ctx = ctx;
139 return NET_API_STATUS_SUCCESS;
142 /****************************************************************
143 Return the static libnetapi context
144 ****************************************************************/
146 NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
150 return NET_API_STATUS_SUCCESS;
153 return libnetapi_init(ctx);
156 /****************************************************************
157 Free the static libnetapi context
158 ****************************************************************/
160 NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
165 return NET_API_STATUS_SUCCESS;
168 frame = talloc_stackframe();
169 libnetapi_samr_free(ctx);
171 libnetapi_shutdown_cm(ctx);
173 if (ctx->krb5_cc_env) {
174 char *env = getenv(KRB5_ENV_CCNAME);
175 if (env && (strequal(ctx->krb5_cc_env, env))) {
176 unsetenv(KRB5_ENV_CCNAME);
186 netlogon_creds_cli_close_global_db();
188 if (ctx == stat_ctx) {
196 return NET_API_STATUS_SUCCESS;
199 /****************************************************************
200 Override the current log level for libnetapi
201 ****************************************************************/
203 NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
204 const char *debuglevel)
206 TALLOC_CTX *frame = talloc_stackframe();
207 ctx->debuglevel = talloc_strdup(ctx, debuglevel);
209 if (!lp_set_cmdline("log level", debuglevel)) {
211 return W_ERROR_V(WERR_GEN_FAILURE);
214 return NET_API_STATUS_SUCCESS;
217 /****************************************************************
218 ****************************************************************/
220 NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
223 *debuglevel = ctx->debuglevel;
224 return NET_API_STATUS_SUCCESS;
227 /****************************************************************
228 ****************************************************************/
231 * @brief Get the username of the libnet context
233 * @param[in] ctx The netapi context
235 * @param[in] username A pointer to hold the username.
237 * @return 0 on success, an werror code otherwise.
239 NET_API_STATUS libnetapi_get_username(struct libnetapi_ctx *ctx,
240 const char **username)
243 return W_ERROR_V(WERR_INVALID_PARAMETER);
246 if (username != NULL) {
247 *username = ctx->username;
250 return NET_API_STATUS_SUCCESS;
254 * @brief Get the password of the libnet context
256 * @param[in] ctx The netapi context
258 * @param[in] password A pointer to hold the password.
260 * @return 0 on success, an werror code otherwise.
262 NET_API_STATUS libnetapi_get_password(struct libnetapi_ctx *ctx,
263 const char **password)
266 return W_ERROR_V(WERR_INVALID_PARAMETER);
269 if (password != NULL) {
270 *password = ctx->password;
273 return NET_API_STATUS_SUCCESS;
276 NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
277 const char *username)
279 TALLOC_FREE(ctx->username);
280 ctx->username = talloc_strdup(ctx, username ? username : "");
282 if (!ctx->username) {
283 return W_ERROR_V(WERR_NOT_ENOUGH_MEMORY);
285 return NET_API_STATUS_SUCCESS;
288 NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
289 const char *password)
291 TALLOC_FREE(ctx->password);
292 ctx->password = talloc_strdup(ctx, password);
293 if (!ctx->password) {
294 return W_ERROR_V(WERR_NOT_ENOUGH_MEMORY);
296 return NET_API_STATUS_SUCCESS;
299 NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
300 const char *workgroup)
302 TALLOC_FREE(ctx->workgroup);
303 ctx->workgroup = talloc_strdup(ctx, workgroup);
304 if (!ctx->workgroup) {
305 return W_ERROR_V(WERR_NOT_ENOUGH_MEMORY);
307 return NET_API_STATUS_SUCCESS;
310 /****************************************************************
311 ****************************************************************/
313 NET_API_STATUS libnetapi_set_use_kerberos(struct libnetapi_ctx *ctx)
315 ctx->use_kerberos = true;
316 return NET_API_STATUS_SUCCESS;
319 /****************************************************************
320 ****************************************************************/
322 NET_API_STATUS libnetapi_set_use_ccache(struct libnetapi_ctx *ctx)
324 ctx->use_ccache = true;
325 return NET_API_STATUS_SUCCESS;
328 /****************************************************************
329 Return a libnetapi error as a string, caller must free with NetApiBufferFree
330 ****************************************************************/
332 char *libnetapi_errstr(NET_API_STATUS status)
334 TALLOC_CTX *frame = talloc_stackframe();
336 if (status & 0xc0000000) {
337 ret = talloc_strdup(NULL,
338 get_friendly_nt_error_msg(NT_STATUS(status)));
340 ret = talloc_strdup(NULL,
341 get_friendly_werror_msg(W_ERROR(status)));
347 /****************************************************************
348 ****************************************************************/
350 NET_API_STATUS libnetapi_set_error_string(struct libnetapi_ctx *ctx,
351 const char *format, ...)
355 TALLOC_FREE(ctx->error_string);
357 va_start(args, format);
358 ctx->error_string = talloc_vasprintf(ctx, format, args);
361 if (!ctx->error_string) {
362 return W_ERROR_V(WERR_NOT_ENOUGH_MEMORY);
364 return NET_API_STATUS_SUCCESS;
367 /****************************************************************
368 Return a libnetapi_errstr(), caller must free with NetApiBufferFree
369 ****************************************************************/
371 char *libnetapi_get_error_string(struct libnetapi_ctx *ctx,
372 NET_API_STATUS status_in)
374 NET_API_STATUS status;
375 struct libnetapi_ctx *tmp_ctx = ctx;
378 status = libnetapi_getctx(&tmp_ctx);
384 if (tmp_ctx->error_string) {
385 return talloc_strdup(NULL, tmp_ctx->error_string);
388 return libnetapi_errstr(status_in);
391 /****************************************************************
392 ****************************************************************/
394 NET_API_STATUS NetApiBufferAllocate(uint32_t byte_count,
400 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
403 if (byte_count == 0) {
407 buf = talloc_size(NULL, byte_count);
409 return W_ERROR_V(WERR_NOT_ENOUGH_MEMORY);
415 return NET_API_STATUS_SUCCESS;
418 /****************************************************************
419 ****************************************************************/
421 NET_API_STATUS NetApiBufferFree(void *buffer)
424 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
429 return NET_API_STATUS_SUCCESS;