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