forked from ubccr/goipa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.go
264 lines (212 loc) · 6.91 KB
/
user.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
// 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
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
)
// UserRecord encapsulates user data returned from ipa user commands
type UserRecord struct {
Dn string `json:"dn"`
First IpaString `json:"givenname"`
Last IpaString `json:"sn"`
DisplayName IpaString `json:"displayname"`
Principal IpaString `json:"krbprincipalname"`
Uid IpaString `json:"uid"`
UidNumber IpaString `json:"uidnumber"`
GidNumber IpaString `json:"gidnumber"`
Groups []string `json:"memberof_group"`
SSHPubKeys []string `json:"ipasshpubkey"`
SSHPubKeyFps []string `json:"sshpubkeyfp"`
AuthTypes []string `json:"ipauserauthtype"`
HasKeytab bool `json:"has_keytab"`
HasPassword bool `json:"has_password"`
Locked bool `json:"nsaccountlock"`
HomeDir IpaString `json:"homedirectory"`
Email IpaString `json:"mail"`
Mobile IpaString `json:"mobile"`
Shell IpaString `json:"loginshell"`
SudoRules IpaString `json:"memberofindirect_sudorule"`
HbacRules IpaString `json:"memberofindirect_hbacrule"`
LastPasswdChange IpaDateTime `json:"krblastpwdchange"`
PasswdExpire IpaDateTime `json:"krbpasswordexpiration"`
PrincipalExpire IpaDateTime `json:"krbprincipalexpiration"`
LastLoginSuccess IpaDateTime `json:"krblastsuccessfulauth"`
LastLoginFail IpaDateTime `json:"krblastfailedauth"`
Randompassword string `json:"randompassword"`
}
// Returns true if OTP is the only authentication type enabled
func (u *UserRecord) OTPOnly() bool {
if len(u.AuthTypes) == 1 && u.AuthTypes[0] == "otp" {
return true
}
return false
}
// Returns true if the User is in group
func (u *UserRecord) HasGroup(group string) bool {
for _, g := range u.Groups {
if g == group {
return true
}
}
return false
}
// Add a user, call FreeIPA user_add method
func (c *Client) UserAdd(uid string, giveName string, sn string, cn string, pwd string) (*UserRecord, error) {
options := map[string]interface{}{
"givenname": giveName,
"sn": sn,
"cn": cn,
"userpassword": pwd,
}
res, err := c.rpc("user_add", []string{uid}, options)
if err != nil {
return nil, err
}
var userRec UserRecord
err = json.Unmarshal(res.Result.Data, &userRec)
if err != nil {
return nil, err
}
return &userRec, nil
}
// Fetch user details by call the FreeIPA user-show method
func (c *Client) UserShow(uid string) (*UserRecord, error) {
options := map[string]interface{}{
"no_members": false,
"all": true}
res, err := c.rpc("user_show", []string{uid}, options)
if err != nil {
return nil, err
}
var userRec UserRecord
err = json.Unmarshal(res.Result.Data, &userRec)
if err != nil {
return nil, err
}
return &userRec, 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 userRec UserRecord
err = json.Unmarshal(res.Result.Data, &userRec)
if err != nil {
return nil, err
}
return userRec.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 userRec UserRecord
err = json.Unmarshal(res.Result.Data, &userRec)
if err != nil {
return "", err
}
if len(userRec.Randompassword) == 0 {
return "", errors.New("ipa: failed to reset user password. empty random password returned")
}
return userRec.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. This function exists to allow an
// administrator to use mokey to send a user a link in an email and allow the
// user to set a new password without it being expired. This is acheived by
// first calling ResetPassword() then immediately calling this function.
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
}