--- /dev/null
+/*
+ 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;
+}
#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;
}
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,
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 */
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,
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 */
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,
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 */
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;
+}