Esempio n. 1
0
/*
  process modifies for one file
*/
static int process_file(struct ldb_context *ldb, FILE *f, int *count)
{
	struct ldb_ldif *ldif;
	int ret = LDB_SUCCESS;
	
	while ((ldif = ldb_ldif_read_file(ldb, f))) {
		switch (ldif->changetype) {
		case LDB_CHANGETYPE_NONE:
		case LDB_CHANGETYPE_ADD:
			ret = ldb_add(ldb, ldif->msg);
			break;
		case LDB_CHANGETYPE_DELETE:
			ret = ldb_delete(ldb, ldif->msg->dn);
			break;
		case LDB_CHANGETYPE_MODIFY:
			ret = ldb_modify(ldb, ldif->msg);
			break;
		}
		if (ret != LDB_SUCCESS) {
			fprintf(stderr, "ERR: \"%s\" on DN %s\n", 
				ldb_errstring(ldb), ldb_dn_get_linearized(ldif->msg->dn));
			failures++;
		} else {
			(*count)++;
		}
		ldb_ldif_read_free(ldb, ldif);
	}

	return ret;
}
Esempio n. 2
0
/*
  add records from an opened file
*/
static int process_file(struct ldb_context *ldb, FILE *f, int *count,
                        int *failures)
{
    struct ldb_ldif *ldif;
    int ret = LDB_SUCCESS;

    while ((ldif = ldb_ldif_read_file(ldb, f))) {
        if (ldif->changetype != LDB_CHANGETYPE_ADD &&
                ldif->changetype != LDB_CHANGETYPE_NONE) {
            fprintf(stderr, "Only CHANGETYPE_ADD records allowed\n");
            break;
        }

        ldif->msg = ldb_msg_canonicalize(ldb, ldif->msg);

        ret = ldb_add(ldb, ldif->msg);
        if (ret != LDB_SUCCESS) {
            fprintf(stderr, "ERR: \"%s\" on DN %s\n",
                    ldb_errstring(ldb), ldb_dn_linearize(ldb, ldif->msg->dn));
            (*failures)++;
        } else {
            (*count)++;
        }
        ldb_ldif_read_free(ldb, ldif);
    }

    return ret;
}
Esempio n. 3
0
} END_TEST

// ^ Unit test ----------------------------------------------------------------

// v Suite definition ---------------------------------------------------------

static void create_ldb_from_ldif(const char *ldb_path, const char *ldif_path,
				 const char *default_context, const char *root_context)
{
	FILE *f;
	struct ldb_ldif *ldif;
	struct ldb_context *ldb_ctx = NULL;
	TALLOC_CTX *local_mem_ctx = talloc_new(NULL);
	struct ldb_message *msg;

	ldb_ctx = ldb_init(local_mem_ctx, NULL);
	ldb_connect(ldb_ctx, ldb_path, 0, 0);
	f = fopen(ldif_path, "r");

	msg = ldb_msg_new(local_mem_ctx);
	msg->dn = ldb_dn_new(msg, ldb_ctx, "@INDEXLIST");
	ldb_msg_add_string(msg, "@IDXATTR", "cn");
	ldb_msg_add_string(msg, "@IDXATTR", "oleguid");
	ldb_msg_add_string(msg, "@IDXATTR", "mappedId");
	msg->elements[0].flags = LDB_FLAG_MOD_ADD;
	ldb_add(ldb_ctx, msg);

	while ((ldif = ldb_ldif_read_file(ldb_ctx, f))) {
		struct ldb_message *normalized_msg;
		ldb_msg_normalize(ldb_ctx, local_mem_ctx, ldif->msg,
				  &normalized_msg);
		ldb_add(ldb_ctx, normalized_msg);
		talloc_free(normalized_msg);
		ldb_ldif_read_free(ldb_ctx, ldif);
	}

	if (default_context && root_context) {
		msg = ldb_msg_new(local_mem_ctx);
		msg->dn = ldb_dn_new(msg, ldb_ctx, "@ROOTDSE");
		ldb_msg_add_string(msg, "defaultNamingContext", default_context);
		ldb_msg_add_string(msg, "rootDomainNamingContext", root_context);
		msg->elements[0].flags = LDB_FLAG_MOD_REPLACE;
		ldb_add(ldb_ctx, msg);
	}

	fclose(f);
	talloc_free(local_mem_ctx);
}
Esempio n. 4
0
int main(int argc, const char **argv)
{
	struct ldb_context *ldb;
	FILE *fileStream;
	struct ldb_ldif *ldifMsg;

	if (argc != 2) {
		printf("Usage %s filename.ldif\n", argv[0]);
		exit(1);
	}

	/*
	  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);

	fileStream = fopen(argv[1], "r");
	if (0 == fileStream) {
		perror(argv[1]);
		exit(1);
	}

	/*
	  We now work through the filestream to get each entry.
	*/
	while ( (ldifMsg = ldb_ldif_read_file(ldb, fileStream)) ) {
		/*
		  Each message has a particular change type. For Add,
		  Modify and Delete, this will also appear in the
		  output listing (as changetype: add, changetype:
		  modify or changetype:delete, respectively).
		*/
		switch (ldifMsg->changetype) {
		case LDB_CHANGETYPE_NONE:
			printf("ChangeType: None\n");
			break;
		case LDB_CHANGETYPE_ADD:
			printf("ChangeType: Add\n");
			break;
		case LDB_CHANGETYPE_MODIFY:
			printf("ChangeType: Modify\n");
			break;
		case LDB_CHANGETYPE_DELETE:
			printf("ChangeType: Delete\n");
			break;
		default:
			printf("ChangeType: Unknown\n");
		}

		/*
		  We can now write out the results, using our custom
		  output routine as defined at the top of this file. 
		*/
		ldb_ldif_write(ldb, vprintf_fn, NULL, ldifMsg);

		/*
		  Clean up the message
		*/
		ldb_ldif_read_free(ldb, ldifMsg);
	}

	/*
	  Clean up the context
	*/
	talloc_free(ldb);

	return 0;
}
/**
   \details Initialize the named properties database or return pointer
   to the existing one if already initialized/opened.

   \param mem_ctx pointer to the memory context
   \param ldb_ctx pointer on pointer to the ldb context the function
   returns

   \return MAPISTORE_SUCCESS on success, otherwise MAPISTORE error
 */
enum mapistore_error mapistore_namedprops_init(TALLOC_CTX *mem_ctx, struct ldb_context **_ldb_ctx)
{
	int			ret;
	struct stat		sb;
	struct ldb_context	*ldb_ctx = NULL;
	struct ldb_ldif		*ldif;
	char			*filename;
	FILE			*f;
	struct tevent_context	*ev;
	char			*database;

	/* Sanity checks */
	MAPISTORE_RETVAL_IF(!mem_ctx, MAPISTORE_ERR_INVALID_PARAMETER, NULL);
	MAPISTORE_RETVAL_IF(!_ldb_ctx, MAPISTORE_ERR_INVALID_PARAMETER, NULL);

	ev = tevent_context_init(mem_ctx);
	MAPISTORE_RETVAL_IF(!ev, MAPISTORE_ERR_NO_MEMORY, NULL);

	database = talloc_asprintf(mem_ctx, "%s/%s", mapistore_get_mapping_path(), MAPISTORE_DB_NAMED);
	DEBUG(0, ("database = %s\n", database));

	/* Step 1. Stat the database and populate it if it doesn't exist */
	if (stat(database, &sb) == -1) {
		ldb_ctx = mapistore_ldb_wrap_connect(ldb_ctx, ev, database, 0);
		talloc_free(database);
		MAPISTORE_RETVAL_IF(!ldb_ctx, MAPISTORE_ERR_DATABASE_INIT, NULL);

		filename = talloc_asprintf(mem_ctx, "%s/mapistore_namedprops.ldif", 
					   mapistore_namedprops_get_ldif_path());
		f = fopen(filename, "r");
		talloc_free(filename);
		MAPISTORE_RETVAL_IF(!f, MAPISTORE_ERROR, NULL);
		
		ldb_transaction_start(ldb_ctx);

		while ((ldif = ldb_ldif_read_file(ldb_ctx, f))) {
			struct ldb_message *normalized_msg;
			ret = ldb_msg_normalize(ldb_ctx, mem_ctx, ldif->msg, &normalized_msg);
			MAPISTORE_RETVAL_IF(ret, MAPISTORE_ERR_DATABASE_INIT, NULL);
			ret = ldb_add(ldb_ctx, normalized_msg);
			talloc_free(normalized_msg);
			if (ret != LDB_SUCCESS) {
				fclose(f);
				MAPISTORE_RETVAL_IF(ret, MAPISTORE_ERR_DATABASE_INIT, NULL);
			}
			ldb_ldif_read_free(ldb_ctx, ldif);
		}

		ldb_transaction_commit(ldb_ctx);
		fclose(f);

	} else {
		ldb_ctx = mapistore_ldb_wrap_connect(ldb_ctx, ev, database, 0);
		talloc_free(database);
		MAPISTORE_RETVAL_IF(!ldb_ctx, MAPISTORE_ERR_DATABASE_INIT, NULL);
	}

	*_ldb_ctx = ldb_ctx;

	return MAPISTORE_SUCCESS;
}
Esempio n. 6
0
/**
  \details Initialize the database and provision it

  \param conn pointer to the MySQL context
  \param schema_path pointer to the path holding schema files

  \return MAPISTORE_SUCCESS on success, otherwise MAPISTORE error
 */
static enum mapistore_error initialize_database(MYSQL *conn, const char *schema_path)
{
	TALLOC_CTX		*mem_ctx;
	enum mapistore_error	retval = MAPISTORE_SUCCESS;
	struct ldb_context	*ldb_ctx;
	struct ldb_ldif		*ldif;
	struct ldb_message	*msg;
	int			ret;
	char			*filename;
	FILE			*f;
	bool			inserted;
	bool			schema_created;

	/* Sanity checks */
	MAPISTORE_RETVAL_IF(!conn, MAPISTORE_ERR_DATABASE_INIT, NULL);

	mem_ctx = talloc_named(NULL, 0, "initialize_database");
	MAPISTORE_RETVAL_IF(!mem_ctx, MAPISTORE_ERR_NO_MEMORY, NULL);

	filename = talloc_asprintf(mem_ctx, "%s/" NAMEDPROPS_MYSQL_SCHEMA,
				   schema_path ? schema_path : mapistore_namedprops_get_ldif_path());
	MAPISTORE_RETVAL_IF(!filename, MAPISTORE_ERR_NO_MEMORY, mem_ctx);
	schema_created = create_schema(conn, filename);
	if (!schema_created) {
		DEBUG(1, ("Failed named properties schema creation, "
			  "last mysql error was: `%s`\n", mysql_error(conn)));
		MAPISTORE_RETVAL_ERR(MAPISTORE_ERR_DATABASE_INIT, mem_ctx);
	}

	ldb_ctx = ldb_init(mem_ctx, NULL);
	MAPISTORE_RETVAL_IF(!ldb_ctx, MAPISTORE_ERR_BACKEND_INIT, mem_ctx);

	filename = talloc_asprintf(mem_ctx, "%s/mapistore_namedprops.ldif",
				   schema_path ? schema_path : mapistore_namedprops_get_ldif_path());
	MAPISTORE_RETVAL_IF(!filename, MAPISTORE_ERR_NO_MEMORY, mem_ctx);

	f = fopen(filename, "r");
	talloc_free(filename);
	MAPISTORE_RETVAL_IF(!f, MAPISTORE_ERR_BACKEND_INIT, mem_ctx);
	
	while ((ldif = ldb_ldif_read_file(ldb_ctx, f))) {
		ret = ldb_msg_normalize(ldb_ctx, mem_ctx, ldif->msg, &msg);
		if (ret) {
			retval = MAPISTORE_ERR_DATABASE_INIT;
			mapistore_set_errno(MAPISTORE_ERR_DATABASE_INIT);
			goto end;
		}

		inserted = insert_ldif_msg(conn, msg);
		ldb_ldif_read_free(ldb_ctx, ldif);
		if (!inserted) {
			retval = MAPISTORE_ERR_DATABASE_OPS;
			mapistore_set_errno(MAPISTORE_ERR_DATABASE_OPS);
			goto end;
		}
	}

end:
	talloc_free(mem_ctx);
	fclose(f);

	return retval;
}