アナログCPU:5108843109

ゲームと音楽とプログラミング(酒と女とロックンロールのノリで)

('ω') < イザユケエンジニャー

htpasswdでつまづいた件

※ほとんどネタ記事です。ちゃんとヘルプを読もうね、という話。


basic認証のユーザーを追加することになったのでググる

「よし、htpasswdというコマンドを使うんだな!」
「使いそうなオプションはbとmとnあたりかな!」
(※ b:パスワードを引数に渡すことができる
   m:md5でハッシュする
   n:実際には追加されず、確認用の表示だけする)

ということで試してみる。

対象のディレクトリへ移動。

cd /etc/httpd/conf/

まずは怖いのでパスワード用ファイルをコピー。

# cp .htpasswd .htpasswd_bk

これで壊れてもたぶん大丈夫。

よし、ググった情報を参考にさっそくコマンドを打ってみよう!

# htpasswd -b -m -n .htpasswd testuser testpass

どれどれ…

Usage:
        htpasswd [-cmdpsD] passwordfile username
        htpasswd -b[cmdpsD] passwordfile username password

        htpasswd -n[mdps] username
        htpasswd -nb[mdps] username password
 -c  Create a new file.
 -n  Don't update file; display results on stdout.
 -m  Force MD5 encryption of the password.
 -d  Force CRYPT encryption of the password (default).
 -p  Do not encrypt the password (plaintext).
 -s  Force SHA encryption of the password.
 -b  Use the password from the command line rather than prompting for it.
 -D  Delete the specified user.
On Windows, NetWare and TPF systems the '-m' flag is used by default.
On all other systems, the '-p' flag will probably not work.

いや使い方なんか聞いてねーし。
でもなんか間違えてるってことだよな…

つーかオプションは別にまとめてもいいよね。

# htpasswd -bmn .htpasswd testuser testpass

とりあえずまとめた。

Usage:
        htpasswd [-cmdpsD] passwordfile username
        htpasswd -b[cmdpsD] passwordfile username password
(以下略)

まあそうだよね!!!

その後もいろいろ試す。

オプションを入れ替えたり

# htpasswd -nbm .htpasswd testuser testpass

md5やめてデフォルトにしてみたり

# htpasswd -nb .htpasswd testuser testpass

オプションを消してみたり

# htpasswd .htpasswd testuser testpass

既にrootなのにsudoつけてみたり

# sudo htpasswd -n .htpasswd testuser testpass

何故か中途半端にオプション分けてみたり

# htpasswd -nb -m .htpasswd testuser testpass

パスワードを引数にするオプションとパスワードを外してみたり

# htpasswd -n .htpasswd testuser

ファイル名書き忘れたり

# htpasswd -bm testuser testpass

ファイル名書き忘れたままmd5オプション消してみたり

# htpasswd -b testuser testpass

さらにパスワードも消えたり

# htpasswd -b testuser

あっやべえいつの間にかnオプション消してたわ、と書き換えたり

# htpasswd -n testuser

New password:

…ん!?
なんか出た!!

Re-type new password:

これは!!!

testuser:qwertyuiop@[

初めて成功したのでは!!!!

意気揚々と.htpasswdをcatするが、増えてない。

なんでや!と思って自分の書いたコマンドをよく読む。
…ファイル名抜けてるじゃん!!!!!(ここで初めて気づいた)
というかそもそもnオプションついてるもんね!!!(明らかに疲れている)

もうね。アボカド。バナナかと。(古い)

nオプションのおかげでどこも書き換わったりはしていないのが救い。

でもこの一歩は大きい。
今度はきちんとファイル名指定しよう。

# htpasswd -n .htpasswd testuser
Usage:
        htpasswd [-cmdpsD] passwordfile username
        htpasswd -b[cmdpsD] passwordfile username password

        htpasswd -n[mdps] username
        htpasswd -nb[mdps] username password
 -c  Create a new file.
 -n  Don't update file; display results on stdout.
 -m  Force MD5 encryption of the password.
 -d  Force CRYPT encryption of the password (default).
 -p  Do not encrypt the password (plaintext).
 -s  Force SHA encryption of the password.
 -b  Use the password from the command line rather than prompting for it.
 -D  Delete the specified user.
On Windows, NetWare and TPF systems the '-m' flag is used by default.
On all other systems, the '-p' flag will probably not work.

…。

……。

なんでや!!

まあ確かに全く同じコマンドを既に試しているな…と脱力。
えーもうやだー…と思いながらこのヘルプのようなものをぼけっと眺めているとあることに気付いた。

htpasswd [-cmdpsD] passwordfile username

htpasswd -n[mdps] username

…nオプション付けたらファイル名指定不可なのか…orz

orz

ということで、迷走しまくった挙句、最終的に

# htpasswd .htpasswd testuser

という、どシンプルなコマンドで解決したのでした。おしまい。
md5すら不要だった)




いくら英語嫌いでも、コマンド例くらいは見ようね!!

orz




…間違ったコマンドまみれの記事なので、せめてきちんと動作するコマンド例も置いとこ…

カレントディレクトリ以下にある「.htpasswd」に「testuser」ユーザーを追加する
パスワードは後から2回聞かれる
mを追加するとmd5ハッシュ

# htpasswd .htpasswd testuser
# htpasswd -m .htpasswd testuser

「testuser」ユーザーを追加しようとすると実際に何が追加されるのかプレビューされる
パスワードは後から2回聞かれる
mを追加するとmd5ハッシュ

# htpasswd -n testuser
# htpasswd -nm testuser

bオプションを使用して、IDとパスワードの両方を引数に渡すことができる
mを追加するとmd5ハッシュ

# htpasswd -b .htpasswd testuser testpass
# htpasswd -bm .htpasswd testuser testpass

番外:ユーザーを削除する

# htpasswd -D .htpasswd testuser