This repository was archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathplugin.c
629 lines (498 loc) · 15 KB
/
plugin.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
#include <fcntl.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef __LINUX__
#include <alloca.h>
#endif
#include "core.h"
#include "utils.h"
#include "plugin.h"
#include "socket.h"
#include "main.h"
/**
*
*/
static char* plugin_basename(char* dst, char* src, supported_plugins_t type){
size_t len;
len = strlen(src) - strlen(plugins_extensions_str[type]);
return memcpy(dst, src, len);
}
/**
*
*/
static void insert_new_plugin_in_list(char* name, supported_plugins_t type, short priority)
{
plugin_t *cur_ptr, *old_ptr;
plugin_t *plugin;
plugin = (plugin_t*)proxenet_xmalloc(sizeof(plugin_t));
plugin->id = proxenet_plugin_list_size() + 1;
plugin->type = type;
plugin->priority = priority;
plugin->next = NULL;
plugin->state = INACTIVE;
plugin->interpreter = &vms[type];
plugin->pre_function = NULL;
plugin->post_function = NULL;
plugin->internal = NULL;
#ifdef DEBUG
xlog(LOG_DEBUG, "Trying to add '%s' [type=%d,prio=%d] to plugin_list\n", name, type, priority);
#endif
if (!strcpy(plugin->filename, name)){
xlog(LOG_CRITICAL, "strcpy() failed for '%s': %s", name, strerror(errno));
abort();
}
if (proxenet_xsnprintf(plugin->fullpath, sizeof(plugin->fullpath), "%s/%s", cfg->plugins_path, name) < 0){
xlog(LOG_CRITICAL, "proxenet_xsnprintf() failed for '%s/%s': %s", cfg->plugins_path, name, strerror(errno));
abort();
}
if (!plugin_basename(plugin->name, name, type)){
xlog(LOG_CRITICAL, "plugin_basename() failed for '%s': %s", name, strerror(errno));
abort();
}
/* if first plugin */
if (!plugins_list) {
plugins_list = plugin;
} else {
old_ptr = NULL;
cur_ptr = plugins_list;
/* browse the plugins list */
while (true) {
/* stop if current node priority is lower (i.e. integer higher) than new node's */
if (cur_ptr->priority > priority) {
plugin->next = cur_ptr;
/* if current node is first node, make it first */
if (!old_ptr)
plugins_list = plugin;
/* otherwise, append */
else
old_ptr->next = plugin;
break;
}
/* stop if last element */
if (cur_ptr->next == NULL) {
cur_ptr->next = plugin;
break;
}
/* otherwise move on to next node */
old_ptr = cur_ptr;
cur_ptr = cur_ptr->next;
}
}
if (cfg->verbose > 1)
xlog(LOG_INFO, "[%s] Plugin '%s' added to list\n", supported_plugins_str[type], plugin->name);
return;
}
/**
*
*/
unsigned int proxenet_plugin_list_size()
{
plugin_t *p;
unsigned int i;
for (p=plugins_list, i=0; p!=NULL; p=p->next, i++);
return i;
}
/**
* This function zero-es out the block allocated for a specific plugin.
*/
void proxenet_free_plugin(plugin_t* plugin)
{
char name[PATH_MAX] = {0, };
int len;
#ifdef _PERL_PLUGIN
if(plugin->type == _PERL_) {
proxenet_xfree(plugin->pre_function);
proxenet_xfree(plugin->post_function);
}
#endif
len = strlen(plugin->filename);
strncpy(name, plugin->filename, len);
proxenet_xfree(plugin);
#ifdef DEBUG
xlog(LOG_DEBUG, "Free-ed plugin '%s'\n", name);
#endif
return;
}
/**
* This function zero-es out all the blocks allocated for plugins and
* NULL-s the plugin_list pointer.
*/
void proxenet_free_all_plugins()
{
plugin_t *p = plugins_list;
plugin_t *next;
while (p != NULL) {
next = p->next;
proxenet_free_plugin(p);
p = next;
}
plugins_list = NULL;
#ifdef DEBUG
xlog(LOG_DEBUG, "%s\n", "Plugins list is free-ed");
#endif
return;
}
/**
*
*/
static bool proxenet_build_plugins_list(char *list_str, int *list_len)
{
plugin_t *p;
char *ptr;
int n, max_line_len, total_len;
max_line_len = 256;
ptr = list_str;
n = sprintf(ptr, "Plugins list:\n");
ptr += n;
total_len = strlen("Plugins list:\n");
for (p = plugins_list; p!=NULL; p=p->next) {
n = proxenet_xsnprintf(ptr, max_line_len,
"|_ priority=%-3d id=%-3d type=%-10s[0x%x] name=%-20s (%sACTIVE)\n",
p->priority,
p->id,
supported_plugins_str[p->type],
p->type,
p->name,
(p->state==ACTIVE?"":"IN")
);
if (n<0) {
xlog(LOG_ERROR, "Error: %s\n", strerror(errno));
return false;
}
ptr += n;
total_len += n;
if ((ptr - list_str + max_line_len) > *list_len) {
xlog(LOG_ERROR, "%s\n", "Overflow detected");
return false;
}
}
*list_len = total_len;
return true;
}
/**
*
*/
void proxenet_print_plugins_list(int fd)
{
char *list_str;
int list_len;
list_len = 2048;
list_str = (char*)alloca(list_len);
memset(list_str, 0, list_len);
if (!proxenet_build_plugins_list(list_str, &list_len)) {
xlog(LOG_ERROR, "%s\n", "Failed to build plugins list string");
return;
}
if (fd<0)
xlog(LOG_INFO, "%s", list_str);
else {
proxenet_write(fd, list_str, list_len);
proxenet_write(fd, "\n", 1);
}
return;
}
/**
* Get the proxenet type associated to a specific file name. The type
* is guessed by the filename extension.
*
* @param the file name of the plugin
* @return the type (int) of the file name if found, -1 otherwise
*/
int proxenet_get_plugin_type(char* filename)
{
unsigned short type;
bool is_valid = false;
char *ext;
ext = strrchr(filename, '.');
if (!ext)
return -1;
for (type=0; plugins_extensions_str[type]!=NULL; type++) {
is_valid = (strcmp(ext, plugins_extensions_str[type])==0);
if ( is_valid ) {
return type;
}
}
return -1;
}
/**
* Search if a loaded plugin has a name matching the argument
*
* @param name : plugin name to look up
* @return true if a match is found, false in any other case
*/
bool proxenet_is_plugin_loaded(char* name)
{
plugin_t* p;
for(p=plugins_list; p!=NULL; p=p->next){
if(!strcmp(p->filename, name)){
return true;
}
}
return false;
}
/**
*
*/
static char* get_full_path(char* path, char* name)
{
ssize_t l = -1;
char *realpath_buf;
char fullpath[PATH_MAX] = {0, };
l = proxenet_xsnprintf(fullpath, PATH_MAX, "%s/%s", path, name);
if(l == -1){
xlog(LOG_ERROR, "proxenet_xsnprintf() failed on %d: %s\n",
errno, strerror(errno));
return NULL;
}
realpath_buf = realpath(fullpath, NULL);
if(!realpath_buf){
xlog(LOG_ERROR, "realpath() failed on '%s': %d - %s\n",
fullpath, errno, strerror(errno));
return NULL;
}
return realpath_buf;
}
/**
* Plugin name structure *MUST* be `PLUGIN_DIR/[<priority>]<name>.<ext>`
* <priority> is an int in [1, 9]. If <priority> is found as the first char of the
* file name, it will be applied to the plugin. If no priority is specify, a default
* priority will be applied.
*
* If a file does not match this pattern, it will be discarded
*
* @param plugin_path is the path to look for plugins
* @param plugin_name is the name of the plugin to add. If NULL, this function will try
* to *all* the files in the directory.
* @return the number of added plugins on success, -1 on error
*/
int proxenet_add_new_plugins(char* plugin_path, char* plugin_name)
{
struct dirent *dir_ptr=NULL;
DIR *dir = NULL;
char* name = NULL;
short type=-1, priority;
int d_name_len;
bool add_all = (plugin_name==NULL)?true:false;
unsigned int nb_plugin_added = 0;
char *realpath_buf;
ssize_t len = -1;
#ifdef DEBUG
if (add_all)
xlog(LOG_DEBUG, "Trying to add all files in '%s'\n", plugin_path);
else
xlog(LOG_DEBUG, "Trying to add '%s/%s'\n", plugin_path, plugin_name);
#endif
dir = opendir(plugin_path);
if (dir == NULL) {
xlog(LOG_ERROR, "Failed to open '%s': %s\n", plugin_path, strerror(errno));
return -1;
}
while ((dir_ptr=readdir(dir))) {
realpath_buf = NULL;
if (strcmp(dir_ptr->d_name,".")==0)
continue;
if (strcmp(dir_ptr->d_name,"..")==0)
continue;
/* if add one plugin, loop until the right name */
if (!add_all && strcmp(dir_ptr->d_name, plugin_name)!=0)
continue;
if (dir_ptr->d_type == DT_LNK){
/* if entry is a symlink, ensure it's pointing to a file in plugins_path */
realpath_buf = get_full_path(plugin_path, dir_ptr->d_name);
if (!realpath_buf)
continue;
len = strlen(cfg->plugins_path);
if( strncmp(realpath_buf, cfg->plugins_path, len) != 0 ){
xlog(LOG_WARNING, "'%s' is not in plugin directory '%s'\n",
realpath_buf, cfg->plugins_path);
proxenet_xfree( realpath_buf );
continue;
}
} else {
/* if not a symlink nor regular file, continue */
if (dir_ptr->d_type != DT_REG)
continue;
/* if file is a regular file, check it is *NOT* in autoload */
realpath_buf = proxenet_xmalloc(PATH_MAX);
len = proxenet_xsnprintf(realpath_buf, PATH_MAX, "%s/%s", plugin_path, dir_ptr->d_name);
if(len == -1){
xlog(LOG_ERROR, "proxenet_xsnprintf() failed on %d: %s\n",
errno, strerror(errno));
continue;
}
len = strlen(cfg->autoload_path);
if( strncmp(realpath_buf, cfg->autoload_path, len) == 0 ){
xlog(LOG_WARNING, "Refusing to load non-symlink '%s' in autoload '%s'\n",
realpath_buf,
cfg->autoload_path);
proxenet_xfree( realpath_buf );
continue;
}
}
proxenet_xfree( realpath_buf );
/* if first char is valid integer, this will be the plugin priority */
if (atoi(&(dir_ptr->d_name[0])) > 0)
priority = (unsigned short)atoi(&(dir_ptr->d_name[0]));
else
priority = (unsigned short) CFG_DEFAULT_PLUGIN_PRIORITY;
/* plugin name */
d_name_len = strlen(dir_ptr->d_name);
if (d_name_len > 510)
continue;
name = dir_ptr->d_name;
/* plugin type */
type = proxenet_get_plugin_type(name);
if (type < 0)
continue;
if ( proxenet_is_plugin_loaded(name) ){
xlog(LOG_WARNING, "A plugin named '%s' is already loaded\n", name);
continue;
}
/* add plugin in correct place (1: high priority, 9: low priority) */
insert_new_plugin_in_list(name, type, priority);
nb_plugin_added++;
/* if add one plugin only, there is no need to keep looping */
if (!add_all)
break;
}
if (closedir(dir) < 0){
xlog(LOG_WARNING, "Error while closing '%s': %s\n", plugin_path, strerror(errno));
}
return nb_plugin_added;
}
/**
*
*/
unsigned int count_plugins_by_type(supported_plugins_t type)
{
unsigned int i=0;
plugin_t* p;
for(p=plugins_list; p!=NULL; p=p->next)
if (p->type == type) i++;
return i;
}
/**
*
*/
unsigned int count_initialized_plugins_by_type(supported_plugins_t type)
{
unsigned int i=0;
plugin_t* p;
for(p=plugins_list; p!=NULL; p=p->next)
if (p->type == type &&
p->state == ACTIVE &&
p->interpreter!=NULL) i++;
return i;
}
/**
* Lookup for a plugin.
*
* @param plugin_id is the identifier of the plugin
* @return the reference to the plugin, or NULL if plugin was not found.
*/
plugin_t* proxenet_get_plugin_by_id(unsigned short plugin_id)
{
plugin_t *p;
for (p=plugins_list; p!=NULL; p=p->next)
if (p->id == plugin_id)
return p;
return NULL;
}
/**
* Check and change a plugin state.
*
* @param p is the reference to the plugin to update
* @param state is the new state to apply
* @return 0 upon success, or -1 if a check has failed.
*/
int proxenet_plugin_set_state(plugin_t* p, proxenet_state state)
{
if (state!=ACTIVE && state!=INACTIVE)
return -1;
p->state = state;
if (cfg->verbose){
xlog(LOG_INFO,
"Plugin %d '%s' is now %sACTIVE\n"NOCOLOR,
p->id,
p->name,
(p->state==INACTIVE ? RED"IN" : GREEN""));
}
return 0;
}
/**
* Check and change a plugin state by the plugin id.
*
* @param p is the reference to the plugin to update
* @param state is the new state to apply
* @return 0 upon success, or -1 if a check has failed.
*/
int proxenet_plugin_set_state_by_id(unsigned short plugin_id, proxenet_state state)
{
plugin_t* p;
p = proxenet_get_plugin_by_id( plugin_id );
if (!p)
return -1;
return proxenet_plugin_set_state(p, state);
}
/**
* Check and change a plugin prority.
*
* @param p is the reference to the plugin to update
* @param prority is the new prority to apply
* @return 0 upon success, or -1 if a check has failed.
*/
int proxenet_plugin_set_prority(unsigned short plugin_id, unsigned short priority)
{
plugin_t* plugin;
plugin_t* p;
bool has_changed;
plugin = proxenet_get_plugin_by_id( plugin_id );
if (!plugin)
return -1;
if (priority==0 || priority>=10)
return -1;
if (!plugins_list)
return -1;
if (!plugin)
return -1;
/* 1. unlink `plugin` */
has_changed = false;
for (p=plugins_list; p!=NULL; p=p->next){
if (p->next == plugin){
p->next = plugin->next;
has_changed = true;
break;
}
}
if (!has_changed)
return -1;
/* 2. relink `plugin` in its new position */
has_changed = false;
for (p=plugins_list; p!=NULL; p=p->next){
if (p->priority > priority)
continue;
plugin->next = p->next;
p->next = plugin;
plugin->priority = priority;
has_changed = true;
}
if (!has_changed)
return -1;
if (cfg->verbose){
xlog(LOG_INFO,
"Plugin %hu '%s' has a priority of %hu\n",
plugin->id,
plugin->name,
plugin->priority);
}
return 0;
}