## OpenCA - CA Command
## (c) 1998-2001 by Massimiliano Pala and OpenCA Group
##
##   File Name: bpImportNewUser
##     Version: $Revision: 1.8 $
##       Brief: import new users of the PKI
## Description: 
##  Parameters:

require "$common_libs/export-import.lib";

sub cmdBpImportNewUser {

print startLogPage (gettext ("Import New User Batch Processor"));

print addLogSection (gettext ("Importing data ..."));
my $tmpdir = createDirectory ();
if (not eximIOImport (DIR => $tmpdir)) {
	print addPreLogSection ("<FONT COLOR=#FF0000>".
				gettext ("Cannot extract archive from import device!").
				"</FONT>");
	print closeLogSection ();
	print closeLogPage ();
	return undef;
}
print addLogLine (gettext ("completed."));
print closeLogSection ();

print addLogSection (gettext ("Running batch processor ..."));

## datadirectory for the batchprocessor
my $batch_dir  = getRequired ("BP_DIR");
my $batch_file = getRequired ("BP_File_ImportNewUser");

## open file
if (not open (FD, $tmpdir."/".$batch_file)) {
	print addPreLogSection ("<FONT COLOR=#FF0000>".
				gettext ("Cannot open file with data for batch processor!").
				"</FONT>");
	print closeLogSection ();
	print closeLogPage ();
	return undef;
}

## file format is as follows:
##
## ID 123456 <required>
## DN cn=michael bell, o=HU, c=de <required>
## ROLE CA Operator <required>
## SUBJECT_ALT_NAME email:michael.bell@web.de <optional>
## EVERY_OTHER_NAME value <optional>
##
## the subject alternative name and the DN must be in an OpenSSL compatible format

## loop
my $line = undef;
while (not eof FD) {

	my %hash = ();

	## ignore blank lines
	do {
		$line = <FD>;
		next if (not $line and eof FD);
	} while ($line =~ /^\s*\r?\n*$/);

	## write lines until next blank line into an hash
	do {
		my $parameter = $line;
		$parameter =~ s/^\s*//;
		$parameter =~ s/\s.*$//;
		$parameter =~ s/\n//;
		my $value = $line;
		$value =~ s/^\s*//;
		$value =~ s/^[^\s]*\s//;
		$value =~ s/\n//;
		$hash{$parameter} = $value;
		$line = <FD>;
	} while ($line !~ /^\s*\r?\n*$/);

	## check parameter
	if (not $hash{"ID"}) {
		print addPreLogLine ("<FONT COLOR=#FF0000>".
				gettext ("Ignoring dataset because there is no ID!").
				"</FONT>");
		next;
	}
	if (not $hash{"DN"}) {
		print addPreLogLine ("<FONT COLOR=#FF0000>".
				gettext ("Ignoring dataset because there is no DN!").
				"</FONT>");
		next;
	}
	if (not $hash{"ROLE"}) {
		print addPreLogLine ("<FONT COLOR=#FF0000>".
				gettext ("Ignoring dataset because there is no ROLE!").
				"</FONT>");
		next;
	}

	## build the directory path for the ID
	my $id_dir = $batch_dir;
	my $h_id   = $hash{"ID"};
	while ($h_id !~ /^$/) {
		$id_dir .= "/".substr $h_id, 0, 1;
		$h_id =~ s/^.//;
	}

	## check that the directory not exist
	if (-d $id_dir) {
		## checking that this directory is only part of a tree
		if (-f $id_dir."/ID") {
			print addPreLogLine ("<FONT COLOR=#FF0000>".
					i18nGettext (
                      "ID __ID__ already exists. Ignoring dataset.",
                      "__ID__", $hash{"ID"}).
					"</FONT>");
			next;
		}
	} else {
		## create the directory
		my $ret = `mkdir -p $id_dir`;
		if ( $? ) {
			print addPreLogLine ("<FONT COLOR=#FF0000>".
					i18nGettext (
                      "Cannot create directory __DIR__. Ignoring dataset of ID __ID__.",
                      "__DIR__", $id_dir,
                      "__ID__", $hash{"ID"}).
					"</FONT>");
			next;
		}
	}

	## create the acl
	if (not -d $id_dir."/acl") {
		if ( not mkdir $id_dir."/acl") {
			print addPreLogLine ("<FONT COLOR=#FF0000>".
					i18nGettext (
                      "Cannot create directory __DIR__. Ignoring dataset of ID __ID__.",
                      "__DIR__", "$id_dir/acl",
                      "__ID__", $hash{"ID"}).
					"</FONT>");
			next;
		}
	}

	## create the data
	if (not -d $id_dir."/data") {
		if ( not mkdir $id_dir."/data") {
			print addPreLogLine ("<FONT COLOR=#FF0000>".
					i18nGettext (
                      "Cannot create directory __DIR__. Ignoring dataset of ID __ID__.",
                      "__DIR__", "$id_dir/data",
                      "__ID__", $hash{"ID"}).
					"</FONT>");
			next;
		}
	}

	## create the keybackup
	if (not -d $id_dir."/keybackup") {
		if ( not mkdir $id_dir."/keybackup") {
			print addPreLogLine ("<FONT COLOR=#FF0000>".
					i18nGettext (
                      "Cannot create directory __DIR__. Ignoring dataset of ID __ID__.",
                      "__DIR__", "$id_dir/keybackup",
                      "__ID__", $hash{"ID"}).
					"</FONT>");
			next;
		}
	}

	## create the private
	if (not -d $id_dir."/private") {
		if ( not mkdir $id_dir."/private") {
			print addPreLogLine ("<FONT COLOR=#FF0000>".
					i18nGettext (
                      "Cannot create directory __DIR__. Ignoring dataset of ID __ID__.",
                      "__DIR__", "$id_dir/private",
                      "__ID__", $hash{"ID"}).
					"</FONT>");
			next;
		}
	}

	## create the public
	if (not -d $id_dir."/public") {
		if ( not mkdir $id_dir."/public") {
			print addPreLogLine ("<FONT COLOR=#FF0000>".
					i18nGettext (
                      "Cannot create directory __DIR__. Ignoring dataset of ID __ID__.",
                      "__DIR__", "$id_dir/public",
                      "__ID__", $hash{"ID"}).
					"</FONT>");
			next;
		}
	}

	## create the user
	if (not $tools->saveFile (FILENAME => $id_dir."/ID", DATA => $hash{ID})) {
		print addPreLogLine ("<FONT COLOR=#FF0000>".
				i18nGettext (
                  "Cannot write file __FILE__. Aborting ...",
                  "__FILE__", "$id_dir/ID").
				"</FONT>");
		next;
	}
	delete $hash{ID};

	## write every key in the appropriate file
	foreach my $h (keys %hash) {
		if (not $tools->saveFile (FILENAME => $id_dir."/data/".$h, DATA => $hash{$h})) {
			print addPreLogLine ("<FONT COLOR=#FF0000>".
				    i18nGettext (
                      "Cannot write file __FILE__. Continuing ...",
                      "__FILE__", "$id_dir/data/$h").
					"</FONT>");
		}
	}

	## write out message
	print addPreLogLine ("<FONT COLOR=#00FF00>".
			i18nGettext ("ID __ID__ successfully imported.", "__ID__", $tools->getFile ($id_dir."/ID")).
			"</FONT>");
}

close FD;

print addPreLogLine ("");
print addLogLine (gettext ("Batch processor finished"));
print closeLogSection ();

removeDirectory ($tmpdir);

print closePage();

}

1;
