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)  

zuluSafe.c
Go to the documentation of this file.
1 
2 /*
3  *
4  * Copyright (c) 2014-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 <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <termios.h>
29 #include <dirent.h>
30 #include <sys/mman.h>
31 #include <locale.h>
32 #include <libintl.h>
33 #include <locale.h>
34 #include <linux/limits.h>
35 
36 #include "zuluwallet.h"
37 #include "version.h"
38 
39 #define PASSWORD_SIZE 512
40 #define WALLET_NAME_SIZE 512
41 
42 #define APPLICATION_NAME "zuluSafe"
43 
44 #define _file( x ) x,strlen( x ) + 1
45 
46 #define StringsAreEqual( x,y ) strcmp( x,y ) == 0
47 #define StringsAreNotEqual( x,y ) strcmp( x,y ) != 0
48 
49 static void _write( int x,const void * y,size_t z )
50 {
51  if( write( x,y,z ) ){;}
52 }
53 static void _read( int x,void * y,size_t z )
54 {
55  if( read( x,y,z ) ){;}
56 }
57 
58 static void _help( void )
59 {
60  const char * help2 ;
61  const char * help1 ;
62 
63  help1 = gettext( "\n\n\
64 This is a simple tool for secure storage of files in a wallet.Added files to the wallet\n\
65 will be stored in a secured file located at \"~/.config/lxqt/wallets/zuluSafe/YYY.lwt\" where YYY is\n\
66 wallet name\n\n" ) ;
67 
68  help2 = gettext( "\
69 To add a file to a wallet,run : zuluSafe-cli --add <path to file>\n\
70 To delete a file in a wallet,run : zuluSafe-cli --delete <file name>\n\
71 To get a file from the wallet,run : zuluSafe-cli --get <file name>\n\
72 To add all files in a folder,run : zuluSafe-cli --add-all <folder path>\n\
73 To get a list of files in the wallet,run : zuluSafe-cli --list\n\
74 To get all files from the wallet,run : zuluSafe-cli --get-all\n\
75 To get a list of wallets,run : zuluSafe-cli --wallets\n" ) ;
76 
77  printf( "\n%s%s\n%s",VERSION_STRING,help1,help2 ) ;
78 }
79 
80 static void _clearKeyBoardBuffer( void )
81 {
82  int c ;
83  while( 1 ){
84  c = getchar() ;
85  if( c == '\n' || c == EOF ){
86  break ;
87  }
88  }
89 }
90 
91 static void _getInputFromUser( char * buffer,size_t size,size_t * len )
92 {
93  char c ;
94  char * e = buffer ;
95  const char * f = e + size ;
96 
97  while( e != f ){
98  c = getchar() ;
99  if( c == '\n' || c == EOF ){
100  break ;
101  }else{
102  *e++ = c ;
103  }
104  }
105 
106  *e = '\0' ;
107 
108  if( len != NULL ){
109  *len = e - buffer ;
110  }
111 }
112 
113 static int _terminalEchoOff( struct termios * old,struct termios * new )
114 {
115  if( tcgetattr ( 1,old ) != 0 ){
116  return 1 ;
117  }
118 
119  *new = *old;
120  new->c_lflag &= ~ECHO;
121 
122  if( tcsetattr ( 1,TCSAFLUSH,new ) != 0 ){
123  return 1 ;
124  }else{
125  return 0 ;
126  }
127 }
128 
129 static int _getPassWordFromUser( char * password,size_t size,size_t * len )
130 {
131  struct termios old ;
132  struct termios new ;
133 
134  if( _terminalEchoOff( &old,&new ) == 1 ){
135  puts( gettext( "failed to read password" ) ) ;
136  return 1 ;
137  }else{
138  _getInputFromUser( password,size,len ) ;
139  tcsetattr( 1,TCSAFLUSH,&old );
140  return 0 ;
141  }
142 }
143 
144 static int _open_wallet( lxqt_wallet_t * wallet,const char * password,size_t password_length,const char * wallet_name )
145 {
146  lxqt_wallet_error r = lxqt_wallet_open( wallet,password,password_length,wallet_name,APPLICATION_NAME ) ;
147  if( r != lxqt_wallet_no_error ){
148  puts( gettext( "wrong password,failed to open wallet" ) ) ;
149  return 1 ;
150  }else{
151  return 0 ;
152  }
153 }
154 
156 {
157  size_t password_length = 0 ;
159 
160  int c ;
161 
162  char password[ PASSWORD_SIZE + 1 ] ;
163  char password_1[ PASSWORD_SIZE + 1 ] ;
164 
165  char wallet_name[ WALLET_NAME_SIZE + 1 ] ;
166 
167  printf( gettext( "enter wallet name: " ) ) ;
168  _getInputFromUser( wallet_name,WALLET_NAME_SIZE,NULL ) ;
169 
170  if( lxqt_wallet_exists( wallet_name,APPLICATION_NAME ) != 0 ){
171  printf( gettext( "wallet \"%s\" does not exist,do you want to create it?(y/n): " ),wallet_name ) ;
172  c = getchar() ;
174  if( c == 'y' ){
175  printf( gettext( "enter wallet password: " ) ) ;
176  _getPassWordFromUser( password,PASSWORD_SIZE,&password_length ) ;
177  puts( "" ) ;
178  printf( gettext( "re enter wallet password: " ) ) ;
179  _getPassWordFromUser( password_1,PASSWORD_SIZE,NULL ) ;
180  puts( "" ) ;
181  if( StringsAreNotEqual( password,password_1 ) ){
182  puts( gettext( "passwords did not match" ) ) ;
183  return 1 ;
184  }else{
185  r = lxqt_wallet_create( password,password_length,wallet_name,APPLICATION_NAME ) ;
186  if( r != lxqt_wallet_no_error ){
187  puts( gettext( "failed to create wallet" ) ) ;
188  return 1 ;
189  }
190  }
191  }else{
192  puts( gettext( "volume not created per user request" ) ) ;
193  return 1 ;
194  }
195  }else{
196  printf( gettext( "enter wallet password: " ) ) ;
197  _getPassWordFromUser( password,PASSWORD_SIZE,&password_length ) ;
198  puts( "" ) ;
199  }
200 
201  return _open_wallet( wallet,password,password_length,wallet_name ) ;
202 }
203 
205 {
207 
208  memset( &iter,'\0',sizeof( iter ) ) ;
209 
210  while( lxqt_wallet_iter_read_value( wallet,&iter ) ){
211  puts( iter.entry.key ) ;
212  }
213 
214  return 0 ;
215 }
216 
217 static const char * _fileName( const char * filePath )
218 {
219  const char * e = strrchr( filePath,'/' ) ;
220  if( e == NULL ){
221  return filePath ;
222  }else{
223  return e + 1 ;
224  }
225 }
226 
227 static int _addFileToWallet_1( lxqt_wallet_t wallet,int fd,const char * filePath,const struct stat * st )
228 {
230  char * e ;
231  const char * fileName = _fileName( filePath ) ;
232  e = malloc( st->st_size ) ;
233  if( e == NULL ){
234  puts( gettext( "failed to allocate memory" ) ) ;
235  return 1 ;
236  }else{
237  _read( fd,e,st->st_size ) ;
238  r = lxqt_wallet_add_key( wallet,_file( fileName ),e,st->st_size ) ;
239  free( e ) ;
240  if( r != lxqt_wallet_no_error ){
241  puts( gettext( "failed to add file to the wallet" ) ) ;
242  return 1 ;
243  }else{
244  return 0 ;
245  }
246  }
247 }
248 
249 static int _addFileToWallet( lxqt_wallet_t wallet,const char * filePath )
250 {
252  struct stat st ;
253  int fd ;
254  void * map ;
255  int k = 1 ;
256  const char * fileName = _fileName( filePath ) ;
257 
258  if( lxqt_wallet_wallet_has_key( wallet,_file( fileName ) ) ){
259  printf( gettext( "wallet already has \"%s\" entry\n" ),fileName ) ;
260  return 1 ;
261  }else{
262  fd = open( filePath,O_RDONLY ) ;
263  if( fd == -1 ){
264  puts( gettext( "failed to open file for reading" ) ) ;
265  return 1 ;
266  }else{
267  fstat( fd,&st ) ;
268  map = mmap( 0,st.st_size,PROT_READ,MAP_PRIVATE,fd,0 ) ;
269  if( map == MAP_FAILED ){
270  k = _addFileToWallet_1( wallet,fd,fileName,&st ) ;
271  }else{
272  r = lxqt_wallet_add_key( wallet,_file( fileName ),map,st.st_size ) ;
273  munmap( map,st.st_size ) ;
274  if( r != lxqt_wallet_no_error ){
275  puts( gettext( "failed to add file to the wallet" ) ) ;
276  k = 1 ;
277  }else{
278  k = 0 ;
279  }
280  }
281 
282  close( fd ) ;
283  return k ;
284  }
285  }
286 }
287 
288 static int _deleteFileFromWallet( lxqt_wallet_t wallet,const char * filePath )
289 {
290  const char * fileName = _fileName( filePath ) ;
291  lxqt_wallet_delete_key( wallet,_file( fileName ) ) ;
292  return 0 ;
293 }
294 
295 static int _getFileFromWallet( lxqt_wallet_t wallet,const char * filePath )
296 {
297  int fd ;
298  int r ;
299  struct stat st ;
301  const char * fileName = _fileName( filePath ) ;
302 
303  r = lxqt_wallet_read_key_value( wallet,_file( fileName ),&k ) ;
304  if( r != 1 ){
305  puts( gettext( "file not found in the wallet" ) ) ;
306  return 1 ;
307  }else{
308  if( stat( filePath,&st ) == 0 ){
309  printf( gettext( "path ./\"%s\" already occupied\n" ),fileName ) ;
310  return 1 ;
311  }else{
312  fd = open( filePath,O_WRONLY|O_CREAT,0644 ) ;
313  if( fd == -1 ){
314  puts( gettext( "failed to open file for writing" ) ) ;
315  return 1 ;
316  }else{
317  _write( fd,k.key_value,k.key_value_size ) ;
318  close( fd ) ;
319  return 0 ;
320  }
321  }
322  }
323 }
324 
326 {
328 
329  memset( &iter,'\0',sizeof( iter ) ) ;
330 
331  while( lxqt_wallet_iter_read_value( wallet,&iter ) ){
333  }
334 
335  return 0 ;
336 }
337 
338 static int _addAllFilesToTheWallet( lxqt_wallet_t wallet,const char * path )
339 {
340  struct stat st ;
341  DIR * dir = opendir( path ) ;
342  struct dirent * entry ;
343  char path_1[ PATH_MAX ] ;
344 
345  if( dir == NULL ){
346  puts( gettext( "failed to open directory for reading" ) ) ;
347  return 1 ;
348  }else{
349  while( ( entry = readdir( dir ) ) != NULL ){
350  snprintf( path_1,PATH_MAX,"%s/%s",path,entry->d_name ) ;
351  if( stat( path_1,&st ) == 0 && S_ISREG( st.st_mode ) ){
352  _addFileToWallet( wallet,path_1 ) ;
353  }
354  }
355  return 0 ;
356  }
357 }
358 
359 static int _printWalletList( void )
360 {
361  int len = 0 ;
362  int k ;
363  char * c ;
364  char ** e = lxqt_wallet_wallet_list( APPLICATION_NAME,&len ) ;
365 
366  for( k = 0 ; k < len ; k++ ){
367  c = *( e + k ) ;
368  puts( c ) ;
369  free( c ) ;
370  }
371 
372  free( e ) ;
373  return 0 ;
374 }
375 
376 int main( int argc,char * argv[] )
377 {
378  lxqt_wallet_t wallet = 0 ;
379 
380  const char * path ;
381  const char * action ;
382  int r = 1 ;
383 
384  if( argc == 1 ){
385  _help() ;
386  return 0 ;
387  }
388 
389  action = argv[ 1 ] ;
390 
391  if( argc == 2 ){
392  if( StringsAreEqual( action,"-h" ) || StringsAreEqual( action,"--help" ) || StringsAreEqual( action,"-help" )
393  || StringsAreEqual( action,"-v" ) || StringsAreEqual( action,"--version" ) ){
394  _help() ;
395  return 0 ;
396  }else if( StringsAreEqual( action,"--wallets" ) ){
397  return _printWalletList() ;
398  }else if( StringsAreEqual( action,"--list" ) || StringsAreEqual( action,"--get-all" ) ){
399  ;
400  }else{
401  _help() ;
402  return 1 ;
403  }
404  }
405 
406  if( _openWallet( &wallet ) ){
407  return 1 ;
408  }else{
409  if( StringsAreEqual( action,"--list" ) ){
411  }else if( StringsAreEqual( action,"--get-all" ) ){
413  }else{
414  if( argc != 3 ){
415  _help() ;
416  }else{
417  path = argv[ 2 ] ;
418  if( StringsAreEqual( action,"--add" ) ){
419  r = _addFileToWallet( wallet,path ) ;
420  }else if( StringsAreEqual( action,"--delete" ) ){
421  r = _deleteFileFromWallet( wallet,path ) ;
422  }else if( StringsAreEqual( action,"--get" ) ){
423  r = _getFileFromWallet( wallet,path ) ;
424  }else if( StringsAreEqual( action,"--add-all" ) ){
425  r = _addAllFilesToTheWallet( wallet,path ) ;
426  }else{
427  puts( "unknown command" ) ;
428  r = 1 ;
429  }
430  }
431  }
432 
434  return r ;
435  }
436 }
lxqt_wallet_delete_key
lxqt_wallet_error lxqt_wallet_delete_key(lxqt_wallet_t wallet, const char *key, u_int32_t key_size)
Definition: lxqtwallet.c:982
lxqt_wallet_error
lxqt_wallet_error
Definition: lxqtwallet.h:53
_read
static void _read(int x, void *y, size_t z)
Definition: zuluSafe.c:53
APPLICATION_NAME
#define APPLICATION_NAME
Definition: zuluSafe.c:42
_help
static void _help(void)
Definition: zuluSafe.c:58
_fileName
static const char * _fileName(const char *filePath)
Definition: zuluSafe.c:217
StringsAreNotEqual
#define StringsAreNotEqual(x, y)
Definition: zuluSafe.c:47
_write
static void _write(int x, const void *y, size_t z)
Definition: zuluSafe.c:49
_getFileFromWallet
static int _getFileFromWallet(lxqt_wallet_t wallet, const char *filePath)
Definition: zuluSafe.c:295
lxqt_wallet_no_error
@ lxqt_wallet_no_error
Definition: lxqtwallet.h:54
_terminalEchoOff
static int _terminalEchoOff(struct termios *old, struct termios *new)
Definition: zuluSafe.c:113
lxqt_wallet_wallet_has_key
int lxqt_wallet_wallet_has_key(lxqt_wallet_t wallet, const char *key, u_int32_t key_size)
Definition: lxqtwallet.c:832
_getAllFilesFromWallet
static int _getAllFilesFromWallet(lxqt_wallet_t wallet)
Definition: zuluSafe.c:325
_printListOfManagedFiles
static int _printListOfManagedFiles(lxqt_wallet_t wallet)
Definition: zuluSafe.c:204
_addFileToWallet_1
static int _addFileToWallet_1(lxqt_wallet_t wallet, int fd, const char *filePath, const struct stat *st)
Definition: zuluSafe.c:227
StringsAreEqual
#define StringsAreEqual(x, y)
Definition: zuluSafe.c:46
_deleteFileFromWallet
static int _deleteFileFromWallet(lxqt_wallet_t wallet, const char *filePath)
Definition: zuluSafe.c:288
lxqt_wallet_wallet_list
char ** lxqt_wallet_wallet_list(const char *application_name, int *size)
Definition: lxqtwallet.c:1174
lxqt_wallet_read_key_value
int lxqt_wallet_read_key_value(lxqt_wallet_t wallet, const char *key, u_int32_t key_size, lxqt_wallet_key_values_t *key_value)
Definition: lxqtwallet.c:794
main
int main(int argc, char *argv[])
Definition: zuluSafe.c:376
_addFileToWallet
static int _addFileToWallet(lxqt_wallet_t wallet, const char *filePath)
Definition: zuluSafe.c:249
_addAllFilesToTheWallet
static int _addAllFilesToTheWallet(lxqt_wallet_t wallet, const char *path)
Definition: zuluSafe.c:338
_getPassWordFromUser
static int _getPassWordFromUser(char *password, size_t size, size_t *len)
Definition: zuluSafe.c:129
lxqt_wallet_add_key
lxqt_wallet_error lxqt_wallet_add_key(lxqt_wallet_t wallet, const char *key, u_int32_t key_size, const char *value, u_int32_t key_value_length)
Definition: lxqtwallet.c:876
lxqt_wallet_iterator_t
Definition: lxqtwallet.h:153
lxqt_wallet_iterator_t::entry
lxqt_wallet_key_values_t entry
Definition: lxqtwallet.h:155
lxqt_wallet_iter_read_value
int lxqt_wallet_iter_read_value(lxqt_wallet_t wallet, lxqt_wallet_iterator_t *iter)
Definition: lxqtwallet.c:921
_getInputFromUser
static void _getInputFromUser(char *buffer, size_t size, size_t *len)
Definition: zuluSafe.c:91
lxqt_wallet_close
lxqt_wallet_error lxqt_wallet_close(lxqt_wallet_t *w)
Definition: lxqtwallet.c:1066
lxqt_wallet_key_values_t::key
const char * key
Definition: lxqtwallet.h:147
lxqt_wallet_key_values_t
Definition: lxqtwallet.h:146
_clearKeyBoardBuffer
static void _clearKeyBoardBuffer(void)
Definition: zuluSafe.c:80
lxqt_wallet_struct
Definition: lxqtwallet.c:71
_open_wallet
static int _open_wallet(lxqt_wallet_t *wallet, const char *password, size_t password_length, const char *wallet_name)
Definition: zuluSafe.c:144
e
static QString e
Definition: about.cpp:31
_printWalletList
static int _printWalletList(void)
Definition: zuluSafe.c:359
zuluwallet.h
lxqt_wallet_create
lxqt_wallet_error lxqt_wallet_create(const char *password, u_int32_t password_length, const char *wallet_name, const char *application_name)
Definition: lxqtwallet.c:275
_openWallet
static int _openWallet(lxqt_wallet_t *wallet)
Definition: zuluSafe.c:155
PASSWORD_SIZE
#define PASSWORD_SIZE
Definition: zuluSafe.c:39
lxqt_wallet_exists
int lxqt_wallet_exists(const char *wallet_name, const char *application_name)
Definition: lxqtwallet.c:1225
_file
#define _file(x)
Definition: zuluSafe.c:44
lxqt_wallet_open
lxqt_wallet_error lxqt_wallet_open(lxqt_wallet_t *wallet, const char *password, u_int32_t password_length, const char *wallet_name, const char *application_name)
Definition: lxqtwallet.c:681
wallet
Definition: wallet.h:35
WALLET_NAME_SIZE
#define WALLET_NAME_SIZE
Definition: zuluSafe.c:40