Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2002 Mike McCormack |
| 3 | * |
| 4 | * CIFS implementation for WINE |
| 5 | * |
| 6 | * This is a WINE's implementation of the Common Internet File System |
| 7 | * |
| 8 | * for specification see: |
| 9 | * |
| 10 | * http://www.codefx.com/CIFS_Explained.htm |
| 11 | * http://www.ubiqx.org/cifs/rfc-draft/rfc1002.html |
| 12 | * http://www.ubiqx.org/cifs/rfc-draft/draft-leach-cifs-v1-spec-02.html |
| 13 | * http://ubiqx.org/cifs/ |
| 14 | * http://www.samba.org |
| 15 | * |
| 16 | * This library is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU Lesser General Public |
| 18 | * License as published by the Free Software Foundation; either |
| 19 | * version 2.1 of the License, or (at your option) any later version. |
| 20 | * |
| 21 | * This library is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | * Lesser General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU Lesser General Public |
| 27 | * License along with this library; if not, write to the Free Software |
| 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 29 | * |
| 30 | * |
| 31 | * FIXME: |
| 32 | * |
| 33 | * - There is a race condition when two threads try to read from the same |
| 34 | * SMB handle. Either we need to lock the SMB handle for the time we |
| 35 | * use it in the client, or do all reading and writing to the socket |
| 36 | * fd in the server. |
| 37 | * |
| 38 | * - Each new handle opens up a new connection to the SMB server. This |
| 39 | * is not ideal, since operations can be multiplexed on one socket. For |
| 40 | * this to work properly we would need to have some way of discovering |
| 41 | * connections that are already open. |
| 42 | * |
| 43 | * - All access is currently anonymous. Password protected shares cannot |
| 44 | * be accessed. We need some way of organising passwords, storing them |
| 45 | * in the config file, or putting up a dialog box for the user. |
| 46 | * |
| 47 | * - We don't deal with SMB dialects at all. |
| 48 | * |
| 49 | * - SMB supports passing unicode over the wire, should use this if possible. |
| 50 | * |
| 51 | * - Implement ability to read named pipes over the network. Would require |
| 52 | * integrate this code with the named pipes code in the server, and |
| 53 | * possibly implementing some support for security tokens. |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 54 | */ |
| 55 | |
| 56 | #include "config.h" |
| 57 | #include "wine/port.h" |
| 58 | |
| 59 | #include <assert.h> |
| 60 | #include <ctype.h> |
| 61 | #include <errno.h> |
| 62 | #include <fcntl.h> |
| 63 | #include <stdlib.h> |
| 64 | #include <stdio.h> |
| 65 | #include <string.h> |
| 66 | #ifdef HAVE_SYS_ERRNO_H |
| 67 | #include <sys/errno.h> |
| 68 | #endif |
| 69 | #include <sys/types.h> |
| 70 | #include <sys/stat.h> |
| 71 | #ifdef HAVE_SYS_MMAN_H |
| 72 | #include <sys/mman.h> |
| 73 | #endif |
Patrik Stridvall | d016f81 | 2002-08-17 00:43:16 +0000 | [diff] [blame^] | 74 | #ifdef HAVE_SYS_TIME_H |
| 75 | # include <sys/time.h> |
| 76 | #endif |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 77 | #include <sys/poll.h> |
| 78 | #include <time.h> |
Patrik Stridvall | d016f81 | 2002-08-17 00:43:16 +0000 | [diff] [blame^] | 79 | #ifdef HAVE_UNISTD_H |
| 80 | # include <unistd.h> |
| 81 | #endif |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 82 | #include <utime.h> |
Patrik Stridvall | f89d4a8 | 2002-03-23 21:39:05 +0000 | [diff] [blame] | 83 | #ifdef HAVE_SYS_SOCKET_H |
| 84 | # include <sys/socket.h> |
| 85 | #endif |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 86 | #include <sys/types.h> |
Gerald Pfeifer | c8764e4 | 2002-03-19 02:06:25 +0000 | [diff] [blame] | 87 | #ifdef HAVE_NETINET_IN_SYSTM_H |
| 88 | #include <netinet/in_systm.h> |
| 89 | #endif |
| 90 | #ifdef HAVE_NETINET_IN_H |
| 91 | #include <netinet/in.h> |
| 92 | #endif |
| 93 | #ifdef HAVE_NETINET_IP_H |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 94 | #include <netinet/ip.h> |
Gerald Pfeifer | c8764e4 | 2002-03-19 02:06:25 +0000 | [diff] [blame] | 95 | #endif |
| 96 | #ifdef HAVE_ARPA_INET_H |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 97 | #include <arpa/inet.h> |
Gerald Pfeifer | c8764e4 | 2002-03-19 02:06:25 +0000 | [diff] [blame] | 98 | #endif |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 99 | #ifdef HAVE_NETDB_H |
| 100 | #include <netdb.h> |
| 101 | #endif |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 102 | |
| 103 | #include "winerror.h" |
| 104 | #include "windef.h" |
| 105 | #include "winbase.h" |
| 106 | #include "file.h" |
| 107 | #include "heap.h" |
| 108 | |
| 109 | #include "smb.h" |
| 110 | |
| 111 | #include "wine/server.h" |
| 112 | #include "wine/debug.h" |
| 113 | |
| 114 | WINE_DEFAULT_DEBUG_CHANNEL(file); |
| 115 | |
| 116 | #define MAX_HOST_NAME 15 |
| 117 | #define NB_TIMEOUT 10000 |
| 118 | |
| 119 | USHORT SMB_MultiplexId = 0; |
| 120 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 121 | struct NB_Buffer |
| 122 | { |
| 123 | unsigned char *buffer; |
| 124 | int len; |
| 125 | }; |
| 126 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 127 | static int netbios_name(const char *p, unsigned char *buffer) |
| 128 | { |
| 129 | char ch; |
| 130 | int i,len=0; |
| 131 | |
| 132 | buffer[len++]=' '; |
| 133 | for(i=0; i<=MAX_HOST_NAME; i++) |
| 134 | { |
| 135 | if(i<MAX_HOST_NAME) |
| 136 | { |
| 137 | if(*p) |
| 138 | ch = *p++&0xdf; /* add character from hostname */ |
| 139 | else |
| 140 | ch = ' '; /* add padding */ |
| 141 | } |
| 142 | else |
| 143 | ch = 0; /* add terminator */ |
| 144 | buffer[len++] = ((ch&0xf0) >> 4) + 'A'; |
| 145 | buffer[len++] = (ch&0x0f) + 'A'; |
| 146 | } |
| 147 | buffer[len++] = 0; /* add second terminator */ |
| 148 | return len; |
| 149 | } |
| 150 | |
| 151 | static DWORD NB_NameReq(LPCSTR host, unsigned char *buffer, int len) |
| 152 | { |
| 153 | int trn = 1234,i=0; |
| 154 | |
| 155 | NBR_ADDWORD(&buffer[i],trn); i+=2; |
| 156 | NBR_ADDWORD(&buffer[i],0x0110); i+=2; |
| 157 | NBR_ADDWORD(&buffer[i],0x0001); i+=2; |
| 158 | NBR_ADDWORD(&buffer[i],0x0000); i+=2; |
| 159 | NBR_ADDWORD(&buffer[i],0x0000); i+=2; |
| 160 | NBR_ADDWORD(&buffer[i],0x0000); i+=2; |
| 161 | |
| 162 | i += netbios_name(host,&buffer[i]); |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 163 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 164 | NBR_ADDWORD(&buffer[i],0x0020); i+=2; |
| 165 | NBR_ADDWORD(&buffer[i],0x0001); i+=2; |
| 166 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 167 | TRACE("packet is %d bytes in length\n",i); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 168 | |
| 169 | { |
| 170 | int j; |
| 171 | for(j=0; j<i; j++) |
| 172 | printf("%02x%c",buffer[j],(((j+1)%16)&&((j+1)!=j))?' ':'\n'); |
| 173 | } |
| 174 | |
| 175 | return i; |
| 176 | } |
| 177 | |
| 178 | /* unc = \\hostname\share\file... */ |
| 179 | static BOOL UNC_SplitName(LPSTR unc, LPSTR *hostname, LPSTR *share, LPSTR *file) |
| 180 | { |
| 181 | char *p; |
| 182 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 183 | TRACE("%s\n",unc); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 184 | |
| 185 | p = strchr(unc,'\\'); |
| 186 | if(!p) |
| 187 | return FALSE; |
| 188 | p = strchr(p+1,'\\'); |
| 189 | if(!p) |
| 190 | return FALSE; |
| 191 | *hostname=++p; |
| 192 | |
| 193 | p = strchr(p,'\\'); |
| 194 | if(!p) |
| 195 | return FALSE; |
| 196 | *p=0; |
| 197 | *share = ++p; |
| 198 | |
| 199 | p = strchr(p,'\\'); |
| 200 | if(!p) |
| 201 | return FALSE; |
| 202 | *p=0; |
| 203 | *file = ++p; |
| 204 | |
| 205 | return TRUE; |
| 206 | } |
| 207 | |
| 208 | static BOOL NB_Lookup(LPCSTR host, struct sockaddr_in *addr) |
| 209 | { |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 210 | int fd,on=1,r,len,i,fromsize; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 211 | struct pollfd fds; |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 212 | struct sockaddr_in sin,fromaddr; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 213 | unsigned char buffer[256]; |
| 214 | |
| 215 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 216 | if(fd<0) |
| 217 | return FALSE; |
| 218 | |
| 219 | r = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof on); |
| 220 | if(r<0) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 221 | goto err; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 222 | |
| 223 | if(0==inet_aton("255.255.255.255", (struct in_addr *)&sin.sin_addr.s_addr)) |
| 224 | { |
| 225 | FIXME("Error getting bcast address\n"); |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 226 | goto err; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 227 | } |
| 228 | sin.sin_family = AF_INET; |
| 229 | sin.sin_port = htons(137); |
| 230 | |
| 231 | len = NB_NameReq(host,buffer,sizeof buffer); |
| 232 | if(len<=0) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 233 | goto err; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 234 | |
| 235 | r = sendto(fd, buffer, len, 0, &sin, sizeof sin); |
| 236 | if(r<0) |
| 237 | { |
| 238 | FIXME("Error sending packet\n"); |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 239 | goto err; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | fds.fd = fd; |
| 243 | fds.events = POLLIN; |
| 244 | fds.revents = 0; |
| 245 | |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 246 | /* FIXME: this is simple and easily fooled logic |
| 247 | * we should loop until we receive the correct packet or timeout |
| 248 | */ |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 249 | r = poll(&fds,1,NB_TIMEOUT); |
| 250 | if(r!=1) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 251 | goto err; |
| 252 | |
| 253 | TRACE("Got response!\n"); |
| 254 | |
| 255 | fromsize = sizeof (fromaddr); |
| 256 | r = recvfrom(fd, buffer, sizeof buffer, 0, &fromaddr, &fromsize); |
| 257 | if(r<0) |
| 258 | goto err; |
| 259 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 260 | TRACE("%d bytes received\n",r); |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 261 | |
| 262 | if(r!=62) |
| 263 | goto err; |
| 264 | |
| 265 | for(i=0; i<r; i++) |
| 266 | DPRINTF("%02X%c",buffer[i],(((i+1)!=r)&&((i+1)%16))?' ':'\n'); |
| 267 | DPRINTF("\n"); |
| 268 | |
| 269 | if(0x0f & buffer[3]) |
| 270 | goto err; |
| 271 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 272 | TRACE("packet is OK\n"); |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 273 | |
| 274 | memcpy(&addr->sin_addr, &buffer[58], sizeof addr->sin_addr); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 275 | |
| 276 | close(fd); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 277 | return TRUE; |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 278 | |
| 279 | err: |
| 280 | close(fd); |
| 281 | return FALSE; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | #define NB_FIRST 0x40 |
| 285 | |
| 286 | #define NB_HDRSIZE 4 |
| 287 | |
| 288 | #define NB_SESSION_MSG 0x00 |
| 289 | #define NB_SESSION_REQ 0x81 |
| 290 | |
| 291 | /* RFC 1002, section 4.3.2 */ |
| 292 | static BOOL NB_SessionReq(int fd, char *called, char *calling) |
| 293 | { |
| 294 | unsigned char buffer[0x100]; |
| 295 | int len = 0,r; |
| 296 | struct pollfd fds; |
| 297 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 298 | TRACE("called %s, calling %s\n",called,calling); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 299 | |
| 300 | buffer[0] = NB_SESSION_REQ; |
| 301 | buffer[1] = NB_FIRST; |
| 302 | |
| 303 | netbios_name(called, &buffer[NB_HDRSIZE]); |
| 304 | len += 34; |
| 305 | netbios_name(calling, &buffer[NB_HDRSIZE+len]); |
| 306 | len += 34; |
| 307 | |
| 308 | NBR_ADDWORD(&buffer[2],len); |
| 309 | |
| 310 | /* for(i=0; i<(len+NB_HDRSIZE); i++) |
| 311 | DPRINTF("%02X%c",buffer[i],(((i+1)!=(len+4))&&((i+1)%16))?' ':'\n'); */ |
| 312 | |
| 313 | r = write(fd,buffer,len+4); |
| 314 | if(r<0) |
| 315 | { |
| 316 | ERR("Write failed\n"); |
| 317 | return FALSE; |
| 318 | } |
| 319 | |
| 320 | fds.fd = fd; |
| 321 | fds.events = POLLIN; |
| 322 | fds.revents = 0; |
| 323 | |
| 324 | r = poll(&fds,1,NB_TIMEOUT); |
| 325 | if(r!=1) |
| 326 | { |
| 327 | ERR("Poll failed\n"); |
| 328 | return FALSE; |
| 329 | } |
| 330 | |
| 331 | r = read(fd, buffer, NB_HDRSIZE); |
| 332 | if((r!=NB_HDRSIZE) || (buffer[0]!=0x82)) |
| 333 | { |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 334 | TRACE("Received %d bytes\n",r); |
| 335 | TRACE("%02x %02x %02x %02x\n", buffer[0],buffer[1],buffer[2],buffer[3]); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 336 | return FALSE; |
| 337 | } |
| 338 | |
| 339 | return TRUE; |
| 340 | } |
| 341 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 342 | static BOOL NB_SendData(int fd, struct NB_Buffer *out) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 343 | { |
| 344 | unsigned char buffer[NB_HDRSIZE]; |
| 345 | int r; |
| 346 | |
| 347 | /* CHECK: is it always OK to do this in two writes? */ |
| 348 | /* perhaps use scatter gather sendmsg instead? */ |
| 349 | |
| 350 | buffer[0] = NB_SESSION_MSG; |
| 351 | buffer[1] = NB_FIRST; |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 352 | NBR_ADDWORD(&buffer[2],out->len); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 353 | |
| 354 | r = write(fd, buffer, NB_HDRSIZE); |
| 355 | if(r!=NB_HDRSIZE) |
| 356 | return FALSE; |
| 357 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 358 | r = write(fd, out->buffer, out->len); |
| 359 | if(r!=out->len) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 360 | { |
| 361 | ERR("write failed\n"); |
| 362 | return FALSE; |
| 363 | } |
| 364 | |
| 365 | return TRUE; |
| 366 | } |
| 367 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 368 | static BOOL NB_RecvData(int fd, struct NB_Buffer *rx) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 369 | { |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 370 | int r; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 371 | unsigned char buffer[NB_HDRSIZE]; |
| 372 | |
| 373 | r = read(fd, buffer, NB_HDRSIZE); |
| 374 | if((r!=NB_HDRSIZE) || (buffer[0]!=NB_SESSION_MSG)) |
| 375 | { |
| 376 | ERR("Received %d bytes\n",r); |
| 377 | return FALSE; |
| 378 | } |
| 379 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 380 | rx->len = NBR_GETWORD(&buffer[2]); |
| 381 | |
| 382 | rx->buffer = HeapAlloc(GetProcessHeap(), 0, rx->len); |
| 383 | if(!rx->buffer) |
| 384 | return FALSE; |
| 385 | |
| 386 | r = read(fd, rx->buffer, rx->len); |
| 387 | if(rx->len!=r) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 388 | { |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 389 | TRACE("Received %d bytes\n",r); |
| 390 | HeapFree(GetProcessHeap(), 0, rx->buffer); |
| 391 | rx->buffer = 0; |
| 392 | rx->len = 0; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 393 | return FALSE; |
| 394 | } |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 395 | |
| 396 | return TRUE; |
| 397 | } |
| 398 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 399 | static BOOL NB_Transaction(int fd, struct NB_Buffer *in, struct NB_Buffer *out) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 400 | { |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 401 | int r; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 402 | struct pollfd fds; |
| 403 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 404 | if(TRACE_ON(file)) |
| 405 | { |
| 406 | int i; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 407 | DPRINTF("Sending request:\n"); |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 408 | for(i=0; i<in->len; i++) |
| 409 | DPRINTF("%02X%c",in->buffer[i],(((i+1)!=in->len)&&((i+1)%16))?' ':'\n'); |
| 410 | } |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 411 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 412 | if(!NB_SendData(fd,in)) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 413 | return FALSE; |
| 414 | |
| 415 | fds.fd = fd; |
| 416 | fds.events = POLLIN; |
| 417 | fds.revents = 0; |
| 418 | |
| 419 | r = poll(&fds,1,NB_TIMEOUT); |
| 420 | if(r!=1) |
| 421 | { |
| 422 | ERR("Poll failed\n"); |
| 423 | return FALSE; |
| 424 | } |
| 425 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 426 | if(!NB_RecvData(fd, out)) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 427 | return FALSE; |
| 428 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 429 | if(TRACE_ON(file)) |
| 430 | { |
| 431 | int i; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 432 | DPRINTF("Got response:\n"); |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 433 | for(i=0; i<out->len; i++) |
| 434 | DPRINTF("%02X%c",out->buffer[i],(((i+1)!=out->len)&&((i+1)%16))?' ':'\n'); |
| 435 | } |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 436 | |
| 437 | return TRUE; |
| 438 | } |
| 439 | |
| 440 | #define SMB_ADDHEADER(b,l) { b[(l)++]=0xff; b[(l)++]='S'; b[(l)++]='M'; b[(l)++]='B'; } |
| 441 | #define SMB_ADDERRINFO(b,l) { b[(l)++]=0; b[(l)++]=0; b[(l)++]=0; b[(l)++]=0; } |
| 442 | #define SMB_ADDPADSIG(b,l) { memset(&b[l],0,12); l+=12; } |
| 443 | |
| 444 | #define SMB_ERRCLASS 5 |
| 445 | #define SMB_ERRCODE 7 |
| 446 | #define SMB_TREEID 24 |
| 447 | #define SMB_PROCID 26 |
| 448 | #define SMB_USERID 28 |
| 449 | #define SMB_PLEXID 30 |
| 450 | #define SMB_PCOUNT 32 |
| 451 | #define SMB_HDRSIZE 33 |
| 452 | |
| 453 | static DWORD SMB_GetError(unsigned char *buffer) |
| 454 | { |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 455 | char *err_class; |
| 456 | |
| 457 | switch(buffer[SMB_ERRCLASS]) |
| 458 | { |
| 459 | case 0: |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 460 | return STATUS_SUCCESS; |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 461 | case 1: |
| 462 | err_class = "DOS"; |
| 463 | break; |
| 464 | case 2: |
| 465 | err_class = "net server"; |
| 466 | break; |
| 467 | case 3: |
| 468 | err_class = "hardware"; |
| 469 | break; |
| 470 | case 0xff: |
| 471 | err_class = "smb"; |
| 472 | break; |
| 473 | default: |
| 474 | err_class = "unknown"; |
| 475 | break; |
| 476 | } |
| 477 | |
| 478 | ERR("%s error %d \n",err_class, buffer[SMB_ERRCODE]); |
| 479 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 480 | /* FIXME: return propper error codes */ |
| 481 | return STATUS_INVALID_PARAMETER; |
| 482 | } |
| 483 | |
| 484 | static int SMB_Header(unsigned char *buffer, unsigned char command, USHORT tree_id, USHORT user_id) |
| 485 | { |
| 486 | int len = 0; |
| 487 | DWORD id; |
| 488 | |
| 489 | /* 0 */ |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 490 | SMB_ADDHEADER(buffer,len); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 491 | |
| 492 | /* 4 */ |
| 493 | buffer[len++] = command; |
| 494 | |
| 495 | /* 5 */ |
| 496 | SMB_ADDERRINFO(buffer,len) |
| 497 | |
| 498 | /* 9 */ |
| 499 | buffer[len++] = 0x00; /* flags */ |
| 500 | SMB_ADDWORD(&buffer[len],1); len += 2; /* flags2 */ |
| 501 | |
| 502 | /* 12 */ |
| 503 | SMB_ADDPADSIG(buffer,len) |
| 504 | |
| 505 | /* 24 */ |
| 506 | SMB_ADDWORD(&buffer[len],tree_id); len += 2; /* treeid */ |
| 507 | id = GetCurrentThreadId(); |
| 508 | SMB_ADDWORD(&buffer[len],id); len += 2; /* process id */ |
| 509 | SMB_ADDWORD(&buffer[len],user_id); len += 2; /* user id */ |
| 510 | SMB_ADDWORD(&buffer[len],SMB_MultiplexId); len += 2; /* multiplex id */ |
| 511 | SMB_MultiplexId++; |
| 512 | |
| 513 | return len; |
| 514 | } |
| 515 | |
| 516 | static const char *SMB_ProtocolDialect = "NT LM 0.12"; |
| 517 | /* = "Windows for Workgroups 3.1a"; */ |
| 518 | |
| 519 | /* FIXME: support multiple SMB dialects */ |
| 520 | static BOOL SMB_NegotiateProtocol(int fd, USHORT *dialect) |
| 521 | { |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 522 | unsigned char buf[0x100]; |
| 523 | int buflen = 0; |
| 524 | struct NB_Buffer tx, rx; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 525 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 526 | TRACE("\n"); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 527 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 528 | memset(buf,0,sizeof buf); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 529 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 530 | tx.buffer = buf; |
| 531 | tx.len = SMB_Header(tx.buffer, SMB_COM_NEGOTIATE, 0, 0); |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 532 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 533 | /* parameters */ |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 534 | tx.buffer[tx.len++] = 0; /* no parameters */ |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 535 | |
| 536 | /* command buffer */ |
| 537 | buflen = strlen(SMB_ProtocolDialect)+2; /* include type and nul byte */ |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 538 | SMB_ADDWORD(&tx.buffer[tx.len],buflen); tx.len += 2; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 539 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 540 | tx.buffer[tx.len] = 0x02; |
| 541 | strcpy(&tx.buffer[tx.len+1],SMB_ProtocolDialect); |
| 542 | tx.len += buflen; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 543 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 544 | rx.buffer = NULL; |
| 545 | rx.len = 0; |
| 546 | if(!NB_Transaction(fd, &tx, &rx)) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 547 | { |
| 548 | ERR("Failed\n"); |
| 549 | return FALSE; |
| 550 | } |
| 551 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 552 | if(!rx.buffer) |
| 553 | return FALSE; |
| 554 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 555 | /* FIXME: check response */ |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 556 | if(SMB_GetError(rx.buffer)) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 557 | { |
| 558 | ERR("returned error\n"); |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 559 | HeapFree(GetProcessHeap(),0,rx.buffer); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 560 | return FALSE; |
| 561 | } |
| 562 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 563 | HeapFree(GetProcessHeap(),0,rx.buffer); |
| 564 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 565 | *dialect = 0; |
| 566 | |
| 567 | return TRUE; |
| 568 | } |
| 569 | |
| 570 | #define SMB_PARAM_COUNT(buffer) ((buffer)[SMB_PCOUNT]) |
| 571 | #define SMB_PARAM(buffer,n) SMB_GETWORD(&(buffer)[SMB_HDRSIZE+2*(n)]) |
| 572 | #define SMB_BUFFER_COUNT(buffer) SMB_GETWORD(buffer+SMB_HDRSIZE+2*SMB_PARAM_COUNT(buffer)) |
| 573 | #define SMB_BUFFER(buffer,n) ((buffer)[SMB_HDRSIZE + 2*SMB_PARAM_COUNT(buffer) + 2 + (n) ]) |
| 574 | |
| 575 | static BOOL SMB_SessionSetup(int fd, USHORT *userid) |
| 576 | { |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 577 | unsigned char buf[0x100]; |
| 578 | int pcount,bcount; |
| 579 | struct NB_Buffer rx, tx; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 580 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 581 | memset(buf,0,sizeof buf); |
| 582 | tx.buffer = buf; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 583 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 584 | tx.len = SMB_Header(tx.buffer, SMB_COM_SESSION_SETUP_ANDX, 0, 0); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 585 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 586 | tx.buffer[tx.len++] = 0; /* no parameters? */ |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 587 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 588 | tx.buffer[tx.len++] = 0xff; /* AndXCommand: secondary request */ |
| 589 | tx.buffer[tx.len++] = 0x00; /* AndXReserved */ |
| 590 | SMB_ADDWORD(&tx.buffer[tx.len],0); /* AndXOffset */ |
| 591 | tx.len += 2; |
| 592 | SMB_ADDWORD(&tx.buffer[tx.len],0x400); /* MaxBufferSize */ |
| 593 | tx.len += 2; |
| 594 | SMB_ADDWORD(&tx.buffer[tx.len],1); /* MaxMpxCount */ |
| 595 | tx.len += 2; |
| 596 | SMB_ADDWORD(&tx.buffer[tx.len],0); /* VcNumber */ |
| 597 | tx.len += 2; |
| 598 | SMB_ADDWORD(&tx.buffer[tx.len],0); /* SessionKey */ |
| 599 | tx.len += 2; |
| 600 | SMB_ADDWORD(&tx.buffer[tx.len],0); /* SessionKey */ |
| 601 | tx.len += 2; |
| 602 | SMB_ADDWORD(&tx.buffer[tx.len],0); /* Password length */ |
| 603 | tx.len += 2; |
| 604 | SMB_ADDWORD(&tx.buffer[tx.len],0); /* Reserved */ |
| 605 | tx.len += 2; |
| 606 | SMB_ADDWORD(&tx.buffer[tx.len],0); /* Reserved */ |
| 607 | tx.len += 2; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 608 | |
| 609 | /* FIXME: add name and password here */ |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 610 | tx.buffer[tx.len++] = 0; /* number of bytes in password */ |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 611 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 612 | rx.buffer = NULL; |
| 613 | rx.len = 0; |
| 614 | if(!NB_Transaction(fd, &tx, &rx)) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 615 | return FALSE; |
| 616 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 617 | if(!rx.buffer) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 618 | return FALSE; |
| 619 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 620 | if(SMB_GetError(rx.buffer)) |
| 621 | goto done; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 622 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 623 | pcount = SMB_PARAM_COUNT(rx.buffer); |
| 624 | |
| 625 | if( (SMB_HDRSIZE+pcount*2) > rx.len ) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 626 | { |
| 627 | ERR("Bad parameter count %d\n",pcount); |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 628 | goto done; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 631 | if(TRACE_ON(file)) |
| 632 | { |
| 633 | int i; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 634 | DPRINTF("SMB_COM_SESSION_SETUP response, %d args: ",pcount); |
| 635 | for(i=0; i<pcount; i++) |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 636 | DPRINTF("%04x ",SMB_PARAM(rx.buffer,i)); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 637 | DPRINTF("\n"); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 640 | bcount = SMB_BUFFER_COUNT(rx.buffer); |
| 641 | if( (SMB_HDRSIZE+pcount*2+2+bcount) > rx.len ) |
| 642 | { |
| 643 | ERR("parameter count %x, buffer count %x, len %x\n",pcount,bcount,rx.len); |
| 644 | goto done; |
| 645 | } |
| 646 | |
| 647 | if(TRACE_ON(file)) |
| 648 | { |
| 649 | int i; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 650 | DPRINTF("response buffer %d bytes: ",bcount); |
| 651 | for(i=0; i<bcount; i++) |
| 652 | { |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 653 | unsigned char ch = SMB_BUFFER(rx.buffer,i); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 654 | DPRINTF("%c", isprint(ch)?ch:' '); |
| 655 | } |
| 656 | DPRINTF("\n"); |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 657 | } |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 658 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 659 | *userid = SMB_GETWORD(&rx.buffer[SMB_USERID]); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 660 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 661 | HeapFree(GetProcessHeap(),0,rx.buffer); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 662 | return TRUE; |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 663 | |
| 664 | done: |
| 665 | HeapFree(GetProcessHeap(),0,rx.buffer); |
| 666 | return FALSE; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 669 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 670 | static BOOL SMB_TreeConnect(int fd, USHORT user_id, LPCSTR share_name, USHORT *treeid) |
| 671 | { |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 672 | unsigned char buf[0x100]; |
| 673 | int slen; |
| 674 | struct NB_Buffer rx,tx; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 675 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 676 | TRACE("%s\n",share_name); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 677 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 678 | memset(buf,0,sizeof buf); |
| 679 | tx.buffer = buf; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 680 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 681 | tx.len = SMB_Header(tx.buffer, SMB_COM_TREE_CONNECT, 0, user_id); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 682 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 683 | tx.buffer[tx.len++] = 4; /* parameters */ |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 684 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 685 | tx.buffer[tx.len++] = 0xff; /* AndXCommand: secondary request */ |
| 686 | tx.buffer[tx.len++] = 0x00; /* AndXReserved */ |
| 687 | SMB_ADDWORD(&tx.buffer[tx.len],0); /* AndXOffset */ |
| 688 | tx.len += 2; |
| 689 | SMB_ADDWORD(&tx.buffer[tx.len],0); /* Flags */ |
| 690 | tx.len += 2; |
| 691 | SMB_ADDWORD(&tx.buffer[tx.len],1); /* Password length */ |
| 692 | tx.len += 2; |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 693 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 694 | /* SMB command buffer */ |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 695 | SMB_ADDWORD(&tx.buffer[tx.len],3); /* command buffer len */ |
| 696 | tx.len += 2; |
| 697 | tx.buffer[tx.len++] = 0; /* null terminated password */ |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 698 | |
| 699 | slen = strlen(share_name); |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 700 | if(slen<(sizeof buf-tx.len)) |
| 701 | strcpy(&tx.buffer[tx.len], share_name); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 702 | else |
| 703 | return FALSE; |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 704 | tx.len += slen+1; |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 705 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 706 | /* name of the service */ |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 707 | tx.buffer[tx.len++] = 0; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 708 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 709 | rx.buffer = NULL; |
| 710 | rx.len = 0; |
| 711 | if(!NB_Transaction(fd, &tx, &rx)) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 712 | return FALSE; |
| 713 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 714 | if(!rx.buffer) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 715 | return FALSE; |
| 716 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 717 | if(SMB_GetError(rx.buffer)) |
| 718 | { |
| 719 | HeapFree(GetProcessHeap(),0,rx.buffer); |
| 720 | return FALSE; |
| 721 | } |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 722 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 723 | *treeid = SMB_GETWORD(&rx.buffer[SMB_TREEID]); |
| 724 | |
| 725 | HeapFree(GetProcessHeap(),0,rx.buffer); |
| 726 | TRACE("OK, treeid = %04x\n", *treeid); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 727 | |
| 728 | return TRUE; |
| 729 | } |
| 730 | |
Alexandre Julliard | 958732d | 2002-07-02 02:09:39 +0000 | [diff] [blame] | 731 | #if 0 /* not yet */ |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 732 | static BOOL SMB_NtCreateOpen(int fd, USHORT tree_id, USHORT user_id, USHORT dialect, |
| 733 | LPCSTR filename, DWORD access, DWORD sharing, |
| 734 | LPSECURITY_ATTRIBUTES sa, DWORD creation, |
| 735 | DWORD attributes, HANDLE template, USHORT *file_id ) |
| 736 | { |
| 737 | unsigned char buffer[0x100]; |
| 738 | int len = 0,slen; |
| 739 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 740 | TRACE("%s\n",filename); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 741 | |
| 742 | memset(buffer,0,sizeof buffer); |
| 743 | |
| 744 | len = SMB_Header(buffer, SMB_COM_NT_CREATE_ANDX, tree_id, user_id); |
| 745 | |
| 746 | /* 0 */ |
| 747 | buffer[len++] = 24; /* parameters */ |
| 748 | |
| 749 | buffer[len++] = 0xff; /* AndXCommand: secondary request */ |
| 750 | buffer[len++] = 0x00; /* AndXReserved */ |
| 751 | SMB_ADDWORD(&buffer[len],0); len += 2; /* AndXOffset */ |
| 752 | |
| 753 | buffer[len++] = 0; /* reserved */ |
| 754 | slen = strlen(filename); |
| 755 | SMB_ADDWORD(&buffer[len],slen); len += 2; /* name length */ |
| 756 | |
| 757 | /* 0x08 */ |
| 758 | SMB_ADDDWORD(&buffer[len],0); len += 4; /* flags */ |
| 759 | SMB_ADDDWORD(&buffer[len],0); len += 4; /* root directory fid */ |
| 760 | /* 0x10 */ |
| 761 | SMB_ADDDWORD(&buffer[len],access); len += 4; /* access */ |
| 762 | SMB_ADDDWORD(&buffer[len],0); len += 4; /* allocation size */ |
| 763 | /* 0x18 */ |
| 764 | SMB_ADDDWORD(&buffer[len],0); len += 4; /* root directory fid */ |
| 765 | |
| 766 | /* 0x1c */ |
| 767 | SMB_ADDDWORD(&buffer[len],0); len += 4; /* initial allocation */ |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 768 | SMB_ADDDWORD(&buffer[len],0); len += 4; |
| 769 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 770 | /* 0x24 */ |
| 771 | SMB_ADDDWORD(&buffer[len],attributes); len += 4; /* ExtFileAttributes*/ |
| 772 | |
| 773 | /* 0x28 */ |
| 774 | SMB_ADDDWORD(&buffer[len],sharing); len += 4; /* ShareAccess */ |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 775 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 776 | /* 0x2c */ |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 777 | TRACE("creation = %08lx\n",creation); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 778 | SMB_ADDDWORD(&buffer[len],creation); len += 4; /* CreateDisposition */ |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 779 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 780 | /* 0x30 */ |
| 781 | SMB_ADDDWORD(&buffer[len],creation); len += 4; /* CreateOptions */ |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 782 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 783 | /* 0x34 */ |
| 784 | SMB_ADDDWORD(&buffer[len],0); len += 4; /* Impersonation */ |
| 785 | |
| 786 | /* 0x38 */ |
| 787 | buffer[len++] = 0; /* security flags */ |
| 788 | |
| 789 | /* 0x39 */ |
| 790 | SMB_ADDWORD(&buffer[len],slen); len += 2; /* size of buffer */ |
| 791 | |
| 792 | if(slen<(sizeof buffer-len)) |
| 793 | strcpy(&buffer[len], filename); |
| 794 | else |
| 795 | return FALSE; |
| 796 | len += slen+1; |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 797 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 798 | /* name of the file */ |
| 799 | buffer[len++] = 0; |
| 800 | |
| 801 | if(!NB_Transaction(fd, buffer, len, &len)) |
| 802 | return FALSE; |
| 803 | |
| 804 | if(SMB_GetError(buffer)) |
| 805 | return FALSE; |
| 806 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 807 | TRACE("OK\n"); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 808 | |
| 809 | /* FIXME */ |
| 810 | /* *file_id = SMB_GETWORD(&buffer[xxx]); */ |
| 811 | *file_id = 0; |
| 812 | return FALSE; |
| 813 | |
| 814 | return TRUE; |
| 815 | } |
Alexandre Julliard | 958732d | 2002-07-02 02:09:39 +0000 | [diff] [blame] | 816 | #endif |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 817 | |
| 818 | static USHORT SMB_GetMode(DWORD access, DWORD sharing) |
| 819 | { |
| 820 | USHORT mode=0; |
| 821 | |
| 822 | switch(access&(GENERIC_READ|GENERIC_WRITE)) |
| 823 | { |
| 824 | case GENERIC_READ: |
| 825 | mode |= OF_READ; |
| 826 | break; |
| 827 | case GENERIC_WRITE: |
| 828 | mode |= OF_WRITE; |
| 829 | break; |
| 830 | case (GENERIC_READ|GENERIC_WRITE): |
| 831 | mode |= OF_READWRITE; |
| 832 | break; |
| 833 | } |
| 834 | |
| 835 | switch(sharing&(FILE_SHARE_READ|FILE_SHARE_WRITE)) |
| 836 | { |
| 837 | case (FILE_SHARE_READ|FILE_SHARE_WRITE): |
| 838 | mode |= OF_SHARE_DENY_NONE; |
| 839 | break; |
| 840 | case FILE_SHARE_READ: |
| 841 | mode |= OF_SHARE_DENY_WRITE; |
| 842 | break; |
| 843 | case FILE_SHARE_WRITE: |
| 844 | mode |= OF_SHARE_DENY_READ; |
| 845 | break; |
| 846 | default: |
| 847 | mode |= OF_SHARE_EXCLUSIVE; |
| 848 | break; |
| 849 | } |
| 850 | |
| 851 | return mode; |
| 852 | } |
| 853 | |
Alexandre Julliard | 958732d | 2002-07-02 02:09:39 +0000 | [diff] [blame] | 854 | #if 0 /* not yet */ |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 855 | /* inverse of FILE_ConvertOFMode */ |
| 856 | static BOOL SMB_OpenAndX(int fd, USHORT tree_id, USHORT user_id, USHORT dialect, |
| 857 | LPCSTR filename, DWORD access, DWORD sharing, |
| 858 | DWORD creation, DWORD attributes, USHORT *file_id ) |
| 859 | { |
| 860 | unsigned char buffer[0x100]; |
| 861 | int len = 0; |
| 862 | USHORT mode; |
| 863 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 864 | TRACE("%s\n",filename); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 865 | |
| 866 | mode = SMB_GetMode(access,sharing); |
| 867 | |
| 868 | memset(buffer,0,sizeof buffer); |
| 869 | |
| 870 | len = SMB_Header(buffer, SMB_COM_OPEN_ANDX, tree_id, user_id); |
| 871 | |
| 872 | /* 0 */ |
| 873 | buffer[len++] = 15; /* parameters */ |
| 874 | buffer[len++] = 0xff; /* AndXCommand: secondary request */ |
| 875 | buffer[len++] = 0x00; /* AndXReserved */ |
| 876 | SMB_ADDWORD(buffer+len,0); len+=2; /* AndXOffset */ |
| 877 | SMB_ADDWORD(buffer+len,0); len+=2; /* Flags */ |
| 878 | SMB_ADDWORD(buffer+len,mode); len+=2; /* desired access */ |
| 879 | SMB_ADDWORD(buffer+len,0); len+=2; /* search attributes */ |
| 880 | SMB_ADDWORD(buffer+len,0); len+=2; |
| 881 | |
| 882 | /*FIXME: complete */ |
| 883 | return FALSE; |
| 884 | } |
Alexandre Julliard | 958732d | 2002-07-02 02:09:39 +0000 | [diff] [blame] | 885 | #endif |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 886 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 887 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 888 | static BOOL SMB_Open(int fd, USHORT tree_id, USHORT user_id, USHORT dialect, |
| 889 | LPCSTR filename, DWORD access, DWORD sharing, |
| 890 | DWORD creation, DWORD attributes, USHORT *file_id ) |
| 891 | { |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 892 | unsigned char buf[0x100]; |
| 893 | int slen,pcount,i; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 894 | USHORT mode = SMB_GetMode(access,sharing); |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 895 | struct NB_Buffer rx,tx; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 896 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 897 | TRACE("%s\n",filename); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 898 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 899 | memset(buf,0,sizeof buf); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 900 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 901 | tx.buffer = buf; |
| 902 | tx.len = SMB_Header(tx.buffer, SMB_COM_OPEN, tree_id, user_id); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 903 | |
| 904 | /* 0 */ |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 905 | tx.buffer[tx.len++] = 2; /* parameters */ |
| 906 | SMB_ADDWORD(tx.buffer+tx.len,mode); tx.len+=2; |
| 907 | SMB_ADDWORD(tx.buffer+tx.len,0); tx.len+=2; /* search attributes */ |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 908 | |
| 909 | slen = strlen(filename)+2; /* inc. nul and BufferFormat */ |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 910 | SMB_ADDWORD(tx.buffer+tx.len,slen); tx.len+=2; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 911 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 912 | tx.buffer[tx.len] = 0x04; /* BufferFormat */ |
| 913 | strcpy(&tx.buffer[tx.len+1],filename); |
| 914 | tx.len += slen; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 915 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 916 | rx.buffer = NULL; |
| 917 | rx.len = 0; |
| 918 | if(!NB_Transaction(fd, &tx, &rx)) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 919 | return FALSE; |
| 920 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 921 | if(!rx.buffer) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 922 | return FALSE; |
| 923 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 924 | if(SMB_GetError(rx.buffer)) |
| 925 | return FALSE; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 926 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 927 | pcount = SMB_PARAM_COUNT(rx.buffer); |
| 928 | |
| 929 | if( (SMB_HDRSIZE+pcount*2) > rx.len ) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 930 | { |
| 931 | ERR("Bad parameter count %d\n",pcount); |
| 932 | return FALSE; |
| 933 | } |
| 934 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 935 | TRACE("response, %d args: ",pcount); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 936 | for(i=0; i<pcount; i++) |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 937 | DPRINTF("%04x ",SMB_PARAM(rx.buffer,i)); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 938 | DPRINTF("\n"); |
| 939 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 940 | *file_id = SMB_PARAM(rx.buffer,0); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 941 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 942 | TRACE("file_id = %04x\n",*file_id); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 943 | |
| 944 | return TRUE; |
| 945 | } |
| 946 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 947 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 948 | static BOOL SMB_Read(int fd, USHORT tree_id, USHORT user_id, USHORT dialect, |
| 949 | USHORT file_id, DWORD offset, LPVOID out, USHORT count, LPUSHORT read) |
| 950 | { |
| 951 | int buf_size,n,i; |
| 952 | struct NB_Buffer rx,tx; |
| 953 | |
| 954 | TRACE("user %04x tree %04x file %04x count %04x offset %08lx\n", |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 955 | user_id, tree_id, file_id, count, offset); |
| 956 | |
| 957 | buf_size = count+0x100; |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 958 | tx.buffer = (unsigned char *) HeapAlloc(GetProcessHeap(),0,buf_size); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 959 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 960 | memset(tx.buffer,0,buf_size); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 961 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 962 | tx.len = SMB_Header(tx.buffer, SMB_COM_READ, tree_id, user_id); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 963 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 964 | tx.buffer[tx.len++] = 5; |
| 965 | SMB_ADDWORD(&tx.buffer[tx.len],file_id); tx.len += 2; |
| 966 | SMB_ADDWORD(&tx.buffer[tx.len],count); tx.len += 2; |
| 967 | SMB_ADDDWORD(&tx.buffer[tx.len],offset); tx.len += 4; |
| 968 | SMB_ADDWORD(&tx.buffer[tx.len],0); tx.len += 2; /* how many more bytes will be read */ |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 969 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 970 | tx.buffer[tx.len++] = 0; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 971 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 972 | rx.buffer = NULL; |
| 973 | rx.len = 0; |
| 974 | if(!NB_Transaction(fd, &tx, &rx)) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 975 | { |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 976 | HeapFree(GetProcessHeap(),0,tx.buffer); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 977 | return FALSE; |
| 978 | } |
| 979 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 980 | if(SMB_GetError(rx.buffer)) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 981 | { |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 982 | HeapFree(GetProcessHeap(),0,rx.buffer); |
| 983 | HeapFree(GetProcessHeap(),0,tx.buffer); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 984 | return FALSE; |
| 985 | } |
| 986 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 987 | n = SMB_PARAM_COUNT(rx.buffer); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 988 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 989 | if( (SMB_HDRSIZE+n*2) > rx.len ) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 990 | { |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 991 | HeapFree(GetProcessHeap(),0,rx.buffer); |
| 992 | HeapFree(GetProcessHeap(),0,tx.buffer); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 993 | ERR("Bad parameter count %d\n",n); |
| 994 | return FALSE; |
| 995 | } |
| 996 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 997 | TRACE("response, %d args: ",n); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 998 | for(i=0; i<n; i++) |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 999 | DPRINTF("%04x ",SMB_PARAM(rx.buffer,i)); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1000 | DPRINTF("\n"); |
| 1001 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 1002 | n = SMB_PARAM(rx.buffer,5) - 3; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1003 | if(n>count) |
| 1004 | n=count; |
| 1005 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 1006 | memcpy( out, &SMB_BUFFER(rx.buffer,3), n); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1007 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 1008 | TRACE("Read %d bytes\n",n); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1009 | *read = n; |
| 1010 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 1011 | HeapFree(GetProcessHeap(),0,tx.buffer); |
| 1012 | HeapFree(GetProcessHeap(),0,rx.buffer); |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1013 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1014 | return TRUE; |
| 1015 | } |
| 1016 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 1017 | |
| 1018 | /* |
| 1019 | * setup_count : number of USHORTs in the setup string |
| 1020 | */ |
| 1021 | struct SMB_Trans2Info |
| 1022 | { |
| 1023 | struct NB_Buffer buf; |
| 1024 | unsigned char *setup; |
| 1025 | int setup_count; |
| 1026 | unsigned char *params; |
| 1027 | int param_count; |
| 1028 | unsigned char *data; |
| 1029 | int data_count; |
| 1030 | }; |
| 1031 | |
| 1032 | /* |
| 1033 | * Do an SMB transaction |
| 1034 | * |
| 1035 | * This function allocates memory in the recv structure. It is |
| 1036 | * the caller's responsibility to free the memory if it finds |
| 1037 | * that recv->buf.buffer is nonzero. |
| 1038 | */ |
| 1039 | static BOOL SMB_Transaction2(int fd, int tree_id, int user_id, |
| 1040 | struct SMB_Trans2Info *send, |
| 1041 | struct SMB_Trans2Info *recv) |
| 1042 | { |
| 1043 | int buf_size; |
| 1044 | const int retmaxparams = 0xf000; |
| 1045 | const int retmaxdata = 1024; |
| 1046 | const int retmaxsetup = 0; /* FIXME */ |
| 1047 | const int flags = 0; |
| 1048 | const int timeout = 0; |
| 1049 | int param_ofs, data_ofs; |
| 1050 | struct NB_Buffer tx; |
| 1051 | BOOL ret = FALSE; |
| 1052 | |
| 1053 | buf_size = 0x100 + send->setup_count*2 + send->param_count + send->data_count ; |
| 1054 | tx.buffer = (unsigned char *) HeapAlloc(GetProcessHeap(),0,buf_size); |
| 1055 | |
| 1056 | tx.len = SMB_Header(tx.buffer, SMB_COM_TRANSACTION2, tree_id, user_id); |
| 1057 | |
| 1058 | tx.buffer[tx.len++] = 14 + send->setup_count; |
| 1059 | SMB_ADDWORD(&tx.buffer[tx.len],send->param_count); /* total param bytes sent */ |
| 1060 | tx.len += 2; |
| 1061 | SMB_ADDWORD(&tx.buffer[tx.len],send->data_count); /* total data bytes sent */ |
| 1062 | tx.len += 2; |
| 1063 | SMB_ADDWORD(&tx.buffer[tx.len],retmaxparams); /*max parameter bytes to return */ |
| 1064 | tx.len += 2; |
| 1065 | SMB_ADDWORD(&tx.buffer[tx.len],retmaxdata); /* max data bytes to return */ |
| 1066 | tx.len += 2; |
| 1067 | tx.buffer[tx.len++] = retmaxsetup; |
| 1068 | tx.buffer[tx.len++] = 0; /* reserved1 */ |
| 1069 | |
| 1070 | SMB_ADDWORD(&tx.buffer[tx.len],flags); /* flags */ |
| 1071 | tx.len += 2; |
| 1072 | SMB_ADDDWORD(&tx.buffer[tx.len],timeout); /* timeout */ |
| 1073 | tx.len += 4; |
| 1074 | SMB_ADDWORD(&tx.buffer[tx.len],0); /* reserved2 */ |
| 1075 | tx.len += 2; |
| 1076 | SMB_ADDWORD(&tx.buffer[tx.len],send->param_count); /* parameter count - this buffer */ |
| 1077 | tx.len += 2; |
| 1078 | |
| 1079 | param_ofs = tx.len; /* parameter offset */ |
| 1080 | tx.len += 2; |
| 1081 | SMB_ADDWORD(&tx.buffer[tx.len],send->data_count); /* data count */ |
| 1082 | tx.len += 2; |
| 1083 | |
| 1084 | data_ofs = tx.len; /* data offset */ |
| 1085 | tx.len += 2; |
| 1086 | tx.buffer[tx.len++] = send->setup_count; /* setup count */ |
| 1087 | tx.buffer[tx.len++] = 0; /* reserved3 */ |
| 1088 | |
| 1089 | memcpy(&tx.buffer[tx.len], send->setup, send->setup_count*2); /* setup */ |
| 1090 | tx.len += send->setup_count*2; |
| 1091 | |
| 1092 | /* add string here when implementing SMB_COM_TRANS */ |
| 1093 | |
| 1094 | SMB_ADDWORD(&tx.buffer[param_ofs], tx.len); |
| 1095 | memcpy(&tx.buffer[tx.len], send->params, send->param_count); /* parameters */ |
| 1096 | tx.len += send->param_count; |
| 1097 | if(tx.len%2) |
| 1098 | tx.len ++; /* pad2 */ |
| 1099 | |
| 1100 | SMB_ADDWORD(&tx.buffer[data_ofs], tx.len); |
| 1101 | if(send->data_count && send->data) |
| 1102 | { |
| 1103 | memcpy(&tx.buffer[tx.len], send->data, send->data_count); /* data */ |
| 1104 | tx.len += send->data_count; |
| 1105 | } |
| 1106 | |
| 1107 | recv->buf.buffer = NULL; |
| 1108 | recv->buf.len = 0; |
| 1109 | if(!NB_Transaction(fd, &tx, &recv->buf)) |
| 1110 | goto done; |
| 1111 | |
| 1112 | if(!recv->buf.buffer) |
| 1113 | goto done; |
| 1114 | |
| 1115 | if(SMB_GetError(recv->buf.buffer)) |
| 1116 | goto done; |
| 1117 | |
| 1118 | /* reuse these two offsets to check the received message */ |
| 1119 | param_ofs = SMB_PARAM(recv->buf.buffer,4); |
| 1120 | data_ofs = SMB_PARAM(recv->buf.buffer,7); |
| 1121 | |
| 1122 | if( (recv->param_count + param_ofs) > recv->buf.len ) |
| 1123 | goto done; |
| 1124 | |
| 1125 | if( (recv->data_count + data_ofs) > recv->buf.len ) |
| 1126 | goto done; |
| 1127 | |
| 1128 | TRACE("Success\n"); |
| 1129 | |
| 1130 | recv->setup = NULL; |
| 1131 | recv->setup_count = 0; |
| 1132 | |
| 1133 | recv->param_count = SMB_PARAM(recv->buf.buffer,0); |
| 1134 | recv->params = &recv->buf.buffer[param_ofs]; |
| 1135 | |
| 1136 | recv->data_count = SMB_PARAM(recv->buf.buffer,6); |
| 1137 | recv->data = &recv->buf.buffer[data_ofs]; |
| 1138 | |
| 1139 | /* |
| 1140 | TRACE("%d words\n",SMB_PARAM_COUNT(recv->buf.buffer)); |
| 1141 | TRACE("total parameters = %d\n",SMB_PARAM(recv->buf.buffer,0)); |
| 1142 | TRACE("total data = %d\n",SMB_PARAM(recv->buf.buffer,1)); |
| 1143 | TRACE("parameters = %d\n",SMB_PARAM(recv->buf.buffer,3)); |
| 1144 | TRACE("parameter offset = %d\n",SMB_PARAM(recv->buf.buffer,4)); |
| 1145 | TRACE("param displace = %d\n",SMB_PARAM(recv->buf.buffer,5)); |
| 1146 | |
| 1147 | TRACE("data count = %d\n",SMB_PARAM(recv->buf.buffer,6)); |
| 1148 | TRACE("data offset = %d\n",SMB_PARAM(recv->buf.buffer,7)); |
| 1149 | TRACE("data displace = %d\n",SMB_PARAM(recv->buf.buffer,8)); |
| 1150 | */ |
| 1151 | |
| 1152 | ret = TRUE; |
| 1153 | |
| 1154 | done: |
| 1155 | if(tx.buffer) |
| 1156 | HeapFree(GetProcessHeap(),0,tx.buffer); |
| 1157 | |
| 1158 | return ret; |
| 1159 | } |
| 1160 | |
| 1161 | static BOOL SMB_SetupFindFirst(struct SMB_Trans2Info *send, LPSTR filename) |
| 1162 | { |
| 1163 | int search_attribs = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM; |
| 1164 | int search_count = 10; |
| 1165 | int flags = 0; |
| 1166 | int infolevel = 0x104; /* SMB_FILE_BOTH_DIRECTORY_INFO */ |
| 1167 | int storagetype = 0; |
| 1168 | int len, buf_size; |
| 1169 | |
| 1170 | memset(send,0,sizeof send); |
| 1171 | |
| 1172 | send->setup_count = 1; |
| 1173 | send->setup = HeapAlloc(GetProcessHeap(),0,send->setup_count*2); |
| 1174 | if(!send->setup) |
| 1175 | return FALSE; |
| 1176 | |
| 1177 | buf_size = 0x10 + lstrlenA(filename); |
| 1178 | send->params = HeapAlloc(GetProcessHeap(),0,buf_size); |
| 1179 | if(!send->params) |
| 1180 | { |
| 1181 | HeapFree(GetProcessHeap(),0,send->setup); |
| 1182 | return FALSE; |
| 1183 | } |
| 1184 | |
| 1185 | SMB_ADDWORD(send->setup,TRANS2_FIND_FIRST2); |
| 1186 | |
| 1187 | len = 0; |
| 1188 | memset(send->params,0,buf_size); |
| 1189 | SMB_ADDWORD(&send->params[len],search_attribs); len += 2; |
| 1190 | SMB_ADDWORD(&send->params[len],search_count); len += 2; |
| 1191 | SMB_ADDWORD(&send->params[len],flags); len += 2; |
| 1192 | SMB_ADDWORD(&send->params[len],infolevel); len += 2; |
| 1193 | SMB_ADDDWORD(&send->params[len],storagetype); len += 4; |
| 1194 | |
| 1195 | strcpy(&send->params[len],filename); |
| 1196 | len += lstrlenA(filename)+1; |
| 1197 | |
| 1198 | send->param_count = len; |
| 1199 | send->data = NULL; |
| 1200 | send->data_count = 0; |
| 1201 | |
| 1202 | return TRUE; |
| 1203 | } |
| 1204 | |
| 1205 | static SMB_DIR *SMB_Trans2FindFirst(int fd, USHORT tree_id, |
| 1206 | USHORT user_id, USHORT dialect, LPSTR filename ) |
| 1207 | { |
| 1208 | int num; |
| 1209 | BOOL ret; |
| 1210 | /* char *filename = "\\*"; */ |
| 1211 | struct SMB_Trans2Info send, recv; |
| 1212 | SMB_DIR *smbdir = NULL; |
| 1213 | |
| 1214 | TRACE("patern = %s\n",filename); |
| 1215 | |
| 1216 | if(!SMB_SetupFindFirst(&send, filename)) |
| 1217 | return FALSE; |
| 1218 | |
| 1219 | memset(&recv,0,sizeof recv); |
| 1220 | |
| 1221 | ret = SMB_Transaction2(fd, tree_id, user_id, &send, &recv); |
| 1222 | HeapFree(GetProcessHeap(),0,send.params); |
| 1223 | HeapFree(GetProcessHeap(),0,send.setup); |
| 1224 | |
| 1225 | if(!ret) |
| 1226 | goto done; |
| 1227 | |
| 1228 | if(recv.setup_count) |
| 1229 | goto done; |
| 1230 | |
| 1231 | if(recv.param_count != 10) |
| 1232 | goto done; |
| 1233 | |
| 1234 | num = SMB_GETWORD(&recv.params[2]); |
| 1235 | TRACE("Success, search id: %d\n",num); |
| 1236 | |
| 1237 | if(SMB_GETWORD(&recv.params[4])) |
| 1238 | FIXME("need to read more!\n"); |
| 1239 | |
| 1240 | smbdir = HeapAlloc(GetProcessHeap(),0,sizeof(*smbdir)); |
| 1241 | if(smbdir) |
| 1242 | { |
| 1243 | int i, ofs=0; |
| 1244 | |
| 1245 | smbdir->current = 0; |
| 1246 | smbdir->num_entries = num; |
| 1247 | smbdir->entries = HeapAlloc(GetProcessHeap(), 0, sizeof(unsigned char*)*num); |
| 1248 | if(!smbdir->entries) |
| 1249 | goto done; |
| 1250 | smbdir->buffer = recv.buf.buffer; /* save to free later */ |
| 1251 | |
| 1252 | for(i=0; i<num; i++) |
| 1253 | { |
| 1254 | int size = SMB_GETDWORD(&recv.data[ofs]); |
| 1255 | |
| 1256 | smbdir->entries[i] = &recv.data[ofs]; |
| 1257 | |
| 1258 | if(TRACE_ON(file)) |
| 1259 | { |
| 1260 | int j; |
| 1261 | for(j=0; j<size; j++) |
| 1262 | DPRINTF("%02x%c",recv.data[ofs+j],((j+1)%16)?' ':'\n'); |
| 1263 | } |
| 1264 | TRACE("file %d : %s\n", i, &recv.data[ofs+0x5e]); |
| 1265 | ofs += size; |
| 1266 | if(ofs>recv.data_count) |
| 1267 | goto done; |
| 1268 | } |
| 1269 | |
| 1270 | ret = TRUE; |
| 1271 | } |
| 1272 | |
| 1273 | done: |
| 1274 | if(!ret) |
| 1275 | { |
| 1276 | if( recv.buf.buffer ) |
| 1277 | HeapFree(GetProcessHeap(),0,recv.buf.buffer); |
| 1278 | if( smbdir ) |
| 1279 | { |
| 1280 | if( smbdir->entries ) |
| 1281 | HeapFree(GetProcessHeap(),0,smbdir->entries); |
| 1282 | HeapFree(GetProcessHeap(),0,smbdir); |
| 1283 | } |
| 1284 | smbdir = NULL; |
| 1285 | } |
| 1286 | |
| 1287 | return smbdir; |
| 1288 | } |
| 1289 | |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1290 | static int SMB_GetSocket(LPCSTR host) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1291 | { |
| 1292 | int fd=-1,r; |
| 1293 | struct sockaddr_in sin; |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1294 | struct hostent *he; |
| 1295 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 1296 | TRACE("host %s\n",host); |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1297 | |
| 1298 | he = gethostbyname(host); |
| 1299 | if(he) |
| 1300 | { |
| 1301 | memcpy(&sin.sin_addr,he->h_addr, sizeof (sin.sin_addr)); |
| 1302 | goto connect; |
| 1303 | } |
| 1304 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 1305 | if(NB_Lookup(host,&sin)) |
| 1306 | goto connect; |
| 1307 | |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1308 | /* FIXME: resolve by WINS too */ |
| 1309 | |
| 1310 | ERR("couldn't resolve SMB host %s\n", host); |
| 1311 | |
| 1312 | return -1; |
| 1313 | |
| 1314 | connect: |
| 1315 | sin.sin_family = AF_INET; |
| 1316 | sin.sin_port = htons(139); /* netbios session */ |
| 1317 | |
| 1318 | fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 1319 | if(fd<0) |
| 1320 | return fd; |
| 1321 | |
| 1322 | { |
| 1323 | unsigned char *x = (unsigned char *)&sin.sin_addr; |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 1324 | TRACE("Connecting to %d.%d.%d.%d ...\n", x[0],x[1],x[2],x[3]); |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1325 | } |
| 1326 | r = connect(fd, &sin, sizeof sin); |
| 1327 | |
| 1328 | if(!NB_SessionReq(fd, "*SMBSERVER", "WINE")) |
| 1329 | { |
| 1330 | close(fd); |
| 1331 | return -1; |
| 1332 | } |
| 1333 | |
| 1334 | return fd; |
| 1335 | } |
| 1336 | |
| 1337 | static BOOL SMB_LoginAndConnect(int fd, LPCSTR host, LPCSTR share, USHORT *tree_id, USHORT *user_id, USHORT *dialect) |
| 1338 | { |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1339 | LPSTR name=NULL; |
| 1340 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 1341 | TRACE("host %s share %s\n",host,share); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1342 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1343 | if(!SMB_NegotiateProtocol(fd, dialect)) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1344 | return FALSE; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1345 | |
| 1346 | if(!SMB_SessionSetup(fd, user_id)) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1347 | return FALSE; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1348 | |
| 1349 | name = HeapAlloc(GetProcessHeap(),0,strlen(host)+strlen(share)+5); |
| 1350 | if(!name) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1351 | return FALSE; |
| 1352 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1353 | sprintf(name,"\\\\%s\\%s",host,share); |
| 1354 | if(!SMB_TreeConnect(fd,*user_id,name,tree_id)) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1355 | { |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1356 | HeapFree(GetProcessHeap(),0,name); |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1357 | return FALSE; |
| 1358 | } |
| 1359 | |
| 1360 | return TRUE; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | static HANDLE SMB_RegisterFile( int fd, USHORT tree_id, USHORT user_id, USHORT dialect, USHORT file_id) |
| 1364 | { |
| 1365 | int r; |
| 1366 | HANDLE ret; |
| 1367 | |
| 1368 | wine_server_send_fd( fd ); |
| 1369 | |
| 1370 | SERVER_START_REQ( create_smb ) |
| 1371 | { |
| 1372 | req->tree_id = tree_id; |
| 1373 | req->user_id = user_id; |
| 1374 | req->file_id = file_id; |
| 1375 | req->dialect = 0; |
| 1376 | req->fd = fd; |
| 1377 | SetLastError(0); |
| 1378 | r = wine_server_call_err( req ); |
| 1379 | ret = reply->handle; |
| 1380 | } |
| 1381 | SERVER_END_REQ; |
| 1382 | |
| 1383 | if(!r) |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 1384 | TRACE("created wineserver smb object, handle = %04x\n",ret); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1385 | else |
| 1386 | SetLastError( ERROR_PATH_NOT_FOUND ); |
| 1387 | |
| 1388 | return ret; |
| 1389 | } |
| 1390 | |
| 1391 | HANDLE WINAPI SMB_CreateFileA( LPCSTR uncname, DWORD access, DWORD sharing, |
| 1392 | LPSECURITY_ATTRIBUTES sa, DWORD creation, |
| 1393 | DWORD attributes, HANDLE template ) |
| 1394 | { |
| 1395 | int fd; |
| 1396 | USHORT tree_id=0, user_id=0, dialect=0, file_id=0; |
| 1397 | LPSTR name,host,share,file; |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1398 | HANDLE handle = INVALID_HANDLE_VALUE; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1399 | |
| 1400 | name = HeapAlloc(GetProcessHeap(),0,lstrlenA(uncname)); |
| 1401 | if(!name) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1402 | return handle; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1403 | |
| 1404 | lstrcpyA(name,uncname); |
| 1405 | |
| 1406 | if( !UNC_SplitName(name, &host, &share, &file) ) |
| 1407 | { |
| 1408 | HeapFree(GetProcessHeap(),0,name); |
| 1409 | return handle; |
| 1410 | } |
| 1411 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 1412 | TRACE("server is %s, share is %s, file is %s\n", host, share, file); |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1413 | |
| 1414 | fd = SMB_GetSocket(host); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1415 | if(fd < 0) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1416 | goto done; |
| 1417 | |
| 1418 | if(!SMB_LoginAndConnect(fd, host, share, &tree_id, &user_id, &dialect)) |
| 1419 | goto done; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1420 | |
| 1421 | #if 0 |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1422 | if(!SMB_NtCreateOpen(fd, tree_id, user_id, dialect, file, |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1423 | access, sharing, sa, creation, attributes, template, &file_id )) |
| 1424 | { |
| 1425 | close(fd); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1426 | ERR("CreateOpen failed\n"); |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1427 | goto done; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1428 | } |
| 1429 | #endif |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1430 | if(!SMB_Open(fd, tree_id, user_id, dialect, file, |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1431 | access, sharing, creation, attributes, &file_id )) |
| 1432 | { |
| 1433 | close(fd); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1434 | ERR("CreateOpen failed\n"); |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1435 | goto done; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1438 | handle = SMB_RegisterFile(fd, tree_id, user_id, dialect, file_id); |
| 1439 | if(!handle) |
| 1440 | { |
| 1441 | ERR("register failed\n"); |
| 1442 | close(fd); |
| 1443 | } |
Vincent BĂ©ron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1444 | |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1445 | done: |
| 1446 | HeapFree(GetProcessHeap(),0,name); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1447 | return handle; |
| 1448 | } |
| 1449 | |
| 1450 | static BOOL SMB_GetSmbInfo(HANDLE hFile, USHORT *tree_id, USHORT *user_id, USHORT *dialect, USHORT *file_id, LPDWORD offset) |
| 1451 | { |
| 1452 | int r; |
| 1453 | |
| 1454 | SERVER_START_REQ( get_smb_info ) |
| 1455 | { |
| 1456 | req->handle = hFile; |
| 1457 | req->flags = 0; |
| 1458 | SetLastError(0); |
| 1459 | r = wine_server_call_err( req ); |
| 1460 | if(tree_id) |
| 1461 | *tree_id = reply->tree_id; |
| 1462 | if(user_id) |
| 1463 | *user_id = reply->user_id; |
| 1464 | if(file_id) |
| 1465 | *file_id = reply->file_id; |
| 1466 | if(dialect) |
| 1467 | *dialect = reply->dialect; |
| 1468 | if(offset) |
| 1469 | *offset = reply->offset; |
| 1470 | } |
| 1471 | SERVER_END_REQ; |
| 1472 | |
| 1473 | return !r; |
| 1474 | } |
| 1475 | |
| 1476 | static BOOL SMB_SetOffset(HANDLE hFile, DWORD offset) |
| 1477 | { |
| 1478 | int r; |
| 1479 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 1480 | TRACE("offset = %08lx\n",offset); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1481 | |
| 1482 | SERVER_START_REQ( get_smb_info ) |
| 1483 | { |
| 1484 | req->handle = hFile; |
| 1485 | req->flags = SMBINFO_SET_OFFSET; |
| 1486 | req->offset = offset; |
| 1487 | SetLastError(0); |
| 1488 | r = wine_server_call_err( req ); |
| 1489 | /* if(offset) |
| 1490 | *offset = reply->offset; */ |
| 1491 | } |
| 1492 | SERVER_END_REQ; |
| 1493 | |
| 1494 | return !r; |
| 1495 | } |
| 1496 | |
Patrik Stridvall | f89d4a8 | 2002-03-23 21:39:05 +0000 | [diff] [blame] | 1497 | BOOL WINAPI SMB_ReadFile(HANDLE hFile, LPVOID buffer, DWORD bytesToRead, LPDWORD bytesRead, LPOVERLAPPED lpOverlapped) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1498 | { |
| 1499 | int fd; |
| 1500 | DWORD total, count, offset; |
| 1501 | USHORT user_id, tree_id, dialect, file_id, read; |
| 1502 | BOOL r=TRUE; |
| 1503 | |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 1504 | TRACE("%04x %p %ld %p\n", hFile, buffer, bytesToRead, bytesRead); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1505 | |
| 1506 | if(!SMB_GetSmbInfo(hFile, &tree_id, &user_id, &dialect, &file_id, &offset)) |
| 1507 | return FALSE; |
| 1508 | |
| 1509 | fd = FILE_GetUnixHandle(hFile, GENERIC_READ); |
| 1510 | if(fd<0) |
| 1511 | return FALSE; |
| 1512 | |
| 1513 | total = 0; |
| 1514 | while(1) |
| 1515 | { |
| 1516 | count = bytesToRead - total; |
| 1517 | if(count>0x400) |
| 1518 | count = 0x400; |
| 1519 | if(count==0) |
| 1520 | break; |
| 1521 | read = 0; |
| 1522 | r = SMB_Read(fd, tree_id, user_id, dialect, file_id, offset, buffer, count, &read); |
| 1523 | if(!r) |
| 1524 | break; |
| 1525 | if(!read) |
| 1526 | break; |
| 1527 | total += read; |
Gregg Mattinson | 7c4cb51 | 2002-07-03 21:10:43 +0000 | [diff] [blame] | 1528 | buffer = (char*)buffer + read; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1529 | offset += read; |
| 1530 | if(total>=bytesToRead) |
| 1531 | break; |
| 1532 | } |
| 1533 | close(fd); |
| 1534 | |
| 1535 | if(bytesRead) |
| 1536 | *bytesRead = total; |
| 1537 | |
| 1538 | if(!SMB_SetOffset(hFile, offset)) |
| 1539 | return FALSE; |
| 1540 | |
| 1541 | return r; |
| 1542 | } |
Mike McCormack | 963985b | 2002-07-19 03:17:19 +0000 | [diff] [blame] | 1543 | |
| 1544 | SMB_DIR* WINAPI SMB_FindFirst(LPCSTR name) |
| 1545 | { |
| 1546 | int fd = -1; |
| 1547 | LPSTR host,share,file; |
| 1548 | USHORT tree_id=0, user_id=0, dialect=0; |
| 1549 | SMB_DIR *ret = NULL; |
| 1550 | LPSTR filename; |
| 1551 | |
| 1552 | TRACE("Find %s\n",debugstr_a(name)); |
| 1553 | |
| 1554 | filename = HeapAlloc(GetProcessHeap(),0,lstrlenA(name)+1); |
| 1555 | if(!filename) |
| 1556 | return ret; |
| 1557 | |
| 1558 | lstrcpyA(filename,name); |
| 1559 | |
| 1560 | if( !UNC_SplitName(filename, &host, &share, &file) ) |
| 1561 | goto done; |
| 1562 | |
| 1563 | fd = SMB_GetSocket(host); |
| 1564 | if(fd < 0) |
| 1565 | goto done; |
| 1566 | |
| 1567 | if(!SMB_LoginAndConnect(fd, host, share, &tree_id, &user_id, &dialect)) |
| 1568 | goto done; |
| 1569 | |
| 1570 | TRACE("server is %s, share is %s, file is %s\n", host, share, file); |
| 1571 | |
| 1572 | ret = SMB_Trans2FindFirst(fd, tree_id, user_id, dialect, file); |
| 1573 | |
| 1574 | done: |
| 1575 | /* disconnect */ |
| 1576 | if(fd != -1) |
| 1577 | close(fd); |
| 1578 | |
| 1579 | if(filename) |
| 1580 | HeapFree(GetProcessHeap(),0,filename); |
| 1581 | |
| 1582 | return ret; |
| 1583 | } |
| 1584 | |
| 1585 | |
| 1586 | BOOL WINAPI SMB_FindNext(SMB_DIR *dir, WIN32_FIND_DATAA *data ) |
| 1587 | { |
| 1588 | unsigned char *ent; |
| 1589 | int len, fnlen; |
| 1590 | |
| 1591 | TRACE("%d of %d\n",dir->current,dir->num_entries); |
| 1592 | |
| 1593 | if(dir->current >= dir->num_entries) |
| 1594 | return FALSE; |
| 1595 | |
| 1596 | memset(data, 0, sizeof *data); |
| 1597 | |
| 1598 | ent = dir->entries[dir->current]; |
| 1599 | len = SMB_GETDWORD(&ent[0]); |
| 1600 | if(len<0x5e) |
| 1601 | return FALSE; |
| 1602 | |
| 1603 | memcpy(&data->ftCreationTime, &ent[8], 8); |
| 1604 | memcpy(&data->ftLastAccessTime, &ent[0x10], 8); |
| 1605 | memcpy(&data->ftLastWriteTime, &ent[0x18], 8); |
| 1606 | data->nFileSizeHigh = SMB_GETDWORD(&ent[0x30]); |
| 1607 | data->nFileSizeLow = SMB_GETDWORD(&ent[0x34]); |
| 1608 | data->dwFileAttributes = SMB_GETDWORD(&ent[0x38]); |
| 1609 | |
| 1610 | /* copy the long filename */ |
| 1611 | fnlen = SMB_GETDWORD(&ent[0x3c]); |
| 1612 | if ( fnlen > (sizeof data->cFileName/sizeof(CHAR)) ) |
| 1613 | return FALSE; |
| 1614 | memcpy(data->cFileName, &ent[0x5e], fnlen); |
| 1615 | |
| 1616 | /* copy the short filename */ |
| 1617 | if ( ent[0x44] > (sizeof data->cAlternateFileName/sizeof(CHAR)) ) |
| 1618 | return FALSE; |
| 1619 | memcpy(data->cAlternateFileName, &ent[0x5e + len], ent[0x44]); |
| 1620 | |
| 1621 | dir->current++; |
| 1622 | |
| 1623 | return TRUE; |
| 1624 | } |
| 1625 | |
| 1626 | BOOL WINAPI SMB_CloseDir(SMB_DIR *dir) |
| 1627 | { |
| 1628 | HeapFree(GetProcessHeap(),0,dir->buffer); |
| 1629 | HeapFree(GetProcessHeap(),0,dir->entries); |
| 1630 | memset(dir,0,sizeof *dir); |
| 1631 | HeapFree(GetProcessHeap(),0,dir); |
| 1632 | return TRUE; |
| 1633 | } |