pubkey/pubkey.go

50 lines
1.1 KiB
Go

package main
import (
"fmt"
"github.com/go-ldap/ldap"
"os"
)
func usage(code int, err error) {
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("error %s\n", err))
}
os.Stderr.WriteString(fmt.Sprintf("usage: %s [uri] [bind_dn] [bind_pw] [base_dn] [uid]\n", os.Args[0]))
os.Exit(code)
}
func main() {
if len(os.Args) != 6 {
usage(1, nil)
}
var ldap_uri string = os.Args[1]
var ldap_bind_dn string = os.Args[2]
var ldap_bind_pw string = os.Args[3]
var ldap_base_dn string = os.Args[4]
var uid string = os.Args[5]
l, err := ldap.DialURL(ldap_uri)
if err != nil {
usage(2, err)
}
defer l.Close()
err = l.Bind(ldap_bind_dn, ldap_bind_pw)
if err != nil {
usage(3, err)
}
search_request := ldap.NewSearchRequest(ldap_base_dn, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, fmt.Sprintf("(&(objectClass=posixAccount)(uid=%s))", uid), []string{"dn", "sshPublicKey"}, nil)
sr, err := l.Search(search_request)
if err != nil {
usage(4, err)
}
for _, entry := range(sr.Entries) {
fmt.Printf("%v", entry.GetAttributeValue("sshPublicKey"))
}
}