Commit 493a8e91 authored by Sangjoon Je's avatar Sangjoon Je Committed by Randeep

[IOT-2494] SVR DB Editor : Add Acl Edit features

Print info for ACL_V2
Support Modify ACL
Split svrdbeditor.c into several files
Fix svace issue
Fix valgrind mem leak issue
Fix wrong error msg

New Feature: https://jira.iotivity.org/browse/IOT-2494
Change-Id: I02636ed59e293108a4e5cf6bf7e75a19dafeb455
Signed-off-by: default avatarSangjoon Je <sangjoon.je@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/21585Reviewed-by: default avatarYoungman Jung <yman.jung@samsung.com>
Tested-by: default avatarjenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Randeep's avatarRandeep Singh <randeep.s@samsung.com>
(cherry picked from commit 32d88bf6)
Reviewed-on: https://gerrit.iotivity.org/gerrit/21801
parent 64b32964
......@@ -30,23 +30,23 @@ target_os = tools_env.get('TARGET_OS')
######################################################################
with_upstream_libcoap = tools_env.get('WITH_UPSTREAM_LIBCOAP')
if with_upstream_libcoap == '1':
tools_env.AppendUnique(CPPPATH=['#extlibs/libcoap/libcoap/include'])
tools_env.AppendUnique(CPPPATH=['#/extlibs/libcoap/libcoap/include'])
else:
tools_env.AppendUnique(CPPPATH=['../../connectivity/lib/libcoap-4.1.1/include'])
tools_env.AppendUnique(CPPPATH=['#/resource/csdk/connectivity/lib/libcoap-4.1.1/include'])
tools_env.PrependUnique(CPPPATH=[
'#/extlibs/cjson',
'../../include',
'../../stack/include',
'../../stack/include/internal',
'../../logger/include',
'../../../oc_logger/include',
'../../connectivity/api',
'../../connectivity/inc/pkix',
'../include',
'../include/internal',
'../provisioning/include',
'../provisioning/include/internal'
'#/resource/csdk/include',
'#/resource/csdk/stack/include',
'#/resource/csdk/stack/include/internal',
'#/resource/csdk/logger/include',
'#/resource/oc_logger/include',
'#/resource/csdk/connectivity/api',
'#/resource/csdk/connectivity/inc/pkix',
'#/resource/include',
'#/resource/include/internal',
'#/resource/csdk/security/provisioning/include',
'#/resource/csdk/security/provisioning/include/internal'
])
tools_env.AppendUnique(CXXFLAGS=['-O2', '-g', '-Wall', '-Wextra', '-std=c++0x'])
tools_env.AppendUnique(RPATH=[tools_env.get('BUILD_DIR')])
......@@ -80,17 +80,25 @@ else:
'ocsrm',
'routingmanager'
])
tools_env.PrependUnique(LIBS=['cjson'])
if tools_env.get('MULTIPLE_OWNER') == '1':
tools_env.AppendUnique(CPPDEFINES=['MULTIPLE_OWNER'])
######################################################################
# Source files and Targets
######################################################################
json2cbor_src = ['json2cbor.c']
json2cbor = tools_env.Program('json2cbor', json2cbor_src)
svrdbeditor = tools_env.Program('svrdbeditor', ['svrdbeditor.c'])
SVRDBEDITOR_DIR = 'svrdbeditor_src/'
svrdbeditor_src = [
SVRDBEDITOR_DIR + 'svrdbeditorcommon.c',
SVRDBEDITOR_DIR + 'svrdbeditoracl.c',
SVRDBEDITOR_DIR + 'svrdbeditorcred.c',
SVRDBEDITOR_DIR + 'svrdbeditordoxm.c',
SVRDBEDITOR_DIR + 'svrdbeditorpstat.c',
SVRDBEDITOR_DIR + 'svrdbeditor.c'
]
svrdbeditor = tools_env.Program("svrdbeditor", svrdbeditor_src)
Alias("securitytool", [json2cbor, svrdbeditor])
tools_env.AppendTarget('securitytool')
/* *****************************************************************
*
* Copyright 2017 Samsung Electronics All Rights Reserved.
*
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* *****************************************************************/
#include <string.h>
#include "octypes.h"
#include "ocstack.h"
#include "psinterface.h"
#include "credresource.h"
#include "svrdbeditorcommon.h"
#include "svrdbeditoracl.h"
#include "svrdbeditorcred.h"
#include "svrdbeditordoxm.h"
#include "svrdbeditorpstat.h"
static bool g_allowedEditMenu[SVR_EDIT_IDX_SIZE] = {false/*unused*/, false, false, false, false};
static char g_svrDbPath[SVR_DB_PATH_LENGTH];
typedef enum OperationType
{
SVR_PRINT_ALL = 1,
SVR_EDIT_CRED = 2,
SVR_EDIT_ACL = 3,
SVR_EDIT_DOXM = 4,
SVR_EDIT_PSTAT = 5,
EXIT = 99
} OperationType_t;
static int MainOperation(const char *svrpath);
static void PrintMainMenu();
static void PrintEditMenu(const char *resourceName, bool print, bool add, bool remove,
bool modify);
static void PrintHelp();
static FILE *SVRDBFopen(const char *path, const char *mode);
int main(int argc, char *argv[])
{
if (2 == argc)
{
PRINT_NORMAL("SVR DB File Path: %s\n", argv[1]);
return MainOperation(argv[1]);
}
else
{
PrintHelp();
return 0;
}
}
static int MainOperation(const char *svrpath)
{
OperationType_t menu = EXIT;
SubOperationType_t editMenu = EXIT;
OCStackResult ocResult = OC_STACK_ERROR;
bool run = true;
// initialize persistent storage for SVR DB
static OCPersistentStorage psInst =
{
.open = SVRDBFopen,
.read = fread,
.write = fwrite,
.close = fclose,
.unlink = unlink
};
if (!svrpath)
{
PRINT_ERR("Incorrect file path");
return -1;
}
strncpy(g_svrDbPath, svrpath, sizeof(g_svrDbPath) - 1);
g_svrDbPath[sizeof(g_svrDbPath) - 1] = '\0';
ocResult = OCRegisterPersistentStorageHandler(&psInst);
if (OC_STACK_OK != ocResult)
{
PRINT_ERR("OCRegisterPersistentStorageHandler : %d", ocResult);
return -1;
}
RefreshACL();
RefreshCred();
RefreshDoxm();
RefreshPstat();
while (run)
{
PrintMainMenu();
menu = (OperationType_t)InputNumber("\tSelect the menu : ");
switch (menu)
{
case SVR_PRINT_ALL:
PrintDoxm();
PrintPstat();
PrintAcl();
PrintCredList(GetCredList());
break;
case SVR_EDIT_CRED:
for (;;)
{
if (NULL == GetCredList())
{
PRINT_WARN("Credential resource is empty.");
PrintEditMenu("Credential Resource", false, true, false, false);
}
else
{
PrintEditMenu("Credential Resource", true, true, true, true);
}
editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
{
if (!g_allowedEditMenu[editMenu])
{
PRINT_ERR("Disabled menu");
continue;
}
}
else if (BACK == editMenu)
{
PRINT_INFO("Back to the previous menu.");
break;
}
else
{
PRINT_ERR("Invalid menu");
continue;
}
HandleCredOperation(editMenu);
RefreshCred();
}
break;
case SVR_EDIT_ACL:
for (;;)
{
PrintEditMenu("ACL Resource", true, true, true, true);
editMenu = (SubOperationType_t)InputNumber("Select the menu : ");
if (0 < editMenu && editMenu < SVR_EDIT_IDX_SIZE)
{
if (!g_allowedEditMenu[editMenu])
{
PRINT_ERR("Disabled menu");
continue;
}
}
else if (BACK == editMenu)
{
PRINT_INFO("Back to the previous menu.");
break;
}
else
{
PRINT_ERR("Invalid menu");
continue;
}
HandleAclOperation(editMenu);
RefreshACL();
}
break;
case SVR_EDIT_DOXM:
PRINT_INFO("NOT SUPPORTED YET");
//PrintEditMenu("Doxm Resource", false, false, true);
//T.B.D
break;
case SVR_EDIT_PSTAT:
PRINT_INFO("NOT SUPPORTED YET");
//PrintEditMenu("Pstat Resource", false, false, true);
//T.B.D
break;
case EXIT:
run = false;
break;
default:
PRINT_ERR("Unknown operation");
PRINT_ERR("Please make sure the menu.");
break;
}
}
DeInitCredResource();
DeInitACL();
DeInitDoxm();
DeInitPstat();
return 0;
}
static FILE *SVRDBFopen(const char *path, const char *mode)
{
(void)path; // unused |path| parameter
return fopen(g_svrDbPath, mode);
}
static void PrintHelp()
{
PRINT_ERR("<This program requires one input>");
PRINT_INFO("./svrdbeditor <svr_db_file_path>");
}
static void PrintEditMenu(const char *resourceName, bool print, bool add, bool remove,
bool modify)
{
PRINT_PROG("\n\nYou can perform the "
CYAN_BEGIN "cyan color opertions " COLOR_END
BOLD_BEGIN "for" COLOR_END
YELLOW_BEGIN " %s" COLOR_END_NL, resourceName);
for (int i = 0; i < SVR_EDIT_IDX_SIZE; i++)
{
g_allowedEditMenu[i] = false;
}
if (print)
{
g_allowedEditMenu[SVR_PRINT] = true;
PRINT_DATA("\t%2d. Print all entities\n", SVR_PRINT);
}
else
{
PRINT_NORMAL("\t%2d. Print all entities\n", SVR_PRINT);
}
if (add)
{
g_allowedEditMenu[SVR_ADD] = true;
PRINT_DATA("\t%2d. Add entity\n", SVR_ADD);
}
else
{
PRINT_NORMAL("\t%2d. Add entity\n", SVR_ADD);
}
if (remove)
{
g_allowedEditMenu[SVR_REMOVE] = true;
PRINT_DATA("\t%2d. Remove entity\n", SVR_REMOVE);
}
else
{
PRINT_NORMAL("\t%2d. Remove entity\n", SVR_REMOVE);
}
if (modify)
{
g_allowedEditMenu[SVR_MODIFY] = true;
PRINT_DATA("\t%2d. Modify entity\n", SVR_MODIFY);
}
else
{
PRINT_NORMAL("\t%2d. Modify entity\n", SVR_MODIFY);
}
PRINT_DATA("\t%2d. Back to the main menu\n", BACK);
}
static void PrintMainMenu()
{
PRINT_PROG("\n\nYou can perform the "
CYAN_BEGIN "cyan color opertions : " COLOR_END_NL);
PRINT_DATA("\t%2d. Print All Security Resource.\n", SVR_PRINT_ALL);
PRINT_DATA("\t%2d. Edit Credential Resource.\n", SVR_EDIT_CRED);
PRINT_DATA("\t%2d. Edit ACL Resource.\n", SVR_EDIT_ACL);
PRINT_PROG("\t%2d. Edit Doxm Resource. (T.B.D)\n", SVR_EDIT_DOXM);
PRINT_PROG("\t%2d. Edit Pstat Resource. (T.B.D)\n", SVR_EDIT_PSTAT);
PRINT_DATA("\t%2d. Exit.\n", EXIT);
}
This diff is collapsed.
/* *****************************************************************
*
* Copyright 2017 Samsung Electronics All Rights Reserved.
*
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* *****************************************************************/
#ifndef SVRDBEDITOR_ACL_H_
#define SVRDBEDITOR_ACL_H_
#include "svrdbeditorcommon.h"
void DeInitACL();
void RefreshACL();
size_t PrintAcl();
void HandleAclOperation(const SubOperationType_t cmd);
#endif /* SVRDBEDITOR_ACL_H_ */
\ No newline at end of file
/* *****************************************************************
*
* Copyright 2017 Samsung Electronics All Rights Reserved.
*
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* *****************************************************************/
#include <string.h>
#include "utlist.h"
#include "oic_malloc.h"
#include "oic_string.h"
#include "srmresourcestrings.h"
#include "experimental/securevirtualresourcetypes.h"
#include "srmutility.h"
#include "svrdbeditorcommon.h"
void PrintUuid(const OicUuid_t *uuid)
{
char *strUuid = NULL;
if (OC_STACK_OK == ConvertUuidToStr(uuid, &strUuid))
{
PRINT_DATA("%s\n", strUuid);
OICFree(strUuid);
}
else
{
PRINT_ERR("Can not convert UUID to String");
}
}
void PrintIntArray(const int *array, size_t length)
{
for (size_t i = 0; i < length; i++)
{
PRINT_DATA("%d ", array[i]);
}
PRINT_NL();
}
void PrintStringArray(const char **array, size_t length)
{
for (size_t i = 0; i < length; i++)
{
PRINT_DATA("%s ", array[i]);
}
PRINT_NL();
}
void PrintInt(int value)
{
PRINT_DATA("%d\n", value);
}
void PrintString(const char *text)
{
PRINT_DATA("%s\n", text);
}
void PrintBuffer(const uint8_t *buf, size_t bufLen)
{
size_t i = 0;
for (i = 0; i < bufLen; i++)
{
if (0 == (i + 1) % 20 || bufLen - 1 == i)
{
PRINT_DATA("%02X \n", buf[i]);
}
else
{
PRINT_DATA("%02X ", buf[i]);
}
}
}
int InputNumber(const char *infoText)
{
int inputValue = 0;
PRINT_PROG("%s", infoText);
for (int ret = 0; 1 != ret; )
{
ret = scanf("%d", &inputValue);
for ( ; 0x20 <= getchar(); ); // for removing overflow garbages
// '0x20<=code' is character region
}
return inputValue;
}
char *InputString(const char *infoText)
{
char tmpStr[SVR_DB_PATH_LENGTH] = {0};
PRINT_PROG("%s", infoText);
for (int ret = 0; 1 != ret; )
{
ret = scanf("%1024s", tmpStr);
for ( ; 0x20 <= getchar(); ); // for removing overflow garbages
// '0x20<=code' is character region
}
return OICStrdup(tmpStr);
}
int InputUuid(OicUuid_t *uuid)
{
char strSubject[UUID_LENGTH * 2 + 4 + 1] = {0};
OCStackResult ocResult = OC_STACK_ERROR;
if (NULL == uuid)
{
PRINT_ERR("Failed InputUuid");
return -1;
}
for (int ret = 0; 1 != ret; )
{
ret = scanf("%37s", strSubject);
for ( ; 0x20 <= getchar(); ); // for removing overflow garbages
// '0x20<=code' is character region
}
if (0 == strncmp(strSubject, (char *)WILDCARD_SUBJECT_ID.id, sizeof(OicUuid_t)))
{
memset(uuid->id, 0x00, sizeof(uuid->id));
memcpy(uuid->id, WILDCARD_SUBJECT_ID.id, WILDCARD_SUBJECT_ID_LEN);
}
else
{
ocResult = ConvertStrToUuid(strSubject, uuid);
if (OC_STACK_OK != ocResult)
{
PRINT_ERR("Failed ConvertStrToUuid");
return -1;
}
}
return 0;
}
/* *****************************************************************
*
* Copyright 2017 Samsung Electronics All Rights Reserved.
*
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* *****************************************************************/
#ifndef SVRDBEDITOR_COMMON_H_
#define SVRDBEDITOR_COMMON_H_
#include "iotivity_config.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdio.h>
#include "experimental/securevirtualresourcetypes.h"
#define TAG "OIC_SVR_DB_EDITOR"
#define BOLD_BEGIN "\033[1m"
#define RED_BEGIN "\033[1;31m"
#define YELLOW_BEGIN "\033[1;33m"
#define CYAN_BEGIN "\033[1;36m"
#define GREEN_BEGIN "\033[1;92m"
#define COLOR_END "\033[0m"
#define COLOR_END_NL "\033[0m\n"
#define SVR_MAX_ENTITY (16)
#define SVR_DB_PATH_LENGTH (1024)
#define PRINT_ERR(fmt,...) printf(RED_BEGIN "error: " fmt COLOR_END_NL, ##__VA_ARGS__)
#define PRINT_WARN(fmt,...) printf(YELLOW_BEGIN "warning : " fmt COLOR_END_NL, ##__VA_ARGS__)
#define PRINT_INFO(fmt,...) printf(YELLOW_BEGIN fmt COLOR_END_NL, ##__VA_ARGS__)
#define PRINT_PROG(fmt,...) printf(BOLD_BEGIN fmt COLOR_END, ##__VA_ARGS__)
#define PRINT_DATA(fmt,...) printf(CYAN_BEGIN fmt COLOR_END, ##__VA_ARGS__)
#define PRINT_NORMAL(fmt,...) printf(fmt, ##__VA_ARGS__)
#define PRINT_NL() printf("\n");
typedef enum SubOperationType
{
SVR_PRINT = 1,
SVR_ADD = 2,
SVR_REMOVE = 3,
SVR_MODIFY = 4,
SVR_EDIT_IDX_SIZE = 5,
BACK = 99
} SubOperationType_t;
void PrintUuid(const OicUuid_t *uuid);
void PrintIntArray(const int *array, size_t length);
void PrintStringArray(const char **array, size_t length);
void PrintInt(int value);
void PrintString(const char *text);
void PrintBuffer(const uint8_t *buf, size_t bufLen);
int InputNumber(const char *infoText);
char *InputString(const char *infoText);
int InputUuid(OicUuid_t *uuid);
#endif /* SVRDBEDITOR_COMMON_H_ */