Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : Samba kpasswd implementation
5 :
6 : Copyright (c) 2016 Andreas Schneider <asn@samba.org>
7 :
8 : This program is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3 of the License, or
11 : (at your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program. If not, see <http://www.gnu.org/licenses/>.
20 : */
21 :
22 : #include "includes.h"
23 : #include "samba/service_task.h"
24 : #include "param/param.h"
25 : #include "auth/auth.h"
26 : #include "auth/gensec/gensec.h"
27 : #include "gensec_krb5_helpers.h"
28 : #include "kdc/kdc-server.h"
29 : #include "kdc/kpasswd_glue.h"
30 : #include "kdc/kpasswd-service.h"
31 : #include "kdc/kpasswd-helper.h"
32 :
33 : #undef DBGC_CLASS
34 : #define DBGC_CLASS DBGC_KERBEROS
35 :
36 33 : static krb5_error_code kpasswd_change_password(struct kdc_server *kdc,
37 : TALLOC_CTX *mem_ctx,
38 : const struct gensec_security *gensec_security,
39 : struct auth_session_info *session_info,
40 : DATA_BLOB *password,
41 : DATA_BLOB *kpasswd_reply,
42 : const char **error_string)
43 : {
44 0 : NTSTATUS status;
45 33 : NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
46 0 : enum samPwdChangeReason reject_reason;
47 33 : const char *reject_string = NULL;
48 0 : struct samr_DomInfo1 *dominfo;
49 0 : bool ok;
50 0 : int ret;
51 :
52 : /*
53 : * We're doing a password change (rather than a password set), so check
54 : * that we were given an initial ticket.
55 : */
56 33 : ret = gensec_krb5_initial_ticket(gensec_security);
57 33 : if (ret != 1) {
58 2 : *error_string = "Expected an initial ticket";
59 2 : return KRB5_KPASSWD_INITIAL_FLAG_NEEDED;
60 : }
61 :
62 31 : status = samdb_kpasswd_change_password(mem_ctx,
63 31 : kdc->task->lp_ctx,
64 31 : kdc->task->event_ctx,
65 : session_info,
66 : password,
67 : &reject_reason,
68 : &dominfo,
69 : &reject_string,
70 : &result);
71 31 : if (!NT_STATUS_IS_OK(status)) {
72 0 : ok = kpasswd_make_error_reply(mem_ctx,
73 : KRB5_KPASSWD_ACCESSDENIED,
74 : reject_string,
75 : kpasswd_reply);
76 0 : if (!ok) {
77 0 : *error_string = "Failed to create reply";
78 0 : return KRB5_KPASSWD_HARDERROR;
79 : }
80 : /* We want to send an an authenticated packet. */
81 0 : return 0;
82 : }
83 :
84 31 : ok = kpasswd_make_pwchange_reply(mem_ctx,
85 : result,
86 : reject_reason,
87 : dominfo,
88 : kpasswd_reply);
89 31 : if (!ok) {
90 0 : *error_string = "Failed to create reply";
91 0 : return KRB5_KPASSWD_HARDERROR;
92 : }
93 :
94 31 : return 0;
95 : }
96 :
97 30 : static krb5_error_code kpasswd_set_password(struct kdc_server *kdc,
98 : TALLOC_CTX *mem_ctx,
99 : const struct gensec_security *gensec_security,
100 : struct auth_session_info *session_info,
101 : DATA_BLOB *decoded_data,
102 : DATA_BLOB *kpasswd_reply,
103 : const char **error_string)
104 : {
105 30 : krb5_context context = kdc->smb_krb5_context->krb5_context;
106 0 : krb5_error_code code;
107 0 : krb5_principal target_principal;
108 30 : ChangePasswdDataMS chpw = {};
109 30 : size_t chpw_len = 0;
110 30 : DATA_BLOB password = data_blob_null;
111 30 : enum samPwdChangeReason reject_reason = SAM_PWD_CHANGE_NO_ERROR;
112 30 : struct samr_DomInfo1 *dominfo = NULL;
113 30 : char *target_principal_string = NULL;
114 30 : bool is_service_principal = false;
115 0 : NTSTATUS status;
116 0 : bool ok;
117 :
118 30 : code = decode_ChangePasswdDataMS(decoded_data->data,
119 : decoded_data->length,
120 : &chpw,
121 : &chpw_len);
122 30 : if (code != 0) {
123 0 : DBG_WARNING("decode_ChangePasswdDataMS failed\n");
124 0 : ok = kpasswd_make_error_reply(mem_ctx,
125 : KRB5_KPASSWD_MALFORMED,
126 : "Failed to decode packet",
127 : kpasswd_reply);
128 0 : if (!ok) {
129 0 : *error_string = "Failed to create reply";
130 0 : return KRB5_KPASSWD_HARDERROR;
131 : }
132 0 : return 0;
133 : }
134 :
135 30 : ok = convert_string_talloc_handle(mem_ctx,
136 30 : lpcfg_iconv_handle(kdc->task->lp_ctx),
137 : CH_UTF8,
138 : CH_UTF16,
139 30 : chpw.newpasswd.data,
140 : chpw.newpasswd.length,
141 : &password.data,
142 : &password.length);
143 30 : if (!ok) {
144 0 : free_ChangePasswdDataMS(&chpw);
145 0 : DBG_WARNING("String conversion failed\n");
146 0 : *error_string = "String conversion failed";
147 0 : return KRB5_KPASSWD_HARDERROR;
148 : }
149 :
150 30 : if ((chpw.targname != NULL && chpw.targrealm == NULL) ||
151 29 : (chpw.targname == NULL && chpw.targrealm != NULL)) {
152 2 : free_ChangePasswdDataMS(&chpw);
153 2 : ok = kpasswd_make_error_reply(mem_ctx,
154 : KRB5_KPASSWD_MALFORMED,
155 : "Realm and principal must be "
156 : "both present, or neither present",
157 : kpasswd_reply);
158 2 : if (!ok) {
159 0 : *error_string = "Failed to create reply";
160 0 : return KRB5_KPASSWD_HARDERROR;
161 : }
162 2 : return 0;
163 : }
164 :
165 28 : if (chpw.targname == NULL || chpw.targrealm == NULL) {
166 10 : free_ChangePasswdDataMS(&chpw);
167 10 : return kpasswd_change_password(kdc,
168 : mem_ctx,
169 : gensec_security,
170 : session_info,
171 : &password,
172 : kpasswd_reply,
173 : error_string);
174 : }
175 18 : code = krb5_build_principal_ext(context,
176 : &target_principal,
177 18 : strlen(*chpw.targrealm),
178 18 : *chpw.targrealm,
179 : 0);
180 18 : if (code != 0) {
181 0 : free_ChangePasswdDataMS(&chpw);
182 0 : return kpasswd_make_error_reply(mem_ctx,
183 : KRB5_KPASSWD_MALFORMED,
184 : "Failed to parse principal",
185 : kpasswd_reply);
186 : }
187 18 : code = copy_PrincipalName(chpw.targname,
188 18 : &target_principal->name);
189 18 : free_ChangePasswdDataMS(&chpw);
190 18 : if (code != 0) {
191 0 : krb5_free_principal(context, target_principal);
192 0 : return kpasswd_make_error_reply(mem_ctx,
193 : KRB5_KPASSWD_MALFORMED,
194 : "Failed to parse principal",
195 : kpasswd_reply);
196 : }
197 :
198 18 : if (target_principal->name.name_string.len >= 2) {
199 0 : is_service_principal = true;
200 :
201 0 : code = krb5_unparse_name_short(context,
202 : target_principal,
203 : &target_principal_string);
204 : } else {
205 18 : code = krb5_unparse_name(context,
206 : target_principal,
207 : &target_principal_string);
208 : }
209 18 : krb5_free_principal(context, target_principal);
210 18 : if (code != 0) {
211 0 : ok = kpasswd_make_error_reply(mem_ctx,
212 : KRB5_KPASSWD_MALFORMED,
213 : "Failed to parse principal",
214 : kpasswd_reply);
215 0 : if (!ok) {
216 0 : *error_string = "Failed to create reply";
217 0 : return KRB5_KPASSWD_HARDERROR;
218 : }
219 : }
220 :
221 18 : status = kpasswd_samdb_set_password(mem_ctx,
222 18 : kdc->task->event_ctx,
223 18 : kdc->task->lp_ctx,
224 : session_info,
225 : is_service_principal,
226 : target_principal_string,
227 : &password,
228 : &reject_reason,
229 : &dominfo);
230 18 : if (!NT_STATUS_IS_OK(status)) {
231 14 : DBG_ERR("kpasswd_samdb_set_password failed - %s\n",
232 : nt_errstr(status));
233 : }
234 :
235 18 : ok = kpasswd_make_pwchange_reply(mem_ctx,
236 : status,
237 : reject_reason,
238 : dominfo,
239 : kpasswd_reply);
240 18 : if (!ok) {
241 0 : *error_string = "Failed to create reply";
242 0 : return KRB5_KPASSWD_HARDERROR;
243 : }
244 :
245 18 : return 0;
246 : }
247 :
248 58 : krb5_error_code kpasswd_handle_request(struct kdc_server *kdc,
249 : TALLOC_CTX *mem_ctx,
250 : struct gensec_security *gensec_security,
251 : uint16_t verno,
252 : DATA_BLOB *decoded_data,
253 : DATA_BLOB *kpasswd_reply,
254 : const char **error_string)
255 : {
256 0 : struct auth_session_info *session_info;
257 0 : NTSTATUS status;
258 0 : krb5_error_code code;
259 :
260 58 : status = gensec_session_info(gensec_security,
261 : mem_ctx,
262 : &session_info);
263 58 : if (!NT_STATUS_IS_OK(status)) {
264 0 : *error_string = talloc_asprintf(mem_ctx,
265 : "gensec_session_info failed - %s",
266 : nt_errstr(status));
267 0 : return KRB5_KPASSWD_HARDERROR;
268 : }
269 :
270 : /*
271 : * Since the kpasswd service shares its keys with the krbtgt, we might
272 : * have received a TGT rather than a kpasswd ticket. We need to check
273 : * the ticket type to ensure that TGTs cannot be misused in this manner.
274 : */
275 58 : code = kpasswd_check_non_tgt(session_info,
276 : error_string);
277 58 : if (code != 0) {
278 4 : DBG_WARNING("%s\n", *error_string);
279 4 : return code;
280 : }
281 :
282 54 : switch(verno) {
283 24 : case KRB5_KPASSWD_VERS_CHANGEPW: {
284 24 : DATA_BLOB password = data_blob_null;
285 0 : bool ok;
286 :
287 24 : ok = convert_string_talloc_handle(mem_ctx,
288 24 : lpcfg_iconv_handle(kdc->task->lp_ctx),
289 : CH_UTF8,
290 : CH_UTF16,
291 24 : decoded_data->data,
292 : decoded_data->length,
293 : &password.data,
294 : &password.length);
295 24 : if (!ok) {
296 1 : *error_string = "String conversion failed!";
297 1 : DBG_WARNING("%s\n", *error_string);
298 1 : return KRB5_KPASSWD_HARDERROR;
299 : }
300 :
301 23 : return kpasswd_change_password(kdc,
302 : mem_ctx,
303 : gensec_security,
304 : session_info,
305 : &password,
306 : kpasswd_reply,
307 : error_string);
308 : }
309 30 : case KRB5_KPASSWD_VERS_SETPW: {
310 30 : return kpasswd_set_password(kdc,
311 : mem_ctx,
312 : gensec_security,
313 : session_info,
314 : decoded_data,
315 : kpasswd_reply,
316 : error_string);
317 : }
318 0 : default:
319 0 : *error_string = talloc_asprintf(mem_ctx,
320 : "Protocol version %u not supported",
321 : verno);
322 0 : return KRB5_KPASSWD_BAD_VERSION;
323 : }
324 :
325 : return 0;
326 : }
|