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"
22 #include "lib/netapi/netapi_private.h"
24 extern bool AllowDebugChange
;
26 struct libnetapi_ctx
*stat_ctx
= NULL
;
27 TALLOC_CTX
*frame
= NULL
;
28 static bool libnetapi_initialized
= false;
30 /****************************************************************
31 ****************************************************************/
33 NET_API_STATUS
libnetapi_init(struct libnetapi_ctx
**context
)
35 struct libnetapi_ctx
*ctx
= NULL
;
36 char *krb5_cc_env
= NULL
;
38 if (stat_ctx
&& libnetapi_initialized
) {
40 return NET_API_STATUS_SUCCESS
;
44 talloc_enable_leak_report();
46 frame
= talloc_stackframe();
48 ctx
= talloc_zero(frame
, struct libnetapi_ctx
);
51 return W_ERROR_V(WERR_NOMEM
);
58 /* prevent setup_logging() from closing x_stderr... */
60 setup_logging("libnetapi", true);
63 x_setbuf(x_stderr
, NULL
);
64 AllowDebugChange
= false;
68 if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
70 fprintf(stderr
, "lp_load failed\n");
71 return W_ERROR_V(WERR_GENERAL_FAILURE
);
74 AllowDebugChange
= true;
80 BlockSignals(True
, SIGPIPE
);
82 krb5_cc_env
= getenv(KRB5_ENV_CCNAME
);
83 if (!krb5_cc_env
|| (strlen(krb5_cc_env
) == 0)) {
84 ctx
->krb5_cc_env
= talloc_strdup(frame
, "MEMORY:libnetapi");
85 setenv(KRB5_ENV_CCNAME
, ctx
->krb5_cc_env
, 1);
88 ctx
->username
= talloc_strdup(frame
, getenv("USER"));
91 fprintf(stderr
, "out of memory\n");
92 return W_ERROR_V(WERR_NOMEM
);
95 libnetapi_initialized
= true;
97 *context
= stat_ctx
= ctx
;
99 return NET_API_STATUS_SUCCESS
;
102 /****************************************************************
103 ****************************************************************/
105 NET_API_STATUS
libnetapi_getctx(struct libnetapi_ctx
**ctx
)
109 return NET_API_STATUS_SUCCESS
;
112 return libnetapi_init(ctx
);
115 /****************************************************************
116 ****************************************************************/
118 NET_API_STATUS
libnetapi_free(struct libnetapi_ctx
*ctx
)
120 libnetapi_shutdown_cm(ctx
);
122 if (ctx
->krb5_cc_env
) {
123 char *env
= getenv(KRB5_ENV_CCNAME
);
124 if (env
&& (strequal(ctx
->krb5_cc_env
, env
))) {
125 unsetenv(KRB5_ENV_CCNAME
);
143 return NET_API_STATUS_SUCCESS
;
146 /****************************************************************
147 ****************************************************************/
149 NET_API_STATUS
libnetapi_set_debuglevel(struct libnetapi_ctx
*ctx
,
150 const char *debuglevel
)
152 AllowDebugChange
= true;
153 ctx
->debuglevel
= talloc_strdup(ctx
, debuglevel
);
154 if (!debug_parse_levels(debuglevel
)) {
155 return W_ERROR_V(WERR_GENERAL_FAILURE
);
157 return NET_API_STATUS_SUCCESS
;
160 /****************************************************************
161 ****************************************************************/
163 NET_API_STATUS
libnetapi_get_debuglevel(struct libnetapi_ctx
*ctx
,
166 *debuglevel
= ctx
->debuglevel
;
167 return NET_API_STATUS_SUCCESS
;
170 /****************************************************************
171 ****************************************************************/
173 NET_API_STATUS
libnetapi_set_username(struct libnetapi_ctx
*ctx
,
174 const char *username
)
176 TALLOC_FREE(ctx
->username
);
177 ctx
->username
= talloc_strdup(ctx
, username
? username
: "");
179 if (!ctx
->username
) {
180 return W_ERROR_V(WERR_NOMEM
);
182 return NET_API_STATUS_SUCCESS
;
185 NET_API_STATUS
libnetapi_set_password(struct libnetapi_ctx
*ctx
,
186 const char *password
)
188 TALLOC_FREE(ctx
->password
);
189 ctx
->password
= talloc_strdup(ctx
, password
);
190 if (!ctx
->password
) {
191 return W_ERROR_V(WERR_NOMEM
);
193 return NET_API_STATUS_SUCCESS
;
196 NET_API_STATUS
libnetapi_set_workgroup(struct libnetapi_ctx
*ctx
,
197 const char *workgroup
)
199 TALLOC_FREE(ctx
->workgroup
);
200 ctx
->workgroup
= talloc_strdup(ctx
, workgroup
);
201 if (!ctx
->workgroup
) {
202 return W_ERROR_V(WERR_NOMEM
);
204 return NET_API_STATUS_SUCCESS
;
207 /****************************************************************
208 ****************************************************************/
210 const char *libnetapi_errstr(NET_API_STATUS status
)
212 if (status
& 0xc0000000) {
213 return get_friendly_nt_error_msg(NT_STATUS(status
));
216 return get_friendly_werror_msg(W_ERROR(status
));
219 /****************************************************************
220 ****************************************************************/
222 NET_API_STATUS
libnetapi_set_error_string(struct libnetapi_ctx
*ctx
,
223 const char *format
, ...)
227 TALLOC_FREE(ctx
->error_string
);
229 va_start(args
, format
);
230 ctx
->error_string
= talloc_vasprintf(ctx
, format
, args
);
233 if (!ctx
->error_string
) {
234 return W_ERROR_V(WERR_NOMEM
);
236 return NET_API_STATUS_SUCCESS
;
239 /****************************************************************
240 ****************************************************************/
242 const char *libnetapi_get_error_string(struct libnetapi_ctx
*ctx
,
243 NET_API_STATUS status
)
245 struct libnetapi_ctx
*tmp_ctx
= ctx
;
248 status
= libnetapi_getctx(&tmp_ctx
);
254 if (tmp_ctx
->error_string
) {
255 return tmp_ctx
->error_string
;
258 return libnetapi_errstr(status
);
261 /****************************************************************
262 ****************************************************************/
264 NET_API_STATUS
NetApiBufferFree(void *buffer
)
267 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER
);
272 return NET_API_STATUS_SUCCESS
;