-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.py
More file actions
109 lines (81 loc) · 2.81 KB
/
shell.py
File metadata and controls
109 lines (81 loc) · 2.81 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
import importlib
import os
import sys
import json
from filesystem import FileSystem
import auth
import importlib.util
fs = FileSystem()
def get_installed_packages():
package_json = os.path.join("fs", "var", "packages.json")
if os.path.exists(package_json):
try:
with open(package_json, 'r') as file:
packages = json.load(file)
return set(packages.keys())
except (json.JSONDecodeError, IOError):
return set()
return set()
def is_package_available(package_name):
commands = {}
if package_name in commands:
return True
command_path = os.path.join("fs", "bin", f"{package_name}.py")
if not os.path.exists(command_path):
return False
installed_packages = get_installed_packages()
return package_name in installed_packages
def load_module(name):
module_name = f"pyos_cmd_{name}"
command_path = os.path.abspath(os.path.join("fs", "bin", f"{name}.py"))
if not os.path.exists(command_path):
return None
if module_name in sys.modules:
del sys.modules[module_name]
spec = importlib.util.spec_from_file_location(module_name, command_path)
if not spec or not spec.loader:
return None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def shell():
while True:
try:
if not auth.get_current_user():
if not auth.login():
continue
else:
continue
command = input(fs.prompt()).strip()
if not command:
continue
if command == "exit":
print("Exiting the shell.")
auth.logout()
break
try:
command = command.split()
command_name = command[0]
args = command[1:]
command_module = load_module(command_name)
if not command_module:
print(f"Unknown command: {command_name}")
continue
if not check_module_permissions(command_module):
print(f"Permission denied: {command_name}")
continue
command_module.run(args, fs)
except ModuleNotFoundError:
print(f"Unknown command: {command}")
except KeyboardInterrupt:
print("\nUse the 'exit' command to quit the shell.")
def check_module_permissions(module):
if auth.is_current_root():
return True
root_commands = {
'useradd', 'userdel'
}
command_name = module.__name__.split('.')[-1]
if command_name in root_commands:
return False
return True