-
Notifications
You must be signed in to change notification settings - Fork 0
/
Object.cpp
216 lines (195 loc) · 5.2 KB
/
Object.cpp
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
/*
* Object.cpp
*
* Created on: Sep 17, 2013
* Author: David Tagatac
*/
#include "Object.h"
#include "common.h"
#include <string>
#include <iostream>
#include <sstream>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/regex.hpp>
#include <boost/lexical_cast.hpp>
using namespace std;
namespace fs = boost::filesystem;
Object::Object(string o, string f)
{
owner = o;
filename = f;
objpath = fs::path(DATA_DIR);
objpath /= owner;
objpath /= filename;
// Create a pointer to the ACL (but only if this Object is not an ACL)
if (owner != ACL_MANAGER && owner != METADATA_MANAGER)
{
ACL = new Object(ACL_MANAGER, owner + OWNER_DELIMITER + filename);
metadata = new Object(METADATA_MANAGER,
owner + OWNER_DELIMITER + filename);
}
}
bool Object::exists()
{
uid_t ruid, euid, suid;
getresuid(&ruid, &euid, &suid);
setuid(suid);
return fs::exists(objpath);
setuid(ruid);
}
int Object::put(string contents)
{
// If the file doesn't yet exist, create a default ACL for it
if (owner != ACL_MANAGER && owner != METADATA_MANAGER && !exists() &&
!ACL->exists())
{
ACL->put(owner + ".*" + GROUP_DELIMITER + DEFAULT_PERMISSIONS + '\n');
}
// Create the directory tree down to the user's directory
fs::path objdir(DATA_DIR);
objdir /= owner;
uid_t ruid, euid, suid;
getresuid(&ruid, &euid, &suid);
setuid(suid);
fs::create_directories(objdir);
// Transfer the data from the passed string to the file
fs::ofstream objectstream(objpath);
objectstream.write(contents.c_str(), sizeof(char) * contents.size());
objectstream.close();
setuid(ruid);
return 0;
}
int Object::get(string &contents)
{
// Open the file
uid_t ruid, euid, suid;
getresuid(&ruid, &euid, &suid);
setuid(suid);
fs::ifstream objectstream(objpath);
if (!objectstream)
{
cerr << "Invalid file: " << objpath << endl;
return 1;
}
// Write the contents of the file to stdout
istreambuf_iterator<char> eos;
// Put the stream into a string (from http://stackoverflow.com/questions/3203452/how-to-read-entire-stream-into-a-stdstring)
contents = string(istreambuf_iterator<char>(objectstream), eos);
setuid(ruid);
return 0;
}
int Object::setACL(string contents)
{
string aclline;
istringstream ss(contents);
static const boost::regex permissionsExpr("r?w?x?p?v?");
bool aclvalidity = true;
while (getline(ss, aclline))
{
// First validate the user.
size_t cursor1 = aclline.find(USER_DELIMITER);
if (cursor1 == string::npos)
{
aclvalidity = false;
break;
}
string user = aclline.substr(0, cursor1);
if (user != "*" && !getpwnam(user.c_str()))
{
aclvalidity = false;
break;
}
// If the user is valid, validate the group.
size_t cursor2 = aclline.find(GROUP_DELIMITER);
if (cursor2 < cursor1 || cursor2 == string::npos)
{
aclvalidity = false;
break;
}
// If the user and the group match, use these permissions.
string permissions = aclline.substr(cursor2 + 1);
if (!regex_match(permissions, permissionsExpr))
{
aclvalidity = false;
break;
}
}
if (!aclvalidity)
{
cerr << "Invalid ACL format" << endl;
cerr << "Valid format is one or more lines of '<username>|*.<groupname>|*\\t[r][w][x][p][v]\\n'" << endl;
return 1;
}
else
return ACL->put(contents);
}
int Object::getACL(string &contents)
{
return ACL->get(contents);
}
bool Object::testACL(char access)
{
string username(getpwuid(getuid())->pw_name);
string groupname(getgrgid(getgid())->gr_name);
if (!ACL->exists())
{
if (username == owner) return true;
else return false;
}
string permissions = ""; // Give no permissions by default.
string contents, aclline;
ACL->get(contents);
istringstream ss(contents);
// Parse each line of the ACL until a match is found.
while (getline(ss, aclline))
{
// First match the user.
int cursor1 = aclline.find(USER_DELIMITER);
string user = aclline.substr(0, cursor1);
if (user == "*" || user == username)
{
// If the user matches, match the group.
int cursor2 = aclline.find(GROUP_DELIMITER);
string group = aclline.substr(cursor1 + 1, cursor2 - (cursor1 + 1));
if (group == "*" || group == groupname)
{
// If the user and the group match, use these permissions.
permissions = aclline.substr(cursor2 + 1);
break;
}
}
}
// If the desired access character is in the permissions string, return true
return (permissions.find(access) != string::npos);
}
string Object::getIV()
{
string metaContents, iv;
metadata->get(metaContents);
size_t cursor = metaContents.find(META_DELIMITER);
iv = metaContents.substr(0, cursor);
return iv;
}
string Object::getKey()
{
string metaContents, key;
metadata->get(metaContents);
size_t cursor1 = metaContents.find(META_DELIMITER);
size_t cursor2 = metaContents.find(META_DELIMITER, cursor1 + 1);
key = metaContents.substr(cursor1 + 1, cursor2 - (cursor1 + 1));
return key;
}
string Object::getSize()
{
string metaContents, size;
metadata->get(metaContents);
size_t cursor1 = metaContents.find(META_DELIMITER);
cursor1 = metaContents.find(META_DELIMITER, cursor1 + 1);
size_t cursor2 = metaContents.find(META_DELIMITER, cursor1 + 1);
size = metaContents.substr(cursor1 + 1, cursor2 - (cursor1 + 1));
return size;
}