r9623: samba3dump now generates LDIF for the registry hives from registry.tdb
[samba.git] / source4 / lib / samba3 / upgrade.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Generate ldb_message 's for samba3_*
4
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 #include "includes.h"
23 #include "lib/samba3/samba3.h"
24 #include "lib/ldb/include/ldb.h"
25
26 static struct ldb_message *msg_array_add(struct ldb_context *ctx, struct ldb_message ***msgs, int *count)
27 {
28         struct ldb_message *ret;
29         *msgs = talloc_realloc(ctx, *msgs, struct ldb_message *, (*count)+1);
30
31         ret = (*msgs)[*count] = talloc_zero(ctx, struct ldb_message);
32         (*count)++;
33
34         return ret;
35 }
36
37 static struct ldb_dn *regkey_to_dn(struct ldb_context *ldb, const char *name)
38 {
39         char *p, *n, *dup;
40         struct ldb_dn *ret = ldb_dn_explode(ldb, "hive=NONE");
41
42         p = dup = talloc_strdup(ldb, name);
43
44         while (p) {
45                 n = strchr(p, '/');
46                 if (n) { *n = '\0';     n++; }
47
48                 ret = ldb_dn_build_child(ldb, "key", p, ret);
49
50                 p = n;
51         }
52
53         talloc_free(dup);
54
55         return ret;
56 }
57
58 /* Where prefix is any of:
59  * - HKLM
60  *   HKU
61  *   HKCR
62  *   HKPD
63  *   HKPT
64  */
65
66 int samba3_upgrade_registry(struct samba3_regdb *regdb, const char *prefix, struct ldb_context *ldb, struct ldb_message ***msgs)
67 {
68         int i;
69         struct ldb_message *msg;
70         int count = 0;
71         char *prefix_up = strupper_talloc(ldb, prefix);
72         *msgs = NULL;
73
74         for (i = 0; i < regdb->key_count; i++) {
75                 int j;
76                 struct samba3_regkey *rk = &regdb->keys[i];
77                 struct ldb_dn *keydn;
78
79                 /* Only handle selected hive */
80                 if (strncmp(prefix_up, rk->name, strlen(prefix_up)) != 0) {
81                         continue;
82                 }
83
84                 msg = msg_array_add(ldb, msgs, &count);
85
86                 msg->num_elements = 0;
87                 msg->elements = NULL;
88                 msg->private_data = NULL;
89
90                 /* Convert key name to dn */
91                 keydn = msg->dn = regkey_to_dn(ldb, rk->name);
92
93                 ldb_msg_add_string(ldb, msg, "name", strrchr(rk->name, '/')?strrchr(rk->name, '/')+1:rk->name);
94                 
95                 for (j = 0; j < rk->value_count; j++) {
96                         struct samba3_regval *rv = &rk->values[j];
97
98                         msg = msg_array_add(ldb, msgs, &count);
99                         msg->dn = ldb_dn_build_child(ldb, "value", rv->name, keydn);
100
101                         ldb_msg_add_string(ldb, msg, "value", rv->name);
102                         ldb_msg_add_fmt(ldb, msg, "type", "%d", rv->type);
103                         ldb_msg_add_value(ldb, msg, "data", &rv->data);
104                 }
105         }
106         
107         talloc_free(prefix_up);
108
109         return count;
110 }
111
112 int samba3_upgrade_sam(struct samba3 *samba3, struct ldb_context *ctx, struct ldb_message ***msgs)
113 {
114         /* FIXME */
115         return -1;
116 }
117
118 int samba3_upgrade_winbind(struct samba3 *samba3, struct ldb_context *ctx, struct ldb_message ***msgs)
119 {
120         /* FIXME */
121         return -1;
122 }
123
124 int samba3_upgrade_winsdb(struct samba3 *samba3, struct ldb_context *ctx, struct ldb_message ***msgs)
125 {
126         /* FIXME */
127         return -1;
128 }