.gitlab-ci-main.yml: Add safe.directory '*'
[Samba.git] / source4 / kdc / kpasswd-service-heimdal.c
blobc1c9be8cd1350210a0282270f813febbe8e92241
1 /*
2 Unix SMB/CIFS implementation.
4 Samba kpasswd implementation
6 Copyright (c) 2016 Andreas Schneider <asn@samba.org>
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.
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.
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/>.
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"
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_KERBEROS
36 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)
44 NTSTATUS status;
45 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
46 enum samPwdChangeReason reject_reason = SAM_PWD_CHANGE_NO_ERROR;
47 const char *reject_string = NULL;
48 struct samr_DomInfo1 *dominfo = NULL;
49 bool ok;
50 int ret;
53 * We're doing a password change (rather than a password set), so check
54 * that we were given an initial ticket.
56 ret = gensec_krb5_initial_ticket(gensec_security);
57 if (ret != 1) {
58 *error_string = "Expected an initial ticket";
59 return KRB5_KPASSWD_INITIAL_FLAG_NEEDED;
62 status = samdb_kpasswd_change_password(mem_ctx,
63 kdc->task->lp_ctx,
64 kdc->task->event_ctx,
65 session_info,
66 password,
67 &reject_reason,
68 &dominfo,
69 &reject_string,
70 &result);
71 if (!NT_STATUS_IS_OK(status)) {
72 ok = kpasswd_make_error_reply(mem_ctx,
73 KRB5_KPASSWD_ACCESSDENIED,
74 reject_string,
75 kpasswd_reply);
76 if (!ok) {
77 *error_string = "Failed to create reply";
78 return KRB5_KPASSWD_HARDERROR;
80 /* We want to send an an authenticated packet. */
81 return 0;
84 ok = kpasswd_make_pwchange_reply(mem_ctx,
85 result,
86 reject_reason,
87 dominfo,
88 kpasswd_reply);
89 if (!ok) {
90 *error_string = "Failed to create reply";
91 return KRB5_KPASSWD_HARDERROR;
94 return 0;
97 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)
105 krb5_context context = kdc->smb_krb5_context->krb5_context;
106 krb5_error_code code;
107 krb5_principal target_principal;
108 ChangePasswdDataMS chpw = {};
109 size_t chpw_len = 0;
110 DATA_BLOB password = data_blob_null;
111 enum samPwdChangeReason reject_reason = SAM_PWD_CHANGE_NO_ERROR;
112 struct samr_DomInfo1 *dominfo = NULL;
113 char *target_principal_string = NULL;
114 bool is_service_principal = false;
115 NTSTATUS status;
116 bool ok;
118 code = decode_ChangePasswdDataMS(decoded_data->data,
119 decoded_data->length,
120 &chpw,
121 &chpw_len);
122 if (code != 0) {
123 DBG_WARNING("decode_ChangePasswdDataMS failed\n");
124 ok = kpasswd_make_error_reply(mem_ctx,
125 KRB5_KPASSWD_MALFORMED,
126 "Failed to decode packet",
127 kpasswd_reply);
128 if (!ok) {
129 *error_string = "Failed to create reply";
130 return KRB5_KPASSWD_HARDERROR;
132 return 0;
135 ok = convert_string_talloc_handle(mem_ctx,
136 lpcfg_iconv_handle(kdc->task->lp_ctx),
137 CH_UTF8,
138 CH_UTF16,
139 chpw.newpasswd.data,
140 chpw.newpasswd.length,
141 &password.data,
142 &password.length);
143 if (!ok) {
144 free_ChangePasswdDataMS(&chpw);
145 DBG_WARNING("String conversion failed\n");
146 *error_string = "String conversion failed";
147 return KRB5_KPASSWD_HARDERROR;
150 if ((chpw.targname != NULL && chpw.targrealm == NULL) ||
151 (chpw.targname == NULL && chpw.targrealm != NULL)) {
152 free_ChangePasswdDataMS(&chpw);
153 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 if (!ok) {
159 *error_string = "Failed to create reply";
160 return KRB5_KPASSWD_HARDERROR;
162 return 0;
165 if (chpw.targname == NULL || chpw.targrealm == NULL) {
166 free_ChangePasswdDataMS(&chpw);
167 return kpasswd_change_password(kdc,
168 mem_ctx,
169 gensec_security,
170 session_info,
171 &password,
172 kpasswd_reply,
173 error_string);
175 code = krb5_build_principal_ext(context,
176 &target_principal,
177 strlen(*chpw.targrealm),
178 *chpw.targrealm,
180 if (code != 0) {
181 free_ChangePasswdDataMS(&chpw);
182 return kpasswd_make_error_reply(mem_ctx,
183 KRB5_KPASSWD_MALFORMED,
184 "Failed to parse principal",
185 kpasswd_reply);
187 code = copy_PrincipalName(chpw.targname,
188 &target_principal->name);
189 free_ChangePasswdDataMS(&chpw);
190 if (code != 0) {
191 krb5_free_principal(context, target_principal);
192 return kpasswd_make_error_reply(mem_ctx,
193 KRB5_KPASSWD_MALFORMED,
194 "Failed to parse principal",
195 kpasswd_reply);
198 if (target_principal->name.name_string.len >= 2) {
199 is_service_principal = true;
201 code = krb5_unparse_name_short(context,
202 target_principal,
203 &target_principal_string);
204 } else {
205 code = krb5_unparse_name(context,
206 target_principal,
207 &target_principal_string);
209 krb5_free_principal(context, target_principal);
210 if (code != 0) {
211 ok = kpasswd_make_error_reply(mem_ctx,
212 KRB5_KPASSWD_MALFORMED,
213 "Failed to parse principal",
214 kpasswd_reply);
215 if (!ok) {
216 krb5_xfree(target_principal_string);
217 *error_string = "Failed to create reply";
218 return KRB5_KPASSWD_HARDERROR;
222 status = kpasswd_samdb_set_password(mem_ctx,
223 kdc->task->event_ctx,
224 kdc->task->lp_ctx,
225 session_info,
226 is_service_principal,
227 target_principal_string,
228 &password,
229 &reject_reason,
230 &dominfo);
231 krb5_xfree(target_principal_string);
232 if (!NT_STATUS_IS_OK(status)) {
233 DBG_ERR("kpasswd_samdb_set_password failed - %s\n",
234 nt_errstr(status));
237 ok = kpasswd_make_pwchange_reply(mem_ctx,
238 status,
239 reject_reason,
240 dominfo,
241 kpasswd_reply);
242 if (!ok) {
243 *error_string = "Failed to create reply";
244 return KRB5_KPASSWD_HARDERROR;
247 return 0;
250 krb5_error_code kpasswd_handle_request(struct kdc_server *kdc,
251 TALLOC_CTX *mem_ctx,
252 struct gensec_security *gensec_security,
253 uint16_t verno,
254 DATA_BLOB *decoded_data,
255 DATA_BLOB *kpasswd_reply,
256 const char **error_string)
258 struct auth_session_info *session_info;
259 NTSTATUS status;
260 krb5_error_code code;
262 status = gensec_session_info(gensec_security,
263 mem_ctx,
264 &session_info);
265 if (!NT_STATUS_IS_OK(status)) {
266 *error_string = talloc_asprintf(mem_ctx,
267 "gensec_session_info failed - %s",
268 nt_errstr(status));
269 return KRB5_KPASSWD_HARDERROR;
273 * Since the kpasswd service shares its keys with the krbtgt, we might
274 * have received a TGT rather than a kpasswd ticket. We need to check
275 * the ticket type to ensure that TGTs cannot be misused in this manner.
277 code = kpasswd_check_non_tgt(session_info,
278 error_string);
279 if (code != 0) {
280 DBG_WARNING("%s\n", *error_string);
281 return code;
284 switch(verno) {
285 case KRB5_KPASSWD_VERS_CHANGEPW: {
286 DATA_BLOB password = data_blob_null;
287 bool ok;
289 ok = convert_string_talloc_handle(mem_ctx,
290 lpcfg_iconv_handle(kdc->task->lp_ctx),
291 CH_UTF8,
292 CH_UTF16,
293 decoded_data->data,
294 decoded_data->length,
295 &password.data,
296 &password.length);
297 if (!ok) {
298 *error_string = "String conversion failed!";
299 DBG_WARNING("%s\n", *error_string);
300 return KRB5_KPASSWD_HARDERROR;
303 return kpasswd_change_password(kdc,
304 mem_ctx,
305 gensec_security,
306 session_info,
307 &password,
308 kpasswd_reply,
309 error_string);
311 case KRB5_KPASSWD_VERS_SETPW: {
312 return kpasswd_set_password(kdc,
313 mem_ctx,
314 gensec_security,
315 session_info,
316 decoded_data,
317 kpasswd_reply,
318 error_string);
320 default:
321 *error_string = talloc_asprintf(mem_ctx,
322 "Protocol version %u not supported",
323 verno);
324 return KRB5_KPASSWD_BAD_VERSION;
327 return 0;