/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release
13 by Björn Påhlsson
Added following support:
1
#define _GNU_SOURCE		/* getline() */
2
#define _FORTIFY_SOURCE 2
3
#include <termios.h> 		/* struct termios, tcsetattr(),
4
				   TCSAFLUSH, tcgetattr(), ECHO */
5
#include <unistd.h>		/* struct termios, tcsetattr(),
6
				   STDIN_FILENO, TCSAFLUSH,
7
				   tcgetattr(), ECHO */
8
#include <signal.h>		/* sig_atomic_t, raise(), struct
9
				   sigaction, sigemptyset(),
10
				   sigaction(), sigaddset(), SIGINT,
11
				   SIGQUIT, SIGHUP, SIGTERM */
12
#include <stddef.h>		/* NULL, size_t */
13
#include <sys/types.h>		/* ssize_t */
14
#include <stdlib.h>		/* EXIT_SUCCESS, EXIT_FAILURE */
15
#include <stdio.h>		/* fprintf(), stderr, getline(),
16
				   stdin, feof(), perror(), fputc(),
17
				   stdout */
18
#include <errno.h>		/* errno, EINVAL */
19
#include <iso646.h>		/* or, not */
20
#include <stdbool.h>		/* bool, false, true */
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
21
#include <string.h> 		/* strlen, rindex, strncmp, strcmp */
13 by Björn Påhlsson
Added following support:
22
23
volatile bool quit_now = false;
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
24
bool debug = false;
13 by Björn Påhlsson
Added following support:
25
26
void termination_handler(int signum){
27
  quit_now = true;
28
}
29
30
int main(int argc, char **argv){
31
  ssize_t ret = -1;
32
  size_t n;
33
  struct termios t_new, t_old;
34
  char *buffer = NULL;
35
  int status = EXIT_SUCCESS;
36
  struct sigaction old_action,
37
    new_action = { .sa_handler = termination_handler,
38
		   .sa_flags = 0 };
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
39
  const char db[] = "--debug";
40
  char *basename = rindex(argv[0], '/');
41
  if(basename == NULL){
42
    basename = argv[0];
43
  } else {
44
    basename++;
45
  }
46
47
  char *program_name = malloc(strlen(basename) + sizeof(db));
48
  if (program_name == NULL){
49
    perror("argv[0]");
50
    return EXIT_FAILURE;
51
  }
52
    
53
  program_name[0] = '\0';
54
    
55
  for (int i = 1; i < argc; i++){
56
    if (not strncmp(argv[i], db, 5)){
57
      strcat(strcat(strcat(program_name, db ), "="), basename);
58
      if(not strcmp(argv[i], db) or not strcmp(argv[i], program_name)){
59
	debug = true;
60
      }
61
    }
62
  }
63
  free(program_name);
64
65
  if (debug){
66
    fprintf(stderr, "Starting %s\n", argv[0]);
67
  }
68
  if (debug){
69
    fprintf(stderr, "Storing current terminal attributes\n");
70
  }
13 by Björn Påhlsson
Added following support:
71
  
72
  if (tcgetattr(STDIN_FILENO, &t_old) != 0){
73
    return EXIT_FAILURE;
74
  }
75
  
76
  sigemptyset(&new_action.sa_mask);
77
  sigaddset(&new_action.sa_mask, SIGINT);
78
  sigaddset(&new_action.sa_mask, SIGQUIT);
79
  sigaddset(&new_action.sa_mask, SIGHUP);
80
  sigaddset(&new_action.sa_mask, SIGTERM);
81
  sigaction(SIGINT, NULL, &old_action);
82
  if (old_action.sa_handler != SIG_IGN)
83
    sigaction(SIGINT, &new_action, NULL);
84
  sigaction(SIGQUIT, NULL, &old_action);
85
  if (old_action.sa_handler != SIG_IGN)
86
    sigaction(SIGQUIT, &new_action, NULL);
87
  sigaction(SIGHUP, NULL, &old_action);
88
  if (old_action.sa_handler != SIG_IGN)
89
    sigaction(SIGHUP, &new_action, NULL);
90
  sigaction(SIGTERM, NULL, &old_action);
91
  if (old_action.sa_handler != SIG_IGN)
92
    sigaction(SIGTERM, &new_action, NULL);
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
93
94
  
95
  if (debug){
96
    fprintf(stderr, "Removing echo flag from terminal attributes\n");
97
  }
13 by Björn Påhlsson
Added following support:
98
  
99
  t_new = t_old;
100
  t_new.c_lflag &= ~ECHO;
101
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
102
    perror("tcsetattr-echo");
103
    return EXIT_FAILURE;
104
  }
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
105
106
  if (debug){
107
    fprintf(stderr, "Waiting for input from stdin \n");
108
  }
13 by Björn Påhlsson
Added following support:
109
  while(true){
110
    if (quit_now){
111
      status = EXIT_FAILURE;
112
      break;
113
    }
114
    fprintf(stderr, "Password: ");
115
    ret = getline(&buffer, &n, stdin);
116
    if (ret > 0){
117
      fprintf(stdout, "%s", buffer);
118
      status = EXIT_SUCCESS;
119
      break;
120
    }
121
    // ret == 0 makes no other sence than to retry to read from stdin
122
    if (ret < 0){
123
      if (errno != EINTR and not feof(stdin)){
124
	perror("getline");
125
	status = EXIT_FAILURE;
126
	break;
127
      }
128
    }
129
    fputc('\n', stderr);
130
  }
131
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
132
  if (debug){
133
    fprintf(stderr, "Restoring terminal attributes\n");
134
  }
13 by Björn Påhlsson
Added following support:
135
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
136
    perror("tcsetattr+echo");
137
  }
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
138
139
  if (debug){
140
    fprintf(stderr, "%s is exiting\n", argv[0]);
141
  }
13 by Björn Påhlsson
Added following support:
142
  
143
  return status;
144
}