[c3e7c06] | 1 | # -*- coding: utf-8 -*- |
---|
| 2 | # |
---|
| 3 | # Copyright (c) 2014 Virtual Cable S.L. |
---|
| 4 | # All rights reserved. |
---|
| 5 | # |
---|
| 6 | # Redistribution and use in source and binary forms, with or without modification, |
---|
| 7 | # are permitted provided that the following conditions are met: |
---|
| 8 | # |
---|
| 9 | # * Redistributions of source code must retain the above copyright notice, |
---|
| 10 | # this list of conditions and the following disclaimer. |
---|
| 11 | # * Redistributions in binary form must reproduce the above copyright notice, |
---|
| 12 | # this list of conditions and the following disclaimer in the documentation |
---|
| 13 | # and/or other materials provided with the distribution. |
---|
| 14 | # * Neither the name of Virtual Cable S.L. nor the names of its contributors |
---|
| 15 | # may be used to endorse or promote products derived from this software |
---|
| 16 | # without specific prior written permission. |
---|
| 17 | # |
---|
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
| 19 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 20 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
| 21 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
---|
| 22 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
| 23 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
---|
| 24 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
---|
| 25 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
---|
| 26 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
| 27 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 28 | |
---|
| 29 | ''' |
---|
| 30 | @author: Adolfo Gómez, dkmaster at dkmon dot com |
---|
| 31 | ''' |
---|
| 32 | from __future__ import unicode_literals |
---|
| 33 | |
---|
| 34 | import socket |
---|
| 35 | import platform |
---|
| 36 | import fcntl |
---|
| 37 | import os |
---|
| 38 | import ctypes # @UnusedImport |
---|
| 39 | import ctypes.util |
---|
| 40 | import subprocess |
---|
| 41 | import struct |
---|
| 42 | import array |
---|
| 43 | import six |
---|
| 44 | from opengnsys import utils |
---|
| 45 | from .renamer import rename |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | def _getMacAddr(ifname): |
---|
| 49 | ''' |
---|
| 50 | Returns the mac address of an interface |
---|
| 51 | Mac is returned as unicode utf-8 encoded |
---|
| 52 | ''' |
---|
| 53 | if isinstance(ifname, list): |
---|
| 54 | return dict([(name, _getMacAddr(name)) for name in ifname]) |
---|
| 55 | if isinstance(ifname, six.text_type): |
---|
| 56 | ifname = ifname.encode('utf-8') # If unicode, convert to bytes (or str in python 2.7) |
---|
| 57 | try: |
---|
| 58 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
---|
| 59 | info = bytearray(fcntl.ioctl(s.fileno(), 0x8927, struct.pack(str('256s'), ifname[:15]))) |
---|
| 60 | return six.text_type(''.join(['%02x:' % char for char in info[18:24]])[:-1]) |
---|
| 61 | except Exception: |
---|
| 62 | return None |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | def _getIpAddr(ifname): |
---|
| 66 | ''' |
---|
| 67 | Returns the ip address of an interface |
---|
| 68 | Ip is returned as unicode utf-8 encoded |
---|
| 69 | ''' |
---|
| 70 | if isinstance(ifname, list): |
---|
| 71 | return dict([(name, _getIpAddr(name)) for name in ifname]) |
---|
| 72 | if isinstance(ifname, six.text_type): |
---|
| 73 | ifname = ifname.encode('utf-8') # If unicode, convert to bytes (or str in python 2.7) |
---|
| 74 | try: |
---|
| 75 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
---|
| 76 | return six.text_type(socket.inet_ntoa(fcntl.ioctl( |
---|
| 77 | s.fileno(), |
---|
| 78 | 0x8915, # SIOCGIFADDR |
---|
| 79 | struct.pack(str('256s'), ifname[:15]) |
---|
| 80 | )[20:24])) |
---|
| 81 | except Exception: |
---|
| 82 | return None |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | def _getInterfaces(): |
---|
| 86 | ''' |
---|
| 87 | Returns a list of interfaces names coded in utf-8 |
---|
| 88 | ''' |
---|
| 89 | max_possible = 128 # arbitrary. raise if needed. |
---|
| 90 | space = max_possible * 16 |
---|
| 91 | if platform.architecture()[0] == '32bit': |
---|
| 92 | offset, length = 32, 32 |
---|
| 93 | elif platform.architecture()[0] == '64bit': |
---|
| 94 | offset, length = 16, 40 |
---|
| 95 | else: |
---|
| 96 | raise OSError('Unknown arquitecture {0}'.format(platform.architecture()[0])) |
---|
| 97 | |
---|
| 98 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
---|
| 99 | names = array.array(str('B'), b'\0' * space) |
---|
| 100 | outbytes = struct.unpack(str('iL'), fcntl.ioctl( |
---|
| 101 | s.fileno(), |
---|
| 102 | 0x8912, # SIOCGIFCONF |
---|
| 103 | struct.pack(str('iL'), space, names.buffer_info()[0]) |
---|
| 104 | ))[0] |
---|
| 105 | namestr = names.tostring() |
---|
| 106 | # return namestr, outbytes |
---|
| 107 | return [namestr[i:i + offset].split(b'\0', 1)[0].decode('utf-8') for i in range(0, outbytes, length)] |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | def _getIpAndMac(ifname): |
---|
| 111 | ip, mac = _getIpAddr(ifname), _getMacAddr(ifname) |
---|
| 112 | return (ip, mac) |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | def getComputerName(): |
---|
| 116 | ''' |
---|
| 117 | Returns computer name, with no domain |
---|
| 118 | ''' |
---|
| 119 | return socket.gethostname().split('.')[0] |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | def getNetworkInfo(): |
---|
| 123 | ''' |
---|
| 124 | Obtains a list of network interfaces |
---|
| 125 | @return: A "generator" of elements, that are dict-as-object, with this elements: |
---|
| 126 | name: Name of the interface |
---|
| 127 | mac: mac of the interface |
---|
| 128 | ip: ip of the interface |
---|
| 129 | ''' |
---|
| 130 | for ifname in _getInterfaces(): |
---|
| 131 | ip, mac = _getIpAndMac(ifname) |
---|
| 132 | if mac != '00:00:00:00:00:00': # Skips local interfaces |
---|
| 133 | yield utils.Bunch(name=ifname, mac=mac, ip=ip) |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | def getDomainName(): |
---|
| 137 | return '' |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | def getLinuxVersion(): |
---|
| 141 | lv = platform.linux_distribution() |
---|
| 142 | return lv[0] + ', ' + lv[1] |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | def reboot(flags=0): |
---|
| 146 | ''' |
---|
| 147 | Simple reboot using os command |
---|
| 148 | ''' |
---|
| 149 | # Workaround for dummy thread |
---|
| 150 | if six.PY3 is False: |
---|
| 151 | import threading |
---|
| 152 | threading._DummyThread._Thread__stop = lambda x: 42 |
---|
| 153 | |
---|
| 154 | # Check for OpenGnsys Client or GNU/Linux distribution. |
---|
| 155 | if os.path.exists('/scripts/oginit'): |
---|
| 156 | subprocess.call('source /opt/opengnsys/etc/preinit/loadenviron.sh; /opt/opengnsys/scripts/reboot', shell=True) |
---|
| 157 | else: |
---|
| 158 | subprocess.call(['/sbin/reboot']) |
---|
| 159 | |
---|
| 160 | def poweroff(flags=0): |
---|
| 161 | ''' |
---|
| 162 | Simple poweroff using os command |
---|
| 163 | ''' |
---|
| 164 | # Workaround for dummy thread |
---|
| 165 | if six.PY3 is False: |
---|
| 166 | import threading |
---|
| 167 | threading._DummyThread._Thread__stop = lambda x: 42 |
---|
| 168 | |
---|
| 169 | # Check for OpenGnsys Client or GNU/Linux distribution. |
---|
| 170 | if os.path.exists('/scripts/oginit'): |
---|
| 171 | subprocess.call('source /opt/opengnsys/etc/preinit/loadenviron.sh; /opt/opengnsys/scripts/poweroff', shell=True) |
---|
| 172 | else: |
---|
| 173 | subprocess.call(['/sbin/poweroff']) |
---|
| 174 | |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | def logoff(): |
---|
| 178 | ''' |
---|
| 179 | Kills all curent user processes, which must send a logogof |
---|
| 180 | caveat: If the user has other sessions, will also disconnect from them |
---|
| 181 | ''' |
---|
| 182 | # Workaround for dummy thread |
---|
| 183 | if six.PY3 is False: |
---|
| 184 | import threading |
---|
| 185 | threading._DummyThread._Thread__stop = lambda x: 42 |
---|
| 186 | |
---|
| 187 | subprocess.call(['/usr/bin/pkill', '-u', os.environ['USER']]) |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | def renameComputer(newName): |
---|
| 191 | rename(newName) |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | def joinDomain(domain, ou, account, password, executeInOneStep=False): |
---|
| 195 | pass |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | def changeUserPassword(user, oldPassword, newPassword): |
---|
| 199 | ''' |
---|
| 200 | Simple password change for user using command line |
---|
| 201 | ''' |
---|
| 202 | os.system('echo "{1}\n{1}" | /usr/bin/passwd {0} 2> /dev/null'.format(user, newPassword)) |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | class XScreenSaverInfo(ctypes.Structure): |
---|
| 206 | _fields_ = [('window', ctypes.c_long), |
---|
| 207 | ('state', ctypes.c_int), |
---|
| 208 | ('kind', ctypes.c_int), |
---|
| 209 | ('til_or_since', ctypes.c_ulong), |
---|
| 210 | ('idle', ctypes.c_ulong), |
---|
| 211 | ('eventMask', ctypes.c_ulong)] |
---|
| 212 | |
---|
| 213 | # Initialize xlib & xss |
---|
| 214 | try: |
---|
| 215 | xlibPath = ctypes.util.find_library('X11') |
---|
| 216 | xssPath = ctypes.util.find_library('Xss') |
---|
| 217 | xlib = ctypes.cdll.LoadLibrary(xlibPath) |
---|
| 218 | xss = ctypes.cdll.LoadLibrary(xssPath) |
---|
| 219 | |
---|
| 220 | # Fix result type to XScreenSaverInfo Structure |
---|
| 221 | xss.XScreenSaverQueryExtension.restype = ctypes.c_int |
---|
| 222 | xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo) # Result in a XScreenSaverInfo structure |
---|
| 223 | except Exception: # Libraries not accesible, not found or whatever.. |
---|
| 224 | xlib = xss = None |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | def initIdleDuration(atLeastSeconds): |
---|
| 228 | ''' |
---|
| 229 | On linux we set the screensaver to at least required seconds, or we never will get "idle" |
---|
| 230 | ''' |
---|
| 231 | # Workaround for dummy thread |
---|
| 232 | if six.PY3 is False: |
---|
| 233 | import threading |
---|
| 234 | threading._DummyThread._Thread__stop = lambda x: 42 |
---|
| 235 | |
---|
| 236 | subprocess.call(['/usr/bin/xset', 's', '{}'.format(atLeastSeconds + 30)]) |
---|
| 237 | # And now reset it |
---|
| 238 | subprocess.call(['/usr/bin/xset', 's', 'reset']) |
---|
| 239 | |
---|
| 240 | |
---|
| 241 | def getIdleDuration(): |
---|
| 242 | ''' |
---|
| 243 | Returns idle duration, in seconds |
---|
| 244 | ''' |
---|
| 245 | if xlib is None or xss is None: |
---|
| 246 | return 0 # Libraries not available |
---|
| 247 | |
---|
| 248 | # production code might want to not hardcode the offset 16... |
---|
| 249 | display = xlib.XOpenDisplay(None) |
---|
| 250 | |
---|
| 251 | event_base = ctypes.c_int() |
---|
| 252 | error_base = ctypes.c_int() |
---|
| 253 | |
---|
| 254 | available = xss.XScreenSaverQueryExtension(display, ctypes.byref(event_base), ctypes.byref(error_base)) |
---|
| 255 | if available != 1: |
---|
| 256 | return 0 # No screen saver is available, no way of getting idle |
---|
| 257 | |
---|
| 258 | info = xss.XScreenSaverAllocInfo() |
---|
| 259 | xss.XScreenSaverQueryInfo(display, xlib.XDefaultRootWindow(display), info) |
---|
| 260 | |
---|
| 261 | if info.contents.state != 0: |
---|
| 262 | return 3600 * 100 * 1000 # If screen saver is active, return a high enough value |
---|
| 263 | |
---|
| 264 | return info.contents.idle / 1000.0 |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | def getCurrentUser(): |
---|
| 268 | ''' |
---|
| 269 | Returns current logged in user |
---|
| 270 | ''' |
---|
| 271 | return os.environ['USER'] |
---|