-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmozplugger.c
3138 lines (2780 loc) · 82.9 KB
/
mozplugger.c
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This file is part of mozplugger a fork of plugger, for list of developers
* see the README file.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdarg.h>
#include <time.h>
#include <utime.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef HAVE_GETPWUID
#include <pwd.h> /* For alternative way to find HOME dir */
#endif
#define XP_UNIX
#include "npapi.h"
#include "npruntime.h"
#include "npn_func_tab.h"
#include "npp_func_tab.h"
#include "npn_funcs.h"
#include "npp_funcs.h"
#include "np_funcs.h"
#include "mozplugger.h"
#include "debug.h"
#include "npn-get-helpers.h"
#include "cmd_flags.h"
#include "scriptable_obj.h"
#include "pipe_msg.h"
#ifndef __GNUC__
#define __inline
#endif
#define AUTO_UPDATE
#define CHUNK_SIZE (8192)
/**
* Element of linked list of commands created when parsing config file
*/
typedef struct command
{
int flags;
const char * cmd;
const char * winname;
const char * fmatchStr;
struct command * pNext;
} command_t;
/**
* Element of fixed size array created when parsing NPP_New call
*/
typedef struct argument
{
char *name;
char *value;
} argument_t;
/**
* Data associated with an instance of an embed object, can be more than
* one
*/
typedef struct data
{
Display * display;
Window window;
uint32_t width;
uint32_t height;
pid_t pid;
int commsPipeFd;
int repeats;
command_t * command; /**< command to execute */
unsigned int mode_flags; /**< flags associated with browser calls */
char *mimetype;
char *href; /**< If QT this is set to handle special case */
char *url; /**< The URL */
char browserCantHandleIt; /**< Is set if browser cant handle protocol */
char *urlFragment;
int tmpFileFd; /**< File descriptor of temp file */
const char * tmpFileName; /**< Name of the temp file */
int tmpFileSize; /**< Size of temp file so far */
char autostart;
char autostartNotSeen;
int num_arguments;
struct argument *args;
} data_t;
/**
* Element of linked list of mimetypes created when parsing config file
*/
typedef struct mimetype
{
const char * type;
struct mimetype * pNext;
} mimetype_t;
/**
* Element of linked list of handlers created when parsing config file
*/
typedef struct handle
{
mimetype_t * types;
command_t * cmds;
struct handle * pNext;
} handler_t;
/**
* Global variables
*/
static char errMsg[512] = {0};
static handler_t * g_handlers = 0;
static const char * g_pluginName = "MozPlugger dummy Plugin";
static const char * g_version = VERSION;
static const char * g_linker = NULL;
static const char * g_controller = NULL;
static const char * g_helper = NULL;
static char staticPool[MAX_STATIC_MEMORY_POOL];
static int staticPoolIdx = 0;
/**
* Wrapper for putenv(). Instead of writing to the envirnoment, the envirnoment
* variables are written to a buffer.
*
* @param[in,out] buffer The buffer where the environment variables are written
* @param[in] bufLen The length of the buffer
* @param[in] offset The current position in the buffer
* @param[in] var The name of the environment variable
* @param[in] value The value of the environment variable
*
* @return The new offset
*/
static int my_putenv(char *buffer, int bufLen, int offset, const char *var,
const char *value)
{
if(value)
{
const int len = strlen(var) + strlen(value) + 2;
if (offset + len >= bufLen)
{
D("Buffer overflow in putenv(%s=%s) offset=%i, bufLen=%i\n",
var, value, offset, bufLen);
}
else
{
snprintf(&buffer[offset], len, "%s=%s", var, value);
putenv(&buffer[offset]);
offset += len;
}
}
else
{
D("putenv did nothing, no value for %s\n", var);
}
return offset;
}
/**
* putenv with a unsigned value
*
* @param[in,out] buffer The buffer where the environment variables are written
* @param[in] bufLen The length of the buffer
* @param[in] offset The current position in the buffer
* @param[in] var The name of the environment variable
* @param[in] value The value of the environment variable
*
* @return The new offset
*/
static int my_putenv_unsigned(char *buffer, int bufLen, int offset,
const char *var, unsigned long value)
{
char temp[50];
snprintf(temp, sizeof(temp), "%lu", value);
return my_putenv(buffer, bufLen, offset, var, temp);
}
/**
* putenv with a hex value
*
* @param[in,out] buffer The buffer where the environment variables are written
* @param[in] bufLen The length of the buffer
* @param[in] offset The current position in the buffer
* @param[in] var The name of the environment variable
* @param[in] value The value of the environment variable
*
* @return The new offset
*/
static int my_putenv_hex(char *buffer, int bufLen, int offset,
const char *var, unsigned long value)
{
char temp[50];
snprintf(temp, sizeof(temp), "0x%lx", value);
return my_putenv(buffer, bufLen, offset, var, temp);
}
/**
* putenv with a string
*
* @param[in,out] buffer The buffer where the environment variables are written
* @param[in] bufLen The length of the buffer
* @param[in] offset The current position in the buffer
* @param[in] var The name of the environment variable
* @param[in] value The value of the environment variable
*
* @return The new offset
*/
static int my_putenv_signed(char *buffer, int bufLen, int offset,
const char *var, long value)
{
char temp[50];
snprintf(temp, sizeof(temp), "%ld", value);
return my_putenv(buffer, bufLen, offset, var, temp);
}
/**
* Report the error
*
* @param[in] fmt The format string
*/
static void reportError(NPP instance, const char * fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(errMsg, sizeof(errMsg), fmt, ap);
va_end(ap);
if(instance)
{
NPN_Status(instance, errMsg);
}
fprintf(stderr, "%s\n",errMsg);
D("%s\n", errMsg);
}
static void clearError(void)
{
errMsg[0] = 0;
}
static bool haveError(void)
{
return errMsg[0] != 0;
}
/**
* Allocate some memory from the static pool. We use a static pool for
* the database because Mozilla can unload the plugin after use and this
* would lead to memory leaks if we use the heap.
*
* @param[in] size The size of the memory to allocate
*
* @return Pointer to the memory allocated or NULL
*/
static void * allocStaticMem(int size)
{
void * retVal = NULL;
const int newIdx = staticPoolIdx + size;
if(newIdx > MAX_STATIC_MEMORY_POOL)
{
reportError(NULL, "MozPlugger: config file is too big - delete some handlers/commands or mimetypes");
}
else
{
retVal = &staticPool[staticPoolIdx];
staticPoolIdx = newIdx;
}
return retVal;
}
/**
* Make a dynamic string static by copying to static memory.
* Given a pointer to a string in temporary memory, return the same string
* but this time stored in permanent (i.e. static) memory. Will only be deleted
* when the plugin is unloaded by Mozilla.
*
* @param[in] str Pointer to the string
* @param[in] len The length of the string
*
* @return Pointer to the copied string
*/
static const char * makeStrStatic(const char * str, int len)
{
/* plus one for string terminator */
char * const buf = allocStaticMem(len + 1);
if(buf)
{
strncpy(buf, str, len);
buf[len] = '\0';
}
return buf;
}
/**
* Search backwards from end of string buffer until first non-white space
* and then terminate string after that point to perform a trim.
*
* @param[in,out] buf The string to be trimmed.
*/
static void trim_trailing_spaces(char * buf)
{
char * end = &buf[strlen(buf)-1];
for( ;end >= buf; end--)
{
if((*end != '\r') && (*end != '\n') && (*end != '\t') && (*end != ' '))
{
end[1] = '\0';
return;
}
}
}
/**
* Trim the line buffer and if the line is empty or contains just a comment
* return false
*
* @param[in] buffer The line read
*
* @return true if not a blank line
*/
static bool chkCfgLine(char * buffer)
{
if(buffer[0] != '#')
{
trim_trailing_spaces(buffer);
return true;
}
return false;
}
static bool is_base_mozplugger(const char * magic)
{
return (magic[0] == '-');
}
/**
* Get the home directory
*
* @return a pointer to const string that contains home directory
*/
static const char * get_home_dir(void)
{
const char * home = getenv("HOME");
#ifdef HAVE_GETPWUID
if(home == NULL)
{
struct passwd * pw = getpwuid(getuid());
home = pw->pw_dir;
}
#endif
return home;
}
/**
* Get the prefix to the path to the config files based on the magic
*
* @param[in] magic references for this particular plugin type
* @param[out] buf The buffer to put the prefix
* @param[in] bufLen The length of the buffer
*
* @return the sizeof the resulting string
*/
static int get_cfg_path_prefix(const char * magic, char * buf, int bufLen)
{
const char * fmt;
int prefixLen = 1;
const char * home;
if(is_base_mozplugger(magic))
{
magic = "0";
}
else
{
prefixLen = strchr(magic, ':') - magic;
}
/* Locations are ...
* $MOZPLUGGER_HOME/.cache/
* $XDG_CACHE_HOME/mozplugger/
* $HOME/.cache/mozplugger/
*/
if( (home = getenv("MOZPLUGGER_HOME")) != NULL)
{
fmt = "%s/.cache/%.*s";
}
else if( (home = getenv("XDG_CACHE_HOME")) != NULL)
{
fmt = "%s/mozplugger/%.*s";
}
else if( (home = get_home_dir()) != NULL)
{
fmt = "%s/.cache/mozplugger/%.*s";
}
else
{
reportError(NULL, "Mozplugger cannot determine HOME directory\n");
*buf = '\0';
return 0;
}
return snprintf(buf, bufLen, fmt, home, prefixLen, magic);
}
/**
* Get the paths to the mozplugger helpers.
*
* @param[in] magic references for this particular plugin type
*
* @return None
*/
static void get_helper_paths(const char * magic)
{
char fname[200];
FILE * fp;
int n;
if(g_controller || g_linker || g_helper)
return;
n = get_cfg_path_prefix(magic, fname, sizeof(fname));
strncat(fname, ".helpers", sizeof(fname) - n);
if((fp = fopen(fname, "rb")) != NULL)
{
char buffer[512];
while(fgets(buffer, sizeof(buffer), fp))
{
if(chkCfgLine(buffer))
{
char * sep = strchr(buffer, '\t');
int pathLen = strlen(&sep[1]);
*sep = '\0';
if(strcmp(buffer, "linker") == 0)
{
g_linker = makeStrStatic(&sep[1], pathLen);
}
else if(strcmp(buffer, "controller") == 0)
{
g_controller = makeStrStatic(&sep[1], pathLen);
}
else if(strcmp(buffer, "version") == 0)
{
g_version = makeStrStatic(&sep[1], pathLen);
}
else if(strcmp(buffer, "name") == 0)
{
g_pluginName = makeStrStatic(&sep[1], pathLen);
}
else if(strcmp(buffer, "helper") == 0)
{
g_helper = makeStrStatic(&sep[1], pathLen);
}
}
}
fclose(fp);
}
}
/**
* Get the path to the configuration file.
*
* @param[in] magic references for this particular plugin type
*
* @return The full path to the configuration file
*/
static char * get_cmds_cfg_path(const char * magic)
{
char fname[200];
int n = get_cfg_path_prefix(magic, fname, sizeof(fname));
strncat(fname, ".cmds", sizeof(fname) - n);
return strdup(fname);
}
/**
* Get the path to the mimetypes file.
*
* @param[in] magic references for this particular plugin type
*
* @return The full path to the configuration file
*/
static char * get_mimetypes_cfg_path(const char * magic)
{
char fname[200];
int n = get_cfg_path_prefix(magic, fname, sizeof(fname));
strncat(fname, ".mimetypes", sizeof(fname) - n);
return strdup(fname);
}
/**
* Wrapper for execlp() that calls the helper.
*
* WARNING: This function runs in the daughter process so one must assume the
* daughter uses a copy (including) heap memory of the parent's memory space
* i.e. any write to memory here does not affect the parent memory space.
* Since Linux uses copy-on-write, best leave memory read-only and once execlp
* is called, all daughter memory is wiped anyway (except the stack).
*
* @param[in] THIS Pointer to the data associated with this instance of the
* plugin
* @param[in] file The url of the embedded object
* @param[in] pipeFd The file descriptor of the pipe to the helper application
*/
static void run(data_t * const THIS, const char *file, int pipeFd)
{
char buffer[ENV_BUFFER_SIZE];
int offset = 0;
int i;
unsigned int flags = THIS->command->flags;
int autostart = THIS->autostart;
const char * launcher = NULL;
const char * nextHelper = NULL;
/* If there is no window to draw the controls in then
* dont use controls -> mozdev bug #18837 */
if((THIS->window == 0) && ((flags & (H_CONTROLS | H_LINKS)) != 0) )
{
D("Cannot use controls or link button as no window to draw"
" controls in\n");
flags &= ~(H_CONTROLS | H_LINKS);
}
/* If no autostart seen and using controls dont autostart by default */
if ((flags & (H_CONTROLS | H_LINKS)) && (THIS->autostartNotSeen))
{
autostart = 0;
}
snprintf(buffer, sizeof(buffer), "%d,%d,%d,%lu,%d,%d",
flags,
THIS->repeats,
pipeFd,
(unsigned long int) THIS->window,
(int) THIS->width,
(int) THIS->height);
offset = strlen(buffer)+1;
offset = my_putenv_unsigned(buffer, sizeof(buffer), offset,
"window", THIS->window);
offset = my_putenv_hex(buffer, sizeof(buffer), offset,
"hexwindow", THIS->window);
offset = my_putenv_signed(buffer, sizeof(buffer), offset,
"repeats", THIS->repeats);
offset = my_putenv_unsigned(buffer, sizeof(buffer), offset,
"width", THIS->width);
offset = my_putenv_unsigned(buffer, sizeof(buffer), offset,
"height", THIS->height);
offset = my_putenv(buffer, sizeof(buffer), offset,
"mimetype", THIS->mimetype);
offset = my_putenv(buffer, sizeof(buffer), offset, "file", file);
offset = my_putenv(buffer, sizeof(buffer), offset,
"fragment", THIS->urlFragment);
offset = my_putenv(buffer, sizeof(buffer), offset,
"autostart", autostart ? "1" : "0");
offset = my_putenv(buffer, sizeof(buffer), offset,
"winname", THIS->command->winname);
if(THIS->display)
{
char * displayname = XDisplayName(DisplayString(THIS->display));
offset = my_putenv(buffer, sizeof(buffer), offset, "DISPLAY", displayname);
}
for (i = 0; i < THIS->num_arguments; i++)
{
offset = my_putenv(buffer, sizeof(buffer), offset,
THIS->args[i].name, THIS->args[i].value);
}
if(flags & H_CONTROLS)
{
launcher = g_controller;
}
else if(flags & H_LINKS)
{
launcher = g_linker;
}
else if(!autostart && !(flags & H_AUTOSTART) && (THIS->window != 0))
{
/* Application doesn't do autostart and autostart is false and
* we have a window to draw in */
nextHelper = g_helper;
launcher = g_linker;
}
else
{
launcher = g_helper;
}
if(launcher == 0)
{
D("No launcher defined");
_exit(EX_UNAVAILABLE); /* Child exit, that's OK */
}
D("Executing helper: %s %s %s %s %s\n",
launcher,
buffer,
file,
THIS->command->cmd,
THIS->mimetype);
execlp(launcher, launcher, buffer, THIS->command->cmd, nextHelper, NULL);
D("EXECLP FAILED! errno=%i\n", errno);
_exit(EX_UNAVAILABLE); /* Child exit, that's OK */
}
static char * NP_strdup2(const char * str, int len)
{
char * dupStr = NPN_MemAlloc(len + 1);
if(dupStr != NULL)
{
strncpy(dupStr, str, len);
dupStr[len] = '\0';
}
else
{
D("NPN_MemAlloc failed, size=%i\n", len+1);
}
return dupStr;
}
/**
* String dup function that uses NPN_MemAlloc as opposed to malloc
*
* WARNING, this function will not work if directly or indirectly called from
* NPP_GetMimeDescription.
*
* @param[in] str The string to duplicate
*
* @return Pointer to the duplicate.
*/
static char * NP_strdup(const char * str)
{
return NP_strdup2(str, strlen(str));
}
/**
* Test if the line buffer contains a mimetype
*
* @param[in] buffer The line buffer
*
* @return true if contains a mimetype
*/
static bool isCfgMimeType(char * buffer)
{
return !isspace(buffer[0]);
}
/**
* Parse the mimetypec in the cfg file
*
* @param[in] buffer The line read from config file
*
* @return a mimetype_t structure if valid mimetype.
*/
static mimetype_t * parseCfgMimeType(char * buffer)
{
mimetype_t * type = (mimetype_t *) allocStaticMem(sizeof(mimetype_t));
if(type == 0)
{
D("Failed to alloc memory for mimetype\n");
return NULL;
}
memset(type, 0, sizeof(mimetype_t));
D("New mime type\n");
/* Cant use NPN_MemAlloc in NPP_GetMimeDescription, use
* makeStrStatic as opposed to strdup otherwise we get a
* memory leak */
type->type = makeStrStatic(buffer, strlen(buffer));
return (type->type == 0) ? NULL : type;
}
/**
* Parse the command found in the cfg file
*
* @param[in] buffer The line read from config file
*
* @return a command structure if command found.
*/
static command_t * parseCfgCmdLine(char * buffer)
{
char * x = &buffer[1];
char * sep;
command_t * cmd = (command_t *) allocStaticMem(sizeof(command_t));
if(cmd == NULL)
{
D("Failed to alloc memory for command\n");
return NULL;
}
memset(cmd, 0, sizeof(command_t));
D("-- reading cmd line %s\n", x);
sep = strchr(x, '\t');
cmd->flags = strtol(x, NULL, 16);
x = &sep[1];
sep = strchr(x, '\t');
if(sep > x)
{
cmd->winname = makeStrStatic(x, sep - x);
}
x = &sep[1];
sep = strchr(x, '\t');
if( sep > x)
{
cmd->fmatchStr = makeStrStatic(x, sep - x);
}
x = &sep[1];
cmd->cmd = makeStrStatic(x, strlen(x));
return cmd;
}
/**
* Read the configuration file into memory.
*
* @param[in] f The FILE pointer
*/
static void read_config(FILE * f)
{
int num_handlers = 0;
handler_t * prev_handler = NULL;
handler_t * handler = NULL;
command_t * prev_cmd = NULL;
mimetype_t * prev_type = NULL;
char lineBuf[512];
int lineNum;
D("read_config\n");
lineNum = 0;
while (fgets(lineBuf, sizeof(lineBuf), f))
{
lineNum++;
if(!chkCfgLine(lineBuf))
{
continue;
}
D("%5i::|%s|\n", lineNum, lineBuf);
if(isCfgMimeType(lineBuf))
{
/* Mime type */
mimetype_t * type = NULL;
if (!handler || handler->cmds)
{
D("------------ Starting new handler ---------\n");
handler = (handler_t *) allocStaticMem(sizeof(handler_t));
if(handler == 0)
{
return;
}
memset(handler, 0, sizeof(handler_t));
prev_type = NULL;
prev_cmd = NULL;
if(prev_handler)
{
/* Add to end of linked list of handlers */
prev_handler->pNext = handler;
}
else
{
/* Add at header of link list of handlers */
g_handlers = handler;
}
/* Remember the current end of linked list */
prev_handler = handler;
num_handlers++;
}
type = parseCfgMimeType(lineBuf);
if(!type)
{
return; /* run out of memory! */
}
if(prev_type)
{
/* Add to end of linked list of handlers */
prev_type->pNext = type;
}
else
{
/* Add at header of link list of handlers */
handler->types = type;
}
/* Remember the current end of linked list */
prev_type = type;
}
else
{
command_t * cmd = parseCfgCmdLine(lineBuf);
if(!cmd)
{
return; /* run out of memory! */
}
if(!handler)
{
D("Command before mimetype!\n");
return;
}
if(prev_cmd)
{
/* Add to end of linked list of handlers */
prev_cmd->pNext = cmd;
}
else
{
/* Add at header of link list of handlers */
handler->cmds = cmd;
}
/* Remember the current end of linked list */
prev_cmd = cmd;
}
}
D("Num handlers: %d\n", num_handlers);
}
/**
* Find configuration file, helper and controller executables. Call the
* appropriate xxx_cb function to handle the action (e.g. for configuration
* file, the parsering and reading into memory).
*
* @param[in] magic references for this particular plugin type
*
* @return false if failed, true otherwise
*/
static bool do_read_config(const char * magic)
{
bool retVal = true;
char * config_fname;
if (g_handlers)
{
return retVal;
}
D("do_read_config(%s)\n", magic);
config_fname = get_cmds_cfg_path(magic);
get_helper_paths(magic);
if(config_fname)
{
FILE * fd = fopen(config_fname, "rb");
if(fd)
{
read_config(fd);
fclose(fd);
D("do_read_config done\n");
}
else
{
D("Failed to read config %s\n", config_fname);
retVal = false;
}
free(config_fname);
}
else
{
if(!haveError())
{
reportError(NULL, "Mozplugger error - failed to locate %s", config_fname);
}
retVal = false;
}
return retVal;
}
/**
* Check URL is safe. Since href's are passed to an app as an argument, just
* check for ways that a shell can be tricked into executing a command.
*
* @param[in] name The name to check
* @param[in] isURL Is the name a URL or filename?
*
* @return true if OK, false if not
*/
static bool safeName(const char* name, int isURL)
{
int i = 0;
const int len = strlen(name);
if ((name[0] == '/') && (isURL))
{
D("safeName() - reject URL '%s' as starts with '/'\n", name);
return false;
}
for (i = 0; i < len; i++)
{
if (name[i] == '`' || name[i] == ';')
{
D("safeName() - reject '%s' as contains either ';' or '`'\n", name);
/* Somebody's trying to do something naughty. */
return false;
}
}
return true;
}
/**
* Extract the file name from the HTTP Headers (if present). This is passed
* via the fragment environment variable to the helper application. Copy
* a suitable filename for the URL to be used for the cached temporay file
* name.
*
* @param[in] THIS Pointer to the instance data
* @param[in] headers The HTTP headers to parse
* @param[out] fileName The file Name extracted from the headers
* @param[in] maxFileNameLen The max length of fileName
*
* @return None
*/
static char * parseHeaders(data_t * const THIS, const char * headers, char * fileName)
{
const char * p = headers;
if (!headers)
{
return fileName;
}
while((p = strstr(p, "Content-Disposition:")) != NULL)
{