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 |
| 29 | */ |
| 30 | |
| 31 | #include "config.h" |
| 32 | #include "wine/port.h" |
| 33 | |
| 34 | #include <assert.h> |
| 35 | #include <ctype.h> |
| 36 | #include <errno.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <stdio.h> |
| 40 | #include <string.h> |
| 41 | #ifdef HAVE_SYS_ERRNO_H |
| 42 | #include <sys/errno.h> |
| 43 | #endif |
| 44 | #include <sys/types.h> |
| 45 | #include <sys/stat.h> |
| 46 | #ifdef HAVE_SYS_MMAN_H |
| 47 | #include <sys/mman.h> |
| 48 | #endif |
| 49 | #include <sys/time.h> |
| 50 | #include <sys/poll.h> |
| 51 | #include <time.h> |
| 52 | #include <unistd.h> |
| 53 | #include <utime.h> |
Patrik Stridvall | f89d4a8 | 2002-03-23 21:39:05 +0000 | [diff] [blame] | 54 | #ifdef HAVE_SYS_SOCKET_H |
| 55 | # include <sys/socket.h> |
| 56 | #endif |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 57 | #include <sys/types.h> |
Gerald Pfeifer | c8764e4 | 2002-03-19 02:06:25 +0000 | [diff] [blame] | 58 | #ifdef HAVE_NETINET_IN_SYSTM_H |
| 59 | #include <netinet/in_systm.h> |
| 60 | #endif |
| 61 | #ifdef HAVE_NETINET_IN_H |
| 62 | #include <netinet/in.h> |
| 63 | #endif |
| 64 | #ifdef HAVE_NETINET_IP_H |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 65 | #include <netinet/ip.h> |
Gerald Pfeifer | c8764e4 | 2002-03-19 02:06:25 +0000 | [diff] [blame] | 66 | #endif |
| 67 | #ifdef HAVE_ARPA_INET_H |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 68 | #include <arpa/inet.h> |
Gerald Pfeifer | c8764e4 | 2002-03-19 02:06:25 +0000 | [diff] [blame] | 69 | #endif |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 70 | #ifdef HAVE_NETDB_H |
| 71 | #include <netdb.h> |
| 72 | #endif |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 73 | |
| 74 | #include "winerror.h" |
| 75 | #include "windef.h" |
| 76 | #include "winbase.h" |
| 77 | #include "file.h" |
| 78 | #include "heap.h" |
| 79 | |
| 80 | #include "smb.h" |
| 81 | |
| 82 | #include "wine/server.h" |
| 83 | #include "wine/debug.h" |
| 84 | |
| 85 | WINE_DEFAULT_DEBUG_CHANNEL(file); |
| 86 | |
| 87 | #define MAX_HOST_NAME 15 |
| 88 | #define NB_TIMEOUT 10000 |
| 89 | |
| 90 | USHORT SMB_MultiplexId = 0; |
| 91 | |
| 92 | static int netbios_name(const char *p, unsigned char *buffer) |
| 93 | { |
| 94 | char ch; |
| 95 | int i,len=0; |
| 96 | |
| 97 | buffer[len++]=' '; |
| 98 | for(i=0; i<=MAX_HOST_NAME; i++) |
| 99 | { |
| 100 | if(i<MAX_HOST_NAME) |
| 101 | { |
| 102 | if(*p) |
| 103 | ch = *p++&0xdf; /* add character from hostname */ |
| 104 | else |
| 105 | ch = ' '; /* add padding */ |
| 106 | } |
| 107 | else |
| 108 | ch = 0; /* add terminator */ |
| 109 | buffer[len++] = ((ch&0xf0) >> 4) + 'A'; |
| 110 | buffer[len++] = (ch&0x0f) + 'A'; |
| 111 | } |
| 112 | buffer[len++] = 0; /* add second terminator */ |
| 113 | return len; |
| 114 | } |
| 115 | |
| 116 | static DWORD NB_NameReq(LPCSTR host, unsigned char *buffer, int len) |
| 117 | { |
| 118 | int trn = 1234,i=0; |
| 119 | |
| 120 | NBR_ADDWORD(&buffer[i],trn); i+=2; |
| 121 | NBR_ADDWORD(&buffer[i],0x0110); i+=2; |
| 122 | NBR_ADDWORD(&buffer[i],0x0001); i+=2; |
| 123 | NBR_ADDWORD(&buffer[i],0x0000); i+=2; |
| 124 | NBR_ADDWORD(&buffer[i],0x0000); i+=2; |
| 125 | NBR_ADDWORD(&buffer[i],0x0000); i+=2; |
| 126 | |
| 127 | i += netbios_name(host,&buffer[i]); |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 128 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 129 | NBR_ADDWORD(&buffer[i],0x0020); i+=2; |
| 130 | NBR_ADDWORD(&buffer[i],0x0001); i+=2; |
| 131 | |
| 132 | ERR("packet is %d bytes in length\n",i); |
| 133 | |
| 134 | { |
| 135 | int j; |
| 136 | for(j=0; j<i; j++) |
| 137 | printf("%02x%c",buffer[j],(((j+1)%16)&&((j+1)!=j))?' ':'\n'); |
| 138 | } |
| 139 | |
| 140 | return i; |
| 141 | } |
| 142 | |
| 143 | /* unc = \\hostname\share\file... */ |
| 144 | static BOOL UNC_SplitName(LPSTR unc, LPSTR *hostname, LPSTR *share, LPSTR *file) |
| 145 | { |
| 146 | char *p; |
| 147 | |
| 148 | ERR("%s\n",unc); |
| 149 | |
| 150 | p = strchr(unc,'\\'); |
| 151 | if(!p) |
| 152 | return FALSE; |
| 153 | p = strchr(p+1,'\\'); |
| 154 | if(!p) |
| 155 | return FALSE; |
| 156 | *hostname=++p; |
| 157 | |
| 158 | p = strchr(p,'\\'); |
| 159 | if(!p) |
| 160 | return FALSE; |
| 161 | *p=0; |
| 162 | *share = ++p; |
| 163 | |
| 164 | p = strchr(p,'\\'); |
| 165 | if(!p) |
| 166 | return FALSE; |
| 167 | *p=0; |
| 168 | *file = ++p; |
| 169 | |
| 170 | return TRUE; |
| 171 | } |
| 172 | |
| 173 | static BOOL NB_Lookup(LPCSTR host, struct sockaddr_in *addr) |
| 174 | { |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 175 | int fd,on=1,r,len,i,fromsize; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 176 | struct pollfd fds; |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 177 | struct sockaddr_in sin,fromaddr; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 178 | unsigned char buffer[256]; |
| 179 | |
| 180 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 181 | if(fd<0) |
| 182 | return FALSE; |
| 183 | |
| 184 | r = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof on); |
| 185 | if(r<0) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 186 | goto err; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 187 | |
| 188 | if(0==inet_aton("255.255.255.255", (struct in_addr *)&sin.sin_addr.s_addr)) |
| 189 | { |
| 190 | FIXME("Error getting bcast address\n"); |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 191 | goto err; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 192 | } |
| 193 | sin.sin_family = AF_INET; |
| 194 | sin.sin_port = htons(137); |
| 195 | |
| 196 | len = NB_NameReq(host,buffer,sizeof buffer); |
| 197 | if(len<=0) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 198 | goto err; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 199 | |
| 200 | r = sendto(fd, buffer, len, 0, &sin, sizeof sin); |
| 201 | if(r<0) |
| 202 | { |
| 203 | FIXME("Error sending packet\n"); |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 204 | goto err; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | fds.fd = fd; |
| 208 | fds.events = POLLIN; |
| 209 | fds.revents = 0; |
| 210 | |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 211 | /* FIXME: this is simple and easily fooled logic |
| 212 | * we should loop until we receive the correct packet or timeout |
| 213 | */ |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 214 | r = poll(&fds,1,NB_TIMEOUT); |
| 215 | if(r!=1) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 216 | goto err; |
| 217 | |
| 218 | TRACE("Got response!\n"); |
| 219 | |
| 220 | fromsize = sizeof (fromaddr); |
| 221 | r = recvfrom(fd, buffer, sizeof buffer, 0, &fromaddr, &fromsize); |
| 222 | if(r<0) |
| 223 | goto err; |
| 224 | |
| 225 | ERR("%d bytes received\n",r); |
| 226 | |
| 227 | if(r!=62) |
| 228 | goto err; |
| 229 | |
| 230 | for(i=0; i<r; i++) |
| 231 | DPRINTF("%02X%c",buffer[i],(((i+1)!=r)&&((i+1)%16))?' ':'\n'); |
| 232 | DPRINTF("\n"); |
| 233 | |
| 234 | if(0x0f & buffer[3]) |
| 235 | goto err; |
| 236 | |
| 237 | ERR("packet is OK\n"); |
| 238 | |
| 239 | memcpy(&addr->sin_addr, &buffer[58], sizeof addr->sin_addr); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 240 | |
| 241 | close(fd); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 242 | return TRUE; |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 243 | |
| 244 | err: |
| 245 | close(fd); |
| 246 | return FALSE; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | #define NB_FIRST 0x40 |
| 250 | |
| 251 | #define NB_HDRSIZE 4 |
| 252 | |
| 253 | #define NB_SESSION_MSG 0x00 |
| 254 | #define NB_SESSION_REQ 0x81 |
| 255 | |
| 256 | /* RFC 1002, section 4.3.2 */ |
| 257 | static BOOL NB_SessionReq(int fd, char *called, char *calling) |
| 258 | { |
| 259 | unsigned char buffer[0x100]; |
| 260 | int len = 0,r; |
| 261 | struct pollfd fds; |
| 262 | |
| 263 | ERR("called %s, calling %s\n",called,calling); |
| 264 | |
| 265 | buffer[0] = NB_SESSION_REQ; |
| 266 | buffer[1] = NB_FIRST; |
| 267 | |
| 268 | netbios_name(called, &buffer[NB_HDRSIZE]); |
| 269 | len += 34; |
| 270 | netbios_name(calling, &buffer[NB_HDRSIZE+len]); |
| 271 | len += 34; |
| 272 | |
| 273 | NBR_ADDWORD(&buffer[2],len); |
| 274 | |
| 275 | /* for(i=0; i<(len+NB_HDRSIZE); i++) |
| 276 | DPRINTF("%02X%c",buffer[i],(((i+1)!=(len+4))&&((i+1)%16))?' ':'\n'); */ |
| 277 | |
| 278 | r = write(fd,buffer,len+4); |
| 279 | if(r<0) |
| 280 | { |
| 281 | ERR("Write failed\n"); |
| 282 | return FALSE; |
| 283 | } |
| 284 | |
| 285 | fds.fd = fd; |
| 286 | fds.events = POLLIN; |
| 287 | fds.revents = 0; |
| 288 | |
| 289 | r = poll(&fds,1,NB_TIMEOUT); |
| 290 | if(r!=1) |
| 291 | { |
| 292 | ERR("Poll failed\n"); |
| 293 | return FALSE; |
| 294 | } |
| 295 | |
| 296 | r = read(fd, buffer, NB_HDRSIZE); |
| 297 | if((r!=NB_HDRSIZE) || (buffer[0]!=0x82)) |
| 298 | { |
| 299 | ERR("Received %d bytes\n",r); |
| 300 | ERR("%02x %02x %02x %02x\n", buffer[0],buffer[1],buffer[2],buffer[3]); |
| 301 | return FALSE; |
| 302 | } |
| 303 | |
| 304 | return TRUE; |
| 305 | } |
| 306 | |
| 307 | static BOOL NB_SendData(int fd, unsigned char *data, int size) |
| 308 | { |
| 309 | unsigned char buffer[NB_HDRSIZE]; |
| 310 | int r; |
| 311 | |
| 312 | /* CHECK: is it always OK to do this in two writes? */ |
| 313 | /* perhaps use scatter gather sendmsg instead? */ |
| 314 | |
| 315 | buffer[0] = NB_SESSION_MSG; |
| 316 | buffer[1] = NB_FIRST; |
| 317 | NBR_ADDWORD(&buffer[2],size); |
| 318 | |
| 319 | r = write(fd, buffer, NB_HDRSIZE); |
| 320 | if(r!=NB_HDRSIZE) |
| 321 | return FALSE; |
| 322 | |
| 323 | r = write(fd, data, size); |
| 324 | if(r!=size) |
| 325 | { |
| 326 | ERR("write failed\n"); |
| 327 | return FALSE; |
| 328 | } |
| 329 | |
| 330 | return TRUE; |
| 331 | } |
| 332 | |
| 333 | static BOOL NB_RecvData(int fd, unsigned char *data, int *outlen) |
| 334 | { |
| 335 | int r,len; |
| 336 | unsigned char buffer[NB_HDRSIZE]; |
| 337 | |
| 338 | r = read(fd, buffer, NB_HDRSIZE); |
| 339 | if((r!=NB_HDRSIZE) || (buffer[0]!=NB_SESSION_MSG)) |
| 340 | { |
| 341 | ERR("Received %d bytes\n",r); |
| 342 | return FALSE; |
| 343 | } |
| 344 | |
| 345 | len = NBR_GETWORD(&buffer[2]); |
| 346 | r = read(fd, data, len); |
| 347 | if(len!=r) |
| 348 | { |
| 349 | ERR("Received %d bytes\n",r); |
| 350 | return FALSE; |
| 351 | } |
| 352 | *outlen = len; |
| 353 | |
| 354 | return TRUE; |
| 355 | } |
| 356 | |
| 357 | static BOOL NB_Transaction(int fd, unsigned char *buffer, int len, int *outlen) |
| 358 | { |
| 359 | int r,i; |
| 360 | struct pollfd fds; |
| 361 | |
| 362 | DPRINTF("Sending request:\n"); |
| 363 | for(i=0; i<len; i++) |
| 364 | DPRINTF("%02X%c",buffer[i],(((i+1)!=len)&&((i+1)%16))?' ':'\n'); |
| 365 | |
| 366 | if(!NB_SendData(fd,buffer,len)) |
| 367 | return FALSE; |
| 368 | |
| 369 | fds.fd = fd; |
| 370 | fds.events = POLLIN; |
| 371 | fds.revents = 0; |
| 372 | |
| 373 | r = poll(&fds,1,NB_TIMEOUT); |
| 374 | if(r!=1) |
| 375 | { |
| 376 | ERR("Poll failed\n"); |
| 377 | return FALSE; |
| 378 | } |
| 379 | |
| 380 | if(!NB_RecvData(fd, buffer, outlen)) |
| 381 | return FALSE; |
| 382 | |
| 383 | len = *outlen; |
| 384 | DPRINTF("Got response:\n"); |
| 385 | for(i=0; i<len; i++) |
| 386 | DPRINTF("%02X%c",buffer[i],(((i+1)!=len)&&((i+1)%16))?' ':'\n'); |
| 387 | |
| 388 | return TRUE; |
| 389 | } |
| 390 | |
| 391 | #define SMB_ADDHEADER(b,l) { b[(l)++]=0xff; b[(l)++]='S'; b[(l)++]='M'; b[(l)++]='B'; } |
| 392 | #define SMB_ADDERRINFO(b,l) { b[(l)++]=0; b[(l)++]=0; b[(l)++]=0; b[(l)++]=0; } |
| 393 | #define SMB_ADDPADSIG(b,l) { memset(&b[l],0,12); l+=12; } |
| 394 | |
| 395 | #define SMB_ERRCLASS 5 |
| 396 | #define SMB_ERRCODE 7 |
| 397 | #define SMB_TREEID 24 |
| 398 | #define SMB_PROCID 26 |
| 399 | #define SMB_USERID 28 |
| 400 | #define SMB_PLEXID 30 |
| 401 | #define SMB_PCOUNT 32 |
| 402 | #define SMB_HDRSIZE 33 |
| 403 | |
| 404 | static DWORD SMB_GetError(unsigned char *buffer) |
| 405 | { |
| 406 | if(buffer[SMB_ERRCLASS]==0) |
| 407 | return STATUS_SUCCESS; |
| 408 | /* FIXME: return propper error codes */ |
| 409 | return STATUS_INVALID_PARAMETER; |
| 410 | } |
| 411 | |
| 412 | static int SMB_Header(unsigned char *buffer, unsigned char command, USHORT tree_id, USHORT user_id) |
| 413 | { |
| 414 | int len = 0; |
| 415 | DWORD id; |
| 416 | |
| 417 | /* 0 */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 418 | SMB_ADDHEADER(buffer,len); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 419 | |
| 420 | /* 4 */ |
| 421 | buffer[len++] = command; |
| 422 | |
| 423 | /* 5 */ |
| 424 | SMB_ADDERRINFO(buffer,len) |
| 425 | |
| 426 | /* 9 */ |
| 427 | buffer[len++] = 0x00; /* flags */ |
| 428 | SMB_ADDWORD(&buffer[len],1); len += 2; /* flags2 */ |
| 429 | |
| 430 | /* 12 */ |
| 431 | SMB_ADDPADSIG(buffer,len) |
| 432 | |
| 433 | /* 24 */ |
| 434 | SMB_ADDWORD(&buffer[len],tree_id); len += 2; /* treeid */ |
| 435 | id = GetCurrentThreadId(); |
| 436 | SMB_ADDWORD(&buffer[len],id); len += 2; /* process id */ |
| 437 | SMB_ADDWORD(&buffer[len],user_id); len += 2; /* user id */ |
| 438 | SMB_ADDWORD(&buffer[len],SMB_MultiplexId); len += 2; /* multiplex id */ |
| 439 | SMB_MultiplexId++; |
| 440 | |
| 441 | return len; |
| 442 | } |
| 443 | |
| 444 | static const char *SMB_ProtocolDialect = "NT LM 0.12"; |
| 445 | /* = "Windows for Workgroups 3.1a"; */ |
| 446 | |
| 447 | /* FIXME: support multiple SMB dialects */ |
| 448 | static BOOL SMB_NegotiateProtocol(int fd, USHORT *dialect) |
| 449 | { |
| 450 | unsigned char buffer[0x100]; |
| 451 | int buflen,len = 0; |
| 452 | |
| 453 | ERR("\n"); |
| 454 | |
| 455 | memset(buffer,0,sizeof buffer); |
| 456 | |
| 457 | len = SMB_Header(buffer, SMB_COM_NEGOTIATE, 0, 0); |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 458 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 459 | /* parameters */ |
| 460 | buffer[len++] = 0; /* no parameters */ |
| 461 | |
| 462 | /* command buffer */ |
| 463 | buflen = strlen(SMB_ProtocolDialect)+2; /* include type and nul byte */ |
| 464 | SMB_ADDWORD(&buffer[len],buflen); len += 2; |
| 465 | |
| 466 | buffer[len] = 0x02; |
| 467 | strcpy(&buffer[len+1],SMB_ProtocolDialect); |
| 468 | len += buflen; |
| 469 | |
| 470 | if(!NB_Transaction(fd, buffer, len, &len)) |
| 471 | { |
| 472 | ERR("Failed\n"); |
| 473 | return FALSE; |
| 474 | } |
| 475 | |
| 476 | /* FIXME: check response */ |
| 477 | if(SMB_GetError(buffer)) |
| 478 | { |
| 479 | ERR("returned error\n"); |
| 480 | return FALSE; |
| 481 | } |
| 482 | |
| 483 | *dialect = 0; |
| 484 | |
| 485 | return TRUE; |
| 486 | } |
| 487 | |
| 488 | #define SMB_PARAM_COUNT(buffer) ((buffer)[SMB_PCOUNT]) |
| 489 | #define SMB_PARAM(buffer,n) SMB_GETWORD(&(buffer)[SMB_HDRSIZE+2*(n)]) |
| 490 | #define SMB_BUFFER_COUNT(buffer) SMB_GETWORD(buffer+SMB_HDRSIZE+2*SMB_PARAM_COUNT(buffer)) |
| 491 | #define SMB_BUFFER(buffer,n) ((buffer)[SMB_HDRSIZE + 2*SMB_PARAM_COUNT(buffer) + 2 + (n) ]) |
| 492 | |
| 493 | static BOOL SMB_SessionSetup(int fd, USHORT *userid) |
| 494 | { |
| 495 | unsigned char buffer[0x100]; |
| 496 | int len = 0; |
| 497 | int i,pcount,bcount; |
| 498 | |
| 499 | memset(buffer,0,sizeof buffer); |
| 500 | |
| 501 | len = SMB_Header(buffer, SMB_COM_SESSION_SETUP_ANDX, 0, 0); |
| 502 | |
| 503 | buffer[len++] = 0; /* no parameters? */ |
| 504 | |
| 505 | buffer[len++] = 0xff; /* AndXCommand: secondary request */ |
| 506 | buffer[len++] = 0x00; /* AndXReserved */ |
| 507 | SMB_ADDWORD(&buffer[len],0); len += 2; /* AndXOffset */ |
| 508 | SMB_ADDWORD(&buffer[len],0x400); len += 2; /* MaxBufferSize */ |
| 509 | SMB_ADDWORD(&buffer[len],1); len += 2; /* MaxMpxCount */ |
| 510 | SMB_ADDWORD(&buffer[len],0); len += 2; /* VcNumber */ |
| 511 | SMB_ADDWORD(&buffer[len],0); len += 2; /* SessionKey */ |
| 512 | SMB_ADDWORD(&buffer[len],0); len += 2; /* SessionKey */ |
| 513 | SMB_ADDWORD(&buffer[len],0); len += 2; /* Password length */ |
| 514 | SMB_ADDWORD(&buffer[len],0); len += 2; /* Reserved */ |
| 515 | SMB_ADDWORD(&buffer[len],0); len += 2; /* Reserved */ |
| 516 | |
| 517 | /* FIXME: add name and password here */ |
| 518 | buffer[len++] = 0; /* number of bytes in password */ |
| 519 | |
| 520 | if(!NB_Transaction(fd, buffer, len, &len)) |
| 521 | return FALSE; |
| 522 | |
| 523 | if(SMB_GetError(buffer)) |
| 524 | return FALSE; |
| 525 | |
| 526 | pcount = SMB_PARAM_COUNT(buffer); |
| 527 | |
| 528 | if( (SMB_HDRSIZE+pcount*2) > len ) |
| 529 | { |
| 530 | ERR("Bad parameter count %d\n",pcount); |
| 531 | return FALSE; |
| 532 | } |
| 533 | |
| 534 | DPRINTF("SMB_COM_SESSION_SETUP response, %d args: ",pcount); |
| 535 | for(i=0; i<pcount; i++) |
| 536 | DPRINTF("%04x ",SMB_PARAM(buffer,i)); |
| 537 | DPRINTF("\n"); |
| 538 | |
| 539 | bcount = SMB_BUFFER_COUNT(buffer); |
| 540 | if( (SMB_HDRSIZE+pcount*2+2+bcount) > len ) |
| 541 | { |
| 542 | ERR("parameter count %x, buffer count %x, len %x\n",pcount,bcount,len); |
| 543 | return FALSE; |
| 544 | } |
| 545 | |
| 546 | DPRINTF("response buffer %d bytes: ",bcount); |
| 547 | for(i=0; i<bcount; i++) |
| 548 | { |
| 549 | unsigned char ch = SMB_BUFFER(buffer,i); |
| 550 | DPRINTF("%c", isprint(ch)?ch:' '); |
| 551 | } |
| 552 | DPRINTF("\n"); |
| 553 | |
| 554 | *userid = SMB_GETWORD(&buffer[SMB_USERID]); |
| 555 | |
| 556 | return TRUE; |
| 557 | } |
| 558 | |
| 559 | static BOOL SMB_TreeConnect(int fd, USHORT user_id, LPCSTR share_name, USHORT *treeid) |
| 560 | { |
| 561 | unsigned char buffer[0x100]; |
| 562 | int len = 0,slen; |
| 563 | |
| 564 | ERR("%s\n",share_name); |
| 565 | |
| 566 | memset(buffer,0,sizeof buffer); |
| 567 | |
| 568 | len = SMB_Header(buffer, SMB_COM_TREE_CONNECT, 0, user_id); |
| 569 | |
| 570 | buffer[len++] = 4; /* parameters */ |
| 571 | |
| 572 | buffer[len++] = 0xff; /* AndXCommand: secondary request */ |
| 573 | buffer[len++] = 0x00; /* AndXReserved */ |
| 574 | SMB_ADDWORD(&buffer[len],0); len += 2; /* AndXOffset */ |
| 575 | SMB_ADDWORD(&buffer[len],0); len += 2; /* Flags */ |
| 576 | SMB_ADDWORD(&buffer[len],1); len += 2; /* Password length */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 577 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 578 | /* SMB command buffer */ |
| 579 | SMB_ADDWORD(&buffer[len],3); len += 2; /* command buffer len */ |
| 580 | buffer[len++] = 0; /* null terminated password */ |
| 581 | |
| 582 | slen = strlen(share_name); |
| 583 | if(slen<(sizeof buffer-len)) |
| 584 | strcpy(&buffer[len], share_name); |
| 585 | else |
| 586 | return FALSE; |
| 587 | len += slen+1; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 588 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 589 | /* name of the service */ |
| 590 | buffer[len++] = 0; |
| 591 | |
| 592 | if(!NB_Transaction(fd, buffer, len, &len)) |
| 593 | return FALSE; |
| 594 | |
| 595 | if(SMB_GetError(buffer)) |
| 596 | return FALSE; |
| 597 | |
| 598 | *treeid = SMB_GETWORD(&buffer[SMB_TREEID]); |
| 599 | |
| 600 | ERR("OK, treeid = %04x\n", *treeid); |
| 601 | |
| 602 | return TRUE; |
| 603 | } |
| 604 | |
| 605 | static BOOL SMB_NtCreateOpen(int fd, USHORT tree_id, USHORT user_id, USHORT dialect, |
| 606 | LPCSTR filename, DWORD access, DWORD sharing, |
| 607 | LPSECURITY_ATTRIBUTES sa, DWORD creation, |
| 608 | DWORD attributes, HANDLE template, USHORT *file_id ) |
| 609 | { |
| 610 | unsigned char buffer[0x100]; |
| 611 | int len = 0,slen; |
| 612 | |
| 613 | ERR("%s\n",filename); |
| 614 | |
| 615 | memset(buffer,0,sizeof buffer); |
| 616 | |
| 617 | len = SMB_Header(buffer, SMB_COM_NT_CREATE_ANDX, tree_id, user_id); |
| 618 | |
| 619 | /* 0 */ |
| 620 | buffer[len++] = 24; /* parameters */ |
| 621 | |
| 622 | buffer[len++] = 0xff; /* AndXCommand: secondary request */ |
| 623 | buffer[len++] = 0x00; /* AndXReserved */ |
| 624 | SMB_ADDWORD(&buffer[len],0); len += 2; /* AndXOffset */ |
| 625 | |
| 626 | buffer[len++] = 0; /* reserved */ |
| 627 | slen = strlen(filename); |
| 628 | SMB_ADDWORD(&buffer[len],slen); len += 2; /* name length */ |
| 629 | |
| 630 | /* 0x08 */ |
| 631 | SMB_ADDDWORD(&buffer[len],0); len += 4; /* flags */ |
| 632 | SMB_ADDDWORD(&buffer[len],0); len += 4; /* root directory fid */ |
| 633 | /* 0x10 */ |
| 634 | SMB_ADDDWORD(&buffer[len],access); len += 4; /* access */ |
| 635 | SMB_ADDDWORD(&buffer[len],0); len += 4; /* allocation size */ |
| 636 | /* 0x18 */ |
| 637 | SMB_ADDDWORD(&buffer[len],0); len += 4; /* root directory fid */ |
| 638 | |
| 639 | /* 0x1c */ |
| 640 | SMB_ADDDWORD(&buffer[len],0); len += 4; /* initial allocation */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 641 | SMB_ADDDWORD(&buffer[len],0); len += 4; |
| 642 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 643 | /* 0x24 */ |
| 644 | SMB_ADDDWORD(&buffer[len],attributes); len += 4; /* ExtFileAttributes*/ |
| 645 | |
| 646 | /* 0x28 */ |
| 647 | SMB_ADDDWORD(&buffer[len],sharing); len += 4; /* ShareAccess */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 648 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 649 | /* 0x2c */ |
| 650 | ERR("creation = %08lx\n",creation); |
| 651 | SMB_ADDDWORD(&buffer[len],creation); len += 4; /* CreateDisposition */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 652 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 653 | /* 0x30 */ |
| 654 | SMB_ADDDWORD(&buffer[len],creation); len += 4; /* CreateOptions */ |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 655 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 656 | /* 0x34 */ |
| 657 | SMB_ADDDWORD(&buffer[len],0); len += 4; /* Impersonation */ |
| 658 | |
| 659 | /* 0x38 */ |
| 660 | buffer[len++] = 0; /* security flags */ |
| 661 | |
| 662 | /* 0x39 */ |
| 663 | SMB_ADDWORD(&buffer[len],slen); len += 2; /* size of buffer */ |
| 664 | |
| 665 | if(slen<(sizeof buffer-len)) |
| 666 | strcpy(&buffer[len], filename); |
| 667 | else |
| 668 | return FALSE; |
| 669 | len += slen+1; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 670 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 671 | /* name of the file */ |
| 672 | buffer[len++] = 0; |
| 673 | |
| 674 | if(!NB_Transaction(fd, buffer, len, &len)) |
| 675 | return FALSE; |
| 676 | |
| 677 | if(SMB_GetError(buffer)) |
| 678 | return FALSE; |
| 679 | |
| 680 | ERR("OK\n"); |
| 681 | |
| 682 | /* FIXME */ |
| 683 | /* *file_id = SMB_GETWORD(&buffer[xxx]); */ |
| 684 | *file_id = 0; |
| 685 | return FALSE; |
| 686 | |
| 687 | return TRUE; |
| 688 | } |
| 689 | |
| 690 | static USHORT SMB_GetMode(DWORD access, DWORD sharing) |
| 691 | { |
| 692 | USHORT mode=0; |
| 693 | |
| 694 | switch(access&(GENERIC_READ|GENERIC_WRITE)) |
| 695 | { |
| 696 | case GENERIC_READ: |
| 697 | mode |= OF_READ; |
| 698 | break; |
| 699 | case GENERIC_WRITE: |
| 700 | mode |= OF_WRITE; |
| 701 | break; |
| 702 | case (GENERIC_READ|GENERIC_WRITE): |
| 703 | mode |= OF_READWRITE; |
| 704 | break; |
| 705 | } |
| 706 | |
| 707 | switch(sharing&(FILE_SHARE_READ|FILE_SHARE_WRITE)) |
| 708 | { |
| 709 | case (FILE_SHARE_READ|FILE_SHARE_WRITE): |
| 710 | mode |= OF_SHARE_DENY_NONE; |
| 711 | break; |
| 712 | case FILE_SHARE_READ: |
| 713 | mode |= OF_SHARE_DENY_WRITE; |
| 714 | break; |
| 715 | case FILE_SHARE_WRITE: |
| 716 | mode |= OF_SHARE_DENY_READ; |
| 717 | break; |
| 718 | default: |
| 719 | mode |= OF_SHARE_EXCLUSIVE; |
| 720 | break; |
| 721 | } |
| 722 | |
| 723 | return mode; |
| 724 | } |
| 725 | |
| 726 | /* inverse of FILE_ConvertOFMode */ |
| 727 | static BOOL SMB_OpenAndX(int fd, USHORT tree_id, USHORT user_id, USHORT dialect, |
| 728 | LPCSTR filename, DWORD access, DWORD sharing, |
| 729 | DWORD creation, DWORD attributes, USHORT *file_id ) |
| 730 | { |
| 731 | unsigned char buffer[0x100]; |
| 732 | int len = 0; |
| 733 | USHORT mode; |
| 734 | |
| 735 | ERR("%s\n",filename); |
| 736 | |
| 737 | mode = SMB_GetMode(access,sharing); |
| 738 | |
| 739 | memset(buffer,0,sizeof buffer); |
| 740 | |
| 741 | len = SMB_Header(buffer, SMB_COM_OPEN_ANDX, tree_id, user_id); |
| 742 | |
| 743 | /* 0 */ |
| 744 | buffer[len++] = 15; /* parameters */ |
| 745 | buffer[len++] = 0xff; /* AndXCommand: secondary request */ |
| 746 | buffer[len++] = 0x00; /* AndXReserved */ |
| 747 | SMB_ADDWORD(buffer+len,0); len+=2; /* AndXOffset */ |
| 748 | SMB_ADDWORD(buffer+len,0); len+=2; /* Flags */ |
| 749 | SMB_ADDWORD(buffer+len,mode); len+=2; /* desired access */ |
| 750 | SMB_ADDWORD(buffer+len,0); len+=2; /* search attributes */ |
| 751 | SMB_ADDWORD(buffer+len,0); len+=2; |
| 752 | |
| 753 | /*FIXME: complete */ |
| 754 | return FALSE; |
| 755 | } |
| 756 | |
| 757 | static BOOL SMB_Open(int fd, USHORT tree_id, USHORT user_id, USHORT dialect, |
| 758 | LPCSTR filename, DWORD access, DWORD sharing, |
| 759 | DWORD creation, DWORD attributes, USHORT *file_id ) |
| 760 | { |
| 761 | unsigned char buffer[0x100]; |
| 762 | int len = 0,slen,pcount,i; |
| 763 | USHORT mode = SMB_GetMode(access,sharing); |
| 764 | |
| 765 | ERR("%s\n",filename); |
| 766 | |
| 767 | memset(buffer,0,sizeof buffer); |
| 768 | |
| 769 | len = SMB_Header(buffer, SMB_COM_OPEN, tree_id, user_id); |
| 770 | |
| 771 | /* 0 */ |
| 772 | buffer[len++] = 2; /* parameters */ |
| 773 | SMB_ADDWORD(buffer+len,mode); len+=2; |
| 774 | SMB_ADDWORD(buffer+len,0); len+=2; /* search attributes */ |
| 775 | |
| 776 | slen = strlen(filename)+2; /* inc. nul and BufferFormat */ |
| 777 | SMB_ADDWORD(buffer+len,slen); len+=2; |
| 778 | |
| 779 | buffer[len] = 0x04; /* BufferFormat */ |
| 780 | strcpy(&buffer[len+1],filename); |
| 781 | len += slen; |
| 782 | |
| 783 | if(!NB_Transaction(fd, buffer, len, &len)) |
| 784 | return FALSE; |
| 785 | |
| 786 | if(SMB_GetError(buffer)) |
| 787 | return FALSE; |
| 788 | |
| 789 | pcount = SMB_PARAM_COUNT(buffer); |
| 790 | |
| 791 | if( (SMB_HDRSIZE+pcount*2) > len ) |
| 792 | { |
| 793 | ERR("Bad parameter count %d\n",pcount); |
| 794 | return FALSE; |
| 795 | } |
| 796 | |
| 797 | ERR("response, %d args: ",pcount); |
| 798 | for(i=0; i<pcount; i++) |
| 799 | DPRINTF("%04x ",SMB_PARAM(buffer,i)); |
| 800 | DPRINTF("\n"); |
| 801 | |
| 802 | *file_id = SMB_PARAM(buffer,0); |
| 803 | |
| 804 | ERR("file_id = %04x\n",*file_id); |
| 805 | |
| 806 | return TRUE; |
| 807 | } |
| 808 | |
| 809 | static BOOL SMB_Read(int fd, USHORT tree_id, USHORT user_id, USHORT dialect, USHORT file_id, DWORD offset, LPVOID out, USHORT count, LPUSHORT read) |
| 810 | { |
| 811 | unsigned char *buffer; |
| 812 | int len,buf_size,n,i; |
| 813 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 814 | ERR("user %04x tree %04x file %04x count %04x offset %08lx\n", |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 815 | user_id, tree_id, file_id, count, offset); |
| 816 | |
| 817 | buf_size = count+0x100; |
| 818 | buffer = (unsigned char *) HeapAlloc(GetProcessHeap(),0,buf_size); |
| 819 | |
| 820 | memset(buffer,0,buf_size); |
| 821 | |
| 822 | len = SMB_Header(buffer, SMB_COM_READ, tree_id, user_id); |
| 823 | |
| 824 | buffer[len++] = 5; |
| 825 | SMB_ADDWORD(&buffer[len],file_id); len += 2; |
| 826 | SMB_ADDWORD(&buffer[len],count); len += 2; |
| 827 | SMB_ADDDWORD(&buffer[len],offset); len += 4; |
| 828 | SMB_ADDWORD(&buffer[len],0); len += 2; /* how many more bytes will be read */ |
| 829 | |
| 830 | buffer[len++] = 0; |
| 831 | |
| 832 | if(!NB_Transaction(fd, buffer, len, &len)) |
| 833 | { |
| 834 | HeapFree(GetProcessHeap(),0,buffer); |
| 835 | return FALSE; |
| 836 | } |
| 837 | |
| 838 | if(SMB_GetError(buffer)) |
| 839 | { |
| 840 | HeapFree(GetProcessHeap(),0,buffer); |
| 841 | return FALSE; |
| 842 | } |
| 843 | |
| 844 | n = SMB_PARAM_COUNT(buffer); |
| 845 | |
| 846 | if( (SMB_HDRSIZE+n*2) > len ) |
| 847 | { |
| 848 | HeapFree(GetProcessHeap(),0,buffer); |
| 849 | ERR("Bad parameter count %d\n",n); |
| 850 | return FALSE; |
| 851 | } |
| 852 | |
| 853 | ERR("response, %d args: ",n); |
| 854 | for(i=0; i<n; i++) |
| 855 | DPRINTF("%04x ",SMB_PARAM(buffer,i)); |
| 856 | DPRINTF("\n"); |
| 857 | |
| 858 | n = SMB_PARAM(buffer,5) - 3; |
| 859 | if(n>count) |
| 860 | n=count; |
| 861 | |
| 862 | memcpy( out, &SMB_BUFFER(buffer,3), n); |
| 863 | |
| 864 | ERR("Read %d bytes\n",n); |
| 865 | *read = n; |
| 866 | |
| 867 | HeapFree(GetProcessHeap(),0,buffer); |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 868 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 869 | return TRUE; |
| 870 | } |
| 871 | |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 872 | static int SMB_GetSocket(LPCSTR host) |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 873 | { |
| 874 | int fd=-1,r; |
| 875 | struct sockaddr_in sin; |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 876 | struct hostent *he; |
| 877 | |
| 878 | ERR("host %s\n",host); |
| 879 | |
| 880 | if(NB_Lookup(host,&sin)) |
| 881 | goto connect; |
| 882 | |
| 883 | he = gethostbyname(host); |
| 884 | if(he) |
| 885 | { |
| 886 | memcpy(&sin.sin_addr,he->h_addr, sizeof (sin.sin_addr)); |
| 887 | goto connect; |
| 888 | } |
| 889 | |
| 890 | /* FIXME: resolve by WINS too */ |
| 891 | |
| 892 | ERR("couldn't resolve SMB host %s\n", host); |
| 893 | |
| 894 | return -1; |
| 895 | |
| 896 | connect: |
| 897 | sin.sin_family = AF_INET; |
| 898 | sin.sin_port = htons(139); /* netbios session */ |
| 899 | |
| 900 | fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 901 | if(fd<0) |
| 902 | return fd; |
| 903 | |
| 904 | { |
| 905 | unsigned char *x = (unsigned char *)&sin.sin_addr; |
| 906 | ERR("Connecting to %d.%d.%d.%d ...\n", x[0],x[1],x[2],x[3]); |
| 907 | } |
| 908 | r = connect(fd, &sin, sizeof sin); |
| 909 | |
| 910 | if(!NB_SessionReq(fd, "*SMBSERVER", "WINE")) |
| 911 | { |
| 912 | close(fd); |
| 913 | return -1; |
| 914 | } |
| 915 | |
| 916 | return fd; |
| 917 | } |
| 918 | |
| 919 | static BOOL SMB_LoginAndConnect(int fd, LPCSTR host, LPCSTR share, USHORT *tree_id, USHORT *user_id, USHORT *dialect) |
| 920 | { |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 921 | LPSTR name=NULL; |
| 922 | |
| 923 | ERR("host %s share %s\n",host,share); |
| 924 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 925 | if(!SMB_NegotiateProtocol(fd, dialect)) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 926 | return FALSE; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 927 | |
| 928 | if(!SMB_SessionSetup(fd, user_id)) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 929 | return FALSE; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 930 | |
| 931 | name = HeapAlloc(GetProcessHeap(),0,strlen(host)+strlen(share)+5); |
| 932 | if(!name) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 933 | return FALSE; |
| 934 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 935 | sprintf(name,"\\\\%s\\%s",host,share); |
| 936 | if(!SMB_TreeConnect(fd,*user_id,name,tree_id)) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 937 | { |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 938 | HeapFree(GetProcessHeap(),0,name); |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 939 | return FALSE; |
| 940 | } |
| 941 | |
| 942 | return TRUE; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | static HANDLE SMB_RegisterFile( int fd, USHORT tree_id, USHORT user_id, USHORT dialect, USHORT file_id) |
| 946 | { |
| 947 | int r; |
| 948 | HANDLE ret; |
| 949 | |
| 950 | wine_server_send_fd( fd ); |
| 951 | |
| 952 | SERVER_START_REQ( create_smb ) |
| 953 | { |
| 954 | req->tree_id = tree_id; |
| 955 | req->user_id = user_id; |
| 956 | req->file_id = file_id; |
| 957 | req->dialect = 0; |
| 958 | req->fd = fd; |
| 959 | SetLastError(0); |
| 960 | r = wine_server_call_err( req ); |
| 961 | ret = reply->handle; |
| 962 | } |
| 963 | SERVER_END_REQ; |
| 964 | |
| 965 | if(!r) |
| 966 | ERR("created wineserver smb object, handle = %04x\n",ret); |
| 967 | else |
| 968 | SetLastError( ERROR_PATH_NOT_FOUND ); |
| 969 | |
| 970 | return ret; |
| 971 | } |
| 972 | |
| 973 | HANDLE WINAPI SMB_CreateFileA( LPCSTR uncname, DWORD access, DWORD sharing, |
| 974 | LPSECURITY_ATTRIBUTES sa, DWORD creation, |
| 975 | DWORD attributes, HANDLE template ) |
| 976 | { |
| 977 | int fd; |
| 978 | USHORT tree_id=0, user_id=0, dialect=0, file_id=0; |
| 979 | LPSTR name,host,share,file; |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 980 | HANDLE handle = INVALID_HANDLE_VALUE; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 981 | |
| 982 | name = HeapAlloc(GetProcessHeap(),0,lstrlenA(uncname)); |
| 983 | if(!name) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 984 | return handle; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 985 | |
| 986 | lstrcpyA(name,uncname); |
| 987 | |
| 988 | if( !UNC_SplitName(name, &host, &share, &file) ) |
| 989 | { |
| 990 | HeapFree(GetProcessHeap(),0,name); |
| 991 | return handle; |
| 992 | } |
| 993 | |
| 994 | ERR("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] | 995 | |
| 996 | fd = SMB_GetSocket(host); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 997 | if(fd < 0) |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 998 | goto done; |
| 999 | |
| 1000 | if(!SMB_LoginAndConnect(fd, host, share, &tree_id, &user_id, &dialect)) |
| 1001 | goto done; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1002 | |
| 1003 | #if 0 |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1004 | if(!SMB_NtCreateOpen(fd, tree_id, user_id, dialect, file, |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1005 | access, sharing, sa, creation, attributes, template, &file_id )) |
| 1006 | { |
| 1007 | close(fd); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1008 | ERR("CreateOpen failed\n"); |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1009 | goto done; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1010 | } |
| 1011 | #endif |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1012 | if(!SMB_Open(fd, tree_id, user_id, dialect, file, |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1013 | access, sharing, creation, attributes, &file_id )) |
| 1014 | { |
| 1015 | close(fd); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1016 | ERR("CreateOpen failed\n"); |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1017 | goto done; |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1020 | handle = SMB_RegisterFile(fd, tree_id, user_id, dialect, file_id); |
| 1021 | if(!handle) |
| 1022 | { |
| 1023 | ERR("register failed\n"); |
| 1024 | close(fd); |
| 1025 | } |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1026 | |
Mike McCormack | 9414adf | 2002-05-05 20:29:15 +0000 | [diff] [blame] | 1027 | done: |
| 1028 | HeapFree(GetProcessHeap(),0,name); |
Mike McCormack | fc93261 | 2002-03-12 19:24:04 +0000 | [diff] [blame] | 1029 | return handle; |
| 1030 | } |
| 1031 | |
| 1032 | static BOOL SMB_GetSmbInfo(HANDLE hFile, USHORT *tree_id, USHORT *user_id, USHORT *dialect, USHORT *file_id, LPDWORD offset) |
| 1033 | { |
| 1034 | int r; |
| 1035 | |
| 1036 | SERVER_START_REQ( get_smb_info ) |
| 1037 | { |
| 1038 | req->handle = hFile; |
| 1039 | req->flags = 0; |
| 1040 | SetLastError(0); |
| 1041 | r = wine_server_call_err( req ); |
| 1042 | if(tree_id) |
| 1043 | *tree_id = reply->tree_id; |
| 1044 | if(user_id) |
| 1045 | *user_id = reply->user_id; |
| 1046 | if(file_id) |
| 1047 | *file_id = reply->file_id; |
| 1048 | if(dialect) |
| 1049 | *dialect = reply->dialect; |
| 1050 | if(offset) |
| 1051 | *offset = reply->offset; |
| 1052 | } |
| 1053 | SERVER_END_REQ; |
| 1054 | |
| 1055 | return !r; |
| 1056 | } |
| 1057 | |
| 1058 | static BOOL SMB_SetOffset(HANDLE hFile, DWORD offset) |
| 1059 | { |
| 1060 | int r; |
| 1061 | |
| 1062 | ERR("offset = %08lx\n",offset); |
| 1063 | |
| 1064 | SERVER_START_REQ( get_smb_info ) |
| 1065 | { |
| 1066 | req->handle = hFile; |
| 1067 | req->flags = SMBINFO_SET_OFFSET; |
| 1068 | req->offset = offset; |
| 1069 | SetLastError(0); |
| 1070 | r = wine_server_call_err( req ); |
| 1071 | /* if(offset) |
| 1072 | *offset = reply->offset; */ |
| 1073 | } |
| 1074 | SERVER_END_REQ; |
| 1075 | |
| 1076 | return !r; |
| 1077 | } |
| 1078 | |
Patrik Stridvall | f89d4a8 | 2002-03-23 21:39:05 +0000 | [diff] [blame] | 1079 | 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] | 1080 | { |
| 1081 | int fd; |
| 1082 | DWORD total, count, offset; |
| 1083 | USHORT user_id, tree_id, dialect, file_id, read; |
| 1084 | BOOL r=TRUE; |
| 1085 | |
| 1086 | ERR("%04x %p %ld %p\n", hFile, buffer, bytesToRead, bytesRead); |
| 1087 | |
| 1088 | if(!SMB_GetSmbInfo(hFile, &tree_id, &user_id, &dialect, &file_id, &offset)) |
| 1089 | return FALSE; |
| 1090 | |
| 1091 | fd = FILE_GetUnixHandle(hFile, GENERIC_READ); |
| 1092 | if(fd<0) |
| 1093 | return FALSE; |
| 1094 | |
| 1095 | total = 0; |
| 1096 | while(1) |
| 1097 | { |
| 1098 | count = bytesToRead - total; |
| 1099 | if(count>0x400) |
| 1100 | count = 0x400; |
| 1101 | if(count==0) |
| 1102 | break; |
| 1103 | read = 0; |
| 1104 | r = SMB_Read(fd, tree_id, user_id, dialect, file_id, offset, buffer, count, &read); |
| 1105 | if(!r) |
| 1106 | break; |
| 1107 | if(!read) |
| 1108 | break; |
| 1109 | total += read; |
| 1110 | buffer += read; |
| 1111 | offset += read; |
| 1112 | if(total>=bytesToRead) |
| 1113 | break; |
| 1114 | } |
| 1115 | close(fd); |
| 1116 | |
| 1117 | if(bytesRead) |
| 1118 | *bytesRead = total; |
| 1119 | |
| 1120 | if(!SMB_SetOffset(hFile, offset)) |
| 1121 | return FALSE; |
| 1122 | |
| 1123 | return r; |
| 1124 | } |
| 1125 | |