permission parsing
Stefan Schuermans

Stefan Schuermans commited on 2020-08-18 20:17:46
Showing 2 changed files, with 77 additions and 3 deletions.

... ...
@@ -3,7 +3,68 @@
3 3
 #include <string>
4 4
 
5 5
 void Permissions::parseParams(std::string const &paramStr) {
6
-  for (char c : paramStr) {
7
-    (void)c;
6
+  // reset permission flags
7
+  set = 0;
8
+  setCond = 0;
9
+  clear = 0;
10
+  clearCond = 0;
11
+  // parse comma-separated groups of [ugoa][+-][rwxX] settings
12
+  Flags who = 0, what = 0, whatCond = 0;
13
+  char action = ' ';
14
+  // append comma at end to process last group
15
+  for (char c : paramStr + ",") {
16
+    switch (c) {
17
+    case ',':
18
+      switch (action) {
19
+        case '=':
20
+          set = who * what;
21
+          setCond = who * whatCond;
22
+          clear = who * (what ^ (flagRead | flagWrite | flagExecute));
23
+          clearCond = who * (what ^ flagExecute);
24
+          break;
25
+        case '+':
26
+          set = who * what;
27
+          setCond = who * whatCond;
28
+          break;
29
+        case '-':
30
+          clear = who * what;
31
+          clearCond = who * whatCond;
32
+          break;
33
+      }
34
+      who = 0;
35
+      what = 0;
36
+      whatCond = 0;
37
+      action = ' ';
38
+      break;
39
+    case 'u':
40
+      who |= flagUser;
41
+      break;
42
+    case 'g':
43
+      who |= flagGroup;
44
+      break;
45
+    case 'o':
46
+      who |= flagOther;
47
+      break;
48
+    case 'a':
49
+      who |= flagUser | flagGroup | flagOther;
50
+      break;
51
+    case '=':
52
+    case '+':
53
+    case '-':
54
+      action = c;
55
+      break;
56
+    case 'r':
57
+      what |= flagRead;
58
+      break;
59
+    case 'w':
60
+      what |= flagWrite;
61
+      break;
62
+    case 'x':
63
+      what |= flagExecute;
64
+      break;
65
+    case 'X':
66
+      whatCond |= flagExecute;
67
+      break;
68
+    }
8 69
   }
9 70
 }
... ...
@@ -1,11 +1,21 @@
1 1
 #ifndef PERMISSIONS_H
2 2
 #define PERMISSIONS_H
3 3
 
4
+#include <cstdint>
4 5
 #include <string>
5 6
 
6 7
 /// permissions configuration
7 8
 class Permissions {
8 9
 public:
10
+  typedef uint64_t Flags;
11
+
12
+  static const Flags flagRead = 01;
13
+  static const Flags flagWrite = 02;
14
+  static const Flags flagExecute = 04;
15
+  static const Flags flagOther = 01;
16
+  static const Flags flagGroup = 010;
17
+  static const Flags flagUser = 0100;
18
+
9 19
   /**
10 20
    * @brief parse permissions parameters
11 21
    * @param[in] parmStr parameter string
... ...
@@ -14,7 +24,10 @@ public:
14 24
   void parseParams(std::string const &paramStr);
15 25
 
16 26
 protected:
17
-  
27
+  Flags set = 0;       ///< permissions to set
28
+  Flags setCond = 0;   ///< permissions to set conditionally on usrexec/dir
29
+  Flags clear = 0;     ///< permissions to clear
30
+  Flags clearCond = 0; ///< permissions to clear conditionally on usrexec/dir
18 31
 };
19 32
 
20 33
 #endif // #ifndef PERMISSIONS_H
21 34