commit 396d6c2b58f658d97d04c933e56ac51b5c5c8db3
parent eaff998d0c2b01b7f1d77b62028814840f773ef4
Author: masayoshi <masayoshi@example.com>
Date: Mon, 23 Feb 2026 14:42:12 +0900
Upload
Diffstat:
| A | add_user.pl | | | 49 | +++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 49 insertions(+), 0 deletions(-)
diff --git a/add_user.pl b/add_user.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+use v5.40;
+use Term::ReadKey; # パスワードを非表示で読み取るために必要
+
+my $secret_file = 'secret.conf';
+
+print "Enter username to add: ";
+my $username = <STDIN>;
+chomp $username;
+
+# 1. バリデーション
+die "Error: Username cannot be empty.\n" if $username eq "";
+die "Error: Invalid username.\n" if $username =~ /[:\s]/;
+
+# 2. パスワードの読み取り (Perl側で制御)
+print "Enter password for '$username': ";
+ReadMode('noecho'); # 入力文字を非表示にする
+my $password = <STDIN>;
+chomp $password;
+ReadMode('normal'); # 元に戻す
+print "\n";
+
+print "Retype password: ";
+ReadMode('noecho');
+my $password_conf = <STDIN>;
+chomp $password_conf;
+ReadMode('normal');
+print "\n";
+
+if ($password ne $password_conf) {
+ die "Error: Passwords do not match. Operation aborted.\n";
+}
+
+# 3. ハッシュ化 (パイプで流し込む)
+print "Analysing and generating hash... (please wait)\n";
+my $hash = `echo '$password' | encrypt -b 12`;
+chomp $hash;
+
+# 4. 保存処理
+if ($hash && $hash =~ /^\$2b\$12\$/) {
+ open my $wfh, '>>', $secret_file or die "Could not open $secret_file: $!";
+ printf $wfh "%s:%s\n", $username, $hash;
+ close $wfh;
+ chmod 0600, $secret_file;
+ print "Success: User '$username' has been added to $secret_file.\n";
+} else {
+ print "Error: Failed to generate a valid hash.\n";
+}
+\ No newline at end of file