Convert some more files to GPLv3.
[jra/samba/.git] / source4 / lib / registry / hive.c
1
2 /*
3    Unix SMB/CIFS implementation.
4    Registry hive interface
5    Copyright (C) Jelmer Vernooij                                  2003-2007.
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 3 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 "hive.h"
24 #include "system/filesys.h"
25 #include "param/param.h"
26
27 /** Open a registry file/host/etc */
28 _PUBLIC_ WERROR reg_open_hive(TALLOC_CTX *parent_ctx, const char *location,
29                               struct auth_session_info *session_info,
30                               struct cli_credentials *credentials,
31                               struct loadparm_context *lp_ctx,
32                               struct hive_key **root)
33 {
34         int fd, num;
35         char peek[20];
36
37         /* Check for directory */
38         if (directory_exist(location)) {
39                 return reg_open_directory(parent_ctx, location, root);
40         }
41
42         fd = open(location, O_RDWR);
43         if (fd == -1) {
44                 if (errno == ENOENT)
45                         return WERR_BADFILE;
46                 return WERR_BADFILE;
47         }
48
49         num = read(fd, peek, 20);
50         if (num == -1) {
51                 return WERR_BADFILE;
52         }
53
54         if (!strncmp(peek, "regf", 4)) {
55                 close(fd);
56                 return reg_open_regf_file(parent_ctx, location, lp_iconv_convenience(lp_ctx), root);
57         } else if (!strncmp(peek, "TDB file", 8)) {
58                 close(fd);
59                 return reg_open_ldb_file(parent_ctx, location, session_info,
60                                          credentials, lp_ctx, root);
61         }
62
63         return WERR_BADFILE;
64 }
65
66 _PUBLIC_ WERROR hive_key_get_info(TALLOC_CTX *mem_ctx,
67                                   const struct hive_key *key,
68                                   const char **classname, uint32_t *num_subkeys,
69                                   uint32_t *num_values,
70                                   NTTIME *last_change_time,
71                                   uint32_t *max_subkeynamelen,
72                                   uint32_t *max_valnamelen,
73                                   uint32_t *max_valbufsize)
74 {
75         return key->ops->get_key_info(mem_ctx, key, classname, num_subkeys,
76                                       num_values, last_change_time,
77                                       max_subkeynamelen,
78                                       max_valnamelen, max_valbufsize);
79 }
80
81 _PUBLIC_ WERROR hive_key_add_name(TALLOC_CTX *ctx,
82                                   const struct hive_key *parent_key,
83                                   const char *name, const char *classname,
84                                   struct security_descriptor *desc,
85                                   struct hive_key **key)
86 {
87         SMB_ASSERT(strchr(name, '\\') == NULL);
88
89         return parent_key->ops->add_key(ctx, parent_key, name, classname,
90                                         desc, key);
91 }
92
93 _PUBLIC_ WERROR hive_key_del(const struct hive_key *key, const char *name)
94 {
95         return key->ops->del_key(key, name);
96 }
97
98 _PUBLIC_ WERROR hive_get_key_by_name(TALLOC_CTX *mem_ctx,
99                                      const struct hive_key *key,
100                                      const char *name,
101                                      struct hive_key **subkey)
102 {
103         return key->ops->get_key_by_name(mem_ctx, key, name, subkey);
104 }
105
106 WERROR hive_enum_key(TALLOC_CTX *mem_ctx,
107                      const struct hive_key *key, uint32_t idx,
108                      const char **name,
109                      const char **classname,
110                      NTTIME *last_mod_time)
111 {
112         return key->ops->enum_key(mem_ctx, key, idx, name, classname,
113                                   last_mod_time);
114 }
115
116 WERROR hive_key_set_value(struct hive_key *key, const char *name, uint32_t type,
117                                           const DATA_BLOB data)
118 {
119         if (key->ops->set_value == NULL)
120                 return WERR_NOT_SUPPORTED;
121
122         return key->ops->set_value(key, name, type, data);
123 }
124
125 WERROR hive_get_value(TALLOC_CTX *mem_ctx,
126                       struct hive_key *key, const char *name,
127                       uint32_t *type, DATA_BLOB *data)
128 {
129         if (key->ops->get_value_by_name == NULL)
130                 return WERR_NOT_SUPPORTED;
131
132         return key->ops->get_value_by_name(mem_ctx, key, name, type, data);
133 }
134
135 WERROR hive_get_value_by_index(TALLOC_CTX *mem_ctx,
136                                struct hive_key *key, uint32_t idx,
137                                const char **name,
138                                uint32_t *type, DATA_BLOB *data)
139 {
140         if (key->ops->enum_value == NULL)
141                 return WERR_NOT_SUPPORTED;
142
143         return key->ops->enum_value(mem_ctx, key, idx, name, type, data);
144 }
145
146
147 WERROR hive_key_del_value(struct hive_key *key, const char *name)
148 {
149         if (key->ops->delete_value == NULL)
150                 return WERR_NOT_SUPPORTED;
151
152         return key->ops->delete_value(key, name);
153 }
154
155 WERROR hive_key_flush(struct hive_key *key)
156 {
157         if (key->ops->flush_key == NULL)
158                 return WERR_OK;
159
160         return key->ops->flush_key(key);
161 }