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