zuluCrypt  5.7.1
About: zuluCrypt is a simple but feature rich solution for hard drives encryption. It can manage PLAIN dm-crypt, LUKS, TrueCrypt and VeraCrypt encrypted volumes.
  Fossies Dox: zuluCrypt-5.7.1.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

write_device_with_junk.c
Go to the documentation of this file.
1 
2 /*
3  *
4  * Copyright (c) 2012-2015
5  * name : Francis Banyikwa
6  * email: mhogomchungu@gmail.com
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "includes.h"
22 #include "../lib/includes.h"
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <blkid/blkid.h>
28 #include <libintl.h>
29 #include <locale.h>
30 #include <stdio.h>
31 #include <sys/stat.h>
32 
33 #include <libcryptsetup.h>
34 
35 #define SIZE 1024
36 #define KEY_SIZE 128
37 
38 #include <signal.h>
39 
40 #define _ignore_result( x ) if( x ){;}
41 
42 static int __exit_as_requested ;
43 static int __sig_caught ;
44 
45 void sigTERMhandler( int sig )
46 {
48  __sig_caught = sig ;
49  printf( "\nINFO: caught signal %d,cleaning up\n",__sig_caught ) ;
50 }
51 
52 /*
53  * define in partition.c
54  *
55  * the function is used to check is a presented path is a system partition or not *
56  */
57 
58 static int zuluExit( stringList_t stl, int status )
59 {
60  ssize_t index ;
61  switch( status ){
62  case 0 : printf( gettext( "SUCCESS: Mapper created successfully\n" ) ) ;
63  index = StringListHasStartSequence( stl,crypt_get_dir() ) ;
64  if( index >= 0 ){
65  printf( gettext( "Opened mapper path: " ) ) ;
66  StringListPrintLineAt( stl,index ) ;
67  }
68  break ;
69  case 1 : printf( gettext( "ERROR: Could not create mapper\n" ) ) ;break ;
70  case 2 : printf( gettext( "ERROR: Could not resolve device path\n" ) ) ;break ;
71  case 3 : printf( gettext( "\nSUCCESS: Random data successfully written\n" ) ) ;break ;
72  case 5 : printf( gettext( "INFO: User chose not to proceed\n" ) ) ;break ;
73  case 8 : printf( gettext( "ERROR: Insufficitied privilege to oped device \n" ) ) ;break ;
74  case 9 : printf( gettext( "ERROR: Device path is invalid\n" ) ) ;break ;
75  case 10: printf( gettext( "ERROR: Passphrase file does not exist\n" ) ) ;break ;
76  case 11: printf( gettext( "ERROR: Could not get enought memory to hold the key file\n" ) ) ;break ;
77  case 12: printf( gettext( "ERROR: Insufficient privilege to open key file for reading\n" ) ) ;break ;
78  case 13: printf( gettext( "ERROR: Can not open a mapper on a device with an opened mapper\n" ) ) ;break ;
79  case 14: printf( gettext( "ERROR: Can not open a mapper on a mounted device\n" ) ) ;break ;
80  case 15: printf( gettext( "INFO: Signal caught,exiting prematurely\n" ) ) ;break ;
81  case 16: printf( gettext( "ERROR: Can not get passphrase in silent mode\n" ) ) ;break ;
82  case 17: printf( gettext( "ERROR: Insufficient memory to hold passphrase\n" ) ) ;break ;
83  case 18: printf( gettext( "ERROR: Insufficient memory to hold 3 characters?really?\n" ) ) ;break ;
84  case 19: printf( gettext( "ERROR: Insufficient privilege to open the file with your privileges?\n" ) ) ;break ;
85  }
86 
87  StringListClearDelete( &stl ) ;
88  return status ;
89 }
90 
91 static int open_plain_as_me_1(const struct_opts * opts,const char * mapping_name,uid_t uid,int op )
92 {
93  /*
94  * Below is a form of memory management.All strings are collected in a stringlist object to easily delete them
95  * when the function returns.This allows for the function to have multiple exit points without risks of leaking
96  * memory from manually examining each exit point to make sure all strings are deleted or go with multiple goto
97  * code deleting blocks to take into account different exit points.
98  */
99  stringList_t stl ;
100  string_t * stringArray = StringListArray( &stl,5 ) ;
101  string_t * mapper = &stringArray[ 0 ] ;
102  string_t * passphrase = &stringArray[ 1 ] ;
103  string_t * p = &stringArray[ 2 ] ;
104  string_t * dev_st = &stringArray[ 3 ] ;
105  string_t * dev_1 = &stringArray[ 4 ] ;
106 
107  size_t len = 0 ;
108 
109  const char * source = opts->key_source ;
110  const char * pass = opts->key ;
111 
112  int k = opts->ask_confirmation ;
113 
114  const char * cpass = NULL ;
115 
116  char * d ;
117 
118  const char * device = opts->device ;
119  const char * dev = opts->device ;
120 
121  int j ;
122  int n ;
123 
124  const char * cmapper ;
125 
126  if( StringPrefixEqual( device,"/dev/loop" ) ){
127  /*
128  * zuluCryptLoopDeviceAddress() is defined in ../lib/create_loop_device.c
129  */
130  d = zuluCryptLoopDeviceAddress( device ) ;
131  *dev_st = StringInherit( &d ) ;
132  dev = StringContent( *dev_st ) ;
133  *dev_1 = StringCopy( *dev_st ) ;
134  device = StringReplaceString( * dev_1,"\\040"," " ) ;
135  }
136 
137  /*
138  * zuluCryptPartitionIsSystemPartition() is defined in ./partition.c
139  */
140  if( zuluCryptPartitionIsSystemPartition( device,uid ) ){
141 
143 
144  if( uid != 0 ){
145 
146  return zuluExit( stl,8 ) ;
147  }
148  }
149  }
150  /*
151  * ZULUCRYPTlongMapperPath and ZULUCRYPTshortMapperPath are in ../constants.h
152  * zuluCryptCreateMapperName() is defined at ../lib/create_mapper_name.c
153  */
154  *mapper = zuluCryptCreateMapperName( device,mapping_name,uid,ZULUCRYPTshortMapperPath ) ;
155 
156  *p = zuluCryptCreateMapperName( device,mapping_name,uid,ZULUCRYPTlongMapperPath ) ;
157 
159 
160  /*
161  * zuluCryptPartitionIsMounted() is defined in ../lib/process_mountinfo.c
162  */
163  n = zuluCryptPartitionIsMounted( dev ) ;
164 
165  if( j == 1 ){
166  return zuluExit( stl,13 ) ;
167  }
168  if( n == 1 ){
169  return zuluExit( stl,14 ) ;
170  }
171  if( k == 0 ){
172  *passphrase = StringRandomString( 64 ) ;
173  cpass = StringContent( *passphrase ) ;
174  len = StringLength( *passphrase ) ;
175  }else if( source == NULL ){
176  printf( gettext( "Enter passphrase: " ) ) ;
177  /*
178  * ZULUCRYPT_KEY_MAX_SIZE is set in ../constants.h
179  */
181  case 1 : return zuluExit( stl,16 ) ;
182  case 2 : return zuluExit( stl,17 ) ;
183  }
184  printf( "\n" ) ;
185  cpass = StringContent( *passphrase ) ;
186  len = StringLength( *passphrase ) ;
187  }else{
188  if( strcmp( source,"-p" ) == 0 ){
189  *passphrase = String( pass ) ;
190  cpass = StringContent( *passphrase ) ;
191  len = StringLength( *passphrase ) ;
192  }else if( strcmp( source,"-f" ) == 0 ){
193  /*
194  * zuluCryptGetPassFromFile() is defined at "path_access.c"
195  */
196  switch( zuluCryptGetPassFromFile( NULL,pass,uid,passphrase ) ){
197  case 1 : return zuluExit( stl,10 ) ;
198  case 2 : return zuluExit( stl,11 ) ;
199  case 4 : return zuluExit( stl,12 ) ;
200  }
201  cpass = StringContent( *passphrase ) ;
202  len = StringLength( *passphrase ) ;
203  }
204  }
205 
207  /*
208  * zuluCryptOpenPlain() is defined in ../lib/open_plain.c
209  */
210  if( zuluCryptOpenPlain( device,StringContent( *mapper ),"rw",cpass,len ) != 0 ){
212  return zuluExit( stl,1 ) ;
213  }
214  }
215 
217 
218  /*
219  * Create a mapper path(usually at /dev/mapper) associated with opened plain mapper above.
220  */
221  cmapper = StringMultiplePrepend( *mapper,"/",crypt_get_dir(),NULL ) ;
222 
223  /*
224  * mapper path is usually a soft link to /dev/dm-X
225  * resolve the mapper path to its respective /dev/dm-X and set permission on it.
226  *
227  * We set permission of /dev/dm-X pointing to the device to "u+rw" because we want notmal user to be able
228  * to write to the device through the mapper.
229  *
230  * Useful when a normal user want to delete content of the device by writing random data to it.
231  */
232  d = zuluCryptRealPath( cmapper ) ;
234  if( d != NULL ){
235  _ignore_result( chown( d,uid,0 ) ) ;
236  _ignore_result( chmod( d,S_IRWXU ) ) ;
237  StringFree( d ) ;
238  }
240  }else{
241  return zuluExit( stl,1 ) ;
242  }
243 
244  if( op == 1 ){
245  return zuluExit( stl,0 ) ;
246  }else{
247  StringListClearDelete( &stl ) ;
248  return 0 ;
249  }
250 }
251 
252 int zuluCryptEXEOpenPlainAsMe(const struct_opts * opts,const char * mapping_name,uid_t uid )
253 {
254  return open_plain_as_me_1( opts,mapping_name,uid,1 ) ;
255 }
256 
257 /*
258  * Purpose of this function is to open a device and write random data to it as a way of hiding information on the disk.
259  *
260  * The above is accomplished by opening a plain mapper against the device and then write to the device through the mapper
261  *
262  */
263 int zuluCryptEXEWriteDeviceWithJunk( const struct_opts * opts,const char * mapping_name,uid_t uid )
264 {
265  stringList_t stl = StringListInit() ;
266  string_t * mapper = StringListAssign( stl ) ;
267  string_t * confirm = StringListAssign( stl );
268 
269  double size ;
270  double size_written ;
271 
272  const char * device = opts->device ;
273 
274  char buffer[ SIZE ] ;
275 
276  int ratio ;
277  int prev_ratio ;
278  int k ;
279 
280  struct sigaction sigac;
281 
282  memset( &sigac,'\0',sizeof( struct sigaction ) ) ;
283 
284  sigac.sa_handler = &sigTERMhandler ;
285 
286  sigaction( SIGINT,&sigac,NULL ) ;
287  sigaction( SIGTERM,&sigac,NULL ) ;
288  sigaction( SIGHUP,&sigac,NULL ) ;
289 
290  __exit_as_requested = 0 ;
291 
292  if( ( k = open_plain_as_me_1( opts,mapping_name,uid,0 ) ) != 0 ){
293  return k ;
294  }
295  *mapper = zuluCryptCreateMapperName( device,mapping_name,uid,ZULUCRYPTshortMapperPath ) ;
296 
297  StringMultiplePrepend( *mapper,"/",crypt_get_dir(),NULL ) ;
298 
299  if( opts->ask_confirmation ){
300  printf( gettext( "\nWARNING, device \"%s\" will be overwritten with random data destroying all present data.\n" ),device ) ;
301  printf( gettext( "Are you sure you want to proceed? Type \"YES\" and press enter if you are sure: " ) ) ;
302 
303  *confirm = StringGetFromTerminal_1( 3 ) ;
304  if( *confirm == StringVoid ){
305  return zuluExit( stl,17 ) ;
306  }else{
307  k = StringsAreEqual_2( *confirm,gettext( "YES" ) ) ;
308 
309  if( k == 0 ){
311  zuluCryptCloseMapper( StringContent( *mapper ) ) ;
313  }
314  return zuluExit( stl,5 ) ;
315  }
316  }
317  }
318 
319  k = open( StringContent( *mapper ),O_WRONLY ) ;
320 
321  size = ( double ) blkid_get_dev_size( k ) ;
322 
323  memset( buffer,0,SIZE ) ;
324 
325  size_written = 0 ;
326  prev_ratio = -1 ;
327 
328  while( write( k,buffer,SIZE ) > 0 ){
329 
330  if( __exit_as_requested == 1 ){
331  break ;
332  }
333  size_written += SIZE ;
334 
335  ratio = ( int ) ( ( size_written / size ) * 100 ) ;
336 
337  if( ratio > prev_ratio ){
338  printf( "\r%s %d%%",gettext( "percentage complete: " ),ratio ) ;
339  fflush( stdout );
340  prev_ratio = ratio ;
341  }
342  }
343 
344  close( k ) ;
346  zuluCryptCloseMapper( StringContent( *mapper ) ) ;
348  }
349 
350  if( __exit_as_requested == 1 ){
351  return zuluExit( stl,15 ) ;
352  }else{
353  return zuluExit( stl,3 ) ;
354  }
355 }
StringSilentlyGetFromTerminal_1
int StringSilentlyGetFromTerminal_1(string_t *q, size_t s)
Definition: String.c:1752
zuluCryptEXEWriteDeviceWithJunk
int zuluCryptEXEWriteDeviceWithJunk(const struct_opts *opts, const char *mapping_name, uid_t uid)
Definition: write_device_with_junk.c:263
StringListAssign
string_t * StringListAssign(stringList_t stl)
Definition: StringList.c:221
StringsAreEqual_2
int StringsAreEqual_2(string_t x, const char *y)
Definition: String.c:1469
__exit_as_requested
static int __exit_as_requested
Definition: write_device_with_junk.c:42
_ignore_result
#define _ignore_result(x)
Definition: write_device_with_junk.c:40
struct_opts_1::key
const char * key
Definition: libzuluCrypt-exe.h:42
StringCopy
string_t StringCopy(string_t st)
Definition: String.c:260
struct_opts_1::device
const char * device
Definition: libzuluCrypt-exe.h:37
StringLength
size_t StringLength(string_t st)
Definition: String.c:678
StringRandomString
string_t StringRandomString(size_t size)
Definition: String.c:1771
__sig_caught
static int __sig_caught
Definition: write_device_with_junk.c:43
struct_opts_1
Definition: libzuluCrypt-exe.h:35
zuluExit
static int zuluExit(stringList_t stl, int status)
Definition: write_device_with_junk.c:58
struct_opts_1::key_source
const char * key_source
Definition: libzuluCrypt-exe.h:41
zuluCryptCreateMapperName
string_t zuluCryptCreateMapperName(const char *device, const char *mapping_name, uid_t uid, int i)
Definition: create_mapper_name.c:42
zuluCryptOpenPlain
int zuluCryptOpenPlain(const char *device, const char *mapping_name, const char *mode, const char *passphrase, size_t passphrase_size)
Definition: open_plain.c:222
StringListInit
stringList_t StringListInit(void)
Definition: StringList.c:293
StringListHasStartSequence
ssize_t StringListHasStartSequence(stringList_t stl, const char *str)
Definition: StringList.c:869
sigTERMhandler
void sigTERMhandler(int sig)
Definition: write_device_with_junk.c:45
zuluCryptEXEOpenPlainAsMe
int zuluCryptEXEOpenPlainAsMe(const struct_opts *opts, const char *mapping_name, uid_t uid)
Definition: write_device_with_junk.c:252
StringInherit
string_t StringInherit(char **data)
Definition: String.c:391
StringContent
static const __inline__ char * StringContent(string_t st)
Definition: String.h:1011
StringType
Definition: String.c:49
zuluCryptSecurityDropElevatedPrivileges
int zuluCryptSecurityDropElevatedPrivileges(void)
Definition: security.c:109
StringListPrintLineAt
void StringListPrintLineAt(stringList_t stl, size_t index)
Definition: StringList.c:1343
String
string_t String(const char *cstring)
Definition: String.c:318
struct_opts_1::ask_confirmation
int ask_confirmation
Definition: libzuluCrypt-exe.h:62
siritask::status
status
Definition: siritask.h:114
zuluCryptSecurityGainElevatedPrivileges
int zuluCryptSecurityGainElevatedPrivileges(void)
Definition: security.c:64
StringListArray
string_t * StringListArray(stringList_t *stz, size_t arraySize)
Definition: StringList.c:244
ZULUCRYPT_KEY_MAX_SIZE
#define ZULUCRYPT_KEY_MAX_SIZE
Definition: constants.h:49
StringReplaceString
const char * StringReplaceString(string_t st, const char *x, const char *s)
Definition: String.c:1318
StringListType
Definition: StringList.c:33
zuluCryptCloseMapper
int zuluCryptCloseMapper(const char *mapper)
Definition: close_mapper.c:84
open_plain_as_me_1
static int open_plain_as_me_1(const struct_opts *opts, const char *mapping_name, uid_t uid, int op)
Definition: write_device_with_junk.c:91
StringMultiplePrepend
const char * StringMultiplePrepend(string_t st,...)
Definition: String.c:1159
zuluCryptPartitionIsMounted
int zuluCryptPartitionIsMounted(const char *device)
Definition: mountinfo.c:205
zuluCryptGetPassFromFile
int zuluCryptGetPassFromFile(int *socket_path, const char *path, uid_t uid, string_t *st)
Definition: path_access.c:121
zuluCryptPartitionIsSystemPartition
int zuluCryptPartitionIsSystemPartition(const char *dev, uid_t uid)
Definition: volumes.c:1143
zuluCryptCheckOpenedMapper
int zuluCryptCheckOpenedMapper(const char *mapper)
Definition: check_opened_mapper.c:23
SIZE
#define SIZE
Definition: write_device_with_junk.c:35
StringListClearDelete
void StringListClearDelete(stringList_t *stl)
Definition: StringList.c:1216
zuluCryptRealPath
char * zuluCryptRealPath(const char *path)
Definition: real_path.c:28
StringGetFromTerminal_1
string_t StringGetFromTerminal_1(size_t s)
Definition: String.c:1675
includes.h
zuluCryptExeOriginalUserIsNotRoot
int zuluCryptExeOriginalUserIsNotRoot()
Definition: security.c:59
ZULUCRYPTshortMapperPath
#define ZULUCRYPTshortMapperPath
Definition: constants.h:35
zuluCryptLoopDeviceAddress
char * zuluCryptLoopDeviceAddress(const char *device)
Definition: create_loop_device.c:134
ZULUCRYPTlongMapperPath
#define ZULUCRYPTlongMapperPath
Definition: constants.h:36
StringPrefixEqual
static __inline__ int StringPrefixEqual(const char *x, const char *y)
Definition: String.h:905
StringFree
static __inline__ void StringFree(const void *str)
Definition: String.h:823
StringVoid
#define StringVoid
Definition: String.h:47