src/platform_unix.c
author Krista Bennett <krista@pep-project.org>
Tue, 01 Aug 2017 16:40:24 +0200
branchmessage-2.0
changeset 1979 3d0a778d035a
parent 1559 c6506bc6a0df
child 2116 aa02b5fd4f00
permissions -rw-r--r--
Changed name to ENGINE-214 for JIRA tracking - please use that branch instead.
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
vb@1
     4
#define _POSIX_C_SOURCE 200809L
vb@1
     5
Edouard@1267
     6
#ifdef ANDROID
Edouard@1267
     7
#ifndef __LP64__ 
Edouard@1267
     8
#include <time64.h>
Edouard@1267
     9
#endif
Edouard@1267
    10
#endif
Edouard@1267
    11
Edouard@1136
    12
#include <time.h>
vb@1
    13
#include <string.h>
vb@1
    14
#include <stdlib.h>
vb@1
    15
#include <assert.h>
Edouard@158
    16
#include <sys/stat.h>
vb@149
    17
#include <sys/types.h>
Edouard@158
    18
#include <fcntl.h>
krista@1033
    19
#include <regex.h>
Edouard@158
    20
vb@1
    21
#include "platform_unix.h"
vb@1
    22
vb@1
    23
#define MAX_PATH 1024
vb@1089
    24
#ifndef LOCAL_DB_FILENAME
vb@1
    25
#define LOCAL_DB_FILENAME ".pEp_management.db"
vb@1089
    26
#endif
Edouard@347
    27
#define SYSTEM_DB_FILENAME "system.db"
vb@1
    28
vb@1
    29
#ifndef bool
vb@1
    30
#define bool int
vb@1
    31
#define true 1
vb@1
    32
#define false 0
vb@1
    33
#endif
vb@1
    34
Edouard@347
    35
#ifdef ANDROID
Edouard@1119
    36
#include <uuid.h>
Edouard@1267
    37
Edouard@1267
    38
/* FIXME : timegm will miss when linking for x86_64 on android, when supported */
Edouard@1267
    39
#ifndef __LP64__ 
Edouard@1267
    40
time_t timegm(struct tm* const t) {
Edouard@1267
    41
    static const time_t kTimeMax = ~(1L << (sizeof(time_t) * CHAR_BIT - 1));
Edouard@1267
    42
    static const time_t kTimeMin = (1L << (sizeof(time_t) * CHAR_BIT - 1));
Edouard@1267
    43
    time64_t result = timegm64(t);
Edouard@1267
    44
    if (result < kTimeMin || result > kTimeMax)
Edouard@1267
    45
        return -1;
Edouard@1267
    46
    return result;
Edouard@1267
    47
}
Edouard@1267
    48
#endif
Edouard@1267
    49
Edouard@347
    50
char *stpncpy(char *dst, const char *src, size_t n)
Edouard@333
    51
{
Edouard@333
    52
    if (n != 0) {
Edouard@333
    53
        char *d = dst;
Edouard@333
    54
        const char *s = src;
Edouard@333
    55
Edouard@333
    56
        dst = &dst[n];
Edouard@333
    57
        do {
Edouard@333
    58
            if ((*d++ = *s++) == 0) {
Edouard@333
    59
                dst = d - 1;
Edouard@333
    60
                /* NUL pad the remaining n-1 bytes */
Edouard@333
    61
                while (--n != 0)
Edouard@333
    62
                    *d++ = 0;
Edouard@333
    63
                break;
Edouard@333
    64
            }
Edouard@333
    65
        } while (--n != 0);
Edouard@333
    66
    }
Edouard@333
    67
    return (dst);
Edouard@333
    68
}
Edouard@333
    69
Edouard@338
    70
char *stpcpy(char *dst, const char *src)
Edouard@338
    71
{
Edouard@338
    72
    for (;; ++dst, ++src) {
Edouard@338
    73
        *dst = *src;
Edouard@338
    74
        if (*dst == 0)
Edouard@338
    75
            break;
Edouard@338
    76
    }
Edouard@338
    77
    return dst;
Edouard@338
    78
}
Edouard@338
    79
huss@1528
    80
/*
roker@438
    81
long int random(void)
roker@438
    82
{
Edouard@841
    83
    static bool seeded = false;
Edouard@838
    84
    static unsigned short xsubi[3];
Edouard@841
    85
    if(!seeded)
Edouard@841
    86
    {
roker@847
    87
        const long long t = (long long)time(NULL);
roker@847
    88
        xsubi[0] = (unsigned short)t;
roker@847
    89
        xsubi[1] = (unsigned short)(t>>16);
roker@847
    90
        xsubi[2] = (unsigned short)(t>>32);
Edouard@841
    91
        seeded = true;
Edouard@841
    92
    }
Edouard@838
    93
vb@436
    94
    return nrand48(xsubi);
huss@1528
    95
} */
Edouard@347
    96
Edouard@347
    97
const char *android_system_db(void)
Edouard@347
    98
{
Edouard@347
    99
    static char buffer[MAX_PATH];
Edouard@347
   100
    static bool done = false;
Edouard@347
   101
Edouard@347
   102
    if (!done) {
Edouard@349
   103
        char *tw_env;
Edouard@349
   104
        if(tw_env = getenv("TRUSTWORDS")){
Edouard@349
   105
            char *p = stpncpy(buffer, tw_env, MAX_PATH);
Edouard@814
   106
            ssize_t len = MAX_PATH - (p - buffer) - 2;
Edouard@347
   107
Edouard@347
   108
            if (len < strlen(SYSTEM_DB_FILENAME)) {
Edouard@347
   109
                assert(0);
Edouard@347
   110
                return NULL;
Edouard@347
   111
            }
Edouard@347
   112
Edouard@347
   113
            *p++ = '/';
Edouard@347
   114
            strncpy(p, SYSTEM_DB_FILENAME, len);
Edouard@347
   115
            done = true;
Edouard@347
   116
        }else{
Edouard@347
   117
            return NULL;
Edouard@347
   118
        }
Edouard@347
   119
Edouard@347
   120
    }
Edouard@347
   121
    return buffer;
Edouard@347
   122
}
Edouard@1119
   123
huss@1318
   124
markus@1226
   125
void uuid_generate_random(pEpUUID out)
Edouard@1119
   126
{
Edouard@1136
   127
    uuid_t *uuid;
Edouard@1136
   128
    uuid_rc_t rc_create;
huss@1318
   129
    size_t size = sizeof(uuid_string_t);
huss@1318
   130
    void *_out = out;
huss@1318
   131
	
Edouard@1136
   132
    if ((rc_create = uuid_create(&uuid)) != UUID_RC_OK ||
Edouard@1136
   133
        uuid_make(uuid, UUID_MAKE_V1) != UUID_RC_OK ||
huss@1318
   134
        uuid_export(uuid, UUID_FMT_BIN, &_out, &size) != UUID_RC_OK)
Edouard@1136
   135
    {
markus@1226
   136
        memset(out, 0, sizeof(pEpUUID));
Edouard@1136
   137
    }
Edouard@1136
   138
Edouard@1136
   139
    if (rc_create == UUID_RC_OK)
Edouard@1136
   140
    {
Edouard@1136
   141
        uuid_destroy(uuid);
Edouard@1136
   142
    }
Edouard@1119
   143
}
Edouard@1119
   144
huss@1318
   145
markus@1226
   146
void uuid_unparse_upper(pEpUUID uu, uuid_string_t out)
Edouard@1119
   147
{
Edouard@1136
   148
    uuid_t *uuid;
Edouard@1136
   149
    uuid_rc_t rc_create;
huss@1318
   150
    size_t size = sizeof(uuid_string_t);
huss@1318
   151
    void *_out = out;
Edouard@1136
   152
Edouard@1136
   153
    if ((rc_create = uuid_create(&uuid)) != UUID_RC_OK ||
Edouard@1262
   154
        uuid_import(uuid, UUID_FMT_BIN, uu, sizeof(pEpUUID)) != UUID_RC_OK ||
huss@1318
   155
        uuid_export(uuid, UUID_FMT_STR, &_out, &size) != UUID_RC_OK)
Edouard@1136
   156
    {
Edouard@1136
   157
        memset(out, 0, sizeof(uuid_string_t));
Edouard@1136
   158
    }
Edouard@1136
   159
    else 
Edouard@1136
   160
    {
Edouard@1136
   161
        out[sizeof(uuid_string_t) - 1] = 0;
Edouard@1136
   162
    }
Edouard@1136
   163
Edouard@1136
   164
    if (rc_create == UUID_RC_OK)
Edouard@1136
   165
    {
Edouard@1136
   166
        uuid_destroy(uuid);
Edouard@1136
   167
    }
Edouard@1119
   168
}
Edouard@1119
   169
Edouard@338
   170
#endif
Edouard@338
   171
vb@926
   172
#if !defined(BSD) && !defined(__APPLE__)
vb@926
   173
krista@918
   174
size_t strlcpy(char* dst, const	char* src, size_t size) {
krista@918
   175
    size_t retval = strlen(src);
krista@918
   176
    size_t size_to_copy = (retval < size ? retval : size - 1);
krista@918
   177
    
krista@918
   178
    // strlcpy doc says src and dst not allowed to overlap, as
krista@918
   179
    // it's undefined. So this is acceptable:
krista@918
   180
    memcpy((void*)dst, (void*)src, size_to_copy); // no defined error return, but strcpy doesn't either
krista@918
   181
    dst[size_to_copy] = '\0';
krista@918
   182
    return retval;
krista@918
   183
}
vb@926
   184
krista@918
   185
size_t strlcat(char* dst, const	char* src, size_t size) {
krista@918
   186
    size_t start_len = strnlen(dst, size);
krista@918
   187
    if (start_len == size)
krista@918
   188
        return size; // no copy, no null termination in size bytes, according to spec
krista@918
   189
    
krista@918
   190
    size_t add_len = strlen(src);
krista@918
   191
    size_t retval = start_len + add_len;
krista@918
   192
    size_t size_to_copy = (retval < size ? add_len : (size - start_len) - 1);
krista@918
   193
    
krista@918
   194
    // strlcat doc says src and dst not allowed to overlap, as
krista@918
   195
    // it's undefined. So this is acceptable:
krista@918
   196
    memcpy((void*)(dst + start_len), (void*)src, size_to_copy); // no defined error return, but strcpy doesn't either
krista@918
   197
    dst[start_len + size_to_copy] = '\0';
krista@918
   198
    return retval;
krista@918
   199
}
vb@926
   200
krista@1095
   201
#ifdef USE_NETPGP
krista@1033
   202
// FIXME: This may cause problems - this is a quick compatibility fix for netpgp code
krista@1033
   203
int regnexec(const regex_t* preg, const char* string,
krista@1033
   204
             size_t len, size_t nmatch, regmatch_t pmatch[], int eflags) {
krista@1033
   205
    return regexec(preg, string, nmatch, pmatch, eflags);
krista@1033
   206
}
krista@1095
   207
#endif
krista@1033
   208
krista@919
   209
#endif
krista@918
   210
vb@1
   211
const char *unix_local_db(void)
vb@1
   212
{
vb@1
   213
    static char buffer[MAX_PATH];
vb@1
   214
    static bool done = false;
vb@1
   215
vb@1
   216
    if (!done) {
Edouard@346
   217
        char *home_env;
vb@352
   218
        if((home_env = getenv("HOME"))){
Edouard@347
   219
            char *p = stpncpy(buffer, home_env, MAX_PATH);
Edouard@814
   220
            ssize_t len = MAX_PATH - (p - buffer) - 2;
vb@1
   221
Edouard@346
   222
            if (len < strlen(LOCAL_DB_FILENAME)) {
Edouard@346
   223
                assert(0);
Edouard@346
   224
                return NULL;
Edouard@346
   225
            }
Edouard@346
   226
Edouard@346
   227
            *p++ = '/';
Edouard@346
   228
            strncpy(p, LOCAL_DB_FILENAME, len);
Edouard@346
   229
            done = true;
Edouard@346
   230
        }else{
vb@1
   231
            return NULL;
vb@1
   232
        }
vb@1
   233
vb@1
   234
    }
vb@1
   235
    return buffer;
vb@1
   236
}
vb@1
   237
vb@1
   238
static const char *gpg_conf_path = ".gnupg";
vb@1
   239
static const char *gpg_conf_name = "gpg.conf";
krista@763
   240
static const char *gpg_agent_conf_name = "gpg-agent.conf";
Edouard@158
   241
static const char *gpg_conf_empty = "# Created by pEpEngine\n";
vb@1
   242
Edouard@168
   243
static bool ensure_gpg_home(const char **conf, const char **home){
Edouard@168
   244
    static char path[MAX_PATH];
Edouard@158
   245
    static char dirname[MAX_PATH];
vb@1
   246
    static bool done = false;
vb@1
   247
vb@1
   248
    if (!done) {
Edouard@168
   249
        char *p;
Edouard@814
   250
        ssize_t len;
Edouard@168
   251
        char *gpg_home_env = getenv("GNUPGHOME");
Edouard@346
   252
        char *home_env = getenv("HOME");
vb@1
   253
Edouard@168
   254
        if(gpg_home_env){
Edouard@168
   255
Edouard@347
   256
            p = stpncpy(path, gpg_home_env, MAX_PATH);
Edouard@168
   257
            len = MAX_PATH - (p - path) - 2;
Edouard@168
   258
Edouard@158
   259
            if (len < strlen(gpg_conf_name))
Edouard@158
   260
            {
Edouard@158
   261
                assert(0);
Edouard@168
   262
                return false;
Edouard@158
   263
            }
Edouard@158
   264
Edouard@346
   265
        }else if(home_env){
Edouard@158
   266
Edouard@347
   267
            p = stpncpy(path, home_env, MAX_PATH);
Edouard@168
   268
            len = MAX_PATH - (p - path) - 3;
Edouard@158
   269
Edouard@158
   270
            if (len < strlen(gpg_conf_path) + strlen(gpg_conf_name))
Edouard@158
   271
            {
Edouard@158
   272
                assert(0);
Edouard@168
   273
                return false;
Edouard@158
   274
            }
Edouard@158
   275
Edouard@158
   276
            *p++ = '/';
Edouard@158
   277
            strncpy(p, gpg_conf_path, len);
Edouard@158
   278
            p += strlen(gpg_conf_path);
Edouard@158
   279
            len -= strlen(gpg_conf_path) - 1;
Edouard@346
   280
Edouard@346
   281
        }else{
Edouard@346
   282
Edouard@346
   283
            assert(0);
Edouard@346
   284
            return false;
vb@1
   285
        }
vb@1
   286
Edouard@168
   287
        strncpy(dirname, path, MAX_PATH);
Edouard@168
   288
        *p++ = '/';
Edouard@168
   289
        strncpy(p, gpg_conf_name, len);
Edouard@168
   290
Edouard@168
   291
        if(access(path, F_OK)){ 
Edouard@158
   292
            int fd;
Edouard@158
   293
            if(access(dirname, F_OK )) { 
Edouard@158
   294
                mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR);
Edouard@158
   295
            }
vb@149
   296
Edouard@168
   297
            fd = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
vb@149
   298
Edouard@158
   299
            if(fd>0) {
Edouard@158
   300
                write(fd, gpg_conf_empty, strlen(gpg_conf_empty));
Edouard@158
   301
                close(fd);
Edouard@158
   302
            }
Edouard@158
   303
        }
vb@1
   304
vb@1
   305
        done = true;
vb@1
   306
    }
Edouard@168
   307
Edouard@168
   308
    if(conf) *conf=path;
Edouard@168
   309
    if(home) *home=dirname;
Edouard@168
   310
Edouard@168
   311
    return true;
vb@1
   312
}
Edouard@168
   313
krista@763
   314
static bool ensure_gpg_agent_conf(const char **agent_conf){
krista@763
   315
    static char agent_path[MAX_PATH];
krista@763
   316
    static bool done = false;
krista@763
   317
krista@763
   318
    if (!done) {
krista@763
   319
        const char *dirname;
krista@763
   320
krista@763
   321
        if (!ensure_gpg_home(NULL, &dirname)) /* Then dirname won't be set. */
krista@763
   322
            return false;
krista@763
   323
roker@1559
   324
        char *p = stpncpy(agent_path, dirname, MAX_PATH);
krista@763
   325
        
Edouard@814
   326
        ssize_t len = MAX_PATH - (p - agent_path) - 2;
krista@763
   327
krista@763
   328
        if (len < strlen(gpg_agent_conf_name))
krista@763
   329
        {
krista@763
   330
            assert(0);
krista@763
   331
            return false;
krista@763
   332
        }
krista@763
   333
krista@763
   334
        *p++ = '/';
krista@763
   335
     
krista@763
   336
        strncpy(p, gpg_agent_conf_name, len);
krista@763
   337
krista@763
   338
        if(access(agent_path, F_OK)){ 
krista@763
   339
            int fd;
krista@763
   340
            if(access(dirname, F_OK )) { 
krista@763
   341
                mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR);
krista@763
   342
            }
krista@763
   343
krista@763
   344
            fd = open(agent_path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
krista@763
   345
krista@763
   346
            if(fd>0) {
krista@763
   347
                write(fd, gpg_conf_empty, strlen(gpg_conf_empty));
krista@763
   348
                close(fd);
krista@763
   349
            }
krista@763
   350
        }
krista@763
   351
        done = true;
krista@763
   352
    }
krista@763
   353
    if(agent_conf) *agent_conf=agent_path;
krista@763
   354
krista@763
   355
    return true;
krista@763
   356
}
krista@763
   357
Edouard@168
   358
const char *gpg_conf(void)
Edouard@168
   359
{
Edouard@168
   360
    const char *conf;
Edouard@168
   361
    if(ensure_gpg_home(&conf, NULL))
Edouard@168
   362
        return conf;
Edouard@168
   363
    return NULL;
Edouard@168
   364
}
Edouard@168
   365
Edouard@168
   366
const char *gpg_home(void)
Edouard@168
   367
{
Edouard@168
   368
    const char *home;
Edouard@168
   369
    if(ensure_gpg_home(NULL, &home))
Edouard@168
   370
        return home;
Edouard@168
   371
    return NULL;
Edouard@168
   372
}
krista@763
   373
krista@763
   374
const char *gpg_agent_conf(void)
krista@763
   375
{
krista@763
   376
    const char *agent_conf;
krista@763
   377
    if(ensure_gpg_agent_conf(&agent_conf))
krista@763
   378
        return agent_conf;
krista@763
   379
    return NULL;
krista@763
   380
}