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