Fix developer build, remove malloc
[samba.git] / source3 / utils / smbta-util.c
1 /*
2    smbta-util: tool for controlling encryption with
3         vfs_smb_traffic_analyzer
4    Copyright (C) 2010 Holger Hetterich <hhetter@novell.com>
5
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.
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 #include "includes.h"
20
21
22 static void delete_key();
23
24
25 static void help()
26 {
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");
35 }
36
37 static void check_key()
38 {       size_t size;
39         if (!secrets_init()) {
40                 printf("Error opening secrets database.");
41                 exit(1);
42         }
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);
51 }
52
53 static void create_keyfile(char *filename, char *key)
54 {
55         FILE *keyfile;
56         keyfile = fopen(filename, "w");
57         if (keyfile == NULL) {
58                 printf("error creating the keyfile!\n");
59                 exit(1);
60         }
61         fprintf(keyfile, "%s", key);
62         fclose(keyfile);
63         printf("File '%s' has been created.\n", filename);
64 }
65
66 /**
67  * Load a key from a file. The caller has to free the
68  * returned string.
69  */
70 static void load_key_from_file(char *filename, char *key)
71 {
72         FILE *keyfile;
73         int l;
74         keyfile = fopen(filename, "r");
75         if (keyfile == NULL) {
76                 printf("Error opening the keyfile!\n");
77                 exit(1);
78         }
79         l = fscanf(keyfile, "%s", key);
80         if (strlen(key) != 16) {
81                 printf("Key file in wrong format\n");
82                 fclose(keyfile);
83                 exit(1);
84         }
85 }
86
87 static void create_file_from_key(char *filename)
88 {
89         size_t size;
90         char *akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
91         if (akey == NULL) {
92                 printf("No key is installed! Can't create file.\n");
93                 exit(1);
94         }
95         create_keyfile(filename, akey);
96         free(akey);
97 }
98
99 /**
100  * Generate a random key. The user has to free the returned
101  * string.
102  */
103 static void generate_key(char *key)
104 {
105         int f;
106         srand( (unsigned)time( NULL ) );
107         for ( f = 0; f < 16; f++) {
108                 *(key+f) = (rand() % 128) +32;
109         }
110         *(key+16)='\0';
111         printf("Random key generated.\n");
112 }
113
114 static void create_new_key_and_activate( char *filename )
115 {
116         char key[17] = {0};
117
118         if (!secrets_init()) {
119                 printf("Error opening secrets database.");
120                 exit(1);
121         }
122
123         generate_key(key);
124         delete_key();
125         secrets_store("smb_traffic_analyzer_key", key, strlen(key)+1 );
126         printf("Key installed, encryption activated.\n");
127         create_file_from_key(filename);
128 }
129
130 static void delete_key()
131 {
132         size_t size;
133         char *akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
134         if (akey != NULL) {
135                 free(akey);
136                 secrets_delete("smb_traffic_analyzer_key");
137                 printf("Removed installed key. Encryption deactivated.\n");
138         } else {
139         printf("No key is installed.\n");
140         }
141 }
142
143
144 static void load_key_from_file_and_activate( char *filename)
145 {
146         char key[17] = {0};
147         char *akey;
148         size_t size;
149         load_key_from_file(filename, key);
150         printf("Loaded key from %s.\n",filename);
151         akey = (char *) secrets_fetch("smb_traffic_analyzer_key", &size);
152         if (akey != NULL) {
153                 printf("Removing the old key.\n");
154                 delete_key();
155         }
156         printf("Installing the key from file %s\n",filename);
157         secrets_store("smb_traffic_analyzer_key", key, strlen(key)+1);
158 }
159
160 static void process_arguments(int argc, char **argv)
161 {
162         char co;
163         while ((co = getopt(argc, argv, "hf:g:uc:s")) != EOF) {
164                 switch(co) {
165                 case 'h':
166                         help();
167                         exit(0);
168                 case 's':
169                         check_key();
170                         break;
171                 case 'g':
172                         create_new_key_and_activate(optarg);
173                         break;
174                 case 'u':
175                         delete_key();
176                         break;
177                 case 'c':
178                         create_file_from_key(optarg);
179                         break;
180                 case 'f':
181                         load_key_from_file_and_activate(optarg);
182                         break;
183                 default:
184                         help();
185                         break;
186                 }
187         }
188 }
189
190 int main(int argc, char **argv)
191 {
192         sec_init();
193         load_case_tables();
194
195         if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
196                 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
197                                                 get_dyn_CONFIGFILE());
198         exit(1);
199         }
200
201         if (argc == 1) {
202                 help();
203                 exit(1);
204         }
205
206         process_arguments(argc, argv);
207 }