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