File: confmodule.c

package info (click to toggle)
cdebconf 0.227
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,552 kB
  • sloc: ansic: 16,568; sh: 549; makefile: 456; sql: 52; perl: 13
file content (382 lines) | stat: -rw-r--r-- 10,372 bytes parent folder | download | duplicates (3)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "confmodule.h"
#include "commands.h"
#include "frontend.h"
#include "database.h"
#include "question.h"
#include "template.h"
#include "strutl.h"
#include "debconfclient.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>

#include <debian-installer.h>

#ifdef SELINUX_ENABLED
#include <selinux/selinux.h>
#else
#define setexecfilecon(filename, fallback) do {} while(0)
#endif

static commands_t commands[] = {
#include "commands-list.h"
    { 0, 0 }
};

volatile sig_atomic_t signal_received = 0;
volatile sig_atomic_t sigchld_status = 0;

/* private functions */
/*
 * @brief helper function to process incoming commands
 * @param struct confmodule *mod - confmodule object
 * @param char *in - input command
 * @param char *out - reply buffer
 * @param size_t outsize - reply buffer length
 * @return int - DC_OK, DC_NOTOK, DC_NOTIMPL
 */
static char *_confmodule_process(struct confmodule *mod, char *in)
{
    int i;
    char *retval = NULL;
    char *argv[2] = { "", "" };

    char *inp = strstrip(in);
	
    INFO(INFO_DEBUG, "--> %s", inp);

    if (*inp == '#') return NULL;

    if (mod->frontend->capability & DCF_CAPB_ESCAPE)
        strunescape(inp, inp, strlen(inp)+1, STRESCAPE_CAPB);

    strcmdsplit(inp, argv, DIM(argv));

    for (i = 0; commands[i].command != 0; i++)
    {
        if (strcasecmp(argv[0], commands[i].command) == 0) {
            retval = (*commands[i].handler)(mod, argv[1]);
            break;
        }
    }

    if (retval == NULL)
        fprintf(stderr, "E: Unimplemented function\n");
		
    return retval;
}

/*
 * @brief mark a file descriptor close-on-exec
 * @param int fd - file descriptor
 * @return int - 0 on success, -1 with errno set on error
 */
static int _confmodule_cloexec(int fd)
{
    int flags = fcntl (fd, F_GETFD);
    if (flags < 0)
        return flags;
    if (fcntl (fd, F_SETFD, flags | FD_CLOEXEC) < 0)
        return -1;
    return 0;
}

/* public functions */
static int confmodule_communicate(struct confmodule *mod)
{
    char buf[1023];
    char *in;
    size_t insize = 1024;
    char *out;
    //size_t outsize = 4096;
    int ret = 0;

    in = malloc(insize);
    if (!in)
        return DC_NOTOK;
    memset(in, 0, insize);

    //out = malloc(outsize);
    //if (!out)
    //return DC_NOTOK;
    //memset(out, 0, outsize);

    while (1) {
        buf[0] = 0;
        in[0] = 0;
        while (strchr(buf, '\n') == NULL) {
            if (signal_received) {
                free(in);
                return DC_OK;
            }

            ret = read(mod->infd, buf, sizeof(buf) - 1);
            if (ret < 0) {
                if (errno == EINTR)
                    continue;
                free(in);
                return DC_NOTOK;
            }
            if (ret == 0) {
                free(in);
                return DC_OK;
            }
            buf[ret] = 0;
            if (strlen(in) + ret + 1 > insize) {
                insize += sizeof(buf);
                in = realloc(in, insize);
            }
            strcat(in, buf);
        }

        if (signal_received) {
            free(in);
            return DC_OK;
        }

        out = _confmodule_process(mod, in);
        if (out == NULL) {
            continue;
        }
        if (out[0] == 0) /* STOP called */
        {
            ret = DC_OK;
            break;
        }
        INFO(INFO_DEBUG, "<-- %s", out);
        write(mod->outfd, out, strlen(out));
        write(mod->outfd, "\n", 1);
        free(out);
    }
    free(in);
    return ret;
}

static char *confmodule_process_command(struct confmodule *mod, char *cmd)
{
    char *out;

    out = _confmodule_process(mod, cmd);
    if (out == NULL) {
        asprintf(&out, "%u Not implemented", DC_NOTOK);
    }
    INFO(INFO_DEBUG, "<-- %s", out);

    return out;
}

static int confmodule_shutdown(struct confmodule *mod)
{
    int status;

    while (waitpid(mod->pid, &status, WNOHANG) > 0)
        sigchld_status = status;

    mod->exitcode = di_exec_mangle_status(sigchld_status);

    return mod->exitcode;
}

static inline void check_fd(int fd, int newfd, bool old[])
{
    if (fd <= 2)
        old[fd] = false;
    dup2(fd, newfd);
    close(fd);
}

static pid_t confmodule_run(struct confmodule *mod, int argc, char **argv)
{
    pid_t pid;
    int i;
    char **args;
    bool old[3] = { true, true, true };
    int config[5]; /* 0=read/to, 1=write/to, 2=read/from, 3=write/from, 4=null */
    pipe(&config[0]);
    pipe(&config[2]);
    switch ((pid = fork()))
    {
        case -1:
            mod->frontend->methods.shutdown(mod->frontend);
            DIE("Cannot execute client config script");
            break;
        case 0:
            /* 50=read/to, 51=write/to, 52=read/from, 53=write/from, 54=null */
            config[4] = open("/dev/null", O_RDWR);
            for (i = 0; i < 5; i++)
                check_fd(config[i], 50 + i, old);
            for (i = 0; i <= 2; i++)
                dup2(old[i] ? i : 54, DEBCONF_OLD_FD_BASE + i);
            dup2(50, 0); dup2(53, 1); dup2(53, 3);
            for (i = 0; i < 5; i++)
                close(50 + i);

            args = (char **)malloc(sizeof(char *) * argc);
            for (i = 1; i < argc; i++)
                args[i-1] = argv[i];
            args[argc-1] = NULL;
            setexecfilecon(argv[1],"dpkg_script_t");
            if (execv(argv[1], args) != 0)
                perror("execv");
            /* should never reach here, otherwise execv failed :( */
            exit (127);
        default:
            close(config[0]); close(config[3]);
            mod->infd = config[2];
            mod->outfd = config[1];
            _confmodule_cloexec(mod->infd);
            _confmodule_cloexec(mod->outfd);
    }
    mod->pid = pid;

    return pid;
}

static int confmodule_update_seen_questions(struct confmodule *mod, enum seen_action action)
{
    struct question *q;
    struct question *qlast = NULL;
    int i, narg;

    switch (action)
    {
        case STACK_SEEN_ADD:
            if (mod->seen_questions == NULL)
                narg = 0;
            else
                narg = mod->number_seen_questions;

            i = narg;
            for (q = mod->frontend->questions; q != NULL; q = q->next)
                narg++;
            if (narg == 0)
                return DC_OK;

            mod->seen_questions = (char **) realloc(mod->seen_questions, sizeof(char *) * narg);
            for (q = mod->frontend->questions; q != NULL; q = q->next)
            {
                *(mod->seen_questions+i) = strdup(q->tag);
                i++;
            }
            mod->number_seen_questions = i;
            break;
        case STACK_SEEN_REMOVE:
            if (mod->seen_questions == NULL)
                return DC_OK;

            for (q = mod->frontend->questions; q != NULL; q = q->next)
                qlast = q;

            for (q = qlast; q != NULL; q = q->prev)
            {
                if (strcmp(*(mod->seen_questions + mod->number_seen_questions - 1), q->tag) != 0)
                    return DC_OK;
                DELETE(*(mod->seen_questions + mod->number_seen_questions - 1));
                (mod->number_seen_questions) --;
                if (mod->number_seen_questions == 0)
                {
                    DELETE(mod->seen_questions);
                    break;
                }
            }
            break;
        case STACK_SEEN_SAVE:
            if (mod->seen_questions == NULL)
                return DC_OK;

            narg = mod->number_seen_questions;
            for (i = 0; i < narg; i++)
            {
                q = mod->questions->methods.get(mod->questions, *(mod->seen_questions+i));
                if (q == NULL)
                    return DC_NOTOK;
#ifndef DI_UDEB
                question_set_flag(q, DC_QFLAG_SEEN);
#endif
                DELETE(*(mod->seen_questions+i));
            }
            DELETE(mod->seen_questions);
            mod->number_seen_questions = 0;
            break;
        default:
            /* should never happen */
            DIE("Mismatch argument in confmodule_update_seen_questions");
    }

    return DC_OK;
}

static int confmodule_save(struct confmodule *mod)
{
    int ret = DC_OK;

    if (!load_all_translations())
    {
        /* This isn't entirely accurate; really it should be done in
         * rfc822db's implementation of mod->templates->methods.save().
         * However, that doesn't have convenient access to the questions
         * database, so we do it here instead for the time being.
         */
        struct question *q = mod->questions->methods.get(
            mod->questions, "debconf/translations-dropped");
        if (q != NULL)
        {
            if (strcmp(question_getvalue(q, ""), "true") != 0)
            {
                question_setvalue(q, "true");
                mod->questions->methods.set(mod->questions, q);
            }
            question_deref(q);
        }
    }

    ret |= mod->update_seen_questions(mod, STACK_SEEN_SAVE);
    if (mod->questions)
        ret |= mod->questions->methods.save(mod->questions);
    if (mod->templates)
        ret |= mod->templates->methods.save(mod->templates);
    return ret != DC_OK ? DC_NOTOK : DC_OK;
}

struct confmodule *confmodule_new(struct configuration *config,
        struct template_db *templates, struct question_db *questions, 
        struct frontend *frontend)
{
    struct confmodule *mod = NEW(struct confmodule);
    memset(mod, 0, sizeof(struct confmodule));

    mod->exitcode = 126;
    mod->config = config;
    mod->templates = templates;
    mod->questions = questions;
    mod->frontend = frontend;
    mod->seen_questions = NULL;
    mod->backed_up = 0;
    mod->run = confmodule_run;
    mod->communicate = confmodule_communicate;
    mod->process_command = confmodule_process_command;
    mod->shutdown = confmodule_shutdown;
    mod->update_seen_questions = confmodule_update_seen_questions;
    mod->save = confmodule_save;

    /* TODO: I wish we don't need gross hacks like this.... */
    setenv("DEBIAN_HAS_FRONTEND", "1", 1);

    return mod;
}

void confmodule_delete(struct confmodule *mod)
{
    DELETE(mod);
}

/* vim: expandtab sw=4
*/