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