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