2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1998-2001
6 Copyright (C) Andrew Bartlett 2001
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 uint32 global_client_caps = 0;
28 /****************************************************************************
29 reply to a session setup spnego negotiate packet for kerberos
30 ****************************************************************************/
31 static int reply_spnego_kerberos(connection_struct *conn,
32 char *inbuf, char *outbuf,
33 int length, int bufsize,
38 krb5_auth_context auth_context = NULL;
39 krb5_keytab keytab = NULL;
41 krb5_ticket *tkt = NULL;
43 char *realm, *client, *p;
44 const struct passwd *pw;
47 auth_serversupplied_info *server_info = NULL;
51 if (!spnego_parse_krb5_wrap(*secblob, &ticket)) {
52 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
55 ret = krb5_init_context(&context);
57 DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
58 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
61 packet.length = ticket.length;
62 packet.data = (krb5_pointer)ticket.data;
65 file_save("/tmp/ticket.dat", ticket.data, ticket.length);
68 if ((ret = krb5_rd_req(context, &auth_context, &packet,
69 NULL, keytab, NULL, &tkt))) {
70 DEBUG(3,("krb5_rd_req failed (%s)\n",
72 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
75 if ((ret = krb5_unparse_name(context, tkt->enc_part2->client,
77 DEBUG(3,("krb5_unparse_name failed (%s)\n",
79 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
82 DEBUG(3,("Ticket name is [%s]\n", client));
84 p = strchr_m(client, '@');
86 DEBUG(3,("Doesn't look like a valid principal\n"));
87 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
91 if (strcasecmp(p+1, realm) != 0) {
92 DEBUG(3,("Ticket for incorrect realm %s\n", p+1));
93 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
98 /* the password is good - let them in */
99 pw = smb_getpwnam(user,False);
101 DEBUG(1,("Username %s is invalid on this system\n",user));
102 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
105 if (!make_server_info_pw(&server_info,pw)) {
106 DEBUG(1,("make_server_info_from_pw failed!\n"));
107 return ERROR_NT(NT_STATUS_NO_MEMORY);
110 sess_vuid = register_vuid(server_info, user);
112 free_server_info(&server_info);
114 if (sess_vuid == -1) {
115 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
118 set_message(outbuf,4,0,True);
119 SSVAL(outbuf, smb_vwv3, 0);
121 p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
122 p += srvstr_push(outbuf, p, "Samba", -1, STR_TERMINATE);
123 p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
124 set_message_end(outbuf,p);
126 SSVAL(outbuf,smb_uid,sess_vuid);
127 SSVAL(inbuf,smb_uid,sess_vuid);
129 return chain_reply(inbuf,outbuf,length,bufsize);
134 /****************************************************************************
135 send a security blob via a session setup reply
136 ****************************************************************************/
137 static BOOL reply_sesssetup_blob(connection_struct *conn, char *outbuf,
142 set_message(outbuf,4,0,True);
144 /* we set NT_STATUS_MORE_PROCESSING_REQUIRED to tell the other end
145 that we aren't finished yet */
147 SIVAL(outbuf, smb_rcls, NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED));
148 SSVAL(outbuf, smb_vwv0, 0xFF); /* no chaining possible */
149 SSVAL(outbuf, smb_vwv3, blob.length);
151 memcpy(p, blob.data, blob.length);
153 p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
154 p += srvstr_push(outbuf, p, "Samba", -1, STR_TERMINATE);
155 p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
156 set_message_end(outbuf,p);
158 return send_smb(smbd_server_fd(),outbuf);
161 /****************************************************************************
162 reply to a session setup spnego negotiate packet
163 ****************************************************************************/
164 static int reply_spnego_negotiate(connection_struct *conn,
167 int length, int bufsize,
170 char *OIDs[ASN1_MAX_OIDS];
173 uint32 ntlmssp_command, neg_flags;
174 DATA_BLOB sess_key, chal, spnego_chal;
176 BOOL got_kerberos = False;
178 /* parse out the OIDs and the first sec blob */
179 if (!parse_negTokenTarg(blob1, OIDs, &secblob)) {
180 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
183 for (i=0;OIDs[i];i++) {
184 DEBUG(3,("Got OID %s\n", OIDs[i]));
185 if (strcmp(OID_KERBEROS5, OIDs[i]) == 0 ||
186 strcmp(OID_KERBEROS5_OLD, OIDs[i]) == 0) {
191 DEBUG(3,("Got secblob of size %d\n", secblob.length));
195 int ret = reply_spnego_kerberos(conn, inbuf, outbuf,
196 length, bufsize, &secblob);
197 data_blob_free(&secblob);
202 /* parse the NTLMSSP packet */
204 file_save("secblob.dat", secblob.data, secblob.length);
207 if (!msrpc_parse(&secblob, "CddB",
212 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
215 data_blob_free(&secblob);
216 data_blob_free(&sess_key);
218 if (ntlmssp_command != NTLMSSP_NEGOTIATE) {
219 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
222 DEBUG(3,("Got neg_flags=%08x\n", neg_flags));
224 if (!last_challenge(cryptkey)) {
225 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
228 /* Give them the challenge. For now, ignore neg_flags and just
229 return the flags we want. Obviously this is not correct */
231 neg_flags = NTLMSSP_NEGOTIATE_UNICODE |
232 NTLMSSP_NEGOTIATE_LM_KEY |
233 NTLMSSP_NEGOTIATE_NTLM;
235 msrpc_gen(&chal, "Cddddbdddd",
245 if (!spnego_gen_challenge(&spnego_chal, &chal, &chal)) {
246 DEBUG(3,("Failed to generate challenge\n"));
247 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
250 /* now tell the client to send the auth packet */
251 reply_sesssetup_blob(conn, outbuf, spnego_chal);
253 data_blob_free(&chal);
254 data_blob_free(&spnego_chal);
256 /* and tell smbd that we have already replied to this packet */
261 /****************************************************************************
262 reply to a session setup spnego auth packet
263 ****************************************************************************/
264 static int reply_spnego_auth(connection_struct *conn, char *inbuf, char *outbuf,
265 int length, int bufsize,
269 char *workgroup, *user, *machine;
270 DATA_BLOB lmhash, nthash, sess_key;
271 DATA_BLOB plaintext_password = data_blob(NULL, 0);
273 uint32 ntlmssp_command, neg_flags;
279 auth_usersupplied_info *user_info = NULL;
280 auth_serversupplied_info *server_info = NULL;
282 if (!spnego_parse_auth(blob1, &auth)) {
284 file_save("auth.dat", blob1.data, blob1.length);
286 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
289 /* now the NTLMSSP encoded auth hashes */
290 if (!msrpc_parse(&auth, "CdBBUUUBd",
300 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
303 data_blob_free(&auth);
304 data_blob_free(&sess_key);
306 DEBUG(3,("Got user=[%s] workgroup=[%s] machine=[%s] len1=%d len2=%d\n",
307 user, workgroup, machine, lmhash.length, nthash.length));
310 file_save("nthash1.dat", nthash.data, nthash.length);
311 file_save("lmhash1.dat", lmhash.data, lmhash.length);
314 if (!last_challenge(chal)) {
315 DEBUG(0,("Encrypted login but no challange set!\n"));
316 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
318 sec_blob = data_blob(chal, 8);
319 if (!sec_blob.data) {
320 return ERROR_NT(NT_STATUS_NO_MEMORY);
323 if (!make_user_info_map(&user_info,
329 return ERROR_NT(NT_STATUS_NO_MEMORY);
332 nt_status = check_password(user_info, &server_info);
334 free_user_info(&user_info);
336 data_blob_free(&lmhash);
338 data_blob_free(&nthash);
340 if (!NT_STATUS_IS_OK(nt_status)) {
341 return ERROR_NT(nt_status_squash(nt_status));
344 sess_vuid = register_vuid(server_info, user);
346 free_server_info(&server_info);
348 if (sess_vuid == -1) {
349 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
352 set_message(outbuf,4,0,True);
353 SSVAL(outbuf, smb_vwv3, 0);
355 p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
356 p += srvstr_push(outbuf, p, "Samba", -1, STR_TERMINATE);
357 p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
358 set_message_end(outbuf,p);
360 SSVAL(outbuf,smb_uid,sess_vuid);
361 SSVAL(inbuf,smb_uid,sess_vuid);
363 return chain_reply(inbuf,outbuf,length,bufsize);
367 /****************************************************************************
368 reply to a session setup spnego anonymous packet
369 ****************************************************************************/
370 static int reply_spnego_anonymous(connection_struct *conn, char *inbuf, char *outbuf,
371 int length, int bufsize)
375 auth_usersupplied_info *user_info = NULL;
376 auth_serversupplied_info *server_info = NULL;
380 DEBUG(3,("Got anonymous request\n"));
382 make_user_info_guest(&user_info);
384 nt_status = check_password(user_info, &server_info);
386 sess_vuid = register_vuid(server_info, lp_guestaccount());
387 free_server_info(&server_info);
389 if (sess_vuid == -1) {
390 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
393 set_message(outbuf,4,0,True);
394 SSVAL(outbuf, smb_vwv3, 0);
396 p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
397 p += srvstr_push(outbuf, p, "Samba", -1, STR_TERMINATE);
398 p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
399 set_message_end(outbuf,p);
401 SSVAL(outbuf,smb_uid,sess_vuid);
402 SSVAL(inbuf,smb_uid,sess_vuid);
404 return chain_reply(inbuf,outbuf,length,bufsize);
408 /****************************************************************************
409 reply to a session setup command
410 ****************************************************************************/
411 static int reply_sesssetup_and_X_spnego(connection_struct *conn, char *inbuf,char *outbuf,
412 int length,int bufsize)
418 DEBUG(3,("Doing spnego session setup\n"));
420 if (global_client_caps == 0) {
421 global_client_caps = IVAL(inbuf,smb_vwv10);
424 p = (uint8 *)smb_buf(inbuf);
426 if (SVAL(inbuf, smb_vwv7) == 0) {
427 /* an anonymous request */
428 return reply_spnego_anonymous(conn, inbuf, outbuf, length, bufsize);
431 /* pull the spnego blob */
432 blob1 = data_blob(p, SVAL(inbuf, smb_vwv7));
435 file_save("negotiate.dat", blob1.data, blob1.length);
438 if (blob1.data[0] == ASN1_APPLICATION(0)) {
439 /* its a negTokenTarg packet */
440 ret = reply_spnego_negotiate(conn, inbuf, outbuf, length, bufsize, blob1);
441 data_blob_free(&blob1);
445 if (blob1.data[0] == ASN1_CONTEXT(1)) {
446 /* its a auth packet */
447 ret = reply_spnego_auth(conn, inbuf, outbuf, length, bufsize, blob1);
448 data_blob_free(&blob1);
452 /* what sort of packet is this? */
453 DEBUG(1,("Unknown packet in reply_sesssetup_and_X_spnego\n"));
455 data_blob_free(&blob1);
457 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
461 /****************************************************************************
462 reply to a session setup command
463 ****************************************************************************/
464 int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
465 int length,int bufsize)
471 DATA_BLOB plaintext_password;
473 pstring sub_user; /* Sainitised username for substituion */
476 fstring native_lanman;
477 static BOOL done_sesssetup = False;
478 extern BOOL global_encrypted_passwords_negotiated;
479 extern BOOL global_spnego_negotiated;
481 extern fstring remote_machine;
482 extern userdom_struct current_user_info;
485 auth_usersupplied_info *user_info = NULL;
486 auth_serversupplied_info *server_info = NULL;
490 BOOL doencrypt = global_encrypted_passwords_negotiated;
492 START_PROFILE(SMBsesssetupX);
494 ZERO_STRUCT(lm_resp);
495 ZERO_STRUCT(nt_resp);
496 ZERO_STRUCT(plaintext_password);
498 DEBUG(3,("wct=%d flg2=0x%x\n", CVAL(inbuf, smb_wct), SVAL(inbuf, smb_flg2)));
500 /* a SPNEGO session setup has 12 command words, whereas a normal
501 NT1 session setup has 13. See the cifs spec. */
502 if (CVAL(inbuf, smb_wct) == 12 &&
503 (SVAL(inbuf, smb_flg2) & FLAGS2_EXTENDED_SECURITY)) {
504 return reply_sesssetup_and_X_spnego(conn, inbuf, outbuf, length, bufsize);
507 smb_bufsize = SVAL(inbuf,smb_vwv2);
509 if (Protocol < PROTOCOL_NT1) {
510 uint16 passlen1 = SVAL(inbuf,smb_vwv7);
511 if (passlen1 > MAX_PASS_LEN) {
512 return ERROR_DOS(ERRDOS,ERRbuftoosmall);
516 lm_resp = data_blob(smb_buf(inbuf), passlen1);
518 plaintext_password = data_blob(smb_buf(inbuf), passlen1+1);
519 if (!plaintext_password.data) {
520 DEBUG(0,("reply_sesssetup_and_X: malloc failed for plaintext_password!\n"));
521 return ERROR_NT(NT_STATUS_NO_MEMORY);
523 /* Ensure null termination */
524 plaintext_password.data[passlen1] = 0;
528 srvstr_pull(inbuf, user, smb_buf(inbuf)+passlen1, sizeof(user), -1, STR_TERMINATE);
531 uint16 passlen1 = SVAL(inbuf,smb_vwv7);
532 uint16 passlen2 = SVAL(inbuf,smb_vwv8);
533 enum remote_arch_types ra_type = get_remote_arch();
534 char *p = smb_buf(inbuf);
536 if(global_client_caps == 0)
537 global_client_caps = IVAL(inbuf,smb_vwv11);
539 /* client_caps is used as final determination if client is NT or Win95.
540 This is needed to return the correct error codes in some
544 if(ra_type == RA_WINNT || ra_type == RA_WIN2K || ra_type == RA_WIN95) {
545 if(!(global_client_caps & (CAP_NT_SMBS | CAP_STATUS32))) {
546 set_remote_arch( RA_WIN95);
550 if (passlen1 > MAX_PASS_LEN) {
551 return ERROR_DOS(ERRDOS,ERRbuftoosmall);
554 passlen1 = MIN(passlen1, MAX_PASS_LEN);
555 passlen2 = MIN(passlen2, MAX_PASS_LEN);
558 /* both Win95 and WinNT stuff up the password lengths for
559 non-encrypting systems. Uggh.
561 if passlen1==24 its a win95 system, and its setting the
562 password length incorrectly. Luckily it still works with the
563 default code because Win95 will null terminate the password
566 if passlen1>0 and passlen2>0 then maybe its a NT box and its
567 setting passlen2 to some random value which really stuffs
568 things up. we need to fix that one. */
570 if (passlen1 > 0 && passlen2 > 0 && passlen2 != 24 && passlen2 != 1)
574 if (lp_restrict_anonymous()) {
575 /* there seems to be no reason behind the
576 * differences in MS clients formatting
577 * various info like the domain, NativeOS, and
578 * NativeLanMan fields. Win95 in particular
579 * seems to have an extra null byte between
580 * the username and the domain, or the
581 * password length calculation is wrong, which
582 * throws off the string extraction routines
583 * below. This makes the value of domain be
584 * the empty string, which fails the restrict
585 * anonymous check further down. This
586 * compensates for that, and allows browsing
587 * to work in mixed NT and win95 environments
588 * even when restrict anonymous is true. AAB
590 dump_data(100, p, 0x70);
591 DEBUG(9, ("passlen1=%d, passlen2=%d\n", passlen1, passlen2));
592 if (ra_type == RA_WIN95 && !passlen1 && !passlen2 && p[0] == 0 && p[1] == 0) {
593 DEBUG(0, ("restrict anonymous parameter used in a win95 environment!\n"));
594 DEBUG(0, ("client is win95 and broken passlen1 offset -- attempting fix\n"));
595 DEBUG(0, ("if win95 cilents are having difficulty browsing, you will be unable to use restrict anonymous\n"));
600 /* Save the lanman2 password and the NT md4 password. */
602 if ((doencrypt) && (passlen1 != 0) && (passlen1 != 24)) {
607 lm_resp = data_blob(p, passlen1);
608 nt_resp = data_blob(p+passlen1, passlen2);
610 plaintext_password = data_blob(p, passlen1+1);
611 /* Ensure null termination */
612 plaintext_password.data[passlen1] = 0;
615 p += passlen1 + passlen2;
616 p += srvstr_pull(inbuf, user, p, sizeof(user), -1,
618 p += srvstr_pull(inbuf, domain, p, sizeof(domain),
620 p += srvstr_pull(inbuf, native_os, p, sizeof(native_os),
622 p += srvstr_pull(inbuf, native_lanman, p, sizeof(native_lanman),
624 DEBUG(3,("Domain=[%s] NativeOS=[%s] NativeLanMan=[%s]\n",
625 domain,native_os,native_lanman));
628 /* don't allow for weird usernames or domains */
629 alpha_strcpy(user, user, ". _-$", sizeof(user));
630 alpha_strcpy(domain, domain, ". _-", sizeof(domain));
631 if (strstr(user, "..") || strstr(domain,"..")) {
632 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
635 DEBUG(3,("sesssetupX:name=[%s]\\[%s]@[%s]\n", domain, user, remote_machine));
638 if (global_spnego_negotiated) {
639 DEBUG(0,("reply_sesssetup_and_X: Rejecting attempt at 'normal' session setup after negotiating spnego.\n"));
640 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
645 pstrcpy(sub_user, user);
647 pstrcpy(sub_user, lp_guestaccount());
650 pstrcpy(current_user_info.smb_name,sub_user);
652 reload_services(True);
654 if (lp_security() == SEC_SHARE) {
655 /* in share level we should ignore any passwords */
657 data_blob_free(&lm_resp);
658 data_blob_free(&nt_resp);
659 data_blob_clear_free(&plaintext_password);
661 map_username(sub_user);
662 add_session_user(sub_user);
663 /* Then force it to null for the benfit of the code below */
667 if (done_sesssetup && lp_restrict_anonymous()) {
668 /* tests show that even if browsing is done over
669 * already validated connections without a username
670 * and password the domain is still provided, which it
671 * wouldn't be if it was a purely anonymous
672 * connection. So, in order to restrict anonymous, we
673 * only deny connections that have no session
674 * information. If a domain has been provided, then
675 * it's not a purely anonymous connection. AAB */
676 if (!*user && !*domain) {
677 DEBUG(0, ("restrict anonymous is True and anonymous connection attempted. Denying access.\n"));
679 data_blob_free(&lm_resp);
680 data_blob_free(&nt_resp);
681 data_blob_clear_free(&plaintext_password);
683 END_PROFILE(SMBsesssetupX);
684 return ERROR_DOS(ERRDOS,ERRnoaccess);
688 if (!make_user_info_for_reply(&user_info,
691 plaintext_password, doencrypt)) {
692 return ERROR_NT(NT_STATUS_NO_MEMORY);
695 nt_status = check_password(user_info, &server_info);
697 free_user_info(&user_info);
699 data_blob_free(&lm_resp);
700 data_blob_free(&nt_resp);
701 data_blob_clear_free(&plaintext_password);
703 if (!NT_STATUS_IS_OK(nt_status)) {
704 if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
705 if ((lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_USER) ||
706 (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_PASSWORD)) {
708 DEBUG(3,("No such user %s [%s] - using guest account\n",user, domain));
709 make_server_info_guest(&server_info);
710 nt_status = NT_STATUS_OK;
713 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
714 if (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_PASSWORD) {
715 DEBUG(3,("Registered username %s for guest access\n",user));
716 make_server_info_guest(&server_info);
717 nt_status = NT_STATUS_OK;
722 if (!NT_STATUS_IS_OK(nt_status)) {
723 return ERROR_NT(nt_status_squash(nt_status));
726 /* it's ok - setup a reply */
727 if (Protocol < PROTOCOL_NT1) {
728 set_message(outbuf,3,0,True);
731 set_message(outbuf,3,0,True);
733 p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
734 p += srvstr_push(outbuf, p, "Samba", -1, STR_TERMINATE);
735 p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
736 set_message_end(outbuf,p);
737 /* perhaps grab OS version here?? */
740 if (server_info->guest) {
741 SSVAL(outbuf,smb_vwv2,1);
743 const char *home_dir = pdb_get_homedir(server_info->sam_account);
744 const char *username = pdb_get_username(server_info->sam_account);
745 if ((home_dir && *home_dir)
746 && (lp_servicenumber(username) < 0)) {
747 add_home_service(username, home_dir);
751 /* register the name and uid as being validated, so further connections
752 to a uid can get through without a password, on the same VC */
754 sess_vuid = register_vuid(server_info, sub_user);
756 free_server_info(&server_info);
758 if (sess_vuid == -1) {
759 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
763 SSVAL(outbuf,smb_uid,sess_vuid);
764 SSVAL(inbuf,smb_uid,sess_vuid);
767 max_send = MIN(max_send,smb_bufsize);
769 done_sesssetup = True;
771 END_PROFILE(SMBsesssetupX);
772 return chain_reply(inbuf,outbuf,length,bufsize);