OSDN Git Service

* sec_auth.cc (str2uni_cat): Move from here...
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / security.h
1 /* security.h: security declarations
2
3    Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4    Red Hat, Inc.
5
6 This file is part of Cygwin.
7
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
10 details. */
11
12 #ifndef _SECURITY_H
13 #define _SECURITY_H
14
15 #include <accctrl.h>
16
17 #define DEFAULT_UID DOMAIN_USER_RID_ADMIN
18 #define UNKNOWN_UID 400 /* Non conflicting number */
19 #define UNKNOWN_GID 401
20
21 #define MAX_SID_LEN 40
22 #define MAX_DACL_LEN(n) (sizeof (ACL) \
23                    + (n) * (sizeof (ACCESS_ALLOWED_ACE) - sizeof (DWORD) + MAX_SID_LEN))
24 #define SD_MIN_SIZE (sizeof (SECURITY_DESCRIPTOR) + MAX_DACL_LEN (1))
25 #define ACL_DEFAULT_SIZE 3072
26 #define NO_SID ((PSID)NULL)
27
28 #ifndef SE_CREATE_TOKEN_PRIVILEGE
29 #define SE_CREATE_TOKEN_PRIVILEGE            2UL
30 #define SE_ASSIGNPRIMARYTOKEN_PRIVILEGE      3UL
31 #define SE_LOCK_MEMORY_PRIVILEGE             4UL
32 #define SE_INCREASE_QUOTA_PRIVILEGE          5UL
33 #define SE_MACHINE_ACCOUNT_PRIVILEGE         6UL
34 #define SE_TCB_PRIVILEGE                     7UL
35 #define SE_SECURITY_PRIVILEGE                8UL
36 #define SE_TAKE_OWNERSHIP_PRIVILEGE          9UL
37 #define SE_LOAD_DRIVER_PRIVILEGE            10UL
38 #define SE_SYSTEM_PROFILE_PRIVILEGE         11UL
39 #define SE_SYSTEMTIME_PRIVILEGE             12UL
40 #define SE_PROF_SINGLE_PROCESS_PRIVILEGE    13UL
41 #define SE_INC_BASE_PRIORITY_PRIVILEGE      14UL
42 #define SE_CREATE_PAGEFILE_PRIVILEGE        15UL
43 #define SE_CREATE_PERMANENT_PRIVILEGE       16UL
44 #define SE_BACKUP_PRIVILEGE                 17UL
45 #define SE_RESTORE_PRIVILEGE                18UL
46 #define SE_SHUTDOWN_PRIVILEGE               19UL
47 #define SE_DEBUG_PRIVILEGE                  20UL
48 #define SE_AUDIT_PRIVILEGE                  21UL
49 #define SE_SYSTEM_ENVIRONMENT_PRIVILEGE     22UL
50 #define SE_CHANGE_NOTIFY_PRIVILEGE          23UL
51 #define SE_REMOTE_SHUTDOWN_PRIVILEGE        24UL
52 /* Starting with Windows 2000 */
53 #define SE_UNDOCK_PRIVILEGE                 25UL
54 #define SE_SYNC_AGENT_PRIVILEGE             26UL
55 #define SE_ENABLE_DELEGATION_PRIVILEGE      27UL
56 #define SE_MANAGE_VOLUME_PRIVILEGE          28UL
57 /* Starting with Windows 2000 SP4, XP SP2, 2003 Server */
58 #define SE_IMPERSONATE_PRIVILEGE            29UL
59 #define SE_CREATE_GLOBAL_PRIVILEGE          30UL
60 /* Starting with Vista */
61 #define SE_TRUSTED_CREDMAN_ACCESS_PRIVILEGE 31UL
62 #define SE_RELABEL_PRIVILEGE                32UL
63 #define SE_INCREASE_WORKING_SET_PRIVILEGE   33UL
64 #define SE_TIME_ZONE_PRIVILEGE              34UL
65 #define SE_CREATE_SYMBOLIC_LINK_PRIVILEGE   35UL
66
67 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_SYMBOLIC_LINK_PRIVILEGE
68
69 #endif /* ! SE_CREATE_TOKEN_PRIVILEGE */
70
71 /* Added for debugging purposes. */
72 typedef struct {
73   BYTE  Revision;
74   BYTE  SubAuthorityCount;
75   SID_IDENTIFIER_AUTHORITY IdentifierAuthority;
76   DWORD SubAuthority[8];
77 } DBGSID, *PDBGSID;
78
79 /* Macro to define variable length SID structures */
80 #define MKSID(name, comment, authority, count, rid...) \
81 static NO_COPY struct  { \
82   BYTE  Revision; \
83   BYTE  SubAuthorityCount; \
84   SID_IDENTIFIER_AUTHORITY IdentifierAuthority; \
85   DWORD SubAuthority[count]; \
86 } name##_struct = { SID_REVISION, count, {authority}, {rid}}; \
87 cygpsid NO_COPY name = (PSID) &name##_struct;
88
89 #define FILE_READ_BITS   (FILE_READ_DATA | GENERIC_READ | GENERIC_ALL)
90 #define FILE_WRITE_BITS  (FILE_WRITE_DATA | GENERIC_WRITE | GENERIC_ALL)
91 #define FILE_EXEC_BITS   (FILE_EXECUTE | GENERIC_EXECUTE | GENERIC_ALL)
92
93 class cygpsid {
94 protected:
95   PSID psid;
96 public:
97   cygpsid () {}
98   cygpsid (PSID nsid) { psid = nsid; }
99   operator PSID () const { return psid; }
100   const PSID operator= (PSID nsid) { return psid = nsid;}
101   __uid32_t get_id (BOOL search_grp, int *type = NULL);
102   int get_uid () { return get_id (FALSE); }
103   int get_gid () { return get_id (TRUE); }
104
105   PWCHAR string (PWCHAR nsidstr) const;
106   char *string (char *nsidstr) const;
107
108   bool operator== (const PSID nsid) const
109     {
110       if (!psid || !nsid)
111         return nsid == psid;
112       return EqualSid (psid, nsid);
113     }
114   bool operator!= (const PSID nsid) const
115     { return !(*this == nsid); }
116   bool operator== (const char *nsidstr) const;
117   bool operator!= (const char *nsidstr) const
118     { return !(*this == nsidstr); }
119
120   void debug_print (const char *prefix = NULL) const
121     {
122       char buf[256] __attribute__ ((unused));
123       debug_printf ("%s %s", prefix ?: "", string (buf) ?: "NULL");
124     }
125 };
126
127 class cygsid : public cygpsid {
128   char sbuf[MAX_SID_LEN];
129   bool well_known_sid;
130
131   const PSID getfromstr (const char *nsidstr, bool well_known);
132   PSID get_sid (DWORD s, DWORD cnt, DWORD *r, bool well_known);
133
134   inline const PSID assign (const PSID nsid, bool well_known)
135     {
136       if (!nsid)
137         psid = NO_SID;
138       else
139         {
140           psid = (PSID) sbuf;
141           CopySid (MAX_SID_LEN, psid, nsid);
142           well_known_sid = well_known;
143         }
144       return psid;
145     }
146
147 public:
148   inline operator const PSID () { return psid; }
149   inline bool is_well_known_sid () { return well_known_sid; }
150
151   /* Both, = and *= are assignment operators.  = creates a "normal" SID,
152      *= marks the SID as being a well-known SID.  This difference is
153      important when creating a SID list for LSA authentication. */
154   inline const PSID operator= (cygsid &nsid)
155     { return assign (nsid, nsid.well_known_sid); }
156   inline const PSID operator= (const PSID nsid)
157     { return assign (nsid, false); }
158   inline const PSID operator= (const char *nsidstr)
159     { return getfromstr (nsidstr, false); }
160   inline const PSID operator*= (cygsid &nsid)
161     { return assign (nsid, true); }
162   inline const PSID operator*= (const PSID nsid)
163     { return assign (nsid, true); }
164   inline const PSID operator*= (const char *nsidstr)
165     { return getfromstr (nsidstr, true); }
166
167   inline cygsid () : cygpsid ((PSID) sbuf), well_known_sid (false) {}
168   inline cygsid (const PSID nsid) { *this = nsid; }
169   inline cygsid (const char *nstrsid) { *this = nstrsid; }
170
171   inline PSID set () { return psid = (PSID) sbuf; }
172
173   BOOL getfrompw (const struct passwd *pw);
174   BOOL getfromgr (const struct __group32 *gr);
175
176   void debug_print (const char *prefix = NULL) const
177     {
178       char buf[256] __attribute__ ((unused));
179       debug_printf ("%s %s%s", prefix ?: "", string (buf) ?: "NULL", well_known_sid ? " (*)" : " (+)");
180     }
181 };
182
183 typedef enum { cygsidlist_empty, cygsidlist_alloc, cygsidlist_auto } cygsidlist_type;
184 class cygsidlist {
185   int maxcnt;
186   int cnt;
187
188   BOOL add (const PSID nsi, bool well_known); /* Only with auto for now */
189
190 public:
191   cygsid *sids;
192   cygsidlist_type type;
193
194   cygsidlist (cygsidlist_type t, int m)
195   : maxcnt (m), cnt (0), type (t)
196     {
197       if (t == cygsidlist_alloc)
198         sids = alloc_sids (m);
199       else
200         sids = new cygsid [m];
201     }
202   ~cygsidlist () { if (type == cygsidlist_auto) delete [] sids; }
203
204   BOOL addfromgr (struct __group32 *gr) /* Only with alloc */
205     { return sids[cnt].getfromgr (gr) && ++cnt; }
206
207   /* += adds a "normal" SID, *= adds a well-known SID.  See comment in class
208      cygsid above. */
209   BOOL operator+= (cygsid &si) { return add ((PSID) si, false); }
210   BOOL operator+= (const char *sidstr) { cygsid nsi (sidstr);
211                                          return add ((PSID) nsi, false); }
212   BOOL operator+= (const PSID psid) { return add (psid, false); }
213   BOOL operator*= (cygsid &si) { return add ((PSID) si, true); }
214   BOOL operator*= (const char *sidstr) { cygsid nsi (sidstr);
215                                          return add ((PSID) nsi, true); }
216   BOOL operator*= (const PSID psid) { return add (psid, true); }
217
218   void count (int ncnt)
219     { cnt = ncnt; }
220   int count () const { return cnt; }
221   int non_well_known_count () const
222     {
223       int wcnt = 0;
224       for (int i = 0; i < cnt; ++i)
225         if (!sids[i].is_well_known_sid ())
226           ++wcnt;
227       return wcnt;
228     }
229
230   int position (const PSID sid) const
231     {
232       for (int i = 0; i < cnt; ++i)
233         if (sids[i] == sid)
234           return i;
235       return -1;
236     }
237
238   int next_non_well_known_sid (int idx)
239     {
240       while (++idx < cnt)
241         if (!sids[idx].is_well_known_sid ())
242           return idx;
243       return -1;
244     }
245   BOOL contains (const PSID sid) const { return position (sid) >= 0; }
246   cygsid *alloc_sids (int n);
247   void free_sids ();
248   void debug_print (const char *prefix = NULL) const
249     {
250       debug_printf ("-- begin sidlist ---");
251       if (!cnt)
252         debug_printf ("No elements");
253       for (int i = 0; i < cnt; ++i)
254         sids[i].debug_print (prefix);
255       debug_printf ("-- ende sidlist ---");
256     }
257 };
258
259 /* Wrapper class to allow simple deleting of buffer space allocated
260    by read_sd() */
261 class security_descriptor {
262 protected:
263   PSECURITY_DESCRIPTOR psd;
264   DWORD sd_size;
265 public:
266   security_descriptor () : psd (NULL), sd_size (0) {}
267   ~security_descriptor () { free (); }
268
269   PSECURITY_DESCRIPTOR malloc (size_t nsize);
270   PSECURITY_DESCRIPTOR realloc (size_t nsize);
271   void free ();
272
273   inline DWORD size () const { return sd_size; }
274   inline DWORD copy (void *buf, DWORD buf_size) const {
275     if (buf_size < size ())
276       return sd_size;
277     memcpy (buf, psd, sd_size);
278     return 0;
279   }
280   inline operator const PSECURITY_DESCRIPTOR () { return psd; }
281   inline operator PSECURITY_DESCRIPTOR *() { return &psd; }
282 };
283
284 class user_groups {
285 public:
286   cygsid pgsid;
287   cygsidlist sgsids;
288   BOOL ischanged;
289
290   BOOL issetgroups () const { return (sgsids.type == cygsidlist_alloc); }
291   void update_supp (const cygsidlist &newsids)
292     {
293       sgsids.free_sids ();
294       sgsids = newsids;
295       ischanged = TRUE;
296     }
297   void clear_supp ()
298     {
299       if (issetgroups ())
300         {
301           sgsids.free_sids ();
302           ischanged = TRUE;
303         }
304     }
305   void update_pgrp (const PSID sid)
306     {
307       pgsid = sid;
308       ischanged = TRUE;
309     }
310 };
311
312 extern cygpsid well_known_null_sid;
313 extern cygpsid well_known_world_sid;
314 extern cygpsid well_known_local_sid;
315 extern cygpsid well_known_creator_owner_sid;
316 extern cygpsid well_known_creator_group_sid;
317 extern cygpsid well_known_dialup_sid;
318 extern cygpsid well_known_network_sid;
319 extern cygpsid well_known_batch_sid;
320 extern cygpsid well_known_interactive_sid;
321 extern cygpsid well_known_service_sid;
322 extern cygpsid well_known_authenticated_users_sid;
323 extern cygpsid well_known_this_org_sid;
324 extern cygpsid well_known_system_sid;
325 extern cygpsid well_known_admins_sid;
326 extern cygpsid fake_logon_sid;
327 extern cygpsid mandatory_medium_integrity_sid;
328 extern cygpsid mandatory_high_integrity_sid;
329 extern cygpsid mandatory_system_integrity_sid;
330 extern cygpsid well_known_samba_unix_user_fake_sid;
331
332 bool privilege_luid (const PWCHAR pname, LUID *luid);
333
334 inline BOOL
335 legal_sid_type (SID_NAME_USE type)
336 {
337   return type == SidTypeUser  || type == SidTypeGroup
338       || type == SidTypeAlias || type == SidTypeWellKnownGroup;
339 }
340
341 class path_conv;
342 /* File manipulation */
343 int __stdcall get_file_attribute (HANDLE, path_conv &, mode_t *,
344                                   __uid32_t *, __gid32_t *);
345 int __stdcall set_file_attribute (HANDLE, path_conv &,
346                                   __uid32_t, __gid32_t, int);
347 int __stdcall get_reg_attribute (HKEY hkey, mode_t *, __uid32_t *, __gid32_t *);
348 LONG __stdcall get_file_sd (HANDLE fh, path_conv &, security_descriptor &sd);
349 LONG __stdcall set_file_sd (HANDLE fh, path_conv &, security_descriptor &sd,
350                             bool is_chown);
351 bool __stdcall add_access_allowed_ace (PACL acl, int offset, DWORD attributes, PSID sid, size_t &len_add, DWORD inherit);
352 bool __stdcall add_access_denied_ace (PACL acl, int offset, DWORD attributes, PSID sid, size_t &len_add, DWORD inherit);
353 int __stdcall check_file_access (path_conv &, int);
354 int __stdcall check_registry_access (HANDLE, int);
355
356 void set_security_attribute (path_conv &pc, int attribute,
357                              PSECURITY_ATTRIBUTES psa,
358                              security_descriptor &sd_buf);
359
360 bool get_sids_info (cygpsid, cygpsid, __uid32_t * , __gid32_t *);
361
362 /* sec_acl.cc */
363 struct __acl32;
364 extern "C" int aclsort32 (int, int, __acl32 *);
365 extern "C" int acl32 (const char *, int, int, __acl32 *);
366 int getacl (HANDLE, path_conv &, int, __acl32 *);
367 int setacl (HANDLE, path_conv &, int, __acl32 *, bool &);
368
369 /* Function creating a token by calling NtCreateToken. */
370 HANDLE create_token (cygsid &usersid, user_groups &groups, struct passwd * pw);
371 /* LSA authentication function. */
372 HANDLE lsaauth (cygsid &, user_groups &, struct passwd *);
373 /* LSA private key storage authentication, same as when using service logons. */
374 HANDLE lsaprivkeyauth (struct passwd *pw);
375 /* Verify an existing token */
376 bool verify_token (HANDLE token, cygsid &usersid, user_groups &groups, bool *pintern = NULL);
377 /* Get groups of a user */
378 bool get_server_groups (cygsidlist &grp_list, PSID usersid, struct passwd *pw);
379
380 /* Extract U-domain\user field from passwd entry. */
381 void extract_nt_dom_user (const struct passwd *pw, PWCHAR domain, PWCHAR user);
382 /* Get default logonserver for a domain. */
383 bool get_logon_server (PWCHAR domain, PWCHAR wserver, bool rediscovery);
384
385 HANDLE open_local_policy (ACCESS_MASK access);
386
387 /* sec_helper.cc: Security helper functions. */
388 int set_privilege (HANDLE token, DWORD privilege, bool enable);
389 void set_cygwin_privileges (HANDLE token);
390
391 #define _push_thread_privilege(_priv, _val, _check) { \
392     HANDLE _dup_token = NULL; \
393     HANDLE _token = (cygheap->user.issetuid () && (_check)) \
394                     ? cygheap->user.primary_token () : hProcToken; \
395     if (!DuplicateTokenEx (_token, MAXIMUM_ALLOWED, NULL, \
396                            SecurityImpersonation, TokenImpersonation, \
397                            &_dup_token)) \
398       debug_printf ("DuplicateTokenEx: %E"); \
399     else if (!ImpersonateLoggedOnUser (_dup_token)) \
400       debug_printf ("ImpersonateLoggedOnUser: %E"); \
401     else \
402       set_privilege (_dup_token, (_priv), (_val));
403
404 #define push_thread_privilege(_priv, _val) _push_thread_privilege(_priv,_val,1)
405 #define push_self_privilege(_priv, _val)   _push_thread_privilege(_priv,_val,0)
406
407 #define pop_thread_privilege() \
408     if (_dup_token) \
409       { \
410         if (!cygheap->user.issetuid ()) \
411           RevertToSelf (); \
412         else \
413           cygheap->user.reimpersonate (); \
414         CloseHandle (_dup_token); \
415       } \
416   }
417
418 #define pop_self_privilege()               pop_thread_privilege()
419
420 /* shared.cc: */
421 /* Retrieve a security descriptor that allows all access */
422 SECURITY_DESCRIPTOR *__stdcall get_null_sd ();
423
424 /* Various types of security attributes for use in Create* functions. */
425 extern SECURITY_ATTRIBUTES sec_none, sec_none_nih, sec_all, sec_all_nih;
426 extern SECURITY_ATTRIBUTES *__stdcall __sec_user (PVOID sa_buf, PSID sid1, PSID sid2,
427                                                   DWORD access2, BOOL inherit)
428   __attribute__ ((regparm (3)));
429 extern PSECURITY_DESCRIPTOR _everyone_sd (void *buf, ACCESS_MASK access);
430 #define everyone_sd(access)     (_everyone_sd (alloca (SD_MIN_SIZE), (access)))
431
432 extern bool sec_acl (PACL acl, bool original, bool admins, PSID sid1 = NO_SID,
433                      PSID sid2 = NO_SID, DWORD access2 = 0);
434
435 ssize_t __stdcall read_ea (HANDLE hdl, path_conv &pc, const char *name,
436                            char *value, size_t size);
437 int __stdcall write_ea (HANDLE hdl, path_conv &pc, const char *name,
438                         const char *value, size_t size, int flags);
439
440 /* Note: sid1 is usually (read: currently always) the current user's
441    effective sid (cygheap->user.sid ()). */
442 extern inline SECURITY_ATTRIBUTES *
443 sec_user_nih (SECURITY_ATTRIBUTES *sa_buf, PSID sid1, PSID sid2 = NULL,
444               DWORD access2 = 0)
445 {
446   return __sec_user (sa_buf, sid1, sid2, access2, FALSE);
447 }
448
449 extern inline SECURITY_ATTRIBUTES *
450 sec_user (SECURITY_ATTRIBUTES *sa_buf, PSID sid1, PSID sid2 = NULL,
451           DWORD access2 = 0)
452 {
453   return __sec_user (sa_buf, sid1, sid2, access2, TRUE);
454 }
455 #endif /*_SECURITY_H*/