summaryrefslogtreecommitdiff
path: root/source3/utils
diff options
context:
space:
mode:
Diffstat (limited to 'source3/utils')
-rw-r--r--source3/utils/smbta-util.c211
1 files changed, 211 insertions, 0 deletions
diff --git a/source3/utils/smbta-util.c b/source3/utils/smbta-util.c
new file mode 100644
index 0000000000..13686ae7a0
--- /dev/null
+++ b/source3/utils/smbta-util.c
@@ -0,0 +1,211 @@
+/*
+ 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"
+
+
+static void delete_key();
+
+
+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;
+ if (!secrets_init()) {
+ printf("Error opening secrets database.");
+ exit(1);
+ }
+ char *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(0);
+ }
+ 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 char *load_key_from_file(char *filename)
+{
+ FILE *keyfile;
+ char *key = malloc(sizeof(char) * 17);
+ int l;
+ keyfile = fopen(filename, "r");
+ if (keyfile == NULL) {
+ printf("Error opening the keyfile!\n");
+ exit(0);
+ }
+ l = fscanf(keyfile, "%s", key);
+ if (strlen(key) != 16) {
+ printf("Key file in wrong format\n");
+ fclose(keyfile);
+ exit(0);
+ }
+ return key;
+}
+
+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 char *generate_key()
+{
+ char *key = malloc(sizeof(char)*17);
+ 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");
+ return key;
+}
+
+static void create_new_key_and_activate( char *filename )
+{
+ if (!secrets_init()) {
+ printf("Error opening secrets database.");
+ exit(1);
+ }
+
+ char *key = generate_key();
+ delete_key();
+ secrets_store("smb_traffic_analyzer_key", key, strlen(key)+1 );
+ printf("Key installed, encryption activated.\n");
+ create_file_from_key(filename);
+ free(key);
+}
+
+static void delete_key()
+{
+ 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;
+ char *akey;
+ size_t size;
+ key = load_key_from_file(filename);
+ 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);
+ free(key);
+}
+
+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);
+}