Added an LDAP search command with some basic searching options

This commit is contained in:
Gregory Ballantine 2018-07-14 15:55:28 -04:00
parent bd24d89628
commit 5d2a7f4304
4 changed files with 80 additions and 3 deletions

21
Gopkg.lock generated
View File

@ -25,9 +25,28 @@
revision = "583c0c0531f06d5278b7d917446061adc344b5cd"
version = "v1.0.1"
[[projects]]
digest = "1:81e1c5cee195fca5de06e2540cb63eea727a850b7e5c213548e7f81521c97a57"
name = "gopkg.in/asn1-ber.v1"
packages = ["."]
pruneopts = "UT"
revision = "379148ca0225df7a432012b8df0355c2a2063ac0"
version = "v1.2"
[[projects]]
digest = "1:93aaeb913621a3a53aaa78592c00f46d63e3bb0ea76e2d9b07327b50959a5778"
name = "gopkg.in/ldap.v2"
packages = ["."]
pruneopts = "UT"
revision = "bb7a9ca6e4fbc2129e3db588a34bc970ffe811a9"
version = "v2.5.1"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
input-imports = ["github.com/spf13/cobra"]
input-imports = [
"github.com/spf13/cobra",
"gopkg.in/ldap.v2",
]
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -34,5 +34,5 @@
version = "2.5.1"
[[constraint]]
name = "github.com/spf13/Cobra"
name = "github.com/spf13/cobra"
version = "0.0.3"

View File

@ -12,7 +12,7 @@ var rootCmd = &cobra.Command{
Short: "muldap is Metaunix.net's LDAP management tool",
Long: "LDAP management tool for Metaunix.net user and group resources.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Metaunix.net LDAP tool - available commands are: version")
fmt.Println("Metaunix.net LDAP tool - available commands are: search, version")
},
}

58
cmd/search.go Normal file
View File

@ -0,0 +1,58 @@
package cmd
import (
"fmt"
"log"
"github.com/spf13/cobra"
ldap "gopkg.in/ldap.v2"
)
var (
flagBase string
flagFilter string
flagIndent int
)
func init() {
// define search command flags
searchCmd.Flags().StringVarP(&flagBase, "base", "b", "dc=example,dc=com", "LDAP search base DN")
searchCmd.Flags().StringVarP(&flagFilter, "filter", "f", "objectClass=*", "LDAP search filter")
searchCmd.Flags().IntVarP(&flagIndent, "indent", "i", 0, "Number of spaces to use while indenting search output")
// register search command
rootCmd.AddCommand(searchCmd)
}
// define search command
var searchCmd = &cobra.Command{
Use: "search",
Short: "Search an LDAP directory",
Long: `Perform an LDAP search operation on an LDAP directory`,
Run: func(cmd *cobra.Command, args []string) {
// create new LDAP connection
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.example.com", 389))
if err != nil {
log.Fatal(err)
}
defer l.Close()
// create a new LDAP search request
searchRequest := ldap.NewSearchRequest(
flagBase, // The base dn to search
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(%s)", flagFilter), // The filter to apply
nil, // A list attributes to retrieve
nil,
)
// perform the LDAP search
sr, err := l.Search(searchRequest)
if err != nil {
log.Fatal(err)
}
// print the results
sr.PrettyPrint(flagIndent)
},
}