Bonjour à tous,
Je suis en train de faire une petit développement en java pour checker des services Windows à distance et je suis confronté à un problème qui dure depuis au moins 4 jours et je commence à perdre patience.
L'application se lancera depuis un pc avec un compte qui n'a aucun droit. Le but est de demander un login/password pour exécuter le reste des actions d'administration. Pour ce faire, j'ai créé une page de login et un singleton pour récupérer le contexte d’exécution de l'utilisateur :
class View_Login:
Class Singleton_AuthAD
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 //Validation des champs pour empêcher les valeurs null ou vide et donc empêcher les connexion anonymes if (!txtLogin.getText().trim().isEmpty() && !new String (txtPassword.getPassword()).trim().isEmpty() && !txtDomain.getText().trim().isEmpty() ) { //On récupere l'instance du singleton et on lance la fonction OpenSessionAD pour créer le context d'execution (la session) Singleton_AuthAD.getInstance().openSessionAD(txtLogin.getText(), new String (txtPassword.getPassword()), txtDomain.getText()); if (Singleton_AuthAD.getInstance().getExecutionContext() != null) { JOptionPane.showMessageDialog( null, "Information : Authentification réussie :). "); frmConnexion.setVisible(false); View_ServicesMeteo servicesMeteoView = new View_ServicesMeteo(); View_ServicesMeteo.LaunchViewServicesMeteo(); } } else { JOptionPane.showMessageDialog( null, "Information : Veuillez remplir l'ensemble des champs svp :). "); }
L'identification fonctionne très bien en revanche lorsque je veux exécuter ma commande powershell, il me dit que je n'ai pas les droits pour le faire alors que quand j'essaye en lançant le powershell en tant que l'utilisateur admin cela fonctionne parfaitement. en claire, je n'arrive pas a lier la commande runtime.exec() a mon contexte jndi
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 import java.util.Hashtable; import javax.naming.AuthenticationException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.swing.JOptionPane; public class Singleton_AuthAD { private Context executionContext = null; public Context getExecutionContext() { return executionContext; } public void setExecutionContext(DirContext varExecutionContext) { executionContext = varExecutionContext; } private static String CONNECTION_USERNAME = null; private static String CONNECTION_PASSWORD = "null"; private String FqdnDomainName = null; private String PortLdap = "389"; private String SecurityMode = "simple"; /** Constructeur privé */ private Singleton_AuthAD() {} /** Holder */ private static class SingletonHolder { /** Instance unique non préinitialisée */ private final static Singleton_AuthAD instance = new Singleton_AuthAD(); } /** Point d'accès pour l'instance unique du singleton */ public static Singleton_AuthAD getInstance() { return SingletonHolder.instance; } public void openSessionAD(String VarLogin, String VarPassword, String VarFqdnDomainName){ try { CONNECTION_USERNAME = VarLogin; CONNECTION_PASSWORD = VarPassword; FqdnDomainName = VarFqdnDomainName; // Set up the environment for creating the initial context Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); env.put("com.sun.jndi.ldap.read.timeout", "5000"); env.put(Context.PROVIDER_URL, "ldap://" + FqdnDomainName +":" + PortLdap); // Authenticate as S. User and password "mysecret" env.put(Context.SECURITY_AUTHENTICATION, SecurityMode); env.put(Context.SECURITY_PRINCIPAL, CONNECTION_USERNAME + "@" + FqdnDomainName); env.put(Context.SECURITY_CREDENTIALS, CONNECTION_PASSWORD); // Create the initial context executionContext = new InitialContext(env); } catch (NamingException e) { JOptionPane.showMessageDialog( null, "Erreur : Merci de verifier que le domaine est disponible" + " et qu'il n'y a pas d'erreur dans les identifiants fournis " + System.getProperty("line.separator") + "Détails : " + e.getMessage() ,"Erreur", JOptionPane.ERROR_MESSAGE); } }
Voici le code qui lance la commande powershell :
La réponse du powershell est la suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public List<Class_Service> getAllServicesByServername(String serverName) { Context executionContext = Singleton_AuthAD.getInstance().getExecutionContext(); if (executionContext != null) { Charset encoding = Charset.forName("Cp850"); try { String command = "powershell.exe Get-WmiObject -computername " + serverName + " -Class win32_service -Property DisplayName, Started, State, StartMode, StartName, Name , SystemName | " + "Sort-Object DisplayName | select -property DisplayName, Started, State, StartMode, StartName , Name, SystemName | ConvertTo-CSV -NoType"; // Executing the command Process powerShellProcess = Runtime.getRuntime().exec(command); // Getting the results powerShellProcess.getOutputStream().close(); String line; //Partie recupération de la sortie standard. BufferedReader stdout = new BufferedReader(new InputStreamReader( powerShellProcess.getInputStream(),encoding)); int idService = 0; while ((line = stdout.readLine()) != null) { System.out.println(line); Class_Service service = new Class_Service(); String[] colonnes = line.split(","); service.setId(idService); service.setDisplayName(colonnes[0]); service.setStarted(Boolean.getBoolean(colonnes[1])); service.setStatus(colonnes[2]); service.setTypeStartup(colonnes[3]); service.setAccountStartup(colonnes[4]); service.setName(colonnes[5]); service.setServerName(colonnes[6]); serviceList.add(service); idService = idService + 1; } stdout.close(); //Partie Recupération des erreurs BufferedReader stderr = new BufferedReader(new InputStreamReader( powerShellProcess.getErrorStream(), encoding)); System.out.println("Après Modification dans ServicesList : "); System.out.println(System.getProperty("user.name")); while ((line = stderr.readLine()) != null) { System.out.println(line); } stderr.close(); } catch (Exception e) { // TODO: handle exception System.out.println(e.getMessage()); } } return serviceList; }
Merci pour votre aide.Get-WmiObject : Accès refusé. (Exception de HRESULT : 0x80070005 (E_ACCESSDENIE
D))
Au niveau de ligne*: 1 Caractère*: 14
+ Get-WmiObject <<<< -computername COMPUTER01 -Class win32_service -Property
DisplayName, Started, State, StartMode, StartName, Name , SystemName | Sort-Ob
ject DisplayName | select -property DisplayName, Started, State, StartMode, Sta
rtName , Name, SystemName | ConvertTo-CSV -NoType
+ CategoryInfo : NotSpecified: ([Get-WmiObject], UnauthorizedA
ccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.Pow
erShell.Commands.GetWmiObjectCommand
Sharco
Partager