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