summaryrefslogtreecommitdiffhomepage
path: root/plugin/npobject.c
blob: b5d99c6fb6c376b4d708fe18d0184ec621dadd86 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343

/*

  Copyright (c) 2009-2011 Samuel Lidén Borell <samuel@kodafritt.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 <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdint.h>

#include <npapi.h>
#include <npruntime.h>

#include "pluginutil.h"
#include "npobject.h"

// Prevent concurrent/recursive calls (and multiple windows appearing)
static bool pluginActive = false;

/* Object methods */
static NPObject *objAllocate(NPP npp, NPClass *aClass) {
    return malloc(sizeof(PluginObject));
}

static void objDeallocate(NPObject *npobj) {
    PluginObject *this = (PluginObject*)npobj;
    plugin_free(this->plugin);
    free(this);
}


static bool objHasMethod(NPObject *npobj, NPIdentifier ident) {
    PluginObject *this = (PluginObject*)npobj;
    char name[64];
    if (!copyIdentifierName(ident, name, sizeof(name)))
        return false;
    
    switch (this->plugin->type) {
        case PT_Version:
            return !strcmp(name, "GetVersion");
        case PT_Authentication:
        case PT_Signer:
            return !strcmp(name, "GetParam") || !strcmp(name, "SetParam") ||
                   !strcmp(name, "Reset") ||
                   !strcmp(name, "PerformAction") || !strcmp(name, "GetLastError");
        case PT_Regutil:
            return !strcmp(name, "GetParam") || !strcmp(name, "SetParam") ||
                   !strcmp(name, "InitRequest") || !strcmp(name, "CreateRequest") ||
                   !strcmp(name, "StoreCertificates") || !strcmp(name, "GetLastError");
        case PT_Webadmin:
            return !strcmp(name, "PerformAction") || !strcmp(name, "GetLastError");
        default:
            return false;
    }
}


static bool objInvokeSafe(PluginObject *this, const char *name,
                          const NPVariant *args, uint32_t argCount,
                          NPVariant *result) {
    switch (this->plugin->type) {
        case PT_Version:
            if (IS_CALL_0("GetVersion")) {
                char *version = version_getVersion(this->plugin);
                return convertStringZToVariant(version, result);
            }
            return false;
        case PT_Authentication:
        case PT_Signer:
            if (IS_CALL_1("GetParam", STRING)) {
                // Get parameter
                char *param = variantToStringZ(&args[0]);
                if (!param) return false;
                
                char *value = sign_getParam(this->plugin, param);
                
                free(param);
                return convertStringZToVariant(value, result);
            } else if (IS_CALL_2("SetParam", STRING, STRING)) {
                // Set parameter
                char *param = variantToStringZ(&args[0]);
                char *value = variantToStringZ(&args[1]);
                bool ok = (param && value);
                
                if (ok) {
                    sign_setParam(this->plugin, param, value);
                    INT32_TO_NPVARIANT((int32_t)this->plugin->lastError, *result);
                }
                
                free(param);
                free(value);
                
                return ok;
            } else if (IS_CALL_0("Reset")) {
                // Clear all parameters
                plugin_reset(this->plugin);
                
                this->plugin->lastError = BIDERR_OK;
                INT32_TO_NPVARIANT((int32_t)this->plugin->lastError, *result);
                
                return true;
            } else if (IS_CALL_1("PerformAction", STRING)) {
                // Perform action
                char *action = variantToStringZ(&args[0]);
                if (!action) return false;
                
                int ret = sign_performAction(this->plugin, action);
                
                free(action);
                INT32_TO_NPVARIANT((int32_t)ret, *result);
                return true;
            } else if (IS_CALL_0("GetLastError")) {
                // Get last error
                INT32_TO_NPVARIANT((int32_t)this->plugin->lastError, *result);
                return true;
            }
            return false;
        case PT_Regutil:
            if (IS_CALL_1("GetParam", STRING)) {
                // Get parameter. Seems to always return null
                this->plugin->lastError = RUERR_InvalidParameter;
                NULL_TO_NPVARIANT(*result);
                return true;
            } else if (IS_CALL_2("SetParam", STRING, STRING)) {
                // Set parameter
                char *param = variantToStringZ(&args[0]);
                char *value = variantToStringZ(&args[1]);
                bool ok = (param && value);
                
                if (ok) {
                    regutil_setParam(this->plugin, param, value);
                    INT32_TO_NPVARIANT((int32_t)this->plugin->lastError, *result);
                }
                
                free(param);
                free(value);
                
                return ok;
            } else if (IS_CALL_1("InitRequest", STRING)) {
                // Init request
                char *type = variantToStringZ(&args[0]);
                if (!type) return false;
                
                regutil_initRequest(this->plugin, type);
                
                free(type);
                INT32_TO_NPVARIANT((int32_t)this->plugin->lastError, *result);
                return true;
            } else if (IS_CALL_0("CreateRequest")) {
                // Create request
                char *value = regutil_createRequest(this->plugin);
                return convertStringZToVariant(value, result);
            } else if (IS_CALL_2("StoreCertificates", STRING, STRING)) {
                // Store a certificate chain
                const NPString *type_nps = &NPVARIANT_TO_STRING(args[0]);
                bool type_is_p7c = (type_nps->utf8length == 3 &&
                                    !strncmp(type_nps->utf8characters, "p7c", 3));
                char *certs = variantToStringZ(&args[1]);
                
                // TODO set the error code instead of just failing and throwing a script exception
                bool ok = (type_is_p7c && certs);
                
                if (ok) {
                    regutil_storeCertificates(this->plugin, certs);
                }
                
                free(certs);
                INT32_TO_NPVARIANT((int32_t)this->plugin->lastError, *result);
                return ok;
            } else if (IS_CALL_0("GetLastError")) {
                // Get last error
                INT32_TO_NPVARIANT((int32_t)this->plugin->lastError, *result);
                return true;
            }
            return false;
        case PT_Webadmin:
            if (IS_CALL_1("PerformAction", STRING)) {
                // Perform action
                
                // RenewPollDates isn't implemented, but is probably not
                // needed either. The purpose of that call is to download
                // version/expiry information through the browser, in case
                // such requests must be proxied
                this->plugin->lastError = BIDERR_InvalidAction;
                INT32_TO_NPVARIANT((int32_t)this->plugin->lastError, *result);
                return true;
            } else if (IS_CALL_0("GetLastError")) {
                // Get last error
                INT32_TO_NPVARIANT((int32_t)this->plugin->lastError, *result);
                return true;
            }
            return false;
        case PT_OldSigner:
            // Not implemented
            return false;
        default:
            return false;
    }
}


static bool objInvoke(NPObject *npobj, NPIdentifier ident,
                      const NPVariant *args, uint32_t argCount,
                      NPVariant *result) {
    PluginObject *this = (PluginObject*)npobj;
    char name[64];
    if (!copyIdentifierName(ident, name, sizeof(name)))
        return false;
    
    // Check argument lengths
    for (uint32_t i = 0; i < argCount; i++) {
        if (NPVARIANT_IS_STRING(args[i]) &&
            NPVARIANT_TO_STRING(args[i]).utf8length > 10*1024*1024) {
            // String is larger than 10 MiB
            return false;
        }
    }
    
    // Prevent recursive calls
    // TODO filter events in the main loop so the browser do anything
    //      except redraw it's window while a call is in progress.
    if (pluginActive) return false;
    pluginActive = true;
    
    bool ok = objInvokeSafe(this, name, args, argCount, result);
    
    pluginActive = false;
    return ok;
}

static bool objInvokeDefault(NPObject *npobj, const NPVariant *args,
                             uint32_t argCount, NPVariant *result) {
    return false;
}

static bool objHasProperty(NPObject *npobj, NPIdentifier name) {
    return false;
}

static bool objGetProperty(NPObject *npobj, NPIdentifier name,
                               NPVariant *result) {
    return false;
}

static bool objSetProperty(NPObject *npobj, NPIdentifier name,
                           const NPVariant *value) {
    return false;
}

static bool objRemoveProperty(NPObject *npobj, NPIdentifier name) {
    return false;
}

static bool objEnumerate(NPObject *npobj, NPIdentifier **value,
                         uint32_t *count) {
    return false;
}

static NPClass baseClass = {
    NP_CLASS_STRUCT_VERSION,
    objAllocate,
    objDeallocate,
    NULL,
    objHasMethod,
    objInvoke,
    objInvokeDefault,
    objHasProperty,
    objGetProperty,
    objSetProperty,
    objRemoveProperty,
    objEnumerate,
    NULL,
};


/* Object construction */
static NPObject *npobject_new(NPP instance, PluginType pluginType) {
    PluginObject *obj;

    obj = (PluginObject*)NPN_CreateObject(instance, &baseClass);
    if (!obj) return NULL;
    assert(obj->base._class != NULL);
    
    char *url = getDocumentURL(instance);
    char *hostname = getDocumentHostname(instance);
    char *ip = getDocumentIP(instance);

    obj->plugin = plugin_new(pluginType,
                             (url != NULL ? url : ""),
                             (hostname != NULL ? hostname : ""),
                             (ip != NULL ? ip : ""),
                             getWindowId(instance));
    free(ip);
    free(hostname);
    free(url);
    
    if (!obj->plugin) {
        NPN_ReleaseObject((NPObject*)obj);
        return NULL;
    }
    
    return (NPObject*)obj;
}

NPObject *npobject_fromMIME(NPP instance, NPMIMEType mimeType) {
    if (!strcmp(mimeType, MIME_VERSION)) {
        return npobject_new(instance, PT_Version);
    } else if (!strcmp(mimeType, MIME_AUTHENTICATION)) {
        return npobject_new(instance, PT_Authentication);
    } else if (!strcmp(mimeType, MIME_SIGNER)) {
        return npobject_new(instance, PT_Signer);
    } else if (!strcmp(mimeType, MIME_REGUTIL)) {
        return npobject_new(instance, PT_Regutil);
    } else if (!strcmp(mimeType, MIME_WEBADMIN)) {
        return npobject_new(instance, PT_Webadmin);
    } else if (!strcmp(mimeType, MIME_OLDSIGNER)) {
        return npobject_new(instance, PT_OldSigner);
    } else {
        return NULL;
    }
}