2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Guenther Deschner 2007-2008
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.
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.
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/>.
21 #include "lib/netapi/netapi.h"
23 extern bool AllowDebugChange
;
25 struct libnetapi_ctx
*stat_ctx
= NULL
;
26 TALLOC_CTX
*frame
= NULL
;
27 static bool libnetapi_initialized
= false;
29 /****************************************************************
30 ****************************************************************/
32 NET_API_STATUS
libnetapi_init(struct libnetapi_ctx
**context
)
34 struct libnetapi_ctx
*ctx
= NULL
;
35 char *krb5_cc_env
= NULL
;
37 if (stat_ctx
&& libnetapi_initialized
) {
39 return NET_API_STATUS_SUCCESS
;
43 talloc_enable_leak_report();
45 frame
= talloc_stackframe();
47 ctx
= talloc_zero(frame
, struct libnetapi_ctx
);
50 return W_ERROR_V(WERR_NOMEM
);
56 setup_logging("libnetapi", true);
59 x_setbuf(x_stderr
, NULL
);
60 AllowDebugChange
= false;
64 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
66 fprintf(stderr
, "lp_load failed\n");
67 return W_ERROR_V(WERR_GENERAL_FAILURE
);
70 AllowDebugChange
= true;
76 BlockSignals(True
, SIGPIPE
);
78 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
79 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
80 ctx
->krb5_cc_env
= talloc_strdup(frame
, "MEMORY:libnetapi");
81 setenv(KRB5_ENV_CCNAME
, ctx
->krb5_cc_env
, 1);
84 libnetapi_initialized
= true;
86 *context
= stat_ctx
= ctx
;
88 return NET_API_STATUS_SUCCESS
;
91 /****************************************************************
92 ****************************************************************/
94 NET_API_STATUS
libnetapi_getctx(struct libnetapi_ctx
**ctx
)
98 return NET_API_STATUS_SUCCESS
;
101 return libnetapi_init(ctx
);
104 /****************************************************************
105 ****************************************************************/
107 NET_API_STATUS
libnetapi_free(struct libnetapi_ctx
*ctx
)
110 if (ctx
->krb5_cc_env
) {
111 char *env
= getenv(KRB5_ENV_CCNAME
);
112 if (env
&& (strequal(ctx
->krb5_cc_env
, env
))) {
113 unsetenv(KRB5_ENV_CCNAME
);
131 return NET_API_STATUS_SUCCESS
;
134 /****************************************************************
135 ****************************************************************/
137 NET_API_STATUS
libnetapi_set_debuglevel(struct libnetapi_ctx
*ctx
,
138 const char *debuglevel
)
140 AllowDebugChange
= true;
141 ctx
->debuglevel
= talloc_strdup(ctx
, debuglevel
);
142 if (!debug_parse_levels(debuglevel
)) {
143 return W_ERROR_V(WERR_GENERAL_FAILURE
);
145 return NET_API_STATUS_SUCCESS
;
148 /****************************************************************
149 ****************************************************************/
151 NET_API_STATUS
libnetapi_get_debuglevel(struct libnetapi_ctx
*ctx
,
154 *debuglevel
= ctx
->debuglevel
;
155 return NET_API_STATUS_SUCCESS
;
158 /****************************************************************
159 ****************************************************************/
161 NET_API_STATUS
libnetapi_set_username(struct libnetapi_ctx
*ctx
,
162 const char *username
)
164 TALLOC_FREE(ctx
->username
);
165 ctx
->username
= talloc_strdup(ctx
, username
);
166 if (!ctx
->username
) {
167 return W_ERROR_V(WERR_NOMEM
);
169 return NET_API_STATUS_SUCCESS
;
172 NET_API_STATUS
libnetapi_set_password(struct libnetapi_ctx
*ctx
,
173 const char *password
)
175 TALLOC_FREE(ctx
->password
);
176 ctx
->password
= talloc_strdup(ctx
, password
);
177 if (!ctx
->password
) {
178 return W_ERROR_V(WERR_NOMEM
);
180 return NET_API_STATUS_SUCCESS
;
183 NET_API_STATUS
libnetapi_set_workgroup(struct libnetapi_ctx
*ctx
,
184 const char *workgroup
)
186 TALLOC_FREE(ctx
->workgroup
);
187 ctx
->workgroup
= talloc_strdup(ctx
, workgroup
);
188 if (!ctx
->workgroup
) {
189 return W_ERROR_V(WERR_NOMEM
);
191 return NET_API_STATUS_SUCCESS
;
194 /****************************************************************
195 ****************************************************************/
197 const char *libnetapi_errstr(NET_API_STATUS status
)
199 if (status
& 0xc0000000) {
200 return get_friendly_nt_error_msg(NT_STATUS(status
));
203 return get_friendly_werror_msg(W_ERROR(status
));
206 /****************************************************************
207 ****************************************************************/
209 NET_API_STATUS
libnetapi_set_error_string(struct libnetapi_ctx
*ctx
,
210 const char *format
, ...)
214 TALLOC_FREE(ctx
->error_string
);
216 va_start(args
, format
);
217 ctx
->error_string
= talloc_vasprintf(ctx
, format
, args
);
220 if (!ctx
->error_string
) {
221 return W_ERROR_V(WERR_NOMEM
);
223 return NET_API_STATUS_SUCCESS
;
226 /****************************************************************
227 ****************************************************************/
229 const char *libnetapi_get_error_string(struct libnetapi_ctx
*ctx
,
230 NET_API_STATUS status
)
232 struct libnetapi_ctx
*tmp_ctx
= ctx
;
235 status
= libnetapi_getctx(&tmp_ctx
);
241 if (tmp_ctx
->error_string
) {
242 return tmp_ctx
->error_string
;
245 return libnetapi_errstr(status
);
248 /****************************************************************
249 ****************************************************************/
251 NET_API_STATUS
NetApiBufferFree(void *buffer
)
254 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER
);
259 return NET_API_STATUS_SUCCESS
;