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