File: ekeyd.c

package info (click to toggle)
ekeyd 1.1.3-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 628 kB
  • ctags: 859
  • sloc: ansic: 5,189; sh: 271; makefile: 199; perl: 148
file content (308 lines) | stat: -rw-r--r-- 6,240 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* daemon/ekeyd.c
 *
 * Entropy key main daemon
 *
 * Copyright 2009 Simtec Electronics
 *
 * For licence terms refer to the COPYING file.
 */

#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#include <poll.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <syslog.h>

#include "nonce.h"
#include "stream.h"
#include "krnlop.h"
#include "foldback.h"
#include "connection.h"
#include "fds.h"
#include "ekeyd.h"

#include "lstate.h"
#include "daemonise.h"

static bool lua_fd_ready = false;
static estream_state_t *output_stream;

void
lua_fd_activity(int fd, short events, void *pw)
{
    lua_fd_ready = true;
}

bool
lstate_cb_newfd(int fd)
{
    ekeyfd_add(fd, POLLIN, lua_fd_activity, NULL);
    return true;
}

void
lstate_cb_delfd(int fd)
{
    ekeyfd_rm(fd);
}

void
lstate_cb_writefd(int fd)
{
    ekeyfd_set_events(fd, POLLOUT);
}

void
lstate_cb_nowritefd(int fd)
{
    ekeyfd_clear_events(fd, POLLOUT);
}

void ekey_fd_activity(int fd, short events, void *pw)
{
    econ_state_t *econ = pw;
    econ_run(econ);
    if (econ_state(econ) == ESTATE_CLOSE) {
        ekeyfd_rm(econ_get_rd_fd(econ));
        estream_close(econ->key_stream);
        econ->key_stream = NULL;
        lstate_inform_about_key(econ);
    }
}

/** lua interface called to add an ekey */
OpaqueEkey *
add_ekey(const char *devpath, const char *serial)
{
    econ_state_t *econ;

    if (output_stream == NULL) {
        errno = EWOULDBLOCK;
        return NULL;
    }

    econ = econ_open(devpath, output_stream);

    if (econ == NULL)
        return NULL;

    if (serial != NULL)
        econ_setsnum(econ, serial);

    ekeyfd_add(econ_get_rd_fd(econ), POLLIN, ekey_fd_activity, econ);

    syslog(LOG_INFO, "Attached new entropy key %s", devpath);

    return econ;
}

void
kill_ekey(OpaqueEkey *ekey)
{
    char *serialnumber = econ_getsnum(ekey);

    if (serialnumber == NULL) {
        if (ekey->key_stream != NULL) {
            ekeyfd_rm(econ_get_rd_fd(ekey));
            syslog(LOG_INFO, "Detaching entropy key %s", ekey->key_stream->uri);
        } else {
            syslog(LOG_INFO, "Detaching Unknown Entropy key");
        }
    } else {
        if (ekey->key_stream != NULL) {
            ekeyfd_rm(econ_get_rd_fd(ekey));
            syslog(LOG_INFO, "Detaching entropy key %s (%s)",
                   ekey->key_stream->uri, serialnumber);
        } else {
            syslog(LOG_INFO, "Detaching entropy key %s", serialnumber);
        }
        free(serialnumber);
    }
    econ_close(ekey);
}


int query_ekey_status(OpaqueEkey *ekey)
{
    switch (econ_state(ekey)) {
    case ESTATE_CLOSE:
        return EKEY_STATUS_KEYCLOSED;

    case ESTATE_UNTRUSTED:
        return EKEY_STATUS_BADKEY;

    case ESTATE_SESSION:
    case ESTATE_SESSION_SENT:
        return EKEY_STATUS_GOODSERIAL;

    case ESTATE_KEYED_FIRST:
    case ESTATE_KEYED:
        return EKEY_STATUS_KEYED;

    default:
        return EKEY_STATUS_UNKNOWN;
    }
}

/* exported interface documented in ekeyd.h */
char *
retrieve_ekey_serial(OpaqueEkey *ekey)
{
    return econ_getsnum(ekey);
}

bool
open_file_output(const char *fname)
{
    if (output_stream != NULL) {
        errno = EADDRINUSE;
        return false;
    }

    output_stream = estream_open(fname);

    return (output_stream != NULL);
}

bool
open_kernel_output(int bits_per_byte)
{
    static char fname[] =
#if defined(EKEY_OS_OPENBSD) || defined(EKEY_OS_MIRBSD)
        "/dev/srandom"
#else
        "/dev/random"
#endif
        ;
    if (output_stream != NULL) {
        errno = EADDRINUSE;
        return false;
    }

    output_stream = estream_krnl_open(fname, bits_per_byte);

    return (output_stream != NULL);
}

bool
open_foldback_output(void)
{
    if (output_stream != NULL) {
        errno = EADDRINUSE;
        return false;
    }

    output_stream = estream_foldback_open();

    return (output_stream != NULL);
}

static const char *usage=
    "Usage: %s [-f <configfile>] [-p <pidfile>] [-v] [-h]\n"
    "Entropy Key Daemon\n\n"
    "\t-f Read configuration from configfile\n"
    "\t-p Write pid to pidfile\n"
    "\t-v Display version and exit\n"
    "\t-h Display this help and exit\n\n";

int main(int argc, char **argv)
{
    int res;
    int opt;
    char *configfile;
    char *pidfile;

    configfile = strdup(CONFIGFILE);
    pidfile = strdup(PIDFILE);

    while ((opt = getopt(argc, argv, "vhf:p:")) != -1) {
        switch (opt) {
        case 'f':
            free(configfile);
            configfile = strdup(optarg);
            break;

        case 'p':
            free(pidfile);
            pidfile = strdup(optarg);
            break;

        case 'v':
            printf("%s: Version 1.1\n", argv[0]);
            return 0;

        case 'h':
        default:
            fprintf(stderr, usage, argv[0]);
            return 1;
        }
    }

    if (optind != argc) {
        fprintf(stderr, "Unexpected argument\n");
        fprintf(stderr, usage, argv[0]);
        return 1;
    }


    if (lstate_init() == false) {
        return 1;
    }

    if (!lstate_runconfig(configfile)) {
        /* Failed to run the configuration */
        return 1;
    }

    /* Everything is good, daemonise */
    if (lstate_request_daemonise())
        do_daemonise(pidfile);

    /* now we are a daemon, start system logging */
    openlog("ekeyd", LOG_ODELAY, LOG_DAEMON);

    syslog(LOG_INFO, "Starting Entropy Key Daemon");

    while (true) {
        res = ekeyfd_poll(-1);
        if (res == 0)
            break; /* no more fd open, finish */

        if (res < 0) {
            if ((errno == EINTR) || (errno == EWOULDBLOCK))
                continue; /* these errors are ok and the poll is retried */

            syslog(LOG_ERR, "Unhandled error in poll %s, exiting", strerror(errno));

            break;
        }

        if (lua_fd_ready) {
            lstate_controlbytes();
            lua_fd_ready = false;
        }
    }

    lstate_finalise();

    estream_close(output_stream);

    close_nonce();

    unlink(pidfile);

    free(configfile);
    free(pidfile);

    syslog(LOG_INFO, "Entropy Key Daemon Stopping");

    closelog();

    return 0;
}