forked from hamano/lb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
123 lines (117 loc) · 2.51 KB
/
setup.go
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"fmt"
"log"
"strings"
"strconv"
"github.com/urfave/cli"
openldap "github.com/hamano/golang-openldap"
)
func setupBase(c *cli.Context) error {
baseDN := c.String("b")
ldap, err := openldap.Initialize(c.Args().First())
if err != nil {
log.Fatal("initialize error: ", err)
}
ldap.SetOption(openldap.LDAP_OPT_PROTOCOL_VERSION, openldap.LDAP_VERSION3)
err = ldap.Bind(c.String("D"), c.String("w"))
if err != nil {
log.Fatal("bind error: ", err)
}
attrs := map[string][]string{
"objectClass": {"dcObject", "organization"},
"o": {"lb"},
}
if ! c.Bool("q") {
fmt.Printf("Adding base entry: %s\n", baseDN)
}
err = ldap.Add(baseDN, attrs)
if err != nil {
log.Fatal("add error: ", err)
}
if ! c.Bool("q") {
fmt.Printf("Added base entry: %s\n", baseDN)
}
ldap.Close()
return nil
}
var setupPersonFlags = []cli.Flag {
cli.StringFlag {
Name: "cn",
Value: "user",
Usage: "cn attribute",
},
cli.StringFlag {
Name: "sn",
Value: "",
Usage: "sn attribute",
},
cli.StringFlag {
Name: "password, userpassword, userPassword",
Value: "secret",
Usage: "userPassword attribute",
},
cli.IntFlag {
Name: "first",
Value: 1,
Usage: "first id",
},
cli.IntFlag {
Name: "last",
Value: 0,
Usage: "last id",
},
}
func setupPerson(c *cli.Context) error {
ldap, err := openldap.Initialize(c.Args().First())
if err != nil {
log.Fatal("initialize error: ", err)
}
ldap.SetOption(openldap.LDAP_OPT_PROTOCOL_VERSION, openldap.LDAP_VERSION3)
err = ldap.Bind(c.String("D"), c.String("w"))
if err != nil {
log.Fatal("bind error: ", err)
}
last := c.Int("last")
if last > 0 {
for i := c.Int("first"); i <= last; i++ {
var cn string
if strings.Contains(c.String("cn"), "%") {
cn = fmt.Sprintf(c.String("cn"), i)
}else {
cn = c.String("cn") + strconv.Itoa(i)
}
setupPersonOne(c, ldap, cn)
}
}else{
setupPersonOne(c, ldap, c.String("cn"))
}
ldap.Close()
return nil
}
func setupPersonOne(c *cli.Context, ldap *openldap.Ldap, cn string) error {
baseDN := c.String("b")
sn := c.String("sn")
if sn == "" {
sn = cn
}
userPassword := c.String("userpassword")
dn := fmt.Sprintf("cn=%s,%s", cn, baseDN)
attrs := map[string][]string{
"objectClass": {"person"},
"cn": {cn},
"sn": {sn},
"userPassword": {userPassword},
}
if ! c.Bool("q") {
fmt.Printf("Adding person entry: %s\n", dn)
}
err := ldap.Add(dn, attrs)
if err != nil {
log.Fatal("add error: ", err)
}
if ! c.Bool("q") {
fmt.Printf("Added person entry: %s\n", dn)
}
return nil
}