eef5f39fc090405bb6da3f4f07af9a2791d60829
[samba.git] / source3 / lib / sharesec.c
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 #define SHARE_DATABASE_VERSION_V3 3 /* canonicalized sharenames as lower case */
30
31 #define SHARE_SECURITY_DB_KEY_PREFIX_STR "SECDESC/"
32 /* Map generic permissions to file object specific permissions */
33
34 extern const struct generic_mapping file_generic_mapping;
35
36 static int delete_fn(struct db_record *rec, void *priv)
37 {
38         rec->delete_rec(rec);
39         return 0;
40 }
41
42 /*****************************************************
43  Looking for keys of the form: SHARE_SECURITY_DB_KEY_PREFIX_STR + "non lower case str".
44  If we find one re-write it into a canonical case form.
45 *****************************************************/
46
47 static int upgrade_v2_to_v3(struct db_record *rec, void *priv)
48 {
49         size_t prefix_len = strlen(SHARE_SECURITY_DB_KEY_PREFIX_STR);
50         const char *servicename = NULL;
51         char *c_servicename = NULL;
52         char *newkey = NULL;
53         bool *p_upgrade_ok = (bool *)priv;
54         NTSTATUS status;
55
56         /* Is there space for a one character sharename ? */
57         if (rec->key.dsize <= prefix_len+2) {
58                 return 0;
59         }
60
61         /* Does it start with the share key prefix ? */
62         if (memcmp(rec->key.dptr, SHARE_SECURITY_DB_KEY_PREFIX_STR,
63                         prefix_len) != 0) {
64                 return 0;
65         }
66
67         /* Is it a null terminated string as a key ? */
68         if (rec->key.dptr[rec->key.dsize-1] != '\0') {
69                 return 0;
70         }
71
72         /* Bytes after the prefix are the sharename string. */
73         servicename = (char *)&rec->key.dptr[prefix_len];
74         c_servicename = canonicalize_servicename(talloc_tos(), servicename);
75         if (!c_servicename) {
76                 smb_panic("out of memory upgrading share security db from v2 -> v3");
77         }
78
79         if (strcmp(servicename, c_servicename) == 0) {
80                 /* Old and new names match. No canonicalization needed. */
81                 TALLOC_FREE(c_servicename);
82                 return 0;
83         }
84
85         /* Oops. Need to canonicalize name, delete old then store new. */
86         status = rec->delete_rec(rec);
87         if (!NT_STATUS_IS_OK(status)) {
88                 DEBUG(1, ("upgrade_v2_to_v3: Failed to delete secdesc for "
89                           "%s: %s\n", rec->key.dptr, nt_errstr(status)));
90                 TALLOC_FREE(c_servicename);
91                 *p_upgrade_ok = false;
92                 return -1;
93         } else {
94                 DEBUG(10, ("upgrade_v2_to_v3: deleted secdesc for "
95                           "%s\n", rec->key.dptr ));
96         }
97
98         if (!(newkey = talloc_asprintf(talloc_tos(),
99                         SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
100                         c_servicename))) {
101                 smb_panic("out of memory upgrading share security db from v2 -> v3");
102         }
103
104         status = dbwrap_store(share_db,
105                                 string_term_tdb_data(newkey),
106                                 rec->value,
107                                 TDB_REPLACE);
108
109         if (!NT_STATUS_IS_OK(status)) {
110                 DEBUG(1, ("upgrade_v2_to_v3: Failed to store secdesc for "
111                           "%s: %s\n", c_servicename, nt_errstr(status)));
112                 TALLOC_FREE(c_servicename);
113                 TALLOC_FREE(newkey);
114                 *p_upgrade_ok = false;
115                 return -1;
116         } else {
117                 DEBUG(10, ("upgrade_v2_to_v3: stored secdesc for "
118                           "%s\n", newkey ));
119         }
120
121         TALLOC_FREE(newkey);
122         TALLOC_FREE(c_servicename);
123
124         return 0;
125 }
126
127 bool share_info_db_init(void)
128 {
129         const char *vstring = "INFO/version";
130         int32 vers_id;
131         int ret;
132         bool upgrade_ok = true;
133
134         if (share_db != NULL) {
135                 return True;
136         }
137
138         share_db = db_open(NULL, state_path("share_info.tdb"), 0,
139                                  TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
140         if (share_db == NULL) {
141                 DEBUG(0,("Failed to open share info database %s (%s)\n",
142                         state_path("share_info.tdb"), strerror(errno) ));
143                 return False;
144         }
145
146         vers_id = dbwrap_fetch_int32(share_db, vstring);
147         if (vers_id == SHARE_DATABASE_VERSION_V3) {
148                 return true;
149         }
150
151         if (share_db->transaction_start(share_db) != 0) {
152                 DEBUG(0, ("transaction_start failed\n"));
153                 TALLOC_FREE(share_db);
154                 return false;
155         }
156
157         vers_id = dbwrap_fetch_int32(share_db, vstring);
158         if (vers_id == SHARE_DATABASE_VERSION_V3) {
159                 /*
160                  * Race condition
161                  */
162                 if (share_db->transaction_cancel(share_db)) {
163                         smb_panic("transaction_cancel failed");
164                 }
165                 return true;
166         }
167
168         /* Move to at least V2. */
169
170         /* Cope with byte-reversed older versions of the db. */
171         if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) {
172                 /* Written on a bigendian machine with old fetch_int code. Save as le. */
173
174                 if (dbwrap_store_int32(share_db, vstring,
175                                        SHARE_DATABASE_VERSION_V2) != 0) {
176                         DEBUG(0, ("dbwrap_store_int32 failed\n"));
177                         goto cancel;
178                 }
179                 vers_id = SHARE_DATABASE_VERSION_V2;
180         }
181
182         if (vers_id != SHARE_DATABASE_VERSION_V2) {
183                 ret = share_db->traverse(share_db, delete_fn, NULL);
184                 if (ret < 0) {
185                         DEBUG(0, ("traverse failed\n"));
186                         goto cancel;
187                 }
188                 if (dbwrap_store_int32(share_db, vstring,
189                                        SHARE_DATABASE_VERSION_V2) != 0) {
190                         DEBUG(0, ("dbwrap_store_int32 failed\n"));
191                         goto cancel;
192                 }
193         }
194
195         /* Finally upgrade to version 3, with canonicalized sharenames. */
196
197         ret = share_db->traverse(share_db, upgrade_v2_to_v3, &upgrade_ok);
198         if (ret < 0 || upgrade_ok == false) {
199                 DEBUG(0, ("traverse failed\n"));
200                 goto cancel;
201         }
202         if (dbwrap_store_int32(share_db, vstring,
203                                SHARE_DATABASE_VERSION_V3) != 0) {
204                 DEBUG(0, ("dbwrap_store_int32 failed\n"));
205                 goto cancel;
206         }
207
208         if (share_db->transaction_commit(share_db) != 0) {
209                 DEBUG(0, ("transaction_commit failed\n"));
210                 return false;
211         }
212
213         return true;
214
215  cancel:
216         if (share_db->transaction_cancel(share_db)) {
217                 smb_panic("transaction_cancel failed");
218         }
219
220         return false;
221 }
222
223 /*******************************************************************
224  Fake up a Everyone, default access as a default.
225  def_access is a GENERIC_XXX access mode.
226  ********************************************************************/
227
228 SEC_DESC *get_share_security_default( TALLOC_CTX *ctx, size_t *psize, uint32 def_access)
229 {
230         uint32_t sa;
231         SEC_ACE ace;
232         SEC_ACL *psa = NULL;
233         SEC_DESC *psd = NULL;
234         uint32 spec_access = def_access;
235
236         se_map_generic(&spec_access, &file_generic_mapping);
237
238         sa = (def_access | spec_access );
239         init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0);
240
241         if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) {
242                 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
243                                     SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
244                                     psa, psize);
245         }
246
247         if (!psd) {
248                 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
249                 return NULL;
250         }
251
252         return psd;
253 }
254
255 /*******************************************************************
256  Pull a security descriptor from the share tdb.
257  ********************************************************************/
258
259 SEC_DESC *get_share_security( TALLOC_CTX *ctx, const char *servicename,
260                               size_t *psize)
261 {
262         char *key;
263         SEC_DESC *psd = NULL;
264         TDB_DATA data;
265         char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
266         NTSTATUS status;
267
268         if (!c_servicename) {
269                 return NULL;
270         }
271
272         if (!share_info_db_init()) {
273                 TALLOC_FREE(c_servicename);
274                 return NULL;
275         }
276
277         if (!(key = talloc_asprintf(ctx, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_servicename))) {
278                 TALLOC_FREE(c_servicename);
279                 DEBUG(0, ("talloc_asprintf failed\n"));
280                 return NULL;
281         }
282
283         TALLOC_FREE(c_servicename);
284
285         data = dbwrap_fetch_bystring(share_db, talloc_tos(), key);
286
287         TALLOC_FREE(key);
288
289         if (data.dptr == NULL) {
290                 return get_share_security_default(ctx, psize,
291                                                   GENERIC_ALL_ACCESS);
292         }
293
294         status = unmarshall_sec_desc(ctx, data.dptr, data.dsize, &psd);
295
296         TALLOC_FREE(data.dptr);
297
298         if (!NT_STATUS_IS_OK(status)) {
299                 DEBUG(0, ("unmarshall_sec_desc failed: %s\n",
300                           nt_errstr(status)));
301                 return get_share_security_default(ctx, psize,
302                                                   GENERIC_ALL_ACCESS);
303         }
304
305         if (psd) {
306                 *psize = ndr_size_security_descriptor(psd, NULL, 0);
307         } else {
308                 return get_share_security_default(ctx, psize,
309                                                   GENERIC_ALL_ACCESS);
310         }
311
312         return psd;
313 }
314
315 /*******************************************************************
316  Store a security descriptor in the share db.
317  ********************************************************************/
318
319 bool set_share_security(const char *share_name, SEC_DESC *psd)
320 {
321         TALLOC_CTX *frame = talloc_stackframe();
322         char *key;
323         bool ret = False;
324         TDB_DATA blob;
325         NTSTATUS status;
326         char *c_share_name = canonicalize_servicename(frame, share_name);
327
328         if (!c_share_name) {
329                 goto out;
330         }
331
332         if (!share_info_db_init()) {
333                 goto out;
334         }
335
336         status = marshall_sec_desc(frame, psd, &blob.dptr, &blob.dsize);
337
338         if (!NT_STATUS_IS_OK(status)) {
339                 DEBUG(0, ("marshall_sec_desc failed: %s\n",
340                           nt_errstr(status)));
341                 goto out;
342         }
343
344         if (!(key = talloc_asprintf(frame, SHARE_SECURITY_DB_KEY_PREFIX_STR "%s", c_share_name))) {
345                 DEBUG(0, ("talloc_asprintf failed\n"));
346                 goto out;
347         }
348
349         status = dbwrap_trans_store(share_db, string_term_tdb_data(key), blob,
350                                     TDB_REPLACE);
351         if (!NT_STATUS_IS_OK(status)) {
352                 DEBUG(1, ("set_share_security: Failed to store secdesc for "
353                           "%s: %s\n", share_name, nt_errstr(status)));
354                 goto out;
355         }
356
357         DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
358         ret = True;
359
360  out:
361         TALLOC_FREE(frame);
362         return ret;
363 }
364
365 /*******************************************************************
366  Delete a security descriptor.
367 ********************************************************************/
368
369 bool delete_share_security(const char *servicename)
370 {
371         TDB_DATA kbuf;
372         char *key;
373         NTSTATUS status;
374         char *c_servicename = canonicalize_servicename(talloc_tos(), servicename);
375
376         if (!c_servicename) {
377                 return NULL;
378         }
379
380         if (!share_info_db_init()) {
381                 TALLOC_FREE(c_servicename);
382                 return False;
383         }
384
385         if (!(key = talloc_asprintf(talloc_tos(), SHARE_SECURITY_DB_KEY_PREFIX_STR "%s",
386                                     c_servicename))) {
387                 TALLOC_FREE(c_servicename);
388                 return False;
389         }
390         kbuf = string_term_tdb_data(key);
391
392         status = dbwrap_trans_delete(share_db, kbuf);
393         if (!NT_STATUS_IS_OK(status)) {
394                 DEBUG(0, ("delete_share_security: Failed to delete entry for "
395                           "share %s: %s\n", c_servicename, nt_errstr(status)));
396                 TALLOC_FREE(c_servicename);
397                 return False;
398         }
399
400         TALLOC_FREE(c_servicename);
401         return True;
402 }
403
404 /*******************************************************************
405  Can this user access with share with the required permissions ?
406 ********************************************************************/
407
408 bool share_access_check(const NT_USER_TOKEN *token, const char *sharename,
409                         uint32 desired_access)
410 {
411         uint32 granted;
412         NTSTATUS status;
413         SEC_DESC *psd = NULL;
414         size_t sd_size;
415
416         psd = get_share_security(talloc_tos(), sharename, &sd_size);
417
418         if (!psd) {
419                 return True;
420         }
421
422         status = se_access_check(psd, token, desired_access, &granted);
423
424         TALLOC_FREE(psd);
425
426         return NT_STATUS_IS_OK(status);
427 }
428
429 /***************************************************************************
430  Parse the contents of an acl string from a usershare file.
431 ***************************************************************************/
432
433 bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, SEC_DESC **ppsd)
434 {
435         size_t s_size = 0;
436         const char *pacl = acl_str;
437         int num_aces = 0;
438         SEC_ACE *ace_list = NULL;
439         SEC_ACL *psa = NULL;
440         SEC_DESC *psd = NULL;
441         size_t sd_size = 0;
442         int i;
443
444         *ppsd = NULL;
445
446         /* If the acl string is blank return "Everyone:R" */
447         if (!*acl_str) {
448                 SEC_DESC *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
449                 if (!default_psd) {
450                         return False;
451                 }
452                 *ppsd = default_psd;
453                 return True;
454         }
455
456         num_aces = 1;
457
458         /* Add the number of ',' characters to get the number of aces. */
459         num_aces += count_chars(pacl,',');
460
461         ace_list = TALLOC_ARRAY(ctx, SEC_ACE, num_aces);
462         if (!ace_list) {
463                 return False;
464         }
465
466         for (i = 0; i < num_aces; i++) {
467                 uint32_t sa;
468                 uint32 g_access;
469                 uint32 s_access;
470                 DOM_SID sid;
471                 char *sidstr;
472                 enum security_ace_type type = SEC_ACE_TYPE_ACCESS_ALLOWED;
473
474                 if (!next_token_talloc(ctx, &pacl, &sidstr, ":")) {
475                         DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
476                                 "for ':' in string '%s'\n", pacl));
477                         return False;
478                 }
479
480                 if (!string_to_sid(&sid, sidstr)) {
481                         DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
482                                 sidstr ));
483                         return False;
484                 }
485
486                 switch (*pacl) {
487                         case 'F': /* Full Control, ie. R+W */
488                         case 'f': /* Full Control, ie. R+W */
489                                 s_access = g_access = GENERIC_ALL_ACCESS;
490                                 break;
491                         case 'R': /* Read only. */
492                         case 'r': /* Read only. */
493                                 s_access = g_access = GENERIC_READ_ACCESS;
494                                 break;
495                         case 'D': /* Deny all to this SID. */
496                         case 'd': /* Deny all to this SID. */
497                                 type = SEC_ACE_TYPE_ACCESS_DENIED;
498                                 s_access = g_access = GENERIC_ALL_ACCESS;
499                                 break;
500                         default:
501                                 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
502                                         pacl ));
503                                 return False;
504                 }
505
506                 pacl++;
507                 if (*pacl && *pacl != ',') {
508                         DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
509                                 pacl ));
510                         return False;
511                 }
512                 pacl++; /* Go past any ',' */
513
514                 se_map_generic(&s_access, &file_generic_mapping);
515                 sa = (g_access | s_access);
516                 init_sec_ace(&ace_list[i], &sid, type, sa, 0);
517         }
518
519         if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, num_aces, ace_list)) != NULL) {
520                 psd = make_sec_desc(ctx, SECURITY_DESCRIPTOR_REVISION_1,
521                                     SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL,
522                                     psa, &sd_size);
523         }
524
525         if (!psd) {
526                 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));
527                 return False;
528         }
529
530         *ppsd = psd;
531         return True;
532 }