Welcome to mirror list, hosted at ThFree Co, Russian Federation.

plugins.cc « lib « src « core - github.com/bareos/bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a68e23b692ad87df53c1579d6137f731455f4dbf (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
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
/*
   BAREOS® - Backup Archiving REcovery Open Sourced

   Copyright (C) 2007-2012 Free Software Foundation Europe e.V.
   Copyright (C) 2011-2012 Planets Communications B.V.
   Copyright (C) 2013-2021 Bareos GmbH & Co. KG

   This program is Free Software; you can redistribute it and/or
   modify it under the terms of version three of the GNU Affero General Public
   License as published by the Free Software Foundation, which is
   listed in the file LICENSE.

   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
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.
*/
/*
 * Plugin load/unloader for all BAREOS daemons
 *
 * Kern Sibbald, October 2007
 */

#include "include/bareos.h"
#include "lib/alist.h"
#include "lib/berrno.h"

#include <dlfcn.h>
#include <dirent.h>
#define NAMELEN(dirent) (strlen((dirent)->d_name))
#ifndef HAVE_READDIR_R
int Readdir_r(DIR* dirp, struct dirent* entry, struct dirent** result);
#endif

#ifndef RTLD_NOW
#  define RTLD_NOW 2
#endif

#if !defined(LT_LAZY_OR_NOW)
#  if defined(RTLD_LAZY)
#    define LT_LAZY_OR_NOW RTLD_LAZY
#  else
#    if defined(DL_LAZY)
#      define LT_LAZY_OR_NOW DL_LAZY
#    endif
#  endif /* !RTLD_LAZY */
#endif
#if !defined(LT_LAZY_OR_NOW)
#  if defined(RTLD_NOW)
#    define LT_LAZY_OR_NOW RTLD_NOW
#  else
#    if defined(DL_NOW)
#      define LT_LAZY_OR_NOW DL_NOW
#    endif
#  endif /* !RTLD_NOW */
#endif

#if !defined(LT_LAZY_OR_NOW)
#  define LT_LAZY_OR_NOW 0
#endif /* !LT_LAZY_OR_NOW */

#if !defined(LT_GLOBAL)
#  if defined(RTLD_GLOBAL)
#    define LT_GLOBAL RTLD_GLOBAL
#  else
#    if defined(DL_GLOBAL)
#      define LT_GLOBAL DL_GLOBAL
#    endif
#  endif /* !RTLD_GLOBAL */
#endif

#include "plugins.h"

static const int debuglevel = 50;

/*
 * Create a new plugin "class" entry and enter it in the list of plugins.
 * Note, this is not the same as an instance of the plugin.
 */
static Plugin* new_plugin()
{
  Plugin* plugin;

  plugin = (Plugin*)malloc(sizeof(Plugin));
  memset(plugin, 0, sizeof(Plugin));
  return plugin;
}

static void ClosePlugin(Plugin* plugin)
{
  if (plugin->file) {
    Dmsg1(50, "Got plugin=%s but not accepted.\n", plugin->file);
  }
  if (plugin->unloadPlugin) { plugin->unloadPlugin(); }
  if (plugin->plugin_handle) { dlclose(plugin->plugin_handle); }
  if (plugin->file) { free(plugin->file); }
  free(plugin);
}

/*
 * Load a specific plugin and check if the plugin had the correct
 * entry points, the license is compatible and initialize the plugin.
 */
static bool load_a_plugin(void* bareos_plugin_interface_version,
                          void* bareos_core_functions,
                          const char* plugin_pathname,
                          const char* plugin_name,
                          const char* type,
                          alist<Plugin*>* plugin_list,
                          bool IsPluginCompatible(Plugin* plugin))
{
  t_loadPlugin loadPlugin;
  Plugin* plugin = NULL;

  plugin = new_plugin();
  plugin->file = strdup(plugin_name);
  plugin->file_len = strstr(plugin->file, type) - plugin->file;

  plugin->plugin_handle = dlopen(plugin_pathname, LT_LAZY_OR_NOW | LT_GLOBAL);

  if (!plugin->plugin_handle) {
    const char* error = dlerror();

    Jmsg(NULL, M_ERROR, 0, _("dlopen plugin %s failed: ERR=%s\n"),
         plugin_pathname, NPRT(error));
    Dmsg2(debuglevel, "dlopen plugin %s failed: ERR=%s\n", plugin_pathname,
          NPRT(error));

    ClosePlugin(plugin);

    return false;
  }

  // Get two global entry points
  loadPlugin = (t_loadPlugin)dlsym(plugin->plugin_handle, "loadPlugin");
  if (!loadPlugin) {
    Jmsg(NULL, M_ERROR, 0,
         _("Lookup of loadPlugin in plugin %s failed: ERR=%s\n"),
         plugin_pathname, NPRT(dlerror()));
    Dmsg2(debuglevel, "Lookup of loadPlugin in plugin %s failed: ERR=%s\n",
          plugin_pathname, NPRT(dlerror()));

    ClosePlugin(plugin);

    return false;
  }

  plugin->unloadPlugin
      = (t_unloadPlugin)dlsym(plugin->plugin_handle, "unloadPlugin");
  if (!plugin->unloadPlugin) {
    Jmsg(NULL, M_ERROR, 0,
         _("Lookup of unloadPlugin in plugin %s failed: ERR=%s\n"),
         plugin_pathname, NPRT(dlerror()));
    Dmsg2(debuglevel, "Lookup of unloadPlugin in plugin %s failed: ERR=%s\n",
          plugin_pathname, NPRT(dlerror()));

    ClosePlugin(plugin);

    return false;
  }

  // Initialize the plugin
  if (loadPlugin(bareos_plugin_interface_version, bareos_core_functions,
                 &plugin->plugin_information, &plugin->plugin_functions)
      != bRC_OK) {
    ClosePlugin(plugin);

    return false;
  }

  if (!IsPluginCompatible) {
    Dmsg0(50, "Plugin compatibility pointer not set.\n");
  } else if (!IsPluginCompatible(plugin)) {
    ClosePlugin(plugin);

    return false;
  }

  plugin_list->append(plugin);

  return true;
}

/*
 * Load all the plugins in the specified directory.
 * Or when plugin_names is give it has a list of plugins
 * to load from the specified directory.
 */
bool LoadPlugins(void* bareos_plugin_interface_version,
                 void* bareos_core_functions,
                 alist<Plugin*>* plugin_list,
                 const char* plugin_dir,
                 alist<const char*>* plugin_names,
                 const char* type,
                 bool IsPluginCompatible(Plugin* plugin))
{
  struct stat statp;
  bool found = false;
  PoolMem fname(PM_FNAME);
  bool need_slash = false;
  int len;

  Dmsg0(debuglevel, "LoadPlugins\n");

  len = strlen(plugin_dir);
  if (len > 0) { need_slash = !IsPathSeparator(plugin_dir[len - 1]); }

  /*
   * See if we are loading certain plugins only or all plugins of a certain
   * type.
   */
  if (plugin_names && plugin_names->size()) {
    char* name = nullptr;
    PoolMem plugin_name(PM_FNAME);

    foreach_alist (name, plugin_names) {
      // Generate the plugin name e.g. <name>-<daemon>.so
      Mmsg(plugin_name, "%s%s", name, type);

      // Generate the full pathname to the plugin to load.
      Mmsg(fname, "%s%s%s", plugin_dir, (need_slash) ? "/" : "",
           plugin_name.c_str());

      // Make sure the plugin exists and is a regular file.
      if (lstat(fname.c_str(), &statp) != 0 || !S_ISREG(statp.st_mode)) {
        continue;
      }

      // Try to load the plugin and resolve the wanted symbols.
      if (load_a_plugin(bareos_plugin_interface_version, bareos_core_functions,
                        fname.c_str(), plugin_name.c_str(), type, plugin_list,
                        IsPluginCompatible)) {
        found = true;
      }
    }
  } else {
    int name_max, type_len;
    DIR* dp = NULL;
    struct dirent* result;
#ifdef USE_READDIR_R
    struct dirent* entry = NULL;
#endif
    name_max = pathconf(".", _PC_NAME_MAX);
    if (name_max < 1024) { name_max = 1024; }

    if (!(dp = opendir(plugin_dir))) {
      BErrNo be;
      Jmsg(NULL, M_ERROR_TERM, 0,
           _("Failed to open Plugin directory %s: ERR=%s\n"), plugin_dir,
           be.bstrerror());
      Dmsg2(debuglevel, "Failed to open Plugin directory %s: ERR=%s\n",
            plugin_dir, be.bstrerror());
      goto bail_out;
    }

#ifdef USE_READDIR_R
    entry = (struct dirent*)malloc(sizeof(struct dirent) + name_max + 1000);
    while (1) {
      if ((Readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
#else
    while (1) {
      result = readdir(dp);
      if (result == NULL) {
#endif
        if (!found) {
          Jmsg(NULL, M_WARNING, 0, _("Failed to find any plugins in %s\n"),
               plugin_dir);
          Dmsg1(debuglevel, "Failed to find any plugins in %s\n", plugin_dir);
        }
        break;
      }

      if (bstrcmp(result->d_name, ".") || bstrcmp(result->d_name, "..")) {
        continue;
      }

      len = strlen(result->d_name);
      type_len = strlen(type);
      if (len < type_len + 1
          || !bstrcmp(&result->d_name[len - type_len], type)) {
        Dmsg3(debuglevel, "Rejected plugin: want=%s name=%s len=%d\n", type,
              result->d_name, len);
        continue;
      }
      Dmsg2(debuglevel, "Found plugin: name=%s len=%d\n", result->d_name, len);

      PmStrcpy(fname, plugin_dir);
      if (need_slash) { PmStrcat(fname, "/"); }
      PmStrcat(fname, result->d_name);

      // Make sure the plugin exists and is a regular file.
      if (lstat(fname.c_str(), &statp) != 0 || !S_ISREG(statp.st_mode)) {
        continue;
      }

      // Try to load the plugin and resolve the wanted symbols.
      if (load_a_plugin(bareos_plugin_interface_version, bareos_core_functions,
                        fname.c_str(), result->d_name, type, plugin_list,
                        IsPluginCompatible)) {
        found = true;
      }
    }

#ifdef USE_READDIR_R
    if (entry) { free(entry); }
#endif
    if (dp) { closedir(dp); }
  }

bail_out:
  return found;
}

// Unload all the loaded plugins
void UnloadPlugins(alist<Plugin*>* plugin_list)
{
  int i{};
  Plugin* plugin{};

  if (!plugin_list) { return; }
  foreach_alist_index (i, plugin, plugin_list) {
    // Shut it down and unload it
    plugin->unloadPlugin();
    dlclose(plugin->plugin_handle);
    if (plugin->file) { free(plugin->file); }
    free(plugin);
  }
}

void UnloadPlugin(alist<Plugin*>* plugin_list, Plugin* plugin, int index)
{
  // Shut it down and unload it
  plugin->unloadPlugin();
  dlclose(plugin->plugin_handle);
  if (plugin->file) { free(plugin->file); }
  plugin_list->remove(index);
  free(plugin);
}

int ListPlugins(alist<Plugin*>* plugin_list, PoolMem& msg)
{
  int i{};
  int len{};
  Plugin* plugin{};

  if (plugin_list && plugin_list->size() > 0) {
    PmStrcpy(msg, "Plugin Info:\n");
    foreach_alist_index (i, plugin, plugin_list) {
      PmStrcat(msg, " Plugin     : ");
      len = PmStrcat(msg, plugin->file);
      if (plugin->plugin_information) {
        PluginInformation* info
            = (PluginInformation*)plugin->plugin_information;
        PmStrcat(msg, "\n");
        PmStrcat(msg, " Description: ");
        PmStrcat(msg, NPRT(info->plugin_description));
        PmStrcat(msg, "\n");

        PmStrcat(msg, " Version    : ");
        PmStrcat(msg, NPRT(info->plugin_version));
        PmStrcat(msg, ", Date: ");
        PmStrcat(msg, NPRT(info->plugin_date));
        PmStrcat(msg, "\n");

        PmStrcat(msg, " Author     : ");
        PmStrcat(msg, NPRT(info->plugin_author));
        PmStrcat(msg, "\n");

        PmStrcat(msg, " License    : ");
        PmStrcat(msg, NPRT(info->plugin_license));
        PmStrcat(msg, "\n");

        if (info->plugin_usage) {
          PmStrcat(msg, " Usage      : ");
          PmStrcat(msg, info->plugin_usage);
          PmStrcat(msg, "\n");
        }

        PmStrcat(msg, "\n");
      }
    }
    len = PmStrcat(msg, "\n");
  }

  return len;
}

/*
 * Dump plugin information
 * Each daemon can register a hook that will be called
 * after a fatal signal.
 */
#define DBG_MAX_HOOK 10
static dbg_plugin_hook_t* dbg_plugin_hooks[DBG_MAX_HOOK];
static dbg_print_plugin_hook_t* dbg_print_plugin_hook = NULL;
static int dbg_plugin_hook_count = 0;

void DbgPluginAddHook(dbg_plugin_hook_t* fct)
{
  ASSERT(dbg_plugin_hook_count < DBG_MAX_HOOK);
  dbg_plugin_hooks[dbg_plugin_hook_count++] = fct;
}

void DbgPrintPluginAddHook(dbg_print_plugin_hook_t* fct)
{
  dbg_print_plugin_hook = fct;
}

void DumpPlugins(alist<Plugin*>* plugin_list, FILE* fp)
{
  int i{};
  Plugin* plugin{};
  fprintf(fp, "Attempt to dump plugins. Hook count=%d\n",
          dbg_plugin_hook_count);

  if (!plugin_list) { return; }
  foreach_alist_index (i, plugin, plugin_list) {
    for (int i = 0; i < dbg_plugin_hook_count; i++) {
      //       dbg_plugin_hook_t *fct = dbg_plugin_hooks[i];
      fprintf(fp, "Plugin %p name=\"%s\"\n", plugin, plugin->file);
      //       fct(plugin, fp);
    }
  }
}

/*
 * Bounce from library to daemon and back to get the proper plugin_list.
 * As the function is called from the signal context we don't have the
 * plugin_list as argument and we don't want to expose it as global variable.
 * If the daemon didn't register a dump plugin function this is a NOP.
 */
void DbgPrintPlugin(FILE* fp)
{
  if (dbg_print_plugin_hook) { dbg_print_plugin_hook(fp); }
}