-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextent_server.cc
344 lines (303 loc) · 9.42 KB
/
extent_server.cc
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// the extent server implementation
#include "extent_server.h"
#include <sstream>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
extent_server::extent_server()
{
pthread_mutex_init(&mutex, NULL);
// put in root directory
extent_protocol::extentid_t root = 0x00000001;
contents[root] = "";
extent_protocol::attr a;
time_t cur = time(NULL);
a.atime = cur;
a.ctime = cur;
a.mtime = cur;
a.size = 0;
a.mode = 0777;
a.uid = getuid();
a.gid = getgid();
attrs[root] = a;
}
int extent_server::put(extent_protocol::extentid_t id, std::string buf,
extent_protocol::userid_t userid, std::string userkey, int &)
{
ScopedLock l(&mutex);
// create a new attr entry for this extentid
// if it doesnt exist
time_t cur = time(NULL);
extent_protocol::attr a;
if (attrs.count(id) == 0) {
a.atime = cur;
// for a new record, we also have to initialize its permissions, otherwise
// the next setattr call will not work. The setattr call is assumed to
// modify this to the right value immediately
a.mode = 0777;
} else {
a = attrs[id];
}
a.ctime = cur;
a.mtime = cur;
a.size = buf.size();
// check permission
if (!has_write_perm(id, userid, userkey)) {
return extent_protocol::NOACCESS;
}
attrs[id] = a;
// saves buf under extentid as its key
contents[id] = buf;
return extent_protocol::OK;
}
int extent_server::get(extent_protocol::extentid_t id,
extent_protocol::userid_t userid, std::string userkey, std::string &buf)
{
ScopedLock l(&mutex);
if (!has_read_perm(id, userid, userkey)) {
return extent_protocol::NOACCESS;
}
// get content corresponding to extentid if it exists
// return NOENT otherwise
if (contents.count(id) == 0) {
return extent_protocol::NOENT;
} else {
attrs[id].atime = time(NULL);
buf = contents[id];
return extent_protocol::OK;
}
}
int extent_server::getattr(extent_protocol::extentid_t id,
extent_protocol::userid_t userid, std::string userkey, extent_protocol::attr &a)
{
ScopedLock l(&mutex);
// get attribute correponding to extentid if it exists
// return NOENT otherwise
if (contents.count(id) == 0) {
a.size = 0;
a.atime = 0;
a.mtime = 0;
a.ctime = 0;
a.mode = 0777;
a.uid = 0;
a.gid = 0;
return extent_protocol::NOENT;
} else {
extent_protocol::attr attr = attrs[id];
a.size = attr.size;
a.atime = attr.atime;
a.mtime = attr.mtime;
a.ctime = attr.ctime;
a.mode = attr.mode;
a.uid = attr.uid;
a.gid = attr.gid;
return extent_protocol::OK;
}
}
int extent_server::setattr(extent_protocol::extentid_t id, extent_protocol::attr a,
extent_protocol::userid_t userid, std::string userkey, int &)
{
ScopedLock l(&mutex);
if (!has_write_perm(id, userid, userkey)) {
return extent_protocol::NOACCESS;
}
// set mode/uid/gid correponding to extentid if it exists
// return NOENT otherwise
if (contents.count(id) == 0) {
return extent_protocol::NOENT;
} else {
extent_protocol::attr attr = attrs[id];
attr.mode = a.mode;
attr.uid = a.uid;
attr.gid = a.gid;
attrs[id] = attr;
return extent_protocol::OK;
}
}
int extent_server::remove(extent_protocol::extentid_t id,
extent_protocol::userid_t userid, std::string userkey, int &)
{
ScopedLock l(&mutex);
if (!has_write_perm(id, userid, userkey)) {
return extent_protocol::NOACCESS;
}
// remove extentid entry if it exists
// return NOENT otherwise
if (contents.count(id) == 0) {
return extent_protocol::NOENT;
} else {
attrs.erase(id);
contents.erase(id);
return extent_protocol::OK;
}
}
/*
* called by extent_client upon construction to make sure that the userid
* userkey association will be made in case it's the first time we are seeing
* this user
*/
int extent_server::reg(extent_protocol::userid_t userid, std::string userkey, int &)
{
//first time userid is seen by extent server
if (userkeys.count(userid) == 0) {
userkeys[userid] = userkey;
}
else{
if(!authenticate(userid, userkey)){
return extent_protocol::NOACCESS;
}
}
return extent_protocol::OK;
}
//inserts groupid into groupusers map, creates an empty list of users
//Note: this will overwrite preexisting groups with the same gid
int extent_server::groupadd(extent_protocol::groupid_t gid, extent_protocol::userid_t admin, std::string adminkey, int &)
{
printf("adding group : %u \n", gid);
//check if admin is actually admin
if(!isadmin(admin) || !authenticate(admin, adminkey)){
printf("admin problems - cannot add group %u\n", gid);
return extent_protocol::NOACCESS;
}
std::set<extent_protocol::userid_t> temp = groupusers[gid];
return extent_protocol::OK;
}
//deletes groupid from groupusers map
//Note: this will overwrite preexisting groups with the same gid
int extent_server::groupdel(extent_protocol::groupid_t gid, extent_protocol::userid_t admin, std::string adminkey, int &)
{
printf("deleting group : %u \n", gid);
//check if admin is actually admin
if(!isadmin(admin) || !authenticate(admin, adminkey)){
printf("admin problems - cannot delete group %u\n", gid);
return extent_protocol::NOACCESS;
}
//group does not exist
if(groupusers.find(gid) == groupusers.end()){
printf("cannot delete group %u because it doesn't exist\n", gid);
return extent_protocol::NOENT;
}
//group exists -> erase from map
groupusers.erase(gid);
return extent_protocol::OK;
}
//adds userid to groupid
int extent_server::useradd(extent_protocol::userid_t userid, extent_protocol::groupid_t gid, extent_protocol::userid_t admin, std::string adminkey, int &)
{
printf("adding user %u to group %u \n", userid, gid);
if (!isadmin(admin) || !authenticate(admin, adminkey)) {
printf("admin problems - cannot add user %u\n", userid);
return extent_protocol::NOACCESS;
}
groupusers[gid].insert(userid);
return extent_protocol::OK;
}
//deletes userid from groupid
int extent_server::userdel(extent_protocol::userid_t userid, extent_protocol::groupid_t gid, extent_protocol::userid_t admin, std::string adminkey, int &)
{
printf("deleting user %u from group %u \n", userid, gid);
if (!isadmin(admin) || !authenticate(admin, adminkey)) {
printf("admin problems - cannot delete user %u\n", userid);
return extent_protocol::OK;
}
//group does not exist
if (groupusers.find(gid) == groupusers.end()) {
printf("cannot delete user %u from group %u because group doesn't exist\n", userid, gid);
return extent_protocol::NOENT;
}
std::set<extent_protocol::userid_t> users = groupusers[gid];
//user does not exist
if(users.find(userid) == users.end()){
printf("cannot delete user %u from group %u because user doesn't exist\n", userid, gid);
return extent_protocol::NOENT;
}
//user does exist -> remove user
users.erase(userid);
groupusers[gid] = users;
return extent_protocol::OK;
}
//NOTE: the following permissions predicates all called with lock held
//check if user has permission to read extent
bool extent_server::has_read_perm(extent_protocol::extentid_t id,
extent_protocol::userid_t userid, std::string userkey)
{
if(!authenticate(userid, userkey)){
printf("no read permission - wrong password\n");
return false;
}
// if we have no record of it yet, just return true
if (attrs.count(id) == 0) {
return true;
}
extent_protocol::attr a = attrs[id];
return (a.mode&0004) || (a.uid==userid && (a.mode&0400)) ||
(ingroup(userid, a.gid) && (a.mode&0040));
}
//check if user has permission to write extent
bool extent_server::has_write_perm(extent_protocol::extentid_t id,
extent_protocol::userid_t userid, std::string userkey)
{
if(!authenticate(userid, userkey)){
printf("no write permission - wrong password\n");
return false;
}
// if we have no record of it yet, just return true
if (attrs.count(id) == 0) {
return true;
}
extent_protocol::attr a = attrs[id];
return (a.mode&0002) || (a.uid==userid && (a.mode&0200)) ||
(ingroup(userid, a.gid) && (a.mode&0020));
}
//check if user has permission to execute extent
bool extent_server::has_execute_perm(extent_protocol::extentid_t id,
extent_protocol::userid_t userid, std::string userkey)
{
if(!authenticate(userid, userkey)){
printf("no read permission - wrong password\n");
return false;
}
// if we have no record of it yet, just return true
if (attrs.count(id) == 0) {
return true;
}
extent_protocol::attr a = attrs[id];
return (a.mode&0001) || (a.uid==userid && (a.mode&0100)) ||
(ingroup(userid, a.gid) && (a.mode&0010));
}
bool extent_server::ingroup(extent_protocol::userid_t userid,
extent_protocol::groupid_t groupid)
{
std::set<extent_protocol::userid_t> users = groupusers[groupid];
if(users.find(userid) == users.end()){
return false;
}
return true;
}
//checks if user is admin user
//assumes lock is held
bool extent_server::isadmin(extent_protocol::userid_t userid)
{
return true;
//userid NOT admin
// if(groupusers[1].find(userid) == groupusers[1].end()){
// printf("user %u is not admin. ", userid);
// return false;
// }
// return true;
}
//check if correct userkey (password) is provided for userid
//assumes lock is held
bool extent_server::authenticate(extent_protocol::userid_t userid, std::string userkey)
{
//return true;
if(userkeys[userid] == userkey){
return true;
}
else{
printf("wrong key provided for user %u\n", userid);
return false;
}
}