1 /* security.h: security declarations
3 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
16 #define DEFAULT_UID DOMAIN_USER_RID_ADMIN
17 #define UNKNOWN_UID 400 /* Non conflicting number */
18 #define UNKNOWN_GID 401
20 #define MAX_SID_LEN 40
21 #define MAX_DACL_LEN(n) (sizeof (ACL) \
22 + (n) * (sizeof (ACCESS_ALLOWED_ACE) - sizeof (DWORD) + MAX_SID_LEN))
23 #define ACL_DEFAULT_SIZE 3072
24 #define NO_SID ((PSID)NULL)
26 /* Added for debugging purposes. */
29 BYTE SubAuthorityCount;
30 SID_IDENTIFIER_AUTHORITY IdentifierAuthority;
31 DWORD SubAuthority[8];
34 /* Macro to define variable length SID structures */
35 #define MKSID(name, comment, authority, count, rid...) \
36 static NO_COPY struct { \
38 BYTE SubAuthorityCount; \
39 SID_IDENTIFIER_AUTHORITY IdentifierAuthority; \
40 DWORD SubAuthority[count]; \
41 } name##_struct = { SID_REVISION, count, {authority}, {rid}}; \
42 cygpsid NO_COPY name = (PSID) &name##_struct;
44 #define FILE_READ_BITS (FILE_READ_DATA | GENERIC_READ | GENERIC_ALL)
45 #define FILE_WRITE_BITS (FILE_WRITE_DATA | GENERIC_WRITE | GENERIC_ALL)
46 #define FILE_EXEC_BITS (FILE_EXECUTE | GENERIC_EXECUTE | GENERIC_ALL)
53 cygpsid (PSID nsid) { psid = nsid; }
54 operator PSID () const { return psid; }
55 const PSID operator= (PSID nsid) { return psid = nsid;}
56 __uid32_t get_id (BOOL search_grp, int *type = NULL);
57 int get_uid () { return get_id (FALSE); }
58 int get_gid () { return get_id (TRUE); }
60 char *string (char *nsidstr) const;
62 bool operator== (const PSID nsid) const
66 return EqualSid (psid, nsid);
68 bool operator!= (const PSID nsid) const
69 { return !(*this == nsid); }
70 bool operator== (const char *nsidstr) const;
71 bool operator!= (const char *nsidstr) const
72 { return !(*this == nsidstr); }
74 void debug_print (const char *prefix = NULL) const
76 char buf[256] __attribute__ ((unused));
77 debug_printf ("%s %s", prefix ?: "", string (buf) ?: "NULL");
81 class cygsid : public cygpsid {
82 char sbuf[MAX_SID_LEN];
85 const PSID getfromstr (const char *nsidstr, bool well_known);
86 PSID get_sid (DWORD s, DWORD cnt, DWORD *r, bool well_known);
88 inline const PSID assign (const PSID nsid, bool well_known)
95 CopySid (MAX_SID_LEN, psid, nsid);
96 well_known_sid = well_known;
102 inline operator const PSID () { return psid; }
103 inline bool is_well_known_sid () { return well_known_sid; }
105 /* Both, = and *= are assignment operators. = creates a "normal" SID,
106 *= marks the SID as being a well-known SID. This difference is
107 important when creating a SID list for LSA authentication. */
108 inline const PSID operator= (cygsid &nsid)
109 { return assign (nsid, nsid.well_known_sid); }
110 inline const PSID operator= (const PSID nsid)
111 { return assign (nsid, false); }
112 inline const PSID operator= (const char *nsidstr)
113 { return getfromstr (nsidstr, false); }
114 inline const PSID operator*= (cygsid &nsid)
115 { return assign (nsid, true); }
116 inline const PSID operator*= (const PSID nsid)
117 { return assign (nsid, true); }
118 inline const PSID operator*= (const char *nsidstr)
119 { return getfromstr (nsidstr, true); }
121 inline cygsid () : cygpsid ((PSID) sbuf), well_known_sid (false) {}
122 inline cygsid (const PSID nsid) { *this = nsid; }
123 inline cygsid (const char *nstrsid) { *this = nstrsid; }
125 inline PSID set () { return psid = (PSID) sbuf; }
127 BOOL getfrompw (const struct passwd *pw);
128 BOOL getfromgr (const struct __group32 *gr);
130 void debug_print (const char *prefix = NULL) const
132 char buf[256] __attribute__ ((unused));
133 debug_printf ("%s %s%s", prefix ?: "", string (buf) ?: "NULL", well_known_sid ? " (*)" : " (+)");
137 typedef enum { cygsidlist_empty, cygsidlist_alloc, cygsidlist_auto } cygsidlist_type;
142 BOOL add (const PSID nsi, bool well_known); /* Only with auto for now */
146 cygsidlist_type type;
148 cygsidlist (cygsidlist_type t, int m)
149 : maxcnt (m), cnt (0), type (t)
151 if (t == cygsidlist_alloc)
152 sids = alloc_sids (m);
154 sids = new cygsid [m];
156 ~cygsidlist () { if (type == cygsidlist_auto) delete [] sids; }
158 BOOL addfromgr (struct __group32 *gr) /* Only with alloc */
159 { return sids[cnt].getfromgr (gr) && ++cnt; }
161 /* += adds a "normal" SID, *= adds a well-known SID. See comment in class
163 BOOL operator+= (cygsid &si) { return add ((PSID) si, false); }
164 BOOL operator+= (const char *sidstr) { cygsid nsi (sidstr);
165 return add ((PSID) nsi, false); }
166 BOOL operator+= (const PSID psid) { return add (psid, false); }
167 BOOL operator*= (cygsid &si) { return add ((PSID) si, true); }
168 BOOL operator*= (const char *sidstr) { cygsid nsi (sidstr);
169 return add ((PSID) nsi, true); }
170 BOOL operator*= (const PSID psid) { return add (psid, true); }
172 void count (int ncnt)
174 int count () const { return cnt; }
175 int non_well_known_count () const
178 for (int i = 0; i < cnt; ++i)
179 if (!sids[i].is_well_known_sid ())
184 int position (const PSID sid) const
186 for (int i = 0; i < cnt; ++i)
192 int next_non_well_known_sid (int idx)
195 if (!sids[idx].is_well_known_sid ())
199 BOOL contains (const PSID sid) const { return position (sid) >= 0; }
200 cygsid *alloc_sids (int n);
202 void debug_print (const char *prefix = NULL) const
204 debug_printf ("-- begin sidlist ---");
206 debug_printf ("No elements");
207 for (int i = 0; i < cnt; ++i)
208 sids[i].debug_print (prefix);
209 debug_printf ("-- ende sidlist ---");
213 /* Wrapper class to allow simple deleting of buffer space allocated
215 class security_descriptor {
217 PSECURITY_DESCRIPTOR psd;
219 enum { local_alloced, malloced } type;
221 security_descriptor () : psd (NULL), sd_size (0), type (local_alloced) {}
222 ~security_descriptor () { free (); }
224 PSECURITY_DESCRIPTOR malloc (size_t nsize);
225 PSECURITY_DESCRIPTOR realloc (size_t nsize);
228 inline DWORD size () {
229 if (!sd_size && psd && type == local_alloced)
230 sd_size = LocalSize (psd);
233 inline DWORD copy (void *buf, DWORD buf_size) {
234 if (buf_size < size ())
236 memcpy (buf, psd, sd_size);
239 inline operator const PSECURITY_DESCRIPTOR () { return psd; }
240 inline operator PSECURITY_DESCRIPTOR *() { return &psd; }
249 BOOL issetgroups () const { return (sgsids.type == cygsidlist_alloc); }
250 void update_supp (const cygsidlist &newsids)
264 void update_pgrp (const PSID sid)
271 extern cygpsid well_known_null_sid;
272 extern cygpsid well_known_world_sid;
273 extern cygpsid well_known_local_sid;
274 extern cygpsid well_known_creator_owner_sid;
275 extern cygpsid well_known_creator_group_sid;
276 extern cygpsid well_known_dialup_sid;
277 extern cygpsid well_known_network_sid;
278 extern cygpsid well_known_batch_sid;
279 extern cygpsid well_known_interactive_sid;
280 extern cygpsid well_known_service_sid;
281 extern cygpsid well_known_authenticated_users_sid;
282 extern cygpsid well_known_this_org_sid;
283 extern cygpsid well_known_system_sid;
284 extern cygpsid well_known_admins_sid;
285 extern cygpsid fake_logon_sid;
287 /* Order must be same as cygpriv in sec_helper.cc. */
289 SE_CREATE_TOKEN_PRIV = 0,
290 SE_ASSIGNPRIMARYTOKEN_PRIV,
292 SE_INCREASE_QUOTA_PRIV,
293 SE_UNSOLICITED_INPUT_PRIV,
294 SE_MACHINE_ACCOUNT_PRIV,
297 SE_TAKE_OWNERSHIP_PRIV,
299 SE_SYSTEM_PROFILE_PRIV,
301 SE_PROF_SINGLE_PROCESS_PRIV,
302 SE_INC_BASE_PRIORITY_PRIV,
303 SE_CREATE_PAGEFILE_PRIV,
304 SE_CREATE_PERMANENT_PRIV,
310 SE_SYSTEM_ENVIRONMENT_PRIV,
311 SE_CHANGE_NOTIFY_PRIV,
312 SE_REMOTE_SHUTDOWN_PRIV,
313 SE_CREATE_GLOBAL_PRIV,
315 SE_MANAGE_VOLUME_PRIV,
317 SE_ENABLE_DELEGATION_PRIV,
320 SE_INCREASE_WORKING_SET_PRIV,
322 SE_CREATE_SYMBOLIC_LINK_PRIV,
327 const LUID *privilege_luid (enum cygpriv_idx idx);
328 const LUID *privilege_luid_by_name (const char *pname);
329 const char *privilege_name (enum cygpriv_idx idx);
332 legal_sid_type (SID_NAME_USE type)
334 return type == SidTypeUser || type == SidTypeGroup
335 || type == SidTypeAlias || type == SidTypeWellKnownGroup;
338 extern bool allow_ntea;
339 extern bool allow_ntsec;
340 extern bool allow_smbntsec;
341 extern bool allow_traverse;
343 /* File manipulation */
344 int __stdcall get_file_attribute (int, HANDLE, const char *, mode_t *,
345 __uid32_t * = NULL, __gid32_t * = NULL);
346 int __stdcall set_file_attribute (bool, HANDLE, const char *, int);
347 int __stdcall set_file_attribute (bool, HANDLE, const char *, __uid32_t, __gid32_t, int);
348 int __stdcall get_nt_object_security (HANDLE, SE_OBJECT_TYPE,
349 security_descriptor &);
350 int __stdcall get_object_attribute (HANDLE handle, SE_OBJECT_TYPE object_type, mode_t *,
351 __uid32_t * = NULL, __gid32_t * = NULL);
352 LONG __stdcall read_sd (const char *file, security_descriptor &sd);
353 LONG __stdcall write_sd (HANDLE fh, const char *file, security_descriptor &sd);
354 bool __stdcall add_access_allowed_ace (PACL acl, int offset, DWORD attributes, PSID sid, size_t &len_add, DWORD inherit);
355 bool __stdcall add_access_denied_ace (PACL acl, int offset, DWORD attributes, PSID sid, size_t &len_add, DWORD inherit);
356 int __stdcall check_file_access (const char *, int);
357 int __stdcall check_registry_access (HANDLE, int);
359 void set_security_attribute (int attribute, PSECURITY_ATTRIBUTES psa,
360 security_descriptor &sd_buf);
362 bool get_sids_info (cygpsid, cygpsid, __uid32_t * , __gid32_t *);
366 extern "C" int aclsort32 (int, int, __acl32 *);
367 extern "C" int acl32 (const char *, int, int, __acl32 *);
368 int getacl (HANDLE, const char *, DWORD, int, __acl32 *);
369 int setacl (HANDLE, const char *, int, __acl32 *);
371 struct _UNICODE_STRING;
372 void __stdcall str2buf2uni (_UNICODE_STRING &, WCHAR *, const char *) __attribute__ ((regparm (3)));
373 void __stdcall str2uni_cat (_UNICODE_STRING &, const char *) __attribute__ ((regparm (2)));
375 /* Function creating a token by calling NtCreateToken. */
376 HANDLE create_token (cygsid &usersid, user_groups &groups, struct passwd * pw,
377 HANDLE subauth_token);
379 /* Subauthentication function. */
380 HANDLE subauth (struct passwd *pw);
382 /* LSA authentication function. */
383 HANDLE lsaauth (cygsid &, user_groups &, struct passwd *);
384 /* Verify an existing token */
385 bool verify_token (HANDLE token, cygsid &usersid, user_groups &groups, bool *pintern = NULL);
386 /* Get groups of a user */
387 bool get_server_groups (cygsidlist &grp_list, PSID usersid, struct passwd *pw);
389 /* Extract U-domain\user field from passwd entry. */
390 void extract_nt_dom_user (const struct passwd *pw, char *domain, char *user);
391 /* Get default logonserver for a domain. */
392 bool get_logon_server (const char * domain, char * server, WCHAR *wserver,
395 /* sec_helper.cc: Security helper functions. */
396 int set_privilege (HANDLE token, enum cygpriv_idx privilege, bool enable);
397 void set_cygwin_privileges (HANDLE token);
399 #define set_process_privilege(p,v) set_privilege (hProcImpToken, (p), (v))
401 #define _push_thread_privilege(_priv, _val, _check) { \
402 HANDLE _token = NULL, _dup_token = NULL; \
403 if (wincap.has_security ()) \
405 _token = (cygheap->user.issetuid () && (_check)) \
406 ? cygheap->user.token () : hProcImpToken; \
407 if (!DuplicateTokenEx (_token, MAXIMUM_ALLOWED, NULL, \
408 SecurityImpersonation, TokenImpersonation, \
410 debug_printf ("DuplicateTokenEx: %E"); \
411 else if (!ImpersonateLoggedOnUser (_dup_token)) \
412 debug_printf ("ImpersonateLoggedOnUser: %E"); \
414 set_privilege (_dup_token, (_priv), (_val)); \
416 #define push_thread_privilege(_priv, _val) _push_thread_privilege(_priv,_val,1)
417 #define push_self_privilege(_priv, _val) _push_thread_privilege(_priv,_val,0)
419 #define pop_thread_privilege() \
422 ImpersonateLoggedOnUser (_token); \
423 CloseHandle (_dup_token); \
426 #define pop_self_privilege() pop_thread_privilege()
429 /* Retrieve a security descriptor that allows all access */
430 SECURITY_DESCRIPTOR *__stdcall get_null_sd ();
432 /* Various types of security attributes for use in Create* functions. */
433 extern SECURITY_ATTRIBUTES sec_none, sec_none_nih, sec_all, sec_all_nih;
434 extern SECURITY_ATTRIBUTES *__stdcall __sec_user (PVOID sa_buf, PSID sid1, PSID sid2,
435 DWORD access2, BOOL inherit)
436 __attribute__ ((regparm (3)));
437 extern bool sec_acl (PACL acl, bool original, bool admins, PSID sid1 = NO_SID,
438 PSID sid2 = NO_SID, DWORD access2 = 0);
440 int __stdcall read_ea (HANDLE hdl, const char *file, const char *attrname,
442 BOOL __stdcall write_ea (HANDLE hdl, const char *file, const char *attrname,
443 const char *buf, int len);
445 /* Note: sid1 is usually (read: currently always) the current user's
446 effective sid (cygheap->user.sid ()). */
447 extern inline SECURITY_ATTRIBUTES *
448 sec_user_nih (SECURITY_ATTRIBUTES *sa_buf, PSID sid1, PSID sid2 = NULL,
451 return __sec_user (sa_buf, sid1, sid2, access2, FALSE);
454 extern inline SECURITY_ATTRIBUTES *
455 sec_user (SECURITY_ATTRIBUTES *sa_buf, PSID sid1, PSID sid2 = NULL,
458 return __sec_user (sa_buf, sid1, sid2, access2, TRUE);
460 #endif /*_SECURITY_H*/