OSDN Git Service

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