forked from ubccr/goipa
/
hbac.go
252 lines (197 loc) · 6.67 KB
/
hbac.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
package ipa
import (
"encoding/json"
)
// HbacRecord encapsulates hbac data returned from ipa user commands
type HbacRecord struct {
Dn string `json:"dn"`
Description []string `json:"description"`
Enable []string `json:"ipaenabledflag"`
AllHosts []string `json:"hostcategory"`
Hosts []string `json:"memberhost_host"`
HostGroups []string `json:"memberhost_hostgroup"`
Services []string `json:"memberservice_hbacsvc"`
ServicesGroup []string `json:"memberservice_hbacsvcgroup"`
Users []string `json:"memberhost_user"`
}
// Fetch hbac details by call the FreeIPA user-show method
func (c *Client) HbacShow(uid string) (*HbacRecord, error) {
options := map[string]interface{}{
"no_members": false,
"all": true}
res, err := c.rpc("hbacrule_show", []string{uid}, options)
if err != nil {
return nil, err
}
var hbacRec HbacRecord
err = json.Unmarshal(res.Result.Data, &hbacRec)
if err != nil {
return nil, err
}
return &hbacRec, nil
}
// // Update ssh public keys for user uid. Returns the fingerprints on success.
// func (c *Client) UpdateSSHPubKeys(uid string, keys []string) ([]string, error) {
// options := map[string]interface{}{
// "no_members": false,
// "ipasshpubkey": keys,
// "all": false}
// res, err := c.rpc("user_mod", []string{uid}, options)
// if err != nil {
// return nil, err
// }
// var hbacRec HbacRecord
// err = json.Unmarshal(res.Result.Data, &hbacRec)
// if err != nil {
// return nil, err
// }
// return hbacRec.SSHPubKeyFps, nil
// }
// // Update mobile number. Currently will store only a single number. Any
// // existing numbers will be overwritten.
// func (c *Client) UpdateMobileNumber(uid string, number string) error {
// options := map[string]interface{}{
// "no_members": false,
// "mobile": []string{number},
// "all": false}
// _, err := c.rpc("user_mod", []string{uid}, options)
// if err != nil {
// return err
// }
// return nil
// }
// // Reset user password and return new random password
// func (c *Client) ResetPassword(uid string) (string, error) {
// options := map[string]interface{}{
// "no_members": false,
// "random": true,
// "all": true}
// res, err := c.rpc("user_mod", []string{uid}, options)
// if err != nil {
// return "", err
// }
// var hbacRec HbacRecord
// err = json.Unmarshal(res.Result.Data, &hbacRec)
// if err != nil {
// return "", err
// }
// if len(hbacRec.Randompassword) == 0 {
// return "", errors.New("ipa: failed to reset user password. empty random password returned")
// }
// return hbacRec.Randompassword, nil
// }
// // Change user password. This will run the passwd ipa command. Optionally
// // provide an OTP if required
// func (c *Client) ChangePassword(uid, old_passwd, new_passwd, otpcode string) error {
// options := map[string]interface{}{
// "current_password": old_passwd,
// "password": new_passwd,
// }
// if len(otpcode) > 0 {
// options["otp"] = otpcode
// }
// _, err := c.rpc("passwd", []string{uid}, options)
// if err != nil {
// return err
// }
// return nil
// }
// // Set user password. In FreeIPA when a password is first set or when a
// // password is later reset it is marked as immediately expired and requires the
// // owner to perform a password change. See here
// // https://www.freeipa.org/page/New_Passwords_Expired for more details. This
// // function exists to circumvent the "new passwords expired" feature of FreeIPA
// // and allow an administrator to set a new password for a user without it being
// // expired. This is acheived, for example, by first calling ResetPassword()
// // then immediately calling this function. *WARNING* See
// // https://www.freeipa.org/page/Self-Service_Password_Reset for security issues
// // and possible weaknesses of this approach.
// func (c *Client) SetPassword(uid, old_passwd, new_passwd, otpcode string) error {
// ipaUrl := fmt.Sprintf("https://%s/ipa/session/change_password", c.host)
// form := url.Values{
// "user": {uid},
// "otp": {otpcode},
// "old_password": {old_passwd},
// "new_password": {new_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))
// tr := &http.Transport{
// TLSClientConfig: &tls.Config{RootCAs: ipaCertPool}}
// client := &http.Client{Transport: tr}
// res, err := client.Do(req)
// if err != nil {
// return err
// }
// defer res.Body.Close()
// if res.StatusCode != 200 {
// return fmt.Errorf("ipa: change password failed with HTTP status code: %d", res.StatusCode)
// }
// status := res.Header.Get("x-ipa-pwchange-result")
// if status == "policy-error" {
// return &ErrPasswordPolicy{}
// } else if status == "invalid-password" {
// return &ErrInvalidPassword{}
// } else if strings.ToLower(status) != "ok" {
// return errors.New("ipa: change password failed. Unknown status")
// }
// return nil
// }
// // Update user authentication types.
// func (c *Client) SetAuthTypes(uid string, types []string) error {
// options := map[string]interface{}{
// "no_members": false,
// "ipauserauthtype": types,
// "all": false}
// if len(types) == 0 {
// options["ipauserauthtype"] = ""
// }
// _, err := c.rpc("user_mod", []string{uid}, options)
// if err != nil {
// return err
// }
// return nil
// }
// // Disable User Account
// func (c *Client) UserDisable(uid string) error {
// _, err := c.rpc("user_disable", []string{uid}, nil)
// if err != nil {
// return err
// }
// return nil
// }
// // Enable User Account
// func (c *Client) UserEnable(uid string) error {
// _, err := c.rpc("user_enable", []string{uid}, nil)
// if err != nil {
// return err
// }
// return nil
// }
// // Add new user. If random is true a random password will be created for the
// // user. Note this requires "User Administrators" Privilege in FreeIPA.
// func (c *Client) UserAdd(uid, email, first, last, homedir, shell string, random bool) (*HbacRecord, error) {
// var options = map[string]interface{}{
// "mail": email,
// "givenname": first,
// "sn": last}
// if len(homedir) > 0 {
// options["homedirectory"] = homedir
// }
// if len(shell) > 0 {
// options["loginshell"] = shell
// }
// if random {
// options["random"] = true
// }
// res, err := c.rpc("user_add", []string{uid}, options)
// if err != nil {
// return nil, err
// }
// var hbacRec HbacRecord
// err = json.Unmarshal(res.Result.Data, &hbacRec)
// if err != nil {
// return nil, err
// }
// return &hbacRec, nil
// }