OSDN Git Service

* dir.cc (mkdir): Use local security_descriptor. Call
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / security.h
1 /* security.h: security declarations
2
3    Copyright 2000, 2001, 2002 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 #define FILE_READ_BITS   (FILE_READ_DATA | GENERIC_READ | GENERIC_ALL)
27 #define FILE_WRITE_BITS  (FILE_WRITE_DATA | GENERIC_WRITE | GENERIC_ALL)
28 #define FILE_EXEC_BITS   (FILE_EXECUTE | GENERIC_EXECUTE | GENERIC_ALL)
29
30 class cygpsid {
31 protected:
32   PSID psid;
33 public:
34   cygpsid () {}
35   cygpsid (PSID nsid) { psid = nsid; }
36   operator const PSID () { return psid; }
37   const PSID operator= (PSID nsid) { return psid = nsid;}
38   __uid32_t get_id (BOOL search_grp, int *type = NULL);
39   int get_uid () { return get_id (FALSE); }
40   int get_gid () { return get_id (TRUE); }
41
42   char *string (char *nsidstr) const;
43
44   bool operator== (const PSID nsid) const
45     {
46       if (!psid || !nsid)
47         return nsid == psid;
48       return EqualSid (psid, nsid);
49     }
50   bool operator!= (const PSID nsid) const
51     { return !(*this == nsid); }
52   bool operator== (const char *nsidstr) const;
53   bool operator!= (const char *nsidstr) const
54     { return !(*this == nsidstr); }
55
56   void debug_print (const char *prefix = NULL) const
57     {
58       char buf[256] __attribute__ ((unused));
59       debug_printf ("%s %s", prefix ?: "", string (buf) ?: "NULL");
60     }
61 };
62
63 class cygsid : public cygpsid {
64   char sbuf[MAX_SID_LEN];
65
66   const PSID getfromstr (const char *nsidstr);
67   PSID get_sid (DWORD s, DWORD cnt, DWORD *r);
68
69   inline const PSID assign (const PSID nsid)
70     {
71       if (!nsid)
72         psid = NO_SID;
73       else
74         {
75           psid = (PSID) sbuf;
76           CopySid (MAX_SID_LEN, psid, nsid);
77         }
78       return psid;
79     }
80
81 public:
82   static void init();
83   inline operator const PSID () { return psid; }
84
85   inline const PSID operator= (cygsid &nsid)
86     { return assign (nsid); }
87   inline const PSID operator= (const PSID nsid)
88     { return assign (nsid); }
89   inline const PSID operator= (const char *nsidstr)
90     { return getfromstr (nsidstr); }
91
92   inline cygsid () : cygpsid ((PSID) sbuf) {}
93   inline cygsid (const PSID nsid) { *this = nsid; }
94   inline cygsid (const char *nstrsid) { *this = nstrsid; }
95
96   inline PSID set () { return psid = (PSID) sbuf; }
97
98   BOOL getfrompw (const struct passwd *pw);
99   BOOL getfromgr (const struct __group32 *gr);
100 };
101
102 typedef enum { cygsidlist_empty, cygsidlist_alloc, cygsidlist_auto } cygsidlist_type;
103 class cygsidlist {
104   int maxcount;
105 public:
106   int count;
107   cygsid *sids;
108   cygsidlist_type type;
109
110   cygsidlist (cygsidlist_type t, int m)
111     {
112       type = t;
113       count = 0;
114       maxcount = m;
115       if (t == cygsidlist_alloc)
116         sids = alloc_sids (m);
117       else
118         sids = new cygsid [m];
119     }
120   ~cygsidlist () { if (type == cygsidlist_auto) delete [] sids; }
121
122   BOOL add (const PSID nsi) /* Only with auto for now */
123     {
124       if (count >= maxcount)
125         {
126           cygsid *tmp = new cygsid [ 2 * maxcount];
127           if (!tmp)
128             return FALSE;
129           maxcount *= 2;
130           for (int i = 0; i < count; ++i)
131             tmp[i] = sids[i];
132           delete [] sids;
133           sids = tmp;
134         }
135       sids[count++] = nsi;
136       return TRUE;
137     }
138   BOOL add (cygsid &nsi) { return add ((PSID) nsi); }
139   BOOL add (const char *sidstr)
140     { cygsid nsi (sidstr); return add (nsi); }
141   BOOL addfromgr (struct __group32 *gr) /* Only with alloc */
142     { return sids[count++].getfromgr (gr); }
143
144   BOOL operator+= (cygsid &si) { return add (si); }
145   BOOL operator+= (const char *sidstr) { return add (sidstr); }
146   BOOL operator+= (const PSID psid) { return add (psid); }
147
148   int position (const PSID sid) const
149     {
150       for (int i = 0; i < count; ++i)
151         if (sids[i] == sid)
152           return i;
153       return -1;
154     }
155
156   BOOL contains (const PSID sid) const { return position (sid) >= 0; }
157   cygsid *alloc_sids (int n);
158   void free_sids ();
159   void debug_print (const char *prefix = NULL) const
160     {
161       debug_printf ("-- begin sidlist ---");
162       if (!count)
163         debug_printf ("No elements");
164       for (int i = 0; i < count; ++i)
165         sids[i].debug_print (prefix);
166       debug_printf ("-- ende sidlist ---");
167     }
168 };
169
170 /* Wrapper class to allow simple deleting of buffer space allocated
171    by read_sd() */
172 class security_descriptor {
173 protected:
174   PSECURITY_DESCRIPTOR psd;
175   DWORD sd_size;
176 public:
177   security_descriptor () : psd (NULL), sd_size (0) {}
178   ~security_descriptor () { free (); }
179
180   PSECURITY_DESCRIPTOR malloc (size_t nsize);
181   PSECURITY_DESCRIPTOR realloc (size_t nsize);
182   void free (void);
183
184   inline DWORD size (void) const { return sd_size; }
185   inline operator const PSECURITY_DESCRIPTOR () { return psd; }
186 };
187
188 class user_groups {
189 public:
190   cygsid pgsid;
191   cygsidlist sgsids;
192   BOOL ischanged;
193
194   BOOL issetgroups () const { return (sgsids.type == cygsidlist_alloc); }
195   void update_supp (const cygsidlist &newsids)
196     {
197       sgsids.free_sids ();
198       sgsids = newsids;
199       ischanged = TRUE;
200     }
201   void clear_supp ()
202     {
203       if (issetgroups ())
204         {
205           sgsids.free_sids ();
206           ischanged = TRUE;
207         }
208     }
209   void update_pgrp (const PSID sid)
210     {
211       pgsid = sid;
212       ischanged = TRUE;
213     }
214 };
215
216 extern cygsid well_known_null_sid;
217 extern cygsid well_known_world_sid;
218 extern cygsid well_known_local_sid;
219 extern cygsid well_known_creator_owner_sid;
220 extern cygsid well_known_creator_group_sid;
221 extern cygsid well_known_dialup_sid;
222 extern cygsid well_known_network_sid;
223 extern cygsid well_known_batch_sid;
224 extern cygsid well_known_interactive_sid;
225 extern cygsid well_known_service_sid;
226 extern cygsid well_known_authenticated_users_sid;
227 extern cygsid well_known_system_sid;
228 extern cygsid well_known_admins_sid;
229
230 inline BOOL
231 legal_sid_type (SID_NAME_USE type)
232 {
233   return type == SidTypeUser  || type == SidTypeGroup
234       || type == SidTypeAlias || type == SidTypeWellKnownGroup;
235 }
236
237 extern bool allow_ntea;
238 extern bool allow_ntsec;
239 extern bool allow_smbntsec;
240
241 /* File manipulation */
242 int __stdcall set_process_privileges ();
243 int __stdcall get_file_attribute (int, const char *, mode_t *,
244                                   __uid32_t * = NULL, __gid32_t * = NULL);
245 int __stdcall set_file_attribute (int, const char *, int);
246 int __stdcall set_file_attribute (int, const char *, __uid32_t, __gid32_t, int);
247 int __stdcall get_object_attribute (HANDLE handle, SE_OBJECT_TYPE object_type, mode_t *,
248                                   __uid32_t * = NULL, __gid32_t * = NULL);
249 LONG __stdcall read_sd (const char *file, security_descriptor &sd);
250 LONG __stdcall write_sd (const char *file, security_descriptor &sd);
251 BOOL __stdcall add_access_allowed_ace (PACL acl, int offset, DWORD attributes, PSID sid, size_t &len_add, DWORD inherit);
252 BOOL __stdcall add_access_denied_ace (PACL acl, int offset, DWORD attributes, PSID sid, size_t &len_add, DWORD inherit);
253 int __stdcall check_file_access (const char *, int);
254
255 void set_security_attribute (int attribute, PSECURITY_ATTRIBUTES psa,
256                              security_descriptor &sd_buf);
257
258 bool get_sids_info (cygpsid, cygpsid, __uid32_t * , __gid32_t *);
259
260 /* Try a subauthentication. */
261 HANDLE subauth (struct passwd *pw);
262 /* Try creating a token directly. */
263 HANDLE create_token (cygsid &usersid, user_groups &groups, struct passwd * pw);
264 /* Verify an existing token */
265 BOOL verify_token (HANDLE token, cygsid &usersid, user_groups &groups, BOOL * pintern = NULL);
266
267 /* Extract U-domain\user field from passwd entry. */
268 void extract_nt_dom_user (const struct passwd *pw, char *domain, char *user);
269 /* Get default logonserver for a domain. */
270 BOOL get_logon_server (const char * domain, char * server, WCHAR *wserver = NULL);
271
272 /* sec_helper.cc: Security helper functions. */
273 int set_process_privilege (const char *privilege, bool enable = true, bool use_thread = false);
274
275 /* shared.cc: */
276 /* Retrieve a security descriptor that allows all access */
277 SECURITY_DESCRIPTOR *__stdcall get_null_sd (void);
278
279 /* Various types of security attributes for use in Create* functions. */
280 extern SECURITY_ATTRIBUTES sec_none, sec_none_nih, sec_all, sec_all_nih;
281 extern SECURITY_ATTRIBUTES *__stdcall __sec_user (PVOID sa_buf, PSID sid1, PSID sid2, 
282                                                   DWORD access2, BOOL inherit)
283   __attribute__ ((regparm (3)));
284 extern BOOL sec_acl (PACL acl, bool original, bool admins, PSID sid1 = NO_SID, 
285                      PSID sid2 = NO_SID, DWORD access2 = 0);
286
287 int __stdcall NTReadEA (const char *file, const char *attrname, char *buf, int len);
288 BOOL __stdcall NTWriteEA (const char *file, const char *attrname, const char *buf, int len);
289
290 extern inline SECURITY_ATTRIBUTES *
291 sec_user_nih (char sa_buf[], PSID sid1 = NULL, PSID sid2 = NULL, DWORD access2 = 0)
292 {
293   return __sec_user (sa_buf, sid1, sid2, access2, FALSE);
294 }
295
296 extern inline SECURITY_ATTRIBUTES *
297 sec_user (char sa_buf[], PSID sid1 = NULL, PSID sid2 = NULL, DWORD access2 = 0)
298 {
299   return __sec_user (sa_buf, sid1, sid2, access2, TRUE);
300 }
301 #endif /*_SECURITY_H*/