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