2007-07-20 22:29:43 +08:00
|
|
|
/* security.cc: NT file access control functions
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2006-10-23 03:31:33 +08:00
|
|
|
Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
2011-03-08 23:12:58 +08:00
|
|
|
2006, 2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
Originaly written by Gunther Ebert, gunther.ebert@ixos-leipzig.de
|
2001-02-22 06:59:11 +08:00
|
|
|
Completely rewritten by Corinna Vinschen <corinna@vinschen.de>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-08-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "cygerrno.h"
|
2001-07-27 03:22:24 +08:00
|
|
|
#include "security.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "path.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 08:37:18 +08:00
|
|
|
#include "fhandler.h"
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "pinfo.h"
|
2000-11-15 08:13:09 +08:00
|
|
|
#include "cygheap.h"
|
* autoload.cc: Add load statements for `LookupAccountNameW',
`LsaClose', `LsaEnumerateAccountRights', `LsaFreeMemory',
`LsaOpenPolicy', `LsaQueryInformationPolicy', `NetLocalGroupEnum',
`NetLocalGroupGetMembers', `NetServerEnum', `NetUserGetGroups' and
`NtCreateToken'.
* ntdll.h: Add declaration for `NtCreateToken'.
* sec_helper.cc: Add `well_known_local_sid', `well_known_dialup_sid',
`well_known_network_sid', `well_known_batch_sid',
`well_known_interactive_sid', `well_known_service_sid' and
`well_known_authenticated_users_sid'.
(cygsid::string): Define as const method.
(cygsid::get_sid): Set psid to NO_SID on error.
(cygsid::getfromstr): Ditto.
(cygsid::getfrompw): Simplify.
(cygsid::getfromgr): Check for gr == NULL.
(legal_sid_type): Move to security.h.
(set_process_privilege): Return -1 on error, otherwise 0 or 1 related
to previous privilege setting.
* security.cc (extract_nt_dom_user): Remove `static'.
(lsa2wchar): New function.
(open_local_policy): Ditto.
(close_local_policy): Ditto.
(get_lsa_srv_inf): Ditto.
(get_logon_server): Ditto.
(get_logon_server_and_user_domain): Ditto.
(get_user_groups): Ditto.
(is_group_member): Ditto.
(get_user_local_groups): Ditto.
(sid_in_token_groups): Ditto.
(get_user_primary_group): Ditto.
(get_group_sidlist): Ditto.
(get_system_priv_list): Ditto.
(get_priv_list): Ditto.
(get_dacl): Ditto.
(create_token): Ditto.
(subauth): Return immediately if SE_TCB_NAME can't be assigned.
Change all return statements in case of error to jumps to `out'
label. Add `out' label to support cleanup.
* security.h: Add extern declarations for `well_known_local_sid',
`well_known_dialup_sid', `well_known_network_sid',
`well_known_batch_sid', `well_known_interactive_sid',
`well_known_service_sid' and `well_known_authenticated_users_sid'.
Add extern declarations for functions `create_token',
`extract_nt_dom_user' and `get_logon_server_and_user_domain'.
(class cygsid): Add method `assign'. Change operator= to call new
`assign' method. Add `debug_print' method.
(class cygsidlist): New class.
(legal_sid_type): Moved from sec_helper.cc to here.
* spawn.cc (spawn_guts) Revert reversion of previous patch.
Call `RevertToSelf' and `ImpersonateLoggedOnUser' instead of `seteuid'
again.
* syscalls.cc (seteuid): Rearranged. Call `create_token' now when
needed. Call `subauth' if `create_token' fails. Try setting token
owner and primary group only if token was not explicitely created
by `create_token'.
* uinfo.cc (internal_getlogin): Try harder to generate correct user
information. Especially don't trust return value of `GetUserName'.
2001-05-20 16:10:47 +08:00
|
|
|
#include "ntdll.h"
|
2002-12-10 Pierre Humblet <pierre.humblet@ieee.org>
* pwdgrp.h (pwdgrp_check::pwdgrp_state): Replace by
pwdgrp_check::isinitializing ().
(pwdgrp_check::isinitializing): Create.
* passwd.cc (grab_int): Change type to unsigned, use strtoul and
set the pointer content to 0 if the field is invalid.
(parse_pwd): Move validity test after getting pw_gid.
(read_etc_passwd): Replace "passwd_state <= " by
passwd_state::isinitializing ().
(internal_getpwuid): Ditto.
(internal_getpwnam): Ditto.
(getpwent): Ditto.
(getpass): Ditto.
* grp.cc (parse_grp): Use strtoul for gr_gid and verify the validity.
(read_etc_group): Replace "group_state <= " by
group_state::isinitializing ().
(internal_getgrgid): Ditto.
(getgrent32): Ditto.
(internal_getgrent): Ditto.
2002-12-10 Pierre Humblet <pierre.humblet@ieee.org>
* security.h: Move declarations of internal_getgrent,
internal_getpwsid and internal_getgrsid to pwdgrp.h.
* pwdgrp.h: Declare internal_getpwsid, internal_getpwnam,
internal_getpwuid, internal_getgrsid, internal_getgrgid,
internal_getgrnam, internal_getgrent and internal_getgroups.
Delete "emulated" from enum pwdgrp_state.
(pwdgrp_check::isuninitialized): Create.
(pwdgrp_check::pwdgrp_state): Change state to initializing
rather than to uninitialized.
(pwdgrp_read::gets): Remove trailing CRs.
* passwd.cc (grab_string): Don't look for NLs.
(grab_int): Ditto.
(parse_pwd): Don't look for CRs. Return 0 if entry is too short.
(search_for): Delete.
(read_etc_passwd): Simplify tests to actually read the file.
Set state to loaded before making internal_getpwXX calls.
Replace search_for calls by equivalent internal_pwgetXX calls.
(internal_getpwsid): Use passwd_state.isuninitialized to decide
to call read_etc_passwd.
(internal_getpwuid): Create.
(internal_getpwnam): Create.
(getpwuid32): Simply call internal_getpwuid.
(getpwuid_r32): Call internal_getpwuid.
(getpwnam): Simply call internal_getpwnam.
(getpwnam_r): Call internal_getpwnam.
* grp.cc (parse_grp): Don't look for CRs. Adjust blank space.
(add_grp_line): Adjust blank space.
(class group_lock): Ditto.
(read_etc_group): Simplify tests to actually read the file.
Set state to loaded before making internal_getgrXX calls.
Replace getgrXX calls by equivalent internal calls.
(internal_getgrsid): Use group_state.isuninitialized to decide
to call read_etc_group.
(internal_getgrgid): Create.
(internal_getgrnam): Create.
(getgroups32): Simply call internal_getgrgid.
(getgrnam32): Simply call internal_getgrnam.
(internal_getgrent): Call group_state.isuninitialized.
(internal_getgroups): Create from the former getgroups32, using
two of the four arguments. Set gid to myself->gid and username
to cygheap->user.name ().
(getgroups32): Simply call internal_getgroup.
(getgroups): Call internal_getgroup instead of getgroups32.
(setgroups32): Call internal versions of get{pw,gr}XX.
* sec_helper.cc: Include pwdgrp.h.
(is_grp_member): Call internal versions of get{pw,gr}XX.
* security.cc: Include pwdgrp.h.
(alloc_sd): Call internal versions of get{pw,gr}XX.
* syscalls.cc: Include pwdgrp.h.
(seteuid32): Call internal versions of get{pw,gr}XX.
(setegid32): Ditto.
* uinfo.cc: Include pwdgrp.h.
(internal_getlogin): Call internal versions of get{pw,gr}XX.
(cygheap_user::ontherange): Ditto.
* sec_acl.cc: Include pwdgrp.h.
(setacl): Call internal versions of get{pw,gr}XX.
(acl_access): Ditto and simplify logic.
(aclfromtext): Ditto.
2002-12-10 20:43:49 +08:00
|
|
|
#include "pwdgrp.h"
|
2010-09-10 22:53:44 +08:00
|
|
|
#include "tls_pbuf.h"
|
2009-10-31 03:58:53 +08:00
|
|
|
#include <aclapi.h>
|
2007-07-20 22:29:43 +08:00
|
|
|
|
|
|
|
#define ALL_SECURITY_INFORMATION (DACL_SECURITY_INFORMATION \
|
|
|
|
| GROUP_SECURITY_INFORMATION \
|
|
|
|
| OWNER_SECURITY_INFORMATION)
|
* autoload.cc: Add load statements for `LookupAccountNameW',
`LsaClose', `LsaEnumerateAccountRights', `LsaFreeMemory',
`LsaOpenPolicy', `LsaQueryInformationPolicy', `NetLocalGroupEnum',
`NetLocalGroupGetMembers', `NetServerEnum', `NetUserGetGroups' and
`NtCreateToken'.
* ntdll.h: Add declaration for `NtCreateToken'.
* sec_helper.cc: Add `well_known_local_sid', `well_known_dialup_sid',
`well_known_network_sid', `well_known_batch_sid',
`well_known_interactive_sid', `well_known_service_sid' and
`well_known_authenticated_users_sid'.
(cygsid::string): Define as const method.
(cygsid::get_sid): Set psid to NO_SID on error.
(cygsid::getfromstr): Ditto.
(cygsid::getfrompw): Simplify.
(cygsid::getfromgr): Check for gr == NULL.
(legal_sid_type): Move to security.h.
(set_process_privilege): Return -1 on error, otherwise 0 or 1 related
to previous privilege setting.
* security.cc (extract_nt_dom_user): Remove `static'.
(lsa2wchar): New function.
(open_local_policy): Ditto.
(close_local_policy): Ditto.
(get_lsa_srv_inf): Ditto.
(get_logon_server): Ditto.
(get_logon_server_and_user_domain): Ditto.
(get_user_groups): Ditto.
(is_group_member): Ditto.
(get_user_local_groups): Ditto.
(sid_in_token_groups): Ditto.
(get_user_primary_group): Ditto.
(get_group_sidlist): Ditto.
(get_system_priv_list): Ditto.
(get_priv_list): Ditto.
(get_dacl): Ditto.
(create_token): Ditto.
(subauth): Return immediately if SE_TCB_NAME can't be assigned.
Change all return statements in case of error to jumps to `out'
label. Add `out' label to support cleanup.
* security.h: Add extern declarations for `well_known_local_sid',
`well_known_dialup_sid', `well_known_network_sid',
`well_known_batch_sid', `well_known_interactive_sid',
`well_known_service_sid' and `well_known_authenticated_users_sid'.
Add extern declarations for functions `create_token',
`extract_nt_dom_user' and `get_logon_server_and_user_domain'.
(class cygsid): Add method `assign'. Change operator= to call new
`assign' method. Add `debug_print' method.
(class cygsidlist): New class.
(legal_sid_type): Moved from sec_helper.cc to here.
* spawn.cc (spawn_guts) Revert reversion of previous patch.
Call `RevertToSelf' and `ImpersonateLoggedOnUser' instead of `seteuid'
again.
* syscalls.cc (seteuid): Rearranged. Call `create_token' now when
needed. Call `subauth' if `create_token' fails. Try setting token
owner and primary group only if token was not explicitely created
by `create_token'.
* uinfo.cc (internal_getlogin): Try harder to generate correct user
information. Especially don't trust return value of `GetUserName'.
2001-05-20 16:10:47 +08:00
|
|
|
|
2011-04-28 15:27:51 +08:00
|
|
|
static NO_COPY GENERIC_MAPPING file_mapping = { FILE_GENERIC_READ,
|
|
|
|
FILE_GENERIC_WRITE,
|
|
|
|
FILE_GENERIC_EXECUTE,
|
|
|
|
FILE_ALL_ACCESS };
|
|
|
|
|
2007-07-20 22:29:43 +08:00
|
|
|
LONG
|
2010-09-10 17:32:13 +08:00
|
|
|
get_file_sd (HANDLE fh, path_conv &pc, security_descriptor &sd,
|
|
|
|
bool justcreated)
|
* autoload.cc: Add load statements for `LookupAccountNameW',
`LsaClose', `LsaEnumerateAccountRights', `LsaFreeMemory',
`LsaOpenPolicy', `LsaQueryInformationPolicy', `NetLocalGroupEnum',
`NetLocalGroupGetMembers', `NetServerEnum', `NetUserGetGroups' and
`NtCreateToken'.
* ntdll.h: Add declaration for `NtCreateToken'.
* sec_helper.cc: Add `well_known_local_sid', `well_known_dialup_sid',
`well_known_network_sid', `well_known_batch_sid',
`well_known_interactive_sid', `well_known_service_sid' and
`well_known_authenticated_users_sid'.
(cygsid::string): Define as const method.
(cygsid::get_sid): Set psid to NO_SID on error.
(cygsid::getfromstr): Ditto.
(cygsid::getfrompw): Simplify.
(cygsid::getfromgr): Check for gr == NULL.
(legal_sid_type): Move to security.h.
(set_process_privilege): Return -1 on error, otherwise 0 or 1 related
to previous privilege setting.
* security.cc (extract_nt_dom_user): Remove `static'.
(lsa2wchar): New function.
(open_local_policy): Ditto.
(close_local_policy): Ditto.
(get_lsa_srv_inf): Ditto.
(get_logon_server): Ditto.
(get_logon_server_and_user_domain): Ditto.
(get_user_groups): Ditto.
(is_group_member): Ditto.
(get_user_local_groups): Ditto.
(sid_in_token_groups): Ditto.
(get_user_primary_group): Ditto.
(get_group_sidlist): Ditto.
(get_system_priv_list): Ditto.
(get_priv_list): Ditto.
(get_dacl): Ditto.
(create_token): Ditto.
(subauth): Return immediately if SE_TCB_NAME can't be assigned.
Change all return statements in case of error to jumps to `out'
label. Add `out' label to support cleanup.
* security.h: Add extern declarations for `well_known_local_sid',
`well_known_dialup_sid', `well_known_network_sid',
`well_known_batch_sid', `well_known_interactive_sid',
`well_known_service_sid' and `well_known_authenticated_users_sid'.
Add extern declarations for functions `create_token',
`extract_nt_dom_user' and `get_logon_server_and_user_domain'.
(class cygsid): Add method `assign'. Change operator= to call new
`assign' method. Add `debug_print' method.
(class cygsidlist): New class.
(legal_sid_type): Moved from sec_helper.cc to here.
* spawn.cc (spawn_guts) Revert reversion of previous patch.
Call `RevertToSelf' and `ImpersonateLoggedOnUser' instead of `seteuid'
again.
* syscalls.cc (seteuid): Rearranged. Call `create_token' now when
needed. Call `subauth' if `create_token' fails. Try setting token
owner and primary group only if token was not explicitely created
by `create_token'.
* uinfo.cc (internal_getlogin): Try harder to generate correct user
information. Especially don't trust return value of `GetUserName'.
2001-05-20 16:10:47 +08:00
|
|
|
{
|
2011-04-28 15:27:51 +08:00
|
|
|
NTSTATUS status;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
ULONG len = SD_MAXIMUM_SIZE, rlen;
|
* autoload.cc: Add load statements for `LookupAccountNameW',
`LsaClose', `LsaEnumerateAccountRights', `LsaFreeMemory',
`LsaOpenPolicy', `LsaQueryInformationPolicy', `NetLocalGroupEnum',
`NetLocalGroupGetMembers', `NetServerEnum', `NetUserGetGroups' and
`NtCreateToken'.
* ntdll.h: Add declaration for `NtCreateToken'.
* sec_helper.cc: Add `well_known_local_sid', `well_known_dialup_sid',
`well_known_network_sid', `well_known_batch_sid',
`well_known_interactive_sid', `well_known_service_sid' and
`well_known_authenticated_users_sid'.
(cygsid::string): Define as const method.
(cygsid::get_sid): Set psid to NO_SID on error.
(cygsid::getfromstr): Ditto.
(cygsid::getfrompw): Simplify.
(cygsid::getfromgr): Check for gr == NULL.
(legal_sid_type): Move to security.h.
(set_process_privilege): Return -1 on error, otherwise 0 or 1 related
to previous privilege setting.
* security.cc (extract_nt_dom_user): Remove `static'.
(lsa2wchar): New function.
(open_local_policy): Ditto.
(close_local_policy): Ditto.
(get_lsa_srv_inf): Ditto.
(get_logon_server): Ditto.
(get_logon_server_and_user_domain): Ditto.
(get_user_groups): Ditto.
(is_group_member): Ditto.
(get_user_local_groups): Ditto.
(sid_in_token_groups): Ditto.
(get_user_primary_group): Ditto.
(get_group_sidlist): Ditto.
(get_system_priv_list): Ditto.
(get_priv_list): Ditto.
(get_dacl): Ditto.
(create_token): Ditto.
(subauth): Return immediately if SE_TCB_NAME can't be assigned.
Change all return statements in case of error to jumps to `out'
label. Add `out' label to support cleanup.
* security.h: Add extern declarations for `well_known_local_sid',
`well_known_dialup_sid', `well_known_network_sid',
`well_known_batch_sid', `well_known_interactive_sid',
`well_known_service_sid' and `well_known_authenticated_users_sid'.
Add extern declarations for functions `create_token',
`extract_nt_dom_user' and `get_logon_server_and_user_domain'.
(class cygsid): Add method `assign'. Change operator= to call new
`assign' method. Add `debug_print' method.
(class cygsidlist): New class.
(legal_sid_type): Moved from sec_helper.cc to here.
* spawn.cc (spawn_guts) Revert reversion of previous patch.
Call `RevertToSelf' and `ImpersonateLoggedOnUser' instead of `seteuid'
again.
* syscalls.cc (seteuid): Rearranged. Call `create_token' now when
needed. Call `subauth' if `create_token' fails. Try setting token
owner and primary group only if token was not explicitely created
by `create_token'.
* uinfo.cc (internal_getlogin): Try harder to generate correct user
information. Especially don't trust return value of `GetUserName'.
2001-05-20 16:10:47 +08:00
|
|
|
|
2011-04-28 15:27:51 +08:00
|
|
|
/* Allocate space for the security descriptor. */
|
|
|
|
if (!sd.malloc (len))
|
* autoload.cc: Add load statements for `LookupAccountNameW',
`LsaClose', `LsaEnumerateAccountRights', `LsaFreeMemory',
`LsaOpenPolicy', `LsaQueryInformationPolicy', `NetLocalGroupEnum',
`NetLocalGroupGetMembers', `NetServerEnum', `NetUserGetGroups' and
`NtCreateToken'.
* ntdll.h: Add declaration for `NtCreateToken'.
* sec_helper.cc: Add `well_known_local_sid', `well_known_dialup_sid',
`well_known_network_sid', `well_known_batch_sid',
`well_known_interactive_sid', `well_known_service_sid' and
`well_known_authenticated_users_sid'.
(cygsid::string): Define as const method.
(cygsid::get_sid): Set psid to NO_SID on error.
(cygsid::getfromstr): Ditto.
(cygsid::getfrompw): Simplify.
(cygsid::getfromgr): Check for gr == NULL.
(legal_sid_type): Move to security.h.
(set_process_privilege): Return -1 on error, otherwise 0 or 1 related
to previous privilege setting.
* security.cc (extract_nt_dom_user): Remove `static'.
(lsa2wchar): New function.
(open_local_policy): Ditto.
(close_local_policy): Ditto.
(get_lsa_srv_inf): Ditto.
(get_logon_server): Ditto.
(get_logon_server_and_user_domain): Ditto.
(get_user_groups): Ditto.
(is_group_member): Ditto.
(get_user_local_groups): Ditto.
(sid_in_token_groups): Ditto.
(get_user_primary_group): Ditto.
(get_group_sidlist): Ditto.
(get_system_priv_list): Ditto.
(get_priv_list): Ditto.
(get_dacl): Ditto.
(create_token): Ditto.
(subauth): Return immediately if SE_TCB_NAME can't be assigned.
Change all return statements in case of error to jumps to `out'
label. Add `out' label to support cleanup.
* security.h: Add extern declarations for `well_known_local_sid',
`well_known_dialup_sid', `well_known_network_sid',
`well_known_batch_sid', `well_known_interactive_sid',
`well_known_service_sid' and `well_known_authenticated_users_sid'.
Add extern declarations for functions `create_token',
`extract_nt_dom_user' and `get_logon_server_and_user_domain'.
(class cygsid): Add method `assign'. Change operator= to call new
`assign' method. Add `debug_print' method.
(class cygsidlist): New class.
(legal_sid_type): Moved from sec_helper.cc to here.
* spawn.cc (spawn_guts) Revert reversion of previous patch.
Call `RevertToSelf' and `ImpersonateLoggedOnUser' instead of `seteuid'
again.
* syscalls.cc (seteuid): Rearranged. Call `create_token' now when
needed. Call `subauth' if `create_token' fails. Try setting token
owner and primary group only if token was not explicitely created
by `create_token'.
* uinfo.cc (internal_getlogin): Try harder to generate correct user
information. Especially don't trust return value of `GetUserName'.
2001-05-20 16:10:47 +08:00
|
|
|
{
|
2011-04-28 15:27:51 +08:00
|
|
|
set_errno (ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Try to fetch the security descriptor if the handle is valid. */
|
|
|
|
if (fh)
|
|
|
|
{
|
|
|
|
status = NtQuerySecurityObject (fh, ALL_SECURITY_INFORMATION,
|
|
|
|
sd, len, &rlen);
|
|
|
|
if (!NT_SUCCESS (status))
|
2001-09-08 05:32:07 +08:00
|
|
|
{
|
2011-04-28 15:27:51 +08:00
|
|
|
debug_printf ("NtQuerySecurityObject (%S), status %p",
|
|
|
|
pc.get_nt_native_path (), status);
|
|
|
|
fh = NULL;
|
2001-09-08 05:32:07 +08:00
|
|
|
}
|
2011-04-28 15:27:51 +08:00
|
|
|
}
|
|
|
|
/* If the handle was NULL, or fetching with the original handle didn't work,
|
|
|
|
try to reopen the file with READ_CONTROL and fetch the security descriptor
|
|
|
|
using that handle. */
|
|
|
|
if (!fh)
|
|
|
|
{
|
|
|
|
status = NtOpenFile (&fh, READ_CONTROL,
|
|
|
|
pc.get_object_attr (attr, sec_none_nih), &io,
|
|
|
|
FILE_SHARE_VALID_FLAGS, FILE_OPEN_FOR_BACKUP_INTENT);
|
|
|
|
if (!NT_SUCCESS (status))
|
2011-06-06 13:02:13 +08:00
|
|
|
{
|
2011-04-28 15:27:51 +08:00
|
|
|
sd.free ();
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
status = NtQuerySecurityObject (fh, ALL_SECURITY_INFORMATION,
|
|
|
|
sd, len, &rlen);
|
|
|
|
NtClose (fh);
|
|
|
|
if (!NT_SUCCESS (status))
|
2008-02-16 01:53:11 +08:00
|
|
|
{
|
2011-04-28 15:27:51 +08:00
|
|
|
sd.free ();
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return -1;
|
2006-11-27 20:59:59 +08:00
|
|
|
}
|
|
|
|
}
|
2011-04-28 15:27:51 +08:00
|
|
|
/* Ok, so we have a security descriptor now. Unfortunately, if you want
|
|
|
|
to know if an ACE is inherited from the parent object, you can't just
|
|
|
|
call NtQuerySecurityObject once. The problem is this:
|
|
|
|
|
|
|
|
In the simple case, the SDs control word contains one of the
|
|
|
|
SE_DACL_AUTO_INHERITED or SE_DACL_PROTECTED flags, or at least one of
|
|
|
|
the ACEs has the INHERITED_ACE flag set. In all of these cases the
|
|
|
|
GetSecurityInfo function calls NtQuerySecurityObject only once, too,
|
|
|
|
apparently because it figures that the DACL is self-sufficient, which
|
|
|
|
it usually is. Windows Explorer, for instance, takes great care to
|
|
|
|
set these flags in a security descriptor if you change the ACL in the
|
|
|
|
GUI property dialog.
|
|
|
|
|
|
|
|
The tricky case is if none of these flags is set in the SD. That means
|
|
|
|
the information whether or not an ACE has been inherited is not available
|
|
|
|
in the DACL of the object. In this case GetSecurityInfo also fetches the
|
|
|
|
SD from the parent directory and tests if the object's SD contains
|
|
|
|
inherited ACEs from the parent. The below code is closly emulating the
|
|
|
|
behaviour of GetSecurityInfo so we can get rid of this advapi32 dependency.
|
|
|
|
|
|
|
|
However, this functionality is slow, and the extra information is only
|
|
|
|
required when the file has been created and the permissions are about
|
|
|
|
to be set to POSIX permissions. Therefore we only use it in case the
|
|
|
|
file just got created.
|
|
|
|
|
|
|
|
Note that GetSecurityInfo has a problem on 5.1 and 5.2 kernels. Sometimes
|
|
|
|
it returns ERROR_INVALID_ADDRESS if a former request for the parent
|
|
|
|
directories' SD used NtQuerySecurityObject, rather than GetSecurityInfo
|
|
|
|
as well. See http://cygwin.com/ml/cygwin-developers/2011-03/msg00027.html
|
|
|
|
for the solution. This problem does not occur with the below code, so
|
2011-07-05 23:42:43 +08:00
|
|
|
the workaround has been removed. */
|
2011-04-28 15:27:51 +08:00
|
|
|
if (justcreated)
|
|
|
|
{
|
|
|
|
SECURITY_DESCRIPTOR_CONTROL ctrl;
|
|
|
|
ULONG dummy;
|
|
|
|
PACL dacl;
|
|
|
|
BOOLEAN exists, def;
|
|
|
|
ACCESS_ALLOWED_ACE *ace;
|
|
|
|
UNICODE_STRING dirname;
|
|
|
|
PSECURITY_DESCRIPTOR psd, nsd;
|
|
|
|
tmp_pathbuf tp;
|
|
|
|
|
|
|
|
/* Check SDs control flags. If SE_DACL_AUTO_INHERITED or
|
|
|
|
SE_DACL_PROTECTED is set we're done. */
|
|
|
|
RtlGetControlSecurityDescriptor (sd, &ctrl, &dummy);
|
|
|
|
if (ctrl & (SE_DACL_AUTO_INHERITED | SE_DACL_PROTECTED))
|
|
|
|
return 0;
|
|
|
|
/* Otherwise iterate over the ACEs and see if any one of them has the
|
|
|
|
INHERITED_ACE flag set. If so, we're done. */
|
|
|
|
if (NT_SUCCESS (RtlGetDaclSecurityDescriptor (sd, &exists, &dacl, &def))
|
|
|
|
&& exists && dacl)
|
|
|
|
for (ULONG idx = 0; idx < dacl->AceCount; ++idx)
|
2011-04-28 17:30:36 +08:00
|
|
|
if (NT_SUCCESS (RtlGetAce (dacl, idx, (PVOID *) &ace))
|
2011-04-28 15:27:51 +08:00
|
|
|
&& (ace->Header.AceFlags & INHERITED_ACE))
|
|
|
|
return 0;
|
|
|
|
/* Otherwise, open the parent directory with READ_CONTROL... */
|
|
|
|
RtlSplitUnicodePath (pc.get_nt_native_path (), &dirname, NULL);
|
|
|
|
InitializeObjectAttributes (&attr, &dirname, pc.objcaseinsensitive (),
|
|
|
|
NULL, NULL);
|
|
|
|
status = NtOpenFile (&fh, READ_CONTROL, &attr, &io,
|
|
|
|
FILE_SHARE_VALID_FLAGS,
|
|
|
|
FILE_OPEN_FOR_BACKUP_INTENT
|
|
|
|
| FILE_OPEN_REPARSE_POINT);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
debug_printf ("NtOpenFile (%S), status %p", &dirname, status);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* ... fetch the parent's security descriptor ... */
|
|
|
|
psd = (PSECURITY_DESCRIPTOR) tp.w_get ();
|
|
|
|
status = NtQuerySecurityObject (fh, ALL_SECURITY_INFORMATION,
|
|
|
|
psd, len, &rlen);
|
|
|
|
NtClose (fh);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
debug_printf ("NtQuerySecurityObject (%S), status %p",
|
|
|
|
&dirname, status);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* ... and create a new security descriptor in which all inherited ACEs
|
2011-06-06 13:02:13 +08:00
|
|
|
are marked with the INHERITED_ACE flag. For a description of the
|
2011-04-28 15:27:51 +08:00
|
|
|
undocumented RtlConvertToAutoInheritSecurityObject function from
|
|
|
|
ntdll.dll see the MSDN man page for the advapi32 function
|
|
|
|
ConvertToAutoInheritPrivateObjectSecurity. Fortunately the latter
|
|
|
|
is just a shim. */
|
|
|
|
status = RtlConvertToAutoInheritSecurityObject (psd, sd, &nsd, NULL,
|
|
|
|
pc.isdir (),
|
|
|
|
&file_mapping);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
debug_printf ("RtlConvertToAutoInheritSecurityObject (%S), status %p",
|
|
|
|
&dirname, status);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Eventually copy the new security descriptor into sd and delete the
|
2011-06-06 13:02:13 +08:00
|
|
|
original one created by RtlConvertToAutoInheritSecurityObject from
|
2011-04-28 15:27:51 +08:00
|
|
|
the heap. */
|
|
|
|
len = RtlLengthSecurityDescriptor (nsd);
|
|
|
|
memcpy ((PSECURITY_DESCRIPTOR) sd, nsd, len);
|
|
|
|
RtlDeleteSecurityObject (&nsd);
|
|
|
|
}
|
|
|
|
return 0;
|
2003-11-26 21:23:27 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2003-11-26 21:23:27 +08:00
|
|
|
LONG
|
2009-04-09 17:19:03 +08:00
|
|
|
set_file_sd (HANDLE fh, path_conv &pc, security_descriptor &sd, bool is_chown)
|
2003-11-26 21:23:27 +08:00
|
|
|
{
|
2007-07-20 22:29:43 +08:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
2004-04-14 21:40:07 +08:00
|
|
|
int retry = 0;
|
2004-04-17 05:22:13 +08:00
|
|
|
int res = -1;
|
2007-07-20 22:29:43 +08:00
|
|
|
|
2004-04-14 21:40:07 +08:00
|
|
|
for (; retry < 2; ++retry)
|
|
|
|
{
|
2007-07-20 22:29:43 +08:00
|
|
|
if (fh)
|
2008-02-16 01:53:11 +08:00
|
|
|
{
|
2009-04-09 17:19:03 +08:00
|
|
|
status = NtSetSecurityObject (fh,
|
|
|
|
is_chown ? ALL_SECURITY_INFORMATION
|
|
|
|
: DACL_SECURITY_INFORMATION,
|
|
|
|
sd);
|
2007-07-20 22:29:43 +08:00
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
res = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!retry)
|
2008-02-16 01:53:11 +08:00
|
|
|
{
|
2007-07-20 22:29:43 +08:00
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
2009-04-09 17:19:03 +08:00
|
|
|
status = NtOpenFile (&fh, (is_chown ? WRITE_OWNER : 0) | WRITE_DAC,
|
2007-07-20 22:29:43 +08:00
|
|
|
pc.get_object_attr (attr, sec_none_nih),
|
|
|
|
&io, FILE_SHARE_VALID_FLAGS,
|
2009-05-04 17:16:42 +08:00
|
|
|
FILE_OPEN_FOR_BACKUP_INTENT);
|
2007-07-20 22:29:43 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
fh = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-04-14 21:40:07 +08:00
|
|
|
}
|
2007-07-20 22:29:43 +08:00
|
|
|
if (retry && fh)
|
|
|
|
NtClose (fh);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
__seterrno_from_nt_status (status);
|
2004-04-15 04:16:29 +08:00
|
|
|
return res;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-08-26 17:56:06 +08:00
|
|
|
static void
|
2003-02-11 06:43:29 +08:00
|
|
|
get_attribute_from_acl (mode_t *attribute, PACL acl, PSID owner_sid,
|
2003-12-08 06:37:12 +08:00
|
|
|
PSID group_sid, bool grp_member)
|
2002-08-26 17:56:06 +08:00
|
|
|
{
|
|
|
|
ACCESS_ALLOWED_ACE *ace;
|
|
|
|
int allow = 0;
|
|
|
|
int deny = 0;
|
|
|
|
int *flags, *anti;
|
|
|
|
|
|
|
|
for (DWORD i = 0; i < acl->AceCount; ++i)
|
|
|
|
{
|
2011-04-28 17:30:36 +08:00
|
|
|
if (!NT_SUCCESS (RtlGetAce (acl, i, (PVOID *) &ace)))
|
2002-08-26 17:56:06 +08:00
|
|
|
continue;
|
2009-10-31 03:58:53 +08:00
|
|
|
if (ace->Header.AceFlags & INHERIT_ONLY_ACE)
|
2002-08-26 17:56:06 +08:00
|
|
|
continue;
|
|
|
|
switch (ace->Header.AceType)
|
|
|
|
{
|
|
|
|
case ACCESS_ALLOWED_ACE_TYPE:
|
|
|
|
flags = &allow;
|
|
|
|
anti = &deny;
|
|
|
|
break;
|
|
|
|
case ACCESS_DENIED_ACE_TYPE:
|
|
|
|
flags = &deny;
|
|
|
|
anti = &allow;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2003-02-05 21:47:47 +08:00
|
|
|
cygpsid ace_sid ((PSID) &ace->SidStart);
|
2002-08-26 17:56:06 +08:00
|
|
|
if (ace_sid == well_known_world_sid)
|
|
|
|
{
|
2003-11-26 18:52:21 +08:00
|
|
|
if (ace->Mask & FILE_READ_BITS)
|
2002-11-20 17:23:21 +08:00
|
|
|
*flags |= ((!(*anti & S_IROTH)) ? S_IROTH : 0)
|
2002-08-26 17:56:06 +08:00
|
|
|
| ((!(*anti & S_IRGRP)) ? S_IRGRP : 0)
|
|
|
|
| ((!(*anti & S_IRUSR)) ? S_IRUSR : 0);
|
2003-11-26 18:52:21 +08:00
|
|
|
if (ace->Mask & FILE_WRITE_BITS)
|
2002-11-20 17:23:21 +08:00
|
|
|
*flags |= ((!(*anti & S_IWOTH)) ? S_IWOTH : 0)
|
2002-08-26 17:56:06 +08:00
|
|
|
| ((!(*anti & S_IWGRP)) ? S_IWGRP : 0)
|
|
|
|
| ((!(*anti & S_IWUSR)) ? S_IWUSR : 0);
|
2003-11-26 18:52:21 +08:00
|
|
|
if (ace->Mask & FILE_EXEC_BITS)
|
2002-11-20 17:23:21 +08:00
|
|
|
*flags |= ((!(*anti & S_IXOTH)) ? S_IXOTH : 0)
|
2003-01-26 14:42:40 +08:00
|
|
|
| ((!(*anti & S_IXGRP)) ? S_IXGRP : 0)
|
|
|
|
| ((!(*anti & S_IXUSR)) ? S_IXUSR : 0);
|
2003-03-20 05:34:38 +08:00
|
|
|
if ((S_ISDIR (*attribute)) &&
|
2002-08-26 17:56:06 +08:00
|
|
|
(ace->Mask & (FILE_WRITE_DATA | FILE_EXECUTE | FILE_DELETE_CHILD))
|
|
|
|
== (FILE_WRITE_DATA | FILE_EXECUTE))
|
|
|
|
*flags |= S_ISVTX;
|
|
|
|
}
|
|
|
|
else if (ace_sid == well_known_null_sid)
|
|
|
|
{
|
|
|
|
/* Read SUID, SGID and VTX bits from NULL ACE. */
|
|
|
|
if (ace->Mask & FILE_READ_DATA)
|
|
|
|
*flags |= S_ISVTX;
|
|
|
|
if (ace->Mask & FILE_WRITE_DATA)
|
|
|
|
*flags |= S_ISGID;
|
|
|
|
if (ace->Mask & FILE_APPEND_DATA)
|
|
|
|
*flags |= S_ISUID;
|
|
|
|
}
|
2002-11-20 17:23:21 +08:00
|
|
|
else if (ace_sid == owner_sid)
|
2002-08-26 17:56:06 +08:00
|
|
|
{
|
2003-11-26 18:52:21 +08:00
|
|
|
if (ace->Mask & FILE_READ_BITS)
|
2002-11-20 17:23:21 +08:00
|
|
|
*flags |= ((!(*anti & S_IRUSR)) ? S_IRUSR : 0);
|
2003-11-26 18:52:21 +08:00
|
|
|
if (ace->Mask & FILE_WRITE_BITS)
|
2002-11-20 17:23:21 +08:00
|
|
|
*flags |= ((!(*anti & S_IWUSR)) ? S_IWUSR : 0);
|
2003-11-26 18:52:21 +08:00
|
|
|
if (ace->Mask & FILE_EXEC_BITS)
|
2002-11-20 17:23:21 +08:00
|
|
|
*flags |= ((!(*anti & S_IXUSR)) ? S_IXUSR : 0);
|
2002-08-26 17:56:06 +08:00
|
|
|
}
|
2002-11-20 17:23:21 +08:00
|
|
|
else if (ace_sid == group_sid)
|
2002-08-26 17:56:06 +08:00
|
|
|
{
|
2003-11-26 18:52:21 +08:00
|
|
|
if (ace->Mask & FILE_READ_BITS)
|
2002-11-20 17:23:21 +08:00
|
|
|
*flags |= ((!(*anti & S_IRGRP)) ? S_IRGRP : 0)
|
2002-08-26 17:56:06 +08:00
|
|
|
| ((grp_member && !(*anti & S_IRUSR)) ? S_IRUSR : 0);
|
2003-11-26 18:52:21 +08:00
|
|
|
if (ace->Mask & FILE_WRITE_BITS)
|
2002-11-20 17:23:21 +08:00
|
|
|
*flags |= ((!(*anti & S_IWGRP)) ? S_IWGRP : 0)
|
2002-08-26 17:56:06 +08:00
|
|
|
| ((grp_member && !(*anti & S_IWUSR)) ? S_IWUSR : 0);
|
2003-11-26 18:52:21 +08:00
|
|
|
if (ace->Mask & FILE_EXEC_BITS)
|
2002-11-20 17:23:21 +08:00
|
|
|
*flags |= ((!(*anti & S_IXGRP)) ? S_IXGRP : 0)
|
2002-08-26 17:56:06 +08:00
|
|
|
| ((grp_member && !(*anti & S_IXUSR)) ? S_IXUSR : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*attribute &= ~(S_IRWXU | S_IRWXG | S_IRWXO | S_ISVTX | S_ISGID | S_ISUID);
|
2011-04-29 18:38:12 +08:00
|
|
|
if (owner_sid && group_sid && RtlEqualSid (owner_sid, group_sid)
|
2002-11-20 17:23:21 +08:00
|
|
|
/* FIXME: temporary exception for /var/empty */
|
|
|
|
&& well_known_system_sid != group_sid)
|
|
|
|
{
|
|
|
|
allow &= ~(S_IRGRP | S_IWGRP | S_IXGRP);
|
|
|
|
allow |= (((allow & S_IRUSR) ? S_IRGRP : 0)
|
|
|
|
| ((allow & S_IWUSR) ? S_IWGRP : 0)
|
|
|
|
| ((allow & S_IXUSR) ? S_IXGRP : 0));
|
|
|
|
}
|
2002-08-26 17:56:06 +08:00
|
|
|
*attribute |= allow;
|
|
|
|
}
|
|
|
|
|
2003-04-11 17:38:07 +08:00
|
|
|
static void
|
|
|
|
get_info_from_sd (PSECURITY_DESCRIPTOR psd, mode_t *attribute,
|
2002-05-29 23:04:29 +08:00
|
|
|
__uid32_t *uidret, __gid32_t *gidret)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-04-11 17:38:07 +08:00
|
|
|
if (!psd)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2003-04-11 17:38:07 +08:00
|
|
|
/* If reading the security descriptor failed, treat the object
|
|
|
|
as unreadable. */
|
|
|
|
if (attribute)
|
|
|
|
*attribute &= ~(S_IRWXU | S_IRWXG | S_IRWXO);
|
|
|
|
if (uidret)
|
|
|
|
*uidret = ILLEGAL_UID;
|
|
|
|
if (gidret)
|
|
|
|
*gidret = ILLEGAL_GID;
|
|
|
|
return;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2003-02-05 21:47:47 +08:00
|
|
|
cygpsid owner_sid;
|
|
|
|
cygpsid group_sid;
|
2011-04-28 17:53:11 +08:00
|
|
|
NTSTATUS status;
|
|
|
|
BOOLEAN dummy;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2011-04-28 17:53:11 +08:00
|
|
|
status = RtlGetOwnerSecurityDescriptor (psd, (PSID *) &owner_sid, &dummy);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
debug_printf ("RtlGetOwnerSecurityDescriptor: %p", status);
|
|
|
|
status = RtlGetGroupSecurityDescriptor (psd, (PSID *) &group_sid, &dummy);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
debug_printf ("RtlGetGroupSecurityDescriptor: %p", status);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2003-02-05 21:47:47 +08:00
|
|
|
__uid32_t uid;
|
|
|
|
__gid32_t gid;
|
2003-12-08 06:37:12 +08:00
|
|
|
bool grp_member = get_sids_info (owner_sid, group_sid, &uid, &gid);
|
2000-03-17 03:35:18 +08:00
|
|
|
if (uidret)
|
|
|
|
*uidret = uid;
|
|
|
|
if (gidret)
|
|
|
|
*gidret = gid;
|
|
|
|
|
2001-01-28 13:51:15 +08:00
|
|
|
if (!attribute)
|
2000-03-17 03:35:18 +08:00
|
|
|
{
|
2003-04-11 17:38:07 +08:00
|
|
|
syscall_printf ("uid %d, gid %d", uid, gid);
|
|
|
|
return;
|
2000-03-17 03:35:18 +08:00
|
|
|
}
|
|
|
|
|
2003-04-11 17:38:07 +08:00
|
|
|
PACL acl;
|
2011-04-28 17:53:11 +08:00
|
|
|
BOOLEAN acl_exists;
|
2003-04-11 17:38:07 +08:00
|
|
|
|
2011-04-28 17:53:11 +08:00
|
|
|
status = RtlGetDaclSecurityDescriptor (psd, &acl_exists, &acl, &dummy);
|
|
|
|
if (!NT_SUCCESS (status))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-04-28 17:53:11 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2003-04-11 17:38:07 +08:00
|
|
|
*attribute &= ~(S_IRWXU | S_IRWXG | S_IRWXO);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2003-04-11 17:38:07 +08:00
|
|
|
else if (!acl_exists || !acl)
|
|
|
|
*attribute |= S_IRWXU | S_IRWXG | S_IRWXO;
|
|
|
|
else
|
|
|
|
get_attribute_from_acl (attribute, acl, owner_sid, group_sid, grp_member);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-09-03 16:18:41 +08:00
|
|
|
syscall_printf ("%sACL %x, uid %d, gid %d",
|
2003-04-11 17:38:07 +08:00
|
|
|
(!acl_exists || !acl)?"NO ":"", *attribute, uid, gid);
|
|
|
|
}
|
|
|
|
|
2006-12-21 01:14:23 +08:00
|
|
|
static int
|
2007-07-20 22:29:43 +08:00
|
|
|
get_reg_sd (HANDLE handle, security_descriptor &sd_ret)
|
2006-12-21 01:14:23 +08:00
|
|
|
{
|
|
|
|
LONG ret;
|
|
|
|
DWORD len = 0;
|
|
|
|
|
2007-07-20 22:29:43 +08:00
|
|
|
ret = RegGetKeySecurity ((HKEY) handle, ALL_SECURITY_INFORMATION,
|
2007-02-20 08:16:18 +08:00
|
|
|
sd_ret, &len);
|
2006-12-21 01:14:23 +08:00
|
|
|
if (ret == ERROR_INSUFFICIENT_BUFFER)
|
2007-02-20 08:16:18 +08:00
|
|
|
{
|
2006-12-21 01:14:23 +08:00
|
|
|
if (!sd_ret.malloc (len))
|
2007-02-20 08:16:18 +08:00
|
|
|
set_errno (ENOMEM);
|
2006-12-21 01:14:23 +08:00
|
|
|
else
|
2007-07-20 22:29:43 +08:00
|
|
|
ret = RegGetKeySecurity ((HKEY) handle, ALL_SECURITY_INFORMATION,
|
2007-02-20 08:16:18 +08:00
|
|
|
sd_ret, &len);
|
2006-12-21 01:14:23 +08:00
|
|
|
}
|
|
|
|
if (ret != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
2007-02-20 08:16:18 +08:00
|
|
|
}
|
2006-12-21 01:14:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-15 00:36:26 +08:00
|
|
|
int
|
2007-07-20 22:29:43 +08:00
|
|
|
get_reg_attribute (HKEY hkey, mode_t *attribute, __uid32_t *uidret,
|
|
|
|
__gid32_t *gidret)
|
2002-07-02 09:36:15 +08:00
|
|
|
{
|
2008-07-15 04:22:03 +08:00
|
|
|
security_descriptor sd;
|
2006-10-22 22:57:43 +08:00
|
|
|
|
2008-07-15 04:22:03 +08:00
|
|
|
if (!get_reg_sd (hkey, sd))
|
|
|
|
{
|
|
|
|
get_info_from_sd (sd, attribute, uidret, gidret);
|
|
|
|
return 0;
|
2002-07-02 09:36:15 +08:00
|
|
|
}
|
2003-04-11 17:38:07 +08:00
|
|
|
/* The entries are already set to default values */
|
|
|
|
return -1;
|
2002-07-02 09:36:15 +08:00
|
|
|
}
|
|
|
|
|
2004-04-08 15:57:28 +08:00
|
|
|
int
|
2007-07-20 22:29:43 +08:00
|
|
|
get_file_attribute (HANDLE handle, path_conv &pc,
|
2004-04-08 15:57:28 +08:00
|
|
|
mode_t *attribute, __uid32_t *uidret, __gid32_t *gidret)
|
|
|
|
{
|
2008-07-15 04:22:03 +08:00
|
|
|
if (pc.has_acls ())
|
2004-04-08 15:57:28 +08:00
|
|
|
{
|
2006-10-22 22:57:43 +08:00
|
|
|
security_descriptor sd;
|
|
|
|
|
2010-09-10 17:32:13 +08:00
|
|
|
if (!get_file_sd (handle, pc, sd, false))
|
2007-07-20 22:29:43 +08:00
|
|
|
{
|
|
|
|
get_info_from_sd (sd, attribute, uidret, gidret);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-09-24 20:41:33 +08:00
|
|
|
/* ENOSYS is returned by get_file_sd if fetching the DACL from a remote
|
|
|
|
share returns STATUS_INVALID_NETWORK_RESPONSE, which in turn is
|
|
|
|
converted to ERROR_BAD_NET_RESP. This potentially occurs when trying
|
|
|
|
to fetch DACLs from a NT4 machine which is not part of the domain of
|
|
|
|
the requesting machine. */
|
|
|
|
else if (get_errno () != ENOSYS)
|
2008-02-16 01:53:11 +08:00
|
|
|
{
|
2007-07-30 18:58:16 +08:00
|
|
|
if (uidret)
|
|
|
|
*uidret = ILLEGAL_UID;
|
|
|
|
if (gidret)
|
|
|
|
*gidret = ILLEGAL_GID;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-08 15:57:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (uidret)
|
|
|
|
*uidret = myself->uid;
|
|
|
|
if (gidret)
|
|
|
|
*gidret = myself->gid;
|
|
|
|
|
2007-03-01 23:13:47 +08:00
|
|
|
return -1;
|
2004-04-08 15:57:28 +08:00
|
|
|
}
|
|
|
|
|
2003-12-08 06:37:12 +08:00
|
|
|
bool
|
2001-04-20 21:02:32 +08:00
|
|
|
add_access_allowed_ace (PACL acl, int offset, DWORD attributes,
|
|
|
|
PSID sid, size_t &len_add, DWORD inherit)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-04-29 18:38:12 +08:00
|
|
|
NTSTATUS status = RtlAddAccessAllowedAce (acl, ACL_REVISION, attributes, sid);
|
|
|
|
if (!NT_SUCCESS (status))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-04-29 18:38:12 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2003-12-08 06:37:12 +08:00
|
|
|
return false;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
ACCESS_ALLOWED_ACE *ace;
|
2011-04-28 17:30:36 +08:00
|
|
|
if (inherit && NT_SUCCESS (RtlGetAce (acl, offset, (PVOID *) &ace)))
|
2000-02-18 03:38:33 +08:00
|
|
|
ace->Header.AceFlags |= inherit;
|
2011-04-28 16:34:49 +08:00
|
|
|
len_add += sizeof (ACCESS_ALLOWED_ACE) - sizeof (DWORD) + RtlLengthSid (sid);
|
2003-12-08 06:37:12 +08:00
|
|
|
return true;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2003-12-08 06:37:12 +08:00
|
|
|
bool
|
2001-04-20 21:02:32 +08:00
|
|
|
add_access_denied_ace (PACL acl, int offset, DWORD attributes,
|
|
|
|
PSID sid, size_t &len_add, DWORD inherit)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-04-29 18:38:12 +08:00
|
|
|
NTSTATUS status = RtlAddAccessDeniedAce (acl, ACL_REVISION, attributes, sid);
|
|
|
|
if (!NT_SUCCESS (status))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-04-29 18:38:12 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2003-12-08 06:37:12 +08:00
|
|
|
return false;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
ACCESS_DENIED_ACE *ace;
|
2011-04-28 17:30:36 +08:00
|
|
|
if (inherit && NT_SUCCESS (RtlGetAce (acl, offset, (PVOID *) &ace)))
|
2000-02-18 03:38:33 +08:00
|
|
|
ace->Header.AceFlags |= inherit;
|
2011-04-28 16:34:49 +08:00
|
|
|
len_add += sizeof (ACCESS_DENIED_ACE) - sizeof (DWORD) + RtlLengthSid (sid);
|
2003-12-08 06:37:12 +08:00
|
|
|
return true;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2003-11-26 21:23:27 +08:00
|
|
|
static PSECURITY_DESCRIPTOR
|
2008-08-14 22:05:04 +08:00
|
|
|
alloc_sd (path_conv &pc, __uid32_t uid, __gid32_t gid, int attribute,
|
2003-11-26 21:23:27 +08:00
|
|
|
security_descriptor &sd_ret)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-04-28 17:53:11 +08:00
|
|
|
NTSTATUS status;
|
|
|
|
BOOLEAN dummy;
|
2010-09-10 22:53:44 +08:00
|
|
|
tmp_pathbuf tp;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2009-10-31 03:58:53 +08:00
|
|
|
/* NOTE: If the high bit of attribute is set, we have just created
|
|
|
|
a file or directory. See below for an explanation. */
|
|
|
|
|
2002-11-20 17:23:21 +08:00
|
|
|
debug_printf("uid %d, gid %d, attribute %x", uid, gid, attribute);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-11-20 17:23:21 +08:00
|
|
|
/* Get owner and group from current security descriptor. */
|
|
|
|
PSID cur_owner_sid = NULL;
|
|
|
|
PSID cur_group_sid = NULL;
|
2011-04-28 17:53:11 +08:00
|
|
|
status = RtlGetOwnerSecurityDescriptor (sd_ret, &cur_owner_sid, &dummy);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
debug_printf ("RtlGetOwnerSecurityDescriptor: %p", status);
|
|
|
|
status = RtlGetGroupSecurityDescriptor (sd_ret, &cur_group_sid, &dummy);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
debug_printf ("RtlGetGroupSecurityDescriptor: %p", status);
|
2002-11-20 17:23:21 +08:00
|
|
|
|
2002-06-21 20:37:51 +08:00
|
|
|
/* Get SID of owner. */
|
2003-01-12 19:38:51 +08:00
|
|
|
cygsid owner_sid;
|
2002-06-21 20:37:51 +08:00
|
|
|
/* Check for current user first */
|
|
|
|
if (uid == myself->uid)
|
|
|
|
owner_sid = cygheap->user.sid ();
|
2002-11-20 17:23:21 +08:00
|
|
|
else if (uid == ILLEGAL_UID)
|
|
|
|
owner_sid = cur_owner_sid;
|
2002-12-10 Pierre Humblet <pierre.humblet@ieee.org>
* pwdgrp.h (pwdgrp_check::pwdgrp_state): Replace by
pwdgrp_check::isinitializing ().
(pwdgrp_check::isinitializing): Create.
* passwd.cc (grab_int): Change type to unsigned, use strtoul and
set the pointer content to 0 if the field is invalid.
(parse_pwd): Move validity test after getting pw_gid.
(read_etc_passwd): Replace "passwd_state <= " by
passwd_state::isinitializing ().
(internal_getpwuid): Ditto.
(internal_getpwnam): Ditto.
(getpwent): Ditto.
(getpass): Ditto.
* grp.cc (parse_grp): Use strtoul for gr_gid and verify the validity.
(read_etc_group): Replace "group_state <= " by
group_state::isinitializing ().
(internal_getgrgid): Ditto.
(getgrent32): Ditto.
(internal_getgrent): Ditto.
2002-12-10 Pierre Humblet <pierre.humblet@ieee.org>
* security.h: Move declarations of internal_getgrent,
internal_getpwsid and internal_getgrsid to pwdgrp.h.
* pwdgrp.h: Declare internal_getpwsid, internal_getpwnam,
internal_getpwuid, internal_getgrsid, internal_getgrgid,
internal_getgrnam, internal_getgrent and internal_getgroups.
Delete "emulated" from enum pwdgrp_state.
(pwdgrp_check::isuninitialized): Create.
(pwdgrp_check::pwdgrp_state): Change state to initializing
rather than to uninitialized.
(pwdgrp_read::gets): Remove trailing CRs.
* passwd.cc (grab_string): Don't look for NLs.
(grab_int): Ditto.
(parse_pwd): Don't look for CRs. Return 0 if entry is too short.
(search_for): Delete.
(read_etc_passwd): Simplify tests to actually read the file.
Set state to loaded before making internal_getpwXX calls.
Replace search_for calls by equivalent internal_pwgetXX calls.
(internal_getpwsid): Use passwd_state.isuninitialized to decide
to call read_etc_passwd.
(internal_getpwuid): Create.
(internal_getpwnam): Create.
(getpwuid32): Simply call internal_getpwuid.
(getpwuid_r32): Call internal_getpwuid.
(getpwnam): Simply call internal_getpwnam.
(getpwnam_r): Call internal_getpwnam.
* grp.cc (parse_grp): Don't look for CRs. Adjust blank space.
(add_grp_line): Adjust blank space.
(class group_lock): Ditto.
(read_etc_group): Simplify tests to actually read the file.
Set state to loaded before making internal_getgrXX calls.
Replace getgrXX calls by equivalent internal calls.
(internal_getgrsid): Use group_state.isuninitialized to decide
to call read_etc_group.
(internal_getgrgid): Create.
(internal_getgrnam): Create.
(getgroups32): Simply call internal_getgrgid.
(getgrnam32): Simply call internal_getgrnam.
(internal_getgrent): Call group_state.isuninitialized.
(internal_getgroups): Create from the former getgroups32, using
two of the four arguments. Set gid to myself->gid and username
to cygheap->user.name ().
(getgroups32): Simply call internal_getgroup.
(getgroups): Call internal_getgroup instead of getgroups32.
(setgroups32): Call internal versions of get{pw,gr}XX.
* sec_helper.cc: Include pwdgrp.h.
(is_grp_member): Call internal versions of get{pw,gr}XX.
* security.cc: Include pwdgrp.h.
(alloc_sd): Call internal versions of get{pw,gr}XX.
* syscalls.cc: Include pwdgrp.h.
(seteuid32): Call internal versions of get{pw,gr}XX.
(setegid32): Ditto.
* uinfo.cc: Include pwdgrp.h.
(internal_getlogin): Call internal versions of get{pw,gr}XX.
(cygheap_user::ontherange): Ditto.
* sec_acl.cc: Include pwdgrp.h.
(setacl): Call internal versions of get{pw,gr}XX.
(acl_access): Ditto and simplify logic.
(aclfromtext): Ditto.
2002-12-10 20:43:49 +08:00
|
|
|
else if (!owner_sid.getfrompw (internal_getpwuid (uid)))
|
2002-06-21 20:37:51 +08:00
|
|
|
{
|
2002-11-20 17:23:21 +08:00
|
|
|
set_errno (EINVAL);
|
|
|
|
return NULL;
|
2002-06-21 20:37:51 +08:00
|
|
|
}
|
|
|
|
owner_sid.debug_print ("alloc_sd: owner SID =");
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-06-21 20:37:51 +08:00
|
|
|
/* Get SID of new group. */
|
2003-01-12 19:38:51 +08:00
|
|
|
cygsid group_sid;
|
2002-10-23 00:22:10 +08:00
|
|
|
/* Check for current user first */
|
|
|
|
if (gid == myself->gid)
|
|
|
|
group_sid = cygheap->user.groups.pgsid;
|
2002-11-20 17:23:21 +08:00
|
|
|
else if (gid == ILLEGAL_GID)
|
|
|
|
group_sid = cur_group_sid;
|
2002-12-10 Pierre Humblet <pierre.humblet@ieee.org>
* pwdgrp.h (pwdgrp_check::pwdgrp_state): Replace by
pwdgrp_check::isinitializing ().
(pwdgrp_check::isinitializing): Create.
* passwd.cc (grab_int): Change type to unsigned, use strtoul and
set the pointer content to 0 if the field is invalid.
(parse_pwd): Move validity test after getting pw_gid.
(read_etc_passwd): Replace "passwd_state <= " by
passwd_state::isinitializing ().
(internal_getpwuid): Ditto.
(internal_getpwnam): Ditto.
(getpwent): Ditto.
(getpass): Ditto.
* grp.cc (parse_grp): Use strtoul for gr_gid and verify the validity.
(read_etc_group): Replace "group_state <= " by
group_state::isinitializing ().
(internal_getgrgid): Ditto.
(getgrent32): Ditto.
(internal_getgrent): Ditto.
2002-12-10 Pierre Humblet <pierre.humblet@ieee.org>
* security.h: Move declarations of internal_getgrent,
internal_getpwsid and internal_getgrsid to pwdgrp.h.
* pwdgrp.h: Declare internal_getpwsid, internal_getpwnam,
internal_getpwuid, internal_getgrsid, internal_getgrgid,
internal_getgrnam, internal_getgrent and internal_getgroups.
Delete "emulated" from enum pwdgrp_state.
(pwdgrp_check::isuninitialized): Create.
(pwdgrp_check::pwdgrp_state): Change state to initializing
rather than to uninitialized.
(pwdgrp_read::gets): Remove trailing CRs.
* passwd.cc (grab_string): Don't look for NLs.
(grab_int): Ditto.
(parse_pwd): Don't look for CRs. Return 0 if entry is too short.
(search_for): Delete.
(read_etc_passwd): Simplify tests to actually read the file.
Set state to loaded before making internal_getpwXX calls.
Replace search_for calls by equivalent internal_pwgetXX calls.
(internal_getpwsid): Use passwd_state.isuninitialized to decide
to call read_etc_passwd.
(internal_getpwuid): Create.
(internal_getpwnam): Create.
(getpwuid32): Simply call internal_getpwuid.
(getpwuid_r32): Call internal_getpwuid.
(getpwnam): Simply call internal_getpwnam.
(getpwnam_r): Call internal_getpwnam.
* grp.cc (parse_grp): Don't look for CRs. Adjust blank space.
(add_grp_line): Adjust blank space.
(class group_lock): Ditto.
(read_etc_group): Simplify tests to actually read the file.
Set state to loaded before making internal_getgrXX calls.
Replace getgrXX calls by equivalent internal calls.
(internal_getgrsid): Use group_state.isuninitialized to decide
to call read_etc_group.
(internal_getgrgid): Create.
(internal_getgrnam): Create.
(getgroups32): Simply call internal_getgrgid.
(getgrnam32): Simply call internal_getgrnam.
(internal_getgrent): Call group_state.isuninitialized.
(internal_getgroups): Create from the former getgroups32, using
two of the four arguments. Set gid to myself->gid and username
to cygheap->user.name ().
(getgroups32): Simply call internal_getgroup.
(getgroups): Call internal_getgroup instead of getgroups32.
(setgroups32): Call internal versions of get{pw,gr}XX.
* sec_helper.cc: Include pwdgrp.h.
(is_grp_member): Call internal versions of get{pw,gr}XX.
* security.cc: Include pwdgrp.h.
(alloc_sd): Call internal versions of get{pw,gr}XX.
* syscalls.cc: Include pwdgrp.h.
(seteuid32): Call internal versions of get{pw,gr}XX.
(setegid32): Ditto.
* uinfo.cc: Include pwdgrp.h.
(internal_getlogin): Call internal versions of get{pw,gr}XX.
(cygheap_user::ontherange): Ditto.
* sec_acl.cc: Include pwdgrp.h.
(setacl): Call internal versions of get{pw,gr}XX.
(acl_access): Ditto and simplify logic.
(aclfromtext): Ditto.
2002-12-10 20:43:49 +08:00
|
|
|
else if (!group_sid.getfromgr (internal_getgrgid (gid)))
|
2002-11-20 17:23:21 +08:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
group_sid.debug_print ("alloc_sd: group SID =");
|
|
|
|
|
2000-10-23 16:02:38 +08:00
|
|
|
/* Initialize local security descriptor. */
|
2000-02-18 03:38:33 +08:00
|
|
|
SECURITY_DESCRIPTOR sd;
|
2011-04-28 17:30:36 +08:00
|
|
|
RtlCreateSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2009-11-03 17:31:45 +08:00
|
|
|
/* We set the SE_DACL_PROTECTED flag here to prevent the DACL from being
|
Drop NT4 support.
* autoload.cc (DnsQuery_A): Fatal if not available.
(DnsRecordListFree): Ditto.
(DsGetDcNameW): Ditto.
(NetGetAnyDCName): Remove.
(NetGetDCName): Remove.
(EnumProcessModules): Fatal if not available.
(GetModuleFileNameExW): Ditto.
(GetModuleInformation): Ditto.
(GetProcessMemoryInfo): Ditto.
(QueryWorkingSet): Ditto.
(LsaRegisterLogonProcess): Ditto.
* fenv.cc (_feinitialise): Drop supports_sse condition.
* fhandler_disk_file.cc (path_conv::isgood_inode): Fix comment.
(fhandler_base::fstat_by_name): Drop has_fileid_dirinfo condition.
(fhandler_disk_file::opendir): Ditto.
* fhandler_netdrive.cc (fhandler_netdrive::readdir): Fix comment.
* fhandler_proc.cc (format_proc_partitions): Drop NT4-only code.
* fhandler_process.cc (get_process_state): Ditto.
* kernel32.cc (GetWindowsDirectoryW): Remove.
(GetWindowsDirectoryA): Remove.
* miscfuncs.cc (nice_to_winprio): Drop NT4-only code.
* mount.cc (fs_info::update): Fix comments.
* net.cc (get_2k_ifs): Drop NT4-only code.
* sec_auth.cc (get_logon_server): Ditto.
(lsaauth): Drop NT4-specific error handling.
* security.cc (alloc_sd): Set SE_DACL_PROTECTED unconditionally.
* select.cc (select_stuff::wait): Always use MWMO_INPUTAVAILABLE.
(peek_windows): Drop NT4-only condition in call to PeekMessage.
* syscalls.cc (gethostid): Remove NT4-only workaround.
* wincap.cc: Througout, drop has_dacl_protect,
has_broken_if_oper_status, has_process_io_counters,
has_terminal_services, has_extended_priority_class, has_guid_volumes,
has_fileid_dirinfo, has_mwmo_inputavailable and supports_sse from
wincaps.
(wincap_nt4sp4): Remove.
(wincap_minimal): Set to wincap_2000.
(wincapc::init): Rely on availability of OSVERSIONINFOEX structure.
Treat error from GetVersionEx as fatal. Treat NT4 as fatal.
* wincap.h (struct wincaps): Drop has_dacl_protect,
has_broken_if_oper_status, has_process_io_counters,
has_terminal_services, has_extended_priority_class, has_guid_volumes,
has_fileid_dirinfo, has_mwmo_inputavailable and supports_sse flags
and methods.
* winlean.h (GetWindowsDirectoryW) Define as GetSystemWindowsDirectoryW.
(GetWindowsDirectoryA): Define as GetSystemWindowsDirectoryA.
2011-04-04 20:23:36 +08:00
|
|
|
modified by inheritable ACEs. */
|
2011-04-28 15:27:51 +08:00
|
|
|
RtlSetControlSecurityDescriptor (&sd, SE_DACL_PROTECTED, SE_DACL_PROTECTED);
|
2009-11-03 17:31:45 +08:00
|
|
|
|
2000-10-23 16:02:38 +08:00
|
|
|
/* Create owner for local security descriptor. */
|
2011-04-28 23:54:47 +08:00
|
|
|
status = RtlSetOwnerSecurityDescriptor (&sd, owner_sid, FALSE);
|
|
|
|
if (!NT_SUCCESS (status))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-04-28 23:54:47 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2000-02-18 03:38:33 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-10-23 16:02:38 +08:00
|
|
|
|
|
|
|
/* Create group for local security descriptor. */
|
2011-04-28 23:54:47 +08:00
|
|
|
status = RtlSetGroupSecurityDescriptor (&sd, group_sid, FALSE);
|
|
|
|
if (!NT_SUCCESS (status))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-04-28 23:54:47 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2000-02-18 03:38:33 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-10-23 16:02:38 +08:00
|
|
|
/* Initialize local access control list. */
|
2010-09-10 22:53:44 +08:00
|
|
|
PACL acl = (PACL) tp.w_get ();
|
2011-04-28 17:30:36 +08:00
|
|
|
RtlCreateAcl (acl, ACL_MAXIMUM_SIZE, ACL_REVISION);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-10-23 16:02:38 +08:00
|
|
|
/* From here fill ACL. */
|
2000-02-18 03:38:33 +08:00
|
|
|
size_t acl_len = sizeof (ACL);
|
|
|
|
int ace_off = 0;
|
2010-04-20 03:52:43 +08:00
|
|
|
/* Only used for sync objects (for ttys). The admins group should
|
|
|
|
always have the right to manipulate the ACL, so we have to make sure
|
|
|
|
that the ACL gives the admins group STANDARD_RIGHTS_ALL access. */
|
|
|
|
bool saw_admins = false;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2008-08-14 22:05:04 +08:00
|
|
|
/* Construct allow attribute for owner.
|
|
|
|
Don't set FILE_READ/WRITE_ATTRIBUTES unconditionally on Samba, otherwise
|
|
|
|
it enforces read permissions. Same for other's below. */
|
2003-03-11 00:25:48 +08:00
|
|
|
DWORD owner_allow = STANDARD_RIGHTS_ALL
|
2008-08-19 17:46:31 +08:00
|
|
|
| (pc.fs_is_samba ()
|
|
|
|
? 0 : (FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES));
|
2000-02-18 03:38:33 +08:00
|
|
|
if (attribute & S_IRUSR)
|
|
|
|
owner_allow |= FILE_GENERIC_READ;
|
|
|
|
if (attribute & S_IWUSR)
|
2003-03-11 00:25:48 +08:00
|
|
|
owner_allow |= FILE_GENERIC_WRITE;
|
2000-02-18 03:38:33 +08:00
|
|
|
if (attribute & S_IXUSR)
|
2008-08-14 22:05:04 +08:00
|
|
|
owner_allow |= FILE_GENERIC_EXECUTE & ~FILE_READ_ATTRIBUTES;
|
2003-03-20 05:34:38 +08:00
|
|
|
if (S_ISDIR (attribute)
|
|
|
|
&& (attribute & (S_IWUSR | S_IXUSR)) == (S_IWUSR | S_IXUSR))
|
2000-02-18 03:38:33 +08:00
|
|
|
owner_allow |= FILE_DELETE_CHILD;
|
2010-04-20 03:52:43 +08:00
|
|
|
/* For sync objects note that the owner is admin. */
|
|
|
|
if (S_ISCHR (attribute) && owner_sid == well_known_admins_sid)
|
|
|
|
saw_admins = true;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-10-23 16:02:38 +08:00
|
|
|
/* Construct allow attribute for group. */
|
2010-04-20 03:52:43 +08:00
|
|
|
DWORD group_allow = STANDARD_RIGHTS_READ | SYNCHRONIZE
|
2008-10-14 00:01:50 +08:00
|
|
|
| (pc.fs_is_samba () ? 0 : FILE_READ_ATTRIBUTES);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (attribute & S_IRGRP)
|
|
|
|
group_allow |= FILE_GENERIC_READ;
|
|
|
|
if (attribute & S_IWGRP)
|
2008-08-14 22:05:04 +08:00
|
|
|
group_allow |= FILE_GENERIC_WRITE;
|
2000-02-18 03:38:33 +08:00
|
|
|
if (attribute & S_IXGRP)
|
2008-08-14 22:05:04 +08:00
|
|
|
group_allow |= FILE_GENERIC_EXECUTE & ~FILE_READ_ATTRIBUTES;
|
2003-03-20 05:34:38 +08:00
|
|
|
if (S_ISDIR (attribute)
|
|
|
|
&& (attribute & (S_IWGRP | S_IXGRP)) == (S_IWGRP | S_IXGRP)
|
|
|
|
&& !(attribute & S_ISVTX))
|
2000-02-18 03:38:33 +08:00
|
|
|
group_allow |= FILE_DELETE_CHILD;
|
2010-04-20 03:52:43 +08:00
|
|
|
/* For sync objects, add STANDARD_RIGHTS_ALL for admins group. */
|
|
|
|
if (S_ISCHR (attribute) && group_sid == well_known_admins_sid)
|
|
|
|
{
|
|
|
|
group_allow |= STANDARD_RIGHTS_ALL;
|
|
|
|
saw_admins = true;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-10-23 16:02:38 +08:00
|
|
|
/* Construct allow attribute for everyone. */
|
2010-04-20 03:52:43 +08:00
|
|
|
DWORD other_allow = STANDARD_RIGHTS_READ | SYNCHRONIZE
|
2008-08-14 22:05:04 +08:00
|
|
|
| (pc.fs_is_samba () ? 0 : FILE_READ_ATTRIBUTES);
|
2000-02-18 03:38:33 +08:00
|
|
|
if (attribute & S_IROTH)
|
|
|
|
other_allow |= FILE_GENERIC_READ;
|
|
|
|
if (attribute & S_IWOTH)
|
2008-08-14 22:05:04 +08:00
|
|
|
other_allow |= FILE_GENERIC_WRITE;
|
2000-02-18 03:38:33 +08:00
|
|
|
if (attribute & S_IXOTH)
|
2008-08-14 22:05:04 +08:00
|
|
|
other_allow |= FILE_GENERIC_EXECUTE & ~FILE_READ_ATTRIBUTES;
|
2003-03-20 05:34:38 +08:00
|
|
|
if (S_ISDIR (attribute)
|
|
|
|
&& (attribute & (S_IWOTH | S_IXOTH)) == (S_IWOTH | S_IXOTH)
|
2001-07-16 06:40:07 +08:00
|
|
|
&& !(attribute & S_ISVTX))
|
2000-02-18 03:38:33 +08:00
|
|
|
other_allow |= FILE_DELETE_CHILD;
|
2000-10-28 13:41:44 +08:00
|
|
|
|
2001-07-16 06:40:07 +08:00
|
|
|
/* Construct SUID, SGID and VTX bits in NULL ACE. */
|
|
|
|
DWORD null_allow = 0L;
|
|
|
|
if (attribute & (S_ISUID | S_ISGID | S_ISVTX))
|
|
|
|
{
|
|
|
|
if (attribute & S_ISUID)
|
2001-09-08 05:32:07 +08:00
|
|
|
null_allow |= FILE_APPEND_DATA;
|
2001-07-16 06:40:07 +08:00
|
|
|
if (attribute & S_ISGID)
|
2001-09-08 05:32:07 +08:00
|
|
|
null_allow |= FILE_WRITE_DATA;
|
2001-07-16 06:40:07 +08:00
|
|
|
if (attribute & S_ISVTX)
|
2001-09-08 05:32:07 +08:00
|
|
|
null_allow |= FILE_READ_DATA;
|
2001-07-16 06:40:07 +08:00
|
|
|
}
|
|
|
|
|
2002-11-20 17:23:21 +08:00
|
|
|
/* Add owner and group permissions if SIDs are equal
|
|
|
|
and construct deny attributes for group and owner. */
|
2003-12-08 06:37:12 +08:00
|
|
|
bool isownergroup;
|
2003-01-12 19:38:51 +08:00
|
|
|
if ((isownergroup = (owner_sid == group_sid)))
|
|
|
|
owner_allow |= group_allow;
|
|
|
|
|
2002-11-20 17:23:21 +08:00
|
|
|
DWORD owner_deny = ~owner_allow & (group_allow | other_allow);
|
2000-02-18 03:38:33 +08:00
|
|
|
owner_deny &= ~(STANDARD_RIGHTS_READ
|
2008-10-14 00:01:50 +08:00
|
|
|
| FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2003-01-12 19:38:51 +08:00
|
|
|
DWORD group_deny = ~group_allow & other_allow;
|
2008-10-14 00:01:50 +08:00
|
|
|
group_deny &= ~(STANDARD_RIGHTS_READ | FILE_READ_ATTRIBUTES);
|
2000-05-25 04:09:43 +08:00
|
|
|
|
2000-10-23 16:02:38 +08:00
|
|
|
/* Set deny ACE for owner. */
|
2000-02-18 03:38:33 +08:00
|
|
|
if (owner_deny
|
2001-01-28 13:51:15 +08:00
|
|
|
&& !add_access_denied_ace (acl, ace_off++, owner_deny,
|
2003-01-12 19:38:51 +08:00
|
|
|
owner_sid, acl_len, NO_INHERITANCE))
|
2001-07-16 06:40:07 +08:00
|
|
|
return NULL;
|
2002-11-20 17:23:21 +08:00
|
|
|
/* Set deny ACE for group here to respect the canonical order,
|
|
|
|
if this does not impact owner */
|
2003-01-12 19:38:51 +08:00
|
|
|
if (group_deny && !(group_deny & owner_allow) && !isownergroup
|
|
|
|
&& !add_access_denied_ace (acl, ace_off++, group_deny,
|
|
|
|
group_sid, acl_len, NO_INHERITANCE))
|
|
|
|
return NULL;
|
2000-10-23 16:02:38 +08:00
|
|
|
/* Set allow ACE for owner. */
|
2001-01-28 13:51:15 +08:00
|
|
|
if (!add_access_allowed_ace (acl, ace_off++, owner_allow,
|
2003-01-12 19:38:51 +08:00
|
|
|
owner_sid, acl_len, NO_INHERITANCE))
|
2000-02-18 03:38:33 +08:00
|
|
|
return NULL;
|
2002-11-20 17:23:21 +08:00
|
|
|
/* Set deny ACE for group, if still needed. */
|
2003-01-12 19:38:51 +08:00
|
|
|
if (group_deny & owner_allow && !isownergroup
|
2001-01-28 13:51:15 +08:00
|
|
|
&& !add_access_denied_ace (acl, ace_off++, group_deny,
|
2003-01-12 19:38:51 +08:00
|
|
|
group_sid, acl_len, NO_INHERITANCE))
|
2001-07-16 06:40:07 +08:00
|
|
|
return NULL;
|
2000-10-23 16:02:38 +08:00
|
|
|
/* Set allow ACE for group. */
|
2003-01-12 19:38:51 +08:00
|
|
|
if (!isownergroup
|
2002-11-20 17:23:21 +08:00
|
|
|
&& !add_access_allowed_ace (acl, ace_off++, group_allow,
|
2003-01-12 19:38:51 +08:00
|
|
|
group_sid, acl_len, NO_INHERITANCE))
|
2000-02-18 03:38:33 +08:00
|
|
|
return NULL;
|
|
|
|
|
2010-04-20 03:52:43 +08:00
|
|
|
/* For sync objects, if we didn't see the admins group so far, add entry
|
|
|
|
with STANDARD_RIGHTS_ALL access. */
|
|
|
|
if (S_ISCHR (attribute) && !saw_admins)
|
|
|
|
{
|
|
|
|
if (!add_access_allowed_ace (acl, ace_off++, STANDARD_RIGHTS_ALL,
|
|
|
|
well_known_admins_sid, acl_len,
|
|
|
|
NO_INHERITANCE))
|
|
|
|
return NULL;
|
|
|
|
saw_admins = true;
|
|
|
|
}
|
|
|
|
|
2001-04-26 17:21:33 +08:00
|
|
|
/* Set allow ACE for everyone. */
|
|
|
|
if (!add_access_allowed_ace (acl, ace_off++, other_allow,
|
2003-01-12 19:38:51 +08:00
|
|
|
well_known_world_sid, acl_len, NO_INHERITANCE))
|
2001-04-26 17:21:33 +08:00
|
|
|
return NULL;
|
2001-07-16 06:40:07 +08:00
|
|
|
/* Set null ACE for special bits. */
|
|
|
|
if (null_allow
|
|
|
|
&& !add_access_allowed_ace (acl, ace_off++, null_allow,
|
2002-07-02 16:11:15 +08:00
|
|
|
well_known_null_sid, acl_len, NO_INHERITANCE))
|
2001-07-16 06:40:07 +08:00
|
|
|
return NULL;
|
2001-04-26 17:21:33 +08:00
|
|
|
|
2000-10-23 16:02:38 +08:00
|
|
|
/* Fill ACL with unrelated ACEs from current security descriptor. */
|
2000-02-18 03:38:33 +08:00
|
|
|
PACL oacl;
|
2011-04-28 17:53:11 +08:00
|
|
|
BOOLEAN acl_exists = FALSE;
|
2000-02-18 03:38:33 +08:00
|
|
|
ACCESS_ALLOWED_ACE *ace;
|
2011-04-28 17:30:36 +08:00
|
|
|
|
2011-04-28 17:53:11 +08:00
|
|
|
status = RtlGetDaclSecurityDescriptor (sd_ret, &acl_exists, &oacl, &dummy);
|
|
|
|
if (NT_SUCCESS (status) && acl_exists && oacl)
|
2000-02-18 03:38:33 +08:00
|
|
|
for (DWORD i = 0; i < oacl->AceCount; ++i)
|
2011-04-28 17:30:36 +08:00
|
|
|
if (NT_SUCCESS (RtlGetAce (oacl, i, (PVOID *) &ace)))
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2003-02-05 21:47:47 +08:00
|
|
|
cygpsid ace_sid ((PSID) &ace->SidStart);
|
|
|
|
|
2010-12-16 18:31:09 +08:00
|
|
|
/* Always skip NULL SID as well as admins SID on virtual device files
|
|
|
|
in /proc/sys. */
|
2010-04-20 03:52:43 +08:00
|
|
|
if (ace_sid == well_known_null_sid
|
|
|
|
|| (S_ISCHR (attribute) && ace_sid == well_known_admins_sid))
|
2003-01-12 19:38:51 +08:00
|
|
|
continue;
|
2010-12-16 18:31:09 +08:00
|
|
|
/* Check for ACEs which are always created in the preceding code
|
|
|
|
and check for the default inheritence ACEs which will be created
|
|
|
|
for just created directories. Skip them for just created
|
|
|
|
directories or if they are not inherited. If they are inherited,
|
|
|
|
make sure they are *only* inherited, so they don't collide with
|
|
|
|
the permissions set in this function. */
|
2002-11-20 17:23:21 +08:00
|
|
|
if ((ace_sid == cur_owner_sid)
|
|
|
|
|| (ace_sid == owner_sid)
|
|
|
|
|| (ace_sid == cur_group_sid)
|
2010-12-16 18:31:09 +08:00
|
|
|
|| (ace_sid == group_sid)
|
|
|
|
|| (ace_sid == well_known_creator_owner_sid)
|
|
|
|
|| (ace_sid == well_known_creator_group_sid)
|
|
|
|
|| (ace_sid == well_known_world_sid))
|
2003-01-12 19:38:51 +08:00
|
|
|
{
|
2010-12-16 18:31:09 +08:00
|
|
|
if ((S_ISDIR (attribute) && (attribute & S_JUSTCREATED))
|
|
|
|
|| (ace->Header.AceFlags
|
|
|
|
& (CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE)) == 0)
|
2003-01-12 19:38:51 +08:00
|
|
|
continue;
|
2010-12-16 18:31:09 +08:00
|
|
|
else
|
|
|
|
ace->Header.AceFlags |= INHERIT_ONLY_ACE;
|
2003-01-12 19:38:51 +08:00
|
|
|
}
|
2010-12-15 21:56:32 +08:00
|
|
|
if (attribute & S_JUSTCREATED)
|
2009-11-03 17:31:45 +08:00
|
|
|
{
|
|
|
|
/* Since files and dirs are created with a NULL descriptor,
|
|
|
|
inheritence rules kick in. If no inheritable entries exist
|
|
|
|
in the parent object, Windows will create entries from the
|
|
|
|
user token's default DACL in the file DACL. These entries
|
|
|
|
are not desired and we drop them silently. */
|
|
|
|
if (!(ace->Header.AceFlags & INHERITED_ACE))
|
|
|
|
continue;
|
|
|
|
/* Remove the INHERITED_ACE flag since on POSIX systems
|
|
|
|
inheritance is settled when the file has been created.
|
|
|
|
This also avoids error messages in Windows Explorer when
|
|
|
|
opening a file's security tab. Explorer complains if
|
|
|
|
inheritable ACEs are preceding non-inheritable ACEs. */
|
|
|
|
ace->Header.AceFlags &= ~INHERITED_ACE;
|
|
|
|
}
|
2000-10-23 16:02:38 +08:00
|
|
|
/*
|
|
|
|
* Add unrelated ACCESS_DENIED_ACE to the beginning but
|
2001-04-26 17:21:33 +08:00
|
|
|
* behind the owner_deny, ACCESS_ALLOWED_ACE to the end.
|
2009-10-31 03:58:53 +08:00
|
|
|
* FIXME: this would break the order of the inherit-only ACEs
|
2000-10-23 16:02:38 +08:00
|
|
|
*/
|
2011-04-28 17:30:36 +08:00
|
|
|
status = RtlAddAce (acl, ACL_REVISION,
|
|
|
|
ace->Header.AceType == ACCESS_DENIED_ACE_TYPE
|
|
|
|
? (owner_deny ? 1 : 0) : MAXDWORD,
|
|
|
|
(LPVOID) ace, ace->Header.AceSize);
|
|
|
|
if (!NT_SUCCESS (status))
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2011-04-28 17:30:36 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2000-09-03 12:16:35 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-11-03 17:31:45 +08:00
|
|
|
ace_off++;
|
2000-09-03 12:16:35 +08:00
|
|
|
acl_len += ace->Header.AceSize;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2010-12-16 18:31:09 +08:00
|
|
|
/* Construct appropriate inherit attribute for new directories. Keep in
|
|
|
|
mind that we do this only for the sake of non-Cygwin applications.
|
|
|
|
Cygwin applications don't need this. */
|
2009-10-31 03:58:53 +08:00
|
|
|
if (S_ISDIR (attribute) && (attribute & S_JUSTCREATED))
|
2003-01-12 19:38:51 +08:00
|
|
|
{
|
2009-10-31 03:58:53 +08:00
|
|
|
const DWORD inherit = CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE
|
|
|
|
| INHERIT_ONLY_ACE;
|
2003-01-12 19:38:51 +08:00
|
|
|
#if 0 /* FIXME: Not done currently as this breaks the canonical order */
|
|
|
|
/* Set deny ACE for owner. */
|
|
|
|
if (owner_deny
|
|
|
|
&& !add_access_denied_ace (acl, ace_off++, owner_deny,
|
|
|
|
well_known_creator_owner_sid, acl_len, inherit))
|
|
|
|
return NULL;
|
|
|
|
/* Set deny ACE for group here to respect the canonical order,
|
|
|
|
if this does not impact owner */
|
|
|
|
if (group_deny && !(group_deny & owner_allow)
|
|
|
|
&& !add_access_denied_ace (acl, ace_off++, group_deny,
|
|
|
|
well_known_creator_group_sid, acl_len, inherit))
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
/* Set allow ACE for owner. */
|
|
|
|
if (!add_access_allowed_ace (acl, ace_off++, owner_allow,
|
2009-10-31 03:58:53 +08:00
|
|
|
well_known_creator_owner_sid, acl_len,
|
|
|
|
inherit))
|
2003-01-12 19:38:51 +08:00
|
|
|
return NULL;
|
|
|
|
#if 0 /* FIXME: Not done currently as this breaks the canonical order and
|
|
|
|
won't be preserved on chown and chmod */
|
|
|
|
/* Set deny ACE for group, conflicting with owner_allow. */
|
|
|
|
if (group_deny & owner_allow
|
|
|
|
&& !add_access_denied_ace (acl, ace_off++, group_deny,
|
|
|
|
well_known_creator_group_sid, acl_len, inherit))
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
/* Set allow ACE for group. */
|
|
|
|
if (!add_access_allowed_ace (acl, ace_off++, group_allow,
|
2009-10-31 03:58:53 +08:00
|
|
|
well_known_creator_group_sid, acl_len,
|
|
|
|
inherit))
|
2003-01-12 19:38:51 +08:00
|
|
|
return NULL;
|
|
|
|
/* Set allow ACE for everyone. */
|
|
|
|
if (!add_access_allowed_ace (acl, ace_off++, other_allow,
|
|
|
|
well_known_world_sid, acl_len, inherit))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-10-23 16:02:38 +08:00
|
|
|
/* Set AclSize to computed value. */
|
2000-02-18 03:38:33 +08:00
|
|
|
acl->AclSize = acl_len;
|
|
|
|
debug_printf ("ACL-Size: %d", acl_len);
|
|
|
|
|
2000-10-23 16:02:38 +08:00
|
|
|
/* Create DACL for local security descriptor. */
|
2011-04-28 23:54:47 +08:00
|
|
|
status = RtlSetDaclSecurityDescriptor (&sd, TRUE, acl, FALSE);
|
|
|
|
if (!NT_SUCCESS (status))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-04-28 23:54:47 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2000-02-18 03:38:33 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-10-23 16:02:38 +08:00
|
|
|
/* Make self relative security descriptor. */
|
2003-11-26 21:23:27 +08:00
|
|
|
DWORD sd_size = 0;
|
2011-04-29 18:38:12 +08:00
|
|
|
RtlAbsoluteToSelfRelativeSD (&sd, sd_ret, &sd_size);
|
2003-11-26 21:23:27 +08:00
|
|
|
if (sd_size <= 0)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-11-26 21:23:27 +08:00
|
|
|
if (!sd_ret.malloc (sd_size))
|
|
|
|
{
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-29 18:38:12 +08:00
|
|
|
status = RtlAbsoluteToSelfRelativeSD (&sd, sd_ret, &sd_size);
|
|
|
|
if (!NT_SUCCESS (status))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-04-29 18:38:12 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2000-02-18 03:38:33 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-11-26 21:23:27 +08:00
|
|
|
debug_printf ("Created SD-Size: %u", sd_ret.size ());
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2003-01-12 19:38:51 +08:00
|
|
|
return sd_ret;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-08-07 23:09:54 +08:00
|
|
|
void
|
2008-08-14 22:05:04 +08:00
|
|
|
set_security_attribute (path_conv &pc, int attribute, PSECURITY_ATTRIBUTES psa,
|
2003-11-26 21:23:27 +08:00
|
|
|
security_descriptor &sd)
|
2001-08-07 23:09:54 +08:00
|
|
|
{
|
2003-11-26 21:23:27 +08:00
|
|
|
psa->lpSecurityDescriptor = sd.malloc (SECURITY_DESCRIPTOR_MIN_LENGTH);
|
2011-04-28 17:30:36 +08:00
|
|
|
RtlCreateSecurityDescriptor ((PSECURITY_DESCRIPTOR) psa->lpSecurityDescriptor,
|
2001-09-08 05:32:07 +08:00
|
|
|
SECURITY_DESCRIPTOR_REVISION);
|
2008-08-14 22:05:04 +08:00
|
|
|
psa->lpSecurityDescriptor = alloc_sd (pc, geteuid32 (), getegid32 (),
|
2004-05-29 03:50:07 +08:00
|
|
|
attribute, sd);
|
2001-08-07 23:09:54 +08:00
|
|
|
}
|
|
|
|
|
2010-04-20 03:52:43 +08:00
|
|
|
int
|
|
|
|
get_object_sd (HANDLE handle, security_descriptor &sd)
|
|
|
|
{
|
|
|
|
ULONG len = 0;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
status = NtQuerySecurityObject (handle, ALL_SECURITY_INFORMATION,
|
|
|
|
sd, len, &len);
|
|
|
|
if (status != STATUS_BUFFER_TOO_SMALL)
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!sd.malloc (len))
|
|
|
|
{
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
status = NtQuerySecurityObject (handle, ALL_SECURITY_INFORMATION,
|
|
|
|
sd, len, &len);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_object_attribute (HANDLE handle, __uid32_t *uidret, __gid32_t *gidret,
|
|
|
|
mode_t *attribute)
|
|
|
|
{
|
|
|
|
security_descriptor sd;
|
|
|
|
|
|
|
|
if (get_object_sd (handle, sd))
|
|
|
|
return -1;
|
|
|
|
get_info_from_sd (sd, attribute, uidret, gidret);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
create_object_sd_from_attribute (HANDLE handle, __uid32_t uid, __gid32_t gid,
|
|
|
|
mode_t attribute, security_descriptor &sd)
|
|
|
|
{
|
|
|
|
path_conv pc;
|
|
|
|
if ((handle && get_object_sd (handle, sd))
|
|
|
|
|| !alloc_sd (pc, uid, gid, attribute, sd))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
set_object_sd (HANDLE handle, security_descriptor &sd, bool chown)
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
status = NtSetSecurityObject (handle, chown ? ALL_SECURITY_INFORMATION
|
|
|
|
: DACL_SECURITY_INFORMATION, sd);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
set_object_attribute (HANDLE handle, __uid32_t uid, __gid32_t gid,
|
|
|
|
mode_t attribute)
|
|
|
|
{
|
|
|
|
security_descriptor sd;
|
|
|
|
|
|
|
|
if (create_object_sd_from_attribute (handle, uid, gid, attribute, sd)
|
|
|
|
|| set_object_sd (handle, sd, uid != ILLEGAL_UID || gid != ILLEGAL_GID))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
int
|
2007-07-20 22:29:43 +08:00
|
|
|
set_file_attribute (HANDLE handle, path_conv &pc,
|
2010-04-20 03:52:43 +08:00
|
|
|
__uid32_t uid, __gid32_t gid, mode_t attribute)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2007-07-20 01:42:11 +08:00
|
|
|
int ret = -1;
|
|
|
|
|
2008-07-15 04:22:03 +08:00
|
|
|
if (pc.has_acls ())
|
2007-07-20 01:42:11 +08:00
|
|
|
{
|
|
|
|
security_descriptor sd;
|
|
|
|
|
2011-03-08 22:26:15 +08:00
|
|
|
if (!get_file_sd (handle, pc, sd, (bool)(attribute & S_JUSTCREATED))
|
2008-08-14 22:05:04 +08:00
|
|
|
&& alloc_sd (pc, uid, gid, attribute, sd))
|
2009-04-09 17:19:03 +08:00
|
|
|
ret = set_file_sd (handle, pc, sd,
|
|
|
|
uid != ILLEGAL_UID || gid != ILLEGAL_GID);
|
2007-07-20 01:42:11 +08:00
|
|
|
}
|
2007-03-01 23:13:47 +08:00
|
|
|
else
|
|
|
|
ret = 0;
|
2007-08-16 22:46:23 +08:00
|
|
|
syscall_printf ("%d = set_file_attribute (%S, %d, %d, %p)",
|
|
|
|
ret, pc.get_nt_native_path (), uid, gid, attribute);
|
2000-03-17 03:35:18 +08:00
|
|
|
return ret;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2006-10-21 19:05:32 +08:00
|
|
|
static int
|
|
|
|
check_access (security_descriptor &sd, GENERIC_MAPPING &mapping,
|
2011-04-29 17:48:25 +08:00
|
|
|
ACCESS_MASK desired, int flags, bool effective)
|
2003-02-21 22:29:18 +08:00
|
|
|
{
|
|
|
|
int ret = -1;
|
2011-04-29 17:48:25 +08:00
|
|
|
NTSTATUS status, allow;
|
|
|
|
ACCESS_MASK granted;
|
2006-10-21 19:05:32 +08:00
|
|
|
DWORD plen = sizeof (PRIVILEGE_SET) + 3 * sizeof (LUID_AND_ATTRIBUTES);
|
|
|
|
PPRIVILEGE_SET pset = (PPRIVILEGE_SET) alloca (plen);
|
2009-09-25 21:44:45 +08:00
|
|
|
HANDLE tok = ((effective && cygheap->user.issetuid ())
|
|
|
|
? cygheap->user.imp_token ()
|
|
|
|
: hProcImpToken);
|
2003-02-21 22:29:18 +08:00
|
|
|
|
2010-02-26 22:51:59 +08:00
|
|
|
if (!tok)
|
|
|
|
{
|
|
|
|
if (!DuplicateTokenEx (hProcToken, MAXIMUM_ALLOWED, NULL,
|
2011-06-06 13:02:13 +08:00
|
|
|
SecurityImpersonation, TokenImpersonation,
|
2010-02-26 22:51:59 +08:00
|
|
|
&hProcImpToken))
|
2011-06-06 13:02:13 +08:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return ret;
|
|
|
|
}
|
2010-02-26 22:51:59 +08:00
|
|
|
tok = hProcImpToken;
|
2011-06-06 13:02:13 +08:00
|
|
|
}
|
2006-12-13 00:27:32 +08:00
|
|
|
|
2011-04-29 17:48:25 +08:00
|
|
|
status = NtAccessCheck (sd, tok, desired, &mapping, pset, &plen, &granted,
|
|
|
|
&allow);
|
|
|
|
if (!NT_SUCCESS (status))
|
2003-02-21 22:29:18 +08:00
|
|
|
__seterrno ();
|
2011-04-29 17:48:25 +08:00
|
|
|
else if (!NT_SUCCESS (allow))
|
2006-10-16 20:26:59 +08:00
|
|
|
{
|
|
|
|
/* CV, 2006-10-16: Now, that's really weird. Imagine a user who has no
|
|
|
|
standard access to a file, but who has backup and restore privileges
|
|
|
|
and these privileges are enabled in the access token. One would
|
|
|
|
expect that the AccessCheck function takes this into consideration
|
|
|
|
when returning the access status. Otherwise, why bother with the
|
|
|
|
pset parameter, right?
|
|
|
|
But not so. AccessCheck actually returns a status of "false" here,
|
2011-04-29 17:48:25 +08:00
|
|
|
even though opening a file with backup resp. restore intent
|
2006-10-16 20:26:59 +08:00
|
|
|
naturally succeeds for this user. This definitely spoils the results
|
|
|
|
of access(2) for administrative users or the SYSTEM account. So, in
|
|
|
|
case the access check fails, another check against the user's
|
|
|
|
backup/restore privileges has to be made. Sigh. */
|
|
|
|
int granted_flags = 0;
|
2011-04-29 17:48:25 +08:00
|
|
|
BOOLEAN has_priv;
|
|
|
|
|
2006-10-16 20:26:59 +08:00
|
|
|
if (flags & R_OK)
|
2006-10-21 19:05:32 +08:00
|
|
|
{
|
2006-10-16 20:26:59 +08:00
|
|
|
pset->PrivilegeCount = 1;
|
|
|
|
pset->Control = 0;
|
2007-07-19 16:33:22 +08:00
|
|
|
pset->Privilege[0].Luid.HighPart = 0L;
|
|
|
|
pset->Privilege[0].Luid.LowPart = SE_BACKUP_PRIVILEGE;
|
2006-10-16 20:26:59 +08:00
|
|
|
pset->Privilege[0].Attributes = 0;
|
2011-04-29 17:48:25 +08:00
|
|
|
status = NtPrivilegeCheck (tok, pset, &has_priv);
|
|
|
|
if (NT_SUCCESS (status) && has_priv)
|
2006-10-16 20:26:59 +08:00
|
|
|
granted_flags |= R_OK;
|
|
|
|
}
|
|
|
|
if (flags & W_OK)
|
2006-10-21 19:05:32 +08:00
|
|
|
{
|
2006-10-16 20:26:59 +08:00
|
|
|
pset->PrivilegeCount = 1;
|
|
|
|
pset->Control = 0;
|
2007-07-19 16:33:22 +08:00
|
|
|
pset->Privilege[0].Luid.HighPart = 0L;
|
|
|
|
pset->Privilege[0].Luid.LowPart = SE_RESTORE_PRIVILEGE;
|
2006-10-16 20:26:59 +08:00
|
|
|
pset->Privilege[0].Attributes = 0;
|
2011-04-29 17:48:25 +08:00
|
|
|
status = NtPrivilegeCheck (tok, pset, &has_priv);
|
|
|
|
if (NT_SUCCESS (status) && has_priv)
|
2006-10-16 20:26:59 +08:00
|
|
|
granted_flags |= W_OK;
|
|
|
|
}
|
|
|
|
if (granted_flags == flags)
|
2006-10-21 19:05:32 +08:00
|
|
|
ret = 0;
|
2006-10-16 20:26:59 +08:00
|
|
|
else
|
|
|
|
set_errno (EACCES);
|
|
|
|
}
|
2003-02-21 22:29:18 +08:00
|
|
|
else
|
|
|
|
ret = 0;
|
2006-10-21 19:05:32 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-09-25 21:44:45 +08:00
|
|
|
check_file_access (path_conv &pc, int flags, bool effective)
|
2006-10-21 19:05:32 +08:00
|
|
|
{
|
|
|
|
security_descriptor sd;
|
|
|
|
int ret = -1;
|
2011-04-29 17:48:25 +08:00
|
|
|
ACCESS_MASK desired = 0;
|
2006-10-21 19:05:32 +08:00
|
|
|
if (flags & R_OK)
|
|
|
|
desired |= FILE_READ_DATA;
|
|
|
|
if (flags & W_OK)
|
|
|
|
desired |= FILE_WRITE_DATA;
|
|
|
|
if (flags & X_OK)
|
|
|
|
desired |= FILE_EXECUTE;
|
2011-07-26 21:30:41 +08:00
|
|
|
if (!get_file_sd (pc.handle (), pc, sd, false))
|
2011-04-28 15:27:51 +08:00
|
|
|
ret = check_access (sd, file_mapping, desired, flags, effective);
|
2006-10-21 19:05:32 +08:00
|
|
|
debug_printf ("flags %x, ret %d", flags, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-09-25 21:44:45 +08:00
|
|
|
check_registry_access (HANDLE hdl, int flags, bool effective)
|
2006-10-21 19:05:32 +08:00
|
|
|
{
|
|
|
|
security_descriptor sd;
|
|
|
|
int ret = -1;
|
2011-04-28 15:27:51 +08:00
|
|
|
static GENERIC_MAPPING NO_COPY reg_mapping = { KEY_READ,
|
|
|
|
KEY_WRITE,
|
|
|
|
KEY_EXECUTE,
|
|
|
|
KEY_ALL_ACCESS };
|
2011-04-29 17:48:25 +08:00
|
|
|
ACCESS_MASK desired = 0;
|
2006-10-21 19:05:32 +08:00
|
|
|
if (flags & R_OK)
|
|
|
|
desired |= KEY_ENUMERATE_SUB_KEYS;
|
|
|
|
if (flags & W_OK)
|
|
|
|
desired |= KEY_SET_VALUE;
|
|
|
|
if (flags & X_OK)
|
|
|
|
desired |= KEY_QUERY_VALUE;
|
2011-05-11 01:19:37 +08:00
|
|
|
|
|
|
|
if ((HKEY) hdl == HKEY_PERFORMANCE_DATA)
|
|
|
|
/* RegGetKeySecurity() always fails with ERROR_INVALID_HANDLE. */
|
|
|
|
ret = 0;
|
|
|
|
else if (!get_reg_sd (hdl, sd))
|
2011-04-28 15:27:51 +08:00
|
|
|
ret = check_access (sd, reg_mapping, desired, flags, effective);
|
2011-05-11 01:19:37 +08:00
|
|
|
|
2006-10-23 03:31:33 +08:00
|
|
|
/* As long as we can't write the registry... */
|
|
|
|
if (flags & W_OK)
|
2007-07-17 22:39:02 +08:00
|
|
|
{
|
|
|
|
set_errno (EROFS);
|
|
|
|
ret = -1;
|
|
|
|
}
|
2003-02-21 22:29:18 +08:00
|
|
|
debug_printf ("flags %x, ret %d", flags, ret);
|
|
|
|
return ret;
|
|
|
|
}
|