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
19
19
* along with this program. If not, see
20
20
* <http://www.gnu.org/licenses/>.
22
* Contact the authors at <mandos@fukt.bsnet.se>.
22
* Contact the authors at <mandos@recompile.se>.
25
25
#define _GNU_SOURCE /* asprintf(), TEMP_FAILURE_RETRY() */
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(),
40
41
#include <unistd.h> /* close(), write(), readlink(),
41
42
read(), STDOUT_FILENO, sleep(),
42
43
fork(), setuid(), geteuid(),
49
50
#include <inttypes.h> /* intmax_t, strtoimax() */
50
51
#include <sys/stat.h> /* struct stat, lstat(), S_ISLNK */
51
52
#include <sysexits.h> /* EX_OSERR, EX_UNAVAILABLE */
53
#include <argz.h> /* argz_count(), argz_extract() */
54
#include <stdarg.h> /* va_list, va_start(), ... */
53
56
sig_atomic_t interrupted_by_signal = 0;
54
57
int signal_received;
55
58
const char usplash_name[] = "/sbin/usplash";
60
/* Function to use when printing errors */
61
void error_plus(int status, int errnum, const char *formatstring,
67
va_start(ap, formatstring);
68
ret = vasprintf(&text, formatstring, ap);
70
fprintf(stderr, "Mandos plugin %s: ",
71
program_invocation_short_name);
72
vfprintf(stderr, formatstring, ap);
73
fprintf(stderr, ": ");
74
fprintf(stderr, "%s\n", strerror(errnum));
75
error(status, errno, "vasprintf while printing error");
78
fprintf(stderr, "Mandos plugin ");
79
error(status, errnum, "%s", text);
57
83
static void termination_handler(int signum){
58
84
if(interrupted_by_signal){
154
180
size_t cmdline_len = 0;
155
181
DIR *proc_dir = opendir("/proc");
156
182
if(proc_dir == NULL){
157
error(0, errno, "opendir");
183
error_plus(0, errno, "opendir");
183
209
ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
185
error(0, errno, "asprintf");
211
error_plus(0, errno, "asprintf");
186
212
goto fail_find_usplash;
225
251
ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
226
252
proc_ent->d_name);
228
error(0, errno, "asprintf");
254
error_plus(0, errno, "asprintf");
229
255
goto fail_find_usplash;
231
257
cl_fd = open(cmdline_filename, O_RDONLY);
232
258
free(cmdline_filename);
234
error(0, errno, "open");
260
error_plus(0, errno, "open");
235
261
goto fail_find_usplash;
243
269
if(cmdline_len + blocksize > cmdline_allocated){
244
270
tmp = realloc(cmdline, cmdline_allocated + blocksize);
246
error(0, errno, "realloc");
272
error_plus(0, errno, "realloc");
248
274
goto fail_find_usplash;
254
280
sret = read(cl_fd, cmdline + cmdline_len,
255
281
cmdline_allocated - cmdline_len);
257
error(0, errno, "read");
283
error_plus(0, errno, "read");
259
285
goto fail_find_usplash;
262
288
} while(sret != 0);
263
289
ret = close(cl_fd);
265
error(0, errno, "close");
291
error_plus(0, errno, "close");
266
292
goto fail_find_usplash;
269
295
/* Close directory */
270
296
ret = closedir(proc_dir);
272
error(0, errno, "closedir");
298
error_plus(0, errno, "closedir");
273
299
goto fail_find_usplash;
324
350
sigemptyset(&new_action.sa_mask);
325
351
ret = sigaddset(&new_action.sa_mask, SIGINT);
327
error(0, errno, "sigaddset");
353
error_plus(0, errno, "sigaddset");
328
354
status = EX_OSERR;
331
357
ret = sigaddset(&new_action.sa_mask, SIGHUP);
333
error(0, errno, "sigaddset");
359
error_plus(0, errno, "sigaddset");
334
360
status = EX_OSERR;
337
363
ret = sigaddset(&new_action.sa_mask, SIGTERM);
339
error(0, errno, "sigaddset");
365
error_plus(0, errno, "sigaddset");
340
366
status = EX_OSERR;
343
369
ret = sigaction(SIGINT, NULL, &old_action);
345
371
if(errno != EINTR){
346
error(0, errno, "sigaction");
372
error_plus(0, errno, "sigaction");
347
373
status = EX_OSERR;
352
378
ret = sigaction(SIGINT, &new_action, NULL);
354
380
if(errno != EINTR){
355
error(0, errno, "sigaction");
381
error_plus(0, errno, "sigaction");
356
382
status = EX_OSERR;
361
387
ret = sigaction(SIGHUP, NULL, &old_action);
363
389
if(errno != EINTR){
364
error(0, errno, "sigaction");
390
error_plus(0, errno, "sigaction");
365
391
status = EX_OSERR;
370
396
ret = sigaction(SIGHUP, &new_action, NULL);
372
398
if(errno != EINTR){
373
error(0, errno, "sigaction");
399
error_plus(0, errno, "sigaction");
374
400
status = EX_OSERR;
379
405
ret = sigaction(SIGTERM, NULL, &old_action);
381
407
if(errno != EINTR){
382
error(0, errno, "sigaction");
408
error_plus(0, errno, "sigaction");
383
409
status = EX_OSERR;
388
414
ret = sigaction(SIGTERM, &new_action, NULL);
390
416
if(errno != EINTR){
391
error(0, errno, "sigaction");
417
error_plus(0, errno, "sigaction");
392
418
status = EX_OSERR;
400
426
/* Write command to FIFO */
401
427
if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
402
428
if(errno != EINTR){
403
error(0, errno, "usplash_write");
429
error_plus(0, errno, "usplash_write");
404
430
status = EX_OSERR;
413
439
if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
414
440
if(errno != EINTR){
415
error(0, errno, "usplash_write");
441
error_plus(0, errno, "usplash_write");
416
442
status = EX_OSERR;
449
475
char *tmp = realloc(buf, buf_allocated + blocksize);
451
477
if(errno != EINTR){
452
error(0, errno, "realloc");
478
error_plus(0, errno, "realloc");
453
479
status = EX_OSERR;
490
516
if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
491
517
if(errno != EINTR){
492
error(0, errno, "usplash_write");
518
error_plus(0, errno, "usplash_write");
493
519
status = EX_OSERR;
516
542
sret = write(STDOUT_FILENO, buf + written, buf_len - written);
518
544
if(errno != EINTR){
519
error(0, errno, "write");
545
error_plus(0, errno, "write");
520
546
status = EX_OSERR;
553
579
if(fifo_fd != -1){
554
580
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
555
581
if(ret == -1 and errno != EINTR){
556
error(0, errno, "close");
582
error_plus(0, errno, "close");
562
588
if(outfifo_fd != -1){
563
589
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;
591
error_plus(0, errno, "close");
595
/* Create argv for new usplash*/
596
char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
597
* sizeof(char *)); /* Count args */
598
if(cmdline_argv == NULL){
599
error_plus(0, errno, "malloc");
602
argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
590
604
/* Kill old usplash */
591
605
kill(usplash_pid, SIGTERM);
603
617
the real user ID (_mandos) */
604
618
ret = setuid(geteuid());
606
error(0, errno, "setuid");
620
error_plus(0, errno, "setuid");
610
624
ret = chdir("/");
612
error(0, errno, "chdir");
626
error_plus(0, errno, "chdir");
615
629
/* if(fork() != 0){ */
618
632
ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
620
error(0, errno, "dup2");
634
error_plus(0, errno, "dup2");
624
638
execv(usplash_name, cmdline_argv);
625
639
if(not interrupted_by_signal){
626
error(0, errno, "execv");
640
error_plus(0, errno, "execv");
629
643
free(cmdline_argv);
635
649
if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
636
650
if(errno != EINTR){
637
error(0, errno, "usplash_write");
651
error_plus(0, errno, "usplash_write");
642
656
if(fifo_fd != -1){
643
657
ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
644
658
if(ret == -1 and errno != EINTR){
645
error(0, errno, "close");
659
error_plus(0, errno, "close");
653
667
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
654
668
&signal_action, NULL));
656
error(0, errno, "sigaction");
670
error_plus(0, errno, "sigaction");
659
673
ret = raise(signal_received);
660
674
} while(ret != 0 and errno == EINTR);
662
error(0, errno, "raise");
676
error_plus(0, errno, "raise");
665
679
TEMP_FAILURE_RETRY(pause());