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