summaryrefslogtreecommitdiff
path: root/usr/src/lib/libcmd/common/chmod.c
blob: 7bfcf09dc881e50a3e02fde8b8cee1cd261ca6dc (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1992-2010 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                  Common Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*            http://www.opensource.org/licenses/cpl1.0.txt             *
*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                  David Korn <dgk@research.att.com>                   *
*                                                                      *
***********************************************************************/
#pragma prototyped
/*
 * David Korn
 * Glenn Fowler
 * AT&T Research
 *
 * chmod
 */

static const char usage[] =
"[-?\n@(#)$Id: chmod (AT&T Research) 2009-07-02 $\n]"
USAGE_LICENSE
"[+NAME?chmod - change the access permissions of files]"
"[+DESCRIPTION?\bchmod\b changes the permission of each file "
	"according to mode, which can be either a symbolic representation "
	"of changes to make, or an octal number representing the bit "
	"pattern for the new permissions.]"
"[+?Symbolic mode strings consist of one or more comma separated list "
	"of operations that can be perfomed on the mode. Each operation is of "
	"the form \auser\a \aop\a \aperm\a where \auser\a is zero or more of "
	"the following letters:]{"
	"[+u?User permission bits.]"
	"[+g?Group permission bits.]"
	"[+o?Other permission bits.]"
	"[+a?All permission bits. This is the default if none are specified.]"
	"}"
"[+?The \aperm\a portion consists of zero or more of the following letters:]{"
	"[+r?Read permission.]"
	"[+s?Setuid when \bu\b is selected for \awho\a and setgid when \bg\b "
		"is selected for \awho\a.]"
	"[+w?Write permission.]"
	"[+x?Execute permission for files, search permission for directories.]"
	"[+X?Same as \bx\b except that it is ignored for files that do not "
		"already have at least one \bx\b bit set.]"
	"[+l?Exclusive lock bit on systems that support it. Group execute "
		"must be off.]"
	"[+t?Sticky bit on systems that support it.]"
	"}"
"[+?The \aop\a portion consists of one or more of the following characters:]{"
	"[++?Cause the permission selected to be added to the existing "
		"permissions. | is equivalent to +.]"
	"[+-?Cause the permission selected to be removed to the existing "
		"permissions.]"
	"[+=?Cause the permission to be set to the given permissions.]"
	"[+&?Cause the permission selected to be \aand\aed with the existing "
		"permissions.]"
	"[+^?Cause the permission selected to be propagated to more "
		"restrictive groups.]"
	"}"
"[+?Symbolic modes with the \auser\a portion omitted are subject to "
	"\bumask\b(2) settings unless the \b=\b \aop\a or the "
	"\b--ignore-umask\b option is specified.]"
"[+?A numeric mode is from one to four octal digits (0-7), "
	"derived by adding up the bits with values 4, 2, and 1. "
	"Any omitted digits are assumed to be leading zeros. The "
	"first digit selects the set user ID (4) and set group ID "
	"(2) and save text image (1) attributes. The second digit "
	"selects permissions for the user who owns the file: read "
	"(4), write (2), and execute (1); the third selects permissions"
	"for other users in the file's group, with the same values; "
	"and the fourth for other users not in the file's group, with "
	"the same values.]"

"[+?For symbolic links, by default, \bchmod\b changes the mode on the file "
	"referenced by the symbolic link, not on the symbolic link itself. "
	"The \b-h\b options can be specified to change the mode of the link. "
	"When traversing directories with \b-R\b, \bchmod\b either follows "
	"symbolic links or does not follow symbolic links, based on the "
	"options \b-H\b, \b-L\b, and \b-P\b. The configuration parameter "
	"\bPATH_RESOLVE\b determines the default behavior if none of these "
	"options is specified.]"

"[+?When the \b-c\b or \b-v\b options are specified, change notifications "
	"are written to standard output using the format, "
	"\b%s: mode changed to %0.4o (%s)\b, with arguments of the "
	"pathname, the numeric mode, and the resulting permission bits as "
	"would be displayed by the \bls\b command.]"

"[+?For backwards compatibility, if an invalid option is given that is a valid "
	"symbolic mode specification, \bchmod\b treats this as a mode "
	"specification rather than as an option specification.]"

"[H:metaphysical?Follow symbolic links for command arguments; otherwise don't "
	"follow symbolic links when traversing directories.]"
"[L:logical|follow?Follow symbolic links when traversing directories.]"
"[P:physical|nofollow?Don't follow symbolic links when traversing directories.]"
"[R:recursive?Change the mode for files in subdirectories recursively.]"
"[c:changes?Describe only files whose permission actually change.]"
"[f:quiet|silent?Do not report files whose permissioins fail to change.]"
"[h:symlink?Change the mode of the symbolic links on systems that "
	"support this.]"
"[i:ignore-umask?Ignore the \bumask\b(2) value in symbolic mode "
	"expressions. This is probably how you expect \bchmod\b to work.]"
"[n:show?Show actions but do not change any file modes.]"
"[F:reference?Omit the \amode\a operand and use the mode of \afile\a "
	"instead.]:[file]"
"[v:verbose?Describe changed permissions of all files.]"
"\n"
"\nmode file ...\n"
"\n"
"[+EXIT STATUS?]{"
	"[+0?All files changed successfully.]"
	"[+>0?Unable to change mode of one or more files.]"
"}"
"[+SEE ALSO?\bchgrp\b(1), \bchown\b(1), \btw\b(1), \bgetconf\b(1), \bls\b(1), "
	"\bumask\b(2)]"
;


#if defined(__STDPP__directive) && defined(__STDPP__hide)
__STDPP__directive pragma pp:hide lchmod
#else
#define lchmod		______lchmod
#endif

#include <cmd.h>
#include <ls.h>
#include <fts_fix.h>

#include "FEATURE/symlink"

#if defined(__STDPP__directive) && defined(__STDPP__hide)
__STDPP__directive pragma pp:nohide lchmod
#else
#undef	lchmod
#endif

extern int	lchmod(const char*, mode_t);

int
b_chmod(int argc, char** argv, void* context)
{
	register int	mode;
	register int	force = 0;
	register int	flags;
	register char*	amode = 0;
	register FTS*	fts;
	register FTSENT*ent;
	char*		last;
	int		(*chmodf)(const char*, mode_t);
	int		logical = 1;
	int		notify = 0;
	int		ignore = 0;
	int		show = 0;
#if _lib_lchmod
	int		chlink = 0;
#endif
	struct stat	st;

	cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY);
	flags = fts_flags() | FTS_TOP | FTS_NOPOSTORDER | FTS_NOSEEDOTDIR;

	/*
	 * NOTE: we diverge from the normal optget boilerplate
	 *	 to allow `chmod -x etc' to fall through
	 */

	for (;;)
	{
		switch (optget(argv, usage))
		{
		case 'c':
			notify = 1;
			continue;
		case 'f':
			force = 1;
			continue;
		case 'h':
#if _lib_lchmod
			chlink = 1;
#endif
			continue;
		case 'i':
			ignore = 1;
			continue;
		case 'n':
			show = 1;
			continue;
		case 'v':
			notify = 2;
			continue;
		case 'F':
			if (stat(opt_info.arg, &st))
				error(ERROR_exit(1), "%s: cannot stat", opt_info.arg);
			mode = st.st_mode;
			amode = "";
			continue;
		case 'H':
			flags |= FTS_META|FTS_PHYSICAL;
			logical = 0;
			continue;
		case 'L':
			flags &= ~(FTS_META|FTS_PHYSICAL);
			logical = 0;
			continue;
		case 'P':
			flags &= ~FTS_META;
			flags |= FTS_PHYSICAL;
			logical = 0;
			continue;
		case 'R':
			flags &= ~FTS_TOP;
			logical = 0;
			continue;
		case '?':
			error(ERROR_usage(2), "%s", opt_info.arg);
			break;
		}
		break;
	}
	argv += opt_info.index;
	if (error_info.errors || !*argv || !amode && !*(argv + 1))
		error(ERROR_usage(2), "%s", optusage(NiL));
	if (logical)
		flags &= ~(FTS_META|FTS_PHYSICAL);
	if (ignore)
		ignore = umask(0);
	if (amode)
		amode = 0;
	else
	{
		amode = *argv++;
		mode = strperm(amode, &last, 0);
		if (*last)
		{
			if (ignore)
				umask(ignore);
			error(ERROR_exit(1), "%s: invalid mode", amode);
		}
	}
	chmodf =
#if _lib_lchmod
		chlink ? lchmod :
#endif
		chmod;
	if (!(fts = fts_open(argv, flags, NiL)))
	{
		if (ignore)
			umask(ignore);
		error(ERROR_system(1), "%s: not found", *argv);
	}
	while (!sh_checksig(context) && (ent = fts_read(fts)))
		switch (ent->fts_info)
		{
		case FTS_SL:
			if (chmodf == chmod)
			{
				if (!(flags & FTS_PHYSICAL) || (flags & FTS_META) && ent->fts_level == 1)
					fts_set(NiL, ent, FTS_FOLLOW);
				break;
			}
			/*FALLTHROUGH*/
		case FTS_F:
		case FTS_D:
		case FTS_SLNONE:
		anyway:
			if (amode)
				mode = strperm(amode, &last, ent->fts_statp->st_mode);
			if (show || (*chmodf)(ent->fts_accpath, mode) >= 0)
			{
				if (notify == 2 || notify == 1 && (mode&S_IPERM) != (ent->fts_statp->st_mode&S_IPERM))
					sfprintf(sfstdout, "%s: mode changed to %0.4o (%s)\n", ent->fts_path, mode, fmtmode(mode, 1)+1);
			}
			else if (!force)
				error(ERROR_system(0), "%s: cannot change mode", ent->fts_accpath);
			break;
		case FTS_DC:
			if (!force)
				error(ERROR_warn(0), "%s: directory causes cycle", ent->fts_accpath);
			break;
		case FTS_DNR:
			if (!force)
				error(ERROR_system(0), "%s: cannot read directory", ent->fts_accpath);
			goto anyway;
		case FTS_DNX:
			if (!force)
				error(ERROR_system(0), "%s: cannot search directory", ent->fts_accpath);
			goto anyway;
		case FTS_NS:
			if (!force)
				error(ERROR_system(0), "%s: not found", ent->fts_accpath);
			break;
		}
	fts_close(fts);
	if (ignore)
		umask(ignore);
	return error_info.errors != 0;
}