-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
245 lines (226 loc) · 5.26 KB
/
main.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
)
var (
version string
revision string
fs = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
dbHostName = fs.String(
"h",
"localhost",
"DB Hostname",
)
dbPort = fs.Int(
"p",
5432,
"DB Port",
)
dbName = fs.String(
"d",
"",
"DB Name",
)
dbSchema = fs.String(
"s",
"",
"DB Schema",
)
dbUser = fs.String(
"u",
"",
"DB User",
)
dbPassword = fs.String(
"w",
"",
"DB Password",
)
dbMaxOpenConns = fs.Int(
"db-max-open-conns",
5,
"DB max open connections",
)
dbMaxIdleConns = fs.Int(
"db-max-idle-conns",
2,
"DB max idle connections",
)
suffix = fs.String(
"suffix",
"",
"Suffix for the LDAP",
)
rootdn = fs.String(
"root-dn",
"",
"Root dn for the LDAP",
)
rootpw = fs.String(
"root-pw",
"",
"Root password for the LDAP",
)
bindAddress = fs.String(
"b",
"127.0.0.1:8389",
"Bind address",
)
logLevel = fs.String(
"log-level",
"info",
"Log level, on of: debug, info, warn, error, alert",
)
pprofServer = fs.String(
"pprof",
"",
"Bind address of pprof server (Don't start the server with default)",
)
gomaxprocs = fs.Int(
"gomaxprocs",
0,
"GOMAXPROCS (Use CPU num with default)",
)
passThroughLDAPDomain = fs.String(
"pass-through-ldap-domain",
"",
"Pass-through/LDAP: Domain for pass-through/LDAP",
)
passThroughLDAPServer = fs.String(
"pass-through-ldap-server",
"",
"Pass-through/LDAP: Server address and port (e.g. myldap:389)",
)
passThroughLDAPSearchBase = fs.String(
"pass-through-ldap-search-base",
"",
"Pass-through/LDAP: Search base",
)
passThroughLDAPFilter = fs.String(
"pass-through-ldap-filter",
"",
"Pass-through/LDAP: Filter for finding an user (e.g. (cn=%u))",
)
passThroughLDAPBindDN = fs.String(
"pass-through-ldap-bind-dn",
"",
"Pass-through/LDAP: Bind DN",
)
passThroughLDAPPassword = fs.String(
"pass-through-ldap-password",
"",
"Pass-through/LDAP: Bind password",
)
passThroughLDAPScope = fs.String(
"pass-through-ldap-scope",
"sub",
"Pass-through/LDAP: Search scope, on of: base, one, sub",
)
passThroughLDAPTimeout = fs.Int(
"pass-through-ldap-timeout",
10,
"Pass-through/LDAP: Timeout seconds",
)
migrationEnabled = fs.Bool(
"migration",
false,
"Enable migration mode which means LDAP server accepts add/modify operational attributes (default false)",
)
defaultPPolicyDN = fs.String(
"default-ppolicy-dn",
"",
"DN of the default password policy entry (e.g. cn=standard-policy,ou=Policies,dc=example,dc=com)",
)
defaultPageSize = fs.Int(
"default-page-size",
500,
"Default page size for search (default 500)",
)
)
type arrayFlags []string
func (i *arrayFlags) String() string {
return strings.Join(*i, "\n")
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
var customSchema arrayFlags
func main() {
fs.Var(&customSchema, "schema", "Additional/overwriting custom schema")
var aclFlags arrayFlags
fs.Var(&aclFlags, "acl", `Simple ACL: the format is <DN(User, Group or empty(everyone))>:<Scope(R, W or RW)>:<Invisible Attributes> (e.g. cn=reader,dc=example,dc=com:R:userPassword,telephoneNumber)`)
fmt.Fprintf(os.Stdout, "ldap-pg %s (rev: %s)\n", version, revision)
fs.Usage = func() {
_, exe := filepath.Split(os.Args[0])
fmt.Fprintf(os.Stderr, "\nUsage:\n\n %s [options]\n\nOptions:\n\n", exe)
fs.PrintDefaults()
}
if err := fs.Parse(os.Args[1:]); err != nil {
log.Fatalf("error: Cannot parse the args: %v, err: %s", os.Args[1:], err)
}
if len(os.Args) == 1 {
fs.Usage()
return
}
rootPW := *rootpw
*rootpw = ""
passThroughConfig := &PassThroughConfig{}
if *passThroughLDAPDomain != "" {
passThroughConfig.Add(*passThroughLDAPDomain, &LDAPPassThroughClient{
Server: *passThroughLDAPServer,
SearchBase: *passThroughLDAPSearchBase,
Timeout: *passThroughLDAPTimeout,
Filter: *passThroughLDAPFilter,
BindDN: *passThroughLDAPBindDN,
Password: *passThroughLDAPPassword,
Scope: *passThroughLDAPScope,
})
}
var acl []string
if aclFlags != nil {
acl = strings.Split(aclFlags.String(), "\n")
}
// When CTRL+C, SIGINT and SIGTERM signal occurs
// Then stop server gracefully
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
server := NewServer(&ServerConfig{
DBHostName: *dbHostName,
DBPort: *dbPort,
DBName: *dbName,
DBSchema: *dbSchema,
DBUser: *dbUser,
DBPassword: *dbPassword,
DBMaxOpenConns: *dbMaxOpenConns,
DBMaxIdleConns: *dbMaxIdleConns,
Suffix: *suffix,
RootDN: *rootdn,
RootPW: rootPW,
BindAddress: *bindAddress,
PassThroughConfig: passThroughConfig,
LogLevel: *logLevel,
PProfServer: *pprofServer,
GoMaxProcs: *gomaxprocs,
MigrationEnabled: *migrationEnabled,
QueryTranslator: "default",
SimpleACL: acl,
DefaultPPolicyDN: *defaultPPolicyDN,
DefaultPageSize: int32(*defaultPageSize),
})
go server.Start()
<-ctx.Done()
_, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
log.Printf("info: Shutdown ldap-pg...")
server.Stop()
}