File: server.c

package info (click to toggle)
zulucrypt 5.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,596 kB
  • sloc: cpp: 25,623; ansic: 23,788; makefile: 24; xml: 10; sh: 2
file content (247 lines) | stat: -rw-r--r-- 5,996 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * copyright: 2014-2015
 * name : Francis Banyikwa
 * email: mhogomchungu@gmail.com
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "network_key.h"

#include <signal.h>

#define DEBUG 1

static void _debug( const char * msg )
{
#if DEBUG
	puts( msg ) ;
#endif
}

#define _cast( x ) ( const char * ) x

static zuluValue_t _get_key_from_wallet( const zuluKey_t * key )
{
	zuluValue_t value ;

	lxqt_wallet_t w ;

	lxqt_wallet_key_values_t key_value ;

	lxqt_wallet_error r ;

	/*
	 * make sure these are NULL terminated
	 */
	char wallet_name[ sizeof( key->wallet_name ) ] ;
	char application_name[ sizeof( key->wallet_name ) ] ;

	memcpy( wallet_name,key->wallet_name,sizeof( key->wallet_name ) ) ;
	memcpy( application_name,key->application_name,sizeof( key->application_name ) ) ;

	*( wallet_name + sizeof( key->wallet_name ) - 1 ) = '\0' ;
	*( application_name + sizeof( key->application_name ) - 1 ) = '\0' ;

	memset( &value,'\0',sizeof( zuluValue_t ) ) ;

	if( key->wallet_key_length > sizeof( key->wallet_key ) ||
		key->key_0_length > sizeof( key->key_0 ) ||
		key->key_1_length > sizeof( key->key_1 ) ){

		return value ;
	}else{
		r = lxqt_wallet_open( &w,key->wallet_key,key->wallet_key_length,wallet_name,application_name ) ;

		if( r == lxqt_wallet_no_error ){

			if( lxqt_wallet_read_key_value( w,key->key_0,key->key_0_length,&key_value ) ){

				if( key_value.key_value_size <= sizeof( value.value ) ){

					value.key_found    = 1 ;
					value.value_length = key_value.key_value_size ;
					memcpy( value.value,key_value.key_value,key_value.key_value_size ) ;
				}
			}else if( lxqt_wallet_read_key_value( w,key->key_1,key->key_1_length,&key_value ) ){

				if( key_value.key_value_size <= sizeof( value.value ) ){

					value.key_found    = 1 ;
					value.value_length = key_value.key_value_size ;
					memcpy( value.value,key_value.key_value,key_value.key_value_size ) ;
				}
			}

			lxqt_wallet_close( &w ) ;
		}

		return value ;
	}
}

static void _process_request( socket_t s,crypt_buffer_ctx ctx,const crypt_buffer_result * r )
{
	zuluValue_t value ;

	crypt_buffer_result e ;

	if( r->length != sizeof( zuluKey_t ) ){

		value.key_found = 0 ;
	}else{
		value = _get_key_from_wallet( r->buffer ) ;

		if( crypt_buffer_encrypt( ctx,_cast( &value ),sizeof( zuluValue_t ),&e ) ){

			SocketSendData( s,e.buffer,e.length ) ;
		}
	}
}

typedef struct{
	crypt_buffer_ctx ctx ;
	socket_t s ;
	char * buffer ;
	size_t buffer_length ;
}exit_struct ;

static exit_struct _clean_on_exit ;

static int _exitServer( const char * msg )
{
	_debug( msg ) ;

	crypt_buffer_uninit( &_clean_on_exit.ctx ) ;

	SocketClose( &_clean_on_exit.s ) ;

	memset( _clean_on_exit.buffer,'\0',_clean_on_exit.buffer_length ) ;
	free( _clean_on_exit.buffer ) ;

	return 1 ;
}

static void _closeServer( int sig )
{
	_exitServer( "got TERM signal,terminating" ) ;
	exit( 1 ) ;
}

int main( int argc,char * argv[] )
{
	crypt_buffer_ctx ctx ;

	crypt_buffer_result r ;
	ssize_t n ;

	char * encryption_key ;
	size_t encryption_key_length ;

	int network_port             = PORT_NUMBER ;
	const char * network_address = NETWORK_ADDRESS ;
	socket_t c ;

	char * e ;

	#define buffer_size sizeof( zuluKey_t ) * 2
	char buffer[ buffer_size ] ;

	socket_t s ;

	struct sigaction sa ;

	if( argc < 2 ){

		puts( "error: invalid number of arguments" ) ;
		return 1 ;
	}else if( argc >= 4 ){

		network_address = *( argv + 2 ) ;
		network_port    = atoi( *( argv + 3 ) ) ;
	}

	memset( &sa,'\0',sizeof( struct sigaction ) ) ;
	sa.sa_handler = _closeServer ;
	sigaction( SIGTERM,&sa,NULL ) ;

	e = *( argv + 1 ) ;

	encryption_key_length = strlen( e ) ;

	encryption_key = malloc( sizeof( char ) * encryption_key_length ) ;
	memcpy( encryption_key,e,encryption_key_length ) ;

	memset( e,'\0',encryption_key_length ) ;

	s = SocketNet( network_address,network_port ) ;

	_clean_on_exit.s             = s ;
	_clean_on_exit.buffer        = encryption_key ;
	_clean_on_exit.buffer_length = encryption_key_length ;

	_debug( "server started" ) ;

	if( !SocketBind( s ) ){

		return _exitServer( "socket bind failed" ) ;
	}

	if( !SocketListen( s ) ){

		return _exitServer( "socket listen failed" ) ;
	}

	if( crypt_buffer_init( &ctx,encryption_key,encryption_key_length ) ){

		_clean_on_exit.ctx = ctx ;

		while( 1 ){

			c = SocketAccept( s ) ;

			n = SocketGetData_3( c,buffer,buffer_size,4 ) ;

			if( n != -1 ){

				if( crypt_buffer_decrypt( ctx,buffer,n,&r ) ){

					_process_request( c,ctx,&r ) ;
				}else{
					_debug( "failed to decrypt data" ) ;
				}
			}else{
				_debug( "network timed out" ) ;
			}

			SocketClose( &c ) ;
		}

	}else{
		_debug( "failed to initialize encryption routine" ) ;
	}

	return 0 ;
}