Guest User

Untitled

a guest
Apr 26th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. Hashtable env = new Hashtable();
  2. String user = "siampuser";
  3. String adminPassword = "S14mpad";
  4. String userName = "CN=" + primerNombre + ' ' + primerApellido + "," + "OU=testunit3,DC=mp,DC=gob,DC=gt";
  5. String groupName = "CN=Configuration,OU=testunit3,DC=mp,DC=gob,DC=gt";
  6.  
  7.  
  8. env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  9. env.put(javax.naming.Context.SECURITY_AUTHENTICATION, "simple");
  10. env.put(javax.naming.Context.SECURITY_PRINCIPAL, user + "@" + domain);
  11. env.put(javax.naming.Context.SECURITY_CREDENTIALS, adminPassword);
  12. env.put(javax.naming.Context.PROVIDER_URL, ldapHost);
  13.  
  14. try {
  15. LdapContext ctx = new InitialLdapContext(env, null);
  16.  
  17. Attributes attrs = new BasicAttributes(true);
  18. attrs.put("objectClass", "user");
  19. attrs.put("samAccountName", usuario);
  20. attrs.put("givenName", primerNombre + ' ' + segundoNombre);
  21. attrs.put("sn", primerApellido + ' ' + segundoApellido);
  22. attrs.put("cn", primerNombre + ' ' + primerApellido);
  23. attrs.put("description", puesto);
  24. attrs.put("physicalDeliveryOfficeName", dependencia + ' ' + departamento);
  25. attrs.put("telephoneNumber", fechaNacimiento + '/' + dpi + '/' + nit);
  26. attrs.put("mail", usuario + "@mp.gob.gt");
  27. attrs.put("title", puesto);
  28. attrs.put("department", dependencia);
  29. if (nip != null) {
  30. attrs.put("wWWHomePage", nip.toString());
  31. //attrs.put("st", nip.toString());
  32. }
  33.  
  34. int UF_ACCOUNTDISABLE = 0x0002;
  35. int UF_PASSWD_NOTREQD = 0x0020;
  36. int UF_PASSWD_CANT_CHANGE = 0x0040;
  37. int UF_NORMAL_ACCOUNT = 0x0200;
  38. int UF_DONT_EXPIRE_PASSWD = 0x10000;
  39. int UF_PASSWORD_EXPIRED = 0x800000;
  40.  
  41. attrs.put("userAccountControl", Integer.toString(UF_NORMAL_ACCOUNT + UF_PASSWD_NOTREQD + UF_PASSWORD_EXPIRED + UF_ACCOUNTDISABLE));
  42. javax.naming.Context result = ctx.createSubcontext(userName, attrs);
  43. System.out.println("usuario creado : " + userName);
  44.  
  45. StartTlsResponse tls = (StartTlsResponse) ctx.extendedOperation(new StartTlsRequest());
  46. SSLSession session = tls.negotiate();
  47. //tls.negotiate();
  48.  
  49. ModificationItem[] mods = new ModificationItem[2];
  50. String newQuotedPassword = """ + password + """;
  51. byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");
  52.  
  53. mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("unicodePwd", newUnicodePassword));
  54. mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userAccountControl", Integer.toString(UF_NORMAL_ACCOUNT + UF_PASSWORD_EXPIRED)));
  55. //mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userAccountControl", Integer.toString(UF_NORMAL_ACCOUNT + UF_PASSWD_NOTREQD)));
  56. // Perform the update
  57. ctx.modifyAttributes(userName, mods);
  58. System.out.println("Set password & updated userccountControl");
  59.  
  60. //now add the user to a group.
  61. try {
  62. ModificationItem member[] = new ModificationItem[1];
  63. member[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("member", userName));
  64.  
  65. ctx.modifyAttributes(groupName, member);
  66. System.out.println("Usuario agregado a la ou: " + groupName);
  67.  
  68. } catch (NamingException e) {
  69. System.err.println("Error al agregar el usario a la ou: " + e);
  70. return new Response("Error al agregar el usario a la ou: " + e, ResponseStatus.ERROR);
  71. }
  72.  
  73. tls.close();
  74. ctx.close();
  75.  
  76. //System.out.println("usuario crearo existosamente: " + userName);
  77. return new Response("usuario crearo existosamente: " + userName, ResponseStatus.OK_QUERY);
  78.  
  79. } catch (NamingException e) {
  80. System.err.println("Error al crear el usuario: " + e);
  81. return new Response("Error al crear el usuario: " + e, ResponseStatus.ERROR);
  82. } catch (IOException e) {
  83. System.err.println("Problem creating object: " + e);
  84. }
  85. return null;
Add Comment
Please, Sign In to add comment