muldap/cmd/add.go

99 lines
3.3 KiB
Go
Raw Normal View History

2018-07-14 22:18:32 -04:00
package cmd
import (
"fmt"
"log"
"strconv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
2018-07-14 22:18:32 -04:00
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"))
2018-07-14 22:18:32 -04:00
// 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")))
2018-07-14 22:18:32 -04:00
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"))
2018-07-14 22:18:32 -04:00
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)
}
},
}