Fix bug #9147 - winbind can't fetch user or group info from AD via LDAP
[Samba.git] / source3 / lib / netapi / netapi.c
blob53d45fd1302e15aaec81f1c5a5241c2c16582f8c
1 /*
2 * Unix SMB/CIFS implementation.
3 * NetApi Support
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/>.
20 #include "includes.h"
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;
37 if (!ctx) {
38 return W_ERROR_V(WERR_INVALID_PARAM);
41 priv = TALLOC_ZERO_P(ctx, struct libnetapi_private_ctx);
42 if (!priv) {
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) {
60 *context = stat_ctx;
61 return NET_API_STATUS_SUCCESS;
64 #if 0
65 talloc_enable_leak_report();
66 #endif
67 frame = talloc_stackframe();
69 ctx = talloc_zero(frame, struct libnetapi_ctx);
70 if (!ctx) {
71 TALLOC_FREE(frame);
72 return W_ERROR_V(WERR_NOMEM);
75 if (!DEBUGLEVEL) {
76 DEBUGLEVEL = 0;
79 /* prevent setup_logging() from closing x_stderr... */
80 dbf = 0;
81 setup_logging("libnetapi", true);
83 dbf = x_stderr;
84 x_setbuf(x_stderr, NULL);
85 AllowDebugChange = false;
87 load_case_tables();
89 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
90 TALLOC_FREE(frame);
91 fprintf(stderr, "error loading %s\n", get_dyn_CONFIGFILE() );
92 return W_ERROR_V(WERR_GENERAL_FAILURE);
95 AllowDebugChange = true;
97 init_names();
98 load_interfaces();
99 reopen_logs();
101 BlockSignals(True, SIGPIPE);
103 if (getenv("USER")) {
104 ctx->username = talloc_strdup(frame, getenv("USER"));
105 } else {
106 ctx->username = talloc_strdup(frame, "");
108 if (!ctx->username) {
109 TALLOC_FREE(frame);
110 fprintf(stderr, "libnetapi_init: out of memory\n");
111 return W_ERROR_V(WERR_NOMEM);
114 status = libnetapi_init_private_context(ctx);
115 if (status != 0) {
116 TALLOC_FREE(frame);
117 return status;
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)
132 if (stat_ctx) {
133 *ctx = stat_ctx;
134 return NET_API_STATUS_SUCCESS;
137 return libnetapi_init(ctx);
140 /****************************************************************
141 ****************************************************************/
143 NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
145 if (!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);
160 gfree_names();
161 gfree_loadparm();
162 gfree_case_tables();
163 gfree_charcnv();
164 gfree_interfaces();
166 secrets_shutdown();
168 TALLOC_FREE(ctx);
169 TALLOC_FREE(frame);
171 gfree_debugsyms();
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,
194 char **debuglevel)
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, ...)
273 va_list args;
275 TALLOC_FREE(ctx->error_string);
277 va_start(args, format);
278 ctx->error_string = talloc_vasprintf(ctx, format, args);
279 va_end(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;
296 if (!tmp_ctx) {
297 status = libnetapi_getctx(&tmp_ctx);
298 if (status != 0) {
299 return NULL;
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,
314 void **buffer)
316 void *buf = NULL;
318 if (!buffer) {
319 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
322 if (byte_count == 0) {
323 goto done;
326 buf = talloc_size(NULL, byte_count);
327 if (!buf) {
328 return W_ERROR_V(WERR_NOMEM);
331 done:
332 *buffer = buf;
334 return NET_API_STATUS_SUCCESS;
337 /****************************************************************
338 ****************************************************************/
340 NET_API_STATUS NetApiBufferFree(void *buffer)
342 if (!buffer) {
343 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
346 talloc_free(buffer);
348 return NET_API_STATUS_SUCCESS;