-
Notifications
You must be signed in to change notification settings - Fork 2
/
intel-power-control-setup
executable file
·70 lines (57 loc) · 1.87 KB
/
intel-power-control-setup
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
#!/usr/bin/env python3
import os
import stat
import sys
def setup_suid(helper, statinfo):
print("Using SUID method")
if statinfo.st_uid != 0:
print("Changing ownership to root:root")
os.chown(helper, 0, 0)
if stat.S_IMODE(statinfo.st_mode) != 0o4755:
print("Setting SUID bit")
os.chmod(helper, 0o4755)
def setup_sudo(helper):
print("Using sudo method")
sudodir = "/etc/sudoers.d"
if not os.path.exists(sudodir):
print(f"{sudodir} does not exist, is sudo installed?")
sys.exit(1)
sudofile = os.path.join(sudodir, "intel-power-control")
with open(sudofile, "w") as f:
f.write(f"%sudo ALL=(ALL) NOPASSWD: {helper}")
print(
f"Successfully installed {sudofile},"
" make sure user is member of group sudo"
)
installdir = os.path.dirname(__file__)
helper = os.path.join(installdir, "intel-power-control-helper")
if not os.path.exists(helper):
print(f"Could not find intel-power-control-helper in {installdir}")
sys.exit(1)
statinfo = os.stat(helper)
if statinfo.st_uid == 0 and stat.S_IMODE(statinfo.st_mode) == 0o4755:
print("intel-power-control-helper is already set up correctly")
sys.exit(0)
if os.geteuid() != 0:
print("Please run this script with root privileges")
sys.exit(1)
dirstat = os.stat(os.path.dirname(helper))
if dirstat.st_uid != 0 or dirstat.st_gid != 0:
print(
"Installation directory does not belong to root,"
" assuming user installation"
)
setup_suid(helper, statinfo)
else:
print("Installation directory belongs to root, choose strategy:")
print("1: suid")
print("2: sudo")
ans = input("Choose 1 or 2, Ctrl-C to abort:").strip()
if ans == "1":
setup_suid(helper, statinfo)
elif ans == "2":
setup_sudo(helper)
else:
print("Aborting")
sys.exit(1)
sys.exit(0)