File: ec_plugins.c

package info (click to toggle)
ettercap 1:0.8.2-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,468 kB
  • ctags: 6,333
  • sloc: ansic: 47,337; yacc: 310; lex: 204; makefile: 121; xml: 31; sh: 24
file content (493 lines) | stat: -rw-r--r-- 11,524 bytes parent folder | download | duplicates (4)
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
/*
    ettercap -- plugin handling

    Copyright (C) ALoR & NaGA

    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-1307, USA.

*/

#include <ec.h>
#include <ec_plugins.h>

#include <dirent.h>

#ifndef HAVE_SCANDIR
   #include <missing/scandir.h>
#endif

#ifdef HAVE_PLUGINS
   #ifdef HAVE_LTDL_H
      #include <ltdl.h>
   #endif
   #ifdef HAVE_DLFCN_H
      #include <dlfcn.h>
   #endif
#endif

/* symbol prefix for some OSes */
#ifdef NEED_USCORE
   #define SYM_PREFIX "_"
#else
   #define SYM_PREFIX ""
#endif
         

/* global data */

struct plugin_entry {
   void *handle;
   char activated;   /* if the init fuction was already called */
   struct plugin_ops *ops;
   SLIST_ENTRY(plugin_entry) next;
};

static SLIST_HEAD(, plugin_entry) plugin_head;

/* mutexes */
static pthread_mutex_t kill_mutex = PTHREAD_MUTEX_INITIALIZER;
#define KILL_LOCK do { pthread_mutex_lock(&kill_mutex); } while (0)
#define KILL_UNLOCK do { pthread_mutex_unlock(&kill_mutex); } while (0)

static pthread_mutex_t plugin_list_mutex = PTHREAD_MUTEX_INITIALIZER;
#define PLUGIN_LIST_LOCK do { pthread_mutex_lock(&plugin_list_mutex); } \
                         while (0)
#define PLUGIN_LIST_UNLOCK do { pthread_mutex_unlock(&plugin_list_mutex); } \
                           while (0)

/* protos... */

void plugin_unload_all(void);
static void plugin_print(char active, struct plugin_ops *ops);
#if defined (OS_DARWIN)
int plugin_filter(struct dirent *d);
#else
int plugin_filter(const struct dirent *d);
#endif

/*******************************************/

/* 
 * load a plugin given the full path
 */

int plugin_load_single(const char *path, char *name)
{
#ifdef HAVE_PLUGINS
   char file[strlen(path)+strlen(name)+2];
   void *handle;
   int (*plugin_load)(void *);
   
   snprintf(file, sizeof(file), "%s/%s", path, name);
  
   DEBUG_MSG("plugin_load_single: %s", file);
   
   /* load the plugin */
   handle = dlopen(file, RTLD_NOW|RTLD_LOCAL);

   if (handle == NULL) {
      DEBUG_MSG("plugin_load_single - %s - dlopen() | %s", file, dlerror());
      return -E_INVALID;
   }
   
   /* find the loading function */
   plugin_load = dlsym(handle, SYM_PREFIX "plugin_load");
   
   if (plugin_load == NULL) {
      DEBUG_MSG("plugin_load_single - %s - lt_dlsym() | %s", file, dlerror());
      dlclose(handle);
      return -E_INVALID;
   }

   /* 
    * return the same value of plugin_register 
    * we pass the handle to the plugin, which
    * in turn passes it to the plugin_register 
    * function
    */
   return plugin_load(handle);
#else
   (void) path;
   (void) name;
   return -E_INVALID;
#endif
}

/*
 * filter for the scandir function
 */
#if defined (OS_DARWIN)
int plugin_filter(struct dirent *d)
#else
int plugin_filter(const struct dirent *d)
#endif
{
   if ( match_pattern(d->d_name, PLUGIN_PATTERN) )
      return 1;

   return 0;
}

/*
 * search and load all plugins in INSTALL_PREFIX/lib
 */

void plugin_load_all(void)
{
#ifdef HAVE_PLUGINS
   struct dirent **namelist = NULL;
   int n, i, ret;
   int t = 0;
   char *where;
   
   DEBUG_MSG("plugin_loadall");

#ifdef OS_WINDOWS
   /* Assume .DLLs are under "<ec_root>/lib". This should be unified for
    * all; ec_get_lib_path()?
    */
   char path[MAX_PATH];

   snprintf(path, sizeof(path), "%s%s", ec_win_get_ec_dir(), INSTALL_LIBDIR);
   where = path;
#else
   /* default path */
   where = INSTALL_LIBDIR "/" EC_PROGRAM;
#endif
   
   /* first search in  INSTALL_LIBDIR/ettercap" */
   n = scandir(where, &namelist, plugin_filter, alphasort);
   /* on error fall back to the current dir */
   if (n <= 0) {
      DEBUG_MSG("plugin_loadall: no plugin in %s searching locally...", where);
      /* change the path to the local one */
      where = "plug-ins";
      n = scandir(where, &namelist, plugin_filter, alphasort);
      DEBUG_MSG("plugin_loadall: %d found", n);
   }
  
   for(i = n-1; i >= 0; i--) {
      ret = plugin_load_single(where, namelist[i]->d_name);
      switch (ret) {
         case E_SUCCESS:
            t++;
            break;
         case -E_DUPLICATE:
            USER_MSG("plugin %s already loaded...\n", namelist[i]->d_name);
            DEBUG_MSG("plugin %s already loaded...", namelist[i]->d_name);
            break;
         case -E_VERSION:
            USER_MSG("plugin %s was compiled for a different ettercap version...\n", namelist[i]->d_name);
            DEBUG_MSG("plugin %s was compiled for a different ettercap version...", namelist[i]->d_name);
            break;
         case -E_INVALID:
         default:
            USER_MSG("plugin %s cannot be loaded...\n", namelist[i]->d_name);
            DEBUG_MSG("plugin %s cannot be loaded...", namelist[i]->d_name);
            break;
      }
      SAFE_FREE(namelist[i]);
   }
   
   USER_MSG("%4d plugins\n", t);

   SAFE_FREE(namelist);
   
   atexit(plugin_unload_all);
#else
   USER_MSG("   0 plugins (disabled by configure...)\n");
#endif
}


/*
 * unload all the plugin
 */
void plugin_unload_all(void)
{
#ifdef HAVE_PLUGINS
   struct plugin_entry *p;
   
   DEBUG_MSG("ATEXIT: plugin_unload_all");   
   
   while (SLIST_FIRST(&plugin_head) != NULL) {
      p = SLIST_FIRST(&plugin_head);
      if(plugin_is_activated(p->ops->name) == 1)
		plugin_fini(p->ops->name);
      dlclose(p->handle);
      SLIST_REMOVE_HEAD(&plugin_head, next);
      SAFE_FREE(p);
   }
#endif
}


/*
 * function used by plugins to register themself
 */
int plugin_register(void *handle, struct plugin_ops *ops)
{
#ifdef HAVE_PLUGINS
   struct plugin_entry *p, *pl;

   /* check for ettercap API version */
   if (strcmp(ops->ettercap_version, EC_VERSION)) {
      dlclose(handle);
      return -E_VERSION;
   }
   
   /* check that this plugin was not already loaded */
   SLIST_FOREACH(pl, &plugin_head, next) {
      /* same name and same version */
      if (!strcmp(ops->name, pl->ops->name) && !strcmp(ops->version, pl->ops->version)) {
         dlclose(handle);
         return -E_DUPLICATE;
      }
   }

   SAFE_CALLOC(p, 1, sizeof(struct plugin_entry));
   
   p->handle = handle;
   p->ops = ops;

   SLIST_INSERT_HEAD(&plugin_head, p, next);

   return E_SUCCESS;
#else
   (void) handle;
   (void) ops;
   return -E_INVALID;
#endif
}


/* 
 * activate a plugin.
 * it launch the plugin init function 
 */
int plugin_init(char *name)
{
   struct plugin_entry *p;
   int ret;

   SLIST_FOREACH(p, &plugin_head, next) {
      if (!strcmp(p->ops->name, name)) {
         /* get the response from the plugin */
         ret = p->ops->init(NULL);
         /* if it is still running, mark it as active */
         if (ret == PLUGIN_RUNNING)
            p->activated = 1;
         return ret;
      }
   }
   
   return -E_NOTFOUND;
}


/* 
 * deactivate a plugin.
 * it launch the plugin fini function 
 */
int plugin_fini(char *name)
{
   struct plugin_entry *p;
   int ret;

   SLIST_FOREACH(p, &plugin_head, next) {
      if (p->activated == 1 && !strcmp(p->ops->name, name)) {
         /* get the response from the plugin */
         ret = p->ops->fini(NULL);
         /* if it is still running, mark it as active */
         if (ret == PLUGIN_FINISHED)
            p->activated = 0;
         return ret;
      }
   }
   
   return -E_NOTFOUND;
}

/* 
 * self-destruct a plugin thread.
 * it resets the activity state and destructs itself by calling the plugin fini function.
 * it does not replace the <plugin>_fini standard function rather than it depends on it.
 * this function does not do anything if not executed by a thread.
 */
int plugin_kill_thread(char *name, char *thread)
{
   struct plugin_entry *p;
   int ret;
   pthread_t pid;

   pid = ec_thread_getpid(thread); 

   /* do not execute if not being a thread */
   if (pthread_equal(pid, EC_PTHREAD_NULL))
      return -E_INVALID;

   /* the thread can only kill itself */
   if (!pthread_equal(pid, pthread_self()))
      return -E_INVALID;

   DEBUG_MSG("plugin_kill_thread");

   KILL_LOCK;
   SLIST_FOREACH(p, &plugin_head, next) {
      if (p->activated == 1 && !strcmp(p->ops->name, name)) {
         /* flag plugin as inactive */
         p->activated = 0;
         /* update the UI */
         ui_update(UI_UPDATE_PLUGINLIST);
         /* release the lock */
         KILL_UNLOCK;
         /* call plugin's destruction routine */
         ret = p->ops->fini(NULL);
         /*
          * normally the thread should not return from the call - 
          * just in case the thread wasn't destroyed in the fini callback
          * destroy it here 
          */
         ec_thread_destroy(pid);

         /* should never be reached */
         return ret;
      }
   }
   KILL_UNLOCK;
   
   return -E_NOTFOUND;
}

/*
 * it print the list of the plugins.
 *
 * func is the callback function to which are passed
 *    - the plugin name
 *    - the plugin version
 *    - the plugin description
 *
 * min is the n-th plugin to start to print
 * max it the n-th plugin to stop to print
 */

int plugin_list_walk(int min, int max, void (*func)(char, struct plugin_ops *))
{
   struct plugin_entry *p;
   int i = min;

   SLIST_FOREACH(p, &plugin_head, next) {
      if (i > max)
         return (i-1);
      if (i < min) {
         i++;
         continue;
      }
      func(p->activated, p->ops);
      i++;
   }
   
   return (i == min) ? -E_NOTFOUND : (i-1);
}

/*
 * returns the activation flag
 */

int plugin_is_activated(char *name)
{
   struct plugin_entry *p;

   SLIST_FOREACH(p, &plugin_head, next) {
      if (!strcmp(p->ops->name, name)) {
         return p->activated;
      }
   }
   
   return 0;
}

/*
 * search if a plugin exists
 */
int search_plugin(char *name)
{
   struct plugin_entry *p;

   SLIST_FOREACH(p, &plugin_head, next) {
      if (!strcmp(p->ops->name, name)) {
         return E_SUCCESS;
      }
   }
   
   return -E_NOTFOUND;
}

/*
 * print the list of available plugins
 */
void plugin_list(void)
{
   int ret;

   DEBUG_MSG("plugin_list");

   /* load all the plugins */
   plugin_load_all();
      
   /* print the list */
   fprintf(stdout, "\nAvailable plugins :\n\n");
   ret = plugin_list_walk(PLP_MIN, PLP_MAX, &plugin_print);
   if (ret == -E_NOTFOUND) {
      fprintf(stdout, "No plugin found !\n\n");
      return;
   }
   fprintf(stdout, "\n\n");

}

/*
 * walk through the list of plugin names and free
 */
void free_plugin_list(struct plugin_list_t plugins)
{
   struct plugin_list *plugin, *tmp;

   PLUGIN_LIST_LOCK;

   LIST_FOREACH_SAFE(plugin, &plugins, next, tmp) {
      LIST_REMOVE(plugin, next);
      SAFE_FREE(plugin->name);
      SAFE_FREE(plugin);
   }

   PLUGIN_LIST_UNLOCK;
}

/*
 * callback function for displaying the plugin list 
 */
static void plugin_print(char active, struct plugin_ops *ops)
{
   /* variable not used */
   (void) active;

   fprintf(stdout, " %15s %4s  %s\n", ops->name, ops->version, ops->info);  
}

/* EOF */

// vim:ts=3:expandtab