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