7e41812789e4d292c7f83637fb6168e0130489e9
[kai/samba.git] / libgpo / gpo_fetch.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Group Policy Object Support
4  *  Copyright (C) Guenther Deschner 2005-2006
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "../libgpo/gpo.h"
23
24 #if _SAMBA_BUILD_ == 4
25 #include "param/param.h"
26 #include "libcli/resolve/resolve.h"
27 #include "../lib/tevent/tevent.h"
28 #include "libcli/libcli.h"
29 #include "libcli/raw/libcliraw.h"
30 #include "libcli/libcli_proto.h"
31 #include "libgpo/ads_convenience.h"
32 #include "libgpo/gpo.h"
33 #endif
34
35 /****************************************************************
36  explode the GPO CIFS URI into their components
37 ****************************************************************/
38
39 NTSTATUS gpo_explode_filesyspath(TALLOC_CTX *mem_ctx,
40                                  const char *cache_dir,
41                                  const char *file_sys_path,
42                                  char **server,
43                                  char **service,
44                                  char **nt_path,
45                                  char **unix_path)
46 {
47         char *path = NULL;
48
49         *server = NULL;
50         *service = NULL;
51         *nt_path = NULL;
52         *unix_path = NULL;
53
54         if (!file_sys_path) {
55                 return NT_STATUS_OK;
56         }
57
58         if (!next_token_talloc(mem_ctx, &file_sys_path, server, "\\")) {
59                 return NT_STATUS_INVALID_PARAMETER;
60         }
61         NT_STATUS_HAVE_NO_MEMORY(*server);
62
63         if (!next_token_talloc(mem_ctx, &file_sys_path, service, "\\")) {
64                 return NT_STATUS_INVALID_PARAMETER;
65         }
66         NT_STATUS_HAVE_NO_MEMORY(*service);
67
68         if ((*nt_path = talloc_asprintf(mem_ctx, "\\%s", file_sys_path))
69                 == NULL) {
70                 return NT_STATUS_NO_MEMORY;
71         }
72         NT_STATUS_HAVE_NO_MEMORY(*nt_path);
73
74         if ((path = talloc_asprintf(mem_ctx,
75                                         "%s/%s",
76                                         cache_dir,
77                                         file_sys_path)) == NULL) {
78                 return NT_STATUS_NO_MEMORY;
79         }
80 #if _SAMBA_BUILD_ == 4
81         path = string_sub_talloc(mem_ctx, path, "\\", "/");
82 #else
83         path = talloc_string_sub(mem_ctx, path, "\\", "/");
84 #endif
85         if (!path) {
86                 return NT_STATUS_NO_MEMORY;
87         }
88
89         *unix_path = talloc_strdup(mem_ctx, path);
90         NT_STATUS_HAVE_NO_MEMORY(*unix_path);
91
92         talloc_free(path);
93         return NT_STATUS_OK;
94 }
95
96 /****************************************************************
97  prepare the local disc storage for "unix_path"
98 ****************************************************************/
99
100 static NTSTATUS gpo_prepare_local_store(TALLOC_CTX *mem_ctx,
101                                         const char *cache_dir,
102                                         const char *unix_path)
103 {
104         char *current_dir;
105         char *tok;
106
107         current_dir = talloc_strdup(mem_ctx, cache_dir);
108         NT_STATUS_HAVE_NO_MEMORY(current_dir);
109
110         if ((mkdir(cache_dir, 0644)) < 0 && errno != EEXIST) {
111                 return NT_STATUS_ACCESS_DENIED;
112         }
113
114         while (next_token_talloc(mem_ctx, &unix_path, &tok, "/")) {
115                 if (strequal(tok, cache_dir)) {
116                         break;
117                 }
118         }
119
120         while (next_token_talloc(mem_ctx, &unix_path, &tok, "/")) {
121                 current_dir = talloc_asprintf_append_buffer(current_dir, "/%s", tok);
122                 NT_STATUS_HAVE_NO_MEMORY(current_dir);
123
124                 if ((mkdir(current_dir, 0644)) < 0 && errno != EEXIST) {
125                         return NT_STATUS_ACCESS_DENIED;
126                 }
127         }
128
129         return NT_STATUS_OK;
130 }
131
132 static NTSTATUS gpo_connect_server(ADS_STRUCT *ads, struct loadparm_context *lp_ctx,
133                                    const char *server, const char *service, void *ret_cli)
134 {
135         NTSTATUS result;
136 #if _SAMBA_BUILD_ == 3
137         struct cli_state *cli;
138
139
140         result = cli_full_connection(&cli,
141                         global_myname(),
142                         server,
143                         NULL, 0,
144                         service, "A:",
145                         ads->auth.user_name, NULL,
146                         ads->auth.password,
147                         CLI_FULL_CONNECTION_USE_KERBEROS |
148                         CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS,
149                         Undefined, NULL);
150         if (!NT_STATUS_IS_OK(result)) {
151                 DEBUG(10,("check_refresh_gpo: "
152                                 "failed to connect: %s\n",
153                                 nt_errstr(result)));
154                 return result;
155         }
156         *(struct cli_state **) ret_cli = cli;
157 #else
158         struct smbcli_state *cli = NULL;
159         struct smbcli_options options;
160         struct smbcli_session_options session_options;
161
162         lp_smbcli_options(lp_ctx, &options);
163         lp_smbcli_session_options(lp_ctx, &session_options);
164
165         result = smbcli_full_connection(NULL, &cli,
166                         server,
167                         NULL, service,
168                         NULL /*devtype*/, NULL /* socket options */,
169                         ads->credentials,
170                         lp_resolve_context(lp_ctx),
171                         tevent_context_init(ads),
172                         &options,
173                         &session_options,
174                         lp_iconv_convenience(lp_ctx),
175                         lp_gensec_settings(ads, lp_ctx));
176         if (!NT_STATUS_IS_OK(result)) {
177                 DEBUG(10,("failed to connect: %s\n",
178                                 nt_errstr(result)));
179                 return result;
180         }
181         *(struct smbcli_state **) ret_cli = cli;
182 #endif
183         return NT_STATUS_OK;
184 }
185
186 /****************************************************************
187  download a full GPO via CIFS
188 ****************************************************************/
189
190 NTSTATUS gpo_fetch_files(TALLOC_CTX *mem_ctx,
191                          ADS_STRUCT *ads,
192                          struct loadparm_context *lp_ctx,
193                          const char *cache_dir,
194                          struct GROUP_POLICY_OBJECT *gpo)
195 {
196         NTSTATUS result;
197         char *server, *service, *nt_path, *unix_path;
198         char *nt_ini_path, *unix_ini_path;
199 #if _SAMBA_BUILD_ == 3
200         struct cli_state *cli;
201 #else
202         struct smbcli_state *cli;
203 #endif
204
205
206         result = gpo_explode_filesyspath(mem_ctx, cache_dir, gpo->file_sys_path,
207                                          &server, &service, &nt_path,
208                                          &unix_path);
209         NT_STATUS_NOT_OK_RETURN(result);
210
211
212         result = gpo_connect_server(ads, lp_ctx, server, service, &cli);
213
214
215         result = gpo_prepare_local_store(mem_ctx, cache_dir, unix_path);
216         NT_STATUS_NOT_OK_RETURN(result);
217
218         unix_ini_path = talloc_asprintf(mem_ctx, "%s/%s", unix_path, GPT_INI);
219         nt_ini_path = talloc_asprintf(mem_ctx, "%s\\%s", nt_path, GPT_INI);
220         NT_STATUS_HAVE_NO_MEMORY(unix_ini_path);
221         NT_STATUS_HAVE_NO_MEMORY(nt_ini_path);
222
223         result = gpo_copy_file(mem_ctx, cli, nt_ini_path, unix_ini_path);
224         NT_STATUS_NOT_OK_RETURN(result);
225
226         result = gpo_sync_directories(mem_ctx, cli, nt_path, unix_path);
227         NT_STATUS_NOT_OK_RETURN(result);
228
229         return NT_STATUS_OK;
230 }
231
232 /****************************************************************
233  get the locally stored gpt.ini version number
234 ****************************************************************/
235
236 NTSTATUS gpo_get_sysvol_gpt_version(TALLOC_CTX *mem_ctx,
237                                     const char *unix_path,
238                                     uint32_t *sysvol_version,
239                                     char **display_name)
240 {
241         NTSTATUS status;
242         uint32_t version = 0;
243         char *local_path = NULL;
244         char *name = NULL;
245
246         if (!unix_path) {
247                 return NT_STATUS_OK;
248         }
249
250         local_path = talloc_asprintf(mem_ctx, "%s/%s", unix_path, GPT_INI);
251         NT_STATUS_HAVE_NO_MEMORY(local_path);
252
253         status = parse_gpt_ini(mem_ctx, local_path, &version, &name);
254         if (!NT_STATUS_IS_OK(status)) {
255                 DEBUG(10,("gpo_get_sysvol_gpt_version: "
256                         "failed to parse ini [%s]: %s\n",
257                         local_path, nt_errstr(status)));
258                 return status;
259         }
260
261         if (sysvol_version) {
262                 *sysvol_version = version;
263         }
264
265         if (name && *display_name) {
266                 *display_name = talloc_strdup(mem_ctx, name);
267                 NT_STATUS_HAVE_NO_MEMORY(*display_name);
268         }
269
270         return NT_STATUS_OK;
271 }