summaryrefslogtreecommitdiffstats
path: root/common/mode.c
blob: b643a60984d23847f738e3adfcefadc63d8e67e4 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2023  Patrick McDermott
 *
 * This file is part of opkg-opk.
 *
 * opkg-opk is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * opkg-opk is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with opkg-opk.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <sys/stat.h>
#include "defs.h"
#include "mode.h"

static const mode_t OPKG_OPK_HELPER_MODE_U_ = 04700;
static const mode_t OPKG_OPK_HELPER_MODE_G_ = 02070;
static const mode_t OPKG_OPK_HELPER_MODE_O_ = 00007;
static const mode_t OPKG_OPK_HELPER_MODE_A_ = 01777;

struct _opkg_opk_helper_mode {
	mode_t      umask;
	mode_t      cur_mode;
	const char *sym_mode;
	mode_t     *new_mode;
	mode_t      who;
	char        op;
};

/*
 * Grammar and operations from POSIX chown utility:
 * https://pubs.opengroup.org/onlinepubs/9699919799/utilities/chmod.html
 */

static void
_opkg_opk_helper_mode_chmod(struct _opkg_opk_helper_mode *mode, mode_t perm)
{
	switch (mode->op) {
		case '+':
			*mode->new_mode |= (mode->who & perm);
			break;
		case '-':
		default:  /* Should be unreachable */
			*mode->new_mode ^= (mode->who & perm);
			break;
	}
}

static int
_opkg_opk_helper_mode_parse_perm(struct _opkg_opk_helper_mode *mode)
{
	switch (*mode->sym_mode) {
		case 'r':
			if (mode->who == 0000) {
				mode->who = OPKG_OPK_HELPER_MODE_A_^mode->umask;
			}
			_opkg_opk_helper_mode_chmod(mode, 00444);
			break;
		case 'w':
			if (mode->who == 0000) {
				mode->who = OPKG_OPK_HELPER_MODE_A_^mode->umask;
			}
			_opkg_opk_helper_mode_chmod(mode, 00222);
			break;
		case 'x':
			if (mode->who == 0000) {
				mode->who = OPKG_OPK_HELPER_MODE_A_^mode->umask;
			}
			_opkg_opk_helper_mode_chmod(mode, 00111);
			break;
		case 'X':
			if (mode->who == 0000) {
				mode->who = OPKG_OPK_HELPER_MODE_A_^mode->umask;
			}
			if (S_ISDIR(mode->cur_mode)
					|| mode->cur_mode & 00111) {
				_opkg_opk_helper_mode_chmod(mode, 00111);
			}
			break;
		case 's':
			if (mode->who == 0000) {
				mode->who = OPKG_OPK_HELPER_MODE_A_^mode->umask;
			}
			_opkg_opk_helper_mode_chmod(mode, 06000);
			break;
		case 't':
			if (S_ISDIR(mode->cur_mode) && (mode->who ==
						OPKG_OPK_HELPER_MODE_A_ ||
						mode->who == 0000)) {
				mode->who = OPKG_OPK_HELPER_MODE_A_;
				_opkg_opk_helper_mode_chmod(mode, 01000);
			}
			break;
		default:
			return OPKG_OPK_ERROR;
	}
	++mode->sym_mode;
	return OPKG_OPK_OK;
}

static int
_opkg_opk_helper_mode_parse_permlist(struct _opkg_opk_helper_mode *mode)
{
	if (_opkg_opk_helper_mode_parse_perm(mode) != OPKG_OPK_OK) {
		return OPKG_OPK_ERROR;
	}
	_opkg_opk_helper_mode_parse_permlist(mode);
	return OPKG_OPK_OK;
}

static int
_opkg_opk_helper_mode_parse_op(struct _opkg_opk_helper_mode *mode)
{
	switch (*mode->sym_mode) {
		case '+':
		case '-':
			mode->op = *mode->sym_mode;
			break;
		case '=':
			if (mode->who == 0000) {
				*mode->new_mode &= ~07777;
			} else {
				*mode->new_mode &= ~mode->who;
			}
			mode->op = '+';
			break;
		default:
			return OPKG_OPK_ERROR;
	}
	++mode->sym_mode;
	return OPKG_OPK_OK;
}

static int
_opkg_opk_helper_mode_parse_permcopy(struct _opkg_opk_helper_mode *mode)
{
	mode_t perm;

	switch (*mode->sym_mode) {
		case 'u':
			perm = (mode->cur_mode & 0700)
			     | (mode->cur_mode & 0700) >> 3
			     | (mode->cur_mode & 0700) >> 6;
			break;
		case 'g':
			perm = (mode->cur_mode & 0070) << 3
			     | (mode->cur_mode & 0070)
			     | (mode->cur_mode & 0070) >> 3;
			break;
		case 'o':
			perm = (mode->cur_mode & 0007) << 6
			     | (mode->cur_mode & 0007) << 3
			     | (mode->cur_mode & 0007);
			break;
		default:
			return OPKG_OPK_ERROR;
	}
	if (mode->who == 0000) {
		mode->who = OPKG_OPK_HELPER_MODE_A_ ^ mode->umask;
	}
	_opkg_opk_helper_mode_chmod(mode, perm);
	++mode->sym_mode;
	return OPKG_OPK_OK;
}

static int
_opkg_opk_helper_mode_parse_action(struct _opkg_opk_helper_mode *mode)
{
	if (_opkg_opk_helper_mode_parse_op(mode) != OPKG_OPK_OK) {
		return OPKG_OPK_ERROR;
	}
	if (_opkg_opk_helper_mode_parse_permlist(mode) != OPKG_OPK_OK) {
		_opkg_opk_helper_mode_parse_permcopy(mode);
	}
	return OPKG_OPK_OK;
}

static int
_opkg_opk_helper_mode_parse_actionlist(struct _opkg_opk_helper_mode *mode)
{
	if (_opkg_opk_helper_mode_parse_action(mode) != OPKG_OPK_OK) {
		return OPKG_OPK_ERROR;
	}
	_opkg_opk_helper_mode_parse_actionlist(mode);
	return OPKG_OPK_OK;
}

static int
_opkg_opk_helper_mode_parse_who(struct _opkg_opk_helper_mode *mode)
{
	switch (*mode->sym_mode) {
		case 'u':
			mode->who |= OPKG_OPK_HELPER_MODE_U_;
			break;
		case 'g':
			mode->who |= OPKG_OPK_HELPER_MODE_G_;
			break;
		case 'o':
			mode->who |= OPKG_OPK_HELPER_MODE_O_;
			break;
		case 'a':
			mode->who |= OPKG_OPK_HELPER_MODE_A_;
			break;
		default:
			return OPKG_OPK_ERROR;
	}
	++mode->sym_mode;
	return OPKG_OPK_OK;
}

static int
_opkg_opk_helper_mode_parse_wholist(struct _opkg_opk_helper_mode *mode)
{
	if (_opkg_opk_helper_mode_parse_who(mode) != OPKG_OPK_OK) {
		return OPKG_OPK_ERROR;
	}
	_opkg_opk_helper_mode_parse_wholist(mode);
	return OPKG_OPK_OK;
}

static int
_opkg_opk_helper_mode_parse_clause(struct _opkg_opk_helper_mode *mode)
{
	mode->who = 0000;
	mode->op  = '\0';
	_opkg_opk_helper_mode_parse_wholist(mode);
	if (_opkg_opk_helper_mode_parse_actionlist(mode) != OPKG_OPK_OK) {
		return OPKG_OPK_ERROR;
	}
	return OPKG_OPK_OK;
}

static int
_opkg_opk_helper_mode_parse_symbolic_mode(struct _opkg_opk_helper_mode *mode)
{
	if (_opkg_opk_helper_mode_parse_clause(mode) != OPKG_OPK_OK) {
		return OPKG_OPK_ERROR;
	}
	if (*mode->sym_mode != ',') {
		return OPKG_OPK_OK;
	}
	++mode->sym_mode;
	if (_opkg_opk_helper_mode_parse_symbolic_mode(mode) != OPKG_OPK_OK) {
		return OPKG_OPK_ERROR;
	}
	return OPKG_OPK_OK;
}

int
opkg_opk_helper_mode_parse(mode_t cur_mode, const char *mode_str,
		mode_t *new_mode)
{
	char                         *end;
	long int                      mode_long;
	struct _opkg_opk_helper_mode  mode;

	mode_long = strtol(mode_str, &end, 8);
	if (*end == '\0') {
		if ((mode_long & 07777) != mode_long) {
			return OPKG_OPK_ERROR;
		}
		*new_mode = (mode_long & 07777);
		return OPKG_OPK_OK;
	}

	mode.umask = umask(0);
	umask(mode.umask);

	mode.cur_mode = cur_mode;
	mode.sym_mode = mode_str;
	mode.new_mode = new_mode;

	*new_mode = cur_mode;

	return _opkg_opk_helper_mode_parse_symbolic_mode(&mode);
}