aboutsummaryrefslogtreecommitdiff
path: root/chmod.c
blob: 225c92d105de16a603ea75190adc748290a37292 (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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include "internal.h"

const char	chmod_usage[] = "chmod [-R] mode file [file ...]\n"
"\nmode may be an octal integer representing the bit pattern for the\n"
"\tnew mode, or a symbolic value matching the pattern\n"
"\t[ugoa]{+|-|=}[rwxst] .\n"
"\t\tu:\tUser\n"
"\t\tg:\tGroup\n"
"\t\to:\tOthers\n"
"\t\ta:\tAll\n"
"\n"
"\n+:\tAdd privilege\n"
"\n-:\tRemove privilege\n"
"\n=:\tSet privilege\n"
"\n"
"\t\tr:\tRead\n"
"\t\tw:\tWrite\n"
"\t\tx:\tExecute\n"
"\t\ts:\tSet User ID\n"
"\t\tt:\t\"Sticky\" Text\n"
"\n"
"\tModes may be concatenated, as in \"u=rwx,g=rx,o=rx,-t,-s\n"
"\n"
"\t-R:\tRecursively change the mode of all files and directories\n"
"\t\tunder the argument directory.";

int
parse_mode(
 const char *	s
,mode_t *		or
,mode_t *		and
,int *			group_execute)
{
	/* [ugoa]{+|-|=}[rwxstl] */
	mode_t	mode = 0;
	mode_t	groups = S_ISVTX;
	char	type;
	char	c;

	do {
		for ( ; ; ) {
			switch ( c = *s++ ) {
			case '\0':
				return -1;
			case 'u':
				groups |= S_ISUID|S_IRWXU;
				continue;
			case 'g':
				groups |= S_ISGID|S_IRWXG;
				continue;
			case 'o':
				groups |= S_IRWXO;
				continue;
			case 'a':
				groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
				continue;
			case '+':
			case '=':
			case '-':
				type = c;
				if ( groups == S_ISVTX ) /* The default is "all" */
					groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
				break;
			default:
				if ( c >= '0' && c <= '7' && mode == 0 && groups == S_ISVTX ) {
					*and = 0;
					*or = strtol(--s, 0, 010);
					return 0;
				}
				else
					return -1;
			}
			break;
		}

		while ( (c = *s++) != '\0' ) {
			switch ( c ) {
			case ',':
				break;
			case 'r':
				mode |= S_IRUSR|S_IRGRP|S_IROTH;
				continue;
			case 'w':
				mode |= S_IWUSR|S_IWGRP|S_IWOTH;
				continue;
			case 'x':
				mode |= S_IXUSR|S_IXGRP|S_IXOTH;
				continue;
			case 's':
				if ( group_execute != 0 && (groups & S_IRWXG) ) {
					if ( *group_execute < 0 )
						return -1;
					if ( type != '-' ) {
						mode |= S_IXGRP;
						*group_execute = 1;
					}
				}
				mode |= S_ISUID|S_ISGID;
				continue;
			case 'l':
				if ( *group_execute > 0 )
					return -1;
				if ( type != '-' ) {
					*and &= ~S_IXGRP;
					*group_execute = -1;
				}
				mode |= S_ISGID;
				groups |= S_ISGID;
				continue;
			case 't':
				mode |= S_ISVTX;
				continue;
			default:
				return -1;
			}
			break;
		}
		switch ( type ) {
		case '=':
			*and &= ~(groups);
			/* fall through */
		case '+':
			*or |= mode & groups;
			break;
		case '-':
			*and &= ~(mode & groups);
			*or &= *and;
			break;
		}
	} while ( c == ',' );
	return 0;
}

extern int
chmod_main(struct FileInfo * i, int argc, char * * argv)
{
	i->andWithMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
	i->orWithMode = 0;

	while ( argc >= 3 ) {
		if ( parse_mode(argv[1], &i->orWithMode, &i->andWithMode, 0)
		 == 0 ) {
			argc--;
			argv++;
		}
		else if ( strcmp(argv[1], "-R") == 0 ) {
			i->recursive = 1;
			argc--;
			argv++;
		}
		else
			break;
	}

	i->changeMode = 1;
	i->complainInPostProcess = 1;

	return monadic_main(i, argc, argv);
}