Use only []string for values, implement add

This commit is contained in:
Alex 2020-01-19 18:24:21 +01:00
parent 131297a33f
commit 2bfb6b4ced

111
main.go
View file

@ -58,26 +58,12 @@ func parseConsulResult(data []*consul.KVPair) (map[string]Entry, error) {
if _, exists := aggregator[dn]; !exists { if _, exists := aggregator[dn]; !exists {
aggregator[dn] = Entry{} aggregator[dn] = Entry{}
} }
var value interface{} var value []string
err := json.Unmarshal(val, &value) err := json.Unmarshal(val, &value)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if vlist, ok := value.([]interface{}); ok { aggregator[dn][attr] = value
vlist2 := []string{}
for _, v := range vlist {
if vstr, ok := v.(string); ok {
vlist2 = append(vlist2, vstr)
} else {
return nil, fmt.Errorf("Not a string: %#v", v)
}
}
aggregator[dn][attr] = vlist2
} else if vstr, ok := value.(string); ok {
aggregator[dn][attr] = vstr
} else {
return nil, fmt.Errorf("Not a string or a list of strings: %#v", value)
}
} }
return aggregator, nil return aggregator, nil
@ -119,7 +105,7 @@ type State struct {
bindDn string bindDn string
} }
type Entry map[string]interface{} type Entry map[string][]string
func main() { func main() {
//ldap logger //ldap logger
@ -152,6 +138,7 @@ func main() {
routes := ldap.NewRouteMux() routes := ldap.NewRouteMux()
routes.Bind(gobottin.handleBind) routes.Bind(gobottin.handleBind)
routes.Search(gobottin.handleSearch) routes.Search(gobottin.handleSearch)
routes.Add(gobottin.handleAdd)
ldapserver.Handle(routes) ldapserver.Handle(routes)
// listen on 10389 // listen on 10389
@ -179,13 +166,13 @@ func (server *Server) init() error {
base_attributes := Entry{ base_attributes := Entry{
"objectClass": []string{"top", "dcObject", "organization"}, "objectClass": []string{"top", "dcObject", "organization"},
"structuralObjectClass": "Organization", "structuralObjectClass": []string{"Organization"},
} }
suffix_dn, err := parseDN(server.config.Suffix) suffix_dn, err := parseDN(server.config.Suffix)
if err != nil { if err != nil {
return err return err
} }
base_attributes[suffix_dn[0].Type] = suffix_dn[0].Value base_attributes[suffix_dn[0].Type] = []string{suffix_dn[0].Value}
err = server.addElements(server.config.Suffix, base_attributes) err = server.addElements(server.config.Suffix, base_attributes)
if err != nil { if err != nil {
@ -200,10 +187,10 @@ func (server *Server) init() error {
admin_dn := "cn=admin," + server.config.Suffix admin_dn := "cn=admin," + server.config.Suffix
admin_attributes := Entry{ admin_attributes := Entry{
"objectClass": []string{"simpleSecurityObject", "organizationalRole"}, "objectClass": []string{"simpleSecurityObject", "organizationalRole"},
"description": "LDAP administrator", "description": []string{"LDAP administrator"},
"cn": "admin", "cn": []string{"admin"},
"userpassword": admin_pass_hash, "userpassword": []string{admin_pass_hash},
"structuralObjectClass": "organizationalRole", "structuralObjectClass": []string{"organizationalRole"},
"permissions": []string{"read", "write"}, "permissions": []string{"read", "write"},
} }
@ -241,7 +228,7 @@ func (server *Server) handleBind(s ldap.UserState, w ldap.ResponseWriter, m *lda
state := s.(*State) state := s.(*State)
r := m.GetBindRequest() r := m.GetBindRequest()
result_code, err := server.handleBindInternal(state, w, &r) result_code, err := server.handleBindInternal(state, &r)
res := ldap.NewBindResponse(result_code) res := ldap.NewBindResponse(result_code)
if err != nil { if err != nil {
@ -251,7 +238,7 @@ func (server *Server) handleBind(s ldap.UserState, w ldap.ResponseWriter, m *lda
w.Write(res) w.Write(res)
} }
func (server *Server) handleBindInternal(state *State, w ldap.ResponseWriter, r *message.BindRequest) (int, error) { func (server *Server) handleBindInternal(state *State, r *message.BindRequest) (int, error) {
pair, _, err := server.kv.Get(dnToConsul(string(r.Name()))+"/attribute=userpassword", nil) pair, _, err := server.kv.Get(dnToConsul(string(r.Name()))+"/attribute=userpassword", nil)
if err != nil { if err != nil {
@ -340,16 +327,9 @@ func (server *Server) handleSearchInternal(state *State, w ldap.ResponseWriter,
} }
} }
// Send result // Send result
if val_str, ok := val.(string); ok { for _, v := range val {
e.AddAttribute(message.AttributeDescription(attr), e.AddAttribute(message.AttributeDescription(attr),
message.AttributeValue(val_str)) message.AttributeValue(v))
} else if val_strlist, ok := val.([]string); ok {
for _, v := range val_strlist {
e.AddAttribute(message.AttributeDescription(attr),
message.AttributeValue(v))
}
} else {
panic(fmt.Sprintf("Invalid value: %#v", val))
} }
} }
w.Write(e) w.Write(e)
@ -402,20 +382,12 @@ func applyFilter(entry Entry, filter message.Filter) (bool, error) {
// Case insensitive attribute search // Case insensitive attribute search
for entry_desc, value := range entry { for entry_desc, value := range entry {
if strings.EqualFold(entry_desc, desc) { if strings.EqualFold(entry_desc, desc) {
if vstr, ok := value.(string); ok { for _, val := range value {
// If we have one value for the key, match exactly if val == target {
return vstr == target, nil return true, nil
} else if vlist, ok := value.([]string); ok {
// If we have several values for the key, one must match
for _, val := range vlist {
if val == target {
return true, nil
}
} }
return false, nil
} else {
panic(fmt.Sprintf("Invalid value: %#v", value))
} }
return false, nil
} }
} }
return false, nil return false, nil
@ -423,3 +395,50 @@ func applyFilter(entry Entry, filter message.Filter) (bool, error) {
return false, fmt.Errorf("Unsupported filter: %#v %T", filter, filter) return false, fmt.Errorf("Unsupported filter: %#v %T", filter, filter)
} }
} }
func (server *Server) handleAdd(s ldap.UserState, w ldap.ResponseWriter, m *ldap.Message) {
state := s.(*State)
r := m.GetAddRequest()
code, err := server.handleAddInternal(state, &r)
res := ldap.NewResponse(code)
if err != nil {
res.SetDiagnosticMessage(err.Error())
}
w.Write(message.AddResponse(res))
}
func (server *Server) handleAddInternal(state *State, r *message.AddRequest) (int, error) {
dn := string(r.Entry())
prefix := dnToConsul(dn) + "/"
data, _, err := server.kv.List(prefix, nil)
if err != nil {
return ldap.LDAPResultOperationsError, err
}
if len(data) > 0 {
return ldap.LDAPResultEntryAlreadyExists, nil
}
// TODO check permissions
entry := Entry{}
for _, attribute := range r.Attributes() {
key := string(attribute.Type_())
vals_str := []string{}
for _, val := range attribute.Vals() {
vals_str = append(vals_str, string(val))
}
entry[key] = vals_str
}
err = server.addElements(dn, entry)
if err != nil {
return ldap.LDAPResultOperationsError, err
}
return ldap.LDAPResultSuccess, nil
}