Passer une commande qui exige une authentification :
Un exemple de Maître Cogier :
Les déclarations, toutes en mémoire
1
2
3
4
5
6
7
8
9
10
11
12
| ' Gambas class file
'Updated by Charlie Reinl to cover all languages. (29/09/2019) 'Aktualisiert von Charlie Reinl, um alle Sprachen abzudecken. (29/09/2019)
CREATE STATIC '<-----------------------------------------------------------------' Store Class in memory
siCount AS SHORT ' Counter sOutput AS STRING ' To return the output of a command sCommand AS STRING ' Command to run 'sPassword As String ' Stores the password for one pass only STATIC sPassword AS STRING '' Stores the password for the whole time the program is running
|
La commande qui doit être authentifiée
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| PUBLIC SUB Command(Command AS STRING) AS STRING ' Takes the Command and returns the output of the command
IF NOT Command THEN RETURN ' If there is no Command then get out of here! sCommand = Command ' Move the Command to a global variable
IF sPassword THEN ' If the password has been stored then.. ButtonAuthenticate_Click ' Click the 'Authenticate' button RETURN sOutput ' Return the Command's output END IF
LabelHeader.text = ("Authentication is required to run this program\n'") & Command & "'" ' Add a notice and the name of the command to be run
CheckCapsLock ' Check if the Caps Lock is on ME.Showmodal ' Show the Authentication Form RETURN sOutput ' Return the Command's output
END
|
Quand le bouton authentification est cliqué après avoir rentré son mot de passe
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
| PUBLIC SUB ButtonAuthenticate_Click() ' When the the Authenticate button is clicked..
DIM sPass AS STRING ' To store the password after the 'Shell$' command DIM sChkOutput AS STRING ' just the checker response
LabelFail.Visible = FALSE ' Hide the LabelFail WAIT ' Once around the Event Loop IF NOT sPassword THEN sPassword = TextBoxPassword.Text ' If there is no password stored then take it from TextBox sPass = Shell$(sPassword) ' The caters for odd characters in the password
IF InStr(sCommand, "2>&1") THEN ' If the error output has been requested then TRY SHELL "echo " & sPass & " | sudo -S " & sCommand TO sOutput ' Shell the Command with the password to sOutput ELSE ' Else.. TRY SHELL "echo " & sPass & " | sudo -S " & sCommand & " 2>&1" TO sOutput ' Shell the Command with the password and output the Error string END IF
TRY SHELL "sudo -K;echo " & sPass & "|sudo -S -k whoami 2>&1" TO sChkOutput ' Shell the Command with the password and outputs the owner
IF NOT InStr(sChkOutput, ("root")) THEN ' must be 'root', if not, then the password is incorrect ' does not work in other languages ' If InStr(sOutput, ("incorrect password attempt")) Then ' If the password is incorrect then.. sPassword = "" ' Clear sPassword TextBoxPassword.text = "" ' Clear the TextBox TextBoxPassword.SetFocus ' Put the cursor in the TexBox Timer1.Start ' Start the Timer ELSE ' Else.. TRY SHELL "echo " & sPass & " | sudo -S " & sCommand TO sOutput ' The password is good so run the command to get the standard output ME.Close ' Close the Form END IF
END
|
Quand le bouton cancel (annuler) est cliqué
1
2
3
4
5
6
| PUBLIC SUB ButtonCancel_Click() ' If the Cancel button is clicked..
ME.Close ' Close the Form RETURN ' Return
END
|
Ce que fait le Timer (ici, il secoue la case en cas de mauvaise entrée du mot de passe)
1
2
3
4
5
6
7
8
9
10
11
12
| PUBLIC SUB Timer1_Timer() ' Timer to 'Shake' the Form when an incorrect password is entered
IF Even(ME.X) THEN ME.x += 9 ELSE ME.x -= 9 ' If the Form's X value is even then add 9 to it Else subtract 9 from the X value LabelFail.Visible = TRUE ' Show the LabelFail WAIT ' Once around the Event Loop INC siCount ' Increase the counter IF siCount > 75 THEN ' If the counter is greater than 75 then.. Timer1.Stop ' Stop the Timer (and therefore the 'Shaking') siCount = 0 ' Reset the counter END IF
END
|
Pour attraper le fait que l'utilisateur tape sur la touche Entrée
1
2
3
4
5
6
| PUBLIC SUB TextBoxPassword_KeyRelease() ' To catch the user pressing [Enter] or [Return]
IF Key.Text = "\r" THEN ButtonAuthenticate_Click ' If the user pressed [Enter] or [Return] then Click the 'Authenticate' button CheckCapsLock ' Check the Caps Lock
END
|
Vérification de l'état du bouton des majuscules
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| PUBLIC SUB CheckCapsLock() ' To check the Caps Lock state
DIM sState AS STRING ' To store the 'Shell' output DIM siCap AS SHORT ' To store the 'Value' of Caps Lock state
SHELL "xset q | grep 'LED mask'" TO sState ' Shell command. Result is similar to: - 'auto repeat: on key click percent: 0 LED mask: 00000003'
siCap = Right(Bin(Right(Trim(sState), 1)), 1) ' Get the last binary value, see alternative code below
IF siCap THEN ' If siCap is not 0 then.. LabelCaps.text = ("Caps Lock is ON") ' Add a message to the Form indicating the Caps Lock is on ELSE ' siCap must be 0 LabelCaps.text = "" ' Clear the Caps Lock message ENDIF
END
|
Plusieurs commentaires de Maître Cogier
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| 'sState = Trim(sState) ' Remove any 'white space' from the ends of the string = 'auto repeat: on key click percent: 0 LED mask: 00000003' 'siCap = Right(sState, 1) ' Get the last charater in the string = 3 'siCap = Bin(siCap) ' Get the binary value of siCap = 11 'siCap = Right(Str(siCap), 1) ' Get the last bianary value = 1 (The last binary is either 0 - Caps Lock Off - or 1 - Caps Lock On)
' Usage ' Authenticate.Command("cp ~/file.txt /usr/bin/file.txt") ' sString = Authenticate.Command("fdisk -l") ' ' Comments or bugs to bugs@cogier.com ' Sudo solution ' The program includes a Class that you can use to pass commands that require authentication (a password to run) ' ' Copyright (C) Charlie Ogier 2018
|
Donc pour appeler ce contrôle d'identification :
1
| Authenticate.Command("La Commande Voulue")
|
Visuellement dans l'IDE :
On peut, bien sûr ajouter un terminal dans cette form pour voir les sorties des commandes.
Il suffit de détourner les sorties vers ce terminal au lieu de la sortie standard.
Visuellement dans le programme :
Où l'on voit que le LabelHeader s'espatare (s'étale) selon ses besoins.
L'exemple complet est téléchargeable à partir de la logithèque de Gambas,
accessible dans l'IDE de Gambas en affichant les filtres et en tapant Cogier dans les Étiquettes puis en validant votre choix.
====================
Commandes utilisées :
Static
Instr
Shell
Wait
Timer
Inc
Shell$ ====================
Navigation :
<-- Liens du Wiki : <--<-- Accueil du WIKI : <-- ====================
Documentation :
====================