2006/10/13

SSLStream for GMail POP3s

GMailに大変お世話になっています。

未だにβ版とはいえ便利すぎて手放せません。まぁGMailFSとかも使ってるからですが。
POP3に対応というかPOP3s 対応しているのもちょっと魅力。

それで以前にC#でTLS物を作ろうとして苦労をしていたのを思い出したのですが、.NET 2.0なるとSSLStreamとかってクラスが追加されてて、簡単に出来そうな感じだったので試してみました。



元のサンプルプログラムを弄っただけもので、あくまでもクラスのテスト用のコードですが。


using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
namespace ConsoleSecureTcpClient
{

    class SecureClient
    {

        [STAThread]
        static void Main()
        {
            string server = "pop.gmail.com";
            string sUser = "USER " +  "Your Account@gmail.com" + "\r\n";
            string sPassword = "PASS " + "Your Password" + "\r\n";
            int port = 995;
            SslStream stream = null;

            try
            {
                // Instantiate a TcpClient with the target server and port number
                TcpClient client = new TcpClient(server, port);

                // Specify the callback function that will act as the validation delegate. This
                // lets you inspect the certificate to see if it meets your
                // validation requirements.
                RemoteCertificateValidationCallback callback =
                                                    new RemoteCertificateValidationCallback(OnCertificateValidation);

                // Instantiate an SslStream with the NetworkStream returned from the TcpClient.
                stream = new SslStream(client.GetStream(), false, callback);

             
                // As a client, you can authenticate the server and validate the results using the SslStream.
                // This is the host name of the server you are connecting to, which may or may not
                // be the name used to connect to the server when TcpClient is instantiated.
                stream.AuthenticateAsClient(server);
                if (stream.IsAuthenticated)
                {
                    // Indicates whether the authentication was successful.
                    Console.WriteLine("IsAuthenticated: {0}", stream.IsAuthenticated);
                    // Indicates whether both the client and server has been authenticated.
                    // In this example only the server is authenticated.
                    Console.WriteLine("IsMutuallyAuthenticated: {0}", stream.IsMutuallyAuthenticated);
                    // Indicates whether the SslStream uses data encryption.
                    Console.WriteLine("IsEncrypted: {0}", stream.IsEncrypted);
                    // Indicates whether the data sent is signed.
                    Console.WriteLine("IsSigned: {0}", stream.IsSigned);
                    // Indicates whether the current side of the connection is authenticated as a server.
                    Console.WriteLine("IsServer: {0}", stream.IsServer);
                }

                Byte []data = new Byte[256];
                int bytes = stream.Read(data, 0, data.Length);
                string responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine("Received: {0}", responseData);

                data = System.Text.Encoding.ASCII.GetBytes(sUser);
                stream.Write(data, 0, data.Length);
                Console.WriteLine("Sent: {0}", Encoding.ASCII.GetString(data));

                data = new Byte[256];
                bytes = stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine("Received: {0}", responseData);

                data = System.Text.Encoding.ASCII.GetBytes(sPassword);
                stream.Write(data, 0, data.Length);
                Console.WriteLine("Sent: {0}", Encoding.ASCII.GetString(data));
             
                data = new Byte[256];
                bytes = stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine("Received: {0}", responseData);

                data = System.Text.Encoding.ASCII.GetBytes("QUIT\r\n");
                stream.Write(data, 0, data.Length);
                Console.WriteLine("Sent: {0}", Encoding.ASCII.GetString(data));
             
                data = new Byte[1024*1024];
                bytes = stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine("Received: {0}", responseData);

            }
            catch (AuthenticationException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                // Make sure that the SslStream is closed.
                if (stream != null)
                    stream.Close();
            }

            Console.WriteLine("\n Press Enter to continue...");
            Console.Read();
        }
        // Check the certificate for errors and to make sure it meets your security policy.
        private static bool OnCertificateValidation(object sender, X509Certificate certificate
                    , X509Chain chain, SslPolicyErrors errors)
        {
            Console.WriteLine("Server Certificate Issued To: {0}", certificate.GetName());
            Console.WriteLine("Server Certificate Issued By: {0}", certificate.GetIssuerName());
            // Return true if there are no policy errors
            // The certificate can also be manually verified to make sure it meets your specific
            // policies by interrogating the x509Certificate object.

            if (errors != SslPolicyErrors.None)
            {
                Console.WriteLine("Server Certificate Validation Error");
                Console.WriteLine(errors.ToString());
                return false;
            }
            else
            {
                Console.WriteLine("No Certificate Validation Errors");
                return true;
            }
        }
    }
}


以前は、Mentalis.org Security Library を使用して満足していましたが、Frameworkに入ったという事でこれはこれでよい感じです。

しかしFrameworkもここまでくると、IntelliSense無しのCodingはちょっと辛いですね。

続きを読む・・

2006/10/10

Dual Head Display

正式名称(?) はなんて言えば良いのでしょうか?

私は今使ってるVideo Cardが、Matrox G450 Dual HeadなんでDual Headと呼んでしまいますが、単純にDual Display とかMulti Displayで良いのかな。

数年前なら贅沢感たっぷりの17LCDでのDual環境ですが、いまどきは大した金額もかからないし、OSが複数のVideo Cardの同時使用を見事にサポートしてくれるのでまったく珍しくありません。自宅の環境はAuctionで買い揃えたものばかりですが、LCD は 2万円程度が2台と本体も3万円もかかっていないというCheapさ。

ですけど、未だに少数派ですよね。LCD(液晶Display)への入れ替えとか、本体入れ替えと同時の新しいDisplayも買っちゃったりして、会社とかではDisplayが余ったり廃棄したりしてるんではないかと思うのですが、机のう上のスペースも限られてるからなのか、ディスプレイは一台のままの人多いですよね。

私の職場では机は十分に広い(21inch 二台も十分にいけるぐらい)なのと、余ったディスプレイを廃棄しようとしていたので、なんとかVideoカードを入手してDual Display環境にしています。
開発環境は特にDual Displayが効果あるんですけどね。Document見ながらCodingとか、実行中の画面を片側に出しながらDebugしたりとか。

費用対効果は絶大なのに何故にやらないのか。これは開発者ならびにその管理者・経営者が開発の作業効率に関して無頓着だからでしょうね。ノートPCの 1024x768の画面での開発作業と、17inch 1280 x1024がDualで開発とどれくらい作業効率が違うとか数値的に考えた事がないでしょうし。

私は業務系のコンサルティングをやってましたので、一つの作業にかかる時間とかに非常に興味があるというか注意がいくようになりました。何をすれば効率が上がるのかって事に。当然開発作業は作業者本人が考えないと・志向しないといけない部分がいまだ多分にあるので、単純作業のように純粋に肉体的な効率だけを考えていけばよいわけではないのですが、視覚を広げる、Diaplay の表示面積を広げる事は重要だと思います。

私にしてみれば、いまだにTouch Typing(旧称Blind Touch)が出来ない人とかはちょっと勘弁してほしいです。Touch Typingできないが故に、コメントか書かないとか、Copy and Pasteでコード書く事が多くて、それが逆に見ていてイライラするぐらい、時間はかかるは直さなければいけない部分を忘れたり。変数名やファンクション名は第三者から見たら意味の分からない略語ばかり。そんなソースコードを書く人と一緒に仕事するのは辛いです。

Joel on Software "http://japanese.joelonsoftware.com/Articles/TheJoelTest.html" とか読んで欲しいです。
特に、"8. プログラマは静かな労働環境にあるか?" って大事じゃない?
>広く書かれていることだが、生産的な利益は、知識労働者に空間と
>静かさとプライバシーを与えることによってもたらされる。古典的なソ
>フトウェア管理の本「ピープルウェア」はこれらの生産性が広範囲に
>渡ることを述べている

もう、顧客からの質問の電話がかかってくるだけで、集中は途切れてしまうし、まわりの動きだって気になる。そんなものは生産性・効率の面からいったら排除すべき問題なのに、そういった事にまったく無頓着な職場はどうなんでしょう?

続きを読む・・

2006/10/03

Allow non-administrators to receive update notifications

Allow non-administrators to receive update notifications

Windows Updateを非管理者にも許可するには、Group Policy(gpedit.msc)で以下の項目を変更する。
Local Computer Policy -> Computer Configuration -> Administrative Templates -> Windows Componetnts -> Windows Update -> Allow non-administrators to receive update notifications

ローカル コンピュータ ポリシー -> コンピュータの構成 -> 管理者用テンプレート -> Windows コンポーネント -> Windows Update -> 「非管理者による更新の通知を許可する」

とは言っても通知を受けてからの処理を許可するだけなんで、手動でのWindows Updateは出来ないような気がする。

と自分用のメモを書いてみました。

続きを読む・・