03d746861f632b4c31b2a93fed5fc9c349cd48e1
[abartlet/samba.git/.git] / source4 / lib / policy / gp_filesys.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Group Policy Object Support
4  *  Copyright (C) Wilco Baan Hofman 2008-2010
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 #include "includes.h"
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"
25 #include <sys/stat.h>
26 #include <fcntl.h>
27
28 #define GP_MAX_DEPTH 25
29
30 struct gp_list_state {
31         struct gp_context *gp_ctx;
32         uint8_t depth;
33         const char *cur_rel_path;
34         const char *share_path;
35         const char *local_path;
36 };
37
38 static NTSTATUS gp_do_list(const char *, struct gp_list_state *);
39
40 /* Create a temporary policy directory */
41 static const char *gp_tmpdir(TALLOC_CTX *mem_ctx)
42 {
43         const char *gp_dir = talloc_asprintf(mem_ctx, "%s/policy", tmpdir());
44         struct stat st;
45
46         if (stat(gp_dir, &st) != 0) {
47                 mkdir(gp_dir, 0755);
48         }
49
50         return gp_dir;
51 }
52
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)
55 {
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;
59         unsigned int i;
60         int fh_remote, fh_local;
61         uint8_t *buf;
62         size_t nread = 0;
63         size_t buf_size = 1024;
64
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] = '/';
70                 }
71         }
72         full_local_path = talloc_asprintf(state, "%s%s/%s", state->local_path, local_rel_path, info->name);
73
74         /* Directory */
75         if (info->attrib & FILE_ATTRIBUTE_DIRECTORY) {
76                 if (state->depth >= GP_MAX_DEPTH)
77                         return;
78                 if (strcmp(info->name, ".") == 0 || strcmp(info->name, "..") == 0)
79                         return;
80
81                 mkdir(full_local_path, 0755);
82
83                 rel_path = talloc_asprintf(state, "%s\\%s", state->cur_rel_path, info->name);
84
85                 /* Recurse into this directory */
86                 gp_do_list(rel_path, state);
87                 return;
88         }
89
90         full_remote_path = talloc_asprintf(state, "%s%s\\%s", state->share_path, state->cur_rel_path, info->name);
91
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));
96                 return;
97         }
98
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));
103                 return;
104         }
105
106         /* Copy the contents of the file */
107         buf = talloc_zero_array(state, uint8_t, buf_size);
108         while (1) {
109                 int n = smbcli_read(state->gp_ctx->cli->tree, fh_remote, buf, nread, buf_size);
110                 if (n <= 0) {
111                         break;
112                 }
113                 if (write(fh_local, buf, n) != n) {
114                         DEBUG(0, ("Short write while copying file.\n"));
115                         return;
116                 }
117                 nread += n;
118         }
119         /* Close the files */
120         smbcli_close(state->gp_ctx->cli->tree, fh_remote);
121         close(fh_local);
122
123         return;
124 }
125
126 static NTSTATUS gp_do_list (const char *rel_path, struct gp_list_state *state)
127 {
128         uint16_t attributes;
129         int success;
130         char *mask;
131         const char *old_rel_path;
132
133         attributes = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
134
135         /* Update the relative paths, while buffering the parent */
136         old_rel_path = state->cur_rel_path;
137         state->cur_rel_path = rel_path;
138         state->depth++;
139
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);
143         talloc_free(mask);
144
145         /* Go back to the state of the parent */
146         state->cur_rel_path = old_rel_path;
147         state->depth--;
148
149         if (!success)
150                 return NT_STATUS_UNSUCCESSFUL;
151
152         return NT_STATUS_OK;
153 }
154
155 static NTSTATUS gp_cli_connect(struct gp_context *gp_ctx)
156 {
157         struct smbcli_options options;
158         struct smbcli_session_options session_options;
159
160         if (gp_ctx->cli != NULL)
161                 return NT_STATUS_OK;
162
163         gp_ctx->cli = smbcli_state_init(gp_ctx);
164
165         lp_smbcli_options(gp_ctx->lp_ctx, &options);
166         lp_smbcli_session_options(gp_ctx->lp_ctx, &session_options);
167
168
169         return smbcli_full_connection(gp_ctx,
170                         &gp_ctx->cli,
171                         gp_ctx->active_dc.name,
172                         lp_smb_ports(gp_ctx->lp_ctx),
173                         "sysvol",
174                         NULL,
175                         lp_socket_options(gp_ctx->lp_ctx),
176                         gp_ctx->credentials,
177                         lp_resolve_context(gp_ctx->lp_ctx),
178                         gp_ctx->ev_ctx,
179                         &options,
180                         &session_options,
181                         lp_iconv_convenience(gp_ctx->lp_ctx),
182                         lp_gensec_settings(gp_ctx, gp_ctx->lp_ctx));
183
184         return NT_STATUS_OK;
185 }
186
187 NTSTATUS gp_fetch_gpo (struct gp_context *gp_ctx, struct gp_object *gpo, const char **ret_local_path)
188 {
189         TALLOC_CTX *mem_ctx;
190         struct gp_list_state *state;
191         NTSTATUS status;
192         unsigned int i, bkslash_cnt;
193         struct stat st;
194         int rv;
195
196         /* Create a forked memory context, as a base for everything here */
197         mem_ctx = talloc_new(gp_ctx);
198
199
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);
205                         return status;
206                 }
207         }
208
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);
213
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] == '\\')
217                         bkslash_cnt++;
218
219                 if (bkslash_cnt == 4) {
220                         state->share_path = talloc_strdup(mem_ctx, &gpo->file_sys_path[i]);
221                         break;
222                 }
223         }
224
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);
228                 if (rv < 0) {
229                         DEBUG(0, ("Could not create local path\n"));
230                         talloc_free(mem_ctx);
231                         return NT_STATUS_UNSUCCESSFUL;
232                 }
233         }
234
235         /* Copy the files */
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);
240                 return status;
241         }
242
243         /* Return the local path to the gpo */
244         *ret_local_path = state->local_path;
245
246         talloc_free(mem_ctx);
247         return NT_STATUS_OK;
248 }