2 Unix SMB/CIFS implementation.
3 client connect/disconnect routines
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Andrew Barteltt 2001-2002
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 {PROTOCOL_CORE,"PC NETWORK PROGRAM 1.0"},
32 {PROTOCOL_COREPLUS,"MICROSOFT NETWORKS 1.03"},
33 {PROTOCOL_LANMAN1,"MICROSOFT NETWORKS 3.0"},
34 {PROTOCOL_LANMAN1,"LANMAN1.0"},
35 {PROTOCOL_LANMAN2,"LM1.2X002"},
36 {PROTOCOL_LANMAN2,"DOS LANMAN2.1"},
37 {PROTOCOL_LANMAN2,"Samba"},
38 {PROTOCOL_NT1,"NT LANMAN 1.0"},
39 {PROTOCOL_NT1,"NT LM 0.12"},
43 /****************************************************************************
44 Do an old lanman2 style session setup.
45 ****************************************************************************/
47 static BOOL cli_session_setup_lanman2(struct cli_state *cli, char *user,
48 char *pass, int passlen, const char *workgroup)
53 if (passlen > sizeof(pword)-1)
56 /* if in share level security then don't send a password now */
57 if (!(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL))
60 if (passlen > 0 && (cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) && passlen != 24) {
61 /* Encrypted mode needed, and non encrypted password supplied. */
63 SMBencrypt(pass,cli->secblob.data,(uchar *)pword);
64 } else if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) && passlen == 24) {
65 /* Encrypted mode needed, and encrypted password supplied. */
66 memcpy(pword, pass, passlen);
67 } else if (passlen > 0) {
68 /* Plaintext mode needed, assume plaintext supplied. */
69 passlen = clistr_push(cli, pword, pass, -1, STR_TERMINATE);
72 /* send a session setup command */
73 memset(cli->outbuf,'\0',smb_size);
74 set_message(cli->outbuf,10, 0, True);
75 SCVAL(cli->outbuf,smb_com,SMBsesssetupX);
76 cli_setup_packet(cli);
78 SCVAL(cli->outbuf,smb_vwv0,0xFF);
79 SSVAL(cli->outbuf,smb_vwv2,cli->max_xmit);
80 SSVAL(cli->outbuf,smb_vwv3,2);
81 SSVAL(cli->outbuf,smb_vwv4,1);
82 SIVAL(cli->outbuf,smb_vwv5,cli->sesskey);
83 SSVAL(cli->outbuf,smb_vwv7,passlen);
85 p = smb_buf(cli->outbuf);
86 memcpy(p,pword,passlen);
88 p += clistr_push(cli, p, user, -1, STR_TERMINATE|STR_UPPER);
89 p += clistr_push(cli, p, workgroup, -1, STR_TERMINATE|STR_UPPER);
90 p += clistr_push(cli, p, "Unix", -1, STR_TERMINATE);
91 p += clistr_push(cli, p, "Samba", -1, STR_TERMINATE);
92 cli_setup_bcc(cli, p);
95 if (!cli_receive_smb(cli))
100 if (cli_is_error(cli))
103 /* use the returned vuid from now on */
104 cli->vuid = SVAL(cli->inbuf,smb_uid);
105 fstrcpy(cli->user_name, user);
110 /****************************************************************************
111 Work out suitable capabilities to offer the server.
112 ****************************************************************************/
114 static uint32 cli_session_setup_capabilities(struct cli_state *cli)
116 uint32 capabilities = CAP_NT_SMBS;
118 if (!cli->force_dos_errors)
119 capabilities |= CAP_STATUS32;
121 if (cli->use_level_II_oplocks)
122 capabilities |= CAP_LEVEL_II_OPLOCKS;
124 if (cli->capabilities & CAP_UNICODE)
125 capabilities |= CAP_UNICODE;
130 /****************************************************************************
131 Do a NT1 guest session setup.
132 ****************************************************************************/
134 static BOOL cli_session_setup_guest(struct cli_state *cli)
137 uint32 capabilities = cli_session_setup_capabilities(cli);
139 set_message(cli->outbuf,13,0,True);
140 SCVAL(cli->outbuf,smb_com,SMBsesssetupX);
141 cli_setup_packet(cli);
143 SCVAL(cli->outbuf,smb_vwv0,0xFF);
144 SSVAL(cli->outbuf,smb_vwv2,CLI_BUFFER_SIZE);
145 SSVAL(cli->outbuf,smb_vwv3,2);
146 SSVAL(cli->outbuf,smb_vwv4,cli->pid);
147 SIVAL(cli->outbuf,smb_vwv5,cli->sesskey);
148 SSVAL(cli->outbuf,smb_vwv7,0);
149 SSVAL(cli->outbuf,smb_vwv8,0);
150 SIVAL(cli->outbuf,smb_vwv11,capabilities);
151 p = smb_buf(cli->outbuf);
152 p += clistr_push(cli, p, "", -1, STR_TERMINATE); /* username */
153 p += clistr_push(cli, p, "", -1, STR_TERMINATE); /* workgroup */
154 p += clistr_push(cli, p, "Unix", -1, STR_TERMINATE);
155 p += clistr_push(cli, p, "Samba", -1, STR_TERMINATE);
156 cli_setup_bcc(cli, p);
159 if (!cli_receive_smb(cli))
162 show_msg(cli->inbuf);
164 if (cli_is_error(cli))
167 cli->vuid = SVAL(cli->inbuf,smb_uid);
169 p = smb_buf(cli->inbuf);
170 p += clistr_pull(cli, cli->server_os, p, sizeof(fstring), -1, STR_TERMINATE);
171 p += clistr_pull(cli, cli->server_type, p, sizeof(fstring), -1, STR_TERMINATE);
172 p += clistr_pull(cli, cli->server_domain, p, sizeof(fstring), -1, STR_TERMINATE);
174 fstrcpy(cli->user_name, "");
179 /****************************************************************************
180 Do a NT1 plaintext session setup.
181 ****************************************************************************/
183 static BOOL cli_session_setup_plaintext(struct cli_state *cli, char *user,
184 char *pass, char *workgroup)
186 uint32 capabilities = cli_session_setup_capabilities(cli);
191 int push_flags = STR_TERMINATE;
193 if (capabilities & CAP_UNICODE)
194 push_flags |= STR_UNICODE;
196 push_flags |= STR_ASCII;
198 passlen = clistr_push(cli, pword, pass, sizeof(pword), push_flags);
200 set_message(cli->outbuf,13,0,True);
201 SCVAL(cli->outbuf,smb_com,SMBsesssetupX);
202 cli_setup_packet(cli);
204 SCVAL(cli->outbuf,smb_vwv0,0xFF);
205 SSVAL(cli->outbuf,smb_vwv2,CLI_BUFFER_SIZE);
206 SSVAL(cli->outbuf,smb_vwv3,2);
207 SSVAL(cli->outbuf,smb_vwv4,cli->pid);
208 SIVAL(cli->outbuf,smb_vwv5,cli->sesskey);
209 SSVAL(cli->outbuf,smb_vwv8,0);
210 SIVAL(cli->outbuf,smb_vwv11,capabilities);
211 p = smb_buf(cli->outbuf);
212 p += clistr_push(cli, p, pass, -1, push_flags); /* password */
213 SSVAL(cli->outbuf,smb_vwv7,PTR_DIFF(p, smb_buf(cli->outbuf)));
214 p += clistr_push(cli, p, user, -1, STR_TERMINATE); /* username */
215 p += clistr_push(cli, p, workgroup, -1, STR_TERMINATE); /* workgroup */
216 p += clistr_push(cli, p, "Unix", -1, STR_TERMINATE);
217 p += clistr_push(cli, p, "Samba", -1, STR_TERMINATE);
218 cli_setup_bcc(cli, p);
221 if (!cli_receive_smb(cli))
224 show_msg(cli->inbuf);
226 if (cli_is_error(cli))
229 cli->vuid = SVAL(cli->inbuf,smb_uid);
230 p = smb_buf(cli->inbuf);
231 p += clistr_pull(cli, cli->server_os, p, sizeof(fstring), -1, STR_TERMINATE);
232 p += clistr_pull(cli, cli->server_type, p, sizeof(fstring), -1, STR_TERMINATE);
233 p += clistr_pull(cli, cli->server_domain, p, sizeof(fstring), -1, STR_TERMINATE);
234 fstrcpy(cli->user_name, user);
239 static void set_signing_on_cli (struct cli_state *cli, char* pass, uint8 response[24])
242 ZERO_STRUCT(zero_sig);
244 DEBUG(5, ("Server returned security sig:\n"));
245 dump_data(5, &cli->inbuf[smb_ss_field], 8);
247 if (cli->sign_info.use_smb_signing) {
248 DEBUG(5, ("smb signing already active on connection\n"));
249 } else if (memcmp(&cli->inbuf[smb_ss_field], zero_sig, 8) != 0) {
251 DEBUG(3, ("smb signing enabled!\n"));
252 cli->sign_info.use_smb_signing = True;
253 cli_calculate_mac_key(cli, pass, response);
255 DEBUG(5, ("smb signing NOT enabled!\n"));
259 static void set_temp_signing_on_cli(struct cli_state *cli)
261 if (cli->sign_info.negotiated_smb_signing)
262 cli->sign_info.temp_smb_signing = True;
266 /****************************************************************************
267 do a NT1 NTLM/LM encrypted session setup
268 @param cli client state to create do session setup on
270 @param pass *either* cleartext password (passlen !=24) or LM response.
271 @param ntpass NT response, implies ntpasslen >=24, implies pass is not clear
272 @param workgroup The user's domain.
273 ****************************************************************************/
275 static BOOL cli_session_setup_nt1(struct cli_state *cli, char *user,
276 char *pass, int passlen,
277 char *ntpass, int ntpasslen,
280 uint32 capabilities = cli_session_setup_capabilities(cli);
284 BOOL have_plaintext = False;
286 if (passlen > sizeof(pword) || ntpasslen > sizeof(ntpword))
290 /* non encrypted password supplied. Ignore ntpass. */
293 SMBencrypt(pass,cli->secblob.data,pword);
294 SMBNTencrypt(pass,cli->secblob.data,ntpword);
296 have_plaintext = True;
297 set_temp_signing_on_cli(cli);
299 /* pre-encrypted password supplied. Only used for
300 security=server, can't do
301 signing becouse we don't have oringial key */
302 memcpy(pword, pass, 24);
304 memcpy(ntpword, ntpass, 24);
306 ZERO_STRUCT(ntpword);
309 /* send a session setup command */
310 memset(cli->outbuf,'\0',smb_size);
312 set_message(cli->outbuf,13,0,True);
313 SCVAL(cli->outbuf,smb_com,SMBsesssetupX);
314 cli_setup_packet(cli);
316 SCVAL(cli->outbuf,smb_vwv0,0xFF);
317 SSVAL(cli->outbuf,smb_vwv2,CLI_BUFFER_SIZE);
318 SSVAL(cli->outbuf,smb_vwv3,2);
319 SSVAL(cli->outbuf,smb_vwv4,cli->pid);
320 SIVAL(cli->outbuf,smb_vwv5,cli->sesskey);
321 SSVAL(cli->outbuf,smb_vwv7,passlen);
322 SSVAL(cli->outbuf,smb_vwv8,ntpasslen);
323 SIVAL(cli->outbuf,smb_vwv11,capabilities);
324 p = smb_buf(cli->outbuf);
325 memcpy(p,pword,passlen); p += passlen;
326 memcpy(p,ntpword,ntpasslen); p += ntpasslen;
327 p += clistr_push(cli, p, user, -1, STR_TERMINATE);
328 p += clistr_push(cli, p, workgroup, -1, STR_TERMINATE);
329 p += clistr_push(cli, p, "Unix", -1, STR_TERMINATE);
330 p += clistr_push(cli, p, "Samba", -1, STR_TERMINATE);
331 cli_setup_bcc(cli, p);
333 if (!cli_send_smb(cli))
336 if (!cli_receive_smb(cli))
339 show_msg(cli->inbuf);
341 if (cli_is_error(cli))
344 /* use the returned vuid from now on */
345 cli->vuid = SVAL(cli->inbuf,smb_uid);
347 p = smb_buf(cli->inbuf);
348 p += clistr_pull(cli, cli->server_os, p, sizeof(fstring), -1, STR_TERMINATE);
349 p += clistr_pull(cli, cli->server_type, p, sizeof(fstring), -1, STR_TERMINATE);
350 p += clistr_pull(cli, cli->server_domain, p, sizeof(fstring), -1, STR_TERMINATE);
352 fstrcpy(cli->user_name, user);
354 if (have_plaintext) {
355 /* Have plaintext orginal */
356 set_signing_on_cli(cli, pass, ntpword);
362 /****************************************************************************
363 Send a extended security session setup blob, returning a reply blob.
364 ****************************************************************************/
366 static DATA_BLOB cli_session_setup_blob(struct cli_state *cli, DATA_BLOB blob)
368 uint32 capabilities = cli_session_setup_capabilities(cli);
373 blob2 = data_blob(NULL, 0);
375 capabilities |= CAP_EXTENDED_SECURITY;
377 /* send a session setup command */
378 memset(cli->outbuf,'\0',smb_size);
380 set_message(cli->outbuf,12,0,True);
381 SCVAL(cli->outbuf,smb_com,SMBsesssetupX);
383 set_temp_signing_on_cli(cli);
385 cli_setup_packet(cli);
387 SCVAL(cli->outbuf,smb_vwv0,0xFF);
388 SSVAL(cli->outbuf,smb_vwv2,CLI_BUFFER_SIZE);
389 SSVAL(cli->outbuf,smb_vwv3,2);
390 SSVAL(cli->outbuf,smb_vwv4,1);
391 SIVAL(cli->outbuf,smb_vwv5,0);
392 SSVAL(cli->outbuf,smb_vwv7,blob.length);
393 SIVAL(cli->outbuf,smb_vwv10,capabilities);
394 p = smb_buf(cli->outbuf);
395 memcpy(p, blob.data, blob.length);
397 p += clistr_push(cli, p, "Unix", -1, STR_TERMINATE);
398 p += clistr_push(cli, p, "Samba", -1, STR_TERMINATE);
399 cli_setup_bcc(cli, p);
402 if (!cli_receive_smb(cli))
405 show_msg(cli->inbuf);
407 if (cli_is_error(cli) && !NT_STATUS_EQUAL(cli_nt_error(cli),
408 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
412 /* use the returned vuid from now on */
413 cli->vuid = SVAL(cli->inbuf,smb_uid);
415 p = smb_buf(cli->inbuf);
417 blob2 = data_blob(p, SVAL(cli->inbuf, smb_vwv3));
420 p += clistr_pull(cli, cli->server_os, p, sizeof(fstring), -1, STR_TERMINATE);
422 /* w2k with kerberos doesn't properly null terminate this field */
423 len = smb_buflen(cli->inbuf) - PTR_DIFF(p, smb_buf(cli->inbuf));
424 p += clistr_pull(cli, cli->server_type, p, sizeof(fstring), len, 0);
430 /****************************************************************************
431 Do a spnego/kerberos encrypted session setup.
432 ****************************************************************************/
434 static BOOL cli_session_setup_kerberos(struct cli_state *cli, char *principal, char *workgroup)
436 DATA_BLOB blob2, negTokenTarg;
438 DEBUG(2,("Doing kerberos session setup\n"));
440 /* generate the encapsulated kerberos5 ticket */
441 negTokenTarg = spnego_gen_negTokenTarg(principal, 0);
443 if (!negTokenTarg.data) return False;
446 file_save("negTokenTarg.dat", negTokenTarg.data, negTokenTarg.length);
449 blob2 = cli_session_setup_blob(cli, negTokenTarg);
451 /* we don't need this blob for kerberos */
452 data_blob_free(&blob2);
454 data_blob_free(&negTokenTarg);
456 return !cli_is_error(cli);
460 /****************************************************************************
461 Do a spnego/NTLMSSP encrypted session setup.
462 ****************************************************************************/
464 static BOOL cli_session_setup_ntlmssp(struct cli_state *cli, char *user,
465 char *pass, char *workgroup)
467 DATA_BLOB msg1, struct_blob;
468 DATA_BLOB blob, chal1, chal2, auth, challenge_blob;
470 uint8 nthash[24], lmhash[24], sess_key[16];
471 uint32 neg_flags, chal_flags, ntlmssp_command, unkn1, unkn2;
472 pstring server_domain; /* FIX THIS, SHOULD be UCS2-LE */
474 neg_flags = NTLMSSP_NEGOTIATE_UNICODE |
475 NTLMSSP_NEGOTIATE_128 |
476 NTLMSSP_NEGOTIATE_NTLM;
478 memset(sess_key, 0, 16);
480 DEBUG(10, ("sending NTLMSSP_NEGOTIATE\n"));
482 /* generate the ntlmssp negotiate packet */
483 msrpc_gen(&blob, "CddAA",
487 workgroup, strlen(workgroup),
488 cli->calling.name, strlen(cli->calling.name) + 1);
489 DEBUG(10, ("neg_flags: %0X, workgroup: %s, calling name %s\n",
490 neg_flags, workgroup, cli->calling.name));
491 /* and wrap it in a SPNEGO wrapper */
492 msg1 = gen_negTokenInit(OID_NTLMSSP, blob);
493 data_blob_free(&blob);
495 /* now send that blob on its way */
496 blob = cli_session_setup_blob(cli, msg1);
498 data_blob_free(&msg1);
500 if (!NT_STATUS_EQUAL(cli_nt_error(cli), NT_STATUS_MORE_PROCESSING_REQUIRED))
504 file_save("chal.dat", blob.data, blob.length);
507 /* the server gives us back two challenges */
508 if (!spnego_parse_challenge(blob, &chal1, &chal2)) {
509 DEBUG(3,("Failed to parse challenges\n"));
513 data_blob_free(&blob);
516 * Ok, chal1 and chal2 are actually two identical copies of
517 * the NTLMSSP Challenge BLOB, and they contain, encoded in them
518 * the challenge to use.
521 if (!msrpc_parse(&chal1, "CdUdbddB",
529 DEBUG(0, ("Failed to parse the NTLMSSP Challenge\n"));
533 if (ntlmssp_command != NTLMSSP_CHALLENGE) {
534 DEBUG(0, ("NTLMSSP Response != NTLMSSP_CHALLENGE. Got %0X\n",
539 DEBUG(10, ("Challenge:\n"));
540 dump_data(10, challenge_blob.data, 8);
542 /* encrypt the password with the challenge which is in the blob */
543 memcpy(challenge, challenge_blob.data, 8);
544 SMBencrypt(pass, challenge,lmhash);
545 SMBNTencrypt(pass, challenge,nthash);
546 data_blob_free(&challenge_blob);
549 file_save("nthash.dat", nthash, 24);
550 file_save("lmhash.dat", lmhash, 24);
551 file_save("chal1.dat", chal1.data, chal1.length);
554 data_blob_free(&chal1);
555 data_blob_free(&chal2);
557 /* this generates the actual auth packet */
558 msrpc_gen(&blob, "CdBBUUUBd",
569 /* wrap it in SPNEGO */
570 auth = spnego_gen_auth(blob);
572 data_blob_free(&blob);
574 /* now send the auth packet and we should be done */
575 blob = cli_session_setup_blob(cli, auth);
577 data_blob_free(&auth);
578 data_blob_free(&blob);
580 if (cli_is_error(cli))
583 set_signing_on_cli(cli, pass, nthash);
588 /****************************************************************************
589 Do a spnego encrypted session setup.
590 ****************************************************************************/
592 static BOOL cli_session_setup_spnego(struct cli_state *cli, char *user,
593 char *pass, char *workgroup)
596 char *OIDs[ASN1_MAX_OIDS];
598 BOOL got_kerberos_mechanism = False;
601 DEBUG(2,("Doing spnego session setup (blob length=%d)\n", cli->secblob.length));
603 /* the server might not even do spnego */
604 if (cli->secblob.length <= 16) {
605 DEBUG(3,("server didn't supply a full spnego negprot\n"));
610 file_save("negprot.dat", cli->secblob.data, cli->secblob.length);
613 /* there is 16 bytes of GUID before the real spnego packet starts */
614 blob = data_blob(cli->secblob.data+16, cli->secblob.length-16);
616 /* the server sent us the first part of the SPNEGO exchange in the negprot
618 if (!spnego_parse_negTokenInit(blob, OIDs, &principal)) {
619 data_blob_free(&blob);
622 data_blob_free(&blob);
624 /* make sure the server understands kerberos */
625 for (i=0;OIDs[i];i++) {
626 DEBUG(3,("got OID=%s\n", OIDs[i]));
627 if (strcmp(OIDs[i], OID_KERBEROS5_OLD) == 0 ||
628 strcmp(OIDs[i], OID_KERBEROS5) == 0) {
629 got_kerberos_mechanism = True;
633 DEBUG(3,("got principal=%s\n", principal));
635 fstrcpy(cli->user_name, user);
638 if (got_kerberos_mechanism && cli->use_kerberos) {
639 return cli_session_setup_kerberos(cli, principal, workgroup);
647 return cli_session_setup_ntlmssp(cli, user, pass, workgroup);
650 /****************************************************************************
651 Send a session setup. The username and workgroup is in UNIX character
652 format and must be converted to DOS codepage format before sending. If the
653 password is in plaintext, the same should be done.
654 ****************************************************************************/
656 BOOL cli_session_setup(struct cli_state *cli,
658 char *pass, int passlen,
659 char *ntpass, int ntpasslen,
665 /* allow for workgroups as part of the username */
666 fstrcpy(user2, user);
667 if ((p=strchr_m(user2,'\\')) || (p=strchr_m(user2,'/')) ||
668 (p=strchr_m(user2,*lp_winbind_separator()))) {
674 if (cli->protocol < PROTOCOL_LANMAN1)
677 /* now work out what sort of session setup we are going to
678 do. I have split this into separate functions to make the
679 flow a bit easier to understand (tridge) */
681 /* if its an older server then we have to use the older request format */
683 if (cli->protocol < PROTOCOL_NT1)
684 return cli_session_setup_lanman2(cli, user, pass, passlen, workgroup);
686 /* if no user is supplied then we have to do an anonymous connection.
687 passwords are ignored */
690 return cli_session_setup_guest(cli);
692 /* if the server is share level then send a plaintext null
693 password at this point. The password is sent in the tree
696 if ((cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) == 0)
697 return cli_session_setup_plaintext(cli, user, "", workgroup);
699 /* if the server doesn't support encryption then we have to use
700 plaintext. The second password is ignored */
702 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0)
703 return cli_session_setup_plaintext(cli, user, pass, workgroup);
705 /* Indidicate signing */
707 /* if the server supports extended security then use SPNEGO */
709 if (cli->capabilities & CAP_EXTENDED_SECURITY)
710 return cli_session_setup_spnego(cli, user, pass, workgroup);
712 /* otherwise do a NT1 style session setup */
714 return cli_session_setup_nt1(cli, user,
715 pass, passlen, ntpass, ntpasslen,
719 /****************************************************************************
721 *****************************************************************************/
723 BOOL cli_ulogoff(struct cli_state *cli)
725 memset(cli->outbuf,'\0',smb_size);
726 set_message(cli->outbuf,2,0,True);
727 SCVAL(cli->outbuf,smb_com,SMBulogoffX);
728 cli_setup_packet(cli);
729 SSVAL(cli->outbuf,smb_vwv0,0xFF);
730 SSVAL(cli->outbuf,smb_vwv2,0); /* no additional info */
733 if (!cli_receive_smb(cli))
736 return !cli_is_error(cli);
739 /****************************************************************************
741 ****************************************************************************/
743 BOOL cli_send_tconX(struct cli_state *cli,
744 const char *share, const char *dev, const char *pass, int passlen)
746 fstring fullshare, pword;
748 memset(cli->outbuf,'\0',smb_size);
749 memset(cli->inbuf,'\0',smb_size);
751 fstrcpy(cli->share, share);
753 /* in user level security don't send a password now */
754 if (cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL) {
759 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) && *pass && passlen != 24) {
761 * Non-encrypted passwords - convert to DOS codepage before encryption.
764 SMBencrypt(pass,cli->secblob.data,(uchar *)pword);
766 if((cli->sec_mode & (NEGOTIATE_SECURITY_USER_LEVEL|NEGOTIATE_SECURITY_CHALLENGE_RESPONSE)) == 0) {
768 * Non-encrypted passwords - convert to DOS codepage before using.
770 passlen = clistr_push(cli, pword, pass, -1, STR_TERMINATE);
772 memcpy(pword, pass, passlen);
776 if (cli->port == 445) {
777 slprintf(fullshare, sizeof(fullshare)-1,
780 slprintf(fullshare, sizeof(fullshare)-1,
781 "\\\\%s\\%s", cli->desthost, share);
784 set_message(cli->outbuf,4, 0, True);
785 SCVAL(cli->outbuf,smb_com,SMBtconX);
786 cli_setup_packet(cli);
788 SSVAL(cli->outbuf,smb_vwv0,0xFF);
789 SSVAL(cli->outbuf,smb_vwv3,passlen);
791 p = smb_buf(cli->outbuf);
792 memcpy(p,pword,passlen);
794 p += clistr_push(cli, p, fullshare, -1, STR_TERMINATE |STR_UPPER);
795 fstrcpy(p, dev); p += strlen(dev)+1;
797 cli_setup_bcc(cli, p);
800 if (!cli_receive_smb(cli))
803 if (cli_is_error(cli))
806 clistr_pull(cli, cli->dev, smb_buf(cli->inbuf), sizeof(fstring), -1, STR_TERMINATE|STR_ASCII);
808 if (strcasecmp(share,"IPC$")==0)
809 fstrcpy(cli->dev, "IPC");
811 if (cli->protocol >= PROTOCOL_NT1 &&
812 smb_buflen(cli->inbuf) == 3) {
813 /* almost certainly win95 - enable bug fixes */
817 cli->cnum = SVAL(cli->inbuf,smb_tid);
821 /****************************************************************************
822 Send a tree disconnect.
823 ****************************************************************************/
825 BOOL cli_tdis(struct cli_state *cli)
827 memset(cli->outbuf,'\0',smb_size);
828 set_message(cli->outbuf,0,0,True);
829 SCVAL(cli->outbuf,smb_com,SMBtdis);
830 SSVAL(cli->outbuf,smb_tid,cli->cnum);
831 cli_setup_packet(cli);
834 if (!cli_receive_smb(cli))
837 return !cli_is_error(cli);
840 /****************************************************************************
841 Send a negprot command.
842 ****************************************************************************/
844 void cli_negprot_send(struct cli_state *cli)
849 if (cli->protocol < PROTOCOL_NT1)
850 cli->use_spnego = False;
852 memset(cli->outbuf,'\0',smb_size);
854 /* setup the protocol strings */
855 set_message(cli->outbuf,0,0,True);
857 p = smb_buf(cli->outbuf);
859 prots[numprots].name && prots[numprots].prot<=cli->protocol;
862 p += clistr_push(cli, p, prots[numprots].name, -1, STR_TERMINATE);
865 SCVAL(cli->outbuf,smb_com,SMBnegprot);
866 cli_setup_bcc(cli, p);
867 cli_setup_packet(cli);
869 SCVAL(smb_buf(cli->outbuf),0,2);
874 /****************************************************************************
875 Send a negprot command.
876 ****************************************************************************/
878 BOOL cli_negprot(struct cli_state *cli)
884 if (cli->sign_info.use_smb_signing) {
885 DEBUG(0, ("Cannot send negprot again, particularly after setting up SMB Signing\n"));
889 if (cli->protocol < PROTOCOL_NT1)
890 cli->use_spnego = False;
892 memset(cli->outbuf,'\0',smb_size);
894 /* setup the protocol strings */
895 for (plength=0,numprots=0;
896 prots[numprots].name && prots[numprots].prot<=cli->protocol;
898 plength += strlen(prots[numprots].name)+2;
900 set_message(cli->outbuf,0,plength,True);
902 p = smb_buf(cli->outbuf);
904 prots[numprots].name && prots[numprots].prot<=cli->protocol;
907 p += clistr_push(cli, p, prots[numprots].name, -1, STR_TERMINATE);
910 SCVAL(cli->outbuf,smb_com,SMBnegprot);
911 cli_setup_packet(cli);
913 SCVAL(smb_buf(cli->outbuf),0,2);
916 if (!cli_receive_smb(cli))
919 show_msg(cli->inbuf);
921 if (cli_is_error(cli) ||
922 ((int)SVAL(cli->inbuf,smb_vwv0) >= numprots)) {
926 cli->protocol = prots[SVAL(cli->inbuf,smb_vwv0)].prot;
928 if (cli->protocol >= PROTOCOL_NT1) {
930 cli->sec_mode = CVAL(cli->inbuf,smb_vwv1);
931 cli->max_mux = SVAL(cli->inbuf, smb_vwv1+1);
932 cli->max_xmit = IVAL(cli->inbuf,smb_vwv3+1);
933 cli->sesskey = IVAL(cli->inbuf,smb_vwv7+1);
934 cli->serverzone = SVALS(cli->inbuf,smb_vwv15+1);
935 cli->serverzone *= 60;
936 /* this time arrives in real GMT */
937 cli->servertime = interpret_long_date(cli->inbuf+smb_vwv11+1);
938 cli->secblob = data_blob(smb_buf(cli->inbuf),smb_buflen(cli->inbuf));
939 cli->capabilities = IVAL(cli->inbuf,smb_vwv9+1);
940 if (cli->capabilities & CAP_RAW_MODE) {
941 cli->readbraw_supported = True;
942 cli->writebraw_supported = True;
944 /* work out if they sent us a workgroup */
945 if (!(cli->capabilities & CAP_EXTENDED_SECURITY) &&
946 smb_buflen(cli->inbuf) > 8) {
947 clistr_pull(cli, cli->server_domain,
948 smb_buf(cli->inbuf)+8, sizeof(cli->server_domain),
949 smb_buflen(cli->inbuf)-8, STR_UNICODE|STR_NOALIGN);
952 if ((cli->sec_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED))
953 cli->sign_info.negotiated_smb_signing = True;
955 } else if (cli->protocol >= PROTOCOL_LANMAN1) {
956 cli->use_spnego = False;
957 cli->sec_mode = SVAL(cli->inbuf,smb_vwv1);
958 cli->max_xmit = SVAL(cli->inbuf,smb_vwv2);
959 cli->sesskey = IVAL(cli->inbuf,smb_vwv6);
960 cli->serverzone = SVALS(cli->inbuf,smb_vwv10);
961 cli->serverzone *= 60;
962 /* this time is converted to GMT by make_unix_date */
963 cli->servertime = make_unix_date(cli->inbuf+smb_vwv8);
964 cli->readbraw_supported = ((SVAL(cli->inbuf,smb_vwv5) & 0x1) != 0);
965 cli->writebraw_supported = ((SVAL(cli->inbuf,smb_vwv5) & 0x2) != 0);
966 cli->secblob = data_blob(smb_buf(cli->inbuf),smb_buflen(cli->inbuf));
968 /* the old core protocol */
969 cli->use_spnego = False;
971 cli->serverzone = TimeDiff(time(NULL));
974 cli->max_xmit = MIN(cli->max_xmit, CLI_BUFFER_SIZE);
976 /* a way to force ascii SMB */
977 if (getenv("CLI_FORCE_ASCII"))
978 cli->capabilities &= ~CAP_UNICODE;
983 /****************************************************************************
984 Send a session request. See rfc1002.txt 4.3 and 4.3.2.
985 ****************************************************************************/
987 BOOL cli_session_request(struct cli_state *cli,
988 struct nmb_name *calling, struct nmb_name *called)
992 extern pstring user_socket_options;
994 memcpy(&(cli->calling), calling, sizeof(*calling));
995 memcpy(&(cli->called ), called , sizeof(*called ));
997 /* put in the destination name */
999 name_mangle(cli->called .name, p, cli->called .name_type);
1003 p = cli->outbuf+len;
1004 name_mangle(cli->calling.name, p, cli->calling.name_type);
1007 /* 445 doesn't have session request */
1008 if (cli->port == 445)
1011 if (cli->sign_info.use_smb_signing) {
1012 DEBUG(0, ("Cannot send session resquest again, particularly after setting up SMB Signing\n"));
1016 /* send a session request (RFC 1002) */
1017 /* setup the packet length
1018 * Remove four bytes from the length count, since the length
1019 * field in the NBT Session Service header counts the number
1020 * of bytes which follow. The cli_send_smb() function knows
1021 * about this and accounts for those four bytes.
1025 _smb_setlen(cli->outbuf,len);
1026 SCVAL(cli->outbuf,0,0x81);
1029 DEBUG(5,("Sent session request\n"));
1031 if (!cli_receive_smb(cli))
1034 if (CVAL(cli->inbuf,0) == 0x84) {
1035 /* C. Hoch 9/14/95 Start */
1036 /* For information, here is the response structure.
1037 * We do the byte-twiddling to for portability.
1038 struct RetargetResponse{
1040 unsigned char flags;
1046 int port = (CVAL(cli->inbuf,8)<<8)+CVAL(cli->inbuf,9);
1047 /* SESSION RETARGET */
1048 putip((char *)&cli->dest_ip,cli->inbuf+4);
1050 cli->fd = open_socket_out(SOCK_STREAM, &cli->dest_ip, port, LONG_CONNECT_TIMEOUT);
1054 DEBUG(3,("Retargeted\n"));
1056 set_socket_options(cli->fd,user_socket_options);
1063 DEBUG(0,("Retarget recursion - failing\n"));
1067 ret = cli_session_request(cli, calling, called);
1071 } /* C. Hoch 9/14/95 End */
1073 if (CVAL(cli->inbuf,0) != 0x82) {
1074 /* This is the wrong place to put the error... JRA. */
1075 cli->rap_error = CVAL(cli->inbuf,4);
1081 /****************************************************************************
1082 Open the client sockets.
1083 ****************************************************************************/
1085 BOOL cli_connect(struct cli_state *cli, const char *host, struct in_addr *ip)
1087 extern pstring user_socket_options;
1088 int name_type = 0x20;
1091 /* reasonable default hostname */
1092 if (!host) host = "*SMBSERVER";
1094 fstrcpy(cli->desthost, host);
1096 /* allow hostnames of the form NAME#xx and do a netbios lookup */
1097 if ((p = strchr(cli->desthost, '#'))) {
1098 name_type = strtol(p+1, NULL, 16);
1102 if (!ip || is_zero_ip(*ip)) {
1103 if (!resolve_name(cli->desthost, &cli->dest_ip, name_type)) {
1106 if (ip) *ip = cli->dest_ip;
1111 if (getenv("LIBSMB_PROG")) {
1112 cli->fd = sock_exec(getenv("LIBSMB_PROG"));
1114 /* try 445 first, then 139 */
1115 int port = cli->port?cli->port:445;
1116 cli->fd = open_socket_out(SOCK_STREAM, &cli->dest_ip,
1117 port, cli->timeout);
1118 if (cli->fd == -1 && cli->port == 0) {
1120 cli->fd = open_socket_out(SOCK_STREAM, &cli->dest_ip,
1121 port, cli->timeout);
1126 if (cli->fd == -1) {
1127 DEBUG(1,("Error connecting to %s (%s)\n",
1128 ip?inet_ntoa(*ip):host,strerror(errno)));
1132 set_socket_options(cli->fd,user_socket_options);
1137 /****************************************************************************
1138 Initialise client credentials for authenticated pipe access.
1139 ****************************************************************************/
1141 static void init_creds(struct ntuser_creds *creds, char* username,
1142 char* domain, char* password)
1144 ZERO_STRUCTP(creds);
1146 pwd_set_cleartext(&creds->pwd, password);
1148 fstrcpy(creds->user_name, username);
1149 fstrcpy(creds->domain, domain);
1152 creds->pwd.null_pwd = True;
1157 establishes a connection right up to doing tconX, password specified.
1158 @param output_cli A fully initialised cli structure, non-null only on success
1159 @param dest_host The netbios name of the remote host
1160 @param dest_ip (optional) The the destination IP, NULL for name based lookup
1161 @param port (optional) The destination port (0 for default)
1162 @param service (optional) The share to make the connection to. Should be 'unqualified' in any way.
1163 @param service_type The 'type' of serivice.
1164 @param user Username, unix string
1165 @param domain User's domain
1166 @param password User's password, unencrypted unix string.
1169 NTSTATUS cli_full_connection(struct cli_state **output_cli,
1170 const char *my_name,
1171 const char *dest_host,
1172 struct in_addr *dest_ip, int port,
1173 char *service, char *service_type,
1174 char *user, char *domain,
1175 char *password, int flags)
1177 struct ntuser_creds creds;
1179 struct nmb_name calling;
1180 struct nmb_name called;
1181 struct cli_state *cli;
1183 extern pstring global_myname;
1186 my_name = global_myname;
1188 if (!(cli = cli_initialise(NULL)))
1189 return NT_STATUS_NO_MEMORY;
1191 make_nmb_name(&calling, my_name, 0x0);
1192 make_nmb_name(&called , dest_host, 0x20);
1194 if (cli_set_port(cli, port) != port) {
1196 return NT_STATUS_UNSUCCESSFUL;
1206 DEBUG(3,("Connecting to host=%s share=%s\n", dest_host, service));
1208 if (!cli_connect(cli, dest_host, &ip)) {
1209 DEBUG(1,("cli_full_connection: failed to connect to %s (%s)\n",
1210 nmb_namestr(&called), inet_ntoa(ip)));
1212 return NT_STATUS_UNSUCCESSFUL;
1215 if (!cli_session_request(cli, &calling, &called)) {
1217 DEBUG(1,("session request to %s failed (%s)\n",
1218 called.name, cli_errstr(cli)));
1219 if ((p=strchr(called.name, '.')) && !is_ipaddress(called.name)) {
1223 if (strcmp(called.name, "*SMBSERVER")) {
1224 make_nmb_name(&called , "*SMBSERVER", 0x20);
1227 return NT_STATUS_UNSUCCESSFUL;
1230 if (flags & CLI_FULL_CONNECTION_DONT_SPNEGO)
1231 cli->use_spnego = False;
1232 else if (flags & CLI_FULL_CONNECTION_USE_KERBEROS)
1233 cli->use_kerberos = True;
1235 if (!cli_negprot(cli)) {
1236 DEBUG(1,("failed negprot\n"));
1237 nt_status = NT_STATUS_UNSUCCESSFUL;
1242 if (!cli_session_setup(cli, user, password, strlen(password)+1,
1243 password, strlen(password)+1,
1245 if ((flags & CLI_FULL_CONNECTION_ANNONYMOUS_FALLBACK)
1246 && cli_session_setup(cli, "", "", 0, "", 0, domain)) {
1248 nt_status = cli_nt_error(cli);
1249 DEBUG(1,("failed session setup with %s\n", nt_errstr(nt_status)));
1251 if (NT_STATUS_IS_OK(nt_status))
1252 nt_status = NT_STATUS_UNSUCCESSFUL;
1258 if (!cli_send_tconX(cli, service, service_type,
1259 (char*)password, strlen(password)+1)) {
1260 DEBUG(1,("failed tcon_X with %s\n", nt_errstr(nt_status)));
1261 nt_status = cli_nt_error(cli);
1263 if (NT_STATUS_IS_OK(nt_status)) {
1264 nt_status = NT_STATUS_UNSUCCESSFUL;
1270 init_creds(&creds, user, domain, password);
1271 cli_init_creds(cli, &creds);
1274 return NT_STATUS_OK;
1277 /****************************************************************************
1278 Attempt a NetBIOS session request, falling back to *SMBSERVER if needed.
1279 ****************************************************************************/
1281 BOOL attempt_netbios_session_request(struct cli_state *cli, char *srchost, char *desthost,
1282 struct in_addr *pdest_ip)
1284 struct nmb_name calling, called;
1286 make_nmb_name(&calling, srchost, 0x0);
1289 * If the called name is an IP address
1290 * then use *SMBSERVER immediately.
1293 if(is_ipaddress(desthost))
1294 make_nmb_name(&called, "*SMBSERVER", 0x20);
1296 make_nmb_name(&called, desthost, 0x20);
1298 if (!cli_session_request(cli, &calling, &called)) {
1299 struct nmb_name smbservername;
1301 make_nmb_name(&smbservername , "*SMBSERVER", 0x20);
1304 * If the name wasn't *SMBSERVER then
1305 * try with *SMBSERVER if the first name fails.
1308 if (nmb_name_equal(&called, &smbservername)) {
1311 * The name used was *SMBSERVER, don't bother with another name.
1314 DEBUG(0,("attempt_netbios_session_request: %s rejected the session for name *SMBSERVER \
1315 with error %s.\n", desthost, cli_errstr(cli) ));
1320 * We need to close the connection here but can't call cli_shutdown as
1321 * will free an allocated cli struct. cli_close_connection was invented
1322 * for this purpose. JRA. Based on work by "Kim R. Pedersen" <krp@filanet.dk>.
1325 cli_close_connection(cli);
1327 if (!cli_initialise(cli) ||
1328 !cli_connect(cli, desthost, pdest_ip) ||
1329 !cli_session_request(cli, &calling, &smbservername)) {
1330 DEBUG(0,("attempt_netbios_session_request: %s rejected the session for \
1331 name *SMBSERVER with error %s\n", desthost, cli_errstr(cli) ));