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)  

crypt_file.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2012-2015
4  * name : Francis Banyikwa
5  * email: mhogomchungu@gmail.com
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "includes.h"
21 #include "../lib/includes.h"
22 #include <stdio.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <libintl.h>
27 #include <locale.h>
28 
29 #define DECRYPT 1
30 #define ENCRYPT 0
31 
32 #define _ignore_result( x ) if( x ){;}
33 
34 static int zuluExit( int st )
35 {
36  switch( st ){
37 
38  case 0 : printf( gettext( "SUCCESS: Encrypted file created successfully\n" ) ) ; break ;
39  case 1 : printf( gettext( "SUCCESS: Decrypted file created successfully\n" ) ) ; break ;
40  case 2 : printf( gettext( "ERROR: Could not open key file for reading\n" ) ) ; break ;
41  case 3 : printf( gettext( "ERROR: Missing key source\n" ) ) ; break ;
42  case 4 : printf( gettext( "ERROR: Could not open encryption routines\n" ) ) ; break ;
43  case 5 : printf( gettext( "ERROR: File or folder already exist at destination address\n" ) ) ; break ;
44  case 6 : printf( gettext( "ERROR: Invalid path to source\n" ) ) ; break ;
45  case 7 : printf( gettext( "ERROR: Could not resolve path to destination file\n" ) ) ; break ;
46  case 8 : printf( gettext( "ERROR: Passphrases do not match\n" ) ) ; break ;
47  case 9 : printf( gettext( "ERROR: Destination path is missing\n" ) ) ; break ;
48  case 10: printf( gettext( "ERROR: Insufficient privilege to create destination file\n" ) ) ; break ;
49  case 11: printf( gettext( "ERROR: Presented key did not match the encryption key\n" ) ) ; break ;
50  case 12: printf( gettext( "ERROR: Can not get passphrase in silent mode\n" ) ) ; break ;
51  case 13: printf( gettext( "ERROR: Insufficient memory to hold passphrase\n" ) ) ; break ;
52  case 14: printf( gettext( "ERROR: Source path is missing\n" ) ) ; break ;
53  case 15: printf( gettext( "ERROR: Insufficient privilege to open source file for reading\n" ) ) ; break ;
54  case 16: printf( gettext( "INFORMATION: Functionality currently disabled\n" ) ) ; ; break ;
55  }
56  return st ;
57 }
58 
59 static int crypt_opt( const struct_opts * opts,uid_t uid,int opt )
60 {
61  string_t q = StringVoid ;
62  string_t p = StringVoid ;
63 
64  int st ;
65 
66  const char * source = opts->device ;
67  const char * dest = opts->m_opts ;
68  const char * passphrase = opts->key ;
69  const char * type = opts->key_source ;
70 
71  return zuluExit( 16 ) ;
72 
73  if( dest == NULL ){
74 
75  return zuluExit( 9 ) ;
76  }
77  if( source == NULL ){
78 
79  return zuluExit( 14 ) ;
80  }
81  /*
82  * zuluCryptPathStartsWith() is defined in real_path.c
83  */
84  if( zuluCryptPathStartsWith( dest,"/dev/" ) ){
85 
86  return zuluExit( 10 ) ;
87  }
88  if( zuluCryptPathStartsWith( source,"/dev/" ) ){
89 
90  return zuluExit( 15 ) ;
91  }
92  /*
93  * zuluCryptPathIsValid() is defined in ../lib/is_path_valid.c
94  */
95  if( zuluCryptPathIsValid( dest ) ){
96 
97  return zuluExit( 5 ) ;
98  }
99  /*
100  * zuluCryptPathIsNotValid() is defined in ../lib/is_path_valid.c
101  */
102  if( zuluCryptPathIsNotValid( source ) ){
103 
104  return zuluExit( 6 ) ;
105  }
106 
107  /*
108  * below two functions are defined in path_access.c
109  */
110  if( zuluCryptCanOpenPathForWriting( dest,uid ) == 1 ){
111 
112  return zuluExit( 10 ) ;
113  }
114  if( zuluCryptCanOpenPathForReading( source,uid ) == 1 ){
115 
116  return zuluExit( 15 ) ;
117  }
118  if( type == NULL ){
119 
120  printf( gettext( "Enter passphrase: " ) ) ;
121  /*
122  * ZULUCRYPT_KEY_MAX_SIZE is set in ../constants.h
123  */
125 
126  case 1 : return zuluExit( 12 ) ;
127  case 2 : return zuluExit( 13 ) ;
128  }
129 
130  printf( gettext( "\nRe enter passphrase: " ) ) ;
131 
133 
134  case 1 : StringClearDelete( &p ) ;
135  return zuluExit( 12 ) ;
136  case 2 : StringClearDelete( &p ) ;
137  return zuluExit( 13 ) ;
138  }
139 
140  printf( "\n" ) ;
141 
142  if( !StringsAreEqual_1( p,q ) ){
143 
144  StringClearDelete( &p ) ;
145  StringClearDelete( &q ) ;
146 
147  return zuluExit( 8 ) ;
148  }else{
149  StringDelete( &q ) ;
150  }
151  }else{
152  if( type == NULL ){
153 
154  return zuluExit( 9 ) ;
155  }
156  if( StringsAreEqual( type,"-p" ) ){
157 
158  p = String( passphrase ) ;
159 
160  }else if( StringsAreEqual( type,"-f" ) ){
161 
162  p = StringGetFromFile( passphrase ) ;
163 
164  if( p == NULL ){
165 
166  return zuluExit( 2 ) ;
167  }
168  }else{
169  return zuluExit( 3 ) ;
170  }
171  }
172 
173  if( opt == ENCRYPT ){
174  /*
175  * zuluCryptEncryptFile() is defined in ./crypt_file.c
176  */
177  st = zuluCryptEncryptFile( source,dest,StringContent( p ),StringLength( p ) ) ;
178  }else{
179  /*
180  * zuluCryptDecryptFile() is defined in ./crypt_file.c
181  */
182  st = zuluCryptDecryptFile( source,dest,StringContent( p ),StringLength( p ) ) ;
183  }
184 
185  StringClearDelete( &p ) ;
186 
187  switch( st ){
188 
189  case 1 : return zuluExit( 4 ) ;
190  case 2 : return zuluExit( 11 ) ;
191  }
192 
193  _ignore_result( chmod( dest,S_IRUSR | S_IWUSR ) ) ;
194  _ignore_result( chown( dest,uid,uid ) ) ;
195 
196  if( opt == 1 ){
197 
198  return zuluExit( 1 ) ;
199  }else{
200  return zuluExit( 0 ) ;
201  }
202 }
203 
204 int zuluCryptExeFileDecrypt( const struct_opts * opts,uid_t uid )
205 {
206  return crypt_opt( opts,uid,DECRYPT ) ;
207 }
208 
209 int zuluCryptExeFileEncrypt( const struct_opts * opts,uid_t uid )
210 {
211  return crypt_opt( opts,uid,ENCRYPT ) ;
212 }
StringSilentlyGetFromTerminal_1
int StringSilentlyGetFromTerminal_1(string_t *q, size_t s)
Definition: String.c:1752
StringGetFromFile
string_t StringGetFromFile(const char *path)
Definition: String.c:2006
struct_opts_1::key
const char * key
Definition: libzuluCrypt-exe.h:42
struct_opts_1::device
const char * device
Definition: libzuluCrypt-exe.h:37
StringLength
size_t StringLength(string_t st)
Definition: String.c:678
zuluCryptEncryptFile
int zuluCryptEncryptFile(const char *source, const char *dest, const char *key, u_int64_t key_len)
Definition: file_encryption.c:207
zuluCryptCanOpenPathForWriting
int zuluCryptCanOpenPathForWriting(const char *path, uid_t uid)
Definition: path_access.c:103
struct_opts_1
Definition: libzuluCrypt-exe.h:35
crypt_opt
static int crypt_opt(const struct_opts *opts, uid_t uid, int opt)
Definition: crypt_file.c:59
struct_opts_1::key_source
const char * key_source
Definition: libzuluCrypt-exe.h:41
_ignore_result
#define _ignore_result(x)
Definition: crypt_file.c:32
zuluCryptCanOpenPathForReading
int zuluCryptCanOpenPathForReading(const char *path, uid_t uid)
Definition: path_access.c:98
StringsAreEqual
#define StringsAreEqual(x, y)
Definition: zuluSafe.c:46
zuluCryptPathIsNotValid
int zuluCryptPathIsNotValid(const char *path)
Definition: is_path_valid.c:29
ENCRYPT
#define ENCRYPT
Definition: crypt_file.c:30
StringContent
static const __inline__ char * StringContent(string_t st)
Definition: String.h:1011
StringType
Definition: String.c:49
StringsAreEqual_1
int StringsAreEqual_1(string_t x, string_t y)
Definition: String.c:1460
String
string_t String(const char *cstring)
Definition: String.c:318
ZULUCRYPT_KEY_MAX_SIZE
#define ZULUCRYPT_KEY_MAX_SIZE
Definition: constants.h:49
DECRYPT
#define DECRYPT
Definition: crypt_file.c:29
zuluExit
static int zuluExit(int st)
Definition: crypt_file.c:34
zuluCryptDecryptFile
int zuluCryptDecryptFile(const char *source, const char *dest, const char *key, u_int64_t key_len)
Definition: file_encryption.c:126
StringDelete
void StringDelete(string_t *st)
Definition: String.c:162
struct_opts_1::m_opts
const char * m_opts
Definition: libzuluCrypt-exe.h:39
zuluCryptPathStartsWith
int zuluCryptPathStartsWith(const char *path, const char *start)
Definition: real_path.c:55
zuluCryptExeFileDecrypt
int zuluCryptExeFileDecrypt(const struct_opts *opts, uid_t uid)
Definition: crypt_file.c:204
includes.h
StringClearDelete
void StringClearDelete(string_t *st)
Definition: String.c:177
zuluCryptExeFileEncrypt
int zuluCryptExeFileEncrypt(const struct_opts *opts, uid_t uid)
Definition: crypt_file.c:209
zuluCryptPathIsValid
int zuluCryptPathIsValid(const char *path)
Definition: is_path_valid.c:23
StringVoid
#define StringVoid
Definition: String.h:47