replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / tool / svrdbeditor_src / svrdbeditor.c
1 /* *****************************************************************
2  *
3  * Copyright 2017 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * *****************************************************************/
20
21 #include <string.h>
22
23 #include "octypes.h"
24 #include "ocstack.h"
25 #include "psinterface.h"
26
27 #include "credresource.h"
28
29 #include "svrdbeditorcommon.h"
30 #include "svrdbeditoracl.h"
31 #include "svrdbeditorcred.h"
32 #include "svrdbeditordoxm.h"
33 #include "svrdbeditorpstat.h"
34
35 static bool g_allowedEditMenu[SVR_EDIT_IDX_SIZE] = {false/*unused*/, false, false, false, false};
36 static char g_svrDbPath[SVR_DB_PATH_LENGTH];
37
38 typedef enum OperationType
39 {
40     SVR_PRINT_ALL = 1,
41     SVR_EDIT_CRED,
42     SVR_EDIT_ACL,
43     SVR_EDIT_DOXM,
44     SVR_EDIT_PSTAT = 5,
45     EXIT = 99
46 } OperationType_t;
47
48 static int MainOperation(const char *svrpath);
49 static void PrintMainMenu(void);
50 static void PrintEditMenu(const char *resourceName, bool print, bool add, bool remove,
51                           bool modify);
52 static void PrintHelp(void);
53 static FILE *SVRDBFopen(const char *path, const char *mode);
54
55 int main(int argc, char *argv[])
56 {
57     if (2 == argc)
58     {
59         PRINT_NORMAL("SVR DB File Path: %s\n", argv[1]);
60         return MainOperation(argv[1]);
61     }
62     else
63     {
64         PrintHelp();
65         return 0;
66     }
67 }
68
69 static int MainOperation(const char *svrpath)
70 {
71     OperationType_t menu = EXIT;
72     SubOperationType_t editMenu = EXIT;
73     OCStackResult ocResult = OC_STACK_ERROR;
74     bool run = true;
75
76     // initialize persistent storage for SVR DB
77     static OCPersistentStorage psInst =
78     {
79         .open = SVRDBFopen,
80         .read = fread,
81         .write = fwrite,
82         .close = fclose,
83         .unlink = unlink
84     };
85
86     if (!svrpath)
87     {
88         PRINT_ERR("Incorrect file path");
89         return -1;
90     }
91
92     strncpy(g_svrDbPath, svrpath, sizeof(g_svrDbPath) - 1);
93     g_svrDbPath[sizeof(g_svrDbPath) - 1] = '\0';
94
95     ocResult = InitPersistentStorageInterface();
96     if (OC_STACK_OK != ocResult)
97     {
98         PRINT_ERR("InitPersistentStorageInterface error : %d", ocResult);
99         return -1;
100     }
101
102     ocResult = OCRegisterPersistentStorageHandler(&psInst);
103     if (OC_STACK_OK != ocResult)
104     {
105         PRINT_ERR("OCRegisterPersistentStorageHandler : %d", ocResult);
106         return -1;
107     }
108     RefreshACL();
109     RefreshCred();
110     RefreshDoxm();
111     RefreshPstat();
112     while (run)
113     {
114         PrintMainMenu();
115         menu = (OperationType_t)InputNumber("\tSelect the menu : ");
116         switch (menu)
117         {
118             case SVR_PRINT_ALL:
119                 PrintDoxm();
120                 PrintPstat();
121                 PrintAcl();
122                 PrintCredList(GetCredList());
123                 break;
124             case SVR_EDIT_CRED:
125                 for (;;)
126                 {
127                     if (NULL == GetCredList())
128                     {
129                         PRINT_WARN("Credential resource is empty.");
130                         PrintEditMenu("Credential Resource", false, true, false, false);
131                     }
132                     else
133                     {
134                         PrintEditMenu("Credential Resource", true, true, true, true);
135                     }
136                     editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
137                     if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
138                     {
139                         if (!g_allowedEditMenu[editMenu])
140                         {
141                             PRINT_ERR("Disabled menu");
142                             continue;
143                         }
144                     }
145                     else if (BACK == editMenu)
146                     {
147                         PRINT_INFO("Back to the previous menu.");
148                         break;
149                     }
150                     else
151                     {
152                         PRINT_ERR("Invalid menu");
153                         continue;
154                     }
155                     HandleCredOperation(editMenu);
156                     RefreshCred();
157                 }
158                 break;
159             case SVR_EDIT_ACL:
160                 for (;;)
161                 {
162                     PrintEditMenu("ACL Resource", true, true, true, true);
163                     editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
164                     if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
165                     {
166                         if (!g_allowedEditMenu[editMenu])
167                         {
168                             PRINT_ERR("Disabled menu");
169                             continue;
170                         }
171                     }
172                     else if (BACK == editMenu)
173                     {
174                         PRINT_INFO("Back to the previous menu.");
175                         break;
176                     }
177                     else
178                     {
179                         PRINT_ERR("Invalid menu");
180                         continue;
181                     }
182                     HandleAclOperation(editMenu);
183                     RefreshACL();
184                 }
185                 break;
186             case SVR_EDIT_DOXM:
187                 for (;;)
188                 {
189                     PrintEditMenu("Doxm Resource", true, true, true, true);
190                     editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
191                     if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
192                     {
193                         if (!g_allowedEditMenu[editMenu])
194                         {
195                             PRINT_ERR("Disabled menu");
196                             continue;
197                         }
198                     }
199                     else if (BACK == editMenu)
200                     {
201                         PRINT_INFO("Back to the previous menu.");
202                         break;
203                     }
204                     else
205                     {
206                         PRINT_ERR("Invalid menu");
207                         continue;
208                     }
209                     HandleDoxmOperation(editMenu);
210                     RefreshDoxm();
211                 }
212                 break;
213             case SVR_EDIT_PSTAT:
214                 for (;;)
215                 {
216                     PrintEditMenu("Pstat Resource", true, true, true, true);
217                     editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
218                     if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
219                     {
220                         if (!g_allowedEditMenu[editMenu])
221                         {
222                             PRINT_ERR("Disabled menu");
223                             continue;
224                         }
225                     }
226                     else if (BACK == editMenu)
227                     {
228                         PRINT_INFO("Back to the previous menu.");
229                         break;
230                     }
231                     else
232                     {
233                         PRINT_ERR("Invalid menu");
234                         continue;
235                     }
236                     HandlePstatOperation(editMenu);
237                     RefreshPstat();
238                 }
239                 break;
240             case EXIT:
241                 run = false;
242                 break;
243             default:
244                 PRINT_ERR("Unknown operation");
245                 PRINT_ERR("Please make sure the menu.");
246                 break;
247         }
248     }
249
250     DeInitCredResource();
251     DeInitACL();
252     DeInitDoxm();
253     DeInitPstat();
254     return 0;
255 }
256
257 static FILE *SVRDBFopen(const char *path, const char *mode)
258 {
259     (void)path;  // unused |path| parameter
260     return fopen(g_svrDbPath, mode);
261 }
262
263 static void PrintHelp(void)
264 {
265     PRINT_ERR("<This program requires one input>");
266     PRINT_INFO("./svrdbeditor <svr_db_file_path>");
267 }
268
269 static void PrintEditMenu(const char *resourceName, bool print, bool add, bool remove,
270                           bool modify)
271 {
272     PRINT_PROG("\n\nYou can perform the "
273                CYAN_BEGIN "cyan color opertions " COLOR_END
274                BOLD_BEGIN "for" COLOR_END
275                YELLOW_BEGIN " %s" COLOR_END_NL, resourceName);
276
277     for (int i = 0; i < SVR_EDIT_IDX_SIZE; i++)
278     {
279         g_allowedEditMenu[i] = false;
280     }
281     if (print)
282     {
283         g_allowedEditMenu[SVR_PRINT] = true;
284         PRINT_DATA("\t%2d. Print all entities\n", SVR_PRINT);
285     }
286     else
287     {
288         PRINT_NORMAL("\t%2d. Print all entities\n", SVR_PRINT);
289     }
290
291     if (add)
292     {
293         g_allowedEditMenu[SVR_ADD] = true;
294         PRINT_DATA("\t%2d. Add entity\n", SVR_ADD);
295     }
296     else
297     {
298         PRINT_NORMAL("\t%2d. Add entity\n", SVR_ADD);
299     }
300     if (remove)
301     {
302         g_allowedEditMenu[SVR_REMOVE] = true;
303         PRINT_DATA("\t%2d. Remove entity\n", SVR_REMOVE);
304     }
305     else
306     {
307         PRINT_NORMAL("\t%2d. Remove entity\n", SVR_REMOVE);
308     }
309
310     if (modify)
311     {
312         g_allowedEditMenu[SVR_MODIFY] = true;
313         PRINT_DATA("\t%2d. Modify entity\n", SVR_MODIFY);
314     }
315     else
316     {
317         PRINT_NORMAL("\t%2d. Modify entity\n", SVR_MODIFY);
318     }
319     PRINT_DATA("\t%2d. Back to the main menu\n", BACK);
320 }
321
322 static void PrintMainMenu(void)
323 {
324     PRINT_PROG("\n\nYou can perform the "
325                CYAN_BEGIN "cyan color opertions : " COLOR_END_NL);
326
327     PRINT_DATA("\t%2d. Print All Security Resource.\n", SVR_PRINT_ALL);
328     PRINT_DATA("\t%2d. Edit Credential Resource.\n", SVR_EDIT_CRED);
329     PRINT_DATA("\t%2d. Edit ACL Resource.\n", SVR_EDIT_ACL);
330     PRINT_DATA("\t%2d. Edit Doxm Resource.\n", SVR_EDIT_DOXM);
331     PRINT_DATA("\t%2d. Edit Pstat Resource.\n", SVR_EDIT_PSTAT);
332     PRINT_DATA("\t%2d. Exit.\n", EXIT);
333 }