source: ldm/trunk/fuentes/src/ldmplugin.c @ 516

Last change on this file since 516 was 516, checked in by mabarracus, 5 years ago

Copy trusty code

File size: 4.7 KB
Line 
1#include <dirent.h>
2#include <dlfcn.h>
3#include <errno.h>
4#include <glib.h>
5#include <glib.h>
6#include <malloc.h>
7#include <setjmp.h>
8#include <stdio.h>
9#include <string.h>
10
11#include "ldmplugin.h"
12#include "ldmgreetercomm.h"
13#include "logging.h"
14
15GTree* plugin_list = NULL;
16gchar** plugin_names = NULL;
17static jmp_buf auth_jmp_buf;
18static gchar* current_plugin = NULL;
19
20static int g_strcmp(gconstpointer a, gconstpointer b) {
21    return strcmp((char*)a, (char*)b);
22}
23
24/*
25 * set_current_plugin
26 *  Set current plugin name
27 */
28void set_current_plugin(char *plug_name) {
29    current_plugin = plug_name;
30}
31
32/*
33 * ldm_start_plugin
34 *  Iterate over plugin_list and start plugin
35 */
36void ldm_start_plugin() {
37    LdmBackend *desc = (LdmBackend*) g_tree_lookup(plugin_list, current_plugin);
38    if(desc->start_cb)
39        desc->start_cb();
40}
41
42/*
43 * ldm_close_plugin
44 *  Iterate over plugin_list and close plugin
45 */
46void ldm_close_plugin() {
47    LdmBackend *desc = (LdmBackend*) g_tree_lookup(plugin_list, current_plugin);
48    if(desc->clean_cb)
49        desc->clean_cb();
50}
51
52/*
53 * ldm_setup_plugin
54 *  Call setup callback function of plugin
55 */
56void ldm_setup_plugin() {
57    log_entry("ldm",7,"setting up plugin: %s", current_plugin);
58    LdmBackend *desc = (LdmBackend*) g_tree_lookup(plugin_list, current_plugin);
59    if(desc->init_cb)
60        desc->init_cb();
61}
62
63/*
64 * ldm_guest_auth_plugin
65 *  Call setup guest authentication values of plugin
66 */
67void ldm_guest_auth_plugin() {
68    log_entry("ldm",7,"guest auth plugin: %s", current_plugin);
69    LdmBackend *desc = (LdmBackend*) g_tree_lookup(plugin_list, current_plugin);
70    if(desc->guest_cb)
71        desc->guest_cb();
72}
73
74/*
75 * ldm_auth_plugin
76 *  Call auth callback function of plugin
77 *  Returns 0 on success, 1 otherwise
78 */
79int ldm_auth_plugin() {
80    LdmBackend *desc = (LdmBackend*) g_tree_lookup(plugin_list, current_plugin);
81
82    if (desc->guest_cb)
83        ask_greeter("allowguest true\n");
84    else
85        ask_greeter("allowguest false\n");
86
87    switch (setjmp(auth_jmp_buf)) {
88        case AUTH_EXC_NONE:
89            if(desc->auth_cb)
90                desc->auth_cb();
91            return 0;
92        case AUTH_EXC_RELOAD_BACKEND:
93            ldm_close_plugin();
94
95            log_entry("ldm",7,"reloading backend");
96            return 1;
97        case AUTH_EXC_GUEST:
98            if(desc->guest_cb)
99                desc->guest_cb();
100            return 0;
101    }
102    return 1;
103}
104
105/*
106 * ldm_init_plugin
107 *  Init plugin function. Must be called at each plugin's init
108 */
109void __attribute__ ((visibility("default")))
110ldm_init_plugin(LdmBackend* descriptor) {
111    gchar** new_plugin_names;
112    int plugin_names_len;
113
114    plugin_names_len = g_strv_length(plugin_names);
115    new_plugin_names = g_realloc(plugin_names,
116                                 (plugin_names_len+1)*sizeof(gchar*));
117    if (new_plugin_names != plugin_names) {
118        g_free(plugin_names);
119        plugin_names = new_plugin_names;
120    }
121    plugin_names[plugin_names_len] = g_strdup(descriptor->name);
122    plugin_names[plugin_names_len+1] = NULL;
123
124    g_tree_replace(plugin_list, descriptor->name, descriptor);
125    log_entry("ldm",7,"%s initialized", descriptor->name);
126}
127
128/*
129 * _load_plugin
130 *  open plugin's lib
131 *      path -- plugin path
132 */
133void _load_plugin(const char *path) {
134    void *handle = dlopen(path, RTLD_LAZY);
135    if (handle) {
136        log_entry("ldm",7,"loaded %s", path);
137        return;
138    }
139
140    log_entry("ldm",4,"%s: Invalid LDM plugin %s", dlerror(), path);
141}
142
143/*
144 * ldm_load_plugins
145 *  Load all plugins at LDM_PLUG_DIR
146 */
147int ldm_load_plugins() {
148    plugin_list = g_tree_new(g_strcmp);
149    g_tree_ref(plugin_list);
150    plugin_names = g_malloc0(sizeof(gchar*));
151
152    DIR* plugin_dir = opendir(LDM_PLUG_DIR);
153
154    if (!plugin_dir) {
155        log_entry("ldm",3,"unable to open plugin dir: %s", LDM_PLUG_DIR);
156        return 1;
157    }
158    struct dirent* entry;
159    while ( (entry = readdir(plugin_dir)) ) {
160        if ((entry->d_type == DT_REG || entry->d_type == DT_UNKNOWN) && (strstr(entry->d_name, ".so") != NULL)) {
161            int name_len = strlen(entry->d_name)+strlen(LDM_PLUG_DIR)+2;
162            char* plug_name = (char *) malloc(name_len);
163            snprintf(plug_name, name_len, "%s/%s", LDM_PLUG_DIR, entry->d_name);
164            log_entry("ldm",7,"loading: %s", plug_name);
165
166            _load_plugin(plug_name);
167            free(plug_name);
168        }
169    }
170
171    if (errno) perror(strerror(errno));
172    closedir(plugin_dir);
173    return 0;
174}
175
176/*
177 * Called on authentication failure from an auth callback.
178 * Will unwind the stack up to the frame above the callback and handle the
179 * error before retrying.
180 */
181void ldm_raise_auth_except(LdmAuthException n) {
182    longjmp(auth_jmp_buf, n);
183}
184
185gchar** ldm_get_plugins() {
186    return plugin_names;
187}
188
Note: See TracBrowser for help on using the repository browser.