/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/password-prompt.c

  • Committer: Teddy Hogeborn
  • Date: 2009-09-05 01:05:25 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090905010525-7qeq05ztwc2ddhuv
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
                                    Bug fix: remove signal handler
                                    before re-raising signal.

* plugins.d/password-prompt.c (main): Check return values from
                                      "sigaddset()".

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8 -*- */
 
1
/*  -*- coding: utf-8; mode: c; mode: orgtbl -*- */
2
2
/*
3
3
 * Password-prompt - Read a password from the terminal and print it
4
4
 * 
33
33
#include <signal.h>             /* sig_atomic_t, raise(), struct
34
34
                                   sigaction, sigemptyset(),
35
35
                                   sigaction(), sigaddset(), SIGINT,
36
 
                                   SIGQUIT, SIGHUP, SIGTERM */
 
36
                                   SIGQUIT, SIGHUP, SIGTERM,
 
37
                                   raise() */
37
38
#include <stddef.h>             /* NULL, size_t, ssize_t */
38
39
#include <sys/types.h>          /* ssize_t */
39
40
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
51
52
                                   ARGP_KEY_ARG, ARGP_KEY_END,
52
53
                                   ARGP_ERR_UNKNOWN */
53
54
 
54
 
volatile bool quit_now = false;
 
55
volatile sig_atomic_t quit_now = 0;
 
56
int signal_received;
55
57
bool debug = false;
56
58
const char *argp_program_version = "password-prompt " VERSION;
57
59
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
58
60
 
59
 
static void termination_handler(__attribute__((unused))int signum){
60
 
  quit_now = true;
 
61
static void termination_handler(int signum){
 
62
  if(quit_now){
 
63
    return;
 
64
  }
 
65
  quit_now = 1;
 
66
  signal_received = signum;
61
67
}
62
68
 
63
69
int main(int argc, char **argv){
80
86
      { .name = NULL }
81
87
    };
82
88
    
83
 
    error_t parse_opt (int key, char *arg, struct argp_state *state) {
84
 
      /* Get the INPUT argument from `argp_parse', which we know is a
85
 
         pointer to our plugin list pointer. */
86
 
      switch (key) {
 
89
    error_t parse_opt (int key, char *arg, struct argp_state *state){
 
90
      switch (key){
87
91
      case 'p':
88
92
        prefix = arg;
89
93
        break;
91
95
        debug = true;
92
96
        break;
93
97
      case ARGP_KEY_ARG:
94
 
        argp_usage (state);
 
98
        argp_usage(state);
95
99
        break;
96
100
      case ARGP_KEY_END:
97
101
        break;
105
109
                         .args_doc = "",
106
110
                         .doc = "Mandos password-prompt -- Read and"
107
111
                         " output a password" };
108
 
    ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
109
 
    if (ret == ARGP_ERR_UNKNOWN){
 
112
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
113
    if(ret == ARGP_ERR_UNKNOWN){
110
114
      fprintf(stderr, "Unknown error while parsing arguments\n");
111
115
      return EXIT_FAILURE;
112
116
    }
113
117
  }
114
118
  
115
 
  if (debug){
 
119
  if(debug){
116
120
    fprintf(stderr, "Starting %s\n", argv[0]);
117
121
  }
118
 
  if (debug){
 
122
  if(debug){
119
123
    fprintf(stderr, "Storing current terminal attributes\n");
120
124
  }
121
125
  
122
 
  if (tcgetattr(STDIN_FILENO, &t_old) != 0){
 
126
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
123
127
    perror("tcgetattr");
124
128
    return EXIT_FAILURE;
125
129
  }
126
130
  
127
131
  sigemptyset(&new_action.sa_mask);
128
 
  sigaddset(&new_action.sa_mask, SIGINT);
129
 
  sigaddset(&new_action.sa_mask, SIGHUP);
130
 
  sigaddset(&new_action.sa_mask, SIGTERM);
 
132
  ret = sigaddset(&new_action.sa_mask, SIGINT);
 
133
  if(ret == -1){
 
134
    perror("sigaddset");
 
135
    return EXIT_FAILURE;
 
136
  }
 
137
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
 
138
  if(ret == -1){
 
139
    perror("sigaddset");
 
140
    return EXIT_FAILURE;
 
141
  }
 
142
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
 
143
  if(ret == -1){
 
144
    perror("sigaddset");
 
145
    return EXIT_FAILURE;
 
146
  }
 
147
  /* Need to check if the handler is SIG_IGN before handling:
 
148
     | [[info:libc:Initial Signal Actions]] |
 
149
     | [[info:libc:Basic Signal Handling]]  |
 
150
  */
131
151
  ret = sigaction(SIGINT, NULL, &old_action);
132
152
  if(ret == -1){
133
153
    perror("sigaction");
134
154
    return EXIT_FAILURE;
135
155
  }
136
 
  if (old_action.sa_handler != SIG_IGN){
 
156
  if(old_action.sa_handler != SIG_IGN){
137
157
    ret = sigaction(SIGINT, &new_action, NULL);
138
158
    if(ret == -1){
139
159
      perror("sigaction");
145
165
    perror("sigaction");
146
166
    return EXIT_FAILURE;
147
167
  }
148
 
  if (old_action.sa_handler != SIG_IGN){
 
168
  if(old_action.sa_handler != SIG_IGN){
149
169
    ret = sigaction(SIGHUP, &new_action, NULL);
150
170
    if(ret == -1){
151
171
      perror("sigaction");
157
177
    perror("sigaction");
158
178
    return EXIT_FAILURE;
159
179
  }
160
 
  if (old_action.sa_handler != SIG_IGN){
 
180
  if(old_action.sa_handler != SIG_IGN){
161
181
    ret = sigaction(SIGTERM, &new_action, NULL);
162
182
    if(ret == -1){
163
183
      perror("sigaction");
166
186
  }
167
187
  
168
188
  
169
 
  if (debug){
 
189
  if(debug){
170
190
    fprintf(stderr, "Removing echo flag from terminal attributes\n");
171
191
  }
172
192
  
173
193
  t_new = t_old;
174
194
  t_new.c_lflag &= ~ECHO;
175
 
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
 
195
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
176
196
    perror("tcsetattr-echo");
177
197
    return EXIT_FAILURE;
178
198
  }
179
199
 
180
 
  if (debug){
 
200
  if(debug){
181
201
    fprintf(stderr, "Waiting for input from stdin \n");
182
202
  }
183
203
  while(true){
184
 
    if (quit_now){
 
204
    if(quit_now){
185
205
      if(debug){
186
206
        fprintf(stderr, "Interrupted by signal, exiting.\n");
187
207
      }
213
233
      }
214
234
    }
215
235
    ret = getline(&buffer, &n, stdin);
216
 
    if (ret > 0){
 
236
    if(ret > 0){
217
237
      status = EXIT_SUCCESS;
218
238
      /* Make n = data size instead of allocated buffer size */
219
239
      n = (size_t)ret;
234
254
      }
235
255
      break;
236
256
    }
237
 
    if (ret < 0){
238
 
      if (errno != EINTR and not feof(stdin)){
 
257
    if(ret < 0){
 
258
      if(errno != EINTR and not feof(stdin)){
239
259
        perror("getline");
240
260
        status = EXIT_FAILURE;
241
261
        break;
245
265
       read from stdin */
246
266
    fputc('\n', stderr);
247
267
    if(debug and not quit_now){
248
 
      /* If quit_now is true, we were interrupted by a signal, and
 
268
      /* If quit_now is nonzero, we were interrupted by a signal, and
249
269
         will print that later, so no need to show this too. */
250
270
      fprintf(stderr, "getline() returned 0, retrying.\n");
251
271
    }
253
273
  
254
274
  free(buffer);
255
275
  
256
 
  if (debug){
 
276
  if(debug){
257
277
    fprintf(stderr, "Restoring terminal attributes\n");
258
278
  }
259
 
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
 
279
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
260
280
    perror("tcsetattr+echo");
261
281
  }
262
282
  
263
 
  if (debug){
 
283
  if(quit_now){
 
284
    sigemptyset(&old_action.sa_mask);
 
285
    old_action.sa_handler = SIG_DFL;
 
286
    ret = sigaction(signal_received, &old_action, NULL);
 
287
    if(ret == -1){
 
288
      perror("sigaction");
 
289
    }
 
290
    raise(signal_received);
 
291
  }
 
292
  
 
293
  if(debug){
264
294
    fprintf(stderr, "%s is exiting with status %d\n", argv[0],
265
295
            status);
266
296
  }