/usr/lib/purple-2/XEP-0027.pl is in pidgin-openpgp 0.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | # XEP-0027 Plugin for Pidgin (GTK).
#
# This plugin implements encryption and decryption of
# jabber messages according to XEP-0027.
# It does not sign nor verify <presence> or <status> messages,
# as these only indicate that XEP-0027 is present at the remote party.
#
# Configuration:
# * configure gpg-agent manually
# * make sure gpg and gpg-agent are in %PATH%
# * JID => GPG key mapping is done search for jid in gpg,
# but may be overridden using config dialog.
#
# I don't take any liabilitity.
#
# 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/>.
#
# (C) 2008, Michael Braun <michael-dev@fami-braun.de>
use strict;
use File::Temp qw /tempfile tmpnam/;
use Purple;
use File::Touch;
use Config::Tiny;
#use Pidgin;
use constant TRUE => 1 ;
use constant FALSE => 0 ;
use constant CONFIGFILENAME => "pidgin-openpgp.ini";
# *********** PLUGIN META DATA *************
our %PLUGIN_INFO = (
perl_api_version => 2,
name => "OpenPGP Plugin",
version => "0.1",
summary => "Send and receive gpg encrypted messages.",
description => "XEP-0027",
author => "Michael Braun <michael-dev\@fami-braun.de>",
url => "http://www.fami-braun.de",
load => "plugin_load",
unload => "plugin_unload",
prefs_info => "prefs_info_cb",
);
my %CONNSTATE = ();
my %GPGMAP = ();
my $gpg = qx(gpg-agent --daemon)."gpg";
sub plugin_init {
return %PLUGIN_INFO;
}
# *********** DECODE received messages **********
# TODO: fork into background, timeout, check return code of gpg, asking for key
# send error message on decryption failure
sub decrypt {
my $ciphertext = shift;
my ($input, $fni) = tempfile();
chmod 0600, $fni;
print $input "-----BEGIN PGP MESSAGE-----\n\n$ciphertext\n-----END PGP MESSAGE-----\n";
close $input;
my $fno = tmpnam();
my $ret = system("$gpg --batch --output $fno --use-agent -q -d $fni");
if ($ret != 0) {
return "*decryption failed*";
}
my $output;
open $output, "<", $fno;
my @plain = <$output>;
close $output;
unlink $fni;
unlink $fno;
return join("",@plain);
}
#******* verify received messages *******
# TODO: implement
# *********** INCOMING handler *************
# TODO: indicate encryption state of message, replace body content,
# OPTIONAL: detect signed presence and status tags and inform the user about
# the remote capabilites
sub conv_receiving_jabber
{
my ($conn, $node, $data) = @_;
my $encrypted_node = $node->get_child_with_namespace("x","jabber:x:encrypted");
my $body = $node->get_child("body");
if (not defined($encrypted_node)) {
Purple::Debug::misc(" * ", "no opengpg message");
if (defined($body)) {
my $newmsg ="?NOGPG?";
$node->get_child("body")->insert_data($newmsg,length($newmsg));
}
} else {
Purple::Debug::misc("opengpg received", $conn->get_display_name().", ".$node->get_attrib("id").", $data\n");
my $crypted = $encrypted_node->get_data();
my $plaintext = decrypt($crypted);
#does not work: results in no message to be shown
#$node->get_child("body")->free();
#$node->new_child("body");
my $newmsg = "?PGP?$plaintext";
$node->get_child("body")->insert_data($newmsg,length($newmsg));
}
@_[2] = $node;
Purple::Debug::misc("opengpg received", $node->to_str(0)."\n");
#return $node;
}
sub conv_receiving_msg
{
my ($account, $from, $message, $conv, $flags, $data) = @_;
my $xm = @_[2];
Purple::Debug::misc("received", "$message\n");
$message =~s/<body>(.*)<\/body>/$1/;
if ($message =~/\?NOGPG\?$/) {
$message =~s/(.*)\?NOGPG\?$/<i><span title="unencrypted">[U]<\/span><\/i> $1/;
} else {
$message =~s/.*\?PGP\?/<i><span title="encrypted">[E]<\/span><\/i> /;
}
$message = "<body>".$message."</body>";
@_[2] = $message;
Purple::Debug::misc("openpgpplugin", "replaced: $message\n");
}
# *********** ENCRYPT outgoing messages ************
# TODO: fork into background, display error message
sub encrypt {
my $plaintext = shift;
my $target = shift;
if (exists($GPGMAP{$target})) {
$target = $GPGMAP{$target};
}
my ($input, $fni) = tempfile();
chmod 0600, $fni;
print $input $plaintext;
close $input;
my $fno = tmpnam();
my $ret = system("$gpg --batch --output $fno --use-agent -q --armor -r \"$target\" -e $fni");
my $output;
open $output, "<", $fno;
my @plain = <$output>;
chomp(@plain);
close $output;
unlink $fni;
unlink $fno;
if ($ret > 0) {
Purple::Debug::misc("openpgp","encryption failed\n");
return "";
}
# find first empty line
for (; not ($plain[0] eq "");) {shift(@plain);};
shift(@plain);
pop(@plain);
Purple::Debug::misc("openpgp","encryption successfull\n");
return join("\n",@plain);
}
# *********** SIGN outgoing messages *********
# TODO: implement
#
# ********** OUTGOING handler ************
# encrypt outgoing message nodes and sign outgoing status and presence nodes
# TODO: implement, configure key to use
sub info_enable_gpg {
my $target = shift;
require Gtk2;
my $frame = Gtk2::Window->new();
my $dialog = Gtk2::MessageDialog->new ($frame,
'destroy-with-parent',
'info', # message type
'ok', # which set of buttons?
"Encryption for $target enabled.");
$dialog->run;
$dialog->destroy;
$frame->destroy;
}
sub info_disable_gpg {
my $target = shift;
require Gtk2;
my $frame = Gtk2::Window->new();
my $dialog = Gtk2::MessageDialog->new ($frame,
'destroy-with-parent',
'info', # message type
'ok', # which set of buttons?
"Encryption for $target disabled.");
$dialog->run;
$dialog->destroy;
$frame->destroy;
}
sub info_err_encrypt {
my $target = shift;
require Gtk2;
my $frame = Gtk2::Window->new();
my $dialog = Gtk2::MessageDialog->new ($frame,
'destroy-with-parent',
'error', # message type
'cancel', # which set of buttons?
"Could not encrypt message for $target.\nPlease check gpg settings and verify gpg-agent is running.");
$dialog->run;
$dialog->destroy;
$frame->destroy;
}
sub conv_sending_msg
{
my ($conn, $node, $data) = @_;
# get text node
my $bnode = $node->get_child("body");
if (not defined($bnode)) { return; }
# fetch target / connid
my $target = $node->get_attrib("to");
$target =~s/\/.*//; # name@host/path => remove path
my $connid = $target;
if (not exists($CONNSTATE{$connid})) {$CONNSTATE{$connid} = 1; } # default off
Purple::Debug::misc("openpgp","sending to $target\n");
# fetch message
my $msg = $bnode->get_data();
# parse commands, decide on encryption
my $do_encrypt = $CONNSTATE{$connid};
Purple::Debug::misc("openpgp","sending message = $msg\n");
if ($msg =~/^ENABLEPGP/) {
Purple::Debug::misc("openpgp","enable pgp\n");
$CONNSTATE{$connid} = 0;
$msg = "The remote party <b>enabled</b> XEP-0027 (OpenPGP) encryption.";
$do_encrypt = 0;
info_enable_gpg($target);
} elsif ($msg =~/^DISABLEPGP/) {
Purple::Debug::misc("openpgp","disable pgp\n");
$CONNSTATE{$connid} = 1;
$msg = "The remote party <b>disabled</b> XEP-0027 (OpenPGP) encryption.";
info_disable_gpg($target);
}
if ($do_encrypt == 1) { return; }
# drop html node
my $htmlbnode = $node->get_child("html");
if (defined($htmlbnode)) { $htmlbnode->free(); }
# encrypt data
my $crypted = encrypt($msg, $target);
if ($crypted eq "") {
Purple::Debug::misc("openpgp","sending error message\n");
info_err_encrypt($target);
#$conn->get_im_data()->write("OpenPGP", "<b>Cannot encrypt last message.</b>", 0, 0);
#$node->free(); -> crashes.
# remove plain data
$msg = "Failed to encrypt message.";
$bnode->free();
$node->new_child("body")->insert_data($msg, length($msg));
@_[1] = $node;
return;
}
# insert encrypted data
my $x = $node->new_child("x");
$x->set_attrib("xmlns","jabber:x:encrypted");
$x->insert_data($crypted, length($crypted));
# remove plain data
$msg = "[This message is *encrypted* (See :XEP:`27`] ([This message is *encrypted* (See :XEP:`27`])";
$bnode->free();
$node->new_child("body")->insert_data($msg, length($msg));
# ensure new node is used!
@_[1] = $node;
Purple::Debug::misc("openpgp sending new", $node->to_str(0)."\n");
}
#****** modified conversation ******
# here to come: integrate into conversation window
#sub conv_switched {
# Purple::Debug::misc("openpgpplugin", "conv switched\n");
#
#}
#
#sub conv_deleted {
# Purple::Debug::misc("openpgpplugin", "conv deleted:".join(",",@_)."\n");
#
#}
#
#sub conv_created {
# Purple::Debug::misc("openpgpplugin", "conv created:".join(",",@_)."\n");
# my $conv = shift; # PurpleConversation
# init_dialog($conv);
#}
sub init_dialog {
# require Gtk2;
# require Pidgin::IMHtmlToolbar;
# Purple::Debug::misc("openpgpplugin", "conv init\n");
#
# my $conv = shift;
# my $button = Gtk2::Button->new();
# $button->set_relief("GTK_RELIEF_NONE");
# bbox = gtkconv->toolbar;
#
# gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
#
# bwbox = gtk_hbox_new(FALSE, 0);
# gtk_container_add(GTK_CONTAINER(button), bwbox);
# icon = otr_icon(NULL, TRUST_NOT_PRIVATE, 1);
# gtk_box_pack_start(GTK_BOX(bwbox), icon, TRUE, FALSE, 0);
# label = gtk_label_new(NULL);
# gtk_box_pack_start(GTK_BOX(bwbox), label, FALSE, FALSE, 0);
#
# if (prefs.show_otr_button) {
# gtk_widget_show_all(button);
# }
}
# ************ CONFIG handler **********
# configure keys to use per contact and per account / global
# TODO: implement, where to store this information
my $LOCKED = 0;
my %JIDINLINE = ();
my %ITEMS=();
sub info_err_savecfg {
my $target = shift;
require Gtk2;
my $frame = Gtk2::Window->new();
my $dialog = Gtk2::MessageDialog->new ($frame,
'destroy-with-parent',
'error', # message type
'cancel', # which set of buttons?
"Could not save config in $target.");
$dialog->run;
$dialog->destroy;
$frame->destroy;
}
sub SaveCfg {
my $cfgfile = Purple::Prefs::get_string("/plugins/core/openpgp/configfile");
Purple::Debug::misc("openpgpplugin", "save:" .join(",",keys(%GPGMAP))." => ".join(",",values(%GPGMAP))." into $cfgfile\n");
if (not -e $cfgfile) {
touch($cfgfile);
}
if (not -w $cfgfile) {
info_err_savecfg($cfgfile);
return;
}
my $conf = Config::Tiny->new();
foreach my $key (keys(%GPGMAP)) {
$conf->{default}->{$key} = $GPGMAP{$key};
}
$conf->write($cfgfile);
}
# file content:
# JID=key
sub LoadCfg {
my $cfgfile = Purple::Prefs::get_string("/plugins/core/openpgp/configfile");
if (not -r $cfgfile) {
%GPGMAP = ();
return;
}
my $conf = Config::Tiny->read($cfgfile);
use Data::Dumper;
Purple::Debug::misc("openpgpplugin", Dumper($conf->{default})."\n");
%GPGMAP = ();
foreach my $key (keys(%{$conf->{default}})) {
$GPGMAP{$key} = $conf->{default}->{$key};
}
Purple::Debug::misc("openpgpplugin", "load:" .join(",",keys(%GPGMAP))." => ".join(",",values(%GPGMAP))."\n");
}
sub delete_event {
Purple::Debug::misc("openpgpplugin", "closing config window\n");
# closing config window
$LOCKED = 0;
}
sub on_ok {
Purple::Debug::misc("openpgpplugin", "ok pressed\n");
my $self = shift;
my $frame = shift;
$frame->destroy;
}
sub on_add {
Purple::Debug::misc("openpgpplugin", "add pressed\n");
my $self = shift;
my $data = shift;
my $ppref1 = $data->[0];
my $ppref2 = $data->[1];
my $xtable = $data->[2];
my $jid = $ppref1->get_text();
my $key = $ppref2->get_text();
if (exists($GPGMAP{$jid})) {
$GPGMAP{$jid} = $key;
Purple::Debug::misc("openpgpplugin", "replacing $jid => $key\n");
my $i = $JIDINLINE{$jid};
$ITEMS{$i}->[1]->set_text($key);
&SaveCfg;
} else {
my $i = keys(%GPGMAP) +2;
Purple::Debug::misc("openpgpplugin", "adding $jid => $key with i=$i\n");
$GPGMAP{$jid} = $key;
$xtable->resize($i+1, 3);
add_to_table($xtable, $jid, $i);
&SaveCfg;
}
$ppref1->set_text("");
$ppref2->set_text("");
}
sub on_del {
Purple::Debug::misc("openpgpplugin", "del pressed\n");
my $self = shift;
my $data = shift;
my $xtable = $data->[0];
my $i = $data->[1];
my $jid = $ITEMS{$i}->[3];
# remove from GPGMAP
Purple::Debug::misc("openpgpplugin", "deleting entry $i ($jid => $GPGMAP{$jid})\n");
delete($GPGMAP{$jid});
# move all consecutive items up
for (my $j = $i; $j < keys(%ITEMS)+1; $j++) {
my $jid = $ITEMS{$j+1}->[3];
$ITEMS{$j}->[3] = $jid; # move jid down
$ITEMS{$j}->[0]->set_text($ITEMS{$j+1}->[0]->get_text()); # move jid label down
$ITEMS{$j}->[1]->set_text($ITEMS{$j+1}->[1]->get_text()); # move key label down
$JIDINLINE{$jid} = $j;
}
# remove last line
my $j = keys(%ITEMS)+1;
$ITEMS{$j}->[0]->destroy;
$ITEMS{$j}->[1]->destroy;
$ITEMS{$j}->[2]->destroy;
delete($ITEMS{$j});
$xtable->resize(keys(%ITEMS)+2,3);
&SaveCfg;
}
sub add_to_table {
my ($xtable, $jid, $i) = @_;
Purple::Debug::misc("openpgpplugin", "show $jid\n");
my $ppref1 = Gtk2::Label->new("$jid");
$xtable->attach_defaults($ppref1, 0, 1, $i, $i+1);
$ppref1->set_selectable(TRUE);
$ppref1->show;
my $value = $GPGMAP{$jid};
my $ppref2 = Gtk2::Label->new("$value");
$xtable->attach_defaults($ppref2, 1, 2, $i, $i+1);
$ppref2->set_selectable(TRUE);
$ppref2->show;
my $button = Gtk2::Button->new("Del");
$button->signal_connect(clicked => \&on_del, [$xtable, $i]);
$xtable->attach_defaults($button, 2, 3, $i, $i+1);
$button->show;
$JIDINLINE{$jid} = $i;
$ITEMS{$i} = [$ppref1, $ppref2, $button, $jid];
}
sub prefs_info_cb {
Purple::Debug::misc("openpgpplugin", "cb\n");
if ($LOCKED > 0) { return; }
$LOCKED = 1;
# *** JID => GPG-KeyID ***
require Gtk2;
my $frame = Gtk2::Window->new("toplevel");
$frame->set_title("OpenPGP Plugin Konfiguration");
$frame->signal_connect(delete_event => \&delete_event);
my $box1 = Gtk2::VBox->new(FALSE, 0);
$frame->add($box1);
$box1->show;
my $ppref = Gtk2::Label->new("Start gpg-agent first.");
$box1->pack_start($ppref, TRUE, TRUE, 0);
$ppref->show;
my $ppref = Gtk2::Label->new("Use ENABLEPGP in conversation to enable encryption.");
$box1->pack_start($ppref, TRUE, TRUE, 0);
$ppref->show;
my $ppref = Gtk2::Label->new("Use DISABLEPGP in conversation to disable encryption.");
$box1->pack_start($ppref, TRUE, TRUE, 0);
$ppref->show;
my $ppref = Gtk2::Label->new("This plugin will DEFAULT to the jabber-id to lookup the remote gpg key.\nThe gpg binary is searched in the common (OS-dependent) path.");
$box1->pack_start($ppref, TRUE, TRUE, 0);
$ppref->show;
my $separator = Gtk2::HSeparator->new;
$box1->pack_start($separator, TRUE, TRUE, 0);
$separator->show;
# *** maps ***
my $xtable = Gtk2::Table->new(keys(%GPGMAP)+1,3,TRUE);
$box1->pack_start($xtable, TRUE, TRUE, 0);
$xtable->show;
my $i = 0;
my $ppref1 = Gtk2::Label->new("JID");
$xtable->attach_defaults($ppref1, 0, 1, $i, $i+1);
$ppref1->set_selectable(FALSE);
$ppref1->show;
my $ppref2 = Gtk2::Label->new("Key-ID");
$xtable->attach_defaults($ppref2, 1, 2, $i, $i+1);
$ppref2->set_selectable(FALSE);
$ppref2->show;
$i = $i + 1;
my $ppref1 = Gtk2::Entry->new;
$xtable->attach_defaults($ppref1, 0, 1, $i, $i+1);
$ppref1->show;
my $ppref2 = Gtk2::Entry->new;
$xtable->attach_defaults($ppref2, 1, 2, $i, $i+1);
$ppref2->show;
my $button = Gtk2::Button->new("Add");
$button->signal_connect(clicked => \&on_add, [$ppref1, $ppref2, $xtable]);
$xtable->attach_defaults($button, 2, 3, $i, $i+1);
$button->show;
$i = $i + 1;
foreach my $jid (keys(%GPGMAP)) {
add_to_table($xtable, $jid, $i);
$i=$i+1;
}
my $separator = Gtk2::HSeparator->new;
$box1->pack_start($separator, TRUE, TRUE, 0);
$separator->show;
# *** buttons ****
my $button = Gtk2::Button->new("Ok");
$button->signal_connect(clicked => \&on_ok, $frame);
$box1->pack_start($button, TRUE, TRUE, 0);
$button->show;
$frame->show;
return undef;
}
# ****** ONLOAD *******
sub plugin_load {
my $plugin = shift;
Purple::Debug::misc("openpgpplugin", "plugin_load() - OpenPGP Plugin Loaded.\n");
Purple::Prefs::add_none("/plugins/core/openpgp");
Purple::Prefs::add_string("/plugins/core/openpgp/configfile", Purple::Util::user_dir()."/".CONFIGFILENAME);
# A pointer to the handle to which the signal belongs needed by the callback function
my $accounts_handle = Purple::Accounts::get_handle();
my $jabber = Purple::Find::prpl("prpl-jabber");
Purple::Signal::connect($jabber, "jabber-receiving-xmlnode", $plugin, \&conv_receiving_jabber, "receiving jabber node");
Purple::Signal::connect($jabber, "jabber-sending-xmlnode", $plugin, \&conv_sending_msg, "sending jabber node");
my $conv = Purple::Conversations::get_handle();
Purple::Signal::connect($conv, "receiving-im-msg", $plugin, \&conv_receiving_msg, "receiving im message");
# Purple::Signal::connect($conv, "conversation-switched", $plugin, \&conv_switched, "conversation switched");
# Purple::Signal::connect($conv, "deleting-conversation", $plugin, \&conv_deleted, "conversation deleted");
# Purple::Signal::connect($conv, "conversation-created", $plugin, \&conv_created, "conversation created");
&LoadCfg();
}
# ****** ON UNLOAD *******
sub plugin_unload {
my $plugin = shift;
Purple::Debug::misc("openpgpplugin", "plugin_unload() - OpenPGP Plugin Unloaded.\n");
}
|