• Welcome, Guest. Please login.
 
August 20, 2019, 08:34:53 pm

News:

Welcome to the SQLitening support forums!


How to unlock a locked database?

Started by Fim, July 17, 2017, 08:32:55 am

Previous topic - Next topic

Fim

Is there any way to unlock a locked database from a client besides going to the server and stopping the service?
/Fim W.
Fim W

cj

July 17, 2017, 03:06:07 pm #1 Last Edit: July 17, 2017, 08:15:02 pm by cj
I will delete this post after you respond as it is probably a security risk.


Fim

Fim W

Bern Ertl

I don't know what cj's solution was, but you might be able to use slKill to terminate the client connection to the server and that should remove any locks they have on the database.

http://sqlitening.com/support/index.php?topic=9087.msg23797#msg23797

cj

July 19, 2017, 12:41:49 pm #4 Last Edit: July 19, 2017, 12:43:29 pm by cj
I sent the command to kill the tcpfile number(s) using the same code as SQLiteningServerAdmin.
Users should not have this ability.   Security is so weak now.

Fim

Fim W

Bern Ertl

Quote from: cj on July 19, 2017, 12:41:49 pm...
Users should not have this ability.   ...


Well, they do need to know the tcpfilenumber to use slKill and that's not available to them unless you expose it for them.

cj

I agree.  I'll work on this a bit and leave security to programmer.
I'm currently just using remote desktop clients and allowing more on the server.
I'll post something within the hour with a dialog.

cj

I'm posting this code anyway and may get back to it with Bern's fixes.
It might work with a single connection, but not reliable.
#PBFORMS CREATED V2.01
#COMPILE EXE
#DIM ALL
%Port = 12345
$Host = "255.255.255.255"
%ReqAdmin = 23

#PBFORMS BEGIN INCLUDES
%USEMACROS = 1
#INCLUDE ONCE "WIN32API.INC"
#PBFORMS END INCLUDES
#PBFORMS BEGIN CONSTANTS
%LISTBOX     = 1001
%LABEL       = 1002
#PBFORMS END CONSTANTS
#PBFORMS DECLARATIONS
FUNCTION PBMAIN()
ShowDIALOG1 %HWND_DESKTOP
END FUNCTION

CALLBACK FUNCTION ShowDIALOG1Proc()
  SELECT CASE AS LONG CB.MSG
    CASE %WM_INITDIALOG
     StartHere CB.HNDL
    CASE %WM_NCACTIVATE
      STATIC hWndSaveFocus AS DWORD
      IF ISFALSE CB.WPARAM THEN
        ' Save control focus
        hWndSaveFocus = GetFocus()
      ELSEIF hWndSaveFocus THEN
        ' Restore control focus
        SetFocus(hWndSaveFocus)
        hWndSaveFocus = 0
      END IF
    CASE %WM_COMMAND
      ' Process control notifications
      SELECT CASE AS LONG CB.CTL
        LOCAL s AS STRING, ItemNumber,TcpFileNumber,x AS LONG
        CASE %LISTBOX
          LISTBOX GET TEXT CB.HNDL,%LISTBOX TO s
          LISTBOX GET SELECT CB.HNDL,%ListBox TO ItemNumber
          TcpfileNumber = VAL(s)
          IF TcpFileNumber THEN
            SendReceiveAdminMessage(3, MKDWD$(TcpFileNumber), "") 'send message to kill tcp handle
            s = "Sent kill request:  " + s
            LISTBOX SET TEXT CB.HNDL,%ListBox,ItemNumber,s
          END IF

      END SELECT
  END SELECT
END FUNCTION

FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
  LOCAL lRslt AS LONG
#PBFORMS BEGIN DIALOG %IDD_DIALOG1->->
  LOCAL hDlg   AS DWORD
  LOCAL hFont1 AS DWORD

  DIALOG NEW hParent, "Kill Connection", 227, 226, 382, 150, %WS_POPUP OR _
    %WS_BORDER OR %WS_DLGFRAME OR %WS_CAPTION OR %WS_SYSMENU OR _
    %WS_MINIMIZEBOX OR %WS_CLIPSIBLINGS OR %WS_VISIBLE OR %DS_MODALFRAME OR _
    %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT, %WS_EX_CONTROLPARENT OR _
    %WS_EX_LEFT OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg
  CONTROL ADD LISTBOX, hDlg, %LISTBOX, , 10, 30, 360, 110, %WS_CHILD OR _
    %WS_VISIBLE OR %WS_HSCROLL OR %WS_VSCROLL OR %LBS_NOTIFY, _
    %WS_EX_CLIENTEDGE OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR _
    %WS_EX_RIGHTSCROLLBAR
  CONTROL ADD LABEL,   hDlg, %LABEL, "Click line to send kill request", 15, _
    10, 245, 15

  FONT NEW "MS Sans Serif", 12, 1, %ANSI_CHARSET TO hFont1

  CONTROL SET FONT hDlg, %LABEL, hFont1
#PBFORMS END DIALOG
  DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
#PBFORMS BEGIN CLEANUP %IDD_DIALOG1
  FONT END hFont1
#PBFORMS END CLEANUP
  FUNCTION = lRslt
END FUNCTION
FUNCTION StartHere(hDlg AS DWORD) AS LONG
LOCAL x,Items AS LONG
LOCAL s AS STRING
SendReceiveAdminMessage(2, "", s)
IF LEN(s) = 0 THEN LISTBOX ADD hdlg, %ListBox,"No connections, please end":EXIT FUNCTION
REPLACE $VT WITH $CR     IN s
REPLACE "," WITH " "     IN s
REPLACE $BS WITH ",    " IN s
Items = PARSECOUNT(s,$CR)
FOR x = 1 TO Items
   LISTBOX ADD hDlg,%ListBox,PARSE$(s,$CR,x)
NEXT
END FUNCTION

FUNCTION SendReceiveAdminMessage(BYVAL rlRequest AS LONG, _
                                 rsSendMessage AS STRING, _
                                 wsReceiveMessage AS STRING) AS LONG
   LOCAL llA, llB AS LONG
   LOCAL llFileNumber AS LONG
   LOCAL lsA AS STRING
   LOCAL lsDataIn AS STRING
   DIM lsaDataIn(0 TO 4) AS STRING

   ' Connect
   llFileNumber = FREEFILE
   TCP OPEN PORT %Port AT $Host AS llFileNumber TIMEOUT 1000
   IF ERR THEN lsA = "Couldn't connect." :GOTO ErrorOccured

   TCP SEND llFileNumber, MKDWD$(15 + LEN(rsSendMessage)) & MKBYT$(0) & MKI$(%reqAdmin) & MKDWD$(rlRequest) & MKDWD$(0) & rsSendMessage
   IF ERR THEN lsA = "Couldn't send." : GOTO ErrorOccured

   ' Receive first chunk of data
   DO
      SLEEP 15
      TCP RECV llFileNumber, 1024 * 32, lsaDataIn(0)
   LOOP WHILE ISFALSE LEN(lsaDataIn(0)) AND ISFALSE ERR AND ISFALSE EOF(llFileNumber)

   ' Receive any remaining chunks
   llA = CVDWD(lsaDataIn(0))
   IF LEN(lsaDataIn(0)) < llA THEN

      ' There is more data
      llA = llA - LEN(lsaDataIn(0))
      DO
         INCR llB
         IF llB > UBOUND(lsaDataIn) THEN REDIM PRESERVE lsaDataIn(0 TO llB + 5)
         TCP RECV llFileNumber, llA, lsaDataIn(llB)
         llA = llA - LEN(lsaDataIn(llB))
      LOOP WHILE llA > 0 AND LEN(lsaDataIn(llB))
      IF llA <> 0 THEN lsA = "Received wrong length." : GOTO ErrorOccured

   ELSEIF ISFALSE llA THEN
      lsA = "Received nothing." : GOTO ErrorOccured
   END IF
   IF ERR THEN lsA = "Couldn't receive. Err=" & ERROR$ : GOTO ErrorOccured       ' Could not recieve request  Probably not connected
   lsDataIn = JOIN$(lsaDataIn(), "")
   FUNCTION = CVI(lsDataIn, 6)
   wsReceiveMessage = MID$(lsDataIn, 12)
   EXIT FUNCTION
   ErrorOccured:
   MSGBOX lsA,,FUNCNAME$
END FUNCTION