1 /* *****************************************************************
3 * Copyright 2017 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * *****************************************************************/
25 #include "psinterface.h"
27 #include "credresource.h"
29 #include "svrdbeditorcommon.h"
30 #include "svrdbeditoracl.h"
31 #include "svrdbeditorcred.h"
32 #include "svrdbeditordoxm.h"
33 #include "svrdbeditorpstat.h"
35 static bool g_allowedEditMenu[SVR_EDIT_IDX_SIZE] = {false/*unused*/, false, false, false, false};
36 static char g_svrDbPath[SVR_DB_PATH_LENGTH];
38 typedef enum OperationType
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,
52 static void PrintHelp(void);
53 static FILE *SVRDBFopen(const char *path, const char *mode);
55 int main(int argc, char *argv[])
59 PRINT_NORMAL("SVR DB File Path: %s\n", argv[1]);
60 return MainOperation(argv[1]);
69 static int MainOperation(const char *svrpath)
71 OperationType_t menu = EXIT;
72 SubOperationType_t editMenu = EXIT;
73 OCStackResult ocResult = OC_STACK_ERROR;
76 // initialize persistent storage for SVR DB
77 static OCPersistentStorage psInst =
88 PRINT_ERR("Incorrect file path");
92 strncpy(g_svrDbPath, svrpath, sizeof(g_svrDbPath) - 1);
93 g_svrDbPath[sizeof(g_svrDbPath) - 1] = '\0';
95 ocResult = InitPersistentStorageInterface();
96 if (OC_STACK_OK != ocResult)
98 PRINT_ERR("InitPersistentStorageInterface error : %d", ocResult);
102 ocResult = OCRegisterPersistentStorageHandler(&psInst);
103 if (OC_STACK_OK != ocResult)
105 PRINT_ERR("OCRegisterPersistentStorageHandler : %d", ocResult);
115 menu = (OperationType_t)InputNumber("\tSelect the menu : ");
122 PrintCredList(GetCredList());
127 if (NULL == GetCredList())
129 PRINT_WARN("Credential resource is empty.");
130 PrintEditMenu("Credential Resource", false, true, false, false);
134 PrintEditMenu("Credential Resource", true, true, true, true);
136 editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
137 if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
139 if (!g_allowedEditMenu[editMenu])
141 PRINT_ERR("Disabled menu");
145 else if (BACK == editMenu)
147 PRINT_INFO("Back to the previous menu.");
152 PRINT_ERR("Invalid menu");
155 HandleCredOperation(editMenu);
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)
166 if (!g_allowedEditMenu[editMenu])
168 PRINT_ERR("Disabled menu");
172 else if (BACK == editMenu)
174 PRINT_INFO("Back to the previous menu.");
179 PRINT_ERR("Invalid menu");
182 HandleAclOperation(editMenu);
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)
193 if (!g_allowedEditMenu[editMenu])
195 PRINT_ERR("Disabled menu");
199 else if (BACK == editMenu)
201 PRINT_INFO("Back to the previous menu.");
206 PRINT_ERR("Invalid menu");
209 HandleDoxmOperation(editMenu);
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)
220 if (!g_allowedEditMenu[editMenu])
222 PRINT_ERR("Disabled menu");
226 else if (BACK == editMenu)
228 PRINT_INFO("Back to the previous menu.");
233 PRINT_ERR("Invalid menu");
236 HandlePstatOperation(editMenu);
244 PRINT_ERR("Unknown operation");
245 PRINT_ERR("Please make sure the menu.");
250 DeInitCredResource();
257 static FILE *SVRDBFopen(const char *path, const char *mode)
259 (void)path; // unused |path| parameter
260 return fopen(g_svrDbPath, mode);
263 static void PrintHelp(void)
265 PRINT_ERR("<This program requires one input>");
266 PRINT_INFO("./svrdbeditor <svr_db_file_path>");
269 static void PrintEditMenu(const char *resourceName, bool print, bool add, bool remove,
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);
277 for (int i = 0; i < SVR_EDIT_IDX_SIZE; i++)
279 g_allowedEditMenu[i] = false;
283 g_allowedEditMenu[SVR_PRINT] = true;
284 PRINT_DATA("\t%2d. Print all entities\n", SVR_PRINT);
288 PRINT_NORMAL("\t%2d. Print all entities\n", SVR_PRINT);
293 g_allowedEditMenu[SVR_ADD] = true;
294 PRINT_DATA("\t%2d. Add entity\n", SVR_ADD);
298 PRINT_NORMAL("\t%2d. Add entity\n", SVR_ADD);
302 g_allowedEditMenu[SVR_REMOVE] = true;
303 PRINT_DATA("\t%2d. Remove entity\n", SVR_REMOVE);
307 PRINT_NORMAL("\t%2d. Remove entity\n", SVR_REMOVE);
312 g_allowedEditMenu[SVR_MODIFY] = true;
313 PRINT_DATA("\t%2d. Modify entity\n", SVR_MODIFY);
317 PRINT_NORMAL("\t%2d. Modify entity\n", SVR_MODIFY);
319 PRINT_DATA("\t%2d. Back to the main menu\n", BACK);
322 static void PrintMainMenu(void)
324 PRINT_PROG("\n\nYou can perform the "
325 CYAN_BEGIN "cyan color opertions : " COLOR_END_NL);
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);