/********************************************************************\ * BitlBee -- An IRC to other IM-networks gateway * * * * Copyright 2002-2004 Wilmer van der Gaast and others * \********************************************************************/ /* SSL module - SSPI backend */ /* Copyright (C) 2005 Jelmer Vernooij <jelmer@samba.org> */ /* 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 2 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 with the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "ssl_client.h" #include <windows.h> #define SECURITY_WIN32 #include <security.h> #include <sspi.h> #include <schannel.h> #include "sock.h" static gboolean initialized = FALSE; int ssl_errno; struct scd { int fd; ssl_input_function func; gpointer data; pre { line-height: 125%; } td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */#!/usr/bin/env ruby # -*- coding: utf-8 -*- # Handle email responses sent to us. # # This script is invoked as a pipe command, i.e. with the raw email message on stdin. # - If a message is identified as a permanent bounce, the user is marked as having a # bounced address, and will not be sent any more messages. # - If a message is identified as an out-of-office autoreply, it is discarded. # - Any other messages are forwarded to config.get("FORWARD_NONBOUNCE_RESPONSES_TO") # We want to avoid loading rails unless we need it, so we start by just loading the # config file ourselves. $alaveteli_dir = File.expand_path(File.join(File.dirname(__FILE__), '..')) $:.push(File.join($alaveteli_dir, "commonlib", "rblib")) load 'config.rb' $:.push(File.join($alaveteli_dir, "lib")) $:.push(File.join($alaveteli_dir, "lib", "mail_handler")) load 'configuration.rb' MySociety::Config.set_file(File.join($alaveteli_dir, 'config', 'general'), true) MySociety::Config.load_default require 'active_support/all' require 'mail_handler' if RUBY_VERSION.to_f >= 1.9 # the default encoding for IO is utf-8, and we use utf-8 internally Encoding.default_external = Encoding.default_internal = Encoding::UTF_8 end def main(in_test_mode) Dir.chdir($alaveteli_dir) do raw_message = $stdin.read begin message = MailHandler.mail_from_raw_email(raw_message) rescue # Error parsing message. Just pass it on, to be on the safe side. forward_on(raw_message) unless in_test_mode return 0 end pfas = permanently_failed_addresses(message) if !pfas.empty? if in_test_mode puts pfas else pfas.each do |pfa| record_bounce(pfa, raw_message) end end return 1 end content_type = MailHandler.get_content_type(message) # If we are still here, there are no permanent failures, # so if the message is a multipart/report then it must be # reporting a temporary failure. In this case we discard it if content_type == "multipart/report" return 1 end # Another style of temporary failure message subject = MailHandler.get_header_string("Subject", message) if content_type == "multipart/mixed" && subject == "Delivery Status Notification (Delay)" return 1 end # Discard out-of-office messages if is_oof?(message) return 2 # Use a different return code, to distinguish OOFs from bounces end # Otherwise forward the message on forward_on(raw_message) unless in_test_mode return 0 end end def permanently_failed_addresses(message) if MailHandler.empty_return_path?(message) # Some sort of auto-response # Check for Exim’s X-Failed-Recipients header failed_recipients = MailHandler.get_header_string("X-Failed-Recipients", message) if !failed_recipients.nil? # The X-Failed-Recipients header contains the email address that failed # Check for the words "This is a permanent error." in the body, to indicate # a permanent failure if MailHandler.get_part_body(message) =~ /This is a permanent error./ return failed_recipients.split(/,\s*/) end end # Next, look for multipart/report if MailHandler.get_content_type(message) == "multipart/report" permanently_failed_recipients = [] message.parts.each do |part| if MailHandler.get_content_type(part) == "message/delivery-status" sections = MailHandler.get_part_body(part).split(/\r?\n\r?\n/) # The first section is a generic header; subsequent sections # represent a particular recipient. Since we sections[1..-1].each do |section| if section !~ /^Status: (\d)/ || $1 != '5' # Either we couldn’t find the Status field, or it was a transient failure break end if section =~ /^Final-Recipient: rfc822;(.+)/ permanently_failed_recipients.push($1) end end end end if !permanently_failed_recipients.empty? return permanently_failed_recipients end end end subject = MailHandler.get_header_string("Subject", message) # Then look for the style we’ve seen in WebShield bounces # (These do not have a return path of <> in the cases I have seen.) if subject == "Returned Mail: Error During Delivery" if MailHandler.get_part_body(message) =~ /^\s*---- Failed Recipients ----\s*((?:<[^>]+>\n)+)/ return $1.scan(/<([^>]+)>/).flatten end end return [] end def is_oof?(message) # Check for out-of-office if MailHandler.get_header_string("X-POST-MessageClass", message) == "9; Autoresponder" return true end subject = MailHandler.get_header_string("Subject", message).downcase if MailHandler.empty_return_path?(message) if subject.start_with? "out of office: " return true end if subject.start_with? "automatic reply: " return true end end if MailHandler.get_header_string("Auto-Submitted", message) == "auto-generated" if subject =~ /out of( the)? office/ return true end end if subject.start_with? "out of office autoreply:" return true end if subject == "out of office" return true end if subject == "out of office reply" return true end if subject.end_with? "is out of the office" return true end return false end def forward_on(raw_message) IO.popen("/usr/sbin/sendmail -i #{AlaveteliConfiguration::forward_nonbounce_responses_to}", "wb") do |f| f.write(raw_message); f.close; end end def load_rails require File.join($alaveteli_dir, 'config', 'boot') require File.join($alaveteli_dir, 'config', 'environment') end def record_bounce(email_address, bounce_message) load_rails User.record_bounce_for_email(email_address, bounce_message) end in_test_mode = (ARGV[0] == "--test") status = main(in_test_mode) exit(status) if in_test_mode
#!/usr/bin/env ruby # -*- coding: utf-8 -*- # Handle email responses sent to us. # # This script is invoked as a pipe command, i.e. with the raw email message on stdin. # - If a message is identified as a permanent bounce, the user is marked as having a # bounced address, and will not be sent any more messages. # - If a message is identified as an out-of-office autoreply, it is discarded. # - Any other messages are forwarded to config.get("FORWARD_NONBOUNCE_RESPONSES_TO") # We want to avoid loading rails unless we need it, so we start by just loading the # config file ourselves. $alaveteli_dir = File.expand_path(File.join(File.dirname(__FILE__), '..')) $:.push(File.join($alaveteli_dir, "commonlib", "rblib")) load 'config.rb' $:.push(File.join($alaveteli_dir, "lib")) $:.push(File.join($alaveteli_dir, "lib", "mail_handler")) load 'configuration.rb' MySociety::Config.set_file(File.join($alaveteli_dir, 'config', 'general'), true) MySociety::Config.load_default require 'active_support/all' require 'mail_handler' if RUBY_VERSION.to_f >= 1.9 # the default encoding for IO is utf-8, and we use utf-8 internally Encoding.default_external = Encoding.default_internal = Encoding::UTF_8 end def main(in_test_mode) Dir.chdir($alaveteli_dir) do raw_message = $stdin.read begin message = MailHandler.mail_from_raw_email(raw_message) rescue # Error parsing message. Just pass it on, to be on the safe side. forward_on(raw_message) unless in_test_mode return 0 end pfas = permanently_failed_addresses(message) if !pfas.empty? if in_test_mode puts pfas else pfas.each do |pfa| record_bounce(pfa, raw_message) end end return 1 end content_type = MailHandler.get_content_type(message) # If we are still here, there are no permanent failures, # so if the message is a multipart/report then it must be # reporting a temporary failure. In this case we discard it if content_type == "multipart/report" return 1 end # Another style of temporary failure message subject = MailHandler.get_header_string("Subject", message) if content_type == "multipart/mixed" && subject == "Delivery Status Notification (Delay)" return 1 end # Discard out-of-office messages if is_oof?(message) return 2 # Use a different return code, to distinguish OOFs from bounces end # Otherwise forward the message on forward_on(raw_message) unless in_test_mode return 0 end end def permanently_failed_addresses(message) if MailHandler.empty_return_path?(message) # Some sort of auto-response # Check for Exim’s X-Failed-Recipients header failed_recipients = MailHandler.get_header_string("X-Failed-Recipients", message) if !failed_recipients.nil? # The X-Failed-Recipients header contains the email address that failed # Check for the words "This is a permanent error." in the body, to indicate # a permanent failure if MailHandler.get_part_body(message) =~ /This is a permanent error./ return failed_recipients.split(/,\s*/) end end # Next, look for multipart/report if MailHandler.get_content_type(message) == "multipart/report" permanently_failed_recipients = [] message.parts.each do |part| if MailHandler.get_content_type(part) == "message/delivery-status" sections = MailHandler.get_part_body(part).split(/\r?\n\r?\n/) # The first section is a generic header; subsequent sections # represent a particular recipient. Since we sections[1..-1].each do |section| if section !~ /^Status: (\d)/ || $1 != '5' # Either we couldn’t find the Status field, or it was a transient failure break end if section =~ /^Final-Recipient: rfc822;(.+)/ permanently_failed_recipients.push($1) end end end end if !permanently_failed_recipients.empty? return permanently_failed_recipients end end end subject = MailHandler.get_header_string("Subject", message) # Then look for the style we’ve seen in WebShield bounces # (These do not have a return path of <> in the cases I have seen.) if subject == "Returned Mail: Error During Delivery" if MailHandler.get_part_body(message) =~ /^\s*---- Failed Recipients ----\s*((?:<[^>]+>\n)+)/ return $1.scan(/<([^>]+)>/).flatten end end return [] end def is_oof?(message) # Check for out-of-office if MailHandler.get_header_string("X-POST-MessageClass", message) == "9; Autoresponder" return true end subject = MailHandler.get_header_string("Subject", message).downcase if MailHandler.empty_return_path?(message) if subject.start_with? "out of office: " return true end if subject.start_with? "automatic reply: " return true end end if MailHandler.get_header_string("Auto-Submitted", message) == "auto-generated" if subject =~ /out of( the)? office/ return true end end if subject.start_with? "out of office autoreply:" return true end if subject == "out of office" return true end if subject == "out of office reply" return true end if subject.end_with? "is out of the office" return true end return false end def forward_on(raw_message) IO.popen("/usr/sbin/sendmail -i #{AlaveteliConfiguration::forward_nonbounce_responses_to}", "wb") do |f| f.write(raw_message); f.close; end end def load_rails require File.join($alaveteli_dir, 'config', 'boot') require File.join($alaveteli_dir, 'config', 'environment') end def record_bounce(email_address, bounce_message) load_rails User.record_bounce_for_email(email_address, bounce_message) end in_test_mode = (ARGV[0] == "--test") status = main(in_test_mode) exit(status) if in_test_mode