예제 #1
0
int main(int argc, const char **argv)
{
	struct ldb_context *ldb;
	const char *expression = "(dn=*)";
	struct ldb_result *resultMsg;
	int i;

	/*
	  This is the always the first thing you want to do in an LDB
	  application - initialise up the context structure.

	  Note that you can use the context structure as a parent
	  for talloc allocations as well
	*/
	ldb = ldb_init(NULL);

	/*
	  We now open the database. In this example we just hard code the connection path.

	  Also note that the database is being opened read-only. This means that the 
	  call will fail unless the database already exists. 
	*/
	if (LDB_SUCCESS != ldb_connect(ldb, "tdb://tdbtest.ldb", LDB_FLG_RDONLY, NULL) ){
		printf("Problem on connection\n");
		exit(-1);
	}

	/*
	  At this stage we have an open database, and can start using it. It is opened
	  read-only, so a query is possible. 

	  We construct a search that just returns all the (sensible) contents. You can do
	  quite fine grained results with the LDAP search syntax, however it is a bit
	  confusing to start with. See RFC2254.
	*/
	if (LDB_SUCCESS != ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT,
				      expression, NULL, &resultMsg) ) {
		printf("Problem in search\n");
		exit(-1);
	}
	
	printf("%i records returned\n", resultMsg->count);

	/*
	  We can now iterate through the results, writing them out
	  (to standard output) with our custom output routine as defined
	  at the top of this file
	*/
	for (i = 0; i < resultMsg->count; ++i) {
		struct ldb_ldif ldifMsg;

		printf("Message: %i\n", i+1);
		
		ldifMsg.changetype = LDB_CHANGETYPE_NONE;
		ldifMsg.msg = resultMsg->msgs[i];
		ldb_ldif_write(ldb, vprintf_fn, NULL, &ldifMsg);
	}

	/*
	  There are two objects to clean up - the result from the 
	  ldb_search() query, and the original ldb context.
	*/
	talloc_free(resultMsg);

	talloc_free(ldb);

	return 0;
}
예제 #2
0
파일: cmdline.c 프로젝트: 0x24bin/winexe-1
/**
  process command line options
*/
struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, 
					int argc, const char **argv,
					void (*usage)(void))
{
	struct ldb_cmdline *ret=NULL;
	poptContext pc;
#if (_SAMBA_BUILD_ >= 4)
	int r;
#endif
	int num_options = 0;
	int opt;
	int flags = 0;

#if (_SAMBA_BUILD_ >= 4)
	r = ldb_register_samba_handlers(ldb);
	if (r != 0) {
		goto failed;
	}

#endif

	/* make the ldb utilities line buffered */
	setlinebuf(stdout);

	ret = talloc_zero(ldb, struct ldb_cmdline);
	if (ret == NULL) {
		fprintf(stderr, "Out of memory!\n");
		goto failed;
	}

	options = *ret;
	
	/* pull in URL */
	options.url = getenv("LDB_URL");

	/* and editor (used by ldbedit) */
	options.editor = getenv("VISUAL");
	if (!options.editor) {
		options.editor = getenv("EDITOR");
	}
	if (!options.editor) {
		options.editor = "vi";
	}

	options.scope = LDB_SCOPE_DEFAULT;

	pc = poptGetContext(argv[0], argc, argv, popt_options, 
			    POPT_CONTEXT_KEEP_FIRST);

	while((opt = poptGetNextOpt(pc)) != -1) {
		switch (opt) {
		case 's': {
			const char *arg = poptGetOptArg(pc);
			if (strcmp(arg, "base") == 0) {
				options.scope = LDB_SCOPE_BASE;
			} else if (strcmp(arg, "sub") == 0) {
				options.scope = LDB_SCOPE_SUBTREE;
			} else if (strcmp(arg, "one") == 0) {
				options.scope = LDB_SCOPE_ONELEVEL;
			} else {
				fprintf(stderr, "Invalid scope '%s'\n", arg);
				goto failed;
			}
			break;
		}

		case 'v':
			options.verbose++;
			break;

		case 'o':
			options.options = talloc_realloc(ret, options.options, 
							 const char *, num_options+3);
			if (options.options == NULL) {
				fprintf(stderr, "Out of memory!\n");
				goto failed;
			}
			options.options[num_options] = poptGetOptArg(pc);
			options.options[num_options+1] = NULL;
			num_options++;
			break;

		case 'c': {
			const char *cs = poptGetOptArg(pc);
			const char *p;

			for (p = cs; p != NULL; ) {
				const char *t, *c;

				t = strchr(p, ',');
				if (t == NULL) {
					c = talloc_strdup(options.controls, p);
					p = NULL;
				} else {
					c = talloc_strndup(options.controls, p, t-p);
			        	p = t + 1;
				}
				if (c == NULL || !add_control(ret, c)) {
					fprintf(stderr, __location__ ": out of memory\n");
					goto failed;
				}
			}

			break;	  
		}
		case 'P':
			if (!add_control(ret, "paged_results:1:1024")) {
				fprintf(stderr, __location__ ": out of memory\n");
				goto failed;
			}
			break;
		case 'D':
			if (!add_control(ret, "show_deleted:1")) {
				fprintf(stderr, __location__ ": out of memory\n");
				goto failed;
			}
			break;
		case 'R':
			if (!add_control(ret, "show_recycled:0")) {
				fprintf(stderr, __location__ ": out of memory\n");
				goto failed;
			}
			break;
		case 'd':
			if (!add_control(ret, "show_deactivated_link:0")) {
				fprintf(stderr, __location__ ": out of memory\n");
				goto failed;
			}
			break;
		case 'r':
			if (!add_control(ret, "reveal_internals:0")) {
				fprintf(stderr, __location__ ": out of memory\n");
				goto failed;
			}
			break;
		case 'N':
			if (!add_control(ret, "search_options:1:2")) {
				fprintf(stderr, __location__ ": out of memory\n");
				goto failed;
			}
			break;
		case 'E':
			if (!add_control(ret, "extended_dn:1:1")) {
				fprintf(stderr, __location__ ": out of memory\n");
				goto failed;
			}
			break;
		default:
			fprintf(stderr, "Invalid option %s: %s\n", 
				poptBadOption(pc, 0), poptStrerror(opt));
			if (usage) usage();
			goto failed;
		}
	}

	/* setup the remaining options for the main program to use */
	options.argv = poptGetArgs(pc);
	if (options.argv) {
		options.argv++;
		while (options.argv[options.argc]) options.argc++;
	}

	*ret = options;

	/* all utils need some option */
	if (ret->url == NULL) {
		fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
		if (usage) usage();
		goto failed;
	}

	if (strcmp(ret->url, "NONE") == 0) {
		return ret;
	}

	if (options.nosync) {
		flags |= LDB_FLG_NOSYNC;
	}

	if (options.show_binary) {
		flags |= LDB_FLG_SHOW_BINARY;
	}

	if (options.tracing) {
		flags |= LDB_FLG_ENABLE_TRACING;
	}

#if (_SAMBA_BUILD_ >= 4)
	/* Must be after we have processed command line options */
	gensec_init(cmdline_lp_ctx); 
	
	if (ldb_set_opaque(ldb, "sessionInfo", system_session(cmdline_lp_ctx))) {
		goto failed;
	}
	if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
		goto failed;
	}
	if (ldb_set_opaque(ldb, "loadparm", cmdline_lp_ctx)) {
		goto failed;
	}

	ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
#endif

	if (options.modules_path != NULL) {
		ldb_set_modules_dir(ldb, options.modules_path);
	} else if (getenv("LDB_MODULES_PATH") != NULL) {
		ldb_set_modules_dir(ldb, getenv("LDB_MODULES_PATH"));
	}

	/* now connect to the ldb */
	if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
		fprintf(stderr, "Failed to connect to %s - %s\n", 
			ret->url, ldb_errstring(ldb));
		goto failed;
	}

	return ret;

failed:
	talloc_free(ret);
	exit(1);
	return NULL;
}
예제 #3
0
/*
  connect to the group mapping ldb
*/
static bool init_group_mapping(void)
{
	bool existed;
	const char *init_ldif[] = 
		{ "dn: @ATTRIBUTES\n" \
		  "ntName: CASE_INSENSITIVE\n" \
		  "\n",
		  "dn: @INDEXLIST\n" \
		  "@IDXATTR: gidNumber\n" \
		  "@IDXATTR: ntName\n" \
		  "@IDXATTR: member\n" };
	const char *db_path, *tdb_path;
	int ret;
	int flags = 0;

	if (ldb != NULL) {
		return True;
	}

	/* this is needed as Samba3 doesn't have this globally yet */
	ldb_global_init();

	db_path = state_path("group_mapping.ldb");

	ldb = ldb_init(NULL);
	if (ldb == NULL) goto failed;

	/* Ensure this db is created read/write for root only. */
	ldb_set_create_perms(ldb, 0600);

	existed = file_exist(db_path, NULL);

	if (lp_parm_bool(-1, "groupmap", "nosync", False)) {
		flags |= LDB_FLG_NOSYNC;
	}

	if (!lp_use_mmap()) {
		flags |= LDB_FLG_NOMMAP;
	}

	ret = ldb_connect(ldb, db_path, flags, NULL);
	if (ret != LDB_SUCCESS) {
		goto failed;
	}

	/* force the permissions on the ldb to 0600 - this will fix
	   existing databases as well as new ones */
	if (chmod(db_path, 0600) != 0) {
		goto failed;
	}

	if (!existed) {
		/* initialise the ldb with an index */
		struct ldb_ldif *ldif;
		int i;
		for (i=0;i<ARRAY_SIZE(init_ldif);i++) {
			ldif = ldb_ldif_read_string(ldb, &init_ldif[i]);
			if (ldif == NULL) goto failed;
			ret = ldb_add(ldb, ldif->msg);
			talloc_free(ldif);
			if (ret == -1) goto failed;
		}
	}

	/* possibly upgrade */
	tdb_path = state_path("group_mapping.tdb");
	if (file_exist(tdb_path, NULL) && !mapping_upgrade(tdb_path)) {
		unlink(state_path("group_mapping.ldb"));
		goto failed;
	}

	return True;

failed:
	DEBUG(0,("Failed to open group mapping ldb '%s' - '%s'\n",
		 db_path, ldb?ldb_errstring(ldb):strerror(errno)));
	talloc_free(ldb);
	ldb = NULL;
	return False;
}
예제 #4
0
static NTSTATUS torture_leave_ads_domain(struct torture_context *torture,
					 TALLOC_CTX *mem_ctx,
					 struct libnet_JoinDomain *libnet_r)
{
	int rtn;
	TALLOC_CTX *tmp_ctx;

	struct ldb_dn *server_dn;
	struct ldb_context *ldb_ctx;

	char *remote_ldb_url; 
	 
	/* Check if we are a domain controller. If not, exit. */
	if (!libnet_r->out.server_dn_str) {
		return NT_STATUS_OK;
	}

	tmp_ctx = talloc_named(mem_ctx, 0, "torture_leave temporary context");
	if (!tmp_ctx) {
		libnet_r->out.error_string = NULL;
		return NT_STATUS_NO_MEMORY;
	}

	ldb_ctx = ldb_init(tmp_ctx, torture->ev);
	if (!ldb_ctx) {
		libnet_r->out.error_string = NULL;
		talloc_free(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	/* Remove CN=Servers,... entry from the AD. */ 
	server_dn = ldb_dn_new(tmp_ctx, ldb_ctx, libnet_r->out.server_dn_str);
	if (! ldb_dn_validate(server_dn)) {
		libnet_r->out.error_string = NULL;
		talloc_free(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	remote_ldb_url = talloc_asprintf(tmp_ctx, "ldap://%s", libnet_r->out.samr_binding->host);
	if (!remote_ldb_url) {
		libnet_r->out.error_string = NULL;
		talloc_free(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	ldb_set_opaque(ldb_ctx, "credentials", cmdline_credentials);
	ldb_set_opaque(ldb_ctx, "loadparm", cmdline_lp_ctx);

	rtn = ldb_connect(ldb_ctx, remote_ldb_url, 0, NULL);
	if (rtn != LDB_SUCCESS) {
		libnet_r->out.error_string = NULL;
		talloc_free(tmp_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}

	rtn = ldb_delete(ldb_ctx, server_dn);
	if (rtn != LDB_SUCCESS) {
		libnet_r->out.error_string = NULL;
		talloc_free(tmp_ctx);
		return NT_STATUS_UNSUCCESSFUL;
	}

	DEBUG(0, ("%s removed successfully.\n", libnet_r->out.server_dn_str));

	talloc_free(tmp_ctx); 
	return NT_STATUS_OK;
}