3
3
* Usplash - Read a password from usplash and output it
5
* Copyright © 2008,2009 Teddy Hogeborn
6
* Copyright © 2008,2009 Björn Påhlsson
5
* Copyright © 2008-2011 Teddy Hogeborn
6
* Copyright © 2008-2011 Björn Påhlsson
8
8
* This program is free software: you can redistribute it and/or
9
9
* modify it under the terms of the GNU General Public License as
35
35
#include <sys/types.h> /* size_t, ssize_t, pid_t, DIR, struct
37
37
#include <stddef.h> /* NULL */
38
#include <string.h> /* strlen(), memcmp() */
39
#include <stdio.h> /* asprintf()*/
38
#include <string.h> /* strlen(), memcmp(), strerror() */
39
#include <stdio.h> /* asprintf(), vasprintf(), vprintf(), fprintf() */
40
40
#include <unistd.h> /* close(), write(), readlink(),
41
41
read(), STDOUT_FILENO, sleep(),
42
42
fork(), setuid(), geteuid(),
49
49
#include <inttypes.h> /* intmax_t, strtoimax() */
50
50
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
51
51
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
52
#include <argz.h> /* argz_count(), argz_extract() */
53
#include <stdarg.h> /* va_list, va_start(), ... */
53
55
sig_atomic_t interrupted_by_signal = 0;
54
56
int signal_received;
55
57
const char usplash_name[] = "/sbin/usplash";
59
/* Function to use when printing errors */
60
void error_plus(int status, int errnum, const char *formatstring, ...){
65
va_start(ap, formatstring);
66
ret = vasprintf(&text, formatstring, ap);
68
fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
69
vfprintf(stderr, formatstring, ap);
70
fprintf(stderr, ": ");
71
fprintf(stderr, "%s\n", strerror(errnum));
72
error(status, errno, "vasprintf while printing error");
75
fprintf(stderr, "Mandos plugin ");
76
error(status, errnum, "%s", text);
57
80
static void termination_handler(int signum){
58
81
if(interrupted_by_signal){
154
177
size_t cmdline_len = 0;
155
178
DIR *proc_dir = opendir("/proc");
156
179
if(proc_dir == NULL){
157
error(0, errno, "opendir");
180
error_plus(0, errno, "opendir");
183
206
ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
185
error(0, errno, "asprintf");
208
error_plus(0, errno, "asprintf");
186
209
goto fail_find_usplash;
225
248
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
226
249
proc_ent->d_name);
228
error(0, errno, "asprintf");
251
error_plus(0, errno, "asprintf");
229
252
goto fail_find_usplash;
231
254
cl_fd = open(cmdline_filename, O_RDONLY);
232
255
free(cmdline_filename);
234
error(0, errno, "open");
257
error_plus(0, errno, "open");
235
258
goto fail_find_usplash;
243
266
if(cmdline_len + blocksize > cmdline_allocated){
244
267
tmp = realloc(cmdline, cmdline_allocated + blocksize);
246
error(0, errno, "realloc");
269
error_plus(0, errno, "realloc");
248
271
goto fail_find_usplash;
254
277
sret = read(cl_fd, cmdline + cmdline_len,
255
278
cmdline_allocated - cmdline_len);
257
error(0, errno, "read");
280
error_plus(0, errno, "read");
259
282
goto fail_find_usplash;
262
285
} while(sret != 0);
263
286
ret = close(cl_fd);
265
error(0, errno, "close");
288
error_plus(0, errno, "close");
266
289
goto fail_find_usplash;
269
292
/* Close directory */
270
293
ret = closedir(proc_dir);
272
error(0, errno, "closedir");
295
error_plus(0, errno, "closedir");
273
296
goto fail_find_usplash;
324
347
sigemptyset(&new_action.sa_mask);
325
348
ret = sigaddset(&new_action.sa_mask, SIGINT);
327
error(0, errno, "sigaddset");
350
error_plus(0, errno, "sigaddset");
328
351
status = EX_OSERR;
331
354
ret = sigaddset(&new_action.sa_mask, SIGHUP);
333
error(0, errno, "sigaddset");
356
error_plus(0, errno, "sigaddset");
334
357
status = EX_OSERR;
337
360
ret = sigaddset(&new_action.sa_mask, SIGTERM);
339
error(0, errno, "sigaddset");
362
error_plus(0, errno, "sigaddset");
340
363
status = EX_OSERR;
343
366
ret = sigaction(SIGINT, NULL, &old_action);
345
368
if(errno != EINTR){
346
error(0, errno, "sigaction");
369
error_plus(0, errno, "sigaction");
347
370
status = EX_OSERR;
352
375
ret = sigaction(SIGINT, &new_action, NULL);
354
377
if(errno != EINTR){
355
error(0, errno, "sigaction");
378
error_plus(0, errno, "sigaction");
356
379
status = EX_OSERR;
361
384
ret = sigaction(SIGHUP, NULL, &old_action);
363
386
if(errno != EINTR){
364
error(0, errno, "sigaction");
387
error_plus(0, errno, "sigaction");
365
388
status = EX_OSERR;
370
393
ret = sigaction(SIGHUP, &new_action, NULL);
372
395
if(errno != EINTR){
373
error(0, errno, "sigaction");
396
error_plus(0, errno, "sigaction");
374
397
status = EX_OSERR;
379
402
ret = sigaction(SIGTERM, NULL, &old_action);
381
404
if(errno != EINTR){
382
error(0, errno, "sigaction");
405
error_plus(0, errno, "sigaction");
383
406
status = EX_OSERR;
388
411
ret = sigaction(SIGTERM, &new_action, NULL);
390
413
if(errno != EINTR){
391
error(0, errno, "sigaction");
414
error_plus(0, errno, "sigaction");
392
415
status = EX_OSERR;
400
423
/* Write command to FIFO */
401
424
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
402
425
if(errno != EINTR){
403
error(0, errno, "usplash_write");
426
error_plus(0, errno, "usplash_write");
404
427
status = EX_OSERR;
413
436
if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
414
437
if(errno != EINTR){
415
error(0, errno, "usplash_write");
438
error_plus(0, errno, "usplash_write");
416
439
status = EX_OSERR;
449
472
char *tmp = realloc(buf, buf_allocated + blocksize);
451
474
if(errno != EINTR){
452
error(0, errno, "realloc");
475
error_plus(0, errno, "realloc");
453
476
status = EX_OSERR;
490
513
if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
491
514
if(errno != EINTR){
492
error(0, errno, "usplash_write");
515
error_plus(0, errno, "usplash_write");
493
516
status = EX_OSERR;
516
539
sret = write(STDOUT_FILENO, buf + written, buf_len - written);
518
541
if(errno != EINTR){
519
error(0, errno, "write");
542
error_plus(0, errno, "write");
520
543
status = EX_OSERR;
553
576
if(fifo_fd != -1){
554
577
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
555
578
if(ret == -1 and errno != EINTR){
556
error(0, errno, "close");
579
error_plus(0, errno, "close");
562
585
if(outfifo_fd != -1){
563
586
ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
565
error(0, errno, "close");
569
/* Create argc and argv for new usplash*/
570
int cmdline_argc = 0;
571
char **cmdline_argv = malloc(sizeof(char *));
574
while(position < cmdline_len){
575
char **tmp = realloc(cmdline_argv,
577
* (size_t)(cmdline_argc + 2)));
579
error(0, errno, "realloc");
584
cmdline_argv[cmdline_argc] = cmdline + position;
586
position += strlen(cmdline + position) + 1;
588
cmdline_argv[cmdline_argc] = NULL;
588
error_plus(0, errno, "close");
592
/* Create argv for new usplash*/
593
char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
594
* sizeof(char *)); /* Count args */
595
if(cmdline_argv == NULL){
596
error_plus(0, errno, "malloc");
599
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
590
601
/* Kill old usplash */
591
602
kill(usplash_pid, SIGTERM);
603
614
the real user ID (_mandos) */
604
615
ret = setuid(geteuid());
606
error(0, errno, "setuid");
617
error_plus(0, errno, "setuid");
610
621
ret = chdir("/");
612
error(0, errno, "chdir");
623
error_plus(0, errno, "chdir");
615
626
/* if(fork() != 0){ */
618
629
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
620
error(0, errno, "dup2");
631
error_plus(0, errno, "dup2");
624
635
execv(usplash_name, cmdline_argv);
625
636
if(not interrupted_by_signal){
626
error(0, errno, "execv");
637
error_plus(0, errno, "execv");
629
640
free(cmdline_argv);
635
646
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
636
647
if(errno != EINTR){
637
error(0, errno, "usplash_write");
648
error_plus(0, errno, "usplash_write");
642
653
if(fifo_fd != -1){
643
654
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
644
655
if(ret == -1 and errno != EINTR){
645
error(0, errno, "close");
656
error_plus(0, errno, "close");
653
664
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
654
665
&signal_action, NULL));
656
error(0, errno, "sigaction");
667
error_plus(0, errno, "sigaction");
659
670
ret = raise(signal_received);
660
671
} while(ret != 0 and errno == EINTR);
662
error(0, errno, "raise");
673
error_plus(0, errno, "raise");
665
676
TEMP_FAILURE_RETRY(pause());