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