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/>.
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"
34 #define DBGC_CLASS DBGC_KERBEROS
36 static krb5_error_code
kpasswd_change_password(struct kdc_server
*kdc
,
38 const struct gensec_security
*gensec_security
,
39 struct auth_session_info
*session_info
,
41 DATA_BLOB
*kpasswd_reply
,
42 const char **error_string
)
45 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
46 enum samPwdChangeReason reject_reason
;
47 const char *reject_string
= NULL
;
48 struct samr_DomInfo1
*dominfo
;
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
);
58 *error_string
= "Expected an initial ticket";
59 return KRB5_KPASSWD_INITIAL_FLAG_NEEDED
;
62 status
= samdb_kpasswd_change_password(mem_ctx
,
71 if (!NT_STATUS_IS_OK(status
)) {
72 ok
= kpasswd_make_error_reply(mem_ctx
,
73 KRB5_KPASSWD_ACCESSDENIED
,
77 *error_string
= "Failed to create reply";
78 return KRB5_KPASSWD_HARDERROR
;
80 /* We want to send an an authenticated packet. */
84 ok
= kpasswd_make_pwchange_reply(mem_ctx
,
90 *error_string
= "Failed to create reply";
91 return KRB5_KPASSWD_HARDERROR
;
97 static krb5_error_code
kpasswd_set_password(struct kdc_server
*kdc
,
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
= {};
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;
118 code
= decode_ChangePasswdDataMS(decoded_data
->data
,
119 decoded_data
->length
,
123 DBG_WARNING("decode_ChangePasswdDataMS failed\n");
124 ok
= kpasswd_make_error_reply(mem_ctx
,
125 KRB5_KPASSWD_MALFORMED
,
126 "Failed to decode packet",
129 *error_string
= "Failed to create reply";
130 return KRB5_KPASSWD_HARDERROR
;
135 ok
= convert_string_talloc_handle(mem_ctx
,
136 lpcfg_iconv_handle(kdc
->task
->lp_ctx
),
140 chpw
.newpasswd
.length
,
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",
159 *error_string
= "Failed to create reply";
160 return KRB5_KPASSWD_HARDERROR
;
165 if (chpw
.targname
== NULL
|| chpw
.targrealm
== NULL
) {
166 free_ChangePasswdDataMS(&chpw
);
167 return kpasswd_change_password(kdc
,
175 code
= krb5_build_principal_ext(context
,
177 strlen(*chpw
.targrealm
),
181 free_ChangePasswdDataMS(&chpw
);
182 return kpasswd_make_error_reply(mem_ctx
,
183 KRB5_KPASSWD_MALFORMED
,
184 "Failed to parse principal",
187 code
= copy_PrincipalName(chpw
.targname
,
188 &target_principal
->name
);
189 free_ChangePasswdDataMS(&chpw
);
191 krb5_free_principal(context
, target_principal
);
192 return kpasswd_make_error_reply(mem_ctx
,
193 KRB5_KPASSWD_MALFORMED
,
194 "Failed to parse principal",
198 if (target_principal
->name
.name_string
.len
>= 2) {
199 is_service_principal
= true;
201 code
= krb5_unparse_name_short(context
,
203 &target_principal_string
);
205 code
= krb5_unparse_name(context
,
207 &target_principal_string
);
209 krb5_free_principal(context
, target_principal
);
211 ok
= kpasswd_make_error_reply(mem_ctx
,
212 KRB5_KPASSWD_MALFORMED
,
213 "Failed to parse principal",
216 *error_string
= "Failed to create reply";
217 return KRB5_KPASSWD_HARDERROR
;
221 status
= kpasswd_samdb_set_password(mem_ctx
,
222 kdc
->task
->event_ctx
,
225 is_service_principal
,
226 target_principal_string
,
230 if (!NT_STATUS_IS_OK(status
)) {
231 DBG_ERR("kpasswd_samdb_set_password failed - %s\n",
235 ok
= kpasswd_make_pwchange_reply(mem_ctx
,
241 *error_string
= "Failed to create reply";
242 return KRB5_KPASSWD_HARDERROR
;
248 krb5_error_code
kpasswd_handle_request(struct kdc_server
*kdc
,
250 struct gensec_security
*gensec_security
,
252 DATA_BLOB
*decoded_data
,
253 DATA_BLOB
*kpasswd_reply
,
254 const char **error_string
)
256 struct auth_session_info
*session_info
;
258 krb5_error_code code
;
260 status
= gensec_session_info(gensec_security
,
263 if (!NT_STATUS_IS_OK(status
)) {
264 *error_string
= talloc_asprintf(mem_ctx
,
265 "gensec_session_info failed - %s",
267 return KRB5_KPASSWD_HARDERROR
;
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.
275 code
= kpasswd_check_non_tgt(session_info
,
278 DBG_WARNING("%s\n", *error_string
);
283 case KRB5_KPASSWD_VERS_CHANGEPW
: {
284 DATA_BLOB password
= data_blob_null
;
287 ok
= convert_string_talloc_handle(mem_ctx
,
288 lpcfg_iconv_handle(kdc
->task
->lp_ctx
),
292 decoded_data
->length
,
296 *error_string
= "String conversion failed!";
297 DBG_WARNING("%s\n", *error_string
);
298 return KRB5_KPASSWD_HARDERROR
;
301 return kpasswd_change_password(kdc
,
309 case KRB5_KPASSWD_VERS_SETPW
: {
310 return kpasswd_set_password(kdc
,
319 *error_string
= talloc_asprintf(mem_ctx
,
320 "Protocol version %u not supported",
322 return KRB5_KPASSWD_BAD_VERSION
;