-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAcl.cpp
More file actions
562 lines (441 loc) · 12.8 KB
/
Copy pathAcl.cpp
File metadata and controls
562 lines (441 loc) · 12.8 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/******************************************************************************/
/* */
/* Copyright (c) 2010, 2014 Sylwester Wysocki <sw143@wp.pl> */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining a */
/* copy of this software and associated documentation files (the "Software"), */
/* to deal in the Software without restriction, including without limitation */
/* the rights to use, copy, modify, merge, publish, distribute, sublicense, */
/* and/or sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in */
/* all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL */
/* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER */
/* DEALINGS IN THE SOFTWARE. */
/* */
/******************************************************************************/
//
// Purpose: Generic implementation of access lists.
//
#include "Secure.h"
namespace Tegenaria
{
//
// Create empty, NULL access list.
//
SecureAcl::SecureAcl()
{
this -> clear();
}
//
// Init access list from raw string.
//
// String format is: "user1:rights1;user2:rights2...*:otherRights"
//
// Example lists are:
//
// "jan:R;jozek:F;*:D" - jan can read, jozek can read and write, others have no access
// "jan:F;*:R" - jan can read and write, others can read only
// "*:F" - all can read and write
// "*:D" - nobody have access
// "" - invalid data, * terminator missing.
//
// R = read only
// F = full access
// D = access denied
//
// * = others users not included in list explicite. SHOULD be the
// last entry on the access list.
//
// Parameters:
//
// acl - string containing correct access list in format describet above (IN).
//
// RETURNS: 0 if OK,
// -1 otherwise.
//
int SecureAcl::initFromString(const char *acl)
{
DBG_ENTER3("SecureAcl::initFromString");
int exitCode = -1;
char *tmp = NULL;
char *token = NULL;
char *user = NULL;
char *rights = NULL;
char *delim = NULL;
//
// Clear current list on startup.
//
this -> clear();
//
// Check args.
//
FAILEX(acl == NULL, "ERROR: 'acl' cannot be NULL in SeucureAcl::initFromString().\n");
//
// Duplicate input string.
// We will tokenize it with destructive way.
//
tmp = strdup(acl);
//
// Tokenize input string.
// We except list in format: "user1:rights1;user2:rights2;..."
//
token = strtok(tmp, ";");
while(token)
{
//
// One element is user:rights.
// Split into user and message part.
//
delim = strchr(token, ':');
FAILEX(delim == NULL, "ERROR: Missing ':' delimer in ACL string.\n");
*delim = 0;
user = token;
rights = delim + 1;
//
// Put to rights into user |-> rights map.
//
DEBUG3("SecureAcl::initFromString : Granting rights [%s] to [%s].\n", rights, user);
rights_[user] = encodeRights(rights);
//
// Go to next entry on list.
//
token = strtok(NULL, ";");
}
//
// Error handler.
//
exitCode = 0;
fail:
if (exitCode)
{
Error("ERROR: Cannot init access list object from '%s' string.\n", acl);
this -> clear();
}
if (tmp)
{
free(tmp);
}
DBG_LEAVE3("SecureAcl::initFromString");
return exitCode;
}
//
// Set user rights to given value.
//
// WARNING#1: If user has already some rights granted function will OVERWRITE
// them with new one.
//
// TIP#1: To read back rights granted to user use getRights() method.
//
// user - username, which we want grant rights to (IN).
//
// rights - rights, which we want to grant in binary representation i.e.
// combination of SECURE_ACL_XXX flags defined in Secure.h (IN).
//
// RETURNS: 0 if OK.
//
int SecureAcl::setRights(const char *user, int rights)
{
DBG_ENTER3("SecureAcl::setRights");
int exitCode = -1;
//
// Check args.
//
FAILEX(user == NULL, "ERROR: 'user' cannot be NULL in SecureAcl::setRights().\n");
//
// Grant rights to user.
//
rights_[user] = rights;
//
// Error handler.
//
exitCode = 0;
fail:
DBG_LEAVE3("SecureAcl::setRights");
return exitCode;
}
//
// Set user rights to given value.
//
// WARNING#1: If user has already some rights granted function will OVERWRITE
// them with new one.
//
// TIP#1: To read back rights granted to user use getRights() method.
//
// user - username, which we want grant rights to (IN).
//
// rights - rights, which we want to grant as human readable string i.e.
// combination of SECURE_ACL_SYMBOL_XXX chars defined in Secure.h.
// Example string is "D" (deny) or "F" (Full access) (IN).
//
// RETURNS: 0 if OK.
//
int SecureAcl::setRights(const char *user, const char *rights)
{
return this -> setRights(user, encodeRights(rights));
}
//
// Set rights granted to others users to given value. Others means all users
// not mentioned in ACL explicite.
//
// WARNING#1: If others has already some rights granted function will OVERWRITE
// them with new one.
//
// rights - rights, which we want to grant in binary representation i.e.
// combination of SECURE_ACL_XXX flags defined in Secure.h (IN).
//
// RETURNS: 0 if OK.
//
int SecureAcl::setOthersRights(int rights)
{
return this -> setRights("*", rights);
}
//
// Set rights granted to others users to given value. Others means all users
// not mentioned in ACL explicite.
//
// WARNING#1: If others has already some rights granted function will OVERWRITE
// them with new one.
//
// rights - rights, which we want to grant as human readable string i.e.
// combination of SECURE_ACL_SYMBOL_XXX chars defined in Secure.h.
// Example string is "D" (deny) or "F" (Full access) (IN).
//
// RETURNS: 0 if OK.
//
int SecureAcl::setOthersRights(const char *rights)
{
return this -> setRights("*", encodeRights(rights));
}
//
// Remove user from accesslist. After that user has no any rights granted.
//
// user - user, which we want revoke rights for (IN).
//
// RETURNS: 0 if OK.
//
int SecureAcl::revokeAll(const char *user)
{
DBG_ENTER3("SecureAcl::revokeAll");
int exitCode = -1;
//
// Check args.
//
FAILEX(user == NULL, "ERROR: 'user' cannot be NULL in SecureAcl::revokeAll().\n");
//
// Remove user from righrs map.
//
rights_.erase(user);
//
// Error handler.
//
exitCode = 0;
fail:
DBG_LEAVE3("SecureAcl::removeAll");
return exitCode;
}
//
// Gather rights for given users.
//
// user - name of user, which we want rights for (IN).
//
// RETURNS: Rights granted to given user.
//
int SecureAcl::getRights(const char *user)
{
DBG_ENTER3("SecureAcl::getRights");
int ret = SECURE_ACL_DENY;
map<string, int>::iterator it;
//
// Check args.
//
FAILEX(user == NULL, "ERROR: 'User' cannot be NULL in SecureAcl::getRights().\n");
//
// Try find user in rights map.
//
it = rights_.find(user);
//
// User exists in map, get rights from map.
//
if (it != rights_.end())
{
ret = it -> second;
}
//
// User does not exist in map, get rights for others user (*).
//
else
{
ret = rights_["*"];
}
//
// Error handler.
//
fail:
DBG_LEAVE3("SecureAcl::getRights");
return ret;
}
//
// Gather rights for given users.
//
// user - name of user, which we want rights for (IN).
//
// RETURNS: Rights granted to given user as human readable string.
//
string SecureAcl::getRightsString(const char *user)
{
return decodeRights(this -> getRights(user));
}
//
// Convert access list stored inside object into ACL string.
//
string SecureAcl::toString()
{
DBG_ENTER3("SecureAcl::toString");
string ret;
int othersRights;
map<string, int>::iterator it;
//
// Iterate over whole [user] |-> [rights] map.
//
for (it = rights_.begin(); it != rights_.end(); it++)
{
//
// Others rights. Save for future.
// We want put it as last entry in string.
//
if (it -> first == "*")
{
othersRights = it -> second;
}
//
// Ordinary user, put to string.
//
else
{
ret += it -> first;
ret += ':';
ret += decodeRights(it -> second);
ret += ';';
}
}
//
// Add others rights as * keyword.
//
ret += "*:";
ret += decodeRights(othersRights);
ret += ';';
DBG_LEAVE3("SecureAcl::toString");
return ret;
}
//
// Revoke all grant from all users stored accesslist.
//
void SecureAcl::clear()
{
DBG_ENTER3("SecureAcl::clear");
rights_.clear();
rights_["*"] = SECURE_ACL_DENY;
DBG_LEAVE3("SecureAcl::clear");
}
//
// Encode rights string into its binary representation.
// Rights in binary representation is combination of SECURE_ACL_XXX flags
// defined in Secure.h file.
//
// WARNING#1: If input rights string is incorrect function assign SECURE_ACL_DENY
// rights value.
//
// rights - human readable string representing rights to encode, for list of
// allowed chars see SECURE_ACL_SYMBOL_XXX defines in Secure.h (IN).
//
// RETURNS: Binary representation of rights.
//
int SecureAcl::encodeRights(const char *str)
{
DBG_ENTER3("SecureAcl::encodeRights");
int rights = 0;
//
// Translate rights from string into rights codes.
//
if (str)
{
for (int i = 0; str[i]; i++)
{
switch(str[i])
{
case SECURE_ACL_SYMBOL_DENY : rights |= SECURE_ACL_DENY; break;
case SECURE_ACL_SYMBOL_FULL : rights |= SECURE_ACL_FULL; break;
case SECURE_ACL_SYMBOL_READ : rights |= SECURE_ACL_READ; break;
case SECURE_ACL_SYMBOL_WRITE : rights |= SECURE_ACL_WRITE; break;
case SECURE_ACL_SYMBOL_ERASE : rights |= SECURE_ACL_ERASE; break;
}
}
}
//
// Normalize rights.
//
if ((rights == 0) || (rights & SECURE_ACL_DENY))
{
rights = SECURE_ACL_DENY;
}
else if (rights & SECURE_ACL_FULL)
{
rights = SECURE_ACL_FULL;
}
DBG_LEAVE3("SecureAcl::encodeRights");
return rights;
}
//
// Decode rights value into string.
// String contains chars representing single rights eg. "RW" means READ+WRITE.
// Full list of allowed rights chars is defined at SECURE_ACL_SYMBOL_XXX defines
// in Secure.h file
//
// WARNING#1: If input rights value is incorrect function assign "D" (access denied)
// rights.
//
// rights - binary encoded rights i.e. combination of SECURE_ACL_XXX flags
// defined in Secure.h (IN).
//
// RETURNS: Human readable string representing rights.
//
string SecureAcl::decodeRights(int rights)
{
DBG_ENTER3("SecureAcl::decodeRights");
string ret;
//
// Deny.
//
if ((rights == 0) || (rights & SECURE_ACL_DENY))
{
ret = SECURE_ACL_SYMBOL_DENY;
}
//
// Full.
//
else if (rights & SECURE_ACL_FULL)
{
ret = SECURE_ACL_SYMBOL_FULL;
}
//
// Complex list.
// Create composition from single rights.
//
else
{
if (rights & SECURE_ACL_READ) ret += SECURE_ACL_SYMBOL_READ;
if (rights & SECURE_ACL_WRITE) ret += SECURE_ACL_SYMBOL_WRITE;
if (rights & SECURE_ACL_ERASE) ret += SECURE_ACL_SYMBOL_ERASE;
}
DBG_LEAVE3("SecureAcl::decodeRights");
return ret;
}
} /* namespace Tegenaria */