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