src/platform_unix.c
author Volker Birk <vb@pep-project.org>
Wed, 29 Apr 2015 14:51:33 +0200
changeset 229 313d152239bf
parent 168 5ddaec401f66
child 333 77d486037ec9
permissions -rw-r--r--
fixing preprocessor
vb@1
     1
#define _POSIX_C_SOURCE 200809L
vb@1
     2
vb@1
     3
#include <string.h>
vb@1
     4
#include <stdlib.h>
vb@1
     5
#include <assert.h>
Edouard@158
     6
#include <sys/stat.h>
vb@149
     7
#include <sys/types.h>
Edouard@158
     8
#include <fcntl.h>
Edouard@158
     9
vb@1
    10
#include "platform_unix.h"
vb@1
    11
vb@1
    12
#define MAX_PATH 1024
vb@1
    13
#define LOCAL_DB_FILENAME ".pEp_management.db"
vb@1
    14
vb@1
    15
#ifndef bool
vb@1
    16
#define bool int
vb@1
    17
#define true 1
vb@1
    18
#define false 0
vb@1
    19
#endif
vb@1
    20
vb@1
    21
const char *unix_local_db(void)
vb@1
    22
{
vb@1
    23
    static char buffer[MAX_PATH];
vb@1
    24
    static bool done = false;
vb@1
    25
vb@1
    26
    if (!done) {
vb@1
    27
        char *p = stpncpy(buffer, getenv("HOME"), MAX_PATH);
vb@1
    28
        size_t len = MAX_PATH - (p - buffer) - 2;
vb@1
    29
vb@1
    30
        if (len < strlen(LOCAL_DB_FILENAME)) {
vb@1
    31
            assert(0);
vb@1
    32
            return NULL;
vb@1
    33
        }
vb@1
    34
vb@1
    35
        *p++ = '/';
vb@1
    36
        strncpy(p, LOCAL_DB_FILENAME, len);
vb@1
    37
        done = true;
vb@1
    38
    }
vb@1
    39
    return buffer;
vb@1
    40
}
vb@1
    41
vb@1
    42
static const char *gpg_conf_path = ".gnupg";
vb@1
    43
static const char *gpg_conf_name = "gpg.conf";
Edouard@158
    44
static const char *gpg_conf_empty = "# Created by pEpEngine\n";
vb@1
    45
Edouard@168
    46
static bool ensure_gpg_home(const char **conf, const char **home){
Edouard@168
    47
    static char path[MAX_PATH];
Edouard@158
    48
    static char dirname[MAX_PATH];
vb@1
    49
    static bool done = false;
vb@1
    50
vb@1
    51
    if (!done) {
Edouard@168
    52
        char *p;
Edouard@168
    53
        size_t len;
Edouard@168
    54
        char *gpg_home_env = getenv("GNUPGHOME");
Edouard@158
    55
Edouard@168
    56
        if(gpg_home_env){
Edouard@168
    57
Edouard@168
    58
            p = stpncpy(path, gpg_home_env, MAX_PATH);
Edouard@168
    59
            len = MAX_PATH - (p - path) - 2;
Edouard@168
    60
Edouard@158
    61
            if (len < strlen(gpg_conf_name))
Edouard@158
    62
            {
Edouard@158
    63
                assert(0);
Edouard@168
    64
                return false;
Edouard@158
    65
            }
Edouard@158
    66
Edouard@158
    67
        }else{
vb@1
    68
Edouard@168
    69
            p = stpncpy(path, getenv("HOME"), MAX_PATH);
Edouard@168
    70
            len = MAX_PATH - (p - path) - 3;
Edouard@158
    71
Edouard@158
    72
            if (len < strlen(gpg_conf_path) + strlen(gpg_conf_name))
Edouard@158
    73
            {
Edouard@158
    74
                assert(0);
Edouard@168
    75
                return false;
Edouard@158
    76
            }
Edouard@158
    77
Edouard@158
    78
            *p++ = '/';
Edouard@158
    79
            strncpy(p, gpg_conf_path, len);
Edouard@158
    80
            p += strlen(gpg_conf_path);
Edouard@158
    81
            len -= strlen(gpg_conf_path) - 1;
vb@1
    82
        }
vb@1
    83
Edouard@168
    84
        strncpy(dirname, path, MAX_PATH);
Edouard@168
    85
        *p++ = '/';
Edouard@168
    86
        strncpy(p, gpg_conf_name, len);
Edouard@168
    87
Edouard@168
    88
        if(access(path, F_OK)){ 
Edouard@158
    89
            int fd;
Edouard@158
    90
            if(access(dirname, F_OK )) { 
Edouard@158
    91
                mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR);
Edouard@158
    92
            }
vb@149
    93
Edouard@168
    94
            fd = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
vb@149
    95
Edouard@158
    96
            if(fd>0) {
Edouard@158
    97
                write(fd, gpg_conf_empty, strlen(gpg_conf_empty));
Edouard@158
    98
                close(fd);
Edouard@158
    99
            }
Edouard@158
   100
        }
vb@1
   101
vb@1
   102
        done = true;
vb@1
   103
    }
Edouard@168
   104
Edouard@168
   105
    if(conf) *conf=path;
Edouard@168
   106
    if(home) *home=dirname;
Edouard@168
   107
Edouard@168
   108
    return true;
vb@1
   109
}
Edouard@168
   110
Edouard@168
   111
const char *gpg_conf(void)
Edouard@168
   112
{
Edouard@168
   113
    const char *conf;
Edouard@168
   114
    if(ensure_gpg_home(&conf, NULL))
Edouard@168
   115
        return conf;
Edouard@168
   116
    return NULL;
Edouard@168
   117
}
Edouard@168
   118
Edouard@168
   119
const char *gpg_home(void)
Edouard@168
   120
{
Edouard@168
   121
    const char *home;
Edouard@168
   122
    if(ensure_gpg_home(NULL, &home))
Edouard@168
   123
        return home;
Edouard@168
   124
    return NULL;
Edouard@168
   125
}