1 /* security.h: security declarations
3 Copyright 2000, 2001, 2002, 2003, 2004, 2005 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 /* Macro to define variable length SID structures */
27 #define SID(name, comment, authority, count, rid...) \
28 static NO_COPY struct { \
30 BYTE SubAuthorityCount; \
31 SID_IDENTIFIER_AUTHORITY IdentifierAuthority; \
32 DWORD SubAuthority[count]; \
33 } name##_struct = { SID_REVISION, count, {authority}, {rid}}; \
34 cygpsid NO_COPY name = (PSID) &name##_struct;
36 #define FILE_READ_BITS (FILE_READ_DATA | GENERIC_READ | GENERIC_ALL)
37 #define FILE_WRITE_BITS (FILE_WRITE_DATA | GENERIC_WRITE | GENERIC_ALL)
38 #define FILE_EXEC_BITS (FILE_EXECUTE | GENERIC_EXECUTE | GENERIC_ALL)
45 cygpsid (PSID nsid) { psid = nsid; }
46 operator const PSID () { return psid; }
47 const PSID operator= (PSID nsid) { return psid = nsid;}
48 __uid32_t get_id (BOOL search_grp, int *type = NULL);
49 int get_uid () { return get_id (FALSE); }
50 int get_gid () { return get_id (TRUE); }
52 char *string (char *nsidstr) const;
54 bool operator== (const PSID nsid) const
58 return EqualSid (psid, nsid);
60 bool operator!= (const PSID nsid) const
61 { return !(*this == nsid); }
62 bool operator== (const char *nsidstr) const;
63 bool operator!= (const char *nsidstr) const
64 { return !(*this == nsidstr); }
66 void debug_print (const char *prefix = NULL) const
68 char buf[256] __attribute__ ((unused));
69 debug_printf ("%s %s", prefix ?: "", string (buf) ?: "NULL");
73 class cygsid : public cygpsid {
74 char sbuf[MAX_SID_LEN];
76 const PSID getfromstr (const char *nsidstr);
77 PSID get_sid (DWORD s, DWORD cnt, DWORD *r);
79 inline const PSID assign (const PSID nsid)
86 CopySid (MAX_SID_LEN, psid, nsid);
92 inline operator const PSID () { return psid; }
94 inline const PSID operator= (cygsid &nsid)
95 { return assign (nsid); }
96 inline const PSID operator= (const PSID nsid)
97 { return assign (nsid); }
98 inline const PSID operator= (const char *nsidstr)
99 { return getfromstr (nsidstr); }
101 inline cygsid () : cygpsid ((PSID) sbuf) {}
102 inline cygsid (const PSID nsid) { *this = nsid; }
103 inline cygsid (const char *nstrsid) { *this = nstrsid; }
105 inline PSID set () { return psid = (PSID) sbuf; }
107 BOOL getfrompw (const struct passwd *pw);
108 BOOL getfromgr (const struct __group32 *gr);
111 typedef enum { cygsidlist_empty, cygsidlist_alloc, cygsidlist_auto } cygsidlist_type;
117 cygsidlist_type type;
119 cygsidlist (cygsidlist_type t, int m)
124 if (t == cygsidlist_alloc)
125 sids = alloc_sids (m);
127 sids = new cygsid [m];
129 ~cygsidlist () { if (type == cygsidlist_auto) delete [] sids; }
131 BOOL add (const PSID nsi) /* Only with auto for now */
133 if (count >= maxcount)
135 cygsid *tmp = new cygsid [ 2 * maxcount];
139 for (int i = 0; i < count; ++i)
147 BOOL add (cygsid &nsi) { return add ((PSID) nsi); }
148 BOOL add (const char *sidstr)
149 { cygsid nsi (sidstr); return add (nsi); }
150 BOOL addfromgr (struct __group32 *gr) /* Only with alloc */
151 { return sids[count].getfromgr (gr) && ++count; }
153 BOOL operator+= (cygsid &si) { return add (si); }
154 BOOL operator+= (const char *sidstr) { return add (sidstr); }
155 BOOL operator+= (const PSID psid) { return add (psid); }
157 int position (const PSID sid) const
159 for (int i = 0; i < count; ++i)
165 BOOL contains (const PSID sid) const { return position (sid) >= 0; }
166 cygsid *alloc_sids (int n);
168 void debug_print (const char *prefix = NULL) const
170 debug_printf ("-- begin sidlist ---");
172 debug_printf ("No elements");
173 for (int i = 0; i < count; ++i)
174 sids[i].debug_print (prefix);
175 debug_printf ("-- ende sidlist ---");
179 /* Wrapper class to allow simple deleting of buffer space allocated
181 class security_descriptor {
183 PSECURITY_DESCRIPTOR psd;
186 security_descriptor () : psd (NULL), sd_size (0) {}
187 ~security_descriptor () { free (); }
189 PSECURITY_DESCRIPTOR malloc (size_t nsize);
190 PSECURITY_DESCRIPTOR realloc (size_t nsize);
193 inline DWORD size (void) const { return sd_size; }
194 inline operator const PSECURITY_DESCRIPTOR () { return psd; }
203 BOOL issetgroups () const { return (sgsids.type == cygsidlist_alloc); }
204 void update_supp (const cygsidlist &newsids)
218 void update_pgrp (const PSID sid)
225 extern cygpsid well_known_null_sid;
226 extern cygpsid well_known_world_sid;
227 extern cygpsid well_known_local_sid;
228 extern cygpsid well_known_creator_owner_sid;
229 extern cygpsid well_known_creator_group_sid;
230 extern cygpsid well_known_dialup_sid;
231 extern cygpsid well_known_network_sid;
232 extern cygpsid well_known_batch_sid;
233 extern cygpsid well_known_interactive_sid;
234 extern cygpsid well_known_service_sid;
235 extern cygpsid well_known_authenticated_users_sid;
236 extern cygpsid well_known_system_sid;
237 extern cygpsid well_known_admins_sid;
239 /* Order must be same as cygpriv in sec_helper.cc. */
241 SE_CREATE_TOKEN_PRIV = 0,
242 SE_ASSIGNPRIMARYTOKEN_PRIV,
244 SE_INCREASE_QUOTA_PRIV,
245 SE_UNSOLICITED_INPUT_PRIV,
246 SE_MACHINE_ACCOUNT_PRIV,
249 SE_TAKE_OWNERSHIP_PRIV,
251 SE_SYSTEM_PROFILE_PRIV,
253 SE_PROF_SINGLE_PROCESS_PRIV,
254 SE_INC_BASE_PRIORITY_PRIV,
255 SE_CREATE_PAGEFILE_PRIV,
256 SE_CREATE_PERMANENT_PRIV,
262 SE_SYSTEM_ENVIRONMENT_PRIV,
263 SE_CHANGE_NOTIFY_PRIV,
264 SE_REMOTE_SHUTDOWN_PRIV,
265 SE_CREATE_GLOBAL_PRIV,
267 SE_MANAGE_VOLUME_PRIV,
269 SE_ENABLE_DELEGATION_PRIV,
275 const LUID *privilege_luid (enum cygpriv_idx idx);
276 const LUID *privilege_luid_by_name (const char *pname);
277 const char *privilege_name (enum cygpriv_idx idx);
280 legal_sid_type (SID_NAME_USE type)
282 return type == SidTypeUser || type == SidTypeGroup
283 || type == SidTypeAlias || type == SidTypeWellKnownGroup;
286 extern bool allow_ntea;
287 extern bool allow_ntsec;
288 extern bool allow_smbntsec;
289 extern bool allow_traverse;
291 /* File manipulation */
292 int __stdcall get_file_attribute (int, HANDLE, const char *, mode_t *,
293 __uid32_t * = NULL, __gid32_t * = NULL);
294 int __stdcall set_file_attribute (bool, HANDLE, const char *, int);
295 int __stdcall set_file_attribute (bool, HANDLE, const char *, __uid32_t, __gid32_t, int);
296 int __stdcall get_nt_object_security (HANDLE, SE_OBJECT_TYPE,
297 security_descriptor &);
298 int __stdcall get_object_attribute (HANDLE handle, SE_OBJECT_TYPE object_type, mode_t *,
299 __uid32_t * = NULL, __gid32_t * = NULL);
300 LONG __stdcall read_sd (const char *file, security_descriptor &sd);
301 LONG __stdcall write_sd (HANDLE fh, const char *file, security_descriptor &sd);
302 bool __stdcall add_access_allowed_ace (PACL acl, int offset, DWORD attributes, PSID sid, size_t &len_add, DWORD inherit);
303 bool __stdcall add_access_denied_ace (PACL acl, int offset, DWORD attributes, PSID sid, size_t &len_add, DWORD inherit);
304 int __stdcall check_file_access (const char *, int);
306 void set_security_attribute (int attribute, PSECURITY_ATTRIBUTES psa,
307 security_descriptor &sd_buf);
309 bool get_sids_info (cygpsid, cygpsid, __uid32_t * , __gid32_t *);
313 extern "C" int aclsort32 (int, int, __acl32 *);
314 extern "C" int acl32 (const char *, int, int, __acl32 *);
315 int getacl (HANDLE, const char *, DWORD, int, __acl32 *);
316 int setacl (HANDLE, const char *, int, __acl32 *);
318 struct _UNICODE_STRING;
319 void __stdcall str2buf2uni (_UNICODE_STRING &, WCHAR *, const char *) __attribute__ ((regparm (3)));
320 void __stdcall str2uni_cat (_UNICODE_STRING &, const char *) __attribute__ ((regparm (2)));
322 /* Try a subauthentication. */
323 HANDLE subauth (struct passwd *pw);
324 /* Try creating a token directly. */
325 HANDLE create_token (cygsid &usersid, user_groups &groups, struct passwd * pw);
326 /* Verify an existing token */
327 bool verify_token (HANDLE token, cygsid &usersid, user_groups &groups, bool *pintern = NULL);
328 /* Get groups of a user */
329 bool get_server_groups (cygsidlist &grp_list, PSID usersid, struct passwd *pw);
331 /* Extract U-domain\user field from passwd entry. */
332 void extract_nt_dom_user (const struct passwd *pw, char *domain, char *user);
333 /* Get default logonserver for a domain. */
334 bool get_logon_server (const char * domain, char * server, WCHAR *wserver = NULL);
336 /* sec_helper.cc: Security helper functions. */
337 int set_privilege (HANDLE token, enum cygpriv_idx privilege, bool enable);
338 void set_cygwin_privileges (HANDLE token);
340 #define set_process_privilege(p,v) set_privilege (hProcImpToken, (p), (v))
342 #define _push_thread_privilege(_priv, _val, _check) { \
343 HANDLE _token = NULL, _dup_token = NULL; \
344 if (wincap.has_security ()) \
346 _token = (cygheap->user.issetuid () && (_check)) \
347 ? cygheap->user.token () : hProcImpToken; \
348 if (!DuplicateTokenEx (_token, MAXIMUM_ALLOWED, NULL, \
349 SecurityImpersonation, TokenImpersonation, \
351 debug_printf ("DuplicateTokenEx: %E"); \
352 else if (!ImpersonateLoggedOnUser (_dup_token)) \
353 debug_printf ("ImpersonateLoggedOnUser: %E"); \
355 set_privilege (_dup_token, (_priv), (_val)); \
357 #define push_thread_privilege(_priv, _val) _push_thread_privilege(_priv,_val,1)
358 #define push_self_privilege(_priv, _val) _push_thread_privilege(_priv,_val,0)
360 #define pop_thread_privilege() \
363 ImpersonateLoggedOnUser (_token); \
364 CloseHandle (_dup_token); \
367 #define pop_self_privilege() pop_thread_privilege()
370 /* Retrieve a security descriptor that allows all access */
371 SECURITY_DESCRIPTOR *__stdcall get_null_sd (void);
373 /* Various types of security attributes for use in Create* functions. */
374 extern SECURITY_ATTRIBUTES sec_none, sec_none_nih, sec_all, sec_all_nih;
375 extern SECURITY_ATTRIBUTES *__stdcall __sec_user (PVOID sa_buf, PSID sid1, PSID sid2,
376 DWORD access2, BOOL inherit)
377 __attribute__ ((regparm (3)));
378 extern bool sec_acl (PACL acl, bool original, bool admins, PSID sid1 = NO_SID,
379 PSID sid2 = NO_SID, DWORD access2 = 0);
381 int __stdcall NTReadEA (const char *file, const char *attrname, char *buf, int len);
382 BOOL __stdcall NTWriteEA (const char *file, const char *attrname, const char *buf, int len);
384 extern inline SECURITY_ATTRIBUTES *
385 sec_user_nih (char sa_buf[], PSID sid1 = NULL, PSID sid2 = NULL, DWORD access2 = 0)
387 return __sec_user (sa_buf, sid1, sid2, access2, FALSE);
390 extern inline SECURITY_ATTRIBUTES *
391 sec_user (char sa_buf[], PSID sid1 = NULL, PSID sid2 = NULL, DWORD access2 = 0)
393 return __sec_user (sa_buf, sid1, sid2, access2, TRUE);
395 #endif /*_SECURITY_H*/