forked from dzhu/keytap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpermissions.c
147 lines (132 loc) · 3.68 KB
/
permissions.c
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <limits.h>
#include <errno.h>
#include <unistd.h>
// user must be null-terminated, but it can be a uid or a username
static int get_uid_from_string(char *user, uid_t *out){
// check if user is a UID already
char *endptr = user;
long int luid = strtol(user, &endptr, 0);
// was it a valid number?
if(user[0] != '\0' && endptr[0] == '\0'){
// user was a UID
if(luid > UINT_MAX){
fprintf(stderr, "UID %ld too big\n", luid);
return -1;
}
if(luid < 0){
fprintf(stderr, "UID %ld cannot be negative\n", luid);
return -1;
}
*out = (uid_t)luid;
return 0;
}
// otherwise, assume user is a name string
struct passwd pwd;
struct passwd *pwd_result;
char namebuf[1024];
errno = 0;
int ret = getpwnam_r(user, &pwd, namebuf, sizeof(namebuf), &pwd_result);
// check for error
if(ret != 0){
perror("getpwnam_r");
return -1;
}
// check for name not found
if(pwd_result == NULL){
fprintf(stderr, "user %s not found\n", user);
return -1;
}
*out = pwd_result->pw_uid;
return 0;
}
// group must be null-terminated, but it can be a gid or a username
static int get_gid_from_string(char *group, gid_t *out){
// check if group is a GID already
char *endptr = group;
long int lgid = strtol(group, &endptr, 0);
// was it a valid number?
if(group[0] != '\0' && endptr[0] == '\0'){
// group was a GID
if(lgid > UINT_MAX){
fprintf(stderr, "GID %ld too big\n", lgid);
return -1;
}
if(lgid < 0){
fprintf(stderr, "GID %ld cannot be negative\n", lgid);
return -1;
}
*out = (gid_t)lgid;
return 0;
}
// otherwise, assume group is a name string
struct group grp;
struct group *grp_result;
char namebuf[1024];
errno = 0;
int ret = getgrnam_r(group, &grp, namebuf, sizeof(namebuf), &grp_result);
// check for error
if(ret != 0){
perror("getgrnam_r");
return -1;
}
// check for name not found
if(grp_result == NULL){
fprintf(stderr, "group %s not found\n", group);
return -1;
}
*out = grp_result->gr_gid;
return 0;
}
int set_file_perms(char *file, char *user, char *group, char *mode){
int ret;
if(user != NULL || group != NULL){
if(user == NULL || group == NULL){
fprintf(stderr, "user and group must both be NULL or non-NULL\n");
return -1;
}
uid_t uid;
ret = get_uid_from_string(user, &uid);
if(ret != 0){
return -1;
}
gid_t gid;
ret = get_gid_from_string(group, &gid);
if(ret != 0){
return -1;
}
ret = chown(file, uid, gid);
if(ret != 0){
perror(file);
return -1;
}
}
if(mode != NULL){
// parse the string as octal
char *endptr = mode;
long int lmode = strtol(mode, &endptr, 8);
if(mode[0] == '\0' || endptr[0] != '\0'){
fprintf(stderr, "invalid permissions: %s\n", mode);
return -1;
}
if(lmode > UINT_MAX){
fprintf(stderr, "mode %ld too big\n", lmode);
return -1;
}
if(lmode < 0){
fprintf(stderr, "mode %ld cannot be negative\n", lmode);
return -1;
}
ret = chmod(file, (mode_t)lmode);
if(ret != 0){
perror(file);
return -1;
}
}
return 0;
}