Skip to content

Commit

Permalink
add an easy ase
Browse files Browse the repository at this point in the history
  • Loading branch information
hanhao committed Jun 15, 2022
1 parent d35c96c commit 0abf780
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cmd/groupmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var groupmodDesc string

func groupmodRun(cmd *cobra.Command, args []string) {
o := eldap.NewOption()
if groupaddGidNumber != "" {
if groupmodGidNumber != "" {
if err := o.GroupMod(args[0], groupmodGidNumber); err != nil {
log.Fatalln(err)
}
Expand All @@ -39,7 +39,7 @@ func groupmodRun(cmd *cobra.Command, args []string) {
var groupmodCmd = &cobra.Command{
Use: "groupmod [flags] GROUP",
Short: "modify a group definition on the system",
Long: `The groupmod command modifies the definition of the specified GROUP by modifying the appropriate entry in the group database.`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
groupmodRun(cmd, args)
},
Expand Down
8 changes: 2 additions & 6 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ func initRun() {
fmt.Printf("Please enter ldap Admin account: ")
fmt.Scanln(&lai.Admin)
pd, _ := gopass.GetPasswdPrompt(`Please enter ldap admin passwd(enter "NO" to not write password): `, true, os.Stdin, os.Stdout)
asepd, err := secret.EncryptAES([]byte(pd), secret.KEY)
if err != nil {
log.Fatalln(err)
return
}
asepd := secret.EasyEncrypt([]byte(pd), secret.KEY)

lai.AdminPW = string(asepd)
homedir, _ := os.UserHomeDir()
Expand All @@ -55,7 +51,7 @@ func initRun() {
dS.NewKey("Admin", lai.Admin)
dS.NewKey("AdminPW", lai.AdminPW)
dS.NewKey("TopDN", lai.TopDN)
if err = cfg.SaveTo(filename); err != nil {
if err := cfg.SaveTo(filename); err != nil {
log.Fatalln(err)
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -87,7 +88,8 @@ func initConfig() {
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
if err := viper.ReadInConfig(); err != nil {
// fmt.Println("Using config file:", viper.ConfigFileUsed())
log.Fatalln("Using config file Error:", viper.ConfigFileUsed())
}
}
2 changes: 0 additions & 2 deletions cmd/userdel.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"github.com/spf13/cobra"
)

var userdelName string

func userdelRun(cmd *cobra.Command, args []string) {
o := eldap.NewOption()
if err := o.UserDel(args[0]); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions eldap/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ func (o Option) DeleteEntry(DN string) error {
}

func NewOption() Option {
pwd, _ := secret.DecryptAES([]byte(viper.GetString("default.adminpw")), secret.KEY)
pwd := secret.EasyDecrypt(viper.GetString("default.adminpw"), secret.KEY)
if string(pwd) == "NO" {
pass, _ := gopass.GetPasswdPrompt("enter admin password: ", true, os.Stdin, os.Stdout)
pwd = pass
pwd = string(pass)
}

return Option{
Expand Down
4 changes: 2 additions & 2 deletions eldap/egroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ func (o Option) GroupMod(groupName string, gidNumber string) error {
if len(arr) != 1 {
return fmt.Errorf("bad dn number %d", len(arr))
}
DN := arr[0]
return o.ModifyEntryAttr(DN, []model.AttrVal{
dn := arr[0]
return o.ModifyEntryAttr(dn, []model.AttrVal{
{AttrOP: Rep, Attr: "gidNumber", Val: []string{gidNumber}},
})
}
Expand Down
16 changes: 16 additions & 0 deletions secret/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,26 @@ import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
)

var KEY = []byte("easy-ldap-admin!")

func EasyEncrypt(src []byte, key []byte) string {
sk := append(src, key...)
return base64.StdEncoding.EncodeToString(sk)
}

func EasyDecrypt(src string, key []byte) string {
sk, err := base64.StdEncoding.DecodeString(src)
if err != nil {
fmt.Println(err)
}
passwd := sk[:len(sk)-len(key)]
return string(passwd)
}

// padding data
func padding(src []byte, blockSize int) []byte {
padNum := blockSize - len(src)%blockSize
Expand Down

0 comments on commit 0abf780

Please sign in to comment.