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