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