/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-11-09 07:35:16 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091109073516-v1vem352uz0vuwrd
* dbus-mandos.conf: New; to be copied to
                    "/etc/dbus-1/system.d/mandos.conf".

Show diffs side-by-side

added added

removed removed

Lines of Context:
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,
40
 
                                   getopt_long, getenv() */
 
40
                                   getenv() */
41
41
#include <stdio.h>              /* fprintf(), stderr, getline(),
42
 
                                   stdin, feof(), perror(), fputc(),
43
 
                                   getopt_long */
 
42
                                   stdin, feof(), perror(), fputc()
 
43
                                */
44
44
#include <errno.h>              /* errno, EBADF, ENOTTY, EINVAL,
45
45
                                   EFAULT, EFBIG, EIO, ENOSPC, EINTR
46
46
                                */
86
86
        .doc = "Prefix shown before the prompt", .group = 2 },
87
87
      { .name = "debug", .key = 128,
88
88
        .doc = "Debug mode", .group = 3 },
 
89
      /*
 
90
       * These reproduce what we would get without ARGP_NO_HELP
 
91
       */
 
92
      { .name = "help", .key = '?',
 
93
        .doc = "Give this help list", .group = -1 },
 
94
      { .name = "usage", .key = -3,
 
95
        .doc = "Give a short usage message", .group = -1 },
 
96
      { .name = "version", .key = 'V',
 
97
        .doc = "Print program version", .group = -1 },
89
98
      { .name = NULL }
90
99
    };
91
100
    
92
101
    error_t parse_opt (int key, char *arg, struct argp_state *state){
 
102
      errno = 0;
93
103
      switch (key){
94
104
      case 'p':
95
105
        prefix = arg;
97
107
      case 128:
98
108
        debug = true;
99
109
        break;
100
 
      case ARGP_KEY_ARG:
101
 
        argp_usage(state);
102
 
        break;
103
 
      case ARGP_KEY_END:
 
110
        /*
 
111
         * These reproduce what we would get without ARGP_NO_HELP
 
112
         */
 
113
      case '?':                 /* --help */
 
114
        argp_state_help(state, state->out_stream,
 
115
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
 
116
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
 
117
      case -3:                  /* --usage */
 
118
        argp_state_help(state, state->out_stream,
 
119
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
 
120
      case 'V':                 /* --version */
 
121
        fprintf(state->out_stream, "%s\n", argp_program_version);
 
122
        exit(argp_err_exit_status);
104
123
        break;
105
124
      default:
106
125
        return ARGP_ERR_UNKNOWN;
107
126
      }
108
 
      return 0;
 
127
      return errno;
109
128
    }
110
129
    
111
130
    struct argp argp = { .options = options, .parser = parse_opt,
112
131
                         .args_doc = "",
113
132
                         .doc = "Mandos password-prompt -- Read and"
114
133
                         " output a password" };
115
 
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
116
 
    if(ret == ARGP_ERR_UNKNOWN){
117
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
118
 
      return EX_SOFTWARE;
 
134
    ret = argp_parse(&argp, argc, argv,
 
135
                     ARGP_IN_ORDER | ARGP_NO_HELP, NULL, NULL);
 
136
    switch(ret){
 
137
    case 0:
 
138
      break;
 
139
    case ENOMEM:
 
140
    default:
 
141
      errno = ret;
 
142
      perror("argp_parse");
 
143
      return EX_OSERR;
 
144
    case EINVAL:
 
145
      return EX_USAGE;
119
146
    }
120
147
  }
121
148
  
231
258
      fprintf(stderr, "%s ", prefix);
232
259
    }
233
260
    {
234
 
      const char *cryptsource = getenv("cryptsource");
235
 
      const char *crypttarget = getenv("crypttarget");
236
 
      const char *const prompt
237
 
        = "Enter passphrase to unlock the disk";
 
261
      const char *cryptsource = getenv("CRYPTTAB_SOURCE");
 
262
      const char *crypttarget = getenv("CRYPTTAB_NAME");
 
263
      /* Before cryptsetup 1.1.0~rc2 */
 
264
      if(cryptsource == NULL){
 
265
        cryptsource = getenv("cryptsource");
 
266
      }
 
267
      if(crypttarget == NULL){
 
268
        crypttarget = getenv("crypttarget");
 
269
      }
 
270
      const char *const prompt1 = "Unlocking the disk";
 
271
      const char *const prompt2 = "Enter passphrase";
238
272
      if(cryptsource == NULL){
239
273
        if(crypttarget == NULL){
240
 
          fprintf(stderr, "%s: ", prompt);
 
274
          fprintf(stderr, "%s to unlock the disk: ", prompt2);
241
275
        } else {
242
 
          fprintf(stderr, "%s (%s): ", prompt, crypttarget);
 
276
          fprintf(stderr, "%s (%s)\n%s: ", prompt1, crypttarget,
 
277
                  prompt2);
243
278
        }
244
279
      } else {
245
280
        if(crypttarget == NULL){
246
 
          fprintf(stderr, "%s %s: ", prompt, cryptsource);
 
281
          fprintf(stderr, "%s %s\n%s: ", prompt1, cryptsource,
 
282
                  prompt2);
247
283
        } else {
248
 
          fprintf(stderr, "%s %s (%s): ", prompt, cryptsource,
249
 
                  crypttarget);
 
284
          fprintf(stderr, "%s %s (%s)\n%s: ", prompt1, cryptsource,
 
285
                  crypttarget, prompt2);
250
286
        }
251
287
      }
252
288
    }