summaryrefslogtreecommitdiffhomepage
path: root/src/cmds/ntux_cmd_chmod.c
blob: 03ab8f5b9e30b42d1dbebdcdacf7e8f748e6f22a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/***********************************************************/
/*  ntux: native translation und extension                 */
/*  Copyright (C) 2016--2018  Z. Gilboa                    */
/*  Released under GPLv2 and GPLv3; see COPYING.NTUX.      */
/***********************************************************/

#include <ntapi/ntapi.h>
#include <psxabi/sys_sysapi.h>
#include <psxabi/sys_stat.h>
#include <psxabi/sys_errno.h>

#include <psxxfi/xfi_fs.h>
#include <psxxfi/xfi_ofd.h>
#include <psxxfi/xfi_unicode.h>

#include <ntux/ntux.h>
#include "ntux_driver_impl.h"
#include "ntux_nolibc_impl.h"
#include "ntux_errinfo_impl.h"

static int ntux_cmd_chmod_ret(int fd, struct __ofd * ofd, void * hasync, int ret)
{
	if (hasync)
		ntapi->zw_close(hasync);

	if (ofd)
		__xfi_ofd_ref_dec(ofd);

	if (fd >= 0)
		__sys_close(fd);

	return ret;
}

int ntux_cmd_chmod(const struct ntux_driver_ctx * dctx, const char * dunit)
{
	intptr_t		ret;
	int32_t			status;
	int			fdout;
	int			fdcwd;
	const unsigned char *	unit;
	nt_sd *			srcsd;
	nt_sd_common_buffer	dstsd;
	nt_sd_common_meta	meta;
	uint32_t		access_owner;
	uint32_t		access_group;
	uint32_t		access_other;
	uint32_t		access_admin;
	size_t			size;
	int			fd      = -1;
	struct __ofd *		ofd     = 0;
	void *			hasync  = 0;
	uint32_t		buf[0x300];

	/* initial version: only support m+p */
	if (!dctx->cctx->strmode || strcmp(dctx->cctx->strmode,"m+p"))
		return ntux_cmd_chmod_ret(
			0,0,0,
			NTUX_CUSTOM_ERROR(
				dctx,
				NTUX_ERR_FLEE_ERROR));

	/* init */
	ntux_driver_set_ectx(
		dctx,0,dunit);

	unit = (const unsigned char *)dunit;

	/* fdctx */
	fdout = ntux_driver_fdout(dctx);
	fdcwd = ntux_driver_fdcwd(dctx);

	/* fd */
	if ((ret = __sys_openat(fdcwd,unit,0,0)) < 0)
		if (ntux_errno_set(dctx,ret))
			return ntux_cmd_chmod_ret(
				0,0,0,
				NTUX_SYSTEM_ERROR(dctx));

	fd = ret;

	/* ofd */
	if (!(ofd = __xfi_ofd_ref_inc(fd)))
		return ntux_cmd_chmod_ret(
			fd,0,0,
			NTUX_CUSTOM_ERROR(
				dctx,
				NTUX_ERR_FLOW_ERROR));

	/* hasync */
	if ((status = __xfi_fs_open_async(
			&hasync,
			ofd->info.hfile,0,
			NT_SEC_READ_CONTROL
				| NT_SEC_WRITE_DAC,
			NT_FILE_SHARE_READ
				| NT_FILE_SHARE_WRITE
				| NT_FILE_SHARE_DELETE)))
		return ntux_cmd_chmod_ret(
			fd,ofd,0,
			NTUX_SYSTEM_ERROR(dctx));

	/* srcsd */
	srcsd = (nt_sd *)buf;

	if ((status = ntapi->zw_query_security_object(
			hasync,
			NT_OWNER_SECURITY_INFORMATION
				| NT_GROUP_SECURITY_INFORMATION
				| NT_DACL_SECURITY_INFORMATION,
			srcsd,sizeof(buf),&size)))
		return ntux_cmd_chmod_ret(
			fd,ofd,hasync,
			NTUX_SYSTEM_ERROR(dctx));

	if ((status = ntapi->acl_init_common_descriptor_meta(
			&meta,srcsd,
			NT_ACL_INIT_COMMON_DESCRIPTION_META_STRICT_MODE)))
		return ntux_cmd_chmod_ret(
			fd,ofd,hasync,
			NTUX_SYSTEM_ERROR(dctx));

	/* source permissions */
	access_owner  = meta.owner_ace  ? meta.owner_ace->mask  : 0;
	access_group  = meta.group_ace  ? meta.group_ace->mask  : 0;
	access_other  = meta.other_ace  ? meta.other_ace->mask  : 0;

	/* initial version: only support m+p */
	access_admin = access_owner | NT_SEC_WRITE_DAC;

	/* updated dacl */
	ntapi->acl_init_common_descriptor(
		&dstsd,
		meta.owner,meta.group,0,0,
		access_owner,access_group,access_other,
		access_admin,meta.system_acc);

	if ((status = ntapi->zw_set_security_object(
			hasync,
			NT_DACL_SECURITY_INFORMATION,
			&dstsd.sd)))
		return ntux_cmd_chmod_ret(
			fd,ofd,hasync,
			NTUX_SYSTEM_ERROR(dctx));

	/* changes */
	(void)fdout;

	/* all done */
	return ntux_cmd_chmod_ret(fd,ofd,hasync,0);
}