/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/plymouth.c

  • Committer: Teddy Hogeborn
  • Date: 2019-02-10 03:50:20 UTC
  • Revision ID: teddy@recompile.se-20190210035020-nttr1tybgwwixueu
Show debconf note about new TLS key IDs

If mandos-client did not see TLS keys and had to create them, or if
mandos sees GnuTLS version 3.6.6 or later, show an important notice on
package installation about the importance of adding the new key_id
options to clients.conf on the Mandos server.

* debian/control (Package: mandos, Package: mandos-client): Depend on
                                                            debconf.
* debian/mandos-client.lintian-overrides: Override warnings.
* debian/mandos-client.postinst (create_keys): Show notice if new TLS
                                               key files were created.
* debian/mandos-client.templates: New.
* debian/mandos.lintian-overrides: Override warnings.
* debian/mandos.postinst (configure): If GnuTLS 3.6.6 or later is
                                      detected, show an important
                                      notice (once) about the new
                                      key_id option required in
                                      clients.conf.
* debian/mandos.templates: New.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  -*- coding: utf-8 -*- */
 
2
/*
 
3
 * Plymouth - Read a password from Plymouth and output it
 
4
 * 
 
5
 * Copyright © 2010-2018 Teddy Hogeborn
 
6
 * Copyright © 2010-2018 Björn Påhlsson
 
7
 * 
 
8
 * This file is part of Mandos.
 
9
 * 
 
10
 * Mandos is free software: you can redistribute it and/or modify it
 
11
 * under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation, either version 3 of the License, or
 
13
 * (at your option) any later version.
 
14
 * 
 
15
 * Mandos is distributed in the hope that it will be useful, but
 
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
18
 * General Public License for more details.
 
19
 * 
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 
22
 * 
 
23
 * Contact the authors at <mandos@recompile.se>.
 
24
 */
 
25
 
1
26
#define _GNU_SOURCE             /* asprintf(), TEMP_FAILURE_RETRY() */
2
27
#include <signal.h>             /* sig_atomic_t, struct sigaction,
3
28
                                   sigemptyset(), sigaddset(), SIGINT,
12
37
#include <stddef.h>             /* NULL */
13
38
#include <string.h>             /* strchr(), memcmp() */
14
39
#include <stdio.h>              /* asprintf(), perror(), fopen(),
15
 
                                   fscanf() */
 
40
                                   fscanf(), vasprintf(), fprintf(),
 
41
                                   vfprintf() */
16
42
#include <unistd.h>             /* close(), readlink(), read(),
17
43
                                   fork(), setsid(), chdir(), dup2(),
18
44
                                   STDERR_FILENO, execv(), access() */
26
52
#include <error.h>              /* error() */
27
53
#include <errno.h>              /* TEMP_FAILURE_RETRY */
28
54
#include <argz.h>               /* argz_count(), argz_extract() */
 
55
#include <stdarg.h>             /* va_list, va_start(), ... */
29
56
 
30
57
sig_atomic_t interrupted_by_signal = 0;
31
 
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
 
58
 
 
59
/* Used by Ubuntu 11.04 (Natty Narwahl) */
 
60
const char plymouth_old_old_pid[] = "/dev/.initramfs/plymouth.pid";
 
61
/* Used by Ubuntu 11.10 (Oneiric Ocelot) */
 
62
const char plymouth_old_pid[] = "/run/initramfs/plymouth.pid";
 
63
/* Used by Debian 9 (stretch) */
 
64
const char plymouth_pid[] = "/run/plymouth/pid";
 
65
 
32
66
const char plymouth_path[] = "/bin/plymouth";
33
67
const char plymouthd_path[] = "/sbin/plymouthd";
34
68
const char *plymouthd_default_argv[] = {"/sbin/plymouthd",
35
69
                                        "--mode=boot",
36
70
                                        "--attach-to-session",
37
 
                                        "--pid-file="
38
 
                                        "/dev/.initramfs/"
39
 
                                        "plymouth.pid",
40
71
                                        NULL };
41
72
 
42
73
static void termination_handler(__attribute__((unused))int signum){
46
77
  interrupted_by_signal = 1;
47
78
}
48
79
 
 
80
/* Function to use when printing errors */
 
81
__attribute__((format (gnu_printf, 3, 4)))
 
82
void error_plus(int status, int errnum, const char *formatstring,
 
83
                ...){
 
84
  va_list ap;
 
85
  char *text;
 
86
  int ret;
 
87
  
 
88
  va_start(ap, formatstring);
 
89
  ret = vasprintf(&text, formatstring, ap);
 
90
  if(ret == -1){
 
91
    fprintf(stderr, "Mandos plugin %s: ",
 
92
            program_invocation_short_name);
 
93
    vfprintf(stderr, formatstring, ap);
 
94
    fprintf(stderr, ": ");
 
95
    fprintf(stderr, "%s\n", strerror(errnum));
 
96
    error(status, errno, "vasprintf while printing error");
 
97
    return;
 
98
  }
 
99
  fprintf(stderr, "Mandos plugin ");
 
100
  error(status, errnum, "%s", text);
 
101
  free(text);
 
102
}
 
103
 
49
104
/* Create prompt string */
50
105
char *makeprompt(void){
51
106
  int ret = 0;
52
107
  char *prompt;
53
108
  const char *const cryptsource = getenv("cryptsource");
54
109
  const char *const crypttarget = getenv("crypttarget");
55
 
  const char prompt_start[] = "Enter passphrase to unlock the disk";
 
110
  const char prompt_start[] = "Unlocking the disk";
 
111
  const char prompt_end[] = "Enter passphrase";
56
112
  
57
113
  if(cryptsource == NULL){
58
114
    if(crypttarget == NULL){
59
 
      ret = asprintf(&prompt, "%s: ", prompt_start);
 
115
      ret = asprintf(&prompt, "%s\n%s", prompt_start, prompt_end);
60
116
    } else {
61
 
      ret = asprintf(&prompt, "%s (%s): ", prompt_start,
62
 
                     crypttarget);
 
117
      ret = asprintf(&prompt, "%s (%s)\n%s", prompt_start,
 
118
                     crypttarget, prompt_end);
63
119
    }
64
120
  } else {
65
121
    if(crypttarget == NULL){
66
 
      ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
 
122
      ret = asprintf(&prompt, "%s %s\n%s", prompt_start, cryptsource,
 
123
                     prompt_end);
67
124
    } else {
68
 
      ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
69
 
                     cryptsource, crypttarget);
 
125
      ret = asprintf(&prompt, "%s %s (%s)\n%s", prompt_start,
 
126
                     cryptsource, crypttarget, prompt_end);
70
127
    }
71
128
  }
72
129
  if(ret == -1){
83
140
bool become_a_daemon(void){
84
141
  int ret = setuid(geteuid());
85
142
  if(ret == -1){
86
 
    error(0, errno, "setuid");
 
143
    error_plus(0, errno, "setuid");
87
144
  }
88
145
    
89
146
  setsid();
90
147
  ret = chdir("/");
91
148
  if(ret == -1){
92
 
    error(0, errno, "chdir");
 
149
    error_plus(0, errno, "chdir");
93
150
    return false;
94
151
  }
95
152
  ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
96
153
  if(ret == -1){
97
 
    error(0, errno, "dup2");
 
154
    error_plus(0, errno, "dup2");
98
155
    return false;
99
156
  }
100
157
  return true;
101
158
}
102
159
 
 
160
__attribute__((nonnull (2, 3)))
103
161
bool exec_and_wait(pid_t *pid_return, const char *path,
104
 
                   const char **argv, bool interruptable,
 
162
                   const char * const *argv, bool interruptable,
105
163
                   bool daemonize){
106
164
  int status;
107
165
  int ret;
108
166
  pid_t pid;
109
167
  pid = fork();
110
168
  if(pid == -1){
111
 
    error(0, errno, "fork");
 
169
    error_plus(0, errno, "fork");
112
170
    return false;
113
171
  }
114
172
  if(pid == 0){
118
176
        _exit(EX_OSERR);
119
177
      }
120
178
    }
121
 
 
122
 
    char **new_argv = NULL;
123
 
    char *tmp;
 
179
    
 
180
    char **new_argv = malloc(sizeof(const char *));
 
181
    if(new_argv == NULL){
 
182
      error_plus(0, errno, "malloc");
 
183
      _exit(EX_OSERR);
 
184
    }
 
185
    char **tmp;
124
186
    int i = 0;
125
 
    for (; argv[i]!=(char *)NULL; i++){
126
 
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
127
 
      if (tmp == NULL){
128
 
        error(0, errno, "realloc");
 
187
    for (; argv[i] != NULL; i++){
 
188
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 2));
 
189
      if(tmp == NULL){
 
190
        error_plus(0, errno, "realloc");
129
191
        free(new_argv);
130
192
        _exit(EX_OSERR);
131
193
      }
132
 
      new_argv = (char **)tmp;
 
194
      new_argv = tmp;
133
195
      new_argv[i] = strdup(argv[i]);
134
196
    }
135
 
    new_argv[i] = (char *) NULL;
 
197
    new_argv[i] = NULL;
136
198
    
137
199
    execv(path, (char *const *)new_argv);
138
 
    error(0, errno, "execv");
 
200
    error_plus(0, errno, "execv");
139
201
    _exit(EXIT_FAILURE);
140
202
  }
141
203
  if(pid_return != NULL){
150
212
    return false;
151
213
  }
152
214
  if(ret == -1){
153
 
    error(0, errno, "waitpid");
 
215
    error_plus(0, errno, "waitpid");
154
216
    return false;
155
217
  }
156
218
  if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
159
221
  return false;
160
222
}
161
223
 
 
224
__attribute__((nonnull))
162
225
int is_plymouth(const struct dirent *proc_entry){
163
226
  int ret;
164
227
  {
165
 
    uintmax_t maxvalue;
 
228
    uintmax_t proc_id;
166
229
    char *tmp;
167
230
    errno = 0;
168
 
    maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
 
231
    proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
169
232
 
170
233
    if(errno != 0 or *tmp != '\0'
171
 
       or maxvalue != (uintmax_t)((pid_t)maxvalue)){
 
234
       or proc_id != (uintmax_t)((pid_t)proc_id)){
172
235
      return 0;
173
236
    }
174
237
  }
175
 
  char exe_target[sizeof(plymouth_path)];
 
238
  char exe_target[sizeof(plymouthd_path)];
176
239
  char *exe_link;
177
240
  ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
178
241
  if(ret == -1){
179
 
    error(0, errno, "asprintf");
 
242
    error_plus(0, errno, "asprintf");
180
243
    return 0;
181
244
  }
182
245
  
185
248
  if(ret == -1){
186
249
    free(exe_link);
187
250
    if(errno != ENOENT){
188
 
      error(0, errno, "lstat");
 
251
      error_plus(0, errno, "lstat");
189
252
    }
190
253
    return 0;
191
254
  }
199
262
  
200
263
  ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
201
264
  free(exe_link);
202
 
  if((sret != (ssize_t)sizeof(plymouth_path)-1) or
203
 
      (memcmp(plymouth_path, exe_target,
204
 
              sizeof(plymouth_path)-1) != 0)){
 
265
  if((sret != (ssize_t)sizeof(plymouthd_path)-1) or
 
266
      (memcmp(plymouthd_path, exe_target,
 
267
              sizeof(plymouthd_path)-1) != 0)){
205
268
    return 0;
206
269
  }
207
270
  return 1;
209
272
 
210
273
pid_t get_pid(void){
211
274
  int ret;
 
275
  uintmax_t proc_id = 0;
212
276
  FILE *pidfile = fopen(plymouth_pid, "r");
213
 
  uintmax_t maxvalue = 0;
 
277
  /* Try the new pid file location */
214
278
  if(pidfile != NULL){
215
 
    ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
 
279
    ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
216
280
    if(ret != 1){
217
 
      maxvalue = 0;
 
281
      proc_id = 0;
218
282
    }
219
283
    fclose(pidfile);
220
284
  }
221
 
  if(maxvalue == 0){
222
 
    struct dirent **direntries;
 
285
  /* Try the old pid file location */
 
286
  if(proc_id == 0){
 
287
    pidfile = fopen(plymouth_old_pid, "r");
 
288
    if(pidfile != NULL){
 
289
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
 
290
      if(ret != 1){
 
291
        proc_id = 0;
 
292
      }
 
293
      fclose(pidfile);
 
294
    }
 
295
  }
 
296
  /* Try the old old pid file location */
 
297
  if(proc_id == 0){
 
298
    pidfile = fopen(plymouth_old_old_pid, "r");
 
299
    if(pidfile != NULL){
 
300
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
 
301
      if(ret != 1){
 
302
        proc_id = 0;
 
303
      }
 
304
      fclose(pidfile);
 
305
    }
 
306
  }
 
307
  /* Look for a plymouth process */
 
308
  if(proc_id == 0){
 
309
    struct dirent **direntries = NULL;
223
310
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
224
 
    sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
 
311
    if(ret == -1){
 
312
      error_plus(0, errno, "scandir");
 
313
    }
 
314
    if(ret > 0){
 
315
      for(int i = ret-1; i >= 0; i--){
 
316
        if(proc_id == 0){
 
317
          ret = sscanf(direntries[i]->d_name, "%" SCNuMAX, &proc_id);
 
318
          if(ret < 0){
 
319
            error_plus(0, errno, "sscanf");
 
320
          }
 
321
        }
 
322
        free(direntries[i]);
 
323
      }
 
324
    }
 
325
    /* scandir might preallocate for this variable (man page unclear).
 
326
       even if ret == 0, therefore we need to free it. */
 
327
    free(direntries);
225
328
  }
226
329
  pid_t pid;
227
 
  pid = (pid_t)maxvalue;
228
 
  if((uintmax_t)pid == maxvalue){
 
330
  pid = (pid_t)proc_id;
 
331
  if((uintmax_t)pid == proc_id){
229
332
    return pid;
230
333
  }
231
334
  
232
335
  return 0;
233
336
}
234
337
 
235
 
const char **getargv(pid_t pid){
 
338
char **getargv(pid_t pid){
236
339
  int cl_fd;
237
340
  char *cmdline_filename;
238
341
  ssize_t sret;
241
344
  ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
242
345
                 (uintmax_t)pid);
243
346
  if(ret == -1){
244
 
    error(0, errno, "asprintf");
 
347
    error_plus(0, errno, "asprintf");
245
348
    return NULL;
246
349
  }
247
350
  
249
352
  cl_fd = open(cmdline_filename, O_RDONLY);
250
353
  free(cmdline_filename);
251
354
  if(cl_fd == -1){
252
 
    error(0, errno, "open");
 
355
    error_plus(0, errno, "open");
253
356
    return NULL;
254
357
  }
255
358
  
263
366
    if(cmdline_len + blocksize > cmdline_allocated){
264
367
      tmp = realloc(cmdline, cmdline_allocated + blocksize);
265
368
      if(tmp == NULL){
266
 
        error(0, errno, "realloc");
 
369
        error_plus(0, errno, "realloc");
267
370
        free(cmdline);
268
371
        close(cl_fd);
269
372
        return NULL;
276
379
    sret = read(cl_fd, cmdline + cmdline_len,
277
380
                cmdline_allocated - cmdline_len);
278
381
    if(sret == -1){
279
 
      error(0, errno, "read");
 
382
      error_plus(0, errno, "read");
280
383
      free(cmdline);
281
384
      close(cl_fd);
282
385
      return NULL;
285
388
  } while(sret != 0);
286
389
  ret = close(cl_fd);
287
390
  if(ret == -1){
288
 
    error(0, errno, "close");
 
391
    error_plus(0, errno, "close");
289
392
    free(cmdline);
290
393
    return NULL;
291
394
  }
294
397
  char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
295
398
                       * sizeof(char *)); /* Get number of args */
296
399
  if(argv == NULL){
297
 
    error(0, errno, "argv = malloc()");
 
400
    error_plus(0, errno, "argv = malloc()");
298
401
    free(cmdline);
299
402
    return NULL;
300
403
  }
301
404
  argz_extract(cmdline, cmdline_len, argv); /* Create argv */
302
 
  return (const char **)argv;
 
405
  return argv;
303
406
}
304
407
 
305
408
int main(__attribute__((unused))int argc,
327
430
        *sig != 0; sig++){
328
431
      ret = sigaddset(&new_action.sa_mask, *sig);
329
432
      if(ret == -1){
330
 
        error(EX_OSERR, errno, "sigaddset");
 
433
        error_plus(EX_OSERR, errno, "sigaddset");
331
434
      }
332
435
      ret = sigaction(*sig, NULL, &old_action);
333
436
      if(ret == -1){
334
 
        error(EX_OSERR, errno, "sigaction");
 
437
        error_plus(EX_OSERR, errno, "sigaction");
335
438
      }
336
439
      if(old_action.sa_handler != SIG_IGN){
337
440
        ret = sigaction(*sig, &new_action, NULL);
338
441
        if(ret == -1){
339
 
          error(EX_OSERR, errno, "sigaction");
 
442
          error_plus(EX_OSERR, errno, "sigaction");
340
443
        }
341
444
      }
342
445
    }
361
464
  ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
362
465
  free(prompt);
363
466
  if(ret == -1){
364
 
    error(EX_OSERR, errno, "asprintf");
 
467
    error_plus(EX_OSERR, errno, "asprintf");
365
468
  }
366
469
  
367
470
  /* plymouth ask-for-password --prompt="$prompt" */
380
483
  }
381
484
  kill_and_wait(plymouth_command_pid);
382
485
  
383
 
  const char **plymouthd_argv;
 
486
  char **plymouthd_argv = NULL;
384
487
  pid_t pid = get_pid();
385
488
  if(pid == 0){
386
 
    error(0, 0, "plymouthd pid not found");
387
 
    plymouthd_argv = plymouthd_default_argv;
 
489
    error_plus(0, 0, "plymouthd pid not found");
388
490
  } else {
389
491
    plymouthd_argv = getargv(pid);
390
492
  }
393
495
                       { plymouth_path, "quit", NULL },
394
496
                       false, false);
395
497
  if(not bret){
 
498
    if(plymouthd_argv != NULL){
 
499
      free(*plymouthd_argv);
 
500
      free(plymouthd_argv);
 
501
    }
396
502
    exit(EXIT_FAILURE);
397
503
  }
398
 
  bret = exec_and_wait(NULL, plymouthd_path, plymouthd_argv,
 
504
  bret = exec_and_wait(NULL, plymouthd_path,
 
505
                       (plymouthd_argv != NULL)
 
506
                       ? (const char * const *)plymouthd_argv
 
507
                       : plymouthd_default_argv,
399
508
                       false, true);
 
509
  if(plymouthd_argv != NULL){
 
510
    free(*plymouthd_argv);
 
511
    free(plymouthd_argv);
 
512
  }
400
513
  if(not bret){
401
514
    exit(EXIT_FAILURE);
402
515
  }