s3-privileges: add handling of both old and new formats in database
[samba.git] / source3 / lib / privileges.c
1 /*
2    Unix SMB/CIFS implementation.
3    Privileges handling functions
4    Copyright (C) Jean François Micouleau       1998-2001
5    Copyright (C) Simo Sorce                     2002-2003
6    Copyright (C) Gerald (Jerry) Carter          2005
7    Copyright (C) Michael Adam                   2007
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23
24 #include "includes.h"
25 #include "dbwrap.h"
26
27 #define PRIVPREFIX              "PRIV_"
28
29 typedef struct {
30         uint32_t count;
31         struct dom_sid *list;
32 } SID_LIST;
33
34 typedef struct {
35         TALLOC_CTX *mem_ctx;
36         uint64_t privilege;
37         SID_LIST sids;
38 } PRIV_SID_LIST;
39
40 /*
41   interpret an old style SE_PRIV structure
42  */
43 static uint64_t map_old_SE_PRIV(unsigned char *dptr)
44 {
45         uint32_t *old_masks = (uint32_t *)dptr;
46         /*
47          * the old privileges code only ever used up to 0x800, except
48          * for a special case of 'SE_ALL_PRIVS' which was 0xFFFFFFFF
49          */
50         if (old_masks[0] == 0xFFFFFFFF) {
51                 /* they set all privileges */
52                 return SE_ALL_PRIVS;
53         }
54
55         /* the old code used the machine byte order, but we don't know
56          * the byte order of the machine that wrote it. However we can
57          * tell what byte order it was by taking advantage of the fact
58          * that it only ever use up to 0x800
59          */
60         if (dptr[0] || dptr[1]) {
61                 /* it was little endian */
62                 return IVAL(dptr, 0);
63         }
64
65         /* it was either zero or big-endian */
66         return RIVAL(dptr, 0);
67 }
68
69
70 static bool get_privileges( const struct dom_sid *sid, uint64_t *mask )
71 {
72         struct db_context *db = get_account_pol_db();
73         fstring tmp, keystr;
74         TDB_DATA data;
75
76         /* Fail if the admin has not enable privileges */
77
78         if ( !lp_enable_privileges() ) {
79                 return False;
80         }
81
82         if ( db == NULL )
83                 return False;
84
85         /* PRIV_<SID> (NULL terminated) as the key */
86
87         fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
88
89         data = dbwrap_fetch_bystring( db, talloc_tos(), keystr );
90
91         if ( !data.dptr ) {
92                 DEBUG(3, ("get_privileges: No privileges assigned to SID "
93                           "[%s]\n", sid_string_dbg(sid)));
94                 return False;
95         }
96
97         if (data.dsize == 4*4) {
98                 /* it's an old style SE_PRIV structure. */
99                 *mask = map_old_SE_PRIV(data.dptr);
100         } else {
101                 if (data.dsize != sizeof( uint64_t ) ) {
102                         DEBUG(3, ("get_privileges: Invalid privileges record assigned to SID "
103                                   "[%s]\n", sid_string_dbg(sid)));
104                         return False;
105                 }
106
107                 *mask = BVAL(data.dptr, 0);
108         }
109
110         TALLOC_FREE(data.dptr);
111
112         return True;
113 }
114
115 /***************************************************************************
116  Store the privilege mask (set) for a given SID
117 ****************************************************************************/
118
119 static bool set_privileges( const struct dom_sid *sid, uint64_t *mask )
120 {
121         struct db_context *db = get_account_pol_db();
122         uint8_t privbuf[8];
123         fstring tmp, keystr;
124         TDB_DATA data;
125
126         if ( !lp_enable_privileges() )
127                 return False;
128
129         if ( db == NULL )
130                 return False;
131
132         if ( !sid || (sid->num_auths == 0) ) {
133                 DEBUG(0,("set_privileges: Refusing to store empty SID!\n"));
134                 return False;
135         }
136
137         /* PRIV_<SID> (NULL terminated) as the key */
138
139         fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
140
141         /* This writes the 64 bit bitmask out in little endian format */
142         SBVAL(privbuf,0,*mask);
143
144         data.dptr  = privbuf;
145         data.dsize = sizeof(privbuf);
146
147         return NT_STATUS_IS_OK(dbwrap_store_bystring(db, keystr, data,
148                                                      TDB_REPLACE));
149 }
150
151 /*********************************************************************
152  get a list of all privileges for all sids in the list
153 *********************************************************************/
154
155 bool get_privileges_for_sids(uint64_t *privileges, struct dom_sid *slist, int scount)
156 {
157         uint64_t mask;
158         int i;
159         bool found = False;
160
161         *privileges = 0;
162
163         for ( i=0; i<scount; i++ ) {
164                 /* don't add unless we actually have a privilege assigned */
165
166                 if ( !get_privileges( &slist[i], &mask ) )
167                         continue;
168
169                 DEBUG(5,("get_privileges_for_sids: sid = %s\nPrivilege "
170                          "set: 0x%llx\n", sid_string_dbg(&slist[i]),
171                          (unsigned long long)mask));
172
173                 *privileges |= mask;
174                 found = True;
175         }
176
177         return found;
178 }
179
180 NTSTATUS get_privileges_for_sid_as_set(TALLOC_CTX *mem_ctx, PRIVILEGE_SET **privileges, struct dom_sid *sid)
181 {
182         uint64_t mask;
183         if (!get_privileges(sid, &mask)) {
184                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
185         }
186
187         *privileges = talloc_zero(mem_ctx, PRIVILEGE_SET);
188         if (!*privileges) {
189                 return NT_STATUS_NO_MEMORY;
190         }
191
192         if (!se_priv_to_privilege_set(*privileges, mask)) {
193                 return NT_STATUS_NO_MEMORY;
194         }
195         return NT_STATUS_OK;
196 }
197
198 /*********************************************************************
199  traversal functions for privilege_enumerate_accounts
200 *********************************************************************/
201
202 static int priv_traverse_fn(struct db_record *rec, void *state)
203 {
204         PRIV_SID_LIST *priv = (PRIV_SID_LIST *)state;
205         int  prefixlen = strlen(PRIVPREFIX);
206         struct dom_sid sid;
207         fstring sid_string;
208
209         /* check we have a PRIV_+SID entry */
210
211         if ( strncmp((char *)rec->key.dptr, PRIVPREFIX, prefixlen) != 0)
212                 return 0;
213
214         /* check to see if we are looking for a particular privilege */
215
216         fstrcpy( sid_string, (char *)&(rec->key.dptr[strlen(PRIVPREFIX)]) );
217
218         if (priv->privilege != 0) {
219                 uint64_t mask;
220
221                 if (rec->value.dsize == 4*4) {
222                         mask = map_old_SE_PRIV(rec->value.dptr);
223                 } else {
224                         if (rec->value.dsize != sizeof( uint64_t ) ) {
225                                 DEBUG(3, ("get_privileges: Invalid privileges record assigned to SID "
226                                           "[%s]\n", sid_string));
227                                 return 0;
228                         }
229                         mask = BVAL(rec->value.dptr, 0);
230                 }
231
232                 /* if the SID does not have the specified privilege
233                    then just return */
234
235                 if ((mask & priv->privilege) == 0) {
236                         return 0;
237                 }
238         }
239
240         /* this is a last ditch safety check to preventing returning
241            and invalid SID (i've somehow run into this on development branches) */
242
243         if ( strcmp( "S-0-0", sid_string ) == 0 )
244                 return 0;
245
246         if ( !string_to_sid(&sid, sid_string) ) {
247                 DEBUG(0,("travsersal_fn_enum__acct: Could not convert SID [%s]\n",
248                         sid_string));
249                 return 0;
250         }
251
252         if (!NT_STATUS_IS_OK(add_sid_to_array(priv->mem_ctx, &sid,
253                                               &priv->sids.list,
254                                               &priv->sids.count)))
255         {
256                 return 0;
257         }
258
259         return 0;
260 }
261
262 /*********************************************************************
263  Retreive list of privileged SIDs (for _lsa_enumerate_accounts()
264 *********************************************************************/
265
266 NTSTATUS privilege_enumerate_accounts(struct dom_sid **sids, int *num_sids)
267 {
268         struct db_context *db = get_account_pol_db();
269         PRIV_SID_LIST priv;
270
271         if (db == NULL) {
272                 return NT_STATUS_ACCESS_DENIED;
273         }
274
275         ZERO_STRUCT(priv);
276
277         db->traverse_read(db, priv_traverse_fn, &priv);
278
279         /* give the memory away; caller will free */
280
281         *sids      = priv.sids.list;
282         *num_sids  = priv.sids.count;
283
284         return NT_STATUS_OK;
285 }
286
287 /*********************************************************************
288  Retrieve list of SIDs granted a particular privilege
289 *********************************************************************/
290
291 NTSTATUS privilege_enum_sids(enum sec_privilege privilege, TALLOC_CTX *mem_ctx,
292                              struct dom_sid **sids, int *num_sids)
293 {
294         struct db_context *db = get_account_pol_db();
295         PRIV_SID_LIST priv;
296
297         if (db == NULL) {
298                 return NT_STATUS_ACCESS_DENIED;
299         }
300
301         ZERO_STRUCT(priv);
302
303         priv.privilege = sec_privilege_mask(privilege);
304         priv.mem_ctx = mem_ctx;
305
306         db->traverse_read(db, priv_traverse_fn, &priv);
307
308         /* give the memory away; caller will free */
309
310         *sids      = priv.sids.list;
311         *num_sids  = priv.sids.count;
312
313         return NT_STATUS_OK;
314 }
315
316 /***************************************************************************
317  Add privilege to sid
318 ****************************************************************************/
319
320 static bool grant_privilege_bitmap(const struct dom_sid *sid, const uint64_t priv_mask)
321 {
322         uint64_t old_mask, new_mask;
323
324         ZERO_STRUCT( old_mask );
325         ZERO_STRUCT( new_mask );
326
327         if ( get_privileges( sid, &old_mask ) )
328                 new_mask = old_mask;
329         else
330                 new_mask = 0;
331
332         new_mask |= priv_mask;
333
334         DEBUG(10,("grant_privilege: %s\n", sid_string_dbg(sid)));
335
336         DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)new_mask));
337
338         DEBUGADD( 10, ("new privilege mask:      0x%llx\n", (unsigned long long)new_mask));
339
340         return set_privileges( sid, &new_mask );
341 }
342
343 /*********************************************************************
344  Add a privilege based on its name
345 *********************************************************************/
346
347 bool grant_privilege_by_name(struct dom_sid *sid, const char *name)
348 {
349         uint64_t mask;
350
351         if (! se_priv_from_name(name, &mask)) {
352                 DEBUG(3, ("grant_privilege_by_name: "
353                           "No Such Privilege Found (%s)\n", name));
354                 return False;
355         }
356
357         return grant_privilege_bitmap( sid, mask );
358 }
359
360 /***************************************************************************
361  Grant a privilege set (list of LUID values) from a sid
362 ****************************************************************************/
363
364 bool grant_privilege_set(const struct dom_sid *sid, struct lsa_PrivilegeSet *set)
365 {
366         uint64_t privilege_mask;
367         if (!privilege_set_to_se_priv(&privilege_mask, set)) {
368                 return false;
369         }
370         return grant_privilege_bitmap(sid, privilege_mask);
371 }
372
373 /***************************************************************************
374  Remove privilege from sid
375 ****************************************************************************/
376
377 static bool revoke_privilege_bitmap(const struct dom_sid *sid, const uint64_t priv_mask)
378 {
379         uint64_t mask;
380
381         /* if the user has no privileges, then we can't revoke any */
382
383         if ( !get_privileges( sid, &mask ) )
384                 return True;
385
386         DEBUG(10,("revoke_privilege: %s\n", sid_string_dbg(sid)));
387
388         DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)mask));
389
390         mask &= ~priv_mask;
391
392         DEBUGADD( 10, ("new privilege mask:      0x%llx\n", (unsigned long long)mask));
393
394         return set_privileges( sid, &mask );
395 }
396
397 /***************************************************************************
398  Remove a privilege set (list of LUID values) from a sid
399 ****************************************************************************/
400
401 bool revoke_privilege_set(const struct dom_sid *sid, struct lsa_PrivilegeSet *set)
402 {
403         uint64_t privilege_mask;
404         if (!privilege_set_to_se_priv(&privilege_mask, set)) {
405                 return false;
406         }
407         return revoke_privilege_bitmap(sid, privilege_mask);
408 }
409
410 /*********************************************************************
411  Revoke all privileges
412 *********************************************************************/
413
414 bool revoke_all_privileges( struct dom_sid *sid )
415 {
416         return revoke_privilege_bitmap( sid, SE_ALL_PRIVS);
417 }
418
419 /*********************************************************************
420  Add a privilege based on its name
421 *********************************************************************/
422
423 bool revoke_privilege_by_name(struct dom_sid *sid, const char *name)
424 {
425         uint64_t mask;
426
427         if (! se_priv_from_name(name, &mask)) {
428                 DEBUG(3, ("revoke_privilege_by_name: "
429                           "No Such Privilege Found (%s)\n", name));
430                 return False;
431         }
432
433         return revoke_privilege_bitmap(sid, mask);
434
435 }
436
437 /***************************************************************************
438  Retrieve the SIDs assigned to a given privilege
439 ****************************************************************************/
440
441 NTSTATUS privilege_create_account(const struct dom_sid *sid )
442 {
443         return ( grant_privilege_bitmap(sid, 0) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
444 }
445
446 /***************************************************************************
447  Delete a privileged account
448 ****************************************************************************/
449
450 NTSTATUS privilege_delete_account(const struct dom_sid *sid)
451 {
452         struct db_context *db = get_account_pol_db();
453         fstring tmp, keystr;
454
455         if (!lp_enable_privileges()) {
456                 return NT_STATUS_OK;
457         }
458
459         if (!db) {
460                 return NT_STATUS_INVALID_HANDLE;
461         }
462
463         if (!sid || (sid->num_auths == 0)) {
464                 return NT_STATUS_INVALID_SID;
465         }
466
467         /* PRIV_<SID> (NULL terminated) as the key */
468
469         fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
470
471         return dbwrap_delete_bystring(db, keystr);
472 }
473
474 /*******************************************************************
475 *******************************************************************/
476
477 bool is_privileged_sid( const struct dom_sid *sid )
478 {
479         uint64_t mask;
480
481         return get_privileges( sid, &mask );
482 }
483
484 /*******************************************************************
485 *******************************************************************/
486
487 bool grant_all_privileges( const struct dom_sid *sid )
488 {
489         uint64_t mask;
490
491         if (!se_priv_put_all_privileges(&mask)) {
492                 return False;
493         }
494
495         return grant_privilege_bitmap( sid, mask );
496 }