/src/FreeRDP/libfreerdp/common/addin.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /**  | 
2  |  |  * FreeRDP: A Remote Desktop Protocol Implementation  | 
3  |  |  * Addin Loader  | 
4  |  |  *  | 
5  |  |  * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>  | 
6  |  |  *  | 
7  |  |  * Licensed under the Apache License, Version 2.0 (the "License");  | 
8  |  |  * you may not use this file except in compliance with the License.  | 
9  |  |  * You may obtain a copy of the License at  | 
10  |  |  *  | 
11  |  |  *     http://www.apache.org/licenses/LICENSE-2.0  | 
12  |  |  *  | 
13  |  |  * Unless required by applicable law or agreed to in writing, software  | 
14  |  |  * distributed under the License is distributed on an "AS IS" BASIS,  | 
15  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
16  |  |  * See the License for the specific language governing permissions and  | 
17  |  |  * limitations under the License.  | 
18  |  |  */  | 
19  |  |  | 
20  |  | #include <freerdp/config.h>  | 
21  |  |  | 
22  |  | #include <stdio.h>  | 
23  |  | #include <stdlib.h>  | 
24  |  | #include <string.h>  | 
25  |  |  | 
26  |  | #include <winpr/crt.h>  | 
27  |  | #include <winpr/path.h>  | 
28  |  | #include <winpr/string.h>  | 
29  |  | #include <winpr/library.h>  | 
30  |  |  | 
31  |  | #include <freerdp/addin.h>  | 
32  |  | #include <freerdp/build-config.h>  | 
33  |  |  | 
34  |  | #include <freerdp/log.h>  | 
35  |  | #define TAG FREERDP_TAG("addin") | 
36  |  |  | 
37  |  | static INLINE BOOL is_path_required(LPCSTR path, size_t len)  | 
38  | 0  | { | 
39  | 0  |   if (!path || (len <= 1))  | 
40  | 0  |     return FALSE;  | 
41  |  |  | 
42  | 0  |   if (strcmp(path, ".") == 0)  | 
43  | 0  |     return FALSE;  | 
44  |  |  | 
45  | 0  |   return TRUE;  | 
46  | 0  | }  | 
47  |  |  | 
48  |  | LPSTR freerdp_get_library_install_path(void)  | 
49  | 0  | { | 
50  | 0  |   LPSTR pszPath = NULL;  | 
51  | 0  |   size_t cchPath = 0;  | 
52  | 0  |   size_t cchLibraryPath = 0;  | 
53  | 0  |   size_t cchInstallPrefix = 0;  | 
54  | 0  |   BOOL needLibPath = 0;  | 
55  | 0  |   BOOL needInstallPath = 0;  | 
56  | 0  |   LPCSTR pszLibraryPath = FREERDP_LIBRARY_PATH;  | 
57  | 0  |   LPCSTR pszInstallPrefix = FREERDP_INSTALL_PREFIX;  | 
58  | 0  |   cchLibraryPath = strlen(pszLibraryPath) + 1;  | 
59  | 0  |   cchInstallPrefix = strlen(pszInstallPrefix) + 1;  | 
60  | 0  |   cchPath = cchInstallPrefix + cchLibraryPath;  | 
61  | 0  |   needInstallPath = is_path_required(pszInstallPrefix, cchInstallPrefix);  | 
62  | 0  |   needLibPath = is_path_required(pszLibraryPath, cchLibraryPath);  | 
63  |  | 
  | 
64  | 0  |   if (!needInstallPath && !needLibPath)  | 
65  | 0  |     return NULL;  | 
66  |  |  | 
67  | 0  |   pszPath = (LPSTR)malloc(cchPath + 1);  | 
68  |  | 
  | 
69  | 0  |   if (!pszPath)  | 
70  | 0  |     return NULL;  | 
71  |  |  | 
72  | 0  |   if (needInstallPath)  | 
73  | 0  |   { | 
74  | 0  |     CopyMemory(pszPath, pszInstallPrefix, cchInstallPrefix);  | 
75  | 0  |     pszPath[cchInstallPrefix] = '\0';  | 
76  | 0  |   }  | 
77  |  | 
  | 
78  | 0  |   if (needLibPath)  | 
79  | 0  |   { | 
80  | 0  |     if (FAILED(NativePathCchAppendA(pszPath, cchPath + 1, pszLibraryPath)))  | 
81  | 0  |     { | 
82  | 0  |       free(pszPath);  | 
83  | 0  |       return NULL;  | 
84  | 0  |     }  | 
85  | 0  |   }  | 
86  |  |  | 
87  | 0  |   return pszPath;  | 
88  | 0  | }  | 
89  |  |  | 
90  |  | LPSTR freerdp_get_dynamic_addin_install_path(void)  | 
91  | 0  | { | 
92  |  | #if defined(WITH_ADD_PLUGIN_TO_RPATH)  | 
93  |  |   return NULL;  | 
94  |  | #else  | 
95  | 0  |   LPSTR pszPath = NULL;  | 
96  | 0  |   size_t cchPath = 0;  | 
97  | 0  |   size_t cchAddinPath = 0;  | 
98  | 0  |   size_t cchInstallPrefix = 0;  | 
99  | 0  |   BOOL needLibPath = 0;  | 
100  | 0  |   BOOL needInstallPath = 0;  | 
101  | 0  |   LPCSTR pszAddinPath = FREERDP_ADDIN_PATH;  | 
102  | 0  |   LPCSTR pszInstallPrefix = FREERDP_INSTALL_PREFIX;  | 
103  | 0  |   cchAddinPath = strlen(pszAddinPath) + 1;  | 
104  | 0  |   cchInstallPrefix = strlen(pszInstallPrefix) + 1;  | 
105  | 0  |   cchPath = cchInstallPrefix + cchAddinPath;  | 
106  | 0  |   needInstallPath = is_path_required(pszInstallPrefix, cchInstallPrefix);  | 
107  | 0  |   needLibPath = is_path_required(pszAddinPath, cchAddinPath);  | 
108  |  | 
  | 
109  | 0  |   WLog_DBG(TAG,  | 
110  | 0  |            "freerdp_get_dynamic_addin_install_path <- pszInstallPrefix: %s, pszAddinPath: %s",  | 
111  | 0  |            pszInstallPrefix, pszAddinPath);  | 
112  |  | 
  | 
113  | 0  |   if (!needInstallPath && !needLibPath)  | 
114  | 0  |     return NULL;  | 
115  |  |  | 
116  | 0  |   pszPath = (LPSTR)calloc(cchPath + 1, sizeof(CHAR));  | 
117  |  | 
  | 
118  | 0  |   if (!pszPath)  | 
119  | 0  |     return NULL;  | 
120  |  |  | 
121  | 0  |   if (needInstallPath)  | 
122  | 0  |   { | 
123  | 0  |     CopyMemory(pszPath, pszInstallPrefix, cchInstallPrefix);  | 
124  | 0  |     pszPath[cchInstallPrefix] = '\0';  | 
125  | 0  |   }  | 
126  |  | 
  | 
127  | 0  |   if (needLibPath)  | 
128  | 0  |   { | 
129  | 0  |     if (FAILED(NativePathCchAppendA(pszPath, cchPath + 1, pszAddinPath)))  | 
130  | 0  |     { | 
131  | 0  |       free(pszPath);  | 
132  | 0  |       return NULL;  | 
133  | 0  |     }  | 
134  | 0  |   }  | 
135  |  |  | 
136  | 0  |   WLog_DBG(TAG, "freerdp_get_dynamic_addin_install_path -> pszPath: %s", pszPath);  | 
137  |  | 
  | 
138  | 0  |   return pszPath;  | 
139  | 0  | #endif  | 
140  | 0  | }  | 
141  |  |  | 
142  |  | PVIRTUALCHANNELENTRY freerdp_load_dynamic_addin(LPCSTR pszFileName, LPCSTR pszPath,  | 
143  |  |                                                 LPCSTR pszEntryName)  | 
144  | 0  | { | 
145  | 0  |   LPSTR pszAddinInstallPath = freerdp_get_dynamic_addin_install_path();  | 
146  | 0  |   PVIRTUALCHANNELENTRY entry = NULL;  | 
147  | 0  |   BOOL bHasExt = TRUE;  | 
148  | 0  |   PCSTR pszExt = NULL;  | 
149  | 0  |   size_t cchExt = 0;  | 
150  | 0  |   HINSTANCE library = NULL;  | 
151  | 0  |   size_t cchFileName = 0;  | 
152  | 0  |   size_t cchFilePath = 0;  | 
153  | 0  |   LPSTR pszAddinFile = NULL;  | 
154  | 0  |   LPSTR pszFilePath = NULL;  | 
155  | 0  |   LPSTR pszRelativeFilePath = NULL;  | 
156  | 0  |   size_t cchAddinFile = 0;  | 
157  | 0  |   size_t cchAddinInstallPath = 0;  | 
158  |  | 
  | 
159  | 0  |   if (!pszFileName || !pszEntryName)  | 
160  | 0  |     goto fail;  | 
161  |  |  | 
162  | 0  |   WLog_DBG(TAG, "freerdp_load_dynamic_addin <- pszFileName: %s, pszPath: %s, pszEntryName: %s",  | 
163  | 0  |            pszFileName, pszPath, pszEntryName);  | 
164  |  | 
  | 
165  | 0  |   cchFileName = strlen(pszFileName);  | 
166  |  |  | 
167  |  |   /* Get file name with prefix and extension */  | 
168  | 0  |   if (FAILED(PathCchFindExtensionA(pszFileName, cchFileName + 1, &pszExt)))  | 
169  | 0  |   { | 
170  | 0  |     pszExt = PathGetSharedLibraryExtensionA(PATH_SHARED_LIB_EXT_WITH_DOT);  | 
171  | 0  |     cchExt = strlen(pszExt);  | 
172  | 0  |     bHasExt = FALSE;  | 
173  | 0  |   }  | 
174  |  | 
  | 
175  | 0  |   if (bHasExt)  | 
176  | 0  |   { | 
177  | 0  |     pszAddinFile = _strdup(pszFileName);  | 
178  |  | 
  | 
179  | 0  |     if (!pszAddinFile)  | 
180  | 0  |       goto fail;  | 
181  | 0  |   }  | 
182  | 0  |   else  | 
183  | 0  |   { | 
184  | 0  |     cchAddinFile = cchFileName + cchExt + 2 + sizeof(FREERDP_SHARED_LIBRARY_PREFIX);  | 
185  | 0  |     pszAddinFile = (LPSTR)malloc(cchAddinFile + 1);  | 
186  |  | 
  | 
187  | 0  |     if (!pszAddinFile)  | 
188  | 0  |       goto fail;  | 
189  |  |  | 
190  | 0  |     (void)sprintf_s(pszAddinFile, cchAddinFile, FREERDP_SHARED_LIBRARY_PREFIX "%s%s",  | 
191  | 0  |                     pszFileName, pszExt);  | 
192  | 0  |   }  | 
193  |  |  | 
194  | 0  |   cchAddinFile = strlen(pszAddinFile);  | 
195  |  |  | 
196  |  |   /* If a path is provided prefix the library name with it. */  | 
197  | 0  |   if (pszPath)  | 
198  | 0  |   { | 
199  | 0  |     size_t relPathLen = strlen(pszPath) + cchAddinFile + 1;  | 
200  | 0  |     pszRelativeFilePath = calloc(relPathLen, sizeof(CHAR));  | 
201  |  | 
  | 
202  | 0  |     if (!pszRelativeFilePath)  | 
203  | 0  |       goto fail;  | 
204  |  |  | 
205  | 0  |     (void)sprintf_s(pszRelativeFilePath, relPathLen, "%s", pszPath);  | 
206  | 0  |     const HRESULT hr = NativePathCchAppendA(pszRelativeFilePath, relPathLen, pszAddinFile);  | 
207  | 0  |     if (FAILED(hr))  | 
208  | 0  |       goto fail;  | 
209  | 0  |   }  | 
210  | 0  |   else  | 
211  | 0  |     pszRelativeFilePath = _strdup(pszAddinFile);  | 
212  |  |  | 
213  | 0  |   if (!pszRelativeFilePath)  | 
214  | 0  |     goto fail;  | 
215  |  |  | 
216  |  |   /* If a system prefix path is provided try these locations too. */  | 
217  | 0  |   if (pszAddinInstallPath)  | 
218  | 0  |   { | 
219  | 0  |     cchAddinInstallPath = strlen(pszAddinInstallPath);  | 
220  | 0  |     cchFilePath = cchAddinInstallPath + cchFileName + 32;  | 
221  | 0  |     pszFilePath = (LPSTR)malloc(cchFilePath + 1);  | 
222  |  | 
  | 
223  | 0  |     if (!pszFilePath)  | 
224  | 0  |       goto fail;  | 
225  |  |  | 
226  | 0  |     CopyMemory(pszFilePath, pszAddinInstallPath, cchAddinInstallPath);  | 
227  | 0  |     pszFilePath[cchAddinInstallPath] = '\0';  | 
228  | 0  |     const HRESULT hr = NativePathCchAppendA(pszFilePath, cchFilePath + 1, pszRelativeFilePath);  | 
229  | 0  |     if (FAILED(hr))  | 
230  | 0  |       goto fail;  | 
231  | 0  |   }  | 
232  | 0  |   else  | 
233  | 0  |     pszFilePath = _strdup(pszRelativeFilePath);  | 
234  |  |  | 
235  | 0  |   library = LoadLibraryX(pszFilePath);  | 
236  |  | 
  | 
237  | 0  |   if (!library)  | 
238  | 0  |     goto fail;  | 
239  |  |  | 
240  | 0  |   entry = GetProcAddressAs(library, pszEntryName, PVIRTUALCHANNELENTRY);  | 
241  | 0  | fail:  | 
242  | 0  |   free(pszRelativeFilePath);  | 
243  | 0  |   free(pszAddinFile);  | 
244  | 0  |   free(pszFilePath);  | 
245  | 0  |   free(pszAddinInstallPath);  | 
246  |  | 
  | 
247  | 0  |   if (!entry && library)  | 
248  | 0  |     FreeLibrary(library);  | 
249  |  | 
  | 
250  | 0  |   return entry;  | 
251  | 0  | }  | 
252  |  |  | 
253  |  | PVIRTUALCHANNELENTRY freerdp_load_dynamic_channel_addin_entry(LPCSTR pszName, LPCSTR pszSubsystem,  | 
254  |  |                                                               LPCSTR pszType, DWORD dwFlags)  | 
255  | 0  | { | 
256  | 0  |   PVIRTUALCHANNELENTRY entry = NULL;  | 
257  | 0  |   LPSTR pszFileName = NULL;  | 
258  | 0  |   const size_t cchBaseFileName = sizeof(FREERDP_SHARED_LIBRARY_PREFIX) + 32;  | 
259  | 0  |   size_t nameLen = 0;  | 
260  | 0  |   size_t subsystemLen = 0;  | 
261  | 0  |   size_t typeLen = 0;  | 
262  | 0  |   size_t cchFileName = 0;  | 
263  |  | 
  | 
264  | 0  |   if (pszName)  | 
265  | 0  |     nameLen = strnlen(pszName, MAX_PATH);  | 
266  | 0  |   if (pszSubsystem)  | 
267  | 0  |     subsystemLen = strnlen(pszSubsystem, MAX_PATH);  | 
268  | 0  |   if (pszType)  | 
269  | 0  |     typeLen = strnlen(pszType, MAX_PATH);  | 
270  |  | 
  | 
271  | 0  |   if (pszName && pszSubsystem && pszType)  | 
272  | 0  |   { | 
273  | 0  |     cchFileName = cchBaseFileName + nameLen + subsystemLen + typeLen;  | 
274  | 0  |     pszFileName = (LPSTR)malloc(cchFileName);  | 
275  |  | 
  | 
276  | 0  |     if (!pszFileName)  | 
277  | 0  |       return NULL;  | 
278  |  |  | 
279  | 0  |     (void)sprintf_s(pszFileName, cchFileName, "%s-client-%s-%s", pszName, pszSubsystem,  | 
280  | 0  |                     pszType);  | 
281  | 0  |   }  | 
282  | 0  |   else if (pszName && pszSubsystem)  | 
283  | 0  |   { | 
284  | 0  |     cchFileName = cchBaseFileName + nameLen + subsystemLen;  | 
285  | 0  |     pszFileName = (LPSTR)malloc(cchFileName);  | 
286  |  | 
  | 
287  | 0  |     if (!pszFileName)  | 
288  | 0  |       return NULL;  | 
289  |  |  | 
290  | 0  |     (void)sprintf_s(pszFileName, cchFileName, "%s-client-%s", pszName, pszSubsystem);  | 
291  | 0  |   }  | 
292  | 0  |   else if (pszName)  | 
293  | 0  |   { | 
294  | 0  |     cchFileName = cchBaseFileName + nameLen;  | 
295  | 0  |     pszFileName = (LPSTR)malloc(cchFileName);  | 
296  |  | 
  | 
297  | 0  |     if (!pszFileName)  | 
298  | 0  |       return NULL;  | 
299  |  |  | 
300  | 0  |     (void)sprintf_s(pszFileName, cchFileName, "%s-client", pszName);  | 
301  | 0  |   }  | 
302  | 0  |   else  | 
303  | 0  |   { | 
304  | 0  |     return NULL;  | 
305  | 0  |   }  | 
306  |  |  | 
307  | 0  |   { | 
308  | 0  |     LPCSTR pszExtension = PathGetSharedLibraryExtensionA(0);  | 
309  | 0  |     const char pszPrefix[] = FREERDP_SHARED_LIBRARY_PREFIX;  | 
310  | 0  |     int rc = 0;  | 
311  |  | 
  | 
312  | 0  |     cchFileName += strnlen(pszPrefix, ARRAYSIZE(pszPrefix));  | 
313  | 0  |     if (pszExtension)  | 
314  | 0  |       cchFileName += strnlen(pszExtension, MAX_PATH) + 1;  | 
315  | 0  |     LPSTR tmp = calloc(cchFileName, sizeof(CHAR));  | 
316  | 0  |     if (tmp)  | 
317  | 0  |       rc = sprintf_s(tmp, cchFileName, "%s%s.%s", pszPrefix, pszFileName, pszExtension);  | 
318  |  | 
  | 
319  | 0  |     free(pszFileName);  | 
320  | 0  |     pszFileName = tmp;  | 
321  | 0  |     if (!pszFileName || (rc < 0))  | 
322  | 0  |     { | 
323  | 0  |       free(pszFileName);  | 
324  | 0  |       return NULL;  | 
325  | 0  |     }  | 
326  | 0  |   }  | 
327  |  |  | 
328  | 0  |   if (pszSubsystem)  | 
329  | 0  |   { | 
330  | 0  |     LPSTR pszEntryName = NULL;  | 
331  | 0  |     size_t cchEntryName = 0;  | 
332  |  |     /* subsystem add-in */  | 
333  | 0  |     cchEntryName = 64 + nameLen;  | 
334  | 0  |     pszEntryName = (LPSTR)malloc(cchEntryName + 1);  | 
335  |  | 
  | 
336  | 0  |     if (!pszEntryName)  | 
337  | 0  |     { | 
338  | 0  |       free(pszFileName);  | 
339  | 0  |       return NULL;  | 
340  | 0  |     }  | 
341  |  |  | 
342  | 0  |     (void)sprintf_s(pszEntryName, cchEntryName + 1, "freerdp_%s_client_subsystem_entry",  | 
343  | 0  |                     pszName);  | 
344  | 0  |     entry = freerdp_load_dynamic_addin(pszFileName, NULL, pszEntryName);  | 
345  | 0  |     free(pszEntryName);  | 
346  | 0  |     free(pszFileName);  | 
347  | 0  |     return entry;  | 
348  | 0  |   }  | 
349  |  |  | 
350  |  |   /* channel add-in */  | 
351  |  |  | 
352  | 0  |   if (dwFlags & FREERDP_ADDIN_CHANNEL_STATIC)  | 
353  | 0  |   { | 
354  | 0  |     if (dwFlags & FREERDP_ADDIN_CHANNEL_ENTRYEX)  | 
355  | 0  |       entry = freerdp_load_dynamic_addin(pszFileName, NULL, "VirtualChannelEntryEx");  | 
356  | 0  |     else  | 
357  | 0  |       entry = freerdp_load_dynamic_addin(pszFileName, NULL, "VirtualChannelEntry");  | 
358  | 0  |   }  | 
359  | 0  |   else if (dwFlags & FREERDP_ADDIN_CHANNEL_DYNAMIC)  | 
360  | 0  |     entry = freerdp_load_dynamic_addin(pszFileName, NULL, "DVCPluginEntry");  | 
361  | 0  |   else if (dwFlags & FREERDP_ADDIN_CHANNEL_DEVICE)  | 
362  | 0  |     entry = freerdp_load_dynamic_addin(pszFileName, NULL, "DeviceServiceEntry");  | 
363  | 0  |   else  | 
364  | 0  |     entry = freerdp_load_dynamic_addin(pszFileName, NULL, pszType);  | 
365  |  | 
  | 
366  | 0  |   free(pszFileName);  | 
367  | 0  |   return entry;  | 
368  | 0  | }  | 
369  |  |  | 
370  |  | static FREERDP_LOAD_CHANNEL_ADDIN_ENTRY_FN freerdp_load_static_channel_addin_entry = NULL;  | 
371  |  |  | 
372  |  | int freerdp_register_addin_provider(FREERDP_LOAD_CHANNEL_ADDIN_ENTRY_FN provider, DWORD dwFlags)  | 
373  | 6.93k  | { | 
374  | 6.93k  |   freerdp_load_static_channel_addin_entry = provider;  | 
375  | 6.93k  |   return 0;  | 
376  | 6.93k  | }  | 
377  |  |  | 
378  |  | FREERDP_LOAD_CHANNEL_ADDIN_ENTRY_FN freerdp_get_current_addin_provider(void)  | 
379  | 0  | { | 
380  | 0  |   return freerdp_load_static_channel_addin_entry;  | 
381  | 0  | }  | 
382  |  |  | 
383  |  | PVIRTUALCHANNELENTRY freerdp_load_channel_addin_entry(LPCSTR pszName, LPCSTR pszSubsystem,  | 
384  |  |                                                       LPCSTR pszType, DWORD dwFlags)  | 
385  | 0  | { | 
386  | 0  |   PVIRTUALCHANNELENTRY entry = NULL;  | 
387  |  | 
  | 
388  | 0  |   if (freerdp_load_static_channel_addin_entry)  | 
389  | 0  |     entry = freerdp_load_static_channel_addin_entry(pszName, pszSubsystem, pszType, dwFlags);  | 
390  |  | 
  | 
391  | 0  |   if (!entry)  | 
392  | 0  |     entry = freerdp_load_dynamic_channel_addin_entry(pszName, pszSubsystem, pszType, dwFlags);  | 
393  |  | 
  | 
394  | 0  |   if (!entry)  | 
395  | 0  |     WLog_WARN(TAG, "Failed to load channel %s [%s]", pszName, pszSubsystem);  | 
396  |  | 
  | 
397  | 0  |   return entry;  | 
398  | 0  | }  |