OSDN Git Service

* sec_auth.cc (get_user_groups): Mark well-known groups as well-known.
[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    2010, 2011 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 /* 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
20
21 #define DEFAULT_UID DOMAIN_USER_RID_ADMIN
22 #define UNKNOWN_UID 400 /* Non conflicting number */
23 #define UNKNOWN_GID 401
24
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)
32
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
71
72 #define SE_MAX_WELL_KNOWN_PRIVILEGE SE_CREATE_SYMBOLIC_LINK_PRIVILEGE
73
74 #endif /* ! SE_CREATE_TOKEN_PRIVILEGE */
75
76 /* Added for debugging purposes. */
77 typedef struct {
78   BYTE  Revision;
79   BYTE  SubAuthorityCount;
80   SID_IDENTIFIER_AUTHORITY IdentifierAuthority;
81   DWORD SubAuthority[8];
82 } DBGSID, *PDBGSID;
83
84 /* Macro to define variable length SID structures */
85 #define MKSID(name, comment, authority, count, rid...) \
86 static NO_COPY struct  { \
87   BYTE  Revision; \
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;
93
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)
97
98 class cygpsid {
99 protected:
100   PSID psid;
101 public:
102   cygpsid () {}
103   cygpsid (PSID nsid) { psid = nsid; }
104   operator PSID () const { return psid; }
105   const PSID operator= (PSID nsid) { return psid = nsid;}
106   __uid32_t get_id (BOOL search_grp, int *type = NULL);
107   int get_uid () { return get_id (FALSE); }
108   int get_gid () { return get_id (TRUE); }
109
110   PWCHAR string (PWCHAR nsidstr) const;
111   char *string (char *nsidstr) const;
112
113   bool operator== (const PSID nsid) const
114     {
115       if (!psid || !nsid)
116         return nsid == psid;
117       return EqualSid (psid, nsid);
118     }
119   bool operator!= (const PSID nsid) const
120     { return !(*this == nsid); }
121   bool operator== (const char *nsidstr) const;
122   bool operator!= (const char *nsidstr) const
123     { return !(*this == nsidstr); }
124
125   void debug_print (const char *prefix = NULL) const
126     {
127       char buf[256] __attribute__ ((unused));
128       debug_printf ("%s %s", prefix ?: "", string (buf) ?: "NULL");
129     }
130 };
131
132 class cygsid : public cygpsid {
133   char sbuf[MAX_SID_LEN];
134   bool well_known_sid;
135
136   const PSID getfromstr (const char *nsidstr, bool well_known);
137   PSID get_sid (DWORD s, DWORD cnt, DWORD *r, bool well_known);
138
139   inline const PSID assign (const PSID nsid, bool well_known)
140     {
141       if (!nsid)
142         psid = NO_SID;
143       else
144         {
145           psid = (PSID) sbuf;
146           CopySid (MAX_SID_LEN, psid, nsid);
147           well_known_sid = well_known;
148         }
149       return psid;
150     }
151
152 public:
153   inline operator const PSID () { return psid; }
154   inline bool is_well_known_sid () { return well_known_sid; }
155
156   /* Both, = and *= are assignment operators.  = creates a "normal" SID,
157      *= marks the SID as being a well-known SID.  This difference is
158      important when creating a SID list for LSA authentication. */
159   inline const PSID operator= (cygsid &nsid)
160     { return assign (nsid, nsid.well_known_sid); }
161   inline const PSID operator= (const PSID nsid)
162     { return assign (nsid, false); }
163   inline const PSID operator= (const char *nsidstr)
164     { return getfromstr (nsidstr, false); }
165   inline const PSID operator*= (cygsid &nsid)
166     { return assign (nsid, true); }
167   inline const PSID operator*= (const PSID nsid)
168     { return assign (nsid, true); }
169   inline const PSID operator*= (const char *nsidstr)
170     { return getfromstr (nsidstr, true); }
171
172   inline cygsid () : cygpsid ((PSID) sbuf), well_known_sid (false) {}
173   inline cygsid (const PSID nsid) { *this = nsid; }
174   inline cygsid (const char *nstrsid) { *this = nstrsid; }
175
176   inline PSID set () { return psid = (PSID) sbuf; }
177
178   BOOL getfrompw (const struct passwd *pw);
179   BOOL getfromgr (const struct __group32 *gr);
180
181   void debug_print (const char *prefix = NULL) const
182     {
183       char buf[256] __attribute__ ((unused));
184       debug_printf ("%s %s%s", prefix ?: "", string (buf) ?: "NULL", well_known_sid ? " (*)" : " (+)");
185     }
186 };
187
188 typedef enum { cygsidlist_empty, cygsidlist_alloc, cygsidlist_auto } cygsidlist_type;
189 class cygsidlist {
190   int maxcnt;
191   int cnt;
192
193   BOOL add (const PSID nsi, bool well_known); /* Only with auto for now */
194
195 public:
196   cygsid *sids;
197   cygsidlist_type type;
198
199   cygsidlist (cygsidlist_type t, int m)
200   : maxcnt (m), cnt (0), type (t)
201     {
202       if (t == cygsidlist_alloc)
203         sids = alloc_sids (m);
204       else
205         sids = new cygsid [m];
206     }
207   ~cygsidlist () { if (type == cygsidlist_auto) delete [] sids; }
208
209   BOOL addfromgr (struct __group32 *gr) /* Only with alloc */
210     { return sids[cnt].getfromgr (gr) && ++cnt; }
211
212   /* += adds a "normal" SID, *= adds a well-known SID.  See comment in class
213      cygsid above. */
214   BOOL operator+= (cygsid &si) { return add ((PSID) si, false); }
215   BOOL operator+= (const char *sidstr) { cygsid nsi (sidstr);
216                                          return add ((PSID) nsi, false); }
217   BOOL operator+= (const PSID psid) { return add (psid, false); }
218   BOOL operator*= (cygsid &si) { return add ((PSID) si, true); }
219   BOOL operator*= (const char *sidstr) { cygsid nsi (sidstr);
220                                          return add ((PSID) nsi, true); }
221   BOOL operator*= (const PSID psid) { return add (psid, true); }
222
223   void count (int ncnt)
224     { cnt = ncnt; }
225   int count () const { return cnt; }
226   int non_well_known_count () const
227     {
228       int wcnt = 0;
229       for (int i = 0; i < cnt; ++i)
230         if (!sids[i].is_well_known_sid ())
231           ++wcnt;
232       return wcnt;
233     }
234
235   int position (const PSID sid) const
236     {
237       for (int i = 0; i < cnt; ++i)
238         if (sids[i] == sid)
239           return i;
240       return -1;
241     }
242
243   int next_non_well_known_sid (int idx)
244     {
245       while (++idx < cnt)
246         if (!sids[idx].is_well_known_sid ())
247           return idx;
248       return -1;
249     }
250   BOOL contains (const PSID sid) const { return position (sid) >= 0; }
251   cygsid *alloc_sids (int n);
252   void free_sids ();
253   void debug_print (const char *prefix = NULL) const
254     {
255       debug_printf ("-- begin sidlist ---");
256       if (!cnt)
257         debug_printf ("No elements");
258       for (int i = 0; i < cnt; ++i)
259         sids[i].debug_print (prefix);
260       debug_printf ("-- ende sidlist ---");
261     }
262 };
263
264 /* Wrapper class to allow simple deleting of buffer space allocated
265    by read_sd() */
266 class security_descriptor {
267 protected:
268   PSECURITY_DESCRIPTOR psd;
269   DWORD sd_size;
270 public:
271   security_descriptor () : psd (NULL), sd_size (0) {}
272   ~security_descriptor () { free (); }
273
274   PSECURITY_DESCRIPTOR malloc (size_t nsize);
275   PSECURITY_DESCRIPTOR realloc (size_t nsize);
276   void free ();
277
278   inline DWORD size () const { return sd_size; }
279   inline DWORD copy (void *buf, DWORD buf_size) const {
280     if (buf_size < size ())
281       return sd_size;
282     memcpy (buf, psd, sd_size);
283     return 0;
284   }
285   inline operator const PSECURITY_DESCRIPTOR () { return psd; }
286   inline operator PSECURITY_DESCRIPTOR *() { return &psd; }
287   inline void operator =(PSECURITY_DESCRIPTOR nsd) { psd = nsd; }
288 };
289
290 class user_groups {
291 public:
292   cygsid pgsid;
293   cygsidlist sgsids;
294   BOOL ischanged;
295
296   BOOL issetgroups () const { return (sgsids.type == cygsidlist_alloc); }
297   void update_supp (const cygsidlist &newsids)
298     {
299       sgsids.free_sids ();
300       sgsids = newsids;
301       ischanged = TRUE;
302     }
303   void clear_supp ()
304     {
305       if (issetgroups ())
306         {
307           sgsids.free_sids ();
308           ischanged = TRUE;
309         }
310     }
311   void update_pgrp (const PSID sid)
312     {
313       pgsid = sid;
314       ischanged = TRUE;
315     }
316 };
317
318 extern cygpsid well_known_null_sid;
319 extern cygpsid well_known_world_sid;
320 extern cygpsid well_known_local_sid;
321 extern cygpsid well_known_creator_owner_sid;
322 extern cygpsid well_known_creator_group_sid;
323 extern cygpsid well_known_dialup_sid;
324 extern cygpsid well_known_network_sid;
325 extern cygpsid well_known_batch_sid;
326 extern cygpsid well_known_interactive_sid;
327 extern cygpsid well_known_service_sid;
328 extern cygpsid well_known_authenticated_users_sid;
329 extern cygpsid well_known_this_org_sid;
330 extern cygpsid well_known_system_sid;
331 extern cygpsid well_known_builtin_sid;
332 extern cygpsid well_known_admins_sid;
333 extern cygpsid well_known_users_sid;
334 extern cygpsid fake_logon_sid;
335 extern cygpsid mandatory_medium_integrity_sid;
336 extern cygpsid mandatory_high_integrity_sid;
337 extern cygpsid mandatory_system_integrity_sid;
338 extern cygpsid well_known_samba_unix_user_fake_sid;
339
340 bool privilege_luid (const PWCHAR pname, LUID *luid);
341
342 inline BOOL
343 well_known_sid_type (SID_NAME_USE type)
344 {
345   return type == SidTypeAlias || type == SidTypeWellKnownGroup;
346 }
347
348 inline BOOL
349 legal_sid_type (SID_NAME_USE type)
350 {
351   return type == SidTypeUser  || type == SidTypeGroup
352       || type == SidTypeAlias || type == SidTypeWellKnownGroup;
353 }
354
355 class path_conv;
356 /* File manipulation */
357 int __stdcall get_file_attribute (HANDLE, path_conv &, mode_t *,
358                                   __uid32_t *, __gid32_t *)
359                 __attribute__ ((regparm (3)));
360 int __stdcall set_file_attribute (HANDLE, path_conv &,
361                                   __uid32_t, __gid32_t, mode_t)
362                 __attribute__ ((regparm (3)));
363 int __stdcall get_object_sd (HANDLE, security_descriptor &)
364                 __attribute__ ((regparm (2)));
365 int __stdcall get_object_attribute (HANDLE, __uid32_t *, __gid32_t *, mode_t *)
366                 __attribute__ ((regparm (3)));
367 int __stdcall set_object_attribute (HANDLE, __uid32_t, __gid32_t, mode_t)
368                 __attribute__ ((regparm (3)));
369 int __stdcall create_object_sd_from_attribute (HANDLE, __uid32_t, __gid32_t,
370                                                mode_t, security_descriptor &)
371                 __attribute__ ((regparm (3)));
372 int __stdcall set_object_sd (HANDLE, security_descriptor &, bool)
373                 __attribute__ ((regparm (3)));
374
375 int __stdcall get_reg_attribute (HKEY hkey, mode_t *, __uid32_t *, __gid32_t *)
376                 __attribute__ ((regparm (3)));
377 LONG __stdcall get_file_sd (HANDLE fh, path_conv &, security_descriptor &, bool)
378                 __attribute__ ((regparm (3)));
379 LONG __stdcall set_file_sd (HANDLE fh, path_conv &, security_descriptor &, bool)
380                 __attribute__ ((regparm (3)));
381 bool __stdcall add_access_allowed_ace (PACL, int, DWORD, PSID, size_t &, DWORD)
382                 __attribute__ ((regparm (3)));
383 bool __stdcall add_access_denied_ace (PACL, int, DWORD, PSID, size_t &, DWORD)
384                 __attribute__ ((regparm (3)));
385 int __stdcall check_file_access (path_conv &, int, bool)
386                 __attribute__ ((regparm (3)));
387 int __stdcall check_registry_access (HANDLE, int, bool)
388                 __attribute__ ((regparm (3)));
389
390 void set_security_attribute (path_conv &pc, int attribute,
391                              PSECURITY_ATTRIBUTES psa,
392                              security_descriptor &sd_buf);
393
394 bool get_sids_info (cygpsid, cygpsid, __uid32_t * , __gid32_t *);
395
396 /* sec_acl.cc */
397 struct __acl32;
398 extern "C" int aclsort32 (int, int, __acl32 *);
399 extern "C" int acl32 (const char *, int, int, __acl32 *);
400 int getacl (HANDLE, path_conv &, int, __acl32 *);
401 int setacl (HANDLE, path_conv &, int, __acl32 *, bool &);
402
403 /* Set impersonation or restricted token.  */
404 void set_imp_token (HANDLE token, int type);
405 /* Function creating a token by calling NtCreateToken. */
406 HANDLE create_token (cygsid &usersid, user_groups &groups, struct passwd * pw);
407 /* LSA authentication function. */
408 HANDLE lsaauth (cygsid &, user_groups &, struct passwd *);
409 /* LSA private key storage authentication, same as when using service logons. */
410 HANDLE lsaprivkeyauth (struct passwd *pw);
411 /* Verify an existing token */
412 bool verify_token (HANDLE token, cygsid &usersid, user_groups &groups, bool *pintern = NULL);
413 /* Get groups of a user */
414 bool get_server_groups (cygsidlist &grp_list, PSID usersid, struct passwd *pw);
415
416 /* Extract U-domain\user field from passwd entry. */
417 void extract_nt_dom_user (const struct passwd *pw, PWCHAR domain, PWCHAR user);
418 /* Get default logonserver for a domain. */
419 bool get_logon_server (PWCHAR domain, PWCHAR wserver, bool rediscovery);
420
421 HANDLE open_local_policy (ACCESS_MASK access);
422
423 /* sec_helper.cc: Security helper functions. */
424 int set_privilege (HANDLE token, DWORD privilege, bool enable);
425 void set_cygwin_privileges (HANDLE token);
426
427 #define _push_thread_privilege(_priv, _val, _check) { \
428     HANDLE _dup_token = NULL; \
429     HANDLE _token = (cygheap->user.issetuid () && (_check)) \
430                     ? cygheap->user.primary_token () : hProcToken; \
431     if (!DuplicateTokenEx (_token, MAXIMUM_ALLOWED, NULL, \
432                            SecurityImpersonation, TokenImpersonation, \
433                            &_dup_token)) \
434       debug_printf ("DuplicateTokenEx: %E"); \
435     else if (!ImpersonateLoggedOnUser (_dup_token)) \
436       debug_printf ("ImpersonateLoggedOnUser: %E"); \
437     else \
438       set_privilege (_dup_token, (_priv), (_val));
439
440 #define push_thread_privilege(_priv, _val) _push_thread_privilege(_priv,_val,1)
441 #define push_self_privilege(_priv, _val)   _push_thread_privilege(_priv,_val,0)
442
443 #define pop_thread_privilege() \
444     if (_dup_token) \
445       { \
446         if (!cygheap->user.issetuid ()) \
447           RevertToSelf (); \
448         else \
449           cygheap->user.reimpersonate (); \
450         CloseHandle (_dup_token); \
451       } \
452   }
453
454 #define pop_self_privilege()               pop_thread_privilege()
455
456 /* shared.cc: */
457
458 /* Various types of security attributes for use in Create* functions. */
459 extern SECURITY_ATTRIBUTES sec_none, sec_none_nih, sec_all, sec_all_nih;
460 extern SECURITY_ATTRIBUTES *__stdcall __sec_user (PVOID, PSID, PSID,
461                                                   DWORD, BOOL)
462                 __attribute__ ((regparm (3)));
463 extern PSECURITY_DESCRIPTOR _everyone_sd (void *buf, ACCESS_MASK access);
464 #define everyone_sd(access)     (_everyone_sd (alloca (SD_MIN_SIZE), (access)))
465
466 #define sec_none_cloexec(f) (((f) & O_CLOEXEC ? &sec_none_nih : &sec_none))
467
468 extern bool sec_acl (PACL acl, bool original, bool admins, PSID sid1 = NO_SID,
469                      PSID sid2 = NO_SID, DWORD access2 = 0);
470
471 ssize_t __stdcall read_ea (HANDLE, path_conv &, const char *,
472                            char *, size_t)
473                 __attribute__ ((regparm (3)));
474 int __stdcall write_ea (HANDLE, path_conv &, const char *, const char *,
475                         size_t, int)
476                 __attribute__ ((regparm (3)));
477
478 /* Note: sid1 is usually (read: currently always) the current user's
479    effective sid (cygheap->user.sid ()). */
480 extern inline SECURITY_ATTRIBUTES *
481 sec_user_nih (SECURITY_ATTRIBUTES *sa_buf, PSID sid1, PSID sid2 = NULL,
482               DWORD access2 = 0)
483 {
484   return __sec_user (sa_buf, sid1, sid2, access2, FALSE);
485 }
486
487 extern inline SECURITY_ATTRIBUTES *
488 sec_user (SECURITY_ATTRIBUTES *sa_buf, PSID sid1, PSID sid2 = NULL,
489           DWORD access2 = 0)
490 {
491   return __sec_user (sa_buf, sid1, sid2, access2, TRUE);
492 }
493
494 #endif /*_SECURITY_H*/