diff options
author | Samuel Lidén Borell <samuel@slbdata.se> | 2011-04-22 18:03:10 +0200 |
---|---|---|
committer | Samuel Lidén Borell <samuel@slbdata.se> | 2011-04-22 18:03:10 +0200 |
commit | a6b0b406279723177bd7d4e6e35e60b62c219f1a (patch) | |
tree | 68837f3e126518e5619e2c3e23dade5fc5b13531 | |
parent | d852ae1be2359f3335ed57f21ee5f9b17d952c78 (diff) | |
download | fribid-a6b0b406279723177bd7d4e6e35e60b62c219f1a.tar.gz fribid-a6b0b406279723177bd7d4e6e35e60b62c219f1a.tar.bz2 fribid-a6b0b406279723177bd7d4e6e35e60b62c219f1a.zip |
Simplify the IPC code
This patch simplifies the IPC code and makes the site information (URL,
hostname, ip) available to all commands.
-rw-r--r-- | client/main.c | 55 | ||||
-rwxr-xr-x | client/sendreq.sh | 11 | ||||
-rwxr-xr-x | client/testsign.sh | 16 | ||||
-rw-r--r-- | common/defines.h | 4 | ||||
-rw-r--r-- | common/pipe.h | 3 | ||||
-rw-r--r-- | plugin/ipc.c | 71 |
6 files changed, 78 insertions, 82 deletions
diff --git a/client/main.c b/client/main.c index 0c83cc3..f4ec55f 100644 --- a/client/main.c +++ b/client/main.c @@ -57,22 +57,27 @@ static void notifyCallback(Token *token, TokenChange change) { } /** - * pipeData is called when the plugin has sent some data. - * This happens when one of the Javascript methods of an - * plugin object is called. + * Called when a command is being sent from the plugin. */ -void pipeData() { - PipeCommand command = pipe_readCommand(stdin); +void pipeCommand(PipeCommand command, const char *url, const char *hostname, + const char *ip) { switch (command) { + case PC_GetVersion: { + char *versionString = bankid_getVersion(); + + pipe_sendString(stdout, versionString); + free(versionString); + pipe_flush(stdout); + + platform_leaveMainloop(); + break; + } case PC_Authenticate: case PC_Sign: { char *challenge = pipe_readString(stdin); int32_t serverTime = pipe_readInt(stdin); free(pipe_readOptionalString(stdin)); // Just ignore the policies list for now char *subjectFilter = pipe_readOptionalString(stdin); - char *url = pipe_readString(stdin); - char *hostname = pipe_readString(stdin); - char *ip = pipe_readString(stdin); char *message = NULL, *invisibleMessage = NULL; if (command == PC_Sign) { message = pipe_readString(stdin); @@ -184,9 +189,6 @@ void pipeData() { free(message); free(invisibleMessage); free(challenge); - free(url); - free(hostname); - free(ip); pipe_sendInt(stdout, error); pipe_sendString(stdout, (signature ? signature : "")); @@ -304,19 +306,21 @@ void pipeData() { } /** - * Processes some command line options that neither require a GUI or the NSS - * libraries. + * pipeData is called when the plugin has sent some data. + * This happens when one of the Javascript methods of an + * plugin object is called. */ -int process_non_ui_args(int argc, char **argv) { - for (int i = 1; i < argc; i++) { - if (!strcmp(argv[i], "--internal--bankid-version-string")) { - char *versionString = bankid_getVersion(); - printf("%s", versionString); - free(versionString); - return 1; - } - } - return 0; +void pipeData() { + PipeCommand command = pipe_readCommand(stdin); + char *url = pipe_readString(stdin); + char *hostname = pipe_readString(stdin); + char *ip = pipe_readString(stdin); + + pipeCommand(command, url, hostname, ip); + + free(ip); + free(hostname); + free(url); } int main(int argc, char **argv) { @@ -326,11 +330,6 @@ int main(int argc, char **argv) { platform_seedRandom(); bankid_checkVersionValidity(); - /* Parse command line and set up the UI component */ - if (process_non_ui_args(argc, argv)) { - return 0; - } - error = secmem_init_pool(); if (error) { fprintf(stderr, BINNAME ": could not initialize secure memory"); diff --git a/client/sendreq.sh b/client/sendreq.sh index 24dc8d9..6290e25 100755 --- a/client/sendreq.sh +++ b/client/sendreq.sh @@ -3,7 +3,7 @@ sendint() { echo "$*;"; } sendstring() { echo "${#1};$1"; } -CreateRequest() { sendint 3; } +CreateRequest() { sendint 4; } MoreData() { sendint 1; } EndOfData() { sendint 0; } @@ -11,8 +11,11 @@ EndOfData() { sendint 0; } { -#### Send request #### +# Send command header CreateRequest +sendstring 'https://example.com/' # URL +sendstring 'example.com' # Hostname +sendstring '198.51.100.200' # IP of example.com # Password policy sendint 12 # Minimum length @@ -40,8 +43,8 @@ sendstring true echo 'hack' -} | valgrind --leak-check=no -q ./sign --internal--ipc=8 | tr ';' '\n' | { -#} | ./sign --internal--ipc=8 | tr ';' '\n' | { +} | valgrind --leak-check=no -q ./sign --internal--ipc=9 | tr ';' '\n' | { +#} | ./sign --internal--ipc=9 | tr ';' '\n' | { #### Parse response #### read error diff --git a/client/testsign.sh b/client/testsign.sh index 45c9bc3..421ea9c 100755 --- a/client/testsign.sh +++ b/client/testsign.sh @@ -3,13 +3,16 @@ sendint() { echo "$*;"; } sendstring() { echo "${#1};$1"; } -SignCommand() { sendint 2; } +SignCommand() { sendint 3; } { -#### Send sign command #### +# Send command header SignCommand +sendstring 'https://example.com/' # URL +sendstring 'example.com' # Hostname +sendstring '198.51.100.200' # IP of example.com # Send common data sendstring 'MTIzNDU2Nzg5' # nonce @@ -17,10 +20,6 @@ sendint 0 # server time (optional) sendstring '' # policies (optional) sendstring '' # subject filter (optional) -sendstring 'https://example.com/' # URL -sendstring 'example.com' # Hostname -sendstring '198.51.100.200' # IP of example.com - # Send data to be signed sendstring 'aGkK' # visible message sendstring '' # hidden data (optional) @@ -28,9 +27,8 @@ sendstring '' # hidden data (optional) # Prevent EOF echo 'hack' - -#} | valgrind --leak-check=no -q ./sign --internal--ipc=8 | tr ';' '\n' | { -} | ./sign --internal--ipc=8 | tr ';' '\n' | { +} | valgrind --leak-check=no -q ./sign --internal--ipc=9 | tr ';' '\n' | { +#} | ./sign --internal--ipc=9 | tr ';' '\n' | { #### Parse response #### read error diff --git a/common/defines.h b/common/defines.h index 20b2188..0358c0b 100644 --- a/common/defines.h +++ b/common/defines.h @@ -1,6 +1,6 @@ /* - Copyright (c) 2009-2010 Samuel Lidén Borell <samuel@slbdata.se> + Copyright (c) 2009-2011 Samuel Lidén Borell <samuel@slbdata.se> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -32,7 +32,7 @@ #define BINNAME "fribid" #define RELEASE_TIME 1292676673 -#define IPCVERSION "8" +#define IPCVERSION "9" #define EMULATED_VERSION "4.15.0.14" #define DNSVERSION "2" diff --git a/common/pipe.h b/common/pipe.h index bf8bca6..7b28212 100644 --- a/common/pipe.h +++ b/common/pipe.h @@ -29,7 +29,8 @@ // Commands to the main program typedef enum { - PC_Authenticate = 1, + PC_GetVersion = 1, + PC_Authenticate, PC_Sign, PC_CreateRequest, PC_StoreCertificates, diff --git a/plugin/ipc.c b/plugin/ipc.c index 04b06eb..07ee51c 100644 --- a/plugin/ipc.c +++ b/plugin/ipc.c @@ -37,7 +37,6 @@ #include "plugin.h" static const char mainBinary[] = SIGNING_EXECUTABLE; -static const char versionOption[] = "--internal--bankid-version-string"; static const char ipcOption[] = "--internal--ipc=" IPCVERSION; static const char windowIdOption[] = "--internal--window-id"; @@ -51,7 +50,7 @@ typedef struct { pid_t child; } PipeInfo; -static void openPipes(PipeInfo *pipeinfo, const char *argv[]) { +static void openPipesWithArgs(PipeInfo *pipeinfo, const char *argv[]) { int pipeIn[2]; int pipeOut[2]; @@ -87,21 +86,22 @@ static void openPipes(PipeInfo *pipeinfo, const char *argv[]) { } } -static void openVersionPipes(PipeInfo *pipeinfo) { - const char *argv[] = { - mainBinary, versionOption, (char *)NULL, - }; - openPipes(pipeinfo, argv); -} - -static void openInteractivePipes(PipeInfo *pipeinfo, Plugin *plugin) { +static void openPipes(PipeInfo *pipeinfo, const Plugin *plugin) { char windowId[21]; // This holds a native window id (such as an XID) const char *argv[] = { mainBinary, ipcOption, windowIdOption, windowId, (char *)NULL, }; snprintf(windowId, 21, "%ld", plugin->windowId); - openPipes(pipeinfo, argv); + openPipesWithArgs(pipeinfo, argv); +} + +static void sendHeader(PipeInfo *pipeinfo, const Plugin *plugin, + PipeCommand command) { + pipe_sendCommand(pipeinfo->out, command); + pipe_sendString(pipeinfo->out, plugin->url); + pipe_sendString(pipeinfo->out, plugin->hostname); + pipe_sendString(pipeinfo->out, plugin->ip); } static BankIDError waitReply(PipeInfo *pipeinfo) { @@ -121,36 +121,31 @@ static void closePipes(PipeInfo *pipeinfo) { char *version_getVersion(Plugin *plugin) { - char buff[1000]; PipeInfo pipeinfo; - openVersionPipes(&pipeinfo); - if (fgets(buff, sizeof(buff), pipeinfo.in) == NULL) { - buff[0] = '\0'; - } - closePipes(&pipeinfo); + openPipes(&pipeinfo, plugin); + sendHeader(&pipeinfo, plugin, PC_GetVersion); + pipe_finishCommand(pipeinfo.out); - return strdup(buff); + char *version = pipe_readString(pipeinfo.in); + closePipes(&pipeinfo); + return version; } -static void sendSignCommon(PipeInfo pipeinfo, Plugin *plugin) { - pipe_sendString(pipeinfo.out, plugin->info.auth.challenge); - pipe_sendInt(pipeinfo.out, plugin->info.auth.serverTime); - pipe_sendOptionalString(pipeinfo.out, plugin->info.auth.policys); - pipe_sendOptionalString(pipeinfo.out, plugin->info.auth.subjectFilter); - pipe_sendString(pipeinfo.out, plugin->url); - pipe_sendString(pipeinfo.out, plugin->hostname); - pipe_sendString(pipeinfo.out, plugin->ip); +static void sendSignCommon(PipeInfo *pipeinfo, const Plugin *plugin) { + pipe_sendString(pipeinfo->out, plugin->info.auth.challenge); + pipe_sendInt(pipeinfo->out, plugin->info.auth.serverTime); + pipe_sendOptionalString(pipeinfo->out, plugin->info.auth.policys); + pipe_sendOptionalString(pipeinfo->out, plugin->info.auth.subjectFilter); } int sign_performAction_Authenticate(Plugin *plugin) { PipeInfo pipeinfo; - openInteractivePipes(&pipeinfo, plugin); - pipe_sendCommand(pipeinfo.out, PC_Authenticate); - - sendSignCommon(pipeinfo, plugin); + openPipes(&pipeinfo, plugin); + sendHeader(&pipeinfo, plugin, PC_Authenticate); + sendSignCommon(&pipeinfo, plugin); plugin->lastError = waitReply(&pipeinfo); plugin->info.auth.signature = pipe_readString(pipeinfo.in); @@ -161,10 +156,10 @@ int sign_performAction_Authenticate(Plugin *plugin) { int sign_performAction_Sign(Plugin *plugin) { PipeInfo pipeinfo; - openInteractivePipes(&pipeinfo, plugin); - pipe_sendCommand(pipeinfo.out, PC_Sign); + openPipes(&pipeinfo, plugin); + sendHeader(&pipeinfo, plugin, PC_Sign); + sendSignCommon(&pipeinfo, plugin); - sendSignCommon(pipeinfo, plugin); pipe_sendString(pipeinfo.out, plugin->info.sign.message); pipe_sendOptionalString(pipeinfo.out, plugin->info.sign.invisibleMessage); @@ -177,10 +172,10 @@ int sign_performAction_Sign(Plugin *plugin) { char *regutil_createRequest(Plugin *plugin) { PipeInfo pipeinfo; - openInteractivePipes(&pipeinfo, plugin); - pipe_sendCommand(pipeinfo.out, PC_CreateRequest); - // TODO should send URL here, maybe it should be a common parameter? + openPipes(&pipeinfo, plugin); + sendHeader(&pipeinfo, plugin, PC_CreateRequest); + // Send password policy pipe_sendInt(pipeinfo.out, plugin->info.regutil.input.minPasswordLength); pipe_sendInt(pipeinfo.out, plugin->info.regutil.input.minPasswordNonDigits); pipe_sendInt(pipeinfo.out, plugin->info.regutil.input.minPasswordDigits); @@ -218,8 +213,8 @@ char *regutil_createRequest(Plugin *plugin) { void regutil_storeCertificates(Plugin *plugin, const char *certs) { PipeInfo pipeinfo; - openInteractivePipes(&pipeinfo, plugin); - pipe_sendCommand(pipeinfo.out, PC_StoreCertificates); + openPipes(&pipeinfo, plugin); + sendHeader(&pipeinfo, plugin, PC_StoreCertificates); pipe_sendOptionalString(pipeinfo.out, certs); |