Explorar el Código

HPCC-23460 New ESP service to list the UserIDs of the gpg keys

- Add ListUserIDs method to ws_codesign service to list all unique
  User IDs of the keys

Signed-off-by: Yanrui Ma <yanrui.ma@lexisnexisrisk.com>
Yanrui Ma hace 5 años
padre
commit
1e989a54dc

+ 10 - 0
esp/scm/ws_codesign.ecm

@@ -34,9 +34,19 @@ ESPresponse [exceptions_inline] SignResponse
     string SignedText;
 };
 
+ESPrequest ListUserIDsRequest
+{
+};
+
+ESPresponse [exceptions_inline] ListUserIDsResponse
+{
+    ESParray<string> UserIDs;
+};
+
 ESPservice [auth_feature("CodeSignAccess:ACCESS"), version("1.0"), default_client_version("1.0"), exceptions_inline("./smc_xslt/exceptions.xslt")] ws_codesign
 {
     ESPmethod [auth_feature("CodeSignAccess:FULL"), client_xslt("/esp/xslt/codesign.xslt")] Sign(SignRequest, SignResponse);
+    ESPmethod [auth_feature("CodeSignAccess:READ")] ListUserIDs(ListUserIDsRequest, ListUserIDsResponse);
 };
 
 SCMexportdef(ws_codesign);

+ 67 - 0
esp/services/ws_codesign/ws_codesignService.cpp

@@ -115,3 +115,70 @@ bool Cws_codesignEx::onSign(IEspContext &context, IEspSignRequest &req, IEspSign
 
     return true;
 }
+
+const char* skipn(const char* str, char c, int n)
+{
+    for (int i = 0; i < n && str && *str; i++)
+    {
+        str = strchr(str, c);
+        if (!str)
+            break;
+        str++;
+    }
+    return str;
+}
+
+bool Cws_codesignEx::onListUserIDs(IEspContext &context, IEspListUserIDsRequest &req, IEspListUserIDsResponse &resp)
+{
+    StringBuffer output, errmsg;
+
+    int ret = runExternalCommand(output, errmsg, "gpg --version", nullptr);
+    if (ret != 0)
+        throw MakeStringException(-1, "Error running gpg: %s", errmsg.str());
+    bool isGPGv1 = strstr(output.str(), "gpg (GnuPG) 1.");
+
+    const char* START = "\nuid:";
+    if (isGPGv1)
+        START = "\nsec:";
+    int startlen = strlen(START);
+    const int SKIP = 8;
+    output.clear().append("\n");
+    errmsg.clear();
+    ret = runExternalCommand(output, errmsg, "gpg --list-secret-keys --with-colon", nullptr);
+    if (ret != 0)
+        throw MakeStringException(-1, "Error running gpg: %s", errmsg.str());
+    const char* line = output.str();
+    StringArray uids;
+    while (line && *line)
+    {
+        line = strstr(line, START);
+        if (!line)
+            break;
+        line += startlen;
+        line = skipn(line, ':', SKIP);
+        if (!line || !*line)
+            break;
+        const char* uid_s = line;
+        while (*line != '\0' && *line != ':')
+            line++;
+        if (line > uid_s)
+        {
+            StringBuffer uid(line - uid_s, uid_s);
+            uid.trim();
+            if (uid.length() > 0)
+                uids.append(uid.str());
+        }
+    }
+    uids.sortAscii(false);
+    const char* current = "";
+    StringArray& respuserids = resp.getUserIDs();
+    for (int i = 0; i < uids.length(); i++)
+    {
+        if (strcmp(uids.item(i), current) != 0)
+        {
+            current = uids.item(i);
+            respuserids.append(current);
+        }
+    }
+    return true;
+}

+ 1 - 0
esp/services/ws_codesign/ws_codesignService.hpp

@@ -32,6 +32,7 @@ public:
     virtual ~Cws_codesignEx();
     virtual void init(IPropertyTree *cfg, const char *process, const char *service);
     virtual bool onSign(IEspContext &context, IEspSignRequest &req, IEspSignResponse &resp);
+    virtual bool onListUserIDs(IEspContext &context, IEspListUserIDsRequest &req, IEspListUserIDsResponse &resp);
 };
 
 #endif // _WS_CODESIGNSERVICE_HPP_