Difference between
modified post 97daa63 by Anonymous on Sat 25th May 2024 11:00
original post 3da241c by Anonymous on Fri 24th May 2024 16:30
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 | 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 | /* | |
| 33 | 33 | * Compile with: | |
| 34 | 34 | * $ clang -target x86_64-pc-windows-gnu -Wall -DUNICODE=1 -D_UNICODE=1 -g winsg.c -o winsg.exe # | |
| 35 | 35 | */ | |
| 36 | 36 | ||
| 37 | 37 | #include <windows.h> | |
| 38 | 38 | #include <stdio.h> | |
| 39 | 39 | #include <stdbool.h> | |
| 40 | 40 | #include <assert.h> | |
| 41 | 41 | #include <Lmcons.h> | |
| 42 | 42 | #include <process.h> | |
| 43 | 43 | ||
| 44 | 44 | #if 0 | |
| 45 | 45 | #define D(x) x | |
| 46 | 46 | #else | |
| 47 | 47 | #define D(x) | |
| 48 | 48 | #endif | |
| 49 | 49 | ||
| 50 | 50 | #ifdef _WIN64 | |
| 51 | 51 | #define CYGWIN_BASH_PATH "C:\\cygwin64\\bin\\bash.exe" | |
| 52 | 52 | #else | |
| 53 | 53 | #define CYGWIN_BASH_PATH "C:\\cygwin\\bin\\bash.exe" | |
| 54 | 54 | #endif /* _WIN64 */ | |
| 55 | 55 | #define WIN32_CMDEXE_PATH "C:\\Windows\\system32\\cmd.exe" | |
| 56 | 56 | ||
| 57 | 57 | /* | |
| 58 | + | * DECLARE_SID_BUFFER - declare a buffer for a SID value | |
| 58 | 67 | * Performance hack: | |
| 59 | 68 | * GETTOKINFO_EXTRA_BUFFER - extra space for more data | |
| 60 | 69 | * |GetTokenInformation()| for |TOKEN_USER| and |TOKEN_PRIMARY_GROUP| | |
| 61 | 70 | * always fails in Win10 with |ERROR_INSUFFICIENT_BUFFER| if you | |
| 62 | 71 | * just pass the |sizeof(TOKEN_*)| value. Instead of calling | |
| 63 | 72 | * |GetTokenInformation()| with |NULL| arg to obtain the size to | |
| 64 | 73 | * allocate we just provide 2048 bytes of extra space after the | |
| 65 | 74 | * |TOKEN_*| size, and pray it is enough | |
| 66 | 75 | */ | |
| 67 | 76 | #define GETTOKINFO_EXTRA_BUFFER (2048) | |
| 68 | 77 | ||
| 69 | 78 | D( | |
| 70 | 79 | static | |
| 71 | 80 | bool get_token_primarygroup_name(HANDLE tok, char *out_buffer) | |
| 72 | 81 | { | |
| 73 | 82 | DWORD tokdatalen; | |
| 74 | 83 | PTOKEN_PRIMARY_GROUP ptpgroup; | |
| 75 | 84 | PSID pgsid; | |
| 76 | 85 | DWORD namesize = GNLEN+1; | |
| 77 | 86 | char domainbuffer[UNLEN+1]; | |
| 78 | 87 | DWORD domainbuffer_size = sizeof(domainbuffer); | |
| 79 | 88 | SID_NAME_USE name_use; | |
| 80 | 89 | ||
| 81 | 90 | tokdatalen = sizeof(TOKEN_PRIMARY_GROUP)+GETTOKINFO_EXTRA_BUFFER; | |
| 82 | 91 | ptpgroup = _alloca(tokdatalen); | |
| 83 | 92 | if (!GetTokenInformation(tok, TokenPrimaryGroup, ptpgroup, | |
| 84 | 93 | tokdatalen, &tokdatalen)) { | |
| 85 | 94 | D((void)fprintf(stderr, "get_token_primarygroup_name: " | |
| 86 | 95 | "GetTokenInformation(tok=0x%p, TokenPrimaryGroup) failed, " | |
| 87 | 96 | "status=%d.\n", | |
| 88 | 97 | (void *)tok, (int)GetLastError())); | |
| 89 | 98 | return false; | |
| 90 | 99 | } | |
| 91 | 100 | ||
| 92 | 101 | pgsid = ptpgroup->PrimaryGroup; | |
| 93 | 102 | ||
| 94 | 103 | if (!LookupAccountSidA(NULL, pgsid, out_buffer, &namesize, | |
| 95 | 104 | domainbuffer, &domainbuffer_size, &name_use)) { | |
| 96 | 105 | D((void)fprintf(stderr, "get_token_primarygroup_name: " | |
| 97 | 106 | "LookupAccountSidA() failed, status=%d.\n", | |
| 98 | 107 | (int)GetLastError())); | |
| 99 | 108 | return false; | |
| 100 | 109 | } | |
| 101 | 110 | ||
| 102 | 111 | return true; | |
| 103 | 112 | } | |
| 104 | 113 | ) | |
| 105 | 114 | ||
| 106 | 115 | static | |
| 107 | 116 | bool is_group_in_token(HANDLE tok, PSID qsid) | |
| 108 | 117 | { | |
| 109 | 118 | DWORD tokdatalen; | |
| 110 | 119 | PTOKEN_GROUPS ptgroups; | |
| 111 | 120 | ||
| 112 | 121 | tokdatalen = sizeof(TOKEN_GROUPS)+GETTOKINFO_EXTRA_BUFFER; | |
| 113 | 122 | ptgroups = _alloca(tokdatalen); | |
| 114 | 123 | if (!GetTokenInformation(tok, TokenGroups, ptgroups, | |
| 115 | 124 | tokdatalen, &tokdatalen)) { | |
| 116 | 125 | D((void)fprintf(stderr, "is_group_in_token: " | |
| 117 | 126 | "GetTokenInformation(tok=0x%p, TokenGroups) failed, " | |
| 118 | 127 | "status=%d.\n", | |
| 119 | 128 | (void *)tok, (int)GetLastError())); | |
| 120 | 129 | return false; | |
| 121 | 130 | } | |
| 122 | 131 | ||
| 123 | 132 | int i; | |
| 124 | 133 | D( | |
| 125 | 134 | (void)fprintf(stderr, "is_group_in_token: got %d groups\n", | |
| 126 | 135 | (int)ptgroups->GroupCount) | |
| 127 | 136 | ); | |
| 128 | 137 | for (i = 0 ; i < ptgroups->GroupCount ; i++) { | |
| 129 | 138 | if (EqualSid(qsid, ptgroups->Groups[i].Sid) && | |
| 130 | 139 | (ptgroups->Groups[i].Attributes & SE_GROUP_ENABLED)) { | |
| 131 | 140 | D((void)puts("is_group_in_token: #match")); | |
| 132 | 141 | return true; | |
| 133 | 142 | } | |
| 134 | 143 | } | |
| 135 | 144 | ||
| 136 | 145 | D((void)puts("is_group_in_token: #no match")); | |
| 137 | 146 | ||
| 138 | 147 | return false; | |
| 139 | 148 | } | |
| 140 | 149 | ||
| 141 | 150 | static | |
| 142 | 151 | bool get_group_sid(const char *groupname, PSID pgsid, PDWORD pgsid_size) | |
| 143 | 152 | { | |
| 144 | 153 | char domainbuffer[UNLEN+1]; | |
| 145 | 154 | DWORD domainbuffer_size = sizeof(domainbuffer); | |
| 146 | 155 | SID_NAME_USE name_use; | |
| 147 | 156 | ||
| 148 | 157 | if (!LookupAccountNameA(NULL, groupname, | |
| 149 | 158 | pgsid, pgsid_size, domainbuffer, &domainbuffer_size, &name_use)) { | |
| 150 | 159 | D((void)fprintf(stderr, "get_group_sid: " | |
| 151 | 160 | "LookupAccountNameA() failed.\n")); | |
| 152 | 161 | return false; | |
| 153 | 162 | } | |
| 154 | 163 | ||
| 155 | 164 | return true; | |
| 156 | 165 | } | |
| 157 | 166 | ||
| 158 | 167 | static | |
| 159 | 168 | bool set_token_primarygroup_sid(HANDLE tok, PSID pgsid) | |
| 160 | 169 | { | |
| 161 | 170 | DWORD tokdatalen; | |
| 162 | 171 | TOKEN_PRIMARY_GROUP tpgroup; | |
| 163 | 172 | ||
| 164 | 173 | tokdatalen = sizeof(TOKEN_PRIMARY_GROUP); | |
| 165 | 174 | tpgroup.PrimaryGroup = pgsid; | |
| 166 | 175 | if (!SetTokenInformation(tok, TokenPrimaryGroup, | |
| 167 | 176 | &tpgroup, tokdatalen)) { | |
| 168 | 177 | D((void)fprintf(stderr, "set_token_primarygroup_sid: " | |
| 169 | 178 | "SetTokenInformation(tok=0x%p, TokenPrimaryGroup) failed, " | |
| 170 | 179 | "status=%d\n", | |
| 171 | 180 | (void *)tok, (int)GetLastError())); | |
| 172 | 181 | return false; | |
| 173 | 182 | } | |
| 174 | 183 | ||
| 175 | 184 | return true; | |
| 176 | 185 | } | |
| 177 | 186 | ||
| 178 | 187 | static | |
| 179 | - | char *stpcpy (char *s1, const char *s2) | |
| 188 | + | char *stpcpy (char *restrict s1, const char *restrict s2) | |
| 180 | 189 | { | |
| 181 | 190 | size_t l = strlen(s2); | |
| 182 | 191 | return memcpy(s1, s2, l+1) + l; | |
| 183 | 192 | } | |
| 184 | 193 | ||
| 185 | 194 | static | |
| 186 | 195 | void win32cmd_quotearg(char *s1, const char *s2) | |
| 187 | 196 | { | |
| 188 | 197 | int c; | |
| 189 | 198 | *s1++ = '"'; | |
| 190 | 199 | while ((c = *s2) != '\0') { | |
| 191 | 200 | switch(c) { | |
| 192 | 201 | case '"': | |
| 193 | 202 | *s1++='\\'; | |
| 194 | 203 | *s1 = c; | |
| 195 | 204 | break; | |
| 196 | 205 | default: | |
| 197 | 206 | *s1 = c; | |
| 198 | 207 | break; | |
| 199 | 208 | } | |
| 200 | 209 | s1++; | |
| 201 | 210 | s2++; | |
| 202 | 211 | } | |
| 203 | 212 | *s1++ = '"'; | |
| 204 | 213 | *s1 = '\0'; | |
| 205 | 214 | } | |
| 206 | 215 | ||
| 207 | - | ||
| 216 | + | static | |
| 208 | - | enum shelltype { | |
| 217 | + | int usage(void) | |
| 216 | 218 | { | |
| 217 | - | enum shelltype st = SHELLTYPE_NOT_SET; | |
| 219 | + | (void)fprintf(stderr, "Usage: " | |
| 218 | - | int cmd_arg_index = -1; | |
| 220 | + | "winsg [-] -g group [-c command | /C command]]\n" | |
| 219 | - | const char *newgrpname = NULL; | |
| 221 | + | "Execute command as different primary group ID\n" | |
| 220 | - | HANDLE tok = INVALID_HANDLE_VALUE; | |
| 222 | + | "\n" | |
| 221 | - | int subcmdret = EXIT_FAILURE; | |
| 223 | + | "Examples:\n" | |
| 222 | - | int retval = 1; | |
| 224 | + | "\t1. Run new cmd.exe with primary group 'abc1':\n" | |
| 223 | - | int i; | |
| 225 | + | "\t\twinsg -g abc1 /C\n" | |
| 224 | - | ||
| 226 | + | "\n" | |
| 225 | - | for (i=1 ; i < ac ; i++) { | |
| 227 | + | "\t2. Run new Cygwin bash with primary group 'abc2':\n" | |
| 226 | - | D((void)fprintf(stderr, "# i=%d, av[i]='%s'\n", i, av[i])); | |
| 228 | + | "\t\twinsg -g abc2 /C\n" | |
| 227 | - | ||
| 229 | + | "\n" | |
| 228 | - | if (!strcmp(av[i], "-")) { | |
| 230 | + | "\t3. Start /bin/id from cmd.exe with primary group 'abc3':\n" | |
| 229 | - | (void)fprintf(stderr, "%s: " | |
| 231 | + | "\t\twinsg abc3 /C 'C:\\cygwin64\\bin\\id.exe -a'\n" | |
| 230 | - | "Run in new login not supported yet.\n", av[0]); | |
| 232 | + | "\n" | |
| 231 | - | retval = 1; | |
| 233 | + | "\t4. Start /bin/id from Cygwin bash with primary group " | |
| 232 | - | goto done; | |
| 234 | + | "'abc4':\n" | |
| 235 | + | "\t\twinsg abc4 -c '/bin/id.exe -a'\n" | |
| 233 | 241 | } | |
| 242 | + |
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