82a08c908872f1c7af72383af9952b6d9fb17ada
[sfrench/samba-autobuild/.git] / source4 / lib / samba3 / registry.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer
4  *  Copyright (C) Gerald Carter                     2002-2005
5  *  Copyright (C) Jelmer Vernooij                   2005
6  *
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.
11  *  
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.
16  *  
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.
20  */
21
22 /* Implementation of internal registry database functions. */
23
24 #include "includes.h"
25 #include "lib/samba3/samba3.h"
26 #include "librpc/gen_ndr/winreg.h"
27 #include "lib/tdb/include/tdbutil.h"
28 #include "system/filesys.h"
29 #include "pstring.h"
30
31 #define VALUE_PREFIX    "SAMBA_REGVAL"
32 #define REGVER_V1       1       /* first db version with write support */
33
34 /****************************************************************************
35  Unpack a list of registry values from the TDB
36  ***************************************************************************/
37  
38 static int regdb_unpack_values(TDB_CONTEXT *tdb, TALLOC_CTX *ctx, struct samba3_regkey *key, TDB_DATA data )
39 {
40         int             len = 0;
41         uint32_t        type;
42         uint32_t        size;
43         uint8_t         *data_p;
44         uint32_t        num_values = 0;
45         int             i;
46         fstring valuename;
47         
48         /* loop and unpack the rest of the registry values */
49         
50         len += tdb_unpack(tdb, data.dptr+len, data.dsize-len, "d", &num_values);
51         
52         for ( i=0; i<num_values; i++ ) {
53                 struct samba3_regval val;
54                 /* unpack the next regval */
55                 
56                 type = REG_NONE;
57                 size = 0;
58                 data_p = NULL;
59                 len += tdb_unpack(tdb, data.dptr+len, data.dsize-len, "fdB",
60                                   valuename,
61                                   &val.type,
62                                   &size,
63                                   &data_p);
64                 val.name = talloc_strdup(ctx, valuename);
65                 val.data = data_blob_talloc(ctx, data_p, size);
66
67                 key->values = talloc_realloc(ctx, key->values, struct samba3_regval, key->value_count+1);
68                 key->values[key->value_count] = val;
69                 key->value_count++;
70         }
71
72         return len;
73 }
74
75
76         
77 /***********************************************************************
78  Open the registry database
79  ***********************************************************************/
80  
81 NTSTATUS samba3_read_regdb ( const char *fn, TALLOC_CTX *ctx, struct samba3_regdb *db )
82 {
83         uint32_t vers_id;
84         TDB_CONTEXT *tdb;
85         TDB_DATA kbuf, vbuf;
86
87         /* placeholder tdb; reinit upon startup */
88         
89         if ( !(tdb = tdb_open(fn, 0, TDB_DEFAULT, O_RDONLY, 0600)) )
90         {
91                 return NT_STATUS_UNSUCCESSFUL;
92         }
93
94         vers_id = tdb_fetch_int32(tdb, "INFO/version");
95
96         db->key_count = 0;
97         db->keys = NULL;
98         
99         if (vers_id > REGVER_V1) 
100                 return NT_STATUS_UNSUCCESSFUL;
101
102         for (kbuf = tdb_firstkey(tdb); kbuf.dptr; kbuf = tdb_nextkey(tdb, kbuf))
103         {
104                 uint32_t len;
105                 int i;
106                 struct samba3_regkey key;
107                 char *skey;
108                         
109                 if (strncmp(kbuf.dptr, VALUE_PREFIX, strlen(VALUE_PREFIX)))
110                         continue;
111
112                 vbuf = tdb_fetch(tdb, kbuf);
113
114                 key.name = talloc_strdup(ctx, kbuf.dptr); 
115
116                 len = tdb_unpack(tdb, vbuf.dptr, vbuf.dsize, "d", &key.subkey_count);
117
118                 key.value_count = 0;
119                 key.values = NULL;
120                 key.subkeys = talloc_array(ctx, char *, key.subkey_count);
121         
122                 for (i = 0; i < key.subkey_count; i++) {
123                         fstring tmp;
124                         len += tdb_unpack( tdb, vbuf.dptr+len, vbuf.dsize-len, "f", tmp );
125                         key.subkeys[i] = talloc_strdup(ctx, tmp);
126                 }
127
128                 skey = talloc_asprintf(ctx, "%s/%s", VALUE_PREFIX, kbuf.dptr );
129         
130                 vbuf = tdb_fetch_bystring( tdb, skey );
131         
132                 if ( vbuf.dptr ) {
133                         regdb_unpack_values( tdb, ctx, &key, vbuf );
134                 }
135
136                 db->keys = talloc_realloc(ctx, db->keys, struct samba3_regkey, db->key_count+1);
137                 db->keys[i] = key;
138                 db->key_count++;
139         }
140
141         tdb_close(tdb);
142
143         return NT_STATUS_OK;
144 }