r25534: Apply some const
[obnox/samba/samba-obnox.git] / source / 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(lock_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                         lock_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         prs_struct ps;
114         fstring key;
115         SEC_DESC *psd = NULL;
116
117         if (!share_info_db_init()) {
118                 return NULL;
119         }
120
121         *psize = 0;
122
123         /* Fetch security descriptor from tdb */
124  
125         slprintf(key, sizeof(key)-1, "SECDESC/%s", servicename);
126  
127         if (tdb_prs_fetch_bystring(share_tdb, key, &ps, ctx)!=0 ||
128                 !sec_io_desc("get_share_security", &psd, &ps, 1)) {
129  
130                 DEBUG(4, ("get_share_security: using default secdesc for %s\n",
131                           servicename));
132  
133                 return get_share_security_default(ctx, psize, GENERIC_ALL_ACCESS);
134         }
135
136         if (psd)
137                 *psize = sec_desc_size(psd);
138
139         prs_mem_free(&ps);
140         return psd;
141 }
142
143 /*******************************************************************
144  Store a security descriptor in the share db.
145  ********************************************************************/
146
147 BOOL set_share_security(const char *share_name, SEC_DESC *psd)
148 {
149         prs_struct ps;
150         TALLOC_CTX *mem_ctx = NULL;
151         fstring key;
152         BOOL ret = False;
153
154         if (!share_info_db_init()) {
155                 return False;
156         }
157
158         mem_ctx = talloc_init("set_share_security");
159         if (mem_ctx == NULL)
160                 return False;
161
162         prs_init(&ps, (uint32)sec_desc_size(psd), mem_ctx, MARSHALL);
163  
164         if (!sec_io_desc("share_security", &psd, &ps, 1))
165                 goto out;
166  
167         slprintf(key, sizeof(key)-1, "SECDESC/%s", share_name);
168  
169         if (tdb_prs_store_bystring(share_tdb, key, &ps)==0) {
170                 ret = True;
171                 DEBUG(5,("set_share_security: stored secdesc for %s\n", share_name ));
172         } else {
173                 DEBUG(1,("set_share_security: Failed to store secdesc for %s\n", share_name ));
174         } 
175
176         /* Free malloc'ed memory */
177  
178 out:
179  
180         prs_mem_free(&ps);
181         TALLOC_FREE(mem_ctx);
182         return ret;
183 }
184
185 /*******************************************************************
186  Delete a security descriptor.
187 ********************************************************************/
188
189 BOOL delete_share_security(const struct share_params *params)
190 {
191         TDB_DATA kbuf;
192         fstring key;
193
194         slprintf(key, sizeof(key)-1, "SECDESC/%s",
195                  lp_servicename(params->service));
196         kbuf = string_term_tdb_data(key);
197
198         if (tdb_trans_delete(share_tdb, kbuf) != 0) {
199                 DEBUG(0,("delete_share_security: Failed to delete entry for share %s\n",
200                          lp_servicename(params->service) ));
201                 return False;
202         }
203
204         return True;
205 }
206
207 /*******************************************************************
208  Can this user access with share with the required permissions ?
209 ********************************************************************/
210
211 BOOL share_access_check(const NT_USER_TOKEN *token, const char *sharename,
212                         uint32 desired_access)
213 {
214         uint32 granted;
215         NTSTATUS status;
216         TALLOC_CTX *mem_ctx = NULL;
217         SEC_DESC *psd = NULL;
218         size_t sd_size;
219         BOOL ret = True;
220
221         if (!(mem_ctx = talloc_init("share_access_check"))) {
222                 return False;
223         }
224
225         psd = get_share_security(mem_ctx, sharename, &sd_size);
226
227         if (!psd) {
228                 TALLOC_FREE(mem_ctx);
229                 return True;
230         }
231
232         ret = se_access_check(psd, token, desired_access, &granted, &status);
233
234         talloc_destroy(mem_ctx);
235         return ret;
236 }
237
238 /***************************************************************************
239  Parse the contents of an acl string from a usershare file.
240 ***************************************************************************/
241
242 BOOL parse_usershare_acl(TALLOC_CTX *ctx, const char *acl_str, SEC_DESC **ppsd)
243 {
244         size_t s_size = 0;
245         const char *pacl = acl_str;
246         int num_aces = 0;
247         SEC_ACE *ace_list = NULL;
248         SEC_ACL *psa = NULL;
249         SEC_DESC *psd = NULL;
250         size_t sd_size = 0;
251         int i;
252
253         *ppsd = NULL;
254
255         /* If the acl string is blank return "Everyone:R" */
256         if (!*acl_str) {
257                 SEC_DESC *default_psd = get_share_security_default(ctx, &s_size, GENERIC_READ_ACCESS);
258                 if (!default_psd) {
259                         return False;
260                 }
261                 *ppsd = default_psd;
262                 return True;
263         }
264
265         num_aces = 1;
266
267         /* Add the number of ',' characters to get the number of aces. */
268         num_aces += count_chars(pacl,',');
269
270         ace_list = TALLOC_ARRAY(ctx, SEC_ACE, num_aces);
271         if (!ace_list) {
272                 return False;
273         }
274
275         for (i = 0; i < num_aces; i++) {
276                 SEC_ACCESS sa;
277                 uint32 g_access;
278                 uint32 s_access;
279                 DOM_SID sid;
280                 fstring sidstr;
281                 uint8 type = SEC_ACE_TYPE_ACCESS_ALLOWED;
282
283                 if (!next_token(&pacl, sidstr, ":", sizeof(sidstr))) {
284                         DEBUG(0,("parse_usershare_acl: malformed usershare acl looking "
285                                 "for ':' in string '%s'\n", pacl));
286                         return False;
287                 }
288
289                 if (!string_to_sid(&sid, sidstr)) {
290                         DEBUG(0,("parse_usershare_acl: failed to convert %s to sid.\n",
291                                 sidstr ));
292                         return False;
293                 }
294
295                 switch (*pacl) {
296                         case 'F': /* Full Control, ie. R+W */
297                         case 'f': /* Full Control, ie. R+W */
298                                 s_access = g_access = GENERIC_ALL_ACCESS;
299                                 break;
300                         case 'R': /* Read only. */
301                         case 'r': /* Read only. */
302                                 s_access = g_access = GENERIC_READ_ACCESS;
303                                 break;
304                         case 'D': /* Deny all to this SID. */
305                         case 'd': /* Deny all to this SID. */
306                                 type = SEC_ACE_TYPE_ACCESS_DENIED;
307                                 s_access = g_access = GENERIC_ALL_ACCESS;
308                                 break;
309                         default:
310                                 DEBUG(0,("parse_usershare_acl: unknown acl type at %s.\n",
311                                         pacl ));
312                                 return False;
313                 }
314
315                 pacl++;
316                 if (*pacl && *pacl != ',') {
317                         DEBUG(0,("parse_usershare_acl: bad acl string at %s.\n",
318                                 pacl ));
319                         return False;
320                 }
321                 pacl++; /* Go past any ',' */
322
323                 se_map_generic(&s_access, &file_generic_mapping);
324                 init_sec_access(&sa, g_access | s_access );
325                 init_sec_ace(&ace_list[i], &sid, type, sa, 0);
326         }
327
328         if ((psa = make_sec_acl(ctx, NT4_ACL_REVISION, num_aces, ace_list)) != NULL) {
329                 psd = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE, NULL, NULL, NULL, psa, &sd_size);
330         }
331
332         if (!psd) {
333                 DEBUG(0,("parse_usershare_acl: Failed to make SEC_DESC.\n"));
334                 return False;
335         }
336
337         *ppsd = psd;
338         return True;
339 }