/* * OLD OBSOLETE libcryptsetup API example * DO NOT USE FOR NEW PROJECTS. */ #include #include #include "libcryptsetup.h" /* definition was introduced together with log */ #ifdef CRYPT_LOG_NORMAL #define HAVE_ICB #endif #ifdef HAVE_ICB static int yesDialog(char *msg) { printf("You are sure, I know.\n"); return 1; } static void cmdLineLog(int class, char *msg) { fputs(msg, stdout); } static struct interface_callbacks cmd_icb = { .yesDialog = yesDialog, .log = cmdLineLog, }; #endif int LuksOpen(const char *device, const char *name) { struct crypt_options co = { .device = device, .name = name, // .key_file = "-", // standard input, you can echo -n"xxx"|prg // or use key file. API cannot give password directly .tries = 1, #ifdef HAVE_ICB .icb = &cmd_icb, #endif }; return crypt_luksOpen(&co); } // r == 0 inactive, r > 0 active (r== opencount), othewise error (-EINVAL, -ENODEV, -EBUSY) int LuksClose(const char *name) { struct crypt_options co = { .name = name, #ifdef HAVE_ICB .icb = &cmd_icb, #endif }; return crypt_remove_device(&co); } void print_error() { char buf[256]; crypt_get_error(buf, sizeof(buf)); printf("%s\n", buf); } int main (int argc, char *argv[]) { const char *device = "/dev/loop0"; const char *name = "xxx"; // let's call me /dev/mapper/xxx int r; r = LuksOpen(device, name); if (r) print_error(); r = LuksClose(name); if (r) print_error(); return r ? 0 : 1; }