summaryrefslogtreecommitdiffhomepage
path: root/src/cmds/ntux_cmd_aceit.c
blob: ed50e6e9303dbc117c5cd465a76dc5bd82d04056 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/***********************************************************/
/*  ntux: native translation und extension                 */
/*  Copyright (C) 2016--2022  SysDeer Technologies, LLC    */
/*  Released under GPLv2 and GPLv3; see COPYING.NTUX.      */
/***********************************************************/

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

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

#include <ntapi/nt_object.h>
#include <ntapi/nt_acl.h>
#include <ntapi/nt_file.h>

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


struct ntux_ace_any {
        nt_ace_header   header;
        uint32_t        mask;
        uint32_t        sid_start;
};


struct ntux_sid_any {
        unsigned char                   revision;
        unsigned char                   sub_authority_count;
        nt_sid_identifier_authority     identifier_authority;
        uint32_t                        sub_authority[];
};


static int ntux_cmd_aceit_ret(int fd, struct __ofd * ofd, void * hasync, int ret)
{
	if (hasync)
		__xfi_close_handle(hasync);

	if (ofd)
		__xfi_ofd_ref_dec(ofd);

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

	return ret;
}


static int ntux_cmd_aceit_dump_acl(nt_acl * acl, const char * acldesc, int fdout)
{
	int                     idx;
	int                     saidx;
	size_t                  addr;
	struct ntux_sid_any *   sid;
	struct ntux_ace_any *   ace;
	char                    comma[2];

	ntux_dprintf(
		fdout,
		"  ::sd::%s = {\n"
		"    .revision = 0x%x,\n"
		"    .sbz_1st = 0x%x,\n"
		"    .acl_size = 0x%x,\n"
		"    .ace_count = 0x%x,\n"
		"    .sbz_2nd = 0x%x\n",
		acldesc,
		acl->acl_revision,
		acl->sbz_1st,
		acl->acl_size,
		acl->ace_count,
		acl->sbz_2nd);

	addr  = (size_t)acl;
	addr += sizeof(*acl);

	for (idx=0; idx<acl->ace_count; idx++) {
		ace = (struct ntux_ace_any *)addr;

		ntux_dprintf(
			fdout,
			"    ::sd::%s::ace[%d] = {\n"
			"      .ace_type = 0x%x,\n"
			"      .ace_flags = 0x%x,\n"
			"      .ace_size = 0x%x,\n"
			"      .mask = 0x%x,\n",
			acldesc,idx,
			ace->header.ace_type,
			ace->header.ace_flags,
			ace->header.ace_size,
			ace->mask);

		sid = (struct ntux_sid_any *)&ace->sid_start;

		ntux_dprintf(fdout,
		"      .sid = {\n"
		"        .revision = 0x%x,\n"
		"        .sub_authority_count = 0x%x,\n"
		"        .identifier_authority = {%u,%u,%u,%u,%u,%u}\n"
		"        .sub_authority = {",
		sid->revision,
		sid->sub_authority_count,
		sid->identifier_authority.value[0],
		sid->identifier_authority.value[1],
		sid->identifier_authority.value[2],
		sid->identifier_authority.value[3],
		sid->identifier_authority.value[4],
		sid->identifier_authority.value[5]);

		comma[0] = '\0';
		comma[1] = 0;

		for (saidx=0; saidx<sid->sub_authority_count; saidx++) {
			ntux_dprintf(fdout,"%s%u",comma,sid->sub_authority[saidx]);
			comma[0] = ',';
		}

		ntux_dprintf(fdout,"}\n");
		ntux_dprintf(fdout,"      }\n");
		ntux_dprintf(fdout,"    }\n");

		addr += ace->header.ace_size;
	}

	ntux_dprintf(fdout,"  }\n");

(void)saidx;
	return 0;
}


static int ntux_cmd_aceit_dump(const char * dunit, nt_sd * sd, int fdout)
{
	int             ret;
	size_t          addr;
	nt_acl *        sacl;
	nt_acl *        dacl;

	ntux_dprintf(
		fdout,
		"%s::sd = {\n"
		"  .revision = 0x%x,\n"
		"  .sbz_1st = 0x%x,\n"
		"  .control = 0x%x,\n"
		"  .offset_owner = 0x%x,\n"
		"  .offset_group = 0x%x,\n"
		"  .offset_sacl = 0x%x,\n"
		"  .offset_dacl = 0x%x\n",
		dunit,
		sd->revision,
		sd->sbz_1st,
		sd->control,
		sd->offset_owner,
		sd->offset_group,
		sd->offset_sacl,
		sd->offset_dacl);

	if (sd->offset_sacl) {
		addr  = (size_t)sd;
		addr += sd->offset_sacl;
		sacl  = (nt_acl *)addr;

		if ((ret = ntux_cmd_aceit_dump_acl(sacl,"sacl",fdout)))
			return ret;
	}

	if (sd->offset_dacl) {
		addr  = (size_t)sd;
		addr += sd->offset_dacl;
		dacl  = (nt_acl *)addr;

		if ((ret = ntux_cmd_aceit_dump_acl(dacl,"dacl",fdout)))
			return ret;
	}

	ntux_dprintf(fdout,"}\n");

	return 0;
}


int ntux_cmd_aceit(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;
	size_t			size;
	int			fd      = -1;
	struct __ofd *		ofd     = 0;
	void *			hasync  = 0;
	uint32_t		buf[0x300];

	/* 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_aceit_ret(
				0,0,0,
				NTUX_SYSTEM_ERROR(dctx));

	fd = ret;

	/* ofd */
	if (!(ofd = __xfi_ofd_ref_inc(fd)))
		return ntux_cmd_aceit_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_FILE_SHARE_READ
				| NT_FILE_SHARE_WRITE
				| NT_FILE_SHARE_DELETE)))
		if (ntux_errno_set(dctx,EACCES))
			return ntux_cmd_aceit_ret(
				fd,ofd,0,
				NTUX_SYSTEM_ERROR(dctx));

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

	if ((status = __xfi_query_security_object(
			hasync,
			NT_OWNER_SECURITY_INFORMATION
				| NT_GROUP_SECURITY_INFORMATION
				| NT_DACL_SECURITY_INFORMATION,
			srcsd,sizeof(buf),&size)))
		if (ntux_errno_set(dctx,ENXIO))
			return ntux_cmd_aceit_ret(
				fd,ofd,hasync,
				NTUX_SYSTEM_ERROR(dctx));

	/* dump */
	if (dctx->cctx->drvflags & NTUX_DRIVER_DUMP)
		if ((ntux_cmd_aceit_dump(dunit,srcsd,fdout)))
			return ntux_cmd_aceit_ret(
				fd,ofd,hasync,
				NTUX_SYSTEM_ERROR(dctx));

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