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;
26 static auth_authsupplied_info *ntlmssp_auth_info;
28 /****************************************************************************
29 Add the standard 'Samba' signature to the end of the session setup.
30 ****************************************************************************/
31 static void add_signature(char *outbuf)
35 p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
36 p += srvstr_push(outbuf, p, "Samba", -1, STR_TERMINATE);
37 p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
38 set_message_end(outbuf,p);
41 /****************************************************************************
42 Do a 'guest' logon, getting back the
43 ****************************************************************************/
44 static NTSTATUS check_guest_password(auth_serversupplied_info **server_info)
47 auth_authsupplied_info *auth_info;
48 auth_usersupplied_info *user_info = NULL;
55 DEBUG(3,("Got anonymous request\n"));
57 make_user_info_guest(&user_info);
58 make_auth_info_fixed(&auth_info, chal);
60 nt_status = check_password(user_info, auth_info, server_info);
61 free_auth_info(&auth_info);
67 /****************************************************************************
68 reply to a session setup spnego negotiate packet for kerberos
69 ****************************************************************************/
70 static int reply_spnego_kerberos(connection_struct *conn,
71 char *inbuf, char *outbuf,
72 int length, int bufsize,
77 krb5_auth_context auth_context = NULL;
78 krb5_keytab keytab = NULL;
80 krb5_ticket *tkt = NULL;
82 char *realm, *client, *p;
83 const struct passwd *pw;
86 auth_serversupplied_info *server_info = NULL;
90 if (!spnego_parse_krb5_wrap(*secblob, &ticket)) {
91 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
94 ret = krb5_init_context(&context);
96 DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
97 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
100 packet.length = ticket.length;
101 packet.data = (krb5_pointer)ticket.data;
104 file_save("/tmp/ticket.dat", ticket.data, ticket.length);
107 if ((ret = krb5_rd_req(context, &auth_context, &packet,
108 NULL, keytab, NULL, &tkt))) {
109 DEBUG(3,("krb5_rd_req failed (%s)\n",
110 error_message(ret)));
111 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
115 if (tkt->enc_part2) {
116 file_save("/tmp/authdata.dat",
117 tkt->enc_part2->authorization_data[0]->contents,
118 tkt->enc_part2->authorization_data[0]->length);
122 if ((ret = krb5_unparse_name(context, tkt->enc_part2->client,
124 DEBUG(3,("krb5_unparse_name failed (%s)\n",
125 error_message(ret)));
126 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
129 DEBUG(3,("Ticket name is [%s]\n", client));
131 p = strchr_m(client, '@');
133 DEBUG(3,("Doesn't look like a valid principal\n"));
134 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
138 if (strcasecmp(p+1, realm) != 0) {
139 DEBUG(3,("Ticket for incorrect realm %s\n", p+1));
140 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
145 /* the password is good - let them in */
146 pw = smb_getpwnam(user,False);
148 DEBUG(1,("Username %s is invalid on this system\n",user));
149 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
152 if (!make_server_info_pw(&server_info,pw)) {
153 DEBUG(1,("make_server_info_from_pw failed!\n"));
154 return ERROR_NT(NT_STATUS_NO_MEMORY);
157 sess_vuid = register_vuid(server_info, user);
159 free_server_info(&server_info);
161 if (sess_vuid == -1) {
162 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
165 set_message(outbuf,4,0,True);
166 SSVAL(outbuf, smb_vwv3, 0);
167 add_signature(outbuf);
169 SSVAL(outbuf,smb_uid,sess_vuid);
170 SSVAL(inbuf,smb_uid,sess_vuid);
172 return chain_reply(inbuf,outbuf,length,bufsize);
177 /****************************************************************************
178 send a security blob via a session setup reply
179 ****************************************************************************/
180 static BOOL reply_sesssetup_blob(connection_struct *conn, char *outbuf,
185 set_message(outbuf,4,0,True);
187 /* we set NT_STATUS_MORE_PROCESSING_REQUIRED to tell the other end
188 that we aren't finished yet */
190 SIVAL(outbuf, smb_rcls, NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED));
191 SSVAL(outbuf, smb_vwv0, 0xFF); /* no chaining possible */
192 SSVAL(outbuf, smb_vwv3, blob.length);
194 memcpy(p, blob.data, blob.length);
196 p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
197 p += srvstr_push(outbuf, p, "Samba", -1, STR_TERMINATE);
198 p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
199 set_message_end(outbuf,p);
201 return send_smb(smbd_server_fd(),outbuf);
204 /****************************************************************************
205 reply to a session setup spnego negotiate packet
206 ****************************************************************************/
207 static int reply_spnego_negotiate(connection_struct *conn,
210 int length, int bufsize,
213 char *OIDs[ASN1_MAX_OIDS];
216 uint32 ntlmssp_command, neg_flags;
217 DATA_BLOB sess_key, chal, spnego_chal;
219 BOOL got_kerberos = False;
221 /* parse out the OIDs and the first sec blob */
222 if (!parse_negTokenTarg(blob1, OIDs, &secblob)) {
223 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
226 for (i=0;OIDs[i];i++) {
227 DEBUG(3,("Got OID %s\n", OIDs[i]));
228 if (strcmp(OID_KERBEROS5, OIDs[i]) == 0 ||
229 strcmp(OID_KERBEROS5_OLD, OIDs[i]) == 0) {
234 DEBUG(3,("Got secblob of size %d\n", secblob.length));
238 int ret = reply_spnego_kerberos(conn, inbuf, outbuf,
239 length, bufsize, &secblob);
240 data_blob_free(&secblob);
245 /* parse the NTLMSSP packet */
247 file_save("secblob.dat", secblob.data, secblob.length);
250 if (!msrpc_parse(&secblob, "CddB",
255 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
258 data_blob_free(&secblob);
259 data_blob_free(&sess_key);
261 if (ntlmssp_command != NTLMSSP_NEGOTIATE) {
262 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
265 DEBUG(3,("Got neg_flags=%08x\n", neg_flags));
267 if (!make_auth_info_subsystem(&ntlmssp_auth_info)) {
268 return ERROR_NT(NT_STATUS_NO_MEMORY);
271 cryptkey = auth_get_challange(ntlmssp_auth_info);
273 /* Give them the challenge. For now, ignore neg_flags and just
274 return the flags we want. Obviously this is not correct */
276 neg_flags = NTLMSSP_NEGOTIATE_UNICODE |
277 NTLMSSP_NEGOTIATE_LM_KEY |
278 NTLMSSP_NEGOTIATE_NTLM;
280 msrpc_gen(&chal, "Cddddbdddd",
286 cryptkey.data, cryptkey.length,
290 if (!spnego_gen_challenge(&spnego_chal, &chal, &chal)) {
291 DEBUG(3,("Failed to generate challenge\n"));
292 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
295 /* now tell the client to send the auth packet */
296 reply_sesssetup_blob(conn, outbuf, spnego_chal);
298 data_blob_free(&chal);
299 data_blob_free(&cryptkey);
300 data_blob_free(&spnego_chal);
302 /* and tell smbd that we have already replied to this packet */
307 /****************************************************************************
308 reply to a session setup spnego auth packet
309 ****************************************************************************/
310 static int reply_spnego_auth(connection_struct *conn, char *inbuf, char *outbuf,
311 int length, int bufsize,
315 char *workgroup, *user, *machine;
316 DATA_BLOB lmhash, nthash, sess_key;
317 DATA_BLOB plaintext_password = data_blob(NULL, 0);
318 uint32 ntlmssp_command, neg_flags;
322 auth_usersupplied_info *user_info = NULL;
323 auth_serversupplied_info *server_info = NULL;
325 if (!spnego_parse_auth(blob1, &auth)) {
327 file_save("auth.dat", blob1.data, blob1.length);
329 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
332 /* now the NTLMSSP encoded auth hashes */
333 if (!msrpc_parse(&auth, "CdBBUUUBd",
343 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
346 data_blob_free(&auth);
347 data_blob_free(&sess_key);
349 DEBUG(3,("Got user=[%s] workgroup=[%s] machine=[%s] len1=%d len2=%d\n",
350 user, workgroup, machine, lmhash.length, nthash.length));
353 file_save("nthash1.dat", nthash.data, nthash.length);
354 file_save("lmhash1.dat", lmhash.data, lmhash.length);
357 if (!make_user_info_map(&user_info,
363 return ERROR_NT(NT_STATUS_NO_MEMORY);
366 nt_status = check_password(user_info, ntlmssp_auth_info, &server_info);
368 free_auth_info(&ntlmssp_auth_info);
370 free_user_info(&user_info);
372 data_blob_free(&lmhash);
374 data_blob_free(&nthash);
376 if (!NT_STATUS_IS_OK(nt_status)) {
377 return ERROR_NT(nt_status_squash(nt_status));
380 sess_vuid = register_vuid(server_info, user);
382 free_server_info(&server_info);
384 if (sess_vuid == -1) {
385 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
388 set_message(outbuf,4,0,True);
389 SSVAL(outbuf, smb_vwv3, 0);
390 add_signature(outbuf);
392 SSVAL(outbuf,smb_uid,sess_vuid);
393 SSVAL(inbuf,smb_uid,sess_vuid);
395 return chain_reply(inbuf,outbuf,length,bufsize);
399 /****************************************************************************
400 reply to a session setup spnego anonymous packet
401 ****************************************************************************/
402 static int reply_spnego_anonymous(connection_struct *conn, char *inbuf, char *outbuf,
403 int length, int bufsize)
406 auth_serversupplied_info *server_info = NULL;
409 nt_status = check_guest_password(&server_info);
411 if (!NT_STATUS_IS_OK(nt_status)) {
412 return ERROR_NT(nt_status_squash(nt_status));
415 sess_vuid = register_vuid(server_info, lp_guestaccount());
417 free_server_info(&server_info);
419 if (sess_vuid == -1) {
420 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
423 set_message(outbuf,4,0,True);
424 SSVAL(outbuf, smb_vwv3, 0);
425 add_signature(outbuf);
427 SSVAL(outbuf,smb_uid,sess_vuid);
428 SSVAL(inbuf,smb_uid,sess_vuid);
430 return chain_reply(inbuf,outbuf,length,bufsize);
434 /****************************************************************************
435 reply to a session setup command
436 ****************************************************************************/
437 static int reply_sesssetup_and_X_spnego(connection_struct *conn, char *inbuf,char *outbuf,
438 int length,int bufsize)
444 DEBUG(3,("Doing spnego session setup\n"));
446 if (global_client_caps == 0) {
447 global_client_caps = IVAL(inbuf,smb_vwv10);
450 p = (uint8 *)smb_buf(inbuf);
452 if (SVAL(inbuf, smb_vwv7) == 0) {
453 /* an anonymous request */
454 return reply_spnego_anonymous(conn, inbuf, outbuf, length, bufsize);
457 /* pull the spnego blob */
458 blob1 = data_blob(p, SVAL(inbuf, smb_vwv7));
461 file_save("negotiate.dat", blob1.data, blob1.length);
464 if (blob1.data[0] == ASN1_APPLICATION(0)) {
465 /* its a negTokenTarg packet */
466 ret = reply_spnego_negotiate(conn, inbuf, outbuf, length, bufsize, blob1);
467 data_blob_free(&blob1);
471 if (blob1.data[0] == ASN1_CONTEXT(1)) {
472 /* its a auth packet */
473 ret = reply_spnego_auth(conn, inbuf, outbuf, length, bufsize, blob1);
474 data_blob_free(&blob1);
478 /* what sort of packet is this? */
479 DEBUG(1,("Unknown packet in reply_sesssetup_and_X_spnego\n"));
481 data_blob_free(&blob1);
483 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
487 /****************************************************************************
488 reply to a session setup command
489 ****************************************************************************/
490 int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
491 int length,int bufsize)
497 DATA_BLOB plaintext_password;
499 pstring sub_user; /* Sainitised username for substituion */
502 fstring native_lanman;
503 static BOOL done_sesssetup = False;
504 extern BOOL global_encrypted_passwords_negotiated;
505 extern BOOL global_spnego_negotiated;
507 extern fstring remote_machine;
508 extern userdom_struct current_user_info;
511 auth_usersupplied_info *user_info = NULL;
512 extern auth_authsupplied_info *negprot_global_auth_info;
513 auth_serversupplied_info *server_info = NULL;
517 BOOL doencrypt = global_encrypted_passwords_negotiated;
519 START_PROFILE(SMBsesssetupX);
521 ZERO_STRUCT(lm_resp);
522 ZERO_STRUCT(nt_resp);
523 ZERO_STRUCT(plaintext_password);
525 DEBUG(3,("wct=%d flg2=0x%x\n", CVAL(inbuf, smb_wct), SVAL(inbuf, smb_flg2)));
527 /* a SPNEGO session setup has 12 command words, whereas a normal
528 NT1 session setup has 13. See the cifs spec. */
529 if (CVAL(inbuf, smb_wct) == 12 &&
530 (SVAL(inbuf, smb_flg2) & FLAGS2_EXTENDED_SECURITY)) {
531 return reply_sesssetup_and_X_spnego(conn, inbuf, outbuf, length, bufsize);
534 smb_bufsize = SVAL(inbuf,smb_vwv2);
536 if (Protocol < PROTOCOL_NT1) {
537 uint16 passlen1 = SVAL(inbuf,smb_vwv7);
538 if (passlen1 > MAX_PASS_LEN) {
539 return ERROR_DOS(ERRDOS,ERRbuftoosmall);
543 lm_resp = data_blob(smb_buf(inbuf), passlen1);
545 plaintext_password = data_blob(smb_buf(inbuf), passlen1+1);
546 /* Ensure null termination */
547 plaintext_password.data[passlen1] = 0;
550 srvstr_pull(inbuf, user, smb_buf(inbuf)+passlen1, sizeof(user), -1, STR_TERMINATE);
554 uint16 passlen1 = SVAL(inbuf,smb_vwv7);
555 uint16 passlen2 = SVAL(inbuf,smb_vwv8);
556 enum remote_arch_types ra_type = get_remote_arch();
557 char *p = smb_buf(inbuf);
559 if(global_client_caps == 0)
560 global_client_caps = IVAL(inbuf,smb_vwv11);
562 /* client_caps is used as final determination if client is NT or Win95.
563 This is needed to return the correct error codes in some
567 if(ra_type == RA_WINNT || ra_type == RA_WIN2K || ra_type == RA_WIN95) {
568 if(!(global_client_caps & (CAP_NT_SMBS | CAP_STATUS32))) {
569 set_remote_arch( RA_WIN95);
573 if (passlen1 > MAX_PASS_LEN) {
574 return ERROR_DOS(ERRDOS,ERRbuftoosmall);
577 passlen1 = MIN(passlen1, MAX_PASS_LEN);
578 passlen2 = MIN(passlen2, MAX_PASS_LEN);
581 /* both Win95 and WinNT stuff up the password lengths for
582 non-encrypting systems. Uggh.
584 if passlen1==24 its a win95 system, and its setting the
585 password length incorrectly. Luckily it still works with the
586 default code because Win95 will null terminate the password
589 if passlen1>0 and passlen2>0 then maybe its a NT box and its
590 setting passlen2 to some random value which really stuffs
591 things up. we need to fix that one. */
593 if (passlen1 > 0 && passlen2 > 0 && passlen2 != 24 && passlen2 != 1)
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));
635 if (global_spnego_negotiated) {
636 DEBUG(0,("reply_sesssetup_and_X: Rejecting attempt at 'normal' session setup after negotiating spnego.\n"));
637 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
642 pstrcpy(sub_user, user);
644 pstrcpy(sub_user, lp_guestaccount());
647 pstrcpy(current_user_info.smb_name,sub_user);
649 reload_services(True);
651 if (lp_security() == SEC_SHARE) {
652 /* in share level we should ignore any passwords */
654 data_blob_free(&lm_resp);
655 data_blob_free(&nt_resp);
656 data_blob_clear_free(&plaintext_password);
658 map_username(sub_user);
659 add_session_user(sub_user);
660 /* Then force it to null for the benfit of the code below */
666 nt_status = check_guest_password(&server_info);
668 } else if (doencrypt) {
669 if (!make_user_info_for_reply_enc(&user_info,
672 plaintext_password)) {
673 return ERROR_NT(NT_STATUS_NO_MEMORY);
676 nt_status = check_password(user_info, negprot_global_auth_info, &server_info);
679 auth_authsupplied_info *plaintext_auth_info = NULL;
681 if (!make_auth_info_subsystem(&plaintext_auth_info)) {
682 return ERROR_NT(NT_STATUS_NO_MEMORY);
685 chal = auth_get_challange(plaintext_auth_info);
687 if (!make_user_info_for_reply(&user_info,
688 user, domain, chal.data,
689 plaintext_password)) {
690 return ERROR_NT(NT_STATUS_NO_MEMORY);
693 nt_status = check_password(user_info, plaintext_auth_info, &server_info);
695 data_blob_free(&chal);
696 free_auth_info(&plaintext_auth_info);
699 free_user_info(&user_info);
701 data_blob_free(&lm_resp);
702 data_blob_free(&nt_resp);
703 data_blob_clear_free(&plaintext_password);
705 if (!NT_STATUS_IS_OK(nt_status)) {
706 if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
707 if ((lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_USER) ||
708 (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_PASSWORD)) {
710 DEBUG(3,("No such user %s [%s] - using guest account\n",user, domain));
711 make_server_info_guest(&server_info);
712 nt_status = NT_STATUS_OK;
715 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
716 if (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_PASSWORD) {
717 DEBUG(3,("Registered username %s for guest access\n",user));
718 make_server_info_guest(&server_info);
719 nt_status = NT_STATUS_OK;
724 if (!NT_STATUS_IS_OK(nt_status)) {
725 return ERROR_NT(nt_status_squash(nt_status));
728 /* it's ok - setup a reply */
729 if (Protocol < PROTOCOL_NT1) {
730 set_message(outbuf,3,0,True);
732 set_message(outbuf,3,0,True);
733 add_signature(outbuf);
734 /* perhaps grab OS version here?? */
737 if (server_info->guest) {
738 SSVAL(outbuf,smb_vwv2,1);
740 const char *home_dir = pdb_get_homedir(server_info->sam_account);
741 const char *username = pdb_get_username(server_info->sam_account);
742 if ((home_dir && *home_dir)
743 && (lp_servicenumber(username) < 0)) {
744 add_home_service(username, home_dir);
748 /* register the name and uid as being validated, so further connections
749 to a uid can get through without a password, on the same VC */
751 sess_vuid = register_vuid(server_info, sub_user);
753 free_server_info(&server_info);
755 if (sess_vuid == -1) {
756 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
760 SSVAL(outbuf,smb_uid,sess_vuid);
761 SSVAL(inbuf,smb_uid,sess_vuid);
764 max_send = MIN(max_send,smb_bufsize);
766 done_sesssetup = True;
768 END_PROFILE(SMBsesssetupX);
769 return chain_reply(inbuf,outbuf,length,bufsize);