2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1998-2001
5 Copyright (C) Andrew Bartlett 2001
6 Copyright (C) Jim McDonough 2002
7 Copyright (C) Luke Howard 2003
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.
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.
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.
26 uint32 global_client_caps = 0;
28 static struct auth_ntlmssp_state *global_ntlmssp_state;
31 on a logon error possibly map the error to success if "map to guest"
34 static NTSTATUS do_map_to_guest(NTSTATUS status, auth_serversupplied_info **server_info,
35 const char *user, const char *domain)
37 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
38 if ((lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_USER) ||
39 (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_PASSWORD)) {
40 DEBUG(3,("No such user %s [%s] - using guest account\n",
42 status = make_server_info_guest(server_info);
46 if (NT_STATUS_EQUAL(status, NT_STATUS_WRONG_PASSWORD)) {
47 if (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_PASSWORD) {
48 DEBUG(3,("Registered username %s for guest access\n",user));
49 status = make_server_info_guest(server_info);
57 /****************************************************************************
58 Add the standard 'Samba' signature to the end of the session setup.
59 ****************************************************************************/
60 static void add_signature(char *outbuf)
65 snprintf( lanman, sizeof(lanman), "Samba %s", VERSION );
68 p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
69 p += srvstr_push(outbuf, p, lanman, -1, STR_TERMINATE);
70 p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
71 set_message_end(outbuf,p);
74 /****************************************************************************
75 send a security blob via a session setup reply
76 ****************************************************************************/
77 static BOOL reply_sesssetup_blob(connection_struct *conn, char *outbuf,
78 DATA_BLOB blob, NTSTATUS nt_status)
82 set_message(outbuf,4,0,True);
84 nt_status = nt_status_squash(nt_status);
85 SIVAL(outbuf, smb_rcls, NT_STATUS_V(nt_status));
86 SSVAL(outbuf, smb_vwv0, 0xFF); /* no chaining possible */
87 SSVAL(outbuf, smb_vwv3, blob.length);
90 /* should we cap this? */
91 memcpy(p, blob.data, blob.length);
94 p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
95 p += srvstr_push(outbuf, p, "Samba", -1, STR_TERMINATE);
96 p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
97 set_message_end(outbuf,p);
99 return send_smb(smbd_server_fd(),outbuf);
102 /****************************************************************************
103 Do a 'guest' logon, getting back the
104 ****************************************************************************/
105 static NTSTATUS check_guest_password(auth_serversupplied_info **server_info)
107 struct auth_context *auth_context;
108 auth_usersupplied_info *user_info = NULL;
111 unsigned char chal[8];
115 DEBUG(3,("Got anonymous request\n"));
117 if (!NT_STATUS_IS_OK(nt_status = make_auth_context_fixed(&auth_context, chal))) {
121 if (!make_user_info_guest(&user_info)) {
122 (auth_context->free)(&auth_context);
123 return NT_STATUS_NO_MEMORY;
126 nt_status = auth_context->check_ntlm_password(auth_context, user_info, server_info);
127 (auth_context->free)(&auth_context);
128 free_user_info(&user_info);
134 /****************************************************************************
135 reply to a session setup spnego negotiate packet for kerberos
136 ****************************************************************************/
137 static int reply_spnego_kerberos(connection_struct *conn,
138 char *inbuf, char *outbuf,
139 int length, int bufsize,
144 const struct passwd *pw;
149 DATA_BLOB ap_rep, ap_rep_wrapped, response;
150 auth_serversupplied_info *server_info = NULL;
152 uint8 session_key[16];
154 BOOL foreign = False;
156 if (!spnego_parse_krb5_wrap(*secblob, &ticket, tok_id)) {
157 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
160 ads = ads_init_simple();
163 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
166 ads->auth.realm = strdup(lp_realm());
168 ret = ads_verify_ticket(ads, &ticket, &client, &auth_data, &ap_rep, session_key);
169 if (!NT_STATUS_IS_OK(ret)) {
170 DEBUG(1,("Failed to verify incoming ticket!\n"));
172 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
175 data_blob_free(&auth_data);
177 DEBUG(3,("Ticket name is [%s]\n", client));
179 p = strchr_m(client, '@');
181 DEBUG(3,("Doesn't look like a valid principal\n"));
183 data_blob_free(&ap_rep);
184 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
188 if (strcasecmp(p+1, ads->auth.realm) != 0) {
189 DEBUG(3,("Ticket for foreign realm %s@%s\n", client, p+1));
190 if (!lp_allow_trusted_domains()) {
191 data_blob_free(&ap_rep);
192 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
197 /* this gives a fully qualified user name (ie. with full realm).
198 that leads to very long usernames, but what else can we do? */
199 asprintf(&user, "%s%s%s", p+1, lp_winbind_separator(), client);
201 pw = Get_Pwnam(user);
202 if (!pw && !foreign) {
203 pw = Get_Pwnam(client);
205 user = smb_xstrdup(client);
210 /* setup the string used by %U */
211 sub_set_smb_name(user);
213 reload_services(True);
216 DEBUG(1,("Username %s is invalid on this system\n",user));
217 data_blob_free(&ap_rep);
218 return ERROR_NT(NT_STATUS_NO_SUCH_USER);
221 if (!NT_STATUS_IS_OK(ret = make_server_info_pw(&server_info,pw))) {
222 DEBUG(1,("make_server_info_from_pw failed!\n"));
223 data_blob_free(&ap_rep);
224 return ERROR_NT(ret);
227 /* Copy out the session key from the AP_REQ. */
228 memcpy(server_info->session_key, session_key, sizeof(session_key));
230 /* register_vuid keeps the server info */
231 sess_vuid = register_vuid(server_info, user);
235 if (sess_vuid == -1) {
236 ret = NT_STATUS_LOGON_FAILURE;
238 set_message(outbuf,4,0,True);
239 SSVAL(outbuf, smb_vwv3, 0);
241 if (server_info->guest) {
242 SSVAL(outbuf,smb_vwv2,1);
245 SSVAL(outbuf, smb_uid, sess_vuid);
248 /* wrap that up in a nice GSS-API wrapping */
249 if (NT_STATUS_IS_OK(ret)) {
250 ap_rep_wrapped = spnego_gen_krb5_wrap(ap_rep, TOK_ID_KRB_AP_REP);
252 ap_rep_wrapped = data_blob(NULL, 0);
254 response = spnego_gen_auth_response(&ap_rep_wrapped, ret, OID_KERBEROS5_OLD);
255 reply_sesssetup_blob(conn, outbuf, response, ret);
257 data_blob_free(&ap_rep);
258 data_blob_free(&ap_rep_wrapped);
259 data_blob_free(&response);
261 return -1; /* already replied */
266 /****************************************************************************
267 send a session setup reply, wrapped in SPNEGO.
268 get vuid and check first.
269 end the NTLMSSP exchange context if we are OK/complete fail
270 ***************************************************************************/
271 static BOOL reply_spnego_ntlmssp(connection_struct *conn, char *outbuf,
272 AUTH_NTLMSSP_STATE **auth_ntlmssp_state,
273 DATA_BLOB *ntlmssp_blob, NTSTATUS nt_status)
277 struct auth_serversupplied_info *server_info = NULL;
279 if (NT_STATUS_IS_OK(nt_status)) {
280 server_info = (*auth_ntlmssp_state)->server_info;
282 nt_status = do_map_to_guest(nt_status,
284 (*auth_ntlmssp_state)->ntlmssp_state->user,
285 (*auth_ntlmssp_state)->ntlmssp_state->domain);
288 if (NT_STATUS_IS_OK(nt_status)) {
290 /* register_vuid keeps the server info */
291 sess_vuid = register_vuid(server_info, (*auth_ntlmssp_state)->ntlmssp_state->user);
292 (*auth_ntlmssp_state)->server_info = NULL;
294 if (sess_vuid == -1) {
295 nt_status = NT_STATUS_LOGON_FAILURE;
298 set_message(outbuf,4,0,True);
299 SSVAL(outbuf, smb_vwv3, 0);
301 if (server_info->guest) {
302 SSVAL(outbuf,smb_vwv2,1);
305 SSVAL(outbuf,smb_uid,sess_vuid);
309 response = spnego_gen_auth_response(ntlmssp_blob, nt_status, OID_NTLMSSP);
310 ret = reply_sesssetup_blob(conn, outbuf, response, nt_status);
311 data_blob_free(&response);
313 /* NT_STATUS_MORE_PROCESSING_REQUIRED from our NTLMSSP code tells us,
314 and the other end, that we are not finished yet. */
316 if (!ret || !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
317 auth_ntlmssp_end(auth_ntlmssp_state);
323 /****************************************************************************
324 reply to a session setup spnego negotiate packet
325 ****************************************************************************/
326 static int reply_spnego_negotiate(connection_struct *conn,
329 int length, int bufsize,
332 char *OIDs[ASN1_MAX_OIDS];
336 BOOL got_kerberos = False;
339 /* parse out the OIDs and the first sec blob */
340 if (!parse_negTokenTarg(blob1, OIDs, &secblob)) {
341 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
344 for (i=0;OIDs[i];i++) {
345 DEBUG(3,("Got OID %s\n", OIDs[i]));
346 if (strcmp(OID_KERBEROS5, OIDs[i]) == 0 ||
347 strcmp(OID_KERBEROS5_OLD, OIDs[i]) == 0) {
352 DEBUG(3,("Got secblob of size %d\n", secblob.length));
355 if (got_kerberos && (SEC_ADS == lp_security())) {
356 int ret = reply_spnego_kerberos(conn, inbuf, outbuf,
357 length, bufsize, &secblob);
358 data_blob_free(&secblob);
363 if (global_ntlmssp_state) {
364 auth_ntlmssp_end(&global_ntlmssp_state);
367 nt_status = auth_ntlmssp_start(&global_ntlmssp_state);
368 if (!NT_STATUS_IS_OK(nt_status)) {
369 return ERROR_NT(nt_status);
372 nt_status = auth_ntlmssp_update(global_ntlmssp_state,
375 data_blob_free(&secblob);
377 reply_spnego_ntlmssp(conn, outbuf, &global_ntlmssp_state,
380 data_blob_free(&chal);
382 /* already replied */
387 /****************************************************************************
388 reply to a session setup spnego auth packet
389 ****************************************************************************/
390 static int reply_spnego_auth(connection_struct *conn, char *inbuf, char *outbuf,
391 int length, int bufsize,
394 DATA_BLOB auth, auth_reply;
395 NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
397 if (!spnego_parse_auth(blob1, &auth)) {
399 file_save("auth.dat", blob1.data, blob1.length);
401 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
404 if (!global_ntlmssp_state) {
405 /* auth before negotiatiate? */
406 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
409 nt_status = auth_ntlmssp_update(global_ntlmssp_state,
412 data_blob_free(&auth);
414 reply_spnego_ntlmssp(conn, outbuf, &global_ntlmssp_state,
415 &auth_reply, nt_status);
417 data_blob_free(&auth_reply);
419 /* and tell smbd that we have already replied to this packet */
424 /****************************************************************************
425 reply to a session setup command
426 ****************************************************************************/
427 static int reply_sesssetup_and_X_spnego(connection_struct *conn, char *inbuf,
429 int length,int bufsize)
435 fstring native_os, native_lanman;
437 uint16 data_blob_len = SVAL(inbuf, smb_vwv7);
438 enum remote_arch_types ra_type = get_remote_arch();
440 DEBUG(3,("Doing spnego session setup\n"));
442 if (global_client_caps == 0) {
443 global_client_caps = IVAL(inbuf,smb_vwv10);
446 p = (uint8 *)smb_buf(inbuf);
448 if (data_blob_len == 0) {
449 /* an invalid request */
450 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
453 bufrem = smb_bufrem(inbuf, p);
454 /* pull the spnego blob */
455 blob1 = data_blob(p, MIN(bufrem, data_blob_len));
458 file_save("negotiate.dat", blob1.data, blob1.length);
461 p2 = inbuf + smb_vwv13 + data_blob_len;
462 p2 += srvstr_pull_buf(inbuf, native_os, p2, sizeof(native_os), STR_TERMINATE);
463 p2 += srvstr_pull_buf(inbuf, native_lanman, p2, sizeof(native_lanman), STR_TERMINATE);
464 DEBUG(3,("NativeOS=[%s] NativeLanMan=[%s]\n", native_os, native_lanman));
466 if ( ra_type == RA_WIN2K )
467 ra_lanman_string( native_lanman );
469 if (blob1.data[0] == ASN1_APPLICATION(0)) {
470 /* its a negTokenTarg packet */
471 ret = reply_spnego_negotiate(conn, inbuf, outbuf, length, bufsize, blob1);
472 data_blob_free(&blob1);
476 if (blob1.data[0] == ASN1_CONTEXT(1)) {
477 /* its a auth packet */
478 ret = reply_spnego_auth(conn, inbuf, outbuf, length, bufsize, blob1);
479 data_blob_free(&blob1);
483 /* what sort of packet is this? */
484 DEBUG(1,("Unknown packet in reply_sesssetup_and_X_spnego\n"));
486 data_blob_free(&blob1);
488 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
492 /****************************************************************************
493 reply to a session setup command
494 ****************************************************************************/
495 int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
496 int length,int bufsize)
502 DATA_BLOB plaintext_password;
504 fstring sub_user; /* Sainitised username for substituion */
507 fstring native_lanman;
508 static BOOL done_sesssetup = False;
509 extern BOOL global_encrypted_passwords_negotiated;
510 extern BOOL global_spnego_negotiated;
514 auth_usersupplied_info *user_info = NULL;
515 extern struct auth_context *negprot_global_auth_context;
516 auth_serversupplied_info *server_info = NULL;
520 BOOL doencrypt = global_encrypted_passwords_negotiated;
522 START_PROFILE(SMBsesssetupX);
524 ZERO_STRUCT(lm_resp);
525 ZERO_STRUCT(nt_resp);
526 ZERO_STRUCT(plaintext_password);
528 DEBUG(3,("wct=%d flg2=0x%x\n", CVAL(inbuf, smb_wct), SVAL(inbuf, smb_flg2)));
530 /* a SPNEGO session setup has 12 command words, whereas a normal
531 NT1 session setup has 13. See the cifs spec. */
532 if (CVAL(inbuf, smb_wct) == 12 &&
533 (SVAL(inbuf, smb_flg2) & FLAGS2_EXTENDED_SECURITY)) {
534 if (!global_spnego_negotiated) {
535 DEBUG(0,("reply_sesssetup_and_X: Rejecting attempt at SPNEGO session setup when it was not negoitiated.\n"));
536 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
539 return reply_sesssetup_and_X_spnego(conn, inbuf, outbuf, length, bufsize);
542 smb_bufsize = SVAL(inbuf,smb_vwv2);
544 if (Protocol < PROTOCOL_NT1) {
545 uint16 passlen1 = SVAL(inbuf,smb_vwv7);
546 if ((passlen1 > MAX_PASS_LEN) || (passlen1 > smb_bufrem(inbuf, smb_buf(inbuf)))) {
547 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
551 lm_resp = data_blob(smb_buf(inbuf), passlen1);
553 plaintext_password = data_blob(smb_buf(inbuf), passlen1+1);
554 /* Ensure null termination */
555 plaintext_password.data[passlen1] = 0;
558 srvstr_pull_buf(inbuf, user, smb_buf(inbuf)+passlen1, sizeof(user), STR_TERMINATE);
562 uint16 passlen1 = SVAL(inbuf,smb_vwv7);
563 uint16 passlen2 = SVAL(inbuf,smb_vwv8);
564 enum remote_arch_types ra_type = get_remote_arch();
565 char *p = smb_buf(inbuf);
567 if(global_client_caps == 0)
568 global_client_caps = IVAL(inbuf,smb_vwv11);
570 /* client_caps is used as final determination if client is NT or Win95.
571 This is needed to return the correct error codes in some
575 if(ra_type == RA_WINNT || ra_type == RA_WIN2K || ra_type == RA_WIN95) {
576 if(!(global_client_caps & (CAP_NT_SMBS | CAP_STATUS32))) {
577 set_remote_arch( RA_WIN95);
582 /* both Win95 and WinNT stuff up the password lengths for
583 non-encrypting systems. Uggh.
585 if passlen1==24 its a win95 system, and its setting the
586 password length incorrectly. Luckily it still works with the
587 default code because Win95 will null terminate the password
590 if passlen1>0 and passlen2>0 then maybe its a NT box and its
591 setting passlen2 to some random value which really stuffs
592 things up. we need to fix that one. */
594 if (passlen1 > 0 && passlen2 > 0 && passlen2 != 24 && passlen2 != 1)
598 /* check for nasty tricks */
599 if (passlen1 > MAX_PASS_LEN || passlen1 > smb_bufrem(inbuf, p)) {
600 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
603 if (passlen2 > MAX_PASS_LEN || passlen2 > smb_bufrem(inbuf, p+passlen1)) {
604 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
607 /* Save the lanman2 password and the NT md4 password. */
609 if ((doencrypt) && (passlen1 != 0) && (passlen1 != 24)) {
614 lm_resp = data_blob(p, passlen1);
615 nt_resp = data_blob(p+passlen1, passlen2);
618 srvstr_pull(inbuf, pass, smb_buf(inbuf),
619 sizeof(pass), passlen1, STR_TERMINATE);
620 plaintext_password = data_blob(pass, strlen(pass)+1);
623 p += passlen1 + passlen2;
624 p += srvstr_pull_buf(inbuf, user, p, sizeof(user), STR_TERMINATE);
625 p += srvstr_pull_buf(inbuf, domain, p, sizeof(domain), STR_TERMINATE);
626 p += srvstr_pull_buf(inbuf, native_os, p, sizeof(native_os), STR_TERMINATE);
627 p += srvstr_pull_buf(inbuf, native_lanman, p, sizeof(native_lanman), STR_TERMINATE);
628 DEBUG(3,("Domain=[%s] NativeOS=[%s] NativeLanMan=[%s]\n",
629 domain,native_os,native_lanman));
631 if ( ra_type == RA_WIN2K )
632 ra_lanman_string( native_lanman );
636 DEBUG(3,("sesssetupX:name=[%s]\\[%s]@[%s]\n", domain, user, get_remote_machine_name()));
639 if (global_spnego_negotiated) {
641 /* This has to be here, becouse this is a perfectly valid behaviour for guest logons :-( */
643 DEBUG(0,("reply_sesssetup_and_X: Rejecting attempt at 'normal' session setup after negotiating spnego.\n"));
644 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
646 fstrcpy(sub_user, user);
648 /* setup the string used by %U */
649 sub_set_smb_name(user);
651 fstrcpy(sub_user, lp_guestaccount());
654 sub_set_smb_name(sub_user);
656 reload_services(True);
658 if (lp_security() == SEC_SHARE) {
659 /* in share level we should ignore any passwords */
661 data_blob_free(&lm_resp);
662 data_blob_free(&nt_resp);
663 data_blob_clear_free(&plaintext_password);
665 map_username(sub_user);
666 add_session_user(sub_user);
667 /* Then force it to null for the benfit of the code below */
673 nt_status = check_guest_password(&server_info);
675 } else if (doencrypt) {
676 if (!negprot_global_auth_context) {
677 DEBUG(0, ("reply_sesssetup_and_X: Attempted encrypted session setup without negprot denied!\n"));
678 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
680 nt_status = make_user_info_for_reply_enc(&user_info, user, domain,
682 if (NT_STATUS_IS_OK(nt_status)) {
683 nt_status = negprot_global_auth_context->check_ntlm_password(negprot_global_auth_context,
688 struct auth_context *plaintext_auth_context = NULL;
690 if (NT_STATUS_IS_OK(nt_status = make_auth_context_subsystem(&plaintext_auth_context))) {
691 chal = plaintext_auth_context->get_ntlm_challenge(plaintext_auth_context);
693 if (!make_user_info_for_reply(&user_info,
695 plaintext_password)) {
696 nt_status = NT_STATUS_NO_MEMORY;
699 if (NT_STATUS_IS_OK(nt_status)) {
700 nt_status = plaintext_auth_context->check_ntlm_password(plaintext_auth_context,
704 (plaintext_auth_context->free)(&plaintext_auth_context);
709 free_user_info(&user_info);
711 data_blob_free(&lm_resp);
712 data_blob_free(&nt_resp);
713 data_blob_clear_free(&plaintext_password);
715 if (!NT_STATUS_IS_OK(nt_status)) {
716 nt_status = do_map_to_guest(nt_status, &server_info, user, domain);
719 if (!NT_STATUS_IS_OK(nt_status)) {
720 return ERROR_NT(nt_status_squash(nt_status));
723 /* it's ok - setup a reply */
724 set_message(outbuf,3,0,True);
725 if (Protocol >= PROTOCOL_NT1) {
726 add_signature(outbuf);
727 /* perhaps grab OS version here?? */
730 if (server_info->guest) {
731 SSVAL(outbuf,smb_vwv2,1);
734 /* register the name and uid as being validated, so further connections
735 to a uid can get through without a password, on the same VC */
737 /* register_vuid keeps the server info */
738 sess_vuid = register_vuid(server_info, sub_user);
740 if (sess_vuid == -1) {
741 return ERROR_NT(NT_STATUS_LOGON_FAILURE);
744 SSVAL(outbuf,smb_uid,sess_vuid);
745 SSVAL(inbuf,smb_uid,sess_vuid);
748 max_send = MIN(max_send,smb_bufsize);
750 done_sesssetup = True;
752 END_PROFILE(SMBsesssetupX);
753 return chain_reply(inbuf,outbuf,length,bufsize);