r26236: Remove more uses of global_loadparm or specify loadparm_context explicitly.
[ira/wip.git] / source4 / libcli / raw / rawnegotiate.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB client negotiate context management functions
5
6    Copyright (C) Andrew Tridgell 1994-2005
7    Copyright (C) James Myers 2003 <myersjj@samba.org>
8    
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 3 of the License, or
12    (at your option) any later version.
13    
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.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "system/time.h"
26 #include "param/param.h"
27
28 static const struct {
29         enum protocol_types prot;
30         const char *name;
31 } prots[] = {
32         {PROTOCOL_CORE,"PC NETWORK PROGRAM 1.0"},
33         {PROTOCOL_COREPLUS,"MICROSOFT NETWORKS 1.03"},
34         {PROTOCOL_LANMAN1,"MICROSOFT NETWORKS 3.0"},
35         {PROTOCOL_LANMAN1,"LANMAN1.0"},
36         {PROTOCOL_LANMAN1,"Windows for Workgroups 3.1a"},
37         {PROTOCOL_LANMAN2,"LM1.2X002"},
38         {PROTOCOL_LANMAN2,"DOS LANMAN2.1"},
39         {PROTOCOL_LANMAN2,"LANMAN2.1"},
40         {PROTOCOL_LANMAN2,"Samba"},
41         {PROTOCOL_NT1,"NT LANMAN 1.0"},
42         {PROTOCOL_NT1,"NT LM 0.12"},
43 };
44
45 /*
46   Send a negprot command.
47 */
48 struct smbcli_request *smb_raw_negotiate_send(struct smbcli_transport *transport, 
49                                               bool unicode,
50                                               int maxprotocol)
51 {
52         struct smbcli_request *req;
53         int i;
54         uint16_t flags2 = 0;
55
56         req = smbcli_request_setup_transport(transport, SMBnegprot, 0, 0);
57         if (!req) {
58                 return NULL;
59         }
60
61         flags2 |= FLAGS2_32_BIT_ERROR_CODES;
62         if (unicode) {
63                 flags2 |= FLAGS2_UNICODE_STRINGS;
64         }
65         flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
66         flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
67         flags2 |= FLAGS2_IS_LONG_NAME;
68
69         if (transport->options.use_spnego) {
70                 flags2 |= FLAGS2_EXTENDED_SECURITY;
71         }
72
73         SSVAL(req->out.hdr,HDR_FLG2, flags2);
74
75         /* setup the protocol strings */
76         for (i=0; i < ARRAY_SIZE(prots) && prots[i].prot <= maxprotocol; i++) {
77                 smbcli_req_append_bytes(req, (const uint8_t *)"\2", 1);
78                 smbcli_req_append_string(req, prots[i].name, STR_TERMINATE | STR_ASCII);
79         }
80
81         if (!smbcli_request_send(req)) {
82                 smbcli_request_destroy(req);
83                 return NULL;
84         }
85
86         return req;
87 }
88
89 /*
90  Send a negprot command.
91 */
92 NTSTATUS smb_raw_negotiate_recv(struct smbcli_request *req)
93 {
94         struct smbcli_transport *transport = req->transport;
95         int protocol;
96
97         if (!smbcli_request_receive(req) ||
98             smbcli_request_is_error(req)) {
99                 return smbcli_request_destroy(req);
100         }
101
102         SMBCLI_CHECK_MIN_WCT(req, 1);
103
104         protocol = SVALS(req->in.vwv, VWV(0));
105
106         if (protocol >= ARRAY_SIZE(prots) || protocol < 0) {
107                 req->status = NT_STATUS_UNSUCCESSFUL;
108                 return smbcli_request_destroy(req);
109         }
110
111         transport->negotiate.protocol = prots[protocol].prot;
112
113         if (transport->negotiate.protocol >= PROTOCOL_NT1) {
114                 NTTIME ntt;
115
116                 /* NT protocol */
117                 SMBCLI_CHECK_WCT(req, 17);
118                 transport->negotiate.sec_mode = CVAL(req->in.vwv,VWV(1));
119                 transport->negotiate.max_mux  = SVAL(req->in.vwv,VWV(1)+1);
120                 transport->negotiate.max_xmit = IVAL(req->in.vwv,VWV(3)+1);
121                 transport->negotiate.sesskey  = IVAL(req->in.vwv,VWV(7)+1);
122                 transport->negotiate.capabilities = IVAL(req->in.vwv,VWV(9)+1);
123
124                 /* this time arrives in real GMT */
125                 ntt = smbcli_pull_nttime(req->in.vwv, VWV(11)+1);
126                 transport->negotiate.server_time = nt_time_to_unix(ntt);                
127                 transport->negotiate.server_zone = SVALS(req->in.vwv,VWV(15)+1) * 60;
128                 transport->negotiate.key_len = CVAL(req->in.vwv,VWV(16)+1);
129
130                 if (transport->negotiate.capabilities & CAP_EXTENDED_SECURITY) {
131                         if (req->in.data_size < 16) {
132                                 goto failed;
133                         }
134                         transport->negotiate.server_guid = smbcli_req_pull_blob(req, transport, req->in.data, 16);
135                         transport->negotiate.secblob = smbcli_req_pull_blob(req, transport, req->in.data + 16, req->in.data_size - 16);
136                 } else {
137                         if (req->in.data_size < (transport->negotiate.key_len)) {
138                                 goto failed;
139                         }
140                         transport->negotiate.secblob = smbcli_req_pull_blob(req, transport, req->in.data, transport->negotiate.key_len);
141                         smbcli_req_pull_string(req, transport, &transport->negotiate.server_domain,
142                                             req->in.data+transport->negotiate.key_len,
143                                             req->in.data_size-transport->negotiate.key_len, STR_UNICODE|STR_NOALIGN);
144                         /* here comes the server name */
145                 }
146
147                 if (transport->negotiate.capabilities & CAP_RAW_MODE) {
148                         transport->negotiate.readbraw_supported = true;
149                         transport->negotiate.writebraw_supported = true;
150                 }
151         } else if (transport->negotiate.protocol >= PROTOCOL_LANMAN1) {
152                 SMBCLI_CHECK_WCT(req, 13);
153                 transport->negotiate.sec_mode = SVAL(req->in.vwv,VWV(1));
154                 transport->negotiate.max_xmit = SVAL(req->in.vwv,VWV(2));
155                 transport->negotiate.sesskey =  IVAL(req->in.vwv,VWV(6));
156                 transport->negotiate.server_zone = SVALS(req->in.vwv,VWV(10)) * 60;
157                 
158                 /* this time is converted to GMT by raw_pull_dos_date */
159                 transport->negotiate.server_time = raw_pull_dos_date(transport,
160                                                                      req->in.vwv+VWV(8));
161                 if ((SVAL(req->in.vwv,VWV(5)) & 0x1)) {
162                         transport->negotiate.readbraw_supported = 1;
163                 }
164                 if ((SVAL(req->in.vwv,VWV(5)) & 0x2)) {
165                         transport->negotiate.writebraw_supported = 1;
166                 }
167                 transport->negotiate.secblob = smbcli_req_pull_blob(req, transport, 
168                                                                  req->in.data, req->in.data_size);
169         } else {
170                 /* the old core protocol */
171                 transport->negotiate.sec_mode = 0;
172                 transport->negotiate.server_time = time(NULL);
173                 transport->negotiate.max_xmit = transport->options.max_xmit;
174                 transport->negotiate.server_zone = get_time_zone(transport->negotiate.server_time);
175         }
176
177         /* a way to force ascii SMB */
178         if (!lp_unicode(global_loadparm)) {
179                 transport->negotiate.capabilities &= ~CAP_UNICODE;
180         }
181
182         if (!lp_nt_status_support(global_loadparm)) {
183                 transport->negotiate.capabilities &= ~CAP_STATUS32;
184         }
185
186 failed:
187         return smbcli_request_destroy(req);
188 }
189
190
191 /*
192  Send a negprot command (sync interface)
193 */
194 NTSTATUS smb_raw_negotiate(struct smbcli_transport *transport, bool unicode, int maxprotocol)
195 {
196         struct smbcli_request *req = smb_raw_negotiate_send(transport, unicode, maxprotocol);
197         return smb_raw_negotiate_recv(req);
198 }