summaryrefslogtreecommitdiff
path: root/plugin/ipc.c
blob: a9e0546f92f143ea86a97c6873f5453c545506ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*

  Copyright (c) 2009-2010 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
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#define _BSD_SOURCE 1
#define _POSIX_C_SOURCE 200112
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

#include "../common/defines.h"
#include "../common/pipe.h"
#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";

#define PIPE_READ_END  0
#define PIPE_WRITE_END 1

typedef struct {
    FILE *in;
    FILE *out;

    pid_t child;
} PipeInfo;

static void openPipes(PipeInfo *pipeinfo, const char *argv[]) {
    int pipeIn[2];
    int pipeOut[2];
    
    if (pipe(pipeIn) == -1 || pipe(pipeOut) == -1) {
        perror(BINNAME ": Failed to create pipe");
        return;
    }
    
    pipeinfo->child = fork();
    if (pipeinfo->child == 0) {
        // Child process
        close(STDOUT_FILENO);
        close(STDIN_FILENO);
        close(pipeIn[PIPE_READ_END]);
        close(pipeOut[PIPE_WRITE_END]);
        dup2(pipeIn[PIPE_WRITE_END], STDOUT_FILENO);
        dup2(pipeOut[PIPE_READ_END], STDIN_FILENO);
        
        // These have been copied now
        //close(pipeIn[PIPE_WRITE_END]);
        //close(pipeOut[PIPE_READ_END]);
        
        execvp(mainBinary, (char *const *)argv);
        perror(BINNAME ": Failed to execute main binary");
        exit(1);
    } else {
        // Parent process
        close(pipeOut[PIPE_READ_END]);
        close(pipeIn[PIPE_WRITE_END]);
        
        pipeinfo->in = fdopen(pipeIn[PIPE_READ_END], "r");
        pipeinfo->out = fdopen(pipeOut[PIPE_WRITE_END], "w");
    }
}

static void openVersionPipes(PipeInfo *pipeinfo) {
    const char *argv[] = {
        mainBinary, versionOption, (char *)NULL,
    };
    openPipes(pipeinfo, argv);
}

static void openInteractivePipes(PipeInfo *pipeinfo, 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);
}

static BankIDError waitReply(PipeInfo *pipeinfo) {
    pipe_finishCommand(pipeinfo->out);
    
    pipe_waitData(pipeinfo->in);
    
    // Return error code
    return pipe_readInt(pipeinfo->in);
}

static void closePipes(PipeInfo *pipeinfo) {
    fclose(pipeinfo->out);
    fclose(pipeinfo->in);
    waitpid(pipeinfo->child, NULL, 0);
}


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);
    
    return strdup(buff);
}


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);
}

int sign_performAction_Authenticate(Plugin *plugin) {
    PipeInfo pipeinfo;
    
    openInteractivePipes(&pipeinfo, plugin);
    pipe_sendCommand(pipeinfo.out, PMC_Authenticate);
    
    sendSignCommon(pipeinfo, plugin);
    
    plugin->lastError = waitReply(&pipeinfo);
    plugin->info.auth.signature = pipe_readString(pipeinfo.in);
    closePipes(&pipeinfo);
    return plugin->lastError;
}

int sign_performAction_Sign(Plugin *plugin) {
    PipeInfo pipeinfo;
    
    openInteractivePipes(&pipeinfo, plugin);
    pipe_sendCommand(pipeinfo.out, PMC_Sign);
    
    sendSignCommon(pipeinfo, plugin);
    pipe_sendString(pipeinfo.out, plugin->info.sign.message);
    pipe_sendOptionalString(pipeinfo.out, plugin->info.sign.invisibleMessage);
    
    plugin->lastError = waitReply(&pipeinfo);
    plugin->info.auth.signature = pipe_readString(pipeinfo.in);
    closePipes(&pipeinfo);
    return plugin->lastError;
}