| 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 | #include "secrets.h"
|
|---|
| 24 | #include "krb5_env.h"
|
|---|
| 25 |
|
|---|
| 26 | struct libnetapi_ctx *stat_ctx = NULL;
|
|---|
| 27 | TALLOC_CTX *frame = NULL;
|
|---|
| 28 | static bool libnetapi_initialized = false;
|
|---|
| 29 |
|
|---|
| 30 | /****************************************************************
|
|---|
| 31 | ****************************************************************/
|
|---|
| 32 |
|
|---|
| 33 | static NET_API_STATUS libnetapi_init_private_context(struct libnetapi_ctx *ctx)
|
|---|
| 34 | {
|
|---|
| 35 | struct libnetapi_private_ctx *priv;
|
|---|
| 36 |
|
|---|
| 37 | if (!ctx) {
|
|---|
| 38 | return W_ERROR_V(WERR_INVALID_PARAM);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | priv = TALLOC_ZERO_P(ctx, struct libnetapi_private_ctx);
|
|---|
| 42 | if (!priv) {
|
|---|
| 43 | return W_ERROR_V(WERR_NOMEM);
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | ctx->private_data = priv;
|
|---|
| 47 |
|
|---|
| 48 | return NET_API_STATUS_SUCCESS;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /****************************************************************
|
|---|
| 52 | Create a libnetapi context, for use in non-Samba applications. This
|
|---|
| 53 | loads the smb.conf file and sets the debug level to 0, so that
|
|---|
| 54 | applications are not flooded with debug logs at level 10, when they
|
|---|
| 55 | were not expecting it.
|
|---|
| 56 | ****************************************************************/
|
|---|
| 57 |
|
|---|
| 58 | NET_API_STATUS libnetapi_init(struct libnetapi_ctx **context)
|
|---|
| 59 | {
|
|---|
| 60 | if (stat_ctx && libnetapi_initialized) {
|
|---|
| 61 | *context = stat_ctx;
|
|---|
| 62 | return NET_API_STATUS_SUCCESS;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | #if 0
|
|---|
| 66 | talloc_enable_leak_report();
|
|---|
| 67 | #endif
|
|---|
| 68 | frame = talloc_stackframe();
|
|---|
| 69 |
|
|---|
| 70 | /* Case tables must be loaded before any string comparisons occour */
|
|---|
| 71 | load_case_tables_library();
|
|---|
| 72 |
|
|---|
| 73 | /* When libnetapi is invoked from an application, it does not
|
|---|
| 74 | * want to be swamped with level 10 debug messages, even if
|
|---|
| 75 | * this has been set for the server in smb.conf */
|
|---|
| 76 | lp_set_cmdline("log level", "0");
|
|---|
| 77 | setup_logging("libnetapi", DEBUG_STDERR);
|
|---|
| 78 |
|
|---|
| 79 | if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
|
|---|
| 80 | TALLOC_FREE(frame);
|
|---|
| 81 | fprintf(stderr, "error loading %s\n", get_dyn_CONFIGFILE() );
|
|---|
| 82 | return W_ERROR_V(WERR_GENERAL_FAILURE);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | init_names();
|
|---|
| 86 | load_interfaces();
|
|---|
| 87 | reopen_logs();
|
|---|
| 88 |
|
|---|
| 89 | BlockSignals(True, SIGPIPE);
|
|---|
| 90 |
|
|---|
| 91 | return libnetapi_net_init(context);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /****************************************************************
|
|---|
| 95 | Create a libnetapi context, for use inside the 'net' binary.
|
|---|
| 96 |
|
|---|
| 97 | As we know net has already loaded the smb.conf file, and set the debug
|
|---|
| 98 | level etc, this avoids doing so again (which causes trouble with -d on
|
|---|
| 99 | the command line).
|
|---|
| 100 | ****************************************************************/
|
|---|
| 101 |
|
|---|
| 102 | NET_API_STATUS libnetapi_net_init(struct libnetapi_ctx **context)
|
|---|
| 103 | {
|
|---|
| 104 | NET_API_STATUS status;
|
|---|
| 105 | struct libnetapi_ctx *ctx = NULL;
|
|---|
| 106 |
|
|---|
| 107 | frame = talloc_stackframe();
|
|---|
| 108 |
|
|---|
| 109 | ctx = talloc_zero(frame, struct libnetapi_ctx);
|
|---|
| 110 | if (!ctx) {
|
|---|
| 111 | TALLOC_FREE(frame);
|
|---|
| 112 | return W_ERROR_V(WERR_NOMEM);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | BlockSignals(True, SIGPIPE);
|
|---|
| 116 |
|
|---|
| 117 | if (getenv("USER")) {
|
|---|
| 118 | ctx->username = talloc_strdup(frame, getenv("USER"));
|
|---|
| 119 | } else {
|
|---|
| 120 | ctx->username = talloc_strdup(frame, "");
|
|---|
| 121 | }
|
|---|
| 122 | if (!ctx->username) {
|
|---|
| 123 | TALLOC_FREE(frame);
|
|---|
| 124 | fprintf(stderr, "libnetapi_init: out of memory\n");
|
|---|
| 125 | return W_ERROR_V(WERR_NOMEM);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | status = libnetapi_init_private_context(ctx);
|
|---|
| 129 | if (status != 0) {
|
|---|
| 130 | TALLOC_FREE(frame);
|
|---|
| 131 | return status;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | libnetapi_initialized = true;
|
|---|
| 135 |
|
|---|
| 136 | *context = stat_ctx = ctx;
|
|---|
| 137 |
|
|---|
| 138 | return NET_API_STATUS_SUCCESS;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /****************************************************************
|
|---|
| 142 | Return the static libnetapi context
|
|---|
| 143 | ****************************************************************/
|
|---|
| 144 |
|
|---|
| 145 | NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
|
|---|
| 146 | {
|
|---|
| 147 | if (stat_ctx) {
|
|---|
| 148 | *ctx = stat_ctx;
|
|---|
| 149 | return NET_API_STATUS_SUCCESS;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | return libnetapi_init(ctx);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /****************************************************************
|
|---|
| 156 | Free the static libnetapi context
|
|---|
| 157 | ****************************************************************/
|
|---|
| 158 |
|
|---|
| 159 | NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
|
|---|
| 160 | {
|
|---|
| 161 | if (!ctx) {
|
|---|
| 162 | return NET_API_STATUS_SUCCESS;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | libnetapi_samr_free(ctx);
|
|---|
| 166 |
|
|---|
| 167 | libnetapi_shutdown_cm(ctx);
|
|---|
| 168 |
|
|---|
| 169 | if (ctx->krb5_cc_env) {
|
|---|
| 170 | char *env = getenv(KRB5_ENV_CCNAME);
|
|---|
| 171 | if (env && (strequal(ctx->krb5_cc_env, env))) {
|
|---|
| 172 | unsetenv(KRB5_ENV_CCNAME);
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | gfree_names();
|
|---|
| 177 | gfree_loadparm();
|
|---|
| 178 | gfree_case_tables();
|
|---|
| 179 | gfree_charcnv();
|
|---|
| 180 | gfree_interfaces();
|
|---|
| 181 |
|
|---|
| 182 | secrets_shutdown();
|
|---|
| 183 |
|
|---|
| 184 | TALLOC_FREE(ctx);
|
|---|
| 185 | TALLOC_FREE(frame);
|
|---|
| 186 |
|
|---|
| 187 | gfree_debugsyms();
|
|---|
| 188 |
|
|---|
| 189 | return NET_API_STATUS_SUCCESS;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | /****************************************************************
|
|---|
| 193 | Override the current log level for libnetapi
|
|---|
| 194 | ****************************************************************/
|
|---|
| 195 |
|
|---|
| 196 | NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
|
|---|
| 197 | const char *debuglevel)
|
|---|
| 198 | {
|
|---|
| 199 | ctx->debuglevel = talloc_strdup(ctx, debuglevel);
|
|---|
| 200 | if (!lp_set_cmdline("log level", debuglevel)) {
|
|---|
| 201 | return W_ERROR_V(WERR_GENERAL_FAILURE);
|
|---|
| 202 | }
|
|---|
| 203 | return NET_API_STATUS_SUCCESS;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /****************************************************************
|
|---|
| 207 | ****************************************************************/
|
|---|
| 208 |
|
|---|
| 209 | NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
|
|---|
| 210 | char **debuglevel)
|
|---|
| 211 | {
|
|---|
| 212 | *debuglevel = ctx->debuglevel;
|
|---|
| 213 | return NET_API_STATUS_SUCCESS;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | /****************************************************************
|
|---|
| 217 | ****************************************************************/
|
|---|
| 218 |
|
|---|
| 219 | NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
|
|---|
| 220 | const char *username)
|
|---|
| 221 | {
|
|---|
| 222 | TALLOC_FREE(ctx->username);
|
|---|
| 223 | ctx->username = talloc_strdup(ctx, username ? username : "");
|
|---|
| 224 |
|
|---|
| 225 | if (!ctx->username) {
|
|---|
| 226 | return W_ERROR_V(WERR_NOMEM);
|
|---|
| 227 | }
|
|---|
| 228 | return NET_API_STATUS_SUCCESS;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
|
|---|
| 232 | const char *password)
|
|---|
| 233 | {
|
|---|
| 234 | TALLOC_FREE(ctx->password);
|
|---|
| 235 | ctx->password = talloc_strdup(ctx, password);
|
|---|
| 236 | if (!ctx->password) {
|
|---|
| 237 | return W_ERROR_V(WERR_NOMEM);
|
|---|
| 238 | }
|
|---|
| 239 | return NET_API_STATUS_SUCCESS;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
|
|---|
| 243 | const char *workgroup)
|
|---|
| 244 | {
|
|---|
| 245 | TALLOC_FREE(ctx->workgroup);
|
|---|
| 246 | ctx->workgroup = talloc_strdup(ctx, workgroup);
|
|---|
| 247 | if (!ctx->workgroup) {
|
|---|
| 248 | return W_ERROR_V(WERR_NOMEM);
|
|---|
| 249 | }
|
|---|
| 250 | return NET_API_STATUS_SUCCESS;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | /****************************************************************
|
|---|
| 254 | ****************************************************************/
|
|---|
| 255 |
|
|---|
| 256 | NET_API_STATUS libnetapi_set_use_kerberos(struct libnetapi_ctx *ctx)
|
|---|
| 257 | {
|
|---|
| 258 | ctx->use_kerberos = true;
|
|---|
| 259 | return NET_API_STATUS_SUCCESS;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /****************************************************************
|
|---|
| 263 | ****************************************************************/
|
|---|
| 264 |
|
|---|
| 265 | NET_API_STATUS libnetapi_set_use_ccache(struct libnetapi_ctx *ctx)
|
|---|
| 266 | {
|
|---|
| 267 | ctx->use_ccache = true;
|
|---|
| 268 | return NET_API_STATUS_SUCCESS;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | /****************************************************************
|
|---|
| 272 | ****************************************************************/
|
|---|
| 273 |
|
|---|
| 274 | const char *libnetapi_errstr(NET_API_STATUS status)
|
|---|
| 275 | {
|
|---|
| 276 | if (status & 0xc0000000) {
|
|---|
| 277 | return get_friendly_nt_error_msg(NT_STATUS(status));
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | return get_friendly_werror_msg(W_ERROR(status));
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | /****************************************************************
|
|---|
| 284 | ****************************************************************/
|
|---|
| 285 |
|
|---|
| 286 | NET_API_STATUS libnetapi_set_error_string(struct libnetapi_ctx *ctx,
|
|---|
| 287 | const char *format, ...)
|
|---|
| 288 | {
|
|---|
| 289 | va_list args;
|
|---|
| 290 |
|
|---|
| 291 | TALLOC_FREE(ctx->error_string);
|
|---|
| 292 |
|
|---|
| 293 | va_start(args, format);
|
|---|
| 294 | ctx->error_string = talloc_vasprintf(ctx, format, args);
|
|---|
| 295 | va_end(args);
|
|---|
| 296 |
|
|---|
| 297 | if (!ctx->error_string) {
|
|---|
| 298 | return W_ERROR_V(WERR_NOMEM);
|
|---|
| 299 | }
|
|---|
| 300 | return NET_API_STATUS_SUCCESS;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | /****************************************************************
|
|---|
| 304 | ****************************************************************/
|
|---|
| 305 |
|
|---|
| 306 | const char *libnetapi_get_error_string(struct libnetapi_ctx *ctx,
|
|---|
| 307 | NET_API_STATUS status_in)
|
|---|
| 308 | {
|
|---|
| 309 | NET_API_STATUS status;
|
|---|
| 310 | struct libnetapi_ctx *tmp_ctx = ctx;
|
|---|
| 311 |
|
|---|
| 312 | if (!tmp_ctx) {
|
|---|
| 313 | status = libnetapi_getctx(&tmp_ctx);
|
|---|
| 314 | if (status != 0) {
|
|---|
| 315 | return NULL;
|
|---|
| 316 | }
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | if (tmp_ctx->error_string) {
|
|---|
| 320 | return tmp_ctx->error_string;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | return libnetapi_errstr(status_in);
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | /****************************************************************
|
|---|
| 327 | ****************************************************************/
|
|---|
| 328 |
|
|---|
| 329 | NET_API_STATUS NetApiBufferAllocate(uint32_t byte_count,
|
|---|
| 330 | void **buffer)
|
|---|
| 331 | {
|
|---|
| 332 | void *buf = NULL;
|
|---|
| 333 |
|
|---|
| 334 | if (!buffer) {
|
|---|
| 335 | return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | if (byte_count == 0) {
|
|---|
| 339 | goto done;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | buf = talloc_size(NULL, byte_count);
|
|---|
| 343 | if (!buf) {
|
|---|
| 344 | return W_ERROR_V(WERR_NOMEM);
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | done:
|
|---|
| 348 | *buffer = buf;
|
|---|
| 349 |
|
|---|
| 350 | return NET_API_STATUS_SUCCESS;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | /****************************************************************
|
|---|
| 354 | ****************************************************************/
|
|---|
| 355 |
|
|---|
| 356 | NET_API_STATUS NetApiBufferFree(void *buffer)
|
|---|
| 357 | {
|
|---|
| 358 | if (!buffer) {
|
|---|
| 359 | return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | talloc_free(buffer);
|
|---|
| 363 |
|
|---|
| 364 | return NET_API_STATUS_SUCCESS;
|
|---|
| 365 | }
|
|---|