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