2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
5 * Copyright (C) Jelmer Vernooij 2005
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 /* Implementation of internal registry database functions. */
25 #include "lib/samba3/samba3.h"
26 #include "librpc/gen_ndr/winreg.h"
27 #include "lib/tdb/include/tdb.h"
28 #include "lib/util/util_tdb.h"
29 #include "system/filesys.h"
32 #define VALUE_PREFIX "SAMBA_REGVAL"
33 #define REGVER_V1 1 /* first db version with write support */
35 /****************************************************************************
36 Unpack a list of registry values from the TDB
37 ***************************************************************************/
39 static int regdb_unpack_values(TDB_CONTEXT *tdb, TALLOC_CTX *ctx, struct samba3_regkey *key, TDB_DATA data )
45 uint32_t num_values = 0;
49 /* loop and unpack the rest of the registry values */
51 len += tdb_unpack(tdb, (char *)data.dptr+len, data.dsize-len, "d", &num_values);
53 for ( i=0; i<num_values; i++ ) {
54 struct samba3_regval val;
55 /* unpack the next regval */
60 len += tdb_unpack(tdb, (char *)data.dptr+len, data.dsize-len, "fdB",
65 val.name = talloc_strdup(ctx, valuename);
66 val.data = data_blob_talloc(ctx, data_p, size);
68 key->values = talloc_realloc(ctx, key->values, struct samba3_regval, key->value_count+1);
69 key->values[key->value_count] = val;
78 /***********************************************************************
79 Open the registry database
80 ***********************************************************************/
82 NTSTATUS samba3_read_regdb ( const char *fn, TALLOC_CTX *ctx, struct samba3_regdb *db )
88 /* placeholder tdb; reinit upon startup */
90 if ( !(tdb = tdb_open(fn, 0, TDB_DEFAULT, O_RDONLY, 0600)) )
92 DEBUG(0, ("Unable to open registry database %s\n", fn));
93 return NT_STATUS_UNSUCCESSFUL;
96 vers_id = tdb_fetch_int32(tdb, "INFO/version");
101 if (vers_id != -1 && vers_id >= REGVER_V1) {
102 DEBUG(0, ("Registry version mismatch: %d\n", vers_id));
103 return NT_STATUS_UNSUCCESSFUL;
106 for (kbuf = tdb_firstkey(tdb); kbuf.dptr; kbuf = tdb_nextkey(tdb, kbuf))
110 struct samba3_regkey key;
113 if (strncmp((char *)kbuf.dptr, VALUE_PREFIX, strlen(VALUE_PREFIX)) == 0)
116 vbuf = tdb_fetch(tdb, kbuf);
118 key.name = talloc_strdup(ctx, (char *)kbuf.dptr);
120 len = tdb_unpack(tdb, (char *)vbuf.dptr, vbuf.dsize, "d", &key.subkey_count);
124 key.subkeys = talloc_array(ctx, char *, key.subkey_count);
126 for (i = 0; i < key.subkey_count; i++) {
128 len += tdb_unpack( tdb, (char *)vbuf.dptr+len, vbuf.dsize-len, "f", tmp );
129 key.subkeys[i] = talloc_strdup(ctx, tmp);
132 skey = talloc_asprintf(ctx, "%s/%s", VALUE_PREFIX, kbuf.dptr );
134 vbuf = tdb_fetch_bystring( tdb, skey );
137 regdb_unpack_values( tdb, ctx, &key, vbuf );
140 db->keys = talloc_realloc(ctx, db->keys, struct samba3_regkey, db->key_count+1);
141 db->keys[db->key_count] = key;