r12542: Move some more prototypes out to seperate headers
[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
36 /* hold information about one kdc socket */
37 struct kpasswd_socket {
38         struct socket_context *sock;
39         struct kdc_server *kdc;
40         struct fd_event *fde;
41
42         /* a queue of outgoing replies that have been deferred */
43         struct kdc_reply *send_queue;
44 };
45
46 /* Return true if there is a valid error packet formed in the error_blob */
47 static BOOL kpasswdd_make_error_reply(struct kdc_server *kdc, 
48                                      TALLOC_CTX *mem_ctx, 
49                                      uint16_t result_code, 
50                                      const char *error_string, 
51                                      DATA_BLOB *error_blob) 
52 {
53         char *error_string_utf8;
54         ssize_t len;
55         
56         DEBUG(result_code ? 3 : 10, ("kpasswdd: %s\n", error_string));
57
58         len = push_utf8_talloc(mem_ctx, &error_string_utf8, error_string);
59         if (len == -1) {
60                 return False;
61         }
62
63         *error_blob = data_blob_talloc(mem_ctx, NULL, 2 + len + 1);
64         if (!error_blob->data) {
65                 return False;
66         }
67         RSSVAL(error_blob->data, 0, result_code);
68         memcpy(error_blob->data + 2, error_string_utf8, len + 1);
69         return True;
70 }
71
72 /* Return true if there is a valid error packet formed in the error_blob */
73 static BOOL kpasswdd_make_unauth_error_reply(struct kdc_server *kdc, 
74                                             TALLOC_CTX *mem_ctx, 
75                                             uint16_t result_code, 
76                                             const char *error_string, 
77                                             DATA_BLOB *error_blob) 
78 {
79         BOOL ret;
80         int kret;
81         DATA_BLOB error_bytes;
82         krb5_data k5_error_bytes, k5_error_blob;
83         ret = kpasswdd_make_error_reply(kdc, mem_ctx, result_code, error_string, 
84                                        &error_bytes);
85         if (!ret) {
86                 return False;
87         }
88         k5_error_bytes.data = error_bytes.data;
89         k5_error_bytes.length = error_bytes.length;
90         kret = krb5_mk_error(kdc->smb_krb5_context->krb5_context,
91                              result_code, NULL, &k5_error_bytes, 
92                              NULL, NULL, NULL, NULL, &k5_error_blob);
93         if (kret) {
94                 return False;
95         }
96         *error_blob = data_blob_talloc(mem_ctx, k5_error_blob.data, k5_error_blob.length);
97         krb5_data_free(&k5_error_blob);
98         if (!error_blob->data) {
99                 return False;
100         }
101         return True;
102 }
103
104 static BOOL kpasswd_make_pwchange_reply(struct kdc_server *kdc, 
105                                         TALLOC_CTX *mem_ctx, 
106                                         NTSTATUS status, 
107                                         enum samr_RejectReason reject_reason,
108                                         struct samr_DomInfo1 *dominfo,
109                                         DATA_BLOB *error_blob) 
110 {
111         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
112                 return kpasswdd_make_error_reply(kdc, mem_ctx, 
113                                                 KRB5_KPASSWD_ACCESSDENIED,
114                                                 "No such user when changing password",
115                                                 error_blob);
116         }
117         if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
118                 return kpasswdd_make_error_reply(kdc, mem_ctx, 
119                                                 KRB5_KPASSWD_ACCESSDENIED,
120                                                 "Not permitted to change password",
121                                                 error_blob);
122         }
123         if (NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION)) {
124                 const char *reject_string;
125                 switch (reject_reason) {
126                 case SAMR_REJECT_TOO_SHORT:
127                         reject_string = talloc_asprintf(mem_ctx, "Password too short, password must be at least %d characters long",
128                                                         dominfo->min_password_length);
129                         break;
130                 case SAMR_REJECT_COMPLEXITY:
131                         reject_string = "Password does not meet complexity requirements";
132                         break;
133                 case SAMR_REJECT_OTHER:
134                 default:
135                         reject_string = talloc_asprintf(mem_ctx, "Password must be at least %d characters long, and cannot match any of your %d previous passwords",
136                                                         dominfo->min_password_length, dominfo->password_history_length);
137                         break;
138                 }
139                 return kpasswdd_make_error_reply(kdc, mem_ctx, 
140                                                 KRB5_KPASSWD_SOFTERROR,
141                                                 reject_string,
142                                                 error_blob);
143         }
144         if (!NT_STATUS_IS_OK(status)) {
145                 return kpasswdd_make_error_reply(kdc, mem_ctx, 
146                                                  KRB5_KPASSWD_HARDERROR,
147                                                  talloc_asprintf(mem_ctx, "failed to set password: %s", nt_errstr(status)),
148                                                  error_blob);
149                 
150         }
151         return kpasswdd_make_error_reply(kdc, mem_ctx, KRB5_KPASSWD_SUCCESS,
152                                         "Password changed",
153                                         error_blob);
154 }
155
156 /* 
157    A user password change
158    
159    Return true if there is a valid error packet (or sucess) formed in
160    the error_blob
161 */
162 static BOOL kpasswdd_change_password(struct kdc_server *kdc,
163                                      TALLOC_CTX *mem_ctx, 
164                                      struct auth_session_info *session_info,
165                                      const char *password,
166                                      DATA_BLOB *reply)
167 {
168         NTSTATUS status;
169         enum samr_RejectReason reject_reason;
170         struct samr_DomInfo1 *dominfo;
171         struct ldb_context *samdb;
172
173         samdb = samdb_connect(mem_ctx, system_session(mem_ctx));
174         if (!samdb) {
175                 return kpasswdd_make_error_reply(kdc, mem_ctx, 
176                                                 KRB5_KPASSWD_HARDERROR,
177                                                 "Failed to open samdb",
178                                                 reply);
179         }
180         
181         DEBUG(3, ("Changing password of %s\n", dom_sid_string(mem_ctx, session_info->security_token->user_sid)));
182
183         /* User password change */
184         status = samdb_set_password_sid(samdb, mem_ctx, 
185                                         session_info->security_token->user_sid,
186                                         password, NULL, NULL, 
187                                         True, /* this is a user password change */
188                                         True, /* run restriction tests */
189                                         &reject_reason,
190                                         &dominfo);
191         return kpasswd_make_pwchange_reply(kdc, mem_ctx, 
192                                            status, 
193                                            reject_reason,
194                                            dominfo, 
195                                            reply);
196
197 }
198
199 static BOOL kpasswd_process_request(struct kdc_server *kdc,
200                                     TALLOC_CTX *mem_ctx, 
201                                     struct gensec_security *gensec_security,
202                                     uint16_t version,
203                                     DATA_BLOB *input, 
204                                     DATA_BLOB *reply)
205 {
206         NTSTATUS status;
207         enum samr_RejectReason reject_reason;
208         struct samr_DomInfo1 *dominfo;
209         struct ldb_context *samdb;
210         struct auth_session_info *session_info;
211         struct ldb_message *msg = ldb_msg_new(gensec_security);
212         krb5_context context = kdc->smb_krb5_context->krb5_context;
213         int ret;
214         if (!msg) {
215                 return False;
216         }
217
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                 size_t len;
240                 ChangePasswdDataMS chpw;
241                 char *password;
242                 krb5_principal principal;
243                 char *set_password_on_princ;
244                 struct ldb_dn *set_password_on_dn;
245
246                 samdb = samdb_connect(gensec_security, session_info);
247
248                 ret = decode_ChangePasswdDataMS(input->data, input->length,
249                                                 &chpw, &len);
250                 if (ret) {
251                         return kpasswdd_make_error_reply(kdc, mem_ctx, 
252                                                         KRB5_KPASSWD_MALFORMED,
253                                                         "failed to decode password change structure",
254                                                         reply);
255                 }
256                 
257                 password = talloc_strndup(mem_ctx, chpw.newpasswd.data, 
258                                           chpw.newpasswd.length);
259                 if (!password) {
260                         free_ChangePasswdDataMS(&chpw);
261                         return False;
262                 }
263                 if ((chpw.targname && !chpw.targrealm) 
264                     || (!chpw.targname && chpw.targrealm)) {
265                         return kpasswdd_make_error_reply(kdc, mem_ctx, 
266                                                         KRB5_KPASSWD_MALFORMED,
267                                                         "Realm and principal must be both present, or neither present",
268                                                         reply);
269                 }
270                 if (chpw.targname && chpw.targrealm) {
271                         if (_krb5_principalname2krb5_principal(&principal, *chpw.targname, 
272                                                                *chpw.targrealm) != 0) {
273                                 free_ChangePasswdDataMS(&chpw);
274                                 return kpasswdd_make_error_reply(kdc, mem_ctx, 
275                                                                 KRB5_KPASSWD_MALFORMED,
276                                                                 "failed to extract principal to set",
277                                                                 reply);
278                                 
279                         }
280                 } else {
281                         free_ChangePasswdDataMS(&chpw);
282                         return kpasswdd_change_password(kdc, mem_ctx, session_info, 
283                                                         password, reply);
284                 }
285                 free_ChangePasswdDataMS(&chpw);
286
287                 if (krb5_unparse_name(context, principal, &set_password_on_princ) != 0) {
288                         krb5_free_principal(context, principal);
289                         return kpasswdd_make_error_reply(kdc, mem_ctx, 
290                                                         KRB5_KPASSWD_MALFORMED,
291                                                         "krb5_unparse_name failed!",
292                                                         reply);
293                 }
294                 
295                 krb5_free_principal(context, principal);
296                 
297                 status = crack_user_principal_name(samdb, mem_ctx, 
298                                                    set_password_on_princ, 
299                                                    &set_password_on_dn, NULL);
300                 free(set_password_on_princ);
301                 if (!NT_STATUS_IS_OK(status)) {
302                         return kpasswd_make_pwchange_reply(kdc, mem_ctx, 
303                                                            status,
304                                                            reject_reason, 
305                                                            dominfo, 
306                                                            reply);
307                 }
308
309                 /* Admin password set */
310                 status = samdb_set_password(samdb, mem_ctx,
311                                             set_password_on_dn, NULL,
312                                             msg, password, NULL, NULL, 
313                                             False, /* this is not a user password change */
314                                             True, /* run restriction tests */
315                                             &reject_reason, &dominfo);
316
317                 return kpasswd_make_pwchange_reply(kdc, mem_ctx, 
318                                                    status,
319                                                    reject_reason, 
320                                                    dominfo, 
321                                                    reply);
322         }
323         default:
324                 return kpasswdd_make_error_reply(kdc, mem_ctx, 
325                                                 KRB5_KPASSWD_BAD_VERSION,
326                                                 talloc_asprintf(mem_ctx, 
327                                                                 "Protocol version %u not supported", 
328                                                                 version),
329                                                 reply);
330         }
331         return True;
332 }
333
334 BOOL kpasswdd_process(struct kdc_server *kdc,
335                       TALLOC_CTX *mem_ctx, 
336                       DATA_BLOB *input, 
337                       DATA_BLOB *reply,
338                       const char *from,
339                       int src_port)
340 {
341         BOOL ret;
342         const uint16_t header_len = 6;
343         uint16_t len;
344         uint16_t ap_req_len;
345         uint16_t krb_priv_len;
346         uint16_t version;
347         NTSTATUS nt_status;
348         DATA_BLOB ap_req, krb_priv_req, krb_priv_rep, ap_rep;
349         DATA_BLOB kpasswd_req, kpasswd_rep;
350         struct cli_credentials *server_credentials;
351         struct gensec_security *gensec_security;
352         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
353         
354         if (!tmp_ctx) {
355                 return False;
356         }
357
358         if (input->length <= header_len) {
359                 talloc_free(tmp_ctx);
360                 return False;
361         }
362
363         len = RSVAL(input->data, 0);
364         if (input->length != len) {
365                 talloc_free(tmp_ctx);
366                 return False;
367         }
368
369         version = RSVAL(input->data, 2);
370         ap_req_len = RSVAL(input->data, 4);
371         if ((ap_req_len >= len) || (ap_req_len + header_len) >= len) {
372                 talloc_free(tmp_ctx);
373                 return False;
374         }
375         
376         krb_priv_len = len - ap_req_len;
377         ap_req = data_blob_const(&input->data[header_len], ap_req_len);
378         krb_priv_req = data_blob_const(&input->data[header_len + ap_req_len], krb_priv_len);
379         
380         nt_status = gensec_server_start(tmp_ctx, &gensec_security, kdc->task->event_ctx);
381         if (!NT_STATUS_IS_OK(nt_status)) {
382                 talloc_free(tmp_ctx);
383                 return False;
384         }
385
386         server_credentials 
387                 = cli_credentials_init(tmp_ctx);
388         if (!server_credentials) {
389                 DEBUG(1, ("Failed to init server credentials\n"));
390                 return False;
391         }
392         
393         cli_credentials_set_conf(server_credentials);
394         nt_status = cli_credentials_set_stored_principal(server_credentials, "kadmin/changepw");
395         if (!NT_STATUS_IS_OK(nt_status)) {
396                 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx, 
397                                                        KRB5_KPASSWD_HARDERROR,
398                                                        talloc_asprintf(mem_ctx, 
399                                                                        "Failed to obtain server credentials for kadmin/changepw: %s\n", 
400                                                                        nt_errstr(nt_status)),
401                                                        &krb_priv_rep);
402                 ap_rep.length = 0;
403                 if (ret) {
404                         goto reply;
405                 }
406                 talloc_free(tmp_ctx);
407                 return ret;
408         }
409         
410         gensec_set_credentials(gensec_security, server_credentials);
411         gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
412
413         nt_status = gensec_start_mech_by_name(gensec_security, "krb5");
414         if (!NT_STATUS_IS_OK(nt_status)) {
415                 talloc_free(tmp_ctx);
416                 return False;
417         }
418
419         nt_status = gensec_update(gensec_security, tmp_ctx, ap_req, &ap_rep);
420         if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
421                 
422                 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx, 
423                                                        KRB5_KPASSWD_HARDERROR,
424                                                        talloc_asprintf(mem_ctx, 
425                                                                        "gensec_update failed: %s", 
426                                                                        nt_errstr(nt_status)),
427                                                        &krb_priv_rep);
428                 ap_rep.length = 0;
429                 if (ret) {
430                         goto reply;
431                 }
432                 talloc_free(tmp_ctx);
433                 return ret;
434         }
435
436         nt_status = gensec_unwrap(gensec_security, tmp_ctx, &krb_priv_req, &kpasswd_req);
437         if (!NT_STATUS_IS_OK(nt_status)) {
438                 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx, 
439                                                        KRB5_KPASSWD_HARDERROR,
440                                                        talloc_asprintf(mem_ctx, 
441                                                                        "gensec_unwrap failed: %s", 
442                                                                        nt_errstr(nt_status)),
443                                                        &krb_priv_rep);
444                 ap_rep.length = 0;
445                 if (ret) {
446                         goto reply;
447                 }
448                 talloc_free(tmp_ctx);
449                 return ret;
450         }
451
452         ret = kpasswd_process_request(kdc, tmp_ctx, 
453                                       gensec_security, 
454                                       version, 
455                                       &kpasswd_req, &kpasswd_rep); 
456         if (!ret) {
457                 /* Argh! */
458                 return False;
459         }
460         
461         nt_status = gensec_wrap(gensec_security, tmp_ctx, 
462                                 &kpasswd_rep, &krb_priv_rep);
463         if (!NT_STATUS_IS_OK(nt_status)) {
464                 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx, 
465                                                        KRB5_KPASSWD_HARDERROR,
466                                                        talloc_asprintf(mem_ctx, 
467                                                                        "gensec_wrap failed: %s", 
468                                                                        nt_errstr(nt_status)),
469                                                        &krb_priv_rep);
470                 ap_rep.length = 0;
471                 if (ret) {
472                         goto reply;
473                 }
474                 talloc_free(tmp_ctx);
475                 return ret;
476         }
477         
478 reply:
479         *reply = data_blob_talloc(mem_ctx, NULL, krb_priv_rep.length + ap_rep.length + header_len);
480         if (!reply->data) {
481                 return False;
482         }
483
484         RSSVAL(reply->data, 0, reply->length);
485         RSSVAL(reply->data, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
486         RSSVAL(reply->data, 4, ap_rep.length);
487         memcpy(reply->data + header_len, 
488                ap_rep.data, 
489                ap_rep.length);
490         memcpy(reply->data + header_len + ap_rep.length, 
491                krb_priv_rep.data, 
492                krb_priv_rep.length);
493
494         talloc_free(tmp_ctx);
495         return ret;
496 }
497