78 lines
3.1 KiB
Go
78 lines
3.1 KiB
Go
|
package user
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"strconv"
|
||
|
|
||
|
"github.com/spf13/cobra"
|
||
|
"github.com/spf13/viper"
|
||
|
ldap "gopkg.in/ldap.v2"
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
// define user add subcommand flags
|
||
|
userAddCmd.Flags().StringP("base_ou", "o", "", "LDAP OU to create the new user entry under")
|
||
|
userAddCmd.Flags().StringP("uid_attribute", "a", "uid", "LDAP DN attribute for users")
|
||
|
userAddCmd.Flags().StringP("home_directory", "d", "/home/%s", "User's home directory")
|
||
|
userAddCmd.Flags().StringVarP(&flagUserUsername, "username", "u", "", "Username for a new user")
|
||
|
userAddCmd.Flags().StringVarP(&flagUserEmail, "email", "e", "", "Email address for a new user")
|
||
|
userAddCmd.Flags().StringVarP(&flagUserFirstName, "first_name", "f", "", "First name of a new user")
|
||
|
userAddCmd.Flags().StringVarP(&flagUserLastName, "last_name", "l", "", "Last name of a new user")
|
||
|
userAddCmd.Flags().IntVarP(&flagUserIdNumber, "id_number", "i", -1, "ID Number for a new user")
|
||
|
// bind config file values to user add flags
|
||
|
viper.BindPFlag("user.base_ou", userAddCmd.Flags().Lookup("base_ou"))
|
||
|
viper.BindPFlag("user.uid_attr", userAddCmd.Flags().Lookup("uid_attribute"))
|
||
|
viper.BindPFlag("user.home_directory", userAddCmd.Flags().Lookup("home_directory"))
|
||
|
}
|
||
|
|
||
|
// define user add subcommand
|
||
|
var userAddCmd = &cobra.Command{
|
||
|
Use: "add",
|
||
|
Short: "Add an LDAP user 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("%s=%s,%s", viper.GetString("user.uid_attr"), flagUserUsername, viper.GetString("user.base_ou"))
|
||
|
userDisplayName := fmt.Sprintf("%s %s", flagUserFirstName, flagUserLastName)
|
||
|
userHome := fmt.Sprintf(viper.GetString("user.home_directory"), flagUserUsername)
|
||
|
|
||
|
// create a new add request object
|
||
|
addRequest := ldap.NewAddRequest(userDn)
|
||
|
// add user attributes to the request
|
||
|
addRequest.Attribute(viper.GetString("user.uid_attr"), []string{flagUserUsername})
|
||
|
addRequest.Attribute("objectClass", viper.GetStringSlice("user.object_class"))
|
||
|
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})
|
||
|
// loop through extra attributes
|
||
|
for key, value := range viper.GetStringMapString("user.extra_attributes") {
|
||
|
addRequest.Attribute(key, []string{value})
|
||
|
}
|
||
|
|
||
|
// perform the add operation
|
||
|
err = l.Add(addRequest)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
},
|
||
|
}
|