例えば秘密鍵などの隠しておきたい情報を取り扱う場合に、予め umask 066 としておくことで、chmod するまでの間に情報流出する危険性を防ぐ。umask はファイルの新規作成時にのみ適用されるので、既存ファイルのパーミッションを変えるものではない。すなわち単純に鍵作成コマンドを実行する前に umask と chmod をやっておこうということです。
# umask 066 # chomod \ 0600 \ /etc/ipsec.d/private/caprivatekey.der \ ; # ipsec \ pki \ --gen \ --type rsa \ --size 4096 \ --outform der \ > /etc/ipsec.d/private/caprivatekey.der \ ;
シェルスクリプト的には、出力するファイルを予め作っておいて、パーミッションを適切なものにする関数を事前に呼び出す。
$ cat --number test.sh
1 #!/bin/dash
2 set -eux;
3
4 mkdir_touch () {
5 local _FILE="${1}"
6 local _DIRNAME="$(
7 dirname \
8 "${_FILE}" \
9 )";
10 if ! test -d "${_DIRNAME}"
11 then
12 mkdir \
13 --parents \
14 "${_DIRNAME}" \
15 ;
16 fi
17 touch \
18 "${_FILE}" \
19 ;
20 }
21
22 sensitive_file_set () {
23 local _FILE="${1}"
24 if test -e "${_FILE}";
25 then
26 chmod \
27 0600 \
28 "${_FILE}" \
29 ;
30 else
31 local _UMASK="$(umask)";
32 if test "${_UMASK}" = "0066";
33 then
34 mkdir_touch \
35 "${_FILE}" \
36 ;
37 else
38 (
39 umask \
40 066 \
41 ;
42 mkdir_touch \
43 "${_FILE}" \
44 ;
45 )
46 fi
47 fi
48 }
49
50 sensitive_data_wrapper () {
51 local _FILE="${1}";
52 shift;
53 sensitive_file_set \
54 "${_FILE}" \
55 ;
56 ${@} \
57 > "${_FILE}" \
58 ;
59 }
60
61
62 sensitive_data_redirection () {
63 local _FILE="${1}"; shift;
64 sensitive_file_set \
65 "${_FILE}" \
66 ;
67 ${@} \
68 > "${_FILE}" \
69 ;
70 }
71
72 sensitive_data_command () {
73 ls -la;
74 echo \
75 'private data' \
76 ;
77 }
78
79 sensitive_data_command1 () {
80 local _FILE="${1}"; shift;
81 sensitive_file_set \
82 "${_FILE}" \
83 ;
84 echo \
85 'sensitive data command1' \
86 | tee \
87 "${_FILE}" \
88 ;
89 }
90
91 sensitive_data_command_function () {
92 sensitive_data_command \
93 ;
94 }
95
96 alias sensitive_data_command_alias='sensitive_data_command hoge';
97
98 main () {
99 sensitive_data_command1 \
100 'path_to_sensitive_file.txt' \
101 ;
102 sensitive_data_redirection \
103 'path_to_sensitive_file.txt' \
104 sensitive_data_command \
105 ;
106 sensitive_data_redirection \
107 'path_to_sensitive_file.txt' \
108 sensitive_data_command_function \
109 ;
110 sensitive_data_redirection \
111 'path_to_sensitive_file.txt' \
112 sensitive_data_command_alias \
113 ;
114 }
115
116 main;
117
118 exit 0;