s3: Set the client_id in substitute.c once
[Samba.git] / libgpo / gpo_fetch.c
blobad6c2a80a0a1fd65c9a7f222537d4ee60c7f745f
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 #endif
39 /****************************************************************
40 explode the GPO CIFS URI into their components
41 ****************************************************************/
43 NTSTATUS gpo_explode_filesyspath(TALLOC_CTX *mem_ctx,
44 const char *cache_dir,
45 const char *file_sys_path,
46 char **server,
47 char **service,
48 char **nt_path,
49 char **unix_path)
51 char *path = NULL;
53 *server = NULL;
54 *service = NULL;
55 *nt_path = NULL;
56 *unix_path = NULL;
58 if (!file_sys_path) {
59 return NT_STATUS_OK;
62 if (!next_token_talloc(mem_ctx, &file_sys_path, server, "\\")) {
63 return NT_STATUS_INVALID_PARAMETER;
65 NT_STATUS_HAVE_NO_MEMORY(*server);
67 if (!next_token_talloc(mem_ctx, &file_sys_path, service, "\\")) {
68 return NT_STATUS_INVALID_PARAMETER;
70 NT_STATUS_HAVE_NO_MEMORY(*service);
72 if ((*nt_path = talloc_asprintf(mem_ctx, "\\%s", file_sys_path))
73 == NULL) {
74 return NT_STATUS_NO_MEMORY;
76 NT_STATUS_HAVE_NO_MEMORY(*nt_path);
78 if ((path = talloc_asprintf(mem_ctx,
79 "%s/%s",
80 cache_dir,
81 file_sys_path)) == NULL) {
82 return NT_STATUS_NO_MEMORY;
84 #if _SAMBA_BUILD_ == 4
85 path = string_sub_talloc(mem_ctx, path, "\\", "/");
86 #else
87 path = talloc_string_sub(mem_ctx, path, "\\", "/");
88 #endif
89 if (!path) {
90 return NT_STATUS_NO_MEMORY;
93 *unix_path = talloc_strdup(mem_ctx, path);
94 NT_STATUS_HAVE_NO_MEMORY(*unix_path);
96 talloc_free(path);
97 return NT_STATUS_OK;
100 /****************************************************************
101 prepare the local disc storage for "unix_path"
102 ****************************************************************/
104 static NTSTATUS gpo_prepare_local_store(TALLOC_CTX *mem_ctx,
105 const char *cache_dir,
106 const char *unix_path)
108 char *current_dir;
109 char *tok;
111 current_dir = talloc_strdup(mem_ctx, cache_dir);
112 NT_STATUS_HAVE_NO_MEMORY(current_dir);
114 if ((mkdir(cache_dir, 0644)) < 0 && errno != EEXIST) {
115 return NT_STATUS_ACCESS_DENIED;
118 while (next_token_talloc(mem_ctx, &unix_path, &tok, "/")) {
119 if (strequal(tok, GPO_CACHE_DIR)) {
120 break;
124 while (next_token_talloc(mem_ctx, &unix_path, &tok, "/")) {
125 current_dir = talloc_asprintf_append_buffer(current_dir, "/%s", tok);
126 NT_STATUS_HAVE_NO_MEMORY(current_dir);
128 if ((mkdir(current_dir, 0644)) < 0 && errno != EEXIST) {
129 return NT_STATUS_ACCESS_DENIED;
133 return NT_STATUS_OK;
136 static NTSTATUS gpo_connect_server(ADS_STRUCT *ads, struct loadparm_context *lp_ctx,
137 const char *server, const char *service, void *ret_cli)
139 NTSTATUS result;
140 #if _SAMBA_BUILD_ == 3
141 struct cli_state *cli;
144 result = cli_full_connection(&cli,
145 global_myname(),
146 server,
147 NULL, 0,
148 service, "A:",
149 ads->auth.user_name, NULL,
150 ads->auth.password,
151 CLI_FULL_CONNECTION_USE_KERBEROS |
152 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS,
153 Undefined, NULL);
154 if (!NT_STATUS_IS_OK(result)) {
155 DEBUG(10,("check_refresh_gpo: "
156 "failed to connect: %s\n",
157 nt_errstr(result)));
158 return result;
160 *(struct cli_state **) ret_cli = cli;
161 #else
162 struct smbcli_state *cli = NULL;
163 struct smbcli_options options;
164 struct smbcli_session_options session_options;
166 lp_smbcli_options(lp_ctx, &options);
167 lp_smbcli_session_options(lp_ctx, &session_options);
169 result = smbcli_full_connection(NULL, &cli,
170 server,
171 NULL, service,
172 NULL /*devtype*/, NULL /* socket options */,
173 ads->credentials,
174 lp_resolve_context(lp_ctx),
175 tevent_context_init(ads),
176 &options,
177 &session_options,
178 lp_iconv_convenience(lp_ctx),
179 lp_gensec_settings(ads, lp_ctx));
180 if (!NT_STATUS_IS_OK(result)) {
181 DEBUG(10,("failed to connect: %s\n",
182 nt_errstr(result)));
183 return result;
185 *(struct smbcli_state **) ret_cli = cli;
186 #endif
187 return NT_STATUS_OK;
190 /****************************************************************
191 download a full GPO via CIFS
192 ****************************************************************/
194 NTSTATUS gpo_fetch_files(TALLOC_CTX *mem_ctx,
195 ADS_STRUCT *ads,
196 struct loadparm_context *lp_ctx,
197 const char *cache_dir,
198 struct GROUP_POLICY_OBJECT *gpo)
200 NTSTATUS result;
201 char *server, *service, *nt_path, *unix_path;
202 char *nt_ini_path, *unix_ini_path;
203 #if _SAMBA_BUILD_ == 3
204 struct cli_state *cli;
205 #else
206 struct smbcli_state *cli;
207 #endif
210 result = gpo_explode_filesyspath(mem_ctx, cache_dir, gpo->file_sys_path,
211 &server, &service, &nt_path,
212 &unix_path);
213 NT_STATUS_NOT_OK_RETURN(result);
216 result = gpo_connect_server(ads, lp_ctx, server, service, &cli);
217 NT_STATUS_NOT_OK_RETURN(result);
220 result = gpo_prepare_local_store(mem_ctx, cache_dir, unix_path);
221 NT_STATUS_NOT_OK_RETURN(result);
223 unix_ini_path = talloc_asprintf(mem_ctx, "%s/%s", unix_path, GPT_INI);
224 nt_ini_path = talloc_asprintf(mem_ctx, "%s\\%s", nt_path, GPT_INI);
225 NT_STATUS_HAVE_NO_MEMORY(unix_ini_path);
226 NT_STATUS_HAVE_NO_MEMORY(nt_ini_path);
228 result = gpo_copy_file(mem_ctx, cli, nt_ini_path, unix_ini_path);
229 NT_STATUS_NOT_OK_RETURN(result);
231 result = gpo_sync_directories(mem_ctx, cli, nt_path, unix_path);
232 NT_STATUS_NOT_OK_RETURN(result);
234 return NT_STATUS_OK;
237 /****************************************************************
238 get the locally stored gpt.ini version number
239 ****************************************************************/
241 NTSTATUS gpo_get_sysvol_gpt_version(TALLOC_CTX *mem_ctx,
242 const char *unix_path,
243 uint32_t *sysvol_version,
244 char **display_name)
246 NTSTATUS status;
247 uint32_t version = 0;
248 char *local_path = NULL;
249 char *name = NULL;
251 if (!unix_path) {
252 return NT_STATUS_OK;
255 local_path = talloc_asprintf(mem_ctx, "%s/%s", unix_path, GPT_INI);
256 NT_STATUS_HAVE_NO_MEMORY(local_path);
258 status = parse_gpt_ini(mem_ctx, local_path, &version, &name);
259 if (!NT_STATUS_IS_OK(status)) {
260 DEBUG(10,("gpo_get_sysvol_gpt_version: "
261 "failed to parse ini [%s]: %s\n",
262 local_path, nt_errstr(status)));
263 return status;
266 if (sysvol_version) {
267 *sysvol_version = version;
270 if (name && *display_name) {
271 *display_name = talloc_strdup(mem_ctx, name);
272 NT_STATUS_HAVE_NO_MEMORY(*display_name);
275 return NT_STATUS_OK;