root/source3/lib/netapi/netapi.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. libnetapi_init_private_context
  2. libnetapi_init
  3. libnetapi_getctx
  4. libnetapi_free
  5. libnetapi_set_debuglevel
  6. libnetapi_get_debuglevel
  7. libnetapi_set_username
  8. libnetapi_set_password
  9. libnetapi_set_workgroup
  10. libnetapi_set_use_kerberos
  11. libnetapi_errstr
  12. libnetapi_set_error_string
  13. libnetapi_get_error_string
  14. NetApiBufferAllocate
  15. NetApiBufferFree

   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 static NET_API_STATUS libnetapi_init_private_context(struct libnetapi_ctx *ctx)
     /* [<][>][^][v][top][bottom][index][help] */
  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 ****************************************************************/
  53 
  54 NET_API_STATUS libnetapi_init(struct libnetapi_ctx **context)
     /* [<][>][^][v][top][bottom][index][help] */
  55 {
  56         NET_API_STATUS status;
  57         struct libnetapi_ctx *ctx = NULL;
  58         char *krb5_cc_env = NULL;
  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         ctx = talloc_zero(frame, struct libnetapi_ctx);
  71         if (!ctx) {
  72                 TALLOC_FREE(frame);
  73                 return W_ERROR_V(WERR_NOMEM);
  74         }
  75 
  76         if (!DEBUGLEVEL) {
  77                 DEBUGLEVEL = 0;
  78         }
  79 
  80         /* prevent setup_logging() from closing x_stderr... */
  81         dbf = 0;
  82         setup_logging("libnetapi", true);
  83 
  84         dbf = x_stderr;
  85         x_setbuf(x_stderr, NULL);
  86         AllowDebugChange = false;
  87 
  88         load_case_tables();
  89 
  90         if (!lp_load(get_dyn_CONFIGFILE(), true, false, false, false)) {
  91                 TALLOC_FREE(frame);
  92                 fprintf(stderr, "error loading %s\n", get_dyn_CONFIGFILE() );
  93                 return W_ERROR_V(WERR_GENERAL_FAILURE);
  94         }
  95 
  96         AllowDebugChange = true;
  97 
  98         init_names();
  99         load_interfaces();
 100         reopen_logs();
 101 
 102         BlockSignals(True, SIGPIPE);
 103 
 104         krb5_cc_env = getenv(KRB5_ENV_CCNAME);
 105         if (!krb5_cc_env || (strlen(krb5_cc_env) == 0)) {
 106                 ctx->krb5_cc_env = talloc_strdup(frame, "MEMORY:libnetapi");
 107                 setenv(KRB5_ENV_CCNAME, ctx->krb5_cc_env, 1);
 108         }
 109 
 110         if (getenv("USER")) {
 111                 ctx->username = talloc_strdup(frame, getenv("USER"));
 112         } else {
 113                 ctx->username = talloc_strdup(frame, "");
 114         }
 115         if (!ctx->username) {
 116                 TALLOC_FREE(frame);
 117                 fprintf(stderr, "libnetapi_init: out of memory\n");
 118                 return W_ERROR_V(WERR_NOMEM);
 119         }
 120 
 121         status = libnetapi_init_private_context(ctx);
 122         if (status != 0) {
 123                 TALLOC_FREE(frame);
 124                 return status;
 125         }
 126 
 127         libnetapi_initialized = true;
 128 
 129         *context = stat_ctx = ctx;
 130 
 131         return NET_API_STATUS_SUCCESS;
 132 }
 133 
 134 /****************************************************************
 135 ****************************************************************/
 136 
 137 NET_API_STATUS libnetapi_getctx(struct libnetapi_ctx **ctx)
     /* [<][>][^][v][top][bottom][index][help] */
 138 {
 139         if (stat_ctx) {
 140                 *ctx = stat_ctx;
 141                 return NET_API_STATUS_SUCCESS;
 142         }
 143 
 144         return libnetapi_init(ctx);
 145 }
 146 
 147 /****************************************************************
 148 ****************************************************************/
 149 
 150 NET_API_STATUS libnetapi_free(struct libnetapi_ctx *ctx)
     /* [<][>][^][v][top][bottom][index][help] */
 151 {
 152         if (!ctx) {
 153                 return NET_API_STATUS_SUCCESS;
 154         }
 155 
 156         libnetapi_samr_free(ctx);
 157 
 158         libnetapi_shutdown_cm(ctx);
 159 
 160         if (ctx->krb5_cc_env) {
 161                 char *env = getenv(KRB5_ENV_CCNAME);
 162                 if (env && (strequal(ctx->krb5_cc_env, env))) {
 163                         unsetenv(KRB5_ENV_CCNAME);
 164                 }
 165         }
 166 
 167         gfree_names();
 168         gfree_loadparm();
 169         gfree_case_tables();
 170         gfree_charcnv();
 171         gfree_interfaces();
 172 
 173         gencache_shutdown();
 174         secrets_shutdown();
 175 
 176         TALLOC_FREE(ctx);
 177         TALLOC_FREE(frame);
 178 
 179         gfree_debugsyms();
 180 
 181         return NET_API_STATUS_SUCCESS;
 182 }
 183 
 184 /****************************************************************
 185 ****************************************************************/
 186 
 187 NET_API_STATUS libnetapi_set_debuglevel(struct libnetapi_ctx *ctx,
     /* [<][>][^][v][top][bottom][index][help] */
 188                                         const char *debuglevel)
 189 {
 190         AllowDebugChange = true;
 191         ctx->debuglevel = talloc_strdup(ctx, debuglevel);
 192         if (!debug_parse_levels(debuglevel)) {
 193                 return W_ERROR_V(WERR_GENERAL_FAILURE);
 194         }
 195         return NET_API_STATUS_SUCCESS;
 196 }
 197 
 198 /****************************************************************
 199 ****************************************************************/
 200 
 201 NET_API_STATUS libnetapi_get_debuglevel(struct libnetapi_ctx *ctx,
     /* [<][>][^][v][top][bottom][index][help] */
 202                                         char **debuglevel)
 203 {
 204         *debuglevel = ctx->debuglevel;
 205         return NET_API_STATUS_SUCCESS;
 206 }
 207 
 208 /****************************************************************
 209 ****************************************************************/
 210 
 211 NET_API_STATUS libnetapi_set_username(struct libnetapi_ctx *ctx,
     /* [<][>][^][v][top][bottom][index][help] */
 212                                       const char *username)
 213 {
 214         TALLOC_FREE(ctx->username);
 215         ctx->username = talloc_strdup(ctx, username ? username : "");
 216 
 217         if (!ctx->username) {
 218                 return W_ERROR_V(WERR_NOMEM);
 219         }
 220         return NET_API_STATUS_SUCCESS;
 221 }
 222 
 223 NET_API_STATUS libnetapi_set_password(struct libnetapi_ctx *ctx,
     /* [<][>][^][v][top][bottom][index][help] */
 224                                       const char *password)
 225 {
 226         TALLOC_FREE(ctx->password);
 227         ctx->password = talloc_strdup(ctx, password);
 228         if (!ctx->password) {
 229                 return W_ERROR_V(WERR_NOMEM);
 230         }
 231         return NET_API_STATUS_SUCCESS;
 232 }
 233 
 234 NET_API_STATUS libnetapi_set_workgroup(struct libnetapi_ctx *ctx,
     /* [<][>][^][v][top][bottom][index][help] */
 235                                        const char *workgroup)
 236 {
 237         TALLOC_FREE(ctx->workgroup);
 238         ctx->workgroup = talloc_strdup(ctx, workgroup);
 239         if (!ctx->workgroup) {
 240                 return W_ERROR_V(WERR_NOMEM);
 241         }
 242         return NET_API_STATUS_SUCCESS;
 243 }
 244 
 245 /****************************************************************
 246 ****************************************************************/
 247 
 248 NET_API_STATUS libnetapi_set_use_kerberos(struct libnetapi_ctx *ctx)
     /* [<][>][^][v][top][bottom][index][help] */
 249 {
 250         ctx->use_kerberos = true;
 251         return NET_API_STATUS_SUCCESS;
 252 }
 253 
 254 /****************************************************************
 255 ****************************************************************/
 256 
 257 const char *libnetapi_errstr(NET_API_STATUS status)
     /* [<][>][^][v][top][bottom][index][help] */
 258 {
 259         if (status & 0xc0000000) {
 260                 return get_friendly_nt_error_msg(NT_STATUS(status));
 261         }
 262 
 263         return get_friendly_werror_msg(W_ERROR(status));
 264 }
 265 
 266 /****************************************************************
 267 ****************************************************************/
 268 
 269 NET_API_STATUS libnetapi_set_error_string(struct libnetapi_ctx *ctx,
     /* [<][>][^][v][top][bottom][index][help] */
 270                                           const char *format, ...)
 271 {
 272         va_list args;
 273 
 274         TALLOC_FREE(ctx->error_string);
 275 
 276         va_start(args, format);
 277         ctx->error_string = talloc_vasprintf(ctx, format, args);
 278         va_end(args);
 279 
 280         if (!ctx->error_string) {
 281                 return W_ERROR_V(WERR_NOMEM);
 282         }
 283         return NET_API_STATUS_SUCCESS;
 284 }
 285 
 286 /****************************************************************
 287 ****************************************************************/
 288 
 289 const char *libnetapi_get_error_string(struct libnetapi_ctx *ctx,
     /* [<][>][^][v][top][bottom][index][help] */
 290                                        NET_API_STATUS status_in)
 291 {
 292         NET_API_STATUS status;
 293         struct libnetapi_ctx *tmp_ctx = ctx;
 294 
 295         if (!tmp_ctx) {
 296                 status = libnetapi_getctx(&tmp_ctx);
 297                 if (status != 0) {
 298                         return NULL;
 299                 }
 300         }
 301 
 302         if (tmp_ctx->error_string) {
 303                 return tmp_ctx->error_string;
 304         }
 305 
 306         return libnetapi_errstr(status_in);
 307 }
 308 
 309 /****************************************************************
 310 ****************************************************************/
 311 
 312 NET_API_STATUS NetApiBufferAllocate(uint32_t byte_count,
     /* [<][>][^][v][top][bottom][index][help] */
 313                                     void **buffer)
 314 {
 315         void *buf = NULL;
 316 
 317         if (!buffer) {
 318                 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
 319         }
 320 
 321         if (byte_count == 0) {
 322                 goto done;
 323         }
 324 
 325         buf = talloc_size(NULL, byte_count);
 326         if (!buf) {
 327                 return W_ERROR_V(WERR_NOMEM);
 328         }
 329 
 330  done:
 331         *buffer = buf;
 332 
 333         return NET_API_STATUS_SUCCESS;
 334 }
 335 
 336 /****************************************************************
 337 ****************************************************************/
 338 
 339 NET_API_STATUS NetApiBufferFree(void *buffer)
     /* [<][>][^][v][top][bottom][index][help] */
 340 {
 341         if (!buffer) {
 342                 return W_ERROR_V(WERR_INSUFFICIENT_BUFFER);
 343         }
 344 
 345         talloc_free(buffer);
 346 
 347         return NET_API_STATUS_SUCCESS;
 348 }

/* [<][>][^][v][top][bottom][index][help] */