Last active
October 1, 2017 16:24
-
-
Save sensq/ae4238da5b090d3f5a45268062bf5b39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 八進表記から記号表記に変換 | |
function ConvertPermissionNumberToString($target) { | |
$res = $target | |
# 特殊権限への対応(事前判定と除去) | |
$sp = "" | |
if($res.Length -eq 4){ | |
$sp = $res[0] | |
$res = $res.Remove(0,1) | |
} | |
# 手っ取り早く総当りで変換 | |
$res = $res -replace 0,"---" | |
$res = $res -replace 1,"--x" | |
$res = $res -replace 2,"-w-" | |
$res = $res -replace 3,"-wx" | |
$res = $res -replace 4,"r--" | |
$res = $res -replace 5,"r-x" | |
$res = $res -replace 6,"rw-" | |
$res = $res -replace 7,"rwx" | |
# 特殊権限への対応(所定位置の特定の文字を入れ替える) | |
function ConvertSUID($res){ | |
if($res[2] -eq "x"){ | |
$res = $res.Insert(3, "s") | |
}else{ | |
$res = $res.Insert(3, "S") | |
} | |
$res = $res.Remove(2, 1) | |
return $res | |
} | |
function ConvertSGID($res){ | |
if($res[5] -eq "x"){ | |
$res = $res.Insert(6, "s") | |
}else{ | |
$res = $res.Insert(6, "S") | |
} | |
$res = $res.Remove(5, 1) | |
return $res | |
} | |
function ConvertSticky($res){ | |
if($res[8] -eq "x"){ | |
$res = $res.Insert(9, "t") | |
}else{ | |
$res = $res.Insert(9, "T") | |
} | |
$res = $res.Remove(8, 1) | |
return $res | |
} | |
switch($sp){ | |
"1" { | |
$res = ConvertSUID $res | |
} | |
"2" { | |
$res = ConvertSGID $res | |
} | |
"3" { | |
$res = ConvertSUID $res | |
$res = ConvertSGID $res | |
} | |
"4" { | |
$res = ConvertSticky $res | |
} | |
"5" { | |
$res = ConvertSUID $res | |
$res = ConvertSticky $res | |
} | |
"6" { | |
$res = ConvertSGID $res | |
$res = ConvertSticky $res | |
} | |
"7" { | |
$res = ConvertSUID $res | |
$res = ConvertSGID $res | |
$res = ConvertSticky $res | |
} | |
} | |
return ($res.ToString()) | |
} | |
# 記号表記から八進表記に変換 | |
function ConvertPermissionStringToNumber($target) { | |
$res = $target | |
# SELinux対策(最後尾が.だったら消す) | |
if($res[-1] -eq "."){ | |
$res = $res -replace "\.","" | |
} | |
# ファイル種別まで指定されていたら(指定文字数が10文字だったら)先頭1文字を消す | |
if($res.Length -eq 10){ | |
$res = $res.Remove(0, 1) | |
} | |
# 特殊権限への対応 | |
$sp = 0 | |
if($res[2] -eq "s"){ $sp += 1 } # SUID | |
if($res[5] -eq "s"){ $sp += 2 } # SGID | |
if($res[8] -eq "t"){ $sp += 4 } # Sticky | |
# 一時的に「,」で3文字ずつに分割(誤変換防止) | |
$res = $res.Insert(3, ",") | |
$res = $res.Insert(7, ",") | |
# 手っ取り早く総当りで変換 | |
$res = $res -creplace "s","x" | |
$res = $res -creplace "t","x" | |
$res = $res -creplace "S","-" | |
$res = $res -creplace "T","-" | |
$res = $res -replace "---",0 | |
$res = $res -replace "--x",1 | |
$res = $res -replace "-w-",2 | |
$res = $res -replace "-wx",3 | |
$res = $res -replace "r--",4 | |
$res = $res -replace "r-x",5 | |
$res = $res -replace "rw-",6 | |
$res = $res -replace "rwx",7 | |
# 一時的に入れた「,」を消去 | |
$res = $res -replace ",","" | |
return ($sp.ToString() + $res.ToString()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# テスト | |
$arr = @( | |
"0777" | |
"0000" | |
"0123" | |
"0321" | |
"0755" | |
"1755" | |
"2755" | |
"3755" | |
"4755" | |
"5755" | |
"6755" | |
"7755" | |
"0644" | |
"7644" | |
"7777" | |
"7666" | |
) | |
echo ("Convert`tsource`tdestination") | |
$arr | %{ | |
echo ("-----") | |
$res1 = ConvertPermissionNumberToString $_ | |
echo ("8進 → rwx:`t" + $_ + "`t" + $res1) | |
$res2 = ConvertPermissionStringToNumber $res1 | |
echo ("rwx → 8進:`t" + $res1 + "`t" + $res2) | |
# 再変換した結果が入力と一致していたらOK | |
if($_ -eq $res2){echo "○"}else{echo "×"} | |
} | |
### 実行結果 | |
Convert source destination | |
----- | |
8進 → rwx: 0777 rwxrwxrwx | |
rwx → 8進: rwxrwxrwx 0777 | |
○ | |
----- | |
8進 → rwx: 0000 --------- | |
rwx → 8進: --------- 0000 | |
○ | |
----- | |
8進 → rwx: 0123 --x-w--wx | |
rwx → 8進: --x-w--wx 0123 | |
○ | |
----- | |
8進 → rwx: 0321 -wx-w---x | |
rwx → 8進: -wx-w---x 0321 | |
○ | |
----- | |
8進 → rwx: 0755 rwxr-xr-x | |
rwx → 8進: rwxr-xr-x 0755 | |
○ | |
----- | |
8進 → rwx: 1755 rwsr-xr-x | |
rwx → 8進: rwsr-xr-x 1755 | |
○ | |
----- | |
8進 → rwx: 2755 rwxr-sr-x | |
rwx → 8進: rwxr-sr-x 2755 | |
○ | |
----- | |
8進 → rwx: 3755 rwsr-sr-x | |
rwx → 8進: rwsr-sr-x 3755 | |
○ | |
----- | |
8進 → rwx: 4755 rwxr-xr-t | |
rwx → 8進: rwxr-xr-t 4755 | |
○ | |
----- | |
8進 → rwx: 5755 rwsr-xr-t | |
rwx → 8進: rwsr-xr-t 5755 | |
○ | |
----- | |
8進 → rwx: 6755 rwxr-sr-t | |
rwx → 8進: rwxr-sr-t 6755 | |
○ | |
----- | |
8進 → rwx: 7755 rwsr-sr-t | |
rwx → 8進: rwsr-sr-t 7755 | |
○ | |
----- | |
8進 → rwx: 0644 rw-r--r-- | |
rwx → 8進: rw-r--r-- 0644 | |
○ | |
----- | |
8進 → rwx: 7644 rwSr-Sr-T | |
rwx → 8進: rwSr-Sr-T 7644 | |
○ | |
----- | |
8進 → rwx: 7777 rwsrwsrwt | |
rwx → 8進: rwsrwsrwt 7777 | |
○ | |
----- | |
8進 → rwx: 7666 rwSrwSrwT | |
rwx → 8進: rwSrwSrwT 7666 | |
○ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment