summaryrefslogtreecommitdiff
path: root/source4/lib/samba3/registry.c
blob: 7f68c9df74562932a1f9dc1520b1a8324607abda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* 
 *  Unix SMB/CIFS implementation.
 *  Virtual Windows Registry Layer
 *  Copyright (C) Gerald Carter                     2002-2005
 *  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.
 */

/* Implementation of internal registry database functions. */

#include "includes.h"
#include "lib/samba3/samba3.h"
#include "librpc/gen_ndr/winreg.h"
#include "lib/tdb/include/tdbutil.h"
#include "system/filesys.h"
#include "pstring.h"

#define VALUE_PREFIX	"SAMBA_REGVAL"
#define REGVER_V1	1	/* first db version with write support */

/****************************************************************************
 Unpack a list of registry values from the TDB
 ***************************************************************************/
 
static int regdb_unpack_values(TDB_CONTEXT *tdb, TALLOC_CTX *ctx, struct samba3_regkey *key, TDB_DATA data )
{
	int 		len = 0;
	uint32_t	type;
	uint32_t	size;
	uint8_t *data_p;
	uint32_t	num_values = 0;
	int 		i;
	fstring valuename;
	
	/* loop and unpack the rest of the registry values */
	
	len += tdb_unpack(tdb, (char *)data.dptr+len, data.dsize-len, "d", &num_values);
	
	for ( i=0; i<num_values; i++ ) {
		struct samba3_regval val;
		/* unpack the next regval */
		
		type = REG_NONE;
		size = 0;
		data_p = NULL;
		len += tdb_unpack(tdb, (char *)data.dptr+len, data.dsize-len, "fdB",
				  valuename,
				  &val.type,
				  &size,
				  &data_p);
		val.name = talloc_strdup(ctx, valuename);
		val.data = data_blob_talloc(ctx, data_p, size);

		key->values = talloc_realloc(ctx, key->values, struct samba3_regval, key->value_count+1);
		key->values[key->value_count] = val;
		key->value_count++;
	}

	return len;
}


	
/***********************************************************************
 Open the registry database
 ***********************************************************************/
 
NTSTATUS samba3_read_regdb ( const char *fn, TALLOC_CTX *ctx, struct samba3_regdb *db )
{
	uint32_t vers_id;
	TDB_CONTEXT *tdb;
	TDB_DATA kbuf, vbuf;

	/* placeholder tdb; reinit upon startup */
	
	if ( !(tdb = tdb_open(fn, 0, TDB_DEFAULT, O_RDONLY, 0600)) )
	{
		DEBUG(0, ("Unable to open registry database %s\n", fn));
		return NT_STATUS_UNSUCCESSFUL;
	}

	vers_id = tdb_fetch_int32(tdb, "INFO/version");

	db->key_count = 0;
	db->keys = NULL;
	
	if (vers_id != -1 && vers_id >= REGVER_V1) {
		DEBUG(0, ("Registry version mismatch: %d\n", vers_id));
		return NT_STATUS_UNSUCCESSFUL;
	}

	for (kbuf = tdb_firstkey(tdb); kbuf.dptr; kbuf = tdb_nextkey(tdb, kbuf))
	{
		uint32_t len;
		int i;
		struct samba3_regkey key;
		char *skey;
			
		if (strncmp((char *)kbuf.dptr, VALUE_PREFIX, strlen(VALUE_PREFIX)) == 0)
			continue;

		vbuf = tdb_fetch(tdb, kbuf);

		key.name = talloc_strdup(ctx, (char *)kbuf.dptr); 

		len = tdb_unpack(tdb, (char *)vbuf.dptr, vbuf.dsize, "d", &key.subkey_count);

		key.value_count = 0;
		key.values = NULL;
		key.subkeys = talloc_array(ctx, char *, key.subkey_count);
	
		for (i = 0; i < key.subkey_count; i++) {
			fstring tmp;
			len += tdb_unpack( tdb, (char *)vbuf.dptr+len, vbuf.dsize-len, "f", tmp );
			key.subkeys[i] = talloc_strdup(ctx, tmp);
		}

		skey = talloc_asprintf(ctx, "%s/%s", VALUE_PREFIX, kbuf.dptr );
	
		vbuf = tdb_fetch_bystring( tdb, skey );
	
		if ( vbuf.dptr ) {
			regdb_unpack_values( tdb, ctx, &key, vbuf );
		}

		db->keys = talloc_realloc(ctx, db->keys, struct samba3_regkey, db->key_count+1);
		db->keys[db->key_count] = key;
		db->key_count++;
	}

	tdb_close(tdb);

	return NT_STATUS_OK;
}