/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

  • Committer: Teddy Hogeborn
  • Date: 2008-08-16 16:58:31 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080816165831-cx018vo5i9oqmgo0
* plugins.d/password-request.c (main): Include environment variables
                                       "cryptsource" and "crypttarget"
                                       in password prompt.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*  -*- coding: utf-8 -*- */
2
2
/*
3
3
 * Passprompt - Read a password from the terminal and print it
4
 
 * 
5
 
 * Copyright © 2008 Teddy Hogeborn
6
 
 * Copyright © 2008 Björn Påhlsson
 
4
 *
 
5
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
7
6
 * 
8
7
 * This program is free software: you can redistribute it and/or
9
8
 * modify it under the terms of the GNU General Public License as
53
52
 
54
53
volatile bool quit_now = false;
55
54
bool debug = false;
56
 
const char *argp_program_version = "password-prompt " VERSION;
 
55
const char *argp_program_version = "password-prompt 1.0";
57
56
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
58
57
 
59
58
static void termination_handler(__attribute__((unused))int signum){
74
73
    struct argp_option options[] = {
75
74
      { .name = "prefix", .key = 'p',
76
75
        .arg = "PREFIX", .flags = 0,
77
 
        .doc = "Prefix shown before the prompt", .group = 2 },
 
76
        .doc = "Prefix used before the passprompt", .group = 2 },
78
77
      { .name = "debug", .key = 128,
79
78
        .doc = "Debug mode", .group = 3 },
80
79
      { .name = NULL }
103
102
  
104
103
    struct argp argp = { .options = options, .parser = parse_opt,
105
104
                         .args_doc = "",
106
 
                         .doc = "Mandos password-prompt -- Read and"
107
 
                         " output a password" };
 
105
                         .doc = "Mandos Passprompt -- Provides a passprompt" };
108
106
    ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
109
107
    if (ret == ARGP_ERR_UNKNOWN){
110
108
      fprintf(stderr, "Unknown error while parsing arguments\n");
120
118
  }
121
119
  
122
120
  if (tcgetattr(STDIN_FILENO, &t_old) != 0){
123
 
    perror("tcgetattr");
124
121
    return EXIT_FAILURE;
125
122
  }
126
123
  
182
179
  }
183
180
  while(true){
184
181
    if (quit_now){
185
 
      if(debug){
186
 
        fprintf(stderr, "Interrupted by signal, exiting.\n");
187
 
      }
188
182
      status = EXIT_FAILURE;
189
183
      break;
190
184
    }
214
208
    }
215
209
    ret = getline(&buffer, &n, stdin);
216
210
    if (ret > 0){
 
211
      fprintf(stdout, "%s", buffer);
217
212
      status = EXIT_SUCCESS;
218
 
      /* Make n = data size instead of allocated buffer size */
219
 
      n = (size_t)ret;
220
 
      /* Strip final newline */
221
 
      if(n>0 and buffer[n-1] == '\n'){
222
 
        buffer[n-1] = '\0';     /* not strictly necessary */
223
 
        n--;
224
 
      }
225
 
      size_t written = 0;
226
 
      while(written < n){
227
 
        ret = write(STDOUT_FILENO, buffer + written, n - written);
228
 
        if(ret < 0){
229
 
          perror("write");
230
 
          status = EXIT_FAILURE;
231
 
          break;
232
 
        }
233
 
        written += (size_t)ret;
234
 
      }
235
213
      break;
236
214
    }
237
215
    if (ret < 0){
244
222
    /* if(ret == 0), then the only sensible thing to do is to retry to
245
223
       read from stdin */
246
224
    fputc('\n', stderr);
247
 
    if(debug and not quit_now){
248
 
      /* If quit_now is true, we were interrupted by a signal, and
249
 
         will print that later, so no need to show this too. */
250
 
      fprintf(stderr, "getline() returned 0, retrying.\n");
251
 
    }
252
225
  }
253
 
 
254
 
  free(buffer);
255
226
  
256
227
  if (debug){
257
228
    fprintf(stderr, "Restoring terminal attributes\n");
261
232
  }
262
233
  
263
234
  if (debug){
264
 
    fprintf(stderr, "%s is exiting with status %d\n", argv[0],
265
 
            status);
266
 
  }
267
 
  if(status == EXIT_SUCCESS){
268
 
    fputc('\n', stderr);
 
235
    fprintf(stderr, "%s is exiting\n", argv[0]);
269
236
  }
270
237
  
271
238
  return status;