OSDN Git Service

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