Ok here it is my latest work on privileges
[ira/wip.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    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /* defines */
25
26 #define ALLOC_CHECK(ptr, err, label, str) do { if ((ptr) == NULL) { DEBUG(0, ("%s: out of memory!\n", str)); err = NT_STATUS_NO_MEMORY; goto label; } } while(0)
27 #define NTSTATUS_CHECK(err, label, str1, str2) do { if (!NT_STATUS_IS_OK(err)) { DEBUG(0, ("%s: %s failed!\n", str1, str2)); } } while(0)
28
29
30 PRIVS privs[] = {
31         {SE_NONE,                       "no_privs",                             "No privilege"}, /* this one MUST be first */
32         {SE_CREATE_TOKEN,               "SeCreateTokenPrivilege",               "Create Token"},
33         {SE_ASSIGN_PRIMARY_TOKEN,       "SeAssignPrimaryTokenPrivilege",        "Assign Primary Token"},
34         {SE_LOCK_MEMORY,                "SeLockMemoryPrivilege",                "Lock Memory"},
35         {SE_INCREASE_QUOTA,             "SeIncreaseQuotaPrivilege",             "Increase Quota"},
36         {SE_UNSOLICITED_INPUT,          "eUnsolicitedInputPrivilege",           "Unsolicited Input"},
37         {SE_MACHINE_ACCOUNT,            "SeMachineAccountPrivilege",            "Can add Machine Accounts to the Domain"},
38         {SE_TCB,                        "SeTcbPrivilege",                       "TCB"},
39         {SE_SECURITY,                   "SeSecurityPrivilege",                  "Security Privilege"},
40         {SE_TAKE_OWNERSHIP,             "SeTakeOwnershipPrivilege",             "Take Ownership Privilege"},
41         {SE_LOAD_DRIVER,                "SeLocalDriverPrivilege",               "Local Driver Privilege"},
42         {SE_SYSTEM_PROFILE,             "SeSystemProfilePrivilege",             "System Profile Privilege"},
43         {SE_SYSTEM_TIME,                "SeSystemtimePrivilege",                "System Time"},
44         {SE_PROF_SINGLE_PROCESS,        "SeProfileSingleProcessPrivilege",      "Profile Single Process Privilege"},
45         {SE_INC_BASE_PRIORITY,          "SeIncreaseBasePriorityPrivilege",      "Increase Base Priority Privilege"},
46         {SE_CREATE_PAGEFILE,            "SeCreatePagefilePrivilege",            "Create Pagefile Privilege"},
47         {SE_CREATE_PERMANENT,           "SeCreatePermanentPrivilege",           "Create Permanent"},
48         {SE_BACKUP,                     "SeBackupPrivilege",                    "Backup Privilege"},
49         {SE_RESTORE,                    "SeRestorePrivilege",                   "Restore Privilege"},
50         {SE_SHUTDOWN,                   "SeShutdownPrivilege",                  "Shutdown Privilege"},
51         {SE_DEBUG,                      "SeDebugPrivilege",                     "Debug Privilege"},
52         {SE_AUDIT,                      "SeAuditPrivilege",                     "Audit"},
53         {SE_SYSTEM_ENVIRONMENT,         "SeSystemEnvironmentPrivilege",         "System Environment Privilege"},
54         {SE_CHANGE_NOTIFY,              "SeChangeNotifyPrivilege",              "Change Notify"},
55         {SE_REMOTE_SHUTDOWN,            "SeRemoteShutdownPrivilege",            "Remote Shutdown Privilege"},
56         {SE_UNDOCK,                     "SeUndockPrivilege",                    "Undock"},
57         {SE_SYNC_AGENT,                 "SeSynchronizationAgentPrivilege",      "Synchronization Agent"},
58         {SE_ENABLE_DELEGATION,          "SeEnableDelegationPrivilege",          "Enable Delegation"},
59         {SE_PRINT_OPERATOR,             "SePrintOperatorPrivilege",             "Printer Operator"},
60         {SE_ADD_USERS,                  "SeAddUsersPrivilege",                  "Add Users"},
61         {SE_ALL_PRIVS,                  "SeAllPrivileges",                      "All Privileges"}
62 };
63
64
65
66 /****************************************************************************
67  Check if a user is a mapped group.
68
69    This function will check if the group SID is mapped onto a
70    system managed gid or onto a winbind manged sid.
71    In the first case it will be threated like a mapped group
72    and the backend should take the member list with a getgrgid
73    and ignore any user that have been possibly set into the group
74    object.
75
76    In the second case, the group is a fully SAM managed group
77    served back to the system through winbind. In this case the
78    members of a Local group are "unrolled" to cope with the fact
79    that unix cannot contain groups inside groups.
80    The backend MUST never call any getgr* / getpw* function or
81    loops with winbind may happen. 
82  ****************************************************************************/
83
84 #if 0
85 NTSTATUS is_mapped_group(BOOL *mapped, const DOM_SID *sid)
86 {
87         NTSTATUS result;
88         gid_t id;
89
90         /* look if mapping exist, do not make idmap alloc an uid if SID is not found */
91         result = idmap_get_gid_from_sid(&id, sid, False);
92         if (NT_STATUS_IS_OK(result)) {
93                 *mapped = gid_is_in_winbind_range(id);
94         } else {
95                 *mapped = False;
96         }
97
98         return result;
99 }
100 #endif
101
102 /****************************************************************************
103  duplicate alloc luid_attr
104  ****************************************************************************/
105 NTSTATUS dupalloc_luid_attr(TALLOC_CTX *mem_ctx, LUID_ATTR **new_la, LUID_ATTR *old_la)
106 {
107         NTSTATUS ret;
108
109         /* don't crash if the source pointer is NULL (since we don't
110            do priviledges now anyways) */
111
112         if ( !old_la )
113                 return NT_STATUS_OK;
114
115         *new_la = (LUID_ATTR *)talloc(mem_ctx, sizeof(LUID_ATTR));
116         ALLOC_CHECK(new_la, ret, done, "dupalloc_luid_attr");
117
118         (*new_la)->luid.high = old_la->luid.high;
119         (*new_la)->luid.low = old_la->luid.low;
120         (*new_la)->attr = old_la->attr;
121         
122         ret = NT_STATUS_OK;
123
124 done:
125         return ret;
126 }
127
128 /****************************************************************************
129  initialise a privilege list
130  ****************************************************************************/
131 NTSTATUS init_privilege(PRIVILEGE_SET **priv_set)
132 {
133         NTSTATUS ret;
134         TALLOC_CTX *mem_ctx = talloc_init("privilege set");
135         ALLOC_CHECK(mem_ctx, ret, done, "init_privilege");
136
137         *priv_set = talloc_zero(mem_ctx, sizeof(PRIVILEGE_SET));
138         ALLOC_CHECK(*priv_set, ret, done, "init_privilege");
139
140         (*priv_set)->mem_ctx = mem_ctx;
141
142         ret = NT_STATUS_OK;
143
144 done:
145         return ret;
146 }
147
148 NTSTATUS init_priv_with_ctx(TALLOC_CTX *mem_ctx, PRIVILEGE_SET **priv_set)
149 {
150         NTSTATUS ret;
151
152         *priv_set = talloc_zero(mem_ctx, sizeof(PRIVILEGE_SET));
153         ALLOC_CHECK(*priv_set, ret, done, "init_privilege");
154
155         (*priv_set)->mem_ctx = mem_ctx;
156         (*priv_set)->ext_ctx = True;
157
158         ret = NT_STATUS_OK;
159
160 done:
161         return ret;
162 }
163
164 void reset_privilege(PRIVILEGE_SET *priv_set)
165 {
166         priv_set->count = 0;
167         priv_set->control = 0;
168         priv_set->set = NULL;
169 }
170
171 void destroy_privilege(PRIVILEGE_SET **priv_set)
172 {
173         reset_privilege(*priv_set);
174         if (!((*priv_set)->ext_ctx))
175                 /* mem_ctx is local, destroy it */
176                 talloc_destroy((*priv_set)->mem_ctx);
177         *priv_set = NULL;
178 }
179
180 /****************************************************************************
181  add a privilege to a privilege array
182  ****************************************************************************/
183 NTSTATUS add_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
184 {
185         NTSTATUS ret;
186         LUID_ATTR *new_set;
187
188         /* check if the privilege is not already in the list */
189         if (NT_STATUS_IS_OK(check_priv_in_privilege(priv_set, set)))
190                 return NT_STATUS_UNSUCCESSFUL;
191
192         /* we can allocate memory to add the new privilege */
193
194         new_set = (LUID_ATTR *)talloc_realloc(priv_set->mem_ctx, priv_set->set, (priv_set->count + 1) * (sizeof(LUID_ATTR)));
195         ALLOC_CHECK(new_set, ret, done, "add_privilege");
196
197         new_set[priv_set->count].luid.high = set.luid.high;
198         new_set[priv_set->count].luid.low = set.luid.low;
199         new_set[priv_set->count].attr = set.attr;
200
201         priv_set->count++;
202         priv_set->set = new_set;
203
204         ret = NT_STATUS_OK;
205
206 done:
207         return ret;
208 }
209
210 NTSTATUS add_privilege_by_name(PRIVILEGE_SET *priv_set, const char *name)
211 {
212         int e;
213
214         for (e = 0; privs[e].se_priv != SE_ALL_PRIVS; e++) {
215                 if (StrCaseCmp(privs[e].priv, name) == 0) {
216                         LUID_ATTR la;
217
218                         la.attr = 0;
219                         la.luid.high = 0;
220                         la.luid.low = privs[e].se_priv;
221
222                         return add_privilege(priv_set, la);
223                 }
224         }
225
226         DEBUG(1, ("add_privilege_by_name: No Such Privilege Found (%s)\n", name));
227
228         return NT_STATUS_UNSUCCESSFUL;
229 }
230
231 /****************************************************************************
232  add all the privileges to a privilege array
233  ****************************************************************************/
234 NTSTATUS add_all_privilege(PRIVILEGE_SET *priv_set)
235 {
236         NTSTATUS result = NT_STATUS_OK;
237         LUID_ATTR set;
238
239         set.attr = 0;
240         set.luid.high = 0;
241
242         /* TODO: set a proper list of privileges */
243         set.luid.low = SE_ADD_USERS;
244         result = add_privilege(priv_set, set);
245         NTSTATUS_CHECK(result, done, "add_all_privilege", "add_privilege");
246
247         set.luid.low = SE_MACHINE_ACCOUNT;
248         result = add_privilege(priv_set, set);
249         NTSTATUS_CHECK(result, done, "add_all_privilege", "add_privilege");
250
251         set.luid.low = SE_PRINT_OPERATOR;
252         result = add_privilege(priv_set, set);
253         NTSTATUS_CHECK(result, done, "add_all_privilege", "add_privilege");
254
255         return result;
256 }
257
258 /****************************************************************************
259  check if the privilege list is empty
260  ****************************************************************************/
261 NTSTATUS check_empty_privilege(PRIVILEGE_SET *priv_set)
262 {
263         if (!priv_set)
264                 return NT_STATUS_INVALID_PARAMETER;
265
266         if (priv_set->count == 0)
267                 return NT_STATUS_OK;
268
269         return NT_STATUS_UNSUCCESSFUL;
270 }
271
272 /****************************************************************************
273  check if the privilege is in the privilege list
274  ****************************************************************************/
275 NTSTATUS check_priv_in_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
276 {
277         int i;
278
279         if (!priv_set)
280                 return NT_STATUS_INVALID_PARAMETER;
281
282         /* if the list is empty, obviously we can't have it */
283         if (NT_STATUS_IS_OK(check_empty_privilege(priv_set)))
284                 return NT_STATUS_UNSUCCESSFUL;
285
286         for (i = 0; i < priv_set->count; i++) {
287                 LUID_ATTR *cur_set;
288
289                 cur_set = &priv_set->set[i];
290                 /* check only the low and high part. Checking the attr field has no meaning */
291                 if (    (cur_set->luid.low == set.luid.low) &&
292                         (cur_set->luid.high == set.luid.high)   ) {
293                         return NT_STATUS_OK;
294                 }
295         }
296
297         return NT_STATUS_UNSUCCESSFUL;
298 }
299
300 /****************************************************************************
301  remove a privilege from a privilege array
302  ****************************************************************************/
303 NTSTATUS remove_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
304 {
305         NTSTATUS ret;
306         LUID_ATTR *new_set;
307         LUID_ATTR *old_set;
308         int i,j;
309
310         if (!priv_set)
311                 return NT_STATUS_INVALID_PARAMETER;
312
313         /* check if the privilege is in the list */
314         if (!NT_STATUS_IS_OK(check_priv_in_privilege(priv_set, set)))
315                 return NT_STATUS_UNSUCCESSFUL;
316
317         /* special case if it's the only privilege in the list */
318         if (priv_set->count == 1) {
319                 reset_privilege(priv_set);      
320                 return NT_STATUS_OK;
321         }
322
323         /* 
324          * the privilege is there, create a new list,
325          * and copy the other privileges
326          */
327
328         old_set = priv_set->set;
329
330         new_set = (LUID_ATTR *)talloc(priv_set->mem_ctx, (priv_set->count - 1) * (sizeof(LUID_ATTR)));
331         ALLOC_CHECK(new_set, ret, done, "remove_privilege");
332
333         for (i=0, j=0; i < priv_set->count; i++) {
334                 if (    (old_set[i].luid.low == set.luid.low) && 
335                         (old_set[i].luid.high == set.luid.high) ) {
336                         continue;
337                 }
338                 
339                 new_set[j].luid.low = old_set[i].luid.low;
340                 new_set[j].luid.high = old_set[i].luid.high;
341                 new_set[j].attr = old_set[i].attr;
342
343                 j++;
344         }
345         
346         if (j != priv_set->count - 1) {
347                 DEBUG(0,("remove_privilege: mismatch ! difference is not -1\n"));
348                 DEBUGADD(0,("old count:%d, new count:%d\n", priv_set->count, j));
349                 return NT_STATUS_INTERNAL_ERROR;
350         }
351                 
352         /* ok everything is fine */
353         
354         priv_set->count--;
355         priv_set->set = new_set;
356         
357         ret = NT_STATUS_OK;
358
359 done:
360         return ret;
361 }
362
363 /****************************************************************************
364  duplicates a privilege array
365  the new privilege set must be passed inited
366  (use init_privilege or init_priv_with_ctx)
367  ****************************************************************************/
368 NTSTATUS dup_priv_set(PRIVILEGE_SET *new_priv_set, PRIVILEGE_SET *priv_set)
369 {
370         NTSTATUS ret;
371         LUID_ATTR *new_set;
372         LUID_ATTR *old_set;
373         int i;
374
375         if (!new_priv_set || !priv_set)
376                 return NT_STATUS_INVALID_PARAMETER;
377
378         /* special case if there are no privileges in the list */
379         if (priv_set->count == 0) {
380                 return NT_STATUS_OK;
381         }
382
383         /* 
384          * create a new list,
385          * and copy the other privileges
386          */
387
388         old_set = priv_set->set;
389
390         new_set = (LUID_ATTR *)talloc(new_priv_set->mem_ctx, (priv_set->count) * (sizeof(LUID_ATTR)));
391         ALLOC_CHECK(new_set, ret, done, "dup_priv_set");
392
393         for (i=0; i < priv_set->count; i++) {
394                 
395                 new_set[i].luid.low = old_set[i].luid.low;
396                 new_set[i].luid.high = old_set[i].luid.high;
397                 new_set[i].attr = old_set[i].attr;
398         }
399                         
400         new_priv_set->count = priv_set->count;
401         new_priv_set->control = priv_set->control;
402         new_priv_set->set = new_set;
403         
404         ret = NT_STATUS_OK;
405
406 done:
407         return ret;
408 }
409
410
411 NTSTATUS user_has_privilege(struct current_user *user, uint32 privilege)
412 {
413         LUID_ATTR set;
414
415         set.attr = 0;
416         set.luid.high = 0;
417         set.luid.low = privilege;
418
419         return check_priv_in_privilege(user->privs, set);
420 }
421