Move rbtree.[ch] to lib/util.
[ira/wip.git] / source3 / 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
22 /****************************************************************
23  explode the GPO CIFS URI into their components
24 ****************************************************************/
25
26 NTSTATUS gpo_explode_filesyspath(TALLOC_CTX *mem_ctx,
27                                  const char *file_sys_path,
28                                  char **server,
29                                  char **service,
30                                  char **nt_path,
31                                  char **unix_path)
32 {
33         char *path = NULL;
34
35         *server = NULL;
36         *service = NULL;
37         *nt_path = NULL;
38         *unix_path = NULL;
39
40         if (!file_sys_path) {
41                 return NT_STATUS_OK;
42         }
43
44         if (!next_token_talloc(mem_ctx, &file_sys_path, server, "\\")) {
45                 return NT_STATUS_INVALID_PARAMETER;
46         }
47         NT_STATUS_HAVE_NO_MEMORY(*server);
48
49         if (!next_token_talloc(mem_ctx, &file_sys_path, service, "\\")) {
50                 return NT_STATUS_INVALID_PARAMETER;
51         }
52         NT_STATUS_HAVE_NO_MEMORY(*service);
53
54         if ((*nt_path = talloc_asprintf(mem_ctx, "\\%s", file_sys_path))
55                 == NULL) {
56                 return NT_STATUS_NO_MEMORY;
57         }
58         NT_STATUS_HAVE_NO_MEMORY(*nt_path);
59
60         if ((path = talloc_asprintf(mem_ctx,
61                                         "%s/%s",
62                                         lock_path(GPO_CACHE_DIR),
63                                         file_sys_path)) == NULL) {
64                 return NT_STATUS_NO_MEMORY;
65         }
66         path = talloc_string_sub(mem_ctx, path, "\\", "/");
67         if (!path) {
68                 return NT_STATUS_NO_MEMORY;
69         }
70
71         *unix_path = talloc_strdup(mem_ctx, path);
72         NT_STATUS_HAVE_NO_MEMORY(*unix_path);
73
74         TALLOC_FREE(path);
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         char *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_talloc(mem_ctx, &unix_path, &tok, "/")) {
97                 if (strequal(tok, GPO_CACHE_DIR)) {
98                         break;
99                 }
100         }
101
102         while (next_token_talloc(mem_ctx, &unix_path, &tok, "/")) {
103                 current_dir = talloc_asprintf_append_buffer(current_dir, "/%s", tok);
104                 NT_STATUS_HAVE_NO_MEMORY(current_dir);
105
106                 if ((mkdir(current_dir, 0644)) < 0 && errno != EEXIST) {
107                         return NT_STATUS_ACCESS_DENIED;
108                 }
109         }
110
111         return NT_STATUS_OK;
112 }
113
114 /****************************************************************
115  download a full GPO via CIFS
116 ****************************************************************/
117
118 NTSTATUS gpo_fetch_files(TALLOC_CTX *mem_ctx,
119                          struct cli_state *cli,
120                          struct GROUP_POLICY_OBJECT *gpo)
121 {
122         NTSTATUS result;
123         char *server, *service, *nt_path, *unix_path;
124         char *nt_ini_path, *unix_ini_path;
125
126         result = gpo_explode_filesyspath(mem_ctx, gpo->file_sys_path,
127                                          &server, &service, &nt_path,
128                                          &unix_path);
129         NT_STATUS_NOT_OK_RETURN(result);
130
131         result = gpo_prepare_local_store(mem_ctx, unix_path);
132         NT_STATUS_NOT_OK_RETURN(result);
133
134         unix_ini_path = talloc_asprintf(mem_ctx, "%s/%s", unix_path, GPT_INI);
135         nt_ini_path = talloc_asprintf(mem_ctx, "%s\\%s", nt_path, GPT_INI);
136         NT_STATUS_HAVE_NO_MEMORY(unix_ini_path);
137         NT_STATUS_HAVE_NO_MEMORY(nt_ini_path);
138
139         result = gpo_copy_file(mem_ctx, cli, nt_ini_path, unix_ini_path);
140         NT_STATUS_NOT_OK_RETURN(result);
141
142         result = gpo_sync_directories(mem_ctx, cli, nt_path, unix_path);
143         NT_STATUS_NOT_OK_RETURN(result);
144
145         return NT_STATUS_OK;
146 }
147
148 /****************************************************************
149  get the locally stored gpt.ini version number
150 ****************************************************************/
151
152 NTSTATUS gpo_get_sysvol_gpt_version(TALLOC_CTX *mem_ctx,
153                                     const char *unix_path,
154                                     uint32_t *sysvol_version,
155                                     char **display_name)
156 {
157         NTSTATUS status;
158         uint32_t version = 0;
159         char *local_path = NULL;
160         char *name = NULL;
161
162         if (!unix_path) {
163                 return NT_STATUS_OK;
164         }
165
166         local_path = talloc_asprintf(mem_ctx, "%s/%s", unix_path, GPT_INI);
167         NT_STATUS_HAVE_NO_MEMORY(local_path);
168
169         status = parse_gpt_ini(mem_ctx, local_path, &version, &name);
170         if (!NT_STATUS_IS_OK(status)) {
171                 DEBUG(10,("gpo_get_sysvol_gpt_version: "
172                         "failed to parse ini [%s]: %s\n",
173                         local_path, nt_errstr(status)));
174                 return status;
175         }
176
177         if (sysvol_version) {
178                 *sysvol_version = version;
179         }
180
181         if (name && *display_name) {
182                 *display_name = talloc_strdup(mem_ctx, name);
183                 NT_STATUS_HAVE_NO_MEMORY(*display_name);
184         }
185
186         return NT_STATUS_OK;
187 }