1 | /*
|
---|
2 | * Unix SMB/Netbios implementation.
|
---|
3 | * SEC_DESC handling functions
|
---|
4 | * Copyright (C) Jeremy R. Allison 1995-2003.
|
---|
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 | Create the share security tdb.
|
---|
24 | ********************************************************************/
|
---|
25 |
|
---|
26 | static struct db_context *share_db; /* used for share security descriptors */
|
---|
27 | #define SHARE_DATABASE_VERSION_V1 1
|
---|
28 | #define SHARE_DATABASE_VERSION_V2 2 /* version id in little endian. */
|
---|
29 |
|
---|
30 | /* Map generic permissions to file object specific permissions */
|
---|
31 |
|
---|
32 | static const struct generic_mapping file_generic_mapping = {
|
---|
33 | FILE_GENERIC_READ,
|
---|
34 | FILE_GENERIC_WRITE,
|
---|
35 | FILE_GENERIC_EXECUTE,
|
---|
36 | FILE_GENERIC_ALL
|
---|
37 | };
|
---|
38 |
|
---|
39 | static int delete_fn(struct db_record *rec, void *priv)
|
---|
40 | {
|
---|
41 | rec->delete_rec(rec);
|
---|
42 | return 0;
|
---|
43 | }
|
---|
44 |
|
---|
45 | static bool share_info_db_init(void)
|
---|
46 | {
|
---|
47 | const char *vstring = "INFO/version";
|
---|
48 | int32 vers_id;
|
---|
49 |
|
---|
50 | if (share_db != NULL) {
|
---|
51 | return True;
|
---|
52 | }
|
---|
53 |
|
---|
54 | share_db = db_open(NULL, state_path("share_info.tdb"), 0,
|
---|
55 | TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
|
---|
56 | if (share_db == NULL) {
|
---|
57 | DEBUG(0,("Failed to open share info database %s (%s)\n",
|
---|
58 | state_path("share_info.tdb"), strerror(errno) ));
|
---|
59 | return False;
|
---|
60 | }
|
---|
61 |
|
---|
62 | vers_id = dbwrap_fetch_int32(share_db, vstring);
|
---|
63 | if (vers_id == SHARE_DATABASE_VERSION_V2) {
|
---|
64 | return true;
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (share_db->transaction_start(share_db) != 0) {
|
---|
68 | DEBUG(0, ("transaction_start failed\n"));
|
---|
69 | TALLOC_FREE(share_db);
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 |
|
---|
73 | vers_id = dbwrap_fetch_int32(share_db, vstring);
|
---|
74 | if (vers_id == SHARE_DATABASE_VERSION_V2) {
|
---|
75 | /*
|
---|
76 | * Race condition
|
---|
77 | */
|
---|
78 | if (share_db->transaction_cancel(share_db)) {
|
---|
79 | smb_panic("transaction_cancel failed");
|
---|
80 | }
|
---|
81 | return true;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /* Cope with byte-reversed older versions of the db. */
|
---|
85 | if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) {
|
---|
86 | /* Written on a bigendian machine with old fetch_int code. Save as le. */
|
---|
87 |
|
---|
88 | if (dbwrap_store_int32(share_db, vstring,
|
---|
89 | SHARE_DATABASE_VERSION_V2) != 0) {
|
---|
90 | DEBUG(0, ("dbwrap_store_int32 failed\n"));
|
---|
91 | goto cancel;
|
---|
92 | }
|
---|
93 | vers_id = SHARE_DATABASE_VERSION_V2;
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (vers_id != SHARE_DATABASE_VERSION_V2) {
|
---|
97 | int ret;
|
---|
98 | ret = share_db->traverse(share_db, delete_fn, NULL);
|
---|
99 | if (ret < 0) {
|
---|
100 | DEBUG(0, ("traverse failed\n"));
|
---|
101 | goto cancel;
|
---|
102 | }
|
---|
103 | if (dbwrap_store_int32(share_db, vstring,
|
---|
104 | SHARE_DATABASE_VERSION_V2) != 0) {
|
---|
105 | DEBUG(0, ("dbwrap_store_int32 failed\n"));
|
---|
106 | goto cancel;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (share_db->transaction_commit(share_db) != 0) {
|
---|
111 | DEBUG(0, ("transaction_commit failed\n"));
|
---|
112 | return false;
|
---|
113 | }
|
---|
114 |
|
---|
115 | return true;
|
---|
116 |
|
---|
117 | cancel:
|
---|
118 | if (share_db->transaction_cancel(share_db)) {
|
---|
119 | smb_panic("transaction_cancel failed");
|
---|
120 | }
|
---|
121 |
|
---|
122 | return false;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /*******************************************************************
|
---|
126 | Fake up a Everyone, default access as a default.
|
---|
127 | def_access is a GENERIC_XXX access mode.
|
---|
128 | ********************************************************************/
|
---|
129 |
|
---|
130 | SEC_DESC *get_share_security_default( TALLOC_CTX *ctx, size_t *psize, uint32 def_access)
|
---|
131 | {
|
---|
132 | uint32_t sa;
|
---|
133 | SEC_ACE ace;
|
---|
134 | SEC_ACL *psa = NULL;
|
---|
135 | SEC_DESC *psd = NULL;
|
---|
136 | uint32 spec_access = def_access;
|
---|
137 |
|
---|
138 | se_map_generic(&spec_access, &file_generic_mapping);
|
---|
139 |
|
---|
140 | sa = (def_access | spec_access );
|
---|
141 | init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0);
|
---|
142 |
|
---|
143 | if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) {
|
---|
144 | psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
|
---|
145 | SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
|
---|
146 | psa, psize);
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (!psd) {
|
---|
150 | DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
|
---|
151 | return NULL;
|
---|
152 | }
|
---|
153 |
|
---|
154 | return psd;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /*******************************************************************
|
---|
158 | Pull a security descriptor from the share tdb.
|
---|
159 | ********************************************************************/
|
---|
160 |
|
---|
161 | SEC_DESC *get_share_security( TALLOC_CTX *ctx, const char *servicename,
|
---|
162 | size_t *psize)
|
---|
163 | {
|
---|
164 | char *key;
|
---|
165 | SEC_DESC *psd = NULL;
|
---|
166 | TDB_DATA data;
|
---|
167 | NTSTATUS status;
|
---|
168 |
|
---|
169 | if (!share_info_db_init()) {
|
---|
170 | return NULL;
|
---|
171 | }
|
---|
172 |
|
---|
173 | if (!(key = talloc_asprintf(ctx, "SECDESC/%s", servicename))) {
|
---|
174 | DEBUG(0, ("talloc_asprintf failed\n"));
|
---|
175 | return NULL;
|
---|
176 | }
|
---|
177 |
|
---|
178 | data = dbwrap_fetch_bystring(share_db, talloc_tos(), key);
|
---|
179 |
|
---|
180 | TALLOC_FREE(key);
|
---|
181 |
|
---|
182 | if (data.dptr == NULL) {
|
---|
183 | return get_share_security_default(ctx, psize,
|
---|
184 | GENERIC_ALL_ACCESS);
|
---|
185 | }
|
---|
186 |
|
---|
187 | status = unmarshall_sec_desc(ctx, data.dptr, data.dsize, &psd);
|
---|
188 |
|
---|
189 | TALLOC_FREE(data.dptr);
|
---|
190 |
|
---|
191 | if (!NT_STATUS_IS_OK(status)) {
|
---|
192 | DEBUG(0, ("unmarshall_sec_desc failed: %s\n",
|
---|
193 | nt_errstr(status)));
|
---|
194 | return NULL;
|
---|
195 | }
|
---|
196 |
|
---|
197 | if (psd)
|
---|
198 | *psize = ndr_size_security_descriptor(psd, 0);
|
---|
199 |
|
---|
200 | return psd;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /*******************************************************************
|
---|
204 | Store a security descriptor in the share db.
|
---|
205 | ********************************************************************/
|
---|
206 |
|
---|
207 | bool set_share_security(const char *share_name, SEC_DESC *psd)
|
---|
208 | {
|
---|
209 | TALLOC_CTX *frame;
|
---|
210 | char *key;
|
---|
211 | bool ret = False;
|
---|
212 | TDB_DATA blob;
|
---|
213 | NTSTATUS status;
|
---|
214 |
|
---|
215 | if (!share_info_db_init()) {
|
---|
216 | return False;
|
---|
217 | }
|
---|
218 |
|
---|
219 | frame = talloc_stackframe();
|
---|
220 |
|
---|
221 | status = marshall_sec_desc(frame, psd, &blob.dptr, &blob.dsize);
|
---|
222 |
|
---|
223 | if (!NT_STATUS_IS_OK(status)) {
|
---|
224 | DEBUG(0, ("marshall_sec_desc failed: %s\n",
|
---|
225 | nt_errstr(status)));
|
---|
226 | goto out;
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (!(key = talloc_asprintf(frame, "SECDESC/%s", share_name))) {
|
---|
230 | DEBUG(0, ("talloc_asprintf failed\n"));
|
---|
231 | goto out;
|
---|
232 | }
|
---|
233 |
|
---|
234 | status = dbwrap_trans_store(share_db, string_term_tdb_data(key), blob,
|
---|
235 | TDB_REPLACE);
|
---|
236 | if (!NT_STATUS_IS_OK(status)) {
|
---|
237 | DEBUG(1, ("set_share_security: Failed to store secdesc for "
|
---|
238 | "%s: %s\n", share_name, nt_errstr(status)));
|
---|
239 | goto out;
|
---|
240 | }
|
---|
241 |
|
---|
242 | DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
|
---|
243 | ret = True;
|
---|
244 |
|
---|
245 | out:
|
---|
246 | TALLOC_FREE(frame);
|
---|
247 | return ret;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /*******************************************************************
|
---|
251 | Delete a security descriptor.
|
---|
252 | ********************************************************************/
|
---|
253 |
|
---|
254 | bool delete_share_security(const char *servicename)
|
---|
255 | {
|
---|
256 | TDB_DATA kbuf;
|
---|
257 | char *key;
|
---|
258 | NTSTATUS status;
|
---|
259 |
|
---|
260 | if (!(key = talloc_asprintf(talloc_tos(), "SECDESC/%s",
|
---|
261 | servicename))) {
|
---|
262 | return False;
|
---|
263 | }
|
---|
264 | kbuf = string_term_tdb_data(key);
|
---|
265 |
|
---|
266 | status = dbwrap_trans_delete(share_db, kbuf);
|
---|
267 | if (!NT_STATUS_IS_OK(status)) {
|
---|
268 | DEBUG(0, ("delete_share_security: Failed to delete entry for "
|
---|
269 | "share %s: %s\n", servicename, nt_errstr(status)));
|
---|
270 | return False;
|
---|
271 | }
|
---|
272 |
|
---|
273 | return True;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /*******************************************************************
|
---|
277 | Can this user access with share with the required permissions ?
|
---|
278 | ********************************************************************/
|
---|
279 |
|
---|
280 | bool share_access_check(const NT_USER_TOKEN *token, const char *sharename,
|
---|
281 | uint32 desired_access)
|
---|
282 | {
|
---|
283 | uint32 granted;
|
---|
284 | NTSTATUS status;
|
---|
285 | SEC_DESC *psd = NULL;
|
---|
286 | size_t sd_size;
|
---|
287 |
|
---|
288 | psd = get_share_security(talloc_tos(), sharename, &sd_size);
|
---|
289 |
|
---|
290 | if (!psd) {
|
---|
291 | return True;
|
---|
292 | }
|
---|
293 |
|
---|
294 | status = se_access_check(psd, token, desired_access, &granted);
|
---|
295 |
|
---|
296 | TALLOC_FREE(psd);
|
---|
297 |
|
---|
298 | return NT_STATUS_IS_OK(status);
|
---|
299 | }
|
---|
300 |
|
---|
301 | /***************************************************************************
|
---|
302 | Parse the contents of an acl string from a usershare file.
|
---|
303 | ***************************************************************************/
|
---|
304 |
|
---|
305 | bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, SEC_DESC **ppsd)
|
---|
306 | {
|
---|
307 | size_t s_size = 0;
|
---|
308 | const char *pacl = acl_str;
|
---|
309 | int num_aces = 0;
|
---|
310 | SEC_ACE *ace_list = NULL;
|
---|
311 | SEC_ACL *psa = NULL;
|
---|
312 | SEC_DESC *psd = NULL;
|
---|
313 | size_t sd_size = 0;
|
---|
314 | int i;
|
---|
315 |
|
---|
316 | *ppsd = NULL;
|
---|
317 |
|
---|
318 | /* If the acl string is blank return "Everyone:R" */
|
---|
319 | if (!*acl_str) {
|
---|
320 | SEC_DESC *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
|
---|
321 | if (!default_psd) {
|
---|
322 | return False;
|
---|
323 | }
|
---|
324 | *ppsd = default_psd;
|
---|
325 | return True;
|
---|
326 | }
|
---|
327 |
|
---|
328 | num_aces = 1;
|
---|
329 |
|
---|
330 | /* Add the number of ',' characters to get the number of aces. */
|
---|
331 | num_aces += count_chars(pacl,',');
|
---|
332 |
|
---|
333 | ace_list = TALLOC_ARRAY(ctx, SEC_ACE, num_aces);
|
---|
334 | if (!ace_list) {
|
---|
335 | return False;
|
---|
336 | }
|
---|
337 |
|
---|
338 | for (i = 0; i < num_aces; i++) {
|
---|
339 | uint32_t sa;
|
---|
340 | uint32 g_access;
|
---|
341 | uint32 s_access;
|
---|
342 | DOM_SID sid;
|
---|
343 | char *sidstr;
|
---|
344 | enum security_ace_type type = SEC_ACE_TYPE_ACCESS_ALLOWED;
|
---|
345 |
|
---|
346 | if (!next_token_talloc(ctx, &pacl, &sidstr, ":")) {
|
---|
347 | DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
|
---|
348 | "for ':' in string '%s'\n", pacl));
|
---|
349 | return False;
|
---|
350 | }
|
---|
351 |
|
---|
352 | if (!string_to_sid(&sid, sidstr)) {
|
---|
353 | DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
|
---|
354 | sidstr ));
|
---|
355 | return False;
|
---|
356 | }
|
---|
357 |
|
---|
358 | switch (*pacl) {
|
---|
359 | case 'F': /* Full Control, ie. R+W */
|
---|
360 | case 'f': /* Full Control, ie. R+W */
|
---|
361 | s_access = g_access = GENERIC_ALL_ACCESS;
|
---|
362 | break;
|
---|
363 | case 'R': /* Read only. */
|
---|
364 | case 'r': /* Read only. */
|
---|
365 | s_access = g_access = GENERIC_READ_ACCESS;
|
---|
366 | break;
|
---|
367 | case 'D': /* Deny all to this SID. */
|
---|
368 | case 'd': /* Deny all to this SID. */
|
---|
369 | type = SEC_ACE_TYPE_ACCESS_DENIED;
|
---|
370 | s_access = g_access = GENERIC_ALL_ACCESS;
|
---|
371 | break;
|
---|
372 | default:
|
---|
373 | DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
|
---|
374 | pacl ));
|
---|
375 | return False;
|
---|
376 | }
|
---|
377 |
|
---|
378 | pacl++;
|
---|
379 | if (*pacl && *pacl != ',') {
|
---|
380 | DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
|
---|
381 | pacl ));
|
---|
382 | return False;
|
---|
383 | }
|
---|
384 | pacl++; /* Go past any ',' */
|
---|
385 |
|
---|
386 | se_map_generic(&s_access, &file_generic_mapping);
|
---|
387 | sa = (g_access | s_access);
|
---|
388 | init_sec_ace(&ace_list[i], &sid, type, sa, 0);
|
---|
389 | }
|
---|
390 |
|
---|
391 | if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, num_aces, ace_list)) != NULL) {
|
---|
392 | psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
|
---|
393 | SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
|
---|
394 | psa, &sd_size);
|
---|
395 | }
|
---|
396 |
|
---|
397 | if (!psd) {
|
---|
398 | DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));
|
---|
399 | return False;
|
---|
400 | }
|
---|
401 |
|
---|
402 | *ppsd = psd;
|
---|
403 | return True;
|
---|
404 | }
|
---|