From c9d3763bfc92b7e99565065fb44b03c47f1f1a0d Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Thu, 25 Aug 2005 22:29:55 +0000 Subject: [PATCH] r9623: samba3dump now generates LDIF for the registry hives from registry.tdb couple of other small fixes (This used to be commit 06bc5d8e0950dd85b6f26a1355fa0a2ae178d30f) --- source4/lib/samba3/PLAN | 5 +- source4/lib/samba3/config.mk | 3 +- source4/lib/samba3/samba3.h | 1 - source4/lib/samba3/samba3dump.c | 116 +++++++++++++++++++++++++++-- source4/lib/samba3/tdbsam.c | 74 +++++++----------- source4/lib/samba3/upgrade.c | 128 ++++++++++++++++++++++++++++++++ source4/lib/samba3/winsdb.c | 4 +- 7 files changed, 273 insertions(+), 58 deletions(-) create mode 100644 source4/lib/samba3/upgrade.c diff --git a/source4/lib/samba3/PLAN b/source4/lib/samba3/PLAN index 6076141ac0c..26bf54634ca 100644 --- a/source4/lib/samba3/PLAN +++ b/source4/lib/samba3/PLAN @@ -1,9 +1,12 @@ Three possible viable approaches: 1) TDB conversion approach. Read in TDB dump out LDIF (one-way) - - samr.ldb: from tdbsam/smbpasswd, account_policy.tdb, secrets.tdb, group_mapping.tdb, idmap.tdb, privilege.tdb + - samr.ldb: from tdbsam/smbpasswd, account_policy.tdb, secrets.tdb, group_mapping.tdb, privilege.tdb - registry.ldb: from registry.tdb - wins.ldif: from wins.tdb/wins.dat - smb.conf/ea's: generated from the old smb.conf + share_info.tdb + - winbind.ldif: from winbindd_idmap.tdb (custom file format, not used + by samba4 yet as it doesn't + have Winbind yet) (one-way upgrades can be done by using ldbsearch -a on these dynamically generated ldb's) diff --git a/source4/lib/samba3/config.mk b/source4/lib/samba3/config.mk index 79142e4f67e..eba694c2af6 100644 --- a/source4/lib/samba3/config.mk +++ b/source4/lib/samba3/config.mk @@ -12,7 +12,8 @@ ADD_OBJ_FILES = \ lib/samba3/registry.o \ lib/samba3/secrets.o \ lib/samba3/ldb_samba3.o \ - lib/samba3/share_info.o + lib/samba3/share_info.o \ + lib/samba3/upgrade.o # End SUBSYSTEM LIBSAMBA3 ################################################ diff --git a/source4/lib/samba3/samba3.h b/source4/lib/samba3/samba3.h index fe4db560a85..5aba790c4ea 100644 --- a/source4/lib/samba3/samba3.h +++ b/source4/lib/samba3/samba3.h @@ -52,7 +52,6 @@ struct samba3_samaccount { }; struct samba3_groupmapping { - struct pdb_methods *methods; gid_t gid; struct dom_sid *sid; int sid_name_use; diff --git a/source4/lib/samba3/samba3dump.c b/source4/lib/samba3/samba3dump.c index b00074be56e..a6166a1c6b8 100644 --- a/source4/lib/samba3/samba3dump.c +++ b/source4/lib/samba3/samba3dump.c @@ -22,6 +22,7 @@ #include "includes.h" #include "lib/samba3/samba3.h" #include "lib/cmdline/popt_common.h" +#include "lib/ldb/include/ldb.h" static void print_header(const char *txt) { @@ -51,10 +52,10 @@ static NTSTATUS print_samba3_policy(struct samba3_policy *ret) static NTSTATUS print_samba3_sam(struct samba3 *samba3) { struct samba3_samaccount *accounts = samba3->samaccounts; - uint32_t count = samba3->samaccount_count, i; + uint32_t i; print_header("SAM Database"); - for (i = 0; i < count; i++) { + for (i = 0; i < samba3->samaccount_count; i++) { printf("%d: %s\n", accounts[i].user_rid, accounts[i].username); } @@ -85,9 +86,13 @@ static NTSTATUS print_samba3_secrets(struct samba3_secrets *secrets) print_header("Secrets"); printf("IPC Credentials:\n"); - printf(" User: %s\n", cli_credentials_get_username(secrets->ipc_cred)); - printf(" Password: %s\n", cli_credentials_get_password(secrets->ipc_cred)); - printf(" Domain: %s\n\n", cli_credentials_get_domain(secrets->ipc_cred)); + if (secrets->ipc_cred->username_obtained) + printf(" User: %s\n", cli_credentials_get_username(secrets->ipc_cred)); + if (secrets->ipc_cred->password_obtained) + printf(" Password: %s\n", cli_credentials_get_password(secrets->ipc_cred)); + + if (secrets->ipc_cred->domain_obtained) + printf(" Domain: %s\n\n", cli_credentials_get_domain(secrets->ipc_cred)); printf("LDAP passwords:\n"); for (i = 0; i < secrets->ldappw_count; i++) { @@ -150,6 +155,50 @@ static NTSTATUS print_samba3_winsdb(struct samba3 *samba3) return NT_STATUS_OK; } +static NTSTATUS print_samba3_groupdb(struct samba3_groupdb *db) +{ + int i; + print_header("Group Mappings"); + + for (i = 0; i < db->groupmap_count; i++) + { + printf("\t--- Group: %s ---\n", db->groupmappings[i].nt_name); + printf("\tComment: %s\n", db->groupmappings[i].comment); + printf("\tGID: %d\n", db->groupmappings[i].gid); + printf("\tSID Name Use: %d\n", db->groupmappings[i].sid_name_use); + printf("\tSID: %s\n\n", dom_sid_string(NULL, db->groupmappings[i].sid)); + } + + for (i = 0; i < db->alias_count; i++) + { + int j; + printf("\t--- Alias: %s ---\n", dom_sid_string(NULL, db->aliases[i].sid)); + for (j = 0; j < db->aliases[i].member_count; j++) { + printf("\t%s\n", dom_sid_string(NULL,db->aliases[i].members[j])); + } + } + + return NT_STATUS_OK; +} + +static NTSTATUS print_samba3_idmapdb(struct samba3_idmapdb *db) +{ + int i; + print_header("Winbindd SID<->GID/UID mappings"); + + printf("User High Water Mark: %d\n", db->user_hwm); + printf("Group High Water Mark: %d\n\n", db->group_hwm); + + for (i = 0; i < db->mapping_count; i++) { + printf("%s -> %cID %d", + dom_sid_string(NULL, db->mappings[i].sid), + (db->mappings[i].type == IDMAP_GROUP)?'G':'U', + db->mappings[i].unix_id); + } + + return NT_STATUS_OK; +} + static NTSTATUS print_samba3(struct samba3 *samba3) { print_samba3_sam(samba3); @@ -158,9 +207,38 @@ static NTSTATUS print_samba3(struct samba3 *samba3) print_samba3_winsdb(samba3); print_samba3_regdb(&samba3->registry); print_samba3_secrets(&samba3->secrets); + print_samba3_groupdb(&samba3->group); + print_samba3_idmapdb(&samba3->idmap); return NT_STATUS_OK; } + +static BOOL write_ldif(const char *fn, struct ldb_message **messages, int count) +{ + FILE *f = fopen(fn, "w+"); + struct ldb_ldif ldif; + int i; + struct ldb_context *ldb = ldb_init(NULL); + + if (!f) { + DEBUG(0, ("Unable to open LDIF file '%s'\n", fn)); + talloc_free(ldb); + return False; + } + + for (i = 0; i < count; i++) { + ldif.changetype = LDB_CHANGETYPE_ADD; + ldif.msg = messages[i]; + + ldb_ldif_write_file(ldb, f, &ldif); + } + + talloc_free(ldb); + + fclose(f); + + return True; +} int main(int argc, char **argv) { @@ -197,7 +275,33 @@ int main(int argc, char **argv) } else if (!strcmp(format, "text")) { print_samba3(samba3); } else if (!strcmp(format, "ldif")) { - printf("FIXME\n"); + struct ldb_message **msgs; + struct ldb_context *ldb = ldb_init(NULL); + int i, ret; + const char *hives[] = { "hklm", "hkcr", "hku", "hkpd", "hkpt", NULL }; + + for (i = 0; hives[i]; i++) { + char *fn; + + ret = samba3_upgrade_registry(&samba3->registry, hives[i], ldb, &msgs); + + printf("Writing %s.ldif\n", hives[i]); + asprintf(&fn, "%s.ldif", hives[i]); + write_ldif(fn, msgs, ret); + SAFE_FREE(fn); + } + + ret = samba3_upgrade_sam(samba3, ldb, &msgs); + printf("Writing sam.ldif\n"); + write_ldif("sam.ldif", msgs, ret); + + ret = samba3_upgrade_winsdb(samba3, ldb, &msgs); + printf("Writing wins.ldif\n"); + write_ldif("wins.ldif", msgs, ret); + + ret = samba3_upgrade_winbind(samba3, ldb, &msgs); + printf("Writing winbind.ldif\n"); + write_ldif("winbind.ldif", msgs, ret); } poptFreeContext(pc); diff --git a/source4/lib/samba3/tdbsam.c b/source4/lib/samba3/tdbsam.c index 577b6633812..23e68717c7d 100644 --- a/source4/lib/samba3/tdbsam.c +++ b/source4/lib/samba3/tdbsam.c @@ -2,11 +2,7 @@ Unix SMB/CIFS implementation. tdb passdb backend format routines - Copyright (C) Andrew Tridgell 1992-1998 Copyright (C) Simo Sorce 2000-2003 - Copyright (C) Gerald Carter 2000 - Copyright (C) Jeremy Allison 2001 - Copyright (C) Andrew Bartlett 2002 Copyright (C) Jelmer Vernooij 2005 This program is free software; you can redistribute it and/or modify @@ -35,41 +31,6 @@ #define TDB_FORMAT_STRING_V2 "dddddddBBBBBBBBBBBBddBBBwwdBwwd" #define TDBSAM_VERSION_STRING "INFO/version" -/** - * Open the TDB passwd database, check version and convert it if needed. - * @param name filename of the tdbsam file. - * @param version version of the tdbsam database - * @return a TDB_CONTEXT handle on the tdbsam file. - **/ - -static TDB_CONTEXT *tdbsam_open (const char *name, int32_t *version) -{ - TDB_CONTEXT *pdb_tdb; - - /* Try to open tdb passwd */ - if (!(pdb_tdb = tdb_open(name, 0, TDB_DEFAULT, - O_RDONLY, 0600))) { - DEBUG(0, ("Unable to open TDB passwd\n")); - return NULL; - } - - /* Check the version */ - *version = tdb_fetch_int32(pdb_tdb, - TDBSAM_VERSION_STRING); - if (*version == -1) - *version = 0; /* Version not found, assume version 0 */ - - /* Compare the version */ - if (*version > 2) { - /* Version more recent than the latest known */ - DEBUG(0, ("TDBSAM version unknown: %d\n", *version)); - tdb_close(pdb_tdb); - pdb_tdb = NULL; - } - - return pdb_tdb; -} - static BOOL init_sam_from_buffer_v0(TDB_CONTEXT *tdb, struct samba3_samaccount *sampass, TDB_DATA buf) { uint32_t username_len, domain_len, nt_username_len, @@ -269,22 +230,37 @@ static BOOL init_sam_from_buffer_v2(TDB_CONTEXT *tdb, struct samba3_samaccount * NTSTATUS samba3_read_tdbsam(const char *filename, TALLOC_CTX *ctx, struct samba3_samaccount **accounts, uint32_t *count) { int32_t version; - TDB_CONTEXT *tdb = tdbsam_open(filename, &version); + TDB_CONTEXT *tdb; TDB_DATA key, val; - if (tdb == NULL) + /* Try to open tdb passwd */ + if (!(tdb = tdb_open(filename, 0, TDB_DEFAULT, O_RDONLY, 0600))) { + DEBUG(0, ("Unable to open TDB passwd file '%s'\n", filename)); return NT_STATUS_UNSUCCESSFUL; + } - if (version < 0 || version > 2) { + /* Check the version */ + version = tdb_fetch_int32(tdb, + TDBSAM_VERSION_STRING); + if (version == -1) + version = 0; /* Version not found, assume version 0 */ + + /* Compare the version */ + if (version > 2) { + /* Version more recent than the latest known */ + DEBUG(0, ("TDBSAM version unknown: %d\n", version)); + tdb_close(tdb); return NT_STATUS_NOT_SUPPORTED; - } + } *accounts = NULL; *count = 0; for (key = tdb_firstkey(tdb); key.dptr; key = tdb_nextkey(tdb, key)) { - if (strncmp(key.dptr, "RID/", 4) == 0) continue; + BOOL ret; + if (strncmp(key.dptr, "USER_", 5) != 0) + continue; val = tdb_fetch(tdb, key); @@ -292,10 +268,14 @@ NTSTATUS samba3_read_tdbsam(const char *filename, TALLOC_CTX *ctx, struct samba3 switch (version) { - case 0: init_sam_from_buffer_v0(tdb, &(*accounts)[*count], val); break; - case 1: init_sam_from_buffer_v1(tdb, &(*accounts)[*count], val); break; - case 2: init_sam_from_buffer_v2(tdb, &(*accounts)[*count], val); break; + case 0: ret = init_sam_from_buffer_v0(tdb, &(*accounts)[*count], val); break; + case 1: ret = init_sam_from_buffer_v1(tdb, &(*accounts)[*count], val); break; + case 2: ret = init_sam_from_buffer_v2(tdb, &(*accounts)[*count], val); break; + + } + if (!ret) { + DEBUG(0, ("Unable to parse SAM account %s\n", key.dptr)); } (*count)++; diff --git a/source4/lib/samba3/upgrade.c b/source4/lib/samba3/upgrade.c new file mode 100644 index 00000000000..907df75c2e7 --- /dev/null +++ b/source4/lib/samba3/upgrade.c @@ -0,0 +1,128 @@ +/* + Unix SMB/CIFS implementation. + Generate ldb_message 's for samba3_* + + Copyright (C) Jelmer Vernooij 2005 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "includes.h" +#include "lib/samba3/samba3.h" +#include "lib/ldb/include/ldb.h" + +static struct ldb_message *msg_array_add(struct ldb_context *ctx, struct ldb_message ***msgs, int *count) +{ + struct ldb_message *ret; + *msgs = talloc_realloc(ctx, *msgs, struct ldb_message *, (*count)+1); + + ret = (*msgs)[*count] = talloc_zero(ctx, struct ldb_message); + (*count)++; + + return ret; +} + +static struct ldb_dn *regkey_to_dn(struct ldb_context *ldb, const char *name) +{ + char *p, *n, *dup; + struct ldb_dn *ret = ldb_dn_explode(ldb, "hive=NONE"); + + p = dup = talloc_strdup(ldb, name); + + while (p) { + n = strchr(p, '/'); + if (n) { *n = '\0'; n++; } + + ret = ldb_dn_build_child(ldb, "key", p, ret); + + p = n; + } + + talloc_free(dup); + + return ret; +} + +/* Where prefix is any of: + * - HKLM + * HKU + * HKCR + * HKPD + * HKPT + */ + +int samba3_upgrade_registry(struct samba3_regdb *regdb, const char *prefix, struct ldb_context *ldb, struct ldb_message ***msgs) +{ + int i; + struct ldb_message *msg; + int count = 0; + char *prefix_up = strupper_talloc(ldb, prefix); + *msgs = NULL; + + for (i = 0; i < regdb->key_count; i++) { + int j; + struct samba3_regkey *rk = ®db->keys[i]; + struct ldb_dn *keydn; + + /* Only handle selected hive */ + if (strncmp(prefix_up, rk->name, strlen(prefix_up)) != 0) { + continue; + } + + msg = msg_array_add(ldb, msgs, &count); + + msg->num_elements = 0; + msg->elements = NULL; + msg->private_data = NULL; + + /* Convert key name to dn */ + keydn = msg->dn = regkey_to_dn(ldb, rk->name); + + ldb_msg_add_string(ldb, msg, "name", strrchr(rk->name, '/')?strrchr(rk->name, '/')+1:rk->name); + + for (j = 0; j < rk->value_count; j++) { + struct samba3_regval *rv = &rk->values[j]; + + msg = msg_array_add(ldb, msgs, &count); + msg->dn = ldb_dn_build_child(ldb, "value", rv->name, keydn); + + ldb_msg_add_string(ldb, msg, "value", rv->name); + ldb_msg_add_fmt(ldb, msg, "type", "%d", rv->type); + ldb_msg_add_value(ldb, msg, "data", &rv->data); + } + } + + talloc_free(prefix_up); + + return count; +} + +int samba3_upgrade_sam(struct samba3 *samba3, struct ldb_context *ctx, struct ldb_message ***msgs) +{ + /* FIXME */ + return -1; +} + +int samba3_upgrade_winbind(struct samba3 *samba3, struct ldb_context *ctx, struct ldb_message ***msgs) +{ + /* FIXME */ + return -1; +} + +int samba3_upgrade_winsdb(struct samba3 *samba3, struct ldb_context *ctx, struct ldb_message ***msgs) +{ + /* FIXME */ + return -1; +} diff --git a/source4/lib/samba3/winsdb.c b/source4/lib/samba3/winsdb.c index c6ead1de39c..f543ebd3f09 100644 --- a/source4/lib/samba3/winsdb.c +++ b/source4/lib/samba3/winsdb.c @@ -55,7 +55,7 @@ NTSTATUS samba3_read_winsdb( const char *fn, TALLOC_CTX *ctx, struct samba3_wins /* Read a line from the wins.dat file. Strips whitespace from the beginning and end of the line. */ - line = fgets_slash(NULL,-1,fp); + line = fgets_slash(NULL,8,fp); if (!line) return NT_STATUS_UNSUCCESSFUL; @@ -169,7 +169,7 @@ NTSTATUS samba3_read_winsdb( const char *fn, TALLOC_CTX *ctx, struct samba3_wins entry.ttl = atol(ttl_str); *entries = talloc_realloc(ctx, *entries, struct samba3_winsdb_entry, (*count)+1); - *entries[*count] = entry; + (*entries)[*count] = entry; (*count)++; } -- 2.34.1