File: text-plugin-entropy.c

package info (click to toggle)
cdebconf-entropy 0.34
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 636 kB
  • ctags: 109
  • sloc: sh: 2,873; ansic: 882; makefile: 54
file content (320 lines) | stat: -rw-r--r-- 8,890 bytes parent folder | download | duplicates (6)
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
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * cdebconf newt plugin to get random data
 *
 * Copyright © 2008 by Jérémy Bobbio <lunar@debian.org>
 * See debian/copyright
 *
 */

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <syslog.h>
#include <errno.h>
#include <termios.h>

#include <cdebconf/frontend.h>
#include <cdebconf/question.h>
#include <cdebconf/strutl.h>

#define MODULE "entropy"
#define FIFO "/var/run/random.fifo"
#define DEFAULT_KEYSIZE 2925

/* XXX: both should be exported by text frontend. */
#define error(fmt, args...) syslog(LOG_ERR, MODULE ": " fmt, ##args)
#define CHAR_GOBACK '<'

int cdebconf_text_handler_entropy(struct frontend * fe,
                                  struct question * question);

struct entropy {
    struct frontend * fe;
    struct question * question;
    long keysize;
    long bytes_read;
    int last_progress;
    const char * fifo;
    const char * success_template;
    int random_fd;
    int fifo_fd;
    char random_byte;
    int is_backing_up;
};

static void destroy_entropy(struct entropy * entropy_data)
{
    if (0 < entropy_data->fifo_fd) {
        (void) close(entropy_data->fifo_fd);
    }
    if (NULL != entropy_data->fifo) {
        (void) unlink(entropy_data->fifo);
    }
    if (0 < entropy_data->random_fd) {
        (void) close(entropy_data->random_fd);
    }
    (void) munlock(&entropy_data->random_byte, sizeof (char));
    free(entropy_data);
}

static int move_bytes(struct entropy * entropy_data)
{
    size_t n;

    while (entropy_data->bytes_read < entropy_data->keysize) {
        n = read(entropy_data->random_fd, &entropy_data->random_byte,
                 sizeof (char));
        if (sizeof (char) != n) {
            if (EAGAIN == errno) {
                break;
            }
            error("read failed: %s", strerror(errno));
            return DC_NOTOK;
        }
        n = write(entropy_data->fifo_fd, &entropy_data->random_byte,
                  sizeof (char));
        if (sizeof (char) != n) {
            error("write failed: %s", strerror(errno));
            return DC_NOTOK;
        }
        entropy_data->random_byte = 0;
        entropy_data->bytes_read++;
    }
    return DC_OK;
}

static int set_keysize(struct entropy * entropy_data,
                       struct question * question) {
    const char * keysize_string;

    keysize_string = question_get_variable(question, "KEYSIZE");
    if (NULL == keysize_string) {
        entropy_data->keysize = DEFAULT_KEYSIZE;
        return DC_OK;
    }
    entropy_data->keysize = strtol(keysize_string, NULL, 10);
    if (0 >= entropy_data->keysize || LONG_MAX == entropy_data->keysize) {
        error("keysize out of range");
        return DC_NOTOK;
    }
    return DC_OK;
}

static struct entropy * init_entropy(struct frontend * fe,
                                     struct question * question)
{
    struct entropy * entropy_data;

    if (NULL == (entropy_data = malloc(sizeof (struct entropy)))) {
        error("malloc failed.");
        return NULL;
    }
    memset(entropy_data, 0, sizeof (struct entropy));
    entropy_data->fe = fe;
    entropy_data->question = question;
    entropy_data->last_progress = -1;
    if (-1 == mlock(&entropy_data->random_byte, sizeof (char))) {
        error("mlock failed: %s", strerror(errno));
        goto failed;
    }
    entropy_data->success_template = question_get_variable(
        question, "SUCCESS");
    if (NULL == entropy_data->success_template) {
        entropy_data->success_template = "debconf/entropy/success";
    }
    entropy_data->random_fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
    if (-1 == entropy_data->random_fd) {
        error("open random_fd failed: %s", strerror(errno));
        goto failed;
    }
    entropy_data->fifo = question_get_variable(question, "FIFO");
    if (NULL == entropy_data->fifo) {
        entropy_data->fifo = FIFO;
    }
    if (-1 == mkfifo(entropy_data->fifo, 0600)) {
        error("mkfifo failed: %s", strerror(errno));
        goto failed;
    }
    entropy_data->fifo_fd = open(entropy_data->fifo, O_WRONLY);
    if (-1 == entropy_data->fifo_fd) {
        error("open fifo_fd failed: %s", strerror(errno));
        goto failed;
    }
    return entropy_data;

failed:
    destroy_entropy(entropy_data);
    return NULL;

}

/* XXX: Should be exported by text frontend. */
static int getwidth(void)
{
    static int res = 80;
    static int inited = DC_NOTOK;
    int fd;
    struct winsize ws;

    if (inited == DC_NOTOK) {
        inited = DC_OK;
        if (0 < (fd = open("/dev/tty", O_RDONLY))) {
            if (0 == ioctl(fd, TIOCGWINSZ, &ws) && 0 < ws.ws_col)
                res = ws.ws_col;
            close(fd);
        }
    }
    return res;
}

/* XXX: Should be exported by text frontend. */
static void wrap_print(const char *str)
{
    int i;
    int line_count;
    char * lines[500];

    line_count = strwrap(str, getwidth() - 1, lines, 499);

    for (i = 0; i < line_count; i++) {
        printf("%s\n", lines[i]);
        free(lines[i]);
    }
}

static void print_action(struct entropy * entropy_data)
{
    printf("%s: ",
        question_get_text(entropy_data->fe, "debconf/entropy/text/action",
                          "Enter random characters"));
}

static void print_help(struct entropy * entropy_data)
{
    wrap_print(question_get_text(
        entropy_data->fe, "debconf/entropy/text/help",
        "You can help speed up the process by entering random characters on "
        "the keyboard, or just wait until enough key data has been collected. "
        "(which can take a long time)."));
    printf("\n");
}

static void print_success(struct entropy * entropy_data)
{
    wrap_print(question_get_text(
        entropy_data->fe, entropy_data->success_template,
        "Key data has been created successfully."));
    printf("\n");
}

static void print_progress(struct entropy * entropy_data)
{
    int progress;

    progress = (double) (entropy_data->bytes_read) /
               (double) (entropy_data->keysize) * 100.0;
    if (entropy_data->last_progress < progress) {
        entropy_data->last_progress = progress;
        printf("\n---> %d%%\n", progress);
        if (100 == progress) {
            print_success(entropy_data);
        } else {
            print_action(entropy_data);
        }
    }
}

static int handle_input(struct entropy * entropy_data)
{
    int c;

    c = fgetc(stdin);
    if (entropy_data->fe->methods.can_go_back(entropy_data->fe,
                                              entropy_data->question)) {
        if (CHAR_GOBACK == c) {
            entropy_data->is_backing_up = DC_OK;
        } else if (('\n' == c || '\r' == c) && entropy_data->is_backing_up) {
            return DC_GOBACK;
        } else {
            entropy_data->is_backing_up = DC_NOTOK;
        }
    }
    return DC_OK;
}

static void wait_enter(void) {
    int c;

    do {
        c = fgetc(stdin);
    } while ('\n' != c && '\r' != c);
}

static int gather_entropy(struct entropy * entropy_data)
{
    struct termios oldt;
    struct termios newt;
    fd_set fds;

    print_progress(entropy_data);

    tcgetattr(0, &oldt);
    memcpy(&newt, &oldt, sizeof (struct termios));
    cfmakeraw(&newt);
    while (entropy_data->bytes_read < entropy_data->keysize) {
        tcsetattr(0, TCSANOW, &newt);
        FD_ZERO(&fds);
        FD_SET(0, &fds);
        FD_SET(entropy_data->random_fd, &fds);
        if (-1 == select(entropy_data->random_fd + 1/* highest fd */,
                         &fds, NULL /* no write fds */,
                         NULL /* no except fds */, NULL /* no timeout */)) {
            error("select failed: %s", strerror(errno));
            return DC_NOTOK;
        }
        if (FD_ISSET(STDIN_FILENO, &fds)) {
            if (DC_GOBACK == handle_input(entropy_data)) {
                tcsetattr(0, TCSANOW, &oldt);
                return DC_GOBACK;
            }
            fputc('*', stdout);
        }
        tcsetattr(0, TCSANOW, &oldt);
        if (FD_ISSET(entropy_data->random_fd, &fds)) {
            move_bytes(entropy_data);
            print_progress(entropy_data);
        }
    }
    wait_enter();
    return DC_OK;
}

int cdebconf_text_handler_entropy(struct frontend * fe,
                                  struct question * question)
{
    struct entropy * entropy_data;
    int ret;

    if (NULL == (entropy_data = init_entropy(fe, question))) {
        error("init_entropy falied.");
        return DC_NOTOK;
    }
    if (!set_keysize(entropy_data, question)) {
        error("set_keysize failed.");
        ret = DC_NOTOK;
        goto out;
    }
    print_help(entropy_data);
    ret = gather_entropy(entropy_data);

out:
    destroy_entropy(entropy_data);
    return ret;
}