/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-04-17 08:26:17 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090417082617-eltetak5lzjyz55o
* initramfs-tools-hook: Bug fix: Add "--userid" and "--groupid" to
                        start of "plugin-runner.conf" file instead of
                        appending, to allow any preexisting options to
                        override.
* plugin-runner.conf: Improved wording.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*  -*- coding: utf-8 -*- */
2
2
/*
3
 
 * Passprompt - Read a password from the terminal and print it
4
 
 *
5
 
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
 
3
 * Password-prompt - Read a password from the terminal and print it
 
4
 * 
 
5
 * Copyright © 2008,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 Björn Påhlsson
6
7
 * 
7
8
 * This program is free software: you can redistribute it and/or
8
9
 * modify it under the terms of the GNU General Public License as
50
51
                                   ARGP_KEY_ARG, ARGP_KEY_END,
51
52
                                   ARGP_ERR_UNKNOWN */
52
53
 
53
 
volatile bool quit_now = false;
 
54
volatile sig_atomic_t quit_now = 0;
54
55
bool debug = false;
55
 
const char *argp_program_version = "password-prompt 1.0";
 
56
const char *argp_program_version = "password-prompt " VERSION;
56
57
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
57
58
 
58
59
static void termination_handler(__attribute__((unused))int signum){
59
 
  quit_now = true;
 
60
  quit_now = 1;
60
61
}
61
62
 
62
63
int main(int argc, char **argv){
78
79
        .doc = "Debug mode", .group = 3 },
79
80
      { .name = NULL }
80
81
    };
81
 
  
82
 
    error_t parse_opt (int key, char *arg, struct argp_state *state) {
83
 
      /* Get the INPUT argument from `argp_parse', which we know is a
84
 
         pointer to our plugin list pointer. */
85
 
      switch (key) {
 
82
    
 
83
    error_t parse_opt (int key, char *arg, struct argp_state *state){
 
84
      switch (key){
86
85
      case 'p':
87
86
        prefix = arg;
88
87
        break;
90
89
        debug = true;
91
90
        break;
92
91
      case ARGP_KEY_ARG:
93
 
        argp_usage (state);
 
92
        argp_usage(state);
94
93
        break;
95
94
      case ARGP_KEY_END:
96
95
        break;
99
98
      }
100
99
      return 0;
101
100
    }
102
 
  
 
101
    
103
102
    struct argp argp = { .options = options, .parser = parse_opt,
104
103
                         .args_doc = "",
105
104
                         .doc = "Mandos password-prompt -- Read and"
106
105
                         " output a password" };
107
 
    ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
108
 
    if (ret == ARGP_ERR_UNKNOWN){
 
106
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
107
    if(ret == ARGP_ERR_UNKNOWN){
109
108
      fprintf(stderr, "Unknown error while parsing arguments\n");
110
109
      return EXIT_FAILURE;
111
110
    }
112
111
  }
113
 
    
114
 
  if (debug){
 
112
  
 
113
  if(debug){
115
114
    fprintf(stderr, "Starting %s\n", argv[0]);
116
115
  }
117
 
  if (debug){
 
116
  if(debug){
118
117
    fprintf(stderr, "Storing current terminal attributes\n");
119
118
  }
120
119
  
121
 
  if (tcgetattr(STDIN_FILENO, &t_old) != 0){
 
120
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
122
121
    perror("tcgetattr");
123
122
    return EXIT_FAILURE;
124
123
  }
132
131
    perror("sigaction");
133
132
    return EXIT_FAILURE;
134
133
  }
135
 
  if (old_action.sa_handler != SIG_IGN){
 
134
  if(old_action.sa_handler != SIG_IGN){
136
135
    ret = sigaction(SIGINT, &new_action, NULL);
137
136
    if(ret == -1){
138
137
      perror("sigaction");
144
143
    perror("sigaction");
145
144
    return EXIT_FAILURE;
146
145
  }
147
 
  if (old_action.sa_handler != SIG_IGN){
 
146
  if(old_action.sa_handler != SIG_IGN){
148
147
    ret = sigaction(SIGHUP, &new_action, NULL);
149
148
    if(ret == -1){
150
149
      perror("sigaction");
156
155
    perror("sigaction");
157
156
    return EXIT_FAILURE;
158
157
  }
159
 
  if (old_action.sa_handler != SIG_IGN){
 
158
  if(old_action.sa_handler != SIG_IGN){
160
159
    ret = sigaction(SIGTERM, &new_action, NULL);
161
160
    if(ret == -1){
162
161
      perror("sigaction");
165
164
  }
166
165
  
167
166
  
168
 
  if (debug){
 
167
  if(debug){
169
168
    fprintf(stderr, "Removing echo flag from terminal attributes\n");
170
169
  }
171
170
  
172
171
  t_new = t_old;
173
172
  t_new.c_lflag &= ~ECHO;
174
 
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
 
173
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
175
174
    perror("tcsetattr-echo");
176
175
    return EXIT_FAILURE;
177
176
  }
178
177
 
179
 
  if (debug){
 
178
  if(debug){
180
179
    fprintf(stderr, "Waiting for input from stdin \n");
181
180
  }
182
181
  while(true){
183
 
    if (quit_now){
 
182
    if(quit_now){
184
183
      if(debug){
185
184
        fprintf(stderr, "Interrupted by signal, exiting.\n");
186
185
      }
195
194
      const char *cryptsource = getenv("cryptsource");
196
195
      const char *crypttarget = getenv("crypttarget");
197
196
      const char *const prompt
198
 
        = "Enter passphrase to unlock the disk";
 
197
        = "Enter passphrase to unlock the disk";      
199
198
      if(cryptsource == NULL){
200
199
        if(crypttarget == NULL){
201
200
          fprintf(stderr, "%s: ", prompt);
212
211
      }
213
212
    }
214
213
    ret = getline(&buffer, &n, stdin);
215
 
    if (ret > 0){
 
214
    if(ret > 0){
216
215
      status = EXIT_SUCCESS;
217
216
      /* Make n = data size instead of allocated buffer size */
218
217
      n = (size_t)ret;
233
232
      }
234
233
      break;
235
234
    }
236
 
    if (ret < 0){
237
 
      if (errno != EINTR and not feof(stdin)){
 
235
    if(ret < 0){
 
236
      if(errno != EINTR and not feof(stdin)){
238
237
        perror("getline");
239
238
        status = EXIT_FAILURE;
240
239
        break;
243
242
    /* if(ret == 0), then the only sensible thing to do is to retry to
244
243
       read from stdin */
245
244
    fputc('\n', stderr);
246
 
    if(debug and not quit_now){
247
 
      /* If quit_now is true, we were interrupted by a signal, and
 
245
    if(debug and quit_now == 0){
 
246
      /* If quit_now is nonzero, we were interrupted by a signal, and
248
247
         will print that later, so no need to show this too. */
249
248
      fprintf(stderr, "getline() returned 0, retrying.\n");
250
249
    }
251
250
  }
252
 
 
 
251
  
253
252
  free(buffer);
254
253
  
255
 
  if (debug){
 
254
  if(debug){
256
255
    fprintf(stderr, "Restoring terminal attributes\n");
257
256
  }
258
 
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
 
257
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
259
258
    perror("tcsetattr+echo");
260
259
  }
261
260
  
262
 
  if (debug){
 
261
  if(debug){
263
262
    fprintf(stderr, "%s is exiting with status %d\n", argv[0],
264
263
            status);
265
264
  }