示例#1
0
static int
Context_init (Context *self, PyObject *args, PyObject *kwds)
{
  PyObject *auth = NULL;
  int debug = 0;
  SMBCCTX *ctx;
  static char *kwlist[] =
    {
      "auth_fn",
      "debug",
      NULL
    };

  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|Oi", kwlist,
				    &auth, &debug))
    {
      return -1;
    }

  if (auth)
    {
      if (!PyCallable_Check (auth))
	{
	  PyErr_SetString (PyExc_TypeError, "auth_fn must be callable");
	  return -1;
	}

      Py_XINCREF (auth);
      self->auth_fn = auth;
    }

  debugprintf ("-> Context_init ()\n");

  errno = 0;
  ctx = smbc_new_context ();
  if (ctx == NULL)
    {
      PyErr_SetFromErrno (PyExc_RuntimeError);
      debugprintf ("<- Context_init() EXCEPTION\n");
      return -1;
    }

  smbc_setDebug (ctx, debug);

  self->context = ctx;
  smbc_setOptionUserData (ctx, self);
  if (auth)
    smbc_setFunctionAuthDataWithContext (ctx, auth_fn);

  if (smbc_init_context (ctx) == NULL)
    {
      PyErr_SetFromErrno (PyExc_RuntimeError);
      smbc_free_context (ctx, 0);
      debugprintf ("<- Context_init() EXCEPTION\n");
      return -1;
    }

  debugprintf ("%p <- Context_init() = 0\n", self->context);
  return 0;
}
示例#2
0
csync_vio_method_t *vio_module_init(const char *method_name, const char *args,
    csync_auth_callback cb, void *userdata) {
  smb_context = smbc_new_context();

  DEBUG_SMB(("csync_smb - method_name: %s\n", method_name));
  DEBUG_SMB(("csync_smb - args: %s\n", args));
  (void) method_name;
  (void) args;
  (void) cb;

  if (smb_context == NULL) {
    fprintf(stderr, "csync_smb - failed to create new smbc context\n");
    return NULL;
  }

  if (cb != NULL) {
    _authcb = cb;
  }

  /* set debug level and authentication function callback */
  smbc_setDebug(smb_context, 0);
  smbc_setOptionUserData(smb_context, userdata);
  smbc_setFunctionAuthDataWithContext(smb_context, get_auth_data_with_context_fn);

  /* Kerberos support */
  smbc_setOptionUseKerberos(smb_context, 1);
  smbc_setOptionFallbackAfterKerberos(smb_context, 1);

  DEBUG_SMB(("csync_smb - use kerberos = %d\n",
        smbc_getOptionUseKerberos(smb_context)));
  DEBUG_SMB(("csync_smb - use fallback after kerberos = %d\n",
        smbc_getOptionFallbackAfterKerberos(smb_context)));

  if (smbc_init_context(smb_context) == NULL) {
    fprintf(stderr, "csync_smb - failed to initialize the smbc context");
    smbc_free_context(smb_context, 0);
    smb_context = NULL;

    return NULL;
  }

  DEBUG_SMB(("csync_smb - KRB5CCNAME = %s\n", getenv("KRB5CCNAME") != NULL ?
        getenv("KRB5CCNAME") : "not set"));

  smbc_set_context(smb_context);

  return &_method;
}
示例#3
0
bool torture_libsmbclient_init_context(struct torture_context *tctx,
				       SMBCCTX **ctx_p)
{
	SMBCCTX *ctx;

	ctx = smbc_new_context();
	torture_assert(tctx, ctx, "failed to get new context");
	torture_assert(tctx, smbc_init_context(ctx), "failed to init context");

	smbc_setDebug(ctx, DEBUGLEVEL);
	smbc_setOptionDebugToStderr(ctx, 1);

	/* yes, libsmbclient API frees the username when freeing the context, so
	 * have to pass malloced data here */
	smbc_setUser(ctx, strdup(cli_credentials_get_username(cmdline_credentials)));

	*ctx_p = ctx;

	return true;
}
示例#4
0
static int
Context_setDebug (Context *self, PyObject *value, void *closure)
{
  int d;

#if PY_MAJOR_VERSION < 3
  if (PyInt_Check (value))
    value = PyLong_FromLong (PyInt_AsLong (value));
#endif

  if (!PyLong_Check (value))
    {
      PyErr_SetString (PyExc_TypeError, "must be int");
      return -1;
    }

  d = PyLong_AsLong (value);
  smbc_setDebug (self->context, d);
  return 0;
}
示例#5
0
int
smbc_init(smbc_get_auth_data_fn fn,
          int debug)
{
	if (!smbc_compat_initialized) {
		statcont = smbc_new_context();
		if (!statcont) 
			return -1;
                
                smbc_setDebug(statcont, debug);
                smbc_setFunctionAuthData(statcont, fn);
                
		if (!smbc_init_context(statcont)) {
			smbc_free_context(statcont, False);
			return -1;
		}
                
		smbc_compat_initialized = 1;
                
		return 0;
	}
	return 0;
}
示例#6
0
static SMBCCTX* create_context(void) {
	SMBCCTX  		*ctx;
	extern int 		gTimeout;
	extern int 		gPassIsHash;

	//Create the Samba context struct , if it didn't work quit. 
	if((ctx = smbc_new_context()) == NULL)
		return NULL;

#ifdef DEBUG
	//Set the options for our context.  a
	//If its enabled at the command line, turn on Samba library debugging
	smbc_setDebug(ctx, 1);

	//We want to log our errors to STDERR instead of STDOUT
	smbc_setOptionDebugToStderr(ctx, 1);
#endif

	//Set the function that the Samba library will call when it needs
	//to authenticate
	smbc_setFunctionAuthData(ctx, auth_fn);
	//Set the timeout, we get the command line option as seconds and the 
	//function takes milliseconds.
	smbc_setTimeout(ctx, 200);
	//If we got a hash at the command line, let the context know we're
	//giving it a hash
	smbc_setOptionUseNTHash(ctx, gPassIsHash);

	//Initialize our context with the options that we have set or null on fail.
	if(smbc_init_context(ctx) == NULL) {
		smbc_free_context(ctx, 1);
		return NULL;
	}

	return ctx;
}
示例#7
0
/*
 * Get a new empty handle to fill in with your own info
 */
SMBCCTX *
smbc_new_context(void)
{
        SMBCCTX *context;
        
        /* The first call to this function should initialize the module */
        SMB_THREAD_ONCE(&SMBC_initialized, SMBC_module_init, NULL);

        /*
         * All newly added context fields should be placed in
         * SMBC_internal_data, not directly in SMBCCTX.
         */
        context = SMB_MALLOC_P(SMBCCTX);
        if (!context) {
                errno = ENOMEM;
                return NULL;
        }
        
        ZERO_STRUCTP(context);
        
        context->internal = SMB_MALLOC_P(struct SMBC_internal_data);
        if (!context->internal) {
                SAFE_FREE(context);
                errno = ENOMEM;
                return NULL;
        }
        
        /* Initialize the context and establish reasonable defaults */
        ZERO_STRUCTP(context->internal);
        
        smbc_setDebug(context, 0);
        smbc_setTimeout(context, 20000);
        
        smbc_setOptionFullTimeNames(context, False);
        smbc_setOptionOpenShareMode(context, SMBC_SHAREMODE_DENY_NONE);
        smbc_setOptionSmbEncryptionLevel(context, SMBC_ENCRYPTLEVEL_NONE);
        smbc_setOptionCaseSensitive(context, False);
        smbc_setOptionBrowseMaxLmbCount(context, 3);    /* # LMBs to query */
        smbc_setOptionUrlEncodeReaddirEntries(context, False);
        smbc_setOptionOneSharePerServer(context, False);
        
        smbc_setFunctionAuthData(context, SMBC_get_auth_data);
        smbc_setFunctionCheckServer(context, SMBC_check_server);
        smbc_setFunctionRemoveUnusedServer(context, SMBC_remove_unused_server);
        
        smbc_setOptionUserData(context, NULL);
        smbc_setFunctionAddCachedServer(context, SMBC_add_cached_server);
        smbc_setFunctionGetCachedServer(context, SMBC_get_cached_server);
        smbc_setFunctionRemoveCachedServer(context, SMBC_remove_cached_server);
        smbc_setFunctionPurgeCachedServers(context, SMBC_purge_cached_servers);
        
        smbc_setFunctionOpen(context, SMBC_open_ctx);
        smbc_setFunctionCreat(context, SMBC_creat_ctx);
        smbc_setFunctionRead(context, SMBC_read_ctx);
        smbc_setFunctionWrite(context, SMBC_write_ctx);
        smbc_setFunctionClose(context, SMBC_close_ctx);
        smbc_setFunctionUnlink(context, SMBC_unlink_ctx);
        smbc_setFunctionRename(context, SMBC_rename_ctx);
        smbc_setFunctionLseek(context, SMBC_lseek_ctx);
        smbc_setFunctionFtruncate(context, SMBC_ftruncate_ctx);
        smbc_setFunctionStat(context, SMBC_stat_ctx);
        smbc_setFunctionStatVFS(context, SMBC_statvfs_ctx);
        smbc_setFunctionFstatVFS(context, SMBC_fstatvfs_ctx);
        smbc_setFunctionFstat(context, SMBC_fstat_ctx);
        smbc_setFunctionOpendir(context, SMBC_opendir_ctx);
        smbc_setFunctionClosedir(context, SMBC_closedir_ctx);
        smbc_setFunctionReaddir(context, SMBC_readdir_ctx);
        smbc_setFunctionGetdents(context, SMBC_getdents_ctx);
        smbc_setFunctionMkdir(context, SMBC_mkdir_ctx);
        smbc_setFunctionRmdir(context, SMBC_rmdir_ctx);
        smbc_setFunctionTelldir(context, SMBC_telldir_ctx);
        smbc_setFunctionLseekdir(context, SMBC_lseekdir_ctx);
        smbc_setFunctionFstatdir(context, SMBC_fstatdir_ctx);
        smbc_setFunctionChmod(context, SMBC_chmod_ctx);
        smbc_setFunctionUtimes(context, SMBC_utimes_ctx);
        smbc_setFunctionSetxattr(context, SMBC_setxattr_ctx);
        smbc_setFunctionGetxattr(context, SMBC_getxattr_ctx);
        smbc_setFunctionRemovexattr(context, SMBC_removexattr_ctx);
        smbc_setFunctionListxattr(context, SMBC_listxattr_ctx);
        
        smbc_setFunctionOpenPrintJob(context, SMBC_open_print_job_ctx);
        smbc_setFunctionPrintFile(context, SMBC_print_file_ctx);
        smbc_setFunctionListPrintJobs(context, SMBC_list_print_jobs_ctx);
        smbc_setFunctionUnlinkPrintJob(context, SMBC_unlink_print_job_ctx);
        
        return context;
}
示例#8
0
int
main(int argc, char * argv[])
{
    int                         debug = 0;
    int                         debug_stderr = 0;
    int                         no_auth = 0;
    int                         context_auth = 0;
    int                         scan = 0;
    int                         iterations = -1;
    int                         opt;
    char *                      p;
    char                        buf[1024];
    poptContext                 pc;
    SMBCCTX *                   context;
    struct poptOption           long_options[] =
        {
            POPT_AUTOHELP
            {
                "debug", 'd', POPT_ARG_INT, &debug,
                0, "Set debug level", "integer"
            },
            {
                "stderr", 'e', POPT_ARG_NONE, &debug_stderr,
                0, "Debug log to stderr instead of stdout", "integer"
            },
            {
                "scan", 's', POPT_ARG_NONE, &scan,
                0, "Scan for servers and shares", "integer"
            },
            {
                "iterations", 'i', POPT_ARG_INT, &iterations,
                0, "Iterations", "integer"
            },
            {
                "noauth", 'A', POPT_ARG_NONE, &no_auth,
                0, "Do not request authentication data", "integer"
            },
            {
                "contextauth", 'C', POPT_ARG_NONE, &context_auth,
                0, "Use new authentication function with context", "integer"
            },
            {
                NULL
            }
        };
    
    setbuf(stdout, NULL);

    pc = poptGetContext("opendir", argc, (const char **)argv, long_options, 0);
    
    poptSetOtherOptionHelp(pc, "");
    
    while ((opt = poptGetNextOpt(pc)) != -1) {
        printf("Got option %d = %c\n", opt, opt);
        switch (opt) {
        }
    }

    /* Allocate a new context */
    context = smbc_new_context();
    if (!context) {
        printf("Could not allocate new smbc context\n");
        return 1;
    }
        
    /* If we're scanning, do no requests for authentication data */
    if (scan) {
        no_auth = 1;
    }

    /* Set mandatory options (is that a contradiction in terms?) */
    smbc_setDebug(context, debug);
    if (context_auth) {
        smbc_setFunctionAuthDataWithContext(context,
                                            get_auth_data_with_context_fn);
        smbc_setOptionUserData(context, (void *)"hello world");
    } else {
        smbc_setFunctionAuthData(context, get_auth_data_fn);
    }

    smbc_setOptionUseKerberos(context, 1);
    smbc_setOptionFallbackAfterKerberos(context, 1);

    /* If we've been asked to log to stderr instead of stdout, ... */
    if (debug_stderr) {
        /* ... then set the option to do so */
        smbc_setOptionDebugToStderr(context, 1);
    }

    /* Initialize the context using the previously specified options */
    if (!smbc_init_context(context)) {
        smbc_free_context(context, 0);
        printf("Could not initialize smbc context\n");
        return 1;
    }

    /* Tell the compatibility layer to use this context */
    smbc_set_context(context);

    if (scan)
    {
        for (;
             iterations == -1 || iterations > 0;
             iterations = (iterations == -1 ? iterations : --iterations))
        {
            snprintf(buf, sizeof(buf), "smb://");
            browse(buf, scan, 0);
        }
    }
    else
    {
        for (;
             iterations == -1 || iterations > 0;
             iterations = (iterations == -1 ? iterations : --iterations))
        {
            fputs("url: ", stdout);
            p = fgets(buf, sizeof(buf), stdin);
            if (! p)
            {
                break;
            }

            if ((p = strchr(buf, '\n')) != NULL)
            {
                *p = '\0';
            }
            
            browse(buf, scan, 0);
        }
    }

    exit(0);
}