~siretart/cryptsetup/debian

« back to all changes in this revision

Viewing changes to src/cryptsetup.c

  • Committer: Reinhard Tartler
  • Date: 2008-05-25 17:39:22 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: siretart@tauware.de-20080525173922-popbnh1gtbt3rxis
merge new upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
#include <stdint.h>
5
5
#include <inttypes.h>
6
6
#include <errno.h>
 
7
#include <unistd.h>
7
8
#include <assert.h>
8
9
 
9
10
#include <libcryptsetup.h>
39
40
static int action_status(int arg);
40
41
static int action_luksFormat(int arg);
41
42
static int action_luksOpen(int arg);
 
43
static int action_luksAddKey(int arg);
42
44
static int action_luksDelKey(int arg);
43
 
static int action_luksAddKey(int arg);
 
45
static int action_luksKillSlot(int arg);
 
46
static int action_luksRemoveKey(int arg);
44
47
static int action_isLuks(int arg);
45
48
static int action_luksUUID(int arg);
46
49
static int action_luksDump(int arg);
60
63
        { "status",     action_status, 0, 1, N_("<name>"), N_("show device status") },
61
64
        { "luksFormat", action_luksFormat, 0, 1, N_("<device> [<new key file>]"), N_("formats a LUKS device") },
62
65
        { "luksOpen",   action_luksOpen, 0, 2, N_("<device> <name> "), N_("open LUKS device as mapping <name>") },
63
 
        { "luksDelKey", action_luksDelKey, 0, 2, N_("<device> <key slot>"), N_("wipes key with number <key slot> from LUKS device") },
64
66
        { "luksAddKey", action_luksAddKey, 0, 1, N_("<device> [<new key file>]"), N_("add key to LUKS device") },
 
67
        { "luksRemoveKey", action_luksRemoveKey, 0, 1, N_("<device> [<key file]]"), N_("removes supplied key or key file from LUKS device") },
 
68
        { "luksKillSlot",  action_luksKillSlot, 0, 2, N_("<device> <key slot>"), N_("wipes key with number <key slot> from LUKS device") },
 
69
        { "luksDelKey",  action_luksDelKey, 0, 2, N_("<device> <key slot>"), N_("identical to luksKillSlot, but deprecated action name") },
65
70
        { "luksUUID",   action_luksUUID, 0, 1, N_("<device>"), N_("print UUID of LUKS device") },
66
71
        { "isLuks",     action_isLuks, 0, 1, N_("<device>"), N_("tests <device> for LUKS partition header") },
67
72
        { "luksClose",  action_remove, 0, 1, N_("<name>"), N_("remove LUKS mapping") },
69
74
        { NULL, NULL, 0, 0, NULL }
70
75
};
71
76
 
 
77
/* Interface Callbacks */
 
78
static int yesDialog(char *msg)
 
79
{
 
80
        int r = 0;
 
81
        if(isatty(0) && !opt_batch_mode) {
 
82
                char *answer=NULL;
 
83
                size_t size=0;
 
84
                fprintf(stderr,"\nWARNING!\n========\n");
 
85
                fprintf(stderr,"%s\n\nAre you sure? (Type uppercase yes): ",msg);
 
86
                if(getline(&answer,&size,stdin) == -1)
 
87
                        return 0;
 
88
                if(strcmp(answer,"YES\n") == 0)
 
89
                        r = 1;
 
90
                free(answer);
 
91
        } else
 
92
                r = 1;
 
93
        return r;
 
94
}
 
95
 
 
96
static void cmdLineLog(int class, char *msg) {
 
97
    switch(class) {
 
98
 
 
99
    case CRYPT_LOG_NORMAL:
 
100
            fputs(msg, stdout);
 
101
            break;
 
102
    case CRYPT_LOG_ERROR:
 
103
            fputs(msg, stderr);
 
104
            break;
 
105
    default:
 
106
            fprintf(stderr, "Internal error on logging class for msg: %s", msg);
 
107
            break;
 
108
    }
 
109
}
 
110
 
 
111
static struct interface_callbacks cmd_icb = {
 
112
        .yesDialog = yesDialog,
 
113
        .log = cmdLineLog,
 
114
};
 
115
 
 
116
/* End ICBs */
 
117
 
72
118
static void show_status(int errcode)
73
119
{
74
120
        char error[256];
75
121
 
76
122
        if(!errcode) {
77
 
          fprintf(stderr, _("Command successful.\n"));
78
 
          return;
 
123
                fprintf(stderr, _("Command successful.\n"));
 
124
                return;
79
125
        }
80
126
 
81
127
        crypt_get_error(error, sizeof(error));
110
156
                .offset = opt_offset,
111
157
                .skip = opt_skip,
112
158
                .timeout = opt_timeout,
113
 
                .tries = opt_tries
 
159
                .tries = opt_tries,
 
160
                .icb = &cmd_icb,
114
161
        };
115
162
        int r;
116
163
 
134
181
{
135
182
        struct crypt_options options = {
136
183
                .name = action_argv[0],
 
184
                .icb = &cmd_icb,
137
185
        };
138
186
        int r;
139
187
 
148
196
        struct crypt_options options = {
149
197
                .name = action_argv[0],
150
198
                .size = opt_size,
 
199
                .icb = &cmd_icb,
151
200
        };
152
201
        int r;
153
202
 
161
210
{
162
211
        struct crypt_options options = {
163
212
                .name = action_argv[0],
 
213
                .icb = &cmd_icb,
164
214
        };
165
215
        int r;
166
216
 
191
241
        return r;
192
242
}
193
243
 
194
 
static int yesDialog(char *msg)
195
 
{
196
 
        int r = 0;
197
 
        if(isatty(0) && !opt_batch_mode) {
198
 
                char *answer=NULL;
199
 
                size_t size=0;
200
 
                fprintf(stderr,"\nWARNING!\n========\n");
201
 
                fprintf(stderr,"%s\n\nAre you sure? (Type uppercase yes): ",msg);
202
 
                getline(&answer,&size,stdin);
203
 
                if(strcmp(answer,"YES\n") == 0)
204
 
                        r = 1;
205
 
                free(answer);
206
 
        } else
207
 
                r = 1;
208
 
        return r;
209
 
}
210
 
 
211
244
static int action_luksFormat(int arg)
212
245
{
213
246
        struct crypt_options options = {
219
252
                .iteration_time = opt_iteration_time,
220
253
                .timeout = opt_timeout,
221
254
                .align_payload = opt_align_payload,
 
255
                .icb = &cmd_icb,
222
256
        };
223
257
 
224
258
        int r = 0; char *msg = NULL;
240
274
                .device = action_argv[0],
241
275
                .key_file = opt_key_file,
242
276
                .timeout = opt_timeout,
243
 
                .tries = opt_tries
 
277
                .tries = opt_tries,
 
278
                .icb = &cmd_icb,
244
279
        };
245
280
        int r; 
246
281
 
255
290
 
256
291
static int action_luksDelKey(int arg)
257
292
{
 
293
    fprintf(stderr,"luksDelKey is a deprecated action name.\nPlease use luksKillSlot that is completely identicaly (but more clear in naming)\n"); 
 
294
    return action_luksKillSlot(arg);
 
295
}
 
296
 
 
297
static int action_luksKillSlot(int arg)
 
298
{
258
299
        struct crypt_options options = {
259
300
                .device = action_argv[0],
260
301
                .key_slot = atoi(action_argv[1]),
261
302
                .key_file = opt_key_file,
262
303
                .timeout = opt_timeout,
263
304
                .flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
264
 
        };
265
 
        int r; 
266
 
 
267
 
        opt_verbose = 1;
268
 
        if(LUKS_is_last_keyslot(options.device, options.key_slot) && 
269
 
           !yesDialog(_("This is the last keyslot. Device will become unusable after purging this key."))) {
270
 
                r = -EINVAL;
271
 
        } else {
272
 
                r = crypt_luksDelKey(&options);
273
 
        }
 
305
                .icb = &cmd_icb,
 
306
        };
 
307
        int r; 
 
308
 
 
309
        opt_verbose = 1;
 
310
        r = crypt_luksKillSlot(&options);
 
311
        show_status(-r);
 
312
        return r;
 
313
}
 
314
 
 
315
static int action_luksRemoveKey(int arg)
 
316
{
 
317
        struct crypt_options options = {
 
318
                .device = action_argv[0],
 
319
                .new_key_file = action_argc>1?action_argv[1]:NULL,
 
320
                .key_file = opt_key_file,
 
321
                .timeout = opt_timeout,
 
322
                .flags = !opt_batch_mode?CRYPT_FLAG_VERIFY_ON_DELKEY : 0,
 
323
                .icb = &cmd_icb,
 
324
        };
 
325
        int r; 
 
326
 
 
327
        opt_verbose = 1;
 
328
        r = crypt_luksRemoveKey(&options);
274
329
        show_status(-r);
275
330
        return r;
276
331
}
284
339
                .flags = opt_verify_passphrase ? CRYPT_FLAG_VERIFY : (!opt_batch_mode?CRYPT_FLAG_VERIFY_IF_POSSIBLE : 0),
285
340
                .iteration_time = opt_iteration_time,
286
341
                .timeout = opt_timeout,
 
342
                .icb = &cmd_icb,
287
343
        };
288
344
        int r; 
289
345
 
297
353
{
298
354
        struct crypt_options options = {
299
355
                .device = action_argv[0],
 
356
                .icb = &cmd_icb,
300
357
        };
301
358
        return crypt_isLuks(&options);
302
359
}
305
362
{
306
363
        struct crypt_options options = {
307
364
                .device = action_argv[0],
 
365
                .icb = &cmd_icb,
308
366
        };
309
 
        int r; 
 
367
        int r;
310
368
 
311
369
        r = crypt_luksUUID(&options);
312
370
        if (r < 0)
318
376
{
319
377
        struct crypt_options options = {
320
378
                .device = action_argv[0],
 
379
                .icb = &cmd_icb,
321
380
        };
322
381
        int r; 
323
382
 
473
532
        }       
474
533
        return action->handler(action->arg);
475
534
}
 
535
 
 
536
// Local Variables:
 
537
// c-basic-offset: 8
 
538
// indent-tabs-mode: nil
 
539
// End: