Add smbta-util to manage the encryption key.
[Samba/nascimento.git] / source3 / utils / smbta-util.c
blob13686ae7a0afea1a1b0cc2e3bcdcdc71584cf432
1 /*
2 smbta-util: tool for controlling encryption with
3 vfs_smb_traffic_analyzer
4 Copyright (C) 2010 Holger Hetterich <hhetter@novell.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 3 of the License, or
9 (at your option) any later version.
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.
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/>. */
19 #include "includes.h"
22 static void delete_key();
25 static void help()
27 printf("-h print this help message.\n");
28 printf("-f <file> install the key from a file and activate\n");
29 printf(" encryption.\n");
30 printf("-g <file> generate a key, save it to a file, and activate encryption.\n");
31 printf("-u uninstall a key, and deactivate encryption.\n");
32 printf("-c <file> create a file from an installed key.\n");
33 printf("-s check if a key is installed, and print the key to stdout.\n");
34 printf("\n");
37 static void check_key()
38 { size_t size;
39 if (!secrets_init()) {
40 printf("Error opening secrets database.");
41 exit(1);
43 char *akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
44 if (akey != NULL) {
45 printf("A key is installed: %s\n",akey);
46 printf("Encryption activated.\n");
47 free(akey);
48 exit(0);
49 } else printf("No key is installed.\n");
50 exit(1);
53 static void create_keyfile(char *filename, char *key)
55 FILE *keyfile;
56 keyfile = fopen(filename, "w");
57 if (keyfile == NULL) {
58 printf("error creating the keyfile!\n");
59 exit(0);
61 fprintf(keyfile, "%s", key);
62 fclose(keyfile);
63 printf("File '%s' has been created.\n", filename);
66 /**
67 * Load a key from a file. The caller has to free the
68 * returned string.
70 static char *load_key_from_file(char *filename)
72 FILE *keyfile;
73 char *key = malloc(sizeof(char) * 17);
74 int l;
75 keyfile = fopen(filename, "r");
76 if (keyfile == NULL) {
77 printf("Error opening the keyfile!\n");
78 exit(0);
80 l = fscanf(keyfile, "%s", key);
81 if (strlen(key) != 16) {
82 printf("Key file in wrong format\n");
83 fclose(keyfile);
84 exit(0);
86 return key;
89 static void create_file_from_key(char *filename)
91 size_t size;
92 char *akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
93 if (akey == NULL) {
94 printf("No key is installed! Can't create file.\n");
95 exit(1);
97 create_keyfile(filename, akey);
98 free(akey);
102 * Generate a random key. The user has to free the returned
103 * string.
105 static char *generate_key()
107 char *key = malloc(sizeof(char)*17);
108 int f;
109 srand( (unsigned)time( NULL ) );
110 for ( f = 0; f < 16; f++) {
111 *(key+f) = (rand() % 128) +32;
113 *(key+16)='\0';
114 printf("Random key generated.\n");
115 return key;
118 static void create_new_key_and_activate( char *filename )
120 if (!secrets_init()) {
121 printf("Error opening secrets database.");
122 exit(1);
125 char *key = generate_key();
126 delete_key();
127 secrets_store("smb_traffic_analyzer_key", key, strlen(key)+1 );
128 printf("Key installed, encryption activated.\n");
129 create_file_from_key(filename);
130 free(key);
133 static void delete_key()
135 size_t size;
136 char *akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
137 if (akey != NULL) {
138 free(akey);
139 secrets_delete("smb_traffic_analyzer_key");
140 printf("Removed installed key. Encryption deactivated.\n");
141 } else {
142 printf("No key is installed.\n");
147 static void load_key_from_file_and_activate( char *filename)
149 char *key;
150 char *akey;
151 size_t size;
152 key = load_key_from_file(filename);
153 printf("Loaded key from %s.\n",filename);
154 akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
155 if (akey != NULL) {
156 printf("Removing the old key.\n");
157 delete_key();
159 printf("Installing the key from file %s\n",filename);
160 secrets_store("smb_traffic_analyzer_key", key, strlen(key)+1);
161 free(key);
164 static void process_arguments(int argc, char **argv)
166 char co;
167 while ((co = getopt(argc, argv, "hf:g:uc:s")) != EOF) {
168 switch(co) {
169 case 'h':
170 help();
171 exit(0);
172 case 's':
173 check_key();
174 break;
175 case 'g':
176 create_new_key_and_activate(optarg);
177 break;
178 case 'u':
179 delete_key();
180 break;
181 case 'c':
182 create_file_from_key(optarg);
183 break;
184 case 'f':
185 load_key_from_file_and_activate(optarg);
186 break;
187 default:
188 help();
189 break;
194 int main(int argc, char **argv)
196 sec_init();
197 load_case_tables();
199 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
200 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
201 get_dyn_CONFIGFILE());
202 exit(1);
205 if (argc == 1) {
206 help();
207 exit(1);
210 process_arguments(argc, argv);