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

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