aboutsummaryrefslogtreecommitdiff
path: root/win32
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2019-03-01 08:55:29 +0000
committerRon Yorston <rmy@pobox.com>2019-03-01 08:55:29 +0000
commit065ac35f8a9fb1470df67b67128559fd7e748a5e (patch)
tree8bc489f36405d25a98326bd13b0ba6614563ca7d /win32
parent0b0b57cee0aa454d1e91cac5f8af7ec3cc48c47c (diff)
downloadbusybox-w32-065ac35f8a9fb1470df67b67128559fd7e748a5e.tar.gz
busybox-w32-065ac35f8a9fb1470df67b67128559fd7e748a5e.tar.bz2
busybox-w32-065ac35f8a9fb1470df67b67128559fd7e748a5e.zip
win32: make stat(2) try to identify the owner of files
Extend stat(2) so it tries to determine whether a file belongs to the current user or not. If not it's said to belong to root.
Diffstat (limited to 'win32')
-rw-r--r--win32/mingw.c71
1 files changed, 64 insertions, 7 deletions
diff --git a/win32/mingw.c b/win32/mingw.c
index f748d2d09..d7bdc10f6 100644
--- a/win32/mingw.c
+++ b/win32/mingw.c
@@ -1,6 +1,9 @@
1#include "libbb.h" 1#include "libbb.h"
2#include <userenv.h> 2#include <userenv.h>
3#include "lazyload.h" 3#include "lazyload.h"
4#if ENABLE_FEATURE_IDENTIFY_OWNER
5#include <aclapi.h>
6#endif
4 7
5#if defined(__MINGW64_VERSION_MAJOR) 8#if defined(__MINGW64_VERSION_MAJOR)
6#if ENABLE_GLOBBING 9#if ENABLE_GLOBBING
@@ -387,6 +390,47 @@ static int has_exec_format(const char *name)
387 return 0; 390 return 0;
388} 391}
389 392
393#if ENABLE_FEATURE_IDENTIFY_OWNER
394static int file_belongs_to_me(HANDLE fh)
395{
396 PSID pSidOwner;
397 PSECURITY_DESCRIPTOR pSD;
398 static PTOKEN_USER user = NULL;
399 static int initialised = 0;
400 int equal;
401
402 /* get SID of current user */
403 if (!initialised) {
404 HANDLE token;
405 DWORD ret = 0;
406
407 initialised = 1;
408 if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &token) ||
409 OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) {
410 GetTokenInformation(token, TokenUser, NULL, 0, &ret);
411 if (ret <= 0 || (user=malloc(ret)) == NULL ||
412 !GetTokenInformation(token, TokenUser, user, ret, &ret)) {
413 free(user);
414 user = NULL;
415 }
416 CloseHandle(token);
417 }
418 }
419
420 if (user == NULL)
421 return TRUE;
422
423 /* get SID of file's owner */
424 if (GetSecurityInfo(fh, SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION,
425 &pSidOwner, NULL, NULL, NULL, &pSD) != ERROR_SUCCESS)
426 return FALSE;
427
428 equal = EqualSid(pSidOwner, user->User.Sid);
429 LocalFree(pSD);
430 return equal;
431}
432#endif
433
390/* We keep the do_lstat code in a separate function to avoid recursion. 434/* We keep the do_lstat code in a separate function to avoid recursion.
391 * When a path ends with a slash, the stat will fail with ENOENT. In 435 * When a path ends with a slash, the stat will fail with ENOENT. In
392 * this case, we strip the trailing slashes and stat again. 436 * this case, we strip the trailing slashes and stat again.
@@ -439,21 +483,34 @@ static int do_lstat(int follow, const char *file_name, struct mingw_stat *buf)
439 } 483 }
440 484
441#if ENABLE_FEATURE_EXTRA_FILE_DATA 485#if ENABLE_FEATURE_EXTRA_FILE_DATA
486#if ENABLE_FEATURE_IDENTIFY_OWNER
487 fh = CreateFile(file_name, READ_CONTROL, 0, NULL,
488 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
489#else
442 fh = CreateFile(file_name, 0, 0, NULL, OPEN_EXISTING, 490 fh = CreateFile(file_name, 0, 0, NULL, OPEN_EXISTING,
443 FILE_FLAG_BACKUP_SEMANTICS, NULL); 491 FILE_FLAG_BACKUP_SEMANTICS, NULL);
444 if (fh != INVALID_HANDLE_VALUE && 492#endif
445 GetFileInformationByHandle(fh, &hdata)) { 493 if (fh != INVALID_HANDLE_VALUE) {
446 buf->st_dev = hdata.dwVolumeSerialNumber; 494 if (GetFileInformationByHandle(fh, &hdata)) {
447 buf->st_ino = hdata.nFileIndexLow | 495 buf->st_dev = hdata.dwVolumeSerialNumber;
448 (((ino_t)hdata.nFileIndexHigh)<<32); 496 buf->st_ino = hdata.nFileIndexLow |
449 buf->st_nlink = S_ISDIR(buf->st_mode) ? 2 : hdata.nNumberOfLinks; 497 (((ino_t)hdata.nFileIndexHigh)<<32);
498 buf->st_nlink = S_ISDIR(buf->st_mode) ? 2 :
499 hdata.nNumberOfLinks;
500 }
501#if ENABLE_FEATURE_IDENTIFY_OWNER
502 if (!file_belongs_to_me(fh)) {
503 buf->st_uid = 0;
504 buf->st_gid = 0;
505 }
506#endif
507 CloseHandle(fh);
450 } 508 }
451 else { 509 else {
452 buf->st_uid = 0; 510 buf->st_uid = 0;
453 buf->st_gid = 0; 511 buf->st_gid = 0;
454 buf->st_mode &= ~(S_IROTH|S_IWOTH|S_IXOTH); 512 buf->st_mode &= ~(S_IROTH|S_IWOTH|S_IXOTH);
455 } 513 }
456 CloseHandle(fh);
457#endif 514#endif
458 515
459 /* 516 /*