9baa2385f11139edc27e79e8635813dff20cb312
[kai/samba-autobuild/.git] / source / 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, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "includes.h"
22
23 /****************************************************************
24  explode the GPO CIFS URI into their components
25 ****************************************************************/
26
27 NTSTATUS gpo_explode_filesyspath(TALLOC_CTX *mem_ctx, 
28                                  const char *file_sys_path, 
29                                  char **server, 
30                                  char **service, 
31                                  char **nt_path,
32                                  char **unix_path)
33 {
34         fstring tok;
35         pstring path;
36
37         *server = NULL;
38         *service = NULL;
39         *nt_path = NULL;
40         *unix_path = NULL;
41
42         if (!file_sys_path) {
43                 return NT_STATUS_OK;
44         }
45
46         if (!next_token(&file_sys_path, tok, "\\", sizeof(tok))) {
47                 return NT_STATUS_INVALID_PARAMETER;
48         }
49
50         if ((*server = talloc_strdup(mem_ctx, tok)) == NULL) {
51                 return NT_STATUS_NO_MEMORY;
52         }
53
54         if (!next_token(&file_sys_path, tok, "\\", sizeof(tok))) {
55                 return NT_STATUS_INVALID_PARAMETER;
56         }
57
58         if ((*service = talloc_strdup(mem_ctx, tok)) == NULL) {
59                 return NT_STATUS_NO_MEMORY;
60         }
61
62         if ((*nt_path = talloc_asprintf(mem_ctx, "\\%s", file_sys_path)) == NULL) {
63                 return NT_STATUS_NO_MEMORY;
64         }
65
66         pstrcpy(path, lock_path(GPO_CACHE_DIR));
67         pstrcat(path, "/");
68         pstrcat(path, file_sys_path);
69         pstring_sub(path, "\\", "/");
70
71         if ((*unix_path = talloc_strdup(mem_ctx, path)) == NULL) {
72                 return NT_STATUS_NO_MEMORY;
73         }
74
75         return NT_STATUS_OK;
76 }
77
78 /****************************************************************
79  prepare the local disc storage for "unix_path"
80 ****************************************************************/
81
82 static NTSTATUS gpo_prepare_local_store(TALLOC_CTX *mem_ctx, 
83                                         const char *unix_path)
84 {
85         const char *top_dir = lock_path(GPO_CACHE_DIR);
86         char *current_dir;
87         fstring tok;
88
89         current_dir = talloc_strdup(mem_ctx, top_dir);
90         NT_STATUS_HAVE_NO_MEMORY(current_dir);
91
92         if ((mkdir(top_dir, 0644)) < 0 && errno != EEXIST) {
93                 return NT_STATUS_ACCESS_DENIED;
94         }
95
96         while (next_token(&unix_path, tok, "/", sizeof(tok))) {
97         
98                 if (strequal(tok, GPO_CACHE_DIR)) {
99                         break;
100                 }
101         }
102
103         while (next_token(&unix_path, tok, "/", sizeof(tok))) {
104
105                 current_dir = talloc_asprintf_append(current_dir, "/%s", tok);
106                 NT_STATUS_HAVE_NO_MEMORY(current_dir);
107
108                 if ((mkdir(current_dir, 0644)) < 0 && errno != EEXIST) {
109                         return NT_STATUS_ACCESS_DENIED;
110                 }
111         }
112
113         return NT_STATUS_OK;
114 }
115
116 /****************************************************************
117  download a full GPO via CIFS
118 ****************************************************************/
119
120 NTSTATUS gpo_fetch_files(TALLOC_CTX *mem_ctx, 
121                          struct cli_state *cli, 
122                          struct GROUP_POLICY_OBJECT *gpo)
123 {
124         NTSTATUS result;
125         char *server, *service, *nt_path, *unix_path, *nt_ini_path, *unix_ini_path;
126
127         result = gpo_explode_filesyspath(mem_ctx, gpo->file_sys_path, 
128                                          &server, &service, &nt_path, &unix_path);
129         if (!NT_STATUS_IS_OK(result)) {
130                 goto out;
131         }
132
133         result = gpo_prepare_local_store(mem_ctx, unix_path);
134         if (!NT_STATUS_IS_OK(result)) {
135                 goto out;
136         }
137
138         unix_ini_path = talloc_asprintf(mem_ctx, "%s/%s", unix_path, GPT_INI);
139         nt_ini_path = talloc_asprintf(mem_ctx, "%s\\%s", nt_path, GPT_INI);
140         if (!unix_path || !nt_ini_path) {
141                 result = NT_STATUS_NO_MEMORY;
142                 goto out;
143         }
144
145         result = gpo_copy_file(mem_ctx, cli, nt_ini_path, unix_ini_path);
146         if (!NT_STATUS_IS_OK(result)) {
147                 goto out;
148         }
149
150         result = gpo_sync_directories(mem_ctx, cli, nt_path, unix_path);
151         if (!NT_STATUS_IS_OK(result)) {
152                 goto out;
153         }
154
155         result = NT_STATUS_OK;
156
157  out:
158         return result;
159 }
160
161 /****************************************************************
162  get the locally stored gpt.ini version number
163 ****************************************************************/
164
165 NTSTATUS gpo_get_sysvol_gpt_version(TALLOC_CTX *mem_ctx, 
166                                     const char *unix_path, 
167                                     uint32 *sysvol_version,
168                                     char **display_name)
169 {
170         NTSTATUS status;
171         uint32 version = 0;
172         char *local_path = NULL;
173         char *name = NULL;
174
175         if (!unix_path) {
176                 return NT_STATUS_OK;
177         }
178
179         local_path = talloc_asprintf(mem_ctx, "%s/%s", unix_path, GPT_INI);
180         NT_STATUS_HAVE_NO_MEMORY(local_path);
181
182         status = parse_gpt_ini(mem_ctx, local_path, &version, &name);
183         if (!NT_STATUS_IS_OK(status)) {
184                 DEBUG(10,("gpo_get_sysvol_gpt_version: failed to parse ini [%s]: %s\n", 
185                         unix_path, nt_errstr(status)));
186                 return status;
187         }
188
189         if (sysvol_version) {
190                 *sysvol_version = version;
191         }
192
193         if (name && *display_name) {
194                 *display_name = talloc_strdup(mem_ctx, name);
195                 NT_STATUS_HAVE_NO_MEMORY(*display_name);
196         }
197
198         return NT_STATUS_OK;
199 }