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