#ifdef USE_GPG | USE_NETPGP + make [OPENPGP=GPG|NETPGP]
authorEdouard Tisserant
Wed, 01 Apr 2015 16:36:52 +0200
changeset 1685ddaec401f66
parent 164 bc618bca4300
child 169 a0dec157c6c4
child 174 9c3d0eaa8ea1
#ifdef USE_GPG | USE_NETPGP + make [OPENPGP=GPG|NETPGP]
src/Makefile
src/cryptotech.c
src/pEp_internal.h
src/platform_unix.c
     1.1 --- a/src/Makefile	Mon Mar 30 07:35:11 2015 +0200
     1.2 +++ b/src/Makefile	Wed Apr 01 16:36:52 2015 +0200
     1.3 @@ -1,5 +1,7 @@
     1.4  include ../Makefile.conf
     1.5  
     1.6 +OPENPGP?=GPG
     1.7 +
     1.8  ifeq ($(BUILD_ON),Darwin)
     1.9  ifeq ($(BUILD_FOR),Darwin)
    1.10  
    1.11 @@ -38,7 +40,18 @@
    1.12  $(error don't know how to make for $(BUILD_FOR) on $(BUILD_ON))
    1.13  endif
    1.14  
    1.15 -ALL_SOURCE=$(wildcard *.c)
    1.16 +ifeq ("$(OPENPGP)","GPG")
    1.17 +	NO_SOURCE=pgp_netpgp.c
    1.18 +	CFLAGS+= -DUSE_GPG
    1.19 +else ifeq ("$(OPENPGP)","NETPGP")
    1.20 +	NO_SOURCE=pgp_gpg.c
    1.21 +	CFLAGS+= -DUSE_NETPGP
    1.22 +	LDFLAGS+= -lnetpgp
    1.23 +else
    1.24 +	$(error Unknown OpenPGP library : $(OPENPGP))
    1.25 +endif
    1.26 +
    1.27 +ALL_SOURCE=$(subst $(NO_SOURCE),,$(wildcard *.c))
    1.28  DEPENDS=$(subst .c,.d,$(ALL_SOURCE))
    1.29  ALL_OBJECTS=$(subst .c,.o,$(ALL_SOURCE))
    1.30  
     2.1 --- a/src/cryptotech.c	Mon Mar 30 07:35:11 2015 +0200
     2.2 +++ b/src/cryptotech.c	Wed Apr 01 16:36:52 2015 +0200
     2.3 @@ -1,9 +1,9 @@
     2.4  #include "pEp_internal.h"
     2.5  
     2.6 -#ifdef NO_GPG
     2.7 +#ifdef USE_GPG
     2.8 +#include "pgp_gpg.h"
     2.9 +#elif USE_NETPGP
    2.10  #include "pgp_netpgp.h"
    2.11 -#else
    2.12 -#include "pgp_gpg.h"
    2.13  #endif
    2.14  
    2.15  #include <stdlib.h>
     3.1 --- a/src/pEp_internal.h	Mon Mar 30 07:35:11 2015 +0200
     3.2 +++ b/src/pEp_internal.h	Wed Apr 01 16:36:52 2015 +0200
     3.3 @@ -47,8 +47,18 @@
     3.4  
     3.5  #define _EXPORT_PEP_ENGINE_DLL
     3.6  #include "pEpEngine.h"
     3.7 -#ifndef NO_GPG
     3.8 +
     3.9 +// If not specified, build for GPG
    3.10 +#ifndef USE_NETPGP
    3.11 +#ifndef USE_GPG
    3.12 +#define USE_GPG
    3.13 +#endif
    3.14 +#endif
    3.15 +
    3.16 +#ifdef USE_GPG
    3.17  #include "pgp_gpg_internal.h"
    3.18 +#elif USE_NETPGP
    3.19 +#include "pgp_netpgp_internal.h"
    3.20  #endif
    3.21  
    3.22  #include "cryptotech.h"
    3.23 @@ -58,8 +68,10 @@
    3.24  
    3.25  typedef struct _pEpSession {
    3.26      const char *version;
    3.27 -#ifndef NO_GPG
    3.28 +#ifdef USE_GPG
    3.29      gpgme_ctx_t ctx;
    3.30 +#elif USE_NETPGP
    3.31 +    netpgp_t ctx;
    3.32  #endif
    3.33  
    3.34      PEP_cryptotech_t *cryptotech;
     4.1 --- a/src/platform_unix.c	Mon Mar 30 07:35:11 2015 +0200
     4.2 +++ b/src/platform_unix.c	Wed Apr 01 16:36:52 2015 +0200
     4.3 @@ -43,55 +43,55 @@
     4.4  static const char *gpg_conf_name = "gpg.conf";
     4.5  static const char *gpg_conf_empty = "# Created by pEpEngine\n";
     4.6  
     4.7 -const char *gpg_conf(void)
     4.8 -{
     4.9 -    static char buffer[MAX_PATH];
    4.10 +static bool ensure_gpg_home(const char **conf, const char **home){
    4.11 +    static char path[MAX_PATH];
    4.12      static char dirname[MAX_PATH];
    4.13      static bool done = false;
    4.14  
    4.15      if (!done) {
    4.16 -        char *gpg_home = getenv("GNUPGHOME");
    4.17 -        if(gpg_home){
    4.18 +        char *p;
    4.19 +        size_t len;
    4.20 +        char *gpg_home_env = getenv("GNUPGHOME");
    4.21  
    4.22 -            char *p = stpncpy(buffer, gpg_home, MAX_PATH);
    4.23 -            size_t len = MAX_PATH - (p - buffer) - 2;
    4.24 +        if(gpg_home_env){
    4.25 +
    4.26 +            p = stpncpy(path, gpg_home_env, MAX_PATH);
    4.27 +            len = MAX_PATH - (p - path) - 2;
    4.28 +
    4.29              if (len < strlen(gpg_conf_name))
    4.30              {
    4.31                  assert(0);
    4.32 -                return NULL;
    4.33 +                return false;
    4.34              }
    4.35  
    4.36 -            strncpy(dirname, buffer, MAX_PATH);
    4.37 -            *p++ = '/';
    4.38 -            strncpy(p, gpg_conf_name, len);
    4.39 -
    4.40          }else{
    4.41  
    4.42 -            char *p = stpncpy(buffer, getenv("HOME"), MAX_PATH);
    4.43 -            size_t len = MAX_PATH - (p - buffer) - 3;
    4.44 +            p = stpncpy(path, getenv("HOME"), MAX_PATH);
    4.45 +            len = MAX_PATH - (p - path) - 3;
    4.46  
    4.47              if (len < strlen(gpg_conf_path) + strlen(gpg_conf_name))
    4.48              {
    4.49                  assert(0);
    4.50 -                return NULL;
    4.51 +                return false;
    4.52              }
    4.53  
    4.54              *p++ = '/';
    4.55              strncpy(p, gpg_conf_path, len);
    4.56 -            strncpy(dirname, buffer, MAX_PATH);
    4.57              p += strlen(gpg_conf_path);
    4.58              len -= strlen(gpg_conf_path) - 1;
    4.59 -            *p++ = '/';
    4.60 -            strncpy(p, gpg_conf_name, len);
    4.61          }
    4.62  
    4.63 -        if(access(buffer, F_OK)){ 
    4.64 +        strncpy(dirname, path, MAX_PATH);
    4.65 +        *p++ = '/';
    4.66 +        strncpy(p, gpg_conf_name, len);
    4.67 +
    4.68 +        if(access(path, F_OK)){ 
    4.69              int fd;
    4.70              if(access(dirname, F_OK )) { 
    4.71                  mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR);
    4.72              }
    4.73  
    4.74 -            fd = open(buffer, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
    4.75 +            fd = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
    4.76  
    4.77              if(fd>0) {
    4.78                  write(fd, gpg_conf_empty, strlen(gpg_conf_empty));
    4.79 @@ -101,5 +101,25 @@
    4.80  
    4.81          done = true;
    4.82      }
    4.83 -    return buffer;
    4.84 +
    4.85 +    if(conf) *conf=path;
    4.86 +    if(home) *home=dirname;
    4.87 +
    4.88 +    return true;
    4.89  }
    4.90 +
    4.91 +const char *gpg_conf(void)
    4.92 +{
    4.93 +    const char *conf;
    4.94 +    if(ensure_gpg_home(&conf, NULL))
    4.95 +        return conf;
    4.96 +    return NULL;
    4.97 +}
    4.98 +
    4.99 +const char *gpg_home(void)
   4.100 +{
   4.101 +    const char *home;
   4.102 +    if(ensure_gpg_home(NULL, &home))
   4.103 +        return home;
   4.104 +    return NULL;
   4.105 +}