gwenhywfar.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  $RCSfile$
00003  -------------------
00004  cvs         : $Id: gwenhywfar.c 1393 2007-11-22 18:26:05Z martin $
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 "crypt3/cryptkey_l.h"
00061 #include "crypttoken/ctplugin_l.h"
00062 #include "iolayer/iomanager_l.h"
00063 
00064 #include "binreloc.h"
00065 
00066 /* for regkey stuff */
00067 #ifdef OS_WIN32
00068 # define DIRSEP "\\"
00069 # include <windows.h>
00070 #else
00071 # define DIRSEP "/"
00072 #endif
00073 
00074 
00075 /* Watch out: Make sure these are identical with the identifiers
00076    in gwenhywfar.iss.in ! */
00077 #define GWEN_REGKEY_PATHS        "Software\\Gwenhywfar\\Paths"
00078 #define GWEN_REGNAME_PREFIX      "prefix"
00079 #define GWEN_REGNAME_LIBDIR      "libdir"
00080 #define GWEN_REGNAME_PLUGINDIR   "plugindir"
00081 #define GWEN_REGNAME_SYSCONFDIR  "sysconfdir"
00082 #define GWEN_REGNAME_LOCALEDIR   "localedir"
00083 #define GWEN_REGNAME_DATADIR     "pkgdatadir"
00084 
00085 
00086 static unsigned int gwen_is_initialized=0;
00087 static int gwen_binreloc_initialized=0;
00088 
00089 char *GWEN__get_plugindir (const char *default_dir);
00090 
00091 int GWEN_Init() {
00092   int err;
00093 
00094   if (gwen_is_initialized==0) {
00095     err=GWEN_Memory_ModuleInit();
00096     if (err)
00097       return err;
00098     err=GWEN_Logger_ModuleInit();
00099     if (err)
00100       return err;
00101 
00102     if (gwen_binreloc_initialized==0) {
00103       BrInitError br_error;
00104 
00105       /* Init binreloc. Note: It is not totally clear whether the correct
00106        function might still be br_init() instead of br_init_lib(). */
00107       if (!br_init_lib(&br_error)) {
00108         DBG_INFO(GWEN_LOGDOMAIN, "Error on br_init: %d\n", br_error);
00109         gwen_binreloc_initialized=-1;
00110       }
00111       else
00112         gwen_binreloc_initialized=1;
00113     }
00114 
00115     GWEN_Error_ModuleInit();
00116 
00117     err=GWEN_PathManager_ModuleInit();
00118     if (err)
00119       return err;
00120 
00121     /* Define some paths used by gwenhywfar; add the windows
00122        registry entries first, because on Unix those functions
00123        simply do nothing and on windows they will ensure that the
00124        most valid paths (which are those from the registry) are
00125        first in the path lists. */
00126 
00127     /* ---------------------------------------------------------------------
00128      * $sysconfdir e.g. "/etc" */
00129     GWEN_PathManager_DefinePath(GWEN_PM_LIBNAME, GWEN_PM_SYSCONFDIR);
00130     GWEN_PathManager_AddPathFromWinReg(GWEN_PM_LIBNAME,
00131                                        GWEN_PM_LIBNAME,
00132                                        GWEN_PM_SYSCONFDIR,
00133                                        GWEN_REGKEY_PATHS,
00134                                        GWEN_REGNAME_SYSCONFDIR);
00135 #if defined(OS_WIN32) || defined(ENABLE_LOCAL_INSTALL)
00136     /* add folder relative to EXE */
00137     GWEN_PathManager_AddRelPath(GWEN_PM_LIBNAME,
00138                                 GWEN_PM_LIBNAME,
00139                                 GWEN_PM_SYSCONFDIR,
00140                                 GWEN_SYSCONF_DIR,
00141                                 GWEN_PathManager_RelModeExe);
00142 #else
00143     /* add absolute folder */
00144     GWEN_PathManager_AddPath(GWEN_PM_LIBNAME,
00145                              GWEN_PM_LIBNAME,
00146                              GWEN_PM_SYSCONFDIR,
00147                              GWEN_SYSCONF_DIR);
00148 #endif
00149 
00150     /* ---------------------------------------------------------------------
00151      * $localedir e.g. "/usr/share/locale" */
00152     GWEN_PathManager_DefinePath(GWEN_PM_LIBNAME, GWEN_PM_LOCALEDIR);
00153     GWEN_PathManager_AddPathFromWinReg(GWEN_PM_LIBNAME,
00154                                        GWEN_PM_LIBNAME,
00155                                        GWEN_PM_LOCALEDIR,
00156                                        GWEN_REGKEY_PATHS,
00157                                        GWEN_REGNAME_LOCALEDIR);
00158 #if defined(OS_WIN32) || defined(ENABLE_LOCAL_INSTALL)
00159     /* add folder relative to EXE */
00160     GWEN_PathManager_AddRelPath(GWEN_PM_LIBNAME,
00161                                 GWEN_PM_LIBNAME,
00162                                 GWEN_PM_LOCALEDIR,
00163                                 LOCALEDIR,
00164                                 GWEN_PathManager_RelModeExe);
00165 #else
00166     /* add absolute folder */
00167     GWEN_PathManager_AddPath(GWEN_PM_LIBNAME,
00168                              GWEN_PM_LIBNAME,
00169                              GWEN_PM_LOCALEDIR,
00170                              LOCALEDIR);
00171 #endif
00172 
00173     /* ---------------------------------------------------------------------
00174      * $plugindir e.g. "/usr/lib/gwenhywfar/plugins/0" */
00175     GWEN_PathManager_DefinePath(GWEN_PM_LIBNAME, GWEN_PM_PLUGINDIR);
00176     GWEN_PathManager_AddPathFromWinReg(GWEN_PM_LIBNAME,
00177                                        GWEN_PM_LIBNAME,
00178                                        GWEN_PM_PLUGINDIR,
00179                                        GWEN_REGKEY_PATHS,
00180                                        GWEN_REGNAME_PLUGINDIR);
00181 #if defined(OS_WIN32) || defined(ENABLE_LOCAL_INSTALL)
00182     /* add folder relative to EXE */
00183     GWEN_PathManager_AddRelPath(GWEN_PM_LIBNAME,
00184                                 GWEN_PM_LIBNAME,
00185                                 GWEN_PM_PLUGINDIR,
00186                                 PLUGINDIR,
00187                                 GWEN_PathManager_RelModeExe);
00188 #else
00189     /* add absolute folder */
00190     GWEN_PathManager_AddPath(GWEN_PM_LIBNAME,
00191                              GWEN_PM_LIBNAME,
00192                              GWEN_PM_PLUGINDIR,
00193                              PLUGINDIR);
00194 #endif
00195 
00196     /* ---------------------------------------------------------------------
00197      * datadir e.g. "/usr/share/gwenhywfar" */
00198     GWEN_PathManager_DefinePath(GWEN_PM_LIBNAME, GWEN_PM_DATADIR);
00199     GWEN_PathManager_AddPathFromWinReg(GWEN_PM_LIBNAME,
00200                                        GWEN_PM_LIBNAME,
00201                                        GWEN_PM_DATADIR,
00202                                        GWEN_REGKEY_PATHS,
00203                                        GWEN_REGNAME_DATADIR);
00204 #if defined(OS_WIN32) || defined(ENABLE_LOCAL_INSTALL)
00205     /* add folder relative to EXE */
00206     GWEN_PathManager_AddRelPath(GWEN_PM_LIBNAME,
00207                                 GWEN_PM_LIBNAME,
00208                                 GWEN_PM_DATADIR,
00209                                 GWEN_DATADIR,
00210                                 GWEN_PathManager_RelModeExe);
00211 #else
00212     /* add absolute folder */
00213     GWEN_PathManager_AddPath(GWEN_PM_LIBNAME,
00214                              GWEN_PM_LIBNAME,
00215                              GWEN_PM_DATADIR,
00216                              GWEN_DATADIR);
00217 #endif
00218 
00219     /* Initialize other modules. */
00220     DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing I18N module");
00221     err=GWEN_I18N_ModuleInit();
00222     if (err)
00223       return err;
00224     DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing InetAddr module");
00225     err=GWEN_InetAddr_ModuleInit();
00226     if (err)
00227       return err;
00228     DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing Socket module");
00229     err=GWEN_Socket_ModuleInit();
00230     if (err)
00231       return err;
00232     DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing Libloader module");
00233     err=GWEN_LibLoader_ModuleInit();
00234     if (err)
00235       return err;
00236     DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing Crypt3 module");
00237     err=GWEN_Crypt3_ModuleInit();
00238     if (err)
00239       return err;
00240     DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing Process module");
00241     err=GWEN_Process_ModuleInit();
00242     if (err)
00243       return err;
00244     DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing IO layer module");
00245     err=GWEN_Io_Manager_ModuleInit();
00246     if (err)
00247       return err;
00248     DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing Plugin module");
00249     err=GWEN_Plugin_ModuleInit();
00250     if (err)
00251       return err;
00252     DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing DataBase IO module");
00253     err=GWEN_DBIO_ModuleInit();
00254     if (err)
00255       return err;
00256     DBG_DEBUG(GWEN_LOGDOMAIN, "Initializing CryptToken2 module");
00257     err=GWEN_Crypt_Token_ModuleInit();
00258     if (err)
00259       return err;
00260     /* add more modules here */
00261 
00262   }
00263   gwen_is_initialized++;
00264 
00265   return 0;
00266 
00267 }
00268 
00269 
00270 
00271 int GWEN_Fini() {
00272   int err;
00273 
00274   err=0;
00275 
00276   if (gwen_is_initialized==0)
00277     return 0;
00278 
00279   gwen_is_initialized--;
00280   if (gwen_is_initialized==0) {
00281     int lerr;
00282 
00283     /* add more modules here */
00284     lerr=GWEN_Crypt_Token_ModuleFini();
00285     if (lerr) {
00286       err=lerr;
00287       DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: "
00288                 "Could not deinitialze module CryptToken2");
00289     }
00290     lerr=GWEN_DBIO_ModuleFini();
00291     if (lerr) {
00292       err=lerr;
00293       DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: "
00294                 "Could not deinitialze module DBIO");
00295     }
00296     lerr=GWEN_Plugin_ModuleFini();
00297     if (lerr) {
00298       err=lerr;
00299       DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: "
00300                 "Could not deinitialze module Plugin");
00301     }
00302     lerr=GWEN_Io_Manager_ModuleFini();
00303     if (lerr) {
00304       err=lerr;
00305       DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: "
00306                 "Could not deinitialze module IO layer");
00307     }
00308     lerr=GWEN_Process_ModuleFini();
00309     if (lerr) {
00310       err=lerr;
00311       DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: "
00312                 "Could not deinitialze module Process");
00313     }
00314     lerr=GWEN_Crypt3_ModuleFini();
00315     if (lerr) {
00316       err=lerr;
00317       DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: "
00318                 "Could not deinitialze module Crypt3");
00319     }
00320     lerr=GWEN_LibLoader_ModuleFini();
00321     if (lerr) {
00322       err=lerr;
00323       DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: "
00324               "Could not deinitialze module LibLoader");
00325     }
00326     lerr=GWEN_Socket_ModuleFini();
00327     if (lerr) {
00328       err=lerr;
00329       DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: "
00330                 "Could not deinitialze module Socket");
00331     }
00332     lerr=GWEN_InetAddr_ModuleFini();
00333     if (lerr) {
00334       err=lerr;
00335       DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: "
00336                 "Could not deinitialze module InetAddr");
00337     }
00338 
00339     lerr=GWEN_I18N_ModuleFini();
00340     if (lerr) {
00341       err=lerr;
00342       DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: "
00343                 "Could not deinitialze module I18N");
00344     }
00345 
00346     lerr=GWEN_PathManager_ModuleFini();
00347     if (lerr) {
00348       err=lerr;
00349       DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: "
00350                 "Could not deinitialze module PathManager");
00351     }
00352 
00353     GWEN_Error_ModuleFini();
00354 
00355     /* these two modules must be deinitialized at last */
00356     lerr=GWEN_Logger_ModuleFini();
00357     if (lerr) {
00358       err=lerr;
00359       DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: "
00360                 "Could not deinitialze module Logger");
00361     }
00362 
00363     lerr=GWEN_Memory_ModuleFini();
00364     if (lerr) {
00365       err=lerr;
00366       DBG_ERROR(GWEN_LOGDOMAIN, "GWEN_Fini: "
00367                 "Could not deinitialze module Memory");
00368     }
00369   }
00370 
00371   return err;
00372 }
00373 
00374 
00375 
00376 int GWEN_Fini_Forced() {
00377   if (gwen_is_initialized)
00378     gwen_is_initialized=1;
00379   return GWEN_Fini();
00380 }
00381 
00382 
00383 
00384 void GWEN_Version(int *major,
00385                   int *minor,
00386                   int *patchlevel,
00387                   int *build){
00388   *major=GWENHYWFAR_VERSION_MAJOR;
00389   *minor=GWENHYWFAR_VERSION_MINOR;
00390   *patchlevel=GWENHYWFAR_VERSION_PATCHLEVEL;
00391   *build=GWENHYWFAR_VERSION_BUILD;
00392 }
00393 
00394 
00395 
00396 
00397 
00398 
00399 #if 0
00400 /* Currently unused. */
00401 int GWEN__GetValueFromWinReg(const char *keyPath,
00402                              const char *varName,
00403                              GWEN_BUFFER *nbuf){
00404 #ifdef OS_WIN32
00405   HKEY hkey;
00406   TCHAR nbuffer[MAX_PATH];
00407   BYTE vbuffer[MAX_PATH];
00408   DWORD nsize;
00409   DWORD vsize;
00410   DWORD typ;
00411   int i;
00412 
00413   snprintf(nbuffer, sizeof(nbuffer), keyPath);
00414 
00415   /* open the key */
00416   if (RegOpenKey(HKEY_LOCAL_MACHINE,
00417                  keyPath,
00418                  &hkey)){
00419     DBG_ERROR(GWEN_LOGDOMAIN,
00420               "RegOpenKey \"%s\" failed.", keyPath);
00421     return -1;
00422   }
00423 
00424   /* find the variablename  */
00425   for (i=0;; i++) {
00426     nsize=sizeof(nbuffer);
00427     vsize=sizeof(vbuffer);
00428     if (ERROR_SUCCESS!=RegEnumValue(hkey,
00429                                     i,    /* index */
00430                                     nbuffer,
00431                                     &nsize,
00432                                     0,       /* reserved */
00433                                     &typ,
00434                                     vbuffer,
00435                                     &vsize))
00436       break;
00437     if (strcasecmp(nbuffer, varName)==0 && typ==REG_SZ) {
00438       /* variable found */
00439       RegCloseKey(hkey);
00440       GWEN_Buffer_AppendString(nbuf, (char*)vbuffer);
00441       return 0;
00442     }
00443   } /* for */
00444 
00445   RegCloseKey(hkey);
00446   return -1;
00447 
00448 #else /* OS_WIN32 */
00449   return -1;
00450 #endif /* OS_WIN32 */
00451 }
00452 #endif /* 0 */
00453 
00454 
00455 
00458 char *GWEN__get_plugindir (const char *default_dir)
00459 {
00460   char *result, *libdir, *pkglibdir, *tmp;
00461 
00462   libdir = br_find_lib_dir (NULL);
00463   if (libdir == (char *) NULL)
00464     /* BinReloc not initialized, use default hard-coded path. */
00465     return strdup (default_dir);
00466 
00467   pkglibdir = br_build_path (libdir, PACKAGE); /* defined in config.h */
00468   tmp = br_build_path (pkglibdir, "plugins");
00469   result = br_build_path (tmp, GWENHYWFAR_SO_EFFECTIVE_STR);
00470 
00471   free (libdir);
00472   free (pkglibdir);
00473   free (tmp);
00474 
00475   return result;
00476 }
00477 
00478 

Generated on Fri Apr 11 01:53:46 2008 for gwenhywfar by  doxygen 1.5.5