Reorganized some of the commands
This commit is contained in:
77
cmd/user/add.go
Normal file
77
cmd/user/add.go
Normal file
@ -0,0 +1,77 @@
|
||||
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)
|
||||
}
|
||||
},
|
||||
}
|
53
cmd/user/delete.go
Normal file
53
cmd/user/delete.go
Normal file
@ -0,0 +1,53 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
ldap "gopkg.in/ldap.v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// define user delete subcommand flags
|
||||
userDeleteCmd.Flags().StringP("base_ou", "o", "", "LDAP OU where your user entries are stored")
|
||||
userDeleteCmd.Flags().StringP("uid_attribute", "a", "uid", "LDAP DN attribute for users")
|
||||
userDeleteCmd.Flags().StringVarP(&flagUserUsername, "username", "u", "", "Username of user to delete")
|
||||
// bind config file values to user delete flags
|
||||
viper.BindPFlag("user.base_ou", userDeleteCmd.Flags().Lookup("base_ou"))
|
||||
viper.BindPFlag("user.uid_attr", userDeleteCmd.Flags().Lookup("uid_attribute"))
|
||||
}
|
||||
|
||||
// define user delete subcommand
|
||||
var userDeleteCmd = &cobra.Command{
|
||||
Use: "delete",
|
||||
Short: "Delete an LDAP user from the directory",
|
||||
Long: `Delete an LDAP user resource from the 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 DN
|
||||
userDn := fmt.Sprintf("%s=%s,%s", viper.GetString("user.uid_attr"), flagUserUsername, viper.GetString("user.base_ou"))
|
||||
|
||||
// create a new delete request object
|
||||
deleteRequest := ldap.NewDelRequest(userDn, []ldap.Control{})
|
||||
|
||||
// perform the delete operation
|
||||
err = l.Del(deleteRequest)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
62
cmd/user/pw.go
Normal file
62
cmd/user/pw.go
Normal file
@ -0,0 +1,62 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
ldap "gopkg.in/ldap.v2"
|
||||
|
||||
cli "git.metaunix.net/metaunix.net/muldap/lib/cli"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// define user delete subcommand flags
|
||||
userPwCmd.Flags().StringP("base_ou", "o", "", "LDAP OU where your user entries are stored")
|
||||
userPwCmd.Flags().StringP("uid_attribute", "a", "uid", "LDAP DN attribute for users")
|
||||
userPwCmd.Flags().StringVarP(&flagUserUsername, "username", "u", "", "Username of user to delete")
|
||||
// bind config file values to user delete flags
|
||||
viper.BindPFlag("user.base_ou", userPwCmd.Flags().Lookup("base_ou"))
|
||||
viper.BindPFlag("user.uid_attr", userPwCmd.Flags().Lookup("uid_attribute"))
|
||||
}
|
||||
|
||||
// define user password subcommand
|
||||
var userPwCmd = &cobra.Command{
|
||||
Use: "pw",
|
||||
Short: "Set an LDAP user's password",
|
||||
Long: "Prompts you for a password to use to set an LDAP user's password",
|
||||
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)
|
||||
}
|
||||
|
||||
// get password from user
|
||||
userPw, pwErr := cli.GetUserInput("New user password", true)
|
||||
|
||||
if pwErr != nil {
|
||||
log.Fatal(pwErr)
|
||||
}
|
||||
|
||||
// set up user DN
|
||||
userDn := fmt.Sprintf("%s=%s,%s", viper.GetString("user.uid_attr"), flagUserUsername, viper.GetString("user.base_ou"))
|
||||
|
||||
// create new password modify request
|
||||
passwordModifyRequest := ldap.NewPasswordModifyRequest(userDn, "", userPw)
|
||||
|
||||
// perform password change operation
|
||||
_, err = l.PasswordModify(passwordModifyRequest)
|
||||
if err != nil {
|
||||
log.Fatalf("Password could not be changed: %s", err.Error())
|
||||
}
|
||||
},
|
||||
}
|
31
cmd/user/user.go
Normal file
31
cmd/user/user.go
Normal file
@ -0,0 +1,31 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
// user subcommand arguments
|
||||
flagUserUsername string
|
||||
flagUserEmail string
|
||||
flagUserFirstName string
|
||||
flagUserLastName string
|
||||
flagUserIdNumber int
|
||||
)
|
||||
|
||||
func init() {
|
||||
// register add command and subcommands
|
||||
UserCmd.AddCommand(userAddCmd, userDeleteCmd, userPwCmd)
|
||||
}
|
||||
|
||||
// define user command
|
||||
var UserCmd = &cobra.Command{
|
||||
Use: "user",
|
||||
Short: "Manage LDAP user resources",
|
||||
Long: `Perform various LDAP operations on user resources.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("Metaunix.net LDAP tool, user command. Available subcommands are: add, delete, pw")
|
||||
},
|
||||
}
|
Reference in New Issue
Block a user