cleaned code
removed std namespace initialized variables cleaned endl added const for constants tagged new version 1.0.8
This commit is contained in:
15
configure.ac
15
configure.ac
@@ -1,4 +1,4 @@
|
||||
AC_INIT([stenc], [1.0.8])
|
||||
AC_INIT([stenc],[1.0.8])
|
||||
AC_CONFIG_SRCDIR([src/main.cpp])
|
||||
AM_INIT_AUTOMAKE([foreign])
|
||||
|
||||
@@ -9,7 +9,15 @@ AC_CHECK_HEADER([sys/machine.h])
|
||||
AC_PROG_CXX
|
||||
|
||||
# Checks for header files.
|
||||
AC_HEADER_STDC
|
||||
m4_warn([obsolete],
|
||||
[The preprocessor macro `STDC_HEADERS' is obsolete.
|
||||
Except in unusual embedded environments, you can safely include all
|
||||
ISO C90 headers unconditionally.])dnl
|
||||
# Autoupdate added the next two lines to ensure that your configure
|
||||
# script's behavior did not change. They are probably safe to remove.
|
||||
AC_CHECK_INCLUDES_DEFAULT
|
||||
AC_PROG_EGREP
|
||||
|
||||
|
||||
|
||||
AC_MSG_CHECKING(whether to output raw SCSI messages)
|
||||
@@ -93,5 +101,6 @@ fi
|
||||
|
||||
|
||||
|
||||
AC_OUTPUT(Makefile src/Makefile man/Makefile)
|
||||
AC_CONFIG_FILES([Makefile src/Makefile man/Makefile])
|
||||
AC_OUTPUT
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <sstream>
|
||||
#include "keyinfo.h"
|
||||
#include "scsiencrypt.h"
|
||||
using namespace std;
|
||||
//using namespace std;
|
||||
|
||||
Keyinfo::Keyinfo(){
|
||||
valid=false;
|
||||
@@ -13,11 +13,11 @@ Keyinfo::Keyinfo(){
|
||||
key=NULL;
|
||||
keySize=0;
|
||||
}
|
||||
void Keyinfo::load(string hexinput){
|
||||
void Keyinfo::load(std::string hexinput){
|
||||
valid=true;
|
||||
if(hexinput.size()<2){
|
||||
valid=false;
|
||||
cout<<"Key input too short!"<<endl;
|
||||
std::cout<<"Key input too short!\n";
|
||||
return;
|
||||
|
||||
}
|
||||
@@ -48,7 +48,7 @@ void Keyinfo::load(string hexinput){
|
||||
case 'F':
|
||||
break;
|
||||
default:
|
||||
cout<<"Invalid character '"<<hexinput.at(i)<<"' found in key!"<<endl;
|
||||
std::cout<<"Invalid character '"<<hexinput.at(i)<<"' found in key!\n";
|
||||
valid=false;
|
||||
return;
|
||||
}
|
||||
@@ -59,7 +59,7 @@ void Keyinfo::load(string hexinput){
|
||||
// check that the input size is divisible by 2
|
||||
if(hexinput.size()%2!=0){
|
||||
valid=false;
|
||||
cout<<"Each hexadecimal byte must consist of 2 digits!"<<endl;
|
||||
std::cout<<"Each hexadecimal byte must consist of 2 digits!\n";
|
||||
return;
|
||||
}
|
||||
//convert the hex input to a char*
|
||||
@@ -68,13 +68,13 @@ void Keyinfo::load(string hexinput){
|
||||
loadCheck();
|
||||
//check for oversized key
|
||||
if(keySize==0 || keySize>SSP_KEY_LENGTH){
|
||||
cout<<"Key size cannot exceed "<<(SSP_KEY_LENGTH*8)<<" bits!"<<endl;
|
||||
cout<<"Provided key is "<<(keySize*8)<<" bits in length."<<endl;
|
||||
std::cout<<"Key size cannot exceed "<<(SSP_KEY_LENGTH*8)<<" bits!\n";
|
||||
std::cout<<"Provided key is "<<(keySize*8)<<" bits in length.\n";
|
||||
valid=false;
|
||||
return;
|
||||
}
|
||||
cout<<"Provided key length is "<<(keySize*8)<<" bits."<<endl;
|
||||
cout<<"Key checksum is "<<check<<"."<<endl;
|
||||
std::cout<<"Provided key length is "<<(keySize*8)<<" bits.\n";
|
||||
std::cout<<"Key checksum is "<<check<<".\n";
|
||||
|
||||
|
||||
}
|
||||
@@ -84,14 +84,14 @@ void Keyinfo::loadCheck() {
|
||||
for (i = 0; i<keySize;i++) {
|
||||
chk += ((int)key[i]) * (i + 1);
|
||||
}
|
||||
stringstream retval;
|
||||
retval<<hex<<chk;
|
||||
std::stringstream retval;
|
||||
retval<<std::hex<<chk;
|
||||
check=retval.str();
|
||||
}
|
||||
Keyinfo::~Keyinfo(){
|
||||
delete key;
|
||||
}
|
||||
void Keyinfo::loadKey(string str)
|
||||
void Keyinfo::loadKey(std::string str)
|
||||
{
|
||||
int length = str.size();
|
||||
// make sure the input string has an even digit numbers
|
||||
@@ -106,7 +106,7 @@ void Keyinfo::loadKey(string str)
|
||||
memset(key,0,(length/2)+1);
|
||||
keySize = length/2;
|
||||
|
||||
stringstream sstr(str);
|
||||
std::stringstream sstr(str);
|
||||
for(int i=0; i < keySize; i++)
|
||||
{
|
||||
char ch1, ch2;
|
||||
|
||||
783
src/main.cpp
783
src/main.cpp
@@ -16,569 +16,616 @@ GNU General Public License for more details.
|
||||
#include <config.h>
|
||||
#include <termios.h>
|
||||
#ifdef HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#ifdef HAVE_STDLIB_H
|
||||
#include <stdlib.h>
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
#ifdef HAVE_STRING_H
|
||||
#include <string.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
#include "scsiencrypt.h"
|
||||
#include "keyinfo.h"
|
||||
#define LOGFILE "/var/log/stenc"
|
||||
#include "scsiencrypt.h"
|
||||
|
||||
#include <ostream>
|
||||
|
||||
#define LOGFILE "/var/log/stenc"
|
||||
|
||||
typedef struct {
|
||||
#if STENC_BIG_ENDIAN == 0
|
||||
unsigned char bit1:1;
|
||||
unsigned char bit2:1;
|
||||
unsigned char bit3:1;
|
||||
unsigned char bit4:1;
|
||||
unsigned char bit5:1;
|
||||
unsigned char bit6:1;
|
||||
unsigned char bit7:1;
|
||||
unsigned char bit8:1;
|
||||
unsigned char bit1 : 1;
|
||||
unsigned char bit2 : 1;
|
||||
unsigned char bit3 : 1;
|
||||
unsigned char bit4 : 1;
|
||||
unsigned char bit5 : 1;
|
||||
unsigned char bit6 : 1;
|
||||
unsigned char bit7 : 1;
|
||||
unsigned char bit8 : 1;
|
||||
#else
|
||||
unsigned char bit8:1;
|
||||
unsigned char bit7:1;
|
||||
unsigned char bit6:1;
|
||||
unsigned char bit5:1;
|
||||
unsigned char bit4:1;
|
||||
unsigned char bit3:1;
|
||||
unsigned char bit2:1;
|
||||
unsigned char bit1:1;
|
||||
unsigned char bit8 : 1;
|
||||
unsigned char bit7 : 1;
|
||||
unsigned char bit6 : 1;
|
||||
unsigned char bit5 : 1;
|
||||
unsigned char bit4 : 1;
|
||||
unsigned char bit3 : 1;
|
||||
unsigned char bit2 : 1;
|
||||
unsigned char bit1 : 1;
|
||||
#endif
|
||||
} bitcheck;
|
||||
|
||||
using namespace std;
|
||||
void showUsage();
|
||||
void errorOut(string message);
|
||||
void inquiryDrive(string tapeDevice);
|
||||
void showDriveStatus(string tapeDevice,bool detail);
|
||||
void showVolumeStatus(string tapeDevice);
|
||||
string randomKey(int length);
|
||||
string timestamp();
|
||||
void errorOut(std::string const message);
|
||||
void inquiryDrive(std::string tapeDevice);
|
||||
void showDriveStatus(std::string tapeDevice, bool detail);
|
||||
void showVolumeStatus(std::string tapeDevice);
|
||||
std::string randomKey(int length);
|
||||
std::string timestamp();
|
||||
void echo(bool);
|
||||
ofstream logFile;
|
||||
//program entry point
|
||||
int main(int argc, char **argv){
|
||||
std::ofstream logFile;
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
bitcheck bc;
|
||||
memset(&bc,0,1);
|
||||
bc.bit2=1;
|
||||
bc.bit5=1;
|
||||
memset(&bc, 0, 1);
|
||||
bc.bit2 = 1;
|
||||
bc.bit5 = 1;
|
||||
unsigned char check;
|
||||
memcpy(&check,&bc,1);
|
||||
memcpy(&check, &bc, 1);
|
||||
|
||||
switch((int)check){
|
||||
switch ((int)check) {
|
||||
case 0x12:
|
||||
//this is good
|
||||
// this is good
|
||||
break;
|
||||
case 0x48:
|
||||
#if STENC_BIG_ENDIAN == 1
|
||||
errorOut("Swapped bit ordering detected(BI). Program needs to be configured without the --enable-swapendian option in order to function properly on your system");
|
||||
errorOut("Swapped bit ordering detected(BI). Program needs to be "
|
||||
"configured without the --enable-swapendian option in order to "
|
||||
"function properly on your system");
|
||||
#else
|
||||
errorOut("Swapped bit ordering detected(LI). Program needs to be configured with the --enable-swapendian option in order to function properly on your system");
|
||||
errorOut("Swapped bit ordering detected(LI). Program needs to be "
|
||||
"configured with the --enable-swapendian option in order to "
|
||||
"function properly on your system");
|
||||
#endif
|
||||
break;
|
||||
default:
|
||||
cerr<<"Unknown bit check result "<<HEX(check)<<endl;
|
||||
std::cerr << "Unknown bit check result " << std::hex << check;
|
||||
std::cerr << std::endl;
|
||||
errorOut("Exiting program because it will not run properly");
|
||||
break;
|
||||
}
|
||||
|
||||
string tapeDrive="";
|
||||
int action=0; // 0 = status, 1 =setting param, 2 = generating key
|
||||
string keyFile,keyDesc;
|
||||
int keyLength=0;
|
||||
bool detail=false;
|
||||
std::string tapeDrive = "";
|
||||
int action = 0; // 0 = status, 1 =setting param, 2 = generating key
|
||||
std::string keyFile, keyDesc;
|
||||
int keyLength = 0;
|
||||
bool detail = false;
|
||||
SCSIEncryptOptions drvOptions;
|
||||
|
||||
//First load all of the options
|
||||
for(int i=1;i<argc;i++){
|
||||
string thisCmd=argv[i];
|
||||
string nextCmd="";
|
||||
if(i+1<argc){
|
||||
if(strncmp(argv[i+1],"-",1)!=0)nextCmd=argv[i+1];
|
||||
// First load all of the options
|
||||
for (int i = 1; i < argc; i++) {
|
||||
std::string thisCmd = argv[i];
|
||||
std::string nextCmd = "";
|
||||
if (i + 1 < argc) {
|
||||
if (strncmp(argv[i + 1], "-", 1) != 0)
|
||||
nextCmd = argv[i + 1];
|
||||
}
|
||||
if(thisCmd=="--version"){
|
||||
cout<<"stenc v"<<VERSION<<" - SCSI Tape Encryption Manager"<<endl;
|
||||
cout<<"https://github.com/scsitape/stenc"<<endl;
|
||||
if (thisCmd == "--version") {
|
||||
std::cout << "stenc v" << VERSION << " - SCSI Tape Encryption Manager\n";
|
||||
std::cout << "https://github.com/scsitape/stenc" << std::endl;
|
||||
exit(EXIT_SUCCESS);
|
||||
|
||||
}
|
||||
if(thisCmd=="-g"){ //Check if the help flag was passed. If it was, show usage and exit
|
||||
if(nextCmd=="")errorOut("Key size must be specified when using -g");
|
||||
i++; //skip the next argument
|
||||
keyLength=atoi(nextCmd.c_str());
|
||||
if (keyLength % 8 != 0)errorOut("Key size must be divisible by 8");
|
||||
keyLength=keyLength/8;
|
||||
if(keyLength>SSP_KEY_LENGTH){
|
||||
cout<<"Warning: Keys over "<<(SSP_KEY_LENGTH*8)<<" bits cannot be used by this program!"<<endl;
|
||||
if (thisCmd == "-g") { // Check if the help flag was passed. If it was,
|
||||
// show usage and exit
|
||||
if (nextCmd == "")
|
||||
errorOut("Key size must be specified when using -g");
|
||||
i++; // skip the next argument
|
||||
keyLength = std::atoi(nextCmd.c_str());
|
||||
if (keyLength % 8 != 0)
|
||||
errorOut("Key size must be divisible by 8");
|
||||
keyLength = keyLength / 8;
|
||||
if (keyLength > SSP_KEY_LENGTH) {
|
||||
std::cout << "Warning: Keys over " << (SSP_KEY_LENGTH * 8)
|
||||
<< " bits cannot be used by this program!" << std::endl;
|
||||
}
|
||||
action=2; //generating key
|
||||
}
|
||||
else if(thisCmd=="-e"){
|
||||
if(nextCmd=="")errorOut("Key file not specified after -k option");
|
||||
if(nextCmd=="on")drvOptions.cryptMode=CRYPTMODE_ON; //encrypt, read only encrypted data
|
||||
else if(nextCmd=="mixed")drvOptions.cryptMode=CRYPTMODE_MIXED;//encrypt, read encrypted and unencrypted data
|
||||
else if(nextCmd=="rawread")drvOptions.cryptMode=CRYPTMODE_RAWREAD;//encrypt, read encrypted and unencrypted data
|
||||
else if(nextCmd=="off")drvOptions.cryptMode=CRYPTMODE_OFF;//encrypt, read encrypted and unencrypted data
|
||||
else errorOut("Unknown encryption mode '"+nextCmd+"'");//encrypt, read encrypted and unencrypted data
|
||||
i++; //skip the next argument
|
||||
action=1;
|
||||
}
|
||||
else if(thisCmd=="-f"){
|
||||
if(nextCmd=="")errorOut("Device not specified after -f option.");
|
||||
tapeDrive=nextCmd; //set the tape drive
|
||||
i++; //skip the next argument
|
||||
}
|
||||
else if(thisCmd=="-k"){
|
||||
if(nextCmd=="")errorOut("Key file not specified after -k option");
|
||||
keyFile=nextCmd; //set the key file
|
||||
i++; //skip the next argument
|
||||
}
|
||||
else if(thisCmd=="-kd"){
|
||||
if(nextCmd=="")errorOut("Key description not specified after the -kd option");
|
||||
keyDesc=nextCmd; //set the key file
|
||||
if(keyDesc.size()>SSP_UKAD_LENGTH){
|
||||
action = 2; // generating key
|
||||
} else if (thisCmd == "-e") {
|
||||
if (nextCmd == "")
|
||||
errorOut("Key file not specified after -k option");
|
||||
if (nextCmd == "on")
|
||||
drvOptions.cryptMode = CRYPTMODE_ON; // encrypt, read only encrypted
|
||||
// data
|
||||
else if (nextCmd == "mixed")
|
||||
drvOptions.cryptMode =
|
||||
CRYPTMODE_MIXED; // encrypt, read encrypted and unencrypted data
|
||||
else if (nextCmd == "rawread")
|
||||
drvOptions.cryptMode =
|
||||
CRYPTMODE_RAWREAD; // encrypt, read encrypted and unencrypted data
|
||||
else if (nextCmd == "off")
|
||||
drvOptions.cryptMode =
|
||||
CRYPTMODE_OFF; // encrypt, read encrypted and unencrypted data
|
||||
else
|
||||
errorOut("Unknown encryption mode '" + nextCmd +
|
||||
"'"); // encrypt, read encrypted and unencrypted data
|
||||
i++; // skip the next argument
|
||||
action = 1;
|
||||
} else if (thisCmd == "-f") {
|
||||
if (nextCmd == "")
|
||||
errorOut("Device not specified after -f option.");
|
||||
tapeDrive = nextCmd; // set the tape drive
|
||||
i++; // skip the next argument
|
||||
} else if (thisCmd == "-k") {
|
||||
if (nextCmd == "")
|
||||
errorOut("Key file not specified after -k option");
|
||||
keyFile = nextCmd; // set the key file
|
||||
i++; // skip the next argument
|
||||
} else if (thisCmd == "-kd") {
|
||||
if (nextCmd == "")
|
||||
errorOut("Key description not specified after the -kd option");
|
||||
keyDesc = nextCmd; // set the key file
|
||||
if (keyDesc.size() > SSP_UKAD_LENGTH) {
|
||||
errorOut("Key description too long!");
|
||||
}
|
||||
i++; //skip the next argument
|
||||
i++; // skip the next argument
|
||||
} else if (thisCmd == "--protect") {
|
||||
if (drvOptions.rdmc == RDMC_UNPROTECT)
|
||||
errorOut("'--protect' cannot be specified at the same time as "
|
||||
"'--unprotect'");
|
||||
drvOptions.rdmc = RDMC_PROTECT;
|
||||
} else if (thisCmd == "--unprotect") {
|
||||
if (drvOptions.rdmc == RDMC_PROTECT)
|
||||
errorOut("'--unprotect' cannot be specified at the same time as "
|
||||
"'--protect'");
|
||||
drvOptions.rdmc = RDMC_UNPROTECT;
|
||||
} else if (thisCmd == "--ckod") {
|
||||
drvOptions.CKOD = true;
|
||||
} else if (thisCmd == "--detail") {
|
||||
detail = true;
|
||||
} else if (thisCmd == "-a") {
|
||||
if (nextCmd == "")
|
||||
errorOut("You must specify a numeric algorithm index when using the -a "
|
||||
"flag");
|
||||
drvOptions.algorithmIndex = std::atoi(nextCmd.c_str());
|
||||
i++; // skip the next argument
|
||||
} else {
|
||||
errorOut("Unknown command '" + thisCmd + "'");
|
||||
}
|
||||
else if(thisCmd=="--protect"){
|
||||
if(drvOptions.rdmc==RDMC_UNPROTECT)errorOut("'--protect' cannot be specified at the same time as '--unprotect'");
|
||||
drvOptions.rdmc=RDMC_PROTECT;
|
||||
}
|
||||
else if(thisCmd=="--unprotect"){
|
||||
if(drvOptions.rdmc==RDMC_PROTECT)errorOut("'--unprotect' cannot be specified at the same time as '--protect'");
|
||||
drvOptions.rdmc=RDMC_UNPROTECT;
|
||||
}
|
||||
else if(thisCmd=="--ckod"){
|
||||
drvOptions.CKOD=true;
|
||||
}
|
||||
else if(thisCmd=="--detail"){
|
||||
detail=true;
|
||||
}
|
||||
else if(thisCmd=="-a"){
|
||||
if(nextCmd=="")errorOut("You must specify a numeric algorithm index when using the -a flag");
|
||||
drvOptions.algorithmIndex=atoi(nextCmd.c_str());
|
||||
i++; //skip the next argument
|
||||
}
|
||||
else{
|
||||
errorOut("Unknown command '"+thisCmd+"'");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(action==2){//generate key
|
||||
if(keyFile==""){
|
||||
if (action == 2) { // generate key
|
||||
if (keyFile == "") {
|
||||
errorOut("Specify file to save into with the -k argument.");
|
||||
}
|
||||
|
||||
string newkey=randomKey(keyLength);
|
||||
ofstream kf;
|
||||
umask(077); //make sure that no one else can read the new key file we are creating
|
||||
kf.open(keyFile.c_str(),ios::trunc);
|
||||
if(!kf.is_open()){
|
||||
errorOut("Could not open '"+keyFile+"' for writing.");
|
||||
|
||||
std::string const newkey = randomKey(keyLength);
|
||||
std::ofstream kf { };
|
||||
umask(077); // make sure that no one else can read the new key file
|
||||
kf.open(keyFile.c_str(), std::ios::trunc);
|
||||
if (!kf.is_open()) {
|
||||
errorOut("Could not open '" + keyFile + "' for writing.");
|
||||
}
|
||||
kf<<newkey<<keyDesc;
|
||||
kf << newkey << keyDesc;
|
||||
kf.close();
|
||||
cout<<"Random key saved into '"<<keyFile<<"'"<<endl;
|
||||
chmod(keyFile.c_str(),0600);
|
||||
cout<<"Permissions of keyfile set to 600"<<endl;
|
||||
std::cout << "Random key saved into '" << keyFile << "'" << std::endl;
|
||||
chmod(keyFile.c_str(), 0600);
|
||||
std::cout << "Permissions of keyfile set to 600" << std::endl;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
//validate the tape device
|
||||
if(tapeDrive==""){
|
||||
// validate the tape device
|
||||
if (tapeDrive == "") {
|
||||
errorOut("Tape drive device must be specified with the -f option");
|
||||
}
|
||||
if(drvOptions.cryptMode==CRYPTMODE_RAWREAD && drvOptions.rdmc==RDMC_PROTECT){
|
||||
errorOut("'--protect' is not valid when setting encryption mode to 'rawread'");
|
||||
if (drvOptions.cryptMode == CRYPTMODE_RAWREAD &&
|
||||
drvOptions.rdmc == RDMC_PROTECT) {
|
||||
errorOut(
|
||||
"'--protect' is not valid when setting encryption mode to 'rawread'");
|
||||
}
|
||||
|
||||
#ifndef DISABLE_DEVICE_NAME_CONVERSION
|
||||
if(tapeDrive.find(".")==string::npos){
|
||||
if(tapeDrive.substr(0,7)=="/dev/st"){
|
||||
tapeDrive="/dev/nst"+tapeDrive.substr(7,tapeDrive.size()-6);
|
||||
if (tapeDrive.find(".") == std::string::npos) {
|
||||
if (tapeDrive.substr(0, 7) == "/dev/st") {
|
||||
tapeDrive = "/dev/nst" + tapeDrive.substr(7, tapeDrive.size() - 6);
|
||||
}
|
||||
|
||||
if(tapeDrive.substr(0,8)=="/dev/rmt" && tapeDrive.substr(tapeDrive.size()-2,2)!=".1" ){
|
||||
tapeDrive="/dev/rmt"+tapeDrive.substr(8,tapeDrive.size()-7)+".1";
|
||||
if (tapeDrive.substr(0, 8) == "/dev/rmt" &&
|
||||
tapeDrive.substr(tapeDrive.size() - 2, 2) != ".1") {
|
||||
tapeDrive = "/dev/rmt" + tapeDrive.substr(8, tapeDrive.size() - 7) + ".1";
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if(getuid()!=0){
|
||||
if (getuid() != 0) {
|
||||
errorOut("You must be root to read or set encryption options on a drive!");
|
||||
}
|
||||
logFile.open(LOGFILE,ios::app);
|
||||
if(!logFile.is_open()){
|
||||
cout<<"Warning: Could not open '"<<LOGFILE<<"' for key change auditing!"<<endl;
|
||||
logFile.open(LOGFILE, std::ios::app);
|
||||
if (!logFile.is_open()) {
|
||||
std::cout << "Warning: Could not open '" << LOGFILE
|
||||
<< "' for key change auditing!" << std::endl;
|
||||
}
|
||||
chmod(LOGFILE,0600);
|
||||
chmod(LOGFILE, 0600);
|
||||
|
||||
if(action==0){
|
||||
cout<<"Status for "<<tapeDrive<<endl;
|
||||
cout<<"--------------------------------------------------"<<endl;
|
||||
if(detail)
|
||||
if (action == 0) {
|
||||
std::cout << "Status for " << tapeDrive << std::endl;
|
||||
std::cout << "--------------------------------------------------"
|
||||
<< std::endl;
|
||||
if (detail)
|
||||
inquiryDrive(tapeDrive);
|
||||
showDriveStatus(tapeDrive,detail);
|
||||
if(detail)
|
||||
showDriveStatus(tapeDrive, detail);
|
||||
if (detail)
|
||||
showVolumeStatus(tapeDrive);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
Keyinfo ki;
|
||||
if(drvOptions.cryptMode!=CRYPTMODE_OFF){
|
||||
if(keyFile==""){
|
||||
string p1="01";
|
||||
string p2="02";
|
||||
bool done=false;
|
||||
while(!done){
|
||||
cout<<"Enter key in hex format: ";
|
||||
Keyinfo ki { };
|
||||
if (drvOptions.cryptMode != CRYPTMODE_OFF) {
|
||||
if (keyFile == "") {
|
||||
std::string p1 = "01";
|
||||
std::string p2 = "02";
|
||||
bool done = false;
|
||||
while (!done) {
|
||||
std::cout << "Enter key in hex format: ";
|
||||
echo(false);
|
||||
getline(cin,p1);
|
||||
getline(std::cin, p1);
|
||||
echo(true);
|
||||
cout<<endl;
|
||||
cout<<"Re-enter key in hex format: ";
|
||||
std::cout << std::endl;
|
||||
std::cout << "Re-enter key in hex format: ";
|
||||
echo(false);
|
||||
getline(cin,p2);
|
||||
getline(std::cin, p2);
|
||||
echo(true);
|
||||
cout<<endl;
|
||||
if(p1!=p2){
|
||||
cout<<"Keys do not match!!"<<endl;
|
||||
}else{
|
||||
std::cout << std::endl;
|
||||
if (p1 != p2) {
|
||||
std::cout << "Keys do not match!!" << std::endl;
|
||||
} else {
|
||||
ki.load(p1);
|
||||
if(ki.valid){
|
||||
cout<<"Set encryption using this key? [y/n]: ";
|
||||
string ans="";
|
||||
getline(cin,ans);
|
||||
if(ans=="y"){
|
||||
done=true;
|
||||
if (ki.valid) {
|
||||
std::cout << "Set encryption using this key? [y/n]: ";
|
||||
std::string ans = "";
|
||||
getline(std::cin, ans);
|
||||
if (ans == "y") {
|
||||
done = true;
|
||||
}
|
||||
}else cout<<"Invalid key!"<<endl;
|
||||
} else
|
||||
std::cout << "Invalid key!" << std::endl;
|
||||
}
|
||||
}
|
||||
drvOptions.keyName=keyDesc;
|
||||
drvOptions.keyName = keyDesc;
|
||||
|
||||
}else{
|
||||
//set keyInput here
|
||||
string keyInput;
|
||||
ifstream myfile(keyFile.c_str());
|
||||
if (myfile.is_open())
|
||||
{
|
||||
getline (myfile,keyInput);
|
||||
getline (myfile,keyDesc);
|
||||
} else {
|
||||
// set keyInput here
|
||||
std::string keyInput;
|
||||
std::ifstream myfile(keyFile.c_str());
|
||||
if (myfile.is_open()) {
|
||||
getline(myfile, keyInput);
|
||||
getline(myfile, keyDesc);
|
||||
myfile.close();
|
||||
ki.load(keyInput);
|
||||
if(!ki.valid)
|
||||
errorOut("Invalid key found in '"+keyFile+"'");
|
||||
drvOptions.keyName=keyDesc;
|
||||
}else errorOut("Could not open '"+keyFile+"' for reading");
|
||||
|
||||
if (!ki.valid)
|
||||
errorOut("Invalid key found in '" + keyFile + "'");
|
||||
drvOptions.keyName = keyDesc;
|
||||
} else
|
||||
errorOut("Could not open '" + keyFile + "' for reading");
|
||||
}
|
||||
drvOptions.cryptoKey.assign(ki.key,ki.keySize);
|
||||
|
||||
drvOptions.cryptoKey.assign(ki.key, ki.keySize);
|
||||
}
|
||||
|
||||
//Write the options to the tape device
|
||||
cout<<"Turning "<<((drvOptions.cryptMode!=CRYPTMODE_OFF)?"on":"off")<<" encryption on device '"<<tapeDrive<<"'..."<<endl;
|
||||
bool res=SCSIWriteEncryptOptions(tapeDrive,&drvOptions);
|
||||
if(res){
|
||||
// Write the options to the tape device
|
||||
std::cout << "Turning "
|
||||
<< ((drvOptions.cryptMode != CRYPTMODE_OFF) ? "on" : "off")
|
||||
<< " encryption on device '" << tapeDrive << "'..." << std::endl;
|
||||
bool res = SCSIWriteEncryptOptions(tapeDrive, &drvOptions);
|
||||
if (res) {
|
||||
|
||||
SSP_DES* opt=SSPGetDES(tapeDrive);
|
||||
if(drvOptions.cryptMode!=CRYPTMODE_OFF && opt->des.encryptionMode!=2){
|
||||
errorOut("Turning encryption on for '"+tapeDrive+"' failed!");
|
||||
SSP_DES *opt = SSPGetDES(tapeDrive);
|
||||
if (drvOptions.cryptMode != CRYPTMODE_OFF && opt->des.encryptionMode != 2) {
|
||||
errorOut("Turning encryption on for '" + tapeDrive + "' failed!");
|
||||
}
|
||||
if(drvOptions.cryptMode==CRYPTMODE_OFF && opt->des.encryptionMode!=0){
|
||||
errorOut("Turning encryption off for '"+tapeDrive+"' failed!");
|
||||
if (drvOptions.cryptMode == CRYPTMODE_OFF && opt->des.encryptionMode != 0) {
|
||||
errorOut("Turning encryption off for '" + tapeDrive + "' failed!");
|
||||
}
|
||||
delete opt;
|
||||
|
||||
if(drvOptions.cryptMode!=CRYPTMODE_OFF){
|
||||
stringstream msg;
|
||||
msg<<"Encryption turned on for device '"<<tapeDrive<<"'. ";
|
||||
if(drvOptions.keyName.size()==0)
|
||||
msg<<"Key Checksum: "<<ki.check;
|
||||
if (drvOptions.cryptMode != CRYPTMODE_OFF) {
|
||||
std::stringstream msg;
|
||||
msg << "Encryption turned on for device '" << tapeDrive << "'. ";
|
||||
if (drvOptions.keyName.size() == 0)
|
||||
msg << "Key Checksum: " << ki.check;
|
||||
else
|
||||
msg<<"Key Descriptor: '"<<drvOptions.keyName<<"'";
|
||||
msg<<" Key Instance: "<<dec<<BSLONG(opt->des.keyInstance)<<endl;
|
||||
msg << "Key Descriptor: '" << drvOptions.keyName << "'";
|
||||
msg << " Key Instance: " << std::dec << BSLONG(opt->des.keyInstance)
|
||||
<< std::endl;
|
||||
|
||||
if(logFile.is_open()){
|
||||
logFile<<timestamp()<<": "<<msg.str();
|
||||
if (logFile.is_open()) {
|
||||
logFile << timestamp() << ": " << msg.str();
|
||||
}
|
||||
}else{
|
||||
stringstream msg;
|
||||
} else {
|
||||
std::stringstream msg { };
|
||||
|
||||
msg<< "Encryption turned off for device '"<<tapeDrive<<"'.";
|
||||
msg<<" Key Instance: "<<dec<<BSLONG(opt->des.keyInstance)<<endl;
|
||||
msg << "Encryption turned off for device '" << tapeDrive << "'.";
|
||||
msg << " Key Instance: " << std::dec << BSLONG(opt->des.keyInstance)
|
||||
<< std::endl;
|
||||
|
||||
if(logFile.is_open())
|
||||
logFile<<timestamp()<<": "<<msg.str();
|
||||
if (logFile.is_open())
|
||||
logFile << timestamp() << ": " << msg.str();
|
||||
}
|
||||
cout<< "Success! See '"<<LOGFILE<<"' for a key change audit log."<<endl;
|
||||
std::cout << "Success! See '" << LOGFILE << "' for a key change audit log."
|
||||
<< std::endl;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
if(drvOptions.cryptMode!=CRYPTMODE_OFF){
|
||||
errorOut("Turning encryption on for '"+tapeDrive+"' failed!");
|
||||
}else{
|
||||
errorOut("Turning encryption off for '"+tapeDrive+"' failed!");
|
||||
if (drvOptions.cryptMode != CRYPTMODE_OFF) {
|
||||
errorOut("Turning encryption on for '" + tapeDrive + "' failed!");
|
||||
} else {
|
||||
errorOut("Turning encryption off for '" + tapeDrive + "' failed!");
|
||||
}
|
||||
}
|
||||
//exits to shell with an error message
|
||||
void errorOut(string message){
|
||||
cerr<<"Error: "<<message<<endl;
|
||||
// exits to shell with an error message
|
||||
|
||||
void errorOut(std::string const message) {
|
||||
std::cerr << "Error: " << message << std::endl;
|
||||
showUsage();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
//shows the command usage
|
||||
void showUsage(){
|
||||
cout<<"Usage: stenc --version | -g <length> -k <file> [-kd <description>] | -f <device> [--detail] [-e <on/mixed/rawread/off> [-k <file>] [-kd <description>] [-a <index>] [--protect | --unprotect] [--ckod] ]"<<endl;
|
||||
cout<<"Type 'man stenc' for more information."<<endl;
|
||||
// shows the command usage
|
||||
void showUsage() {
|
||||
std::cout
|
||||
<< "Usage: stenc --version | -g <length> -k <file> [-kd <description>] | "
|
||||
"-f <device> [--detail] [-e <on/mixed/rawread/off> [-k <file>] [-kd "
|
||||
"<description>] [-a <index>] [--protect | --unprotect] [--ckod] ]"
|
||||
<< std::endl;
|
||||
std::cout << "Type 'man stenc' for more information." << std::endl;
|
||||
}
|
||||
void inquiryDrive(string tapeDevice){
|
||||
SCSI_PAGE_INQ* iresult=SCSIGetInquiry(tapeDevice);
|
||||
cout<<left<<setw(25)<<"Device Mfg:";
|
||||
cout.write((const char*)iresult->vender,8);
|
||||
cout<<endl;
|
||||
cout<<left<<setw(25)<<"Product ID:";
|
||||
cout.write((const char*)iresult->productID,16);
|
||||
cout<<endl;
|
||||
cout<<left<<setw(25)<<"Product Revision:";
|
||||
cout.write((const char*)iresult->productRev,4);
|
||||
cout<<endl;
|
||||
void inquiryDrive(std::string tapeDevice) {
|
||||
//todo: std::cout should not be used outside main()
|
||||
SCSI_PAGE_INQ *const iresult = SCSIGetInquiry(tapeDevice);
|
||||
std::cout << std::left << std::setw(25) << "Device Mfg:";
|
||||
std::cout.write((const char *)iresult->vender, 8);
|
||||
std::cout << std::endl;
|
||||
std::cout << std::left << std::setw(25) << "Product ID:";
|
||||
std::cout.write((const char *)iresult->productID, 16);
|
||||
std::cout << std::endl;
|
||||
std::cout << std::left << std::setw(25) << "Product Revision:";
|
||||
std::cout.write((const char *)iresult->productRev, 4);
|
||||
std::cout << std::endl;
|
||||
|
||||
delete iresult;
|
||||
}
|
||||
|
||||
|
||||
void showDriveStatus(string tapeDrive,bool detail){
|
||||
SSP_DES* opt=SSPGetDES(tapeDrive);
|
||||
if(opt==NULL)return;
|
||||
string emode="unknown";
|
||||
cout<<left<<setw(25)<<"Drive Encryption:";
|
||||
if(
|
||||
(int)opt->des.encryptionMode==0x2 && //encrypt
|
||||
(int)opt->des.decryptionMode==0x2 //read only encrypted data
|
||||
void showDriveStatus(std::string tapeDrive, bool detail) {
|
||||
SSP_DES *opt = SSPGetDES(tapeDrive);
|
||||
if (opt == NULL)
|
||||
return;
|
||||
std::string emode = "unknown";
|
||||
std::cout << std::left << std::setw(25) << "Drive Encryption:";
|
||||
if ((int)opt->des.encryptionMode == 0x2 && // encrypt
|
||||
(int)opt->des.decryptionMode == 0x2 // read only encrypted data
|
||||
)
|
||||
emode="on";
|
||||
if(
|
||||
(int)opt->des.encryptionMode==0x2 && //encrypt
|
||||
(int)opt->des.decryptionMode==0x3 //read encrypted and unencrypted
|
||||
emode = "on";
|
||||
if ((int)opt->des.encryptionMode == 0x2 && // encrypt
|
||||
(int)opt->des.decryptionMode == 0x3 // read encrypted and unencrypted
|
||||
)
|
||||
emode="mixed";
|
||||
emode = "mixed";
|
||||
|
||||
if(
|
||||
(int)opt->des.encryptionMode==0x2 && //encrypt
|
||||
(int)opt->des.decryptionMode==0x1 //read encrypted and unencrypted
|
||||
if ((int)opt->des.encryptionMode == 0x2 && // encrypt
|
||||
(int)opt->des.decryptionMode == 0x1 // read encrypted and unencrypted
|
||||
)
|
||||
emode="rawread";
|
||||
emode = "rawread";
|
||||
|
||||
if(
|
||||
(int)opt->des.encryptionMode==0x0 && //encrypt
|
||||
(int)opt->des.decryptionMode==0x0 //read encrypted and unencrypted
|
||||
if ((int)opt->des.encryptionMode == 0x0 && // encrypt
|
||||
(int)opt->des.decryptionMode == 0x0 // read encrypted and unencrypted
|
||||
)
|
||||
emode="off";
|
||||
emode = "off";
|
||||
|
||||
cout<<emode<<endl;
|
||||
if(detail){
|
||||
cout<<left<<setw(25)<<"Drive Output:";
|
||||
switch ((int)opt->des.decryptionMode){
|
||||
std::cout << emode << std::endl;
|
||||
if (detail) {
|
||||
std::cout << std::left << std::setw(25) << "Drive Output:";
|
||||
switch ((int)opt->des.decryptionMode) {
|
||||
case 0x0:
|
||||
cout<<"Not decrypting"<<endl;
|
||||
cout<<setw(25)<<" "<<"Raw encrypted data not outputted"<<endl;
|
||||
std::cout << "Not decrypting" << std::endl;
|
||||
std::cout << std::setw(25) << " "
|
||||
<< "Raw encrypted data not outputted" << std::endl;
|
||||
break;
|
||||
case 0x1:
|
||||
cout<<"Not decrypting"<<endl;
|
||||
cout<<setw(25)<<" "<<"Raw encrypted data outputted"<<endl;
|
||||
std::cout << "Not decrypting" << std::endl;
|
||||
std::cout << std::setw(25) << " "
|
||||
<< "Raw encrypted data outputted" << std::endl;
|
||||
break;
|
||||
case 0x2:
|
||||
cout<<"Decrypting"<<endl;
|
||||
cout<<setw(25)<<" "<<"Unencrypted data not outputted"<<endl;
|
||||
std::cout << "Decrypting" << std::endl;
|
||||
std::cout << std::setw(25) << " "
|
||||
<< "Unencrypted data not outputted" << std::endl;
|
||||
break;
|
||||
case 0x3:
|
||||
cout<<"Decrypting"<<endl;
|
||||
cout<<setw(25)<<" "<<"Unencrypted data outputted"<<endl;
|
||||
std::cout << "Decrypting" << std::endl;
|
||||
std::cout << std::setw(25) << " "
|
||||
<< "Unencrypted data outputted" << std::endl;
|
||||
break;
|
||||
default:
|
||||
cout<<"Unknown '0x"<<hex<<(int)opt->des.decryptionMode<<"' "<<endl;
|
||||
std::cout << "Unknown '0x" << std::hex << (int)opt->des.decryptionMode
|
||||
<< "' " << std::endl;
|
||||
break;
|
||||
}
|
||||
cout<<setw(25)<<"Drive Input:";
|
||||
switch((int)opt->des.encryptionMode){
|
||||
std::cout << std::setw(25) << "Drive Input:";
|
||||
switch ((int)opt->des.encryptionMode) {
|
||||
case 0x0:
|
||||
cout<<"Not encrypting"<<endl;
|
||||
std::cout << "Not encrypting" << std::endl;
|
||||
break;
|
||||
case 0x2:
|
||||
cout<<"Encrypting"<<endl;
|
||||
std::cout << "Encrypting" << std::endl;
|
||||
break;
|
||||
default:
|
||||
cout<<"Unknown result '0x"<<hex<<(int)opt->des.encryptionMode<<"'"<<endl;
|
||||
std::cout << "Unknown result '0x" << std::hex
|
||||
<< (int)opt->des.encryptionMode << "'" << std::endl;
|
||||
break;
|
||||
}
|
||||
if(opt->des.RDMD==1){
|
||||
cout<<setw(25)<<" "<<"Protecting from raw read"<<endl;
|
||||
if (opt->des.RDMD == 1) {
|
||||
std::cout << std::setw(25) << " "
|
||||
<< "Protecting from raw read" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
cout<<setw(25)<<"Key Instance Counter:"<<dec<<BSLONG(opt->des.keyInstance)<<endl;
|
||||
if(opt->des.algorithmIndex!=0){
|
||||
cout<<setw(25)<<"Encryption Algorithm:"<<hex<<(int)opt->des.algorithmIndex<<endl;
|
||||
std::cout << std::setw(25) << "Key Instance Counter:" << std::dec
|
||||
<< BSLONG(opt->des.keyInstance) << std::endl;
|
||||
if (opt->des.algorithmIndex != 0) {
|
||||
std::cout << std::setw(25) << "Encryption Algorithm:" << std::hex
|
||||
<< (int)opt->des.algorithmIndex << std::endl;
|
||||
}
|
||||
}
|
||||
if(opt->kads.size()>0){
|
||||
for(unsigned int i=0;i<opt->kads.size();i++){
|
||||
stringstream lbl;
|
||||
lbl<<"Drive Key Desc.(";
|
||||
switch(opt->kads[i].type){
|
||||
if (opt->kads.size() > 0) {
|
||||
for (unsigned int i = 0; i < opt->kads.size(); i++) {
|
||||
std::stringstream lbl { };
|
||||
lbl << "Drive Key Desc.(";
|
||||
switch (opt->kads[i].type) {
|
||||
case KAD_TYPE_UKAD:
|
||||
lbl<<"uKAD): ";
|
||||
cout<<setw(25)<<lbl.str();
|
||||
cout.write((const char*)&opt->kads[i].descriptor,BSSHORT(opt->kads[i].descriptorLength));
|
||||
cout<<endl;
|
||||
lbl << "uKAD): ";
|
||||
std::cout << std::setw(25) << lbl.str();
|
||||
std::cout.write((const char *)&opt->kads[i].descriptor,
|
||||
BSSHORT(opt->kads[i].descriptorLength));
|
||||
std::cout << std::endl;
|
||||
break;
|
||||
case KAD_TYPE_AKAD:
|
||||
lbl<<"aKAD): ";
|
||||
cout<<setw(25)<<lbl.str();
|
||||
cout.write((const char*)&opt->kads[i].descriptor,BSSHORT(opt->kads[i].descriptorLength));
|
||||
cout<<endl;
|
||||
lbl << "aKAD): ";
|
||||
std::cout << std::setw(25) << lbl.str();
|
||||
std::cout.write((const char *)&opt->kads[i].descriptor,
|
||||
BSSHORT(opt->kads[i].descriptorLength));
|
||||
std::cout << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delete opt;
|
||||
|
||||
}
|
||||
|
||||
void showVolumeStatus(string tapeDrive){
|
||||
SSP_NBES* opt=SSPGetNBES(tapeDrive,true);
|
||||
if(opt==NULL)return;
|
||||
if(opt->nbes.compressionStatus!=0){
|
||||
cout<<left<<setw(25)<<"Volume Compressed:";
|
||||
switch(opt->nbes.compressionStatus){
|
||||
void showVolumeStatus(std::string tapeDrive) {
|
||||
SSP_NBES *opt = SSPGetNBES(tapeDrive, true);
|
||||
if (opt == NULL)
|
||||
return;
|
||||
if (opt->nbes.compressionStatus != 0) {
|
||||
std::cout << std::left << std::setw(25) << "Volume Compressed:";
|
||||
switch (opt->nbes.compressionStatus) {
|
||||
case 0x00:
|
||||
cout<<"Drive cannot determine"<<endl;
|
||||
std::cout << "Drive cannot determine" << std::endl;
|
||||
break;
|
||||
default:
|
||||
cout<<"Unknown result '"<<hex<<(int)opt->nbes.compressionStatus<<"'"<<endl;
|
||||
std::cout << "Unknown result '" << std::hex
|
||||
<< (int)opt->nbes.compressionStatus << "'" << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
cout<<left<<setw(25)<<"Volume Encryption:";
|
||||
switch((int)opt->nbes.encryptionStatus){
|
||||
std::cout << std::left << std::setw(25) << "Volume Encryption:";
|
||||
switch ((int)opt->nbes.encryptionStatus) {
|
||||
case 0x01:
|
||||
cout<<"Unable to determine"<<endl;
|
||||
std::cout << "Unable to determine" << std::endl;
|
||||
break;
|
||||
case 0x02:
|
||||
cout<<"Logical block is not a logical block"<<endl;
|
||||
std::cout << "Logical block is not a logical block" << std::endl;
|
||||
break;
|
||||
case 0x03:
|
||||
cout<<"Not encrypted"<<endl;
|
||||
std::cout << "Not encrypted" << std::endl;
|
||||
break;
|
||||
case 0x05:
|
||||
cout<<"Encrypted and able to decrypt"<<endl;
|
||||
if(opt->nbes.RDMDS==1)
|
||||
cout<<left<<setw(25)<<" "<<"Protected from raw read"<<endl;
|
||||
std::cout << "Encrypted and able to decrypt" << std::endl;
|
||||
if (opt->nbes.RDMDS == 1)
|
||||
std::cout << std::left << std::setw(25) << " "
|
||||
<< "Protected from raw read" << std::endl;
|
||||
break;
|
||||
case 0x06:
|
||||
cout<<"Encrypted, but unable to decrypt due to invalid key. "<<endl;
|
||||
if(opt->kads.size()>0){
|
||||
for(unsigned int i=0;i<opt->kads.size();i++){
|
||||
stringstream lbl;
|
||||
lbl<<"Volume Key Desc.(";
|
||||
switch(opt->kads[i].type){
|
||||
std::cout << "Encrypted, but unable to decrypt due to invalid key. "
|
||||
<< std::endl;
|
||||
if (opt->kads.size() > 0) {
|
||||
for (unsigned int i = 0; i < opt->kads.size(); i++) {
|
||||
std::stringstream lbl;
|
||||
lbl << "Volume Key Desc.(";
|
||||
switch (opt->kads[i].type) {
|
||||
case KAD_TYPE_UKAD:
|
||||
lbl<<"uKAD): ";
|
||||
cout<<setw(25)<<lbl.str();
|
||||
cout.write((const char*)&opt->kads[i].descriptor,BSSHORT(opt->kads[i].descriptorLength));
|
||||
cout<<endl;
|
||||
lbl << "uKAD): ";
|
||||
std::cout << std::setw(25) << lbl.str();
|
||||
std::cout.write((const char *)&opt->kads[i].descriptor,
|
||||
BSSHORT(opt->kads[i].descriptorLength));
|
||||
std::cout << std::endl;
|
||||
break;
|
||||
case KAD_TYPE_AKAD:
|
||||
lbl<<"aKAD): ";
|
||||
cout<<setw(25)<<lbl.str();
|
||||
cout.write((const char*)&opt->kads[i].descriptor,BSSHORT(opt->kads[i].descriptorLength));
|
||||
cout<<endl;
|
||||
lbl << "aKAD): ";
|
||||
std::cout << std::setw(25) << lbl.str();
|
||||
std::cout.write((const char *)&opt->kads[i].descriptor,
|
||||
BSSHORT(opt->kads[i].descriptorLength));
|
||||
std::cout << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(opt->nbes.RDMDS==1)
|
||||
cout<<left<<setw(25)<<" "<<"Protected from raw read"<<endl;
|
||||
if (opt->nbes.RDMDS == 1)
|
||||
std::cout << std::left << std::setw(25) << " "
|
||||
<< "Protected from raw read" << std::endl;
|
||||
break;
|
||||
|
||||
default:
|
||||
cout<<"Unknown result '"<<hex<<(int)opt->nbes.encryptionStatus<<"'"<<endl;
|
||||
std::cout << "Unknown result '" << std::hex
|
||||
<< (int)opt->nbes.encryptionStatus << "'" << std::endl;
|
||||
break;
|
||||
}
|
||||
if(opt->nbes.algorithmIndex!=0){
|
||||
cout<<left<<setw(25)<<"Volume Algorithm:"<<(int)opt->nbes.algorithmIndex<<endl;
|
||||
if (opt->nbes.algorithmIndex != 0) {
|
||||
std::cout << std::left << std::setw(25)
|
||||
<< "Volume Algorithm:" << (int)opt->nbes.algorithmIndex
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
delete opt;
|
||||
|
||||
}
|
||||
void echo( bool on = true )
|
||||
{
|
||||
struct termios settings;
|
||||
tcgetattr( STDIN_FILENO, &settings );
|
||||
settings.c_lflag = on
|
||||
? (settings.c_lflag | ECHO )
|
||||
: (settings.c_lflag & ~(ECHO));
|
||||
tcsetattr( STDIN_FILENO, TCSANOW, &settings );
|
||||
|
||||
void echo(bool on = true) {
|
||||
struct termios settings { };
|
||||
tcgetattr(STDIN_FILENO, &settings);
|
||||
settings.c_lflag =
|
||||
on ? (settings.c_lflag | ECHO) : (settings.c_lflag & ~(ECHO));
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &settings);
|
||||
}
|
||||
std::string timestamp(){
|
||||
time_t tm;
|
||||
std::string timestamp() {
|
||||
time_t tm { };
|
||||
time(&tm);
|
||||
char buffer[80];
|
||||
int len=strftime((char*)&buffer,80,"%Y-%m-%d",localtime(&tm));
|
||||
string val;
|
||||
val.assign(buffer,len);
|
||||
return val;
|
||||
int len = strftime((char *)&buffer, 80, "%Y-%m-%d", localtime(&tm));
|
||||
std::string val;
|
||||
val.assign(buffer, len);
|
||||
return(val);
|
||||
}
|
||||
|
||||
string randomKey(int length)
|
||||
{
|
||||
std::string randomKey(int length) {
|
||||
unsigned char rnd;
|
||||
stringstream retval;
|
||||
ifstream random;
|
||||
std::stringstream retval { };
|
||||
std::ifstream random { };
|
||||
|
||||
//Under Linux and AIX /dev/random provides much more cryptographically secure random output than rand()
|
||||
random.open("/dev/random", ios::in|ios::binary);
|
||||
if(random.is_open()){
|
||||
for(int i=0; i<length; i++)
|
||||
{
|
||||
random.read(reinterpret_cast<char*>(&rnd), 1);
|
||||
retval <<HEX(rnd);
|
||||
// Under Linux and AIX /dev/random provides much more cryptographically secure
|
||||
// random output than rand()
|
||||
random.open("/dev/random", std::ios::in | std::ios::binary);
|
||||
if (random.is_open()) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
random.read(reinterpret_cast<char *>(&rnd), 1);
|
||||
retval << std::hex << rnd;
|
||||
}
|
||||
random.close();
|
||||
}else{
|
||||
cout<<"Enter random keys on the keyboard to seed the generator."<<endl<<"End by pressing enter..."<<endl;
|
||||
double check=0;
|
||||
char c=0;
|
||||
} else {
|
||||
std::cout << "Enter random keys on the keyboard to seed the "
|
||||
"generator.\nEnd by pressing enter..."
|
||||
<< std::endl;
|
||||
double check = 0;
|
||||
char c = 0;
|
||||
echo(false);
|
||||
while(c!=10){
|
||||
check+=(int)c;
|
||||
c=getchar();
|
||||
while (c != 10) {
|
||||
check += (int)c;
|
||||
c = getchar();
|
||||
}
|
||||
echo(true);
|
||||
srand(time(NULL)+(int)check);
|
||||
for(int i=0; i<length; i++)
|
||||
{
|
||||
retval <<HEX(rand() % 256);
|
||||
srand(time(NULL) + (int)check);
|
||||
for (int i = 0; i < length; i++) {
|
||||
retval << std::hex << (std::rand() % 256);
|
||||
}
|
||||
}
|
||||
retval << endl;
|
||||
return retval.str();
|
||||
retval << std::endl;
|
||||
return(retval.str());
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ GNU General Public License for more details.
|
||||
#define BSINTTOCHAR(x) (unsigned char)((x & 0xff000000)>>24), (unsigned char)((x & 0x00ff0000)>>16),(unsigned char)((x & 0x0000ff00)>>8),(unsigned char)(x & 0x000000ff)
|
||||
|
||||
|
||||
using namespace std;
|
||||
//using namespace std;
|
||||
|
||||
|
||||
void byteswap(unsigned char* array,int size,int value);
|
||||
@@ -71,7 +71,7 @@ bool moveTape(std::string tapeDevice,int count,bool dirForward);
|
||||
void outputSense(SCSI_PAGE_SENSE* sd);
|
||||
void readIOError(int err);
|
||||
|
||||
bool SCSIExecute(string tapedevice, unsigned char* cmd_p,int cmd_len,unsigned char* dxfer_p,int dxfer_len, bool cmd_to_device, bool show_error);
|
||||
bool SCSIExecute(std::string tapedevice, unsigned char* cmd_p,int cmd_len,unsigned char* dxfer_p,int dxfer_len, bool cmd_to_device, bool show_error);
|
||||
|
||||
typedef struct { //structure for setting data encryption
|
||||
unsigned char pageCode [2];
|
||||
@@ -144,7 +144,7 @@ unsigned char
|
||||
};
|
||||
|
||||
//Gets encryption options on the tape drive
|
||||
SSP_DES* SSPGetDES(string tapeDevice){
|
||||
SSP_DES* SSPGetDES(std::string tapeDevice){
|
||||
SSP_PAGE_BUFFER buffer;
|
||||
memset(&buffer,0,sizeof(SSP_PAGE_BUFFER));
|
||||
if(!SCSIExecute(tapeDevice,
|
||||
@@ -162,7 +162,7 @@ SSP_DES* SSPGetDES(string tapeDevice){
|
||||
}
|
||||
|
||||
//Gets encryption options on the tape drive
|
||||
SSP_NBES* SSPGetNBES(string tapeDevice,bool retry){
|
||||
SSP_NBES* SSPGetNBES(std::string tapeDevice,bool retry){
|
||||
|
||||
SSP_PAGE_BUFFER buffer;
|
||||
memset(&buffer,0,sizeof(SSP_PAGE_BUFFER));
|
||||
@@ -198,7 +198,7 @@ SSP_NBES* SSPGetNBES(string tapeDevice,bool retry){
|
||||
}
|
||||
|
||||
//Sends and inquiry to the device
|
||||
SCSI_PAGE_INQ* SCSIGetInquiry(string tapeDevice){
|
||||
SCSI_PAGE_INQ* SCSIGetInquiry(std::string tapeDevice){
|
||||
SCSI_PAGE_INQ* status=new SCSI_PAGE_INQ;
|
||||
memset(status,0,sizeof(SCSI_PAGE_INQ));
|
||||
if(!SCSIExecute(tapeDevice,
|
||||
@@ -216,7 +216,7 @@ SCSI_PAGE_INQ* SCSIGetInquiry(string tapeDevice){
|
||||
|
||||
|
||||
//Writes encryption options to the tape drive
|
||||
bool SCSIWriteEncryptOptions(string tapeDevice, SCSIEncryptOptions* eOptions){
|
||||
bool SCSIWriteEncryptOptions(std::string tapeDevice, SCSIEncryptOptions* eOptions){
|
||||
|
||||
char buffer[1024];
|
||||
memset(&buffer,0,1024);
|
||||
@@ -302,7 +302,7 @@ bool SCSIWriteEncryptOptions(string tapeDevice, SCSIEncryptOptions* eOptions){
|
||||
);
|
||||
}
|
||||
|
||||
bool SCSIExecute(string tapedrive, unsigned char* cmd_p,int cmd_len,unsigned char* dxfer_p,int dxfer_len, bool cmd_to_device, bool show_error)
|
||||
bool SCSIExecute(std::string tapedrive, unsigned char* cmd_p,int cmd_len,unsigned char* dxfer_p,int dxfer_len, bool cmd_to_device, bool show_error)
|
||||
{
|
||||
const char* tapedevice=tapedrive.c_str();
|
||||
int sg_fd,eresult,sresult,ioerr,retries;
|
||||
@@ -313,7 +313,7 @@ bool SCSIExecute(string tapedrive, unsigned char* cmd_p,int cmd_len,unsigned cha
|
||||
errno=0;
|
||||
sg_fd = open(tapedevice, O_RDONLY);
|
||||
if( sg_fd==-1){
|
||||
cerr<<"Could not open device '"<<tapedevice<<"': ";
|
||||
std::cerr<<"Could not open device '"<<tapedevice<<"': ";
|
||||
readIOError(errno);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -346,7 +346,7 @@ bool SCSIExecute(string tapedrive, unsigned char* cmd_p,int cmd_len,unsigned cha
|
||||
errno=0;
|
||||
sg_fd = openx((char*)tapedevice, O_RDONLY , NULL, SC_DIAGNOSTIC);
|
||||
if(!sg_fd || sg_fd==-1){
|
||||
cerr<<"Could not open device '"<<tapedevice<<"'"<<endl;
|
||||
std::cerr<<"Could not open device '"<<tapedevice<<"'"<<std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -394,18 +394,18 @@ bool SCSIExecute(string tapedrive, unsigned char* cmd_p,int cmd_len,unsigned cha
|
||||
#error "OS type is not set"
|
||||
#endif
|
||||
#ifdef DEBUGSCSI
|
||||
cout<<"SCSI Command: ";
|
||||
std::cout<<"SCSI Command: ";
|
||||
for(int i=0;i<cmd_len;i++){
|
||||
cout<<HEX(cmd_p[i]);
|
||||
std::cout << std::hex << cmd_p[i];
|
||||
}
|
||||
cout<<endl;
|
||||
std::cout<<"\n";
|
||||
|
||||
|
||||
cout<<"SCSI Data: ";
|
||||
std::cout<<"SCSI Data: ";
|
||||
for(int i=0;i<dxfer_len;i++){
|
||||
cout<<HEX(dxfer_p[i]);
|
||||
std::cout<< std::hex << (dxfer_p[i]);
|
||||
}
|
||||
cout<<endl;
|
||||
std::cout<<std::endl;
|
||||
#endif
|
||||
close(sg_fd);
|
||||
|
||||
@@ -441,7 +441,7 @@ void byteswap(unsigned char* array,int size,int value){
|
||||
|
||||
break;
|
||||
default:
|
||||
cout<<"Unhandled byte swap length of "<<size<<endl;
|
||||
std::cout<<"Unhandled byte swap length of "<<size<<std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -518,107 +518,114 @@ bool moveTape(std::string tapeDevice,int count,bool dirForward){
|
||||
|
||||
void readIOError(int err){
|
||||
if(err==0)return;
|
||||
cerr<<"ERROR: ";
|
||||
std::cerr<<"ERROR: ";
|
||||
switch(err){
|
||||
case EAGAIN:
|
||||
cerr<<"Device already open"<<endl;
|
||||
std::cerr<<"Device already open"<<std::endl;
|
||||
break;
|
||||
case EBUSY:
|
||||
cerr<<"Device Busy"<<endl;
|
||||
std::cerr<<"Device Busy"<<std::endl;
|
||||
break;
|
||||
case ETIMEDOUT:
|
||||
cerr<<"Device operation timed out"<<endl;
|
||||
std::cerr<<"Device operation timed out"<<std::endl;
|
||||
break;
|
||||
case EIO:
|
||||
cerr<<"Device I/O Error."<<endl;
|
||||
std::cerr<<"Device I/O Error."<<std::endl;
|
||||
break;
|
||||
case EPERM:
|
||||
cerr<<"You do not have privileges to do this. Are you root?"<<endl;
|
||||
std::cerr<<"You do not have privileges to do this. Are you root?"<<std::endl;
|
||||
break;
|
||||
#ifdef OS_AIX
|
||||
case EBADF:
|
||||
cerr<<"EBADF"<<endl;
|
||||
std::cerr<<"EBADF"<<std::endl;
|
||||
break;
|
||||
case EFAULT:
|
||||
cerr<<"EFAULT"<<endl;
|
||||
std::cerr<<"EFAULT"<<std::endl;
|
||||
break;
|
||||
case EINTR:
|
||||
cerr<<"EINTR"<<endl;
|
||||
std::cerr<<"EINTR"<<std::endl;
|
||||
break;
|
||||
case EINVAL:
|
||||
cerr<<"Invalid device"<<endl;
|
||||
std::cerr<<"Invalid device"<<std::endl;
|
||||
break;
|
||||
|
||||
case ENOTTY:
|
||||
cerr<<"ENOTTY"<<endl;
|
||||
std::cerr<<"ENOTTY"<<std::endl;
|
||||
break;
|
||||
|
||||
case ENODEV:
|
||||
cerr<<"Device is not responding"<<endl;
|
||||
std::cerr<<"Device is not responding"<<std::endl;
|
||||
break;
|
||||
|
||||
case ENXIO:
|
||||
cerr<<"ENXIO"<<endl;
|
||||
std::cerr<<"ENXIO"<<std::endl;
|
||||
break;
|
||||
|
||||
#endif
|
||||
default:
|
||||
if(errno!=0){
|
||||
cerr<<"0x"<<hex<<errno<<" "<<strerror(errno)<<endl;
|
||||
std::cerr<<"0x"<<std::hex<<errno<<" "<<strerror(errno)<<std::endl;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
void outputSense(SCSI_PAGE_SENSE* sd){
|
||||
cerr<<left<<setw(25)<<"Sense Code: ";
|
||||
std::cerr<<std::left<<std::setw(25)<<"Sense Code: ";
|
||||
|
||||
switch((int)sd->senseKey){
|
||||
case 0:
|
||||
cerr<<"No specific error";
|
||||
std::cerr<<"No specific error";
|
||||
break;
|
||||
case 2:
|
||||
cerr<<"Device not ready";
|
||||
std::cerr<<"Device not ready";
|
||||
break;
|
||||
case 3:
|
||||
cerr<<"Medium Error";
|
||||
std::cerr<<"Medium Error";
|
||||
break;
|
||||
case 4:
|
||||
cerr<<"Hardware Error";
|
||||
std::cerr<<"Hardware Error";
|
||||
break;
|
||||
case 5:
|
||||
cerr<<"Illegal Request";
|
||||
std::cerr<<"Illegal Request";
|
||||
break;
|
||||
case 6:
|
||||
cerr<<"Unit Attention";
|
||||
std::cerr<<"Unit Attention";
|
||||
break;
|
||||
case 7:
|
||||
cerr<<"Data protect";
|
||||
std::cerr<<"Data protect";
|
||||
break;
|
||||
case 8:
|
||||
cerr<<"Blank tape";
|
||||
std::cerr<<"Blank tape";
|
||||
break;
|
||||
|
||||
}
|
||||
cerr<<" (0x"<<HEX(sd->senseKey)<<")"<<endl;
|
||||
cerr<<left<<setw(25)<<" ASC:"<<"0x"<<HEX(sd->addSenseCode)<<endl;
|
||||
cerr<<left<<setw(25)<<" ASCQ:"<<"0x"<<HEX(sd->addSenseCodeQual)<<endl;
|
||||
|
||||
std::cerr<<" (0x"<<std::hex << (sd->senseKey);
|
||||
std::cerr<<")\n";
|
||||
|
||||
std::cerr<<std::left<<std::setw(25)<<" ASC:"<<"0x"<<std::hex<< (sd->addSenseCode);
|
||||
std::cerr<<"\n";
|
||||
|
||||
std::cerr<<std::left<<std::setw(25)<<" ASCQ:"<<"0x"<<std::hex << (sd->addSenseCodeQual);
|
||||
std::cerr<<"\n";
|
||||
|
||||
if(sd->addSenseLen>0){
|
||||
cerr<<left<<setw(25)<<" Additional data:"<<"0x";
|
||||
std::cerr<<std::left<<std::setw(25)<<" Additional data:"<<"0x";
|
||||
|
||||
for(int i=0;i<sd->addSenseLen;i++){
|
||||
cerr<<HEX(sd->addSenseData[i]);
|
||||
std::cerr<< std::hex << (sd->addSenseData[i]);
|
||||
}
|
||||
cerr<<endl;
|
||||
std::cerr<<"\n";
|
||||
}
|
||||
#ifdef DEBUGSCSI
|
||||
cerr<<left<<setw(25)<<" Raw Sense:"<<"0x";
|
||||
std::cerr<<std::left<<std::setw(25)<<" Raw Sense:"<<"0x";
|
||||
char* rawsense=(char*)sd;
|
||||
|
||||
for(int i=0;i<sizeof(SCSI_PAGE_SENSE);i++){
|
||||
cerr<<HEX(rawsense[i]);
|
||||
std::cerr<< std::hex << (rawsense[i]);
|
||||
}
|
||||
cerr<<endl;
|
||||
std::cerr<<"\n";
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
Name: stenc
|
||||
Version: 1.0.8
|
||||
Version: 1.0.9
|
||||
Release: 3%{?dist}
|
||||
Summary: SCSI Tape Encryption Manager
|
||||
|
||||
|
||||
Reference in New Issue
Block a user