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
| 1 | 1 | ||
| 2 | 2 | /* | |
| 3 | 3 | * MIT License | |
| 4 | 4 | * | |
| 5 | 5 | * Copyright (c) 2024 Roland Mainz <roland.mainz@nrubsig.org> | |
| 6 | 6 | * | |
| 7 | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 8 | 8 | * of this software and associated documentation files (the "Software"), to deal | |
| 9 | 9 | * in the Software without restriction, including without limitation the rights | |
| 10 | 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 11 | 11 | * copies of the Software, and to permit persons to whom the Software is | |
| 12 | 12 | * furnished to do so, subject to the following conditions: | |
| 13 | 13 | * | |
| 14 | 14 | * The above copyright notice and this permission notice shall be included in all | |
| 15 | 15 | * copies or substantial portions of the Software. | |
| 16 | 16 | * | |
| 17 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 18 | 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 19 | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 20 | 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 21 | 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 22 | 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| 23 | 23 | * SOFTWARE. | |
| 24 | 24 | */ | |
| 25 | 25 | ||
| 26 | 26 | /* | |
| 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 | |
| 28 | 28 | * | |
| 29 | 29 | * Written by Roland Mainz <roland.mainz@nrubsig.org> | |
| 30 | 30 | */ | |
| 31 | 31 | ||
| 32 | 32 | #include <windows.h> | |
| 33 | 33 | #include <stdio.h> | |
| 34 | 34 | #include <stdbool.h> | |
| 35 | 35 | #include <Lmcons.h> | |
| 36 | 36 | #include <process.h> | |
| 37 | 37 | ||
| 38 | + | #if 0 | |
| 38 | 44 | /* | |
| 39 | 45 | * Performance hack: | |
| 40 | 46 | * GETTOKINFO_EXTRA_BUFFER - extra space for more data | |
| 41 | 47 | * |GetTokenInformation()| for |TOKEN_USER| and |TOKEN_PRIMARY_GROUP| | |
| 42 | 48 | * always fails in Win10 with |ERROR_INSUFFICIENT_BUFFER| if you | |
| 43 | 49 | * just pass the |sizeof(TOKEN_*)| value. Instead of calling | |
| 44 | 50 | * |GetTokenInformation()| with |NULL| arg to obtain the size to | |
| 45 | 51 | * allocate we just provide 2048 bytes of extra space after the | |
| 46 | 52 | * |TOKEN_*| size, and pray it is enough | |
| 47 | 53 | */ | |
| 48 | 54 | #define GETTOKINFO_EXTRA_BUFFER (2048) | |
| 49 | 55 | ||
| 50 | 56 | bool get_token_primarygroup_name(HANDLE tok, char *out_buffer) | |
| 51 | 57 | { | |
| 52 | 58 | DWORD tokdatalen; | |
| 53 | 59 | PTOKEN_PRIMARY_GROUP ptpgroup; | |
| 54 | 60 | PSID pgsid; | |
| 55 | 61 | DWORD namesize = GNLEN+1; | |
| 56 | 62 | char domainbuffer[UNLEN+1]; | |
| 57 | 63 | DWORD domainbuffer_size = sizeof(domainbuffer); | |
| 58 | 64 | SID_NAME_USE name_use; | |
| 59 | 65 | ||
| 60 | 66 | tokdatalen = sizeof(TOKEN_PRIMARY_GROUP)+GETTOKINFO_EXTRA_BUFFER; | |
| 61 | 67 | ptpgroup = _alloca(tokdatalen); | |
| 62 | 68 | if (!GetTokenInformation(tok, TokenPrimaryGroup, ptpgroup, | |
| 63 | 69 | tokdatalen, &tokdatalen)) { | |
| 64 | - | (void)fprintf(stderr, "get_token_primarygroup_name: " | |
| 70 | + | D((void)fprintf(stderr, "get_token_primarygroup_name: " | |
| 65 | 71 | "GetTokenInformation(tok=0x%p, TokenPrimaryGroup) failed, " | |
| 66 | 72 | "status=%d\n", | |
| 67 | - | (void *)tok, (int)GetLastError()); | |
| 73 | + | (void *)tok, (int)GetLastError())); | |
| 68 | 74 | return false; | |
| 69 | 75 | } | |
| 70 | 76 | ||
| 71 | 77 | pgsid = ptpgroup->PrimaryGroup; | |
| 72 | 78 | ||
| 73 | 79 | if (!LookupAccountSidA(NULL, pgsid, out_buffer, &namesize, | |
| 74 | 80 | domainbuffer, &domainbuffer_size, &name_use)) { | |
| 75 | - | (void)fprintf(stderr, "get_token_primarygroup_name: " | |
| 81 | + | D((void)fprintf(stderr, "get_token_primarygroup_name: " | |
| 76 | 82 | "LookupAccountSidA() failed, status=%d\n", | |
| 77 | - | (int)GetLastError()); | |
| 83 | + | (int)GetLastError())); | |
| 78 | 84 | return false; | |
| 79 | 85 | } | |
| 80 | 86 | ||
| 81 | 87 | return true; | |
| 82 | 88 | } | |
| 83 | 89 | ||
| 84 | 90 | bool is_group_in_token(HANDLE tok, PSID qsid) | |
| 85 | 91 | { | |
| 86 | 92 | DWORD tokdatalen; | |
| 87 | 93 | PTOKEN_GROUPS ptgroups; | |
| 88 | 94 | ||
| 89 | 95 | tokdatalen = sizeof(TOKEN_GROUPS)+GETTOKINFO_EXTRA_BUFFER; | |
| 90 | 96 | ptgroups = _alloca(tokdatalen); | |
| 91 | 97 | if (!GetTokenInformation(tok, TokenGroups, ptgroups, | |
| 92 | 98 | tokdatalen, &tokdatalen)) { | |
| 93 | - | (void)fprintf(stderr, "is_group_in_token: " | |
| 99 | + | D((void)fprintf(stderr, "is_group_in_token: " | |
| 94 | 100 | "GetTokenInformation(tok=0x%p, TokenGroups) failed, " | |
| 95 | 101 | "status=%d\n", | |
| 96 | - | (void *)tok, (int)GetLastError()); | |
| 102 | + | (void *)tok, (int)GetLastError())); | |
| 97 | 103 | return false; | |
| 98 | 104 | } | |
| 99 | 105 | ||
| 100 | 106 | 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)); | |
| 102 | 108 | for (i = 0 ; i < ptgroups->GroupCount ; i++) { | |
| 103 | 109 | if (EqualSid(qsid, ptgroups->Groups[i].Sid) && | |
| 104 | 110 | (ptgroups->Groups[i].Attributes & SE_GROUP_ENABLED)) { | |
| 105 | - | (void)puts("#match"); | |
| 111 | + | D((void)puts("#match")); | |
| 106 | 112 | return true; | |
| 107 | 113 | } | |
| 108 | 114 | } | |
| 109 | 115 | ||
| 110 | - | (void)puts("#no match"); | |
| 116 | + | D((void)puts("#no match")); | |
| 111 | 117 | ||
| 112 | 118 | return false; | |
| 113 | 119 | } | |
| 114 | 120 | ||
| 115 | 121 | bool set_token_primarygroup_name(HANDLE tok, const char *new_groupname) | |
| 116 | 122 | { | |
| 117 | 123 | DWORD tokdatalen; | |
| 118 | 124 | TOKEN_PRIMARY_GROUP tpgroup; | |
| 119 | 125 | char sidbuff[SECURITY_MAX_SID_SIZE+1]; | |
| 120 | 126 | PSID pgsid = (PSID)sidbuff; | |
| 121 | 127 | DWORD pgsid_size = sizeof(sidbuff); | |
| 122 | 128 | char domainbuffer[UNLEN+1]; | |
| 123 | 129 | DWORD domainbuffer_size = sizeof(domainbuffer); | |
| 124 | 130 | SID_NAME_USE name_use; | |
| 125 | 131 | ||
| 126 | 132 | if (!LookupAccountNameA(NULL, new_groupname, | |
| 127 | 133 | 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")); | |
| 130 | 136 | return false; | |
| 131 | 137 | } | |
| 132 | 138 | ||
| 134 | 139 | 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)); | |
| 137 | 142 | return false; | |
| 138 | 143 | } | |
| 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.
nrubsig.kpaste.net RSS