/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 */
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
14
#include <stdlib.h>		/* EXIT_SUCCESS, EXIT_FAILURE,
15
				   getopt_long */
13 by Björn Påhlsson
Added following support:
16
#include <stdio.h>		/* fprintf(), stderr, getline(),
17
				   stdin, feof(), perror(), fputc(),
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
18
				   stdout, getopt_long */
13 by Björn Påhlsson
Added following support:
19
#include <errno.h>		/* errno, EINVAL */
20
#include <iso646.h>		/* or, not */
21
#include <stdbool.h>		/* bool, false, true */
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
22
#include <string.h> 		/* strlen, rindex, strncmp, strcmp */
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
23
#include <getopt.h>		/* getopt_long */
13 by Björn Påhlsson
Added following support:
24
25
volatile bool quit_now = false;
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
26
bool debug = false;
13 by Björn Påhlsson
Added following support:
27
28
void termination_handler(int signum){
29
  quit_now = true;
30
}
31
32
int main(int argc, char **argv){
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
33
  ssize_t ret;
13 by Björn Påhlsson
Added following support:
34
  size_t n;
35
  struct termios t_new, t_old;
36
  char *buffer = NULL;
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
37
  char *prefix = NULL;
13 by Björn Påhlsson
Added following support:
38
  int status = EXIT_SUCCESS;
39
  struct sigaction old_action,
40
    new_action = { .sa_handler = termination_handler,
41
		   .sa_flags = 0 };
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
42
43
  while (true){
44
    static struct option long_options[] = {
45
      {"debug", no_argument, (int *)&debug, 1},
46
      {"prefix", required_argument, 0, 'p'},
47
      {0, 0, 0, 0} };
48
49
    int option_index = 0;
50
    ret = getopt_long (argc, argv, "p:", long_options, &option_index);
51
52
    if (ret == -1){
53
      break;
54
    }
55
      
56
    switch(ret){
57
    case 0:
58
      break;
59
    case 'p':
60
      prefix = optarg;
61
      break;
62
    default:
63
      fprintf(stderr, "bad arguments\n");
64
      exit(EXIT_FAILURE);
65
    }
66
  }
67
      
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
68
  if (debug){
69
    fprintf(stderr, "Starting %s\n", argv[0]);
70
  }
71
  if (debug){
72
    fprintf(stderr, "Storing current terminal attributes\n");
73
  }
13 by Björn Påhlsson
Added following support:
74
  
75
  if (tcgetattr(STDIN_FILENO, &t_old) != 0){
76
    return EXIT_FAILURE;
77
  }
78
  
79
  sigemptyset(&new_action.sa_mask);
80
  sigaddset(&new_action.sa_mask, SIGINT);
81
  sigaddset(&new_action.sa_mask, SIGQUIT);
82
  sigaddset(&new_action.sa_mask, SIGHUP);
83
  sigaddset(&new_action.sa_mask, SIGTERM);
84
  sigaction(SIGINT, NULL, &old_action);
85
  if (old_action.sa_handler != SIG_IGN)
86
    sigaction(SIGINT, &new_action, NULL);
87
  sigaction(SIGQUIT, NULL, &old_action);
88
  if (old_action.sa_handler != SIG_IGN)
89
    sigaction(SIGQUIT, &new_action, NULL);
90
  sigaction(SIGHUP, NULL, &old_action);
91
  if (old_action.sa_handler != SIG_IGN)
92
    sigaction(SIGHUP, &new_action, NULL);
93
  sigaction(SIGTERM, NULL, &old_action);
94
  if (old_action.sa_handler != SIG_IGN)
95
    sigaction(SIGTERM, &new_action, NULL);
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
96
97
  
98
  if (debug){
99
    fprintf(stderr, "Removing echo flag from terminal attributes\n");
100
  }
13 by Björn Påhlsson
Added following support:
101
  
102
  t_new = t_old;
103
  t_new.c_lflag &= ~ECHO;
104
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
105
    perror("tcsetattr-echo");
106
    return EXIT_FAILURE;
107
  }
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
108
109
  if (debug){
110
    fprintf(stderr, "Waiting for input from stdin \n");
111
  }
13 by Björn Påhlsson
Added following support:
112
  while(true){
113
    if (quit_now){
114
      status = EXIT_FAILURE;
115
      break;
116
    }
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
117
118
    if(prefix){
119
      fprintf(stderr, "%s Password: ", prefix);
120
    } else {
121
      fprintf(stderr, "Password: ");
122
    }      
13 by Björn Påhlsson
Added following support:
123
    ret = getline(&buffer, &n, stdin);
124
    if (ret > 0){
125
      fprintf(stdout, "%s", buffer);
126
      status = EXIT_SUCCESS;
127
      break;
128
    }
129
    // ret == 0 makes no other sence than to retry to read from stdin
130
    if (ret < 0){
131
      if (errno != EINTR and not feof(stdin)){
132
	perror("getline");
133
	status = EXIT_FAILURE;
134
	break;
135
      }
136
    }
137
    fputc('\n', stderr);
138
  }
139
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
140
  if (debug){
141
    fprintf(stderr, "Restoring terminal attributes\n");
142
  }
13 by Björn Påhlsson
Added following support:
143
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
144
    perror("tcsetattr+echo");
145
  }
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
146
147
  if (debug){
148
    fprintf(stderr, "%s is exiting\n", argv[0]);
149
  }
13 by Björn Påhlsson
Added following support:
150
  
151
  return status;
152
}