src/platform_unix.c
author Krista Grothoff <krista@pep-project.org>
Fri, 24 Jun 2016 13:11:18 +0200
changeset 763 9c0e286f0a23
parent 438 106b9765559d
child 814 91f317ce921f
permissions -rw-r--r--
Added gpa-agent.conf defaults for passphrase timeouts.
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"
Edouard@347
    14
#define SYSTEM_DB_FILENAME "system.db"
vb@1
    15
vb@1
    16
#ifndef bool
vb@1
    17
#define bool int
vb@1
    18
#define true 1
vb@1
    19
#define false 0
vb@1
    20
#endif
vb@1
    21
Edouard@347
    22
#ifdef ANDROID
Edouard@347
    23
char *stpncpy(char *dst, const char *src, size_t n)
Edouard@333
    24
{
Edouard@333
    25
    if (n != 0) {
Edouard@333
    26
        char *d = dst;
Edouard@333
    27
        const char *s = src;
Edouard@333
    28
Edouard@333
    29
        dst = &dst[n];
Edouard@333
    30
        do {
Edouard@333
    31
            if ((*d++ = *s++) == 0) {
Edouard@333
    32
                dst = d - 1;
Edouard@333
    33
                /* NUL pad the remaining n-1 bytes */
Edouard@333
    34
                while (--n != 0)
Edouard@333
    35
                    *d++ = 0;
Edouard@333
    36
                break;
Edouard@333
    37
            }
Edouard@333
    38
        } while (--n != 0);
Edouard@333
    39
    }
Edouard@333
    40
    return (dst);
Edouard@333
    41
}
Edouard@333
    42
Edouard@338
    43
char *stpcpy(char *dst, const char *src)
Edouard@338
    44
{
Edouard@338
    45
    for (;; ++dst, ++src) {
Edouard@338
    46
        *dst = *src;
Edouard@338
    47
        if (*dst == 0)
Edouard@338
    48
            break;
Edouard@338
    49
    }
Edouard@338
    50
    return dst;
Edouard@338
    51
}
Edouard@338
    52
roker@438
    53
long int random(void)
roker@438
    54
{
roker@438
    55
    static unsigned short xsubi[3] = {'p', 'E', 'p'};
vb@436
    56
    return nrand48(xsubi);
Edouard@338
    57
}
Edouard@347
    58
Edouard@347
    59
const char *android_system_db(void)
Edouard@347
    60
{
Edouard@347
    61
    static char buffer[MAX_PATH];
Edouard@347
    62
    static bool done = false;
Edouard@347
    63
Edouard@347
    64
    if (!done) {
Edouard@349
    65
        char *tw_env;
Edouard@349
    66
        if(tw_env = getenv("TRUSTWORDS")){
Edouard@349
    67
            char *p = stpncpy(buffer, tw_env, MAX_PATH);
Edouard@347
    68
            size_t len = MAX_PATH - (p - buffer) - 2;
Edouard@347
    69
Edouard@347
    70
            if (len < strlen(SYSTEM_DB_FILENAME)) {
Edouard@347
    71
                assert(0);
Edouard@347
    72
                return NULL;
Edouard@347
    73
            }
Edouard@347
    74
Edouard@347
    75
            *p++ = '/';
Edouard@347
    76
            strncpy(p, SYSTEM_DB_FILENAME, len);
Edouard@347
    77
            done = true;
Edouard@347
    78
        }else{
Edouard@347
    79
            return NULL;
Edouard@347
    80
        }
Edouard@347
    81
Edouard@347
    82
    }
Edouard@347
    83
    return buffer;
Edouard@347
    84
}
Edouard@338
    85
#endif
Edouard@338
    86
vb@1
    87
const char *unix_local_db(void)
vb@1
    88
{
vb@1
    89
    static char buffer[MAX_PATH];
vb@1
    90
    static bool done = false;
vb@1
    91
vb@1
    92
    if (!done) {
Edouard@346
    93
        char *home_env;
vb@352
    94
        if((home_env = getenv("HOME"))){
Edouard@347
    95
            char *p = stpncpy(buffer, home_env, MAX_PATH);
Edouard@346
    96
            size_t len = MAX_PATH - (p - buffer) - 2;
vb@1
    97
Edouard@346
    98
            if (len < strlen(LOCAL_DB_FILENAME)) {
Edouard@346
    99
                assert(0);
Edouard@346
   100
                return NULL;
Edouard@346
   101
            }
Edouard@346
   102
Edouard@346
   103
            *p++ = '/';
Edouard@346
   104
            strncpy(p, LOCAL_DB_FILENAME, len);
Edouard@346
   105
            done = true;
Edouard@346
   106
        }else{
vb@1
   107
            return NULL;
vb@1
   108
        }
vb@1
   109
vb@1
   110
    }
vb@1
   111
    return buffer;
vb@1
   112
}
vb@1
   113
vb@1
   114
static const char *gpg_conf_path = ".gnupg";
vb@1
   115
static const char *gpg_conf_name = "gpg.conf";
krista@763
   116
static const char *gpg_agent_conf_name = "gpg-agent.conf";
Edouard@158
   117
static const char *gpg_conf_empty = "# Created by pEpEngine\n";
vb@1
   118
Edouard@168
   119
static bool ensure_gpg_home(const char **conf, const char **home){
Edouard@168
   120
    static char path[MAX_PATH];
Edouard@158
   121
    static char dirname[MAX_PATH];
vb@1
   122
    static bool done = false;
vb@1
   123
vb@1
   124
    if (!done) {
Edouard@168
   125
        char *p;
Edouard@168
   126
        size_t len;
Edouard@168
   127
        char *gpg_home_env = getenv("GNUPGHOME");
Edouard@346
   128
        char *home_env = getenv("HOME");
Edouard@158
   129
Edouard@168
   130
        if(gpg_home_env){
Edouard@168
   131
Edouard@347
   132
            p = stpncpy(path, gpg_home_env, MAX_PATH);
Edouard@168
   133
            len = MAX_PATH - (p - path) - 2;
Edouard@168
   134
Edouard@158
   135
            if (len < strlen(gpg_conf_name))
Edouard@158
   136
            {
Edouard@158
   137
                assert(0);
Edouard@168
   138
                return false;
Edouard@158
   139
            }
Edouard@158
   140
Edouard@346
   141
        }else if(home_env){
vb@1
   142
Edouard@347
   143
            p = stpncpy(path, home_env, MAX_PATH);
Edouard@168
   144
            len = MAX_PATH - (p - path) - 3;
Edouard@158
   145
Edouard@158
   146
            if (len < strlen(gpg_conf_path) + strlen(gpg_conf_name))
Edouard@158
   147
            {
Edouard@158
   148
                assert(0);
Edouard@168
   149
                return false;
Edouard@158
   150
            }
Edouard@158
   151
Edouard@158
   152
            *p++ = '/';
Edouard@158
   153
            strncpy(p, gpg_conf_path, len);
Edouard@158
   154
            p += strlen(gpg_conf_path);
Edouard@158
   155
            len -= strlen(gpg_conf_path) - 1;
Edouard@346
   156
Edouard@346
   157
        }else{
Edouard@346
   158
Edouard@346
   159
            assert(0);
Edouard@346
   160
            return false;
vb@1
   161
        }
vb@1
   162
Edouard@168
   163
        strncpy(dirname, path, MAX_PATH);
Edouard@168
   164
        *p++ = '/';
Edouard@168
   165
        strncpy(p, gpg_conf_name, len);
Edouard@168
   166
Edouard@168
   167
        if(access(path, F_OK)){ 
Edouard@158
   168
            int fd;
Edouard@158
   169
            if(access(dirname, F_OK )) { 
Edouard@158
   170
                mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR);
Edouard@158
   171
            }
vb@149
   172
Edouard@168
   173
            fd = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
vb@149
   174
Edouard@158
   175
            if(fd>0) {
Edouard@158
   176
                write(fd, gpg_conf_empty, strlen(gpg_conf_empty));
Edouard@158
   177
                close(fd);
Edouard@158
   178
            }
Edouard@158
   179
        }
vb@1
   180
vb@1
   181
        done = true;
vb@1
   182
    }
Edouard@168
   183
Edouard@168
   184
    if(conf) *conf=path;
Edouard@168
   185
    if(home) *home=dirname;
Edouard@168
   186
Edouard@168
   187
    return true;
vb@1
   188
}
Edouard@168
   189
krista@763
   190
static bool ensure_gpg_agent_conf(const char **agent_conf){
krista@763
   191
    static char agent_path[MAX_PATH];
krista@763
   192
    static bool done = false;
krista@763
   193
krista@763
   194
    if (!done) {
krista@763
   195
        const char *dirname;
krista@763
   196
krista@763
   197
        if (!ensure_gpg_home(NULL, &dirname)) /* Then dirname won't be set. */
krista@763
   198
            return false;
krista@763
   199
krista@763
   200
        char *p;
krista@763
   201
        p = stpncpy(agent_path, dirname, MAX_PATH);
krista@763
   202
        
krista@763
   203
        size_t len = MAX_PATH - (p - agent_path) - 2;
krista@763
   204
krista@763
   205
        if (len < strlen(gpg_agent_conf_name))
krista@763
   206
        {
krista@763
   207
            assert(0);
krista@763
   208
            return false;
krista@763
   209
        }
krista@763
   210
krista@763
   211
        *p++ = '/';
krista@763
   212
     
krista@763
   213
        strncpy(p, gpg_agent_conf_name, len);
krista@763
   214
krista@763
   215
        if(access(agent_path, F_OK)){ 
krista@763
   216
            int fd;
krista@763
   217
            if(access(dirname, F_OK )) { 
krista@763
   218
                mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR);
krista@763
   219
            }
krista@763
   220
krista@763
   221
            fd = open(agent_path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
krista@763
   222
krista@763
   223
            if(fd>0) {
krista@763
   224
                write(fd, gpg_conf_empty, strlen(gpg_conf_empty));
krista@763
   225
                close(fd);
krista@763
   226
            }
krista@763
   227
        }
krista@763
   228
        done = true;
krista@763
   229
    }
krista@763
   230
    if(agent_conf) *agent_conf=agent_path;
krista@763
   231
krista@763
   232
    return true;
krista@763
   233
}
krista@763
   234
Edouard@168
   235
const char *gpg_conf(void)
Edouard@168
   236
{
Edouard@168
   237
    const char *conf;
Edouard@168
   238
    if(ensure_gpg_home(&conf, NULL))
Edouard@168
   239
        return conf;
Edouard@168
   240
    return NULL;
Edouard@168
   241
}
Edouard@168
   242
Edouard@168
   243
const char *gpg_home(void)
Edouard@168
   244
{
Edouard@168
   245
    const char *home;
Edouard@168
   246
    if(ensure_gpg_home(NULL, &home))
Edouard@168
   247
        return home;
Edouard@168
   248
    return NULL;
Edouard@168
   249
}
krista@763
   250
krista@763
   251
const char *gpg_agent_conf(void)
krista@763
   252
{
krista@763
   253
    const char *agent_conf;
krista@763
   254
    if(ensure_gpg_agent_conf(&agent_conf))
krista@763
   255
        return agent_conf;
krista@763
   256
    return NULL;
krista@763
   257
}