s4-waf: install the rest of our python files
[nivanova/samba-autobuild/.git] / source4 / kdc / kpasswdd.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    kpasswd Server implementation
5
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7    Copyright (C) Andrew Tridgell        2005
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "smbd/service_task.h"
25 #include "lib/events/events.h"
26 #include "lib/socket/socket.h"
27 #include "lib/tsocket/tsocket.h"
28 #include "system/network.h"
29 #include "../lib/util/dlinklist.h"
30 #include "lib/ldb/include/ldb.h"
31 #include "auth/gensec/gensec.h"
32 #include "auth/credentials/credentials.h"
33 #include "auth/credentials/credentials_krb5.h"
34 #include "auth/auth.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "rpc_server/dcerpc_server.h"
37 #include "rpc_server/samr/proto.h"
38 #include "libcli/security/security.h"
39 #include "param/param.h"
40 #include "kdc/kdc.h"
41
42 /* TODO: remove all SAMBA4_INTERNAL_HEIMDAL stuff from this file */
43 #ifdef SAMBA4_INTERNAL_HEIMDAL
44 #include "heimdal_build/kpasswdd-glue.h"
45 #endif
46
47 /* Return true if there is a valid error packet formed in the error_blob */
48 static bool kpasswdd_make_error_reply(struct kdc_server *kdc,
49                                      TALLOC_CTX *mem_ctx,
50                                      uint16_t result_code,
51                                      const char *error_string,
52                                      DATA_BLOB *error_blob)
53 {
54         char *error_string_utf8;
55         size_t len;
56
57         DEBUG(result_code ? 3 : 10, ("kpasswdd: %s\n", error_string));
58
59         if (!push_utf8_talloc(mem_ctx, &error_string_utf8, error_string, &len)) {
60                 return false;
61         }
62
63         *error_blob = data_blob_talloc(mem_ctx, NULL, 2 + len + 1);
64         if (!error_blob->data) {
65                 return false;
66         }
67         RSSVAL(error_blob->data, 0, result_code);
68         memcpy(error_blob->data + 2, error_string_utf8, len + 1);
69         return true;
70 }
71
72 /* Return true if there is a valid error packet formed in the error_blob */
73 static bool kpasswdd_make_unauth_error_reply(struct kdc_server *kdc,
74                                             TALLOC_CTX *mem_ctx,
75                                             uint16_t result_code,
76                                             const char *error_string,
77                                             DATA_BLOB *error_blob)
78 {
79         bool ret;
80         int kret;
81         DATA_BLOB error_bytes;
82         krb5_data k5_error_bytes, k5_error_blob;
83         ret = kpasswdd_make_error_reply(kdc, mem_ctx, result_code, error_string,
84                                        &error_bytes);
85         if (!ret) {
86                 return false;
87         }
88         k5_error_bytes.data = error_bytes.data;
89         k5_error_bytes.length = error_bytes.length;
90         kret = krb5_mk_error(kdc->smb_krb5_context->krb5_context,
91                              result_code, NULL, &k5_error_bytes,
92                              NULL, NULL, NULL, NULL, &k5_error_blob);
93         if (kret) {
94                 return false;
95         }
96         *error_blob = data_blob_talloc(mem_ctx, k5_error_blob.data, k5_error_blob.length);
97         krb5_data_free(&k5_error_blob);
98         if (!error_blob->data) {
99                 return false;
100         }
101         return true;
102 }
103
104 static bool kpasswd_make_pwchange_reply(struct kdc_server *kdc,
105                                         TALLOC_CTX *mem_ctx,
106                                         NTSTATUS status,
107                                         enum samPwdChangeReason reject_reason,
108                                         struct samr_DomInfo1 *dominfo,
109                                         DATA_BLOB *error_blob)
110 {
111         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
112                 return kpasswdd_make_error_reply(kdc, mem_ctx,
113                                                 KRB5_KPASSWD_ACCESSDENIED,
114                                                 "No such user when changing password",
115                                                 error_blob);
116         }
117         if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
118                 return kpasswdd_make_error_reply(kdc, mem_ctx,
119                                                 KRB5_KPASSWD_ACCESSDENIED,
120                                                 "Not permitted to change password",
121                                                 error_blob);
122         }
123         if (dominfo && NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION)) {
124                 const char *reject_string;
125                 switch (reject_reason) {
126                 case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT:
127                         reject_string = talloc_asprintf(mem_ctx, "Password too short, password must be at least %d characters long",
128                                                         dominfo->min_password_length);
129                         break;
130                 case SAM_PWD_CHANGE_NOT_COMPLEX:
131                         reject_string = "Password does not meet complexity requirements";
132                         break;
133                 case SAM_PWD_CHANGE_PWD_IN_HISTORY:
134                         reject_string = "Password is already in password history";
135                         break;
136                 default:
137                         reject_string = talloc_asprintf(mem_ctx, "Password must be at least %d characters long, and cannot match any of your %d previous passwords",
138                                                         dominfo->min_password_length, dominfo->password_history_length);
139                         break;
140                 }
141                 return kpasswdd_make_error_reply(kdc, mem_ctx,
142                                                 KRB5_KPASSWD_SOFTERROR,
143                                                 reject_string,
144                                                 error_blob);
145         }
146         if (!NT_STATUS_IS_OK(status)) {
147                 return kpasswdd_make_error_reply(kdc, mem_ctx,
148                                                  KRB5_KPASSWD_HARDERROR,
149                                                  talloc_asprintf(mem_ctx, "failed to set password: %s", nt_errstr(status)),
150                                                  error_blob);
151
152         }
153         return kpasswdd_make_error_reply(kdc, mem_ctx, KRB5_KPASSWD_SUCCESS,
154                                         "Password changed",
155                                         error_blob);
156 }
157
158 /*
159    A user password change
160
161    Return true if there is a valid error packet (or success) formed in
162    the error_blob
163 */
164 static bool kpasswdd_change_password(struct kdc_server *kdc,
165                                      TALLOC_CTX *mem_ctx,
166                                      struct auth_session_info *session_info,
167                                      const DATA_BLOB *password,
168                                      DATA_BLOB *reply)
169 {
170         NTSTATUS status;
171         enum samPwdChangeReason reject_reason;
172         struct samr_DomInfo1 *dominfo;
173         struct ldb_context *samdb;
174
175         samdb = samdb_connect(mem_ctx, kdc->task->event_ctx, kdc->task->lp_ctx, system_session(kdc->task->lp_ctx));
176         if (!samdb) {
177                 return kpasswdd_make_error_reply(kdc, mem_ctx,
178                                                 KRB5_KPASSWD_HARDERROR,
179                                                 "Failed to open samdb",
180                                                 reply);
181         }
182
183         DEBUG(3, ("Changing password of %s\\%s (%s)\n",
184                   session_info->server_info->domain_name,
185                   session_info->server_info->account_name,
186                   dom_sid_string(mem_ctx, session_info->security_token->user_sid)));
187
188         /* User password change */
189         status = samdb_set_password_sid(samdb, mem_ctx,
190                                         session_info->security_token->user_sid,
191                                         password, NULL, NULL,
192                                         true, /* this is a user password change */
193                                         &reject_reason,
194                                         &dominfo);
195         return kpasswd_make_pwchange_reply(kdc, mem_ctx,
196                                            status,
197                                            reject_reason,
198                                            dominfo,
199                                            reply);
200
201 }
202
203 static bool kpasswd_process_request(struct kdc_server *kdc,
204                                     TALLOC_CTX *mem_ctx,
205                                     struct gensec_security *gensec_security,
206                                     uint16_t version,
207                                     DATA_BLOB *input,
208                                     DATA_BLOB *reply)
209 {
210         struct auth_session_info *session_info;
211         size_t pw_len;
212
213         if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security,
214                                                  &session_info))) {
215                 return kpasswdd_make_error_reply(kdc, mem_ctx,
216                                                 KRB5_KPASSWD_HARDERROR,
217                                                 "gensec_session_info failed!",
218                                                 reply);
219         }
220
221         switch (version) {
222         case KRB5_KPASSWD_VERS_CHANGEPW:
223         {
224                 DATA_BLOB password;
225                 if (!convert_string_talloc_convenience(mem_ctx, lp_iconv_convenience(kdc->task->lp_ctx),
226                                                CH_UTF8, CH_UTF16,
227                                                (const char *)input->data,
228                                                input->length,
229                                                (void **)&password.data, &pw_len, false)) {
230                         return false;
231                 }
232                 password.length = pw_len;
233
234                 return kpasswdd_change_password(kdc, mem_ctx, session_info,
235                                                 &password, reply);
236                 break;
237         }
238         case KRB5_KPASSWD_VERS_SETPW:
239         {
240                 NTSTATUS status;
241                 enum samPwdChangeReason reject_reason = SAM_PWD_CHANGE_NO_ERROR;
242                 struct samr_DomInfo1 *dominfo = NULL;
243                 struct ldb_context *samdb;
244                 struct ldb_message *msg;
245                 krb5_context context = kdc->smb_krb5_context->krb5_context;
246
247                 ChangePasswdDataMS chpw;
248                 DATA_BLOB password;
249
250                 krb5_principal principal;
251                 char *set_password_on_princ;
252                 struct ldb_dn *set_password_on_dn;
253                 bool service_principal_name = false;
254
255                 size_t len;
256                 int ret;
257
258                 msg = ldb_msg_new(mem_ctx);
259                 if (!msg) {
260                         return false;
261                 }
262
263                 ret = decode_ChangePasswdDataMS(input->data, input->length,
264                                                 &chpw, &len);
265                 if (ret) {
266                         return kpasswdd_make_error_reply(kdc, mem_ctx,
267                                                         KRB5_KPASSWD_MALFORMED,
268                                                         "failed to decode password change structure",
269                                                         reply);
270                 }
271
272                 if (!convert_string_talloc_convenience(mem_ctx, lp_iconv_convenience(kdc->task->lp_ctx),
273                                                CH_UTF8, CH_UTF16,
274                                                (const char *)chpw.newpasswd.data,
275                                                chpw.newpasswd.length,
276                                                (void **)&password.data, &pw_len, false)) {
277                         free_ChangePasswdDataMS(&chpw);
278                         return false;
279                 }
280
281                 password.length = pw_len;
282
283                 if ((chpw.targname && !chpw.targrealm)
284                     || (!chpw.targname && chpw.targrealm)) {
285                         return kpasswdd_make_error_reply(kdc, mem_ctx,
286                                                         KRB5_KPASSWD_MALFORMED,
287                                                         "Realm and principal must be both present, or neither present",
288                                                         reply);
289                 }
290                 if (chpw.targname && chpw.targrealm) {
291 #ifdef SAMBA4_INTERNAL_HEIMDAL
292                         if (_krb5_principalname2krb5_principal(kdc->smb_krb5_context->krb5_context,
293                                                                &principal, *chpw.targname,
294                                                                *chpw.targrealm) != 0) {
295                                 free_ChangePasswdDataMS(&chpw);
296                                 return kpasswdd_make_error_reply(kdc, mem_ctx,
297                                                                 KRB5_KPASSWD_MALFORMED,
298                                                                 "failed to extract principal to set",
299                                                                 reply);
300
301                         }
302 #else /* SAMBA4_INTERNAL_HEIMDAL */
303                                 return kpasswdd_make_error_reply(kdc, mem_ctx,
304                                                                 KRB5_KPASSWD_BAD_VERSION,
305                                                                 "Operation Not Implemented",
306                                                                 reply);
307 #endif /* SAMBA4_INTERNAL_HEIMDAL */
308                 } else {
309                         free_ChangePasswdDataMS(&chpw);
310                         return kpasswdd_change_password(kdc, mem_ctx, session_info,
311                                                         &password, reply);
312                 }
313                 free_ChangePasswdDataMS(&chpw);
314
315                 if (principal->name.name_string.len >= 2) {
316                         service_principal_name = true;
317
318                         /* We use this, rather than 'no realm' flag,
319                          * as we don't want to accept a password
320                          * change on a principal from another realm */
321
322                         if (krb5_unparse_name_short(context, principal, &set_password_on_princ) != 0) {
323                                 krb5_free_principal(context, principal);
324                                 return kpasswdd_make_error_reply(kdc, mem_ctx,
325                                                                  KRB5_KPASSWD_MALFORMED,
326                                                                  "krb5_unparse_name failed!",
327                                                                  reply);
328                         }
329                 } else {
330                         if (krb5_unparse_name(context, principal, &set_password_on_princ) != 0) {
331                                 krb5_free_principal(context, principal);
332                                 return kpasswdd_make_error_reply(kdc, mem_ctx,
333                                                                  KRB5_KPASSWD_MALFORMED,
334                                                                  "krb5_unparse_name failed!",
335                                                                  reply);
336                         }
337                 }
338                 krb5_free_principal(context, principal);
339
340                 samdb = samdb_connect(mem_ctx, kdc->task->event_ctx, kdc->task->lp_ctx, session_info);
341                 if (!samdb) {
342                         return kpasswdd_make_error_reply(kdc, mem_ctx,
343                                                          KRB5_KPASSWD_HARDERROR,
344                                                          "Unable to open database!",
345                                                          reply);
346                 }
347
348                 DEBUG(3, ("%s\\%s (%s) is changing password of %s\n",
349                           session_info->server_info->domain_name,
350                           session_info->server_info->account_name,
351                           dom_sid_string(mem_ctx, session_info->security_token->user_sid),
352                           set_password_on_princ));
353                 ret = ldb_transaction_start(samdb);
354                 if (ret) {
355                         status = NT_STATUS_TRANSACTION_ABORTED;
356                         return kpasswd_make_pwchange_reply(kdc, mem_ctx,
357                                                            status,
358                                                            SAM_PWD_CHANGE_NO_ERROR,
359                                                            NULL,
360                                                            reply);
361                 }
362
363                 if (service_principal_name) {
364                         status = crack_service_principal_name(samdb, mem_ctx,
365                                                               set_password_on_princ,
366                                                               &set_password_on_dn, NULL);
367                 } else {
368                         status = crack_user_principal_name(samdb, mem_ctx,
369                                                            set_password_on_princ,
370                                                            &set_password_on_dn, NULL);
371                 }
372                 free(set_password_on_princ);
373                 if (!NT_STATUS_IS_OK(status)) {
374                         ldb_transaction_cancel(samdb);
375                         return kpasswd_make_pwchange_reply(kdc, mem_ctx,
376                                                            status,
377                                                            SAM_PWD_CHANGE_NO_ERROR,
378                                                            NULL,
379                                                            reply);
380                 }
381
382                 msg = ldb_msg_new(mem_ctx);
383                 if (msg == NULL) {
384                         ldb_transaction_cancel(samdb);
385                         status = NT_STATUS_NO_MEMORY;
386                 } else {
387                         msg->dn = ldb_dn_copy(msg, set_password_on_dn);
388                         if (!msg->dn) {
389                                 status = NT_STATUS_NO_MEMORY;
390                         }
391                 }
392
393                 if (NT_STATUS_IS_OK(status)) {
394                         /* Admin password set */
395                         status = samdb_set_password(samdb, mem_ctx,
396                                                     set_password_on_dn, NULL,
397                                                     msg, &password, NULL, NULL,
398                                                     false, /* this is not a user password change */
399                                                     &reject_reason, &dominfo);
400                 }
401
402                 if (NT_STATUS_IS_OK(status)) {
403                         /* modify the samdb record */
404                         ret = dsdb_replace(samdb, msg, 0);
405                         if (ret != 0) {
406                                 DEBUG(2,("Failed to modify record to set password on %s: %s\n",
407                                          ldb_dn_get_linearized(msg->dn),
408                                          ldb_errstring(samdb)));
409                                 status = NT_STATUS_ACCESS_DENIED;
410                         }
411                 }
412                 if (NT_STATUS_IS_OK(status)) {
413                         ret = ldb_transaction_commit(samdb);
414                         if (ret != 0) {
415                                 DEBUG(1,("Failed to commit transaction to set password on %s: %s\n",
416                                          ldb_dn_get_linearized(msg->dn),
417                                          ldb_errstring(samdb)));
418                                 status = NT_STATUS_TRANSACTION_ABORTED;
419                         }
420                 } else {
421                         ldb_transaction_cancel(samdb);
422                 }
423                 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
424                                                    status,
425                                                    reject_reason,
426                                                    dominfo,
427                                                    reply);
428         }
429         default:
430                 return kpasswdd_make_error_reply(kdc, mem_ctx,
431                                                  KRB5_KPASSWD_BAD_VERSION,
432                                                  talloc_asprintf(mem_ctx,
433                                                                  "Protocol version %u not supported",
434                                                                  version),
435                                                  reply);
436         }
437         return true;
438 }
439
440 bool kpasswdd_process(struct kdc_server *kdc,
441                       TALLOC_CTX *mem_ctx,
442                       DATA_BLOB *input,
443                       DATA_BLOB *reply,
444                       struct tsocket_address *peer_addr,
445                       struct tsocket_address *my_addr,
446                       int datagram_reply)
447 {
448         bool ret;
449         const uint16_t header_len = 6;
450         uint16_t len;
451         uint16_t ap_req_len;
452         uint16_t krb_priv_len;
453         uint16_t version;
454         NTSTATUS nt_status;
455         DATA_BLOB ap_req, krb_priv_req;
456         DATA_BLOB krb_priv_rep = data_blob(NULL, 0);
457         DATA_BLOB ap_rep = data_blob(NULL, 0);
458         DATA_BLOB kpasswd_req, kpasswd_rep;
459         struct cli_credentials *server_credentials;
460         struct gensec_security *gensec_security;
461         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
462
463         char *keytab_name;
464
465         if (!tmp_ctx) {
466                 return false;
467         }
468
469         /* Be parinoid.  We need to ensure we don't just let the
470          * caller lead us into a buffer overflow */
471         if (input->length <= header_len) {
472                 talloc_free(tmp_ctx);
473                 return false;
474         }
475
476         len = RSVAL(input->data, 0);
477         if (input->length != len) {
478                 talloc_free(tmp_ctx);
479                 return false;
480         }
481
482         /* There are two different versions of this protocol so far,
483          * plus others in the standards pipe.  Fortunetly they all
484          * take a very similar framing */
485         version = RSVAL(input->data, 2);
486         ap_req_len = RSVAL(input->data, 4);
487         if ((ap_req_len >= len) || (ap_req_len + header_len) >= len) {
488                 talloc_free(tmp_ctx);
489                 return false;
490         }
491
492         krb_priv_len = len - ap_req_len;
493         ap_req = data_blob_const(&input->data[header_len], ap_req_len);
494         krb_priv_req = data_blob_const(&input->data[header_len + ap_req_len], krb_priv_len);
495
496         server_credentials = cli_credentials_init(tmp_ctx);
497         if (!server_credentials) {
498                 DEBUG(1, ("Failed to init server credentials\n"));
499                 return false;
500         }
501
502         /* We want the credentials subsystem to use the krb5 context
503          * we already have, rather than a new context */
504         cli_credentials_set_krb5_context(server_credentials, kdc->smb_krb5_context);
505         cli_credentials_set_conf(server_credentials, kdc->task->lp_ctx);
506
507         keytab_name = talloc_asprintf(server_credentials, "HDB:samba4&%p", kdc->base_ctx);
508
509         cli_credentials_set_username(server_credentials, "kadmin/changepw", CRED_SPECIFIED);
510         ret = cli_credentials_set_keytab_name(server_credentials, kdc->task->event_ctx, kdc->task->lp_ctx, keytab_name, CRED_SPECIFIED);
511         if (ret != 0) {
512                 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
513                                                        KRB5_KPASSWD_HARDERROR,
514                                                        talloc_asprintf(mem_ctx,
515                                                                        "Failed to obtain server credentials for kadmin/changepw: %s\n",
516                                                                        nt_errstr(nt_status)),
517                                                        &krb_priv_rep);
518                 ap_rep.length = 0;
519                 if (ret) {
520                         goto reply;
521                 }
522                 talloc_free(tmp_ctx);
523                 return ret;
524         }
525
526         /* We don't strictly need to call this wrapper, and could call
527          * gensec_server_start directly, as we have no need for NTLM
528          * and we have a PAC, but this ensures that the wrapper can be
529          * safely extended for other helpful things in future */
530         nt_status = samba_server_gensec_start(tmp_ctx, kdc->task->event_ctx,
531                                               kdc->task->msg_ctx,
532                                               kdc->task->lp_ctx,
533                                               server_credentials,
534                                               "kpasswd",
535                                               &gensec_security);
536         if (!NT_STATUS_IS_OK(nt_status)) {
537                 talloc_free(tmp_ctx);
538                 return false;
539         }
540
541         /* The kerberos PRIV packets include these addresses.  MIT
542          * clients check that they are present */
543 #if 0
544         /* Skip this part for now, it breaks with a NetAPP filer and
545          * in any case where the client address is behind NAT.  If
546          * older MIT clients need this, we might have to insert more
547          * complex code */
548
549         nt_status = gensec_set_local_address(gensec_security, peer_addr);
550         if (!NT_STATUS_IS_OK(nt_status)) {
551                 talloc_free(tmp_ctx);
552                 return false;
553         }
554 #endif
555
556         nt_status = gensec_set_local_address(gensec_security, my_addr);
557         if (!NT_STATUS_IS_OK(nt_status)) {
558                 talloc_free(tmp_ctx);
559                 return false;
560         }
561
562         /* We want the GENSEC wrap calls to generate PRIV tokens */
563         gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
564
565         nt_status = gensec_start_mech_by_name(gensec_security, "krb5");
566         if (!NT_STATUS_IS_OK(nt_status)) {
567                 talloc_free(tmp_ctx);
568                 return false;
569         }
570
571         /* Accept the AP-REQ and generate teh AP-REP we need for the reply */
572         nt_status = gensec_update(gensec_security, tmp_ctx, ap_req, &ap_rep);
573         if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
574
575                 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
576                                                        KRB5_KPASSWD_HARDERROR,
577                                                        talloc_asprintf(mem_ctx,
578                                                                        "gensec_update failed: %s",
579                                                                        nt_errstr(nt_status)),
580                                                        &krb_priv_rep);
581                 ap_rep.length = 0;
582                 if (ret) {
583                         goto reply;
584                 }
585                 talloc_free(tmp_ctx);
586                 return ret;
587         }
588
589         /* Extract the data from the KRB-PRIV half of the message */
590         nt_status = gensec_unwrap(gensec_security, tmp_ctx, &krb_priv_req, &kpasswd_req);
591         if (!NT_STATUS_IS_OK(nt_status)) {
592                 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
593                                                        KRB5_KPASSWD_HARDERROR,
594                                                        talloc_asprintf(mem_ctx,
595                                                                        "gensec_unwrap failed: %s",
596                                                                        nt_errstr(nt_status)),
597                                                        &krb_priv_rep);
598                 ap_rep.length = 0;
599                 if (ret) {
600                         goto reply;
601                 }
602                 talloc_free(tmp_ctx);
603                 return ret;
604         }
605
606         /* Figure out something to do with it (probably changing a password...) */
607         ret = kpasswd_process_request(kdc, tmp_ctx,
608                                       gensec_security,
609                                       version,
610                                       &kpasswd_req, &kpasswd_rep);
611         if (!ret) {
612                 /* Argh! */
613                 return false;
614         }
615
616         /* And wrap up the reply: This ensures that the error message
617          * or success can be verified by the client */
618         nt_status = gensec_wrap(gensec_security, tmp_ctx,
619                                 &kpasswd_rep, &krb_priv_rep);
620         if (!NT_STATUS_IS_OK(nt_status)) {
621                 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
622                                                        KRB5_KPASSWD_HARDERROR,
623                                                        talloc_asprintf(mem_ctx,
624                                                                        "gensec_wrap failed: %s",
625                                                                        nt_errstr(nt_status)),
626                                                        &krb_priv_rep);
627                 ap_rep.length = 0;
628                 if (ret) {
629                         goto reply;
630                 }
631                 talloc_free(tmp_ctx);
632                 return ret;
633         }
634
635 reply:
636         *reply = data_blob_talloc(mem_ctx, NULL, krb_priv_rep.length + ap_rep.length + header_len);
637         if (!reply->data) {
638                 return false;
639         }
640
641         RSSVAL(reply->data, 0, reply->length);
642         RSSVAL(reply->data, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
643         RSSVAL(reply->data, 4, ap_rep.length);
644         memcpy(reply->data + header_len,
645                ap_rep.data,
646                ap_rep.length);
647         memcpy(reply->data + header_len + ap_rep.length,
648                krb_priv_rep.data,
649                krb_priv_rep.length);
650
651         talloc_free(tmp_ctx);
652         return ret;
653 }
654