-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwgen
87 lines (79 loc) · 2.52 KB
/
pwgen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"log"
"os"
"flag"
"time"
"strings"
"math/rand"
"github.com/kless/osutil/user/crypt/sha512_crypt"
)
const (
digits string = "0123456789"
specials string = "~=+%^*/()[]{}/!@#$?|"
letters string = "abcdefghijklmnopqrstuvwxyz"
length int = 12
)
func main() {
DealerName := flag.String("name", "", "Dealer name, like mir-avto")
DealerIP := flag.String("iprange", "", "IP list, \"127.0.0.1,127.0.0.2,127.0.0.3\"")
JiraTask := flag.String("task", "", "Jira Task, AD-5555")
UID := flag.String("uid", "", "UID for this user, 6067")
flag.Parse()
if *DealerName == "" || *DealerIP == "" || *JiraTask == "" || *UID == "" {
fmt.Println("Run with args!")
os.Exit(1)
}
pwd := genPwd()
hash := hashAndSalt(pwd)
generateconfigkeepalive := genKeepaliveConfig(*DealerIP, *DealerName, *JiraTask)
generateconfigshadow := genShadowConfig(*DealerName, hash, *UID)
print("\nData for dealer ", string(*DealerName), ":\n")
print("\nConfig for keepalived:\n", generateconfigkeepalive)
print("\nConfig for shadow:\n", generateconfigshadow)
print("\nUsername: ", *DealerName)
print("\nPassword: ", string(pwd))
print("\nHash: ", hash)
print("\n")
}
func genKeepaliveConfig (iprange string, name string, jira string) string {
var configtemplate strings.Builder
// Split data for separate ip
ip := strings.Split(iprange, ",")
for i := range ip {
tmp := "'" + ip[i] + "', #" + jira + " " + name + "\n"
configtemplate.WriteString(tmp)
}
return configtemplate.String()
}
func genShadowConfig (name string, hash string, uid string) string {
tmp := "road_sftp::dealer { '" + name + "':\n uid => '" + uid + "',\n password => '" + hash + "',\n}"
return tmp
}
func genPwd() []byte {
rand.Seed(time.Now().UnixNano())
all := strings.ToUpper(letters) +
letters +
digits + specials
buf := make([]byte, length)
buf[0] = digits[rand.Intn(len(digits))]
buf[1] = specials[rand.Intn(len(specials))]
for i := 2; i < length; i++ {
buf[i] = all[rand.Intn(len(all))]
}
rand.Shuffle(len(buf), func(i, j int) {
buf[i], buf[j] = buf[j], buf[i]
})
str := string(buf) // E.g. "3i[g0|)z"
return []byte(str)
}
func hashAndSalt(pwd []byte) string {
c := sha512_crypt.New()
magic := sha512_crypt.MagicPrefix
hash, err := c.Generate(pwd, []byte(magic))
if err != nil {
log.Println(err)
}
return string(hash)
}