xref: /titanic_51/usr/src/lib/libcmd/common/chgrp.c (revision d8d810637042c2a73a269f125dee75e8ed1421d1)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1992-2008 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                                                                      *
20 ***********************************************************************/
21 #pragma prototyped
22 /*
23  * David Korn
24  * Glenn Fowler
25  * AT&T Research
26  *
27  * chgrp+chown
28  */
29 
30 static const char usage_1[] =
31 "[-?@(#)$Id: chgrp (AT&T Research) 2008-03-28 $\n]"
32 USAGE_LICENSE
33 ;
34 
35 static const char usage_grp_1[] =
36 "[+NAME?chgrp - change the group ownership of files]"
37 "[+DESCRIPTION?\bchgrp\b changes the group ownership of each file"
38 "	to \agroup\a, which can be either a group name or a numeric"
39 "	group id. The user ownership of each file may also be changed to"
40 "	\auser\a by prepending \auser\a\b:\b to the group name.]"
41 ;
42 
43 static const char usage_own_1[] =
44 "[+NAME?chown - change the ownership of files]"
45 "[+DESCRIPTION?\bchown\b changes the ownership of each file"
46 "	to \auser\a, which can be either a user name or a numeric"
47 "	user id. The group ownership of each file may also be changed to"
48 "	\auser\a by appending \b:\b\agroup\a to the user name.]"
49 ;
50 
51 static const char usage_2[] =
52 "[b:before?Only change files with \bctime\b before (less than) the "
53     "\bmtime\b of \afile\a.]:[file]"
54 "[c:changes?Describe only files whose ownership actually changes.]"
55 "[f:quiet|silent?Do not report files whose ownership fails to change.]"
56 "[l|h:symlink?Change the ownership of the symbolic links on systems that "
57     "support this.]"
58 "[m:map?The first operand is interpreted as a file that contains a map "
59     "of space separated \afrom_uid:from_gid to_uid:to_gid\a pairs. The "
60     "\auid\a or \agid\a part of each pair may be omitted to mean any \auid\a "
61     "or \agid\a. Ownership of files matching the \afrom\a part of any pair "
62     "is changed to the corresponding \ato\a part of the pair. The matching "
63     "for each file operand is in the order \auid\a:\agid\a, \auid\a:, "
64     ":\agid\a. For a given file, once a \auid\a or \agid\a mapping is "
65     "determined it is not overridden by any subsequent match. Unmatched "
66     "files are silently ignored.]"
67 "[n:show?Show actions but don't execute.]"
68 "[r:reference?Omit the explicit ownership operand and use the ownership "
69     "of \afile\a instead.]:[file]"
70 "[u:unmapped?Print a diagnostic for each file for which either the "
71     "\auid\a or \agid\a or both were not mapped.]"
72 "[v:verbose?Describe changed permissions of all files.]"
73 "[H:metaphysical?Follow symbolic links for command arguments; otherwise "
74     "don't follow symbolic links when traversing directories.]"
75 "[L:logical|follow?Follow symbolic links when traversing directories.]"
76 "[P:physical|nofollow?Don't follow symbolic links when traversing "
77     "directories.]"
78 "[R:recursive?Recursively change ownership of directories and their "
79     "contents.]"
80 "[X:test?Canonicalize output for testing.]"
81 
82 "\n"
83 "\n"
84 ;
85 
86 static const char usage_3[] =
87 " file ...\n"
88 "\n"
89 "[+EXIT STATUS?]{"
90 	"[+0?All files changed successfully.]"
91 	"[+>0?Unable to change ownership of one or more files.]"
92 "}"
93 "[+SEE ALSO?\bchmod\b(1), \btw\b(1), \bgetconf\b(1), \bls\b(1)]"
94 ;
95 
96 #if defined(__STDPP__directive) && defined(__STDPP__hide)
97 __STDPP__directive pragma pp:hide lchown
98 #else
99 #define lchown		______lchown
100 #endif
101 
102 #include <cmd.h>
103 #include <cdt.h>
104 #include <ls.h>
105 #include <ctype.h>
106 #include <fts.h>
107 
108 #include "FEATURE/symlink"
109 
110 #if defined(__STDPP__directive) && defined(__STDPP__hide)
111 __STDPP__directive pragma pp:nohide lchown
112 #else
113 #undef	lchown
114 #endif
115 
116 typedef struct Key_s			/* uid/gid key			*/
117 {
118 	int		uid;		/* uid				*/
119 	int		gid;		/* gid				*/
120 } Key_t;
121 
122 typedef struct Map_s			/* uid/gid map			*/
123 {
124 	Dtlink_t	link;		/* dictionary link		*/
125 	Key_t		key;		/* key				*/
126 	Key_t		to;		/* map to these			*/
127 } Map_t;
128 
129 #define NOID		(-1)
130 
131 #define OPT_CHOWN	(1<<0)		/* chown			*/
132 #define OPT_FORCE	(1<<1)		/* ignore errors		*/
133 #define OPT_GID		(1<<2)		/* have gid			*/
134 #define OPT_LCHOWN	(1<<3)		/* lchown			*/
135 #define OPT_SHOW	(1<<4)		/* show but don't do		*/
136 #define OPT_TEST	(1<<5)		/* canonicalize output		*/
137 #define OPT_UID		(1<<6)		/* have uid			*/
138 #define OPT_UNMAPPED	(1<<7)		/* unmapped file diagnostic	*/
139 #define OPT_VERBOSE	(1<<8)		/* have uid			*/
140 
141 extern int	lchown(const char*, uid_t, gid_t);
142 
143 #if !_lib_lchown
144 
145 #ifndef ENOSYS
146 #define ENOSYS	EINVAL
147 #endif
148 
149 int
150 lchown(const char* path, uid_t uid, gid_t gid)
151 {
152 	return ENOSYS;
153 }
154 
155 #endif /* _lib_chown */
156 
157 /*
158  * parse uid and gid from s
159  */
160 
161 static void
162 getids(register char* s, char** e, Key_t* key, int options)
163 {
164 	register char*	t;
165 	register int	n;
166 	char*		z;
167 	char		buf[64];
168 
169 	key->uid = key->gid = NOID;
170 	while (isspace(*s))
171 		s++;
172 	for (t = s; (n = *t) && n != ':' && n != '.' && !isspace(n); t++);
173 	if (n)
174 	{
175 		options |= OPT_CHOWN;
176 		if ((n = t++ - s) >= sizeof(buf))
177 			n = sizeof(buf) - 1;
178 		*((s = (char*)memcpy(buf, s, n)) + n) = 0;
179 	}
180 	if (options & OPT_CHOWN)
181 	{
182 		if (*s)
183 		{
184 			if ((n = struid(s)) == NOID)
185 			{
186 				n = (int)strtol(s, &z, 0);
187 				if (*z)
188 					error(ERROR_exit(1), "%s: unknown user", s);
189 			}
190 			key->uid = n;
191 		}
192 		for (s = t; (n = *t) && !isspace(n); t++);
193 		if (n)
194 		{
195 			if ((n = t++ - s) >= sizeof(buf))
196 				n = sizeof(buf) - 1;
197 			*((s = (char*)memcpy(buf, s, n)) + n) = 0;
198 		}
199 	}
200 	if (*s)
201 	{
202 		if ((n = strgid(s)) == NOID)
203 		{
204 			n = (int)strtol(s, &z, 0);
205 			if (*z)
206 				error(ERROR_exit(1), "%s: unknown group", s);
207 		}
208 		key->gid = n;
209 	}
210 	if (e)
211 		*e = t;
212 }
213 
214 int
215 b_chgrp(int argc, char** argv, void* context)
216 {
217 	register int	options = 0;
218 	register char*	s;
219 	register Map_t*	m;
220 	register FTS*	fts;
221 	register FTSENT*ent;
222 	register int	i;
223 	Dt_t*		map = 0;
224 	int		flags;
225 	int		uid;
226 	int		gid;
227 	char*		op;
228 	char*		usage;
229 	char*		t;
230 	Sfio_t*		sp;
231 	unsigned long	before;
232 	Dtdisc_t	mapdisc;
233 	Key_t		keys[3];
234 	Key_t		key;
235 	struct stat	st;
236 	int		(*chownf)(const char*, uid_t, gid_t);
237 
238 	cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY);
239 	flags = fts_flags() | FTS_TOP | FTS_NOPOSTORDER | FTS_NOSEEDOTDIR;
240 	before = ~0;
241 	if (!(sp = sfstropen()))
242 		error(ERROR_SYSTEM|3, "out of space");
243 	sfputr(sp, usage_1, -1);
244 	if (error_info.id[2] == 'g')
245 		sfputr(sp, usage_grp_1, -1);
246 	else
247 	{
248 		sfputr(sp, usage_own_1, -1);
249 		options |= OPT_CHOWN;
250 	}
251 	sfputr(sp, usage_2, -1);
252 	if (options & OPT_CHOWN)
253 		sfputr(sp, ERROR_translate(0, 0, 0, "[owner[:group]]"), -1);
254 	else
255 		sfputr(sp, ERROR_translate(0, 0, 0, "[[owner:]group]"), -1);
256 	sfputr(sp, usage_3, -1);
257 	if (!(usage = sfstruse(sp)))
258 		error(ERROR_SYSTEM|3, "out of space");
259 	for (;;)
260 	{
261 		switch (optget(argv, usage))
262 		{
263 		case 'b':
264 			if (stat(opt_info.arg, &st))
265 				error(ERROR_exit(1), "%s: cannot stat", opt_info.arg);
266 			before = st.st_mtime;
267 			continue;
268 		case 'c':
269 		case 'v':
270 			options |= OPT_VERBOSE;
271 			continue;
272 		case 'f':
273 			options |= OPT_FORCE;
274 			continue;
275 		case 'l':
276 			options |= OPT_LCHOWN;
277 			continue;
278 		case 'm':
279 			memset(&mapdisc, 0, sizeof(mapdisc));
280 			mapdisc.key = offsetof(Map_t, key);
281 			mapdisc.size = sizeof(Key_t);
282 			if (!(map = dtopen(&mapdisc, Dthash)))
283 				error(ERROR_exit(1), "out of space [id map]");
284 			continue;
285 		case 'n':
286 			options |= OPT_SHOW;
287 			continue;
288 		case 'r':
289 			if (stat(opt_info.arg, &st))
290 				error(ERROR_exit(1), "%s: cannot stat", opt_info.arg);
291 			uid = st.st_uid;
292 			gid = st.st_gid;
293 			options |= OPT_UID|OPT_GID;
294 			continue;
295 		case 'u':
296 			options |= OPT_UNMAPPED;
297 			continue;
298 		case 'H':
299 			flags |= FTS_META|FTS_PHYSICAL;
300 			continue;
301 		case 'L':
302 			flags &= ~(FTS_META|FTS_PHYSICAL);
303 			continue;
304 		case 'P':
305 			flags &= ~FTS_META;
306 			flags |= FTS_PHYSICAL;
307 			continue;
308 		case 'R':
309 			flags &= ~FTS_TOP;
310 			continue;
311 		case 'X':
312 			options |= OPT_TEST;
313 			continue;
314 		case ':':
315 			error(2, "%s", opt_info.arg);
316 			continue;
317 		case '?':
318 			error(ERROR_usage(2), "%s", opt_info.arg);
319 			break;
320 		}
321 		break;
322 	}
323 	argv += opt_info.index;
324 	argc -= opt_info.index;
325 	if (error_info.errors || argc < 2)
326 		error(ERROR_usage(2), "%s", optusage(NiL));
327 	s = *argv;
328 	if (map)
329 	{
330 		if (streq(s, "-"))
331 			sp = sfstdin;
332 		else if (!(sp = sfopen(NiL, s, "r")))
333 			error(ERROR_exit(1), "%s: cannot read", s);
334 		while (s = sfgetr(sp, '\n', 1))
335 		{
336 			getids(s, &t, &key, options);
337 			if (!(m = (Map_t*)dtmatch(map, &key)))
338 			{
339 				if (!(m = (Map_t*)stakalloc(sizeof(Map_t))))
340 					error(ERROR_exit(1), "out of space [id dictionary]");
341 				m->key = key;
342 				m->to.uid = m->to.gid = NOID;
343 				dtinsert(map, m);
344 			}
345 			getids(t, NiL, &m->to, options);
346 		}
347 		if (sp != sfstdin)
348 			sfclose(sp);
349 		keys[1].gid = keys[2].uid = NOID;
350 	}
351 	else if (!(options & (OPT_UID|OPT_GID)))
352 	{
353 		getids(s, NiL, &key, options);
354 		if ((uid = key.uid) != NOID)
355 			options |= OPT_UID;
356 		if ((gid = key.gid) != NOID)
357 			options |= OPT_GID;
358 	}
359 	switch (options & (OPT_UID|OPT_GID))
360 	{
361 	case OPT_UID:
362 		s = ERROR_translate(0, 0, 0, " owner");
363 		break;
364 	case OPT_GID:
365 		s = ERROR_translate(0, 0, 0, " group");
366 		break;
367 	case OPT_UID|OPT_GID:
368 		s = ERROR_translate(0, 0, 0, " owner and group");
369 		break;
370 	default:
371 		s = "";
372 		break;
373 	}
374 	if (!(fts = fts_open(argv + 1, flags, NiL)))
375 		error(ERROR_system(1), "%s: not found", argv[1]);
376 	while (!sh_checksig(context) && (ent = fts_read(fts)))
377 		switch (ent->fts_info)
378 		{
379 		case FTS_F:
380 		case FTS_D:
381 		case FTS_SL:
382 		case FTS_SLNONE:
383 		anyway:
384 			if ((unsigned long)ent->fts_statp->st_ctime >= before)
385 				break;
386 			if (map)
387 			{
388 				options &= ~(OPT_UID|OPT_GID);
389 				uid = gid = NOID;
390 				keys[0].uid = keys[1].uid = ent->fts_statp->st_uid;
391 				keys[0].gid = keys[2].gid = ent->fts_statp->st_gid;
392 				i = 0;
393 				do
394 				{
395 					if (m = (Map_t*)dtmatch(map, &keys[i]))
396 					{
397 						if (uid == NOID && m->to.uid != NOID)
398 						{
399 							uid = m->to.uid;
400 							options |= OPT_UID;
401 						}
402 						if (gid == NOID && m->to.gid != NOID)
403 						{
404 							gid = m->to.gid;
405 							options |= OPT_GID;
406 						}
407 					}
408 				} while (++i < elementsof(keys) && (uid == NOID || gid == NOID));
409 			}
410 			else
411 			{
412 				if (!(options & OPT_UID))
413 					uid = ent->fts_statp->st_uid;
414 				if (!(options & OPT_GID))
415 					gid = ent->fts_statp->st_gid;
416 			}
417 			if ((options & OPT_UNMAPPED) && (uid == NOID || gid == NOID))
418 			{
419 				if (uid == NOID && gid == NOID)
420 					error(ERROR_warn(0), "%s: uid and gid not mapped", ent->fts_path);
421 				else if (uid == NOID)
422 					error(ERROR_warn(0), "%s: uid not mapped", ent->fts_path);
423 				else
424 					error(ERROR_warn(0), "%s: gid not mapped", ent->fts_path);
425 			}
426 			if (uid != ent->fts_statp->st_uid && uid != NOID || gid != ent->fts_statp->st_gid && gid != NOID)
427 			{
428 				if ((ent->fts_info & FTS_SL) && (flags & FTS_PHYSICAL) && (options & OPT_LCHOWN))
429 				{
430 					op = "lchown";
431 					chownf = lchown;
432 				}
433 				else
434 				{
435 					op = "chown";
436 					chownf = chown;
437 				}
438 				if (options & (OPT_SHOW|OPT_VERBOSE))
439 				{
440 					if (options & OPT_TEST)
441 					{
442 						ent->fts_statp->st_uid = 0;
443 						ent->fts_statp->st_gid = 0;
444 					}
445 					sfprintf(sfstdout, "%s uid:%05d->%05d gid:%05d->%05d %s\n", op, ent->fts_statp->st_uid, uid, ent->fts_statp->st_gid, gid, ent->fts_path);
446 				}
447 				if (!(options & OPT_SHOW) && (*chownf)(ent->fts_accpath, uid, gid) && !(options & OPT_FORCE))
448 					error(ERROR_system(0), "%s: cannot change%s", ent->fts_accpath, s);
449 			}
450 			break;
451 		case FTS_DC:
452 			if (!(options & OPT_FORCE))
453 				error(ERROR_warn(0), "%s: directory causes cycle", ent->fts_accpath);
454 			break;
455 		case FTS_DNR:
456 			if (!(options & OPT_FORCE))
457 				error(ERROR_system(0), "%s: cannot read directory", ent->fts_accpath);
458 			goto anyway;
459 		case FTS_DNX:
460 			if (!(options & OPT_FORCE))
461 				error(ERROR_system(0), "%s: cannot search directory", ent->fts_accpath);
462 			goto anyway;
463 		case FTS_NS:
464 			if (!(options & OPT_FORCE))
465 				error(ERROR_system(0), "%s: not found", ent->fts_accpath);
466 			break;
467 		}
468 	fts_close(fts);
469 	if (map)
470 		dtclose(map);
471 	return error_info.errors != 0;
472 }
473