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