aboutsummaryrefslogtreecommitdiffstats
path: root/win/rfb_win32/Security.h
blob: 85317fae3c7425e620f9a692fb083811f8b6e111 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

// Security.h

// Wrapper classes for a few Windows NT security structures/functions
// that are used by VNC

#ifndef __RFB_WIN32_SECURITY_H__
#define __RFB_WIN32_SECURITY_H__

#include <stdint.h>
#include <vector>
#include <rfb_win32/LocalMem.h>
#include <rfb_win32/TCharArray.h>
#include <aclapi.h>

namespace rfb {

  namespace win32 {

    struct Trustee : public TRUSTEE {
      Trustee(const TCHAR* name,
              TRUSTEE_FORM form=TRUSTEE_IS_NAME,
              TRUSTEE_TYPE type=TRUSTEE_IS_UNKNOWN);
    };

    struct ExplicitAccess : public EXPLICIT_ACCESS {
      ExplicitAccess(const TCHAR* name,
                     TRUSTEE_FORM type,
                     DWORD perms,
                     ACCESS_MODE mode,
                     DWORD inherit=0);
    };

    // Helper class for building access control lists
    struct AccessEntries {
      AccessEntries();
      ~AccessEntries();
      void allocMinEntries(int count);
      void addEntry(const TCHAR* trusteeName,
                    DWORD permissions,
                    ACCESS_MODE mode);
      void addEntry(const PSID sid,
                    DWORD permissions,
                    ACCESS_MODE mode);

      EXPLICIT_ACCESS* entries;
      int entry_count;
    };

    // Helper class for handling SIDs
    struct Sid : std::vector<uint8_t> {
      Sid() {}
      operator PSID() const {return (PSID)data();}

      static PSID copySID(const PSID sid);

      void setSID(const PSID sid);

      void getUserNameAndDomain(TCHAR** name, TCHAR** domain);

      struct Administrators;
      struct SYSTEM;
      struct FromToken;

    private:
      Sid(const Sid&);
      Sid& operator=(const Sid&);
    };
      
    struct Sid::Administrators : public Sid {
      Administrators();
    };
    struct Sid::SYSTEM : public Sid {
      SYSTEM();
    };
    struct Sid::FromToken : public Sid {
      FromToken(HANDLE h);
    };

    // Helper class for handling & freeing ACLs
    struct AccessControlList : public LocalMem {
      AccessControlList(int size) : LocalMem(size) {}
      AccessControlList(PACL acl_=0) : LocalMem(acl_) {}
      operator PACL() {return (PACL)ptr;}
    };

    // Create a new ACL based on supplied entries and, if supplied, existing ACL 
    PACL CreateACL(const AccessEntries& ae, PACL existing_acl=0);

    // Helper class for memory-management of self-relative SecurityDescriptors
    struct SecurityDescriptorPtr : LocalMem {
      SecurityDescriptorPtr(int size) : LocalMem(size) {}
      SecurityDescriptorPtr(PSECURITY_DESCRIPTOR sd_=0) : LocalMem(sd_) {}
      PSECURITY_DESCRIPTOR takeSD() {return (PSECURITY_DESCRIPTOR)takePtr();}
    };

    // Create a new self-relative Security Descriptor, owned by SYSTEM/Administrators,
    //   with the supplied DACL and no SACL.  The returned value can be assigned
    //   to a SecurityDescriptorPtr to be managed.
    PSECURITY_DESCRIPTOR CreateSdWithDacl(const PACL dacl);

  }

}

#endif