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 "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(
147 mem_ctx
, context
, target_principal
);
148 code
= krb5_unparse_name_flags(context
,
150 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
153 DBG_WARNING("Failed to parse principal\n");
154 *error_string
= "String conversion failed";
155 return KRB5_KPASSWD_HARDERROR
;
158 if ((target_name
!= NULL
&& target_realm
== NULL
) ||
159 (target_name
== NULL
&& target_realm
!= NULL
)) {
160 krb5_free_principal(context
, target_principal
);
161 TALLOC_FREE(target_realm
);
162 SAFE_FREE(target_name
);
164 ok
= kpasswd_make_error_reply(mem_ctx
,
165 KRB5_KPASSWD_MALFORMED
,
166 "Realm and principal must be "
167 "both present, or neither "
171 *error_string
= "Failed to create reply";
172 return KRB5_KPASSWD_HARDERROR
;
177 if (target_name
!= NULL
&& target_realm
!= NULL
) {
178 TALLOC_FREE(target_realm
);
179 SAFE_FREE(target_name
);
181 krb5_free_principal(context
, target_principal
);
182 TALLOC_FREE(target_realm
);
183 SAFE_FREE(target_name
);
185 return kpasswd_change_password(kdc
,
193 num_components
= krb5_princ_size(context
, target_principal
);
194 if (num_components
>= 2) {
195 is_service_principal
= true;
196 code
= krb5_unparse_name_flags(context
,
198 KRB5_PRINCIPAL_UNPARSE_SHORT
,
199 &target_principal_string
);
201 code
= krb5_unparse_name(context
,
203 &target_principal_string
);
205 krb5_free_principal(context
, target_principal
);
207 ok
= kpasswd_make_error_reply(mem_ctx
,
208 KRB5_KPASSWD_MALFORMED
,
209 "Failed to parse principal",
212 *error_string
= "Failed to create reply";
213 return KRB5_KPASSWD_HARDERROR
;
217 status
= kpasswd_samdb_set_password(mem_ctx
,
218 kdc
->task
->event_ctx
,
221 is_service_principal
,
222 target_principal_string
,
226 if (!NT_STATUS_IS_OK(status
)) {
227 DBG_ERR("kpasswd_samdb_set_password failed - %s\n",
231 ok
= kpasswd_make_pwchange_reply(mem_ctx
,
237 *error_string
= "Failed to create reply";
238 return KRB5_KPASSWD_HARDERROR
;
244 krb5_error_code
kpasswd_handle_request(struct kdc_server
*kdc
,
246 struct gensec_security
*gensec_security
,
248 DATA_BLOB
*decoded_data
,
249 DATA_BLOB
*kpasswd_reply
,
250 const char **error_string
)
252 struct auth_session_info
*session_info
;
255 status
= gensec_session_info(gensec_security
,
258 if (!NT_STATUS_IS_OK(status
)) {
259 *error_string
= talloc_asprintf(mem_ctx
,
260 "gensec_session_info failed - "
263 return KRB5_KPASSWD_HARDERROR
;
271 ok
= convert_string_talloc_handle(mem_ctx
,
272 lpcfg_iconv_handle(kdc
->task
->lp_ctx
),
275 (const char *)decoded_data
->data
,
276 decoded_data
->length
,
277 (void **)&password
.data
,
280 *error_string
= "String conversion failed!";
281 DBG_WARNING("%s\n", *error_string
);
282 return KRB5_KPASSWD_HARDERROR
;
285 return kpasswd_change_password(kdc
,
292 case RFC3244_VERSION
: {
293 return kpasswd_set_password(kdc
,
301 return KRB5_KPASSWD_BAD_VERSION
;