Repository navigation
Expand file tree
/
Copy pathchown.c
More file actions
180 lines (158 loc) · 3.38 KB
/
Copy pathchown.c
File metadata and controls
180 lines (158 loc) · 3.38 KB
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
/* chown/chgrp - Change file ownership Author: V. Archer */
/* Copyright 1991 by Vincent Archer
* You may freely redistribute this software, in source or binary
* form, provided that you do not alter this copyright mention in any
* way.
8000
div> */
/* Changed 3 Feb 93 by Kees J. Bot: setuid execution nonsense removed.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <minix/minlib.h>
#include <stdio.h>
#ifndef S_ISLNK
#define S_ISLNK(mode) 0
#define lstat stat
#endif
#define S_IUGID (S_ISUID|S_ISGID)
/* Global variables, such as flags and path names */
int gflag, oflag, rflag, error, hflag = 0;
char *pgmname, path[PATH_MAX + 1];
uid_t nuid;
gid_t ngid;
_PROTOTYPE(int main, (int argc, char **argv));
_PROTOTYPE(void do_chown, (char *file));
_PROTOTYPE(void usage, (void));
/* Main module. If chown(1) is invoked as chgrp(1), the behaviour is nearly
* identical, except that the default when a single name is given as an
* argument is to take a group id rather than an user id. This allow the
* non-Posix "chgrp user:group file".
*/
int main(argc, argv)
int argc;
char *argv[];
{
char *id, *id2;
struct group *grp;
struct passwd *pwp;
int ch;
if (pgmname = strrchr(*argv, '/'))
pgmname++;
else
pgmname = *argv;
gflag = strcmp(pgmname, "chgrp");
while((ch = getopt(argc, argv, "Rh")) != -1) {
switch(ch) {
case 'R':
rflag = 1;
break;
case 'h':
hflag = 1;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc < 2) usage();
id = *argv++;
argc--;
if (id2 = strchr(id, ':')) *id2++ = '\0';
if (!id2 && !gflag) {
id2 = id;
id = 0;
}
if (id) {
if (isdigit(*id))
nuid = atoi(id);
else {
if (!(pwp = getpwnam(id))) {
std_err(id);
std_err(": unknown user name\n");
exit(1);
}
nuid = pwp->pw_uid;
}
oflag = 1;
} else
oflag = 0;
if (id2) {
if (isdigit(*id2))
ngid = atoi(id2);
else {
if (!(grp = getgrnam(id2))) {
std_err(id2);
std_err(": unknown group name\n");
exit(1);
}
ngid = grp->gr_gid;
}
gflag = 1;
} else
gflag = 0;
error = 0;
while (argc--) do_chown(*argv++);
return(error);
}
/* Apply the user/group modification here.
*/
void do_chown(file)
char *file;
{
DIR *dirp;
struct dirent *entp;
char *namp;
struct stat st;
if (lstat(file, &st)) {
perror(file);
error = 1;
return;
}
if (S_ISLNK(st.st_mode) && rflag) return; /* Note: violates POSIX. */
if (S_ISLNK(st.st_mode) && hflag) {
fprintf(stderr, "chown: cannot lchown %s\n", file);
error = 1;
}
if (chown(file, oflag ? nuid : st.st_uid, gflag ? ngid : st.st_gid)) {
perror(file);
error = 1;
}
if (S_ISDIR(st.st_mode) && rflag) {
if (!(dirp = opendir(file))) {
perror(file);
error = 1;
return;
}
if (path != file) strcpy(path, file);
namp = path + strlen(path);
*namp++ = '/';
while (entp = readdir(dirp))
if (entp->d_name[0] != '.' ||
(entp->d_name[1] &&
(entp->d_name[1] != '.' || entp->d_name[2]))) {
strcpy(namp, entp->d_name);
do_chown(path);
}
closedir(dirp);
*--namp = '\0';
}
}
/* Posix prototype of the chown/chgrp function */
void usage()
{
std_err("Usage: ");
std_err(pgmname);
std_err(gflag ? " owner[:group]" : " [owner:]group");
std_err(" file...\n");
exit(1);
}