winch.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package main
  2. import (
  3. "crypto/rand"
  4. "crypto/sha512"
  5. "encoding/base64"
  6. "fmt"
  7. "log"
  8. randm "math/rand"
  9. "os"
  10. "strings"
  11. "github.com/go-ldap/ldap/v3"
  12. "github.com/go-yaml/yaml"
  13. )
  14. type conf struct {
  15. LdapUrl string `yaml:"ldapurl"`
  16. BaseDN string `yaml:"basedn"`
  17. BindDN string `yaml:"binddn"`
  18. BindPW string `yaml:"bindpw"`
  19. }
  20. func (c *conf) get_config() *conf {
  21. f, err := os.ReadFile("config.yaml")
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. err = yaml.Unmarshal(f, c)
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. return c
  30. }
  31. // Gettings uids of people that needs a password change
  32. func get_uids(ldap_conn *ldap.Conn, basedn string) []string {
  33. uids := []string{}
  34. search_req := ldap.NewSearchRequest(basedn, ldap.ScopeWholeSubtree, 0, 0, 0, false, "(uid=*)", []string{"uid", "userPassword"}, nil)
  35. res, err := ldap_conn.Search(search_req)
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. for _, entry := range res.Entries {
  40. uid := entry.GetAttributeValue("uid")
  41. password := entry.GetAttributeValue("userPassword")
  42. if strings.HasPrefix(password, "{SSHA}") {
  43. uids = append(uids, uid)
  44. }
  45. }
  46. return uids
  47. }
  48. func get_random_password(length int) string {
  49. characters := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
  50. var sb strings.Builder
  51. for i := 0; i < length; i++ {
  52. sb.WriteRune(characters[randm.Intn(len(characters))])
  53. }
  54. return sb.String()
  55. }
  56. func get_salt() ([]byte, error) {
  57. salt := make([]byte, 4)
  58. _, err := rand.Read(salt)
  59. if err != nil {
  60. return nil, err
  61. }
  62. return salt, nil
  63. }
  64. func get_hash(password string, salt []byte) []byte {
  65. password_bytes := []byte(password)
  66. password_salt := append(password_bytes, salt...)
  67. sum := sha512.Sum512(password_salt)
  68. res := append(sum[:], salt...)
  69. return res
  70. }
  71. func change_password(ldap_conn *ldap.Conn, basedn string, uid string) {
  72. password := get_random_password(20)
  73. salt, err := get_salt()
  74. if err != nil {
  75. log.Fatal(err)
  76. }
  77. new_pass := "{SSHA512}" + base64.StdEncoding.EncodeToString(get_hash(password, salt))
  78. modify_req := ldap.NewModifyRequest("uid="+uid+","+basedn, nil)
  79. modify_req.Replace("userPassword", []string{new_pass})
  80. err = ldap_conn.Modify(modify_req)
  81. if err != nil {
  82. log.Fatal(err)
  83. }
  84. fmt.Printf("%s : %s \n", uid, password)
  85. }
  86. func main() {
  87. var c conf
  88. c.get_config()
  89. l, err := ldap.DialURL(c.LdapUrl)
  90. if err != nil {
  91. log.Fatal(err)
  92. }
  93. defer l.Close()
  94. err = l.Bind(c.BindDN, c.BindPW)
  95. if err != nil {
  96. log.Fatal(err)
  97. }
  98. uids := get_uids(l, c.BaseDN)
  99. for _, uid := range uids {
  100. change_password(l, c.BaseDN, uid)
  101. }
  102. }