PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/zuluCrypt-cli/pluginManager/zuluCryptPluginManager.c

https://gitlab.com/m.schmidt/zuluCrypt
C | 224 lines | 146 code | 48 blank | 30 comment | 18 complexity | 4955cbf0e76b82d78cf2291bca11a7fb MD5 | raw file
Possible License(s): BSD-3-Clause-No-Nuclear-License-2014, BSD-2-Clause
  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. #include <dlfcn.h>
  20. #include <sys/stat.h>
  21. #include <unistd.h>
  22. #include <pwd.h>
  23. #include <blkid/blkid.h>
  24. #include <sys/syscall.h>
  25. #include "libzuluCryptPluginManager.h"
  26. #include "../utility/process/process.h"
  27. #include "../utility/socket/socket.h"
  28. #include "../utility/string/String.h"
  29. #include "../constants.h"
  30. #include "../bin/includes.h"
  31. #include "../bin/libzuluCrypt-exe.h"
  32. #define _ignore_result( x ) if( x ){;}
  33. /*
  34. * below header file is created at config time.
  35. */
  36. #include "plugin_path.h"
  37. #include <stdio.h>
  38. #if 0
  39. static void _debug_0( process_t p,ProcessIO std_io )
  40. {
  41. char * e = NULL ;
  42. if( std_io == ProcessStdOut ){
  43. puts( "--------stdout------------" ) ;
  44. }else{
  45. puts( "--------stderror----------" ) ;
  46. }
  47. while( 1 ){
  48. ProcessGetOutPut( p,&e,std_io ) ;
  49. if( e ){
  50. printf( "%s",e ) ;
  51. fflush( stdout ) ;
  52. free( e ) ;
  53. e = NULL ;
  54. }else{
  55. break ;
  56. }
  57. }
  58. }
  59. static void _debug( process_t p )
  60. {
  61. _debug_0( p,ProcessStdOut ) ;
  62. _debug_0( p,ProcessStdError ) ;
  63. }
  64. #else
  65. static void _debug( process_t p )
  66. {
  67. if( p ){;}
  68. }
  69. #endif
  70. void zuluCryptGetKeyFromSocket( const char * sockpath,string_t * key,uid_t uid )
  71. {
  72. ssize_t dataLength = -1 ;
  73. char * buffer = NULL ;
  74. socket_t client ;
  75. socket_t server = SocketLocal( sockpath ) ;
  76. if( SocketBind( server ) ){
  77. _ignore_result( chown( sockpath,uid,uid ) ) ;
  78. chmod( sockpath,S_IRWXU | S_IRWXG | S_IRWXO ) ;
  79. if( SocketListen( server ) ){
  80. client = SocketAccept( server ) ;
  81. /*
  82. * ZULUCRYPT_INT_MAX_KEYSIZE is set in ../constants.h
  83. */
  84. dataLength = SocketGetData_1( client,&buffer,ZULUCRYPT_INT_MAX_KEYSIZE ) ;
  85. if( dataLength != -1 ){
  86. *key = StringInheritWithSize( &buffer,dataLength,dataLength + 1 ) ;
  87. }else{
  88. *key = StringEmpty() ;
  89. }
  90. SocketClose( &client ) ;
  91. }
  92. SocketClose( &server ) ;
  93. }
  94. }
  95. void * zuluCryptPluginManagerOpenConnection( const char * sockpath )
  96. {
  97. int i ;
  98. socket_t client ;
  99. for( i = 0 ; i < 10 ; i++ ){
  100. client = SocketLocal( sockpath ) ;
  101. if( SocketConnect( &client ) ){
  102. return client ;
  103. }else{
  104. sleep( 1 ) ;
  105. }
  106. }
  107. return NULL ;
  108. }
  109. ssize_t zuluCryptPluginManagerSendKey( void * client,const char * key,size_t length )
  110. {
  111. return SocketSendData( client,key,length ) ;
  112. }
  113. void zuluCryptPluginManagerCloseConnection( void * e )
  114. {
  115. socket_t client = e ;
  116. SocketClose( &client ) ;
  117. }
  118. string_t zuluCryptPluginManagerGetKeyFromModule( const char * device,const char * plugin,
  119. const char * uuid,uid_t uid,const struct_opts * opts,int * r )
  120. {
  121. process_t p ;
  122. struct stat st ;
  123. const char * sockpath ;
  124. const char * argv = opts->argv ;
  125. char * const * env = opts->env ;
  126. const char * args[ 7 ] ;
  127. ProcessStructure * str ;
  128. string_t key = StringVoid ;
  129. string_t plugin_path = StringVoid ;
  130. string_t path = StringVoid ;
  131. struct passwd * pass = getpwuid( uid ) ;
  132. if( pass == NULL ){
  133. return key ;
  134. }
  135. /*
  136. * ZULUCRYPTpluginPath is set at config time at it equals $prefix/lib(64)/zuluCrypt/
  137. */
  138. plugin_path = String( ZULUCRYPTpluginPath ) ;
  139. plugin = plugin + StringLastIndexOfChar_1( plugin,'/' ) + 1 ;
  140. plugin = StringAppend( plugin_path,plugin ) ;
  141. if( stat( plugin,&st ) == 0 && S_ISREG( st.st_mode ) ) {
  142. path = String( pass->pw_dir ) ;
  143. sockpath = StringAppend( path,"/.zuluCrypt-socket/" ) ;
  144. mkdir( sockpath,S_IRWXU | S_IRWXG | S_IRWXO ) ;
  145. _ignore_result( chown( sockpath,uid,uid ) ) ;
  146. _ignore_result( chmod( sockpath,S_IRWXU ) ) ;
  147. sockpath = StringAppendInt( path,syscall( SYS_gettid ) ) ;
  148. *( args + 0 ) = plugin ;
  149. *( args + 1 ) = device ;
  150. if( uuid != NULL ){
  151. *( args + 2 ) = uuid ;
  152. }else{
  153. *( args + 2 ) = "Nil" ;
  154. }
  155. *( args + 3 ) = sockpath ;
  156. /*
  157. * ZULUCRYPT_CHAR_MAX_KEYSIZE is set in ../constants.h
  158. */
  159. *( args + 4 ) = ZULUCRYPT_CHAR_MAX_KEYSIZE ;
  160. *( args + 5 ) = argv ;
  161. *( args + 6 ) = NULL ;
  162. p = Process( NULL,NULL ) ;
  163. str = ProcessArgumentStructure( p ) ;
  164. str->args = ( char * const * )args ;
  165. str->env = env ;
  166. str->user_id = uid ;
  167. ProcessStart( p ) ;
  168. zuluCryptGetKeyFromSocket( sockpath,&key,uid ) ;
  169. _debug( p ) ;
  170. *r = ProcessWaitUntilFinished( &p ) ;
  171. }else{
  172. *r = 1 ;
  173. }
  174. StringMultipleDelete( &plugin_path,&path,NULL ) ;
  175. return key ;
  176. }