gwenhywfar
4.3.3
|
00001 /*************************************************************************** 00002 begin : Thu Sep 11 2003 00003 copyright : (C) 2003 by Martin Preuss 00004 email : martin@libchipcard.de 00005 00006 *************************************************************************** 00007 * * 00008 * This library is free software; you can redistribute it and/or * 00009 * modify it under the terms of the GNU Lesser General Public * 00010 * License as published by the Free Software Foundation; either * 00011 * version 2.1 of the License, or (at your option) any later version. * 00012 * * 00013 * This library is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00016 * Lesser General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU Lesser General Public * 00019 * License along with this library; if not, write to the Free Software * 00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * 00021 * MA 02111-1307 USA * 00022 * * 00023 ***************************************************************************/ 00024 00025 00026 #ifdef HAVE_CONFIG_H 00027 # include <config.h> 00028 #endif 00029 00030 /* Internationalization */ 00031 #ifdef ENABLE_NLS 00032 # include <libintl.h> 00033 # include <locale.h> 00034 #endif 00035 00036 00037 #include <gwenhywfar/gwenhywfar.h> 00038 #include <gwenhywfar/directory.h> 00039 #include <gwenhywfar/pathmanager.h> 00040 00041 #include "base/debug.h" 00042 #include "base/logger_l.h" 00043 00044 #include "base/error_l.h" 00045 #include "base/memory_l.h" 00046 #include "base/pathmanager_l.h" 00047 #include "base/plugin_l.h" 00048 #include "base/i18n_l.h" 00049 00050 #include "os/inetaddr_l.h" 00051 #include "os/inetsocket_l.h" 00052 #include "os/libloader_l.h" 00053 #include "os/process_l.h" 00054 00055 #include "parser/dbio_l.h" 00056 #include "parser/configmgr_l.h" 00057 #include "crypt3/cryptkey_l.h" 00058 #include "crypttoken/ctplugin_l.h" 00059 00060 #include "binreloc.h" 00061 00062 #include <gnutls/gnutls.h> 00063 00064 00065 /* for regkey stuff */ 00066 #ifdef OS_WIN32 00067 # define DIRSEP "\\" 00068 # include <windows.h> 00069 #else 00070 # define DIRSEP "/" 00071 #endif 00072 00073 00074 /* Watch out: Make sure these are identical with the identifiers 00075 in gwenhywfar.iss.in ! */ 00076 #define GWEN_REGKEY_PATHS "Software\\Gwenhywfar\\Paths" 00077 #define GWEN_REGNAME_PREFIX "prefix" 00078 #define GWEN_REGNAME_LIBDIR "libdir" 00079 #define GWEN_REGNAME_PLUGINDIR "plugindir" 00080 #define GWEN_REGNAME_SYSCONFDIR "sysconfdir" 00081 #define GWEN_REGNAME_LOCALEDIR "localedir" 00082 #define GWEN_REGNAME_DATADIR "pkgdatadir" 00083 #define GWEN_REGNAME_SYSDATADIR "sysdatadir" 00084 00085 00086 00087 static unsigned int gwen_is_initialized=0; 00088 static int gwen_binreloc_initialized=0; 00089 00090 char *GWEN__get_plugindir (const char *default_dir); 00091 00092 int GWEN_Init(void) { 00093 int err; 00094 00095 if (gwen_is_initialized==0) { 00096 err=GWEN_Memory_ModuleInit(); 00097 if (err) 00098 return err; 00099 err=GWEN_Logger_ModuleInit(); 00100 if (err) 00101 return err; 00102 00103 gnutls_global_init(); 00104 00105 if (gwen_binreloc_initialized==0) { 00106 #ifdef ENABLE_BINRELOC 00107 BrInitError br_error; 00108 00109 /* Init binreloc. Note: It is not totally clear whether the correct 00110 function might still be br_init() instead of br_init_lib(). */ 00111 if (!br_init_lib(&br_error)) { 00112 DBG_INFO(GWEN_LOGDOMAIN, "Error on br_init: %d\n", br_error); 00113 gwen_binreloc_initialized=-1; 00114 } 00115 else 00116 gwen_binreloc_initialized=1; 00117 #else 00118 gwen_binreloc_initialized=1; 00119 #endif 00120 } 00121 00122 GWEN_Error_ModuleInit(); 00123 00124 err=GWEN_PathManager_ModuleInit(); 00125 if (err) 00126 return err; 00127 00128 /* Define some paths used by gwenhywfar; add the windows 00129 registry entries first, because on Unix those functions 00130 simply do nothing and on windows they will ensure that the 00131 most valid paths (which are those from the registry) are 00132 first in the path lists. */ 00133 00134 /* --------------------------------------------------------------------- 00135 * $sysconfdir e.g. "/etc" */ 00136 GWEN_PathManager_DefinePath(GWEN_PM_LIBNAME, GWEN_PM_SYSCONFDIR); 00137 GWEN_PathManager_AddPathFromWinReg(GWEN_PM_LIBNAME, 00138 GWEN_PM_LIBNAME, 00139 GWEN_PM_SYSCONFDIR, 00140 GWEN_REGKEY_PATHS, 00141 GWEN_REGNAME_SYSCONFDIR); 00142 #if defined(OS_WIN32) || defined(ENABLE_LOCAL_INSTALL) 00143 /* add folder relative to EXE */ 00144 GWEN_PathManager_AddRelPath(GWEN_PM_LIBNAME, 00145 GWEN_PM_LIBNAME, 00146 GWEN_PM_SYSCONFDIR, 00147 GWEN_SYSCONF_DIR, 00148 GWEN_PathManager_RelModeExe); 00149 #else 00150 /* add absolute folder */ 00151 GWEN_PathManager_AddPath(GWEN_PM_LIBNAME, 00152 GWEN_PM_LIBNAME, 00153 GWEN_PM_SYSCONFDIR, 00154 GWEN_SYSCONF_DIR); 00155 #endif 00156 00157 /* --------------------------------------------------------------------- 00158 * $localedir e.g. "/usr/share/locale" */ 00159 GWEN_PathManager_DefinePath(GWEN_PM_LIBNAME, GWEN_PM_LOCALEDIR); 00160 GWEN_PathManager_AddPathFromWinReg(GWEN_PM_LIBNAME, 00161 GWEN_PM_LIBNAME, 00162 GWEN_PM_LOCALEDIR, 00163 GWEN_REGKEY_PATHS, 00164 GWEN_REGNAME_LOCALEDIR); 00165 #if defined(OS_WIN32) || defined(ENABLE_LOCAL_INSTALL) 00166 /* add folder relative to EXE */ 00167 GWEN_PathManager_AddRelPath(GWEN_PM_LIBNAME, 00168 GWEN_PM_LIBNAME, 00169 GWEN_PM_LOCALEDIR, 00170 LOCALEDIR, 00171 GWEN_PathManager_RelModeExe); 00172 #else 00173 /* add absolute folder */ 00174 GWEN_PathManager_AddPath(GWEN_PM_LIBNAME, 00175 GWEN_PM_LIBNAME, 00176 GWEN_PM_LOCALEDIR, 00177 LOCALEDIR); 00178 #endif 00179 00180 /* --------------------------------------------------------------------- 00181 * $plugindir e.g. "/usr/lib/gwenhywfar/plugins/0" */ 00182 GWEN_PathManager_DefinePath(GWEN_PM_LIBNAME, GWEN_PM_PLUGINDIR); 00183 GWEN_PathManager_AddPathFromWinReg(GWEN_PM_LIBNAME, 00184 GWEN_PM_LIBNAME, 00185 GWEN_PM_PLUGINDIR, 00186 GWEN_REGKEY_PATHS, 00187 GWEN_REGNAME_PLUGINDIR); 00188 #if defined(OS_WIN32) || defined(ENABLE_LOCAL_INSTALL) 00189 /* add folder relative to EXE */ 00190 GWEN_PathManager_AddRelPath(GWEN_PM_LIBNAME, 00191 GWEN_PM_LIBNAME, 00192 GWEN_PM_PLUGINDIR, 00193 PLUGINDIR, 00194 GWEN_PathManager_RelModeExe); 00195 #else 00196 /* add absolute folder */ 00197 GWEN_PathManager_AddPath(GWEN_PM_LIBNAME, 00198 GWEN_PM_LIBNAME, 00199 GWEN_PM_PLUGINDIR, 00200 PLUGINDIR); 00201 #endif 00202 00203 /* --------------------------------------------------------------------- 00204 * datadir e.g. "/usr/share/gwenhywfar" */ 00205 GWEN_PathManager_DefinePath(GWEN_PM_LIBNAME, GWEN_PM_DATADIR); 00206 GWEN_PathManager_AddPathFromWinReg(GWEN_PM_LIBNAME, 00207 GWEN_PM_LIBNAME, 00208 GWEN_PM_DATADIR, 00209 GWEN_REGKEY_PATHS, 00210 GWEN_REGNAME_DATADIR); 00211 #if defined(OS_WIN32) || defined(ENABLE_LOCAL_INSTALL) 00212 /* add folder relative to EXE */ 00213 GWEN_PathManager_AddRelPath(GWEN_PM_LIBNAME, 00214 GWEN_PM_LIBNAME, 00215 GWEN_PM_DATADIR, 00216 GWEN_DATADIR, 00217 GWEN_PathManager_RelModeExe); 00218 #else 00219 /* add absolute folder */ 00220 GWEN_PathManager_AddPath(GWEN_PM_LIBNAME, 00221 GWEN_PM_LIBNAME, 00222 GWEN_PM_DATADIR, 00223 GWEN_DATADIR); 00224 #endif 00225 00226 /* --------------------------------------------------------------------- 00227 * system datadir e.g. "/usr/share" */ 00228 GWEN_PathManager_DefinePath(GWEN_PM_LIBNAME, GWEN_PM_SYSDATADIR); 00229 GWEN_PathManager_AddPathFromWinReg(GWEN_PM_LIBNAME, 00230 GWEN_PM_LIBNAME, 00231 GWEN_PM_SYSDATADIR, 00232 GWEN_REGKEY_PATHS, 00233 GWEN_REGNAME_SYSDATADIR); 00234 #if defined(OS_WIN32) || defined(ENABLE_LOCAL_INSTALL) 00235 /* add folder relative to EXE */ 00236 GWEN_PathManager_AddRelPath(GWEN_PM_LIBNAME, 00237 GWEN_PM_LIBNAME, 00238 GWEN_PM_SYSDATADIR, 00239 GWEN_SYSDATADIR, 00240 GWEN_PathManager_RelModeExe); 00241 #else 00242 /* add absolute folder */ 00243 GWEN_PathManager_AddPath(GWEN_PM_LIBNAME, 00244 GWEN_PM_LIBNAME, 00245 GWEN_PM_SYSDATADIR, 00246 GWEN_SYSDATADIR); 00247 #endif 00248 00249 /* Initialize other modules. */ 00250 DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing I18N module"); 00251 err=GWEN_I18N_ModuleInit(); 00252 if (err) 00253 return err; 00254 DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing InetAddr module"); 00255 err=GWEN_InetAddr_ModuleInit(); 00256 if (err) 00257 return err; 00258 DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing Socket module"); 00259 err=GWEN_Socket_ModuleInit(); 00260 if (err) 00261 return err; 00262 DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing Libloader module"); 00263 err=GWEN_LibLoader_ModuleInit(); 00264 if (err) 00265 return err; 00266 DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing Crypt3 module"); 00267 err=GWEN_Crypt3_ModuleInit(); 00268 if (err) 00269 return err; 00270 DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing Process module"); 00271 err=GWEN_Process_ModuleInit(); 00272 if (err) 00273 return err; 00274 DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing Plugin module"); 00275 err=GWEN_Plugin_ModuleInit(); 00276 if (err) 00277 return err; 00278 DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing DataBase IO module"); 00279 err=GWEN_DBIO_ModuleInit(); 00280 if (err) 00281 return err; 00282 DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing ConfigMgr module"); 00283 err=GWEN_ConfigMgr_ModuleInit(); 00284 if (err) 00285 return err; 00286 DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing CryptToken2 module"); 00287 err=GWEN_Crypt_Token_ModuleInit(); 00288 if (err) 00289 return err; 00290 /* add more modules here */ 00291 00292 } 00293 gwen_is_initialized++; 00294 00295 return 0; 00296 00297 } 00298 00299 00300 00301 int GWEN_Fini(void) { 00302 int err; 00303 00304 err=0; 00305 00306 if (gwen_is_initialized==0) 00307 return 0; 00308 00309 gwen_is_initialized--; 00310 if (gwen_is_initialized==0) { 00311 int lerr; 00312 00313 /* add more modules here */ 00314 lerr=GWEN_Crypt_Token_ModuleFini(); 00315 if (lerr) { 00316 err=lerr; 00317 DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: " 00318 "Could not deinitialze module CryptToken2"); 00319 } 00320 lerr=GWEN_ConfigMgr_ModuleFini(); 00321 if (lerr) { 00322 err=lerr; 00323 DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: " 00324 "Could not deinitialze module ConfigMgr"); 00325 } 00326 lerr=GWEN_DBIO_ModuleFini(); 00327 if (lerr) { 00328 err=lerr; 00329 DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: " 00330 "Could not deinitialze module DBIO"); 00331 } 00332 lerr=GWEN_Plugin_ModuleFini(); 00333 if (lerr) { 00334 err=lerr; 00335 DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: " 00336 "Could not deinitialze module Plugin"); 00337 } 00338 lerr=GWEN_Process_ModuleFini(); 00339 if (lerr) { 00340 err=lerr; 00341 DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: " 00342 "Could not deinitialze module Process"); 00343 } 00344 lerr=GWEN_Crypt3_ModuleFini(); 00345 if (lerr) { 00346 err=lerr; 00347 DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: " 00348 "Could not deinitialze module Crypt3"); 00349 } 00350 lerr=GWEN_LibLoader_ModuleFini(); 00351 if (lerr) { 00352 err=lerr; 00353 DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: " 00354 "Could not deinitialze module LibLoader"); 00355 } 00356 lerr=GWEN_Socket_ModuleFini(); 00357 if (lerr) { 00358 err=lerr; 00359 DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: " 00360 "Could not deinitialze module Socket"); 00361 } 00362 lerr=GWEN_InetAddr_ModuleFini(); 00363 if (lerr) { 00364 err=lerr; 00365 DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: " 00366 "Could not deinitialze module InetAddr"); 00367 } 00368 00369 lerr=GWEN_I18N_ModuleFini(); 00370 if (lerr) { 00371 err=lerr; 00372 DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: " 00373 "Could not deinitialze module I18N"); 00374 } 00375 00376 lerr=GWEN_PathManager_ModuleFini(); 00377 if (lerr) { 00378 err=lerr; 00379 DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: " 00380 "Could not deinitialze module PathManager"); 00381 } 00382 00383 GWEN_Error_ModuleFini(); 00384 00385 /* these two modules must be deinitialized at last */ 00386 lerr=GWEN_Logger_ModuleFini(); 00387 if (lerr) { 00388 err=lerr; 00389 DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: " 00390 "Could not deinitialze module Logger"); 00391 } 00392 00393 lerr=GWEN_Memory_ModuleFini(); 00394 if (lerr) { 00395 err=lerr; 00396 DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: " 00397 "Could not deinitialze module Memory"); 00398 } 00399 00400 gnutls_global_deinit(); 00401 00402 } 00403 00404 return err; 00405 } 00406 00407 00408 00409 int GWEN_Fini_Forced(void) { 00410 if (gwen_is_initialized) 00411 gwen_is_initialized=1; 00412 return GWEN_Fini(); 00413 } 00414 00415 00416 00417 void GWEN_Version(int *major, 00418 int *minor, 00419 int *patchlevel, 00420 int *build){ 00421 *major=GWENHYWFAR_VERSION_MAJOR; 00422 *minor=GWENHYWFAR_VERSION_MINOR; 00423 *patchlevel=GWENHYWFAR_VERSION_PATCHLEVEL; 00424 *build=GWENHYWFAR_VERSION_BUILD; 00425 } 00426 00427 00428