Initial commit for phosphor certificate manager

Certificate management application allows to replace the
existing certificate and private key file with another
(possibly CA signed Certificate and private key file.

Change-Id: I1129cc92ca4aae5f84cb304dda9073cc74781009
Signed-off-by: Jayanth Othayoth <ojayanth@in.ibm.com>
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..ea71ad6
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,99 @@
+---
+Language:        Cpp
+# BasedOnStyle:  LLVM
+AccessModifierOffset: -2
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlinesLeft: false
+AlignOperands:   true
+AlignTrailingComments: true
+AllowAllParametersOfDeclarationOnNextLine: true
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: true
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass:      true
+  AfterControlStatement: true
+  AfterEnum:       true
+  AfterFunction:   true
+  AfterNamespace:  true
+  AfterObjCDeclaration: true
+  AfterStruct:     true
+  AfterUnion:      true
+  BeforeCatch:     true
+  BeforeElse:      true
+  IndentBraces:    false
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializers: AfterColon
+ColumnLimit:     80
+CommentPragmas:  '^ IWYU pragma:'
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 4
+ContinuationIndentWidth: 4
+Cpp11BracedListStyle: true
+DerivePointerAlignment: false
+PointerAlignment: Left
+DisableFormat:   false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: true
+ForEachMacros:   [ foreach, Q_FOREACH, BOOST_FOREACH ]
+IncludeBlocks: Regroup
+IncludeCategories:
+  - Regex:           '^[<"](gtest|gmock)'
+    Priority:        5
+  - Regex:           '^"config.h"'
+    Priority:        -1
+  - Regex:           '^".*\.hpp"'
+    Priority:        1
+  - Regex:           '^<.*\.h>'
+    Priority:        2
+  - Regex:           '^<.*'
+    Priority:        3
+  - Regex:           '.*'
+    Priority:        4
+IndentCaseLabels: true
+IndentWidth:     4
+IndentWrappedFunctionNames: true
+KeepEmptyLinesAtTheStartOfBlocks: true
+MacroBlockBegin: ''
+MacroBlockEnd:   ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBlockIndentWidth: 2
+ObjCSpaceAfterProperty: false
+ObjCSpaceBeforeProtocolList: true
+PenaltyBreakBeforeFirstCallParameter: 19
+PenaltyBreakComment: 300
+PenaltyBreakFirstLessLess: 120
+PenaltyBreakString: 1000
+PenaltyExcessCharacter: 1000000
+PenaltyReturnTypeOnItsOwnLine: 60
+ReflowComments:  true
+SortIncludes:    true
+SortUsingDeclarations: true
+SpaceAfterCStyleCast: false
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeParens: ControlStatements
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles:  false
+SpacesInContainerLiterals: true
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard:        Cpp11
+TabWidth:        4
+UseTab:          Never
+...
+
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c6198f6
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,46 @@
+# Template from:
+# https://github.com/github/gitignore/blob/master/Autotools.gitignore
+
+# http://www.gnu.org/software/automake
+
+Makefile.in
+/ar-lib
+/mdate-sh
+/py-compile
+/test-driver
+/ylwrap
+
+# http://www.gnu.org/software/autoconf
+
+/autom4te.cache
+/aclocal.m4
+/compile
+/config.guess
+/config.h.in
+/config.sub
+/configure
+/configure.scan
+/depcomp
+/install-sh
+/missing
+/stamp-h1
+
+# https://www.gnu.org/software/libtool/
+
+/ltmain.sh
+
+# http://www.gnu.org/software/texinfo
+
+/texinfo.tex
+
+# Repo Specific Items
+*.o
+*.trs
+/config.h
+/config.h.in~
+*.log
+/config.status
+Makefile
+.deps
+*-libtool
+/phosphor_certificate_manager
diff --git a/Makefile.am b/Makefile.am
new file mode 100644
index 0000000..824d780
--- /dev/null
+++ b/Makefile.am
@@ -0,0 +1,12 @@
+AM_DEFAULT_SOURCE_EXT = .cpp
+
+# Build these headers, don't install them
+noinst_HEADERS = \
+	argument.hpp
+
+sbin_PROGRAMS = \
+	phosphor-certificate-manager
+
+phosphor_certificate_manager_SOURCES = \
+	mainapp.cpp \
+	argument.cpp
diff --git a/README.md b/README.md
index 793dde9..17832ca 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,15 @@
-# phosphor-certificate-manager
\ No newline at end of file
+# phosphor-certificate-manager
+Certificate management allows to replace the existing certificate and private
+key file with another (possibly CA signed) Certificate key file. Certificate
+management allows the user to install both the server and client certificates.
+
+## To Build
+```
+To build this package, do the following steps:
+
+    1. ./bootstrap.sh
+    2. ./configure ${CONFIGURE_FLAGS}
+    3. make
+
+To clean the repository run `./bootstrap.sh clean`.
+```
diff --git a/argument.cpp b/argument.cpp
new file mode 100644
index 0000000..a5402e6
--- /dev/null
+++ b/argument.cpp
@@ -0,0 +1,95 @@
+/**
+ * Copyright © 2018 IBM Corporation
+ *
+ * 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 "argument.hpp"
+
+#include <algorithm>
+#include <iostream>
+#include <iterator>
+
+namespace phosphor
+{
+namespace certs
+{
+namespace util
+{
+
+ArgumentParser::ArgumentParser(int argc, char** argv)
+{
+    auto option = 0;
+    while (-1 != (option = getopt_long(argc, argv, optionstr, options, NULL)))
+    {
+        if ((option == '?') || (option == 'h'))
+        {
+            usage(argv);
+            exit(-1);
+        }
+
+        auto i = &options[0];
+        while ((i->val != option) && (i->val != 0))
+        {
+            ++i;
+        }
+
+        if (i->val)
+        {
+            arguments[i->name] = (i->has_arg ? optarg : true_string);
+        }
+    }
+}
+
+const std::string& ArgumentParser::operator[](const std::string& opt)
+{
+    auto i = arguments.find(opt);
+    if (i == arguments.end())
+    {
+        return empty_string;
+    }
+    else
+    {
+        return i->second;
+    }
+}
+
+void ArgumentParser::usage(char** argv)
+{
+    std::cerr << "Usage: " << argv[0] << " [options]\n";
+    std::cerr << "Options:\n";
+    std::cerr << "    --help            Print this menu\n";
+    std::cerr << "    --type            certificate type\n";
+    std::cerr << "                      Valid types: Client,Server\n";
+    std::cerr << "    --endpoint        d-bus endpoint\n";
+    std::cerr << "    --path            certificate file path\n";
+    std::cerr << "    --unit=<name>     Optional systemd unit need to reload\n";
+    std::cerr << std::flush;
+}
+
+const option ArgumentParser::options[] = {
+    {"type", required_argument, nullptr, 't'},
+    {"endpoint", required_argument, nullptr, 'e'},
+    {"path", required_argument, nullptr, 'p'},
+    {"unit", optional_argument, nullptr, 'u'},
+    {"help", no_argument, nullptr, 'h'},
+    {0, 0, 0, 0},
+};
+
+const char* ArgumentParser::optionstr = "tepuh?";
+
+const std::string ArgumentParser::true_string = "true";
+const std::string ArgumentParser::empty_string = "";
+
+} // namespace util
+} // namespace certs
+} // namespace phosphor
diff --git a/argument.hpp b/argument.hpp
new file mode 100644
index 0000000..af5ac46
--- /dev/null
+++ b/argument.hpp
@@ -0,0 +1,45 @@
+#pragma once
+
+#include <getopt.h>
+
+#include <map>
+#include <string>
+
+namespace phosphor
+{
+namespace certs
+{
+namespace util
+{
+
+/**
+ * @brief Class - Encapsulates parsing command line options and
+ *                populating arguments.
+ */
+class ArgumentParser
+{
+  public:
+    ArgumentParser(int argc, char** argv);
+    ArgumentParser() = delete;
+    ArgumentParser(const ArgumentParser&) = delete;
+    ArgumentParser(ArgumentParser&&) = default;
+    ArgumentParser& operator=(const ArgumentParser&) = delete;
+    ArgumentParser& operator=(ArgumentParser&&) = default;
+    ~ArgumentParser() = default;
+    const std::string& operator[](const std::string& opt);
+
+    static void usage(char** argv);
+
+    static const std::string true_string;
+    static const std::string empty_string;
+
+  private:
+    std::map<const std::string, std::string> arguments;
+
+    static const option options[];
+    static const char* optionstr;
+};
+
+} // namespace util
+} // namespace certs
+} // namespace phosphor
diff --git a/bootstrap.sh b/bootstrap.sh
new file mode 100755
index 0000000..50b75b7
--- /dev/null
+++ b/bootstrap.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+AUTOCONF_FILES="Makefile.in aclocal.m4 ar-lib autom4te.cache compile \
+        config.guess config.h.in config.sub configure depcomp install-sh \
+        ltmain.sh missing *libtool test-driver"
+
+case $1 in
+    clean)
+        test -f Makefile && make maintainer-clean
+        for file in ${AUTOCONF_FILES}; do
+            find -name "$file" | xargs -r rm -rf
+        done
+        exit 0
+        ;;
+esac
+
+autoreconf -i
+echo 'Run "./configure ${CONFIGURE_FLAGS} && make"'
diff --git a/configure.ac b/configure.ac
new file mode 100644
index 0000000..e89eea5
--- /dev/null
+++ b/configure.ac
@@ -0,0 +1,23 @@
+# Initialization
+AC_PREREQ([2.69])
+AC_INIT([phosphor-certificate-manager], [1.0], [https://github.com/openbmc/phosphor-certificate-manager/issues])
+AC_LANG([C++])
+AC_CONFIG_HEADERS([config.h])
+AM_INIT_AUTOMAKE([subdir-objects -Wall -Werror foreign dist-xz])
+AM_SILENT_RULES([yes])
+
+# Checks for programs.
+AC_PROG_CXX
+AC_PROG_INSTALL
+AC_PROG_MAKE_SET
+
+# Checks for library functions
+LT_INIT # Required for systemd linking
+
+# Checks for typedefs, structures, and compiler characteristics.
+AX_CXX_COMPILE_STDCXX([17], [noext], [mandatory])
+AX_APPEND_COMPILE_FLAGS([-Wall -Werror], [CXXFLAGS])
+
+# Create configured output
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
diff --git a/mainapp.cpp b/mainapp.cpp
new file mode 100644
index 0000000..bbe2a85
--- /dev/null
+++ b/mainapp.cpp
@@ -0,0 +1,58 @@
+/**
+ * Copyright © 2018 IBM Corporation
+ *
+ * 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 "argument.hpp"
+
+#include <iostream>
+#include <string>
+
+static void ExitWithError(const char* err, char** argv)
+{
+    phosphor::certs::util::ArgumentParser::usage(argv);
+    std::cerr << std::endl;
+    std::cerr << "ERROR: " << err << std::endl;
+    exit(EXIT_FAILURE);
+}
+
+int main(int argc, char** argv)
+{
+    // Read arguments.
+    auto options = phosphor::certs::util::ArgumentParser(argc, argv);
+
+    // Parse arguments
+    auto type = std::move((options)["type"]);
+    if (type == phosphor::certs::util::ArgumentParser::empty_string)
+    {
+        ExitWithError("type not specified.", argv);
+    }
+
+    auto endpoint = std::move((options)["endpoint"]);
+    if (endpoint == phosphor::certs::util::ArgumentParser::empty_string)
+    {
+        ExitWithError("endpoint not specified.", argv);
+    }
+
+    auto path = std::move((options)["path"]);
+    if (path == phosphor::certs::util::ArgumentParser::empty_string)
+    {
+        ExitWithError("path not specified.", argv);
+    }
+
+    // unit is an optional parametr
+    auto unit = std::move((options)["unit"]);
+
+    return 0;
+}