-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptmount.c
1592 lines (1364 loc) · 48.6 KB
/
cryptmount.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* cryptmount - a utility for user-level mounting of encrypted filesystems
* (C)Copyright 2005-2024, RW Penney
*/
/*
This file is part of cryptmount
cryptmount is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
cryptmount is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include <ctype.h>
#include <fcntl.h>
#include <getopt.h>
#include <inttypes.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#ifdef HAVE_SYSLOG
# include <syslog.h>
#endif
#include "armour.h"
#include "cryptmount.h"
#include "delegates.h"
#include "dmutils.h"
#include "fsutils.h"
#include "looputils.h"
#include "tables.h"
#include "utils.h"
#if WITH_CSWAP
# include <sys/swap.h>
#endif
#ifdef TESTING
# include "cmtesting.h"
#endif
/** Record of getuid() output at start of process */
uid_t cm_initial_uid = ~0u;
/**
* An element within a linked-list of filesystem-targets,
* typically supplied from command-line.
*/
typedef struct targelt
{
const tgtdefn_t *tgt;
struct targelt *nx;
} targelt_t;
/** Identifiers of top-level operating mode, and configuration switches
*
* The lower-order bits specify one of a sequence of mutuall-exclusive
* operating modes. The higher-order bits consist of a few control flags,
* some of which may be set implicitly according the the operating mode.
*/
typedef enum {
M_UNSET, M_HELP,
M_PREPARE, M_RELEASE,
M_MOUNT, M_UNMOUNT,
M_SWAPON, M_SWAPOFF,
M_LIST, M_KEYMGRS, M_STATUS,
M_PASSWORD, M_KEYGEN, M_KEYREU,
M_SYSBOOT, M_SYSQUIT,
M_SAFETYNET, M_VERSION,
M_SELFTEST,
M_MODE_MASK = 0x00ff,
F_NEEDS_TGT = 0x0100, /*!< command requires a filesystem target */
F_ALL_TARGETS = 0x0200, /*!< command will be applied to all known targets */
F_NOWARN_ALL = 0x0400, /*!< suppress warnings with '--all' targets */
F_VERIFY_PW = 0x0800 /*!< encryption password should be double-checked */
} cmmode_t;
static int64_t getblk512count(const char *device, int *blklen);
static int execute_list(cmmode_t mode, const tgtdefn_t *tgttable,
const km_pw_context_t *pw_ctxt,
const char *params, const targelt_t *eltlist);
static int do_list(const targelt_t *eltlist);
static int do_status(const targelt_t *eltlist);
static int do_keymgrlist();
static int do_sysboot_updown(int booting, const tgtdefn_t *tgttable,
const km_pw_context_t *pw_ctxt);
static int do_devsetup(const km_pw_context_t *pw_ctxt,
bound_tgtdefn_t *boundtgt, char **mntdev);
static int do_devshutdown(const bound_tgtdefn_t *boundtgt);
static int do_mount(const km_pw_context_t *pw_ctxt, bound_tgtdefn_t *boundtgt);
static int do_unmount(const bound_tgtdefn_t *boundtgt);
static int do_swapon(const km_pw_context_t *pw_ctxt, bound_tgtdefn_t *boundtgt);
static int do_swapoff(const bound_tgtdefn_t *boundtgt);
static int do_passwd(const km_pw_context_t *pw_ctxt, bound_tgtdefn_t *boundtgt);
static int do_keygen(const km_pw_context_t *pw_ctxt, bound_tgtdefn_t *boundtgt,
const char *params, int reuse, const tgtdefn_t *tgttable);
static int do_safetynet();
static const char *USAGE_STRING = N_("\
usage: cryptmount [OPTION [target ...]]\n\
\n\
available options are as follows:\n\
\n\
-h | --help\n\
-a | --all\n\
-c | --change-password <target>\n\
-k | --key-managers\n\
-l | --list\n\
-S | --status\n\
-m | --mount <target>\n\
-u | --unmount <target>\n\
--generate-key <key-size> <target>\n\
--reuse-key <src-target> <dst-target>\n\
--prepare <target>\n\
--release <target>\n\
--config-fd <num>\n\
--passwd-fd <num>\n\
--swapon <target>\n\
--swapoff <target>\n\
--version\n\
\n\
please report bugs to <[email protected]>\n\
");
#ifdef TESTING
cm_testinfo_t test_context;
cm_testinfo_t *test_ctxtptr = &test_context;
int fs_test_blkgetsz()
/** Check that 32bit & 64bit device size calculations agree */
{
#ifdef BLKGETSIZE64
int fd, n_open, seclen;
long len;
uint64_t len64;
const char **dev;
const char *devices[] = {
"/dev/hda", "/dev/hda1", "/dev/hda2", "/dev/hda3",
"/dev/nvme0n1", "/dev/nvme0n2", "/dev/nvme1n1", "/dev/nvme1n2",
"/dev/sda", "/dev/sda1", "/dev/sda2", "/dev/sda3",
"/dev/sdb", "/dev/sdb1", "/dev/sdb2", "/dev/sdb3",
"/dev/vda", "/dev/vda1", "/dev/xvda", "/dev/xvda1",
"/dev/sr0", "/dev/sr1",
NULL };
#endif
CM_TEST_START("BLKGETSIZE ioctl calls");
#ifndef BLKGETSIZE64
/* Assume that there is no ambiguity with BLKGETSIZE */
CM_TEST_PASS();
#else
dev = devices;
n_open = 0;
while (*dev != NULL) {
fd = open(*dev, O_RDONLY);
if (fd >= 0) {
++n_open;
if (ioctl(fd, BLKSSZGET, &seclen) != 0
|| ioctl(fd, BLKGETSIZE, &len) != 0
|| ioctl(fd, BLKGETSIZE64, &len64) != 0) {
CM_TEST_FAIL();
}
close(fd);
if (len64 < (1<<31)) {
CM_ASSERT_EQUAL(len64, ((int64_t)len * (int64_t)512));
}
}
#if 0
if (fd >= 0) fprintf(stderr, "%s: %d %ld %lld\n", *dev, seclen, len * 512, len64);
#endif
++dev;
}
if (n_open > 0) {
CM_TEST_OK();
} else {
CM_TEST_ABORT();
}
#endif /* BLKGETSIZE64 */
}
#endif /* TESTING */
static int64_t getblk512count(const char *device, int *blklen)
/** Find size of raw device in blocks of size 512-bytes */
{ int64_t count = -1;
int fd;
#ifndef BLKGETSIZE64
long len;
#endif
*blklen = 512;
fd = open(device, O_RDONLY);
if (fd < 0) return (int64_t)-1;
#ifdef BLKGETSIZE64
if (ioctl(fd, BLKGETSIZE64, &count) == 0
&& ioctl(fd, BLKSSZGET, blklen) == 0) {
count /= (int64_t)512;
} else {
count = -1;
}
#else
if (ioctl(fd, BLKGETSIZE, &len) == 0) {
/* This directly gives the number of 512-byte blocks */
count = (int64_t)len;
}
#endif
(void)close(fd);
return count;
}
/*! @brief Print list of available filing-system targets to stdout
*
* This provides the back-end functionality for
* the '--list' command-line option.
*/
static int do_list(const targelt_t *eltlist)
{ const targelt_t *elt;
for (elt=eltlist; elt!=NULL; elt=elt->nx) {
const tgtdefn_t *tgt = elt->tgt;
/* TRANSLATORS: this string is marked as 'no-c-format' because
some localizations may require the mount-point and filesystem type
to be printed in a different order, but the untranslated string needs
to remain an ordinary string that can be printed without gettext. */
/* xgettext:no-c-format */
printf(_("%-16s [to mount on \"%s\" as \"%s\"]\n"),
tgt->ident, tgt->dir, tgt->fstype);
}
return ERR_NOERROR;
}
/*! @brief Print mounting status of set of targets to stdout
*
* This provides the back-end functionality for
* the '--status' command-line option.
*/
static int do_status(const targelt_t *eltlist)
{ const targelt_t *elt;
tgtstat_t *tgtstats = NULL, *ts;
tgtstats = get_all_tgtstatus();
for (elt=eltlist; elt!=NULL; elt=elt->nx) {
const tgtdefn_t *tgt = elt->tgt;
for (ts=tgtstats; ts!=NULL; ts=ts->nx) {
if (strcmp(ts->ident, tgt->ident) == 0) break;
}
printf("%-16s %s\n",
tgt->ident, (ts != NULL ? "mounted" : "not_mounted"));
}
free_tgtstatus(tgtstats);
return ERR_NOERROR;
}
/*! @brief Print a list of all available key managers to stdout.
*
* This provides the back-end functionality for
* the '--key-managers' command-line option.
*/
static int do_keymgrlist()
{ const char **keymgrs = get_keymgr_list();
int i = 0;
while (keymgrs[i] != NULL) {
printf("%s", keymgrs[i]);
++i;
if (keymgrs[i] != NULL) {
printf(", ");
} else {
printf("\n");
}
}
free((void*)keymgrs);
return ERR_NOERROR;
}
/*! @brief Setup all devices which have a bootaction option specified
*
* This provides the back-end functionality for
* the '--system-boot' and '--system-shutdown' command-line options.
*
* \see do_mount(), do_swapon(), do_devsetup().
*/
static int do_sysboot_updown(int booting, const tgtdefn_t *tgttable,
const km_pw_context_t *pw_ctxt)
{ const tgtdefn_t *tgt;
bound_tgtdefn_t *boundtgt = NULL;
int eflag = ERR_NOERROR;
for (tgt=tgttable; tgt!=NULL && eflag<ERR_threshold; tgt=tgt->nx) {
const unsigned boot_mode = (tgt->flags & FLG_BOOT_MASK);
if (boot_mode == 0) continue;
boundtgt = bind_tgtdefn(tgt);
if (boundtgt == NULL) continue;
switch (boot_mode) {
case FLG_BOOT_MOUNT:
eflag = (booting ? do_mount(pw_ctxt, boundtgt)
: do_unmount(boundtgt));
break;
case FLG_BOOT_SWAP:
eflag = (booting ? do_swapon(pw_ctxt, boundtgt)
: do_swapoff(boundtgt));
break;
case FLG_BOOT_PREP:
eflag = (booting ? do_devsetup(pw_ctxt, boundtgt, NULL)
: do_devshutdown(boundtgt));
break;
default:
break;
}
free_boundtgt(boundtgt);
}
return eflag;
}
/*! @brief Setup all devices needed to access encrypted target
*
* This will wrap any file-based targets within a loopback device,
* and then setup the device-mapper to provide encryption/decryption
* of the target block device after obtaining the access password
* from the user.
*
* This provides the back-end functionality for
* the '--prepare' command-line option.
*
* \see cm_get_key(), blockify_file(), devmap_create(), do_devshutdown().
*/
static int do_devsetup(const km_pw_context_t *pw_ctxt,
bound_tgtdefn_t *boundtgt, char **mntdev)
{ enum { BUFFMIN=1024 };
uint8_t *key = NULL;
int buffpos, blklen, readonly, isloop = 0, killloop = 0,
keylen = 0, eflag = ERR_NOERROR;
int64_t devlen = 0, fslen = 0;
size_t dpsize;
char *dmparams = NULL;
const char *tgtdev = NULL;
const tgtdefn_t *tgt = NULL;
/* Get crypto-key for filing system: */
eflag = cm_get_key(boundtgt, pw_ctxt, &key, &keylen);
if (eflag != ERR_NOERROR) {
fprintf(stderr, _("Failed to extract cipher key\n"));
goto bail_out;
}
tgt = boundtgt->tgt;
readonly = is_readonlyfs(tgt->dev);
eflag = blockify_file(tgt->dev, (readonly ? O_RDONLY : O_RDWR),
tgt->loopdev, &tgtdev, &isloop);
if (eflag != ERR_NOERROR) {
fprintf(stderr, _("Cannot open device \"%s\" for target \"%s\"\n"),
(tgt->dev != NULL ? tgt->dev : "(NULL)"), tgt->ident);
goto bail_out;
}
/* Get size in blocks of target device: */
devlen = getblk512count(tgtdev, &blklen);
if (devlen < 0) {
fprintf(stderr, _("Failed to get size of \"%s\"\n"), tgtdev);
eflag = ERR_BADIOCTL;
goto bail_out;
}
if (tgt->length < 0 || (tgt->start + tgt->length) > devlen) {
fslen = devlen - tgt->start;
} else {
fslen = tgt->length;
}
if (tgt->start < 0 || fslen <= 0) {
fprintf(stderr,_("Bad device-mapper start/length"));
fprintf(stderr, " (%" PRId64 ",%" PRId64 ")\n",
tgt->start, tgt->length);
eflag = ERR_BADDEVICE;
goto bail_out;
}
/* Setup device-mapper crypt table (CIPHER KEY IV_OFFSET DEV START): */
dpsize = 2 * keylen + BUFFMIN;
dmparams = (char*)sec_realloc(dmparams, dpsize);
buffpos = snprintf(dmparams, dpsize, "%s ",
(tgt->cipher != NULL ? tgt->cipher
: CM_DEFAULT_CIPHER));
buffpos += mk_key_string(key, (size_t)keylen, dmparams + buffpos);
buffpos += snprintf(dmparams + buffpos, (dpsize - buffpos),
" %" PRId64 " %s %" PRId64,
tgt->ivoffset, tgtdev, tgt->start);
if ((tgt->flags & FLG_TRIM) != 0) {
buffpos += snprintf(dmparams + buffpos, (dpsize - buffpos),
" 1 allow_discards");
}
/* Setup device-mapper target: */
eflag = devmap_create(tgt->ident,
(uint64_t)0, (uint64_t)fslen, "crypt", dmparams);
if (eflag != ERR_NOERROR) {
fprintf(stderr,
_("Device-mapper target-creation failed for \"%s\"\n"),
tgt->ident);
killloop = 1;
goto bail_out;
}
if (mntdev != NULL) {
devmap_path(mntdev, tgt->ident);
}
bail_out:
if (killloop) unblockify_file(&tgtdev, isloop); /* mounting failed? */
if (tgtdev) free((void*)tgtdev);
sec_free(dmparams);
sec_free(key);
return eflag;
} /* do_devsetup() */
/*! @brief Remove all devices attached to encrypted target
*
* This will close-down device-mapper and loopback devices
* configured by do_setup().
*
* This provides the back-end functionality for
* the '--release' command-line option.
*
* \see do_devsetup(), devmap_remove(), loop_dellist().
*/
int do_devshutdown(const bound_tgtdefn_t *boundtgt)
{ const tgtdefn_t *tgt = boundtgt->tgt;
struct stat sbuff;
unsigned devcnt = 0;
dev_t *devids = NULL;
int eflag = ERR_NOERROR;
/* Check if filing system has been configured at all: */
if (!is_configured(tgt->ident, NULL)) {
fprintf(stderr, _("Target \"%s\" does not appear to be configured\n"),
tgt->ident);
eflag = WRN_UNCONFIG;
goto bail_out;
}
/* Delay to allow udev to settle */
millisleep(500); /* FIXME - find more deterministic solution! */
/* Find any underlying (e.g. loopback) devices for device-mapper target: */
(void)devmap_dependencies(tgt->ident, &devcnt, &devids);
#ifdef DEBUG
fprintf(stderr, "Shutting down %s [%u dependencies]\n",
tgt->ident, devcnt);
#endif
memset((void*)&sbuff, 0, sizeof(sbuff));
if (stat(tgt->dev, &sbuff) != 0) {
fprintf(stderr, _("Cannot stat \"%s\"\n"), tgt->dev);
sbuff.st_mode |= S_IFREG;
eflag = ERR_BADDEVICE;
}
/* Remove demice-mapper target: */
eflag = devmap_remove(tgt->ident);
if (eflag != ERR_NOERROR) {
fprintf(stderr, _("Failed to remove device-mapper target \"%s\"\n"),
tgt->ident);
}
await_devmap(tgt->ident, 0, 2000);
/* Tidy-up any associated loopback devices: */
if (S_ISREG(sbuff.st_mode) && devids != NULL) {
(void)loop_dellist(devcnt, devids);
}
bail_out:
if (devids != NULL) free((void*)devids);
return eflag;
}
/*! @brief Mount an encrypted filesystem
*
* This provides the back-end functionality for
* the '--mount' command-line option.
*
* \see do_unmount(), do_devsetup(), fs_mount().
*/
static int do_mount(const km_pw_context_t *pw_ctxt, bound_tgtdefn_t *boundtgt)
{ const tgtdefn_t *tgt = NULL;
int freedev = 0, eflag = ERR_NOERROR;
char *mntdev = NULL;
tgtstat_t *tstat;
if (is_mounted(boundtgt->tgt)) {
fprintf(stderr, _("Target \"%s\" is already mounted\n"),
boundtgt->tgt->ident);
eflag = WRN_MOUNTED;
goto bail_out;
}
eflag = do_devsetup(pw_ctxt, boundtgt, &mntdev);
if (eflag != ERR_NOERROR) goto bail_out;
tgt = boundtgt->tgt;
await_device(mntdev, 1, 5000);
#if WITH_FSCK
if ((tgt->flags & FLG_FSCK) != 0) {
const int fsck_flag = fs_check(mntdev, tgt);
if (fsck_flag != ERR_NOERROR) {
freedev = 1; eflag = fsck_flag;
goto bail_out;
}
}
#endif
if (fs_mount(mntdev, tgt) != ERR_NOERROR) {
freedev = 1; eflag = ERR_BADMOUNT;
goto bail_out;
}
tstat = alloc_tgtstatus(tgt);
tstat->uid = (unsigned long)cm_initial_uid;
put_tgtstatus(tgt, tstat);
free_tgtstatus(tstat);
bail_out:
if (freedev) {
/* Tidy-up debris if mount failed */
do_devshutdown(boundtgt);
}
if (mntdev != NULL) free((void*)mntdev);
return eflag;
}
/*! @brief Unmount an encrypted filesystem
*
* This provides the back-end functionality for
* the '--unmount' command-line option.
*
* \see do_mount(), do_devshutdown(), fs_unmount().
*/
static int do_unmount(const bound_tgtdefn_t *boundtgt)
{ const tgtdefn_t *tgt = boundtgt->tgt;
int eflag=ERR_NOERROR;
struct passwd *pwent;
char *mntdev = NULL;
tgtstat_t *tstat = NULL;
/* Check if filing system has been configured at all: */
if (!is_mounted(tgt) || (tstat = get_tgtstatus(tgt)) == NULL) {
fprintf(stderr, _("Target \"%s\" does not appear to be mounted\n"),
tgt->ident);
eflag = WRN_UNCONFIG;
goto bail_out;
}
/* Check if filing system has been mounted & locked by another user: */
if (getuid() != 0 && (uid_t)tstat->uid != cm_initial_uid) {
pwent = getpwuid((uid_t)tstat->uid);
if (pwent != NULL) {
fprintf(stderr, _("Only \"%s\" can unmount \"%s\"\n"),
pwent->pw_name, tgt->ident);
} else {
/* TRANSLATORS: the following expands to include
the *numerical* user-identity in place of '%lu',
e.g. giving 'only user-16 can unmount "target"': */
fprintf(stderr, _("Only user-%lu can unmount \"%s\"\n"),
tstat->uid, tgt->ident);
}
eflag = ERR_BADPRIV;
goto bail_out;
}
/* Unmount filing system: */
if (fs_unmount(tgt) != ERR_NOERROR) {
eflag = ERR_BADMOUNT;
goto bail_out;
}
put_tgtstatus(tgt, NULL);
/* Remove supporting device-mapper target etc */
if (do_devshutdown(boundtgt) != ERR_NOERROR) {
eflag = ERR_BADDEVICE;
}
bail_out:
if (mntdev != NULL) free((void*)mntdev);
if (tstat != NULL) free_tgtstatus(tstat);
return eflag;
}
/*! @brief Setup an encrypted swap partition
*
* This provides the back-end functionality for
* the '--swapon' command-line option.
*
* \see do_swapoff(), do_devsetup(), fs_swapon().
*/
static int do_swapon(const km_pw_context_t *pw_ctxt, bound_tgtdefn_t *boundtgt)
{ const tgtdefn_t *tgt = NULL;
int freedev = 0, eflag = ERR_NOERROR;
char *mntdev = NULL;
tgtstat_t *tstat = NULL;
#if WITH_CSWAP
if (is_configured(boundtgt->tgt->ident, NULL)) {
fprintf(stderr, _("Target \"%s\" is already configured\n"),
boundtgt->tgt->ident);
eflag = WRN_MOUNTED;
goto bail_out;
}
eflag = do_devsetup(pw_ctxt, boundtgt, &mntdev);
if (eflag != ERR_NOERROR) goto bail_out;
tgt = boundtgt->tgt;
await_device(mntdev, 1, 5000);
if (fs_swapon(mntdev, tgt) != ERR_NOERROR) {
freedev = 1;
eflag = ERR_BADSWAP;
goto bail_out;
}
tstat = alloc_tgtstatus(tgt);
tstat->uid = (unsigned long)cm_initial_uid;
put_tgtstatus(tgt, tstat);
free_tgtstatus(tstat);
#else /* !WITH_CSWAP */
fprintf(stderr, _("Crypto-swap is not supported by this installation of cryptmount\n"));
eflag = ERR_BADSWAP;
#endif
bail_out:
if (freedev) {
/* Tidy-up debris if swapon failed */
do_devshutdown(boundtgt);
}
if (mntdev != NULL) free((void*)mntdev);
return eflag;
}
/*! @brief Close down an encrypted swap partition
*
* This provides the back-end functionality for
* the '--swapoff' command-line option.
*
* \see do_swapon(), do_devshutdown(), fs_swapoff().
*/
static int do_swapoff(const bound_tgtdefn_t *boundtgt)
{ const tgtdefn_t *tgt = boundtgt->tgt;
int eflag=ERR_NOERROR;
char *mntdev=NULL;
tgtstat_t *tstat;
#if WITH_CSWAP
/* Check if device has been configured at all: */
if ((tstat = get_tgtstatus(tgt)) == NULL) {
fprintf(stderr, _("Target \"%s\" does not appear to be configured\n"),
tgt->ident);
eflag = WRN_UNCONFIG;
goto bail_out;
}
/* Remove swap-partition: */
if (fs_swapoff(tgt) != ERR_NOERROR) {
eflag = ERR_BADSWAP;
goto bail_out;
}
put_tgtstatus(tgt, NULL);
/* Remove supporting device-mapper target etc */
if (do_devshutdown(boundtgt) != ERR_NOERROR) {
eflag = ERR_BADDEVICE;
}
#else /* !WITH_CSWAP */
fprintf(stderr, _("Crypto-swap is not supported by this installation of cryptmount\n"));
eflag = ERR_BADSWAP;
#endif
bail_out:
if (mntdev != NULL) free((void*)mntdev);
return eflag;
}
/*! @brief Change access password on particular target
*
* This provides the back-end functionality for
* the '--change-password' command-line option.
*
* \see cm_get_key(), cm_put_key().
*/
static int do_passwd(const km_pw_context_t *pw_ctxt, bound_tgtdefn_t *boundtgt)
{ tgtdefn_t *tgt = boundtgt->tgt;
uint8_t *key = NULL;
unsigned keyprops;
int keylen = 0, eflag = ERR_NOERROR;
char *newfname = NULL, *oldfname = NULL;
struct stat sbuff;
size_t sz;
FILE *fp = NULL;
keyprops = cm_get_keyproperties(boundtgt);
if ((keyprops & KM_PROP_HASPASSWD) == 0) {
fprintf(stderr, _("Key-file for \"%s\" isn't password-protected\n"),
tgt->ident);
eflag = WRN_NOPASSWD;
goto bail_out;
}
/* Attempt to read current key: */
eflag = cm_get_key(boundtgt, pw_ctxt, &key, &keylen);
if (eflag != ERR_NOERROR) goto bail_out;
/* Setup location to re-encrypt key: */
if (tgt->key.filename != NULL) {
const char *outfname=NULL;
if ((keyprops & KM_PROP_FIXEDLOC) == 0) {
sz = strlen(tgt->key.filename) + 16;
oldfname = (char*)malloc(2 * sz);
newfname = oldfname + sz;
snprintf(oldfname, sz, "%s-old", tgt->key.filename);
snprintf(newfname, sz, "%s-new", tgt->key.filename);
fp = fopen(newfname, "wb");
outfname = newfname;
} else {
fp = fopen(tgt->key.filename, "r+b");
outfname = tgt->key.filename;
}
if (fp == NULL) {
fprintf(stderr, _("Cannot open \"%s\" for writing\n"), outfname);
eflag = ERR_BADFILE;
goto bail_out;
}
}
eflag = cm_put_key(boundtgt, pw_ctxt, key, keylen, fp);
if (fclose(fp) != 0) eflag = ERR_BADFILE;
if (eflag != ERR_NOERROR) goto bail_out;
/* Replace old key-container with new key-container: */
if (oldfname != NULL && newfname != NULL) {
if (stat(tgt->key.filename, &sbuff) != 0
|| rename(tgt->key.filename, oldfname) != 0
|| chown(oldfname, 0, 0) != 0
|| chmod(oldfname, S_IRUSR | S_IWUSR) != 0) {
fprintf(stderr, _("Retiring old key (%s -> %s) failed\n"),
tgt->key.filename, oldfname);
goto bail_out;
}
if (rename(newfname, tgt->key.filename) != 0
|| chown(tgt->key.filename, sbuff.st_uid, sbuff.st_gid) != 0
|| chmod(tgt->key.filename, sbuff.st_mode) != 0) {
fprintf(stderr, _("Installing new key (%s -> %s) failed\n"),
newfname, tgt->key.filename);
goto bail_out;
}
newfname = NULL;
fprintf(stderr, _("Backup of previous key is in \"%s\"\n"), oldfname);
}
bail_out:
if (newfname != NULL) {
unlink(newfname);
}
if (oldfname != NULL) free((void*)oldfname);
return eflag;
}
/*! @brief Create new filesystem crypto-key
*
* This provides the back-end functionality for
* the '--generate-key' command-line option.
*
* \see cm_generate_key(), cm_put_key().
*/
static int do_keygen(const km_pw_context_t *pw_ctxt, bound_tgtdefn_t *boundtgt,
const char *params, int reuse, const tgtdefn_t *tgttable)
{ uint8_t *key = NULL;
unsigned keyprops;
int keylen = 0, fileexists = 0, eflag = ERR_NOERROR;
char *newfname = NULL;
tgtdefn_t *tgt = boundtgt->tgt;
const tgtdefn_t *parent = NULL;
bound_tgtdefn_t *boundparent = NULL;
size_t sz;
struct stat sbuff;
FILE *fp = NULL;
const unsigned mask_fmtfxd = KM_PROP_FIXEDLOC | KM_PROP_FORMATTED;
if (params != NULL) {
if (reuse) {
parent = get_tgtdefn(tgttable, params);
boundparent = bind_tgtdefn(parent);
if (parent == NULL || boundparent == NULL) {
fprintf(stderr, _("Target name \"%s\" is not recognized\n"),
params);
eflag = ERR_BADPARAM;
}
} else {
if (sscanf(params, "%i", &keylen) != 1 || keylen < 1) {
fprintf(stderr, _("Bad key-length parameter"));
eflag = ERR_BADPARAM;
}
}
}
if (params == NULL || eflag != ERR_NOERROR) goto bail_out;
/* Check if keyfile already exists: */
keyprops = cm_get_keyproperties(boundtgt);
fileexists = (tgt->key.filename != NULL
&& stat(tgt->key.filename, &sbuff) == 0);
if (fileexists && (keyprops & mask_fmtfxd) != KM_PROP_FIXEDLOC) {
fprintf(stderr,_("Key-file \"%s\" already exists for target \"%s\"\n"),
tgt->key.filename, tgt->ident);
eflag = ERR_BADFILE;
goto bail_out;
}
/* Assemble new key material: */
if (reuse) {
eflag = cm_get_key(boundparent, pw_ctxt, &key, &keylen);
} else {
fprintf(stderr, _("Generating random key; please be patient...\n"));
key = (uint8_t*)sec_realloc(NULL, (size_t)keylen);
eflag = cm_generate_key(key, (size_t)keylen);
if (eflag != ERR_NOERROR) {
fprintf(stderr, _("Failed to generate new key\n"));
goto bail_out;
}
}
/* Setup location for new key: */
if (tgt->key.filename != NULL) {
const char *outfname=NULL;
if ((keyprops & KM_PROP_FIXEDLOC) == 0) {
sz = strlen(tgt->key.filename) + 16;
newfname = (char*)malloc(sz);
snprintf(newfname, sz, "%s-new", tgt->key.filename);
fp = fopen(newfname, "wb");
outfname = newfname;
} else {
fp = fopen(tgt->key.filename, (fileexists ? "r+b" : "wb"));
outfname = tgt->key.filename;
}
if (fp == NULL) {
fprintf(stderr, _("Cannot open \"%s\" for writing\n"), outfname);
eflag = ERR_BADFILE;
goto bail_out;
}
}
eflag = cm_put_key(boundtgt, pw_ctxt, key, keylen, fp);
if (fp != NULL && fclose(fp) != 0) eflag = ERR_BADFILE;
if (eflag != ERR_NOERROR) goto bail_out;
/* Move new key file into prefered location: */
if (newfname != NULL) {
if (rename(newfname, tgt->key.filename) != 0
|| chown(tgt->key.filename, 0, 0) != 0
|| chmod(tgt->key.filename, S_IRUSR | S_IWUSR) != 0) {
fprintf(stderr, _("Installation of new keyfile \"%s\" failed"),
tgt->key.filename);
eflag = ERR_BADFILE;
}
free((void*)newfname);
newfname = NULL;
}
bail_out:
if (newfname != NULL) {
unlink(newfname);
free((void*)newfname);
}
if (key != NULL) sec_free((void*)key);
if (boundparent != NULL) free_boundtgt(boundparent);
return eflag;
}
/*! @brief Attempt to unmount/shutdown all targets currently mounted.
*
* This will identify all filesystems listed in cryptmount's own
* mounting table (typically /run/cryptmount.status), and try
* to unmount, or de-configure, any targets that seem still to be in use.
*
* This will generally only be useful during a system shutdown,
* to provide a safety mechanism for filesystems that have
* not been unmounted normally. Accordingly, this routine
* is more aggressive in its approach than do_unmount(), do_swapoff(), etc.
*
* This provides the back-end functionality for
* the '--safetynet' command-line option.
*/
static int do_safetynet()
{ tgtstat_t *all_tsts=NULL, *tst;
char *devname=NULL;
const char *old_ident=NULL;
tgtdefn_t *tgt=NULL;
dev_t *devids=NULL;
unsigned devcnt=0;
int mflag;
struct {
unsigned targets, unmounted, unswapped, undeviced, unlooped; }
counts = {
0, 0, 0, 0, 0 };
/* This routine is ugly, but may be the best we can do to prevent
* damage to filesystems that should have been properly removed
* by other mechanisms (e.g. --unmount, --swapoff) */
tgt = alloc_tgtdefn(NULL);
old_ident = tgt->ident;
/* Get list of all targets in status-file: */
all_tsts = get_all_tgtstatus();
for (tst=all_tsts; tst!=NULL; tst=tst->nx) {
++counts.targets;
devmap_path(&devname, tst->ident);
tgt->ident = tst->ident;
#ifdef DLGT_UMOUNT