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