import HEAD into svn+ssh://svn.samba.org/home/svn/samba/trunk
[metze/old/v3-2-winbind-ndr.git] / source / 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,          "SeUnsolicitedInputPrivilege",          "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, int count)
106 {
107         NTSTATUS ret;
108         int i;
109
110         /* don't crash if the source pointer is NULL (since we don't
111            do priviledges now anyways) */
112
113         if ( !old_la )
114                 return NT_STATUS_OK;
115
116         *new_la = (LUID_ATTR *)talloc(mem_ctx, count*sizeof(LUID_ATTR));
117         ALLOC_CHECK(new_la, ret, done, "dupalloc_luid_attr");
118
119         for (i=0; i<count; i++) {
120                 (*new_la)[i].luid.high = old_la[i].luid.high;
121                 (*new_la)[i].luid.low = old_la[i].luid.low;
122                 (*new_la)[i].attr = old_la[i].attr;
123         }
124         
125         ret = NT_STATUS_OK;
126
127 done:
128         return ret;
129 }
130
131 /****************************************************************************
132  initialise a privilege list
133  ****************************************************************************/
134 NTSTATUS init_privilege(PRIVILEGE_SET **priv_set)
135 {
136         NTSTATUS ret;
137         TALLOC_CTX *mem_ctx = talloc_init("privilege set");
138         ALLOC_CHECK(mem_ctx, ret, done, "init_privilege");
139
140         *priv_set = talloc_zero(mem_ctx, sizeof(PRIVILEGE_SET));
141         ALLOC_CHECK(*priv_set, ret, done, "init_privilege");
142
143         (*priv_set)->mem_ctx = mem_ctx;
144
145         ret = NT_STATUS_OK;
146
147 done:
148         return ret;
149 }
150
151 NTSTATUS init_priv_with_ctx(TALLOC_CTX *mem_ctx, PRIVILEGE_SET **priv_set)
152 {
153         NTSTATUS ret;
154
155         *priv_set = talloc_zero(mem_ctx, sizeof(PRIVILEGE_SET));
156         ALLOC_CHECK(*priv_set, ret, done, "init_privilege");
157
158         (*priv_set)->mem_ctx = mem_ctx;
159         (*priv_set)->ext_ctx = True;
160
161         ret = NT_STATUS_OK;
162
163 done:
164         return ret;
165 }
166
167 void reset_privilege(PRIVILEGE_SET *priv_set)
168 {
169         priv_set->count = 0;
170         priv_set->control = 0;
171         priv_set->set = NULL;
172 }
173
174 void destroy_privilege(PRIVILEGE_SET **priv_set)
175 {
176         if (priv_set == NULL || *priv_set == NULL)
177                 return;
178
179         reset_privilege(*priv_set);
180         if (!((*priv_set)->ext_ctx))
181                 /* mem_ctx is local, destroy it */
182                 talloc_destroy((*priv_set)->mem_ctx);
183         *priv_set = NULL;
184 }
185
186 /****************************************************************************
187  add a privilege to a privilege array
188  ****************************************************************************/
189 NTSTATUS add_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
190 {
191         NTSTATUS ret;
192         LUID_ATTR *new_set;
193
194         /* check if the privilege is not already in the list */
195         if (NT_STATUS_IS_OK(check_priv_in_privilege(priv_set, set)))
196                 return NT_STATUS_UNSUCCESSFUL;
197
198         /* we can allocate memory to add the new privilege */
199
200         new_set = (LUID_ATTR *)talloc_realloc(priv_set->mem_ctx, priv_set->set, (priv_set->count + 1) * (sizeof(LUID_ATTR)));
201         ALLOC_CHECK(new_set, ret, done, "add_privilege");
202
203         new_set[priv_set->count].luid.high = set.luid.high;
204         new_set[priv_set->count].luid.low = set.luid.low;
205         new_set[priv_set->count].attr = set.attr;
206
207         priv_set->count++;
208         priv_set->set = new_set;
209
210         ret = NT_STATUS_OK;
211
212 done:
213         return ret;
214 }
215
216 NTSTATUS add_privilege_by_name(PRIVILEGE_SET *priv_set, const char *name)
217 {
218         int e;
219
220         for (e = 0; privs[e].se_priv != SE_ALL_PRIVS; e++) {
221                 if (StrCaseCmp(privs[e].priv, name) == 0) {
222                         LUID_ATTR la;
223
224                         la.attr = 0;
225                         la.luid.high = 0;
226                         la.luid.low = privs[e].se_priv;
227
228                         return add_privilege(priv_set, la);
229                 }
230         }
231
232         DEBUG(1, ("add_privilege_by_name: No Such Privilege Found (%s)\n", name));
233
234         return NT_STATUS_UNSUCCESSFUL;
235 }
236
237 /****************************************************************************
238  add all the privileges to a privilege array
239  ****************************************************************************/
240 NTSTATUS add_all_privilege(PRIVILEGE_SET *priv_set)
241 {
242         NTSTATUS result = NT_STATUS_OK;
243         LUID_ATTR set;
244
245         set.attr = 0;
246         set.luid.high = 0;
247
248         /* TODO: set a proper list of privileges */
249         set.luid.low = SE_ADD_USERS;
250         result = add_privilege(priv_set, set);
251         NTSTATUS_CHECK(result, done, "add_all_privilege", "add_privilege");
252
253         set.luid.low = SE_MACHINE_ACCOUNT;
254         result = add_privilege(priv_set, set);
255         NTSTATUS_CHECK(result, done, "add_all_privilege", "add_privilege");
256
257         set.luid.low = SE_PRINT_OPERATOR;
258         result = add_privilege(priv_set, set);
259         NTSTATUS_CHECK(result, done, "add_all_privilege", "add_privilege");
260
261         return result;
262 }
263
264 /****************************************************************************
265  check if the privilege list is empty
266  ****************************************************************************/
267 NTSTATUS check_empty_privilege(PRIVILEGE_SET *priv_set)
268 {
269         if (!priv_set)
270                 return NT_STATUS_INVALID_PARAMETER;
271
272         if (priv_set->count == 0)
273                 return NT_STATUS_OK;
274
275         return NT_STATUS_UNSUCCESSFUL;
276 }
277
278 /****************************************************************************
279  check if the privilege is in the privilege list
280  ****************************************************************************/
281 NTSTATUS check_priv_in_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
282 {
283         int i;
284
285         if (!priv_set)
286                 return NT_STATUS_INVALID_PARAMETER;
287
288         /* if the list is empty, obviously we can't have it */
289         if (NT_STATUS_IS_OK(check_empty_privilege(priv_set)))
290                 return NT_STATUS_UNSUCCESSFUL;
291
292         for (i = 0; i < priv_set->count; i++) {
293                 LUID_ATTR *cur_set;
294
295                 cur_set = &priv_set->set[i];
296                 /* check only the low and high part. Checking the attr field has no meaning */
297                 if (    (cur_set->luid.low == set.luid.low) &&
298                         (cur_set->luid.high == set.luid.high)   ) {
299                         return NT_STATUS_OK;
300                 }
301         }
302
303         return NT_STATUS_UNSUCCESSFUL;
304 }
305
306 /****************************************************************************
307  remove a privilege from a privilege array
308  ****************************************************************************/
309 NTSTATUS remove_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
310 {
311         NTSTATUS ret;
312         LUID_ATTR *new_set;
313         LUID_ATTR *old_set;
314         int i,j;
315
316         if (!priv_set)
317                 return NT_STATUS_INVALID_PARAMETER;
318
319         /* check if the privilege is in the list */
320         if (!NT_STATUS_IS_OK(check_priv_in_privilege(priv_set, set)))
321                 return NT_STATUS_UNSUCCESSFUL;
322
323         /* special case if it's the only privilege in the list */
324         if (priv_set->count == 1) {
325                 reset_privilege(priv_set);      
326                 return NT_STATUS_OK;
327         }
328
329         /* 
330          * the privilege is there, create a new list,
331          * and copy the other privileges
332          */
333
334         old_set = priv_set->set;
335
336         new_set = (LUID_ATTR *)talloc(priv_set->mem_ctx, (priv_set->count - 1) * (sizeof(LUID_ATTR)));
337         ALLOC_CHECK(new_set, ret, done, "remove_privilege");
338
339         for (i=0, j=0; i < priv_set->count; i++) {
340                 if (    (old_set[i].luid.low == set.luid.low) && 
341                         (old_set[i].luid.high == set.luid.high) ) {
342                         continue;
343                 }
344                 
345                 new_set[j].luid.low = old_set[i].luid.low;
346                 new_set[j].luid.high = old_set[i].luid.high;
347                 new_set[j].attr = old_set[i].attr;
348
349                 j++;
350         }
351         
352         if (j != priv_set->count - 1) {
353                 DEBUG(0,("remove_privilege: mismatch ! difference is not -1\n"));
354                 DEBUGADD(0,("old count:%d, new count:%d\n", priv_set->count, j));
355                 return NT_STATUS_INTERNAL_ERROR;
356         }
357                 
358         /* ok everything is fine */
359         
360         priv_set->count--;
361         priv_set->set = new_set;
362         
363         ret = NT_STATUS_OK;
364
365 done:
366         return ret;
367 }
368
369 /****************************************************************************
370  duplicates a privilege array
371  the new privilege set must be passed inited
372  (use init_privilege or init_priv_with_ctx)
373  ****************************************************************************/
374 NTSTATUS dup_priv_set(PRIVILEGE_SET *new_priv_set, PRIVILEGE_SET *priv_set)
375 {
376         NTSTATUS ret;
377         LUID_ATTR *new_set;
378         LUID_ATTR *old_set;
379         int i;
380
381         if (new_priv_set == NULL || priv_set == NULL)
382                 return NT_STATUS_INVALID_PARAMETER;
383
384         /* special case if there are no privileges in the list */
385         if (priv_set->count == 0) {
386                 return NT_STATUS_OK;
387         }
388
389         /* 
390          * create a new list,
391          * and copy the other privileges
392          */
393
394         old_set = priv_set->set;
395
396         new_set = (LUID_ATTR *)talloc(new_priv_set->mem_ctx, (priv_set->count) * (sizeof(LUID_ATTR)));
397         ALLOC_CHECK(new_set, ret, done, "dup_priv_set");
398
399         for (i=0; i < priv_set->count; i++) {
400                 
401                 new_set[i].luid.low = old_set[i].luid.low;
402                 new_set[i].luid.high = old_set[i].luid.high;
403                 new_set[i].attr = old_set[i].attr;
404         }
405                         
406         new_priv_set->count = priv_set->count;
407         new_priv_set->control = priv_set->control;
408         new_priv_set->set = new_set;
409         
410         ret = NT_STATUS_OK;
411
412 done:
413         return ret;
414 }
415
416
417 NTSTATUS user_has_privilege(struct current_user *user, uint32 privilege)
418 {
419         LUID_ATTR set;
420
421         set.attr = 0;
422         set.luid.high = 0;
423         set.luid.low = privilege;
424
425         return check_priv_in_privilege(user->privs, set);
426 }
427
428 BOOL luid_to_privilege_name(const LUID *set, fstring name)
429 {
430         int i;
431
432         if (set->high != 0)
433                 return False;
434
435         for (i=1; i<PRIV_ALL_INDEX-1; i++) {
436                 if (set->low == privs[i].se_priv) {
437                         fstrcpy(name, privs[i].priv);
438                         return True;
439                 }
440         }
441         return False;
442 }