guichet/utils.go

95 lines
2.3 KiB
Go
Raw Normal View History

2023-07-20 09:20:46 +00:00
package main
import (
"fmt"
"log"
2023-07-20 09:56:11 +00:00
2023-07-20 18:02:54 +00:00
"math/rand"
2023-07-21 04:23:03 +00:00
"github.com/go-ldap/ldap/v3"
2023-07-20 09:20:46 +00:00
)
type NewUser struct {
DN string
CN string
GivenName string
DisplayName string
Mail string
SN string
UID string
2023-07-20 09:56:11 +00:00
Description string
2023-07-20 11:46:32 +00:00
Password string
2023-07-20 09:56:11 +00:00
}
func openLdap(config ConfigFile) *ldap.Conn {
l, err := ldap.DialURL(config.LdapServerAddr)
if err != nil {
log.Printf(fmt.Sprint("Erreur connect LDAP %v", err))
return nil
} else {
return l
}
2023-07-20 09:20:46 +00:00
}
2023-07-20 17:55:00 +00:00
func suggestPassword() string {
2023-07-20 18:02:54 +00:00
password := ""
chars := "abcdfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*+_-="
for i := 0; i < 12; i++ {
password += string([]rune(chars)[rand.Intn(len(chars))])
2023-07-20 17:55:00 +00:00
}
2023-07-20 18:02:54 +00:00
return password
2023-07-20 15:52:16 +00:00
}
2023-07-21 07:51:57 +00:00
func addNewUser(newUser NewUser, config *ConfigFile, ldapConn *ldap.Conn) bool {
2023-07-20 09:20:46 +00:00
log.Printf(fmt.Sprint("Adding New User"))
2023-07-21 07:51:57 +00:00
// l, _ := ldap.DialURL(config.LdapServerAddr)
// l.Bind(config.NewUserDN, config.NewUserPassword)
// err := l.StartTLS(&tls.Config{InsecureSkipVerify: true})
// if err != nil {
// log.Printf(fmt.Sprintf("86: %v", err))
// }
2023-07-21 07:41:30 +00:00
2023-07-20 09:56:11 +00:00
// l.Bind(config.)
dn := newUser.DN
req := ldap.NewAddRequest(dn, nil)
req.Attribute("objectClass", []string{"top", "inetOrgPerson"})
if newUser.DisplayName != "" {
req.Attribute("displayName", []string{newUser.DisplayName})
}
if newUser.GivenName != "" {
req.Attribute("givenName", []string{newUser.GivenName})
}
if newUser.Mail != "" {
req.Attribute("mail", []string{newUser.Mail})
}
2023-07-21 06:29:09 +00:00
if newUser.UID != "" {
req.Attribute("uid", []string{newUser.UID})
}
2023-07-20 09:56:11 +00:00
// if newUser.Member != "" {
// req.Attribute("member", []string{newUser.Member})
// }
if newUser.SN != "" {
req.Attribute("sn", []string{newUser.SN})
}
if newUser.Description != "" {
req.Attribute("description", []string{newUser.Description})
}
2023-07-20 11:46:32 +00:00
if newUser.Password != "" {
pw, _ := SSHAEncode(newUser.Password)
req.Attribute("userPassword", []string{pw})
}
2023-07-21 07:13:17 +00:00
// conn :=
2023-07-21 07:51:57 +00:00
err := ldapConn.Add(req)
2023-07-20 10:04:18 +00:00
log.Printf(fmt.Sprintf("71: %v", err))
log.Printf(fmt.Sprintf("72: %v", req))
log.Printf(fmt.Sprintf("73: %v", newUser))
2023-07-20 09:56:11 +00:00
if err != nil {
2023-07-21 07:41:30 +00:00
log.Printf(fmt.Sprintf("86: %v", err))
2023-07-20 09:56:11 +00:00
return false
} else {
return true
}
2023-07-20 09:20:46 +00:00
}