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)  

remove_key.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2011-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 <stdio.h>
22 #include <libintl.h>
23 
24 static int _zuluCryptExECheckEmptySlots( const char * device )
25 {
26  int status = 0 ;
27  char * c ;
28  char * d ;
29 
31 
32  c = zuluCryptEmptySlots( device ) ;
33 
35 
36  if( c == NULL ){
37 
38  return 1 ;
39  }
40 
41  d = c - 1 ;
42 
43  while( *++d ){
44 
45  if( *d == '3' ){
46 
47  status = 3 ;
48 
49  break ;
50  }
51  }
52 
53  StringFree( c ) ;
54 
55  return status ;
56 }
57 
58 static int zuluExit( int st,stringList_t stl )
59 {
61  /*
62  * this function is defined in ../string/StringList.c
63  */
64  StringListClearDelete( &stl ) ;
65 
66  switch ( st ){
67 
68  case 0 : printf( gettext( "SUCCESS: Key removed successfully\n" ) ) ; break ;
69  case 2 : printf( gettext( "ERROR: There is no key in the volume that match the presented key\n" ) ) ; break ;
70  case 3 : printf( gettext( "ERROR: Could not open the volume\n" ) ) ; break ;
71  case 4 : printf( gettext( "ERROR: Insufficient privilege to open a system device,\
72 only root user or members of group zulucrypt can do that\n" ) ) ; break ;
73  case 5 : printf( gettext( "ERROR: Could not open the volume in write mode\n" ) ) ; break ;
74  case 6 : printf( gettext( "ERROR: Insufficient memory to hold your response\n" ) ) ; break ;
75  case 7 : printf( gettext( "INFO: Operation terminated per user request\n" ) ) ; break ;
76  case 8 : printf( gettext( "ERROR: Can not get passphrase in silent mode\n" ) ) ; break ;
77  case 9 : printf( gettext( "ERROR: Insufficient memory to hold passphrase\n" ) ) ; break ;
78  case 10: printf( gettext( "ERROR: One or more required argument(s) for this operation is missing\n" ) );break ;
79  case 11: printf( gettext( "ERROR: Keyfile does not exist\n" ) ) ; break ;
80  case 12: printf( gettext( "ERROR: Could not get enough memory to open the key file\n" ) ) ; break ;
81  case 13: printf( gettext( "ERROR: Insufficient privilege to open key file for reading\n" ) ) ; break ;
82  case 14: printf( gettext( "ERROR: Could not get a key from a socket\n" ) ) ; break ;
83  default: printf( gettext( "ERROR: Unrecognized error with status number %d encountered\n" ),st );
84  }
85  return st ;
86 }
87 
88 static int zuluExit_1( int st,const char * device,stringList_t stl )
89 {
90  printf( gettext( "ERROR: Device \"%s\" is not a luks device\n" ),device ) ;
91  StringListClearDelete( &stl ) ;
92  return st ;
93 }
94 
95 int zuluCryptEXERemoveKey( const struct_opts * opts,uid_t uid )
96 {
97  int ask_confirmation = opts->ask_confirmation ;
98  const char * device = opts->device ;
99  const char * keyType = opts->key_source ;
100  const char * keytoremove = opts->key ;
101 
102  stringList_t stl = StringListInit() ;
103 
104  string_t * pass = StringListAssign( stl ) ;
105  string_t * confirm = StringListAssign( stl ) ;
106 
107  int status = 0 ;
108 
109  const char * key ;
110  size_t key_size ;
111 
112  /*
113  * zuluCryptPartitionIsSystemPartition() is defined in ./partitions.c
114  */
115  if( zuluCryptPartitionIsSystemPartition( device,uid ) ){
116 
118 
119  if( !zuluCryptUserIsAMemberOfAGroup( uid,"zulucrypt" ) ){
120 
121  return zuluExit( 4,stl ) ;
122  }
123  }
124  }
125 
126  /*
127  * zuluCryptCanOpenPathForWriting is defined in path_access.c
128  */
129  status = zuluCryptCanOpenPathForWriting( device,uid ) ;
130  /*
131  * 1-permissions denied
132  * 2-invalid path
133  * 3-shenanigans
134  * 4-common error
135  */
136  switch( status ){
137 
138  case 0 : break ;
139  case 1 : return zuluExit( 5,stl ) ;
140  case 2 : return zuluExit( 5,stl ) ;
141  case 3 : return zuluExit( 5,stl ) ;
142  case 4 : return zuluExit( 5,stl ) ;
143  default: return zuluExit( 5,stl ) ;
144  }
145 
146  if( _zuluCryptExECheckEmptySlots( device ) == 3 ){
147 
148  if( ask_confirmation ){
149 
150  printf( gettext( "WARNING: There is only one key in the volume and all data in it will be lost if you continue.\n" ) ) ;
151  printf( gettext( "Do you still want to continue? Type \"YES\" if you do: " ) ) ;
152 
153  *confirm = StringGetFromTerminal_1( 3 ) ;
154 
155  if( *confirm == StringVoid ){
156 
157  return zuluExit( 6,stl ) ;
158  }
159  if( !StringsAreEqual_2( *confirm,gettext( "YES" ) ) ){
160 
161  return zuluExit( 7,stl ) ;
162  }
163  }
164  }
165 
166  if( keyType == NULL ){
167 
168  printf( gettext( "Enter a key to be removed: " ) ) ;
169  /*
170  * ZULUCRYPT_KEY_MAX_SIZE is set in ../constants.h
171  */
173 
174  case 1 : return zuluExit( 8,stl ) ;
175  case 2 : return zuluExit( 9,stl ) ;
176  }
177 
178  printf( "\n" ) ;
179 
180  key = StringContent( *pass ) ;
181  key_size = StringLength( *pass ) ;
182 
184  }else{
185  if( keyType == NULL || keytoremove == NULL ){
186 
187  return zuluExit( 10,stl ) ;
188  }
189  if( StringsAreEqual( keyType,"-f" ) ){
190  /*
191  * zuluCryptGetPassFromFile() is defined at path_access.c"
192  */
193  switch( zuluCryptGetPassFromFile( NULL,keytoremove,uid,pass ) ){
194 
195  case 1 : return zuluExit( 11,stl ) ;
196  case 2 : return zuluExit( 12,stl ) ;
197  case 4 : return zuluExit( 13,stl ) ;
198  case 5 : return zuluExit( 14,stl ) ;
199  }
200 
201  key = StringContent( *pass ) ;
202  key_size = StringLength( *pass ) ;
203 
205  }else if( StringsAreEqual( keyType, "-p" ) ){
206 
207  key = keytoremove ;
208  key_size = StringSize( keytoremove ) ;
209  }else{
210  return zuluExit( 10,stl ) ;
211  }
212  }
213 
215  /*
216  * zuluCryptRemoveKey() is defined in ../lib/remove_key.c
217  */
218  status = zuluCryptRemoveKey( device,key,key_size ) ;
220 
221  if( status == 1 ){
222 
223  status = zuluExit_1( status,device,stl ) ;
224  }else{
225  status = zuluExit( status,stl ) ;
226  }
227 
228  /*
229  * zuluCryptCheckInvalidKey() is defined in check_invalid_key.c
230  */
232  return status ;
233 }
StringSilentlyGetFromTerminal_1
int StringSilentlyGetFromTerminal_1(string_t *q, size_t s)
Definition: String.c:1752
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
struct_opts_1::key
const char * key
Definition: libzuluCrypt-exe.h:42
struct_opts_1::device
const char * device
Definition: libzuluCrypt-exe.h:37
zuluCryptCheckInvalidKey
void zuluCryptCheckInvalidKey(const char *device)
Definition: check_invalid_key.c:23
StringLength
size_t StringLength(string_t st)
Definition: String.c:678
zuluCryptCanOpenPathForWriting
int zuluCryptCanOpenPathForWriting(const char *path, uid_t uid)
Definition: path_access.c:103
struct_opts_1
Definition: libzuluCrypt-exe.h:35
struct_opts_1::key_source
const char * key_source
Definition: libzuluCrypt-exe.h:41
zuluCryptRemoveKey
int zuluCryptRemoveKey(const char *device, const char *pass, size_t pass_size)
Definition: remove_key.c:73
StringListInit
stringList_t StringListInit(void)
Definition: StringList.c:293
_zuluCryptExECheckEmptySlots
static int _zuluCryptExECheckEmptySlots(const char *device)
Definition: remove_key.c:24
zuluCryptUserIsAMemberOfAGroup
int zuluCryptUserIsAMemberOfAGroup(uid_t uid, const char *groupname)
Definition: mount_fs_options.c:172
StringsAreEqual
#define StringsAreEqual(x, y)
Definition: zuluSafe.c:46
StringSize
static __inline__ size_t StringSize(const char *str)
Definition: String.h:846
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
struct_opts_1::ask_confirmation
int ask_confirmation
Definition: libzuluCrypt-exe.h:62
zuluCryptSecurityUnlockMemory
void zuluCryptSecurityUnlockMemory(stringList_t stl)
Definition: security.c:229
siritask::status
status
Definition: siritask.h:114
zuluCryptSecurityGainElevatedPrivileges
int zuluCryptSecurityGainElevatedPrivileges(void)
Definition: security.c:64
ZULUCRYPT_KEY_MAX_SIZE
#define ZULUCRYPT_KEY_MAX_SIZE
Definition: constants.h:49
StringListType
Definition: StringList.c:33
zuluExit_1
static int zuluExit_1(int st, const char *device, stringList_t stl)
Definition: remove_key.c:88
zuluCryptSecurityLockMemory_1
void zuluCryptSecurityLockMemory_1(string_t st)
Definition: security.c:185
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
StringListClearDelete
void StringListClearDelete(stringList_t *stl)
Definition: StringList.c:1216
zuluExit
static int zuluExit(int st, stringList_t stl)
Definition: remove_key.c:58
StringGetFromTerminal_1
string_t StringGetFromTerminal_1(size_t s)
Definition: String.c:1675
zuluCryptEmptySlots
char * zuluCryptEmptySlots(const char *device)
Definition: empty_slots.c:86
includes.h
zuluCryptExeOriginalUserIsNotRoot
int zuluCryptExeOriginalUserIsNotRoot()
Definition: security.c:59
zuluCryptEXERemoveKey
int zuluCryptEXERemoveKey(const struct_opts *opts, uid_t uid)
Definition: remove_key.c:95
StringFree
static __inline__ void StringFree(const void *str)
Definition: String.h:823
StringVoid
#define StringVoid
Definition: String.h:47