1 ## @file
2 # @details A GOsa-SI event module containing all functions used by GOsa dak
3 # @brief Implementation of a GOsa-SI-client event module.
5 package dak;
6 use Exporter;
7 @ISA = qw(Exporter);
8 my @events = (
9 "get_events",
10 "get_dak_keyring",
11 "import_dak_key",
12 "remove_dak_key",
13 );
14 @EXPORT = @events;
17 use strict;
18 use warnings;
19 use GOSA::GosaSupportDaemon;
20 use Data::Dumper;
21 use MIME::Base64;
23 BEGIN {}
25 END {}
27 our ($dak_base_directory, $dak_signing_keys_directory, $dak_queue_directory, $dak_user);
29 my %cfg_defaults = (
30 "client" =>
31 {"dak-base" => [\$dak_base_directory, "/srv/archive"],
32 "dak-keyring" => [\$dak_signing_keys_directory, "/srv/archive/keyrings"],
33 "dak-queue" => [\$dak_queue_directory, "/srv/archive/queue"],
34 "dak-user" => [\$dak_user, "deb-dak"],
35 },
36 );
37 &GOSA::GosaSupportDaemon::read_configfile($main::config_file, %cfg_defaults);
40 ## @method get_events()
41 # A brief function returning a list of functions which are exported by importing the module.
42 # @return List of all provided functions
43 sub get_events { return \@events; }
46 sub get_dak_keyring {
47 my ($msg, $msg_hash) = @_;
48 my $source = @{$msg_hash->{'source'}}[0];
49 my $target = @{$msg_hash->{'target'}}[0];
50 my $header= @{$msg_hash->{'header'}}[0];
51 my $session_id = @{$msg_hash->{'session_id'}}[0];
53 # build return message with twisted target and source
54 my $out_hash = &main::create_xml_hash("answer_$header", $target, $source);
55 &add_content2xml_hash($out_hash, "session_id", $session_id);
57 my @keys;
58 my %data;
60 my $keyring = $main::dak_signing_keys_directory."/keyring.gpg";
62 my $gpg_cmd = `which gpg`; chomp $gpg_cmd;
63 my $gpg = "$gpg_cmd --no-default-keyring --no-random-seed --keyring $keyring";
65 # Check if the keyrings are in place and readable
66 if(
67 &run_as($main::dak_user, "test -r $keyring")->{'resultCode'} != 0
68 ) {
69 &add_content2xml_hash($out_hash, "error", "DAK Keyring is not readable");
70 } else {
71 my $command = "$gpg --list-keys";
72 my $output = &run_as($main::dak_user, $command);
73 &main::daemon_log("$session_id DEBUG: ".$output->{'command'}, 7);
75 my $i=0;
76 foreach (@{$output->{'output'}}) {
77 if ($_ =~ m/^pub\s.*$/) {
78 ($keys[$i]->{'pub'}->{'length'}, $keys[$i]->{'pub'}->{'uid'}, $keys[$i]->{'pub'}->{'created'}) = ($1, $2, $3)
79 if $_ =~ m/^pub\s*?(\w*?)\/(\w*?)\s(\d{4}-\d{2}-\d{2})/;
80 $keys[$i]->{'pub'}->{'expires'} = $1 if $_ =~ m/^pub\s*?\w*?\/\w*?\s\d{4}-\d{2}-\d{2}\s\[expires:\s(\d{4}-\d{2}-\d{2})\]/;
81 $keys[$i]->{'pub'}->{'expired'} = $1 if $_ =~ m/^pub\s*?\w*?\/\w*?\s\d{4}-\d{2}-\d{2}\s\[expired:\s(\d{4}-\d{2}-\d{2})\]/;
82 } elsif ($_ =~ m/^sub\s.*$/) {
83 ($keys[$i]->{'sub'}->{'length'}, $keys[$i]->{'sub'}->{'uid'}, $keys[$i]->{'sub'}->{'created'}) = ($1, $2, $3)
84 if $_ =~ m/^sub\s*?(\w*?)\/(\w*?)\s(\d{4}-\d{2}-\d{2})/;
85 $keys[$i]->{'sub'}->{'expires'} = $1 if $_ =~ m/^pub\s*?\w*?\/\w*?\s\d{4}-\d{2}-\d{2}\s\[expires:\s(\d{4}-\d{2}-\d{2})\]/;
86 $keys[$i]->{'sub'}->{'expired'} = $1 if $_ =~ m/^pub\s*?\w*?\/\w*?\s\d{4}-\d{2}-\d{2}\s\[expired:\s(\d{4}-\d{2}-\d{2})\]/;
87 } elsif ($_ =~ m/^uid\s.*$/) {
88 push @{$keys[$i]->{'uid'}}, $1 if $_ =~ m/^uid\s*?([^\s].*?)$/;
89 } elsif ($_ =~ m/^$/) {
90 $i++;
91 }
92 }
93 }
95 my $i=0;
96 foreach my $key (@keys) {
97 # &main::daemon_log(Dumper($key));
98 &add_content2xml_hash($out_hash, "answer".$i++, $key);
99 }
100 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
101 if (defined $forward_to_gosa) {
102 &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
103 }
104 return &create_xml_string($out_hash);
105 }
108 sub import_dak_key {
109 my ($msg, $msg_hash) = @_;
110 my $source = @{$msg_hash->{'source'}}[0];
111 my $target = @{$msg_hash->{'target'}}[0];
112 my $header= @{$msg_hash->{'header'}}[0];
113 my $session_id = @{$msg_hash->{'session_id'}}[0];
114 my $key = &decode_base64(@{$msg_hash->{'key'}}[0]);
116 # build return message with twisted target and source
117 my $out_hash = &main::create_xml_hash("answer_$header", $target, $source);
118 &add_content2xml_hash($out_hash, "session_id", $session_id);
120 my %data;
122 my $keyring = $main::dak_signing_keys_directory."/keyring.gpg";
124 my $gpg_cmd = `which gpg`; chomp $gpg_cmd;
125 my $gpg = "$gpg_cmd --no-default-keyring --no-random-seed --keyring $keyring";
127 # Check if the keyrings are in place and writable
128 if(
129 &run_as($main::dak_user, "test -w $keyring")->{'resultCode'} != 0
130 ) {
131 &add_content2xml_hash($out_hash, "error", "DAK Keyring is not writable");
132 } else {
133 my $keyfile;
134 open($keyfile, ">/tmp/gosa_si_tmp_dak_key");
135 print $keyfile $key;
136 close($keyfile);
137 my $command = "$gpg --import /tmp/gosa_si_tmp_dak_key";
138 my $output = &run_as($main::dak_user, $command);
139 &main::daemon_log("$session_id DEBUG: ".$output->{'command'}, 7);
140 unlink("/tmp/gosa_si_tmp_dak_key");
142 if($output->{'resultCode'} != 0) {
143 &add_content2xml_hash($out_hash, "error", "Import of DAK key failed! Output was '".$output->{'output'}."'");
144 } else {
145 &add_content2xml_hash($out_hash, "answer", "Import of DAK key successfull! Output was '".$output->{'output'}."'");
146 }
147 }
149 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
150 if (defined $forward_to_gosa) {
151 &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
152 }
153 return &create_xml_string($out_hash);
154 }
157 sub remove_dak_key {
158 my ($msg, $msg_hash) = @_;
159 my $source = @{$msg_hash->{'source'}}[0];
160 my $target = @{$msg_hash->{'target'}}[0];
161 my $header= @{$msg_hash->{'header'}}[0];
162 my $session_id = @{$msg_hash->{'session_id'}}[0];
163 my $key = @{$msg_hash->{'keyid'}}[0];
164 # build return message with twisted target and source
165 my $out_hash = &main::create_xml_hash("answer_$header", $target, $source);
166 &add_content2xml_hash($out_hash, "session_id", $session_id);
168 my %data;
170 my $keyring = $main::dak_signing_keys_directory."/keyring.gpg";
172 my $gpg_cmd = `which gpg`; chomp $gpg_cmd;
173 my $gpg = "$gpg_cmd --no-default-keyring --no-random-seed --homedir ".$main::dak_signing_keys_directory." --keyring $keyring";
175 # Check if the keyrings are in place and writable
176 if(
177 &run_as($main::dak_user, "test -w $keyring")->{'resultCode'} != 0
178 ) {
179 &add_content2xml_hash($out_hash, "error", "DAK keyring is not writable");
180 } else {
181 # Check if the key is present in the keyring
182 if(&run_as($main::dak_user, "$gpg --list-keys $key")->{'resultCode'} == 0) {
183 my $command = "$gpg --batch --yes --delete-key $key";
184 my $output = &run_as($main::dak_user, $command);
185 &main::daemon_log("$session_id DEBUG: ".$output->{'command'}, 7);
186 } else {
187 &add_content2xml_hash($out_hash, "error", "DAK key with id '$key' was not found in keyring");
188 }
189 }
191 my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
192 if (defined $forward_to_gosa) {
193 &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
194 }
195 return &create_xml_string($out_hash);
196 }
199 #sub get_dak_queue {
200 # my ($msg, $msg_hash, $session_id) = @_;
201 # my %data;
202 # my $source = @{$msg_hash->{'source'}}[0];
203 # my $target = @{$msg_hash->{'target'}}[0];
204 # my $header= @{$msg_hash->{'header'}}[0];
205 #
206 # my %data;
207 #
208 # foreach my $dir ("unchecked", "new", "accepted") {
209 # foreach my $file(<"$main::dak_queue_directory/$dir/*.changes">) {
210 # }
211 # }
212 #
213 # my $out_msg = &build_msg("get_dak_queue", $target, $source, \%data);
214 # my @out_msg_l = ($out_msg);
215 # return @out_msg_l;
216 #}
218 1;