libcli/cldap: fix a crash bug in cldap_socket_recv_dgram() (bug #8593)
[Samba.git] / libgpo / gpo_fetch.c
blob3cfe1d5b9424da975cc7dfab8381dd0da2682c8b
1 /*
2 * Unix SMB/CIFS implementation.
3 * Group Policy Object Support
4 * Copyright (C) Guenther Deschner 2005-2006
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "../libgpo/gpo.h"
23 #include "../libgpo/gpo_ini.h"
25 #if _SAMBA_BUILD_ == 4
26 #include "param/param.h"
27 #include "libcli/resolve/resolve.h"
28 #include <tevent.h>
29 #include "libcli/libcli.h"
30 #include "libcli/raw/libcliraw.h"
31 #include "libcli/libcli_proto.h"
32 #include "libgpo/ads_convenience.h"
33 #include "libgpo/gpo_s4.h"
34 #include "lib/util/util.h"
35 #else
36 #include "libgpo/gpo_proto.h"
37 #include "libsmb/libsmb.h"
38 #endif
40 /****************************************************************
41 explode the GPO CIFS URI into their components
42 ****************************************************************/
44 NTSTATUS gpo_explode_filesyspath(TALLOC_CTX *mem_ctx,
45 const char *cache_dir,
46 const char *file_sys_path,
47 char **server,
48 char **service,
49 char **nt_path,
50 char **unix_path)
52 char *path = NULL;
54 *server = NULL;
55 *service = NULL;
56 *nt_path = NULL;
57 *unix_path = NULL;
59 if (!file_sys_path) {
60 return NT_STATUS_OK;
63 if (!next_token_talloc(mem_ctx, &file_sys_path, server, "\\")) {
64 return NT_STATUS_INVALID_PARAMETER;
66 NT_STATUS_HAVE_NO_MEMORY(*server);
68 if (!next_token_talloc(mem_ctx, &file_sys_path, service, "\\")) {
69 return NT_STATUS_INVALID_PARAMETER;
71 NT_STATUS_HAVE_NO_MEMORY(*service);
73 if ((*nt_path = talloc_asprintf(mem_ctx, "\\%s", file_sys_path))
74 == NULL) {
75 return NT_STATUS_NO_MEMORY;
77 NT_STATUS_HAVE_NO_MEMORY(*nt_path);
79 if ((path = talloc_asprintf(mem_ctx,
80 "%s/%s",
81 cache_dir,
82 file_sys_path)) == NULL) {
83 return NT_STATUS_NO_MEMORY;
85 #if _SAMBA_BUILD_ == 4
86 path = string_sub_talloc(mem_ctx, path, "\\", "/");
87 #else
88 path = talloc_string_sub(mem_ctx, path, "\\", "/");
89 #endif
90 if (!path) {
91 return NT_STATUS_NO_MEMORY;
94 *unix_path = talloc_strdup(mem_ctx, path);
95 NT_STATUS_HAVE_NO_MEMORY(*unix_path);
97 talloc_free(path);
98 return NT_STATUS_OK;
101 /****************************************************************
102 prepare the local disc storage for "unix_path"
103 ****************************************************************/
105 static NTSTATUS gpo_prepare_local_store(TALLOC_CTX *mem_ctx,
106 const char *cache_dir,
107 const char *unix_path)
109 char *current_dir;
110 char *tok;
112 current_dir = talloc_strdup(mem_ctx, cache_dir);
113 NT_STATUS_HAVE_NO_MEMORY(current_dir);
115 if ((mkdir(cache_dir, 0644)) < 0 && errno != EEXIST) {
116 return NT_STATUS_ACCESS_DENIED;
119 while (next_token_talloc(mem_ctx, &unix_path, &tok, "/")) {
120 if (strequal(tok, GPO_CACHE_DIR)) {
121 break;
125 while (next_token_talloc(mem_ctx, &unix_path, &tok, "/")) {
126 current_dir = talloc_asprintf_append_buffer(current_dir, "/%s", tok);
127 NT_STATUS_HAVE_NO_MEMORY(current_dir);
129 if ((mkdir(current_dir, 0644)) < 0 && errno != EEXIST) {
130 return NT_STATUS_ACCESS_DENIED;
134 return NT_STATUS_OK;
137 static NTSTATUS gpo_connect_server(ADS_STRUCT *ads, struct loadparm_context *lp_ctx,
138 const char *server, const char *service, void *ret_cli)
140 NTSTATUS result;
141 #if _SAMBA_BUILD_ == 3
142 struct cli_state *cli;
145 result = cli_full_connection(&cli,
146 global_myname(),
147 server,
148 NULL, 0,
149 service, "A:",
150 ads->auth.user_name, NULL,
151 ads->auth.password,
152 CLI_FULL_CONNECTION_USE_KERBEROS |
153 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS,
154 Undefined);
155 if (!NT_STATUS_IS_OK(result)) {
156 DEBUG(10,("check_refresh_gpo: "
157 "failed to connect: %s\n",
158 nt_errstr(result)));
159 return result;
161 *(struct cli_state **) ret_cli = cli;
162 #else
163 struct smbcli_state *cli = NULL;
164 struct smbcli_options options;
165 struct smbcli_session_options session_options;
167 lp_smbcli_options(lp_ctx, &options);
168 lp_smbcli_session_options(lp_ctx, &session_options);
170 result = smbcli_full_connection(NULL, &cli,
171 server,
172 NULL, service,
173 NULL /*devtype*/, NULL /* socket options */,
174 ads->credentials,
175 lp_resolve_context(lp_ctx),
176 tevent_context_init(ads),
177 &options,
178 &session_options,
179 lp_iconv_convenience(lp_ctx),
180 lp_gensec_settings(ads, lp_ctx));
181 if (!NT_STATUS_IS_OK(result)) {
182 DEBUG(10,("failed to connect: %s\n",
183 nt_errstr(result)));
184 return result;
186 *(struct smbcli_state **) ret_cli = cli;
187 #endif
188 return NT_STATUS_OK;
191 /****************************************************************
192 download a full GPO via CIFS
193 ****************************************************************/
195 NTSTATUS gpo_fetch_files(TALLOC_CTX *mem_ctx,
196 ADS_STRUCT *ads,
197 struct loadparm_context *lp_ctx,
198 const char *cache_dir,
199 struct GROUP_POLICY_OBJECT *gpo)
201 NTSTATUS result;
202 char *server, *service, *nt_path, *unix_path;
203 char *nt_ini_path, *unix_ini_path;
204 #if _SAMBA_BUILD_ == 3
205 struct cli_state *cli;
206 #else
207 struct smbcli_state *cli;
208 #endif
211 result = gpo_explode_filesyspath(mem_ctx, cache_dir, gpo->file_sys_path,
212 &server, &service, &nt_path,
213 &unix_path);
214 NT_STATUS_NOT_OK_RETURN(result);
217 result = gpo_connect_server(ads, lp_ctx, server, service, &cli);
218 NT_STATUS_NOT_OK_RETURN(result);
221 result = gpo_prepare_local_store(mem_ctx, cache_dir, unix_path);
222 NT_STATUS_NOT_OK_RETURN(result);
224 unix_ini_path = talloc_asprintf(mem_ctx, "%s/%s", unix_path, GPT_INI);
225 nt_ini_path = talloc_asprintf(mem_ctx, "%s\\%s", nt_path, GPT_INI);
226 NT_STATUS_HAVE_NO_MEMORY(unix_ini_path);
227 NT_STATUS_HAVE_NO_MEMORY(nt_ini_path);
229 result = gpo_copy_file(mem_ctx, cli, nt_ini_path, unix_ini_path);
230 NT_STATUS_NOT_OK_RETURN(result);
232 result = gpo_sync_directories(mem_ctx, cli, nt_path, unix_path);
233 NT_STATUS_NOT_OK_RETURN(result);
235 return NT_STATUS_OK;
238 /****************************************************************
239 get the locally stored gpt.ini version number
240 ****************************************************************/
242 NTSTATUS gpo_get_sysvol_gpt_version(TALLOC_CTX *mem_ctx,
243 const char *unix_path,
244 uint32_t *sysvol_version,
245 char **display_name)
247 NTSTATUS status;
248 uint32_t version = 0;
249 char *local_path = NULL;
250 char *name = NULL;
252 if (!unix_path) {
253 return NT_STATUS_OK;
256 local_path = talloc_asprintf(mem_ctx, "%s/%s", unix_path, GPT_INI);
257 NT_STATUS_HAVE_NO_MEMORY(local_path);
259 status = parse_gpt_ini(mem_ctx, local_path, &version, &name);
260 if (!NT_STATUS_IS_OK(status)) {
261 DEBUG(10,("gpo_get_sysvol_gpt_version: "
262 "failed to parse ini [%s]: %s\n",
263 local_path, nt_errstr(status)));
264 return status;
267 if (sysvol_version) {
268 *sysvol_version = version;
271 if (name && *display_name) {
272 *display_name = talloc_strdup(mem_ctx, name);
273 NT_STATUS_HAVE_NO_MEMORY(*display_name);
276 return NT_STATUS_OK;