source: vendor/3.6.0/source3/lib/netapi/netapi.c

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

Samba Server: update vendor to 3.6.0

File size: 9.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#include "secrets.h"
24#include "krb5_env.h"
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/****************************************************************
52Create a libnetapi context, for use in non-Samba applications. This
53loads the smb.conf file and sets the debug level to 0, so that
54applications are not flooded with debug logs at level 10, when they
55were not expecting it.
56****************************************************************/
57
58NET_API_STATUS libnetapi_init(struct libnetapi_ctx **context)
59{
60 if (stat_ctx && libnetapi_initialized) {
61 *context = stat_ctx;
62 return NET_API_STATUS_SUCCESS;
63 }
64
65#if 0
66 talloc_enable_leak_report();
67#endif
68 frame = talloc_stackframe();
69
70 /* Case tables must be loaded before any string comparisons occour */
71 load_case_tables_library();
72
73 /* When libnetapi is invoked from an application, it does not
74 * want to be swamped with level 10 debug messages, even if
75 * this has been set for the server in smb.conf */
76 lp_set_cmdline("log level", "0");
77 setup_logging("libnetapi", DEBUG_STDERR);
78
79 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
80 TALLOC_FREE(frame);
81 fprintf(stderr, "error loading %s\n", get_dyn_CONFIGFILE() );
82 return W_ERROR_V(WERR_GENERAL_FAILURE);
83 }
84
85 init_names();
86 load_interfaces();
87 reopen_logs();
88
89 BlockSignals(True, SIGPIPE);
90
91 return libnetapi_net_init(context);
92}
93
94/****************************************************************
95Create a libnetapi context, for use inside the 'net' binary.
96
97As we know net has already loaded the smb.conf file, and set the debug
98level etc, this avoids doing so again (which causes trouble with -d on
99the command line).
100****************************************************************/
101
102NET_API_STATUS libnetapi_net_init(struct libnetapi_ctx **context)
103{
104 NET_API_STATUS status;
105 struct libnetapi_ctx *ctx = NULL;
106 char *krb5_cc_env = NULL;
107
108 frame = talloc_stackframe();
109
110 ctx = talloc_zero(frame, struct libnetapi_ctx);
111 if (!ctx) {
112 TALLOC_FREE(frame);
113 return W_ERROR_V(WERR_NOMEM);
114 }
115
116 BlockSignals(True, SIGPIPE);
117
118 krb5_cc_env = getenv(KRB5_ENV_CCNAME);
119 if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
120 ctx->krb5_cc_env = talloc_strdup(frame, "MEMORY:libnetapi");
121 setenv(KRB5_ENV_CCNAME, ctx->krb5_cc_env, 1);
122 }
123
124 if (getenv("USER")) {
125 ctx->username = talloc_strdup(frame, getenv("USER"));
126 } else {
127 ctx->username = talloc_strdup(frame, "");
128 }
129 if (!ctx->username) {
130 TALLOC_FREE(frame);
131 fprintf(stderr, "libnetapi_init: out of memory\n");
132 return W_ERROR_V(WERR_NOMEM);
133 }
134
135 status = libnetapi_init_private_context(ctx);
136 if (status != 0) {
137 TALLOC_FREE(frame);
138 return status;
139 }
140
141 libnetapi_initialized = true;
142
143 *context = stat_ctx = ctx;
144
145 return NET_API_STATUS_SUCCESS;
146}
147
148/****************************************************************
149 Return the static libnetapi context
150****************************************************************/
151
152NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
153{
154 if (stat_ctx) {
155 *ctx = stat_ctx;
156 return NET_API_STATUS_SUCCESS;
157 }
158
159 return libnetapi_init(ctx);
160}
161
162/****************************************************************
163 Free the static libnetapi context
164****************************************************************/
165
166NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
167{
168 if (!ctx) {
169 return NET_API_STATUS_SUCCESS;
170 }
171
172 libnetapi_samr_free(ctx);
173
174 libnetapi_shutdown_cm(ctx);
175
176 if (ctx->krb5_cc_env) {
177 char *env = getenv(KRB5_ENV_CCNAME);
178 if (env && (strequal(ctx->krb5_cc_env, env))) {
179 unsetenv(KRB5_ENV_CCNAME);
180 }
181 }
182
183 gfree_names();
184 gfree_loadparm();
185 gfree_case_tables();
186 gfree_charcnv();
187 gfree_interfaces();
188
189 secrets_shutdown();
190
191 TALLOC_FREE(ctx);
192 TALLOC_FREE(frame);
193
194 gfree_debugsyms();
195
196 return NET_API_STATUS_SUCCESS;
197}
198
199/****************************************************************
200 Override the current log level for libnetapi
201****************************************************************/
202
203NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
204 const char *debuglevel)
205{
206 ctx->debuglevel = talloc_strdup(ctx, debuglevel);
207 if (!lp_set_cmdline("log level", debuglevel)) {
208 return W_ERROR_V(WERR_GENERAL_FAILURE);
209 }
210 return NET_API_STATUS_SUCCESS;
211}
212
213/****************************************************************
214****************************************************************/
215
216NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
217 char **debuglevel)
218{
219 *debuglevel = ctx->debuglevel;
220 return NET_API_STATUS_SUCCESS;
221}
222
223/****************************************************************
224****************************************************************/
225
226NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
227 const char *username)
228{
229 TALLOC_FREE(ctx->username);
230 ctx->username = talloc_strdup(ctx, username ? username : "");
231
232 if (!ctx->username) {
233 return W_ERROR_V(WERR_NOMEM);
234 }
235 return NET_API_STATUS_SUCCESS;
236}
237
238NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
239 const char *password)
240{
241 TALLOC_FREE(ctx->password);
242 ctx->password = talloc_strdup(ctx, password);
243 if (!ctx->password) {
244 return W_ERROR_V(WERR_NOMEM);
245 }
246 return NET_API_STATUS_SUCCESS;
247}
248
249NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
250 const char *workgroup)
251{
252 TALLOC_FREE(ctx->workgroup);
253 ctx->workgroup = talloc_strdup(ctx, workgroup);
254 if (!ctx->workgroup) {
255 return W_ERROR_V(WERR_NOMEM);
256 }
257 return NET_API_STATUS_SUCCESS;
258}
259
260/****************************************************************
261****************************************************************/
262
263NET_API_STATUS libnetapi_set_use_kerberos(struct libnetapi_ctx *ctx)
264{
265 ctx->use_kerberos = true;
266 return NET_API_STATUS_SUCCESS;
267}
268
269NET_API_STATUS libnetapi_set_use_ccache(struct libnetapi_ctx *ctx)
270{
271 ctx->use_ccache = true;
272 return NET_API_STATUS_SUCCESS;
273}
274
275/****************************************************************
276****************************************************************/
277
278const char *libnetapi_errstr(NET_API_STATUS status)
279{
280 if (status & 0xc0000000) {
281 return get_friendly_nt_error_msg(NT_STATUS(status));
282 }
283
284 return get_friendly_werror_msg(W_ERROR(status));
285}
286
287/****************************************************************
288****************************************************************/
289
290NET_API_STATUS libnetapi_set_error_string(struct libnetapi_ctx *ctx,
291 const char *format, ...)
292{
293 va_list args;
294
295 TALLOC_FREE(ctx->error_string);
296
297 va_start(args, format);
298 ctx->error_string = talloc_vasprintf(ctx, format, args);
299 va_end(args);
300
301 if (!ctx->error_string) {
302 return W_ERROR_V(WERR_NOMEM);
303 }
304 return NET_API_STATUS_SUCCESS;
305}
306
307/****************************************************************
308****************************************************************/
309
310const char *libnetapi_get_error_string(struct libnetapi_ctx *ctx,
311 NET_API_STATUS status_in)
312{
313 NET_API_STATUS status;
314 struct libnetapi_ctx *tmp_ctx = ctx;
315
316 if (!tmp_ctx) {
317 status = libnetapi_getctx(&tmp_ctx);
318 if (status != 0) {
319 return NULL;
320 }
321 }
322
323 if (tmp_ctx->error_string) {
324 return tmp_ctx->error_string;
325 }
326
327 return libnetapi_errstr(status_in);
328}
329
330/****************************************************************
331****************************************************************/
332
333NET_API_STATUS NetApiBufferAllocate(uint32_t byte_count,
334 void **buffer)
335{
336 void *buf = NULL;
337
338 if (!buffer) {
339 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
340 }
341
342 if (byte_count == 0) {
343 goto done;
344 }
345
346 buf = talloc_size(NULL, byte_count);
347 if (!buf) {
348 return W_ERROR_V(WERR_NOMEM);
349 }
350
351 done:
352 *buffer = buf;
353
354 return NET_API_STATUS_SUCCESS;
355}
356
357/****************************************************************
358****************************************************************/
359
360NET_API_STATUS NetApiBufferFree(void *buffer)
361{
362 if (!buffer) {
363 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
364 }
365
366 talloc_free(buffer);
367
368 return NET_API_STATUS_SUCCESS;
369}
Note: See TracBrowser for help on using the repository browser.