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 2 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. */
26 #define VALUE_PREFIX "SAMBA_REGVAL"
27 #define REGVER_V1 1 /* first db version with write support */
29 /***********************************************************************
30 Open the registry database
31 ***********************************************************************/
33 static TDB_CONTEXT *samba3_open_registry ( const char *fn )
37 /* placeholder tdb; reinit upon startup */
39 if ( !(tdb = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0600)) )
44 vers_id = tdb_fetch_int32(tdb, "INFO/version");
46 if (vers_id > REGVER_V1)
52 /***********************************************************************
53 Retrieve an array of strings containing subkeys. Memory should be
54 released by the caller.
55 ***********************************************************************/
57 int regdb_fetch_keys( TDB_CONTEXT *tdb, const char* key, REGSUBKEY_CTR *ctr )
67 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
69 path = talloc_strdup(key);
71 /* convert to key format */
72 for ( i = 0; path[i]; i++) {
73 if ( path[i] == '\\' )
78 dbuf = tdb_fetch_bystring( tdb, path );
84 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
88 len = tdb_unpack( buf, buflen, "d", &num_items);
90 for (i=0; i<num_items; i++) {
91 len += tdb_unpack( buf+len, buflen-len, "f", subkeyname );
92 regsubkey_ctr_addkey( ctr, subkeyname );
95 SAFE_FREE( dbuf.dptr );
97 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
102 /****************************************************************************
103 Unpack a list of registry values frem the TDB
104 ***************************************************************************/
106 static int regdb_unpack_values(REGVAL_CTR *values, char *buf, int buflen)
113 uint32_t num_values = 0;
116 /* loop and unpack the rest of the registry values */
118 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
120 for ( i=0; i<num_values; i++ ) {
121 /* unpack the next regval */
126 len += tdb_unpack(buf+len, buflen-len, "fdB",
132 /* add the new value. Paranoid protective code -- make sure data_p is valid */
134 if ( size && data_p ) {
135 regval_ctr_addvalue( values, valuename, type, (const char *)data_p, size );
136 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
139 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
145 /***********************************************************************
146 Retrieve an array of strings containing subkeys. Memory should be
147 released by the caller.
148 ***********************************************************************/
150 int regdb_fetch_values( TDB_CONTEXT *tdb, const char* key, REGVAL_CTR *values )
155 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
157 pstr_sprintf( keystr, "%s/%s", VALUE_PREFIX, key );
158 normalize_reg_path( keystr );
160 data = tdb_fetch_bystring( tdb, keystr );
163 /* all keys have zero values by default */
167 regdb_unpack_values( values, data.dptr, data.dsize );
169 SAFE_FREE( data.dptr );
171 return regval_ctr_numvals(values);