1 /*
2   Copyright 2020 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 #include <enterprise_extension.h>
26 
27 #include <known_dirs.h>
28 #include <misc_lib.h>
29 
30 #include <pthread.h>
31 
32 /*
33  * A note regarding the loading of the extension plugins:
34  *
35  * The extension plugin was originally statically linked into each agent,
36  * but was then refactored into plugins.
37  * Therefore, since it hasn't been written according to plugin guidelines,
38  * it is not safe to assume that we can unload it once we have
39  * loaded it, since it may allocate resources that are not freed. It is also
40  * not safe to assume that we can load it after initialization is complete
41  * (for example if the plugin is dropped into the directory after cf-serverd
42  * has already been running for some time), because some data structures may
43  * not have been initialized.
44  *
45  * Therefore, the load strategy is as follows:
46  *
47  * - If we find the plugin immediately, load it, and keep it loaded.
48  *
49  * - If we don't find the plugin, NEVER attempt to load it again afterwards.
50  *
51  * - Never unload the plugin.
52  *
53  * - Any installation/upgrade/removal of the plugin requires daemon restarts.
54  *
55  * - The exception is for testing (see getenv below).
56  */
57 
58 #ifndef BUILTIN_EXTENSIONS
59 
60 static bool enable_extension_libraries = true; /* GLOBAL_X */
61 static bool attempted_loading = false; /* GLOBAL_X */
62 
extension_libraries_disable()63 void extension_libraries_disable()
64 {
65     if (attempted_loading)
66     {
67         ProgrammingError("extension_libraries_disable() MUST be called before any call to extension functions");
68     }
69     enable_extension_libraries = false;
70 }
71 
extension_library_open(const char * name)72 void *extension_library_open(const char *name)
73 {
74     if (!enable_extension_libraries)
75     {
76         return NULL;
77     }
78 
79     if (getenv("CFENGINE_TEST_OVERRIDE_EXTENSION_LIBRARY_DO_CLOSE") == NULL)
80     {
81         // Only do loading checks if we are not doing tests.
82         attempted_loading = true;
83     }
84 
85     const char *dirs_to_try[3] = { NULL };
86     const char *dir = getenv("CFENGINE_TEST_OVERRIDE_EXTENSION_LIBRARY_DIR");
87     char lib[] = "/lib";
88     if (dir)
89     {
90         lib[0] = '\0';
91         dirs_to_try[0] = dir;
92     }
93     else
94     {
95         dirs_to_try[0] = GetWorkDir();
96         if (strcmp(WORKDIR, dirs_to_try[0]) != 0)
97         {
98             // try to load from the real WORKDIR in case GetWorkDir returned the local workdir to the user
99             // We try this because enterprise "make install" is in WORKDIR, not per user
100             dirs_to_try[1] = WORKDIR;
101         }
102     }
103 
104     void *handle = NULL;
105     for (int i = 0; dirs_to_try[i]; i++)
106     {
107         char path[strlen(dirs_to_try[i]) + strlen(lib) + strlen(name) + 2];
108         xsnprintf(path, sizeof(path), "%s%s/%s", dirs_to_try[i], lib, name);
109 
110         Log(LOG_LEVEL_DEBUG, "Trying to shlib_open extension plugin '%s' from '%s'", name, path);
111 
112         handle = shlib_open(path);
113         if (handle)
114         {
115             Log(LOG_LEVEL_VERBOSE, "Successfully opened extension plugin '%s' from '%s'", name, path);
116             break;
117         }
118         else
119         {
120             const char *error;
121             if (errno == ENOENT)
122             {
123                 error = "(not installed)";
124             }
125             else
126             {
127                 error = GetErrorStr();
128             }
129             Log(LOG_LEVEL_VERBOSE, "Could not open extension plugin '%s' from '%s': %s", name, path, error);
130         }
131     }
132 
133     if (!handle)
134     {
135         return handle;
136     }
137 
138     // Version check, to avoid binary incompatible plugins.
139     const char * (*GetExtensionLibraryVersion)() = shlib_load(handle, "GetExtensionLibraryVersion");
140     if (!GetExtensionLibraryVersion)
141     {
142         Log(LOG_LEVEL_ERR, "Could not retrieve version from extension plugin (%s). Not loading the plugin.", name);
143         goto close_and_fail;
144     }
145 
146     const char *plugin_version = GetExtensionLibraryVersion();
147     unsigned int bin_major, bin_minor, bin_patch;
148     unsigned int plug_major, plug_minor, plug_patch;
149     if (sscanf(VERSION, "%u.%u.%u", &bin_major, &bin_minor, &bin_patch) != 3)
150     {
151         Log(LOG_LEVEL_ERR, "Not able to extract version number from binary (%s). Not loading extension plugin.", name);
152         goto close_and_fail;
153     }
154     if (sscanf(plugin_version, "%u.%u.%u", &plug_major, &plug_minor, &plug_patch) != 3)
155     {
156         Log(LOG_LEVEL_ERR, "Not able to extract version number from plugin (%s). Not loading extension plugin.", name);
157         goto close_and_fail;
158     }
159 
160     if (bin_major != plug_major || bin_minor != plug_minor || bin_patch != plug_patch)
161     {
162         Log(LOG_LEVEL_ERR, "Extension plugin version does not match CFEngine Community version "
163             "(CFEngine Community v%u.%u.%u, Extension (%s) v%u.%u.%u). Refusing to load it.",
164             bin_major, bin_minor, bin_patch, name, plug_major, plug_minor, plug_patch);
165         goto close_and_fail;
166     }
167 
168     Log(LOG_LEVEL_VERBOSE, "Successfully loaded extension plugin '%s'", name);
169 
170     return handle;
171 
172 close_and_fail:
173     shlib_close(handle);
174     return NULL;
175 }
176 
extension_library_close(void * handle)177 void extension_library_close(void *handle)
178 {
179     shlib_close(handle);
180 }
181 
182 #endif // !BUILTIN_EXTENSIONS
183