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)  

zuluCryptPluginManager.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 <dlfcn.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <pwd.h>
24 #include <blkid/blkid.h>
25 #include <sys/syscall.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <sys/stat.h>
29 
31 #include "../utility/process/process.h"
32 #include "../utility/socket/socket.h"
33 #include "../utility/string/String.h"
34 #include "../constants.h"
35 #include "../bin/includes.h"
36 #include "../bin/libzuluCrypt-exe.h"
37 
38 #define _ignore_result( x ) if( x ){;}
39 
40 /*
41  * below header file is created at config time.
42  */
43 #include "plugin_path.h"
44 
45 static void _debug_0( process_t p,ProcessIO std_io )
46 {
47  char * e = NULL ;
48 
49  if( std_io == ProcessStdOut ){
50  puts( "--------stdout------------" ) ;
51  }else{
52  puts( "--------stderror----------" ) ;
53  }
54 
55  while( 1 ){
56  ProcessGetOutPut( p,&e,std_io ) ;
57  if( e ){
58  printf( "%s",e ) ;
59  fflush( stdout ) ;
60  free( e ) ;
61  e = NULL ;
62  }else{
63  break ;
64  }
65  }
66 }
67 
68 static void _debug( process_t p )
69 {
70  _debug_0( p,ProcessStdOut ) ;
72 }
73 
74 void zuluCryptGetKeyFromSocket( const char * sockpath,string_t * key,uid_t uid )
75 {
76  ssize_t e ;
77  char * buffer = NULL ;
78 
79  socket_t client ;
80  socket_t server = SocketLocal( sockpath ) ;
81 
82  *key = StringVoid ;
83 
84  if( uid ){}
85 
86  errno = 0 ;
87 
88  if( SocketBind( server ) ){
89 
90  if( SocketListen( server ) ){
91 
92  client = SocketAccept( server ) ;
93 
94  /*
95  * ZULUCRYPT_INT_MAX_KEYSIZE is set in ../constants.h
96  */
97  e = SocketGetData_1( client,&buffer,ZULUCRYPT_INT_MAX_KEYSIZE ) ;
98 
99  if( e == 0 ){
100 
101  *key = StringEmpty() ;
102 
103  }else if( e == -1 ){
104 
105  perror( "Failed to read data from socket" ) ;
106  }else{
107  *key = StringInheritWithSize( &buffer,e,e + 1 ) ;
108  }
109 
110  SocketClose( &client ) ;
111  }else{
112  perror( "Socket listen failed" ) ;
113  }
114 
115  SocketClose( &server ) ;
116  }else{
117  perror( "Socket bind failed" ) ;
118  }
119 }
120 
121 void * zuluCryptPluginManagerOpenConnection( const char * sockpath )
122 {
123  int i ;
124  socket_t client ;
125 
126  for( i = 0 ; i < 10 ; i++ ){
127 
128  client = SocketLocal( sockpath ) ;
129 
130  if( SocketConnect( &client ) ){
131 
132  return client ;
133  }else{
134  sleep( 1 ) ;
135  }
136  }
137 
138  printf( "Client: Failed to open connection at:%s\n",sockpath ) ;
139  perror( "Reason:" ) ;
140 
141  return NULL ;
142 }
143 
144 ssize_t zuluCryptPluginManagerSendKey( void * client,const char * key,size_t length )
145 {
146  return SocketSendData( client,key,length ) ;
147 }
148 
150 {
151  socket_t client = e ;
152  SocketClose( &client ) ;
153 }
154 
155 static void _create_path( const char * path,uid_t uid )
156 {
157  if( mkdir( path,0700 ) == 0 ){
158 
159  _ignore_result( chmod( path,0700 ) ) ;
160  _ignore_result( chown( path,uid,uid ) ) ;
161  }
162 }
163 
164 string_t zuluCryptPluginManagerGetKeyFromModule( const char * device,const char * plugin,
165  const char * uuid,
166  uid_t uid,
167  const struct_opts * opts,
168  const char * run_path,
169  int * r )
170 {
171  process_t p ;
172  struct stat st ;
173 
174  const char * sockpath ;
175  const char * argv = opts->argv ;
176  char * const * env = opts->env ;
177  const char * args[ 7 ] ;
178 
179  ProcessStructure * str ;
180 
181  string_t key = StringVoid ;
182  string_t plugin_path = StringVoid ;
183  string_t path = StringVoid ;
184 
185  struct passwd * pass = getpwuid( uid ) ;
186 
187  if( pass == NULL ){
188  return key ;
189  }
190  /*
191  * ZULUCRYPTpluginPath is set at config time at it equals $prefix/lib(64)/zuluCrypt/
192  */
193  plugin_path = String( ZULUCRYPTpluginPath ) ;
194 
196 
197  plugin = StringAppend( plugin_path,plugin ) ;
198 
199  if( stat( plugin,&st ) == 0 && S_ISREG( st.st_mode ) ) {
200 
201  _create_path( run_path,uid ) ;
202 
203  path = String( run_path ) ;
204  sockpath = StringAppendInt( path,syscall( SYS_gettid ) ) ;
205 
206  *( args + 0 ) = plugin ;
207  *( args + 1 ) = device ;
208 
209  if( uuid != NULL ){
210  *( args + 2 ) = uuid ;
211  }else{
212  *( args + 2 ) = "Nil" ;
213  }
214 
215  *( args + 3 ) = sockpath ;
216  /*
217  * ZULUCRYPT_CHAR_MAX_KEYSIZE is set in ../constants.h
218  */
219  *( args + 4 ) = ZULUCRYPT_CHAR_MAX_KEYSIZE ;
220  *( args + 5 ) = argv ;
221  *( args + 6 ) = NULL ;
222 
223  p = Process( NULL,NULL ) ;
224  str = ProcessArgumentStructure( p ) ;
225  str->args = ( char * const * )args ;
226  str->env = env ;
227  str->user_id = uid ;
228 
229  ProcessStart( p ) ;
230 
231  zuluCryptGetKeyFromSocket( sockpath,&key,uid ) ;
232 
233  *r = ProcessWaitUntilFinished( &p ) ;
234 
235  _debug( p ) ;
236  }else{
237  *r = 1 ;
238  }
239 
240  StringMultipleDelete( &plugin_path,&path,NULL ) ;
241 
242  return key ;
243 }
ProcessGetOutPut
size_t ProcessGetOutPut(process_t p, char **data, ProcessIO std_io)
Definition: process.c:438
zuluCryptPluginManagerCloseConnection
void zuluCryptPluginManagerCloseConnection(void *e)
Definition: zuluCryptPluginManager.c:149
StringAppendInt
const char * StringAppendInt(string_t st, u_int64_t z)
Definition: String.c:1410
ProcessStructure::env
char *const * env
Definition: process.h:41
ZULUCRYPT_INT_MAX_KEYSIZE
#define ZULUCRYPT_INT_MAX_KEYSIZE
Definition: constants.h:51
struct_opts_1::argv
const char * argv
Definition: libzuluCrypt-exe.h:50
StringInheritWithSize
string_t StringInheritWithSize(char **data, size_t size, size_t length)
Definition: String.c:429
ZULUCRYPT_CHAR_MAX_KEYSIZE
#define ZULUCRYPT_CHAR_MAX_KEYSIZE
Definition: constants.h:52
ProcessArgumentStructure
ProcessStructure * ProcessArgumentStructure(process_t p)
Definition: process.c:73
plugin
Definition: plugin.h:42
struct_opts_1
Definition: libzuluCrypt-exe.h:35
ProcessStdError
@ ProcessStdError
Definition: process.h:200
SocketClose
void SocketClose(socket_t *p)
Definition: socket.c:583
SocketListen
int SocketListen(socket_t s)
Definition: socket.c:659
ProcessStructure
Definition: process.h:29
libzuluCryptPluginManager.h
SocketConnect
int SocketConnect(socket_t *s)
Definition: socket.c:609
ProcessWaitUntilFinished
int ProcessWaitUntilFinished(process_t *e)
Definition: process.c:614
Process
process_t Process(const char *path,...)
Definition: process.c:185
zuluCryptPluginManagerGetKeyFromModule
string_t zuluCryptPluginManagerGetKeyFromModule(const char *device, const char *plugin, const char *uuid, uid_t uid, const struct_opts *opts, const char *run_path, int *r)
Definition: zuluCryptPluginManager.c:164
_debug
static void _debug(process_t p)
Definition: zuluCryptPluginManager.c:68
SocketSendData
ssize_t SocketSendData(socket_t s, const char *buffer, size_t len)
Definition: socket.c:860
args
Definition: open_plain.c:25
ProcessType_t
Definition: process.c:41
StringType
Definition: String.c:49
StringMultipleDelete
void StringMultipleDelete(string_t *xt,...)
Definition: String.c:195
StringLastIndexOfChar_1
static __inline__ ssize_t StringLastIndexOfChar_1(const char *str, char s)
Definition: String.h:982
String
string_t String(const char *cstring)
Definition: String.c:318
ProcessStart
pid_t ProcessStart(process_t p)
Definition: process.c:337
StringEmpty
string_t StringEmpty()
Definition: String.c:292
SocketGetData_1
ssize_t SocketGetData_1(socket_t s, char **e, size_t len)
Definition: socket.c:784
_debug_0
static void _debug_0(process_t p, ProcessIO std_io)
Definition: zuluCryptPluginManager.c:45
_create_path
static void _create_path(const char *path, uid_t uid)
Definition: zuluCryptPluginManager.c:155
SocketType_t
Definition: socket.c:42
ProcessStructure::user_id
uid_t user_id
Definition: process.h:37
e
static QString e
Definition: about.cpp:31
ProcessIO
ProcessIO
Definition: process.h:197
zuluCryptPluginManagerOpenConnection
void * zuluCryptPluginManagerOpenConnection(const char *sockpath)
Definition: zuluCryptPluginManager.c:121
SocketLocal
socket_t SocketLocal(const char *address)
Definition: socket.c:205
zuluCryptPluginManagerSendKey
ssize_t zuluCryptPluginManagerSendKey(void *client, const char *key, size_t length)
Definition: zuluCryptPluginManager.c:144
ProcessStructure::args
char *const * args
Definition: process.h:33
ProcessStdOut
@ ProcessStdOut
Definition: process.h:199
_ignore_result
#define _ignore_result(x)
Definition: zuluCryptPluginManager.c:38
zuluCryptGetKeyFromSocket
void zuluCryptGetKeyFromSocket(const char *sockpath, string_t *key, uid_t uid)
Definition: zuluCryptPluginManager.c:74
struct_opts_1::env
char *const * env
Definition: libzuluCrypt-exe.h:59
StringAppend
const char * StringAppend(string_t st, const char *s)
Definition: String.c:1052
SocketAccept
socket_t SocketAccept(socket_t s)
Definition: socket.c:500
SocketBind
int SocketBind(socket_t s)
Definition: socket.c:344
StringVoid
#define StringVoid
Definition: String.h:47