Skip to content

Commit fa21ee8

Browse files
authored
Merge pull request #22 from TheManticoreProject/improve-ldap-functions
[enhancement] Improve ldap functions
2 parents 9a7191b + 389776f commit fa21ee8

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

network/ldap/modify.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ func (req *ModifyRequest) Replace(attrType string, attrVals []string) {
185185
// - attrVals: A slice of strings representing the new values for the attribute.
186186
func (ldapSession *Session) OverwriteAttributeValues(distinguishedName string, attrName string, attrVals []string) error {
187187
if len(attrVals) == 0 {
188-
ldapSession.FlushAttribute(distinguishedName, attrName)
188+
err := ldapSession.FlushAttributeValues(distinguishedName, attrName)
189+
if err != nil {
190+
return fmt.Errorf("error flushing attribute %s of %s: %s", attrName, distinguishedName, err)
191+
}
189192
} else {
190193
controls := NewControlsWithOIDs([]string{LDAP_SERVER_PERMISSIVE_MODIFY_OID}, false)
191194

@@ -196,7 +199,7 @@ func (ldapSession *Session) OverwriteAttributeValues(distinguishedName string, a
196199
// Execute the modify request
197200
err := ldapSession.connection.Modify(m)
198201
if err != nil {
199-
return fmt.Errorf("error overwriting attribute %s of %s: %s", attrName, distinguishedName, err)
202+
return fmt.Errorf("error flushing attribute %s of %s: %w", attrName, distinguishedName, err)
200203
}
201204
}
202205
return nil
@@ -325,15 +328,15 @@ func (ldapSession *Session) AddStringToAttributeList(distinguishedName string, a
325328
return nil
326329
}
327330

328-
// FlushAttribute flushes the attribute by deleting it
331+
// FlushAttributeValues flushes the attribute by deleting it
329332
//
330333
// Parameters:
331334
// - dn: A string representing the distinguished name (DN) of the LDAP entry to be modified.
332335
// - attributeName: A string representing the name of the attribute to be flushed.
333336
//
334337
// Returns:
335338
// - An error object if the flush operation fails, otherwise nil.
336-
func (ldapSession *Session) FlushAttribute(distinguishedName string, attributeName string) error {
339+
func (ldapSession *Session) FlushAttributeValues(distinguishedName string, attributeName string) error {
337340
// Create a modify request
338341
m := goldapv3.NewModifyRequest(distinguishedName, nil)
339342
m.Replace(attributeName, []string{})

network/ldap/naming_contexts.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
goldapv3 "github.com/go-ldap/ldap/v3"
77
)
88

9-
// BaseDNExists checks if a given base distinguished name (baseDN) exists in the LDAP directory.
9+
// DistinguishedNameExists checks if a given base distinguished name (baseDN) exists in the LDAP directory.
1010
//
1111
// This function performs an LDAP search with a base scope to determine if the specified baseDN exists.
1212
// It constructs an LDAP search request with the provided baseDN and a search filter of "(objectClass=*)",
@@ -18,11 +18,16 @@ import (
1818
//
1919
// Returns:
2020
// - bool: True if the baseDN exists, false if it does not exist or if an error occurs.
21+
// - error: An error if the search fails.
2122
//
2223
// Example usage:
2324
//
2425
// ldapSession := &Session{}
25-
// exists := ldapSession.BaseDNExists("DC=example,DC=com")
26+
// exists, err := ldapSession.DistinguishedNameExists("DC=example,DC=com")
27+
// if err != nil {
28+
// fmt.Println("Error checking if baseDN exists:", err)
29+
// return
30+
// }
2631
// if exists {
2732
// fmt.Println("The baseDN exists in the LDAP directory.")
2833
// } else {
@@ -32,12 +37,12 @@ import (
3237
// Note:
3338
// - This function assumes that the Session struct has a valid connection object and that the ldap package
3439
// is correctly imported and used.
35-
func (ldapSession *Session) BaseDNExists(baseDN string) bool {
40+
func (ldapSession *Session) DistinguishedNameExists(distinguishedName string) (bool, error) {
3641
// Specify LDAP search parameters
3742
// https://pkg.go.dev/gopkg.in/ldap.v3#NewSearchRequest
3843
searchRequest := goldapv3.NewSearchRequest(
3944
// Base DN
40-
baseDN,
45+
distinguishedName,
4146
// Scope
4247
goldapv3.ScopeBaseObject,
4348
// DerefAliases
@@ -58,11 +63,15 @@ func (ldapSession *Session) BaseDNExists(baseDN string) bool {
5863

5964
// Perform LDAP search
6065
_, err := ldapSession.connection.Search(searchRequest)
61-
if goldapv3.IsErrorWithCode(err, goldapv3.LDAPResultNoSuchObject) {
62-
return false
63-
} else {
64-
return true
66+
if err != nil {
67+
if goldapv3.IsErrorWithCode(err, goldapv3.LDAPResultNoSuchObject) {
68+
return false, nil
69+
} else {
70+
return false, err
71+
}
6572
}
73+
74+
return true, nil
6675
}
6776

6877
// GetAllNamingContexts retrieves all naming contexts from the LDAP server.

0 commit comments

Comments
 (0)