/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/password-prompt.c

* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
                                    Bug fix: If taking down network
                                    interface, do not drop privileges
                                    completely; save them and reassert
                                    privileges when needed.

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
 * 
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <https://www.fukt.bsnet.se/~belorn/> and
23
 
 * <https://www.fukt.bsnet.se/~teddy/>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
24
23
 */
25
24
 
26
25
#define _GNU_SOURCE             /* getline() */
33
32
#include <signal.h>             /* sig_atomic_t, raise(), struct
34
33
                                   sigaction, sigemptyset(),
35
34
                                   sigaction(), sigaddset(), SIGINT,
36
 
                                   SIGQUIT, SIGHUP, SIGTERM */
 
35
                                   SIGQUIT, SIGHUP, SIGTERM,
 
36
                                   raise() */
37
37
#include <stddef.h>             /* NULL, size_t, ssize_t */
38
38
#include <sys/types.h>          /* ssize_t */
39
39
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
52
52
                                   ARGP_ERR_UNKNOWN */
53
53
 
54
54
volatile sig_atomic_t quit_now = 0;
 
55
int signal_received;
55
56
bool debug = false;
56
57
const char *argp_program_version = "password-prompt " VERSION;
57
58
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
58
59
 
59
 
static void termination_handler(__attribute__((unused))int signum){
 
60
static void termination_handler(int signum){
 
61
  if(quit_now){
 
62
    return;
 
63
  }
60
64
  quit_now = 1;
 
65
  signal_received = signum;
61
66
}
62
67
 
63
68
int main(int argc, char **argv){
123
128
  }
124
129
  
125
130
  sigemptyset(&new_action.sa_mask);
126
 
  sigaddset(&new_action.sa_mask, SIGINT);
127
 
  sigaddset(&new_action.sa_mask, SIGHUP);
128
 
  sigaddset(&new_action.sa_mask, SIGTERM);
 
131
  ret = sigaddset(&new_action.sa_mask, SIGINT);
 
132
  if(ret == -1){
 
133
    perror("sigaddset");
 
134
    return EXIT_FAILURE;
 
135
  }
 
136
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
 
137
  if(ret == -1){
 
138
    perror("sigaddset");
 
139
    return EXIT_FAILURE;
 
140
  }
 
141
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
 
142
  if(ret == -1){
 
143
    perror("sigaddset");
 
144
    return EXIT_FAILURE;
 
145
  }
 
146
  /* Need to check if the handler is SIG_IGN before handling:
 
147
     | [[info:libc:Initial Signal Actions]] |
 
148
     | [[info:libc:Basic Signal Handling]]  |
 
149
  */
129
150
  ret = sigaction(SIGINT, NULL, &old_action);
130
151
  if(ret == -1){
131
152
    perror("sigaction");
194
215
      const char *cryptsource = getenv("cryptsource");
195
216
      const char *crypttarget = getenv("crypttarget");
196
217
      const char *const prompt
197
 
        = "Enter passphrase to unlock the disk";      
 
218
        = "Enter passphrase to unlock the disk";
198
219
      if(cryptsource == NULL){
199
220
        if(crypttarget == NULL){
200
221
          fprintf(stderr, "%s: ", prompt);
242
263
    /* if(ret == 0), then the only sensible thing to do is to retry to
243
264
       read from stdin */
244
265
    fputc('\n', stderr);
245
 
    if(debug and quit_now == 0){
 
266
    if(debug and not quit_now){
246
267
      /* If quit_now is nonzero, we were interrupted by a signal, and
247
268
         will print that later, so no need to show this too. */
248
269
      fprintf(stderr, "getline() returned 0, retrying.\n");
258
279
    perror("tcsetattr+echo");
259
280
  }
260
281
  
 
282
  if(quit_now){
 
283
    sigemptyset(&old_action.sa_mask);
 
284
    old_action.sa_handler = SIG_DFL;
 
285
    ret = sigaction(signal_received, &old_action, NULL);
 
286
    if(ret == -1){
 
287
      perror("sigaction");
 
288
    }
 
289
    raise(signal_received);
 
290
  }
 
291
  
261
292
  if(debug){
262
293
    fprintf(stderr, "%s is exiting with status %d\n", argv[0],
263
294
            status);