Skip to content

Commit cc75c33

Browse files
committed
Use user home directories
Replace hard-coded '/home/' and '/root/' with `~username` shorthand.
1 parent 013e45e commit cc75c33

File tree

1 file changed

+29
-35
lines changed

1 file changed

+29
-35
lines changed

modules/post/linux/gather/enum_users_history.rb

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ def run
4444
vprint_status("Retrieving history for #{users.length} users")
4545
shells = %w{ ash bash csh ksh sh tcsh zsh }
4646
users.each do |u|
47+
home = get_home_dir(u)
4748
shells.each do |shell|
48-
get_shell_history(u, shell)
49+
get_shell_history(u, home, shell)
4950
end
50-
get_mysql_history(u)
51-
get_psql_history(u)
52-
get_mongodb_history(u)
53-
get_vim_history(u)
51+
get_mysql_history(u, home)
52+
get_psql_history(u, home)
53+
get_mongodb_history(u, home)
54+
get_vim_history(u, home)
5455
end
5556

5657
last = execute('/usr/bin/last && /usr/bin/lastlog')
@@ -88,54 +89,47 @@ def cat_file(filename)
8889
output
8990
end
9091

91-
def get_shell_history(user, shell)
92+
def get_home_dir(user)
93+
home = execute("echo ~#{user}")
94+
if home.empty?
95+
if user == 'root'
96+
return '/root'
97+
else
98+
return "/home/#{user}"
99+
end
100+
end
101+
home
102+
end
103+
104+
def get_shell_history(user, home, shell)
92105
return if shell.nil?
93106
vprint_status("Extracting #{shell} history for #{user}")
94-
if user == 'root'
95-
hist = cat_file("/root/.#{shell}_history")
96-
else
97-
hist = cat_file("/home/#{user}/.#{shell}_history")
98-
end
107+
hist = cat_file("#{home}/.#{shell}_history")
99108
save("#{shell} History for #{user}", hist) unless hist.blank? || hist =~ /No such file or directory/
100109
end
101110

102-
def get_mysql_history(user)
111+
def get_mysql_history(user, home)
103112
vprint_status("Extracting MySQL history for #{user}")
104-
if user == 'root'
105-
sql_hist = cat_file('/root/.mysql_history')
106-
else
107-
sql_hist = cat_file("/home/#{user}/.mysql_history")
108-
end
113+
sql_hist = cat_file("#{home}/.mysql_history")
109114
save("MySQL History for #{user}", sql_hist) unless sql_hist.blank? || sql_hist =~ /No such file or directory/
110115
end
111116

112-
def get_psql_history(user)
117+
def get_psql_history(user, home)
113118
vprint_status("Extracting PostgreSQL history for #{user}")
114-
if user == 'root'
115-
sql_hist = cat_file('/root/.psql_history')
116-
else
117-
sql_hist = cat_file("/home/#{user}/.psql_history")
118-
end
119+
sql_hist = cat_file("#{home}/.psql_history")
119120
save("PostgreSQL History for #{user}", sql_hist) unless sql_hist.blank? || sql_hist =~ /No such file or directory/
120121
end
121122

122-
def get_mongodb_history(user)
123+
def get_mongodb_history(user, home)
123124
vprint_status("Extracting MongoDB history for #{user}")
124-
if user == 'root'
125-
sql_hist = cat_file('/root/.dbshell')
126-
else
127-
sql_hist = cat_file("/home/#{user}/.dbshell")
128-
end
125+
sql_hist = cat_file("#{home}/.dbshell")
129126
save("MongoDB History for #{user}", sql_hist) unless sql_hist.blank? || sql_hist =~ /No such file or directory/
130127
end
131128

132-
def get_vim_history(user)
129+
def get_vim_history(user, home)
133130
vprint_status("Extracting VIM history for #{user}")
134-
if user == 'root'
135-
vim_hist = cat_file('/root/.viminfo')
136-
else
137-
vim_hist = cat_file("/home/#{user}/.viminfo")
138-
end
131+
vim_hist = cat_file("#{home}/.viminfo")
139132
save("VIM History for #{user}", vim_hist) unless vim_hist.blank? || vim_hist =~ /No such file or directory/
140133
end
141134
end
135+

0 commit comments

Comments
 (0)