OSDN Git Service

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