s3:registry: compile reg_create_path() & reg_delete_path()
[mat/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 /**
86  * Utility function to create a registry key without opening the hive
87  * before. Assumes the hive already exists.
88  */
89
90 WERROR reg_create_path(TALLOC_CTX *mem_ctx, const char *orig_path,
91                        uint32 desired_access,
92                        const struct security_token *token,
93                        enum winreg_CreateAction *paction,
94                        struct registry_key **pkey)
95 {
96         struct registry_key *hive;
97         char *path, *p;
98         WERROR err;
99
100         if (!(path = SMB_STRDUP(orig_path))) {
101                 return WERR_NOMEM;
102         }
103
104         p = strchr(path, '\\');
105
106         if ((p == NULL) || (p[1] == '\0')) {
107                 /*
108                  * No key behind the hive, just return the hive
109                  */
110
111                 err = reg_openhive(mem_ctx, path, desired_access, token,
112                                    &hive);
113                 if (!W_ERROR_IS_OK(err)) {
114                         SAFE_FREE(path);
115                         return err;
116                 }
117                 SAFE_FREE(path);
118                 *pkey = hive;
119                 *paction = REG_OPENED_EXISTING_KEY;
120                 return WERR_OK;
121         }
122
123         *p = '\0';
124
125         err = reg_openhive(mem_ctx, path,
126                            (strchr(p+1, '\\') != NULL) ?
127                            KEY_ENUMERATE_SUB_KEYS : KEY_CREATE_SUB_KEY,
128                            token, &hive);
129         if (!W_ERROR_IS_OK(err)) {
130                 SAFE_FREE(path);
131                 return err;
132         }
133
134         err = reg_createkey(mem_ctx, hive, p+1, desired_access, pkey, paction);
135         SAFE_FREE(path);
136         TALLOC_FREE(hive);
137         return err;
138 }
139
140 /*
141  * Utility function to create a registry key without opening the hive
142  * before. Will not delete a hive.
143  */
144
145 WERROR reg_delete_path(const struct security_token *token,
146                        const char *orig_path)
147 {
148         struct registry_key *hive;
149         char *path, *p;
150         WERROR err;
151
152         if (!(path = SMB_STRDUP(orig_path))) {
153                 return WERR_NOMEM;
154         }
155
156         p = strchr(path, '\\');
157
158         if ((p == NULL) || (p[1] == '\0')) {
159                 SAFE_FREE(path);
160                 return WERR_INVALID_PARAM;
161         }
162
163         *p = '\0';
164
165         err = reg_openhive(NULL, path,
166                            (strchr(p+1, '\\') != NULL) ?
167                            KEY_ENUMERATE_SUB_KEYS : KEY_CREATE_SUB_KEY,
168                            token, &hive);
169         if (!W_ERROR_IS_OK(err)) {
170                 SAFE_FREE(path);
171                 return err;
172         }
173
174         err = reg_deletekey(hive, p+1);
175         SAFE_FREE(path);
176         TALLOC_FREE(hive);
177         return err;
178 }