source: branches/samba-3.5.x/source3/lib/netapi/netapi.c

Last change on this file was 732, checked in by Silvan Scherrer, 13 years ago

Samba Server 3.5: update branche to 3.5.12

File size: 8.5 KB
Line 
1/*
2 * Unix SMB/CIFS implementation.
3 * NetApi Support
4 * Copyright (C) Guenther Deschner 2007-2008
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include "includes.h"
21#include "lib/netapi/netapi.h"
22#include "lib/netapi/netapi_private.h"
23
24extern bool AllowDebugChange;
25
26struct libnetapi_ctx *stat_ctx = NULL;
27TALLOC_CTX *frame = NULL;
28static bool libnetapi_initialized = false;
29
30/****************************************************************
31****************************************************************/
32
33static NET_API_STATUS libnetapi_init_private_context(struct libnetapi_ctx *ctx)
34{
35 struct libnetapi_private_ctx *priv;
36
37 if (!ctx) {
38 return W_ERROR_V(WERR_INVALID_PARAM);
39 }
40
41 priv = TALLOC_ZERO_P(ctx, struct libnetapi_private_ctx);
42 if (!priv) {
43 return W_ERROR_V(WERR_NOMEM);
44 }
45
46 ctx->private_data = priv;
47
48 return NET_API_STATUS_SUCCESS;
49}
50
51/****************************************************************
52****************************************************************/
53
54NET_API_STATUS libnetapi_init(struct libnetapi_ctx **context)
55{
56 NET_API_STATUS status;
57 struct libnetapi_ctx *ctx = NULL;
58
59 if (stat_ctx && libnetapi_initialized) {
60 *context = stat_ctx;
61 return NET_API_STATUS_SUCCESS;
62 }
63
64#if 0
65 talloc_enable_leak_report();
66#endif
67 frame = talloc_stackframe();
68
69 ctx = talloc_zero(frame, struct libnetapi_ctx);
70 if (!ctx) {
71 TALLOC_FREE(frame);
72 return W_ERROR_V(WERR_NOMEM);
73 }
74
75 if (!DEBUGLEVEL) {
76 DEBUGLEVEL = 0;
77 }
78
79 /* prevent setup_logging() from closing x_stderr... */
80 dbf = 0;
81 setup_logging("libnetapi", true);
82
83 dbf = x_stderr;
84 x_setbuf(x_stderr, NULL);
85 AllowDebugChange = false;
86
87 load_case_tables();
88
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);
93 }
94
95 AllowDebugChange = true;
96
97 init_names();
98 load_interfaces();
99 reopen_logs();
100
101 BlockSignals(True, SIGPIPE);
102
103 if (getenv("USER")) {
104 ctx->username = talloc_strdup(frame, getenv("USER"));
105 } else {
106 ctx->username = talloc_strdup(frame, "");
107 }
108 if (!ctx->username) {
109 TALLOC_FREE(frame);
110 fprintf(stderr, "libnetapi_init: out of memory\n");
111 return W_ERROR_V(WERR_NOMEM);
112 }
113
114 status = libnetapi_init_private_context(ctx);
115 if (status != 0) {
116 TALLOC_FREE(frame);
117 return status;
118 }
119
120 libnetapi_initialized = true;
121
122 *context = stat_ctx = ctx;
123
124 return NET_API_STATUS_SUCCESS;
125}
126
127/****************************************************************
128****************************************************************/
129
130NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
131{
132 if (stat_ctx) {
133 *ctx = stat_ctx;
134 return NET_API_STATUS_SUCCESS;
135 }
136
137 return libnetapi_init(ctx);
138}
139
140/****************************************************************
141****************************************************************/
142
143NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
144{
145 if (!ctx) {
146 return NET_API_STATUS_SUCCESS;
147 }
148
149 libnetapi_samr_free(ctx);
150
151 libnetapi_shutdown_cm(ctx);
152
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);
157 }
158 }
159
160 gfree_names();
161 gfree_loadparm();
162 gfree_case_tables();
163 gfree_charcnv();
164 gfree_interfaces();
165
166 secrets_shutdown();
167
168 TALLOC_FREE(ctx);
169 TALLOC_FREE(frame);
170
171 gfree_debugsyms();
172
173 return NET_API_STATUS_SUCCESS;
174}
175
176/****************************************************************
177****************************************************************/
178
179NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
180 const char *debuglevel)
181{
182 AllowDebugChange = true;
183 ctx->debuglevel = talloc_strdup(ctx, debuglevel);
184 if (!debug_parse_levels(debuglevel)) {
185 return W_ERROR_V(WERR_GENERAL_FAILURE);
186 }
187 return NET_API_STATUS_SUCCESS;
188}
189
190/****************************************************************
191****************************************************************/
192
193NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
194 char **debuglevel)
195{
196 *debuglevel = ctx->debuglevel;
197 return NET_API_STATUS_SUCCESS;
198}
199
200/****************************************************************
201****************************************************************/
202
203NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
204 const char *username)
205{
206 TALLOC_FREE(ctx->username);
207 ctx->username = talloc_strdup(ctx, username ? username : "");
208
209 if (!ctx->username) {
210 return W_ERROR_V(WERR_NOMEM);
211 }
212 return NET_API_STATUS_SUCCESS;
213}
214
215NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
216 const char *password)
217{
218 TALLOC_FREE(ctx->password);
219 ctx->password = talloc_strdup(ctx, password);
220 if (!ctx->password) {
221 return W_ERROR_V(WERR_NOMEM);
222 }
223 return NET_API_STATUS_SUCCESS;
224}
225
226NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
227 const char *workgroup)
228{
229 TALLOC_FREE(ctx->workgroup);
230 ctx->workgroup = talloc_strdup(ctx, workgroup);
231 if (!ctx->workgroup) {
232 return W_ERROR_V(WERR_NOMEM);
233 }
234 return NET_API_STATUS_SUCCESS;
235}
236
237/****************************************************************
238****************************************************************/
239
240NET_API_STATUS libnetapi_set_use_kerberos(struct libnetapi_ctx *ctx)
241{
242 ctx->use_kerberos = true;
243 return NET_API_STATUS_SUCCESS;
244}
245
246/****************************************************************
247****************************************************************/
248
249NET_API_STATUS libnetapi_set_use_ccache(struct libnetapi_ctx *ctx)
250{
251 ctx->use_ccache = true;
252 return NET_API_STATUS_SUCCESS;
253}
254
255/****************************************************************
256****************************************************************/
257
258const char *libnetapi_errstr(NET_API_STATUS status)
259{
260 if (status & 0xc0000000) {
261 return get_friendly_nt_error_msg(NT_STATUS(status));
262 }
263
264 return get_friendly_werror_msg(W_ERROR(status));
265}
266
267/****************************************************************
268****************************************************************/
269
270NET_API_STATUS libnetapi_set_error_string(struct libnetapi_ctx *ctx,
271 const char *format, ...)
272{
273 va_list args;
274
275 TALLOC_FREE(ctx->error_string);
276
277 va_start(args, format);
278 ctx->error_string = talloc_vasprintf(ctx, format, args);
279 va_end(args);
280
281 if (!ctx->error_string) {
282 return W_ERROR_V(WERR_NOMEM);
283 }
284 return NET_API_STATUS_SUCCESS;
285}
286
287/****************************************************************
288****************************************************************/
289
290const char *libnetapi_get_error_string(struct libnetapi_ctx *ctx,
291 NET_API_STATUS status_in)
292{
293 NET_API_STATUS status;
294 struct libnetapi_ctx *tmp_ctx = ctx;
295
296 if (!tmp_ctx) {
297 status = libnetapi_getctx(&tmp_ctx);
298 if (status != 0) {
299 return NULL;
300 }
301 }
302
303 if (tmp_ctx->error_string) {
304 return tmp_ctx->error_string;
305 }
306
307 return libnetapi_errstr(status_in);
308}
309
310/****************************************************************
311****************************************************************/
312
313NET_API_STATUS NetApiBufferAllocate(uint32_t byte_count,
314 void **buffer)
315{
316 void *buf = NULL;
317
318 if (!buffer) {
319 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
320 }
321
322 if (byte_count == 0) {
323 goto done;
324 }
325
326 buf = talloc_size(NULL, byte_count);
327 if (!buf) {
328 return W_ERROR_V(WERR_NOMEM);
329 }
330
331 done:
332 *buffer = buf;
333
334 return NET_API_STATUS_SUCCESS;
335}
336
337/****************************************************************
338****************************************************************/
339
340NET_API_STATUS NetApiBufferFree(void *buffer)
341{
342 if (!buffer) {
343 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
344 }
345
346 talloc_free(buffer);
347
348 return NET_API_STATUS_SUCCESS;
349}
Note: See TracBrowser for help on using the repository browser.