use set_error instead of printf in library to report errors. Signed-off-by: Ludwig Nussel Index: cryptsetup-luks-1.0.4_SVN30/lib/setup.c =================================================================== --- cryptsetup-luks-1.0.4_SVN30.orig/lib/setup.c +++ cryptsetup-luks-1.0.4_SVN30/lib/setup.c @@ -96,7 +96,7 @@ static int timed_read(int fd, char *pass if (select(fd+1, &fds, NULL, NULL, &t) > 0) failed = untimed_read(fd, pass, maxlen); else - fprintf(stderr, "Operation timed out.\n"); + set_error("Operation timed out"); return failed; } @@ -195,14 +195,16 @@ static int get_key(struct crypt_options pass = safe_alloc(512); if (!pass || (i = interactive_pass(prompt, pass, 512, options->timeout))) { - set_error("Error reading passphrase"); + if(!get_error()) + set_error("Error reading passphrase"); goto out_err; } if (verify || verify_if_possible) { char pass_verify[512]; i = interactive_pass("Verify passphrase: ", pass_verify, sizeof(pass_verify), options->timeout); if (i || strcmp(pass, pass_verify) != 0) { - set_error("Passphrases do not match"); + if(!get_error()) + set_error("Passphrases do not match"); goto out_err; } memset(pass_verify, 0, sizeof(pass_verify)); @@ -383,7 +385,7 @@ static int parse_into_name_and_mode(cons strncpy(mode,"cbc-plain",10); } else { - fprintf(stderr, "no known cipher-spec pattern detected\n"); + set_error("no known cipher-spec pattern detected"); return -EINVAL; } } @@ -642,7 +644,6 @@ start: r = -EINVAL; goto out; } if((r = LUKS_open_any_key(options->device, password, passwordLen, &hdr, &mk, backend)) < 0) { - set_error("No key available with this passphrase.\n"); goto out1; } @@ -716,7 +717,7 @@ static int __crypt_luks_add_key(int arg, r = -EINVAL; goto out; } if(LUKS_open_any_key(device, password, passwordLen, &hdr, &mk, backend) < 0) { - printf("No key available with this passphrase.\n"); + set_error("No key available with this passphrase"); r = -EPERM; goto out; } safe_free(password); @@ -759,7 +760,7 @@ static int __crypt_luks_del_key(int arg, } openedIndex = LUKS_open_any_key(device, password, passwordLen, &hdr, &mk, backend); if(openedIndex < 0 || keyIndex == openedIndex) { - printf("No remaining key available with this passphrase.\n"); + set_error("No remaining key available with this passphrase"); r = -EPERM; goto out; } } Index: cryptsetup-luks-1.0.4_SVN30/luks/keymanage.c =================================================================== --- cryptsetup-luks-1.0.4_SVN30.orig/luks/keymanage.c +++ cryptsetup-luks-1.0.4_SVN30/luks/keymanage.c @@ -76,20 +76,20 @@ int LUKS_read_phdr(const char *device, s devfd = open(device,O_RDONLY | O_DIRECT | O_SYNC); if(-1 == devfd) { - fprintf(stderr, _("Can't open device: %s\n"), device); + set_error(_("Can't open device %s"), device); return -EINVAL; } if(read_blockwise(devfd, hdr, sizeof(struct luks_phdr)) < sizeof(struct luks_phdr)) { r = -EIO; } else if(memcmp(hdr->magic, luksMagic, LUKS_MAGIC_L)) { /* Check magic */ - fprintf(stderr, _("%s is not a LUKS partition\n"), device); + set_error(_("%s is not a LUKS partition\n"), device); r = -EINVAL; } else if(memcmp(hdr->hashSpec, "sha1", 4)) { /* Check for SHA1 - other hashspecs are not implemented ATM */ - fputs(_("unknown hash spec in phdr"), stderr); + set_error(_("unknown hash spec in phdr")); r = -EINVAL; } else if((hdr->version = ntohs(hdr->version)) != 1) { /* Convert every uint16/32_t item from network byte order */ - fprintf(stderr, _("unknown version %d\n"), hdr->version); + set_error( _("unknown LUKS version %d\n"), hdr->version); r = -EINVAL; } else { hdr->payloadOffset = ntohl(hdr->payloadOffset); @@ -117,7 +117,7 @@ int LUKS_write_phdr(const char *device, devfd = open(device,O_RDWR | O_DIRECT | O_SYNC); if(-1 == devfd) { - fprintf(stderr, _("Can't open device: %s\n"), device); + set_error(_("Can't open device %s"), device); return -EINVAL; } @@ -210,12 +210,12 @@ int LUKS_set_key(const char *device, uns int r; if(hdr->keyblock[keyIndex].active != LUKS_KEY_DISABLED) { - fprintf(stderr, _("key %d active, purge first.\n"), keyIndex); + set_error( _("key %d active, purge first"), keyIndex); return -EINVAL; } if(hdr->keyblock[keyIndex].stripes < LUKS_STRIPES) { - fprintf(stderr,_("key material section %d includes too few stripes. Header manipulation?\n"),keyIndex); + set_error(_("key material section %d includes too few stripes. Header manipulation?"),keyIndex); return -EINVAL; } r = getRandom(hdr->keyblock[keyIndex].passwordSalt, LUKS_SALTSIZE); @@ -247,7 +247,8 @@ int LUKS_set_key(const char *device, uns hdr->keyblock[keyIndex].keyMaterialOffset, backend); if(r < 0) { - fprintf(stderr,"Failed to write to key storage.\n"); + if(!get_error()) + set_error("Failed to write to key storage"); goto out; } @@ -303,7 +304,8 @@ int LUKS_open_key(const char *device, hdr->keyblock[keyIndex].keyMaterialOffset, backend); if(r < 0) { - fprintf(stderr,"Failed to read from key storage\n"); + if(!get_error()) + set_error("Failed to read from key storage"); goto out; } @@ -387,7 +389,7 @@ static int wipe(const char *device, unsi devfd = open(device, O_RDWR | O_DIRECT); if(devfd == -1) { - fprintf(stderr, _("Can't open device: %s\n"), device); + set_error(_("Can't open device %s"), device); return -EINVAL; } Index: cryptsetup-luks-1.0.4_SVN30/lib/utils.c =================================================================== --- cryptsetup-luks-1.0.4_SVN30.orig/lib/utils.c +++ cryptsetup-luks-1.0.4_SVN30/lib/utils.c @@ -226,7 +226,7 @@ ssize_t read_blockwise(int fd, void *ori while(count) { r = read(fd,padbuf,bsize); if(r < 0 || r != bsize) { - fprintf(stderr, "read failed in read_blockwise.\n"); + set_error("read failed in read_blockwise()"); goto out; } step = countsize = round_up_modulo(srcLength,device_sector_size)/SECTOR_SIZE; @@ -121,7 +121,8 @@ static int LUKS_endec_template(char *src r = setup_mapping(dmCipherSpec,name,device,hdr->payloadOffset,key,keyLength,sector,srcLength,backend,mode); if(r < 0) { - fprintf(stderr,"Failed to setup dm-crypt key mapping.\nCheck kernel for support for the %s cipher spec and verify that %s contains at least %d sectors.\n", + if(!get_error()) + set_error("Failed to setup dm-crypt key mapping.\nCheck kernel for support for the %s cipher spec and verify that %s contains at least %d sectors", dmCipherSpec, device, sector + div_round_up(srcLength,SECTOR_SIZE));