PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/zuluCrypt-cli/bin/remove_key.c

https://gitlab.com/m.schmidt/zuluCrypt
C | 205 lines | 135 code | 25 blank | 45 comment | 23 complexity | ec9dc91a19b6669c72cabf856d73d2a6 MD5 | raw file
Possible License(s): BSD-3-Clause-No-Nuclear-License-2014, BSD-2-Clause
  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. #include "includes.h"
  20. #include <stdio.h>
  21. #include <libintl.h>
  22. static int _zuluCryptExECheckEmptySlots( const char * device )
  23. {
  24. int status = 0 ;
  25. char * c ;
  26. char * d ;
  27. zuluCryptSecurityGainElevatedPrivileges() ;
  28. c = zuluCryptEmptySlots( device ) ;
  29. zuluCryptSecurityDropElevatedPrivileges() ;
  30. if( c == NULL ){
  31. return 1 ;
  32. }
  33. d = c - 1 ;
  34. while( *++d ){
  35. if( *d == '3' ){
  36. status = 3 ;
  37. break ;
  38. }
  39. }
  40. StringFree( c ) ;
  41. return status ;
  42. }
  43. static int zuluExit( int st,stringList_t stl )
  44. {
  45. zuluCryptSecurityUnlockMemory( stl ) ;
  46. /*
  47. * this function is defined in ../string/StringList.c
  48. */
  49. StringListClearDelete( &stl ) ;
  50. switch ( st ){
  51. case 0 : printf( gettext( "SUCCESS: Key removed successfully\n" ) ) ; break ;
  52. case 2 : printf( gettext( "ERROR: There is no key in the volume that match the presented key\n" ) ) ; break ;
  53. case 3 : printf( gettext( "ERROR: Could not open the volume\n" ) ) ; break ;
  54. case 4 : printf( gettext( "ERROR: Insufficient privilege to open a system device,\
  55. only root user or members of group zulucrypt can do that\n" ) ) ; break ;
  56. case 5 : printf( gettext( "ERROR: Could not open the volume in write mode\n" ) ) ; break ;
  57. case 6 : printf( gettext( "ERROR: Insufficient memory to hold your response\n" ) ) ; break ;
  58. case 7 : printf( gettext( "INFO: Operation terminated per user request\n" ) ) ; break ;
  59. case 8 : printf( gettext( "ERROR: Can not get passphrase in silent mode\n" ) ) ; break ;
  60. case 9 : printf( gettext( "ERROR: Insufficient memory to hold passphrase\n" ) ) ; break ;
  61. case 10: printf( gettext( "ERROR: One or more required argument(s) for this operation is missing\n" ) );break ;
  62. case 11: printf( gettext( "ERROR: Keyfile does not exist\n" ) ) ; break ;
  63. case 12: printf( gettext( "ERROR: Could not get enough memory to open the key file\n" ) ) ; break ;
  64. case 13: printf( gettext( "ERROR: Insufficient privilege to open key file for reading\n" ) ) ; break ;
  65. case 14: printf( gettext( "ERROR: Could not get a key from a socket\n" ) ) ; break ;
  66. default: printf( gettext( "ERROR: Unrecognized error with status number %d encountered\n" ),st );
  67. }
  68. return st ;
  69. }
  70. static int zuluExit_1( int st,const char * device,stringList_t stl )
  71. {
  72. printf( gettext( "ERROR: Device \"%s\" is not a luks device\n" ),device ) ;
  73. StringListClearDelete( &stl ) ;
  74. return st ;
  75. }
  76. int zuluCryptEXERemoveKey( const struct_opts * opts,uid_t uid )
  77. {
  78. int ask_confirmation = opts->ask_confirmation ;
  79. const char * device = opts->device ;
  80. const char * keyType = opts->key_source ;
  81. const char * keytoremove = opts->key ;
  82. stringList_t stl = StringListInit() ;
  83. string_t * pass = StringListAssign( stl ) ;
  84. string_t * confirm = StringListAssign( stl ) ;
  85. int status = 0 ;
  86. const char * key ;
  87. size_t key_size ;
  88. /*
  89. * zuluCryptPartitionIsSystemPartition() is defined in ./partitions.c
  90. */
  91. if( zuluCryptPartitionIsSystemPartition( device,uid ) ){
  92. if( !zuluCryptUserIsAMemberOfAGroup( uid,"zulucrypt" ) ){
  93. return zuluExit( 4,stl ) ;
  94. }
  95. }
  96. /*
  97. * zuluCryptCanOpenPathForWriting is defined in path_access.c
  98. */
  99. status = zuluCryptCanOpenPathForWriting( device,uid ) ;
  100. /*
  101. * 1-permissions denied
  102. * 2-invalid path
  103. * 3-shenanigans
  104. * 4-common error
  105. */
  106. switch( status ){
  107. case 0 : break ;
  108. case 1 : return zuluExit( 5,stl ) ;
  109. case 2 : return zuluExit( 5,stl ) ;
  110. case 3 : return zuluExit( 5,stl ) ;
  111. case 4 : return zuluExit( 5,stl ) ;
  112. default: return zuluExit( 5,stl ) ;
  113. }
  114. if( _zuluCryptExECheckEmptySlots( device ) == 3 ){
  115. if( ask_confirmation ){
  116. printf( gettext( "WARNING: There is only one key in the volume and all data in it will be lost if you continue.\n" ) ) ;
  117. printf( gettext( "Do you still want to continue? Type \"YES\" if you do: " ) ) ;
  118. *confirm = StringGetFromTerminal_1( 3 ) ;
  119. if( *confirm == StringVoid ){
  120. return zuluExit( 6,stl ) ;
  121. }
  122. if( !StringEqual( *confirm,gettext( "YES" ) ) ){
  123. return zuluExit( 7,stl ) ;
  124. }
  125. }
  126. }
  127. if( keyType == NULL ){
  128. printf( gettext( "Enter a key to be removed: " ) ) ;
  129. /*
  130. * ZULUCRYPT_KEY_MAX_SIZE is set in ../constants.h
  131. */
  132. switch( StringSilentlyGetFromTerminal_1( pass,ZULUCRYPT_KEY_MAX_SIZE ) ){
  133. case 1 : return zuluExit( 8,stl ) ;
  134. case 2 : return zuluExit( 9,stl ) ;
  135. }
  136. printf( "\n" ) ;
  137. key = StringContent( *pass ) ;
  138. key_size = StringLength( *pass ) ;
  139. zuluCryptSecurityLockMemory_1( *pass ) ;
  140. }else{
  141. if( keyType == NULL || keytoremove == NULL ){
  142. return zuluExit( 10,stl ) ;
  143. }
  144. if( StringsAreEqual( keyType,"-f" ) ){
  145. /*
  146. * zuluCryptGetPassFromFile() is defined at path_access.c"
  147. */
  148. switch( zuluCryptGetPassFromFile( keytoremove,uid,pass ) ){
  149. case 1 : return zuluExit( 11,stl ) ;
  150. case 2 : return zuluExit( 12,stl ) ;
  151. case 4 : return zuluExit( 13,stl ) ;
  152. case 5 : return zuluExit( 14,stl ) ;
  153. }
  154. key = StringContent( *pass ) ;
  155. key_size = StringLength( *pass ) ;
  156. zuluCryptSecurityLockMemory_1( *pass ) ;
  157. }else if( StringsAreEqual( keyType, "-p" ) ){
  158. key = keytoremove ;
  159. key_size = StringSize( keytoremove ) ;
  160. }else{
  161. return zuluExit( 10,stl ) ;
  162. }
  163. }
  164. zuluCryptSecurityGainElevatedPrivileges() ;
  165. /*
  166. * zuluCryptRemoveKey() is defined in ../lib/remove_key.c
  167. */
  168. status = zuluCryptRemoveKey( device,key,key_size ) ;
  169. zuluCryptSecurityDropElevatedPrivileges() ;
  170. if( status == 1 ){
  171. status = zuluExit_1( status,device,stl ) ;
  172. }else{
  173. status = zuluExit( status,stl ) ;
  174. }
  175. /*
  176. * zuluCryptCheckInvalidKey() is defined in check_invalid_key.c
  177. */
  178. zuluCryptCheckInvalidKey( opts->device ) ;
  179. return status ;
  180. }