[IOT-2883] SVR DB editor is broken in 1.3-rel
[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 #include "pstatresource.h"
35
36 static bool g_allowedEditMenu[SVR_EDIT_IDX_SIZE] = {false/*unused*/, false, false, false, false};
37 static char g_svrDbPath[SVR_DB_PATH_LENGTH];
38
39 typedef enum OperationType
40 {
41     SVR_PRINT_ALL = 1,
42     SVR_EDIT_CRED = 2,
43     SVR_EDIT_ACL = 3,
44     SVR_EDIT_DOXM = 4,
45     SVR_EDIT_PSTAT = 5,
46     EXIT = 99
47 } OperationType_t;
48
49 static int MainOperation(const char *svrpath);
50 static void PrintMainMenu(void);
51 static void PrintEditMenu(const char *resourceName, bool print, bool add, bool remove,
52                           bool modify);
53 static void PrintHelp(void);
54 static FILE *SVRDBFopen(const char *path, const char *mode);
55
56 int main(int argc, char *argv[])
57 {
58     if (2 == argc)
59     {
60         PRINT_NORMAL("SVR DB File Path: %s\n", argv[1]);
61         return MainOperation(argv[1]);
62     }
63     else
64     {
65         PrintHelp();
66         return 0;
67     }
68 }
69
70 static int MainOperation(const char *svrpath)
71 {
72     OperationType_t menu = EXIT;
73     SubOperationType_t editMenu = EXIT;
74     OCStackResult ocResult = OC_STACK_ERROR;
75     bool run = true;
76
77     // initialize persistent storage for SVR DB
78     static OCPersistentStorage psInst =
79     {
80         .open = SVRDBFopen,
81         .read = fread,
82         .write = fwrite,
83         .close = fclose,
84         .unlink = unlink
85     };
86
87     if (!svrpath)
88     {
89         PRINT_ERR("Incorrect file path");
90         return -1;
91     }
92
93     strncpy(g_svrDbPath, svrpath, sizeof(g_svrDbPath) - 1);
94     g_svrDbPath[sizeof(g_svrDbPath) - 1] = '\0';
95
96     ocResult = OCRegisterPersistentStorageHandler(&psInst);
97     if (OC_STACK_OK != ocResult)
98     {
99         PRINT_ERR("OCRegisterPersistentStorageHandler : %d", ocResult);
100         return -1;
101     }
102
103     InitPstatResourceToDefault();
104
105     RefreshACL();
106     RefreshCred();
107     RefreshDoxm();
108     RefreshPstat();
109     while (run)
110     {
111         PrintMainMenu();
112         menu = (OperationType_t)InputNumber("\tSelect the menu : ");
113         switch (menu)
114         {
115             case SVR_PRINT_ALL:
116                 PrintDoxm();
117                 PrintPstat();
118                 PrintAcl();
119                 PrintCredList(GetCredList());
120                 break;
121             case SVR_EDIT_CRED:
122                 for (;;)
123                 {
124                     if (NULL == GetCredList())
125                     {
126                         PRINT_WARN("Credential resource is empty.");
127                         PrintEditMenu("Credential Resource", false, true, false, false);
128                     }
129                     else
130                     {
131                         PrintEditMenu("Credential Resource", true, true, true, true);
132                     }
133                     editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
134                     if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
135                     {
136                         if (!g_allowedEditMenu[editMenu])
137                         {
138                             PRINT_ERR("Disabled menu");
139                             continue;
140                         }
141                     }
142                     else if (BACK == editMenu)
143                     {
144                         PRINT_INFO("Back to the previous menu.");
145                         break;
146                     }
147                     else
148                     {
149                         PRINT_ERR("Invalid menu");
150                         continue;
151                     }
152                     HandleCredOperation(editMenu);
153                     RefreshCred();
154                 }
155                 break;
156             case SVR_EDIT_ACL:
157                 for (;;)
158                 {
159                     PrintEditMenu("ACL Resource", true, true, true, true);
160                     editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
161                     if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
162                     {
163                         if (!g_allowedEditMenu[editMenu])
164                         {
165                             PRINT_ERR("Disabled menu");
166                             continue;
167                         }
168                     }
169                     else if (BACK == editMenu)
170                     {
171                         PRINT_INFO("Back to the previous menu.");
172                         break;
173                     }
174                     else
175                     {
176                         PRINT_ERR("Invalid menu");
177                         continue;
178                     }
179                     HandleAclOperation(editMenu);
180                     RefreshACL();
181                 }
182                 break;
183             case SVR_EDIT_DOXM:
184                 for (;;)
185                 {
186                     PrintEditMenu("Doxm Resource", true, true, true, true);
187                     editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
188                     if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
189                     {
190                         if (!g_allowedEditMenu[editMenu])
191                         {
192                             PRINT_ERR("Disabled menu");
193                             continue;
194                         }
195                     }
196                     else if (BACK == editMenu)
197                     {
198                         PRINT_INFO("Back to the previous menu.");
199                         break;
200                     }
201                     else
202                     {
203                         PRINT_ERR("Invalid menu");
204                         continue;
205                     }
206                     HandleDoxmOperation(editMenu);
207                     RefreshDoxm();
208                 }
209                 break;
210             case SVR_EDIT_PSTAT:
211                 for (;;)
212                 {
213                     PrintEditMenu("Pstat Resource", true, true, true, true);
214                     editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
215                     if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
216                     {
217                         if (!g_allowedEditMenu[editMenu])
218                         {
219                             PRINT_ERR("Disabled menu");
220                             continue;
221                         }
222                     }
223                     else if (BACK == editMenu)
224                     {
225                         PRINT_INFO("Back to the previous menu.");
226                         break;
227                     }
228                     else
229                     {
230                         PRINT_ERR("Invalid menu");
231                         continue;
232                     }
233                     HandlePstatOperation(editMenu);
234                     RefreshPstat();
235                 }
236                 break;
237             case EXIT:
238                 run = false;
239                 break;
240             default:
241                 PRINT_ERR("Unknown operation");
242                 PRINT_ERR("Please make sure the menu.");
243                 break;
244         }
245     }
246
247     DeInitCredResource();
248     DeInitACL();
249     DeInitDoxm();
250     DeInitPstat();
251     return 0;
252 }
253
254 static FILE *SVRDBFopen(const char *path, const char *mode)
255 {
256     (void)path;  // unused |path| parameter
257     return fopen(g_svrDbPath, mode);
258 }
259
260 static void PrintHelp(void)
261 {
262     PRINT_ERR("<This program requires one input>");
263     PRINT_INFO("./svrdbeditor <svr_db_file_path>");
264 }
265
266 static void PrintEditMenu(const char *resourceName, bool print, bool add, bool remove,
267                           bool modify)
268 {
269     PRINT_PROG("\n\nYou can perform the "
270                CYAN_BEGIN "cyan color opertions " COLOR_END
271                BOLD_BEGIN "for" COLOR_END
272                YELLOW_BEGIN " %s" COLOR_END_NL, resourceName);
273
274     for (int i = 0; i < SVR_EDIT_IDX_SIZE; i++)
275     {
276         g_allowedEditMenu[i] = false;
277     }
278     if (print)
279     {
280         g_allowedEditMenu[SVR_PRINT] = true;
281         PRINT_DATA("\t%2d. Print all entities\n", SVR_PRINT);
282     }
283     else
284     {
285         PRINT_NORMAL("\t%2d. Print all entities\n", SVR_PRINT);
286     }
287
288     if (add)
289     {
290         g_allowedEditMenu[SVR_ADD] = true;
291         PRINT_DATA("\t%2d. Add entity\n", SVR_ADD);
292     }
293     else
294     {
295         PRINT_NORMAL("\t%2d. Add entity\n", SVR_ADD);
296     }
297     if (remove)
298     {
299         g_allowedEditMenu[SVR_REMOVE] = true;
300         PRINT_DATA("\t%2d. Remove entity\n", SVR_REMOVE);
301     }
302     else
303     {
304         PRINT_NORMAL("\t%2d. Remove entity\n", SVR_REMOVE);
305     }
306
307     if (modify)
308     {
309         g_allowedEditMenu[SVR_MODIFY] = true;
310         PRINT_DATA("\t%2d. Modify entity\n", SVR_MODIFY);
311     }
312     else
313     {
314         PRINT_NORMAL("\t%2d. Modify entity\n", SVR_MODIFY);
315     }
316     PRINT_DATA("\t%2d. Back to the main menu\n", BACK);
317 }
318
319 static void PrintMainMenu(void)
320 {
321     PRINT_PROG("\n\nYou can perform the "
322                CYAN_BEGIN "cyan color opertions : " COLOR_END_NL);
323
324     PRINT_DATA("\t%2d. Print All Security Resource.\n", SVR_PRINT_ALL);
325     PRINT_DATA("\t%2d. Edit Credential Resource.\n", SVR_EDIT_CRED);
326     PRINT_DATA("\t%2d. Edit ACL Resource.\n", SVR_EDIT_ACL);
327     PRINT_DATA("\t%2d. Edit Doxm Resource.\n", SVR_EDIT_DOXM);
328     PRINT_DATA("\t%2d. Edit Pstat Resource.\n", SVR_EDIT_PSTAT);
329     PRINT_DATA("\t%2d. Exit.\n", EXIT);
330 }