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