static struct tdb_wrap_private *tdb_wrap_private_open(TALLOC_CTX *mem_ctx, const char *name, int hash_size, int tdb_flags, int open_flags, mode_t mode) { struct tdb_wrap_private *result; result = talloc(mem_ctx, struct tdb_wrap_private); if (result == NULL) { return NULL; } result->name = talloc_strdup(result, name); if (result->name == NULL) { goto fail; } #if _SAMBA_BUILD_ == 3 /* This #if _SAMBA_BUILD == 3 is very unfortunate, as it means * that in the top level build, these options are not * available for these databases. However, having two * different tdb_wrap lists is a worse fate, so this will do * for now */ if (!lp_use_mmap()) { tdb_flags |= TDB_NOMMAP; } if ((hash_size == 0) && (name != NULL)) { const char *base; base = strrchr_m(name, '/'); if (base != NULL) { base += 1; } else { base = name; } hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0); } #endif result->tdb = tdb_open_compat(name, hash_size, tdb_flags, open_flags, mode, tdb_wrap_log, NULL); if (result->tdb == NULL) { goto fail; } talloc_set_destructor(result, tdb_wrap_private_destructor); DLIST_ADD(tdb_list, result); return result; fail: TALLOC_FREE(result); return NULL; }
/** * open a database */ struct db_context *db_open(TALLOC_CTX *mem_ctx, const char *name, int hash_size, int tdb_flags, int open_flags, mode_t mode, enum dbwrap_lock_order lock_order, uint64_t dbwrap_flags) { struct db_context *result = NULL; const char *sockname; if (!DBWRAP_LOCK_ORDER_VALID(lock_order)) { errno = EINVAL; return NULL; } if (tdb_flags & TDB_CLEAR_IF_FIRST) { const char *base; bool try_readonly = false; base = strrchr_m(name, '/'); if (base != NULL) { base += 1; } else { base = name; } if (dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS) { try_readonly = true; } try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", "*", try_readonly); try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", base, try_readonly); if (try_readonly) { dbwrap_flags |= DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS; } else { dbwrap_flags &= ~DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS; } } if (tdb_flags & TDB_CLEAR_IF_FIRST) { const char *base; bool try_mutex = true; bool require_mutex = false; base = strrchr_m(name, '/'); if (base != NULL) { base += 1; } else { base = name; } try_mutex = lp_parm_bool(-1, "dbwrap_tdb_mutexes", "*", try_mutex); try_mutex = lp_parm_bool(-1, "dbwrap_tdb_mutexes", base, try_mutex); if (!lp_use_mmap()) { /* * Mutexes require mmap. "use mmap = no" can * be a debugging tool, so let it override the * mutex parameters */ try_mutex = false; } if (try_mutex && tdb_runtime_check_for_robust_mutexes()) { tdb_flags |= TDB_MUTEX_LOCKING; } require_mutex = lp_parm_bool(-1, "dbwrap_tdb_require_mutexes", "*", require_mutex); require_mutex = lp_parm_bool(-1, "dbwrap_tdb_require_mutexes", base, require_mutex); if (require_mutex) { tdb_flags |= TDB_MUTEX_LOCKING; } } sockname = lp_ctdbd_socket(); if (lp_clustering()) { const char *partname; if (!socket_exist(sockname)) { DEBUG(1, ("ctdb socket does not exist - is ctdb not " "running?\n")); return NULL; } /* ctdb only wants the file part of the name */ partname = strrchr(name, '/'); if (partname) { partname++; } else { partname = name; } /* allow ctdb for individual databases to be disabled */ if (lp_parm_bool(-1, "ctdb", partname, True)) { struct messaging_context *msg_ctx; struct ctdbd_connection *conn; conn = messaging_ctdb_connection(); if (conn == NULL) { DBG_WARNING("No ctdb connection\n"); errno = EIO; return NULL; } msg_ctx = server_messaging_context(); result = db_open_ctdb(mem_ctx, msg_ctx, partname, hash_size, tdb_flags, open_flags, mode, lock_order, dbwrap_flags); if (result == NULL) { DEBUG(0,("failed to attach to ctdb %s\n", partname)); if (errno == 0) { errno = EIO; } return NULL; } } } if (result == NULL) { struct loadparm_context *lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers()); if (hash_size == 0) { hash_size = lpcfg_tdb_hash_size(lp_ctx, name); } tdb_flags = lpcfg_tdb_flags(lp_ctx, tdb_flags); result = dbwrap_local_open( mem_ctx, name, hash_size, tdb_flags, open_flags, mode, lock_order, dbwrap_flags); talloc_unlink(mem_ctx, lp_ctx); } return result; }
/* 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; }