summaryrefslogtreecommitdiffstats
path: root/private/oleutest/act/common/reg.c
blob: 1c3ce233d8651aeb33e10a0142971516f67d2a1c (plain) (blame)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//
// reg.c
//
// Common registry manipulation routines.
//

#ifdef UNICODE
#define _UNICODE 1
#endif

#include <windows.h>
#include <ole2.h>
#include "acttest.h"
#include "tchar.h"
#ifndef CHICO
#include <subauth.h>
#include <ntlsa.h>
#endif

void DeleteSubTree( TCHAR * pszClsid, TCHAR * SubTree )
{
    HKEY        hClsidRoot;
    HKEY        hClsid;
    long        RegStatus;
    TCHAR       szKeyName[256]; 
    DWORD       KeyNameSize;
    FILETIME    FileTime;
    int         SubKey;

    RegStatus = RegOpenKeyEx( HKEY_CLASSES_ROOT,
                              SubTree,
                              0,
                              KEY_ALL_ACCESS,
                              &hClsidRoot );

    if ( RegStatus != ERROR_SUCCESS )
        return;

    RegStatus = RegOpenKeyEx( hClsidRoot,
                              pszClsid,
                              0,
                              KEY_ALL_ACCESS,
                              &hClsid );

    if ( RegStatus != ERROR_SUCCESS )
        return;

    for ( SubKey = 0; ; SubKey++ )
    {
        KeyNameSize = sizeof(szKeyName);

        RegStatus = RegEnumKeyEx(
                        hClsid,
                        SubKey,
                        szKeyName,
                        &KeyNameSize,
                        0,
                        NULL,
                        NULL,
                        &FileTime );

        if ( RegStatus != ERROR_SUCCESS )
            break;

        RegStatus = RegDeleteKey( hClsid, szKeyName );
    }

    RegCloseKey( hClsid );
    RegDeleteKey( hClsidRoot, pszClsid );
    RegCloseKey( hClsidRoot );
}


void DeleteClsidKey( TCHAR * pwszClsid )
{

    // Note that we also delete the corresponding AppID entries

    DeleteSubTree( pwszClsid, TEXT("CLSID"));
    DeleteSubTree( pwszClsid, TEXT("AppID"));
}

long SetAppIDSecurity( TCHAR * pszAppID )
{
    HKEY        hActKey;
    HKEY        hAppIDKey;
    BYTE        SecurityDescriptor[2000];
    LONG        RegStatus;
    SECURITY_INFORMATION        SI;
    DWORD       dwSize = sizeof( SecurityDescriptor );
    DWORD       Disposition;

    RegStatus = RegOpenKeyEx( HKEY_CLASSES_ROOT,
                              TEXT("AppID"),
                              0,
                              KEY_ALL_ACCESS,
                              &hAppIDKey );

    if ( RegStatus != ERROR_SUCCESS )
        return RegStatus;

    RegStatus  = RegCreateKeyEx(
                    hAppIDKey,
                    pszAppID,
                    0,
                    TEXT("REG_SZ"),
                    REG_OPTION_NON_VOLATILE,
                    KEY_ALL_ACCESS,
                    NULL,
                    &hActKey,
                    &Disposition );

    if ( RegStatus != ERROR_SUCCESS )
        return RegStatus;
#ifndef CHICO
    RegStatus  = RegGetKeySecurity( hActKey, 
                                    OWNER_SECURITY_INFORMATION 
                                        | GROUP_SECURITY_INFORMATION 
                                        | DACL_SECURITY_INFORMATION,
                                    &SecurityDescriptor,
                                    &dwSize );

    if ( RegStatus != ERROR_SUCCESS )
        return RegStatus;
#endif
    RegStatus  = RegSetValueEx(
                    hActKey,
                    TEXT("LaunchPermission"),
                    0,
                    REG_BINARY,
                    SecurityDescriptor,
                    dwSize );


    if ( RegStatus != ERROR_SUCCESS )
        return RegStatus;

    RegStatus  = RegSetValueEx(
                    hActKey,
                    TEXT("AccessPermission"),
                    0,
                    REG_BINARY,
                    SecurityDescriptor,
                    dwSize );


    if ( RegStatus != ERROR_SUCCESS )
        return RegStatus;

    RegCloseKey(hActKey);

    // make the key for the exe
    RegStatus  = RegCreateKeyEx(
                    hAppIDKey,
                    TEXT("ActSrv.Exe"),
                    0,
                    TEXT("REG_SZ"),
                    REG_OPTION_NON_VOLATILE,
                    KEY_ALL_ACCESS,
                    NULL,
                    &hActKey,
                    &Disposition );

    if ( RegStatus != ERROR_SUCCESS )
        return RegStatus;

    RegStatus  = RegSetValueEx(
                    hActKey,
                    TEXT("AppID"),
                    0,
                    REG_SZ,
                    (BYTE*) pszAppID,
                    (_tcslen(pszAppID) + 1) * sizeof(TCHAR) );


    if ( RegStatus != ERROR_SUCCESS )
        return RegStatus;

    RegCloseKey(hActKey);
    RegCloseKey(hAppIDKey);

    return ERROR_SUCCESS;
}



int SetAccountRights(const TCHAR *szUser, TCHAR *szPrivilege)
{
#ifndef CHICO
    int                   err;
    LSA_HANDLE            hPolicy;
    LSA_OBJECT_ATTRIBUTES objAtt;
    DWORD                 cbSid = 1;
    TCHAR                 szDomain[128];
    DWORD                 cbDomain = 128;
    PSID                  pSid = NULL;
    SID_NAME_USE          snu;
    LSA_UNICODE_STRING    privStr;

    // Get a policy handle
    memset(&objAtt, 0, sizeof(LSA_OBJECT_ATTRIBUTES));
    if (!NT_SUCCESS(LsaOpenPolicy(NULL,
                                  &objAtt,
                                  POLICY_CREATE_ACCOUNT | POLICY_LOOKUP_NAMES,
                                  &hPolicy)))
    {
        return GetLastError();
    }

    // Fetch the SID for the specified user
    LookupAccountName(NULL, szUser, pSid, &cbSid, szDomain, &cbDomain, &snu);
    if ((err = GetLastError()) != ERROR_INSUFFICIENT_BUFFER)
    {
        return err;
    }
    pSid = (PSID*) malloc(cbSid);
    if (pSid == NULL)
    {
        return ERROR_OUTOFMEMORY;
    }
    if (!LookupAccountName(NULL, szUser, pSid, &cbSid,
                          szDomain, &cbDomain, &snu))
    {
        return GetLastError();
    }

    // Set the specified privilege on this account
    privStr.Length = _tcslen(szPrivilege) * sizeof(WCHAR);
    privStr.MaximumLength = privStr.Length + sizeof(WCHAR);
    privStr.Buffer = szPrivilege;
    if (!NT_SUCCESS(LsaAddAccountRights(hPolicy, pSid, &privStr, 1)))
    {
        return GetLastError();
    }

    // We're done
    free( pSid );
    LsaClose(hPolicy);
#endif
    return ERROR_SUCCESS;
}

int AddBatchPrivilege(const TCHAR *szUser)
{
#ifndef CHICO
    return !SetAccountRights( szUser, SE_BATCH_LOGON_NAME );
#else
    return(TRUE);
#endif
}