Install public header files again and include required prototypes.
[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 "libcli/raw/raw_proto.h"
26 #include "system/time.h"
27 #include "param/param.h"
28
29 static const struct {
30         enum protocol_types prot;
31         const char *name;
32 } prots[] = {
33         {PROTOCOL_CORE,"PC NETWORK PROGRAM 1.0"},
34         {PROTOCOL_COREPLUS,"MICROSOFT NETWORKS 1.03"},
35         {PROTOCOL_LANMAN1,"MICROSOFT NETWORKS 3.0"},
36         {PROTOCOL_LANMAN1,"LANMAN1.0"},
37         {PROTOCOL_LANMAN1,"Windows for Workgroups 3.1a"},
38         {PROTOCOL_LANMAN2,"LM1.2X002"},
39         {PROTOCOL_LANMAN2,"DOS LANMAN2.1"},
40         {PROTOCOL_LANMAN2,"LANMAN2.1"},
41         {PROTOCOL_LANMAN2,"Samba"},
42         {PROTOCOL_NT1,"NT LANMAN 1.0"},
43         {PROTOCOL_NT1,"NT LM 0.12"},
44 #if 0
45         /* we don't yet handle chaining a SMB transport onto SMB2 */
46         {PROTOCOL_SMB2,"SMB 2.002"},
47 #endif
48 };
49
50 /*
51   Send a negprot command.
52 */
53 struct smbcli_request *smb_raw_negotiate_send(struct smbcli_transport *transport, 
54                                               bool unicode,
55                                               int maxprotocol)
56 {
57         struct smbcli_request *req;
58         int i;
59         uint16_t flags2 = 0;
60
61         req = smbcli_request_setup_transport(transport, SMBnegprot, 0, 0);
62         if (!req) {
63                 return NULL;
64         }
65
66         flags2 |= FLAGS2_32_BIT_ERROR_CODES;
67         if (unicode) {
68                 flags2 |= FLAGS2_UNICODE_STRINGS;
69         }
70         flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
71         flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
72         flags2 |= FLAGS2_IS_LONG_NAME;
73
74         if (transport->options.use_spnego) {
75                 flags2 |= FLAGS2_EXTENDED_SECURITY;
76         }
77
78         SSVAL(req->out.hdr,HDR_FLG2, flags2);
79
80         /* setup the protocol strings */
81         for (i=0; i < ARRAY_SIZE(prots) && prots[i].prot <= maxprotocol; i++) {
82                 smbcli_req_append_bytes(req, (const uint8_t *)"\2", 1);
83                 smbcli_req_append_string(req, prots[i].name, STR_TERMINATE | STR_ASCII);
84         }
85
86         if (!smbcli_request_send(req)) {
87                 smbcli_request_destroy(req);
88                 return NULL;
89         }
90
91         return req;
92 }
93
94 /*
95  Send a negprot command.
96 */
97 NTSTATUS smb_raw_negotiate_recv(struct smbcli_request *req)
98 {
99         struct smbcli_transport *transport = req->transport;
100         int protocol;
101
102         if (!smbcli_request_receive(req) ||
103             smbcli_request_is_error(req)) {
104                 return smbcli_request_destroy(req);
105         }
106
107         SMBCLI_CHECK_MIN_WCT(req, 1);
108
109         protocol = SVALS(req->in.vwv, VWV(0));
110
111         if (protocol >= ARRAY_SIZE(prots) || protocol < 0) {
112                 req->status = NT_STATUS_UNSUCCESSFUL;
113                 return smbcli_request_destroy(req);
114         }
115
116         transport->negotiate.protocol = prots[protocol].prot;
117
118         if (transport->negotiate.protocol >= PROTOCOL_NT1) {
119                 NTTIME ntt;
120
121                 /* NT protocol */
122                 SMBCLI_CHECK_WCT(req, 17);
123                 transport->negotiate.sec_mode = CVAL(req->in.vwv,VWV(1));
124                 transport->negotiate.max_mux  = SVAL(req->in.vwv,VWV(1)+1);
125                 transport->negotiate.max_xmit = IVAL(req->in.vwv,VWV(3)+1);
126                 transport->negotiate.sesskey  = IVAL(req->in.vwv,VWV(7)+1);
127                 transport->negotiate.capabilities = IVAL(req->in.vwv,VWV(9)+1);
128
129                 /* this time arrives in real GMT */
130                 ntt = smbcli_pull_nttime(req->in.vwv, VWV(11)+1);
131                 transport->negotiate.server_time = nt_time_to_unix(ntt);                
132                 transport->negotiate.server_zone = SVALS(req->in.vwv,VWV(15)+1) * 60;
133                 transport->negotiate.key_len = CVAL(req->in.vwv,VWV(16)+1);
134
135                 if (transport->negotiate.capabilities & CAP_EXTENDED_SECURITY) {
136                         if (req->in.data_size < 16) {
137                                 goto failed;
138                         }
139                         transport->negotiate.server_guid = smbcli_req_pull_blob(&req->in.bufinfo, transport, req->in.data, 16);
140                         transport->negotiate.secblob = smbcli_req_pull_blob(&req->in.bufinfo, transport, req->in.data + 16, req->in.data_size - 16);
141                 } else {
142                         if (req->in.data_size < (transport->negotiate.key_len)) {
143                                 goto failed;
144                         }
145                         transport->negotiate.secblob = smbcli_req_pull_blob(&req->in.bufinfo, transport, req->in.data, transport->negotiate.key_len);
146                         smbcli_req_pull_string(&req->in.bufinfo, transport, &transport->negotiate.server_domain,
147                                             req->in.data+transport->negotiate.key_len,
148                                             req->in.data_size-transport->negotiate.key_len, STR_UNICODE|STR_NOALIGN);
149                         /* here comes the server name */
150                 }
151
152                 if (transport->negotiate.capabilities & CAP_RAW_MODE) {
153                         transport->negotiate.readbraw_supported = true;
154                         transport->negotiate.writebraw_supported = true;
155                 }
156         } else if (transport->negotiate.protocol >= PROTOCOL_LANMAN1) {
157                 SMBCLI_CHECK_WCT(req, 13);
158                 transport->negotiate.sec_mode = SVAL(req->in.vwv,VWV(1));
159                 transport->negotiate.max_xmit = SVAL(req->in.vwv,VWV(2));
160                 transport->negotiate.sesskey =  IVAL(req->in.vwv,VWV(6));
161                 transport->negotiate.server_zone = SVALS(req->in.vwv,VWV(10)) * 60;
162                 
163                 /* this time is converted to GMT by raw_pull_dos_date */
164                 transport->negotiate.server_time = raw_pull_dos_date(transport,
165                                                                      req->in.vwv+VWV(8));
166                 if ((SVAL(req->in.vwv,VWV(5)) & 0x1)) {
167                         transport->negotiate.readbraw_supported = 1;
168                 }
169                 if ((SVAL(req->in.vwv,VWV(5)) & 0x2)) {
170                         transport->negotiate.writebraw_supported = 1;
171                 }
172                 transport->negotiate.secblob = smbcli_req_pull_blob(&req->in.bufinfo, transport, 
173                                                                  req->in.data, req->in.data_size);
174         } else {
175                 /* the old core protocol */
176                 transport->negotiate.sec_mode = 0;
177                 transport->negotiate.server_time = time(NULL);
178                 transport->negotiate.max_xmit = transport->options.max_xmit;
179                 transport->negotiate.server_zone = get_time_zone(transport->negotiate.server_time);
180         }
181
182         /* a way to force ascii SMB */
183         if (!transport->options.unicode) {
184                 transport->negotiate.capabilities &= ~CAP_UNICODE;
185         }
186
187         if (!transport->options.ntstatus_support) {
188                 transport->negotiate.capabilities &= ~CAP_STATUS32;
189         }
190
191         if (!transport->options.use_level2_oplocks) {
192                 transport->negotiate.capabilities &= ~CAP_LEVEL_II_OPLOCKS;
193         }
194
195 failed:
196         return smbcli_request_destroy(req);
197 }
198
199
200 /*
201  Send a negprot command (sync interface)
202 */
203 NTSTATUS smb_raw_negotiate(struct smbcli_transport *transport, bool unicode, int maxprotocol)
204 {
205         struct smbcli_request *req = smb_raw_negotiate_send(transport, unicode, maxprotocol);
206         return smb_raw_negotiate_recv(req);
207 }