03759262cd6cbdd1ddd0e3df9b9a33d9665888e3
[jra/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 /****************************************************************
25  explode the GPO CIFS URI into their components
26 ****************************************************************/
27
28 NTSTATUS gpo_explode_filesyspath(TALLOC_CTX *mem_ctx,
29                                  const char *file_sys_path,
30                                  char **server,
31                                  char **service,
32                                  char **nt_path,
33                                  char **unix_path)
34 {
35         char *path = NULL;
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_talloc(mem_ctx, &file_sys_path, server, "\\")) {
47                 return NT_STATUS_INVALID_PARAMETER;
48         }
49         NT_STATUS_HAVE_NO_MEMORY(*server);
50
51         if (!next_token_talloc(mem_ctx, &file_sys_path, service, "\\")) {
52                 return NT_STATUS_INVALID_PARAMETER;
53         }
54         NT_STATUS_HAVE_NO_MEMORY(*service);
55
56         if ((*nt_path = talloc_asprintf(mem_ctx, "\\%s", file_sys_path))
57                 == NULL) {
58                 return NT_STATUS_NO_MEMORY;
59         }
60         NT_STATUS_HAVE_NO_MEMORY(*nt_path);
61
62         if ((path = talloc_asprintf(mem_ctx,
63                                         "%s/%s",
64                                         cache_path(GPO_CACHE_DIR),
65                                         file_sys_path)) == NULL) {
66                 return NT_STATUS_NO_MEMORY;
67         }
68         path = talloc_string_sub(mem_ctx, path, "\\", "/");
69         if (!path) {
70                 return NT_STATUS_NO_MEMORY;
71         }
72
73         *unix_path = talloc_strdup(mem_ctx, path);
74         NT_STATUS_HAVE_NO_MEMORY(*unix_path);
75
76         talloc_free(path);
77         return NT_STATUS_OK;
78 }
79
80 /****************************************************************
81  prepare the local disc storage for "unix_path"
82 ****************************************************************/
83
84 static NTSTATUS gpo_prepare_local_store(TALLOC_CTX *mem_ctx,
85                                         const char *unix_path)
86 {
87         const char *top_dir = cache_path(GPO_CACHE_DIR);
88         char *current_dir;
89         char *tok;
90
91         current_dir = talloc_strdup(mem_ctx, top_dir);
92         NT_STATUS_HAVE_NO_MEMORY(current_dir);
93
94         if ((mkdir(top_dir, 0644)) < 0 && errno != EEXIST) {
95                 return NT_STATUS_ACCESS_DENIED;
96         }
97
98         while (next_token_talloc(mem_ctx, &unix_path, &tok, "/")) {
99                 if (strequal(tok, GPO_CACHE_DIR)) {
100                         break;
101                 }
102         }
103
104         while (next_token_talloc(mem_ctx, &unix_path, &tok, "/")) {
105                 current_dir = talloc_asprintf_append_buffer(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;
126         char *nt_ini_path, *unix_ini_path;
127
128         result = gpo_explode_filesyspath(mem_ctx, gpo->file_sys_path,
129                                          &server, &service, &nt_path,
130                                          &unix_path);
131         NT_STATUS_NOT_OK_RETURN(result);
132
133         result = gpo_prepare_local_store(mem_ctx, unix_path);
134         NT_STATUS_NOT_OK_RETURN(result);
135
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         NT_STATUS_HAVE_NO_MEMORY(unix_ini_path);
139         NT_STATUS_HAVE_NO_MEMORY(nt_ini_path);
140
141         result = gpo_copy_file(mem_ctx, cli, nt_ini_path, unix_ini_path);
142         NT_STATUS_NOT_OK_RETURN(result);
143
144         result = gpo_sync_directories(mem_ctx, cli, nt_path, unix_path);
145         NT_STATUS_NOT_OK_RETURN(result);
146
147         return NT_STATUS_OK;
148 }
149
150 /****************************************************************
151  get the locally stored gpt.ini version number
152 ****************************************************************/
153
154 NTSTATUS gpo_get_sysvol_gpt_version(TALLOC_CTX *mem_ctx,
155                                     const char *unix_path,
156                                     uint32_t *sysvol_version,
157                                     char **display_name)
158 {
159         NTSTATUS status;
160         uint32_t version = 0;
161         char *local_path = NULL;
162         char *name = NULL;
163
164         if (!unix_path) {
165                 return NT_STATUS_OK;
166         }
167
168         local_path = talloc_asprintf(mem_ctx, "%s/%s", unix_path, GPT_INI);
169         NT_STATUS_HAVE_NO_MEMORY(local_path);
170
171         status = parse_gpt_ini(mem_ctx, local_path, &version, &name);
172         if (!NT_STATUS_IS_OK(status)) {
173                 DEBUG(10,("gpo_get_sysvol_gpt_version: "
174                         "failed to parse ini [%s]: %s\n",
175                         local_path, nt_errstr(status)));
176                 return status;
177         }
178
179         if (sysvol_version) {
180                 *sysvol_version = version;
181         }
182
183         if (name && *display_name) {
184                 *display_name = talloc_strdup(mem_ctx, name);
185                 NT_STATUS_HAVE_NO_MEMORY(*display_name);
186         }
187
188         return NT_STATUS_OK;
189 }