99 lines
3.3 KiB
Go
99 lines
3.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"strconv"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
ldap "gopkg.in/ldap.v2"
|
|
)
|
|
|
|
var (
|
|
// user subcommand arguments
|
|
flagUserUsername string
|
|
flagUserEmail string
|
|
flagUserFirstName string
|
|
flagUserLastName string
|
|
flagUserIdNumber int
|
|
)
|
|
|
|
func init() {
|
|
// define add command flags
|
|
addCmd.PersistentFlags().StringP("bind_dn", "D", "", "Privileged LDAP user to bind as")
|
|
addCmd.PersistentFlags().StringP("bind_pw", "P", "", "Privileged LDAP user password")
|
|
// bind config file values to add flags
|
|
viper.BindPFlag("bind_dn", searchCmd.Flags().Lookup("bind_dn"))
|
|
viper.BindPFlag("bind_pw", searchCmd.Flags().Lookup("bind_pw"))
|
|
|
|
// define add user subcommand flags
|
|
addUserCmd.Flags().StringVarP(&flagUserUsername, "username", "u", "", "Username for a new user")
|
|
addUserCmd.Flags().StringVarP(&flagUserEmail, "email", "e", "", "Email address for a new user")
|
|
addUserCmd.Flags().StringVarP(&flagUserFirstName, "first_name", "f", "", "First name of a new user")
|
|
addUserCmd.Flags().StringVarP(&flagUserLastName, "last_name", "l", "", "Last name of a new user")
|
|
addUserCmd.Flags().IntVarP(&flagUserIdNumber, "id_number", "i", -1, "ID Number for a new user")
|
|
|
|
// register add command and subcommands
|
|
addCmd.AddCommand(addUserCmd)
|
|
rootCmd.AddCommand(addCmd)
|
|
}
|
|
|
|
// define add command
|
|
var addCmd = &cobra.Command{
|
|
Use: "add",
|
|
Short: "Add an LDAP resource to the directory",
|
|
Long: `Create and add an LDAP resource to your directory.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
fmt.Println("This is a test")
|
|
},
|
|
}
|
|
|
|
// define add user subcommand
|
|
var addUserCmd = &cobra.Command{
|
|
Use: "user",
|
|
Short: "Add an LDAP user resource to the directory",
|
|
Long: `Create and add an LDAP user resource to your directory.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
// create new LDAP connection
|
|
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", viper.GetString("host"), viper.GetInt("port")))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer l.Close()
|
|
|
|
// bind as the admin user
|
|
err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw"))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// set up user attributes
|
|
userDn := fmt.Sprintf("uid=%s,ou=Accounts,dc=example,dc=com", flagUserUsername)
|
|
userDisplayName := fmt.Sprintf("%s %s", flagUserFirstName, flagUserLastName)
|
|
userHome := fmt.Sprintf("/home/%s", flagUserUsername)
|
|
|
|
// create a new add request object
|
|
addRequest := ldap.NewAddRequest(userDn)
|
|
// add user attributes to the request
|
|
addRequest.Attribute("uid", []string{flagUserUsername})
|
|
addRequest.Attribute("objectClass", []string{"top", "inetOrgPerson", "posixAccount", "shadowAccount"})
|
|
addRequest.Attribute("mail", []string{flagUserEmail})
|
|
addRequest.Attribute("givenName", []string{flagUserFirstName})
|
|
addRequest.Attribute("sn", []string{flagUserLastName})
|
|
addRequest.Attribute("cn", []string{flagUserFirstName})
|
|
addRequest.Attribute("displayName", []string{userDisplayName})
|
|
addRequest.Attribute("uidNumber", []string{strconv.Itoa(flagUserIdNumber)})
|
|
addRequest.Attribute("gidNumber", []string{strconv.Itoa(flagUserIdNumber)})
|
|
addRequest.Attribute("homeDirectory", []string{userHome})
|
|
addRequest.Attribute("loginShell", []string{"/bin/bash"})
|
|
addRequest.Attribute("shadowExpire", []string{"-1"})
|
|
|
|
// perform the add operation
|
|
err = l.Add(addRequest)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
},
|
|
}
|