Add libnetapi_set_error_string and libnetapi_get_error_string.
[metze/samba/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         frame = talloc_stackframe();
43
44         ctx = talloc_zero(frame, struct libnetapi_ctx);
45         if (!ctx) {
46                 TALLOC_FREE(frame);
47                 return W_ERROR_V(WERR_NOMEM);
48         }
49
50         DEBUGLEVEL = 0;
51         setup_logging("libnetapi", true);
52
53         dbf = x_stderr;
54         x_setbuf(x_stderr, NULL);
55         AllowDebugChange = false;
56
57         load_case_tables();
58
59         if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
60                 TALLOC_FREE(frame);
61                 return W_ERROR_V(WERR_GENERAL_FAILURE);
62         }
63
64         AllowDebugChange = true;
65
66         init_names();
67         load_interfaces();
68         reopen_logs();
69
70         BlockSignals(True, SIGPIPE);
71
72         krb5_cc_env = getenv(KRB5_ENV_CCNAME);
73         if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
74                 ctx->krb5_cc_env = talloc_strdup(frame, "MEMORY:libnetapi");
75                 setenv(KRB5_ENV_CCNAME, ctx->krb5_cc_env, 1);
76         }
77
78         libnetapi_initialized = true;
79
80         *context = stat_ctx = ctx;
81
82         return NET_API_STATUS_SUCCESS;
83 }
84
85 /****************************************************************
86 ****************************************************************/
87
88 NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
89 {
90         if (stat_ctx) {
91                 *ctx = stat_ctx;
92                 return NET_API_STATUS_SUCCESS;
93         }
94
95         return libnetapi_init(ctx);
96 }
97
98 /****************************************************************
99 ****************************************************************/
100
101 NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
102 {
103         gfree_names();
104         gfree_loadparm();
105         gfree_case_tables();
106         gfree_charcnv();
107         gfree_interfaces();
108
109         gencache_shutdown();
110         secrets_shutdown();
111         regdb_close();
112
113         if (ctx->krb5_cc_env &&
114             (strequal(ctx->krb5_cc_env, getenv(KRB5_ENV_CCNAME)))) {
115                 unsetenv(KRB5_ENV_CCNAME);
116         }
117
118         TALLOC_FREE(ctx);
119         TALLOC_FREE(frame);
120
121         gfree_debugsyms();
122
123         return NET_API_STATUS_SUCCESS;
124 }
125
126 /****************************************************************
127 ****************************************************************/
128
129 NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
130                                         const char *debuglevel)
131 {
132         AllowDebugChange = true;
133         ctx->debuglevel = talloc_strdup(ctx, debuglevel);
134         if (!debug_parse_levels(debuglevel)) {
135                 return W_ERROR_V(WERR_GENERAL_FAILURE);
136         }
137         return NET_API_STATUS_SUCCESS;
138 }
139
140 /****************************************************************
141 ****************************************************************/
142
143 NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
144                                         char **debuglevel)
145 {
146         *debuglevel = ctx->debuglevel;
147         return NET_API_STATUS_SUCCESS;
148 }
149
150 /****************************************************************
151 ****************************************************************/
152
153 NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
154                                       const char *username)
155 {
156         TALLOC_FREE(ctx->username);
157         ctx->username = talloc_strdup(ctx, username);
158         if (!ctx->username) {
159                 return W_ERROR_V(WERR_NOMEM);
160         }
161         return NET_API_STATUS_SUCCESS;
162 }
163
164 NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
165                                       const char *password)
166 {
167         TALLOC_FREE(ctx->password);
168         ctx->password = talloc_strdup(ctx, password);
169         if (!ctx->password) {
170                 return W_ERROR_V(WERR_NOMEM);
171         }
172         return NET_API_STATUS_SUCCESS;
173 }
174
175 NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
176                                        const char *workgroup)
177 {
178         TALLOC_FREE(ctx->workgroup);
179         ctx->workgroup = talloc_strdup(ctx, workgroup);
180         if (!ctx->workgroup) {
181                 return W_ERROR_V(WERR_NOMEM);
182         }
183         return NET_API_STATUS_SUCCESS;
184 }
185
186 /****************************************************************
187 ****************************************************************/
188
189 const char *libnetapi_errstr(struct libnetapi_ctx *ctx,
190                              NET_API_STATUS status)
191 {
192         if (status & 0xc0000000) {
193                 return get_friendly_nt_error_msg(NT_STATUS(status));
194         }
195
196         return get_friendly_werror_msg(W_ERROR(status));
197 }
198
199 /****************************************************************
200 ****************************************************************/
201
202 NET_API_STATUS libnetapi_set_error_string(struct libnetapi_ctx *ctx,
203                                           const char *error_string)
204 {
205         TALLOC_FREE(ctx->error_string);
206         ctx->error_string = talloc_strdup(ctx, error_string);
207         if (!ctx->error_string) {
208                 return W_ERROR_V(WERR_NOMEM);
209         }
210         return NET_API_STATUS_SUCCESS;
211
212 }
213
214 /****************************************************************
215 ****************************************************************/
216
217 const char *libnetapi_get_error_string(struct libnetapi_ctx *ctx)
218 {
219         return ctx->error_string;
220 }