OSDN Git Service

* security.h (DBGSID): Define for debugging purposes.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / security.h
1 /* security.h: security declarations
2
3    Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006 Red Hat, Inc.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #ifndef _SECURITY_H
12 #define _SECURITY_H
13
14 #include <accctrl.h>
15
16 #define DEFAULT_UID DOMAIN_USER_RID_ADMIN
17 #define UNKNOWN_UID 400 /* Non conflicting number */
18 #define UNKNOWN_GID 401
19
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)
25
26 /* Added for debugging purposes. */
27 typedef struct {
28   BYTE  Revision;
29   BYTE  SubAuthorityCount;
30   SID_IDENTIFIER_AUTHORITY IdentifierAuthority;
31   DWORD SubAuthority[8];
32 } DBGSID, *PDBGSID;
33
34 /* Macro to define variable length SID structures */
35 #define MKSID(name, comment, authority, count, rid...) \
36 static NO_COPY struct  { \
37   BYTE  Revision; \
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;
43
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)
47
48 class cygpsid {
49 protected:
50   PSID psid;
51 public:
52   cygpsid () {}
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); }
59
60   char *string (char *nsidstr) const;
61
62   bool operator== (const PSID nsid) const
63     {
64       if (!psid || !nsid)
65         return nsid == psid;
66       return EqualSid (psid, nsid);
67     }
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); }
73
74   void debug_print (const char *prefix = NULL) const
75     {
76       char buf[256] __attribute__ ((unused));
77       debug_printf ("%s %s", prefix ?: "", string (buf) ?: "NULL");
78     }
79 };
80
81 class cygsid : public cygpsid {
82   char sbuf[MAX_SID_LEN];
83
84   const PSID getfromstr (const char *nsidstr);
85   PSID get_sid (DWORD s, DWORD cnt, DWORD *r);
86
87   inline const PSID assign (const PSID nsid)
88     {
89       if (!nsid)
90         psid = NO_SID;
91       else
92         {
93           psid = (PSID) sbuf;
94           CopySid (MAX_SID_LEN, psid, nsid);
95         }
96       return psid;
97     }
98
99 public:
100   inline operator const PSID () { return psid; }
101
102   inline const PSID operator= (cygsid &nsid)
103     { return assign (nsid); }
104   inline const PSID operator= (const PSID nsid)
105     { return assign (nsid); }
106   inline const PSID operator= (const char *nsidstr)
107     { return getfromstr (nsidstr); }
108
109   inline cygsid () : cygpsid ((PSID) sbuf) {}
110   inline cygsid (const PSID nsid) { *this = nsid; }
111   inline cygsid (const char *nstrsid) { *this = nstrsid; }
112
113   inline PSID set () { return psid = (PSID) sbuf; }
114
115   BOOL getfrompw (const struct passwd *pw);
116   BOOL getfromgr (const struct __group32 *gr);
117 };
118
119 typedef enum { cygsidlist_empty, cygsidlist_alloc, cygsidlist_auto } cygsidlist_type;
120 class cygsidlist {
121   int maxcount;
122 public:
123   int count;
124   cygsid *sids;
125   cygsidlist_type type;
126
127   cygsidlist (cygsidlist_type t, int m)
128     {
129       type = t;
130       count = 0;
131       maxcount = m;
132       if (t == cygsidlist_alloc)
133         sids = alloc_sids (m);
134       else
135         sids = new cygsid [m];
136     }
137   ~cygsidlist () { if (type == cygsidlist_auto) delete [] sids; }
138
139   BOOL add (const PSID nsi) /* Only with auto for now */
140     {
141       if (count >= maxcount)
142         {
143           cygsid *tmp = new cygsid [ 2 * maxcount];
144           if (!tmp)
145             return FALSE;
146           maxcount *= 2;
147           for (int i = 0; i < count; ++i)
148             tmp[i] = sids[i];
149           delete [] sids;
150           sids = tmp;
151         }
152       sids[count++] = nsi;
153       return TRUE;
154     }
155   BOOL add (cygsid &nsi) { return add ((PSID) nsi); }
156   BOOL add (const char *sidstr)
157     { cygsid nsi (sidstr); return add (nsi); }
158   BOOL addfromgr (struct __group32 *gr) /* Only with alloc */
159     { return sids[count].getfromgr (gr) && ++count; }
160
161   BOOL operator+= (cygsid &si) { return add (si); }
162   BOOL operator+= (const char *sidstr) { return add (sidstr); }
163   BOOL operator+= (const PSID psid) { return add (psid); }
164
165   int position (const PSID sid) const
166     {
167       for (int i = 0; i < count; ++i)
168         if (sids[i] == sid)
169           return i;
170       return -1;
171     }
172
173   BOOL contains (const PSID sid) const { return position (sid) >= 0; }
174   cygsid *alloc_sids (int n);
175   void free_sids ();
176   void debug_print (const char *prefix = NULL) const
177     {
178       debug_printf ("-- begin sidlist ---");
179       if (!count)
180         debug_printf ("No elements");
181       for (int i = 0; i < count; ++i)
182         sids[i].debug_print (prefix);
183       debug_printf ("-- ende sidlist ---");
184     }
185 };
186
187 /* Wrapper class to allow simple deleting of buffer space allocated
188    by read_sd() */
189 class security_descriptor {
190 protected:
191   PSECURITY_DESCRIPTOR psd;
192   DWORD sd_size;
193   enum { local_alloced, malloced } type;
194 public:
195   security_descriptor () : psd (NULL), sd_size (0), type (local_alloced) {}
196   ~security_descriptor () { free (); }
197
198   PSECURITY_DESCRIPTOR malloc (size_t nsize);
199   PSECURITY_DESCRIPTOR realloc (size_t nsize);
200   void free ();
201
202   inline DWORD size () {
203     if (!sd_size && psd && type == local_alloced)
204       sd_size = LocalSize (psd);
205     return sd_size;
206   }
207   inline DWORD copy (void *buf, DWORD buf_size) {
208     if (buf_size < size ())
209       return sd_size;
210     memcpy (buf, psd, sd_size);
211     return 0;
212   }
213   inline operator const PSECURITY_DESCRIPTOR () { return psd; }
214   inline operator PSECURITY_DESCRIPTOR *() { return &psd; }
215 };
216
217 class user_groups {
218 public:
219   cygsid pgsid;
220   cygsidlist sgsids;
221   BOOL ischanged;
222
223   BOOL issetgroups () const { return (sgsids.type == cygsidlist_alloc); }
224   void update_supp (const cygsidlist &newsids)
225     {
226       sgsids.free_sids ();
227       sgsids = newsids;
228       ischanged = TRUE;
229     }
230   void clear_supp ()
231     {
232       if (issetgroups ())
233         {
234           sgsids.free_sids ();
235           ischanged = TRUE;
236         }
237     }
238   void update_pgrp (const PSID sid)
239     {
240       pgsid = sid;
241       ischanged = TRUE;
242     }
243 };
244
245 extern cygpsid well_known_null_sid;
246 extern cygpsid well_known_world_sid;
247 extern cygpsid well_known_local_sid;
248 extern cygpsid well_known_creator_owner_sid;
249 extern cygpsid well_known_creator_group_sid;
250 extern cygpsid well_known_dialup_sid;
251 extern cygpsid well_known_network_sid;
252 extern cygpsid well_known_batch_sid;
253 extern cygpsid well_known_interactive_sid;
254 extern cygpsid well_known_service_sid;
255 extern cygpsid well_known_authenticated_users_sid;
256 extern cygpsid well_known_this_org_sid;
257 extern cygpsid well_known_system_sid;
258 extern cygpsid well_known_admins_sid;
259 extern cygpsid mandatory_medium_integrity_sid;
260 extern cygpsid mandatory_high_integrity_sid;
261 extern cygpsid mandatory_system_integrity_sid;
262
263 /* Order must be same as cygpriv in sec_helper.cc. */
264 enum cygpriv_idx {
265   SE_CREATE_TOKEN_PRIV = 0,
266   SE_ASSIGNPRIMARYTOKEN_PRIV,
267   SE_LOCK_MEMORY_PRIV,
268   SE_INCREASE_QUOTA_PRIV,
269   SE_UNSOLICITED_INPUT_PRIV,
270   SE_MACHINE_ACCOUNT_PRIV,
271   SE_TCB_PRIV,
272   SE_SECURITY_PRIV,
273   SE_TAKE_OWNERSHIP_PRIV,
274   SE_LOAD_DRIVER_PRIV,
275   SE_SYSTEM_PROFILE_PRIV,
276   SE_SYSTEMTIME_PRIV,
277   SE_PROF_SINGLE_PROCESS_PRIV,
278   SE_INC_BASE_PRIORITY_PRIV,
279   SE_CREATE_PAGEFILE_PRIV,
280   SE_CREATE_PERMANENT_PRIV,
281   SE_BACKUP_PRIV,
282   SE_RESTORE_PRIV,
283   SE_SHUTDOWN_PRIV,
284   SE_DEBUG_PRIV,
285   SE_AUDIT_PRIV,
286   SE_SYSTEM_ENVIRONMENT_PRIV,
287   SE_CHANGE_NOTIFY_PRIV,
288   SE_REMOTE_SHUTDOWN_PRIV,
289   SE_CREATE_GLOBAL_PRIV,
290   SE_UNDOCK_PRIV,
291   SE_MANAGE_VOLUME_PRIV,
292   SE_IMPERSONATE_PRIV,
293   SE_ENABLE_DELEGATION_PRIV,
294   SE_SYNC_AGENT_PRIV,
295   SE_RELABEL_PRIV,
296   SE_INCREASE_WORKING_SET_PRIV,
297   SE_TIME_ZONE_PRIV,
298   SE_CREATE_SYMBOLIC_LINK_PRIV,
299
300   SE_NUM_PRIVS
301 };
302
303 const LUID *privilege_luid (enum cygpriv_idx idx);
304 const LUID *privilege_luid_by_name (const char *pname);
305 const char *privilege_name (enum cygpriv_idx idx);
306
307 inline BOOL
308 legal_sid_type (SID_NAME_USE type)
309 {
310   return type == SidTypeUser  || type == SidTypeGroup
311       || type == SidTypeAlias || type == SidTypeWellKnownGroup;
312 }
313
314 extern bool allow_ntea;
315 extern bool allow_ntsec;
316 extern bool allow_smbntsec;
317 extern bool allow_traverse;
318
319 /* File manipulation */
320 int __stdcall get_file_attribute (int, HANDLE, const char *, mode_t *,
321                                   __uid32_t * = NULL, __gid32_t * = NULL);
322 int __stdcall set_file_attribute (bool, HANDLE, const char *, int);
323 int __stdcall set_file_attribute (bool, HANDLE, const char *, __uid32_t, __gid32_t, int);
324 int __stdcall get_nt_object_security (HANDLE, SE_OBJECT_TYPE,
325                                       security_descriptor &);
326 int __stdcall get_object_attribute (HANDLE handle, SE_OBJECT_TYPE object_type, mode_t *,
327                                   __uid32_t * = NULL, __gid32_t * = NULL);
328 LONG __stdcall read_sd (const char *file, security_descriptor &sd);
329 LONG __stdcall write_sd (HANDLE fh, const char *file, security_descriptor &sd);
330 bool __stdcall add_access_allowed_ace (PACL acl, int offset, DWORD attributes, PSID sid, size_t &len_add, DWORD inherit);
331 bool __stdcall add_access_denied_ace (PACL acl, int offset, DWORD attributes, PSID sid, size_t &len_add, DWORD inherit);
332 int __stdcall check_file_access (const char *, int);
333 int __stdcall check_registry_access (HANDLE, int);
334
335 void set_security_attribute (int attribute, PSECURITY_ATTRIBUTES psa,
336                              security_descriptor &sd_buf);
337
338 bool get_sids_info (cygpsid, cygpsid, __uid32_t * , __gid32_t *);
339
340 /* sec_acl.cc */
341 struct __acl32;
342 extern "C" int aclsort32 (int, int, __acl32 *);
343 extern "C" int acl32 (const char *, int, int, __acl32 *);
344 int getacl (HANDLE, const char *, DWORD, int, __acl32 *);
345 int setacl (HANDLE, const char *, int, __acl32 *);
346
347 struct _UNICODE_STRING;
348 void __stdcall str2buf2uni (_UNICODE_STRING &, WCHAR *, const char *) __attribute__ ((regparm (3)));
349 void __stdcall str2uni_cat (_UNICODE_STRING &, const char *) __attribute__ ((regparm (2)));
350
351 /* Try a subauthentication. */
352 HANDLE subauth (struct passwd *pw);
353 /* Try creating a token directly. */
354 HANDLE create_token (cygsid &usersid, user_groups &groups, struct passwd * pw,
355                      HANDLE subauth_token);
356 /* Verify an existing token */
357 bool verify_token (HANDLE token, cygsid &usersid, user_groups &groups, bool *pintern = NULL);
358 /* Get groups of a user */
359 bool get_server_groups (cygsidlist &grp_list, PSID usersid, struct passwd *pw);
360
361 /* Extract U-domain\user field from passwd entry. */
362 void extract_nt_dom_user (const struct passwd *pw, char *domain, char *user);
363 /* Get default logonserver for a domain. */
364 bool get_logon_server (const char * domain, char * server, WCHAR *wserver,
365                        bool rediscovery);
366
367 /* sec_helper.cc: Security helper functions. */
368 int set_privilege (HANDLE token, enum cygpriv_idx privilege, bool enable);
369 void set_cygwin_privileges (HANDLE token);
370
371 #define set_process_privilege(p,v) set_privilege (hProcImpToken, (p), (v))
372
373 #define _push_thread_privilege(_priv, _val, _check) { \
374     HANDLE _token = NULL, _dup_token = NULL; \
375     if (wincap.has_security ()) \
376       { \
377         _token = (cygheap->user.issetuid () && (_check)) \
378                  ? cygheap->user.token () : hProcImpToken; \
379         if (!DuplicateTokenEx (_token, MAXIMUM_ALLOWED, NULL, \
380                                SecurityImpersonation, TokenImpersonation, \
381                                &_dup_token)) \
382           debug_printf ("DuplicateTokenEx: %E"); \
383         else if (!ImpersonateLoggedOnUser (_dup_token)) \
384           debug_printf ("ImpersonateLoggedOnUser: %E"); \
385         else \
386           set_privilege (_dup_token, (_priv), (_val)); \
387       }
388 #define push_thread_privilege(_priv, _val) _push_thread_privilege(_priv,_val,1)
389 #define push_self_privilege(_priv, _val)   _push_thread_privilege(_priv,_val,0)
390
391 #define pop_thread_privilege() \
392     if (_dup_token) \
393       { \
394         ImpersonateLoggedOnUser (_token); \
395         CloseHandle (_dup_token); \
396       } \
397   }
398 #define pop_self_privilege()               pop_thread_privilege()
399
400 /* shared.cc: */
401 /* Retrieve a security descriptor that allows all access */
402 SECURITY_DESCRIPTOR *__stdcall get_null_sd ();
403
404 /* Various types of security attributes for use in Create* functions. */
405 extern SECURITY_ATTRIBUTES sec_none, sec_none_nih, sec_all, sec_all_nih;
406 extern SECURITY_ATTRIBUTES *__stdcall __sec_user (PVOID sa_buf, PSID sid1, PSID sid2,
407                                                   DWORD access2, BOOL inherit)
408   __attribute__ ((regparm (3)));
409 extern bool sec_acl (PACL acl, bool original, bool admins, PSID sid1 = NO_SID,
410                      PSID sid2 = NO_SID, DWORD access2 = 0);
411
412 int __stdcall read_ea (HANDLE hdl, const char *file, const char *attrname,
413                        char *buf, int len);
414 BOOL __stdcall write_ea (HANDLE hdl, const char *file, const char *attrname,
415                          const char *buf, int len);
416
417 /* Note: sid1 is usually (read: currently always) the current user's
418    effective sid (cygheap->user.sid ()). */
419 extern inline SECURITY_ATTRIBUTES *
420 sec_user_nih (SECURITY_ATTRIBUTES *sa_buf, PSID sid1, PSID sid2 = NULL,
421               DWORD access2 = 0)
422 {
423   return __sec_user (sa_buf, sid1, sid2, access2, FALSE);
424 }
425
426 extern inline SECURITY_ATTRIBUTES *
427 sec_user (SECURITY_ATTRIBUTES *sa_buf, PSID sid1, PSID sid2 = NULL,
428           DWORD access2 = 0)
429 {
430   return __sec_user (sa_buf, sid1, sid2, access2, TRUE);
431 }
432 #endif /*_SECURITY_H*/