2015-05-21 08:52:10 +00:00
|
|
|
#!/usr/bin/env python
|
2015-05-27 13:41:08 +00:00
|
|
|
#
|
|
|
|
# gstfsd - WebVirtCloud daemon for managing VM's filesystem
|
|
|
|
#
|
2015-05-21 08:52:10 +00:00
|
|
|
|
2020-03-16 13:59:45 +00:00
|
|
|
import socketserver
|
2015-05-21 08:52:10 +00:00
|
|
|
import json
|
|
|
|
import guestfs
|
|
|
|
import re
|
|
|
|
|
|
|
|
PORT = 16510
|
|
|
|
ADDRESS = "0.0.0.0"
|
|
|
|
|
|
|
|
|
2020-03-16 13:59:45 +00:00
|
|
|
class MyTCPServer(socketserver.ThreadingTCPServer):
|
2015-05-21 08:52:10 +00:00
|
|
|
allow_reuse_address = True
|
|
|
|
|
|
|
|
|
2020-03-16 13:59:45 +00:00
|
|
|
class MyTCPServerHandler(socketserver.BaseRequestHandler):
|
2015-05-21 08:52:10 +00:00
|
|
|
def handle(self):
|
|
|
|
# recive data
|
2020-07-13 09:33:09 +00:00
|
|
|
d = self.request.recv(1024).strip()
|
|
|
|
data = json.loads(d)
|
2015-05-21 08:52:10 +00:00
|
|
|
|
|
|
|
# GuestFS
|
2015-06-08 19:45:56 +00:00
|
|
|
gfs = guestfs.GuestFS(python_return_dict=True)
|
2015-05-21 08:52:10 +00:00
|
|
|
try:
|
|
|
|
gfs.add_domain(data['vname'])
|
|
|
|
gfs.launch()
|
|
|
|
parts = gfs.list_partitions()
|
|
|
|
for part in parts:
|
|
|
|
try:
|
|
|
|
gfs.mount(part, '/')
|
|
|
|
if gfs.is_file('/etc/shadow'):
|
2015-05-27 13:23:49 +00:00
|
|
|
if data['action'] == 'password':
|
|
|
|
file_shadow = gfs.cat('/etc/shadow')
|
|
|
|
new_root_hash = "root:" + data['passwd'] + ":"
|
|
|
|
file_shadow_new = re.sub('^root:.*?:', new_root_hash, file_shadow)
|
|
|
|
gfs.write('/etc/shadow', file_shadow_new)
|
|
|
|
gfs.chmod(640, '/etc/shadow')
|
|
|
|
self.request.sendall(json.dumps({'return': 'success'}))
|
|
|
|
if data['action'] == 'publickey':
|
|
|
|
if not gfs.is_dir('/root/.ssh'):
|
2015-06-08 19:45:56 +00:00
|
|
|
gfs.mkdir('/root/.ssh')
|
2020-03-16 13:59:45 +00:00
|
|
|
gfs.chmod(700, "/root/.ssh")
|
2015-05-27 13:23:49 +00:00
|
|
|
gfs.write('/root/.ssh/authorized_keys', data['key'])
|
2020-03-16 13:59:45 +00:00
|
|
|
gfs.chmod(600, '/root/.ssh/authorized_keys')
|
2015-05-27 13:23:49 +00:00
|
|
|
self.request.sendall(json.dumps({'return': 'success'}))
|
|
|
|
gfs.umount(part)
|
2015-05-21 08:52:10 +00:00
|
|
|
except RuntimeError:
|
|
|
|
pass
|
|
|
|
gfs.shutdown()
|
|
|
|
gfs.close()
|
2020-07-13 09:33:09 +00:00
|
|
|
except Exception as err:
|
|
|
|
self.request.sendall(bytes(json.dumps({'return': 'error', 'message': str(err)}).encode()))
|
2015-05-21 08:52:10 +00:00
|
|
|
|
2020-01-24 07:07:45 +00:00
|
|
|
|
2015-05-21 08:52:10 +00:00
|
|
|
server = MyTCPServer((ADDRESS, PORT), MyTCPServerHandler)
|
|
|
|
server.serve_forever()
|