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 #include "libgpo/gpo_proto.h"
26 #include "libsmb/libsmb.h"
27 
28 /****************************************************************
29  explode the GPO CIFS URI into their components
30 ****************************************************************/
31 
gpo_explode_filesyspath(TALLOC_CTX * mem_ctx,const char * cache_dir,const char * file_sys_path,char ** server,char ** service,char ** nt_path,char ** unix_path)32 NTSTATUS gpo_explode_filesyspath(TALLOC_CTX *mem_ctx,
33                                  const char *cache_dir,
34 				 const char *file_sys_path,
35 				 char **server,
36 				 char **service,
37 				 char **nt_path,
38 				 char **unix_path)
39 {
40 	char *path = NULL;
41 
42 	*server = NULL;
43 	*service = NULL;
44 	*nt_path = NULL;
45 	*unix_path = NULL;
46 
47 	if (!file_sys_path) {
48 		return NT_STATUS_OK;
49 	}
50 
51 	if (!next_token_talloc(mem_ctx, &file_sys_path, server, "\\")) {
52 		return NT_STATUS_INVALID_PARAMETER;
53 	}
54 	NT_STATUS_HAVE_NO_MEMORY(*server);
55 
56 	if (!next_token_talloc(mem_ctx, &file_sys_path, service, "\\")) {
57 		return NT_STATUS_INVALID_PARAMETER;
58 	}
59 	NT_STATUS_HAVE_NO_MEMORY(*service);
60 
61 	if ((*nt_path = talloc_asprintf(mem_ctx, "\\%s", file_sys_path))
62 		== NULL) {
63 		return NT_STATUS_NO_MEMORY;
64 	}
65 	NT_STATUS_HAVE_NO_MEMORY(*nt_path);
66 
67 	if ((path = talloc_asprintf(mem_ctx,
68 					"%s/%s",
69 					cache_dir,
70 					file_sys_path)) == NULL) {
71 		return NT_STATUS_NO_MEMORY;
72 	}
73 	path = talloc_string_sub(mem_ctx, path, "\\", "/");
74 	if (!path) {
75 		return NT_STATUS_NO_MEMORY;
76 	}
77 
78 	*unix_path = talloc_strdup(mem_ctx, path);
79 	NT_STATUS_HAVE_NO_MEMORY(*unix_path);
80 
81 	talloc_free(path);
82 	return NT_STATUS_OK;
83 }
84 
85 /****************************************************************
86  prepare the local disc storage for "unix_path"
87 ****************************************************************/
88 
gpo_prepare_local_store(TALLOC_CTX * mem_ctx,const char * cache_dir,const char * unix_path)89 static NTSTATUS gpo_prepare_local_store(TALLOC_CTX *mem_ctx,
90                                         const char *cache_dir,
91 					const char *unix_path)
92 {
93 	char *current_dir;
94 	char *tok;
95 
96 	current_dir = talloc_strdup(mem_ctx, cache_dir);
97 	NT_STATUS_HAVE_NO_MEMORY(current_dir);
98 
99 	if ((mkdir(cache_dir, 0644)) < 0 && errno != EEXIST) {
100 		return NT_STATUS_ACCESS_DENIED;
101 	}
102 
103 	while (next_token_talloc(mem_ctx, &unix_path, &tok, "/")) {
104 		if (strequal(tok, GPO_CACHE_DIR)) {
105 			break;
106 		}
107 	}
108 
109 	while (next_token_talloc(mem_ctx, &unix_path, &tok, "/")) {
110 		current_dir = talloc_asprintf_append_buffer(current_dir, "/%s", tok);
111 		NT_STATUS_HAVE_NO_MEMORY(current_dir);
112 
113 		if ((mkdir(current_dir, 0644)) < 0 && errno != EEXIST) {
114 			return NT_STATUS_ACCESS_DENIED;
115 		}
116 	}
117 
118 	return NT_STATUS_OK;
119 }
120 
gpo_connect_server(ADS_STRUCT * ads,const char * server,const char * service,void * ret_cli)121 static NTSTATUS gpo_connect_server(ADS_STRUCT *ads,
122                                    const char *server, const char *service, void *ret_cli)
123 {
124 	NTSTATUS result;
125 	struct cli_state *cli;
126 
127 	result = cli_full_connection(&cli,
128 			lp_netbios_name(),
129 			server,
130 			NULL, 0,
131 			service, "A:",
132 			ads->auth.user_name, NULL,
133 			ads->auth.password,
134 			CLI_FULL_CONNECTION_USE_KERBEROS |
135 			CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS,
136 			SMB_SIGNING_REQUIRED);
137 	if (!NT_STATUS_IS_OK(result)) {
138 		DEBUG(10,("check_refresh_gpo: "
139 				"failed to connect: %s\n",
140 				nt_errstr(result)));
141 		return result;
142 	}
143 	*(struct cli_state **) ret_cli = cli;
144 	return NT_STATUS_OK;
145 }
146 
147 /****************************************************************
148  download a full GPO via CIFS
149 ****************************************************************/
150 
gpo_fetch_files(TALLOC_CTX * mem_ctx,ADS_STRUCT * ads,const char * cache_dir,const struct GROUP_POLICY_OBJECT * gpo)151 NTSTATUS gpo_fetch_files(TALLOC_CTX *mem_ctx,
152                          ADS_STRUCT *ads,
153                          const char *cache_dir,
154 			 const struct GROUP_POLICY_OBJECT *gpo)
155 {
156 	NTSTATUS result;
157 	char *server, *service, *nt_path, *unix_path;
158 	char *nt_ini_path, *unix_ini_path;
159 	struct cli_state *cli = NULL;
160 
161 
162 	result = gpo_explode_filesyspath(mem_ctx, cache_dir, gpo->file_sys_path,
163 					 &server, &service, &nt_path,
164 					 &unix_path);
165 	NT_STATUS_NOT_OK_RETURN(result);
166 
167 	/* for now reuse the existing ds connection */
168 
169 	result = gpo_connect_server(ads, server, service, &cli);
170 	NT_STATUS_NOT_OK_RETURN(result);
171 
172 	result = gpo_prepare_local_store(mem_ctx, cache_dir, unix_path);
173 	NT_STATUS_NOT_OK_RETURN(result);
174 
175 	unix_ini_path = talloc_asprintf(mem_ctx, "%s/%s", unix_path, GPT_INI);
176 	nt_ini_path = talloc_asprintf(mem_ctx, "%s\\%s", nt_path, GPT_INI);
177 	NT_STATUS_HAVE_NO_MEMORY(unix_ini_path);
178 	NT_STATUS_HAVE_NO_MEMORY(nt_ini_path);
179 
180 	result = gpo_copy_file(mem_ctx, cli, nt_ini_path, unix_ini_path);
181 	NT_STATUS_NOT_OK_RETURN(result);
182 
183 	result = gpo_sync_directories(mem_ctx, cli, nt_path, unix_path);
184 	NT_STATUS_NOT_OK_RETURN(result);
185 
186 	return NT_STATUS_OK;
187 }
188 
189 /****************************************************************
190  get the locally stored gpt.ini version number
191 ****************************************************************/
192 
gpo_get_sysvol_gpt_version(TALLOC_CTX * mem_ctx,const char * unix_path,uint32_t * sysvol_version,char ** display_name)193 NTSTATUS gpo_get_sysvol_gpt_version(TALLOC_CTX *mem_ctx,
194 				    const char *unix_path,
195 				    uint32_t *sysvol_version,
196 				    char **display_name)
197 {
198 	NTSTATUS status;
199 	uint32_t version = 0;
200 	char *local_path = NULL;
201 	char *name = NULL;
202 
203 	if (!unix_path) {
204 		return NT_STATUS_OK;
205 	}
206 
207 	local_path = talloc_asprintf(mem_ctx, "%s/%s", unix_path, GPT_INI);
208 	NT_STATUS_HAVE_NO_MEMORY(local_path);
209 
210 	status = parse_gpt_ini(mem_ctx, local_path, &version, &name);
211 	if (!NT_STATUS_IS_OK(status)) {
212 		DEBUG(10,("gpo_get_sysvol_gpt_version: "
213 			"failed to parse ini [%s]: %s\n",
214 			local_path, nt_errstr(status)));
215 		return status;
216 	}
217 
218 	if (sysvol_version) {
219 		*sysvol_version = version;
220 	}
221 
222 	if (name && *display_name) {
223 		*display_name = talloc_strdup(mem_ctx, name);
224 		NT_STATUS_HAVE_NO_MEMORY(*display_name);
225 	}
226 
227 	return NT_STATUS_OK;
228 }
229