-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmdo
More file actions
executable file
·329 lines (274 loc) · 5.97 KB
/
vmdo
File metadata and controls
executable file
·329 lines (274 loc) · 5.97 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/ruby
# vmdo
# virtual machine infrastructure management, ruby 2.2.8
# by mrush 11.2017
# m@root.dance
# github.com/mattrush
##############
# signal handling
##############
Signal.trap('INT') { command = ''; puts "\r"; u_quit }
#Signal.trap('TERM') { exit } # i wish
##############
# dependencies
##############
require 'mysql'
require 'readline'
##############
# initialized data
##############
# authentication database credentials
database_creds = {
host: '',
user: '',
pass: '',
name: ''
}
# define authorization levels
levels = {
'issue' => 1,
'admin' => 2,
'production' => 3,
'research' => 4,
'player' => 5
}
# repl and api commands white-lists
# the _api arrays must be removed before deployment to production, they are just for testing / debugging during development, and are ultimately called by level_commands and down by higer-level _apis.
commands = {
universal: ['help','?','h','l','p','n','bye','quit','exit','q','env','alerts','!'],
global_api: [],
host_api: [],
storage_api: [],
guest_api: [
'conf',
'create',
'boot',
'halt',
'kill',
'reboot',
'nic_add',
'nic_del',
'disk_add',
'disk_del',
'disk_resize',
'mem_resize'
]
}
# level commands white-lists
level_filters = {
issue: [
'creds_generate',
'creds_revoke'
],
admin: [
'host_add',
'host_del'
],
production: [],
research: [],
player: []
}
##############
# methods
##############
# authentication
def authenticate(db,username,password)
query = "SELECT users.id AS uid, users.username AS user, roles.name AS role FROM roles INNER JOIN users ON (users.roles_id = roles.id) WHERE (users.username = '#{username}') AND (users.password_hash = PASSWORD('#{password}')) LIMIT 1"
connection = Mysql.new(db[:host],db[:user],db[:pass],db[:name])
result = connection.query(query)
connection.close
return false if result.num_rows == 0
keys = [:uid,:user,:authorization]
result.each do |row|
return keys.zip(row).to_h
end
end
def login_prompt
print "[>][username] > "
user_name = gets.chomp
print "[>][passphrase] > "
pass_word = gets.chomp
return :user_name => user_name, :pass_word => pass_word
end
def login(database_creds)
counter = 0
while counter < 3
login_data = login_prompt
auth_data = authenticate(database_creds,login_data[:user_name],login_data[:pass_word])
if auth_data
puts "[+][authentication success]"
$env = {user: auth_data[:user], authorization: auth_data[:authorization], context: '/'}
break
else
puts "[-][authentication failure]"
counter += 1
end
end
exit if counter >= 3
end
# authorization
def authorize(levels,level_filters,commands)
l = levels[$env[:authorization]]
levels.select do |k, v|
current = level_filters[k.to_sym].clone
commands[k.to_sym] = current if v >= l
end
end
# repl
def repl_prompt(commands)
prompt = "vmdo> "
completion = proc { |c| commands.values.flatten.grep(/^#{Regexp.escape(c)}/) }
Readline.completion_append_character = " "
Readline.completion_proc = completion
command = Readline.readline(prompt, true)
command.strip!
begin
case command
when /^\s*$/ then Readline::HISTORY.pop
when Readline::HISTORY[-2] then Readline::HISTORY.pop
end
rescue IndexError
end
return command.strip
# todo: sanitize inputs here, black-listing or whitelisting characters to strip.
# todo: ^C should stop a current task, or if no task is running, either exit or newline.
end
def repl(commands)
puts "Welcome to vmdo."
# todo: puts motd, pulled from the database.
loop do
command = ''
while command.empty?
command = repl_prompt(commands)
command = command.to_s.strip
end
command_context = nil
commands.each do |k, v|
command_context = k if v.include? "#{command}"
end
if command_context.nil?
puts "[-][Not found: #{command}]"
else
case command_context
# repl command classes
when :universal then prefix = 'u_'
# level command classes
when :issue then prefix = 'i_'
when :admin then prefix = 'a_'
when :production then prefix = 'p_'
when :research then prefix = 'r_'
when :player then prefix = 'pl_'
# api command classes
# N.B.: all _api cases below, should remain commented out, except during testing and/or debugging. this allows us to only expose top-level commands, per authorization level, which use the underlying apis.
when :global_api then prefix = 'gl_'
when :host_api then prefix = 'h_'
when :storage_api then prefix = 's_'
when :guest_api then prefix = 'g_'
end
self.send("#{prefix}#{command}".to_sym)
end
end
end
# issue
def i_creds_revoke
puts "yodo"
end
def i_creds_generate
puts "yolo"
end
# admin
def a_host_add
puts "host add"
end
def a_host_del
puts "host del"
end
# production
# research
# player
# universal
def u_help
puts "[+][vmdo help]"
puts "[.]"
puts "[.][]"
puts "[.][]"
puts "[.][]"
puts "[.][]"
end
alias :u_? :u_help
def u_previous
puts "Stepping back."
end
alias :u_h :u_previous
alias :u_p :u_previous
def u_next
puts "Stepping forward."
end
alias :u_l :u_next
alias :u_n :u_next
def u_quit
puts "[+][quit]"
exit
end
alias :u_exit :u_quit
alias :u_bye :u_quit
alias :u_q :u_quit
def u_env
puts "[+][environment]"
puts "[.]"
$env.each do |k,v|
puts "[.][#{k}: #{v}]"
end
end
def u_alerts
puts "[+][alerts]"
puts "[.]"
puts "[.][none]"
end
alias :u_! :u_alerts
# global_api
# host_api
# storage_api
# guest_api
def g_conf
true
end
def g_create
true
end
def g_boot
true
end
def g_halt
true
end
def g_kill
true
end
def g_reboot
true
end
def g_nic_add
true
end
def g_nic_del
true
end
def g_disk_add
true
end
def g_disk_del
true
end
def g_disk_resize
true
end
def g_mem_resize
true
end
##############
# flow control
##############
login(database_creds)
authorize(levels,level_filters,commands)
repl(commands)