s3:registry: move reg_api_regf prototypes to their own header file.
[amitay/samba.git] / source3 / registry / reg_api_util.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer
4  *  Copyright (C) Volker Lendecke 2006
5  *  Copyright (C) Michael Adam 2007-2010
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, see <http://www.gnu.org/licenses/>.
19  */
20
21 /*
22  * Higher level utility functions on top of reg_api.c
23  */
24
25 #include "includes.h"
26 #include "registry.h"
27 #include "reg_api.h"
28 #include "reg_api_util.h"
29
30 /**
31  * Utility function to open a complete registry path including the hive prefix.
32  */
33 WERROR reg_open_path(TALLOC_CTX *mem_ctx, const char *orig_path,
34                      uint32 desired_access, const struct security_token *token,
35                      struct registry_key **pkey)
36 {
37         struct registry_key *hive, *key;
38         char *path, *p;
39         WERROR err;
40
41         if (!(path = SMB_STRDUP(orig_path))) {
42                 return WERR_NOMEM;
43         }
44
45         p = strchr(path, '\\');
46
47         if ((p == NULL) || (p[1] == '\0')) {
48                 /*
49                  * No key behind the hive, just return the hive
50                  */
51
52                 err = reg_openhive(mem_ctx, path, desired_access, token,
53                                    &hive);
54                 if (!W_ERROR_IS_OK(err)) {
55                         SAFE_FREE(path);
56                         return err;
57                 }
58                 SAFE_FREE(path);
59                 *pkey = hive;
60                 return WERR_OK;
61         }
62
63         *p = '\0';
64
65         err = reg_openhive(mem_ctx, path, KEY_ENUMERATE_SUB_KEYS, token,
66                            &hive);
67         if (!W_ERROR_IS_OK(err)) {
68                 SAFE_FREE(path);
69                 return err;
70         }
71
72         err = reg_openkey(mem_ctx, hive, p+1, desired_access, &key);
73
74         TALLOC_FREE(hive);
75         SAFE_FREE(path);
76
77         if (!W_ERROR_IS_OK(err)) {
78                 return err;
79         }
80
81         *pkey = key;
82         return WERR_OK;
83 }
84
85 #if 0
86 /* these two functions are unused. */
87
88 /**
89  * Utility function to create a registry key without opening the hive
90  * before. Assumes the hive already exists.
91  */
92
93 WERROR reg_create_path(TALLOC_CTX *mem_ctx, const char *orig_path,
94                        uint32 desired_access,
95                        const struct security_token *token,
96                        enum winreg_CreateAction *paction,
97                        struct registry_key **pkey)
98 {
99         struct registry_key *hive;
100         char *path, *p;
101         WERROR err;
102
103         if (!(path = SMB_STRDUP(orig_path))) {
104                 return WERR_NOMEM;
105         }
106
107         p = strchr(path, '\\');
108
109         if ((p == NULL) || (p[1] == '\0')) {
110                 /*
111                  * No key behind the hive, just return the hive
112                  */
113
114                 err = reg_openhive(mem_ctx, path, desired_access, token,
115                                    &hive);
116                 if (!W_ERROR_IS_OK(err)) {
117                         SAFE_FREE(path);
118                         return err;
119                 }
120                 SAFE_FREE(path);
121                 *pkey = hive;
122                 *paction = REG_OPENED_EXISTING_KEY;
123                 return WERR_OK;
124         }
125
126         *p = '\0';
127
128         err = reg_openhive(mem_ctx, path,
129                            (strchr(p+1, '\\') != NULL) ?
130                            KEY_ENUMERATE_SUB_KEYS : KEY_CREATE_SUB_KEY,
131                            token, &hive);
132         if (!W_ERROR_IS_OK(err)) {
133                 SAFE_FREE(path);
134                 return err;
135         }
136
137         err = reg_createkey(mem_ctx, hive, p+1, desired_access, pkey, paction);
138         SAFE_FREE(path);
139         TALLOC_FREE(hive);
140         return err;
141 }
142
143 /*
144  * Utility function to create a registry key without opening the hive
145  * before. Will not delete a hive.
146  */
147
148 WERROR reg_delete_path(const struct security_token *token,
149                        const char *orig_path)
150 {
151         struct registry_key *hive;
152         char *path, *p;
153         WERROR err;
154
155         if (!(path = SMB_STRDUP(orig_path))) {
156                 return WERR_NOMEM;
157         }
158
159         p = strchr(path, '\\');
160
161         if ((p == NULL) || (p[1] == '\0')) {
162                 SAFE_FREE(path);
163                 return WERR_INVALID_PARAM;
164         }
165
166         *p = '\0';
167
168         err = reg_openhive(NULL, path,
169                            (strchr(p+1, '\\') != NULL) ?
170                            KEY_ENUMERATE_SUB_KEYS : KEY_CREATE_SUB_KEY,
171                            token, &hive);
172         if (!W_ERROR_IS_OK(err)) {
173                 SAFE_FREE(path);
174                 return err;
175         }
176
177         err = reg_deletekey(hive, p+1);
178         SAFE_FREE(path);
179         TALLOC_FREE(hive);
180         return err;
181 }
182 #endif /* #if 0 */