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.
26 /****************************************************************************
27 reply to a session setup spnego negotiate packet for kerberos
28 ****************************************************************************/
29 static int reply_spnego_kerberos(connection_struct *conn,
30 char *inbuf, char *outbuf,
31 int length, int bufsize,
36 krb5_auth_context auth_context = NULL;
37 krb5_keytab keytab = NULL;
39 krb5_ticket *tkt = NULL;
41 char *realm, *client, *p;
42 const struct passwd *pw;
45 auth_serversupplied_info *server_info = NULL;
49 if (!spnego_parse_krb5_wrap(*secblob, &ticket)) {
50 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
53 ret = krb5_init_context(&context);
55 DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
56 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
59 packet.length = ticket.length;
60 packet.data = (krb5_pointer)ticket.data;
63 file_save("/tmp/ticket.dat", ticket.data, ticket.length);
66 if ((ret = krb5_rd_req(context, &auth_context, &packet,
67 NULL, keytab, NULL, &tkt))) {
68 DEBUG(3,("krb5_rd_req failed (%s)\n",
70 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
73 if ((ret = krb5_unparse_name(context, tkt->enc_part2->client,
75 DEBUG(3,("krb5_unparse_name failed (%s)\n",
77 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
80 DEBUG(3,("Ticket name is [%s]\n", client));
82 p = strchr_m(client, '@');
84 DEBUG(3,("Doesn't look like a valid principal\n"));
85 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
89 if (strcasecmp(p+1, realm) != 0) {
90 DEBUG(3,("Ticket for incorrect realm %s\n", p+1));
91 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
96 /* the password is good - let them in */
97 pw = smb_getpwnam(user,False);
99 DEBUG(1,("Username %s is invalid on this system\n",user));
100 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
103 if (!make_server_info_pw(&server_info,pw)) {
104 DEBUG(1,("make_server_info_from_pw failed!\n"));
105 return ERROR_NT(NT_STATUS_NO_MEMORY);
108 sess_vuid = register_vuid(server_info, user, False);
110 free_server_info(&server_info);
112 if (sess_vuid == -1) {
113 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
116 set_message(outbuf,4,0,True);
117 SSVAL(outbuf, smb_vwv3, 0);
119 p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
120 p += srvstr_push(outbuf, p, "Samba", -1, STR_TERMINATE);
121 p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
122 set_message_end(outbuf,p);
124 SSVAL(outbuf,smb_uid,sess_vuid);
125 SSVAL(inbuf,smb_uid,sess_vuid);
127 return chain_reply(inbuf,outbuf,length,bufsize);
132 /****************************************************************************
133 send a security blob via a session setup reply
134 ****************************************************************************/
135 static BOOL reply_sesssetup_blob(connection_struct *conn, char *outbuf,
140 set_message(outbuf,4,0,True);
142 /* we set NT_STATUS_MORE_PROCESSING_REQUIRED to tell the other end
143 that we aren't finished yet */
145 SIVAL(outbuf, smb_rcls, NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED));
146 SSVAL(outbuf, smb_vwv0, 0xFF); /* no chaining possible */
147 SSVAL(outbuf, smb_vwv3, blob.length);
149 memcpy(p, blob.data, blob.length);
151 p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
152 p += srvstr_push(outbuf, p, "Samba", -1, STR_TERMINATE);
153 p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
154 set_message_end(outbuf,p);
156 return send_smb(smbd_server_fd(),outbuf);
159 /****************************************************************************
160 reply to a session setup spnego negotiate packet
161 ****************************************************************************/
162 static int reply_spnego_negotiate(connection_struct *conn,
165 int length, int bufsize,
168 char *OIDs[ASN1_MAX_OIDS];
171 uint32 ntlmssp_command, neg_flags;
172 DATA_BLOB sess_key, chal, spnego_chal;
174 BOOL got_kerberos = False;
176 /* parse out the OIDs and the first sec blob */
177 if (!parse_negTokenTarg(blob1, OIDs, &secblob)) {
178 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
181 for (i=0;OIDs[i];i++) {
182 DEBUG(3,("Got OID %s\n", OIDs[i]));
183 if (strcmp(OID_KERBEROS5, OIDs[i]) == 0 ||
184 strcmp(OID_KERBEROS5_OLD, OIDs[i]) == 0) {
189 DEBUG(3,("Got secblob of size %d\n", secblob.length));
193 int ret = reply_spnego_kerberos(conn, inbuf, outbuf,
194 length, bufsize, &secblob);
195 data_blob_free(&secblob);
200 /* parse the NTLMSSP packet */
202 file_save("secblob.dat", secblob.data, secblob.length);
205 if (!msrpc_parse(&secblob, "CddB",
210 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
213 data_blob_free(&secblob);
214 data_blob_free(&sess_key);
216 if (ntlmssp_command != NTLMSSP_NEGOTIATE) {
217 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
220 DEBUG(3,("Got neg_flags=%08x\n", neg_flags));
222 if (!last_challenge(cryptkey)) {
223 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
226 /* Give them the challenge. For now, ignore neg_flags and just
227 return the flags we want. Obviously this is not correct */
229 neg_flags = NTLMSSP_NEGOTIATE_UNICODE |
230 NTLMSSP_NEGOTIATE_LM_KEY |
231 NTLMSSP_NEGOTIATE_NTLM;
233 msrpc_gen(&chal, "Cddddbdddd",
243 if (!spnego_gen_challenge(&spnego_chal, &chal, &chal)) {
244 DEBUG(3,("Failed to generate challenge\n"));
245 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
248 /* now tell the client to send the auth packet */
249 reply_sesssetup_blob(conn, outbuf, spnego_chal);
251 data_blob_free(&chal);
252 data_blob_free(&spnego_chal);
254 /* and tell smbd that we have already replied to this packet */
259 /****************************************************************************
260 reply to a session setup spnego auth packet
261 ****************************************************************************/
262 static int reply_spnego_auth(connection_struct *conn, char *inbuf, char *outbuf,
263 int length, int bufsize,
267 char *workgroup, *user, *machine;
268 DATA_BLOB lmhash, nthash, sess_key;
269 DATA_BLOB plaintext_password = data_blob(NULL, 0);
271 uint32 ntlmssp_command, neg_flags;
277 auth_usersupplied_info *user_info = NULL;
278 auth_serversupplied_info *server_info = NULL;
280 if (!spnego_parse_auth(blob1, &auth)) {
282 file_save("auth.dat", blob1.data, blob1.length);
284 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
287 /* now the NTLMSSP encoded auth hashes */
288 if (!msrpc_parse(&auth, "CdBBUUUBd",
298 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
301 data_blob_free(&auth);
302 data_blob_free(&sess_key);
304 DEBUG(3,("Got user=[%s] workgroup=[%s] machine=[%s] len1=%d len2=%d\n",
305 user, workgroup, machine, lmhash.length, nthash.length));
308 file_save("nthash1.dat", nthash.data, nthash.length);
309 file_save("lmhash1.dat", lmhash.data, lmhash.length);
312 if (!last_challenge(chal)) {
313 DEBUG(0,("Encrypted login but no challange set!\n"));
314 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
316 sec_blob = data_blob(chal, 8);
317 if (!sec_blob.data) {
318 return ERROR_NT(NT_STATUS_NO_MEMORY);
321 if (!make_user_info_map(&user_info,
327 return ERROR_NT(NT_STATUS_NO_MEMORY);
330 nt_status = check_password(user_info, &server_info);
332 free_user_info(&user_info);
334 data_blob_free(&lmhash);
336 data_blob_free(&nthash);
338 if (!NT_STATUS_IS_OK(nt_status)) {
339 return ERROR_NT(nt_status_squash(nt_status));
342 sess_vuid = register_vuid(server_info, user, False);
344 free_server_info(&server_info);
346 if (sess_vuid == -1) {
347 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
350 set_message(outbuf,4,0,True);
351 SSVAL(outbuf, smb_vwv3, 0);
353 p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
354 p += srvstr_push(outbuf, p, "Samba", -1, STR_TERMINATE);
355 p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
356 set_message_end(outbuf,p);
358 SSVAL(outbuf,smb_uid,sess_vuid);
359 SSVAL(inbuf,smb_uid,sess_vuid);
361 return chain_reply(inbuf,outbuf,length,bufsize);
365 /****************************************************************************
366 reply to a session setup spnego anonymous packet
367 ****************************************************************************/
368 static int reply_spnego_anonymous(connection_struct *conn, char *inbuf, char *outbuf,
369 int length, int bufsize)
373 auth_serversupplied_info *server_info = NULL;
375 DEBUG(3,("Got anonymous request\n"));
377 make_server_info_guest(&server_info);
378 sess_vuid = register_vuid(server_info, lp_guestaccount(-1), False);
379 free_server_info(&server_info);
381 if (sess_vuid == -1) {
382 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
385 set_message(outbuf,4,0,True);
386 SSVAL(outbuf, smb_vwv3, 0);
388 p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
389 p += srvstr_push(outbuf, p, "Samba", -1, STR_TERMINATE);
390 p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
391 set_message_end(outbuf,p);
393 SSVAL(outbuf,smb_uid,sess_vuid);
394 SSVAL(inbuf,smb_uid,sess_vuid);
396 return chain_reply(inbuf,outbuf,length,bufsize);
400 /****************************************************************************
401 reply to a session setup command
402 ****************************************************************************/
403 static int reply_sesssetup_and_X_spnego(connection_struct *conn, char *inbuf,char *outbuf,
404 int length,int bufsize)
408 extern uint32 global_client_caps;
411 DEBUG(3,("Doing spnego session setup\n"));
413 if (global_client_caps == 0) {
414 global_client_caps = IVAL(inbuf,smb_vwv10);
417 p = (uint8 *)smb_buf(inbuf);
419 if (SVAL(inbuf, smb_vwv7) == 0) {
420 /* an anonymous request */
421 return reply_spnego_anonymous(conn, inbuf, outbuf, length, bufsize);
424 /* pull the spnego blob */
425 blob1 = data_blob(p, SVAL(inbuf, smb_vwv7));
428 file_save("negotiate.dat", blob1.data, blob1.length);
431 if (blob1.data[0] == ASN1_APPLICATION(0)) {
432 /* its a negTokenTarg packet */
433 ret = reply_spnego_negotiate(conn, inbuf, outbuf, length, bufsize, blob1);
434 data_blob_free(&blob1);
438 if (blob1.data[0] == ASN1_CONTEXT(1)) {
439 /* its a auth packet */
440 ret = reply_spnego_auth(conn, inbuf, outbuf, length, bufsize, blob1);
441 data_blob_free(&blob1);
445 /* what sort of packet is this? */
446 DEBUG(1,("Unknown packet in reply_sesssetup_and_X_spnego\n"));
448 data_blob_free(&blob1);
450 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
454 /****************************************************************************
455 reply to a session setup command
456 ****************************************************************************/
457 int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
458 int length,int bufsize)
464 DATA_BLOB plaintext_password;
468 fstring native_lanman;
470 static BOOL done_sesssetup = False;
471 extern BOOL global_encrypted_passwords_negotiated;
472 extern BOOL global_spnego_negotiated;
473 extern uint32 global_client_caps;
475 extern fstring remote_machine;
476 extern userdom_struct current_user_info;
479 auth_usersupplied_info *user_info = NULL;
480 auth_serversupplied_info *server_info = NULL;
482 BOOL doencrypt = global_encrypted_passwords_negotiated;
484 START_PROFILE(SMBsesssetupX);
486 ZERO_STRUCT(lm_resp);
487 ZERO_STRUCT(nt_resp);
488 ZERO_STRUCT(plaintext_password);
490 DEBUG(3,("wct=%d flg2=0x%x\n", CVAL(inbuf, smb_wct), SVAL(inbuf, smb_flg2)));
492 /* a SPNEGO session setup has 12 command words, whereas a normal
493 NT1 session setup has 13. See the cifs spec. */
494 if (CVAL(inbuf, smb_wct) == 12 &&
495 (SVAL(inbuf, smb_flg2) & FLAGS2_EXTENDED_SECURITY)) {
496 return reply_sesssetup_and_X_spnego(conn, inbuf, outbuf, length, bufsize);
499 if (global_spnego_negotiated) {
500 DEBUG(0,("reply_sesssetup_and_X: Rejecting attempt at 'normal' session setup after negotiating spnego.\n"));
501 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
504 smb_bufsize = SVAL(inbuf,smb_vwv2);
506 if (Protocol < PROTOCOL_NT1) {
507 uint16 passlen1 = SVAL(inbuf,smb_vwv7);
508 if (passlen1 > MAX_PASS_LEN) {
509 return ERROR_DOS(ERRDOS,ERRbuftoosmall);
513 lm_resp = data_blob(smb_buf(inbuf), passlen1);
515 plaintext_password = data_blob(smb_buf(inbuf), passlen1+1);
516 if (!plaintext_password.data) {
517 DEBUG(0,("reply_sesssetup_and_X: malloc failed for plaintext_password!\n"));
518 return ERROR_NT(NT_STATUS_NO_MEMORY);
520 /* Ensure null termination */
521 plaintext_password.data[passlen1] = 0;
525 srvstr_pull(inbuf, user, smb_buf(inbuf)+passlen1, sizeof(user), -1, STR_TERMINATE);
528 uint16 passlen1 = SVAL(inbuf,smb_vwv7);
529 uint16 passlen2 = SVAL(inbuf,smb_vwv8);
530 enum remote_arch_types ra_type = get_remote_arch();
531 char *p = smb_buf(inbuf);
533 if(global_client_caps == 0)
534 global_client_caps = IVAL(inbuf,smb_vwv11);
536 /* client_caps is used as final determination if client is NT or Win95.
537 This is needed to return the correct error codes in some
541 if(ra_type == RA_WINNT || ra_type == RA_WIN2K || ra_type == RA_WIN95) {
542 if(!(global_client_caps & (CAP_NT_SMBS | CAP_STATUS32))) {
543 set_remote_arch( RA_WIN95);
547 if (passlen1 > MAX_PASS_LEN) {
548 return ERROR_DOS(ERRDOS,ERRbuftoosmall);
551 passlen1 = MIN(passlen1, MAX_PASS_LEN);
552 passlen2 = MIN(passlen2, MAX_PASS_LEN);
555 /* both Win95 and WinNT stuff up the password lengths for
556 non-encrypting systems. Uggh.
558 if passlen1==24 its a win95 system, and its setting the
559 password length incorrectly. Luckily it still works with the
560 default code because Win95 will null terminate the password
563 if passlen1>0 and passlen2>0 then maybe its a NT box and its
564 setting passlen2 to some random value which really stuffs
565 things up. we need to fix that one. */
567 if (passlen1 > 0 && passlen2 > 0 && passlen2 != 24 && passlen2 != 1)
571 if (lp_restrict_anonymous()) {
572 /* there seems to be no reason behind the
573 * differences in MS clients formatting
574 * various info like the domain, NativeOS, and
575 * NativeLanMan fields. Win95 in particular
576 * seems to have an extra null byte between
577 * the username and the domain, or the
578 * password length calculation is wrong, which
579 * throws off the string extraction routines
580 * below. This makes the value of domain be
581 * the empty string, which fails the restrict
582 * anonymous check further down. This
583 * compensates for that, and allows browsing
584 * to work in mixed NT and win95 environments
585 * even when restrict anonymous is true. AAB
587 dump_data(100, p, 0x70);
588 DEBUG(9, ("passlen1=%d, passlen2=%d\n", passlen1, passlen2));
589 if (ra_type == RA_WIN95 && !passlen1 && !passlen2 && p[0] == 0 && p[1] == 0) {
590 DEBUG(0, ("restrict anonymous parameter used in a win95 environment!\n"));
591 DEBUG(0, ("client is win95 and broken passlen1 offset -- attempting fix\n"));
592 DEBUG(0, ("if win95 cilents are having difficulty browsing, you will be unable to use restrict anonymous\n"));
597 /* Save the lanman2 password and the NT md4 password. */
599 if ((doencrypt) && (passlen1 != 0) && (passlen1 != 24)) {
604 lm_resp = data_blob(p, passlen1);
605 nt_resp = data_blob(p+passlen1, passlen2);
607 plaintext_password = data_blob(p, passlen1+1);
608 /* Ensure null termination */
609 plaintext_password.data[passlen1] = 0;
612 p += passlen1 + passlen2;
613 p += srvstr_pull(inbuf, user, p, sizeof(user), -1,
615 p += srvstr_pull(inbuf, domain, p, sizeof(domain),
617 p += srvstr_pull(inbuf, native_os, p, sizeof(native_os),
619 p += srvstr_pull(inbuf, native_lanman, p, sizeof(native_lanman),
621 DEBUG(3,("Domain=[%s] NativeOS=[%s] NativeLanMan=[%s]\n",
622 domain,native_os,native_lanman));
625 /* don't allow for weird usernames or domains */
626 alpha_strcpy(user, user, ". _-$", sizeof(user));
627 alpha_strcpy(domain, domain, ". _-", sizeof(domain));
628 if (strstr(user, "..") || strstr(domain,"..")) {
629 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
632 DEBUG(3,("sesssetupX:name=[%s]\\[%s]@[%s]\n", domain, user, remote_machine));
634 /* If no username is sent use the guest account */
636 pstrcpy(user,lp_guestaccount(-1));
640 pstrcpy(current_user_info.smb_name,user);
642 reload_services(True);
644 if (lp_security() == SEC_SHARE) {
645 /* in share level we should ignore any passwords */
647 data_blob_free(&lm_resp);
648 data_blob_free(&nt_resp);
649 data_blob_clear_free(&plaintext_password);
653 add_session_user(user);
656 if (done_sesssetup && lp_restrict_anonymous()) {
657 /* tests show that even if browsing is done over
658 * already validated connections without a username
659 * and password the domain is still provided, which it
660 * wouldn't be if it was a purely anonymous
661 * connection. So, in order to restrict anonymous, we
662 * only deny connections that have no session
663 * information. If a domain has been provided, then
664 * it's not a purely anonymous connection. AAB */
665 if (!*user && !*domain) {
666 DEBUG(0, ("restrict anonymous is True and anonymous connection attempted. Denying access.\n"));
668 data_blob_free(&lm_resp);
669 data_blob_free(&nt_resp);
670 data_blob_clear_free(&plaintext_password);
672 END_PROFILE(SMBsesssetupX);
673 return ERROR_DOS(ERRDOS,ERRnoaccess);
679 if (!make_user_info_for_reply(&user_info,
682 plaintext_password, doencrypt)) {
683 return ERROR_NT(NT_STATUS_NO_MEMORY);
686 nt_status = check_password(user_info, &server_info);
688 free_user_info(&user_info);
690 data_blob_free(&lm_resp);
691 data_blob_free(&nt_resp);
692 data_blob_clear_free(&plaintext_password);
694 if (!NT_STATUS_IS_OK(nt_status)) {
695 if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
696 if ((lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_USER) ||
697 (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_PASSWORD)) {
698 DEBUG(3,("No such user %s [%s] - using guest account\n",user, domain));
699 pstrcpy(user,lp_guestaccount(-1));
703 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
704 if (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_PASSWORD) {
705 pstrcpy(user,lp_guestaccount(-1));
706 DEBUG(3,("Registered username %s for guest access\n",user));
709 /* Match WinXP and don't give the game away */
710 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
714 free_server_info(&server_info);
715 return ERROR_NT(nt_status_squash(nt_status));
720 /* it's ok - setup a reply */
721 if (Protocol < PROTOCOL_NT1) {
722 set_message(outbuf,3,0,True);
725 set_message(outbuf,3,0,True);
727 p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
728 p += srvstr_push(outbuf, p, "Samba", -1, STR_TERMINATE);
729 p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
730 set_message_end(outbuf,p);
731 /* perhaps grab OS version here?? */
735 SSVAL(outbuf,smb_vwv2,1);
736 free_server_info(&server_info);
737 make_server_info_guest(&server_info);
739 const char *home_dir = pdb_get_homedir(server_info->sam_account);
740 const char *username = pdb_get_username(server_info->sam_account);
741 if ((home_dir && *home_dir)
742 && (lp_servicenumber(username) < 0)) {
743 add_home_service(username, home_dir);
747 /* register the name and uid as being validated, so further connections
748 to a uid can get through without a password, on the same VC */
750 sess_vuid = register_vuid(server_info, user, guest);
752 free_server_info(&server_info);
754 if (sess_vuid == -1) {
755 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
759 SSVAL(outbuf,smb_uid,sess_vuid);
760 SSVAL(inbuf,smb_uid,sess_vuid);
763 max_send = MIN(max_send,smb_bufsize);
765 done_sesssetup = True;
767 END_PROFILE(SMBsesssetupX);
768 return chain_reply(inbuf,outbuf,length,bufsize);