2010-02-02 02:14:28 +03:00
/*
smbta - util : tool for controlling encryption with
vfs_smb_traffic_analyzer
Copyright ( C ) 2010 Holger Hetterich < hhetter @ novell . com >
This program 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 3 of the License , or
( at your option ) any later version .
This program 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 , see < http : //www.gnu.org/licenses/>. */
# include "includes.h"
2010-08-05 04:25:37 +04:00
# include "secrets.h"
2010-02-02 02:14:28 +03:00
2010-03-17 00:17:55 +03:00
static void delete_key ( void ) ;
2010-02-02 02:14:28 +03:00
2011-03-23 00:02:13 +03:00
static void help ( void )
2010-02-02 02:14:28 +03:00
{
printf ( " -h print this help message. \n " ) ;
printf ( " -f <file> install the key from a file and activate \n " ) ;
printf ( " encryption. \n " ) ;
printf ( " -g <file> generate a key, save it to a file, and activate encryption. \n " ) ;
printf ( " -u uninstall a key, and deactivate encryption. \n " ) ;
printf ( " -c <file> create a file from an installed key. \n " ) ;
printf ( " -s check if a key is installed, and print the key to stdout. \n " ) ;
printf ( " \n " ) ;
}
2011-03-23 00:02:13 +03:00
static void check_key ( void )
2010-02-02 02:14:28 +03:00
{ size_t size ;
2010-03-17 00:17:55 +03:00
char * akey ;
2010-02-02 02:14:28 +03:00
if ( ! secrets_init ( ) ) {
printf ( " Error opening secrets database. " ) ;
exit ( 1 ) ;
}
2010-03-17 00:17:55 +03:00
akey = ( char * ) secrets_fetch ( " smb_traffic_analyzer_key " , & size ) ;
2010-02-02 02:14:28 +03:00
if ( akey ! = NULL ) {
printf ( " A key is installed: %s \n " , akey ) ;
printf ( " Encryption activated. \n " ) ;
free ( akey ) ;
exit ( 0 ) ;
} else printf ( " No key is installed. \n " ) ;
exit ( 1 ) ;
}
static void create_keyfile ( char * filename , char * key )
{
FILE * keyfile ;
keyfile = fopen ( filename , " w " ) ;
if ( keyfile = = NULL ) {
printf ( " error creating the keyfile! \n " ) ;
2010-03-16 16:58:34 +03:00
exit ( 1 ) ;
2010-02-02 02:14:28 +03:00
}
fprintf ( keyfile , " %s " , key ) ;
fclose ( keyfile ) ;
printf ( " File '%s' has been created. \n " , filename ) ;
}
/**
* Load a key from a file . The caller has to free the
* returned string .
*/
2010-03-16 20:27:00 +03:00
static void load_key_from_file ( char * filename , char * key )
2010-02-02 02:14:28 +03:00
{
FILE * keyfile ;
int l ;
keyfile = fopen ( filename , " r " ) ;
if ( keyfile = = NULL ) {
printf ( " Error opening the keyfile! \n " ) ;
2010-03-16 16:58:34 +03:00
exit ( 1 ) ;
2010-02-02 02:14:28 +03:00
}
l = fscanf ( keyfile , " %s " , key ) ;
if ( strlen ( key ) ! = 16 ) {
printf ( " Key file in wrong format \n " ) ;
fclose ( keyfile ) ;
2010-03-16 16:58:34 +03:00
exit ( 1 ) ;
2010-02-02 02:14:28 +03:00
}
2011-01-20 01:13:47 +03:00
fclose ( keyfile ) ;
2010-02-02 02:14:28 +03:00
}
static void create_file_from_key ( char * filename )
{
size_t size ;
char * akey = ( char * ) secrets_fetch ( " smb_traffic_analyzer_key " , & size ) ;
if ( akey = = NULL ) {
printf ( " No key is installed! Can't create file. \n " ) ;
exit ( 1 ) ;
}
create_keyfile ( filename , akey ) ;
free ( akey ) ;
}
/**
* Generate a random key . The user has to free the returned
* string .
*/
2010-03-16 20:27:00 +03:00
static void generate_key ( char * key )
2010-02-02 02:14:28 +03:00
{
int f ;
srand ( ( unsigned ) time ( NULL ) ) ;
for ( f = 0 ; f < 16 ; f + + ) {
* ( key + f ) = ( rand ( ) % 128 ) + 32 ;
}
* ( key + 16 ) = ' \0 ' ;
printf ( " Random key generated. \n " ) ;
}
static void create_new_key_and_activate ( char * filename )
{
2010-03-16 20:27:00 +03:00
char key [ 17 ] = { 0 } ;
2010-02-02 02:14:28 +03:00
if ( ! secrets_init ( ) ) {
printf ( " Error opening secrets database. " ) ;
exit ( 1 ) ;
}
2010-03-16 20:27:00 +03:00
generate_key ( key ) ;
2010-02-02 02:14:28 +03:00
delete_key ( ) ;
secrets_store ( " smb_traffic_analyzer_key " , key , strlen ( key ) + 1 ) ;
printf ( " Key installed, encryption activated. \n " ) ;
create_file_from_key ( filename ) ;
}
2010-03-17 00:17:55 +03:00
static void delete_key ( void )
2010-02-02 02:14:28 +03:00
{
size_t size ;
char * akey = ( char * ) secrets_fetch ( " smb_traffic_analyzer_key " , & size ) ;
if ( akey ! = NULL ) {
free ( akey ) ;
secrets_delete ( " smb_traffic_analyzer_key " ) ;
printf ( " Removed installed key. Encryption deactivated. \n " ) ;
} else {
printf ( " No key is installed. \n " ) ;
}
}
static void load_key_from_file_and_activate ( char * filename )
{
2010-03-16 20:27:00 +03:00
char key [ 17 ] = { 0 } ;
2010-02-02 02:14:28 +03:00
char * akey ;
size_t size ;
2010-03-16 20:27:00 +03:00
load_key_from_file ( filename , key ) ;
2010-02-02 02:14:28 +03:00
printf ( " Loaded key from %s. \n " , filename ) ;
akey = ( char * ) secrets_fetch ( " smb_traffic_analyzer_key " , & size ) ;
if ( akey ! = NULL ) {
printf ( " Removing the old key. \n " ) ;
delete_key ( ) ;
2011-03-27 22:44:01 +04:00
SAFE_FREE ( akey ) ;
2010-02-02 02:14:28 +03:00
}
printf ( " Installing the key from file %s \n " , filename ) ;
secrets_store ( " smb_traffic_analyzer_key " , key , strlen ( key ) + 1 ) ;
}
static void process_arguments ( int argc , char * * argv )
{
char co ;
while ( ( co = getopt ( argc , argv , " hf:g:uc:s " ) ) ! = EOF ) {
switch ( co ) {
case ' h ' :
help ( ) ;
exit ( 0 ) ;
case ' s ' :
check_key ( ) ;
break ;
case ' g ' :
create_new_key_and_activate ( optarg ) ;
break ;
case ' u ' :
delete_key ( ) ;
break ;
case ' c ' :
create_file_from_key ( optarg ) ;
break ;
case ' f ' :
load_key_from_file_and_activate ( optarg ) ;
break ;
default :
help ( ) ;
break ;
}
}
}
int main ( int argc , char * * argv )
{
sec_init ( ) ;
load_case_tables ( ) ;
if ( ! lp_load_initial_only ( get_dyn_CONFIGFILE ( ) ) ) {
fprintf ( stderr , " Can't load %s - run testparm to debug it \n " ,
get_dyn_CONFIGFILE ( ) ) ;
exit ( 1 ) ;
}
if ( argc = = 1 ) {
help ( ) ;
exit ( 1 ) ;
}
process_arguments ( argc , argv ) ;
2010-03-17 00:17:55 +03:00
exit ( 0 ) ;
2010-02-02 02:14:28 +03:00
}