src/pEp_internal.h
author us@chu.huenfield.org
Tue, 25 Dec 2018 14:46:45 +0100
branchsync
changeset 3209 c15b4ca2b52a
parent 3191 53dcb2892c3f
child 3212 fb4124923112
permissions -rw-r--r--
Replace use of Sequoia's backend with a custom key store.

- Sequoia's key store doesn't meet pep's needs (in particular, the
ability to search on a key's user id) and trying to shoehorn pep's
needs onto Sequoia's key store abstractions is just introducing
overhead with no appreciable gain in functionality.

- This patch changes the Sequoia backend to use a local sqlite
database to store the public keys.
vb@1517
     1
// This file is under GNU General Public License 3.0
vb@1517
     2
// see LICENSE.txt
vb@1517
     3
krista@3051
     4
#define PEP_ENGINE_VERSION "1.1.1"
vb@908
     5
vb@908
     6
// maximum attachment size to import as key 1MB, maximum of 20 attachments
vb@908
     7
vb@908
     8
#define MAX_KEY_SIZE (1024 * 1024)
vb@908
     9
#define MAX_KEYS_TO_IMPORT  20
vb@24
    10
krista@2612
    11
#define KEY_EXPIRE_DELTA (60 * 60 * 24 * 365)
krista@2612
    12
vb@233
    13
// this is 20 trustwords with 79 chars max
vb@251
    14
#define MAX_TRUSTWORDS_SPACE (20 * 80)
vb@24
    15
vb@24
    16
// XML parameters string
vb@24
    17
#define PARMS_MAX 32768
vb@24
    18
vb@24
    19
// maximum busy wait time in ms
vb@24
    20
#define BUSY_WAIT_TIME 5000
vb@24
    21
vb@24
    22
// maximum line length for reading gpg.conf
vb@24
    23
#define MAX_LINELENGTH 1024
vb@24
    24
vb@24
    25
// default keyserver
vb@450
    26
#ifndef DEFAULT_KEYSERVER
vb@24
    27
#define DEFAULT_KEYSERVER "hkp://keys.gnupg.net"
vb@450
    28
#endif
vb@450
    29
vb@450
    30
// crashdump constants
vb@450
    31
#ifndef CRASHDUMP_DEFAULT_LINES
vb@450
    32
#define CRASHDUMP_DEFAULT_LINES 100
vb@450
    33
#endif
vb@450
    34
#define CRASHDUMP_MAX_LINES 32767
vb@24
    35
krista@2045
    36
// p≡p full string, NUL-terminated
krista@2045
    37
#ifndef PEP_SUBJ_STRING
krista@2045
    38
#define PEP_SUBJ_STRING {0x70,0xE2,0x89,0xA1,0x70,0x00}
krista@2045
    39
#define PEP_SUBJ_BYTELEN 5
krista@2045
    40
#endif
krista@2045
    41
krista@2078
    42
#ifndef PEP_SUBJ_KEY
krista@2078
    43
#define PEP_SUBJ_KEY "Subject: "
krista@2078
    44
#define PEP_SUBJ_KEY_LC "subject: "
krista@2078
    45
#define PEP_SUBJ_KEY_LEN 9
krista@2078
    46
#endif
krista@2078
    47
krista@2120
    48
#ifndef PEP_MSG_WRAP_KEY
krista@2120
    49
#define PEP_MSG_WRAP_KEY "pEp-Wrapped-Message-Info: "
krista@2120
    50
#define PEP_MSG_WRAP_KEY_LC "pep-wrapped-message-info: "
krista@2120
    51
#define PEP_MSG_WRAP_KEY_LEN 26
krista@2078
    52
#endif
krista@2078
    53
krista@2078
    54
vb@130
    55
#include "platform.h"
vb@130
    56
vb@24
    57
#ifdef WIN32
vb@24
    58
#define LOCAL_DB windoze_local_db()
vb@24
    59
#define SYSTEM_DB windoze_system_db()
vb@24
    60
#define LIBGPGME "libgpgme-11.dll"
vb@24
    61
#else // UNIX
vb@24
    62
#define _POSIX_C_SOURCE 200809L
vb@24
    63
#include <dlfcn.h>
krista@2703
    64
#ifdef NDEBUG
vb@24
    65
#define LOCAL_DB unix_local_db()
krista@2703
    66
#else
krista@2703
    67
#define LOCAL_DB unix_local_db(false)
krista@2703
    68
#endif
vb@24
    69
#ifndef SYSTEM_DB
vb@24
    70
#define SYSTEM_DB "/usr/share/pEp/system.db"
vb@24
    71
#endif
vb@24
    72
#ifndef LIBGPGME
vb@24
    73
#define LIBGPGME "libgpgme-pthread.so"
vb@24
    74
#endif
vb@24
    75
#endif
vb@24
    76
vb@24
    77
#include <locale.h>
vb@24
    78
#include <stdlib.h>
vb@24
    79
#include <string.h>
vb@24
    80
#include <assert.h>
vb@24
    81
#include <stdio.h>
edouard@1521
    82
#include <ctype.h>
krista@2176
    83
#include <math.h>
vb@24
    84
damiano@1997
    85
#ifdef SQLITE3_FROM_OS
damiano@1997
    86
#include <sqlite3.h>
damiano@1997
    87
#else
vb@24
    88
#include "sqlite3.h"
damiano@1997
    89
#endif
vb@24
    90
vb@24
    91
#include "pEpEngine.h"
Edouard@168
    92
Edouard@168
    93
// If not specified, build for GPG
neal@3191
    94
#ifndef USE_SEQUOIA
Edouard@168
    95
#ifndef USE_NETPGP
Edouard@168
    96
#ifndef USE_GPG
Edouard@168
    97
#define USE_GPG
Edouard@168
    98
#endif
Edouard@168
    99
#endif
neal@3191
   100
#endif
Edouard@168
   101
Edouard@168
   102
#ifdef USE_GPG
vb@24
   103
#include "pgp_gpg_internal.h"
vb@229
   104
#elif defined(USE_NETPGP)
Edouard@168
   105
#include "pgp_netpgp_internal.h"
neal@3191
   106
#elif defined(USE_SEQUOIA)
neal@3191
   107
#include "pgp_sequoia_internal.h"
vb@24
   108
#endif
vb@24
   109
vb@292
   110
#include "keymanagement.h"
vb@28
   111
#include "cryptotech.h"
vb@28
   112
#include "transport.h"
vb@2847
   113
#include "sync_api.h"
vb@2830
   114
#include "Sync_func.h"
vb@28
   115
krista@2947
   116
#include "key_reset.h"
vb@28
   117
vb@125
   118
#define NOT_IMPLEMENTED assert(0); return PEP_UNKNOWN_ERROR;
vb@24
   119
edouard@1603
   120
struct _pEpSession;
edouard@1603
   121
typedef struct _pEpSession pEpSession;
edouard@1603
   122
struct _pEpSession {
vb@24
   123
    const char *version;
vb@2833
   124
    messageToSend_t messageToSend;
vb@2833
   125
Edouard@168
   126
#ifdef USE_GPG
vb@24
   127
    gpgme_ctx_t ctx;
vb@229
   128
#elif defined(USE_NETPGP)
Edouard@252
   129
    pEpNetPGPSession ctx;
neal@3191
   130
#elif defined(USE_SEQUOIA)
neal@3191
   131
    sq_context_t ctx;
neal@3191
   132
    sqlite3 *key_db;
neal@3191
   133
    sqlite3_stmt *begin_transaction;
neal@3191
   134
    sqlite3_stmt *commit_transaction;
neal@3191
   135
    sqlite3_stmt *rollback_transaction;
us@3209
   136
    sqlite3_stmt *tpk_find;
us@3209
   137
    sqlite3_stmt *tsk_find;
us@3209
   138
    sqlite3_stmt *tpk_find_by_keyid;
us@3209
   139
    sqlite3_stmt *tsk_find_by_keyid;
us@3209
   140
    sqlite3_stmt *tpk_find_by_email;
us@3209
   141
    sqlite3_stmt *tsk_find_by_email;
us@3209
   142
    sqlite3_stmt *tpk_all;
neal@3191
   143
    sqlite3_stmt *tsk_all;
us@3209
   144
    sqlite3_stmt *tpk_save_insert_primary;
us@3209
   145
    sqlite3_stmt *tpk_save_insert_subkeys;
us@3209
   146
    sqlite3_stmt *tpk_save_insert_userids;
vb@24
   147
#endif
vb@24
   148
vb@62
   149
    PEP_cryptotech_t *cryptotech;
vb@62
   150
    PEP_transport_t *transports;
vb@28
   151
vb@24
   152
    sqlite3 *db;
vb@24
   153
    sqlite3 *system_db;
vb@24
   154
vb@24
   155
    sqlite3_stmt *log;
vb@233
   156
    sqlite3_stmt *trustword;
vb@24
   157
    sqlite3_stmt *get_identity;
krista@2461
   158
    sqlite3_stmt *get_identity_without_trust_check;
krista@2461
   159
    sqlite3_stmt *get_identities_by_address;
krista@2966
   160
    sqlite3_stmt *get_identities_by_userid;
krista@2893
   161
    sqlite3_stmt *get_identities_by_main_key_id;
krista@1799
   162
    sqlite3_stmt *replace_identities_fpr;
krista@2461
   163
    sqlite3_stmt *replace_main_user_fpr;
krista@2461
   164
    sqlite3_stmt *get_main_user_fpr;
krista@2461
   165
    sqlite3_stmt *refresh_userid_default_key;
krista@2948
   166
    sqlite3_stmt *delete_key;
krista@2461
   167
    sqlite3_stmt *remove_fpr_as_default;
vb@24
   168
    sqlite3_stmt *set_person;
krista@2480
   169
    sqlite3_stmt *update_person;
krista@2965
   170
    sqlite3_stmt *delete_person;
krista@2480
   171
    sqlite3_stmt *exists_person;    
vb@2833
   172
    sqlite3_stmt *set_as_pEp_user;
vb@2833
   173
    sqlite3_stmt *is_pEp_user;
krista@2747
   174
    sqlite3_stmt *add_into_social_graph;
krista@2752
   175
    sqlite3_stmt *get_own_address_binding_from_contact;
krista@2752
   176
    sqlite3_stmt *set_revoke_contact_as_notified;
krista@2752
   177
    sqlite3_stmt *get_contacted_ids_from_revoke_fpr;
krista@2752
   178
    sqlite3_stmt *was_id_for_revoke_contacted;
krista@2800
   179
    sqlite3_stmt *get_last_contacted;
edouard@1234
   180
    sqlite3_stmt *set_device_group;
edouard@1235
   181
    sqlite3_stmt *get_device_group;
vb@24
   182
    sqlite3_stmt *set_pgp_keypair;
krista@2478
   183
    sqlite3_stmt *set_identity_entry;
krista@2480
   184
    sqlite3_stmt *update_identity_entry;
krista@2480
   185
    sqlite3_stmt *exists_identity_entry;        
vb@932
   186
    sqlite3_stmt *set_identity_flags;
edouard@1394
   187
    sqlite3_stmt *unset_identity_flags;
vb@24
   188
    sqlite3_stmt *set_trust;
krista@2543
   189
    sqlite3_stmt *update_trust;
krista@2480
   190
    sqlite3_stmt *exists_trust_entry;
vb@2834
   191
    sqlite3_stmt *update_trust_to_pEp;
krista@1799
   192
    sqlite3_stmt *update_trust_for_fpr;
vb@24
   193
    sqlite3_stmt *get_trust;
krista@2967
   194
    sqlite3_stmt *get_trust_by_userid;
vb@251
   195
    sqlite3_stmt *least_trust;
krista@2593
   196
    sqlite3_stmt *mark_compromised;
Edouard@409
   197
    sqlite3_stmt *reset_trust;
vb@450
   198
    sqlite3_stmt *crashdump;
vb@458
   199
    sqlite3_stmt *languagelist;
vb@458
   200
    sqlite3_stmt *i18n_token;
krista@2461
   201
    sqlite3_stmt *replace_userid;
fdik@494
   202
fdik@494
   203
    // blacklist
fdik@494
   204
    sqlite3_stmt *blacklist_add;
fdik@494
   205
    sqlite3_stmt *blacklist_delete;
fdik@494
   206
    sqlite3_stmt *blacklist_is_listed;
fdik@494
   207
    sqlite3_stmt *blacklist_retrieve;
Edouard@584
   208
    
krista@2893
   209
    // Keys
Edouard@584
   210
    sqlite3_stmt *own_key_is_listed;
vb@955
   211
    sqlite3_stmt *own_identities_retrieve;
edouard@1394
   212
    sqlite3_stmt *own_keys_retrieve;
krista@2461
   213
    sqlite3_stmt *get_user_default_key;
krista@2893
   214
    sqlite3_stmt *get_all_keys_for_user;
krista@2461
   215
        
krista@2461
   216
    sqlite3_stmt *get_default_own_userid;
krista@2461
   217
krista@2893
   218
krista@2461
   219
//    sqlite3_stmt *set_own_key;
vb@292
   220
vb@632
   221
    // sequence value
vb@633
   222
    sqlite3_stmt *sequence_value1;
vb@633
   223
    sqlite3_stmt *sequence_value2;
vb@632
   224
edouard@1236
   225
    // revoked keys
Edouard@693
   226
    sqlite3_stmt *set_revoked;
Edouard@693
   227
    sqlite3_stmt *get_revoked;
krista@2752
   228
    sqlite3_stmt *get_replacement_fpr;
Edouard@693
   229
krista@2471
   230
    // mistrusted
krista@2471
   231
    sqlite3_stmt* add_mistrusted_key;
krista@2471
   232
    sqlite3_stmt* is_mistrusted_key;    
krista@2471
   233
    sqlite3_stmt* delete_mistrusted_key;
krista@2471
   234
    
krista@2461
   235
    // aliases
krista@2461
   236
    sqlite3_stmt *get_userid_alias_default;
krista@2461
   237
    sqlite3_stmt *add_userid_alias;
krista@2461
   238
Edouard@693
   239
    // callbacks
vb@292
   240
    examine_identity_t examine_identity;
vb@292
   241
    void *examine_management;
edouard@1462
   242
    void *sync_management;
vb@599
   243
    void *sync_obj;
krista@2271
   244
    notifyHandshake_t notifyHandshake;
vb@2839
   245
    inject_sync_event_t inject_sync_event;
vb@2839
   246
    retrieve_next_sync_event_t retrieve_next_sync_event;
vb@464
   247
vb@2830
   248
    // pEp Sync
vb@2830
   249
    struct Sync_state_s sync_state;
vb@2830
   250
    struct own_Sync_state_s own_sync_state;
vb@2830
   251
vb@2830
   252
//     void* sync_state_payload;
vb@2830
   253
//     char sync_uuid[37];
vb@2830
   254
//     time_t LastCannotDecrypt;
vb@2830
   255
//     time_t LastUpdateRequest;
vb@690
   256
vb@464
   257
    // runtime config
vb@464
   258
vb@464
   259
    bool passive_mode;
vb@464
   260
    bool unencrypted_subject;
vb@1110
   261
    bool keep_sync_msg;
vb@1819
   262
    bool service_log;
Edouard@720
   263
    
roker@1722
   264
#ifdef DEBUG_ERRORSTACK
roker@1722
   265
    stringlist_t* errorstack;
roker@1722
   266
#endif
edouard@1603
   267
};
vb@48
   268
roker@1722
   269
vb@62
   270
PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first);
vb@62
   271
void release_transport_system(PEP_SESSION session, bool out_last);
vb@48
   272
krista@1639
   273
/* NOT to be exposed to the outside!!! */
krista@1639
   274
PEP_STATUS encrypt_only(
krista@1639
   275
        PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
krista@1639
   276
        size_t psize, char **ctext, size_t *csize
krista@1639
   277
);
krista@1639
   278
krista@2047
   279
#if defined(NDEBUG) || defined(NOLOG)
vb@216
   280
#define DEBUG_LOG(TITLE, ENTITY, DESC)
vb@216
   281
#else
huss@1571
   282
#ifdef ANDROID
huss@1571
   283
#include <android/log.h>
vb@1819
   284
#define  LOG_MORE(...)  __android_log_print(ANDROID_LOG_DEBUG, "pEpEngine", " %s :: %s :: %s :: %s ", __VA_ARGS__);
edouard@1630
   285
#else
edouard@1630
   286
#include <stdio.h>
vb@1819
   287
#define  LOG_MORE(...)  fprintf(stderr, "pEpEngine DEBUG_LOG('%s','%s','%s','%s')\n", __VA_ARGS__);
huss@1571
   288
#endif
edouard@1630
   289
#define DEBUG_LOG(TITLE, ENTITY, DESC) {\
vb@1819
   290
    log_event(session, (TITLE), (ENTITY), (DESC), "debug " __FILE__ ":" S_LINE);\
vb@1819
   291
    LOG_MORE((TITLE), (ENTITY), (DESC), __FILE__ ":" S_LINE)\
edouard@1630
   292
}
vb@216
   293
#endif
vb@216
   294
edouard@1746
   295
typedef enum _normalize_hex_rest_t {
edouard@1746
   296
    accept_hex,
edouard@1746
   297
    ignore_hex,
edouard@1746
   298
    reject_hex
edouard@1746
   299
} normalize_hex_res_t;
edouard@1746
   300
edouard@1746
   301
static inline normalize_hex_res_t _normalize_hex(char *hex) 
edouard@1746
   302
{
edouard@1746
   303
    if (*hex >= '0' && *hex <= '9')
edouard@1746
   304
        return accept_hex;
edouard@1746
   305
edouard@1746
   306
    if (*hex >= 'A' && *hex <= 'F') {
edouard@1746
   307
        *hex += 'a' - 'A';
edouard@1746
   308
        return accept_hex;
edouard@1746
   309
    }
edouard@1746
   310
edouard@1746
   311
    if (*hex >= 'a' && *hex <= 'f') 
edouard@1746
   312
        return accept_hex;
edouard@1746
   313
edouard@1746
   314
    if (*hex == ' ') 
edouard@1746
   315
        return ignore_hex;
edouard@1746
   316
edouard@1746
   317
    return reject_hex;
edouard@1746
   318
}
edouard@1746
   319
edouard@1521
   320
// Space tolerant and case insensitive fingerprint string compare
edouard@1746
   321
static inline PEP_STATUS _compare_fprs(
edouard@1746
   322
        const char* fpra,
edouard@1746
   323
        size_t fpras,
edouard@1746
   324
        const char* fprb,
edouard@1746
   325
        size_t fprbs,
edouard@1746
   326
        int* comparison)
edouard@1746
   327
{
edouard@1746
   328
edouard@1746
   329
    size_t ai = 0;
edouard@1746
   330
    size_t bi = 0;
edouard@1746
   331
    size_t significant = 0;
edouard@1746
   332
    int _comparison = 0;
edouard@1746
   333
    const int _FULL_FINGERPRINT_LENGTH = 40;
edouard@1746
   334
   
edouard@1746
   335
    // First compare every non-ignored chars until an end is reached
edouard@1746
   336
    while(ai < fpras && bi < fprbs)
edouard@1746
   337
    {
edouard@1746
   338
        char fprac = fpra[ai];
edouard@1746
   339
        char fprbc = fprb[bi];
edouard@1746
   340
        normalize_hex_res_t fprah = _normalize_hex(&fprac);
edouard@1746
   341
        normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
edouard@1746
   342
edouard@1746
   343
        if(fprah == reject_hex || fprbh == reject_hex)
edouard@1746
   344
            return PEP_ILLEGAL_VALUE;
edouard@1746
   345
edouard@1746
   346
        if ( fprah == ignore_hex )
edouard@1746
   347
        {
edouard@1746
   348
            ai++;
edouard@1746
   349
        }
edouard@1746
   350
        else if ( fprbh == ignore_hex )
edouard@1746
   351
        {
edouard@1746
   352
            bi++;
edouard@1746
   353
        }
edouard@1746
   354
        else
edouard@1746
   355
        {
edouard@1746
   356
            if(fprac != fprbc && _comparison == 0 )
edouard@1746
   357
            {
edouard@1746
   358
                _comparison = fprac > fprbc ? 1 : -1;
edouard@1746
   359
            }
edouard@1746
   360
edouard@1746
   361
            significant++;
edouard@1746
   362
            ai++;
edouard@1746
   363
            bi++;
edouard@1746
   364
edouard@1746
   365
        } 
edouard@1746
   366
    }
edouard@1746
   367
edouard@1746
   368
    // Bail out if we didn't got enough significnt chars
edouard@1746
   369
    if (significant != _FULL_FINGERPRINT_LENGTH )
edouard@1746
   370
        return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
edouard@1746
   371
edouard@1746
   372
    // Then purge remaining chars, all must be ignored chars
edouard@1746
   373
    while ( ai < fpras )
edouard@1746
   374
    {
edouard@1746
   375
        char fprac = fpra[ai];
edouard@1746
   376
        normalize_hex_res_t fprah = _normalize_hex(&fprac);
edouard@1746
   377
        if( fprah == reject_hex )
edouard@1746
   378
            return PEP_ILLEGAL_VALUE;
edouard@1746
   379
        if ( fprah != ignore_hex )
edouard@1746
   380
            return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
edouard@1746
   381
        ai++;
edouard@1746
   382
    }
edouard@1746
   383
    while ( bi < fprbs )
edouard@1746
   384
    {
edouard@1746
   385
        char fprbc = fprb[bi];
edouard@1746
   386
        normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
edouard@1746
   387
        if( fprbh == reject_hex )
edouard@1746
   388
            return PEP_ILLEGAL_VALUE;
edouard@1746
   389
        if ( fprbh != ignore_hex )
edouard@1746
   390
            return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
edouard@1746
   391
        bi++;
edouard@1746
   392
    }
edouard@1746
   393
edouard@1746
   394
    *comparison = _comparison;
edouard@1746
   395
    return PEP_STATUS_OK;
edouard@1746
   396
}
edouard@1746
   397
edouard@1521
   398
static inline int _same_fpr(
edouard@1521
   399
        const char* fpra,
edouard@1521
   400
        size_t fpras,
edouard@1521
   401
        const char* fprb,
edouard@1521
   402
        size_t fprbs
edouard@1521
   403
    )
edouard@1521
   404
{
edouard@1746
   405
    // illegal values are ignored, and considered not same.
edouard@1746
   406
    int comparison = 1;
edouard@1746
   407
edouard@1746
   408
    _compare_fprs(fpra, fpras, fprb, fprbs, &comparison);
edouard@1746
   409
edouard@1746
   410
    return comparison == 0;
edouard@1521
   411
}
roker@1722
   412
krista@2045
   413
// size is the length of the bytestr that's coming in. This is really only intended
krista@2045
   414
// for comparing two full strings. If charstr's length is different from bytestr_size,
krista@2045
   415
// we'll return a non-zero value.
krista@2045
   416
static inline int _unsigned_signed_strcmp(const unsigned char* bytestr, const char* charstr, int bytestr_size) {
krista@2045
   417
    int charstr_len = strlen(charstr);
krista@2045
   418
    if (charstr_len != bytestr_size)
krista@2045
   419
        return -1; // we don't actually care except that it's non-zero
krista@2045
   420
    return memcmp(bytestr, charstr, bytestr_size);
krista@2045
   421
}
krista@2045
   422
krista@2045
   423
// This is just a horrible example of C type madness. UTF-8 made me do it.
vb@2834
   424
static inline char* _pEp_subj_copy() {
krista@2285
   425
#ifndef WIN32
vb@2834
   426
    unsigned char pEpstr[] = PEP_SUBJ_STRING;
krista@2045
   427
    void* retval = calloc(1, sizeof(unsigned char)*PEP_SUBJ_BYTELEN + 1);
vb@2834
   428
    memcpy(retval, pEpstr, PEP_SUBJ_BYTELEN);
krista@2045
   429
    return (char*)retval;
krista@2285
   430
#else
krista@2285
   431
    return strdup("pEp");
krista@2285
   432
#endif
krista@2045
   433
}
krista@2045
   434
krista@2461
   435
static inline bool is_me(PEP_SESSION session, pEp_identity* test_ident) {
krista@2461
   436
    bool retval = false;
krista@2461
   437
    if (test_ident && test_ident->user_id) {
krista@2461
   438
        char* def_id = NULL;
krista@2461
   439
        get_default_own_userid(session, &def_id);
krista@2461
   440
        if (test_ident->me || 
krista@2461
   441
            (def_id && strcmp(def_id, test_ident->user_id) == 0)) {
krista@2461
   442
            retval = true;
krista@2461
   443
        }
krista@2461
   444
        free(def_id);
krista@2461
   445
    }
krista@2461
   446
    return retval;
krista@2461
   447
}
krista@2461
   448
krista@2461
   449
#ifndef EMPTYSTR
krista@2461
   450
#define EMPTYSTR(STR) ((STR) == NULL || (STR)[0] == '\0')
krista@2461
   451
#endif
krista@2461
   452
krista@2549
   453
#ifndef IS_PGP_CT
krista@2549
   454
#define IS_PGP_CT(CT) (((CT) | PEP_ct_confirmed) == PEP_ct_OpenPGP)
krista@2549
   455
#endif
krista@2549
   456
krista@2461
   457
#ifndef _MIN
krista@2461
   458
#define _MIN(A, B) ((B) > (A) ? (A) : (B))
krista@2461
   459
#endif
krista@2461
   460
#ifndef _MAX
krista@2461
   461
#define _MAX(A, B) ((B) > (A) ? (B) : (A))
krista@2461
   462
#endif
krista@2461
   463
krista@2461
   464
krista@2176
   465
// These are globals used in generating message IDs and should only be
krista@2176
   466
// computed once, as they're either really constants or OS-dependent
krista@2176
   467
krista@2467
   468
extern int _pEp_rand_max_bits;
krista@2467
   469
extern double _pEp_log2_36;
krista@2176
   470
krista@2176
   471
static inline void _init_globals() {
vb@2571
   472
    _pEp_rand_max_bits = (int) ceil(log2((double) RAND_MAX));
krista@2176
   473
    _pEp_log2_36 = log2(36);
krista@2176
   474
}