-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupm
More file actions
executable file
·163 lines (139 loc) · 4.75 KB
/
Copy pathupm
File metadata and controls
executable file
·163 lines (139 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env bash
# ─── CONFIG ────────────────────────────────────────────────────────────────────
DB_NAME="password_store"
DB_USER="password_admin"
# ─── FUNCTIONS ─────────────────────────────────────────────────────────────────
get_db_password() {
if [ -z "${PGPASSWORD:-}" ]; then
echo -n "Database password for $DB_USER: " >&2
read -s PGPASSWORD
echo >&2
export PGPASSWORD
fi
}
generate_random_password() {
# Generate a 16-character random password (24 bytes base64 encoded gives ~16 chars after filtering)
openssl rand -base64 12 | tr -dc '[:alnum:]' | cut -c1-16
# Alternative with more special characters:
# openssl rand -base64 16 | tr -dc 'a-zA-Z0-9!@#$%^&*()_+' | cut -c1-16
}
get_password_input() {
while true; do
echo "Choose password option:"
echo "1) Enter my own password"
echo "2) Generate random password"
read -p "Select option (1/2): " choice
case $choice in
1)
read -s -p "Enter password: " password
echo
break
;;
2)
password=$(generate_random_password)
echo "Generated password: $password"
break
;;
*)
echo "Invalid option. Please try again."
;;
esac
done
}
store_password() {
echo "=== Store New Password ==="
read -p "Service: " SERVICE
read -p "Username: " USERNAME
get_password_input
# Check if entry exists before inserting
local COUNT=$(PGPASSWORD="$PGPASSWORD" psql -U "$DB_USER" -d "$DB_NAME" -t -A \
-c "SELECT COUNT(*) FROM credentials
WHERE service = '$(echo "$SERVICE" | sed "s/'/''/g")'
AND username = '$(echo "$USERNAME" | sed "s/'/''/g")';")
if [ "$COUNT" -eq "0" ]; then
PGPASSWORD="$PGPASSWORD" psql -U "$DB_USER" -d "$DB_NAME" <<-SQL
INSERT INTO credentials (service, username, password)
VALUES (
'$(echo "$SERVICE" | sed "s/'/''/g")',
'$(echo "$USERNAME" | sed "s/'/''/g")',
'$(echo "$password" | sed "s/'/''/g")'
);
SQL
else
PGPASSWORD="$PGPASSWORD" psql -U "$DB_USER" -d "$DB_NAME" <<-SQL
UPDATE credentials
SET password = '$(echo "$password" | sed "s/'/''/g")'
WHERE service = '$(echo "$SERVICE" | sed "s/'/''/g")'
AND username = '$(echo "$USERNAME" | sed "s/'/''/g")';
SQL
fi
if [ $? -eq 0 ]; then
echo "✅ Password saved successfully."
else
echo "❌ Failed to save password."
return 1
fi
}
retrieve_password() {
echo "=== Retrieve Password ==="
read -p "Service: " SERVICE
read -p "Username: " USERNAME
# Fetch from database
local PASSWORD=$(PGPASSWORD="$PGPASSWORD" psql -U "$DB_USER" -d "$DB_NAME" -t -A \
-c "SELECT password
FROM credentials
WHERE service = '$(echo "$SERVICE" | sed "s/'/''/g")'
AND username = '$(echo "$USERNAME" | sed "s/'/''/g")';")
if [ -z "$PASSWORD" ]; then
echo "❌ No entry found for $SERVICE / $USERNAME"
# Show available entries
echo -e "\nAvailable entries:"
PGPASSWORD="$PGPASSWORD" psql -U "$DB_USER" -d "$DB_NAME" -c \
"SELECT service, username FROM credentials ORDER BY service, username;"
return 1
fi
echo -n "Password: "
echo "$PASSWORD"
}
list_entries() {
echo "=== Available Credentials ==="
PGPASSWORD="$PGPASSWORD" psql -U "$DB_USER" -d "$DB_NAME" -c \
"SELECT service, username FROM credentials ORDER BY service, username;"
}
delete_entry() {
echo "=== Delete Password Entry ==="
read -p "Service: " SERVICE
read -p "Username: " USERNAME
PGPASSWORD="$PGPASSWORD" psql -U "$DB_USER" -d "$DB_NAME" -c \
"DELETE FROM credentials
WHERE service = '$(echo "$SERVICE" | sed "s/'/''/g")'
AND username = '$(echo "$USERNAME" | sed "s/'/''/g")';"
if [ $? -eq 0 ]; then
echo "✅ Entry deleted (if it existed)."
else
echo "❌ Failed to delete entry."
return 1
fi
}
# ─── MAIN PROGRAM ───────────────────────────────────────────────────────────────
get_db_password
while true; do
echo
echo "==== Simple Password Manager ===="
echo "1) Store new password"
echo "2) Retrieve password"
echo "3) List all entries"
echo "4) Delete entry"
echo "0) Exit"
echo
read -p "Select option: " OPTION
echo
case $OPTION in
1) store_password ;;
2) retrieve_password ;;
3) list_entries ;;
4) delete_entry ;;
0) echo "Goodbye!"; exit 0 ;;
*) echo "Invalid option" ;;
esac
done