src/platform_unix.c
author Krista Grothoff <krista@pep-project.org>
Fri, 22 Jul 2016 12:29:06 +0200
branchstrl-imp
changeset 918 45c80cc4c2ed
parent 847 967c385ea856
child 919 5d02ed589daa
permissions -rw-r--r--
work on fix #36: conditional strlcpy/strcat impl for non-BSD systems. Branched to make testing on other OSs easier before merge to default.
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
{
Edouard@841
    55
    static bool seeded = false;
Edouard@838
    56
    static unsigned short xsubi[3];
Edouard@841
    57
    if(!seeded)
Edouard@841
    58
    {
roker@847
    59
        const long long t = (long long)time(NULL);
roker@847
    60
        xsubi[0] = (unsigned short)t;
roker@847
    61
        xsubi[1] = (unsigned short)(t>>16);
roker@847
    62
        xsubi[2] = (unsigned short)(t>>32);
Edouard@841
    63
        seeded = true;
Edouard@841
    64
    }
Edouard@838
    65
vb@436
    66
    return nrand48(xsubi);
Edouard@338
    67
}
Edouard@347
    68
Edouard@347
    69
const char *android_system_db(void)
Edouard@347
    70
{
Edouard@347
    71
    static char buffer[MAX_PATH];
Edouard@347
    72
    static bool done = false;
Edouard@347
    73
Edouard@347
    74
    if (!done) {
Edouard@349
    75
        char *tw_env;
Edouard@349
    76
        if(tw_env = getenv("TRUSTWORDS")){
Edouard@349
    77
            char *p = stpncpy(buffer, tw_env, MAX_PATH);
Edouard@814
    78
            ssize_t len = MAX_PATH - (p - buffer) - 2;
Edouard@347
    79
Edouard@347
    80
            if (len < strlen(SYSTEM_DB_FILENAME)) {
Edouard@347
    81
                assert(0);
Edouard@347
    82
                return NULL;
Edouard@347
    83
            }
Edouard@347
    84
Edouard@347
    85
            *p++ = '/';
Edouard@347
    86
            strncpy(p, SYSTEM_DB_FILENAME, len);
Edouard@347
    87
            done = true;
Edouard@347
    88
        }else{
Edouard@347
    89
            return NULL;
Edouard@347
    90
        }
Edouard@347
    91
Edouard@347
    92
    }
Edouard@347
    93
    return buffer;
Edouard@347
    94
}
Edouard@338
    95
#endif
Edouard@338
    96
krista@918
    97
#ifndef BSD
krista@918
    98
size_t strlcpy(char* dst, const	char* src, size_t size) {
krista@918
    99
    size_t retval = strlen(src);
krista@918
   100
    size_t size_to_copy = (retval < size ? retval : size - 1);
krista@918
   101
    
krista@918
   102
    // strlcpy doc says src and dst not allowed to overlap, as
krista@918
   103
    // it's undefined. So this is acceptable:
krista@918
   104
    memcpy((void*)dst, (void*)src, size_to_copy); // no defined error return, but strcpy doesn't either
krista@918
   105
    dst[size_to_copy] = '\0';
krista@918
   106
    return retval;
krista@918
   107
}
krista@918
   108
size_t strlcat(char* dst, const	char* src, size_t size) {
krista@918
   109
    size_t start_len = strnlen(dst, size);
krista@918
   110
    if (start_len == size)
krista@918
   111
        return size; // no copy, no null termination in size bytes, according to spec
krista@918
   112
    
krista@918
   113
    size_t add_len = strlen(src);
krista@918
   114
    size_t retval = start_len + add_len;
krista@918
   115
    size_t size_to_copy = (retval < size ? add_len : (size - start_len) - 1);
krista@918
   116
    
krista@918
   117
    // strlcat doc says src and dst not allowed to overlap, as
krista@918
   118
    // it's undefined. So this is acceptable:
krista@918
   119
    memcpy((void*)(dst + start_len), (void*)src, size_to_copy); // no defined error return, but strcpy doesn't either
krista@918
   120
    dst[start_len + size_to_copy] = '\0';
krista@918
   121
    return retval;
krista@918
   122
}
krista@918
   123
#endif
krista@918
   124
vb@1
   125
const char *unix_local_db(void)
vb@1
   126
{
vb@1
   127
    static char buffer[MAX_PATH];
vb@1
   128
    static bool done = false;
vb@1
   129
vb@1
   130
    if (!done) {
Edouard@346
   131
        char *home_env;
vb@352
   132
        if((home_env = getenv("HOME"))){
Edouard@347
   133
            char *p = stpncpy(buffer, home_env, MAX_PATH);
Edouard@814
   134
            ssize_t len = MAX_PATH - (p - buffer) - 2;
vb@1
   135
Edouard@346
   136
            if (len < strlen(LOCAL_DB_FILENAME)) {
Edouard@346
   137
                assert(0);
Edouard@346
   138
                return NULL;
Edouard@346
   139
            }
Edouard@346
   140
Edouard@346
   141
            *p++ = '/';
Edouard@346
   142
            strncpy(p, LOCAL_DB_FILENAME, len);
Edouard@346
   143
            done = true;
Edouard@346
   144
        }else{
vb@1
   145
            return NULL;
vb@1
   146
        }
vb@1
   147
vb@1
   148
    }
vb@1
   149
    return buffer;
vb@1
   150
}
vb@1
   151
vb@1
   152
static const char *gpg_conf_path = ".gnupg";
vb@1
   153
static const char *gpg_conf_name = "gpg.conf";
krista@763
   154
static const char *gpg_agent_conf_name = "gpg-agent.conf";
Edouard@158
   155
static const char *gpg_conf_empty = "# Created by pEpEngine\n";
vb@1
   156
Edouard@168
   157
static bool ensure_gpg_home(const char **conf, const char **home){
Edouard@168
   158
    static char path[MAX_PATH];
Edouard@158
   159
    static char dirname[MAX_PATH];
vb@1
   160
    static bool done = false;
vb@1
   161
vb@1
   162
    if (!done) {
Edouard@168
   163
        char *p;
Edouard@814
   164
        ssize_t len;
Edouard@168
   165
        char *gpg_home_env = getenv("GNUPGHOME");
Edouard@346
   166
        char *home_env = getenv("HOME");
Edouard@158
   167
Edouard@168
   168
        if(gpg_home_env){
Edouard@168
   169
Edouard@347
   170
            p = stpncpy(path, gpg_home_env, MAX_PATH);
Edouard@168
   171
            len = MAX_PATH - (p - path) - 2;
Edouard@168
   172
Edouard@158
   173
            if (len < strlen(gpg_conf_name))
Edouard@158
   174
            {
Edouard@158
   175
                assert(0);
Edouard@168
   176
                return false;
Edouard@158
   177
            }
Edouard@158
   178
Edouard@346
   179
        }else if(home_env){
vb@1
   180
Edouard@347
   181
            p = stpncpy(path, home_env, MAX_PATH);
Edouard@168
   182
            len = MAX_PATH - (p - path) - 3;
Edouard@158
   183
Edouard@158
   184
            if (len < strlen(gpg_conf_path) + strlen(gpg_conf_name))
Edouard@158
   185
            {
Edouard@158
   186
                assert(0);
Edouard@168
   187
                return false;
Edouard@158
   188
            }
Edouard@158
   189
Edouard@158
   190
            *p++ = '/';
Edouard@158
   191
            strncpy(p, gpg_conf_path, len);
Edouard@158
   192
            p += strlen(gpg_conf_path);
Edouard@158
   193
            len -= strlen(gpg_conf_path) - 1;
Edouard@346
   194
Edouard@346
   195
        }else{
Edouard@346
   196
Edouard@346
   197
            assert(0);
Edouard@346
   198
            return false;
vb@1
   199
        }
vb@1
   200
Edouard@168
   201
        strncpy(dirname, path, MAX_PATH);
Edouard@168
   202
        *p++ = '/';
Edouard@168
   203
        strncpy(p, gpg_conf_name, len);
Edouard@168
   204
Edouard@168
   205
        if(access(path, F_OK)){ 
Edouard@158
   206
            int fd;
Edouard@158
   207
            if(access(dirname, F_OK )) { 
Edouard@158
   208
                mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR);
Edouard@158
   209
            }
vb@149
   210
Edouard@168
   211
            fd = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
vb@149
   212
Edouard@158
   213
            if(fd>0) {
Edouard@158
   214
                write(fd, gpg_conf_empty, strlen(gpg_conf_empty));
Edouard@158
   215
                close(fd);
Edouard@158
   216
            }
Edouard@158
   217
        }
vb@1
   218
vb@1
   219
        done = true;
vb@1
   220
    }
Edouard@168
   221
Edouard@168
   222
    if(conf) *conf=path;
Edouard@168
   223
    if(home) *home=dirname;
Edouard@168
   224
Edouard@168
   225
    return true;
vb@1
   226
}
Edouard@168
   227
krista@763
   228
static bool ensure_gpg_agent_conf(const char **agent_conf){
krista@763
   229
    static char agent_path[MAX_PATH];
krista@763
   230
    static bool done = false;
krista@763
   231
krista@763
   232
    if (!done) {
krista@763
   233
        const char *dirname;
krista@763
   234
krista@763
   235
        if (!ensure_gpg_home(NULL, &dirname)) /* Then dirname won't be set. */
krista@763
   236
            return false;
krista@763
   237
krista@763
   238
        char *p;
krista@763
   239
        p = stpncpy(agent_path, dirname, MAX_PATH);
krista@763
   240
        
Edouard@814
   241
        ssize_t len = MAX_PATH - (p - agent_path) - 2;
krista@763
   242
krista@763
   243
        if (len < strlen(gpg_agent_conf_name))
krista@763
   244
        {
krista@763
   245
            assert(0);
krista@763
   246
            return false;
krista@763
   247
        }
krista@763
   248
krista@763
   249
        *p++ = '/';
krista@763
   250
     
krista@763
   251
        strncpy(p, gpg_agent_conf_name, len);
krista@763
   252
krista@763
   253
        if(access(agent_path, F_OK)){ 
krista@763
   254
            int fd;
krista@763
   255
            if(access(dirname, F_OK )) { 
krista@763
   256
                mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR);
krista@763
   257
            }
krista@763
   258
krista@763
   259
            fd = open(agent_path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
krista@763
   260
krista@763
   261
            if(fd>0) {
krista@763
   262
                write(fd, gpg_conf_empty, strlen(gpg_conf_empty));
krista@763
   263
                close(fd);
krista@763
   264
            }
krista@763
   265
        }
krista@763
   266
        done = true;
krista@763
   267
    }
krista@763
   268
    if(agent_conf) *agent_conf=agent_path;
krista@763
   269
krista@763
   270
    return true;
krista@763
   271
}
krista@763
   272
Edouard@168
   273
const char *gpg_conf(void)
Edouard@168
   274
{
Edouard@168
   275
    const char *conf;
Edouard@168
   276
    if(ensure_gpg_home(&conf, NULL))
Edouard@168
   277
        return conf;
Edouard@168
   278
    return NULL;
Edouard@168
   279
}
Edouard@168
   280
Edouard@168
   281
const char *gpg_home(void)
Edouard@168
   282
{
Edouard@168
   283
    const char *home;
Edouard@168
   284
    if(ensure_gpg_home(NULL, &home))
Edouard@168
   285
        return home;
Edouard@168
   286
    return NULL;
Edouard@168
   287
}
krista@763
   288
krista@763
   289
const char *gpg_agent_conf(void)
krista@763
   290
{
krista@763
   291
    const char *agent_conf;
krista@763
   292
    if(ensure_gpg_agent_conf(&agent_conf))
krista@763
   293
        return agent_conf;
krista@763
   294
    return NULL;
krista@763
   295
}