Mobile-OTP を使うと umask がおかしくなる問題を修正する

Mobile-OTP については,スマートフォン/ガラケー/PDA等を使ってワンタイムパスワードを実現するMobile-OTPのススメ - Dマイナー志向に詳しい.一言で言うと,携帯電話のような皆が当たり前のように持ち歩いているデバイスを,時限性のワンタイムパスワード生成器にして,秘密鍵を持たない出先のインセキュアな端末からもスマートに SSH を使いましょう,というお話.

しかし,これを使うと umask がおかしくなる.

これは誤解で,本当は 0077 になっていた.

以下,パッチ.

% diff -u pam_mobile_otp/pam_mobile_otp.c.org pam_mobile_otp/pam_mobile_otp.c
--- pam_mobile_otp/pam_mobile_otp.c.org 2011-02-07 00:33:40.000000000 +0900
+++ pam_mobile_otp/pam_mobile_otp.c     2012-03-11 16:50:10.000000000 +0900
@@ -324,7 +324,7 @@
       }
     }

-    umask(077);
+    mode_t umask_orig = umask(077);
     if ( (fp=fopen(statfile,"w")) == NULL ) {
       _PAM_LOG(LOG_ERR, "cannot write statfile %s",statfile);
       return PAM_AUTHINFO_UNAVAIL;
@@ -336,6 +336,7 @@
     TRACE(stderr,"written new stats - tries: %d, last time: %lu\n",tries,last_time);
     if ( fclose(fp) ) if (!no_warn)
       _PAM_LOG(LOG_INFO, "warning: cannot close file (w) %s",statfile);
+    umask(umask_orig);
   }


@@ -595,7 +596,7 @@
       tries = 0;

       /* write user's status file */
-      umask(077);
+      mode_t umask_orig = umask(077);
       if ( (fp=fopen(statfile,"w")) == NULL ) {
        _PAM_LOG(LOG_ERR, "cannot write statfile %s",statfile);
        return PAM_SERVICE_ERR;
@@ -607,6 +608,7 @@
       TRACE(stderr,"Has reset tries and written new stats - tries: %d, last time: %lu\n",tries,last_time);
       if ( fclose(fp) ) if (!no_warn)
        _PAM_LOG(LOG_INFO, "warning: cannot close file (w) %s",statfile);
+      umask(umask_orig);
     }
   }

こういう変なバグはあるけれど,すばらしく便利なツールなので,泣く泣く秘密鍵を USB メモリに入れて持ち歩いている人は是非一度おためしあれ.