Patch 2 of 3 from Debian Samba packagers:
[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 TDB_CONTEXT *share_tdb; /* 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
40 bool share_info_db_init(void)
41 {
42         const char *vstring = "INFO/version";
43         int32 vers_id;
44  
45         if (share_tdb) {
46                 return True;
47         }
48
49         share_tdb = tdb_open_log(state_path("share_info.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
50         if (!share_tdb) {
51                 DEBUG(0,("Failed to open share info database %s (%s)\n",
52                         state_path("share_info.tdb"), strerror(errno) ));
53                 return False;
54         }
55  
56         /* handle a Samba upgrade */
57         tdb_lock_bystring(share_tdb, vstring);
58
59         /* Cope with byte-reversed older versions of the db. */
60         vers_id = tdb_fetch_int32(share_tdb, vstring);
61         if ((vers_id == SHARE_DATABASE_VERSION_V1) || (IREV(vers_id) == SHARE_DATABASE_VERSION_V1)) {
62                 /* Written on a bigendian machine with old fetch_int code. Save as le. */
63                 tdb_store_int32(share_tdb, vstring, SHARE_DATABASE_VERSION_V2);
64                 vers_id = SHARE_DATABASE_VERSION_V2;
65         }
66
67         if (vers_id != SHARE_DATABASE_VERSION_V2) {
68                 tdb_traverse(share_tdb, tdb_traverse_delete_fn, NULL);
69                 tdb_store_int32(share_tdb, vstring, SHARE_DATABASE_VERSION_V2);
70         }
71         tdb_unlock_bystring(share_tdb, vstring);
72
73         return True;
74 }
75
76 /*******************************************************************
77  Fake up a Everyone, default access as a default.
78  def_access is a GENERIC_XXX access mode.
79  ********************************************************************/
80
81 SEC_DESC *get_share_security_default( TALLOC_CTX *ctx, size_t *psize, uint32 def_access)
82 {
83         SEC_ACCESS sa;
84         SEC_ACE ace;
85         SEC_ACL *psa = NULL;
86         SEC_DESC *psd = NULL;
87         uint32 spec_access = def_access;
88
89         se_map_generic(&spec_access, &file_generic_mapping);
90
91         init_sec_access(&sa, def_access | spec_access );
92         init_sec_ace(&ace, &global_sid_World, SEC_ACE_TYPE_ACCESS_ALLOWED, sa, 0);
93
94         if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, 1, &ace)) != NULL) {
95                 psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, psa, psize);
96         }
97
98         if (!psd) {
99                 DEBUG(0,("get_share_security: Failed to make SEC_DESC.\n"));
100                 return NULL;
101         }
102
103         return psd;
104 }
105
106 /*******************************************************************
107  Pull a security descriptor from the share tdb.
108  ********************************************************************/
109
110 SEC_DESC *get_share_security( TALLOC_CTX *ctx, const char *servicename,
111                               size_t *psize)
112 {
113         char *key;
114         SEC_DESC *psd = NULL;
115         TDB_DATA data;
116         NTSTATUS status;
117
118         if (!share_info_db_init()) {
119                 return NULL;
120         }
121
122         if (!(key = talloc_asprintf(ctx, "SECDESC/%s", servicename))) {
123                 DEBUG(0, ("talloc_asprintf failed\n"));
124                 return NULL;
125         }
126
127         data = tdb_fetch_bystring(share_tdb, key);
128
129         TALLOC_FREE(key);
130
131         if (data.dptr == NULL) {
132                 return get_share_security_default(ctx, psize,
133                                                   GENERIC_ALL_ACCESS);
134         }
135
136         status = unmarshall_sec_desc(ctx, data.dptr, data.dsize, &psd);
137
138         if (!NT_STATUS_IS_OK(status)) {
139                 DEBUG(0, ("unmarshall_sec_desc failed: %s\n",
140                           nt_errstr(status)));
141                 return NULL;
142         }
143
144         if (psd)
145                 *psize = sec_desc_size(psd);
146
147         return psd;
148 }
149
150 /*******************************************************************
151  Store a security descriptor in the share db.
152  ********************************************************************/
153
154 bool set_share_security(const char *share_name, SEC_DESC *psd)
155 {
156         TALLOC_CTX *frame;
157         char *key;
158         bool ret = False;
159         TDB_DATA blob;
160         NTSTATUS status;
161
162         if (!share_info_db_init()) {
163                 return False;
164         }
165
166         frame = talloc_stackframe();
167
168         status = marshall_sec_desc(frame, psd, &blob.dptr, &blob.dsize);
169
170         if (!NT_STATUS_IS_OK(status)) {
171                 DEBUG(0, ("marshall_sec_desc failed: %s\n",
172                           nt_errstr(status)));
173                 goto out;
174         }
175
176         if (!(key = talloc_asprintf(frame, "SECDESC/%s", share_name))) {
177                 DEBUG(0, ("talloc_asprintf failed\n"));
178                 goto out;
179         }
180
181         if (tdb_trans_store_bystring(share_tdb, key, blob,
182                                      TDB_REPLACE) == -1) {
183                 DEBUG(1,("set_share_security: Failed to store secdesc for "
184                          "%s\n", share_name ));
185                 goto out;
186         }
187
188         DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
189         ret = True;
190
191  out:
192         TALLOC_FREE(frame);
193         return ret;
194 }
195
196 /*******************************************************************
197  Delete a security descriptor.
198 ********************************************************************/
199
200 bool delete_share_security(const char *servicename)
201 {
202         TDB_DATA kbuf;
203         char *key;
204
205         if (!(key = talloc_asprintf(talloc_tos(), "SECDESC/%s",
206                                     servicename))) {
207                 return False;
208         }
209         kbuf = string_term_tdb_data(key);
210
211         if (tdb_trans_delete(share_tdb, kbuf) != 0) {
212                 DEBUG(0, ("delete_share_security: Failed to delete entry for "
213                           "share %s\n", servicename));
214                 return False;
215         }
216
217         return True;
218 }
219
220 /*******************************************************************
221  Can this user access with share with the required permissions ?
222 ********************************************************************/
223
224 bool share_access_check(const NT_USER_TOKEN *token, const char *sharename,
225                         uint32 desired_access)
226 {
227         uint32 granted;
228         NTSTATUS status;
229         TALLOC_CTX *mem_ctx = NULL;
230         SEC_DESC *psd = NULL;
231         size_t sd_size;
232         bool ret = True;
233
234         if (!(mem_ctx = talloc_init("share_access_check"))) {
235                 return False;
236         }
237
238         psd = get_share_security(mem_ctx, sharename, &sd_size);
239
240         if (!psd) {
241                 TALLOC_FREE(mem_ctx);
242                 return True;
243         }
244
245         ret = se_access_check(psd, token, desired_access, &granted, &status);
246
247         talloc_destroy(mem_ctx);
248         return ret;
249 }
250
251 /***************************************************************************
252  Parse the contents of an acl string from a usershare file.
253 ***************************************************************************/
254
255 bool parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, SEC_DESC **ppsd)
256 {
257         size_t s_size = 0;
258         const char *pacl = acl_str;
259         int num_aces = 0;
260         SEC_ACE *ace_list = NULL;
261         SEC_ACL *psa = NULL;
262         SEC_DESC *psd = NULL;
263         size_t sd_size = 0;
264         int i;
265
266         *ppsd = NULL;
267
268         /* If the acl string is blank return "Everyone:R" */
269         if (!*acl_str) {
270                 SEC_DESC *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
271                 if (!default_psd) {
272                         return False;
273                 }
274                 *ppsd = default_psd;
275                 return True;
276         }
277
278         num_aces = 1;
279
280         /* Add the number of ',' characters to get the number of aces. */
281         num_aces += count_chars(pacl,',');
282
283         ace_list = TALLOC_ARRAY(ctx, SEC_ACE, num_aces);
284         if (!ace_list) {
285                 return False;
286         }
287
288         for (i = 0; i < num_aces; i++) {
289                 SEC_ACCESS sa;
290                 uint32 g_access;
291                 uint32 s_access;
292                 DOM_SID sid;
293                 fstring sidstr;
294                 uint8 type = SEC_ACE_TYPE_ACCESS_ALLOWED;
295
296                 if (!next_token(&pacl, sidstr, ":", sizeof(sidstr))) {
297                         DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
298                                 "for ':' in string '%s'\n", pacl));
299                         return False;
300                 }
301
302                 if (!string_to_sid(&sid, sidstr)) {
303                         DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
304                                 sidstr ));
305                         return False;
306                 }
307
308                 switch (*pacl) {
309                         case 'F': /* Full Control, ie. R+W */
310                         case 'f': /* Full Control, ie. R+W */
311                                 s_access = g_access = GENERIC_ALL_ACCESS;
312                                 break;
313                         case 'R': /* Read only. */
314                         case 'r': /* Read only. */
315                                 s_access = g_access = GENERIC_READ_ACCESS;
316                                 break;
317                         case 'D': /* Deny all to this SID. */
318                         case 'd': /* Deny all to this SID. */
319                                 type = SEC_ACE_TYPE_ACCESS_DENIED;
320                                 s_access = g_access = GENERIC_ALL_ACCESS;
321                                 break;
322                         default:
323                                 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
324                                         pacl ));
325                                 return False;
326                 }
327
328                 pacl++;
329                 if (*pacl && *pacl != ',') {
330                         DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
331                                 pacl ));
332                         return False;
333                 }
334                 pacl++; /* Go past any ',' */
335
336                 se_map_generic(&s_access, &file_generic_mapping);
337                 init_sec_access(&sa, g_access | s_access );
338                 init_sec_ace(&ace_list[i], &sid, type, sa, 0);
339         }
340
341         if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, num_aces, ace_list)) != NULL) {
342                 psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, psa, &sd_size);
343         }
344
345         if (!psd) {
346                 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));
347                 return False;
348         }
349
350         *ppsd = psd;
351         return True;
352 }