2 * Unix SMB/CIFS implementation.
3 * Group Policy Object Support
4 * Copyright (C) Wilco Baan Hofman 2008-2010
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.
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.
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/>.
20 #include "lib/policy/policy.h"
21 #include "libcli/raw/smb.h"
22 #include "libcli/libcli.h"
23 #include "param/param.h"
24 #include "libcli/resolve/resolve.h"
28 #define GP_MAX_DEPTH 25
30 struct gp_list_state {
31 struct gp_context *gp_ctx;
33 const char *cur_rel_path;
34 const char *share_path;
35 const char *local_path;
38 static NTSTATUS gp_do_list(const char *, struct gp_list_state *);
40 /* Create a temporary policy directory */
41 static const char *gp_tmpdir(TALLOC_CTX *mem_ctx)
43 const char *gp_dir = talloc_asprintf(mem_ctx, "%s/policy", tmpdir());
46 if (stat(gp_dir, &st) != 0) {
53 /* This function is called by the smbcli_list function */
54 static void gp_list_helper (struct clilist_file_info *info, const char *mask, void *list_state_ptr)
56 struct gp_list_state *state = list_state_ptr;
57 const char *rel_path, *full_remote_path;
58 char *local_rel_path, *full_local_path;
60 int fh_remote, fh_local;
63 size_t buf_size = 1024;
65 /* Get local path by replacing backslashes with slashes */
66 local_rel_path = talloc_strdup(state, state->cur_rel_path);
67 for (i = 0; local_rel_path[i] != '\0'; i++) {
68 if (local_rel_path[i] == '\\') {
69 local_rel_path[i] = '/';
72 full_local_path = talloc_asprintf(state, "%s%s/%s", state->local_path, local_rel_path, info->name);
75 if (info->attrib & FILE_ATTRIBUTE_DIRECTORY) {
76 if (state->depth >= GP_MAX_DEPTH)
78 if (strcmp(info->name, ".") == 0 || strcmp(info->name, "..") == 0)
81 mkdir(full_local_path, 0755);
83 rel_path = talloc_asprintf(state, "%s\\%s", state->cur_rel_path, info->name);
85 /* Recurse into this directory */
86 gp_do_list(rel_path, state);
90 full_remote_path = talloc_asprintf(state, "%s%s\\%s", state->share_path, state->cur_rel_path, info->name);
92 /* Open the remote file */
93 fh_remote = smbcli_open(state->gp_ctx->cli->tree, full_remote_path, O_RDONLY, DENY_NONE);
94 if (fh_remote == -1) {
95 DEBUG(0, ("Failed to open remote file: %s\n", full_remote_path));
99 /* Open the local file */
100 fh_local = open(full_local_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
101 if (fh_local == -1) {
102 DEBUG(0, ("Failed to open local file: %s\n", full_local_path));
106 /* Copy the contents of the file */
107 buf = talloc_zero_array(state, uint8_t, buf_size);
109 int n = smbcli_read(state->gp_ctx->cli->tree, fh_remote, buf, nread, buf_size);
113 if (write(fh_local, buf, n) != n) {
114 DEBUG(0, ("Short write while copying file.\n"));
119 /* Close the files */
120 smbcli_close(state->gp_ctx->cli->tree, fh_remote);
126 static NTSTATUS gp_do_list (const char *rel_path, struct gp_list_state *state)
131 const char *old_rel_path;
133 attributes = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
135 /* Update the relative paths, while buffering the parent */
136 old_rel_path = state->cur_rel_path;
137 state->cur_rel_path = rel_path;
140 /* Get the current mask */
141 mask = talloc_asprintf(state, "%s%s\\*", state->share_path, rel_path);
142 success = smbcli_list(state->gp_ctx->cli->tree, mask, attributes, gp_list_helper, state);
145 /* Go back to the state of the parent */
146 state->cur_rel_path = old_rel_path;
150 return NT_STATUS_UNSUCCESSFUL;
155 static NTSTATUS gp_cli_connect(struct gp_context *gp_ctx)
157 struct smbcli_options options;
158 struct smbcli_session_options session_options;
160 if (gp_ctx->cli != NULL)
163 gp_ctx->cli = smbcli_state_init(gp_ctx);
165 lp_smbcli_options(gp_ctx->lp_ctx, &options);
166 lp_smbcli_session_options(gp_ctx->lp_ctx, &session_options);
169 return smbcli_full_connection(gp_ctx,
171 gp_ctx->active_dc.name,
172 lp_smb_ports(gp_ctx->lp_ctx),
175 lp_socket_options(gp_ctx->lp_ctx),
177 lp_resolve_context(gp_ctx->lp_ctx),
181 lp_iconv_convenience(gp_ctx->lp_ctx),
182 lp_gensec_settings(gp_ctx, gp_ctx->lp_ctx));
187 NTSTATUS gp_fetch_gpo (struct gp_context *gp_ctx, struct gp_object *gpo, const char **ret_local_path)
190 struct gp_list_state *state;
192 unsigned int i, bkslash_cnt;
196 /* Create a forked memory context, as a base for everything here */
197 mem_ctx = talloc_new(gp_ctx);
200 if (gp_ctx->cli == NULL) {
201 status = gp_cli_connect(gp_ctx);
202 if (!NT_STATUS_IS_OK(status)) {
203 DEBUG(0, ("Failed to create cli connection to DC\n"));
204 talloc_free(mem_ctx);
209 /* Prepare the state structure */
210 state = talloc_zero(mem_ctx, struct gp_list_state);
211 state->gp_ctx = gp_ctx;
212 state->local_path = talloc_asprintf(gp_ctx, "%s/%s", gp_tmpdir(mem_ctx), gpo->name);
214 /* Get the path from the share down (\\..\..\(this\stuff) */
215 for (i = 0, bkslash_cnt = 0; gpo->file_sys_path[i] != '\0'; i++) {
216 if (gpo->file_sys_path[i] == '\\')
219 if (bkslash_cnt == 4) {
220 state->share_path = talloc_strdup(mem_ctx, &gpo->file_sys_path[i]);
225 /* Create the GPO dir if it does not exist */
226 if (stat(state->local_path, &st) != 0) {
227 rv = mkdir(state->local_path, 755);
229 DEBUG(0, ("Could not create local path\n"));
230 talloc_free(mem_ctx);
231 return NT_STATUS_UNSUCCESSFUL;
236 status = gp_do_list("", state);
237 if (!NT_STATUS_IS_OK(status)) {
238 DEBUG(0, ("Could not list GPO files on remote server\n"));
239 talloc_free(mem_ctx);
243 /* Return the local path to the gpo */
244 *ret_local_path = state->local_path;
246 talloc_free(mem_ctx);