1 /* security.h: security declarations
3 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 2010, 2011 Red Hat, Inc.
6 This file is part of Cygwin.
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
17 /* Special file attribute set, for instance, in open() and mkdir() to
18 flag that a file has just been created. Used in alloc_sd, see there. */
19 #define S_JUSTCREATED 0x80000000
21 #define DEFAULT_UID DOMAIN_USER_RID_ADMIN
22 #define UNKNOWN_UID 400 /* Non conflicting number */
23 #define UNKNOWN_GID 401
25 #define MAX_SID_LEN 40
26 #define MAX_DACL_LEN(n) (sizeof (ACL) \
27 + (n) * (sizeof (ACCESS_ALLOWED_ACE) - sizeof (DWORD) + MAX_SID_LEN))
28 #define SD_MIN_SIZE (sizeof (SECURITY_DESCRIPTOR) + MAX_DACL_LEN (1))
29 #define ACL_MAXIMUM_SIZE 65532 /* Yeah, right. 64K - sizeof (DWORD). */
30 #define SD_MAXIMUM_SIZE 65536
31 #define NO_SID ((PSID)NULL)
33 #ifndef SE_CREATE_TOKEN_PRIVILEGE
34 #define SE_CREATE_TOKEN_PRIVILEGE 2UL
35 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE 3UL
36 #define SE_LOCK_MEMORY_PRIVILEGE 4UL
37 #define SE_INCREASE_QUOTA_PRIVILEGE 5UL
38 #define SE_MACHINE_ACCOUNT_PRIVILEGE 6UL
39 #define SE_TCB_PRIVILEGE 7UL
40 #define SE_SECURITY_PRIVILEGE 8UL
41 #define SE_TAKE_OWNERSHIP_PRIVILEGE 9UL
42 #define SE_LOAD_DRIVER_PRIVILEGE 10UL
43 #define SE_SYSTEM_PROFILE_PRIVILEGE 11UL
44 #define SE_SYSTEMTIME_PRIVILEGE 12UL
45 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE 13UL
46 #define SE_INC_BASE_PRIORITY_PRIVILEGE 14UL
47 #define SE_CREATE_PAGEFILE_PRIVILEGE 15UL
48 #define SE_CREATE_PERMANENT_PRIVILEGE 16UL
49 #define SE_BACKUP_PRIVILEGE 17UL
50 #define SE_RESTORE_PRIVILEGE 18UL
51 #define SE_SHUTDOWN_PRIVILEGE 19UL
52 #define SE_DEBUG_PRIVILEGE 20UL
53 #define SE_AUDIT_PRIVILEGE 21UL
54 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE 22UL
55 #define SE_CHANGE_NOTIFY_PRIVILEGE 23UL
56 #define SE_REMOTE_SHUTDOWN_PRIVILEGE 24UL
57 /* Starting with Windows 2000 */
58 #define SE_UNDOCK_PRIVILEGE 25UL
59 #define SE_SYNC_AGENT_PRIVILEGE 26UL
60 #define SE_ENABLE_DELEGATION_PRIVILEGE 27UL
61 #define SE_MANAGE_VOLUME_PRIVILEGE 28UL
62 /* Starting with Windows 2000 SP4, XP SP2, 2003 Server */
63 #define SE_IMPERSONATE_PRIVILEGE 29UL
64 #define SE_CREATE_GLOBAL_PRIVILEGE 30UL
65 /* Starting with Vista */
66 #define SE_TRUSTED_CREDMAN_ACCESS_PRIVILEGE 31UL
67 #define SE_RELABEL_PRIVILEGE 32UL
68 #define SE_INCREASE_WORKING_SET_PRIVILEGE 33UL
69 #define SE_TIME_ZONE_PRIVILEGE 34UL
70 #define SE_CREATE_SYMBOLIC_LINK_PRIVILEGE 35UL
72 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_SYMBOLIC_LINK_PRIVILEGE
74 #endif /* ! SE_CREATE_TOKEN_PRIVILEGE */
76 /* Added for debugging purposes. */
79 BYTE SubAuthorityCount;
80 SID_IDENTIFIER_AUTHORITY IdentifierAuthority;
81 DWORD SubAuthority[8];
84 /* Macro to define variable length SID structures */
85 #define MKSID(name, comment, authority, count, rid...) \
86 static NO_COPY struct { \
88 BYTE SubAuthorityCount; \
89 SID_IDENTIFIER_AUTHORITY IdentifierAuthority; \
90 DWORD SubAuthority[count]; \
91 } name##_struct = { SID_REVISION, count, {authority}, {rid}}; \
92 cygpsid NO_COPY name = (PSID) &name##_struct;
94 #define FILE_READ_BITS (FILE_READ_DATA | GENERIC_READ | GENERIC_ALL)
95 #define FILE_WRITE_BITS (FILE_WRITE_DATA | GENERIC_WRITE | GENERIC_ALL)
96 #define FILE_EXEC_BITS (FILE_EXECUTE | GENERIC_EXECUTE | GENERIC_ALL)
102 /* We need these declarations, otherwise g++ complains that the below
103 inline methods use an undefined function, if ntdll.h isn't included. */
104 BOOLEAN NTAPI RtlEqualSid (PSID, PSID);
105 NTSTATUS NTAPI RtlCopySid (ULONG, PSID, PSID);
115 cygpsid (PSID nsid) { psid = nsid; }
116 operator PSID () const { return psid; }
117 const PSID operator= (PSID nsid) { return psid = nsid;}
118 __uid32_t get_id (BOOL search_grp, int *type = NULL);
119 int get_uid () { return get_id (FALSE); }
120 int get_gid () { return get_id (TRUE); }
122 PWCHAR string (PWCHAR nsidstr) const;
123 char *string (char *nsidstr) const;
125 bool operator== (const PSID nsid) const
129 return RtlEqualSid (psid, nsid);
131 bool operator!= (const PSID nsid) const
132 { return !(*this == nsid); }
133 bool operator== (const char *nsidstr) const;
134 bool operator!= (const char *nsidstr) const
135 { return !(*this == nsidstr); }
137 void debug_print (const char *prefix = NULL) const
139 char buf[256] __attribute__ ((unused));
140 debug_printf ("%s %s", prefix ?: "", string (buf) ?: "NULL");
144 class cygsid : public cygpsid {
145 char sbuf[MAX_SID_LEN];
148 const PSID getfromstr (const char *nsidstr, bool well_known);
149 PSID get_sid (DWORD s, DWORD cnt, DWORD *r, bool well_known);
151 inline const PSID assign (const PSID nsid, bool well_known)
158 RtlCopySid (MAX_SID_LEN, psid, nsid);
159 well_known_sid = well_known;
165 inline operator const PSID () { return psid; }
166 inline bool is_well_known_sid () { return well_known_sid; }
168 /* Both, = and *= are assignment operators. = creates a "normal" SID,
169 *= marks the SID as being a well-known SID. This difference is
170 important when creating a SID list for LSA authentication. */
171 inline const PSID operator= (cygsid &nsid)
172 { return assign (nsid, nsid.well_known_sid); }
173 inline const PSID operator= (const PSID nsid)
174 { return assign (nsid, false); }
175 inline const PSID operator= (const char *nsidstr)
176 { return getfromstr (nsidstr, false); }
177 inline const PSID operator*= (cygsid &nsid)
178 { return assign (nsid, true); }
179 inline const PSID operator*= (const PSID nsid)
180 { return assign (nsid, true); }
181 inline const PSID operator*= (const char *nsidstr)
182 { return getfromstr (nsidstr, true); }
184 inline cygsid () : cygpsid ((PSID) sbuf), well_known_sid (false) {}
185 inline cygsid (const PSID nsid) { *this = nsid; }
186 inline cygsid (const char *nstrsid) { *this = nstrsid; }
188 inline PSID set () { return psid = (PSID) sbuf; }
190 BOOL getfrompw (const struct passwd *pw);
191 BOOL getfromgr (const struct __group32 *gr);
193 void debug_print (const char *prefix = NULL) const
195 char buf[256] __attribute__ ((unused));
196 debug_printf ("%s %s%s", prefix ?: "", string (buf) ?: "NULL", well_known_sid ? " (*)" : " (+)");
200 typedef enum { cygsidlist_empty, cygsidlist_alloc, cygsidlist_auto } cygsidlist_type;
205 BOOL add (const PSID nsi, bool well_known); /* Only with auto for now */
209 cygsidlist_type type;
211 cygsidlist (cygsidlist_type t, int m)
212 : maxcnt (m), cnt (0), type (t)
214 if (t == cygsidlist_alloc)
215 sids = alloc_sids (m);
217 sids = new cygsid [m];
219 ~cygsidlist () { if (type == cygsidlist_auto) delete [] sids; }
221 BOOL addfromgr (struct __group32 *gr) /* Only with alloc */
222 { return sids[cnt].getfromgr (gr) && ++cnt; }
224 /* += adds a "normal" SID, *= adds a well-known SID. See comment in class
226 BOOL operator+= (cygsid &si) { return add ((PSID) si, false); }
227 BOOL operator+= (const char *sidstr) { cygsid nsi (sidstr);
228 return add ((PSID) nsi, false); }
229 BOOL operator+= (const PSID psid) { return add (psid, false); }
230 BOOL operator*= (cygsid &si) { return add ((PSID) si, true); }
231 BOOL operator*= (const char *sidstr) { cygsid nsi (sidstr);
232 return add ((PSID) nsi, true); }
233 BOOL operator*= (const PSID psid) { return add (psid, true); }
235 void count (int ncnt)
237 int count () const { return cnt; }
238 int non_well_known_count () const
241 for (int i = 0; i < cnt; ++i)
242 if (!sids[i].is_well_known_sid ())
247 int position (const PSID sid) const
249 for (int i = 0; i < cnt; ++i)
255 int next_non_well_known_sid (int idx)
258 if (!sids[idx].is_well_known_sid ())
262 BOOL contains (const PSID sid) const { return position (sid) >= 0; }
263 cygsid *alloc_sids (int n);
265 void debug_print (const char *prefix = NULL) const
267 debug_printf ("-- begin sidlist ---");
269 debug_printf ("No elements");
270 for (int i = 0; i < cnt; ++i)
271 sids[i].debug_print (prefix);
272 debug_printf ("-- ende sidlist ---");
276 /* Wrapper class to allow simple deleting of buffer space allocated
278 class security_descriptor {
280 PSECURITY_DESCRIPTOR psd;
283 security_descriptor () : psd (NULL), sd_size (0) {}
284 ~security_descriptor () { free (); }
286 PSECURITY_DESCRIPTOR malloc (size_t nsize);
287 PSECURITY_DESCRIPTOR realloc (size_t nsize);
290 inline DWORD size () const { return sd_size; }
291 inline DWORD copy (void *buf, DWORD buf_size) const {
292 if (buf_size < size ())
294 memcpy (buf, psd, sd_size);
297 inline operator const PSECURITY_DESCRIPTOR () { return psd; }
298 inline operator PSECURITY_DESCRIPTOR *() { return &psd; }
299 inline void operator =(PSECURITY_DESCRIPTOR nsd) { psd = nsd; }
308 BOOL issetgroups () const { return (sgsids.type == cygsidlist_alloc); }
309 void update_supp (const cygsidlist &newsids)
323 void update_pgrp (const PSID sid)
330 extern cygpsid well_known_null_sid;
331 extern cygpsid well_known_world_sid;
332 extern cygpsid well_known_local_sid;
333 extern cygpsid well_known_console_logon_sid;
334 extern cygpsid well_known_creator_owner_sid;
335 extern cygpsid well_known_creator_group_sid;
336 extern cygpsid well_known_dialup_sid;
337 extern cygpsid well_known_network_sid;
338 extern cygpsid well_known_batch_sid;
339 extern cygpsid well_known_interactive_sid;
340 extern cygpsid well_known_service_sid;
341 extern cygpsid well_known_authenticated_users_sid;
342 extern cygpsid well_known_this_org_sid;
343 extern cygpsid well_known_system_sid;
344 extern cygpsid well_known_builtin_sid;
345 extern cygpsid well_known_admins_sid;
346 extern cygpsid well_known_users_sid;
347 extern cygpsid fake_logon_sid;
348 extern cygpsid mandatory_medium_integrity_sid;
349 extern cygpsid mandatory_high_integrity_sid;
350 extern cygpsid mandatory_system_integrity_sid;
351 extern cygpsid well_known_samba_unix_user_fake_sid;
353 bool privilege_luid (const PWCHAR pname, LUID &luid, bool &high_integrity);
356 well_known_sid_type (SID_NAME_USE type)
358 return type == SidTypeAlias || type == SidTypeWellKnownGroup;
362 legal_sid_type (SID_NAME_USE type)
364 return type == SidTypeUser || type == SidTypeGroup
365 || type == SidTypeAlias || type == SidTypeWellKnownGroup;
369 /* File manipulation */
370 int __stdcall get_file_attribute (HANDLE, path_conv &, mode_t *,
371 __uid32_t *, __gid32_t *)
372 __attribute__ ((regparm (3)));
373 int __stdcall set_file_attribute (HANDLE, path_conv &,
374 __uid32_t, __gid32_t, mode_t)
375 __attribute__ ((regparm (3)));
376 int __stdcall get_object_sd (HANDLE, security_descriptor &)
377 __attribute__ ((regparm (2)));
378 int __stdcall get_object_attribute (HANDLE, __uid32_t *, __gid32_t *, mode_t *)
379 __attribute__ ((regparm (3)));
380 int __stdcall set_object_attribute (HANDLE, __uid32_t, __gid32_t, mode_t)
381 __attribute__ ((regparm (3)));
382 int __stdcall create_object_sd_from_attribute (HANDLE, __uid32_t, __gid32_t,
383 mode_t, security_descriptor &)
384 __attribute__ ((regparm (3)));
385 int __stdcall set_object_sd (HANDLE, security_descriptor &, bool)
386 __attribute__ ((regparm (3)));
388 int __stdcall get_reg_attribute (HKEY hkey, mode_t *, __uid32_t *, __gid32_t *)
389 __attribute__ ((regparm (3)));
390 LONG __stdcall get_file_sd (HANDLE fh, path_conv &, security_descriptor &, bool)
391 __attribute__ ((regparm (3)));
392 LONG __stdcall set_file_sd (HANDLE fh, path_conv &, security_descriptor &, bool)
393 __attribute__ ((regparm (3)));
394 bool __stdcall add_access_allowed_ace (PACL, int, DWORD, PSID, size_t &, DWORD)
395 __attribute__ ((regparm (3)));
396 bool __stdcall add_access_denied_ace (PACL, int, DWORD, PSID, size_t &, DWORD)
397 __attribute__ ((regparm (3)));
398 int __stdcall check_file_access (path_conv &, int, bool)
399 __attribute__ ((regparm (3)));
400 int __stdcall check_registry_access (HANDLE, int, bool)
401 __attribute__ ((regparm (3)));
403 void set_security_attribute (path_conv &pc, int attribute,
404 PSECURITY_ATTRIBUTES psa,
405 security_descriptor &sd_buf);
407 bool get_sids_info (cygpsid, cygpsid, __uid32_t * , __gid32_t *);
411 extern "C" int aclsort32 (int, int, __acl32 *);
412 extern "C" int acl32 (const char *, int, int, __acl32 *);
413 int getacl (HANDLE, path_conv &, int, __acl32 *);
414 int setacl (HANDLE, path_conv &, int, __acl32 *, bool &);
416 /* Set impersonation or restricted token. */
417 void set_imp_token (HANDLE token, int type);
418 /* Function creating a token by calling NtCreateToken. */
419 HANDLE create_token (cygsid &usersid, user_groups &groups, struct passwd * pw);
420 /* LSA authentication function. */
421 HANDLE lsaauth (cygsid &, user_groups &, struct passwd *);
422 /* LSA private key storage authentication, same as when using service logons. */
423 HANDLE lsaprivkeyauth (struct passwd *pw);
424 /* Verify an existing token */
425 bool verify_token (HANDLE token, cygsid &usersid, user_groups &groups, bool *pintern = NULL);
426 /* Get groups of a user */
427 bool get_server_groups (cygsidlist &grp_list, PSID usersid, struct passwd *pw);
429 /* Extract U-domain\user field from passwd entry. */
430 void extract_nt_dom_user (const struct passwd *pw, PWCHAR domain, PWCHAR user);
431 /* Get default logonserver for a domain. */
432 bool get_logon_server (PWCHAR domain, PWCHAR wserver, bool rediscovery);
434 HANDLE open_local_policy (ACCESS_MASK access);
436 /* sec_helper.cc: Security helper functions. */
437 int set_privilege (HANDLE token, DWORD privilege, bool enable);
438 void set_cygwin_privileges (HANDLE token);
440 #define _push_thread_privilege(_priv, _val, _check) { \
441 HANDLE _dup_token = NULL; \
442 HANDLE _token = (cygheap->user.issetuid () && (_check)) \
443 ? cygheap->user.primary_token () : hProcToken; \
444 if (!DuplicateTokenEx (_token, MAXIMUM_ALLOWED, NULL, \
445 SecurityImpersonation, TokenImpersonation, \
447 debug_printf ("DuplicateTokenEx: %E"); \
448 else if (!ImpersonateLoggedOnUser (_dup_token)) \
449 debug_printf ("ImpersonateLoggedOnUser: %E"); \
451 set_privilege (_dup_token, (_priv), (_val));
453 #define push_thread_privilege(_priv, _val) _push_thread_privilege(_priv,_val,1)
454 #define push_self_privilege(_priv, _val) _push_thread_privilege(_priv,_val,0)
456 #define pop_thread_privilege() \
459 if (!cygheap->user.issetuid ()) \
462 cygheap->user.reimpersonate (); \
463 CloseHandle (_dup_token); \
467 #define pop_self_privilege() pop_thread_privilege()
471 /* Various types of security attributes for use in Create* functions. */
472 extern SECURITY_ATTRIBUTES sec_none, sec_none_nih, sec_all, sec_all_nih;
473 extern SECURITY_ATTRIBUTES *__stdcall __sec_user (PVOID, PSID, PSID,
475 __attribute__ ((regparm (3)));
476 extern PSECURITY_DESCRIPTOR _everyone_sd (void *buf, ACCESS_MASK access);
477 #define everyone_sd(access) (_everyone_sd (alloca (SD_MIN_SIZE), (access)))
479 #define sec_none_cloexec(f) (((f) & O_CLOEXEC ? &sec_none_nih : &sec_none))
481 extern bool sec_acl (PACL acl, bool original, bool admins, PSID sid1 = NO_SID,
482 PSID sid2 = NO_SID, DWORD access2 = 0);
484 ssize_t __stdcall read_ea (HANDLE, path_conv &, const char *,
486 __attribute__ ((regparm (3)));
487 int __stdcall write_ea (HANDLE, path_conv &, const char *, const char *,
489 __attribute__ ((regparm (3)));
491 /* Note: sid1 is usually (read: currently always) the current user's
492 effective sid (cygheap->user.sid ()). */
493 extern inline SECURITY_ATTRIBUTES *
494 sec_user_nih (SECURITY_ATTRIBUTES *sa_buf, PSID sid1, PSID sid2 = NULL,
497 return __sec_user (sa_buf, sid1, sid2, access2, FALSE);
500 extern inline SECURITY_ATTRIBUTES *
501 sec_user (SECURITY_ATTRIBUTES *sa_buf, PSID sid1, PSID sid2 = NULL,
504 return __sec_user (sa_buf, sid1, sid2, access2, TRUE);
507 #endif /*_SECURITY_H*/