summaryrefslogtreecommitdiff
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
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.
-rw-r--r--Config.in8
-rw-r--r--configs/mingw32_defconfig3
-rw-r--r--configs/mingw64_defconfig3
-rw-r--r--win32/mingw.c71
4 files changed, 76 insertions, 9 deletions
diff --git a/Config.in b/Config.in
index cd0cab412..2b4b81515 100644
--- a/Config.in
+++ b/Config.in
@@ -465,6 +465,14 @@ config FEATURE_EXTRA_FILE_DATA
465 permits extra features such as warning of attempts to copy a file 465 permits extra features such as warning of attempts to copy a file
466 onto itself or to store a tar archive in itself. 466 onto itself or to store a tar archive in itself.
467 467
468config FEATURE_IDENTIFY_OWNER
469 bool "Check whether files belong to current user (0.6 kb)"
470 default y
471 depends on FEATURE_EXTRA_FILE_DATA
472 help
473 Try to determine if files belong to the current user. If they don't
474 they're listed as belonging to root.
475
468comment 'Build Options' 476comment 'Build Options'
469 477
470config STATIC 478config STATIC
diff --git a/configs/mingw32_defconfig b/configs/mingw32_defconfig
index cc0cf0359..c30ffa1b5 100644
--- a/configs/mingw32_defconfig
+++ b/configs/mingw32_defconfig
@@ -1,7 +1,7 @@
1# 1#
2# Automatically generated make config: don't edit 2# Automatically generated make config: don't edit
3# Busybox version: 1.31.0.git 3# Busybox version: 1.31.0.git
4# Tue Feb 12 08:23:28 2019 4# Fri Mar 1 08:50:17 2019
5# 5#
6CONFIG_HAVE_DOT_CONFIG=y 6CONFIG_HAVE_DOT_CONFIG=y
7# CONFIG_PLATFORM_POSIX is not set 7# CONFIG_PLATFORM_POSIX is not set
@@ -49,6 +49,7 @@ CONFIG_FEATURE_ICON=y
49CONFIG_FEATURE_ICON_ALL=y 49CONFIG_FEATURE_ICON_ALL=y
50CONFIG_FEATURE_EURO=y 50CONFIG_FEATURE_EURO=y
51CONFIG_FEATURE_EXTRA_FILE_DATA=y 51CONFIG_FEATURE_EXTRA_FILE_DATA=y
52CONFIG_FEATURE_IDENTIFY_OWNER=y
52 53
53# 54#
54# Build Options 55# Build Options
diff --git a/configs/mingw64_defconfig b/configs/mingw64_defconfig
index 541090e2a..46cb6941f 100644
--- a/configs/mingw64_defconfig
+++ b/configs/mingw64_defconfig
@@ -1,7 +1,7 @@
1# 1#
2# Automatically generated make config: don't edit 2# Automatically generated make config: don't edit
3# Busybox version: 1.31.0.git 3# Busybox version: 1.31.0.git
4# Tue Feb 12 08:23:28 2019 4# Fri Mar 1 08:50:17 2019
5# 5#
6CONFIG_HAVE_DOT_CONFIG=y 6CONFIG_HAVE_DOT_CONFIG=y
7# CONFIG_PLATFORM_POSIX is not set 7# CONFIG_PLATFORM_POSIX is not set
@@ -49,6 +49,7 @@ CONFIG_FEATURE_ICON=y
49CONFIG_FEATURE_ICON_ALL=y 49CONFIG_FEATURE_ICON_ALL=y
50CONFIG_FEATURE_EURO=y 50CONFIG_FEATURE_EURO=y
51CONFIG_FEATURE_EXTRA_FILE_DATA=y 51CONFIG_FEATURE_EXTRA_FILE_DATA=y
52CONFIG_FEATURE_IDENTIFY_OWNER=y
52 53
53# 54#
54# Build Options 55# Build Options
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 /*