| 1 |
/* |
| 2 |
* tclWinTest.c -- |
| 3 |
* |
| 4 |
* Contains commands for platform specific tests on Windows. |
| 5 |
* |
| 6 |
* Copyright (c) 1996 Sun Microsystems, Inc. |
| 7 |
* |
| 8 |
* See the file "license.terms" for information on usage and redistribution of |
| 9 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 10 |
*/ |
| 11 |
|
| 12 |
#ifndef USE_TCL_STUBS |
| 13 |
# define USE_TCL_STUBS |
| 14 |
#endif |
| 15 |
#include "tclInt.h" |
| 16 |
|
| 17 |
/* |
| 18 |
* For TestplatformChmod on Windows |
| 19 |
*/ |
| 20 |
#include <aclapi.h> |
| 21 |
#include <sddl.h> |
| 22 |
|
| 23 |
/* |
| 24 |
* MinGW 3.4.2 does not define this. |
| 25 |
*/ |
| 26 |
#ifndef INHERITED_ACE |
| 27 |
#define INHERITED_ACE (0x10) |
| 28 |
#endif |
| 29 |
|
| 30 |
/* |
| 31 |
* Forward declarations of functions defined later in this file: |
| 32 |
*/ |
| 33 |
|
| 34 |
static Tcl_ObjCmdProc TesteventloopCmd; |
| 35 |
static Tcl_ObjCmdProc TestvolumetypeCmd; |
| 36 |
static Tcl_ObjCmdProc TestwinclockCmd; |
| 37 |
static Tcl_ObjCmdProc TestwinsleepCmd; |
| 38 |
static Tcl_ObjCmdProc TestExceptionCmd; |
| 39 |
static int TestplatformChmod(const char *nativePath, int pmode); |
| 40 |
static Tcl_ObjCmdProc TestchmodCmd; |
| 41 |
|
| 42 |
/* |
| 43 |
*---------------------------------------------------------------------- |
| 44 |
* |
| 45 |
* TclplatformtestInit -- |
| 46 |
* |
| 47 |
* Defines commands that test platform specific functionality for Windows |
| 48 |
* platforms. |
| 49 |
* |
| 50 |
* Results: |
| 51 |
* A standard Tcl result. |
| 52 |
* |
| 53 |
* Side effects: |
| 54 |
* Defines new commands. |
| 55 |
* |
| 56 |
*---------------------------------------------------------------------- |
| 57 |
*/ |
| 58 |
|
| 59 |
int |
| 60 |
TclplatformtestInit( |
| 61 |
Tcl_Interp *interp) /* Interpreter to add commands to. */ |
| 62 |
{ |
| 63 |
/* |
| 64 |
* Add commands for platform specific tests for Windows here. |
| 65 |
*/ |
| 66 |
|
| 67 |
Tcl_CreateObjCommand(interp, "testchmod", TestchmodCmd, NULL, NULL); |
| 68 |
Tcl_CreateObjCommand(interp, "testeventloop", TesteventloopCmd, NULL, NULL); |
| 69 |
Tcl_CreateObjCommand(interp, "testvolumetype", TestvolumetypeCmd, |
| 70 |
NULL, NULL); |
| 71 |
Tcl_CreateObjCommand(interp, "testwinclock", TestwinclockCmd, NULL, NULL); |
| 72 |
Tcl_CreateObjCommand(interp, "testwinsleep", TestwinsleepCmd, NULL, NULL); |
| 73 |
Tcl_CreateObjCommand(interp, "testexcept", TestExceptionCmd, NULL, NULL); |
| 74 |
return TCL_OK; |
| 75 |
} |
| 76 |
|
| 77 |
/* |
| 78 |
*---------------------------------------------------------------------- |
| 79 |
* |
| 80 |
* TesteventloopCmd -- |
| 81 |
* |
| 82 |
* This function implements the "testeventloop" command. It is used to |
| 83 |
* test the Tcl notifier from an "external" event loop (i.e. not |
| 84 |
* Tcl_DoOneEvent()). |
| 85 |
* |
| 86 |
* Results: |
| 87 |
* A standard Tcl result. |
| 88 |
* |
| 89 |
* Side effects: |
| 90 |
* None. |
| 91 |
* |
| 92 |
*---------------------------------------------------------------------- |
| 93 |
*/ |
| 94 |
|
| 95 |
static int |
| 96 |
TesteventloopCmd( |
| 97 |
void *clientData, /* Not used. */ |
| 98 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 99 |
int objc, /* Number of arguments. */ |
| 100 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 101 |
{ |
| 102 |
static int *framePtr = NULL;/* Pointer to integer on stack frame of |
| 103 |
* innermost invocation of the "wait" |
| 104 |
* subcommand. */ |
| 105 |
(void)clientData; |
| 106 |
|
| 107 |
if (objc < 2) { |
| 108 |
Tcl_WrongNumArgs(interp, 1, objv, "option ..."); |
| 109 |
return TCL_ERROR; |
| 110 |
} |
| 111 |
if (strcmp(Tcl_GetString(objv[1]), "done") == 0) { |
| 112 |
*framePtr = 1; |
| 113 |
} else if (strcmp(Tcl_GetString(objv[1]), "wait") == 0) { |
| 114 |
int *oldFramePtr, done; |
| 115 |
int oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL); |
| 116 |
|
| 117 |
/* |
| 118 |
* Save the old stack frame pointer and set up the current frame. |
| 119 |
*/ |
| 120 |
|
| 121 |
oldFramePtr = framePtr; |
| 122 |
framePtr = &done; |
| 123 |
|
| 124 |
/* |
| 125 |
* Enter a standard Windows event loop until the flag changes. Note |
| 126 |
* that we do not explicitly call Tcl_ServiceEvent(). |
| 127 |
*/ |
| 128 |
|
| 129 |
done = 0; |
| 130 |
while (!done) { |
| 131 |
MSG msg; |
| 132 |
|
| 133 |
if (!GetMessageW(&msg, NULL, 0, 0)) { |
| 134 |
/* |
| 135 |
* The application is exiting, so repost the quit message and |
| 136 |
* start unwinding. |
| 137 |
*/ |
| 138 |
|
| 139 |
PostQuitMessage((int) msg.wParam); |
| 140 |
break; |
| 141 |
} |
| 142 |
TranslateMessage(&msg); |
| 143 |
DispatchMessageW(&msg); |
| 144 |
} |
| 145 |
(void) Tcl_SetServiceMode(oldMode); |
| 146 |
framePtr = oldFramePtr; |
| 147 |
} else { |
| 148 |
Tcl_AppendResult(interp, "bad option \"", Tcl_GetString(objv[1]), |
| 149 |
"\": must be done or wait", (char *)NULL); |
| 150 |
return TCL_ERROR; |
| 151 |
} |
| 152 |
return TCL_OK; |
| 153 |
} |
| 154 |
|
| 155 |
/* |
| 156 |
*---------------------------------------------------------------------- |
| 157 |
* |
| 158 |
* Testvolumetype -- |
| 159 |
* |
| 160 |
* This function implements the "testvolumetype" command. It is used to |
| 161 |
* check the volume type (FAT, NTFS) of a volume. |
| 162 |
* |
| 163 |
* Results: |
| 164 |
* A standard Tcl result. |
| 165 |
* |
| 166 |
* Side effects: |
| 167 |
* None. |
| 168 |
* |
| 169 |
*---------------------------------------------------------------------- |
| 170 |
*/ |
| 171 |
|
| 172 |
static int |
| 173 |
TestvolumetypeCmd( |
| 174 |
void *clientData, /* Not used. */ |
| 175 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 176 |
int objc, /* Number of arguments. */ |
| 177 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 178 |
{ |
| 179 |
#define VOL_BUF_SIZE 32 |
| 180 |
int found; |
| 181 |
char volType[VOL_BUF_SIZE]; |
| 182 |
const char *path; |
| 183 |
(void)clientData; |
| 184 |
|
| 185 |
if (objc > 2) { |
| 186 |
Tcl_WrongNumArgs(interp, 1, objv, "?name?"); |
| 187 |
return TCL_ERROR; |
| 188 |
} |
| 189 |
if (objc == 2) { |
| 190 |
/* |
| 191 |
* path has to be really a proper volume, but we don't get query APIs |
| 192 |
* for that until NT5 |
| 193 |
*/ |
| 194 |
|
| 195 |
path = Tcl_GetString(objv[1]); |
| 196 |
} else { |
| 197 |
path = NULL; |
| 198 |
} |
| 199 |
found = GetVolumeInformationA(path, NULL, 0, NULL, NULL, NULL, volType, |
| 200 |
VOL_BUF_SIZE); |
| 201 |
|
| 202 |
if (found == 0) { |
| 203 |
Tcl_AppendResult(interp, "could not get volume type for \"", |
| 204 |
(path?path:""), "\"", (char *)NULL); |
| 205 |
TclWinConvertError(GetLastError()); |
| 206 |
return TCL_ERROR; |
| 207 |
} |
| 208 |
Tcl_AppendResult(interp, volType, (char *)NULL); |
| 209 |
return TCL_OK; |
| 210 |
#undef VOL_BUF_SIZE |
| 211 |
} |
| 212 |
|
| 213 |
/* |
| 214 |
*---------------------------------------------------------------------- |
| 215 |
* |
| 216 |
* TestwinclockCmd -- |
| 217 |
* |
| 218 |
* Command that returns the seconds and microseconds portions of the |
| 219 |
* system clock and of the Tcl clock so that they can be compared to |
| 220 |
* validate that the Tcl clock is staying in sync. |
| 221 |
* |
| 222 |
* Usage: |
| 223 |
* testclock |
| 224 |
* |
| 225 |
* Parameters: |
| 226 |
* None. |
| 227 |
* |
| 228 |
* Results: |
| 229 |
* Returns a standard Tcl result comprising a four-element list: the |
| 230 |
* seconds and microseconds portions of the system clock, and the seconds |
| 231 |
* and microseconds portions of the Tcl clock. |
| 232 |
* |
| 233 |
* Side effects: |
| 234 |
* None. |
| 235 |
* |
| 236 |
*---------------------------------------------------------------------- |
| 237 |
*/ |
| 238 |
|
| 239 |
static int |
| 240 |
TestwinclockCmd( |
| 241 |
void *dummy, /* Unused */ |
| 242 |
Tcl_Interp* interp, /* Tcl interpreter */ |
| 243 |
int objc, /* Argument count */ |
| 244 |
Tcl_Obj *const objv[]) /* Argument vector */ |
| 245 |
{ |
| 246 |
static const FILETIME posixEpoch = { 0xD53E8000, 0x019DB1DE }; |
| 247 |
/* The Posix epoch, expressed as a Windows |
| 248 |
* FILETIME */ |
| 249 |
Tcl_Time tclTime; /* Tcl clock */ |
| 250 |
FILETIME sysTime; /* System clock */ |
| 251 |
Tcl_Obj *result; /* Result of the command */ |
| 252 |
LARGE_INTEGER t1, t2; |
| 253 |
LARGE_INTEGER p1, p2; |
| 254 |
(void)dummy; |
| 255 |
|
| 256 |
if (objc != 1) { |
| 257 |
Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 258 |
return TCL_ERROR; |
| 259 |
} |
| 260 |
|
| 261 |
QueryPerformanceCounter(&p1); |
| 262 |
|
| 263 |
Tcl_GetTime(&tclTime); |
| 264 |
GetSystemTimeAsFileTime(&sysTime); |
| 265 |
t1.LowPart = posixEpoch.dwLowDateTime; |
| 266 |
t1.HighPart = posixEpoch.dwHighDateTime; |
| 267 |
t2.LowPart = sysTime.dwLowDateTime; |
| 268 |
t2.HighPart = sysTime.dwHighDateTime; |
| 269 |
t2.QuadPart -= t1.QuadPart; |
| 270 |
|
| 271 |
QueryPerformanceCounter(&p2); |
| 272 |
|
| 273 |
result = Tcl_NewObj(); |
| 274 |
Tcl_ListObjAppendElement(interp, result, |
| 275 |
Tcl_NewIntObj((int) (t2.QuadPart / 10000000))); |
| 276 |
Tcl_ListObjAppendElement(interp, result, |
| 277 |
Tcl_NewIntObj((int) ((t2.QuadPart / 10) % 1000000))); |
| 278 |
Tcl_ListObjAppendElement(interp, result, Tcl_NewIntObj(tclTime.sec)); |
| 279 |
Tcl_ListObjAppendElement(interp, result, Tcl_NewIntObj(tclTime.usec)); |
| 280 |
|
| 281 |
Tcl_ListObjAppendElement(interp, result, Tcl_NewWideIntObj(p1.QuadPart)); |
| 282 |
Tcl_ListObjAppendElement(interp, result, Tcl_NewWideIntObj(p2.QuadPart)); |
| 283 |
|
| 284 |
Tcl_SetObjResult(interp, result); |
| 285 |
|
| 286 |
return TCL_OK; |
| 287 |
} |
| 288 |
|
| 289 |
static int |
| 290 |
TestwinsleepCmd( |
| 291 |
void *clientData, /* Unused */ |
| 292 |
Tcl_Interp* interp, /* Tcl interpreter */ |
| 293 |
int objc, /* Parameter count */ |
| 294 |
Tcl_Obj *const * objv) /* Parameter vector */ |
| 295 |
{ |
| 296 |
int ms; |
| 297 |
(void)clientData; |
| 298 |
|
| 299 |
if (objc != 2) { |
| 300 |
Tcl_WrongNumArgs(interp, 1, objv, "ms"); |
| 301 |
return TCL_ERROR; |
| 302 |
} |
| 303 |
if (Tcl_GetIntFromObj(interp, objv[1], &ms) != TCL_OK) { |
| 304 |
return TCL_ERROR; |
| 305 |
} |
| 306 |
Sleep((DWORD) ms); |
| 307 |
return TCL_OK; |
| 308 |
} |
| 309 |
|
| 310 |
/* |
| 311 |
*---------------------------------------------------------------------- |
| 312 |
* |
| 313 |
* TestExceptionCmd -- |
| 314 |
* |
| 315 |
* Causes this process to end with the named exception. Used for testing |
| 316 |
* Tcl_WaitPid(). |
| 317 |
* |
| 318 |
* Usage: |
| 319 |
* testexcept <type> |
| 320 |
* |
| 321 |
* Parameters: |
| 322 |
* Type of exception. |
| 323 |
* |
| 324 |
* Results: |
| 325 |
* None, this process closes now and doesn't return. |
| 326 |
* |
| 327 |
* Side effects: |
| 328 |
* This Tcl process closes, hard... Bang! |
| 329 |
* |
| 330 |
*---------------------------------------------------------------------- |
| 331 |
*/ |
| 332 |
|
| 333 |
static int |
| 334 |
TestExceptionCmd( |
| 335 |
void *dummy, /* Unused */ |
| 336 |
Tcl_Interp* interp, /* Tcl interpreter */ |
| 337 |
int objc, /* Argument count */ |
| 338 |
Tcl_Obj *const objv[]) /* Argument vector */ |
| 339 |
{ |
| 340 |
static const char *const cmds[] = { |
| 341 |
"access_violation", "datatype_misalignment", "array_bounds", |
| 342 |
"float_denormal", "float_divbyzero", "float_inexact", |
| 343 |
"float_invalidop", "float_overflow", "float_stack", "float_underflow", |
| 344 |
"int_divbyzero", "int_overflow", "private_instruction", "inpageerror", |
| 345 |
"illegal_instruction", "noncontinue", "stack_overflow", |
| 346 |
"invalid_disp", "guard_page", "invalid_handle", "ctrl+c", |
| 347 |
NULL |
| 348 |
}; |
| 349 |
static const DWORD exceptions[] = { |
| 350 |
EXCEPTION_ACCESS_VIOLATION, EXCEPTION_DATATYPE_MISALIGNMENT, |
| 351 |
EXCEPTION_ARRAY_BOUNDS_EXCEEDED, EXCEPTION_FLT_DENORMAL_OPERAND, |
| 352 |
EXCEPTION_FLT_DIVIDE_BY_ZERO, EXCEPTION_FLT_INEXACT_RESULT, |
| 353 |
EXCEPTION_FLT_INVALID_OPERATION, EXCEPTION_FLT_OVERFLOW, |
| 354 |
EXCEPTION_FLT_STACK_CHECK, EXCEPTION_FLT_UNDERFLOW, |
| 355 |
EXCEPTION_INT_DIVIDE_BY_ZERO, EXCEPTION_INT_OVERFLOW, |
| 356 |
EXCEPTION_PRIV_INSTRUCTION, EXCEPTION_IN_PAGE_ERROR, |
| 357 |
EXCEPTION_ILLEGAL_INSTRUCTION, EXCEPTION_NONCONTINUABLE_EXCEPTION, |
| 358 |
EXCEPTION_STACK_OVERFLOW, EXCEPTION_INVALID_DISPOSITION, |
| 359 |
EXCEPTION_GUARD_PAGE, EXCEPTION_INVALID_HANDLE, CONTROL_C_EXIT |
| 360 |
}; |
| 361 |
int cmd; |
| 362 |
(void)dummy; |
| 363 |
|
| 364 |
if (objc != 2) { |
| 365 |
Tcl_WrongNumArgs(interp, 0, objv, "<type-of-exception>"); |
| 366 |
return TCL_ERROR; |
| 367 |
} |
| 368 |
if (Tcl_GetIndexFromObj(interp, objv[1], cmds, "command", 0, |
| 369 |
&cmd) != TCL_OK) { |
| 370 |
return TCL_ERROR; |
| 371 |
} |
| 372 |
|
| 373 |
/* |
| 374 |
* Make sure the GPF dialog doesn't popup. |
| 375 |
*/ |
| 376 |
|
| 377 |
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); |
| 378 |
|
| 379 |
/* |
| 380 |
* As Tcl does not handle structured exceptions, this falls all the way |
| 381 |
* back up the instruction stack to the C run-time portion that called |
| 382 |
* main() where the process will now be terminated with this exception |
| 383 |
* code by the default handler the C run-time provides. |
| 384 |
*/ |
| 385 |
|
| 386 |
/* SMASH! */ |
| 387 |
RaiseException(exceptions[cmd], EXCEPTION_NONCONTINUABLE, 0, NULL); |
| 388 |
|
| 389 |
return TCL_OK; |
| 390 |
} |
| 391 |
|
| 392 |
/* |
| 393 |
* This "chmod" works sufficiently for test script purposes. Do not expect |
| 394 |
* it to be exact emulation of Unix chmod (not sure if that's even possible) |
| 395 |
*/ |
| 396 |
static int |
| 397 |
TestplatformChmod( |
| 398 |
const char *nativePath, |
| 399 |
int pmode) |
| 400 |
{ |
| 401 |
/* |
| 402 |
* Note FILE_DELETE_CHILD missing from dirWriteMask because we do |
| 403 |
* not want overriding of child's delete setting when testing |
| 404 |
*/ |
| 405 |
static const DWORD dirWriteMask = |
| 406 |
FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | |
| 407 |
FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY | STANDARD_RIGHTS_WRITE | DELETE | |
| 408 |
SYNCHRONIZE; |
| 409 |
static const DWORD dirReadMask = |
| 410 |
FILE_READ_ATTRIBUTES | FILE_READ_EA | FILE_LIST_DIRECTORY | |
| 411 |
STANDARD_RIGHTS_READ | SYNCHRONIZE; |
| 412 |
/* Note - default user privileges allow ignoring TRAVERSE setting */ |
| 413 |
static const DWORD dirExecuteMask = |
| 414 |
FILE_TRAVERSE | STANDARD_RIGHTS_READ | SYNCHRONIZE; |
| 415 |
|
| 416 |
static const DWORD fileWriteMask = |
| 417 |
FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_WRITE_DATA | |
| 418 |
FILE_APPEND_DATA | STANDARD_RIGHTS_WRITE | DELETE | SYNCHRONIZE; |
| 419 |
static const DWORD fileReadMask = |
| 420 |
FILE_READ_ATTRIBUTES | FILE_READ_EA | FILE_READ_DATA | |
| 421 |
STANDARD_RIGHTS_READ | SYNCHRONIZE; |
| 422 |
static const DWORD fileExecuteMask = |
| 423 |
FILE_EXECUTE | STANDARD_RIGHTS_READ | SYNCHRONIZE; |
| 424 |
|
| 425 |
DWORD attr, newAclSize; |
| 426 |
PACL newAcl = NULL; |
| 427 |
int res = 0; |
| 428 |
|
| 429 |
HANDLE hToken = NULL; |
| 430 |
int i; |
| 431 |
int nSids = 0; |
| 432 |
struct { |
| 433 |
PSID pSid; |
| 434 |
DWORD mask; |
| 435 |
DWORD sidLen; |
| 436 |
} aceEntry[3]; |
| 437 |
DWORD dw; |
| 438 |
int isDir; |
| 439 |
TOKEN_USER *pTokenUser = NULL; |
| 440 |
Tcl_DString ds; |
| 441 |
|
| 442 |
res = -1; /* Assume failure */ |
| 443 |
|
| 444 |
Tcl_DStringInit(&ds); |
| 445 |
Tcl_UtfToExternalDString(NULL, nativePath, -1, &ds); |
| 446 |
|
| 447 |
attr = GetFileAttributesA(Tcl_DStringValue(&ds)); |
| 448 |
if (attr == 0xFFFFFFFF) { |
| 449 |
goto done; /* Not found */ |
| 450 |
} |
| 451 |
|
| 452 |
isDir = (attr & FILE_ATTRIBUTE_DIRECTORY) != 0; |
| 453 |
|
| 454 |
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) { |
| 455 |
goto done; |
| 456 |
} |
| 457 |
|
| 458 |
/* Get process SID */ |
| 459 |
if (!GetTokenInformation(hToken, TokenUser, NULL, 0, &dw) |
| 460 |
&& GetLastError() != ERROR_INSUFFICIENT_BUFFER) { |
| 461 |
goto done; |
| 462 |
} |
| 463 |
pTokenUser = (TOKEN_USER *)ckalloc(dw); |
| 464 |
if (!GetTokenInformation(hToken, TokenUser, pTokenUser, dw, &dw)) { |
| 465 |
goto done; |
| 466 |
} |
| 467 |
aceEntry[nSids].sidLen = GetLengthSid(pTokenUser->User.Sid); |
| 468 |
aceEntry[nSids].pSid = (PSID)ckalloc(aceEntry[nSids].sidLen); |
| 469 |
if (!CopySid(aceEntry[nSids].sidLen, aceEntry[nSids].pSid, |
| 470 |
pTokenUser->User.Sid)) { |
| 471 |
ckfree(aceEntry[nSids].pSid); /* Since we have not ++'ed nSids */ |
| 472 |
goto done; |
| 473 |
} |
| 474 |
/* |
| 475 |
* Always include DACL modify rights so we don't get locked out |
| 476 |
*/ |
| 477 |
aceEntry[nSids].mask = READ_CONTROL | WRITE_DAC | WRITE_OWNER | SYNCHRONIZE | |
| 478 |
FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES; |
| 479 |
if (pmode & 0700) { |
| 480 |
/* Owner permissions. Assumes current process is owner */ |
| 481 |
if (pmode & 0400) { |
| 482 |
aceEntry[nSids].mask |= isDir ? dirReadMask : fileReadMask; |
| 483 |
} |
| 484 |
if (pmode & 0200) { |
| 485 |
aceEntry[nSids].mask |= isDir ? dirWriteMask : fileWriteMask; |
| 486 |
} |
| 487 |
if (pmode & 0100) { |
| 488 |
aceEntry[nSids].mask |= isDir ? dirExecuteMask : fileExecuteMask; |
| 489 |
} |
| 490 |
} |
| 491 |
++nSids; |
| 492 |
|
| 493 |
if (pmode & 0070) { |
| 494 |
/* Group permissions. */ |
| 495 |
|
| 496 |
TOKEN_PRIMARY_GROUP *pTokenGroup; |
| 497 |
|
| 498 |
/* Get primary group SID */ |
| 499 |
if (!GetTokenInformation( |
| 500 |
hToken, TokenPrimaryGroup, NULL, 0, &dw) && |
| 501 |
GetLastError() != ERROR_INSUFFICIENT_BUFFER) { |
| 502 |
goto done; |
| 503 |
} |
| 504 |
pTokenGroup = (TOKEN_PRIMARY_GROUP *)ckalloc(dw); |
| 505 |
if (!GetTokenInformation(hToken, TokenPrimaryGroup, pTokenGroup, dw, &dw)) { |
| 506 |
ckfree(pTokenGroup); |
| 507 |
goto done; |
| 508 |
} |
| 509 |
aceEntry[nSids].sidLen = GetLengthSid(pTokenGroup->PrimaryGroup); |
| 510 |
aceEntry[nSids].pSid = (PSID)ckalloc(aceEntry[nSids].sidLen); |
| 511 |
if (!CopySid(aceEntry[nSids].sidLen, aceEntry[nSids].pSid, pTokenGroup->PrimaryGroup)) { |
| 512 |
ckfree(pTokenGroup); |
| 513 |
ckfree(aceEntry[nSids].pSid); /* Since we have not ++'ed nSids */ |
| 514 |
goto done; |
| 515 |
} |
| 516 |
ckfree(pTokenGroup); |
| 517 |
|
| 518 |
/* Generate mask for group ACL */ |
| 519 |
|
| 520 |
aceEntry[nSids].mask = 0; |
| 521 |
if (pmode & 0040) { |
| 522 |
aceEntry[nSids].mask |= isDir ? dirReadMask : fileReadMask; |
| 523 |
} |
| 524 |
if (pmode & 0020) { |
| 525 |
aceEntry[nSids].mask |= isDir ? dirWriteMask : fileWriteMask; |
| 526 |
} |
| 527 |
if (pmode & 0010) { |
| 528 |
aceEntry[nSids].mask |= isDir ? dirExecuteMask : fileExecuteMask; |
| 529 |
} |
| 530 |
++nSids; |
| 531 |
} |
| 532 |
|
| 533 |
if (pmode & 0007) { |
| 534 |
/* World permissions */ |
| 535 |
PSID pWorldSid; |
| 536 |
if (!ConvertStringSidToSidA("S-1-1-0", &pWorldSid)) { |
| 537 |
goto done; |
| 538 |
} |
| 539 |
aceEntry[nSids].sidLen = GetLengthSid(pWorldSid); |
| 540 |
aceEntry[nSids].pSid = (PSID)ckalloc(aceEntry[nSids].sidLen); |
| 541 |
if (!CopySid(aceEntry[nSids].sidLen, aceEntry[nSids].pSid, pWorldSid)) { |
| 542 |
LocalFree(pWorldSid); |
| 543 |
ckfree(aceEntry[nSids].pSid); /* Since we have not ++'ed nSids */ |
| 544 |
goto done; |
| 545 |
} |
| 546 |
LocalFree(pWorldSid); |
| 547 |
|
| 548 |
/* Generate mask for world ACL */ |
| 549 |
|
| 550 |
aceEntry[nSids].mask = 0; |
| 551 |
if (pmode & 0004) { |
| 552 |
aceEntry[nSids].mask |= isDir ? dirReadMask : fileReadMask; |
| 553 |
} |
| 554 |
if (pmode & 0002) { |
| 555 |
aceEntry[nSids].mask |= isDir ? dirWriteMask : fileWriteMask; |
| 556 |
} |
| 557 |
if (pmode & 0001) { |
| 558 |
aceEntry[nSids].mask |= isDir ? dirExecuteMask : fileExecuteMask; |
| 559 |
} |
| 560 |
++nSids; |
| 561 |
} |
| 562 |
|
| 563 |
/* Allocate memory and initialize the new ACL. */ |
| 564 |
|
| 565 |
newAclSize = sizeof(ACL); |
| 566 |
/* Add in size required for each ACE entry in the ACL */ |
| 567 |
for (i = 0; i < nSids; ++i) { |
| 568 |
newAclSize += |
| 569 |
TclOffset(ACCESS_ALLOWED_ACE, SidStart) + aceEntry[i].sidLen; |
| 570 |
} |
| 571 |
newAcl = (PACL)ckalloc(newAclSize); |
| 572 |
if (!InitializeAcl(newAcl, newAclSize, ACL_REVISION)) { |
| 573 |
goto done; |
| 574 |
} |
| 575 |
|
| 576 |
for (i = 0; i < nSids; ++i) { |
| 577 |
if (!AddAccessAllowedAce(newAcl, ACL_REVISION, aceEntry[i].mask, aceEntry[i].pSid)) { |
| 578 |
goto done; |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
/* |
| 583 |
* Apply the new ACL. Note PROTECTED_DACL_SECURITY_INFORMATION can be used |
| 584 |
* to remove inherited ACL (we need to overwrite the default ACL's in this case) |
| 585 |
*/ |
| 586 |
|
| 587 |
if (SetNamedSecurityInfoA((LPSTR)Tcl_DStringValue(&ds), SE_FILE_OBJECT, |
| 588 |
DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION, |
| 589 |
NULL, NULL, newAcl, NULL) == ERROR_SUCCESS) { |
| 590 |
res = 0; |
| 591 |
} |
| 592 |
|
| 593 |
done: |
| 594 |
if (pTokenUser) { |
| 595 |
ckfree(pTokenUser); |
| 596 |
} |
| 597 |
if (hToken) { |
| 598 |
CloseHandle(hToken); |
| 599 |
} |
| 600 |
if (newAcl) { |
| 601 |
ckfree(newAcl); |
| 602 |
} |
| 603 |
for (i = 0; i < nSids; ++i) { |
| 604 |
ckfree(aceEntry[i].pSid); |
| 605 |
} |
| 606 |
|
| 607 |
if (res == 0) { |
| 608 |
/* Run normal chmod command */ |
| 609 |
res = _chmod(Tcl_DStringValue(&ds), pmode); |
| 610 |
} |
| 611 |
Tcl_DStringFree(&ds); |
| 612 |
return res; |
| 613 |
} |
| 614 |
|
| 615 |
/* |
| 616 |
*--------------------------------------------------------------------------- |
| 617 |
* |
| 618 |
* TestchmodCmd -- |
| 619 |
* |
| 620 |
* Implements the "testchmod" cmd. Used when testing "file" command. The |
| 621 |
* only attribute used by the Windows platform is the user write flag; if |
| 622 |
* this is not set, the file is made read-only. Otherwise, the file is |
| 623 |
* made read-write. |
| 624 |
* |
| 625 |
* Results: |
| 626 |
* A standard Tcl result. |
| 627 |
* |
| 628 |
* Side effects: |
| 629 |
* Changes permissions of specified files. |
| 630 |
* |
| 631 |
*--------------------------------------------------------------------------- |
| 632 |
*/ |
| 633 |
|
| 634 |
static int |
| 635 |
TestchmodCmd( |
| 636 |
void *dummy, /* Not used. */ |
| 637 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 638 |
int objc, /* Parameter count */ |
| 639 |
Tcl_Obj *const * objv) /* Parameter vector */ |
| 640 |
{ |
| 641 |
int i, mode; |
| 642 |
(void)dummy; |
| 643 |
|
| 644 |
if (objc < 2) { |
| 645 |
Tcl_WrongNumArgs(interp, 1, objv, "mode file ?file ...?"); |
| 646 |
return TCL_ERROR; |
| 647 |
} |
| 648 |
|
| 649 |
if (Tcl_GetIntFromObj(interp, objv[1], &mode) != TCL_OK) { |
| 650 |
return TCL_ERROR; |
| 651 |
} |
| 652 |
|
| 653 |
for (i = 2; i < objc; i++) { |
| 654 |
Tcl_DString buffer; |
| 655 |
const char *translated; |
| 656 |
|
| 657 |
translated = Tcl_TranslateFileName(interp, Tcl_GetString(objv[i]), &buffer); |
| 658 |
if (translated == NULL) { |
| 659 |
return TCL_ERROR; |
| 660 |
} |
| 661 |
if (TestplatformChmod(translated, mode) != 0) { |
| 662 |
Tcl_AppendResult(interp, translated, ": ", Tcl_PosixError(interp), |
| 663 |
(char *)NULL); |
| 664 |
return TCL_ERROR; |
| 665 |
} |
| 666 |
Tcl_DStringFree(&buffer); |
| 667 |
} |
| 668 |
return TCL_OK; |
| 669 |
} |
| 670 |
|
| 671 |
/* |
| 672 |
* Local Variables: |
| 673 |
* mode: c |
| 674 |
* c-basic-offset: 4 |
| 675 |
* fill-column: 78 |
| 676 |
* End: |
| 677 |
*/ |