Only set DEBUGLEVEL to 0 in libnetapi when not set already.
[samba.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
23 extern bool AllowDebugChange;
24
25 struct libnetapi_ctx *stat_ctx = NULL;
26 TALLOC_CTX *frame = NULL;
27 static bool libnetapi_initialized = false;
28
29 /****************************************************************
30 ****************************************************************/
31
32 NET_API_STATUS libnetapi_init(struct libnetapi_ctx **context)
33 {
34         struct libnetapi_ctx *ctx = NULL;
35         char *krb5_cc_env = NULL;
36
37         if (stat_ctx && libnetapi_initialized) {
38                 *context = stat_ctx;
39                 return NET_API_STATUS_SUCCESS;
40         }
41
42 #ifdef DEVELOPER
43         talloc_enable_leak_report();
44 #endif
45         frame = talloc_stackframe();
46
47         ctx = talloc_zero(frame, struct libnetapi_ctx);
48         if (!ctx) {
49                 TALLOC_FREE(frame);
50                 return W_ERROR_V(WERR_NOMEM);
51         }
52
53         if (!DEBUGLEVEL) {
54                 DEBUGLEVEL = 0;
55         }
56         setup_logging("libnetapi", true);
57
58         dbf = x_stderr;
59         x_setbuf(x_stderr, NULL);
60         AllowDebugChange = false;
61
62         load_case_tables();
63
64         if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
65                 TALLOC_FREE(frame);
66                 return W_ERROR_V(WERR_GENERAL_FAILURE);
67         }
68
69         AllowDebugChange = true;
70
71         init_names();
72         load_interfaces();
73         reopen_logs();
74
75         BlockSignals(True, SIGPIPE);
76
77         krb5_cc_env = getenv(KRB5_ENV_CCNAME);
78         if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
79                 ctx->krb5_cc_env = talloc_strdup(frame, "MEMORY:libnetapi");
80                 setenv(KRB5_ENV_CCNAME, ctx->krb5_cc_env, 1);
81         }
82
83         libnetapi_initialized = true;
84
85         *context = stat_ctx = ctx;
86
87         return NET_API_STATUS_SUCCESS;
88 }
89
90 /****************************************************************
91 ****************************************************************/
92
93 NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
94 {
95         if (stat_ctx) {
96                 *ctx = stat_ctx;
97                 return NET_API_STATUS_SUCCESS;
98         }
99
100         return libnetapi_init(ctx);
101 }
102
103 /****************************************************************
104 ****************************************************************/
105
106 NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
107 {
108
109         if (ctx->krb5_cc_env) {
110                 char *env = getenv(KRB5_ENV_CCNAME);
111                 if (env && (strequal(ctx->krb5_cc_env, env))) {
112                         unsetenv(KRB5_ENV_CCNAME);
113                 }
114         }
115
116         gfree_names();
117         gfree_loadparm();
118         gfree_case_tables();
119         gfree_charcnv();
120         gfree_interfaces();
121
122         gencache_shutdown();
123         secrets_shutdown();
124
125         TALLOC_FREE(ctx);
126         TALLOC_FREE(frame);
127
128         gfree_debugsyms();
129
130         return NET_API_STATUS_SUCCESS;
131 }
132
133 /****************************************************************
134 ****************************************************************/
135
136 NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
137                                         const char *debuglevel)
138 {
139         AllowDebugChange = true;
140         ctx->debuglevel = talloc_strdup(ctx, debuglevel);
141         if (!debug_parse_levels(debuglevel)) {
142                 return W_ERROR_V(WERR_GENERAL_FAILURE);
143         }
144         return NET_API_STATUS_SUCCESS;
145 }
146
147 /****************************************************************
148 ****************************************************************/
149
150 NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
151                                         char **debuglevel)
152 {
153         *debuglevel = ctx->debuglevel;
154         return NET_API_STATUS_SUCCESS;
155 }
156
157 /****************************************************************
158 ****************************************************************/
159
160 NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
161                                       const char *username)
162 {
163         TALLOC_FREE(ctx->username);
164         ctx->username = talloc_strdup(ctx, username);
165         if (!ctx->username) {
166                 return W_ERROR_V(WERR_NOMEM);
167         }
168         return NET_API_STATUS_SUCCESS;
169 }
170
171 NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
172                                       const char *password)
173 {
174         TALLOC_FREE(ctx->password);
175         ctx->password = talloc_strdup(ctx, password);
176         if (!ctx->password) {
177                 return W_ERROR_V(WERR_NOMEM);
178         }
179         return NET_API_STATUS_SUCCESS;
180 }
181
182 NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
183                                        const char *workgroup)
184 {
185         TALLOC_FREE(ctx->workgroup);
186         ctx->workgroup = talloc_strdup(ctx, workgroup);
187         if (!ctx->workgroup) {
188                 return W_ERROR_V(WERR_NOMEM);
189         }
190         return NET_API_STATUS_SUCCESS;
191 }
192
193 /****************************************************************
194 ****************************************************************/
195
196 const char *libnetapi_errstr(NET_API_STATUS status)
197 {
198         if (status & 0xc0000000) {
199                 return get_friendly_nt_error_msg(NT_STATUS(status));
200         }
201
202         return get_friendly_werror_msg(W_ERROR(status));
203 }
204
205 /****************************************************************
206 ****************************************************************/
207
208 NET_API_STATUS libnetapi_set_error_string(struct libnetapi_ctx *ctx,
209                                           const char *format, ...)
210 {
211         va_list args;
212
213         TALLOC_FREE(ctx->error_string);
214
215         va_start(args, format);
216         ctx->error_string = talloc_vasprintf(ctx, format, args);
217         va_end(args);
218
219         if (!ctx->error_string) {
220                 return W_ERROR_V(WERR_NOMEM);
221         }
222         return NET_API_STATUS_SUCCESS;
223 }
224
225 /****************************************************************
226 ****************************************************************/
227
228 const char *libnetapi_get_error_string(struct libnetapi_ctx *ctx,
229                                        NET_API_STATUS status)
230 {
231         struct libnetapi_ctx *tmp_ctx = ctx;
232
233         if (!tmp_ctx) {
234                 status = libnetapi_getctx(&tmp_ctx);
235                 if (status != 0) {
236                         return NULL;
237                 }
238         }
239
240         if (tmp_ctx->error_string) {
241                 return tmp_ctx->error_string;
242         }
243
244         return libnetapi_errstr(status);
245 }
246
247 /****************************************************************
248 ****************************************************************/
249
250 NET_API_STATUS NetApiBufferFree(void *buffer)
251 {
252         if (!buffer) {
253                 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
254         }
255
256         talloc_free(buffer);
257
258         return NET_API_STATUS_SUCCESS;
259 }