pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS

Difference between
modified post b668b4 by Anonymous on Fri 24th May 2024 14:03
original post 928609bd by Anonymous on Fri 24th May 2024 13:38
Download diff
Show old version | new version | both versions

    
11
22
/*
33
 * MIT License
44
 *
55
 * Copyright (c) 2024 Roland Mainz <roland.mainz@nrubsig.org>
66
 *
77
 * Permission is hereby granted, free of charge, to any person obtaining a copy
88
 * of this software and associated documentation files (the "Software"), to deal
99
 * in the Software without restriction, including without limitation the rights
1010
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1111
 * copies of the Software, and to permit persons to whom the Software is
1212
 * furnished to do so, subject to the following conditions:
1313
 *
1414
 * The above copyright notice and this permission notice shall be included in all
1515
 * copies or substantial portions of the Software.
1616
 *
1717
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1818
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1919
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2020
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2121
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2222
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
 * SOFTWARE.
2424
 */
2525
2626
/*
27-
 * winsg.c - run Win32 or Cygwin program with a different group
27+
 * winsg.c - run Win32 or Cygwin program with a different (primary) group
2828
 *
2929
 * Written by Roland Mainz <roland.mainz@nrubsig.org>
3030
 */
3131
3232
#include <windows.h>
3333
#include <stdio.h>
3434
#include <stdbool.h>
3535
#include <Lmcons.h>
3636
#include <process.h>
3737
38+
#if 0
3844
/*
3945
 * Performance hack:
4046
 * GETTOKINFO_EXTRA_BUFFER - extra space for more data
4147
 * |GetTokenInformation()| for |TOKEN_USER| and |TOKEN_PRIMARY_GROUP|
4248
 * always fails in Win10 with |ERROR_INSUFFICIENT_BUFFER| if you
4349
 * just pass the |sizeof(TOKEN_*)| value. Instead of calling
4450
 * |GetTokenInformation()| with |NULL| arg to obtain the size to
4551
 * allocate we just provide 2048 bytes of extra space after the
4652
 * |TOKEN_*| size, and pray it is enough
4753
 */
4854
#define GETTOKINFO_EXTRA_BUFFER (2048)
4955
5056
bool get_token_primarygroup_name(HANDLE tok, char *out_buffer)
5157
{
5258
    DWORD tokdatalen;
5359
    PTOKEN_PRIMARY_GROUP ptpgroup;
5460
    PSID pgsid;
5561
    DWORD namesize = GNLEN+1;
5662
    char domainbuffer[UNLEN+1];
5763
    DWORD domainbuffer_size = sizeof(domainbuffer);
5864
    SID_NAME_USE name_use;
5965
6066
    tokdatalen = sizeof(TOKEN_PRIMARY_GROUP)+GETTOKINFO_EXTRA_BUFFER;
6167
    ptpgroup = _alloca(tokdatalen);
6268
    if (!GetTokenInformation(tok, TokenPrimaryGroup, ptpgroup,
6369
        tokdatalen, &tokdatalen)) {
64-
        (void)fprintf(stderr, "get_token_primarygroup_name: "
70+
        D((void)fprintf(stderr, "get_token_primarygroup_name: "
6571
            "GetTokenInformation(tok=0x%p, TokenPrimaryGroup) failed, "
6672
            "status=%d\n",
67-
            (void *)tok, (int)GetLastError());
73+
            (void *)tok, (int)GetLastError()));
6874
        return false;
6975
    }
7076
7177
    pgsid = ptpgroup->PrimaryGroup;
7278
7379
    if (!LookupAccountSidA(NULL, pgsid, out_buffer, &namesize,
7480
        domainbuffer, &domainbuffer_size, &name_use)) {
75-
        (void)fprintf(stderr, "get_token_primarygroup_name: "
81+
        D((void)fprintf(stderr, "get_token_primarygroup_name: "
7682
            "LookupAccountSidA() failed, status=%d\n",
77-
            (int)GetLastError());
83+
            (int)GetLastError()));
7884
        return false;
7985
    }
8086
8187
    return true;
8288
}
8389
8490
bool is_group_in_token(HANDLE tok, PSID qsid)
8591
{
8692
    DWORD tokdatalen;
8793
    PTOKEN_GROUPS ptgroups;
8894
8995
    tokdatalen = sizeof(TOKEN_GROUPS)+GETTOKINFO_EXTRA_BUFFER;
9096
    ptgroups = _alloca(tokdatalen);
9197
    if (!GetTokenInformation(tok, TokenGroups, ptgroups,
9298
        tokdatalen, &tokdatalen)) {
93-
        (void)fprintf(stderr, "is_group_in_token: "
99+
        D((void)fprintf(stderr, "is_group_in_token: "
94100
            "GetTokenInformation(tok=0x%p, TokenGroups) failed, "
95101
            "status=%d\n",
96-
            (void *)tok, (int)GetLastError());
102+
            (void *)tok, (int)GetLastError()));
97103
        return false;
98104
    }
99105
100106
    int i;
101-
    (void)fprintf(stderr, "is_group_in_token: got %d groups\n", (int)ptgroups->GroupCount);
107+
    D((void)fprintf(stderr, "is_group_in_token: got %d groups\n", (int)ptgroups->GroupCount));
102108
    for (i = 0 ; i < ptgroups->GroupCount ; i++) {
103109
        if (EqualSid(qsid, ptgroups->Groups[i].Sid) &&
104110
            (ptgroups->Groups[i].Attributes & SE_GROUP_ENABLED)) {
105-
            (void)puts("#match");
111+
            D((void)puts("#match"));
106112
            return true;
107113
        }
108114
    }
109115
110-
    (void)puts("#no match");
116+
    D((void)puts("#no match"));
111117
112118
    return false;
113119
}
114120
115121
bool set_token_primarygroup_name(HANDLE tok, const char *new_groupname)
116122
{
117123
    DWORD tokdatalen;
118124
    TOKEN_PRIMARY_GROUP tpgroup;
119125
    char sidbuff[SECURITY_MAX_SID_SIZE+1];
120126
    PSID pgsid = (PSID)sidbuff;
121127
    DWORD pgsid_size = sizeof(sidbuff);
122128
    char domainbuffer[UNLEN+1];
123129
    DWORD domainbuffer_size = sizeof(domainbuffer);
124130
    SID_NAME_USE name_use;
125131
126132
    if (!LookupAccountNameA(NULL, new_groupname,
127133
        pgsid, &pgsid_size, domainbuffer, &domainbuffer_size, &name_use)) {
128-
        (void)fprintf(stderr, "set_token_primarygroup_name: "
134+
        D((void)fprintf(stderr, "set_token_primarygroup_name: "
129-
            "LookupAccountNameA() failed.\n");
135+
            "LookupAccountNameA() failed.\n"));
130136
        return false;
131137
    }
132138
134139
    if (!is_group_in_token(tok, pgsid)) {
135-
        (void)fprintf(stderr, "set_token_primarygroup_name: "
140+
        D((void)fprintf(stderr, "set_token_primarygroup_name: "
136-
            "group '%s' is not a member.\n", new_groupname);
141+
            "group '%s' is not a member.\n", new_groupname));
137142
        return false;
138143
    }
144+

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at