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)  

create_volume.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 
22 #include <sys/syscall.h>
23 #include <libcryptsetup.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <limits.h>
29 
30 int zuluCryptOpenPlain_2( const char * device,
31  const char * mapper,
32  const char * mode,
33  const char * key,
34  size_t key_len,
35  const char * rng ) ;
36 
37 static int zuluExit( int st,string_t m )
38 {
39  StringDelete( &m ) ;
40  return st ;
41 }
42 
43 int zuluCryptCreateFileSystemInAVolume( const char * fs,const char * device_mapper )
44 {
45  int r ;
46  char * e = NULL ;
47  process_t p ;
48 
49  if( StringsAreEqual( fs,"none" ) ){
50 
51  return 0 ;
52  }
53  /*
54  * zulucryptFileSystemIsSupported() is defined in mount_fs_options.c
55  */
56  if( zulucryptFileSystemIsSupported( fs ) == 0 ){
57 
58  return 1 ;
59  }
60 
61  p = Process( ZULUCRYPTmkfs,NULL ) ;
62 
63  if( StringAtLeastOneMatch_1( fs,"ext2","ext3","ext4",NULL ) ){
64 
65  ProcessSetArgumentList( p,"-t",fs,"-m","1",device_mapper,NULL ) ;
66 
67  }else if( StringsAreEqual( fs,"reiserfs" ) ){
68 
69  ProcessSetArgumentList( p,"-t",fs,"-f","-f","-q",device_mapper,NULL ) ;
70 
71  }else if( StringsAreEqual( fs,"jfs" ) ){
72 
73  ProcessSetArgumentList( p,"-t",fs,"-q",device_mapper,NULL ) ;
74 
75  }else if( StringsAreEqual( fs,"ntfs" ) ){
76 
77  ProcessSetArgumentList( p,"-t",fs,"-f",device_mapper,NULL ) ;
78 
79  }else if( StringsAreEqual( fs,"xfs" ) ){
80 
81  ProcessSetArgumentList( p,"-t",fs,"-f",device_mapper,NULL ) ;
82 
83  }else{
84  ProcessSetArgumentList( p,"-t",fs,device_mapper,NULL ) ;
85 
86  /*
87  * unhandled fs are processed here.They are given 60 seconds to accomplish their task
88  * and are assumed to be running in interactive more and are blocked waiting for user input
89  * when they fail to return in time and hence are killed since we cant get to them from GUI
90  */
91 
92  ProcessSetOptionTimeout( p,60,SIGKILL ) ;
93  }
94 
95  ProcessStart( p ) ;
96 
97  r = ProcessExitStatus( p ) ;
98 
99  if( r != 0 ){
100 
102 
103  if( e ){
104 
105  puts( e ) ;
106 
107  StringFree( e ) ;
108  }
109  }
110 
111  ProcessCleanUp( &p ) ;
112 
113  return r ;
114 }
115 
116 typedef struct{
117 
118  const char * fs ;
119  const char * type ;
120  const char * pass ;
121  size_t pass_size ;
122  const char * rng ;
123 
124 }arguments ;
125 
126 static int _create_volume( const char * device,const resolve_path_t * opts )
127 {
128  int r ;
129 
130  const char * mapper ;
131 
132  const arguments * args = opts->args ;
133 
134  string_t m = String( "/zuluCrypt-create-volume-" ) ;
135  mapper = StringAppendInt( m,syscall( SYS_gettid ) ) + 1 ;
136 
137  if( StringsAreEqual( args->type,"plain" ) ){
138 
139  if( zuluCryptOpenPlain_2( device,mapper,"rw",args->pass,args->pass_size,args->rng ) != 0 ){
140 
141  return zuluExit( 3,m ) ;
142  }
143  }else{
144  if( StringAtLeastOneMatch_1( args->type,"luks","luks1",NULL ) ){
145 
146  if( zuluCryptCreateLuks( device,args->pass,args->pass_size,args->rng ) != 0 ){
147 
148  return zuluExit( 3,m ) ;
149  }
150 
151  }else if( StringsAreEqual( args->type,"luks2" ) ){
152 
153  if( zuluCryptCreateLuks2( device,args->pass,args->pass_size,args->rng ) != 0 ){
154 
155  return zuluExit( 3,m ) ;
156  }
157  }else{
158  return zuluExit( 3,m ) ;
159  }
160 
161  if( zuluCryptOpenLuks( device,mapper,"rw",args->pass,args->pass_size ) != 0 ){
162 
163  return zuluExit( 3,m ) ;
164  }
165  }
166 
167  mapper = StringPrepend( m,crypt_get_dir() ) ;
168 
169  r = zuluCryptCreateFileSystemInAVolume( args->fs,mapper ) ;
170  /*
171  * zuluCryptCloseMapper() is defined in close_mapper.c
172  */
173  zuluCryptCloseMapper( mapper ) ;
174 
175  if( r == 0 ){
176 
177  return zuluExit( 0,m ) ;
178  }else{
179  return zuluExit( 3,m ) ;
180  }
181 }
182 
183 int zuluCryptCreateVolume( const char * device,const char * fs,
184  const char * type,const char * pass,
185  size_t pass_size,const char * rng )
186 {
187  /*
188  * resolve_path_t is defined in includes.h
189  */
190  resolve_path_t opts ;
191  arguments args ;
192 
193  memset( &opts,'\0',sizeof( opts ) ) ;
194  memset( &args,'\0',sizeof( args ) ) ;
195 
196  args.fs = fs ;
197  args.type = type ;
198  args.pass = pass ;
199  args.pass_size = pass_size ;
200  args.rng = rng ;
201 
202  opts.device = device ;
203  opts.args = &args ;
204  opts.open_mode = O_RDWR ;
205  opts.error_value = 3 ;
206 
207  /*
208  * zuluCryptResolveDevicePath() is defined in resolve_path.c
209  */
211 }
ProcessGetOutPut
size_t ProcessGetOutPut(process_t p, char **data, ProcessIO std_io)
Definition: process.c:438
StringAppendInt
const char * StringAppendInt(string_t st, u_int64_t z)
Definition: String.c:1410
zuluCryptOpenLuks
int zuluCryptOpenLuks(const char *device, const char *mapping_name, const char *mode, const char *passphrase, size_t passphrase_size)
Definition: open_luks.c:104
zuluCryptResolveDevicePath
int zuluCryptResolveDevicePath(int(*)(const char *, const resolve_path_t *), const resolve_path_t *)
Definition: resolve_paths.c:389
zuluCryptCreateVolume
int zuluCryptCreateVolume(const char *device, const char *fs, const char *type, const char *pass, size_t pass_size, const char *rng)
Definition: create_volume.c:183
zulucryptFileSystemIsSupported
int zulucryptFileSystemIsSupported(const char *fs)
Definition: mount_fs_options.c:64
zuluCryptCreateFileSystemInAVolume
int zuluCryptCreateFileSystemInAVolume(const char *fs, const char *device_mapper)
Definition: create_volume.c:43
arguments
Definition: add_key.c:25
zuluCryptOpenPlain_2
int zuluCryptOpenPlain_2(const char *device, const char *mapper, const char *mode, const char *key, size_t key_len, const char *rng)
Definition: open_plain.c:164
arguments::pass
const char * pass
Definition: create_volume.c:120
ZULUCRYPTmkfs
#define ZULUCRYPTmkfs
Definition: constants.h:25
ProcessStdError
@ ProcessStdError
Definition: process.h:200
StringPrepend
const char * StringPrepend(string_t st, const char *s)
Definition: String.c:1015
resolve_path_t::args
const void * args
Definition: includes.h:119
StringsAreEqual
#define StringsAreEqual(x, y)
Definition: zuluSafe.c:46
Process
process_t Process(const char *path,...)
Definition: process.c:185
arguments
struct arguments arguments
zuluExit
static int zuluExit(int st, string_t m)
Definition: create_volume.c:37
arguments::fs
const char * fs
Definition: create_volume.c:118
args
Definition: open_plain.c:25
ProcessType_t
Definition: process.c:41
ProcessSetArgumentList
void ProcessSetArgumentList(process_t p,...)
Definition: process.c:138
StringAtLeastOneMatch_1
int StringAtLeastOneMatch_1(const char *x,...)
Definition: String.c:1504
StringType
Definition: String.c:49
resolve_path_t::error_value
int error_value
Definition: includes.h:116
zuluCryptCreateLuks
int zuluCryptCreateLuks(const char *device, const char *key, size_t key_len, const char *options)
Definition: create_luks.c:285
String
string_t String(const char *cstring)
Definition: String.c:318
ProcessStart
pid_t ProcessStart(process_t p)
Definition: process.c:337
zuluCryptCreateLuks2
int zuluCryptCreateLuks2(const char *device, const char *pass, size_t pass_size, const char *options)
Definition: create_luks.c:427
resolve_path_t::device
const char * device
Definition: includes.h:115
args::rng
const char * rng
Definition: open_plain.c:27
zuluCryptCloseMapper
int zuluCryptCloseMapper(const char *mapper)
Definition: close_mapper.c:84
e
static QString e
Definition: about.cpp:31
includes.h
StringDelete
void StringDelete(string_t *st)
Definition: String.c:162
resolve_path_t::open_mode
int open_mode
Definition: includes.h:118
ProcessExitStatus
int ProcessExitStatus(process_t p)
Definition: process.c:634
ProcessSetOptionTimeout
void ProcessSetOptionTimeout(process_t p, int timeout, int signal)
Definition: process.c:538
resolve_path_t
Definition: includes.h:114
_create_volume
static int _create_volume(const char *device, const resolve_path_t *opts)
Definition: create_volume.c:126
ProcessCleanUp
void ProcessCleanUp(process_t *p)
Definition: process.c:570
arguments::pass_size
size_t pass_size
Definition: create_volume.c:121
StringFree
static __inline__ void StringFree(const void *str)
Definition: String.h:823