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