From 50b512e08e86c6d0afd9a201285ed7eb560de15d Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Fri, 5 Jan 2024 10:53:57 +1100 Subject: [PATCH] re-enable dell upload --- .drone.yml | 30 ++++++++++++++-------------- models/ldap.go | 54 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 63 insertions(+), 21 deletions(-) diff --git a/.drone.yml b/.drone.yml index 036368b..2c2fee9 100644 --- a/.drone.yml +++ b/.drone.yml @@ -98,21 +98,21 @@ steps: - sudo bash -c 'mv /home/l075239/smt/test.env /home/l075239/smt/.env' - sudo bash -c '/etc/init.d/smt restart' -#- name: dell-deploy -## # https://github.com/cschlosser/drone-ftps/blob/master/README.md -# image: cschlosser/drone-ftps -# environment: -# FTP_USERNAME: -# from_secret: FTP_USERNAME -# FTP_PASSWORD: -# from_secret: FTP_PASSWORD -# PLUGIN_HOSTNAME: ftp.emc.com:21 -# PLUGIN_SECURE: false -# PLUGIN_VERIFY: false -# PLUGIN_CHMOD: false -# #PLUGIN_DEBUG: false -# PLUGIN_INCLUDE: ^smt$,^smt_checksum.txt$ -# PLUGIN_EXCLUDE: ^\.git/$,^\controllers/$,^\middlewares/$,^\models/$,^\utils/$ +- name: dell-deploy +# # https://github.com/cschlosser/drone-ftps/blob/master/README.md + image: cschlosser/drone-ftps + environment: + FTP_USERNAME: + from_secret: FTP_USERNAME + FTP_PASSWORD: + from_secret: FTP_PASSWORD + PLUGIN_HOSTNAME: ftp.emc.com:21 + PLUGIN_SECURE: false + PLUGIN_VERIFY: false + PLUGIN_CHMOD: false + #PLUGIN_DEBUG: false + PLUGIN_INCLUDE: ^smt$,^smt_checksum.txt$ + PLUGIN_EXCLUDE: ^\.git/$,^\controllers/$,^\middlewares/$,^\models/$,^\utils/$ volumes: - name: shared diff --git a/models/ldap.go b/models/ldap.go index b9940ce..cd56c04 100644 --- a/models/ldap.go +++ b/models/ldap.go @@ -265,6 +265,7 @@ func VerifyLdapCreds(username string, password string) error { username = CheckUsername(username) ldaps := ldapConnect() + defer ldaps.Close() // try an authenticated bind to AD to verify credentials log.Printf("Attempting LDAP bind with user '%s' and password length '%d'\n", username, len(password)) @@ -289,17 +290,17 @@ func VerifyLdapCreds(username string, password string) error { // GetGroupsOfUser returns the group for a user. // Taken from https://github.com/jtblin/go-ldap-client/issues/13#issuecomment-456090979 func GetGroupsOfUser(username string, baseDN string, conn *ldap.Conn) ([]string, error) { - var samAccountName string + var sAMAccountName string var groups []string if strings.Contains(username, "@") { s := strings.Split(username, "@") - samAccountName = s[0] + sAMAccountName = s[0] } else if strings.Contains(username, "\\") { s := strings.Split(username, "\\") - samAccountName = s[len(s)-1] + sAMAccountName = s[len(s)-1] } else { - samAccountName = username + sAMAccountName = username } // Get the users DN @@ -307,7 +308,7 @@ func GetGroupsOfUser(username string, baseDN string, conn *ldap.Conn) ([]string, searchRequest := ldap.NewSearchRequest( baseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, - fmt.Sprintf("(sAMAccountName=%s)", ldap.EscapeFilter(samAccountName)), + fmt.Sprintf("(sAMAccountName=%s)", ldap.EscapeFilter(sAMAccountName)), []string{}, nil, ) @@ -320,7 +321,7 @@ func GetGroupsOfUser(username string, baseDN string, conn *ldap.Conn) ([]string, } if len(sr.Entries) != 1 { - return nil, fmt.Errorf("user '%s' does not exist", samAccountName) + return nil, fmt.Errorf("user '%s' does not exist", sAMAccountName) } else { // Get the groups of the first result groups = sr.Entries[0].GetAttributeValues("memberOf") @@ -328,3 +329,44 @@ func GetGroupsOfUser(username string, baseDN string, conn *ldap.Conn) ([]string, return groups, nil } + +func GetLdapUserDn(username string, baseDN string, conn *ldap.Conn) (string, error) { + var sAMAccountName string + + if strings.Contains(username, "@") { + s := strings.Split(username, "@") + sAMAccountName = s[0] + } else if strings.Contains(username, "\\") { + s := strings.Split(username, "\\") + sAMAccountName = s[len(s)-1] + } else { + sAMAccountName = username + } + + // Search for the user's distinguishedName + searchRequest := ldap.NewSearchRequest( + baseDN, + ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, + fmt.Sprintf("(sAMAccountName=%s)", sAMAccountName), + []string{"distinguishedName"}, + nil, + ) + + searchResult, err := conn.Search(searchRequest) + if err != nil { + log.Fatal(err) + } + + if len(searchResult.Entries) == 0 { + return "", fmt.Errorf("user '%s' does not exist", sAMAccountName) + } else { + // Retrieve the distinguishedName of the user + distinguishedName := searchResult.Entries[0].GetAttributeValue("distinguishedName") + if distinguishedName != "" { + log.Printf("GetLdapUserDn located user's distinguishedName : '%s'\n", distinguishedName) + return distinguishedName, nil + } else { + return "", fmt.Errorf("could not find distinguishedName for user '%s'", sAMAccountName) + } + } +}