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
|
/*
smbta-util: tool for controlling encryption with
vfs_smb_traffic_analyzer
Copyright (C) 2010 Holger Hetterich <hhetter@novell.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "includes.h"
#include "secrets.h"
static void delete_key(void);
static void help()
{
printf("-h print this help message.\n");
printf("-f <file> install the key from a file and activate\n");
printf(" encryption.\n");
printf("-g <file> generate a key, save it to a file, and activate encryption.\n");
printf("-u uninstall a key, and deactivate encryption.\n");
printf("-c <file> create a file from an installed key.\n");
printf("-s check if a key is installed, and print the key to stdout.\n");
printf("\n");
}
static void check_key()
{ size_t size;
char *akey;
if (!secrets_init()) {
printf("Error opening secrets database.");
exit(1);
}
akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
if (akey != NULL) {
printf("A key is installed: %s\n",akey);
printf("Encryption activated.\n");
free(akey);
exit(0);
} else printf("No key is installed.\n");
exit(1);
}
static void create_keyfile(char *filename, char *key)
{
FILE *keyfile;
keyfile = fopen(filename, "w");
if (keyfile == NULL) {
printf("error creating the keyfile!\n");
exit(1);
}
fprintf(keyfile, "%s", key);
fclose(keyfile);
printf("File '%s' has been created.\n", filename);
}
/**
* Load a key from a file. The caller has to free the
* returned string.
*/
static void load_key_from_file(char *filename, char *key)
{
FILE *keyfile;
int l;
keyfile = fopen(filename, "r");
if (keyfile == NULL) {
printf("Error opening the keyfile!\n");
exit(1);
}
l = fscanf(keyfile, "%s", key);
if (strlen(key) != 16) {
printf("Key file in wrong format\n");
fclose(keyfile);
exit(1);
}
}
static void create_file_from_key(char *filename)
{
size_t size;
char *akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
if (akey == NULL) {
printf("No key is installed! Can't create file.\n");
exit(1);
}
create_keyfile(filename, akey);
free(akey);
}
/**
* Generate a random key. The user has to free the returned
* string.
*/
static void generate_key(char *key)
{
int f;
srand( (unsigned)time( NULL ) );
for ( f = 0; f < 16; f++) {
*(key+f) = (rand() % 128) +32;
}
*(key+16)='\0';
printf("Random key generated.\n");
}
static void create_new_key_and_activate( char *filename )
{
char key[17] = {0};
if (!secrets_init()) {
printf("Error opening secrets database.");
exit(1);
}
generate_key(key);
delete_key();
secrets_store("smb_traffic_analyzer_key", key, strlen(key)+1 );
printf("Key installed, encryption activated.\n");
create_file_from_key(filename);
}
static void delete_key(void)
{
size_t size;
char *akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
if (akey != NULL) {
free(akey);
secrets_delete("smb_traffic_analyzer_key");
printf("Removed installed key. Encryption deactivated.\n");
} else {
printf("No key is installed.\n");
}
}
static void load_key_from_file_and_activate( char *filename)
{
char key[17] = {0};
char *akey;
size_t size;
load_key_from_file(filename, key);
printf("Loaded key from %s.\n",filename);
akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
if (akey != NULL) {
printf("Removing the old key.\n");
delete_key();
}
printf("Installing the key from file %s\n",filename);
secrets_store("smb_traffic_analyzer_key", key, strlen(key)+1);
}
static void process_arguments(int argc, char **argv)
{
char co;
while ((co = getopt(argc, argv, "hf:g:uc:s")) != EOF) {
switch(co) {
case 'h':
help();
exit(0);
case 's':
check_key();
break;
case 'g':
create_new_key_and_activate(optarg);
break;
case 'u':
delete_key();
break;
case 'c':
create_file_from_key(optarg);
break;
case 'f':
load_key_from_file_and_activate(optarg);
break;
default:
help();
break;
}
}
}
int main(int argc, char **argv)
{
sec_init();
load_case_tables();
if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
fprintf(stderr, "Can't load %s - run testparm to debug it\n",
get_dyn_CONFIGFILE());
exit(1);
}
if (argc == 1) {
help();
exit(1);
}
process_arguments(argc, argv);
exit(0);
}
|