From 6ab4e8c86981e38441e523efd5ad385605105515 Mon Sep 17 00:00:00 2001 From: "Endi S. Dewata" Date: Thu, 27 Feb 2025 11:05:01 -0600 Subject: [PATCH] Refactor RA_Client::Execute() The RA_Client::Execute() has been converted into Java code in TPSClientCLI.execute(). The original C code has been moved into tpsclient.cpp. The TPSClientCLI.invokeOperation() has been added to call RA_Client::InvokeOperation(). --- .../netscape/cmstools/tps/TPSClientCLI.java | 73 +++++++++++- .../main/native/tpsclient/src/CMakeLists.txt | 1 + .../tpsclient/src/include/main/RA_Client.h | 1 - .../native/tpsclient/src/main/RA_Client.cpp | 101 ---------------- .../tpsclient/src/main/TPSClientCLI.cpp | 109 ++++++++++++++++++ .../tpsclient/tools/raclient/tpsclient.cpp | 98 +++++++++++++++- 6 files changed, 277 insertions(+), 106 deletions(-) create mode 100644 base/tools/src/main/native/tpsclient/src/main/TPSClientCLI.cpp diff --git a/base/tools/src/main/java/com/netscape/cmstools/tps/TPSClientCLI.java b/base/tools/src/main/java/com/netscape/cmstools/tps/TPSClientCLI.java index 8a58d445f92..2563c5d04eb 100644 --- a/base/tools/src/main/java/com/netscape/cmstools/tps/TPSClientCLI.java +++ b/base/tools/src/main/java/com/netscape/cmstools/tps/TPSClientCLI.java @@ -5,7 +5,12 @@ // package com.netscape.cmstools.tps; +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + import org.apache.commons.cli.CommandLine; +import org.apache.commons.lang3.StringUtils; import org.dogtagpki.cli.CommandCLI; import com.netscape.cmstools.cli.MainCLI; @@ -33,7 +38,33 @@ public void printHelp() { formatter.printHelp(getFullName() + " [OPTIONS...]", options); } - public native void execute() throws Exception; + public Map parse(String line) { + + logger.info("Parsing " + line); + + Map map = new HashMap<>(); + + for (String param : line.split(" ")) { + + String[] parts = param.split("=", 2); + String key = parts[0]; + String value = parts[1]; + logger.info("- " + key + ": " + value); + + map.put(key, value); + } + + return map; + } + + public native long createClient() throws Exception; + public native void removeClient(long client) throws Exception; + + public native void invokeOperation( + long client, + String op, + Map params) + throws Exception; @Override public void execute(CommandLine cmd) throws Exception { @@ -41,6 +72,44 @@ public void execute(CommandLine cmd) throws Exception { MainCLI mainCLI = (MainCLI) getRoot(); mainCLI.init(); - execute(); + System.out.println("TPS Client"); + System.out.println("'op=help' for Help"); + + long client = createClient(); + + try (Scanner input = new Scanner(System.in)) { + while (true) { + System.out.print("Command> "); + System.out.flush(); + + String line = input.nextLine(); + + if (line == null) { + break; + } + + System.out.println(line); + + if (StringUtils.isBlank(line)) { + continue; + } + + if (line.startsWith("#")) { + continue; + } + + Map params = parse(line); + String op = params.get("op"); + + if ("exit".equals(op)) { + break; + } + + invokeOperation(client, op, params); + } + + } finally { + removeClient(client); + } } } diff --git a/base/tools/src/main/native/tpsclient/src/CMakeLists.txt b/base/tools/src/main/native/tpsclient/src/CMakeLists.txt index 0729806252a..48eebecf497 100644 --- a/base/tools/src/main/native/tpsclient/src/CMakeLists.txt +++ b/base/tools/src/main/native/tpsclient/src/CMakeLists.txt @@ -41,6 +41,7 @@ set(tps_library_SRCS main/RA_Token.cpp main/Memory.cpp main/AuthParams.cpp + main/TPSClientCLI.cpp apdu/APDU.cpp apdu/Unblock_Pin_APDU.cpp apdu/Create_Object_APDU.cpp diff --git a/base/tools/src/main/native/tpsclient/src/include/main/RA_Client.h b/base/tools/src/main/native/tpsclient/src/include/main/RA_Client.h index a9061ec3356..b5b704e44eb 100644 --- a/base/tools/src/main/native/tpsclient/src/include/main/RA_Client.h +++ b/base/tools/src/main/native/tpsclient/src/include/main/RA_Client.h @@ -68,7 +68,6 @@ class RA_Client int OpExit(NameValueSet *set); public: void Debug(const char *func_name, const char *fmt, ...); - void Execute(); void InvokeOperation(char *op, NameValueSet *set); public: RA_Token m_token; diff --git a/base/tools/src/main/native/tpsclient/src/main/RA_Client.cpp b/base/tools/src/main/native/tpsclient/src/main/RA_Client.cpp index c4187d6d862..42a36dad849 100644 --- a/base/tools/src/main/native/tpsclient/src/main/RA_Client.cpp +++ b/base/tools/src/main/native/tpsclient/src/main/RA_Client.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include "prinrval.h" #include "prmem.h" @@ -85,13 +84,6 @@ RA_Client::~RA_Client () } } -static void -PrintHeader () -{ - printf ("Registration Authority Client\n"); - printf ("'op=help' for Help\n"); -} - static void Output (const char *fmt, ...) { @@ -103,12 +95,6 @@ Output (const char *fmt, ...) va_end (ap); } -static void -PrintPrompt () -{ - printf ("Command>"); -} - static void OutputSuccess (const char *fmt, ...) { @@ -131,28 +117,6 @@ OutputError (const char *fmt, ...) va_end (ap); } -static int -ReadLine (char *buf, int len) -{ - char *cur = buf; - - while (1) - { - *cur = getchar (); - if (*cur == '\r') - { - continue; - } - if (*cur == '\n') - { - *cur = '\0'; - return 1; - } - cur++; - } - return 0; -} - void RA_Client::Debug (const char *func_name, const char *fmt, ...) { @@ -1575,68 +1539,3 @@ RA_Client::InvokeOperation (char *op, NameValueSet * params) (end - start) / 1000); } } - -/** - * Execute RA client. - */ -void -RA_Client::Execute () -{ - char line[1024]; - int rc; - char *op; - int done = 0; - char *lasts = NULL; - - /* start main loop */ - PrintHeader (); - while (!done) - { - PrintPrompt (); - rc = ReadLine (line, 1024); - printf ("%s\n", line); - if (rc <= 0) - { - break; /* exit if no more line */ - } - if (line[0] == '#') - { - continue; /* ignore comment line */ - } - /* format: 'op=cmd ' */ - NameValueSet *params = NameValueSet::Parse (line, " "); - if (params == NULL) - { - continue; - } - op = params->GetValue ("op"); - if (op == NULL) - { - /* user did not type op= */ - op = PL_strtok_r (line, " ", &lasts); - if (op == NULL) - continue; - } - if (strcmp (op, "exit") == 0) - { - done = 1; - } - else - { - InvokeOperation (op, params); - } - if (params != NULL) - { - delete params; - params = NULL; - } - } -} /* Execute */ - -extern "C" JNIEXPORT void JNICALL -Java_com_netscape_cmstools_tps_TPSClientCLI_execute -(JNIEnv* env, jclass clazz) -{ - RA_Client client; - client.Execute(); -} diff --git a/base/tools/src/main/native/tpsclient/src/main/TPSClientCLI.cpp b/base/tools/src/main/native/tpsclient/src/main/TPSClientCLI.cpp new file mode 100644 index 00000000000..3bcefec0505 --- /dev/null +++ b/base/tools/src/main/native/tpsclient/src/main/TPSClientCLI.cpp @@ -0,0 +1,109 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, +// Boston, MA 02110-1301 USA +// +// Copyright (C) 2007 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- + +#include + +#include "main/NameValueSet.h" +#include "main/RA_Client.h" + +extern "C" JNIEXPORT jlong JNICALL +Java_com_netscape_cmstools_tps_TPSClientCLI_createClient +(JNIEnv* env, jclass clazz) { + RA_Client* client = new RA_Client(); + return (jlong) client; +} + +extern "C" JNIEXPORT void JNICALL +Java_com_netscape_cmstools_tps_TPSClientCLI_removeClient +(JNIEnv* env, jclass clazz, jlong client) { + RA_Client* cclient = (RA_Client*) client; + delete cclient; +} + +extern "C" JNIEXPORT void JNICALL +Java_com_netscape_cmstools_tps_TPSClientCLI_invokeOperation +(JNIEnv* env, jclass clazz, jlong client, jstring op, jobject params) { + + // Map + jclass mapClass = env->FindClass("java/util/Map"); + jmethodID keySetMethod = env->GetMethodID( + mapClass, + "keySet", + "()Ljava/util/Set;"); + jmethodID getMethod = env->GetMethodID( + mapClass, + "get", + "(Ljava/lang/Object;)Ljava/lang/Object;"); + + // Set + jclass setClass = env->FindClass("java/util/Set"); + jmethodID iteratorMethod = env->GetMethodID( + setClass, + "iterator", + "()Ljava/util/Iterator;"); + + // Iterator + jclass iteratorClass = env->FindClass("java/util/Iterator"); + jmethodID hasNextMethod = env->GetMethodID( + iteratorClass, + "hasNext", + "()Z"); + jmethodID nextMethod = env->GetMethodID( + iteratorClass, + "next", + "()Ljava/lang/Object;"); + + RA_Client* cclient = (RA_Client*) client; + char* cop = (char*) env->GetStringUTFChars(op, NULL); + NameValueSet *set = new NameValueSet(); + + // Set keys = params.keySet(); + jobject keys = env->CallObjectMethod(params, keySetMethod); + + // Iterator iterator = keys.iterator(); + jobject iterator = env->CallObjectMethod(keys, iteratorMethod); + + while (true) { + // boolean hasNext = iterator.hasNext(); + jboolean hasNext = env->CallBooleanMethod(iterator, hasNextMethod); + + if (!hasNext) { + break; + } + + // String key = iterator.next(); + jstring key = (jstring) env->CallObjectMethod(iterator, nextMethod); + + // String value = params.get(key); + jstring value = (jstring) env->CallObjectMethod(params, getMethod, key); + + char* ckey = (char*) env->GetStringUTFChars(key, NULL); + char* cvalue = (char*) env->GetStringUTFChars(value, NULL); + + set->Add(ckey, cvalue); + + env->ReleaseStringUTFChars(value, cvalue); + env->ReleaseStringUTFChars(key, ckey); + } + + cclient->InvokeOperation(cop, set); + + env->ReleaseStringUTFChars(op, cop); +} diff --git a/base/tools/src/main/native/tpsclient/tools/raclient/tpsclient.cpp b/base/tools/src/main/native/tpsclient/tools/raclient/tpsclient.cpp index 511dca54ed7..cbddef6320a 100644 --- a/base/tools/src/main/native/tpsclient/tools/raclient/tpsclient.cpp +++ b/base/tools/src/main/native/tpsclient/tools/raclient/tpsclient.cpp @@ -22,11 +22,104 @@ #include #include +#include "plstr.h" #include "pk11func.h" #include "nss.h" #include "main/RA_Client.h" +void +PrintHeader () +{ + printf ("Registration Authority Client\n"); + printf ("'op=help' for Help\n"); +} + +void +PrintPrompt () +{ + printf ("Command> "); +} + +int +ReadLine (char *buf, int len) +{ + char *cur = buf; + + while (1) + { + *cur = getchar (); + if (*cur == '\r') + { + continue; + } + if (*cur == '\n') + { + *cur = '\0'; + return 1; + } + cur++; + } + return 0; +} + +/** + * Execute RA client. + */ +void +Execute (RA_Client* client) +{ + char line[1024]; + int rc; + char *op; + int done = 0; + char *lasts = NULL; + + /* start main loop */ + PrintHeader (); + while (!done) + { + PrintPrompt (); + rc = ReadLine (line, 1024); + printf ("%s\n", line); + if (rc <= 0) + { + break; /* exit if no more line */ + } + if (line[0] == '#') + { + continue; /* ignore comment line */ + } + /* format: 'op=cmd ' */ + NameValueSet *params = NameValueSet::Parse (line, " "); + if (params == NULL) + { + continue; + } + op = params->GetValue ("op"); + if (op == NULL) + { + /* user did not type op= */ + op = PL_strtok_r (line, " ", &lasts); + if (op == NULL) + continue; + } + if (strcmp (op, "exit") == 0) + { + done = 1; + } + else + { + client->InvokeOperation (op, params); + } + if (params != NULL) + { + delete params; + params = NULL; + } + } +} /* Execute */ + char * ownPasswd (PK11SlotInfo * slot, PRBool retry, void *arg) { @@ -84,8 +177,9 @@ main (int argc, char *argv[]) } /* Start RA Client */ - RA_Client client; - client.Execute (); + RA_Client* client = new RA_Client(); + Execute(client); + delete client; /* Shutdown NSS and NSPR */ NSS_Shutdown ();