fixing uds actor service for windows

This commit is contained in:
Adolfo Gómez 2019-12-03 17:43:19 +01:00
parent aabb08cad4
commit 72efa3221f
6 changed files with 33 additions and 26 deletions

View File

@ -51,7 +51,7 @@ class UDSActorSvc(daemon.Daemon, CommonService):
signal.signal(signal.SIGINT, self.markForExit) signal.signal(signal.SIGINT, self.markForExit)
signal.signal(signal.SIGTERM, self.markForExit) signal.signal(signal.SIGTERM, self.markForExit)
def markForExit(self, signum, frame) -> None: def markForExit(self, signum, frame) -> None: # pylint: disable=unused-argument
self._isAlive = False self._isAlive = False
def joinDomain( # pylint: disable=unused-argument, too-many-arguments def joinDomain( # pylint: disable=unused-argument, too-many-arguments

View File

@ -25,10 +25,10 @@
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
''' '''
@author: Adolfo Gómez, dkmaster at dkmon dot com @author: Adolfo Gómez, dkmaster at dkmon dot com
''' '''
# pylint: disable=invalid-name
import traceback import traceback
import sys import sys
import typing import typing

View File

@ -55,7 +55,7 @@ from .http import registry, server
# else: # else:
# logger.setLevel(20000) # logger.setLevel(20000)
class CommonService: class CommonService: # pylint: disable=too-many-instance-attributes
_isAlive: bool = True _isAlive: bool = True
_rebootRequested: bool = False _rebootRequested: bool = False
_loggedIn = False _loggedIn = False
@ -87,8 +87,11 @@ class CommonService:
self._certificate = types.CertificateInfoType('', '', '') self._certificate = types.CertificateInfoType('', '', '')
self._http = None self._http = None
# Initialzies loglevel # Initialzies loglevel and serviceLogger
logger.setLevel(self._cfg.log_level * 10000) logger.setLevel(self._cfg.log_level * 10000)
# If windows, enable service logger
if logger.localLogger.windows:
logger.localLogger.serviceLogger = True
socket.setdefaulttimeout(20) socket.setdefaulttimeout(20)

View File

@ -184,7 +184,7 @@ def changeUserPassword(user: str, oldPassword: str, newPassword: str) -> None:
# Try to set new password "a las bravas", ignoring old one. This will not work with domain users # Try to set new password "a las bravas", ignoring old one. This will not work with domain users
res = win32net.NetUserSetInfo(None, user, 1003, {'password': newPassword}) res = win32net.NetUserSetInfo(None, user, 1003, {'password': newPassword})
if res != 0: if res:
# Log the error, and raise exception to parent # Log the error, and raise exception to parent
error = getErrorMessage(res) error = getErrorMessage(res)
raise Exception('Error changing password for user {}: {} {}'.format(user, res, error)) raise Exception('Error changing password for user {}: {} {}'.format(user, res, error))

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# Copyright (c) 2014 Virtual Cable S.L. # Copyright (c) 2019 Virtual Cable S.L.
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without modification, # Redistribution and use in source and binary forms, with or without modification,
@ -29,9 +29,18 @@
@author: Adolfo Gómez, dkmaster at dkmon dot com @author: Adolfo Gómez, dkmaster at dkmon dot com
''' '''
# pylint: disable=invalid-name # pylint: disable=invalid-name
import sys
import win32serviceutil import win32serviceutil
import servicemanager
import win32timezone # pylint: disable=unused-import
from .service import UDSActorSvc from .service import UDSActorSvc
def run() -> None: def run() -> None:
win32serviceutil.HandleCommandLine(UDSActorSvc) if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(UDSActorSvc)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(UDSActorSvc)

View File

@ -30,9 +30,6 @@
''' '''
# pylint: disable=invalid-name # pylint: disable=invalid-name
import struct import struct
import subprocess
import os
import stat
import typing import typing
import win32serviceutil import win32serviceutil
@ -84,7 +81,7 @@ class UDSActorSvc(win32serviceutil.ServiceFramework, CommonService):
def SvcStop(self) -> None: def SvcStop(self) -> None:
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self._isAlive = False self._isAlive = False
win32event.SetEvent(self.hWaitStop) win32event.SetEvent(self._hWaitStop)
SvcShutdown = SvcStop SvcShutdown = SvcStop
@ -93,7 +90,7 @@ class UDSActorSvc(win32serviceutil.ServiceFramework, CommonService):
super().notifyStop() super().notifyStop()
def doWait(self, miliseconds: int) -> None: def doWait(self, miliseconds: int) -> None:
win32event.WaitForSingleObject(self.hWaitStop, miliseconds) win32event.WaitForSingleObject(self._hWaitStop, miliseconds)
def oneStepJoin(self, name: str, domain: str, ou: str, account: str, password: str) -> None: def oneStepJoin(self, name: str, domain: str, ou: str, account: str, password: str) -> None:
''' '''
@ -186,7 +183,7 @@ class UDSActorSvc(win32serviceutil.ServiceFramework, CommonService):
""" """
# Compose packet for ov # Compose packet for ov
usernameBytes = username.encode() usernameBytes = username.encode()
passwordBytes = username.encode() passwordBytes = password.encode()
packet = struct.pack('!I', len(usernameBytes)) + usernameBytes + struct.pack('!I', len(passwordBytes)) + passwordBytes packet = struct.pack('!I', len(usernameBytes)) + usernameBytes + struct.pack('!I', len(passwordBytes)) + passwordBytes
# Send packet with username/password to ov pipe # Send packet with username/password to ov pipe
operations.writeToPipe("\\\\.\\pipe\\VDSMDPipe", packet, True) operations.writeToPipe("\\\\.\\pipe\\VDSMDPipe", packet, True)
@ -211,27 +208,26 @@ class UDSActorSvc(win32serviceutil.ServiceFramework, CommonService):
''' '''
Main service loop Main service loop
''' '''
logger.debug('running SvcDoRun')
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, '')) servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, ''))
# call the CoInitialize to allow the registration to run in an other # call the CoInitialize to allow the registration to run in an other
# thread # thread
logger.debug('Initializing com...') logger.debug('Initializing coms')
pythoncom.CoInitialize() pythoncom.CoInitialize() # pylint: disable=no-member
if not self.initialize(): if not self.initialize():
self.finish() self.finish()
win32event.WaitForSingleObject(self.hWaitStop, 5000) win32event.WaitForSingleObject(self._hWaitStop, 5000)
return # Stop daemon if initializes told to do so return # Stop daemon if initializes told to do so
# ******************************** # # ********************************
# * Registers SENS subscriptions * # # * Registers SENS subscriptions *
# ******************************** # # ********************************
logevent('Registering ISensLogon') logger.debug('Registering isens logon')
subscription_guid = '{41099152-498E-11E4-8FD3-10FEED05884B}' subscription_guid = '{41099152-498E-11E4-8FD3-10FEED05884B}'
sl = SensLogon(self) sl = SensLogon(self)
subscription_interface = pythoncom.WrapObject(sl) subscription_interface = pythoncom.WrapObject(sl) # pylint: disable=no-member
event_system = win32com.client.Dispatch(PROGID_EventSystem) event_system = win32com.client.Dispatch(PROGID_EventSystem)
@ -253,14 +249,13 @@ class UDSActorSvc(win32serviceutil.ServiceFramework, CommonService):
# ********************* # *********************
# * Main Service loop * # * Main Service loop *
# ********************* # *********************
# Counter used to check ip changes only once every 10 seconds, for # Counter used to check ip changes only once every 10 seconds
# example
counter = 0 counter = 0
while self._isAlive: while self._isAlive:
counter += 1 counter += 1
try: try:
# Process SENS messages, This will be a bit asyncronous (1 second delay) # Process SENS messages, This will be a bit asyncronous (1 second delay)
pythoncom.PumpWaitingMessages() pythoncom.PumpWaitingMessages() # pylint: disable=no-member
if counter >= 10: # Once every 10 seconds if counter >= 10: # Once every 10 seconds
counter = 0 counter = 0
@ -271,7 +266,7 @@ class UDSActorSvc(win32serviceutil.ServiceFramework, CommonService):
# Continue after a while... # Continue after a while...
# In milliseconds, will break if event hWaitStop is set # In milliseconds, will break if event hWaitStop is set
win32event.WaitForSingleObject(self.hWaitStop, 1000) win32event.WaitForSingleObject(self._hWaitStop, 1000)
logger.debug('Exited main loop, deregistering SENS') logger.debug('Exited main loop, deregistering SENS')