-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathipa.go
448 lines (367 loc) · 10.2 KB
/
ipa.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2015 Andrew E. Bruno. All rights reserved.
// Use of this source code is governed by a BSD style
// license that can be found in the LICENSE file.
// Package ipa is a Go client library for FreeIPA
package ipa
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"strings"
"time"
"github.com/go-ini/ini"
"github.com/jcmturner/gokrb5/v8/client"
"github.com/jcmturner/gokrb5/v8/config"
"github.com/jcmturner/gokrb5/v8/credentials"
"github.com/jcmturner/gokrb5/v8/keytab"
"github.com/jcmturner/gokrb5/v8/spnego"
log "github.com/sirupsen/logrus"
)
const (
DefaultKerbConf = "/etc/krb5.conf"
IpaClientVersion = "2.237"
IpaDatetimeFormat = "20060102150405Z"
)
var (
ipaDefaultHost string
ipaDefaultRealm string
ipaCertPool *x509.CertPool
ipaSessionPattern = regexp.MustCompile(`^ipa_session=([^;]+);`)
// ErrPasswordPolicy is returned when a password does not conform to the password policy
ErrPasswordPolicy = errors.New("password does not conform to policy")
// ErrInvalidPassword is returned when a password is invalid
ErrInvalidPassword = errors.New("invalid current password")
// ErrExpiredPassword is returned when a password is expired
ErrExpiredPassword = errors.New("password expired")
// ErrUnauthorized is returned when user is not authorized
ErrUnauthorized = errors.New("unauthorized")
// ErrUserExists is returned when user account already exists
ErrUserExists = errors.New("unauthorized")
)
// FreeIPA Client
type Client struct {
host string
realm string
keyTab string
sessionID string
sticky bool
httpClient *http.Client
krbClient *client.Client
}
// FreeIPA api options map
type Options map[string]interface{}
// FreeIPA error
type IpaError struct {
Message string
Code int
}
// Result returned from a FreeIPA JSON rpc call
type Result struct {
Summary string `json:"summary"`
Value interface{} `json:"value"`
Data json.RawMessage `json:"result"`
}
// Response returned from a FreeIPA JSON rpc call
type Response struct {
Error *IpaError `json:"error"`
ID int `json:"id"`
Principal string `json:"principal"`
Version string `json:"version"`
Result *Result `json:"result"`
}
func init() {
// If ca.crt for ipa exists, use it as the cert pool
// otherwise default to system root ca.
pem, err := ioutil.ReadFile("/etc/ipa/ca.crt")
if err == nil {
ipaCertPool = x509.NewCertPool()
if !ipaCertPool.AppendCertsFromPEM(pem) {
ipaCertPool = nil
}
}
// Load default IPA host
cfg, err := ini.Load("/etc/ipa/default.conf")
if err == nil {
ipaServerURL, err := url.Parse(cfg.Section("global").Key("xmlrpc_uri").MustString("http://localhost"))
if err == nil {
ipaDefaultHost = ipaServerURL.Host
}
ipaDefaultRealm = cfg.Section("global").Key("realm").MustString("LOCAL")
}
}
func newHTTPClient() *http.Client {
return &http.Client{
Timeout: 1 * time.Minute,
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{RootCAs: ipaCertPool},
DisableCompression: false,
},
}
}
// New default IPA Client using host and realm from /etc/ipa/default.conf
func NewDefaultClient() *Client {
return &Client{
host: ipaDefaultHost,
realm: ipaDefaultRealm,
sticky: true,
httpClient: newHTTPClient(),
}
}
// New default IPA Client with existing sessionID using host and realm from /etc/ipa/default.conf
func NewDefaultClientWithSession(sessionID string) *Client {
return &Client{
host: ipaDefaultHost,
realm: ipaDefaultRealm,
httpClient: newHTTPClient(),
sticky: true,
sessionID: sessionID,
}
}
// New IPA Client with host and realm
func NewClient(host, realm string) *Client {
return &Client{
host: host,
realm: realm,
sticky: true,
httpClient: newHTTPClient(),
}
}
// New IPA Client with host, realm and custom http client
func NewClientCustomHttp(host, realm string, httpClient *http.Client) *Client {
return &Client{
host: host,
realm: realm,
sticky: true,
httpClient: httpClient,
}
}
func (e *IpaError) Error() string {
return fmt.Sprintf("ipa: error %d - %s", e.Code, e.Message)
}
// Call FreeIPA API with method, params and options
func (c *Client) rpc(method string, params []string, options Options) (*Response, error) {
if options == nil {
options = Options{}
}
options["version"] = IpaClientVersion
data := []interface{}{
params,
options,
}
payload := Options{
"id": 0,
"method": method,
"params": data,
}
b, err := json.Marshal(payload)
if err != nil {
return nil, err
}
ipaUrl := fmt.Sprintf("https://%s/ipa/json", c.host)
if len(c.sessionID) > 0 {
ipaUrl = fmt.Sprintf("https://%s/ipa/session/json", c.host)
}
req, err := http.NewRequest("POST", ipaUrl, bytes.NewBuffer(b))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Referer", fmt.Sprintf("https://%s/ipa/xml", c.host))
if len(c.sessionID) > 0 {
// If session is set, use the session id
req.Header.Set("Cookie", fmt.Sprintf("ipa_session=%s", c.sessionID))
} else if c.krbClient != nil {
// use Kerberos auth (SPNEGO)
spnego.SetSPNEGOHeader(c.krbClient, req, "")
}
if log.IsLevelEnabled(log.TraceLevel) {
dump, _ := httputil.DumpRequestOut(req, true)
log.Tracef("FreeIPA RPC request: %s", dump)
}
res, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("IPA RPC called failed with HTTP status code: %d", res.StatusCode)
}
if err = c.setSessionID(res); err != nil {
return nil, err
}
rawJson, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
log.Tracef("FreeIPA JSON response: %s", string(rawJson))
var ipaRes Response
err = json.Unmarshal(rawJson, &ipaRes)
if err != nil {
return nil, err
}
if ipaRes.Error != nil {
return nil, ipaRes.Error
}
return &ipaRes, nil
}
// Returns FreeIPA server hostname
func (c *Client) Host() string {
return c.host
}
// Returns FreeIPA realm
func (c *Client) Realm() string {
return c.realm
}
// Ping FreeIPA server to check connection
func (c *Client) Ping() (*Response, error) {
res, err := c.rpc("ping", []string{}, nil)
if err != nil {
return nil, err
}
return res, nil
}
// Return current FreeIPA sessionID
func (c *Client) SessionID() string {
return c.sessionID
}
// Clears out FreeIPA session id
func (c *Client) ClearSession() {
c.sessionID = ""
}
// Set stick sessions.
func (c *Client) StickySession(enable bool) {
c.sticky = enable
}
// Set FreeIPA sessionID from http response cookie
func (c *Client) setSessionID(res *http.Response) error {
if !c.sticky {
return nil
}
cookie := res.Header.Get("Set-Cookie")
if len(cookie) == 0 {
return nil
}
ipaSession := ""
matches := ipaSessionPattern.FindStringSubmatch(cookie)
if len(matches) == 2 {
ipaSession = matches[1]
}
if len(ipaSession) == 32 || strings.HasPrefix(ipaSession, "MagBearerToken") {
c.sessionID = ipaSession
} else {
return errors.New("invalid set-cookie header")
}
return nil
}
// Login to FreeIPA using web API with uid/passwd and set the FreeIPA session
// id on the client for subsequent requests.
func (c *Client) RemoteLogin(uid, passwd string) error {
ipaUrl := fmt.Sprintf("https://%s/ipa/session/login_password", c.host)
form := url.Values{"user": {uid}, "password": {passwd}}
req, err := http.NewRequest("POST", ipaUrl, strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Referer", fmt.Sprintf("https://%s/ipa", c.host))
res, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if log.IsLevelEnabled(log.TraceLevel) {
dump, _ := httputil.DumpResponse(res, true)
log.Tracef("FreeIPA RemoteLogin response: %s", dump)
}
if res.StatusCode == 401 && res.Header.Get("X-IPA-Rejection-Reason") == "password-expired" {
return ErrExpiredPassword
}
if res.StatusCode == 401 && res.Header.Get("X-IPA-Rejection-Reason") == "invalid-password" {
return ErrInvalidPassword
}
if res.StatusCode == 401 {
return ErrUnauthorized
}
if res.StatusCode != 200 {
return fmt.Errorf("IPA login failed with HTTP status code: %d", res.StatusCode)
}
if err = c.setSessionID(res); err != nil {
return err
}
return nil
}
// Login to FreeIPA using local kerberos login username and password
func (c *Client) Login(username, password string) error {
cfg, err := config.Load(DefaultKerbConf)
if err != nil {
return err
}
cl := client.NewWithPassword(username, c.realm, password, cfg)
err = cl.Login()
if err != nil {
return err
}
c.krbClient = cl
return nil
}
// Login to FreeIPA using local kerberos login with keytab and username
func (c *Client) LoginWithKeytab(ktab, username string) error {
cfg, err := config.Load(DefaultKerbConf)
if err != nil {
return err
}
kt, err := keytab.Load(ktab)
if err != nil {
return err
}
cl := client.NewWithKeytab(username, c.realm, kt, cfg)
err = cl.Login()
if err != nil {
return err
}
c.krbClient = cl
return nil
}
// Login to FreeIPA using credentials cache
func (c *Client) LoginFromCCache(cpath string) error {
cfg, err := config.Load(DefaultKerbConf)
if err != nil {
return err
}
ccache, err := credentials.LoadCCache(cpath)
if err != nil {
return err
}
cl, err := client.NewFromCCache(ccache, cfg, client.AssumePreAuthentication(true))
if err != nil {
return err
}
err = cl.Login()
if err != nil {
return err
}
c.krbClient = cl
return nil
}
// Parse a FreeIPA datetime. Datetimes in FreeIPA are returned using a
// class-hint system. Values are stored as an array with a single element
// indicating the type and value, for example, '[{"__datetime__": "YYYY-MM-DDTHH:MM:SSZ"]}'
func ParseDateTime(str string) time.Time {
dt, err := time.Parse(IpaDatetimeFormat, str)
if err != nil {
return time.Time{}
}
return dt
}