@cryptotaxi247 / netdata-1 / commits / c35149a3b

[python.d/samba] Only use sudo when not running as root user (#9038)

Dries Michiels committed May 14, 2020 at 16:20 UTC c35149a3b44c6ef6a4c8c71b5e390700076c2884
1 file changed +9 -6
collectors/python.d.plugin/samba/samba.chart.py
+9 -6
@@ -17,6 +17,7 @@
17 # (like find and notify... good examples).
18
19 import re
20 +import os
21
22 from bases.FrameworkServices.ExecutableService import ExecutableService
23 from bases.collection import find_binary
@@ -107,23 +108,25 @@ class Service(ExecutableService):
108 self.rgx_smb2 = re.compile(r'(smb2_[^:]+|syscall_.*file_bytes):\s+(\d+)')
109
110 def check(self):
110 - sudo_binary = find_binary(SUDO)
111 - if not sudo_binary:
112 - self.error("can't locate '{0}' binary".format(SUDO))
113 - return False
114 -
111 smbstatus_binary = find_binary(SMBSTATUS)
112 if not smbstatus_binary:
113 self.error("can't locate '{0}' binary".format(SMBSTATUS))
114 return False
115
116 + if os.getuid() == 0:
117 + self.command = ' '.join([smbstatus_binary, '-P'])
118 + return ExecutableService.check(self)
119 +
120 + sudo_binary = find_binary(SUDO)
121 + if not sudo_binary:
122 + self.error("can't locate '{0}' binary".format(SUDO))
123 + return False
124 command = [sudo_binary, '-n', '-l', smbstatus_binary, '-P']
125 smbstatus = '{0} -P'.format(smbstatus_binary)
126 allowed = self._get_raw_data(command=command)
127 if not (allowed and allowed[0].strip() == smbstatus):
128 self.error("not allowed to run sudo for command '{0}'".format(smbstatus))
129 return False
126 -
130 self.command = ' '.join([sudo_binary, '-n', smbstatus_binary, '-P'])
131 return ExecutableService.check(self)
132