2007-07-20 16:29:43 +02:00
|
|
|
/* security.cc: NT file access control functions
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2006-10-22 21:31:33 +02:00
|
|
|
Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
2007-02-23 16:15:50 +01:00
|
|
|
2006, 2007 Red Hat, Inc.
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
Originaly written by Gunther Ebert, gunther.ebert@ixos-leipzig.de
|
2001-02-21 23:59:11 +01:00
|
|
|
Completely rewritten by Corinna Vinschen <corinna@vinschen.de>
|
2000-02-17 20:38:33 +01: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-02 18:28:18 +02:00
|
|
|
#include "winsup.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "cygerrno.h"
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "security.h"
|
2000-08-22 07:10:20 +02: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 02:37:18 +02:00
|
|
|
#include "fhandler.h"
|
2000-08-12 07:35:42 +02:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "pinfo.h"
|
2000-11-15 01:13:09 +01: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 10:10:47 +02: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 13:43:49 +01:00
|
|
|
#include "pwdgrp.h"
|
2007-07-20 16:29:43 +02: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 10:10:47 +02:00
|
|
|
|
2007-02-23 16:15:50 +01:00
|
|
|
/* Set ntsec explicit as default. */
|
|
|
|
bool allow_ntsec = true;
|
2000-12-19 20:52:57 +01:00
|
|
|
/* allow_smbntsec is handled exclusively in path.cc (path_conv::check).
|
2005-04-16 17:21:47 +02:00
|
|
|
It's defined here because of it's strong relationship to allow_ntsec. */
|
2003-07-02 05:16:00 +02:00
|
|
|
bool allow_smbntsec;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2007-07-20 16:29:43 +02:00
|
|
|
LONG
|
|
|
|
get_file_sd (HANDLE fh, path_conv &pc, security_descriptor &sd)
|
* 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 10:10:47 +02:00
|
|
|
{
|
2007-07-20 16:29:43 +02:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
|
|
|
ULONG len = 0;
|
|
|
|
int retry = 0;
|
|
|
|
int res = -1;
|
* 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 10:10:47 +02:00
|
|
|
|
2007-07-20 16:29:43 +02:00
|
|
|
for (; retry < 2; ++retry)
|
* 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 10:10:47 +02:00
|
|
|
{
|
2007-07-20 16:29:43 +02:00
|
|
|
if (fh)
|
2001-09-07 23:32:07 +02:00
|
|
|
{
|
2007-07-20 16:29:43 +02:00
|
|
|
status = NtQuerySecurityObject (fh, ALL_SECURITY_INFORMATION,
|
|
|
|
sd, len, &len);
|
|
|
|
if (status == STATUS_BUFFER_TOO_SMALL)
|
2005-04-03 15:06:43 +02:00
|
|
|
{
|
2007-07-20 16:29:43 +02:00
|
|
|
if (!sd.malloc (len))
|
|
|
|
{
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
break;
|
2008-02-15 18:53:11 +01:00
|
|
|
}
|
2007-07-20 16:29:43 +02:00
|
|
|
status = NtQuerySecurityObject (fh, ALL_SECURITY_INFORMATION,
|
|
|
|
sd, len, &len);
|
2005-04-03 15:06:43 +02:00
|
|
|
}
|
2007-07-20 16:29:43 +02:00
|
|
|
if (NT_SUCCESS (status))
|
2001-09-07 23:32:07 +02:00
|
|
|
{
|
2007-07-20 16:29:43 +02:00
|
|
|
res = 0;
|
|
|
|
break;
|
2001-09-07 23:32:07 +02:00
|
|
|
}
|
|
|
|
}
|
2007-07-20 16:29:43 +02:00
|
|
|
if (!retry)
|
2008-02-15 18:53:11 +01:00
|
|
|
{
|
2007-07-20 16:29:43 +02:00
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
fh = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2006-11-27 13:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
2007-07-20 16:29:43 +02:00
|
|
|
if (retry && fh)
|
|
|
|
NtClose (fh);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return res;
|
2003-11-26 14:23:27 +01:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-11-26 14:23:27 +01:00
|
|
|
LONG
|
2007-07-20 16:29:43 +02:00
|
|
|
set_file_sd (HANDLE fh, path_conv &pc, security_descriptor &sd)
|
2003-11-26 14:23:27 +01:00
|
|
|
{
|
2007-07-20 16:29:43 +02:00
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
2004-04-14 15:40:07 +02:00
|
|
|
int retry = 0;
|
2004-04-16 23:22:13 +02:00
|
|
|
int res = -1;
|
2007-07-20 16:29:43 +02:00
|
|
|
|
2004-04-14 15:40:07 +02:00
|
|
|
for (; retry < 2; ++retry)
|
|
|
|
{
|
2007-07-20 16:29:43 +02:00
|
|
|
if (fh)
|
2008-02-15 18:53:11 +01:00
|
|
|
{
|
2007-07-20 16:29:43 +02:00
|
|
|
status = NtSetSecurityObject (fh, ALL_SECURITY_INFORMATION, sd);
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
res = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!retry)
|
2008-02-15 18:53:11 +01:00
|
|
|
{
|
2007-07-20 16:29:43 +02:00
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
|
|
|
|
status = NtOpenFile (&fh, WRITE_OWNER | WRITE_DAC,
|
|
|
|
pc.get_object_attr (attr, sec_none_nih),
|
|
|
|
&io, FILE_SHARE_VALID_FLAGS,
|
2007-09-18 16:36:29 +02:00
|
|
|
FILE_OPEN_FOR_BACKUP_INTENT
|
|
|
|
| FILE_OPEN_FOR_RECOVERY);
|
2007-07-20 16:29:43 +02:00
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
fh = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-04-14 15:40:07 +02:00
|
|
|
}
|
2007-07-20 16:29:43 +02:00
|
|
|
if (retry && fh)
|
|
|
|
NtClose (fh);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
__seterrno_from_nt_status (status);
|
2004-04-14 22:16:29 +02:00
|
|
|
return res;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2002-08-26 11:56:06 +02:00
|
|
|
static void
|
2003-02-10 23:43:29 +01:00
|
|
|
get_attribute_from_acl (mode_t *attribute, PACL acl, PSID owner_sid,
|
2003-12-07 23:37:12 +01:00
|
|
|
PSID group_sid, bool grp_member)
|
2002-08-26 11:56:06 +02:00
|
|
|
{
|
|
|
|
ACCESS_ALLOWED_ACE *ace;
|
|
|
|
int allow = 0;
|
|
|
|
int deny = 0;
|
|
|
|
int *flags, *anti;
|
|
|
|
|
|
|
|
for (DWORD i = 0; i < acl->AceCount; ++i)
|
|
|
|
{
|
|
|
|
if (!GetAce (acl, i, (PVOID *) &ace))
|
|
|
|
continue;
|
|
|
|
if (ace->Header.AceFlags & INHERIT_ONLY)
|
|
|
|
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 14:47:47 +01:00
|
|
|
cygpsid ace_sid ((PSID) &ace->SidStart);
|
2002-08-26 11:56:06 +02:00
|
|
|
if (ace_sid == well_known_world_sid)
|
|
|
|
{
|
2003-11-26 11:52:21 +01:00
|
|
|
if (ace->Mask & FILE_READ_BITS)
|
2002-11-20 10:23:21 +01:00
|
|
|
*flags |= ((!(*anti & S_IROTH)) ? S_IROTH : 0)
|
2002-08-26 11:56:06 +02:00
|
|
|
| ((!(*anti & S_IRGRP)) ? S_IRGRP : 0)
|
|
|
|
| ((!(*anti & S_IRUSR)) ? S_IRUSR : 0);
|
2003-11-26 11:52:21 +01:00
|
|
|
if (ace->Mask & FILE_WRITE_BITS)
|
2002-11-20 10:23:21 +01:00
|
|
|
*flags |= ((!(*anti & S_IWOTH)) ? S_IWOTH : 0)
|
2002-08-26 11:56:06 +02:00
|
|
|
| ((!(*anti & S_IWGRP)) ? S_IWGRP : 0)
|
|
|
|
| ((!(*anti & S_IWUSR)) ? S_IWUSR : 0);
|
2003-11-26 11:52:21 +01:00
|
|
|
if (ace->Mask & FILE_EXEC_BITS)
|
2002-11-20 10:23:21 +01:00
|
|
|
*flags |= ((!(*anti & S_IXOTH)) ? S_IXOTH : 0)
|
2003-01-26 07:42:40 +01:00
|
|
|
| ((!(*anti & S_IXGRP)) ? S_IXGRP : 0)
|
|
|
|
| ((!(*anti & S_IXUSR)) ? S_IXUSR : 0);
|
2003-03-19 22:34:38 +01:00
|
|
|
if ((S_ISDIR (*attribute)) &&
|
2002-08-26 11:56:06 +02: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 10:23:21 +01:00
|
|
|
else if (ace_sid == owner_sid)
|
2002-08-26 11:56:06 +02:00
|
|
|
{
|
2003-11-26 11:52:21 +01:00
|
|
|
if (ace->Mask & FILE_READ_BITS)
|
2002-11-20 10:23:21 +01:00
|
|
|
*flags |= ((!(*anti & S_IRUSR)) ? S_IRUSR : 0);
|
2003-11-26 11:52:21 +01:00
|
|
|
if (ace->Mask & FILE_WRITE_BITS)
|
2002-11-20 10:23:21 +01:00
|
|
|
*flags |= ((!(*anti & S_IWUSR)) ? S_IWUSR : 0);
|
2003-11-26 11:52:21 +01:00
|
|
|
if (ace->Mask & FILE_EXEC_BITS)
|
2002-11-20 10:23:21 +01:00
|
|
|
*flags |= ((!(*anti & S_IXUSR)) ? S_IXUSR : 0);
|
2002-08-26 11:56:06 +02:00
|
|
|
}
|
2002-11-20 10:23:21 +01:00
|
|
|
else if (ace_sid == group_sid)
|
2002-08-26 11:56:06 +02:00
|
|
|
{
|
2003-11-26 11:52:21 +01:00
|
|
|
if (ace->Mask & FILE_READ_BITS)
|
2002-11-20 10:23:21 +01:00
|
|
|
*flags |= ((!(*anti & S_IRGRP)) ? S_IRGRP : 0)
|
2002-08-26 11:56:06 +02:00
|
|
|
| ((grp_member && !(*anti & S_IRUSR)) ? S_IRUSR : 0);
|
2003-11-26 11:52:21 +01:00
|
|
|
if (ace->Mask & FILE_WRITE_BITS)
|
2002-11-20 10:23:21 +01:00
|
|
|
*flags |= ((!(*anti & S_IWGRP)) ? S_IWGRP : 0)
|
2002-08-26 11:56:06 +02:00
|
|
|
| ((grp_member && !(*anti & S_IWUSR)) ? S_IWUSR : 0);
|
2003-11-26 11:52:21 +01:00
|
|
|
if (ace->Mask & FILE_EXEC_BITS)
|
2002-11-20 10:23:21 +01:00
|
|
|
*flags |= ((!(*anti & S_IXGRP)) ? S_IXGRP : 0)
|
2002-08-26 11:56:06 +02:00
|
|
|
| ((grp_member && !(*anti & S_IXUSR)) ? S_IXUSR : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*attribute &= ~(S_IRWXU | S_IRWXG | S_IRWXO | S_ISVTX | S_ISGID | S_ISUID);
|
2002-11-20 10:23:21 +01:00
|
|
|
if (owner_sid && group_sid && EqualSid (owner_sid, group_sid)
|
|
|
|
/* 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 11:56:06 +02:00
|
|
|
*attribute |= allow;
|
|
|
|
}
|
|
|
|
|
2003-04-11 11:38:07 +02:00
|
|
|
static void
|
|
|
|
get_info_from_sd (PSECURITY_DESCRIPTOR psd, mode_t *attribute,
|
2002-05-29 17:04:29 +02:00
|
|
|
__uid32_t *uidret, __gid32_t *gidret)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2003-04-11 11:38:07 +02:00
|
|
|
if (!psd)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2003-04-11 11:38:07 +02: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-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2003-02-05 14:47:47 +01:00
|
|
|
cygpsid owner_sid;
|
|
|
|
cygpsid group_sid;
|
2000-02-17 20:38:33 +01:00
|
|
|
BOOL dummy;
|
|
|
|
|
2003-02-05 14:47:47 +01:00
|
|
|
if (!GetSecurityDescriptorOwner (psd, (PSID *) &owner_sid, &dummy))
|
2000-02-17 20:38:33 +01:00
|
|
|
debug_printf ("GetSecurityDescriptorOwner %E");
|
2003-02-05 14:47:47 +01:00
|
|
|
if (!GetSecurityDescriptorGroup (psd, (PSID *) &group_sid, &dummy))
|
2000-02-17 20:38:33 +01:00
|
|
|
debug_printf ("GetSecurityDescriptorGroup %E");
|
|
|
|
|
2003-02-05 14:47:47 +01:00
|
|
|
__uid32_t uid;
|
|
|
|
__gid32_t gid;
|
2003-12-07 23:37:12 +01:00
|
|
|
bool grp_member = get_sids_info (owner_sid, group_sid, &uid, &gid);
|
2000-03-16 20:35:18 +01:00
|
|
|
if (uidret)
|
|
|
|
*uidret = uid;
|
|
|
|
if (gidret)
|
|
|
|
*gidret = gid;
|
|
|
|
|
2001-01-28 06:51:15 +01:00
|
|
|
if (!attribute)
|
2000-03-16 20:35:18 +01:00
|
|
|
{
|
2003-04-11 11:38:07 +02:00
|
|
|
syscall_printf ("uid %d, gid %d", uid, gid);
|
|
|
|
return;
|
2000-03-16 20:35:18 +01:00
|
|
|
}
|
|
|
|
|
2003-04-11 11:38:07 +02:00
|
|
|
PACL acl;
|
|
|
|
BOOL acl_exists;
|
|
|
|
|
|
|
|
if (!GetSecurityDescriptorDacl (psd, &acl_exists, &acl, &dummy))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2003-04-11 11:38:07 +02:00
|
|
|
__seterrno ();
|
|
|
|
debug_printf ("GetSecurityDescriptorDacl %E");
|
|
|
|
*attribute &= ~(S_IRWXU | S_IRWXG | S_IRWXO);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2003-04-11 11:38:07 +02: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-17 20:38:33 +01:00
|
|
|
|
2004-09-03 10:18:41 +02:00
|
|
|
syscall_printf ("%sACL %x, uid %d, gid %d",
|
2003-04-11 11:38:07 +02:00
|
|
|
(!acl_exists || !acl)?"NO ":"", *attribute, uid, gid);
|
|
|
|
}
|
|
|
|
|
2006-12-20 18:14:23 +01:00
|
|
|
static int
|
2007-07-20 16:29:43 +02:00
|
|
|
get_reg_sd (HANDLE handle, security_descriptor &sd_ret)
|
2006-12-20 18:14:23 +01:00
|
|
|
{
|
|
|
|
LONG ret;
|
|
|
|
DWORD len = 0;
|
|
|
|
|
2007-07-20 16:29:43 +02:00
|
|
|
ret = RegGetKeySecurity ((HKEY) handle, ALL_SECURITY_INFORMATION,
|
2007-02-20 01:16:18 +01:00
|
|
|
sd_ret, &len);
|
2006-12-20 18:14:23 +01:00
|
|
|
if (ret == ERROR_INSUFFICIENT_BUFFER)
|
2007-02-20 01:16:18 +01:00
|
|
|
{
|
2006-12-20 18:14:23 +01:00
|
|
|
if (!sd_ret.malloc (len))
|
2007-02-20 01:16:18 +01:00
|
|
|
set_errno (ENOMEM);
|
2006-12-20 18:14:23 +01:00
|
|
|
else
|
2007-07-20 16:29:43 +02:00
|
|
|
ret = RegGetKeySecurity ((HKEY) handle, ALL_SECURITY_INFORMATION,
|
2007-02-20 01:16:18 +01:00
|
|
|
sd_ret, &len);
|
2006-12-20 18:14:23 +01:00
|
|
|
}
|
|
|
|
if (ret != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
2007-02-20 01:16:18 +01:00
|
|
|
}
|
2006-12-20 18:14:23 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-14 18:36:26 +02:00
|
|
|
int
|
2007-07-20 16:29:43 +02:00
|
|
|
get_reg_attribute (HKEY hkey, mode_t *attribute, __uid32_t *uidret,
|
|
|
|
__gid32_t *gidret)
|
2002-07-02 03:36:15 +02:00
|
|
|
{
|
2004-10-06 03:33:39 +02:00
|
|
|
if (allow_ntsec)
|
2002-07-02 03:36:15 +02:00
|
|
|
{
|
2006-10-22 16:57:43 +02:00
|
|
|
security_descriptor sd;
|
|
|
|
|
2007-07-20 16:29:43 +02:00
|
|
|
if (!get_reg_sd (hkey, sd))
|
2006-10-22 16:57:43 +02:00
|
|
|
{
|
2007-07-20 16:29:43 +02:00
|
|
|
get_info_from_sd (sd, attribute, uidret, gidret);
|
|
|
|
return 0;
|
2006-10-22 16:57:43 +02:00
|
|
|
}
|
2002-07-02 03:36:15 +02:00
|
|
|
}
|
2003-04-11 11:38:07 +02:00
|
|
|
/* The entries are already set to default values */
|
|
|
|
return -1;
|
2002-07-02 03:36:15 +02:00
|
|
|
}
|
|
|
|
|
2004-04-08 09:57:28 +02:00
|
|
|
int
|
2007-07-20 16:29:43 +02:00
|
|
|
get_file_attribute (HANDLE handle, path_conv &pc,
|
2004-04-08 09:57:28 +02:00
|
|
|
mode_t *attribute, __uid32_t *uidret, __gid32_t *gidret)
|
|
|
|
{
|
2007-07-20 16:29:43 +02:00
|
|
|
if (pc.has_acls () && allow_ntsec)
|
2004-04-08 09:57:28 +02:00
|
|
|
{
|
2006-10-22 16:57:43 +02:00
|
|
|
security_descriptor sd;
|
|
|
|
|
2007-07-20 16:29:43 +02:00
|
|
|
if (!get_file_sd (handle, pc, sd))
|
|
|
|
{
|
|
|
|
get_info_from_sd (sd, attribute, uidret, gidret);
|
|
|
|
return 0;
|
|
|
|
}
|
2007-07-30 12:58:16 +02:00
|
|
|
else
|
2008-02-15 18:53:11 +01:00
|
|
|
{
|
2007-07-30 12:58:16 +02:00
|
|
|
if (uidret)
|
|
|
|
*uidret = ILLEGAL_UID;
|
|
|
|
if (gidret)
|
|
|
|
*gidret = ILLEGAL_GID;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2004-04-08 09:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (uidret)
|
|
|
|
*uidret = myself->uid;
|
|
|
|
if (gidret)
|
|
|
|
*gidret = myself->gid;
|
|
|
|
|
2007-03-01 16:13:47 +01:00
|
|
|
return -1;
|
2004-04-08 09:57:28 +02:00
|
|
|
}
|
|
|
|
|
2003-12-07 23:37:12 +01:00
|
|
|
bool
|
2001-04-20 15:02:32 +02:00
|
|
|
add_access_allowed_ace (PACL acl, int offset, DWORD attributes,
|
|
|
|
PSID sid, size_t &len_add, DWORD inherit)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-01-28 06:51:15 +01:00
|
|
|
if (!AddAccessAllowedAce (acl, ACL_REVISION, attributes, sid))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
__seterrno ();
|
2003-12-07 23:37:12 +01:00
|
|
|
return false;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
ACCESS_ALLOWED_ACE *ace;
|
2002-11-20 10:23:21 +01:00
|
|
|
if (inherit && GetAce (acl, offset, (PVOID *) &ace))
|
2000-02-17 20:38:33 +01:00
|
|
|
ace->Header.AceFlags |= inherit;
|
2002-11-20 10:23:21 +01:00
|
|
|
len_add += sizeof (ACCESS_ALLOWED_ACE) - sizeof (DWORD) + GetLengthSid (sid);
|
2003-12-07 23:37:12 +01:00
|
|
|
return true;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2003-12-07 23:37:12 +01:00
|
|
|
bool
|
2001-04-20 15:02:32 +02:00
|
|
|
add_access_denied_ace (PACL acl, int offset, DWORD attributes,
|
|
|
|
PSID sid, size_t &len_add, DWORD inherit)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-01-28 06:51:15 +01:00
|
|
|
if (!AddAccessDeniedAce (acl, ACL_REVISION, attributes, sid))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
__seterrno ();
|
2003-12-07 23:37:12 +01:00
|
|
|
return false;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
ACCESS_DENIED_ACE *ace;
|
2002-11-20 10:23:21 +01:00
|
|
|
if (inherit && GetAce (acl, offset, (PVOID *) &ace))
|
2000-02-17 20:38:33 +01:00
|
|
|
ace->Header.AceFlags |= inherit;
|
2002-07-29 14:51:52 +02:00
|
|
|
len_add += sizeof (ACCESS_DENIED_ACE) - sizeof (DWORD) + GetLengthSid (sid);
|
2003-12-07 23:37:12 +01:00
|
|
|
return true;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2003-11-26 14:23:27 +01:00
|
|
|
static PSECURITY_DESCRIPTOR
|
2002-06-06 13:17:51 +02:00
|
|
|
alloc_sd (__uid32_t uid, __gid32_t gid, int attribute,
|
2003-11-26 14:23:27 +01:00
|
|
|
security_descriptor &sd_ret)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
BOOL dummy;
|
|
|
|
|
2002-11-20 10:23:21 +01:00
|
|
|
debug_printf("uid %d, gid %d, attribute %x", uid, gid, attribute);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-11-20 10:23:21 +01:00
|
|
|
/* Get owner and group from current security descriptor. */
|
|
|
|
PSID cur_owner_sid = NULL;
|
|
|
|
PSID cur_group_sid = NULL;
|
|
|
|
if (!GetSecurityDescriptorOwner (sd_ret, &cur_owner_sid, &dummy))
|
|
|
|
debug_printf ("GetSecurityDescriptorOwner %E");
|
|
|
|
if (!GetSecurityDescriptorGroup (sd_ret, &cur_group_sid, &dummy))
|
|
|
|
debug_printf ("GetSecurityDescriptorGroup %E");
|
|
|
|
|
2002-06-21 14:37:51 +02:00
|
|
|
/* Get SID of owner. */
|
2003-01-12 12:38:51 +01:00
|
|
|
cygsid owner_sid;
|
2002-06-21 14:37:51 +02:00
|
|
|
/* Check for current user first */
|
|
|
|
if (uid == myself->uid)
|
|
|
|
owner_sid = cygheap->user.sid ();
|
2002-11-20 10:23:21 +01: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 13:43:49 +01:00
|
|
|
else if (!owner_sid.getfrompw (internal_getpwuid (uid)))
|
2002-06-21 14:37:51 +02:00
|
|
|
{
|
2002-11-20 10:23:21 +01:00
|
|
|
set_errno (EINVAL);
|
|
|
|
return NULL;
|
2002-06-21 14:37:51 +02:00
|
|
|
}
|
|
|
|
owner_sid.debug_print ("alloc_sd: owner SID =");
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-06-21 14:37:51 +02:00
|
|
|
/* Get SID of new group. */
|
2003-01-12 12:38:51 +01:00
|
|
|
cygsid group_sid;
|
2002-10-22 18:22:10 +02:00
|
|
|
/* Check for current user first */
|
|
|
|
if (gid == myself->gid)
|
|
|
|
group_sid = cygheap->user.groups.pgsid;
|
2002-11-20 10:23:21 +01: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 13:43:49 +01:00
|
|
|
else if (!group_sid.getfromgr (internal_getgrgid (gid)))
|
2002-11-20 10:23:21 +01:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
group_sid.debug_print ("alloc_sd: group SID =");
|
|
|
|
|
2000-10-23 10:02:38 +02:00
|
|
|
/* Initialize local security descriptor. */
|
2000-02-17 20:38:33 +01:00
|
|
|
SECURITY_DESCRIPTOR sd;
|
2001-01-28 06:51:15 +01:00
|
|
|
if (!InitializeSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-07-19 11:06:54 +02:00
|
|
|
/* We set the SE_DACL_PROTECTED flag here to prevent the DACL from being
|
|
|
|
* modified by inheritable ACEs. This flag is available since Win2K. */
|
|
|
|
if (wincap.has_dacl_protect ())
|
|
|
|
sd.Control |= SE_DACL_PROTECTED;
|
2000-12-20 13:42:43 +01:00
|
|
|
|
2000-10-23 10:02:38 +02:00
|
|
|
/* Create owner for local security descriptor. */
|
2002-06-19 17:27:27 +02:00
|
|
|
if (!SetSecurityDescriptorOwner (&sd, owner_sid, FALSE))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-10-23 10:02:38 +02:00
|
|
|
|
|
|
|
/* Create group for local security descriptor. */
|
2002-11-20 10:23:21 +01:00
|
|
|
if (!SetSecurityDescriptorGroup (&sd, group_sid, FALSE))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-10-23 10:02:38 +02:00
|
|
|
/* Initialize local access control list. */
|
2005-06-07 21:31:42 +02:00
|
|
|
PACL acl = (PACL) alloca (3072);
|
2001-01-28 06:51:15 +01:00
|
|
|
if (!InitializeAcl (acl, 3072, ACL_REVISION))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-10-23 10:02:38 +02:00
|
|
|
/* From here fill ACL. */
|
2000-02-17 20:38:33 +01:00
|
|
|
size_t acl_len = sizeof (ACL);
|
|
|
|
int ace_off = 0;
|
|
|
|
|
2000-10-23 10:02:38 +02:00
|
|
|
/* Construct allow attribute for owner. */
|
2003-03-10 17:25:48 +01:00
|
|
|
DWORD owner_allow = STANDARD_RIGHTS_ALL
|
2000-09-03 06:16:35 +02:00
|
|
|
| FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA;
|
2000-02-17 20:38:33 +01:00
|
|
|
if (attribute & S_IRUSR)
|
|
|
|
owner_allow |= FILE_GENERIC_READ;
|
|
|
|
if (attribute & S_IWUSR)
|
2003-03-10 17:25:48 +01:00
|
|
|
owner_allow |= FILE_GENERIC_WRITE;
|
2000-02-17 20:38:33 +01:00
|
|
|
if (attribute & S_IXUSR)
|
|
|
|
owner_allow |= FILE_GENERIC_EXECUTE;
|
2003-03-19 22:34:38 +01:00
|
|
|
if (S_ISDIR (attribute)
|
|
|
|
&& (attribute & (S_IWUSR | S_IXUSR)) == (S_IWUSR | S_IXUSR))
|
2000-02-17 20:38:33 +01:00
|
|
|
owner_allow |= FILE_DELETE_CHILD;
|
|
|
|
|
2000-10-23 10:02:38 +02:00
|
|
|
/* Construct allow attribute for group. */
|
2000-02-17 20:38:33 +01:00
|
|
|
DWORD group_allow = STANDARD_RIGHTS_READ
|
2000-09-03 06:16:35 +02:00
|
|
|
| FILE_READ_ATTRIBUTES | FILE_READ_EA;
|
2000-02-17 20:38:33 +01:00
|
|
|
if (attribute & S_IRGRP)
|
|
|
|
group_allow |= FILE_GENERIC_READ;
|
|
|
|
if (attribute & S_IWGRP)
|
2001-07-16 00:40:07 +02:00
|
|
|
group_allow |= STANDARD_RIGHTS_WRITE | FILE_GENERIC_WRITE;
|
2000-02-17 20:38:33 +01:00
|
|
|
if (attribute & S_IXGRP)
|
|
|
|
group_allow |= FILE_GENERIC_EXECUTE;
|
2003-03-19 22:34:38 +01:00
|
|
|
if (S_ISDIR (attribute)
|
|
|
|
&& (attribute & (S_IWGRP | S_IXGRP)) == (S_IWGRP | S_IXGRP)
|
|
|
|
&& !(attribute & S_ISVTX))
|
2000-02-17 20:38:33 +01:00
|
|
|
group_allow |= FILE_DELETE_CHILD;
|
|
|
|
|
2000-10-23 10:02:38 +02:00
|
|
|
/* Construct allow attribute for everyone. */
|
2000-02-17 20:38:33 +01:00
|
|
|
DWORD other_allow = STANDARD_RIGHTS_READ
|
2000-09-03 06:16:35 +02:00
|
|
|
| FILE_READ_ATTRIBUTES | FILE_READ_EA;
|
2000-02-17 20:38:33 +01:00
|
|
|
if (attribute & S_IROTH)
|
|
|
|
other_allow |= FILE_GENERIC_READ;
|
|
|
|
if (attribute & S_IWOTH)
|
2001-07-16 00:40:07 +02:00
|
|
|
other_allow |= STANDARD_RIGHTS_WRITE | FILE_GENERIC_WRITE;
|
2000-02-17 20:38:33 +01:00
|
|
|
if (attribute & S_IXOTH)
|
|
|
|
other_allow |= FILE_GENERIC_EXECUTE;
|
2003-03-19 22:34:38 +01:00
|
|
|
if (S_ISDIR (attribute)
|
|
|
|
&& (attribute & (S_IWOTH | S_IXOTH)) == (S_IWOTH | S_IXOTH)
|
2001-07-16 00:40:07 +02:00
|
|
|
&& !(attribute & S_ISVTX))
|
2000-02-17 20:38:33 +01:00
|
|
|
other_allow |= FILE_DELETE_CHILD;
|
2000-10-28 07:41:44 +02:00
|
|
|
|
2001-07-16 00:40:07 +02: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-07 23:32:07 +02:00
|
|
|
null_allow |= FILE_APPEND_DATA;
|
2001-07-16 00:40:07 +02:00
|
|
|
if (attribute & S_ISGID)
|
2001-09-07 23:32:07 +02:00
|
|
|
null_allow |= FILE_WRITE_DATA;
|
2001-07-16 00:40:07 +02:00
|
|
|
if (attribute & S_ISVTX)
|
2001-09-07 23:32:07 +02:00
|
|
|
null_allow |= FILE_READ_DATA;
|
2001-07-16 00:40:07 +02:00
|
|
|
}
|
|
|
|
|
2002-11-20 10:23:21 +01:00
|
|
|
/* Add owner and group permissions if SIDs are equal
|
|
|
|
and construct deny attributes for group and owner. */
|
2003-12-07 23:37:12 +01:00
|
|
|
bool isownergroup;
|
2003-01-12 12:38:51 +01:00
|
|
|
if ((isownergroup = (owner_sid == group_sid)))
|
|
|
|
owner_allow |= group_allow;
|
|
|
|
|
2002-11-20 10:23:21 +01:00
|
|
|
DWORD owner_deny = ~owner_allow & (group_allow | other_allow);
|
2000-02-17 20:38:33 +01:00
|
|
|
owner_deny &= ~(STANDARD_RIGHTS_READ
|
2000-09-03 06:16:35 +02:00
|
|
|
| FILE_READ_ATTRIBUTES | FILE_READ_EA
|
|
|
|
| FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-01-12 12:38:51 +01:00
|
|
|
DWORD group_deny = ~group_allow & other_allow;
|
|
|
|
group_deny &= ~(STANDARD_RIGHTS_READ
|
|
|
|
| FILE_READ_ATTRIBUTES | FILE_READ_EA);
|
2000-05-24 22:09:43 +02:00
|
|
|
|
2000-10-23 10:02:38 +02:00
|
|
|
/* Set deny ACE for owner. */
|
2000-02-17 20:38:33 +01:00
|
|
|
if (owner_deny
|
2001-01-28 06:51:15 +01:00
|
|
|
&& !add_access_denied_ace (acl, ace_off++, owner_deny,
|
2003-01-12 12:38:51 +01:00
|
|
|
owner_sid, acl_len, NO_INHERITANCE))
|
2001-07-16 00:40:07 +02:00
|
|
|
return NULL;
|
2002-11-20 10:23:21 +01:00
|
|
|
/* Set deny ACE for group here to respect the canonical order,
|
|
|
|
if this does not impact owner */
|
2003-01-12 12:38:51 +01: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 10:02:38 +02:00
|
|
|
/* Set allow ACE for owner. */
|
2001-01-28 06:51:15 +01:00
|
|
|
if (!add_access_allowed_ace (acl, ace_off++, owner_allow,
|
2003-01-12 12:38:51 +01:00
|
|
|
owner_sid, acl_len, NO_INHERITANCE))
|
2000-02-17 20:38:33 +01:00
|
|
|
return NULL;
|
2002-11-20 10:23:21 +01:00
|
|
|
/* Set deny ACE for group, if still needed. */
|
2003-01-12 12:38:51 +01:00
|
|
|
if (group_deny & owner_allow && !isownergroup
|
2001-01-28 06:51:15 +01:00
|
|
|
&& !add_access_denied_ace (acl, ace_off++, group_deny,
|
2003-01-12 12:38:51 +01:00
|
|
|
group_sid, acl_len, NO_INHERITANCE))
|
2001-07-16 00:40:07 +02:00
|
|
|
return NULL;
|
2000-10-23 10:02:38 +02:00
|
|
|
/* Set allow ACE for group. */
|
2003-01-12 12:38:51 +01:00
|
|
|
if (!isownergroup
|
2002-11-20 10:23:21 +01:00
|
|
|
&& !add_access_allowed_ace (acl, ace_off++, group_allow,
|
2003-01-12 12:38:51 +01:00
|
|
|
group_sid, acl_len, NO_INHERITANCE))
|
2000-02-17 20:38:33 +01:00
|
|
|
return NULL;
|
|
|
|
|
2001-04-26 11:21:33 +02:00
|
|
|
/* Set allow ACE for everyone. */
|
|
|
|
if (!add_access_allowed_ace (acl, ace_off++, other_allow,
|
2003-01-12 12:38:51 +01:00
|
|
|
well_known_world_sid, acl_len, NO_INHERITANCE))
|
2001-04-26 11:21:33 +02:00
|
|
|
return NULL;
|
2001-07-16 00:40:07 +02:00
|
|
|
/* Set null ACE for special bits. */
|
|
|
|
if (null_allow
|
|
|
|
&& !add_access_allowed_ace (acl, ace_off++, null_allow,
|
2002-07-02 10:11:15 +02:00
|
|
|
well_known_null_sid, acl_len, NO_INHERITANCE))
|
2001-07-16 00:40:07 +02:00
|
|
|
return NULL;
|
2001-04-26 11:21:33 +02:00
|
|
|
|
2000-10-23 10:02:38 +02:00
|
|
|
/* Fill ACL with unrelated ACEs from current security descriptor. */
|
2000-02-17 20:38:33 +01:00
|
|
|
PACL oacl;
|
2003-01-12 12:38:51 +01:00
|
|
|
BOOL acl_exists = FALSE;
|
2000-02-17 20:38:33 +01:00
|
|
|
ACCESS_ALLOWED_ACE *ace;
|
|
|
|
if (GetSecurityDescriptorDacl (sd_ret, &acl_exists, &oacl, &dummy)
|
|
|
|
&& acl_exists && oacl)
|
|
|
|
for (DWORD i = 0; i < oacl->AceCount; ++i)
|
|
|
|
if (GetAce (oacl, i, (PVOID *) &ace))
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
2003-02-05 14:47:47 +01:00
|
|
|
cygpsid ace_sid ((PSID) &ace->SidStart);
|
|
|
|
|
2000-10-23 10:02:38 +02:00
|
|
|
/* Check for related ACEs. */
|
2003-01-12 12:38:51 +01:00
|
|
|
if (ace_sid == well_known_null_sid)
|
|
|
|
continue;
|
2002-11-20 10:23:21 +01:00
|
|
|
if ((ace_sid == cur_owner_sid)
|
|
|
|
|| (ace_sid == owner_sid)
|
|
|
|
|| (ace_sid == cur_group_sid)
|
|
|
|
|| (ace_sid == group_sid)
|
2003-01-12 12:38:51 +01:00
|
|
|
|| (ace_sid == well_known_world_sid))
|
|
|
|
{
|
|
|
|
if (ace->Header.AceFlags & SUB_CONTAINERS_AND_OBJECTS_INHERIT)
|
|
|
|
ace->Header.AceFlags |= INHERIT_ONLY;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
2000-10-23 10:02:38 +02:00
|
|
|
/*
|
|
|
|
* Add unrelated ACCESS_DENIED_ACE to the beginning but
|
2001-04-26 11:21:33 +02:00
|
|
|
* behind the owner_deny, ACCESS_ALLOWED_ACE to the end.
|
2003-01-12 12:38:51 +01:00
|
|
|
* FIXME: this would break the order of the inherit_only ACEs
|
2000-10-23 10:02:38 +02:00
|
|
|
*/
|
2002-06-19 17:27:27 +02:00
|
|
|
if (!AddAce (acl, ACL_REVISION,
|
2003-01-12 12:38:51 +01:00
|
|
|
ace->Header.AceType == ACCESS_DENIED_ACE_TYPE?
|
2000-09-03 06:16:35 +02:00
|
|
|
(owner_deny ? 1 : 0) : MAXDWORD,
|
|
|
|
(LPVOID) ace, ace->Header.AceSize))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
acl_len += ace->Header.AceSize;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-01-12 12:38:51 +01:00
|
|
|
/* Construct appropriate inherit attribute for new directories */
|
2003-09-25 05:51:51 +02:00
|
|
|
if (S_ISDIR (attribute) && !acl_exists)
|
2003-01-12 12:38:51 +01:00
|
|
|
{
|
|
|
|
const DWORD inherit = SUB_CONTAINERS_AND_OBJECTS_INHERIT | INHERIT_ONLY;
|
|
|
|
|
|
|
|
#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,
|
|
|
|
well_known_creator_owner_sid, acl_len, inherit))
|
|
|
|
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,
|
|
|
|
well_known_creator_group_sid, acl_len, inherit))
|
|
|
|
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 10:02:38 +02:00
|
|
|
/* Set AclSize to computed value. */
|
2000-02-17 20:38:33 +01:00
|
|
|
acl->AclSize = acl_len;
|
|
|
|
debug_printf ("ACL-Size: %d", acl_len);
|
|
|
|
|
2000-10-23 10:02:38 +02:00
|
|
|
/* Create DACL for local security descriptor. */
|
2001-01-28 06:51:15 +01:00
|
|
|
if (!SetSecurityDescriptorDacl (&sd, TRUE, acl, FALSE))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-10-23 10:02:38 +02:00
|
|
|
/* Make self relative security descriptor. */
|
2003-11-26 14:23:27 +01:00
|
|
|
DWORD sd_size = 0;
|
|
|
|
MakeSelfRelativeSD (&sd, sd_ret, &sd_size);
|
|
|
|
if (sd_size <= 0)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-11-26 14:23:27 +01:00
|
|
|
if (!sd_ret.malloc (sd_size))
|
|
|
|
{
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!MakeSelfRelativeSD (&sd, sd_ret, &sd_size))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-11-26 14:23:27 +01:00
|
|
|
debug_printf ("Created SD-Size: %u", sd_ret.size ());
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-01-12 12:38:51 +01:00
|
|
|
return sd_ret;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2001-08-07 17:09:54 +02:00
|
|
|
void
|
|
|
|
set_security_attribute (int attribute, PSECURITY_ATTRIBUTES psa,
|
2003-11-26 14:23:27 +01:00
|
|
|
security_descriptor &sd)
|
2001-08-07 17:09:54 +02:00
|
|
|
{
|
2003-11-26 14:23:27 +01:00
|
|
|
psa->lpSecurityDescriptor = sd.malloc (SECURITY_DESCRIPTOR_MIN_LENGTH);
|
|
|
|
InitializeSecurityDescriptor ((PSECURITY_DESCRIPTOR)psa->lpSecurityDescriptor,
|
2001-09-07 23:32:07 +02:00
|
|
|
SECURITY_DESCRIPTOR_REVISION);
|
2003-11-26 14:23:27 +01:00
|
|
|
psa->lpSecurityDescriptor = alloc_sd (geteuid32 (), getegid32 (),
|
2004-05-28 21:50:07 +02:00
|
|
|
attribute, sd);
|
2001-08-07 17:09:54 +02:00
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
int
|
2007-07-20 16:29:43 +02:00
|
|
|
set_file_attribute (HANDLE handle, path_conv &pc,
|
2002-07-29 14:51:52 +02:00
|
|
|
__uid32_t uid, __gid32_t gid, int attribute)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2007-07-19 19:42:11 +02:00
|
|
|
int ret = -1;
|
|
|
|
|
2007-07-20 16:29:43 +02:00
|
|
|
if (pc.has_acls () && allow_ntsec)
|
2007-07-19 19:42:11 +02:00
|
|
|
{
|
|
|
|
security_descriptor sd;
|
|
|
|
|
2007-07-20 16:29:43 +02:00
|
|
|
if (!get_file_sd (handle, pc, sd) && alloc_sd (uid, gid, attribute, sd))
|
|
|
|
ret = set_file_sd (handle, pc, sd);
|
2007-07-19 19:42:11 +02:00
|
|
|
}
|
2007-03-01 16:13:47 +01:00
|
|
|
else
|
|
|
|
ret = 0;
|
2007-08-16 16:46:23 +02:00
|
|
|
syscall_printf ("%d = set_file_attribute (%S, %d, %d, %p)",
|
|
|
|
ret, pc.get_nt_native_path (), uid, gid, attribute);
|
2000-03-16 20:35:18 +01:00
|
|
|
return ret;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2006-10-21 13:05:32 +02:00
|
|
|
static int
|
|
|
|
check_access (security_descriptor &sd, GENERIC_MAPPING &mapping,
|
|
|
|
DWORD desired, int flags)
|
2003-02-21 15:29:18 +01:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
BOOL status;
|
2006-10-21 13:05:32 +02:00
|
|
|
DWORD granted;
|
|
|
|
DWORD plen = sizeof (PRIVILEGE_SET) + 3 * sizeof (LUID_AND_ATTRIBUTES);
|
|
|
|
PPRIVILEGE_SET pset = (PPRIVILEGE_SET) alloca (plen);
|
2007-07-16 22:01:15 +02:00
|
|
|
HANDLE tok = cygheap->user.issetuid () ? cygheap->user.imp_token ()
|
2006-10-21 13:05:32 +02:00
|
|
|
: hProcImpToken;
|
2003-02-21 15:29:18 +01:00
|
|
|
|
2006-12-12 17:27:32 +01:00
|
|
|
if (!tok && !DuplicateTokenEx (hProcToken, MAXIMUM_ALLOWED, NULL,
|
|
|
|
SecurityImpersonation, TokenImpersonation,
|
|
|
|
&hProcImpToken))
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
system_printf ("DuplicateTokenEx failed, %E");
|
|
|
|
#else
|
|
|
|
syscall_printf ("DuplicateTokenEx failed, %E");
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
tok = hProcImpToken;
|
|
|
|
|
2006-10-21 13:05:32 +02:00
|
|
|
if (!AccessCheck (sd, tok, desired, &mapping, pset, &plen, &granted, &status))
|
2003-02-21 15:29:18 +01:00
|
|
|
__seterrno ();
|
|
|
|
else if (!status)
|
2006-10-16 14:26:59 +02: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,
|
|
|
|
even though opening a file with backup resp. restore intent
|
|
|
|
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;
|
|
|
|
if (flags & R_OK)
|
2006-10-21 13:05:32 +02:00
|
|
|
{
|
2006-10-16 14:26:59 +02:00
|
|
|
pset->PrivilegeCount = 1;
|
|
|
|
pset->Control = 0;
|
2007-07-19 10:33:22 +02:00
|
|
|
pset->Privilege[0].Luid.HighPart = 0L;
|
|
|
|
pset->Privilege[0].Luid.LowPart = SE_BACKUP_PRIVILEGE;
|
2006-10-16 14:26:59 +02:00
|
|
|
pset->Privilege[0].Attributes = 0;
|
2006-10-21 13:05:32 +02:00
|
|
|
if (PrivilegeCheck (tok, pset, &status) && status)
|
2006-10-16 14:26:59 +02:00
|
|
|
granted_flags |= R_OK;
|
|
|
|
}
|
|
|
|
if (flags & W_OK)
|
2006-10-21 13:05:32 +02:00
|
|
|
{
|
2006-10-16 14:26:59 +02:00
|
|
|
pset->PrivilegeCount = 1;
|
|
|
|
pset->Control = 0;
|
2007-07-19 10:33:22 +02:00
|
|
|
pset->Privilege[0].Luid.HighPart = 0L;
|
|
|
|
pset->Privilege[0].Luid.LowPart = SE_RESTORE_PRIVILEGE;
|
2006-10-16 14:26:59 +02:00
|
|
|
pset->Privilege[0].Attributes = 0;
|
2006-10-21 13:05:32 +02:00
|
|
|
if (PrivilegeCheck (tok, pset, &status) && status)
|
2006-10-16 14:26:59 +02:00
|
|
|
granted_flags |= W_OK;
|
|
|
|
}
|
|
|
|
if (granted_flags == flags)
|
2006-10-21 13:05:32 +02:00
|
|
|
ret = 0;
|
2006-10-16 14:26:59 +02:00
|
|
|
else
|
|
|
|
set_errno (EACCES);
|
|
|
|
}
|
2003-02-21 15:29:18 +01:00
|
|
|
else
|
|
|
|
ret = 0;
|
2006-10-21 13:05:32 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-07-20 16:29:43 +02:00
|
|
|
check_file_access (path_conv &pc, int flags)
|
2006-10-21 13:05:32 +02:00
|
|
|
{
|
|
|
|
security_descriptor sd;
|
|
|
|
int ret = -1;
|
|
|
|
static GENERIC_MAPPING NO_COPY mapping = { FILE_GENERIC_READ,
|
|
|
|
FILE_GENERIC_WRITE,
|
|
|
|
FILE_GENERIC_EXECUTE,
|
|
|
|
FILE_ALL_ACCESS };
|
|
|
|
DWORD desired = 0;
|
|
|
|
if (flags & R_OK)
|
|
|
|
desired |= FILE_READ_DATA;
|
|
|
|
if (flags & W_OK)
|
|
|
|
desired |= FILE_WRITE_DATA;
|
|
|
|
if (flags & X_OK)
|
|
|
|
desired |= FILE_EXECUTE;
|
2007-07-20 16:29:43 +02:00
|
|
|
if (!get_file_sd (NULL, pc, sd))
|
2006-10-21 13:05:32 +02:00
|
|
|
ret = check_access (sd, mapping, desired, flags);
|
|
|
|
debug_printf ("flags %x, ret %d", flags, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
check_registry_access (HANDLE hdl, int flags)
|
|
|
|
{
|
|
|
|
security_descriptor sd;
|
|
|
|
int ret = -1;
|
|
|
|
static GENERIC_MAPPING NO_COPY mapping = { KEY_READ,
|
|
|
|
KEY_WRITE,
|
|
|
|
KEY_EXECUTE,
|
|
|
|
KEY_ALL_ACCESS };
|
|
|
|
DWORD desired = 0;
|
|
|
|
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;
|
2007-07-20 16:29:43 +02:00
|
|
|
if (!get_reg_sd (hdl, sd))
|
2006-10-21 13:05:32 +02:00
|
|
|
ret = check_access (sd, mapping, desired, flags);
|
2006-10-22 21:31:33 +02:00
|
|
|
/* As long as we can't write the registry... */
|
|
|
|
if (flags & W_OK)
|
2007-07-17 16:39:02 +02:00
|
|
|
{
|
|
|
|
set_errno (EROFS);
|
|
|
|
ret = -1;
|
|
|
|
}
|
2003-02-21 15:29:18 +01:00
|
|
|
debug_printf ("flags %x, ret %d", flags, ret);
|
|
|
|
return ret;
|
|
|
|
}
|