r9449: Add simple utility for dumping Samba3 domain information. Currently
authorJelmer Vernooij <jelmer@samba.org>
Sun, 21 Aug 2005 17:18:35 +0000 (17:18 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:34:16 +0000 (13:34 -0500)
only prints rids and usernames of users in passdb database.

Update plan
(This used to be commit 651d06728fb21393c15268ea47689c738c2c6b86)

source4/lib/samba3/PLAN
source4/lib/samba3/config.mk
source4/lib/samba3/sam.h
source4/lib/samba3/samba3dump.c [new file with mode: 0644]
source4/lib/samba3/tdbsam.c

index 6e83e8fc33fc76a644efc2f1cf30eccb8080089e..6076141ac0ca65f1e5b011a2d0c51da833bff3e7 100644 (file)
@@ -7,8 +7,11 @@ Three possible viable approaches:
 
   (one-way upgrades can be done by using ldbsearch -a on these dynamically
   generated ldb's)
+  Since TDB's are local, there isn't much point in writing back backwards 
+  compatible data.
 
  2) samr "mapping" backend (alternative for samr.ldb) (two-way)
+    This would allow users to keep mixed domains containing Samba3 and Samba4.
 
  3) The vampire way of doing things (one-way)
   - samba3 pidl backend 
index 43608577c5fdfdfcaf779d2b3377d8069cf833eb..5045901b3b1c00c53745ef756bbe1f9825b88865 100644 (file)
@@ -7,4 +7,12 @@ INIT_OBJ_FILES = \
 # End SUBSYSTEM LIBSAMBA3
 ################################################
 
-
+################################################
+# Start BINARY samba3dump
+[BINARY::samba3dump]
+INSTALLDIR = BINDIR
+INIT_OBJ_FILES = \
+               lib/samba3/samba3dump.o
+REQUIRED_SUBSYSTEMS = LIBBASIC LIBCMDLINE LIBSAMBA3
+# End BINARY samba3dump
+################################################
index 11a9f42ec70c08de8dce0463ca13e19da1213976..bc13b28e2a8729a4934ef3d8dc0d759b7df66be5 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef _SAMBA3_SAM_H /* _SAMBA3_SAM_H */
 #define _SAMBA3_SAM_H 
 
+#include "librpc/gen_ndr/security.h"
+
 struct samba3_samaccount {
        uint32_t logon_time,
                logoff_time,
@@ -49,4 +51,28 @@ struct samba3_samaccount {
        uint8_t *hours;
 };
 
+/* SID Types */
+enum SID_NAME_USE
+{
+       SID_NAME_USE_NONE = 0,
+       SID_NAME_USER    = 1, /* user */
+       SID_NAME_DOM_GRP,     /* domain group */
+       SID_NAME_DOMAIN,      /* domain sid */
+       SID_NAME_ALIAS,       /* local group */
+       SID_NAME_WKN_GRP,     /* well-known group */
+       SID_NAME_DELETED,     /* deleted account: needed for c2 rating */
+       SID_NAME_INVALID,     /* invalid account */
+       SID_NAME_UNKNOWN,     /* unknown sid type */
+       SID_NAME_COMPUTER     /* sid for a computer */
+};
+
+struct samba3_groupmapping {
+       struct pdb_methods *methods;
+       gid_t gid;
+       struct dom_sid *sid;
+       enum SID_NAME_USE sid_name_use;
+       const char *nt_name;
+       const char *comment;
+};
+
 #endif /* _SAMBA3_SAM_H */
diff --git a/source4/lib/samba3/samba3dump.c b/source4/lib/samba3/samba3dump.c
new file mode 100644 (file)
index 0000000..4071ba1
--- /dev/null
@@ -0,0 +1,75 @@
+/* 
+   Unix SMB/CIFS implementation.
+   Samba3 database dump utility
+
+    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/sam.h"
+#include "lib/cmdline/popt_common.h"
+
+static const char *libdir = "/var/lib/samba";
+
+static NTSTATUS print_sam(void)
+{
+       struct samba3_samaccount *accounts;
+       uint32_t count, i;
+       char *tdbsam_file;
+       NTSTATUS status;
+       
+       asprintf(&tdbsam_file, "%s/passdb.tdb", libdir);
+
+       printf("Opening TDB sam %s\n", tdbsam_file);
+
+       status = samba3_read_tdbsam(NULL, tdbsam_file, &accounts, &count);
+       if (NT_STATUS_IS_ERR(status)) {
+               fprintf(stderr, "Error reading tdbsam database %s\n", tdbsam_file);
+               return status;
+       }
+
+       for (i = 0; i < count; i++) {
+               printf("%d: %s\n", accounts[i].user_rid, accounts[i].username);
+       }
+
+       return NT_STATUS_OK;
+}
+int main(int argc, char **argv)
+{
+       int opt;
+       poptContext pc;
+       struct poptOption long_options[] = {
+               POPT_AUTOHELP
+               { "libdir", 0, POPT_ARG_STRING, &libdir, 'l', "Set libdir [/var/lib/samba]", "LIBDIR" },
+               POPT_COMMON_SAMBA
+               POPT_TABLEEND
+       };
+
+       pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
+
+       poptSetOtherOptionHelp(pc, "<smb.conf>");
+
+       while((opt = poptGetNextOpt(pc)) != -1) {
+       }
+
+       print_sam();
+
+       poptFreeContext(pc);
+
+       return 0;
+}
index 0b2f9754410928df96c4dcc1799b6fc4236e0ebc..a16c07d2d10f4177371e2ebebb0e4c57e4141971 100644 (file)
@@ -26,6 +26,7 @@
 
 #include "includes.h"
 #include "system/iconv.h"
+#include "system/filesys.h"
 #include "lib/tdb/include/tdbutil.h"
 #include "lib/samba3/sam.h"
 
 /**
  * Open the TDB passwd database, check version and convert it if needed.
  * @param name filename of the tdbsam file.
- * @param open_flags file access mode.
+ * @param version version of the tdbsam database
  * @return a TDB_CONTEXT handle on the tdbsam file.
  **/
 
-static TDB_CONTEXT * tdbsam_open (const char *name, int open_flags, int32_t *version)
+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, 
-                                    open_flags, 0600))) {
-               DEBUG(0, ("Unable to open/create TDB passwd\n"));
+                                    O_RDONLY, 0600))) {
+               DEBUG(0, ("Unable to open TDB passwd\n"));
                return NULL;
        }
 
@@ -69,7 +70,7 @@ static TDB_CONTEXT * tdbsam_open (const char *name, int open_flags, int32_t *ver
        return pdb_tdb;
 }
 
-static BOOL init_sam_from_buffer_v0(TDB_CONTEXT *tdb, struct samba3_samaccount *sampass, uint8_t *buf, uint32_t buflen)
+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,
                dir_drive_len, unknown_str_len, munged_dial_len,
@@ -80,13 +81,13 @@ static BOOL init_sam_from_buffer_v0(TDB_CONTEXT *tdb, struct samba3_samaccount *
        uint32_t                len = 0;
        uint32_t                lm_pw_len, nt_pw_len, hourslen;
        
-       if(sampass == NULL || buf == NULL) {
+       if(sampass == NULL || buf.dptr == NULL) {
                DEBUG(0, ("init_sam_from_buffer_v0: NULL parameters found!\n"));
                return False;
        }
 
        /* unpack the buffer into variables */
-       len = tdb_unpack (tdb, (char *)buf, buflen, TDB_FORMAT_STRING_V0,
+       len = tdb_unpack (tdb, (char *)buf.dptr, buf.dsize, TDB_FORMAT_STRING_V0,
                &sampass->logon_time,                                   /* d */
                &sampass->logoff_time,                                  /* d */
                &sampass->kickoff_time,                                 /* d */
@@ -133,7 +134,7 @@ static BOOL init_sam_from_buffer_v0(TDB_CONTEXT *tdb, struct samba3_samaccount *
        return True;
 }
 
-static BOOL init_sam_from_buffer_v1(TDB_CONTEXT *tdb, struct samba3_samaccount *sampass, uint8_t *buf, uint32_t buflen)
+static BOOL init_sam_from_buffer_v1(TDB_CONTEXT *tdb, struct samba3_samaccount *sampass, TDB_DATA buf)
 {
        uint32_t        username_len, domain_len, nt_username_len,
                dir_drive_len, unknown_str_len, munged_dial_len,
@@ -144,13 +145,13 @@ static BOOL init_sam_from_buffer_v1(TDB_CONTEXT *tdb, struct samba3_samaccount *
        uint32_t                len = 0;
        uint32_t                lm_pw_len, nt_pw_len, hourslen;
        
-       if(sampass == NULL || buf == NULL) {
+       if(sampass == NULL || buf.dptr == NULL) {
                DEBUG(0, ("init_sam_from_buffer_v1: NULL parameters found!\n"));
                return False;
        }
 
        /* unpack the buffer into variables */
-       len = tdb_unpack (tdb, (char *)buf, buflen, TDB_FORMAT_STRING_V1,
+       len = tdb_unpack (tdb, (char *)buf.dptr, buf.dsize, TDB_FORMAT_STRING_V1,
                &sampass->logon_time,                                   /* d */
                &sampass->logoff_time,                                  /* d */
                &sampass->kickoff_time,                         /* d */
@@ -199,7 +200,7 @@ static BOOL init_sam_from_buffer_v1(TDB_CONTEXT *tdb, struct samba3_samaccount *
        return True;
 }
 
-static BOOL init_sam_from_buffer_v2(TDB_CONTEXT *tdb, struct samba3_samaccount *sampass, uint8_t *buf, uint32_t buflen)
+static BOOL init_sam_from_buffer_v2(TDB_CONTEXT *tdb, struct samba3_samaccount *sampass, TDB_DATA buf)
 {
        uint32_t        username_len, domain_len, nt_username_len,
                dir_drive_len, unknown_str_len, munged_dial_len,
@@ -209,13 +210,13 @@ static BOOL init_sam_from_buffer_v2(TDB_CONTEXT *tdb, struct samba3_samaccount *
        uint32_t                len = 0;
        uint32_t                lm_pw_len, nt_pw_len, nt_pw_hist_len, hourslen;
        
-       if(sampass == NULL || buf == NULL) {
+       if(sampass == NULL || buf.dptr == NULL) {
                DEBUG(0, ("init_sam_from_buffer_v2: NULL parameters found!\n"));
                return False;
        }
 
        /* unpack the buffer into variables */
-       len = tdb_unpack (tdb, (char *)buf, buflen, TDB_FORMAT_STRING_V2,
+       len = tdb_unpack (tdb, (char *)buf.dptr, buf.dsize, TDB_FORMAT_STRING_V2,
                &sampass->logon_time,                                   /* d */
                &sampass->logoff_time,                                  /* d */
                &sampass->kickoff_time,                                 /* d */
@@ -264,3 +265,43 @@ static BOOL init_sam_from_buffer_v2(TDB_CONTEXT *tdb, struct samba3_samaccount *
 
        return True;
 }
+
+NTSTATUS samba3_read_tdbsam(TALLOC_CTX *ctx, const char *filename, struct samba3_samaccount **accounts, uint32_t *count)
+{
+       int32_t version;
+       TDB_CONTEXT *tdb = tdbsam_open(filename, &version);
+       TDB_DATA key, val;
+
+       if (tdb == NULL)
+               return NT_STATUS_UNSUCCESSFUL;
+
+       if (version < 0 || version > 2) {
+               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;
+
+               val = tdb_fetch(tdb, key);
+
+               *accounts = talloc_realloc(ctx, *accounts, struct samba3_samaccount, (*count)+1);
+
+               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;
+
+               }
+
+               (*count)++;
+       }
+       
+       tdb_close(tdb);
+       
+       return NT_STATUS_OK;
+}