1pass.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 28 Mar 2015 22:33:10 -0400
changeset 43 85b1cb11d948
parent 39 ddb45e88adc9
child 45 cf6a06f368e6
permissions -rw-r--r--
Whoops, forgot to remove original code that I was fixing. :)
icculus@28
     1
#include <linux/input.h>
icculus@28
     2
#include <fcntl.h>
icculus@0
     3
#include <stdio.h>
icculus@0
     4
#include <stdlib.h>
icculus@0
     5
#include <string.h>
icculus@0
     6
#include <assert.h>
icculus@28
     7
#include <errno.h>
icculus@28
     8
#include <unistd.h>
icculus@31
     9
#include <dirent.h>
icculus@33
    10
#include <signal.h>
icculus@28
    11
icculus@0
    12
#include "lua.h"
icculus@0
    13
#include "lauxlib.h"
icculus@0
    14
#include "lualib.h"
icculus@0
    15
#include "pkcs5_pbkdf2.h"
icculus@0
    16
#include "aes.h"
icculus@0
    17
#include "base64.h"
icculus@0
    18
#include "md5.h"
icculus@17
    19
#include "keyhook.h"
icculus@11
    20
#include <gtk/gtk.h>
icculus@0
    21
icculus@0
    22
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
icculus@0
    23
icculus@28
    24
// plug in a Griffin Powermate, make sure you have access to it, and run with
icculus@28
    25
//  --powermate=/dev/input/eventX
icculus@28
    26
static int powermate_fd = -1;
icculus@28
    27
static int pumpPowermate(void)
icculus@28
    28
{
icculus@28
    29
    struct input_event buf[32];
icculus@28
    30
    int pressed = 0;
icculus@28
    31
    ssize_t br;
icculus@28
    32
icculus@28
    33
    if (powermate_fd == -1)
icculus@28
    34
        return 0;  // nothing to do.
icculus@28
    35
icculus@28
    36
    while ((br = read(powermate_fd, buf, sizeof (buf))) > 0)
icculus@28
    37
    {
icculus@28
    38
        ssize_t i;
icculus@28
    39
        br /= sizeof (buf[0]);
icculus@28
    40
        for (i = 0; i < br; i++)
icculus@28
    41
        {
icculus@28
    42
            struct input_event *ev = &buf[i];
icculus@28
    43
            if ((ev->type == EV_KEY) && (ev->code == BTN_0) && (ev->value))
icculus@28
    44
                pressed = 1;
icculus@28
    45
        } // for
icculus@28
    46
    } // while
icculus@28
    47
icculus@28
    48
    return pressed;
icculus@28
    49
}
icculus@28
    50
icculus@28
    51
static void setPowermateLED(const int enable)
icculus@28
    52
{
icculus@28
    53
    struct input_event ev;
icculus@28
    54
    const int brightness = enable ? 255 : 0;
icculus@28
    55
    const int pulse_speed = 255;
icculus@28
    56
    const int pulse_table = 0;
icculus@28
    57
    const int pulse_awake = enable ? 1 : 0;
icculus@28
    58
    const int pulse_asleep = 0;
icculus@28
    59
icculus@28
    60
    if (powermate_fd == -1)
icculus@28
    61
        return;
icculus@28
    62
icculus@28
    63
    memset(&ev, '\0', sizeof (ev));
icculus@28
    64
    ev.type = EV_MSC;
icculus@28
    65
    ev.code = MSC_PULSELED;
icculus@28
    66
    ev.value = brightness | (pulse_speed << 8) | (pulse_table << 17) | (pulse_asleep << 19) | (pulse_awake << 20);
icculus@28
    67
icculus@28
    68
    if (write(powermate_fd, &ev, sizeof (ev)) != sizeof (ev))
icculus@28
    69
        fprintf(stderr, "WARNING: tried to set Powermate LED and failed: %s\n", strerror(errno));
icculus@28
    70
} // setPowermateLED
icculus@28
    71
icculus@28
    72
icculus@31
    73
static int openPowermate(const char *fname)
icculus@31
    74
{
icculus@31
    75
    static const char const *known_names[] = {
icculus@31
    76
        "Griffin PowerMate", "Griffin SoundKnob"
icculus@31
    77
    };
icculus@31
    78
icculus@31
    79
    char buf[255];
icculus@31
    80
    int ok = 0;
icculus@31
    81
    int fd;
icculus@31
    82
    int i;
icculus@31
    83
icculus@31
    84
    if (!fname)
icculus@31
    85
        return -1;
icculus@31
    86
icculus@31
    87
    if ((fd = open(fname, O_RDWR)) == -1)
icculus@31
    88
        fprintf(stderr, "WARNING: couldn't open Powermate at %s: %s\n", fname, strerror(errno));
icculus@31
    89
icculus@31
    90
    if (ioctl(fd, EVIOCGNAME(sizeof (buf)), buf) == -1)
icculus@31
    91
    {
icculus@31
    92
        fprintf(stderr, "EVIOCGNAME failed for %s: %s\n", fname, strerror(errno));
icculus@31
    93
        close(fd);
icculus@31
    94
        return -1;
icculus@31
    95
    } // if
icculus@31
    96
icculus@31
    97
    for (i = 0; !ok && (i < sizeof (known_names) / sizeof (known_names[0])); i++)
icculus@31
    98
    {
icculus@31
    99
        if (strncmp(buf, known_names[i], strlen(known_names[i])) == 0)
icculus@31
   100
            ok = 1;
icculus@31
   101
    } // for
icculus@31
   102
icculus@31
   103
    if (!ok)
icculus@31
   104
    {
icculus@31
   105
        close(fd);
icculus@31
   106
        return -1;
icculus@31
   107
    } // if
icculus@31
   108
icculus@31
   109
    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
icculus@31
   110
    return fd;
icculus@31
   111
} // openPowermate
icculus@31
   112
icculus@31
   113
icculus@31
   114
static void deinitPowermate(void)
icculus@31
   115
{
icculus@34
   116
    if (powermate_fd != -1)
icculus@31
   117
    {
icculus@31
   118
        setPowermateLED(0);
icculus@31
   119
        close(powermate_fd);
icculus@31
   120
        powermate_fd = -1;
icculus@31
   121
    } // if
icculus@31
   122
} // deinitPowermate
icculus@31
   123
icculus@31
   124
icculus@28
   125
static void initPowermate(int *_argc, char **argv)
icculus@28
   126
{
icculus@28
   127
    const char *arg = "--powermate=";
icculus@28
   128
    const size_t arglen = strlen(arg);
icculus@28
   129
    int argc = *_argc;
icculus@28
   130
    int i;
icculus@28
   131
icculus@28
   132
    for (i = 1; i < argc; i++)
icculus@28
   133
    {
icculus@28
   134
        const char *thisarg = argv[i];
icculus@28
   135
        if (strncmp(thisarg, arg, arglen) != 0)
icculus@28
   136
            continue;
icculus@28
   137
icculus@28
   138
        thisarg += arglen;
icculus@31
   139
icculus@31
   140
        if (strcmp(thisarg, "auto") == 0)
icculus@28
   141
        {
icculus@31
   142
            DIR *dirp = opendir("/dev/input");
icculus@31
   143
            if (dirp)
icculus@31
   144
            {
icculus@31
   145
                struct dirent *dent;
icculus@31
   146
                while ((dent = readdir(dirp)) != NULL)
icculus@31
   147
                {
icculus@31
   148
                    const char *name = dent->d_name;
icculus@31
   149
                    char buf[PATH_MAX];
icculus@31
   150
                    if (strncmp(name, "event", 5) != 0)
icculus@31
   151
                        continue;
icculus@31
   152
                    snprintf(buf, sizeof (buf), "/dev/input/%s", name);
icculus@31
   153
                    if (powermate_fd == -1)
icculus@31
   154
                    {
icculus@31
   155
                        powermate_fd = openPowermate(buf);
icculus@31
   156
                        if (powermate_fd != -1)
icculus@31
   157
                        {
icculus@31
   158
                            printf("Found Powermate at %s\n", buf);
icculus@31
   159
                            break;
icculus@31
   160
                        } // if
icculus@31
   161
                    } // if
icculus@31
   162
                } // while
icculus@31
   163
                closedir(dirp);
icculus@31
   164
            } // if
icculus@31
   165
            thisarg = NULL;
icculus@31
   166
icculus@31
   167
        } // if
icculus@28
   168
icculus@28
   169
        // eliminate this command line.
icculus@28
   170
        memmove(&argv[i], &argv[i+1], (argc-i) * sizeof (char *));
icculus@28
   171
        argc--;
icculus@31
   172
icculus@31
   173
        if (powermate_fd == -1)
icculus@31
   174
            powermate_fd = openPowermate(thisarg);
icculus@28
   175
    } // for
icculus@28
   176
icculus@31
   177
    atexit(deinitPowermate);
icculus@31
   178
icculus@28
   179
    *_argc = argc;
icculus@28
   180
} // initPowermate
icculus@28
   181
icculus@28
   182
icculus@0
   183
static lua_State *luaState = NULL;
icculus@0
   184
static const uint8_t zero16[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
icculus@0
   185
static const char saltprefix[] = { 'S', 'a', 'l', 't', 'e', 'd', '_', '_' };
icculus@0
   186
icculus@0
   187
static inline int retvalStringBytes(lua_State *L, const uint8_t *str, size_t len)
icculus@0
   188
{
icculus@0
   189
    if (str != NULL)
icculus@0
   190
        lua_pushlstring(L, (const char *) str, len);
icculus@0
   191
    else
icculus@0
   192
        lua_pushnil(L);
icculus@0
   193
    return 1;
icculus@0
   194
} // retvalStringBytes
icculus@0
   195
icculus@12
   196
static inline int retvalString(lua_State *L, const char *str)
icculus@12
   197
{
icculus@15
   198
    return retvalStringBytes(L, (const uint8_t *) str, str ? strlen(str) : 0);
icculus@12
   199
} // retvalString
icculus@12
   200
icculus@11
   201
static inline int retvalPointer(lua_State *L, void *ptr)
icculus@11
   202
{
icculus@11
   203
    if (ptr != NULL)
icculus@11
   204
        lua_pushlightuserdata(L, ptr);
icculus@11
   205
    else
icculus@11
   206
        lua_pushnil(L);
icculus@11
   207
    return 1;
icculus@11
   208
} // retvalPointer
icculus@11
   209
icculus@0
   210
static inline void xorBlock(uint8_t *dst, const uint8_t *src)
icculus@0
   211
{
icculus@0
   212
    int i;
icculus@0
   213
    for (i = 0; i < 16; i++, dst++, src++)
icculus@0
   214
        *dst ^= *src;
icculus@0
   215
} // xorBlock
icculus@0
   216
icculus@0
   217
static int decryptUsingKeyAndIvec(uint8_t *data, size_t *datalen, const uint8_t *key, const uint8_t *iv)
icculus@0
   218
{
icculus@0
   219
    const size_t blocks = *datalen / 16;
icculus@0
   220
    uint8_t *block = data + ((blocks-1) * 16);   // start at final block, work backwards
icculus@0
   221
    const uint8_t *padding = &block[15];
icculus@0
   222
    uint8_t expkey[aesExpandedKeySize];
icculus@0
   223
    size_t i;
icculus@0
   224
icculus@0
   225
    if (blocks == 0)
icculus@0
   226
        return 1;  // nothing to do.
icculus@0
   227
icculus@0
   228
	aesExpandKey(key, expkey);
icculus@0
   229
icculus@0
   230
    for (i = 0; i < blocks-1; i++)
icculus@0
   231
    {
icculus@0
   232
        aesDecrypt(block, expkey, block);   // decrypt in place.
icculus@0
   233
        xorBlock(block, block-16);
icculus@0
   234
        block -= 16;
icculus@0
   235
    }
icculus@0
   236
    aesDecrypt(block, expkey, block);   // decrypt in place.
icculus@0
   237
    xorBlock(block, iv);   // xor against initial vector for final block.
icculus@0
   238
icculus@0
   239
    if (*padding > 16)
icculus@0
   240
        return 0;  // bad data?
icculus@0
   241
icculus@0
   242
    *datalen -= *padding;
icculus@0
   243
icculus@0
   244
    return 1;
icculus@0
   245
} // decryptBinaryUsingKeyAndIvec
icculus@0
   246
icculus@0
   247
icculus@0
   248
static inline int isSalted(const uint8_t *data, const size_t datalen)
icculus@0
   249
{
icculus@0
   250
    return ( (datalen > sizeof (saltprefix)) &&
icculus@0
   251
             (memcmp(data, saltprefix, sizeof (saltprefix)) == 0) );
icculus@0
   252
} // isSalted
icculus@0
   253
icculus@0
   254
icculus@0
   255
static int decryptUsingPBKDF2(lua_State *L)
icculus@0
   256
{
icculus@0
   257
    const char *base64 = luaL_checkstring(L, 1);
icculus@0
   258
    const char *password = luaL_checkstring(L, 2);
icculus@0
   259
    const int iterations = luaL_checkinteger(L, 3);
icculus@0
   260
    size_t datalen = strlen(base64);
icculus@0
   261
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
icculus@0
   262
    uint8_t *data = dataptr;
icculus@0
   263
    base64_decodestate base64state;
icculus@0
   264
icculus@0
   265
    base64_init_decodestate(&base64state);
icculus@0
   266
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
icculus@0
   267
icculus@0
   268
    const uint8_t *salt = zero16;
icculus@0
   269
    int saltlen = sizeof (zero16);
icculus@0
   270
    if (isSalted(data, datalen))
icculus@0
   271
    {
icculus@0
   272
        salt = data + 8;
icculus@0
   273
        saltlen = 8;
icculus@0
   274
        data += 16;
icculus@0
   275
        datalen -= 16;
icculus@0
   276
    } // if
icculus@0
   277
icculus@0
   278
    uint8_t output[32];
icculus@0
   279
    pkcs5_pbkdf2(password, strlen(password), salt, saltlen, output, sizeof (output), (unsigned int) iterations);
icculus@0
   280
icculus@0
   281
    const uint8_t *aeskey = &output[0];
icculus@0
   282
    const uint8_t *aesiv = &output[16];
icculus@0
   283
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
icculus@0
   284
        retvalStringBytes(L, data, datalen);
icculus@0
   285
    else
icculus@0
   286
        lua_pushnil(L);
icculus@0
   287
icculus@0
   288
    free(dataptr);
icculus@0
   289
    return 1;
icculus@0
   290
} // decryptUsingPBKDF2
icculus@0
   291
icculus@0
   292
icculus@0
   293
static int decryptBase64UsingKey(lua_State *L)
icculus@0
   294
{
icculus@0
   295
    size_t keylen = 0;
icculus@0
   296
    const char *base64 = luaL_checkstring(L, 1);
icculus@0
   297
    const uint8_t *key = (const uint8_t *) luaL_checklstring(L, 2, &keylen);
icculus@0
   298
    size_t datalen = strlen(base64);
icculus@0
   299
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
icculus@0
   300
    uint8_t *data = dataptr;
icculus@0
   301
    base64_decodestate base64state;
icculus@0
   302
icculus@0
   303
    base64_init_decodestate(&base64state);
icculus@0
   304
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
icculus@0
   305
icculus@0
   306
    uint8_t aeskey[16];
icculus@0
   307
    uint8_t aesiv[16];
icculus@0
   308
    MD5_CTX md5;
icculus@0
   309
icculus@0
   310
    if (isSalted(data, datalen))
icculus@0
   311
    {
icculus@0
   312
        const uint8_t *salt = data + 8;
icculus@0
   313
        const size_t saltlen = 8;
icculus@0
   314
        data += 16;
icculus@0
   315
        datalen -= 16;
icculus@0
   316
icculus@0
   317
        assert(aesNr == 10);  // AES-256 needs more rounds.
icculus@0
   318
        assert(aesNk == 4);   // hashing size is hardcoded later.
icculus@0
   319
        uint8_t hashing[32];
icculus@0
   320
icculus@0
   321
        MD5_init(&md5);
icculus@0
   322
        MD5_append(&md5, key, keylen);
icculus@0
   323
        MD5_append(&md5, salt, saltlen);
icculus@0
   324
        MD5_finish(&md5, hashing);
icculus@0
   325
icculus@0
   326
        MD5_init(&md5);
icculus@0
   327
        MD5_append(&md5, hashing, 16);
icculus@0
   328
        MD5_append(&md5, key, keylen);
icculus@0
   329
        MD5_append(&md5, salt, saltlen);
icculus@0
   330
        MD5_finish(&md5, &hashing[16]);
icculus@0
   331
icculus@0
   332
        memcpy(aeskey, hashing, 4 * aesNk);
icculus@0
   333
        memcpy(aesiv, &hashing[4 * aesNk], 16);
icculus@0
   334
    } // if
icculus@0
   335
    else
icculus@0
   336
    {
icculus@0
   337
        MD5_init(&md5);
icculus@0
   338
        MD5_append(&md5, key, keylen);
icculus@0
   339
        MD5_finish(&md5, aeskey);
icculus@0
   340
        memset(aesiv, '\0', sizeof (aesiv));
icculus@0
   341
    } // else
icculus@0
   342
icculus@0
   343
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
icculus@0
   344
        retvalStringBytes(L, data, datalen);
icculus@0
   345
    else
icculus@0
   346
        lua_pushnil(L);
icculus@0
   347
icculus@0
   348
    free(dataptr);
icculus@0
   349
    return 1;
icculus@0
   350
} // decryptBase64UsingKey
icculus@0
   351
icculus@0
   352
icculus@12
   353
static int runGuiPasswordPrompt(lua_State *L)
icculus@12
   354
{
icculus@12
   355
    const char *hintstr = lua_tostring(L, 1);
icculus@12
   356
    GtkWidget *dialog = gtk_dialog_new_with_buttons(
icculus@12
   357
                            "Master Password", NULL, GTK_DIALOG_MODAL,
icculus@12
   358
                            GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
icculus@12
   359
                            GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
icculus@12
   360
                            NULL);
icculus@12
   361
icculus@12
   362
    GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
icculus@12
   363
icculus@12
   364
    if (hintstr != NULL)
icculus@12
   365
    {
icculus@12
   366
        GtkWidget *label = gtk_label_new(hintstr);
icculus@12
   367
        gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
icculus@12
   368
        gtk_container_add(GTK_CONTAINER(content_area), label);
icculus@12
   369
    } // if
icculus@12
   370
icculus@12
   371
    GtkWidget *entry = gtk_entry_new();
icculus@12
   372
    gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
icculus@12
   373
    gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
icculus@12
   374
    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
icculus@12
   375
    gtk_container_add(GTK_CONTAINER(content_area), entry);
icculus@12
   376
icculus@12
   377
    gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
icculus@12
   378
    gtk_widget_show_all(dialog);
icculus@26
   379
    gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);
icculus@12
   380
    const int ok = (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT);
icculus@12
   381
    retvalString(L, ok ? (const char *) gtk_entry_get_text(GTK_ENTRY(entry)) : NULL);
icculus@12
   382
    gtk_widget_destroy(dialog);
icculus@12
   383
icculus@12
   384
    return 1;
icculus@12
   385
} // runGuiPasswordPrompt
icculus@12
   386
icculus@12
   387
icculus@12
   388
static int copyToClipboard(lua_State *L)
icculus@12
   389
{
icculus@12
   390
    const char *str = luaL_checkstring(L, 1);
icculus@12
   391
    gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), str, -1);
icculus@12
   392
    gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), str, -1);
icculus@12
   393
} // copyToClipboard
icculus@12
   394
icculus@12
   395
icculus@11
   396
static int makeGuiMenu(lua_State *L)
icculus@11
   397
{
icculus@11
   398
    return retvalPointer(L, gtk_menu_new());
icculus@11
   399
} // makeGuiMenu
icculus@11
   400
icculus@11
   401
icculus@11
   402
static void clickedMenuItem(void *arg)
icculus@11
   403
{
icculus@11
   404
    // This is the callback from GTK+; now call into our actual Lua callback!
icculus@11
   405
    const int callback = (int) ((size_t)arg);
icculus@11
   406
    lua_rawgeti(luaState, LUA_REGISTRYINDEX, callback);
icculus@11
   407
    lua_call(luaState, 0, 0);
icculus@11
   408
} // clickedMenuItem
icculus@11
   409
icculus@12
   410
#if 0  // !!! FIXME: figure out how to fire this.
icculus@12
   411
static void deletedMenuItem(void *arg)
icculus@12
   412
{
icculus@12
   413
    // Clean up the Lua function we referenced in the Registry.
icculus@12
   414
    const int callback = (int) ((size_t)arg);
icculus@12
   415
printf("unref callback %d\n", callback);
icculus@12
   416
    luaL_unref(luaState, LUA_REGISTRYINDEX, callback);
icculus@12
   417
} // deletedMenuItem
icculus@12
   418
#endif
icculus@11
   419
icculus@11
   420
static int appendGuiMenuItem(lua_State *L)
icculus@11
   421
{
icculus@11
   422
    const int argc = lua_gettop(L);
icculus@11
   423
    GtkWidget *menu = (GtkWidget *) lua_touserdata(L, 1);
icculus@11
   424
    const char *label = luaL_checkstring(L, 2);
icculus@11
   425
    GtkWidget *item = gtk_menu_item_new_with_label(label);
icculus@11
   426
icculus@11
   427
    if ((argc >= 3) && (!lua_isnil(L, 3)))
icculus@11
   428
    {
icculus@11
   429
        assert(lua_isfunction(L, 3));
icculus@11
   430
        lua_pushvalue(L, 3);  // copy the Lua callback (luaL_ref() pops it).
icculus@11
   431
        const int callback = luaL_ref(L, LUA_REGISTRYINDEX);
icculus@11
   432
        gtk_signal_connect_object(GTK_OBJECT(item), "activate", GTK_SIGNAL_FUNC(clickedMenuItem), (gpointer) ((size_t)callback));
icculus@11
   433
    } // if
icculus@11
   434
icculus@11
   435
    gtk_widget_show(item);
icculus@11
   436
    gtk_menu_append(menu, item);
icculus@11
   437
    return retvalPointer(L, item);
icculus@11
   438
} // appendGuiMenuItem
icculus@11
   439
icculus@11
   440
icculus@11
   441
static int setGuiMenuItemSubmenu(lua_State *L)
icculus@11
   442
{
icculus@11
   443
    GtkMenuItem *item = (GtkMenuItem *) lua_touserdata(L, 1);
icculus@11
   444
    GtkWidget *submenu = (GtkWidget *) lua_touserdata(L, 2);
icculus@11
   445
    gtk_menu_item_set_submenu(item, submenu);
icculus@11
   446
    return 0;
icculus@11
   447
} // setGuiMenuItemSubmenu
icculus@11
   448
icculus@11
   449
icculus@11
   450
static int popupGuiMenu(lua_State *L)
icculus@11
   451
{
icculus@11
   452
    GtkMenu *menu = (GtkMenu *) lua_touserdata(L, 1);
icculus@11
   453
    gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
icculus@11
   454
    return 0;
icculus@11
   455
} // popupGuiMenu
icculus@11
   456
icculus@11
   457
icculus@28
   458
static int setPowermateLED_Lua(lua_State *L)
icculus@28
   459
{
icculus@28
   460
    const int enable = lua_toboolean(L, 1);
icculus@28
   461
    setPowermateLED(enable);
icculus@28
   462
    return 0;
icculus@28
   463
} // setPowermateLED_Lua
icculus@28
   464
icculus@28
   465
icculus@17
   466
static void keyhookPressed(void)
icculus@17
   467
{
icculus@17
   468
    lua_getglobal(luaState, "keyhookPressed");
icculus@17
   469
    lua_call(luaState, 0, 0);
icculus@17
   470
} // keyhookPressed
icculus@17
   471
icculus@17
   472
icculus@29
   473
static int pumpLua(void)
icculus@29
   474
{
icculus@29
   475
    lua_getglobal(luaState, "pumpLua");
icculus@29
   476
    lua_call(luaState, 0, 0);
icculus@29
   477
} // pumpLua
icculus@29
   478
icculus@29
   479
icculus@17
   480
static gboolean keyhookPumper(void *arg)
icculus@17
   481
{
icculus@29
   482
    pumpLua();
icculus@17
   483
    if (pumpKeyHook())
icculus@17
   484
        keyhookPressed();
icculus@28
   485
    else if (pumpPowermate())
icculus@28
   486
        keyhookPressed();
icculus@28
   487
icculus@17
   488
    return TRUE;  // keep firing timer
icculus@17
   489
} // keyhookPumper
icculus@17
   490
icculus@17
   491
icculus@11
   492
static int giveControlToGui(lua_State *L)
icculus@11
   493
{
icculus@17
   494
    if (initKeyHook())
icculus@17
   495
    {
icculus@17
   496
        atexit(deinitKeyHook);
icculus@17
   497
        g_timeout_add(200, (GSourceFunc) keyhookPumper, (gpointer) NULL);
icculus@17
   498
    } // if
icculus@17
   499
icculus@11
   500
    gtk_main();
icculus@11
   501
    return 0;
icculus@11
   502
} // giveControlToGui
icculus@11
   503
icculus@7
   504
icculus@0
   505
static void *luaAlloc(void *ud, void *ptr, size_t osize, size_t nsize)
icculus@0
   506
{
icculus@0
   507
    if (nsize == 0)
icculus@0
   508
    {
icculus@0
   509
        free(ptr);
icculus@0
   510
        return NULL;
icculus@0
   511
    } // if
icculus@0
   512
    return realloc(ptr, nsize);
icculus@0
   513
} // luaAlloc
icculus@0
   514
icculus@0
   515
icculus@0
   516
static inline void luaSetCFunc(lua_State *L, lua_CFunction f, const char *sym)
icculus@0
   517
{
icculus@0
   518
    lua_pushcfunction(L, f);
icculus@0
   519
    lua_setglobal(luaState, sym);
icculus@0
   520
} // luaSetCFunc
icculus@0
   521
icculus@0
   522
icculus@0
   523
static int luaFatal(lua_State *L)
icculus@0
   524
{
icculus@0
   525
    const char *errstr = lua_tostring(L, -1);
icculus@0
   526
    fprintf(stderr, "Lua panic: %s\n", errstr ? errstr : "(?)");
icculus@0
   527
    fflush(stderr);
icculus@0
   528
    exit(1);
icculus@0
   529
} // luaFatal
icculus@0
   530
icculus@0
   531
icculus@11
   532
static void deinitLua(void)
icculus@11
   533
{
icculus@11
   534
    if (luaState != NULL)
icculus@11
   535
    {
icculus@11
   536
        lua_close(luaState);
icculus@11
   537
        luaState = NULL;
icculus@11
   538
    } // if
icculus@11
   539
} // deinitLua
icculus@11
   540
icculus@11
   541
icculus@7
   542
static int initLua(const int argc, char **argv)
icculus@0
   543
{
icculus@11
   544
    atexit(deinitLua);
icculus@11
   545
icculus@0
   546
    assert(luaState == NULL);
icculus@0
   547
    luaState = lua_newstate(luaAlloc, NULL);
icculus@0
   548
icculus@0
   549
    lua_atpanic(luaState, luaFatal);
icculus@0
   550
    assert(lua_checkstack(luaState, 20));  // Just in case.
icculus@2
   551
    luaL_openlibs(luaState);
icculus@0
   552
icculus@0
   553
    // Set up initial C functions, etc we want to expose to Lua code...
icculus@0
   554
    luaSetCFunc(luaState, decryptUsingPBKDF2, "decryptUsingPBKDF2");
icculus@0
   555
    luaSetCFunc(luaState, decryptBase64UsingKey, "decryptBase64UsingKey");
icculus@11
   556
    luaSetCFunc(luaState, makeGuiMenu, "makeGuiMenu");
icculus@11
   557
    luaSetCFunc(luaState, appendGuiMenuItem, "appendGuiMenuItem");
icculus@11
   558
    luaSetCFunc(luaState, setGuiMenuItemSubmenu, "setGuiMenuItemSubmenu");
icculus@11
   559
    luaSetCFunc(luaState, popupGuiMenu, "popupGuiMenu");
icculus@11
   560
    luaSetCFunc(luaState, giveControlToGui, "giveControlToGui");
icculus@12
   561
    luaSetCFunc(luaState, runGuiPasswordPrompt, "runGuiPasswordPrompt");
icculus@12
   562
    luaSetCFunc(luaState, copyToClipboard, "copyToClipboard");
icculus@28
   563
    luaSetCFunc(luaState, setPowermateLED_Lua, "setPowermateLED");
icculus@0
   564
icculus@7
   565
    // Set up argv table...
icculus@7
   566
    lua_newtable(luaState);
icculus@7
   567
    int i;
icculus@28
   568
    int luai = 1;
icculus@7
   569
    for (i = 0; i < argc; i++)
icculus@7
   570
    {
icculus@28
   571
        if (argv[i])
icculus@28
   572
        {
icculus@28
   573
            lua_pushinteger(luaState, luai);
icculus@28
   574
            lua_pushstring(luaState, argv[i]);
icculus@28
   575
            lua_settable(luaState, -3);
icculus@28
   576
            luai++;
icculus@28
   577
        } // if
icculus@7
   578
    } // for
icculus@7
   579
    lua_setglobal(luaState, "argv");
icculus@7
   580
icculus@3
   581
    // Transfer control to Lua...
icculus@0
   582
    if (luaL_dofile(luaState, "1pass.lua") != 0)
icculus@16
   583
        luaFatal(luaState);
icculus@0
   584
icculus@0
   585
    return 1;
icculus@0
   586
} // initLua
icculus@0
   587
icculus@0
   588
icculus@33
   589
static void deinitAll()
icculus@33
   590
{
icculus@33
   591
    deinitPowermate();
icculus@33
   592
    deinitKeyHook();
icculus@33
   593
    deinitLua();
icculus@33
   594
} // deinitAll
icculus@33
   595
icculus@33
   596
icculus@33
   597
static void killerSignalCatcher(int sig)
icculus@33
   598
{
icculus@33
   599
    static int been_run = 0;
icculus@33
   600
    if (been_run)
icculus@33
   601
    {
icculus@33
   602
        fprintf(stderr, "Caught signal %d, terminating HARD.\n", sig);
icculus@33
   603
        _exit(0);
icculus@33
   604
    } // if
icculus@33
   605
icculus@33
   606
    been_run = 1;
icculus@33
   607
    fprintf(stderr, "Caught signal %d, terminating.\n", sig);
icculus@33
   608
    exit(0);  // trigger atexit handlers.
icculus@33
   609
} // killerSignalCatcher
icculus@33
   610
icculus@33
   611
icculus@33
   612
static void initSignals(void)
icculus@33
   613
{
icculus@33
   614
    signal(SIGINT, killerSignalCatcher);
icculus@33
   615
    signal(SIGQUIT, killerSignalCatcher);
icculus@33
   616
    signal(SIGILL, killerSignalCatcher);
icculus@33
   617
    signal(SIGFPE, killerSignalCatcher);
icculus@33
   618
    signal(SIGTERM, killerSignalCatcher);
icculus@33
   619
    signal(SIGPIPE, killerSignalCatcher);
icculus@33
   620
    signal(SIGSEGV, killerSignalCatcher);
icculus@33
   621
    signal(SIGABRT, killerSignalCatcher);
icculus@33
   622
    signal(SIGHUP, killerSignalCatcher);
icculus@33
   623
} // initSignals
icculus@33
   624
icculus@33
   625
icculus@39
   626
// this was from PhysicsFS ( https://icculus.org/physfs/ ), zlib license.
icculus@39
   627
static char *readSymLink(const char *path)
icculus@39
   628
{
icculus@39
   629
    ssize_t len = 64;
icculus@39
   630
    ssize_t rc = -1;
icculus@39
   631
    char *retval = NULL;
icculus@39
   632
icculus@39
   633
    while (1)
icculus@39
   634
    {
icculus@39
   635
         char *ptr = (char *) realloc(retval, (size_t) len);
icculus@39
   636
         if (ptr == NULL)
icculus@39
   637
             break;   // out of memory.
icculus@39
   638
         retval = ptr;
icculus@39
   639
icculus@39
   640
         rc = readlink(path, retval, len);
icculus@39
   641
         if (rc == -1)
icculus@39
   642
             break;  // not a symlink, i/o error, etc.
icculus@39
   643
icculus@39
   644
         else if (rc < len)
icculus@39
   645
         {
icculus@39
   646
             retval[rc] = '\0';  // readlink doesn't null-terminate.
icculus@39
   647
             return retval;  // we're good to go.
icculus@39
   648
         } // else if
icculus@39
   649
icculus@39
   650
         len *= 2;  // grow buffer, try again.
icculus@39
   651
    } // while
icculus@39
   652
icculus@39
   653
    if (retval != NULL)
icculus@39
   654
        free(retval);
icculus@39
   655
    return NULL;
icculus@39
   656
} // readSymLink
icculus@39
   657
icculus@39
   658
icculus@39
   659
static void chdirToApp(void)
icculus@39
   660
{
icculus@39
   661
    char *ptr;
icculus@39
   662
    char *path = readSymLink("/proc/self/exe");
icculus@39
   663
    
icculus@39
   664
    if (path == NULL)
icculus@39
   665
        return;  // maybe we're already there, fail later if not.
icculus@39
   666
icculus@39
   667
    ptr = strrchr(path, '/');
icculus@39
   668
    if (ptr != NULL)
icculus@39
   669
    {
icculus@39
   670
        *ptr = '\0';
icculus@39
   671
        if (chdir(path) == -1)
icculus@39
   672
            fprintf(stderr, "Couldn't chdir() to app directory?! %s\n", strerror(errno));
icculus@39
   673
    } // if
icculus@39
   674
icculus@39
   675
    free(path);
icculus@39
   676
} // chdirToApp
icculus@39
   677
icculus@39
   678
icculus@0
   679
int main(int argc, char **argv)
icculus@0
   680
{
icculus@39
   681
    chdirToApp();
icculus@33
   682
    initSignals();
icculus@28
   683
    initPowermate(&argc, argv);
icculus@11
   684
    gtk_init(&argc, &argv);
icculus@0
   685
icculus@7
   686
    if (!initLua(argc, argv))  // this will move control to 1pass.lua
icculus@0
   687
        return 1;
icculus@0
   688
icculus@0
   689
    return 0;
icculus@0
   690
} // main
icculus@0
   691
icculus@0
   692
// end of 1pass.c ...
icculus@0
   693