/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: 2023-02-11 06:58:15 UTC
  • Revision ID: teddy@recompile.se-20230211065815-jtffi61tbdrgr875
Suppress warnings

Suppress warnings about mlock()ing uninitialized memory and reading an
ievent struct.

* dracut-module/password-agent.c (send_password_to_socket): Suppress
  "-Wmaybe-uninitialized" when mlock()ing password send buffer.
  (test_read_inotify_event_IN_DELETE_badname): When reading from
    a struct ievent, suppress "-Wstringop-overread".

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Plymouth - Read a password from Plymouth and output it
4
4
 * 
5
 
 * Copyright © 2010-2014 Teddy Hogeborn
6
 
 * Copyright © 2010-2014 Björn Påhlsson
7
 
 * 
8
 
 * This program is free software: you can redistribute it and/or
9
 
 * modify it under the terms of the GNU General Public License as
10
 
 * published by the Free Software Foundation, either version 3 of the
11
 
 * License, or (at your option) any later version.
12
 
 * 
13
 
 * This program is distributed in the hope that it will be useful, but
 
5
 * Copyright © 2010-2022 Teddy Hogeborn
 
6
 * Copyright © 2010-2022 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
14
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
15
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
18
 * General Public License for more details.
17
19
 * 
18
20
 * You should have received a copy of the GNU General Public License
19
 
 * along with this program.  If not, see
20
 
 * <http://www.gnu.org/licenses/>.
 
21
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
21
22
 * 
22
23
 * Contact the authors at <mandos@recompile.se>.
23
24
 */
24
25
 
25
 
#define _GNU_SOURCE             /* asprintf(), TEMP_FAILURE_RETRY() */
26
 
#include <signal.h>             /* sig_atomic_t, struct sigaction,
27
 
                                   sigemptyset(), sigaddset(), SIGINT,
28
 
                                   SIGHUP, SIGTERM, sigaction(),
29
 
                                   kill(), SIG_IGN */
 
26
#define _GNU_SOURCE             /* program_invocation_short_name,
 
27
                                   vasprintf(), asprintf(),
 
28
                                   TEMP_FAILURE_RETRY() */
 
29
#include <sys/types.h>          /* sig_atomic_t, pid_t, setuid(),
 
30
                                   geteuid(), setsid() */
 
31
#include <argp.h>               /* argp_program_version,
 
32
                                   argp_program_bug_address,
 
33
                                   struct argp_option,
 
34
                                   struct argp_state,
 
35
                                   ARGP_ERR_UNKNOWN, struct argp,
 
36
                                   argp_parse(), ARGP_IN_ORDER */
 
37
#include <stddef.h>             /* NULL, size_t */
30
38
#include <stdbool.h>            /* bool, false, true */
 
39
#include <stdio.h>              /* FILE, fprintf(), vfprintf(),
 
40
                                   vasprintf(), stderr, asprintf(),
 
41
                                   fopen(), fscanf(), fclose(),
 
42
                                   sscanf() */
 
43
#include <stdarg.h>             /* va_list, va_start(), vfprintf() */
 
44
#include <errno.h>              /* program_invocation_short_name,
 
45
                                   errno, ENOMEM, EINTR, ENOENT,
 
46
                                   error_t, EINVAL */
 
47
#include <string.h>             /* strerror(), strdup(), memcmp() */
 
48
#include <error.h>              /* error() */
 
49
#include <stdlib.h>             /* free(), getenv(), malloc(),
 
50
                                   reallocarray(), realloc(),
 
51
                                   EXIT_FAILURE, EXIT_SUCCESS */
 
52
#include <unistd.h>             /* TEMP_FAILURE_RETRY(), setuid(),
 
53
                                   geteuid(), setsid(), chdir(),
 
54
                                   dup2(), STDERR_FILENO,
 
55
                                   STDOUT_FILENO, fork(), _exit(),
 
56
                                   execv(), ssize_t, readlink(),
 
57
                                   close(), read(), access(), X_OK */
 
58
#include <signal.h>             /* kill(), SIGTERM, struct sigaction,
 
59
                                   sigemptyset(), SIGINT, SIGHUP,
 
60
                                   sigaddset(), SIG_IGN */
 
61
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
 
62
                                   WEXITSTATUS(), WIFSIGNALED(),
 
63
                                   WTERMSIG() */
 
64
#include <iso646.h>             /* not, and, or */
 
65
#include <sysexits.h>           /* EX_OSERR, EX_USAGE,
 
66
                                   EX_UNAVAILABLE */
 
67
#include <stdint.h>             /* SIZE_MAX */
 
68
#include <dirent.h>             /* struct dirent, scandir(),
 
69
                                   alphasort() */
 
70
#include <inttypes.h>           /* uintmax_t, strtoumax(), SCNuMAX,
 
71
                                   PRIuMAX */
 
72
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK() */
31
73
#include <fcntl.h>              /* open(), O_RDONLY */
32
 
#include <iso646.h>             /* and, or, not*/
33
 
#include <sys/types.h>          /* size_t, ssize_t, pid_t, struct
34
 
                                   dirent, waitpid() */
35
 
#include <sys/wait.h>           /* waitpid() */
36
 
#include <stddef.h>             /* NULL */
37
 
#include <string.h>             /* strchr(), memcmp() */
38
 
#include <stdio.h>              /* asprintf(), perror(), fopen(),
39
 
                                   fscanf(), vasprintf(), fprintf(),
40
 
                                   vfprintf() */
41
 
#include <unistd.h>             /* close(), readlink(), read(),
42
 
                                   fork(), setsid(), chdir(), dup2(),
43
 
                                   STDERR_FILENO, execv(), access() */
44
 
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
45
 
                                   EXIT_SUCCESS, malloc(), _exit(),
46
 
                                   getenv() */
47
 
#include <dirent.h>             /* scandir(), alphasort() */
48
 
#include <inttypes.h>           /* intmax_t, strtoumax(), SCNuMAX */
49
 
#include <sys/stat.h>           /* struct stat, lstat() */
50
 
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
51
 
#include <error.h>              /* error() */
52
 
#include <errno.h>              /* TEMP_FAILURE_RETRY */
53
74
#include <argz.h>               /* argz_count(), argz_extract() */
54
 
#include <stdarg.h>             /* va_list, va_start(), ... */
55
75
 
56
76
sig_atomic_t interrupted_by_signal = 0;
 
77
const char *argp_program_version = "plymouth " VERSION;
 
78
const char *argp_program_bug_address = "<mandos@recompile.se>";
57
79
 
58
80
/* Used by Ubuntu 11.04 (Natty Narwahl) */
59
 
const char plymouth_old_pid[] = "/dev/.initramfs/plymouth.pid";
 
81
const char plymouth_old_old_pid[] = "/dev/.initramfs/plymouth.pid";
60
82
/* Used by Ubuntu 11.10 (Oneiric Ocelot) */
61
 
const char plymouth_pid[] = "/run/initramfs/plymouth.pid";
 
83
const char plymouth_old_pid[] = "/run/initramfs/plymouth.pid";
 
84
/* Used by Debian 9 (stretch) */
 
85
const char plymouth_pid[] = "/run/plymouth/pid";
62
86
 
63
87
const char plymouth_path[] = "/bin/plymouth";
64
88
const char plymouthd_path[] = "/sbin/plymouthd";
66
90
                                        "--mode=boot",
67
91
                                        "--attach-to-session",
68
92
                                        NULL };
 
93
bool debug = false;
69
94
 
70
95
static void termination_handler(__attribute__((unused))int signum){
71
96
  if(interrupted_by_signal){
74
99
  interrupted_by_signal = 1;
75
100
}
76
101
 
 
102
__attribute__((format (gnu_printf, 2, 3), nonnull))
 
103
int fprintf_plus(FILE *stream, const char *format, ...){
 
104
  va_list ap;
 
105
  va_start (ap, format);
 
106
  fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name);
 
107
  return vfprintf(stream, format, ap);
 
108
}
 
109
 
77
110
/* Function to use when printing errors */
78
111
__attribute__((format (gnu_printf, 3, 4)))
79
112
void error_plus(int status, int errnum, const char *formatstring,
156
189
 
157
190
__attribute__((nonnull (2, 3)))
158
191
bool exec_and_wait(pid_t *pid_return, const char *path,
159
 
                   const char * const *argv, bool interruptable,
 
192
                   const char * const * const argv, bool interruptable,
160
193
                   bool daemonize){
161
194
  int status;
162
195
  int ret;
163
196
  pid_t pid;
 
197
  if(debug){
 
198
    for(const char * const *arg = argv; *arg != NULL; arg++){
 
199
      fprintf_plus(stderr, "exec_and_wait arg: %s\n", *arg);
 
200
    }
 
201
    fprintf_plus(stderr, "exec_and_wait end of args\n");
 
202
  }
 
203
 
164
204
  pid = fork();
165
205
  if(pid == -1){
166
206
    error_plus(0, errno, "fork");
174
214
      }
175
215
    }
176
216
    
177
 
    char **new_argv = NULL;
 
217
    char **new_argv = malloc(sizeof(const char *));
 
218
    if(new_argv == NULL){
 
219
      error_plus(0, errno, "malloc");
 
220
      _exit(EX_OSERR);
 
221
    }
178
222
    char **tmp;
179
223
    int i = 0;
180
 
    for (; argv[i]!=NULL; i++){
181
 
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
 
224
    for (; argv[i] != NULL; i++){
 
225
#if defined(__GLIBC_PREREQ) and __GLIBC_PREREQ(2, 26)
 
226
      tmp = reallocarray(new_argv, ((size_t)i + 2),
 
227
                         sizeof(const char *));
 
228
#else
 
229
      if(((size_t)i + 2) > (SIZE_MAX / sizeof(const char *))){
 
230
        /* overflow */
 
231
        tmp = NULL;
 
232
        errno = ENOMEM;
 
233
      } else {
 
234
        tmp = realloc(new_argv, ((size_t)i + 2) * sizeof(const char *));
 
235
      }
 
236
#endif
182
237
      if(tmp == NULL){
183
 
        error_plus(0, errno, "realloc");
 
238
        error_plus(0, errno, "reallocarray");
184
239
        free(new_argv);
185
240
        _exit(EX_OSERR);
186
241
      }
202
257
          and ((not interrupted_by_signal)
203
258
               or (not interruptable)));
204
259
  if(interrupted_by_signal and interruptable){
 
260
    if(debug){
 
261
      fprintf_plus(stderr, "Interrupted by signal\n");
 
262
    }
205
263
    return false;
206
264
  }
207
265
  if(ret == -1){
208
266
    error_plus(0, errno, "waitpid");
209
267
    return false;
210
268
  }
 
269
  if(debug){
 
270
    if(WIFEXITED(status)){
 
271
      fprintf_plus(stderr, "exec_and_wait exited: %d\n",
 
272
                   WEXITSTATUS(status));
 
273
    } else if(WIFSIGNALED(status)) {
 
274
      fprintf_plus(stderr, "exec_and_wait signaled: %d\n",
 
275
                   WTERMSIG(status));
 
276
    }
 
277
  }
211
278
  if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
212
279
    return true;
213
280
  }
277
344
  }
278
345
  /* Try the old pid file location */
279
346
  if(proc_id == 0){
280
 
    pidfile = fopen(plymouth_pid, "r");
 
347
    pidfile = fopen(plymouth_old_pid, "r");
 
348
    if(pidfile != NULL){
 
349
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
 
350
      if(ret != 1){
 
351
        proc_id = 0;
 
352
      }
 
353
      fclose(pidfile);
 
354
    }
 
355
  }
 
356
  /* Try the old old pid file location */
 
357
  if(proc_id == 0){
 
358
    pidfile = fopen(plymouth_old_old_pid, "r");
281
359
    if(pidfile != NULL){
282
360
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
283
361
      if(ret != 1){
294
372
      error_plus(0, errno, "scandir");
295
373
    }
296
374
    if(ret > 0){
297
 
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &proc_id);
298
 
      if(ret < 0){
299
 
        error_plus(0, errno, "sscanf");
 
375
      for(int i = ret-1; i >= 0; i--){
 
376
        if(proc_id == 0){
 
377
          ret = sscanf(direntries[i]->d_name, "%" SCNuMAX, &proc_id);
 
378
          if(ret < 0){
 
379
            error_plus(0, errno, "sscanf");
 
380
          }
 
381
        }
 
382
        free(direntries[i]);
300
383
      }
301
384
    }
302
385
    /* scandir might preallocate for this variable (man page unclear).
312
395
  return 0;
313
396
}
314
397
 
315
 
const char * const * getargv(pid_t pid){
 
398
char **getargv(pid_t pid){
316
399
  int cl_fd;
317
400
  char *cmdline_filename;
318
401
  ssize_t sret;
379
462
    return NULL;
380
463
  }
381
464
  argz_extract(cmdline, cmdline_len, argv); /* Create argv */
382
 
  return (const char * const *)argv;
 
465
  return argv;
383
466
}
384
467
 
385
468
int main(__attribute__((unused))int argc,
386
469
         __attribute__((unused))char **argv){
387
 
  char *prompt;
 
470
  char *prompt = NULL;
388
471
  char *prompt_arg;
389
472
  pid_t plymouth_command_pid;
390
473
  int ret;
391
474
  bool bret;
392
475
 
 
476
  {
 
477
    struct argp_option options[] = {
 
478
      { .name = "prompt", .key = 128, .arg = "PROMPT",
 
479
        .doc = "The prompt to show" },
 
480
      { .name = "debug", .key = 129,
 
481
        .doc = "Debug mode" },
 
482
      { .name = NULL }
 
483
    };
 
484
    
 
485
    __attribute__((nonnull(3)))
 
486
    error_t parse_opt (int key, char *arg, __attribute__((unused))
 
487
                       struct argp_state *state){
 
488
      errno = 0;
 
489
      switch (key){
 
490
      case 128:                 /* --prompt */
 
491
        prompt = arg;
 
492
        if(debug){
 
493
          fprintf_plus(stderr, "Custom prompt \"%s\"\n", prompt);
 
494
        }
 
495
        break;
 
496
      case 129:                 /* --debug */
 
497
        debug = true;
 
498
        break;
 
499
      default:
 
500
        return ARGP_ERR_UNKNOWN;
 
501
      }
 
502
      return errno;
 
503
    }
 
504
    
 
505
    struct argp argp = { .options = options, .parser = parse_opt,
 
506
                         .args_doc = "",
 
507
                         .doc = "Mandos plymouth -- Read and"
 
508
                         " output a password" };
 
509
    ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, NULL);
 
510
    switch(ret){
 
511
    case 0:
 
512
      break;
 
513
    case ENOMEM:
 
514
    default:
 
515
      errno = ret;
 
516
      error_plus(0, errno, "argp_parse");
 
517
      return EX_OSERR;
 
518
    case EINVAL:
 
519
      error_plus(0, errno, "argp_parse");
 
520
      return EX_USAGE;
 
521
    }
 
522
  }
 
523
  
393
524
  /* test -x /bin/plymouth */
394
525
  ret = access(plymouth_path, X_OK);
395
526
  if(ret == -1){
396
527
    /* Plymouth is probably not installed.  Don't print an error
397
528
       message, just exit. */
 
529
    if(debug){
 
530
      fprintf_plus(stderr, "Plymouth (%s) not found\n",
 
531
                   plymouth_path);
 
532
    }
398
533
    exit(EX_UNAVAILABLE);
399
534
  }
400
535
  
434
569
    }
435
570
    /* Plymouth is probably not running.  Don't print an error
436
571
       message, just exit. */
 
572
    if(debug){
 
573
      fprintf_plus(stderr, "Plymouth not running\n");
 
574
    }
437
575
    exit(EX_UNAVAILABLE);
438
576
  }
439
577
  
440
 
  prompt = makeprompt();
441
 
  ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
442
 
  free(prompt);
 
578
  if(prompt != NULL){
 
579
    ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
 
580
  } else {
 
581
    char *made_prompt = makeprompt();
 
582
    ret = asprintf(&prompt_arg, "--prompt=%s", made_prompt);
 
583
    free(made_prompt);
 
584
  }
443
585
  if(ret == -1){
444
586
    error_plus(EX_OSERR, errno, "asprintf");
445
587
  }
446
588
  
447
589
  /* plymouth ask-for-password --prompt="$prompt" */
 
590
  if(debug){
 
591
    fprintf_plus(stderr, "Prompting for password via Plymouth\n");
 
592
  }
448
593
  bret = exec_and_wait(&plymouth_command_pid,
449
594
                       plymouth_path, (const char *[])
450
595
                       { plymouth_path, "ask-for-password",
460
605
  }
461
606
  kill_and_wait(plymouth_command_pid);
462
607
  
463
 
  const char * const *plymouthd_argv;
 
608
  char **plymouthd_argv = NULL;
464
609
  pid_t pid = get_pid();
465
610
  if(pid == 0){
466
611
    error_plus(0, 0, "plymouthd pid not found");
467
 
    plymouthd_argv = plymouthd_default_argv;
468
612
  } else {
469
613
    plymouthd_argv = getargv(pid);
470
614
  }
473
617
                       { plymouth_path, "quit", NULL },
474
618
                       false, false);
475
619
  if(not bret){
 
620
    if(plymouthd_argv != NULL){
 
621
      free(*plymouthd_argv);
 
622
      free(plymouthd_argv);
 
623
    }
476
624
    exit(EXIT_FAILURE);
477
625
  }
478
 
  bret = exec_and_wait(NULL, plymouthd_path, plymouthd_argv,
 
626
  bret = exec_and_wait(NULL, plymouthd_path,
 
627
                       (plymouthd_argv != NULL)
 
628
                       ? (const char * const *)plymouthd_argv
 
629
                       : plymouthd_default_argv,
479
630
                       false, true);
 
631
  if(plymouthd_argv != NULL){
 
632
    free(*plymouthd_argv);
 
633
    free(plymouthd_argv);
 
634
  }
480
635
  if(not bret){
481
636
    exit(EXIT_FAILURE);
482
637
  }