tiny simplification
[Samba/gebeck_regimport.git] / source3 / libgpo / gpo_fetch.c
blob916db2b3d354d36c36fa2da9acbbfb58b6f32ac9
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"
22 /****************************************************************
23 explode the GPO CIFS URI into their components
24 ****************************************************************/
26 NTSTATUS gpo_explode_filesyspath(TALLOC_CTX *mem_ctx,
27 const char *file_sys_path,
28 char **server,
29 char **service,
30 char **nt_path,
31 char **unix_path)
33 char *path = NULL;
35 *server = NULL;
36 *service = NULL;
37 *nt_path = NULL;
38 *unix_path = NULL;
40 if (!file_sys_path) {
41 return NT_STATUS_OK;
44 if (!next_token_talloc(mem_ctx, &file_sys_path, server, "\\")) {
45 return NT_STATUS_INVALID_PARAMETER;
48 if (!next_token_talloc(mem_ctx, &file_sys_path, service, "\\")) {
49 return NT_STATUS_INVALID_PARAMETER;
52 if ((*nt_path = talloc_asprintf(mem_ctx, "\\%s", file_sys_path))
53 == NULL) {
54 return NT_STATUS_NO_MEMORY;
57 if ((path = talloc_asprintf(mem_ctx,
58 "%s/%s",
59 lock_path(GPO_CACHE_DIR),
60 file_sys_path)) == NULL) {
61 return NT_STATUS_NO_MEMORY;
63 path = talloc_string_sub(mem_ctx, path, "\\", "/");
64 if (!path) {
65 return NT_STATUS_NO_MEMORY;
68 if ((*unix_path = talloc_strdup(mem_ctx, path)) == NULL) {
69 return NT_STATUS_NO_MEMORY;
72 TALLOC_FREE(path);
73 return NT_STATUS_OK;
76 /****************************************************************
77 prepare the local disc storage for "unix_path"
78 ****************************************************************/
80 static NTSTATUS gpo_prepare_local_store(TALLOC_CTX *mem_ctx,
81 const char *unix_path)
83 const char *top_dir = lock_path(GPO_CACHE_DIR);
84 char *current_dir;
85 char *tok;
87 current_dir = talloc_strdup(mem_ctx, top_dir);
88 NT_STATUS_HAVE_NO_MEMORY(current_dir);
90 if ((mkdir(top_dir, 0644)) < 0 && errno != EEXIST) {
91 return NT_STATUS_ACCESS_DENIED;
94 while (next_token_talloc(mem_ctx, &unix_path, &tok, "/")) {
95 if (strequal(tok, GPO_CACHE_DIR)) {
96 break;
100 while (next_token_talloc(mem_ctx, &unix_path, &tok, "/")) {
101 current_dir = talloc_asprintf_append_buffer(current_dir, "/%s", tok);
102 NT_STATUS_HAVE_NO_MEMORY(current_dir);
104 if ((mkdir(current_dir, 0644)) < 0 && errno != EEXIST) {
105 return NT_STATUS_ACCESS_DENIED;
109 return NT_STATUS_OK;
112 /****************************************************************
113 download a full GPO via CIFS
114 ****************************************************************/
116 NTSTATUS gpo_fetch_files(TALLOC_CTX *mem_ctx,
117 struct cli_state *cli,
118 struct GROUP_POLICY_OBJECT *gpo)
120 NTSTATUS result;
121 char *server, *service, *nt_path, *unix_path;
122 char *nt_ini_path, *unix_ini_path;
124 result = gpo_explode_filesyspath(mem_ctx, gpo->file_sys_path,
125 &server, &service, &nt_path,
126 &unix_path);
127 if (!NT_STATUS_IS_OK(result)) {
128 goto out;
131 result = gpo_prepare_local_store(mem_ctx, unix_path);
132 if (!NT_STATUS_IS_OK(result)) {
133 goto out;
136 unix_ini_path = talloc_asprintf(mem_ctx, "%s/%s", unix_path, GPT_INI);
137 nt_ini_path = talloc_asprintf(mem_ctx, "%s\\%s", nt_path, GPT_INI);
138 if (!unix_path || !nt_ini_path) {
139 result = NT_STATUS_NO_MEMORY;
140 goto out;
143 result = gpo_copy_file(mem_ctx, cli, nt_ini_path, unix_ini_path);
144 if (!NT_STATUS_IS_OK(result)) {
145 goto out;
148 result = gpo_sync_directories(mem_ctx, cli, nt_path, unix_path);
149 if (!NT_STATUS_IS_OK(result)) {
150 goto out;
153 result = NT_STATUS_OK;
155 out:
156 return result;
159 /****************************************************************
160 get the locally stored gpt.ini version number
161 ****************************************************************/
163 NTSTATUS gpo_get_sysvol_gpt_version(TALLOC_CTX *mem_ctx,
164 const char *unix_path,
165 uint32_t *sysvol_version,
166 char **display_name)
168 NTSTATUS status;
169 uint32_t version = 0;
170 char *local_path = NULL;
171 char *name = NULL;
173 if (!unix_path) {
174 return NT_STATUS_OK;
177 local_path = talloc_asprintf(mem_ctx, "%s/%s", unix_path, GPT_INI);
178 NT_STATUS_HAVE_NO_MEMORY(local_path);
180 status = parse_gpt_ini(mem_ctx, local_path, &version, &name);
181 if (!NT_STATUS_IS_OK(status)) {
182 DEBUG(10,("gpo_get_sysvol_gpt_version: "
183 "failed to parse ini [%s]: %s\n",
184 local_path, nt_errstr(status)));
185 return status;
188 if (sysvol_version) {
189 *sysvol_version = version;
192 if (name && *display_name) {
193 *display_name = talloc_strdup(mem_ctx, name);
194 NT_STATUS_HAVE_NO_MEMORY(*display_name);
197 return NT_STATUS_OK;