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