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 "smbd/service_task.h"
24 #include "param/param.h"
25 #include "auth/auth.h"
26 #include "auth/gensec/gensec.h"
27 #include "kdc/kdc-server.h"
28 #include "kdc/kpasswd_glue.h"
29 #include "kdc/kpasswd-service.h"
30 #include "kdc/kpasswd-helper.h"
32 #define RFC3244_VERSION 0xff80
34 krb5_error_code
decode_krb5_setpw_req(const krb5_data
*code
,
35 krb5_data
**password_out
,
36 krb5_principal
*target_out
);
38 static krb5_error_code
kpasswd_change_password(struct kdc_server
*kdc
,
40 struct auth_session_info
*session_info
,
42 DATA_BLOB
*kpasswd_reply
,
43 const char **error_string
)
46 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
47 enum samPwdChangeReason reject_reason
;
48 const char *reject_string
= NULL
;
49 struct samr_DomInfo1
*dominfo
;
52 status
= samdb_kpasswd_change_password(mem_ctx
,
62 if (!NT_STATUS_IS_OK(status
)) {
63 ok
= kpasswd_make_error_reply(mem_ctx
,
64 KRB5_KPASSWD_ACCESSDENIED
,
68 *error_string
= "Failed to create reply";
69 return KRB5_KPASSWD_HARDERROR
;
71 /* We want to send an an authenticated packet. */
75 ok
= kpasswd_make_pwchange_reply(mem_ctx
,
81 *error_string
= "Failed to create reply";
82 return KRB5_KPASSWD_HARDERROR
;
88 static krb5_error_code
kpasswd_set_password(struct kdc_server
*kdc
,
90 struct auth_session_info
*session_info
,
91 DATA_BLOB
*decoded_data
,
92 DATA_BLOB
*kpasswd_reply
,
93 const char **error_string
)
95 krb5_context context
= kdc
->smb_krb5_context
->krb5_context
;
97 krb5_data
*k_clear_data
;
98 krb5_principal target_principal
;
101 char *target_realm
= NULL
;
102 char *target_name
= NULL
;
103 char *target_principal_string
= NULL
;
104 bool is_service_principal
= false;
106 size_t num_components
;
107 enum samPwdChangeReason reject_reason
= SAM_PWD_CHANGE_NO_ERROR
;
108 struct samr_DomInfo1
*dominfo
= NULL
;
111 k_dec_data
.length
= decoded_data
->length
;
112 k_dec_data
.data
= (char *)decoded_data
->data
;
114 code
= decode_krb5_setpw_req(&k_dec_data
,
118 DBG_WARNING("decode_krb5_setpw_req failed: %s\n",
119 error_message(code
));
120 ok
= kpasswd_make_error_reply(mem_ctx
,
121 KRB5_KPASSWD_MALFORMED
,
122 "Failed to decode packet",
125 *error_string
= "Failed to create reply";
126 return KRB5_KPASSWD_HARDERROR
;
131 ok
= convert_string_talloc_handle(mem_ctx
,
132 lpcfg_iconv_handle(kdc
->task
->lp_ctx
),
135 (const char *)k_clear_data
->data
,
136 k_clear_data
->length
,
137 (void **)&password
.data
,
139 krb5_free_data(context
, k_clear_data
);
141 DBG_WARNING("String conversion failed\n");
142 *error_string
= "String conversion failed";
143 return KRB5_KPASSWD_HARDERROR
;
146 target_realm
= smb_krb5_principal_get_realm(context
, target_principal
);
147 code
= krb5_unparse_name_flags(context
,
149 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
152 DBG_WARNING("Failed to parse principal\n");
153 *error_string
= "String conversion failed";
154 return KRB5_KPASSWD_HARDERROR
;
157 if ((target_name
!= NULL
&& target_realm
== NULL
) ||
158 (target_name
== NULL
&& target_realm
!= NULL
)) {
159 krb5_free_principal(context
, target_principal
);
160 SAFE_FREE(target_realm
);
161 SAFE_FREE(target_name
);
163 ok
= kpasswd_make_error_reply(mem_ctx
,
164 KRB5_KPASSWD_MALFORMED
,
165 "Realm and principal must be "
166 "both present, or neither "
170 *error_string
= "Failed to create reply";
171 return KRB5_KPASSWD_HARDERROR
;
176 if (target_name
!= NULL
&& target_realm
!= NULL
) {
177 SAFE_FREE(target_realm
);
178 SAFE_FREE(target_name
);
180 krb5_free_principal(context
, target_principal
);
181 SAFE_FREE(target_realm
);
182 SAFE_FREE(target_name
);
184 return kpasswd_change_password(kdc
,
192 num_components
= krb5_princ_size(context
, target_principal
);
193 if (num_components
>= 2) {
194 is_service_principal
= true;
195 code
= krb5_unparse_name_flags(context
,
197 KRB5_PRINCIPAL_UNPARSE_SHORT
,
198 &target_principal_string
);
200 code
= krb5_unparse_name(context
,
202 &target_principal_string
);
204 krb5_free_principal(context
, target_principal
);
206 ok
= kpasswd_make_error_reply(mem_ctx
,
207 KRB5_KPASSWD_MALFORMED
,
208 "Failed to parse principal",
211 *error_string
= "Failed to create reply";
212 return KRB5_KPASSWD_HARDERROR
;
216 status
= kpasswd_samdb_set_password(mem_ctx
,
217 kdc
->task
->event_ctx
,
220 is_service_principal
,
221 target_principal_string
,
225 if (!NT_STATUS_IS_OK(status
)) {
226 DBG_ERR("kpasswd_samdb_set_password failed - %s\n",
230 ok
= kpasswd_make_pwchange_reply(mem_ctx
,
236 *error_string
= "Failed to create reply";
237 return KRB5_KPASSWD_HARDERROR
;
243 krb5_error_code
kpasswd_handle_request(struct kdc_server
*kdc
,
245 struct gensec_security
*gensec_security
,
247 DATA_BLOB
*decoded_data
,
248 DATA_BLOB
*kpasswd_reply
,
249 const char **error_string
)
251 struct auth_session_info
*session_info
;
254 status
= gensec_session_info(gensec_security
,
257 if (!NT_STATUS_IS_OK(status
)) {
258 *error_string
= talloc_asprintf(mem_ctx
,
259 "gensec_session_info failed - "
262 return KRB5_KPASSWD_HARDERROR
;
270 ok
= convert_string_talloc_handle(mem_ctx
,
271 lpcfg_iconv_handle(kdc
->task
->lp_ctx
),
274 (const char *)decoded_data
->data
,
275 decoded_data
->length
,
276 (void **)&password
.data
,
279 *error_string
= "String conversion failed!";
280 DBG_WARNING("%s\n", *error_string
);
281 return KRB5_KPASSWD_HARDERROR
;
284 return kpasswd_change_password(kdc
,
291 case RFC3244_VERSION
: {
292 return kpasswd_set_password(kdc
,
300 return KRB5_KPASSWD_BAD_VERSION
;