gwenhywfar.c

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