75ee66bb006c858f6dc456bb1b8471419170f79a
[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 = 2,
42     SVR_EDIT_ACL = 3,
43     SVR_EDIT_DOXM = 4,
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 = OCRegisterPersistentStorageHandler(&psInst);
96     if (OC_STACK_OK != ocResult)
97     {
98         PRINT_ERR("OCRegisterPersistentStorageHandler : %d", ocResult);
99         return -1;
100     }
101     RefreshACL();
102     RefreshCred();
103     RefreshDoxm();
104     RefreshPstat();
105     while (run)
106     {
107         PrintMainMenu();
108         menu = (OperationType_t)InputNumber("\tSelect the menu : ");
109         switch (menu)
110         {
111             case SVR_PRINT_ALL:
112                 PrintDoxm();
113                 PrintPstat();
114                 PrintAcl();
115                 PrintCredList(GetCredList());
116                 break;
117             case SVR_EDIT_CRED:
118                 for (;;)
119                 {
120                     if (NULL == GetCredList())
121                     {
122                         PRINT_WARN("Credential resource is empty.");
123                         PrintEditMenu("Credential Resource", false, true, false, false);
124                     }
125                     else
126                     {
127                         PrintEditMenu("Credential Resource", true, true, true, true);
128                     }
129                     editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
130                     if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
131                     {
132                         if (!g_allowedEditMenu[editMenu])
133                         {
134                             PRINT_ERR("Disabled menu");
135                             continue;
136                         }
137                     }
138                     else if (BACK == editMenu)
139                     {
140                         PRINT_INFO("Back to the previous menu.");
141                         break;
142                     }
143                     else
144                     {
145                         PRINT_ERR("Invalid menu");
146                         continue;
147                     }
148                     HandleCredOperation(editMenu);
149                     RefreshCred();
150                 }
151                 break;
152             case SVR_EDIT_ACL:
153                 for (;;)
154                 {
155                     PrintEditMenu("ACL Resource", true, true, true, true);
156                     editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
157                     if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
158                     {
159                         if (!g_allowedEditMenu[editMenu])
160                         {
161                             PRINT_ERR("Disabled menu");
162                             continue;
163                         }
164                     }
165                     else if (BACK == editMenu)
166                     {
167                         PRINT_INFO("Back to the previous menu.");
168                         break;
169                     }
170                     else
171                     {
172                         PRINT_ERR("Invalid menu");
173                         continue;
174                     }
175                     HandleAclOperation(editMenu);
176                     RefreshACL();
177                 }
178                 break;
179             case SVR_EDIT_DOXM:
180                 for (;;)
181                 {
182                     PrintEditMenu("Doxm Resource", true, true, true, true);
183                     editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
184                     if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
185                     {
186                         if (!g_allowedEditMenu[editMenu])
187                         {
188                             PRINT_ERR("Disabled menu");
189                             continue;
190                         }
191                     }
192                     else if (BACK == editMenu)
193                     {
194                         PRINT_INFO("Back to the previous menu.");
195                         break;
196                     }
197                     else
198                     {
199                         PRINT_ERR("Invalid menu");
200                         continue;
201                     }
202                     HandleDoxmOperation(editMenu);
203                     RefreshDoxm();
204                 }
205                 break;
206             case SVR_EDIT_PSTAT:
207                 for (;;)
208                 {
209                     PrintEditMenu("Pstat Resource", true, true, true, true);
210                     editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
211                     if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
212                     {
213                         if (!g_allowedEditMenu[editMenu])
214                         {
215                             PRINT_ERR("Disabled menu");
216                             continue;
217                         }
218                     }
219                     else if (BACK == editMenu)
220                     {
221                         PRINT_INFO("Back to the previous menu.");
222                         break;
223                     }
224                     else
225                     {
226                         PRINT_ERR("Invalid menu");
227                         continue;
228                     }
229                     HandlePstatOperation(editMenu);
230                     RefreshPstat();
231                 }
232                 break;
233             case EXIT:
234                 run = false;
235                 break;
236             default:
237                 PRINT_ERR("Unknown operation");
238                 PRINT_ERR("Please make sure the menu.");
239                 break;
240         }
241     }
242
243     DeInitCredResource();
244     DeInitACL();
245     DeInitDoxm();
246     DeInitPstat();
247     return 0;
248 }
249
250 static FILE *SVRDBFopen(const char *path, const char *mode)
251 {
252     (void)path;  // unused |path| parameter
253     return fopen(g_svrDbPath, mode);
254 }
255
256 static void PrintHelp(void)
257 {
258     PRINT_ERR("<This program requires one input>");
259     PRINT_INFO("./svrdbeditor <svr_db_file_path>");
260 }
261
262 static void PrintEditMenu(const char *resourceName, bool print, bool add, bool remove,
263                           bool modify)
264 {
265     PRINT_PROG("\n\nYou can perform the "
266                CYAN_BEGIN "cyan color opertions " COLOR_END
267                BOLD_BEGIN "for" COLOR_END
268                YELLOW_BEGIN " %s" COLOR_END_NL, resourceName);
269
270     for (int i = 0; i < SVR_EDIT_IDX_SIZE; i++)
271     {
272         g_allowedEditMenu[i] = false;
273     }
274     if (print)
275     {
276         g_allowedEditMenu[SVR_PRINT] = true;
277         PRINT_DATA("\t%2d. Print all entities\n", SVR_PRINT);
278     }
279     else
280     {
281         PRINT_NORMAL("\t%2d. Print all entities\n", SVR_PRINT);
282     }
283
284     if (add)
285     {
286         g_allowedEditMenu[SVR_ADD] = true;
287         PRINT_DATA("\t%2d. Add entity\n", SVR_ADD);
288     }
289     else
290     {
291         PRINT_NORMAL("\t%2d. Add entity\n", SVR_ADD);
292     }
293     if (remove)
294     {
295         g_allowedEditMenu[SVR_REMOVE] = true;
296         PRINT_DATA("\t%2d. Remove entity\n", SVR_REMOVE);
297     }
298     else
299     {
300         PRINT_NORMAL("\t%2d. Remove entity\n", SVR_REMOVE);
301     }
302
303     if (modify)
304     {
305         g_allowedEditMenu[SVR_MODIFY] = true;
306         PRINT_DATA("\t%2d. Modify entity\n", SVR_MODIFY);
307     }
308     else
309     {
310         PRINT_NORMAL("\t%2d. Modify entity\n", SVR_MODIFY);
311     }
312     PRINT_DATA("\t%2d. Back to the main menu\n", BACK);
313 }
314
315 static void PrintMainMenu(void)
316 {
317     PRINT_PROG("\n\nYou can perform the "
318                CYAN_BEGIN "cyan color opertions : " COLOR_END_NL);
319
320     PRINT_DATA("\t%2d. Print All Security Resource.\n", SVR_PRINT_ALL);
321     PRINT_DATA("\t%2d. Edit Credential Resource.\n", SVR_EDIT_CRED);
322     PRINT_DATA("\t%2d. Edit ACL Resource.\n", SVR_EDIT_ACL);
323     PRINT_DATA("\t%2d. Edit Doxm Resource.\n", SVR_EDIT_DOXM);
324     PRINT_DATA("\t%2d. Edit Pstat Resource.\n", SVR_EDIT_PSTAT);
325     PRINT_DATA("\t%2d. Exit.\n", EXIT);
326 }